BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / HuntingEvent.php

HuntingEvent.php

easier to expand
timfa Tim Falken · pushed by timfa · 9 months ago
d16c25a2 History
HuntingEvent.php 8.9 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/Event.php';
4require_once __DIR__ . '/../../src/Classes/Kobold.php';
5require_once __DIR__ . '/../../src/Utilities/GameHelper.php';
6
7class HuntingEvent extends Event
8{
9 public function getMinAge()
10 {
11 return 17;
12 }
13
14 public function getMinParticipants()
15 {
16 return 2;
17 }
18
19 public function getMaxParticipants()
20 {
21 return 5;
22 }
23
24 public function getCost()
25 {
26 return 25;
27 }
28
29 public function isAvailable()
30 {
31 return true;
32 }
33
34 private function runHunt(array $hunters, string $quarry, int $fullMin, int $fullMax, int $partialMin, int $partialMax)
35 {
36 $totalFood = 0;
37 $successes = 0;
38
39 $injuries = ['smallCut', 'brokenLeg', 'limp'];
40
41 foreach ($hunters as $hunter) {
42 $tracked = $hunter->checkStat('intelligence');
43 $downed = $hunter->checkStat('fighting');
44
45 if ($tracked && $downed) {
46 $totalFood += rand($fullMin, $fullMax);
47 $successes++;
48 } elseif ($tracked || $downed) {
49 $totalFood += rand($partialMin, $partialMax);
50 }
51
52 if (!$hunter->checkStat('vitality') && rand(1, 100) <= 15) {
53 $injury = $injuries[array_rand($injuries)];
54 if ($hunter->addInjury($injury))
55 $this->story .= "{$hunter->getName()} was hurt while chasing {$quarry}, but managed to limp back.\n";
56 }
57 }
58
59 if ($successes === 0) {
60 $this->story .= "The party failed to bring down any {$quarry}.\n";
61 } else {
62 $this->story .= "The party brought down {$quarry} and returned with food.\n";
63 }
64
65 return $totalFood;
66 }
67
68 private function spring(array $hunters, Kobold $bestTracker, Kobold $bestFighter, Kobold $toughest)
69 {
70 $this->story .= "Spring game was plentiful—rabbits and ground birds darted through the brush.\n";
71
72 $totalFood = $this->runHunt($hunters, 'small game', 5, 8, 1, 2);
73
74 if ($bestTracker->checkStat('intelligence') && rand(1, 100) <= 35) {
75 $herbs = GameHelper::loadItem('healing-herbs');
76 if ($herbs) {
77 $this->tribe->addToStorage($herbs, rand(1, 2));
78 $this->story .= "{$bestTracker->getName()} tracked fresh growth and gathered healing herbs along the way.\n";
79 }
80 }
81
82 if (!$toughest->checkStat('vitality') && rand(1, 100) <= 25) {
83 if ($toughest->addInjury('smallCut'))
84 $this->story .= "A thicket tore at {$toughest->getName()}'s arms, leaving a nasty cut.\n";
85 }
86
87 return $totalFood;
88 }
89
90 private function summer(array $hunters, Kobold $bestTracker, Kobold $bestFighter, Kobold $toughest)
91 {
92 $this->story .= "Summer trails led to boar and wild goats, but the heat was punishing.\n";
93
94 $totalFood = $this->runHunt($hunters, 'boar and goats', 6, 12, 1, 3);
95
96 if ($bestFighter->checkStat('fighting') && rand(1, 100) <= 30) {
97 $bonus = rand(3, 6);
98 $totalFood += $bonus;
99 $this->story .= "{$bestFighter->getName()} brought down a stubborn boar, adding extra meat.\n";
100 }
101
102 if (!$toughest->checkStat('vitality') && rand(1, 100) <= 25) {
103 if ($toughest->addInjury('brokenLeg')) {
104 $this->story .= "A charging beast knocked {$toughest->getName()} down, breaking a leg.\n";
105 }
106 }
107
108 return $totalFood;
109 }
110
111 private function autumn(array $hunters, Kobold $bestTracker, Kobold $bestFighter, Kobold $toughest)
112 {
113 $this->story .= "Autumn brought fattened deer and migrating herds to track.\n";
114
115 $totalFood = $this->runHunt($hunters, 'deer', 5, 9, 2, 4);
116
117 if ($bestTracker->checkStat('intelligence') && rand(1, 100) <= 35) {
118 $bonus = rand(3, 8);
119 $totalFood += $bonus;
120 $this->story .= "{$bestTracker->getName()} followed fresh tracks to a second herd.\n";
121 }
122
123 if (!$toughest->checkStat('vitality') && rand(1, 100) <= 20) {
124 if ($toughest->addInjury('limp'))
125 $this->story .= "A hidden root tripped {$toughest->getName()}, leaving {$toughest->him()} limping.\n";
126 }
127
128 return $totalFood;
129 }
130
131 private function winter(array $hunters, Kobold $bestTracker, Kobold $bestFighter, Kobold $toughest)
132 {
133 $this->story .= "Winter was harsh, and only lean game moved across the snow.\n";
134
135 $totalFood = $this->runHunt($hunters, 'winter game', 2, 6, 0, 1);
136
137 if ($bestFighter->checkStat('fighting') && rand(1, 100) <= 25) {
138 $bonus = rand(2, 4);
139 $totalFood += $bonus;
140 $this->story .= "{$bestFighter->getName()} felled a tough quarry in the cold, adding a little extra meat.\n";
141 }
142
143 if (!$toughest->checkStat('vitality') && rand(1, 100) <= 30) {
144 if ($toughest->addInjury('smallCut'))
145 $this->story .= "Freezing winds numbed {$toughest->getName()}'s hands, and a slip left a painful cut.\n";
146 }
147
148 return $totalFood;
149 }
150
151 public function execute()
152 {
153 $this->story = "The hunting party set out into the wilderness...\n\n";
154
155 $participantIds = $this->getParticipants();
156 $hunters = [];
157 foreach ($participantIds as $pid) {
158 $k = $this->tribe->getKobold($pid);
159 if ($k && !$k->isDead())
160 $hunters[] = $k;
161 }
162
163 if (empty($hunters)) {
164 $this->story .= "But there were no kobolds fit to hunt!";
165 return $this->story;
166 }
167
168 if (count($hunters) > 1) {
169 $this->story .= "A group of " . count($hunters) . " hunters set out:\n";
170 foreach ($hunters as $hunter) {
171 $this->story .= "- " . $hunter->getName() . "\n";
172 }
173 $this->story .= "\n";
174 } else {
175 $this->story .= $hunters[0]->getName() . " set out alone.\n\n";
176 }
177
178 $bestTracker = GameHelper::findBestKoboldForStat($hunters, 'intelligence');
179 $bestFighter = GameHelper::findBestKoboldForStat($hunters, 'fighting');
180 $toughest = GameHelper::findBestKoboldForStat($hunters, 'vitality');
181
182 $this->story .= "{$bestTracker->getName()} tracked the trail while {$bestFighter->getName()} led the strike.\n";
183
184 $season = $this->tribe->getCurrentSeason();
185 $totalFood = 0;
186
187 if ($season === 'spring') {
188 $totalFood = $this->spring($hunters, $bestTracker, $bestFighter, $toughest);
189 } elseif ($season === 'summer') {
190 $totalFood = $this->summer($hunters, $bestTracker, $bestFighter, $toughest);
191 } elseif ($season === 'autumn') {
192 $totalFood = $this->autumn($hunters, $bestTracker, $bestFighter, $toughest);
193 } else {
194 $totalFood = $this->winter($hunters, $bestTracker, $bestFighter, $toughest);
195 }
196
197 if ($totalFood > 0) {
198 $reason = $this->tribe->modifyFoodGain($totalFood);
199 if ($reason === 'draught') {
200 $this->story .= "The drought thinned the game, cutting the haul in half.\n";
201 } elseif ($reason === 'heatWave') {
202 $this->story .= "The heat slowed the hunters, cutting the haul in half.\n";
203 }
204 $months = $this->tribe->foodMonthsLeft($totalFood);
205 $this->tribe->addFood($totalFood);
206
207 if ($months > 0) {
208 if ($months == 1)
209 $this->story .= "The tribe gained a month worth of food from the hunt.\n";
210 else
211 $this->story .= "The tribe gained {$months} months worth of food from the hunt.\n";
212 } else
213 $this->story .= "The hunt brought back {$this->tribe->foodTimeStr($totalFood)} worth of food.\n";
214 } else {
215 $this->story .= "The hunt brought back no usable food.\n";
216 }
217
218 if (rand(1, 100) <= 20) {
219 $lucky = $hunters[array_rand($hunters)];
220 if ($lucky->checkStat('intelligence')) {
221 $splint = GameHelper::loadItem('bone-setting');
222 if ($splint) {
223 $this->tribe->addToStorage($splint);
224 $this->story .= "{$lucky->getName()} found a sturdy bone and crafted a splint for the tribe.\n";
225 }
226 }
227 }
228
229 if (rand(1, 100) <= 20) {
230 $unlucky = $hunters[array_rand($hunters)];
231 if (!$unlucky->checkStat('vitality')) {
232 $injuries = ['smallCut', 'brokenLeg', 'limp'];
233 $injury = $injuries[array_rand($injuries)];
234 if ($unlucky->addInjury($injury)) {
235 $this->story .= "Bad luck struck and {$unlucky->getName()} was injured on the way back.\n";
236 }
237 }
238 }
239
240 $this->story .= "\nThe hunters returned to the tribe.";
241
242 return $this->story;
243 }
244}