-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplant.py
More file actions
629 lines (520 loc) · 22.8 KB
/
plant.py
File metadata and controls
629 lines (520 loc) · 22.8 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
"""
Plant Module
Defines the Plant class representing individual pea plants in the garden.
Handles growth stages, trait revelation, watering, health, and reproduction.
"""
from dataclasses import dataclass, field, InitVar
import random
from typing import Optional
import tkinter as tk
# Growth stage names for display
STAGE_NAMES = {
0: "Empty",
1: "Seed",
2: "Seedling",
3: "Young plant",
4: "Budding",
5: "Flowering",
6: "Pod development",
7: "Mature seeds",
}
# Traits and the stage at which they become observable
TRAITS = {
"plant_height": 3,
"flower_position": 5,
"flower_color": 5,
"pod_shape": 7,
"pod_color": 6,
"seed_shape": 7,
"seed_color": 7,
}
# Difficulty-based lifecycle settings
LIFECYCLE_SETTINGS = {
"off": { # Casual - Fast growth, short lifespan
"base_thresholds": [0, 0, 4, 8, 13, 18, 25, 30],
"max_age_range": (60, 90),
"description": "Casual: Fast growth for genetics experimentation"
},
"overlay": { # Moderate - Medium growth, medium lifespan
"base_thresholds": [0, 0, 5, 10, 20, 30, 42, 55],
"max_age_range": (85, 115), # stage 7 at day 55; min 85 gives ≥30-day harvest window
"description": "Moderate: Balanced growth speed"
},
"enforce": { # Realistic - Slow growth, long lifespan (Mendel-era timing)
"base_thresholds": [0, 0, 7, 14, 28, 42, 60, 85],
"max_age_range": (125, 160), # stage 7 at day 85; min 125 gives ≥40-day harvest window
"description": "Realistic: Historical pea plant lifecycle"
}
}
def get_lifecycle_settings(difficulty="off"):
"""
Get lifecycle settings for the given difficulty.
Args:
difficulty: One of "off" (casual), "overlay" (moderate), "enforce" (realistic)
Returns:
Dict with base_thresholds and max_age_range
"""
return LIFECYCLE_SETTINGS.get(difficulty, LIFECYCLE_SETTINGS["off"])
@dataclass
class Plant:
"""
Represents a single pea plant in the garden.
Tracks growth, health, water levels, trait revelation, and reproduction state.
"""
# Core identity
id: int
env: InitVar['GardenEnvironment'] # type: ignore
generation: str = "F0"
# Growth state
stage: int = 0
alive: bool = True
days_since_planting: int = 0
# Resource levels
health: int = 100
water: int = 50
# Genetics and traits
traits: dict = field(default_factory=dict)
revealed_traits: dict = field(default_factory=dict)
reveal_order: list = field(default_factory=list)
observed_today: set = field(default_factory=set)
# Growth timing
entered_stage5_age: Optional[int] = None
max_age_days: int = 0 # Will be set in __post_init__ based on difficulty
senescent: bool = False
germination_delay: int = 0
difficulty: str = "off" # Track difficulty for this plant
# Reproduction state
pending_cross: Optional[dict] = None
pollinated: bool = False
emasculated: bool = False
emasc_day: Optional[int] = None
emasc_phase: Optional[str] = None
selfing_frac_before_emasc: float = 0.0
# Reproductive capacity (realistic pod/ovule counts)
pods_total: int = 0
pods_remaining: int = 0
ovules_per_pod: int = 0
ovules_left: int = 0
aborted_ovules: int = 0
# Lineage tracking
ancestry: list = field(default_factory=list)
paternal_ancestry: list = field(default_factory=list)
# Pollen collection state
last_anther_check_day: Optional[int] = None
anthers_available_today: bool = False
anthers_collected_day: Optional[int] = None
# Vigour / weak-plant flag (Mendel excluded weak specimens from experiments)
# Set once at planting based on difficulty; never changes afterwards.
is_weak: bool = False
# If True the plant is constitutionally unable to reach flowering (stage 5+).
# ~70% of weak plants are blocked; the other 30% flower but bear very few pods.
weak_blocks_flowering: bool = False
# Late-season stress flag (enforce/realistic mode only).
# Set externally by _season_daily_update when the calendar enters autumn.
# Once True: health cannot recover, color shifts to sick palette, stress
# damage is amplified — plant will die before January.
late_season_stress: bool = False
# Class-level icon cache (lazy-loaded)
_ICONS = None
def __post_init__(self, env):
"""Initialize plant after dataclass creation."""
# Register with environment
env.register_plant(self)
# Get difficulty from environment (with fallback)
try:
# Try to get difficulty from the app via the garden environment
if hasattr(env, '_app'):
app = env._app
self.difficulty = str(getattr(app, '_season_mode', 'off'))
else:
self.difficulty = "off"
except Exception:
self.difficulty = "off"
# Set max_age based on difficulty
if self.max_age_days == 0: # Only set if not already set
settings = get_lifecycle_settings(self.difficulty)
min_age, max_age = settings["max_age_range"]
self.max_age_days = random.randint(min_age, max_age)
# Roll weak-plant status based on difficulty (only if not already set)
# Probabilities follow Mendel's own observation that a noticeable minority
# of plants were too weak to give reliable results.
if not getattr(self, "is_weak", False):
_weak_prob = {"off": 0.0, "overlay": 0.125, "enforce": 0.225}.get(
self.difficulty, 0.0)
if _weak_prob > 0.0 and random.random() < _weak_prob:
self.is_weak = True
# 70% blocked from flowering entirely; 30% flower but low fertility
self.weak_blocks_flowering = (random.random() < 0.70)
# Set up F0 lineage
try:
gen = str(getattr(self, "generation", "F0") or "F0")
if gen.upper() == "F0":
if not (getattr(self, "ancestry", None) or []):
self.ancestry = [self.id]
if not (getattr(self, "paternal_ancestry", None) or []):
self.paternal_ancestry = [self.id]
except Exception:
pass
# Normalize trait keys (plant_height vs stem_length)
if "plant_height" not in self.traits and "stem_length" in self.traits:
self.traits["plant_height"] = self.traits.get("stem_length")
# Set default traits if missing
defaults = {
"plant_height": random.choice(("tall", "dwarf")),
"flower_position": random.choice(("axial", "terminal")),
"flower_color": random.choice(("purple", "white")),
"pod_shape": random.choice(("inflated", "constricted")),
"pod_color": random.choice(("green", "yellow")),
"seed_shape": random.choice(("round", "wrinkled")),
"seed_color": random.choice(("yellow", "green")),
}
for trait_name, default_value in defaults.items():
if trait_name not in self.traits or self.traits[trait_name] in (None, ""):
self.traits[trait_name] = default_value
# Initialize reproductive capacity
self._init_reproduction()
def _init_reproduction(self):
"""Initialize realistic pod and ovule counts."""
try:
if not getattr(self, "pods_total", 0):
if getattr(self, "is_weak", False):
# Weak plants that manage to flower bear very few pods (1-3)
self.pods_total = random.randint(1, 3)
else:
# Mendel-era average ~10 pods per plant (±2), clamped 5-20
self.pods_total = max(5, min(20, int(round(random.gauss(10, 2)))))
# Pods exist by default ONLY for non-emasculated plants (selfing possible)
if not self.emasculated:
self.pods_remaining = int(self.pods_total)
else:
self.pods_remaining = 0
if not getattr(self, "ovules_per_pod", 0):
# Typical peas ~7±2 ovules per flower (clamped 5-12)
self.ovules_per_pod = max(5, min(12, int(round(random.gauss(7, 2)))))
if not getattr(self, "ovules_left", 0):
self.ovules_left = int(self.ovules_per_pod)
if not hasattr(self, "aborted_ovules"):
self.aborted_ovules = 0
except Exception:
pass
def __hash__(self):
"""Make plants hashable by ID."""
return self.id
# ========================================================================
# Growth & Trait Revelation
# ========================================================================
def advance_growth(self):
"""
Advance plant growth stage based on age and health.
Growth speed varies by difficulty setting.
"""
if not self.alive or self.stage >= 7:
return
if getattr(self, 'fully_harvested', False):
return
# ------------------------------------------------------------
# Safety invariant:
# Emasculated plants without pollination must not have pods
# ------------------------------------------------------------
if self.emasculated and not self.pollinated:
self.pods_remaining = 0
elif self.pollinated and self.pods_remaining <= 0:
# Pollination restores pod development
self.pods_remaining = int(self.pods_total)
# Get difficulty-appropriate thresholds
settings = get_lifecycle_settings(getattr(self, 'difficulty', 'off'))
base_thresholds = settings["base_thresholds"]
# Handle germination delay for seeds
delay = getattr(self, "germination_delay", 0)
effective_age = self.days_since_planting - delay
if self.stage == 1 and effective_age < 0:
return # Still underground, not germinated
# Weak plants that block flowering cannot advance past stage 4 (budding).
# This mirrors Mendel's observation that weak specimens "entweder gar nicht
# zur Blüthe gelangen" (either don't reach flowering at all).
flowering_cap = 4 if (getattr(self, "is_weak", False)
and getattr(self, "weak_blocks_flowering", False)) else 7
# Check all stages we can advance to
# Keep advancing while we meet the requirements
while self.stage < flowering_cap:
next_stage = self.stage + 1
# Health affects growth speed (penalty days added to threshold)
health_factor = 0 if self.health > 70 else (1 if self.health > 40 else 2)
required_days = base_thresholds[next_stage] + health_factor
# Check if ready to advance to next stage
if effective_age >= required_days:
self.stage = next_stage
# Mark entry to flowering stage
try:
if self.stage == 5 and getattr(self, 'entered_stage5_age', None) is None:
self.entered_stage5_age = self.days_since_planting
except Exception:
pass
# Reveal traits appropriate to new stage
self.reveal_trait()
self.reveal_all_available()
# Legacy staged reveal support
stage_to_index = {3: 0, 4: 1, 5: 2}
if self.stage in stage_to_index:
idx = stage_to_index[self.stage]
while len(self.revealed_traits) <= idx and len(self.revealed_traits) < len(self.reveal_order):
self.discover_next_trait()
else:
# Can't advance further, stop checking
break
# Ensure full revelation at maturity
if self.stage >= 7:
self.reveal_all_available()
def reveal_trait(self):
"""
Reveal a random trait that has reached its stage threshold.
Returns:
Tuple of (trait_name, trait_value) if revealed, None otherwise
"""
if not self.alive:
return None
# Ensure reveal_order is mutable
if isinstance(getattr(self, "reveal_order", []), tuple):
try:
self.reveal_order = list(self.reveal_order)
except Exception:
self.reveal_order = []
# Find available traits to reveal
available = [
trait for trait, stage_threshold in TRAITS.items()
if stage_threshold <= self.stage and trait not in self.revealed_traits
]
if available:
trait = random.choice(available)
self.revealed_traits[trait] = self.traits.get(trait, "?")
self.reveal_order.append(trait)
return trait, self.revealed_traits[trait]
return None
def reveal_all_available(self):
"""Reveal all traits whose stage threshold has been reached."""
try:
for trait, threshold in TRAITS.items():
if self.stage >= threshold and trait not in self.revealed_traits:
self.revealed_traits[trait] = self.traits.get(trait, "?")
try:
if trait not in getattr(self, "reveal_order", []):
self.reveal_order.append(trait)
except Exception:
pass
except Exception:
pass
def discover_next_trait(self):
"""Reveal the next trait using true values (no randomness)."""
# Check reveal_order first
try:
for trait in getattr(self, "reveal_order", []):
if trait not in self.revealed_traits:
self.revealed_traits[trait] = self.traits.get(trait, self.revealed_traits.get(trait, "?"))
return
except Exception:
pass
# Otherwise reveal any eligible trait
try:
for trait, threshold in TRAITS.items():
if self.stage >= threshold and trait not in self.revealed_traits:
self.revealed_traits[trait] = self.traits.get(trait, "?")
try:
if trait not in getattr(self, "reveal_order", []):
self.reveal_order.append(trait)
except Exception:
pass
return
except Exception:
pass
# ========================================================================
# Water & Health Management
# ========================================================================
def tick_phase(self, weather: str):
"""
Update plant per growth phase (legacy 4-phase system).
Args:
weather: Current weather symbol
"""
if not self.alive:
return
# Evaporation based on weather (weak plants lose ~50% more)
if weather in ("☀️", "⛅"):
evap = 6
elif weather in ("☁️",):
evap = 4
else:
evap = 2
if getattr(self, "is_weak", False):
evap = int(round(evap * 1.5))
self.water = max(0, self.water - evap)
# Rain replenishes water
if weather in ("🌧", "⛈"):
self.water = min(100, self.water + 8)
# Health adjustments based on water level
self._update_health_from_water()
def tick_hour(self, weather: str, temp: float):
"""
Update plant per hour (fine-grained system).
Args:
weather: Current weather symbol
temp: Current temperature in °C
"""
if not self.alive:
return
# Temperature-adjusted evaporation
try:
temp_diff = float(temp) - 15.0
except Exception:
temp_diff = 0.0
base_evap = 0.8
evap_rate = base_evap + 0.06 * max(0.0, temp_diff) - 0.04 * max(0.0, -temp_diff)
# Weather modifier
weather_factor = {
'☀️': 1.0, '🌞': 1.0,
'⛅': 0.85,
'☁️': 0.7,
'🌧': 0.4, '⛈': 0.4,
}.get(weather, 0.8)
# Weak plants lose water ~50% faster (reduced root system / poor stomatal control)
if getattr(self, "is_weak", False):
evap_rate *= 1.5
evap = max(0.2, min(3.3, evap_rate * weather_factor))
self.water = max(0, int(round(self.water - evap)))
# Rain adds water (with ceiling)
if weather in ("🌧", "⛈") and self.water < 55:
inc = 1 if weather == "🌧" else 2
self.water = min(70, self.water + inc)
# Micro health adjustments
self._update_health_from_water()
def _update_health_from_water(self):
"""Adjust health based on current water level.
Weak plants suffer ~50% more from stress (any out-of-range water level
deals 1.5× the normal penalty), consistent with Mendel's note that weak
specimens produce unreliable results under adverse conditions.
"""
_weak = getattr(self, "is_weak", False)
if self.water < 20 or self.water > 95:
penalty = 3 if _weak else 2
self.health = max(0, self.health - penalty)
elif self.water > 85:
penalty = 2 if _weak else 1
self.health = max(0, self.health - penalty)
elif 40 <= self.water <= 70:
# Senescent, weak, or late-season-stressed plants cannot recover health
_late = getattr(self, 'late_season_stress', False)
if not getattr(self, 'senescent', False) and not _weak and not _late:
self.health = min(100, self.health + 1)
elif 30 <= self.water < 40 or 70 < self.water <= 85:
if _weak:
self.health = max(0, self.health - 1) # weak plants decline even in neutral band
else:
penalty = 2 if _weak else 1
self.health = max(0, self.health - penalty)
# Death check
if self.health <= 0:
self.alive = False
# Don't change stage - dead plant keeps its stage at time of death
def water_plant(self, phase: str):
"""
Manually water the plant.
Args:
phase: Current time of day ("morning", "noon", "afternoon", "evening")
Returns:
Status message
"""
if not self.alive:
return "Plant is dead."
self.water = min(100, self.water + 30)
# Midday watering causes stress
if phase in ("noon", "afternoon"):
penalty = random.randint(1, 3) if self.health >= 50 else random.randint(5, 10)
self.health = max(0, self.health - penalty)
if self.health == 0:
self.alive = False
# Don't change stage - dead plant keeps its stage
return f"Watered (stress -{penalty})."
return "Watered."
# ========================================================================
# Reproduction & Pollination
# ========================================================================
def can_emasculate(self):
"""
Check if plant can be emasculated (anthers removed).
Returns:
Tuple of (bool, str) - (can_emasculate, reason)
"""
if not self.alive:
return (False, "Plant not alive")
if self.stage not in (4, 5):
return (False, "Emasculate during bud/early flowering (Stages 4–5)")
if self.emasculated:
return (False, "Already emasculated")
return (True, "")
def can_collect_pollen(self):
"""
Check if pollen can be collected from this plant.
Returns:
Tuple of (bool, str) - (can_collect, reason)
"""
if getattr(self, "is_weak", False):
return False, "Weak plant — pollen from weak specimens gives unreliable results."
if self.emasculated:
return False, "This flower was emasculated: it cannot produce pollen anymore."
if not self.alive:
return False, "Plant not alive!"
if self.stage != 5:
return False, "Collect during flowering stage!"
if self.health < 70:
return False, "Health must be ≥ 70!"
return True, ""
# ========================================================================
# Display & Visual
# ========================================================================
def color(self) -> str:
"""
Get display color based on health status.
Weak plants are shown in a yellowish palette to distinguish them visually.
Returns:
Hex color string
"""
if not self.alive:
return "#666666"
if getattr(self, "is_weak", False) or getattr(self, "late_season_stress", False):
# Yellowish-amber sick palette: used for weak plants and late-season decline.
# Health 80+ stays green so a freshly-stressed plant isn't immediately alarming;
# below 80 it gradually shifts through amber and ochre as the season kills it.
if self.health >= 80:
return "#008b1c" # still green at full health
elif self.health >= 60:
return "#c8a822" # pale yellow — first sign of stress
elif self.health >= 40:
return "#b07010" # amber
elif self.health >= 20:
return "#885500" # ochre-brown
else:
return "#704000" # dark brown — near death
if self.health >= 80:
return "#008b1c"
elif self.health >= 60:
return "#6f6000"
elif self.health >= 40:
return "#cc6000"
elif self.health >= 20:
return "#f90000"
else:
return "#f33000"
@classmethod
def get_icons(cls):
"""
Lazy-load icon resources (class method).
Returns:
Dictionary of icon PhotoImages
"""
if cls._ICONS is None:
print("Loading icons from disk...")
cls._ICONS = {
'seedling': tk.PhotoImage(file="seedling.png"),
'adult': tk.PhotoImage(file="plant.png"),
'dead': tk.PhotoImage(file="withered.png")
}
return cls._ICONS