Spaces:
Running
Running
Download app/src/pet/engine.js from FormaLau/DuckAgochi: direct link, hf CLI and curl.
- Browser
- Download file 8.77 kB
-
https://huggingface.co/spaces/FormaLau/DuckAgochi/resolve/main/app/src/pet/engine.js
- Command line
-
hf download hf://spaces/FormaLau/DuckAgochi/app/src/pet/engine.js
-
curl -L -o engine.js https://huggingface.co/spaces/FormaLau/DuckAgochi/resolve/main/app/src/pet/engine.js
8.77 kB
| // 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; | |
| } | |