| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../src/System/SaveManager.php'; |
| 4 | require_once __DIR__ . '/../../src/Classes/Kobold.php'; |
| 5 | |
| 6 | class Tribe |
| 7 | { |
| 8 | private $userId; |
| 9 | private $saveId; |
| 10 | private $name; |
| 11 | public $kobolds = []; |
| 12 | private $storage = []; |
| 13 | private $currentMonth = 0; |
| 14 | private $currentSeason = 'spring'; |
| 15 | private $foodCount = 0; |
| 16 | private $environmentId = null; |
| 17 | private $createdAt; |
| 18 | private $lastPlayed; |
| 19 | private $saveManager; |
| 20 | public $dataStore; |
| 21 | |
| 22 | // Food requirement by age tier (portion units) |
| 23 | public $foodRequirements = [ |
| 24 | 'egg' => 0, |
| 25 | 'baby' => 1, |
| 26 | 'young' => 2, |
| 27 | 'adult' => 3, |
| 28 | 'old' => 2 |
| 29 | ]; |
| 30 | |
| 31 | public function __construct($userId, $saveId, $name = '') |
| 32 | { |
| 33 | $this->userId = $userId; |
| 34 | $this->saveId = $saveId; |
| 35 | $this->name = $name; |
| 36 | $this->saveManager = new SaveManager(); |
| 37 | $this->loadFromFile(); |
| 38 | } |
| 39 | |
| 40 | public static function create($userId, $saveName) |
| 41 | { |
| 42 | $saveManager = new SaveManager(); |
| 43 | $saveId = $saveManager->generateSaveId($userId); |
| 44 | |
| 45 | $tribe = new self($userId, $saveId, $saveName); |
| 46 | $tribe->createdAt = date('Y-m-d H:i:s'); |
| 47 | $tribe->lastPlayed = date('Y-m-d H:i:s'); |
| 48 | $tribe->save(); |
| 49 | |
| 50 | return $tribe; |
| 51 | } |
| 52 | |
| 53 | public static function load($userId, $saveId) |
| 54 | { |
| 55 | return new self($userId, $saveId); |
| 56 | } |
| 57 | |
| 58 | private function loadFromFile() |
| 59 | { |
| 60 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 61 | $saveFile = $savePath . '/save.json'; |
| 62 | |
| 63 | if (file_exists($saveFile)) { |
| 64 | $data = $this->saveManager->loadJson($saveFile); |
| 65 | |
| 66 | if ($data) { |
| 67 | $this->name = $data['name'] ?? ''; |
| 68 | $this->currentMonth = $data['current_month'] ?? 0; |
| 69 | $this->currentSeason = $data['current_season'] ?? 'spring'; |
| 70 | $this->foodCount = $data['food_count'] ?? 0; |
| 71 | $this->environmentId = $data['environment_id'] ?? null; |
| 72 | $this->createdAt = $data['created_at'] ?? date('Y-m-d H:i:s'); |
| 73 | $this->lastPlayed = $data['last_played'] ?? date('Y-m-d H:i:s'); |
| 74 | $this->dataStore = $data['data_store'] ?? new stdClass(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | $this->loadKobolds(); |
| 79 | $this->loadStorage(); |
| 80 | } |
| 81 | |
| 82 | private function loadKobolds() |
| 83 | { |
| 84 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 85 | $koboldsFile = $savePath . '/kobolds.json'; |
| 86 | |
| 87 | $this->kobolds = []; |
| 88 | |
| 89 | if (file_exists($koboldsFile)) { |
| 90 | $data = $this->saveManager->loadJson($koboldsFile); |
| 91 | |
| 92 | if ($data && is_array($data)) { |
| 93 | foreach ($data as $koboldData) { |
| 94 | $kobold = Kobold::fromArray($koboldData, $this->userId, $this->saveId); |
| 95 | if ($kobold) { |
| 96 | $this->kobolds[$kobold->getId()] = $kobold; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private function loadStorage() |
| 104 | { |
| 105 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 106 | $itemsFile = $savePath . '/items.json'; |
| 107 | |
| 108 | $this->storage = []; |
| 109 | |
| 110 | if (file_exists($itemsFile)) { |
| 111 | $this->storage = $this->saveManager->loadJson($itemsFile) ?? []; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public function save() |
| 116 | { |
| 117 | $this->lastPlayed = date('Y-m-d H:i:s'); |
| 118 | |
| 119 | $savePath = $this->saveManager->getSavePath($this->userId, $this->saveId); |
| 120 | |
| 121 | // Save tribe metadata |
| 122 | $tribeData = [ |
| 123 | 'name' => $this->name, |
| 124 | 'current_month' => $this->currentMonth, |
| 125 | 'current_season' => $this->currentSeason, |
| 126 | 'food_count' => $this->foodCount, |
| 127 | 'food_months_left' => $this->foodMonthsLeft($this->foodCount), |
| 128 | 'environment_id' => $this->environmentId, |
| 129 | 'created_at' => $this->createdAt, |
| 130 | 'last_played' => $this->lastPlayed, |
| 131 | 'data_store' => $this->dataStore ?? new stdClass() |
| 132 | ]; |
| 133 | |
| 134 | $this->saveManager->saveJson($savePath . '/save.json', $tribeData); |
| 135 | |
| 136 | // Save kobolds |
| 137 | $koboldsData = []; |
| 138 | foreach ($this->kobolds as $kobold) { |
| 139 | if (!$kobold->isDead()) { |
| 140 | $kobold->save(); |
| 141 | $koboldsData[] = $kobold->toArray(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | $this->saveManager->saveJson($savePath . '/kobolds.json', $koboldsData); |
| 146 | |
| 147 | // Save items |
| 148 | $this->saveManager->saveJson($savePath . '/items.json', $this->storage); |
| 149 | |
| 150 | GameHelper::cleanupUnusedKoboldSprites($this->userId); |
| 151 | } |
| 152 | |
| 153 | // Getters and Setters |
| 154 | public function getName() |
| 155 | { |
| 156 | return $this->name; |
| 157 | } |
| 158 | |
| 159 | public function setName($name) |
| 160 | { |
| 161 | $this->name = $name; |
| 162 | } |
| 163 | |
| 164 | public function getSaveId() |
| 165 | { |
| 166 | return $this->saveId; |
| 167 | } |
| 168 | |
| 169 | public function getUserId() |
| 170 | { |
| 171 | return $this->userId; |
| 172 | } |
| 173 | |
| 174 | public function getCreatedAt() |
| 175 | { |
| 176 | return $this->createdAt; |
| 177 | } |
| 178 | |
| 179 | public function getLastPlayed() |
| 180 | { |
| 181 | return $this->lastPlayed; |
| 182 | } |
| 183 | |
| 184 | public function getCurrentMonth() |
| 185 | { |
| 186 | return $this->currentMonth; |
| 187 | } |
| 188 | |
| 189 | public function setCurrentMonth($month) |
| 190 | { |
| 191 | $this->currentMonth = max(0, $month); |
| 192 | } |
| 193 | |
| 194 | public function getCurrentSeason() |
| 195 | { |
| 196 | return $this->currentSeason; |
| 197 | } |
| 198 | |
| 199 | public function setCurrentSeason($season) |
| 200 | { |
| 201 | $this->currentSeason = $season; |
| 202 | } |
| 203 | |
| 204 | public function getEnvironmentId() |
| 205 | { |
| 206 | return $this->environmentId; |
| 207 | } |
| 208 | |
| 209 | public function setEnvironmentId($environmentId) |
| 210 | { |
| 211 | $this->environmentId = $environmentId; |
| 212 | } |
| 213 | |
| 214 | // Kobold management |
| 215 | public function getKobolds($includeDeadAndExpelled = false) |
| 216 | { |
| 217 | if ($includeDeadAndExpelled) { |
| 218 | return $this->kobolds; |
| 219 | } |
| 220 | |
| 221 | return array_filter($this->kobolds, function (Kobold $k) { |
| 222 | return !$k->isDead() && !$k->isExpelled(); |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | public function getKobold($id) |
| 227 | { |
| 228 | return $this->kobolds[$id] ?? null; |
| 229 | } |
| 230 | |
| 231 | public function addKobold(Kobold $kobold) |
| 232 | { |
| 233 | $this->kobolds[$kobold->getId()] = $kobold; |
| 234 | return $kobold; |
| 235 | } |
| 236 | |
| 237 | public function removeKobold($id) |
| 238 | { |
| 239 | unset($this->kobolds[$id]); |
| 240 | } |
| 241 | |
| 242 | public function getAliveKobolds() |
| 243 | { |
| 244 | return array_filter($this->kobolds, fn($k) => !$k->isDead()); |
| 245 | } |
| 246 | |
| 247 | // Storage management |
| 248 | public function addToStorage(Item $item, int $count = 1): array |
| 249 | { |
| 250 | // Accept either Item object or array |
| 251 | if (is_object($item) && method_exists($item, 'toArray')) { |
| 252 | $this->storage[] = $item->toArray(); |
| 253 | } elseif (is_array($item)) { |
| 254 | // Ensure it has an id |
| 255 | if (!isset($item['id'])) { |
| 256 | $item['id'] = uniqid('itm_'); |
| 257 | } |
| 258 | for ($i = 0; $i < $count; $i++) |
| 259 | $this->storage[] = $item; |
| 260 | } |
| 261 | return $this->storage; |
| 262 | } |
| 263 | |
| 264 | public function getStorage() |
| 265 | { |
| 266 | return $this->storage; |
| 267 | } |
| 268 | |
| 269 | // Food system methods |
| 270 | public function getFoodCount() |
| 271 | { |
| 272 | return $this->foodCount; |
| 273 | } |
| 274 | |
| 275 | public function addFood($amount) |
| 276 | { |
| 277 | $this->foodCount += $amount; |
| 278 | return $this->foodCount; |
| 279 | } |
| 280 | |
| 281 | public function removeFood($amount) |
| 282 | { |
| 283 | $this->foodCount = max(0, $this->foodCount - $amount); |
| 284 | return $this->foodCount; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Calculate total food needed for all alive kobolds for one month |
| 289 | */ |
| 290 | public function calculateFoodNeeded() |
| 291 | { |
| 292 | $needed = 0; |
| 293 | |
| 294 | foreach ($this->getAliveKobolds() as $kobold) { |
| 295 | $ageCategory = $kobold->getAgeCategory(); |
| 296 | $requirement = $this->foodRequirements[$ageCategory] ?? 1.0; |
| 297 | $needed += $requirement; |
| 298 | } |
| 299 | |
| 300 | return $needed; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Calculate how many full months the current kobolds can be fed |
| 305 | * with the provided amount of food. |
| 306 | */ |
| 307 | public function foodMonthsLeft($amount) |
| 308 | { |
| 309 | $foodPerMonth = $this->calculateFoodNeeded(); |
| 310 | if ($foodPerMonth <= 0) { |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | $available = max(0, (float) $amount); |
| 315 | return (int) floor($available / $foodPerMonth); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Get food requirement for a specific kobold |
| 320 | */ |
| 321 | public function getFoodRequirement(Kobold $kobold) |
| 322 | { |
| 323 | $ageCategory = $kobold->getAgeCategory(); |
| 324 | return $this->foodRequirements[$ageCategory] ?? 1.0; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Consume food at end of month after events |
| 329 | * Prioritizes: malnourished kobolds first, then younger before older |
| 330 | * Returns story of food consumption and any starvations |
| 331 | */ |
| 332 | public function consumeFood() |
| 333 | { |
| 334 | $story = ""; |
| 335 | $aliveKobolds = $this->getAliveKobolds(); |
| 336 | |
| 337 | if (empty($aliveKobolds)) { |
| 338 | return $story; |
| 339 | } |
| 340 | |
| 341 | // Sort kobolds: malnourished first, then by age (younger first) |
| 342 | usort($aliveKobolds, function ($a, $b) { |
| 343 | // Check for malnourished injury |
| 344 | $aMalnourished = $this->hasInjuryCategory($a, 'malnourished'); |
| 345 | $bMalnourished = $this->hasInjuryCategory($b, 'malnourished'); |
| 346 | |
| 347 | if ($aMalnourished && !$bMalnourished) |
| 348 | return -1; |
| 349 | if (!$aMalnourished && $bMalnourished) |
| 350 | return 1; |
| 351 | |
| 352 | // Both have or both don't have malnourished |
| 353 | // Younger first (lower age = higher priority) |
| 354 | return $a->getAge() - $b->getAge(); |
| 355 | }); |
| 356 | |
| 357 | $fedKobolds = []; |
| 358 | $starvedKobolds = []; |
| 359 | |
| 360 | foreach ($aliveKobolds as $kobold) { |
| 361 | $needed = $this->getFoodRequirement($kobold); |
| 362 | |
| 363 | if ($this->foodCount >= $needed) { |
| 364 | // Fed this kobold |
| 365 | $this->foodCount -= $needed; |
| 366 | $fedKobolds[$kobold->getId()] = $kobold; |
| 367 | |
| 368 | // If malnourished, cure it |
| 369 | if ($this->hasInjuryCategory($kobold, 'malnourished')) { |
| 370 | $this->removeMalnourishedInjury($kobold); |
| 371 | $story .= "{$kobold->getName()} was fed and recovered from malnutrition.\n"; |
| 372 | } |
| 373 | } else { |
| 374 | // Not enough food - starve this kobold |
| 375 | $starvedKobolds[$kobold->getId()] = $kobold; |
| 376 | $this->foodCount = 0; // Use remaining food |
| 377 | |
| 378 | |
| 379 | $malnourishedId = $this->getMalnourishedInjuryId($kobold); |
| 380 | if (!$malnourishedId) { |
| 381 | $kobold->addInjury('malnourished-slight'); |
| 382 | $story .= "{$kobold->getName()} went hungry and became slightly malnourished.\n"; |
| 383 | } else { |
| 384 | if ($malnourishedId === 'malnourished-slight') { |
| 385 | $kobold->removeInjury($malnourishedId); |
| 386 | $kobold->addInjury('malnourished-mild'); |
| 387 | $story .= "{$kobold->getName()} remained hungry and became mildly malnourished.\n"; |
| 388 | } elseif ($malnourishedId === 'malnourished-mild') { |
| 389 | $kobold->removeInjury($malnourishedId); |
| 390 | $kobold->addInjury('malnourished-severe'); |
| 391 | $story .= "{$kobold->getName()} remained hungry and became severely malnourished.\n"; |
| 392 | } elseif ($malnourishedId === 'malnourished-severe') { |
| 393 | $kobold->removeInjury($malnourishedId); |
| 394 | $kobold->addInjury('malnourished-brink'); |
| 395 | $story .= "{$kobold->getName()} is now on the brink of death from starvation.\n"; |
| 396 | } elseif ($malnourishedId === 'malnourished-brink') { |
| 397 | $kobold->die(); |
| 398 | $story .= "{$kobold->getName()} succumbed to starvation.\n"; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Save updated kobolds |
| 405 | foreach ($aliveKobolds as $kobold) { |
| 406 | $kobold->save(); |
| 407 | } |
| 408 | |
| 409 | return $story; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Check if a kobold has an injury in a specific category |
| 414 | */ |
| 415 | private function hasInjuryCategory(Kobold $kobold, $category) |
| 416 | { |
| 417 | $injuries = $kobold->getInjuries(); |
| 418 | foreach ($injuries as $injury) { |
| 419 | if ($injury['category'] === $category) { |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Remove all malnourished injuries from a kobold |
| 428 | */ |
| 429 | private function removeMalnourishedInjury(Kobold $kobold) |
| 430 | { |
| 431 | $injuries = $kobold->getInjuries(); |
| 432 | foreach ($injuries as $injury) { |
| 433 | if ($injury['category'] === 'malnourished') { |
| 434 | $kobold->removeInjury($injury['id']); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Get current malnourished injury id if present |
| 441 | */ |
| 442 | private function getMalnourishedInjuryId(Kobold $kobold) |
| 443 | { |
| 444 | $injuries = $kobold->getInjuries(); |
| 445 | foreach ($injuries as $injury) { |
| 446 | if (($injury['category'] ?? null) === 'malnourished') { |
| 447 | return $injury['id'] ?? null; |
| 448 | } |
| 449 | } |
| 450 | return null; |
| 451 | } |
| 452 | |
| 453 | public function tryMate() |
| 454 | { |
| 455 | $kobolds = array_values($this->getKobolds()); |
| 456 | $count = count($kobolds); |
| 457 | |
| 458 | if ($count === 0) { |
| 459 | return ''; |
| 460 | } |
| 461 | |
| 462 | $roll = rand(1, 30); |
| 463 | if ($roll <= $count) { |
| 464 | return ''; |
| 465 | } |
| 466 | |
| 467 | $males = array_values(array_filter($kobolds, fn($k) => $k->getMale() && $k->getAgeCategory() === 'adult')); |
| 468 | $females = array_values(array_filter($kobolds, fn($k) => !$k->getMale() && $k->getAgeCategory() === 'adult')); |
| 469 | |
| 470 | if (empty($males) || empty($females)) { |
| 471 | return ''; |
| 472 | } |
| 473 | |
| 474 | $pair = null; |
| 475 | $attempts = 20; |
| 476 | while ($attempts-- > 0) { |
| 477 | $male = $males[array_rand($males)]; |
| 478 | $female = $females[array_rand($females)]; |
| 479 | |
| 480 | $maleParents = $male->getParents(); |
| 481 | $femaleParents = $female->getParents(); |
| 482 | |
| 483 | $sharedFather = !empty($maleParents['father_id']) && $maleParents['father_id'] === ($femaleParents['father_id'] ?? null); |
| 484 | $sharedMother = !empty($maleParents['mother_id']) && $maleParents['mother_id'] === ($femaleParents['mother_id'] ?? null); |
| 485 | $isMaleParent = $male->getId() === ($femaleParents['father_id'] ?? null) || $male->getId() === ($femaleParents['mother_id'] ?? null); |
| 486 | $isFemaleParent = $female->getId() === ($maleParents['father_id'] ?? null) || $female->getId() === ($maleParents['mother_id'] ?? null); |
| 487 | |
| 488 | if ($sharedFather || $sharedMother || $isMaleParent || $isFemaleParent) { |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | $pair = [$male, $female]; |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | if (!$pair) { |
| 497 | return ''; |
| 498 | } |
| 499 | |
| 500 | [$male, $female] = $pair; |
| 501 | |
| 502 | if (!$male->checkStat('personality') || !$female->checkStat('personality')) { |
| 503 | return "{$male->getName()} and {$female->getName()} spent some time together this month, but didn't quite hit it off."; |
| 504 | } |
| 505 | |
| 506 | $child = Kobold::generate($this->userId, $this->saveId, -6); |
| 507 | |
| 508 | $inheritRoll = rand(1, 100); |
| 509 | if ($inheritRoll <= 40) { |
| 510 | $child->setSpriteMorph($male->getSpriteMorph()); |
| 511 | } elseif ($inheritRoll <= 80) { |
| 512 | $child->setSpriteMorph($female->getSpriteMorph()); |
| 513 | } |
| 514 | |
| 515 | $blendHex = function ($a, $b) { |
| 516 | $hexToRgb = function ($hex) { |
| 517 | $hex = ltrim($hex, '#'); |
| 518 | return [ |
| 519 | 'r' => hexdec(substr($hex, 0, 2)), |
| 520 | 'g' => hexdec(substr($hex, 2, 2)), |
| 521 | 'b' => hexdec(substr($hex, 4, 2)) |
| 522 | ]; |
| 523 | }; |
| 524 | |
| 525 | $rgbToHex = function ($rgb) { |
| 526 | return sprintf('#%02X%02X%02X', $rgb['r'], $rgb['g'], $rgb['b']); |
| 527 | }; |
| 528 | |
| 529 | $ra = $hexToRgb($a); |
| 530 | $rb = $hexToRgb($b); |
| 531 | $ratio = rand(0, 100) / 100; |
| 532 | |
| 533 | $offset = function () { |
| 534 | return rand(-20, 20); |
| 535 | }; |
| 536 | |
| 537 | $mix = function ($c1, $c2) use ($ratio, $offset) { |
| 538 | $val = (int) round(($c1 * $ratio) + ($c2 * (1 - $ratio)) + $offset()); |
| 539 | return max(0, min(255, $val)); |
| 540 | }; |
| 541 | |
| 542 | $mixed = [ |
| 543 | 'r' => $mix($ra['r'], $rb['r']), |
| 544 | 'g' => $mix($ra['g'], $rb['g']), |
| 545 | 'b' => $mix($ra['b'], $rb['b']) |
| 546 | ]; |
| 547 | |
| 548 | return $rgbToHex($mixed); |
| 549 | }; |
| 550 | |
| 551 | $maleColours = $male->getColours(); |
| 552 | $femaleColours = $female->getColours(); |
| 553 | |
| 554 | $childPrimary = $blendHex($maleColours['primary'], $femaleColours['primary']); |
| 555 | $childSecondary = $blendHex($maleColours['secondary'], $femaleColours['secondary']); |
| 556 | $childTertiary = $blendHex($maleColours['tertiary'], $femaleColours['tertiary']); |
| 557 | |
| 558 | $child->setColours($childPrimary, $childSecondary, $childTertiary); |
| 559 | $child->setParents($male->getId(), $female->getId()); |
| 560 | $child->regenerateSprite(); |
| 561 | |
| 562 | $this->addKobold($child); |
| 563 | |
| 564 | $pairName = $female->getName() . " and " . $male->getName(); |
| 565 | $momName = $female->getName(); |
| 566 | $variations = [ |
| 567 | "{$pairName} spent a lot of time together this month. After a while, {$momName} laid an egg!", |
| 568 | "A quiet bond formed between {$pairName}, and soon {$momName} laid an egg.", |
| 569 | "The tribe whispers as {$pairName} draw close. By month's end, {$momName} laid an egg.", |
| 570 | "Under the clan's watchful eyes, {$pairName} grew inseparable, and {$momName} laid an egg.", |
| 571 | "After many shared moments, {$pairName} welcomed a new egg from {$momName}.", |
| 572 | "A spark passed between {$pairName}. Before the month ended, {$momName} laid an egg.", |
| 573 | "The camp celebrates softly as {$pairName} brought new life—{$momName} laid an egg.", |
| 574 | "Fortunate days came for {$pairName}; the month closes with {$momName} laying an egg.", |
| 575 | "The tribe smiles as {$pairName} found harmony, and {$momName} laid an egg.", |
| 576 | "Among the kobolds, {$pairName} share a rare warmth, and {$momName} laid an egg." |
| 577 | ]; |
| 578 | |
| 579 | return $variations[array_rand($variations)]; |
| 580 | } |
| 581 | |
| 582 | public function endMonth() |
| 583 | { |
| 584 | $this->currentMonth++; |
| 585 | |
| 586 | foreach ($this->kobolds as $kobold) { |
| 587 | $kobold->resetFatigue(); |
| 588 | } |
| 589 | |
| 590 | $seasons = ['spring', 'summer', 'autumn', 'winter']; |
| 591 | |
| 592 | $monthOfYear = (($this->currentMonth - 1) % 12) + 1; |
| 593 | $index = intdiv($monthOfYear - 1, 3); |
| 594 | $pIndex = ($index + 3) % 4; |
| 595 | |
| 596 | $previousSeason = $seasons[$pIndex]; |
| 597 | $this->currentSeason = $seasons[$index]; |
| 598 | |
| 599 | if ($previousSeason == 'spring' && $this->currentSeason == 'summer') { |
| 600 | if (!isset($this->dataStore->wasSowedInSpring) || !$this->dataStore->wasSowedInSpring) { |
| 601 | $noHarvestVariations = [ |
| 602 | "No seeds were sown in spring, so there can be no harvest come autumn.\n", |
| 603 | "With spring left untilled, there can be no harvest in autumn.\n", |
| 604 | "Spring passed without sowing; there can be no harvest this autumn.\n", |
| 605 | "The tribe never sowed in spring, so there can be no harvest later.\n", |
| 606 | "Nothing was planted in spring—there can be no harvest in autumn.\n", |
| 607 | "Spring's fields lay empty, and there can be no harvest ahead.\n", |
| 608 | "No planting happened in spring, so there can be no harvest when autumn arrives.\n" |
| 609 | ]; |
| 610 | $foodStory = $noHarvestVariations[array_rand($noHarvestVariations)]; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | if ( |
| 615 | isset($this->dataStore->wasSowedInSpring) |
| 616 | && $this->dataStore->wasSowedInSpring |
| 617 | && $previousSeason == 'summer' |
| 618 | && $this->currentSeason == 'autumn' |
| 619 | ) { |
| 620 | if (!isset($this->dataStore->wasTendedInSummer) || !$this->dataStore->wasTendedInSummer) { //this check is redundant but whatever |
| 621 | $cropLossVariations = [ |
| 622 | "The fields were not tended in summer, and all of the crops are lost.\n", |
| 623 | "Neglected through summer, all of the crops are lost.\n", |
| 624 | "With no summer care, all of the crops are lost to blight and pests.\n", |
| 625 | "Summer passed without tending the fields, and all of the crops are lost.\n", |
| 626 | "The fields were left to wild growth; all of the crops are lost.\n", |
| 627 | "No hands worked the fields in summer, so all of the crops are lost.\n", |
| 628 | "Left untended through summer, all of the crops are lost.\n" |
| 629 | ]; |
| 630 | $foodStory = $cropLossVariations[array_rand($cropLossVariations)]; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | if ( |
| 635 | isset($this->dataStore->wasTendedInSummer) //this being true means no harvest happened |
| 636 | && $this->dataStore->wasTendedInSummer |
| 637 | && $previousSeason == 'summer' |
| 638 | && $this->currentSeason == 'autumn' |
| 639 | ) { |
| 640 | $cropLossVariations = [ |
| 641 | "The crops stood too long, and winter's cold ruined what should have been gathered.\n", |
| 642 | "Harvest came too late; the first hard freezes destroyed the crops.\n", |
| 643 | "No harvest was taken in time, and the winter cold ruined the fields.\n", |
| 644 | "The fields were left unharvested, and winter's chill destroyed the crops.\n", |
| 645 | "The tribe missed the harvest window, and the cold of winter claimed the crops.\n", |
| 646 | "By the time harvest was attempted, winter had already destroyed the crops.\n", |
| 647 | "The crops were left in the fields until winter, and the cold destroyed them.\n" |
| 648 | ]; |
| 649 | $foodStory = $cropLossVariations[array_rand($cropLossVariations)]; |
| 650 | } |
| 651 | |
| 652 | // Random month events happen here |
| 653 | // After events, consume food |
| 654 | |
| 655 | // age eggs |
| 656 | $foodStory = "A month passes...\n" . $this->consumeFood(); |
| 657 | $foodStory .= "\n"; |
| 658 | |
| 659 | foreach ($this->kobolds as $kobold) { |
| 660 | if ($kobold->getAge() < 0) { |
| 661 | $kobold->incrementAge(); |
| 662 | |
| 663 | if ($kobold->getAge() >= 0) { |
| 664 | $kobold->setName(Kobold::generateRandomName()); |
| 665 | $foodStory .= "An egg hatched! {$kobold->getName()} was born.\n"; |
| 666 | } |
| 667 | |
| 668 | $kobold->save(); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // Age all kobolds |
| 673 | if ($this->currentMonth % 12 === 0) { |
| 674 | $foodStory .= "\nA new year begins.\n"; |
| 675 | |
| 676 | foreach ($this->kobolds as $kobold) { |
| 677 | if ($kobold->getAge() >= 0) { |
| 678 | $kobold->incrementAge(); |
| 679 | |
| 680 | // Check for old-age injuries |
| 681 | if ($kobold->getAge() >= 35 && $this->shouldAddOldAgeInjury($kobold)) { |
| 682 | $oldAgeInjuries = ['bad_back', 'bad_liver', 'poor_eyesight', 'dementia', 'limp', 'poor_hearing', 'chronic_fatigue']; |
| 683 | $injury = $oldAgeInjuries[array_rand($oldAgeInjuries)]; |
| 684 | $kobold->addInjury($injury); |
| 685 | } |
| 686 | |
| 687 | // Old-age death roll: if roll < (age + total injury severity), kobold dies |
| 688 | if ($kobold->getAge() >= 35) { |
| 689 | $roll = rand(1, 100); |
| 690 | $totalSeverity = $kobold->getTotalInjurySeverity(); |
| 691 | if ($roll < ($kobold->getAge() + $totalSeverity)) { |
| 692 | $foodStory .= $kobold->getName() . " dies of old age.\n"; |
| 693 | $kobold->die(); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | $kobold->save(); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | $foodStory .= "\n" . $this->tryMate(); |
| 702 | $foodStory .= "\nIt's {$this->currentSeason}.\n"; |
| 703 | |
| 704 | foreach ($this->kobolds as $kobold) { |
| 705 | if ($kobold->isDead()) { |
| 706 | $foodStory = $foodStory . "\n" . $kobold->getName() . " has died."; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | $this->save(); |
| 711 | |
| 712 | return $foodStory; |
| 713 | } |
| 714 | |
| 715 | private function shouldAddOldAgeInjury(Kobold $kobold) |
| 716 | { |
| 717 | // 1d100 roll under age starting at 35 |
| 718 | $roll = rand(1, 100); |
| 719 | return $roll <= $kobold->getAge(); |
| 720 | } |
| 721 | } |