BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / FarmingEvent.php

FarmingEvent.php

easier to expand
timfa Tim Falken · pushed by timfa · 9 months ago
d16c25a2 History
FarmingEvent.php 8.3 KiB Download Raw
1<?php
2
3require_once __DIR__ . '/../../src/Events/Event.php';
4require_once __DIR__ . '/../../src/Utilities/GameHelper.php';
5
6class FarmingEvent extends Event
7{
8 public function getMinAge()
9 {
10 return 10;
11 }
12
13 public function getMinParticipants()
14 {
15 return 2;
16 }
17
18 public function getMaxParticipants()
19 {
20 return 6;
21 }
22
23 public function getCost()
24 {
25 switch ($this->tribe->getCurrentSeason()) {
26 case 'spring':
27 return 25;
28 case 'summer':
29 return 15;
30 case 'autumn':
31 return 20;
32 case 'winter':
33 return 60;
34 default:
35 return 60;
36 }
37 }
38
39 public function isAvailable()
40 {
41 if ($this->tribe->getCurrentSeason() !== 'winter') {
42 if ($this->tribe->getCurrentSeason() === 'spring')
43 return !isset($this->tribe->dataStore->wasSowedInSpring) || !$this->tribe->dataStore->wasSowedInSpring;
44
45 if ($this->tribe->getCurrentSeason() === 'summer')
46 return isset($this->tribe->dataStore->wasSowedInSpring) && $this->tribe->dataStore->wasSowedInSpring;
47
48 if ($this->tribe->getCurrentSeason() === 'autumn')
49 return isset($this->tribe->dataStore->wasTendedInSummer) && $this->tribe->dataStore->wasTendedInSummer;
50 }
51
52 // reset values since this part of the code is only ever reached in winter so we might as well.
53 $this->tribe->dataStore->wasSowedInSpring = false;
54 $this->tribe->dataStore->wasTendedInSummer = false;
55 return false;
56 }
57
58 public function spring(array $all, Kobold $bestInt, Kobold $bestVit)
59 {
60 $successes = 0;
61 $this->tribe->dataStore->farmYieldMultiplier = 0.5;
62 $this->tribe->dataStore->wasSowedInSpring = true;
63
64 for ($i = 0; $i < count($all); $i++) {
65 $farmer = $all[$i];
66
67 if ($farmer->checkStat('vitality') || $farmer->checkStat('intelligence')) {
68 $this->tribe->dataStore->farmYieldMultiplier += 1;
69 $successes++;
70 }
71
72 if (!$farmer->checkStat('will') && rand(1, 100) <= 30) {
73 if ($farmer->addInjury("smallCut"))
74 $this->story .= "{$farmer->getName()} had an accident plowing the field, cutting {$farmer->his()} leg with the hoe.\n";
75 break;
76 }
77 }
78
79 $word = "terribly";
80 $perc = ($successes / $this->getMaxParticipants());
81
82 if ($perc >= 0.95)
83 $word = "perfectly";
84 else if ($perc >= 0.75)
85 $word = "great";
86 else if ($perc >= 0.5)
87 $word = "well";
88 else if ($perc >= 0.25)
89 $word = "okay";
90 else if ($perc >= 0.1)
91 $word = "badly";
92
93 $this->story .= "Tilling and sowing the fields went {$word}.\n";
94
95 $this->tribe->dataStore->wasSowedInSpring = true;
96 }
97
98 public function summer(array $all, Kobold $bestInt, Kobold $bestVit)
99 {
100 $successes = 0;
101 $this->tribe->dataStore->wasSowedInSpring = false;
102 $this->tribe->dataStore->wasTendedInSummer = true;
103
104 for ($i = 0; $i < count($all); $i++) {
105 $farmer = $all[$i];
106
107 if ($farmer->checkStat('vitality') || $farmer->checkStat('intelligence')) {
108 $this->tribe->dataStore->farmYieldMultiplier += 0.5;
109 $successes++;
110 }
111
112 if (!$farmer->checkStat('vitality') && rand(1, 100) <= 25) {
113 if ($farmer->addInjury("sunburn"))
114 $this->story .= "{$farmer->getName()} got badly sunburned while working in the fields under the hot sun.\n";
115 break;
116 }
117
118 if ($farmer->checkStat('intelligence') && rand(1, 100) <= 40) {
119 $herbs = GameHelper::loadItem('healing-herbs');
120 $this->tribe->addToStorage($herbs, rand(1, 3));
121 $this->story .= "{$farmer->getName()} managed to grow some useful healing herbs while tending the crops.\n";
122 break;
123 }
124 }
125
126 $word = "terribly";
127 $perc = ($successes / $this->getMaxParticipants());
128
129 if ($perc >= 0.95)
130 $word = "perfectly";
131 else if ($perc >= 0.75)
132 $word = "great";
133 else if ($perc >= 0.5)
134 $word = "well";
135 else if ($perc >= 0.25)
136 $word = "okay";
137 else if ($perc >= 0.1)
138 $word = "badly";
139
140 $this->story .= "Tending to the crops went {$word}.\n";
141 }
142
143 public function autumn(array $all, Kobold $bestInt, Kobold $bestVit)
144 {
145 $harvestBaseMultiplier = 3.0;
146 $this->tribe->dataStore->wasTendedInSummer = false;
147
148 $multiplier = $this->tribe->dataStore->farmYieldMultiplier ?? 1.0;
149 if (!$bestInt->checkStat('intelligence')) {
150 $this->story .= "Unfortunately, {$bestInt->getName()} made several mistakes in {$bestInt->his()} plan to harvest the crops.\n";
151 $this->story .= "Despite {$bestVit->getName()}'s best efforts, many crops were lost.\n";
152 $multiplier *= floatval(rand(2, 6)) * 0.1;
153 }
154
155 $totalFood = 0;
156 $mostFood = 0;
157 $mostFarmedBy = '';
158
159 for ($i = 0; $i < count($all); $i++) {
160 $farmer = $all[$i];
161
162 $farmed = rand(0, $this->tribe->foodRequirements[$farmer->getAgeCategory()] * $harvestBaseMultiplier) * $multiplier;
163 $totalFood += $farmed;
164
165 if ($farmed > $mostFood) {
166 $mostFood = $farmed;
167 $mostFarmedBy = $farmer->getName();
168 }
169 }
170
171 if ($mostFarmedBy == $bestVit->getName()) {
172 $this->story .= "{$bestVit->getName()} worked the hardest and managed to harvest the most food for the tribe!\n";
173 } else {
174 $this->story .= "Despite {$bestVit->getName()} working the hardest out of everyone, it was {$mostFarmedBy} who harvested the most food for the tribe.\n";
175 }
176
177
178 $reason = $this->tribe->modifyFoodGain($totalFood);
179 if ($reason === 'draught') {
180 $this->story .= "The drought wilted the crops, halving what could be gathered.\n";
181 } elseif ($reason === 'heatWave') {
182 $this->story .= "The heat wilted the crops, halving what could be gathered.\n";
183 }
184 $months = $this->tribe->foodMonthsLeft($totalFood);
185 $this->tribe->addFood($totalFood);
186
187 $max = $this->tribe->foodRequirements[GameHelper::getAgeCategory("adult")] * $harvestBaseMultiplier * $multiplier * $this->getMaxParticipants();
188
189 $word = "terribly";
190 $perc = ($totalFood / $max);
191
192 if ($perc >= 0.95)
193 $word = "perfectly";
194 else if ($perc >= 0.75)
195 $word = "great";
196 else if ($perc >= 0.5)
197 $word = "well";
198 else if ($perc >= 0.25)
199 $word = "okay";
200 else if ($perc >= 0.1)
201 $word = "badly";
202
203 $this->story .= "Tending to the crops went {$word}.\n";
204
205 if ($months < 1)
206 $this->story .= "The tribe gained only {$this->tribe->foodTimeStr($totalFood)} worth of food from the harvest.\n";
207 else {
208 if ($months == 1)
209 $this->story .= "The tribe gained only a month worth of food from the harvest.\n";
210 else
211 $this->story .= "The tribe gained " . $months . " months worth of food from the harvest!\n";
212 }
213 }
214
215 public function execute()
216 {
217 $participantIds = $this->getParticipants();
218 $explorers = [];
219 foreach ($participantIds as $pid) {
220 $k = $this->tribe->getKobold($pid);
221 if ($k && !$k->isDead())
222 $explorers[] = $k;
223 }
224
225 $leader = GameHelper::findBestKoboldForStat($explorers, "intelligence");
226 $hardestWorker = GameHelper::findBestKoboldForStat($explorers, "vitality");
227
228 $this->story = "The farmers tended to the fields. {$leader->getName()} took the lead.\n";
229
230 if ($this->tribe->getCurrentSeason() === 'spring')
231 $this->spring($explorers, $leader, $hardestWorker);
232 else if ($this->tribe->getCurrentSeason() === 'summer')
233 $this->summer($explorers, $leader, $hardestWorker);
234 else if ($this->tribe->getCurrentSeason() === 'autumn')
235 $this->autumn($explorers, $leader, $hardestWorker);
236
237 return $this->story;
238 }
239}