| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../src/Events/Event.php'; |
| 4 | require_once __DIR__ . '/../../src/Utilities/GameHelper.php'; |
| 5 | require_once __DIR__ . '/../../src/Classes/Kobold.php'; |
| 6 | |
| 7 | class ExplorationEvent extends Event |
| 8 | { |
| 9 | public function execute() |
| 10 | { |
| 11 | $this->story = "The exploration party ventured forth into uncharted territory...\n\n"; |
| 12 | |
| 13 | $available = $this->tribe->getAliveKobolds(); |
| 14 | |
| 15 | if (empty($available)) { |
| 16 | $this->story .= "But there were no kobolds to send exploring!"; |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Exploration must be started with explicit participants (player-started) |
| 21 | $participantIds = $this->getParticipants(); |
| 22 | if (empty($participantIds) || !is_array($participantIds)) { |
| 23 | throw new \Exception('ExplorationEvent requires a participants array of kobold IDs.'); |
| 24 | } |
| 25 | |
| 26 | $explorers = []; |
| 27 | foreach ($participantIds as $pid) { |
| 28 | $k = $this->tribe->getKobold($pid); |
| 29 | if ($k && !$k->isDead()) |
| 30 | $explorers[] = $k; |
| 31 | } |
| 32 | |
| 33 | if (empty($explorers)) { |
| 34 | throw new \Exception('No valid living kobolds found in participants list.'); |
| 35 | } |
| 36 | |
| 37 | $this->story .= count($explorers) > 1 ? |
| 38 | "A group of " . count($explorers) . " intrepid explorers set out:\n" |
| 39 | : $explorers[0]->getName() . " sets out to explore.\n"; |
| 40 | |
| 41 | if (count($explorers) > 1) { |
| 42 | foreach ($explorers as $explorer) { |
| 43 | $this->story .= "- " . $explorer->getName() . "\n"; |
| 44 | } |
| 45 | $this->story .= "\n"; |
| 46 | } |
| 47 | |
| 48 | // Chance to find food (50% base) |
| 49 | $foodFound = rand(1, 100) <= 50; |
| 50 | if ($foodFound) { |
| 51 | $baseFood = rand(5, 15); |
| 52 | // Intelligence affects food discovery - smarter kobolds find more |
| 53 | $avgIntelligence = array_sum(array_map(function ($k) { |
| 54 | return $k->getStat('intelligence'); |
| 55 | }, $explorers)) / count($explorers); |
| 56 | |
| 57 | $foodBonus = intval($avgIntelligence / 20); // Max +5 food from intelligence |
| 58 | $totalFood = $baseFood + $foodBonus; |
| 59 | |
| 60 | $this->tribe->addFood($totalFood); |
| 61 | $this->story .= "Success! The explorers discovered edible plants and small game.\n"; |
| 62 | $this->story .= "The tribe gained " . $totalFood . " units of food.\n\n"; |
| 63 | } else { |
| 64 | $this->story .= "The terrain was barren. No food was found.\n\n"; |
| 65 | } |
| 66 | |
| 67 | $injuryChance = rand(1, 100) <= 20; |
| 68 | if ($injuryChance && count($explorers) > 0) { |
| 69 | |
| 70 | // Select a random explorer to get injured |
| 71 | $injuredKobold = $explorers[array_rand($explorers)]; |
| 72 | |
| 73 | $this->story .= "Disaster! " . $injuredKobold->getName() . " tripped over a loose stone!\n"; |
| 74 | |
| 75 | $injuredKobold->addInjury("brokenLeg"); |
| 76 | |
| 77 | $this->story .= $injuredKobold->getName() . " broke a leg!\n"; |
| 78 | } |
| 79 | |
| 80 | $stickFound = rand(1, 100) <= 20; |
| 81 | if ($stickFound) { |
| 82 | $bestKobold = GameHelper::findBestKoboldForStat($explorers, "crafting"); |
| 83 | |
| 84 | $success = $bestKobold->checkStat("crafting"); |
| 85 | |
| 86 | if ($success) { |
| 87 | $boneItem = GameHelper::loadItem('bone-setting'); |
| 88 | if ($boneItem) { |
| 89 | $this->tribe->addToStorage($boneItem); |
| 90 | $this->story .= "{$bestKobold->getName()} finds a nice stick and fashions it into a Splint the tribe can use to treat fractures.\n"; |
| 91 | } |
| 92 | } else { |
| 93 | $this->story .= "{$bestKobold->getName()} finds a stick suitable to be a splint, but it breaks as {$bestKobold->he()} tries to craft it.\n"; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | $eggFound = rand(1, 100) <= 3; |
| 98 | if ($eggFound) { |
| 99 | $this->tribe->addKobold(Kobold::generate($this->tribe->getUserId(), $this->tribe->getSaveId(), rand(-6, -1))); |
| 100 | $this->story .= "The party finds a lost Kobold egg... What might be inside?\n"; |
| 101 | } |
| 102 | |
| 103 | $this->story .= "\nThe exploration party returned to the tribe with their findings."; |
| 104 | |
| 105 | return $this->story; |
| 106 | } |
| 107 | } |