BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / ExplorationEvent.php

ExplorationEvent.php

event selection ui
timfa Tim Falken · pushed by timfa · 9 months ago
c37c9e3f History
ExplorationEvent.php 4.9 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/Event.php';
4require_once __DIR__ . '/../../src/Utilities/GameHelper.php';
5
6class ExplorationEvent extends Event
7{
8 public function execute()
9 {
10 $this->story = "The exploration party ventured forth into uncharted territory...\n\n";
11
12 $available = $this->tribe->getAliveKobolds();
13
14 if (empty($available)) {
15 $this->story .= "But there were no kobolds to send exploring!";
16 return;
17 }
18
19 // Exploration must be started with explicit participants (player-started)
20 $participantIds = $this->getParticipants();
21 if (empty($participantIds) || !is_array($participantIds)) {
22 throw new \Exception('ExplorationEvent requires a participants array of kobold IDs.');
23 }
24
25 $explorers = [];
26 foreach ($participantIds as $pid) {
27 $k = $this->tribe->getKobold($pid);
28 if ($k && !$k->isDead())
29 $explorers[] = $k;
30 }
31
32 if (empty($explorers)) {
33 throw new \Exception('No valid living kobolds found in participants list.');
34 }
35
36 $this->story .= "A group of " . count($explorers) . " intrepid explorers set out:\n";
37 foreach ($explorers as $explorer) {
38 $this->story .= "- " . $explorer->getName() . " (Intelligence: " . GameHelper::getStatLabel($explorer->getStat('intelligence')) . ")\n";
39 }
40 $this->story .= "\n";
41
42 // Chance to find food (50% base)
43 $foodFound = rand(1, 100) <= 50;
44 if ($foodFound) {
45 $baseFood = rand(5, 15);
46 // Intelligence affects food discovery - smarter kobolds find more
47 $avgIntelligence = array_sum(array_map(function ($k) {
48 return $k->getStat('intelligence');
49 }, $explorers)) / count($explorers);
50 $foodBonus = intval($avgIntelligence / 20); // Max +5 food from intelligence
51 $totalFood = $baseFood + $foodBonus;
52
53 $this->tribe->addFood($totalFood);
54 $this->story .= "Success! The explorers discovered edible plants and small game.\n";
55 $this->story .= "The tribe gained " . $totalFood . " units of food.\n\n";
56 } else {
57 $this->story .= "The terrain was barren. No food was found.\n\n";
58 }
59
60 // Chance for injury (large chance for testing) - someone trips on loose stone
61 $injuryChance = rand(1, 100) <= 70; // higher for testing
62 if ($injuryChance && count($explorers) > 0) {
63 // Vitality affects injury chance - higher vitality = less likely to be injured
64 $avgVitality = array_sum(array_map(function ($k) {
65 return $k->getStat('vitality');
66 }, $explorers)) / count($explorers);
67 $injuryProbability = (100 - $avgVitality) / 100;
68
69 if (rand(1, 100) / 100 <= $injuryProbability) {
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 // Try to load a predefined injury (brokenLeg) from assets
76 $inj = GameHelper::loadInjury('brokenLeg');
77 if ($inj) {
78 $inj['kobold_id'] = $injuredKobold->getId();
79 $inj['id'] = uniqid('inj_');
80 $injuries = $injuredKobold->getInjuries();
81 $injuries[] = $inj;
82 $injuredKobold->setInjuries($injuries);
83
84 $this->story .= $injuredKobold->getName() . " suffered a " . ($inj['injury_type'] ?? 'broken leg') . " (severity: " . ($inj['severity'] ?? 1) . "/10).\n";
85 } else {
86 // Fallback to simple injury
87 $newInjury = [
88 'id' => uniqid('inj_'),
89 'kobold_id' => $injuredKobold->getId(),
90 'injury_type' => 'broken_leg',
91 'category' => 'fracture',
92 'severity' => rand(3, 7),
93 'created_at' => date('Y-m-d H:i:s')
94 ];
95 $injuries = $injuredKobold->getInjuries();
96 $injuries[] = $newInjury;
97 $injuredKobold->setInjuries($injuries);
98
99 $this->story .= $injuredKobold->getName() . " suffered a broken leg (severity: " . $newInjury['severity'] . "/10).\n";
100 }
101 }
102 }
103
104 // For testing: add a 'bone-setting' item to tribe storage so player can cure fractures
105 $boneItem = GameHelper::loadItem('bone-setting');
106 if ($boneItem) {
107 $this->tribe->addToStorage($boneItem);
108 $this->story .= "The explorers also salvaged a Bone-setting kit and brought it back to storage.\n";
109 }
110
111 $this->story .= "\nThe exploration party returned to the tribe with their findings.";
112
113 return $this->story;
114 }
115}