-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathreset_context.py
More file actions
295 lines (235 loc) · 11.1 KB
/
Copy pathreset_context.py
File metadata and controls
295 lines (235 loc) · 11.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Lazy-loaded reset helpers for GameContext.
This module is imported only when a reset is requested, then immediately
unloaded via sys.modules.pop() so its bytecode and data don't occupy RAM
during normal gameplay.
"""
def _generate_seed():
"""Generate a 64-bit seed using hardware RNG (ESP32) or os.urandom fallback."""
try:
import os
b = os.urandom(8)
seed = 0
for byte in b:
seed = (seed << 8) | byte
return seed
except Exception:
import random
return random.getrandbits(32) | (random.getrandbits(32) << 32)
def _xorshift32(x):
x ^= (x << 13) & 0xFFFFFFFF
x ^= (x >> 17)
x ^= (x << 5) & 0xFFFFFFFF
return x & 0xFFFFFFFF
_PERSONALITY_TRAITS = ('courage', 'loyalty', 'mischievousness', 'curiosity', 'sociability')
_MEALS = ('kibble', 'cod', 'haddock', 'trout', 'shrimp', 'herring',
'turkey', 'tuna', 'salmon', 'chicken', 'liver', 'beef', 'lamb')
_SNACKS = ('carrots', 'pumpkin', 'treats', 'fish_bite', 'eggs',
'nugget', 'milk', 'chew_stick', 'puree')
_TOY_VARIANTS = ('string', 'feather', 'ball', 'laser', 'mouse')
_LOCATIONS = ('outside', 'kitchen', 'treehouse', 'bedroom')
_FAV_WEATHERS = ('sunny', 'rainy', 'snowy', 'overcast')
_STAR_SIGNS = ('Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo',
'Libra', 'Scorpio', 'Sagittarius', 'Capricorn', 'Aquarius', 'Pisces')
_TRAIT_MAGNITUDE = 10 # max offset in either direction
def _derive_trait_offsets(seed):
"""Derive balanced personality offsets from a 64-bit seed.
Generates an offset for each personality trait in [-_TRAIT_MAGNITUDE, +_TRAIT_MAGNITUDE].
The offsets are mean-centered so no seed produces a uniformly happier or sadder cat.
"""
state = (seed ^ (seed >> 32)) & 0xFFFFFFFF or 1
span = 2 * _TRAIT_MAGNITUDE + 1 # 0 to 2*MAGNITUDE inclusive
raw = []
for _ in range(len(_PERSONALITY_TRAITS)):
state = _xorshift32(state)
raw.append(state % span)
mean = sum(raw) // len(raw)
return [v - mean for v in raw]
def _derive_favorites(seed):
"""Derive gender and fav/least-fav prefs from a 64-bit seed.
Uses the upper 32 bits as the xorshift32 starting state, keeping this
derivation independent from _derive_trait_offsets (which uses the lower 32).
Safe to call on any existing seed without affecting personality traits.
"""
state = (seed >> 32) & 0xFFFFFFFF or 1
def _next():
nonlocal state
state = _xorshift32(state)
return state
gender = 'tom' if _next() % 2 == 0 else 'queen'
fav_weather = _FAV_WEATHERS[_next() % 4]
def _pick_two(items):
n = len(items)
fi = _next() % n
li = (fi + 1 + _next() % (n - 1)) % n
return items[fi], items[li]
fav_meal, least_fav_meal = _pick_two(_MEALS)
fav_snack, least_fav_snack = _pick_two(_SNACKS)
fav_toy, least_fav_toy = _pick_two(_TOY_VARIANTS)
fav_location, least_fav_location = _pick_two(_LOCATIONS)
return {
'pet_gender': gender,
'fav_weather': fav_weather,
'star_sign': _STAR_SIGNS[_next() % 12],
'fav_meal': fav_meal,
'least_fav_meal': least_fav_meal,
'fav_snack': fav_snack,
'least_fav_snack': least_fav_snack,
'fav_toy': fav_toy,
'least_fav_toy': least_fav_toy,
'fav_location': fav_location,
'least_fav_location': least_fav_location,
}
def _make_starter_plants():
"""Return the initial set of plants that replace the old static scene decorations.
These mirror the developer-placed planters that were hard-coded in each
scene's setup_scene() method. They start at 'young' stage with zero water
debt so the world feels alive without immediately demanding player attention.
"""
_id = [0]
def _p(scene, layer, x, y_snap, pot, plant_type, stage='young', age_hours=80, mirror=False):
plant = {
'id': _id[0],
'type': plant_type,
'scene': scene,
'layer': layer,
'x': x,
'y_snap': y_snap,
'pot': pot,
'stage': stage,
'age_hours': age_hours,
'water_debt_hours': 0,
'fertilizer': 0.0,
'planted_day': 0,
'mirror': mirror,
}
_id[0] += 1
return plant
return [
_p('inside', 'midground', 110, 29, 'small', 'rose', stage='growing', age_hours=200),
_p('kitchen', 'midground', 130, 24, 'medium', 'cat_grass', stage='mature', age_hours=160),
_p('outside', 'foreground', 10, 63, 'small', 'cat_grass', stage='growing', age_hours=160),
_p('outside', 'midground', 130, 61, 'ground', 'sunflower', stage='mature', age_hours=360, mirror=True),
_p('outside', 'midground', 40, 61, 'ground', 'cat_grass', stage='thriving', age_hours=150),
_p('outside', 'background', 110, 56, 'ground', 'cat_grass', stage='thriving', age_hours=320),
_p('treehouse', 'foreground', 15, 63, 'small', 'rose', stage='young', age_hours=72),
_p('treehouse', 'midground', 120, 59, 'medium', 'freesia', stage='thriving', age_hours=88, mirror=True),
]
def reset_plants(ctx):
"""Restore all plants to the default starter set."""
ctx.plants = _make_starter_plants()
ctx.next_plant_id = len(ctx.plants)
ctx._last_plant_tick_hour = None
def do_reset(ctx, delete_save):
"""Reset all stats on ctx to defaults. Optionally delete the save file."""
# Unique 64-bit identity for this pet (survives between saves)
ctx.pet_seed = _generate_seed()
# Gender and favorites derived deterministically from the seed
for k, v in _derive_favorites(ctx.pet_seed).items():
setattr(ctx, k, v)
# Meta stat (computed from other stats)
ctx.health = 50
# Rapidly changing stats (change on a daily basis)
ctx.fullness = 50 # Inverse of hunger. Feed to maintain.
ctx.energy = 50 # How rested the pet is
ctx.comfort = 50 # Physical comfort. Temperature, environment, etc...
ctx.playfulness = 50 # Mood to play
ctx.focus = 50 # Ability to concentrate on tasks/training
# Slower changing stats (change on more of a weekly basis)
ctx.fulfillment = 50 # Feeling like the pet has purpose and things to do
ctx.cleanliness = 50 # How clean the pet and its environment are
ctx.intelligence = 50 # Problem-solving, learning new skills/tricks
ctx.maturity = 50 # Behavioral sophistication
ctx.affection = 50 # How much the pet feels loved
# Even slower changing stats (change on more of a monthly basis)
ctx.fitness = 50 # Athleticism
ctx.serenity = 50 # Inner peace. Makes them less likely to be stressed
# Slowest changing stats (basically traits with little or no change).
# Offset by a balanced, seed-derived personality so each pet feels distinct.
_offsets = _derive_trait_offsets(ctx.pet_seed)
ctx.courage = 50 + _offsets[0]
ctx.loyalty = 50 + _offsets[1]
ctx.mischievousness = 50 + _offsets[2]
ctx.curiosity = 50 + _offsets[3]
ctx.sociability = 50 + _offsets[4]
# Coins (earned from minigames and hunting, spent in the store)
ctx.coins = 50
# Quantities of food purchased from the store (uses per type)
ctx.food_stock = {
"chicken": 0, "salmon": 0, "tuna": 0, "shrimp": 0, "mackerel": 0, "kibble": 5,
"chew_stick": 0, "nugget": 3, "puree": 0, "milk": 0, "fish_bite": 0,
}
# Inventory of owned items
ctx.inventory = {
"toys": [],
"pots": {"small": 0, "medium": 0, "large": 0, "planter": 0},
"seeds": {"cat_grass": 0, "sunflower": 0, "rose": 0, "tulip": 0},
"tools": {"watering_can": False, "spade": False},
"fertilizer": 0,
"medicine": 0,
}
# Living plants placed by the player (and the developer-seeded starters).
# Each entry is a dict; see plant_system.py for the full schema.
reset_plants(ctx)
# Non-persisted: pending plant move across scenes.
# Set by the Tend→"Move to" action; consumed by the destination scene on enter.
ctx.pending_gardening_move = None
# Minigame high scores
ctx.zoomies_high_score = 0
ctx.maze_best_time = 0 # Best time in seconds (0 = not played)
ctx.snake_high_score = 0
ctx.memory_best_score = -1 # Fewest mismatches (-1 = not yet played)
ctx.hanjie_best_time = -1 # Fastest solve in seconds (-1 = not yet played)
# For storing time/weather/season/moon-phase type data
ctx.environment = {}
# Debug: time scale multiplier (1.0 = normal, 2.0 = 2x speed, 0.0 = paused)
ctx.time_speed = 1.0
# Scene bounds for character movement (world coordinates, set by each scene on load)
ctx.scene_x_min = 10
ctx.scene_x_max = 118
# Time of last save in ticks_ms; None = never saved this session
ctx.last_save_time = None
# WiFi location tracking (lists persisted; flag is not)
ctx.wifi_familiar = [] # up to 16 well-known APs (persisted)
ctx.wifi_recent = [] # up to 8 candidate APs (persisted)
ctx.in_familiar_location = True # updated each scan; defaults True when WiFi disabled
# Recent completed behavior names for loop prevention (most recent first, not persisted)
ctx.recent_behaviors = []
# Recent meals for variety tracking (most recent first, persisted)
ctx.recent_meals = []
# Sickness level (0.0–10.0 hard cap). Accumulates from neglect, weather, snack abuse.
ctx.sickness = 0.0
# True when the player has given medicine; cleared after the next sleep/nap applies the bonus.
ctx.medicine_pending = False
# Name of the most recently started behavior (not persisted, used to restore on scene re-entry)
ctx.current_behavior_name = None
# Last active "main" scene (inside/outside/etc) - used by secondary scenes to return home
ctx.last_main_scene = 'inside'
# Requested scene change from a behavior (e.g. go_to on arrival). Cleared by scene_manager.
ctx.pending_scene = None
# ESP-NOW manager; injected by Game.__init__ when WIFI_ENABLED. None otherwise.
ctx.espnow = None
# Pet display name. Derived from MAC on first social scene entry; None until then.
ctx.pet_name = None
# First-run tutorial state (not persisted)
ctx.first_impressions = False
ctx.pending_popup = None
ctx.milestones = {'fed': False, 'groomed': False, 'played': False, 'petted': False, 'store': False}
# Active visit state. None when not visiting, otherwise:
# {'peer_mac': bytes, 'peer_name': str, 'role': str,
# 'greeted': bool, 'play_time': float}
ctx.visit = None
# Friends: mac_hex_str -> {'n': name, 't': total_seconds, 'c': visit_count}
ctx.friends = {}
if delete_save:
try:
from context import _SAVE_PATH
_path = _SAVE_PATH
except Exception:
_path = '/save.json'
try:
import uos
uos.remove(_path)
print("[Context] Save file deleted")
except:
pass
print("[Context] Reset to defaults")