Spaces:
Running
Running
File size: 8,770 Bytes
b2721ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | // Pure, clock-injected companion model. No rendering, storage or robot dependency.
export const VERSION = 2;
export const clamp = (n, lo = 0, hi = 100) => Math.max(lo, Math.min(hi, n));
export const ACTIVITY = { idle: 'Watching the world', explore: 'Exploring', play: 'Playing together', rest: 'Resting', attention: 'Looking for company', eat: 'Enjoying a meal', cuddle: 'Enjoying a cuddle', drink: 'Drinking water', treat: 'Enjoying a treat' };
export function createPet(now = Date.now(), random = Math.random) {
return { version: VERSION, name: 'Nugget', bornAt: now, savedAt: now, clock: 0,
needs: { satiety: 22, energy: 16, stimulation: 12, affection: 14, hydration: 18 },
traits: { playful: 0.25 + random() * 0.5, curious: 0.25 + random() * 0.5, social: 0.25 + random() * 0.5 },
preferences: { play: 0.5, cuddle: 0.5, feed: 0.5 }, bond: 4, interactions: 0,
activity: 'idle', remaining: 5, actionId: 0, emotion: 'curious', emotionSeq: 0, task: null, playStyle: 'auto', reason: 'Getting to know my new home.',
lastAction: -100, lastReward: -100, journal: [] };
}
export function happiness(p) {
return Math.round((p.needs.satiety + p.needs.energy + p.needs.stimulation + p.needs.affection + p.needs.hydration) / 5);
}
function record(p, text) {
p.journal.unshift({ at: p.clock, text });
p.journal = p.journal.slice(0, 30);
}
function choose(p, activity, reason, duration = 16) {
p.activity = activity; p.reason = reason; p.remaining = duration;
p.actionId = (p.actionId || 0) + 1;
p.sleeping = false; p.autonomous = true;
p.task = ['eat', 'play', 'drink', 'treat'].includes(activity) ? activity : null;
p.emotion = { idle: 'mmh', explore: 'curious', play: 'excited', rest: 'sad', attention: 'impatient', eat: 'yes', cuddle: 'excited', drink: 'yes', treat: 'excited' }[activity];
p.emotionSeq = (p.emotionSeq || 0) + 1;
record(p, reason);
}
export function decide(p, random = Math.random) {
const choices = ['drink', 'ball', 'walk', 'rollers', 'roll'].filter(a => a !== p.lastSpontaneous);
const action = choices[Math.min(choices.length - 1, Math.floor(random() * choices.length))];
p.lastSpontaneous = action;
p.playStyle = action;
choose(p, action === 'drink' ? 'drink' : ['walk', 'rollers'].includes(action) ? 'explore' : 'play',
'Taking a little spontaneous break.', 16);
p.emotion = null;
}
export function express(p, emotion) {
p.emotion = emotion; p.emotionSeq = (p.emotionSeq || 0) + 1;
}
export function tick(p, seconds) {
if (!Number.isFinite(seconds) || seconds <= 0) return p;
// Small substeps make accelerated lessons and normal play obey identical rules.
let left = Math.min(seconds, 3600);
while (left > 0) {
const dt = Math.min(left, 1); left -= dt; p.clock += dt;
const n = p.needs, resting = p.activity === 'rest';
n.satiety = clamp(n.satiety - dt * 0.035);
n.hydration = clamp(n.hydration - dt * 0.04);
n.energy = clamp(n.energy + dt * (resting && p.sleeping ? 0.65 : resting ? 0 : p.activity === 'play' ? -0.15 : -0.045));
n.stimulation = clamp(n.stimulation + dt * (!p.autonomous && ['play', 'explore'].includes(p.activity) ? 0.23 : -0.055));
n.affection = clamp(n.affection - dt * 0.035);
if (!p.task && (!resting || p.sleeping)) p.remaining -= dt;
if (p.remaining <= 0 && p.activity !== 'idle') {
if(resting && n.energy<75)p.remaining=25;
else {choose(p, 'idle', 'Watching the world.', 20);p.emotion=null;}
}
}
return p;
}
export function interact(p, action) {
const style = ['ball', 'goal', 'roll', 'course'].includes(action) ? action : null;
if (style) action = 'play';
if (!['feed', 'play', 'cuddle', 'rest', 'treat', 'drink'].includes(action)) return false;
const n = p.needs;
// Refusal must be visible even when an activity or click cooldown is active.
if (action === 'play' && n.energy < 30) {
choose(p, 'rest', 'No! I am exhausted! Please let me sleep.', 30);
p.emotion='defiant';p.autonomous=false;p.lastAction=p.clock;return false;
}
if (action==='play' && n.satiety<45) {choose(p,'attention','I am too hungry to play. Food first?',8);p.emotion='no';p.autonomous=false;p.lastAction=p.clock;return false;}
if (p.clock - p.lastAction < 3) {p.reason='One moment, please.';return false;}
if (p.task && !p.autonomous && action !== 'rest') {p.reason='Please let me finish this activity first.';return false;}
p.lastAction=p.clock;
if (action === 'feed' && n.satiety > 90) { p.reason = 'My tummy is full already.'; p.emotion='no'; p.emotionSeq=(p.emotionSeq||0)+1; record(p, p.reason); return false; }
if (action === 'treat') choose(p,'treat','A little treat! Going to the snack plate.',12);
if (action === 'drink') choose(p, 'drink', 'Going for a drink.', 10);
if (action === 'feed') { choose(p, 'eat', 'Food is ready! Walking to my plate.', 10); }
if (action === 'play') { p.playStyle = style || 'auto'; choose(p, 'play', 'Let us play!', 20); if(style==='roll')p.emotion=null; }
if (action === 'cuddle') { n.affection = clamp(n.affection + 20); choose(p, 'cuddle', 'That feels lovely! I am so happy with you.', 12); }
if (action === 'rest') choose(p, 'rest', 'Good idea. I am going to my bed.', 30);
if (!p.task) {
if (action in p.preferences) p.preferences[action] = clamp(p.preferences[action] + 0.015, 0.1, 0.9);
p.interactions++; p.bond = clamp(p.bond + 0.7);
}
p.autonomous=false;
return true;
}
// Rewards follow completed physical actions, never the invitation button.
export function completeBite(p) {
if (p.autonomous) return false;
if (p.activity==='treat' && p.task==='treat') {p.needs.stimulation=clamp(p.needs.stimulation+7);p.needs.affection=clamp(p.needs.affection+6);return true;}
if (p.activity !== 'eat' || p.task !== 'eat') return false;
p.needs.satiety = clamp(p.needs.satiety + 8); return true;
}
export function completeTask(p, success) {
const task=p.task;if(!task) return;
if(success && !p.autonomous) {
const key=task==='eat'?'feed':task==='play'?'play':null;if(key)p.preferences[key]=clamp(p.preferences[key]+.015,.1,.9);
if(task==='drink')p.needs.hydration=clamp(p.needs.hydration+45);
p.bond=clamp(p.bond+.7);p.interactions++;
if(task==='play') {p.needs.stimulation=clamp(p.needs.stimulation+15);p.needs.energy=clamp(p.needs.energy-4);}
}
choose(p,'idle',success?(task==='eat'?'All done! Three tasty bites.':task==='drink'?'Fresh water! That feels better.':task==='treat'?'Delicious! Thank you for the treats.':'That was fun!'): 'Activity stopped. Taking a break.',8);
p.emotion=null;
}
export function restorePet(raw, now = Date.now()) {
try {
const s = JSON.parse(raw);
if (s.version !== VERSION) return null;
const p = createPet(now, () => 0.5);
for (const [group, lo, hi] of [['needs', 0, 100], ['traits', 0, 1], ['preferences', 0.1, 0.9]]) {
for (const key of Object.keys(p[group])) {
if (!Number.isFinite(s[group]?.[key])) return null;
p[group][key] = clamp(s[group][key], lo, hi);
}
}
for (const key of ['bornAt', 'savedAt', 'clock', 'bond', 'interactions']) if (!Number.isFinite(s[key]) || s[key] < 0) return null;
p.name = typeof s.name === 'string' && s.name.trim() ? s.name.trim().slice(0, 24) : p.name;
p.bornAt = Math.min(s.bornAt, now); p.bond = clamp(s.bond); p.interactions = Math.floor(s.interactions); p.clock = s.clock;
p.journal = Array.isArray(s.journal) ? s.journal.filter(e => Number.isFinite(e?.at) && typeof e?.text === 'string').slice(0, 30).map(e => ({ at: e.at, text: e.text.slice(0, 240) })) : [];
advanceAway(p, Math.max(0, (now - s.savedAt) / 1000));
p.reason = 'Welcome back! I missed you.';
record(p, p.reason); return p;
} catch { return null; }
}
// Real elapsed time, independent of the demonstration speed. Rest restores
// energy, but food, water, company and play still need the owner's attention.
export function advanceAway(p, seconds) {
if (!Number.isFinite(seconds) || seconds <= 0) return p;
for (const [key, rate] of Object.entries({satiety:.001, hydration:.0012, stimulation:.0008, affection:.0005}))
p.needs[key] = clamp(p.needs[key] - seconds * rate);
p.needs.energy = clamp(p.needs.energy + seconds * .006);
p.clock += seconds;
return p;
}
// One call per physical incident, not per simulation frame.
export function incident(p,kind) {
if(kind==='grab') {p.needs.affection=clamp(p.needs.affection-9);p.reason='Please let me walk! I do not like being picked up.';p.emotion='angry';}
else if(kind==='fall') {p.needs.stimulation=clamp(p.needs.stimulation-12);p.needs.energy=clamp(p.needs.energy-10);p.reason='Ouch! That tumble made me tired and unhappy.';p.emotion='angry';}
else return false;
p.emotionSeq++;record(p,p.reason);return true;
}
|