BlueGit

blue-star tribegen

Files

tribegen / web / src / Events / FarmingEvent.php

FarmingEvent.php

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