# Food System Implementation ## Overview The food system has been added to TribeGen. Food is stored at the tribe level and consumed each month after random progression events. Kobolds with insufficient food develop the "malnourished" injury, which is automatically cured when they have enough food again. ## Storage Location **Food is stored in the `saves` table** as a `food_count` integer column. This represents the tribe's total food supply, not given to specific kobolds. Items not given to kobolds are stored separately in the `items` table (linked to a save but not to a specific kobold via the junction table). ## Food Requirements by Age Tier Each kobold requires a portion of food based on their age: | Age Tier | Food Required | |----------|---------------| | Egg | 0.0 (don't eat) | | Baby (0-3) | 0.5 portions | | Adult (4-34) | 1.0 portions | | Old (35-59) | 0.6 portions | | Very Old (60+) | 0.4 portions | These values are configurable in the `Tribe` class `$foodRequirements` array. ## How Food Consumption Works ### On Month Progression 1. **Random events occur first** (handled by EventManager) 2. **Food consumption begins** (called from `Tribe::endMonth()`) 3. **Kobolds are fed in priority order**: - Malnourished kobolds are fed **first** (highest priority) - After that, **younger kobolds are fed before older ones** 4. **Kobolds without food get injured**: - First starvation: Receive "malnourished" injury (severity 1) - Future starvations: Remain malnourished 5. **Recovery**: When malnourished kobolds have enough food next month, injury is automatically removed ### Example Scenario Tribe has 10 food, 6 kobolds: - 1 malnourished baby (0.5 required) - **FED FIRST** - 1 young adult (1.0 required) - 2 older adults (1.0 each) - 1 old kobold (0.6 required) - 1 very old kobold (0.4 required) **Consumption Order:** 1. Malnourished baby: 0.5 food consumed (9 left) ✓ Healed 2. Baby adult: 1.0 food consumed (8 left) ✓ 3. First adult: 1.0 food consumed (7 left) ✓ 4. Second adult: 1.0 food consumed (6 left) ✓ 5. Old kobold: 0.6 food consumed (5.4 left) ✓ 6. Very old kobold: 0.4 food consumed (5.0 left) ✓ All fed! No new injuries. ## Implementation Details ### New Methods in `Tribe` Class ```php // Get current food count $tribe->getFoodCount() // Add food to storage $tribe->addFood($amount) // Remove food from storage $tribe->removeFood($amount) // Calculate total food needed for all alive kobolds $tribe->calculateFoodNeeded() // Get food requirement for specific kobold $tribe->getFoodRequirement($kobold) // Consume food at end of month (returns story) $foodStory = $tribe->consumeFood() // Get end-of-month food and aging story $tribe->endMonth() // Now returns combined story ``` ### Malnourished Injury - **Category**: `malnourished` - **Cure**: Automatic when food available (not by medicine) - **Severity**: 1 (contributes to total injury severity for death calculation) - **Behavior**: - Added when kobold can't be fed - Removed automatically next month if fed ### Database Changes **Table: `saves`** - Added column: ```sql food_count INTEGER DEFAULT 0 ``` ### Food Requirement Configuration Modify in `Tribe` class constructor: ```php private $foodRequirements = [ 'egg' => 0, // Current: 0 'baby' => 0.5, // Current: 0.5 'adult' => 1.0, // Current: 1.0 'old' => 0.6, // Current: 0.6 'very_old' => 0.4 // Current: 0.4 ]; ``` ## Integration Points ### From Events Events can add food to the tribe during play: ```php // In an event $tribe->addFood(5); // Add 5 food portions from successful hunt $story = "The hunting party returned with 5 portions of food!\n"; $this->tribe->addFood(5); return $story; ``` ### From API The `endMonth` API endpoint will include food consumption story: ```php // In src/api/game.php endMonth endpoint $foodStory = $tribe->endMonth(); // Include in response narrative $monthStory = $foodStory . "\n\n" . $otherEvents; ``` ### Displaying Food Count Frontend can show food in game screen: ```javascript // In app.js when rendering game const food = data.tribe.foodCount; document.getElementById('foodDisplay').textContent = food; ``` ## Special Cases ### Eggs - Don't require food (food requirement = 0) - Won't get malnourished injuries - Can't die from starvation ### Babies Transitioning to Adult - At age 4, requirement increases from 0.5 to 1.0 portions - If food is tight, newly adult kobolds might go hungry their first adult month ### Death by Starvation - Starvation indirectly causes death through repeated malnourished injuries - A kobold with malnourished injury (severity 1) + other injuries might reach vitality threshold - Or repeated starvation months could stack multiple injuries ## Events and Food ### Hunting Event Example ```php class HuntingEvent extends Event { public function execute() { $this->story = "The hunting party set out...\n\n"; $success = 0; foreach ($this->tribe->getAliveKobolds() as $kobold) { if ($kobold->checkStat('fighting')) { // Actually hunting skill $success++; } } // 0-3 food based on hunters $foodFound = max(0, $success - 1); if ($foodFound > 0) { $this->tribe->addFood($foodFound); $this->story .= "The hunters returned with {$foodFound} portions of food!"; } else { $this->story .= "The hunt was unsuccessful."; } return $this->story; } } ``` ### Farming Event Example ```php class FarmingEvent extends Event { public function execute() { $this->story = "The farmers worked the fields...\n\n"; $totalCrafting = 0; foreach ($this->tribe->getAliveKobolds() as $kobold) { $totalCrafting += $kobold->getStat('crafting'); } // Average crafting skill determines harvest $avgCrafting = $totalCrafting / max(1, count($this->tribe->getAliveKobolds())); $harvest = (int)($avgCrafting / 20); // Scale to 0-5 if ($harvest > 0) { $this->tribe->addFood($harvest); $this->story .= "The fields yielded {$harvest} portions of food!"; } else { $this->story .= "The crops failed this season."; } return $this->story; } } ``` ## Testing ### Manual Test 1. Create a tribe with 6 kobolds 2. Give it 5 food (6 × 1.0 = 6 needed) 3. End the month 4. One kobold should get malnourished injury 5. Next month, add more food 6. Malnourished kobold should recover ### Test Prioritization 1. Create malnourished baby and normal adults 2. Set food to exactly 0.5 portions 3. End month 4. Baby should be fed, adult should be malnourished 5. Verify story says baby was healed ## Configuration & Customization ### Adjusting Food Requirements In `src/Classes/Tribe.php`, change the `$foodRequirements` array: ```php private $foodRequirements = [ 'egg' => 0, 'baby' => 0.3, // Babies eat less 'adult' => 1.5, // Adults eat more 'old' => 0.8, // Old eat more 'very_old' => 0.6 ]; ``` ### Adjusting Starvation Severity In `Tribe::consumeFood()`, change the injury severity: ```php // Currently severity 1 $kobold->addInjury('malnourished', 'malnourished', 1); // Could be 2 for more serious $kobold->addInjury('malnourished', 'malnourished', 2); ``` ### Seasonal Food Modifiers Could be added to events: ```php // In farming event, modify based on season $harvest = ...; if ($season === 'summer') { $harvest *= 1.5; // Better harvest in summer } elseif ($season === 'winter') { $harvest *= 0.5; // Poor harvest in winter } ``` ## Files Modified - `src/Classes/Tribe.php` - Added food system methods - `src/Database/Database.php` - Added `food_count` to `saves` table - `src/Classes/Injury.php` - Added malnourished to cure map - `src/Utilities/GameHelper.php` - Added malnourished to injury helper ## Summary The food system is now fully integrated: - ✅ Stored at tribe level (in `saves.food_count`) - ✅ Consumed each month after random events - ✅ Prioritizes malnourished, then younger kobolds - ✅ Automatically applies and removes malnourished injury - ✅ Events can add food (via hunting, farming, etc.) - ✅ Ready for configuration and balance tuning