large flood ruins the farms, weighted random events
@@ -826,8 +826,27 @@ class Tribe
826
826
827
827
// Select events randomly until cost exceeds maxCost
828
828
while ($totalCost <= $maxCost && !empty($availableEventClasses)) {
829
// Pick a random event class
830
$randomClass = $availableEventClasses[array_rand($availableEventClasses)];
829
// Pick a weighted random event class (lower cost = higher weight)
830
$weights = [];
831
$weightTotal = 0;
832
foreach ($availableEventClasses as $className) {
833
$tempEvent = new $className($this);
834
$eventCost = max(1, (int) $tempEvent->getCost());
835
$weight = 1 / $eventCost;
836
$weights[] = $weight;
837
$weightTotal += $weight;
838
}
839
840
$roll = (mt_rand() / mt_getrandmax()) * $weightTotal;
841
$running = 0;
842
$randomClass = $availableEventClasses[0];
843
foreach ($availableEventClasses as $index => $className) {
844
$running += $weights[$index];
845
if ($roll <= $running) {
846
$randomClass = $className;
847
break;
848
}
849
}
831
850
832
851
// Create new instance
833
852
$event = new $randomClass($this);
@@ -826,8 +826,27 @@ class Tribe826
826
827
// Select events randomly until cost exceeds maxCost827
// Select events randomly until cost exceeds maxCost828
while ($totalCost <= $maxCost && !empty($availableEventClasses)) {828
while ($totalCost <= $maxCost && !empty($availableEventClasses)) {829
// Pick a random event class829
// Pick a weighted random event class (lower cost = higher weight)830
$randomClass = $availableEventClasses[array_rand($availableEventClasses)];830
$weights = [];831
$weightTotal = 0;832
foreach ($availableEventClasses as $className) {833
$tempEvent = new $className($this);834
$eventCost = max(1, (int) $tempEvent->getCost());835
$weight = 1 / $eventCost;836
$weights[] = $weight;837
$weightTotal += $weight;838
}839
840
$roll = (mt_rand() / mt_getrandmax()) * $weightTotal;841
$running = 0;842
$randomClass = $availableEventClasses[0];843
foreach ($availableEventClasses as $index => $className) {844
$running += $weights[$index];845
if ($roll <= $running) {846
$randomClass = $className;847
break;848
}849
}831
850
832
// Create new instance851
// Create new instance833
$event = new $randomClass($this);852
$event = new $randomClass($this);@@ -21,7 +21,7 @@ class LargeFloodEvent extends MonthEndEvent
21
21
22
22
public function getCost()
23
23
{
24
return 85;
24
return 125;
25
25
}
26
26
27
27
public function isAvailable()
@@ -46,6 +46,16 @@ class LargeFloodEvent extends MonthEndEvent
46
46
}
47
47
}
48
48
49
$harvestText = '';
50
if (!empty($this->tribe->dataStore->wasTendedInSummer)) {
51
$harvestText = "The flood ruined the ready harvest before it could be gathered.";
52
$this->tribe->dataStore->wasTendedInSummer = false;
53
$this->tribe->dataStore->wasSowedInSpring = false;
54
} elseif (!empty($this->tribe->dataStore->wasSowedInSpring)) {
55
$harvestText = "The flood washed out the planted fields before they could grow.";
56
$this->tribe->dataStore->wasSowedInSpring = false;
57
}
58
49
59
$alive = array_values($this->tribe->getAliveKobolds());
50
60
$drowned = [];
51
61
if (!empty($alive)) {
@@ -62,6 +72,9 @@ class LargeFloodEvent extends MonthEndEvent
62
72
63
73
$drownedNames = array_map(fn($k) => $k->getName(), $drowned);
64
74
$lossText = $foodLost > 0 ? "The flood ruined {$this->tribe->foodTimeStr($foodLost)} of food." : "Food stores were battered but not fully ruined.";
75
if ($harvestText !== '') {
76
$lossText .= " {$harvestText}";
77
}
65
78
$drownText = empty($drownedNames)
66
79
? "Miraculously, no one was swept away."
67
80
: "The waters took " . implode(', ', $drownedNames) . ", and the tribe grieved as the flood passed.";
@@ -21,7 +21,7 @@ class LargeFloodEvent extends MonthEndEvent21
21
22
public function getCost()22
public function getCost()23
{23
{24
return 85;24
return 125;25
}25
}26
26
27
public function isAvailable()27
public function isAvailable()@@ -46,6 +46,16 @@ class LargeFloodEvent extends MonthEndEvent46
}46
}47
}47
}48
48
49
$harvestText = '';50
if (!empty($this->tribe->dataStore->wasTendedInSummer)) {51
$harvestText = "The flood ruined the ready harvest before it could be gathered.";52
$this->tribe->dataStore->wasTendedInSummer = false;53
$this->tribe->dataStore->wasSowedInSpring = false;54
} elseif (!empty($this->tribe->dataStore->wasSowedInSpring)) {55
$harvestText = "The flood washed out the planted fields before they could grow.";56
$this->tribe->dataStore->wasSowedInSpring = false;57
}58
49
$alive = array_values($this->tribe->getAliveKobolds());59
$alive = array_values($this->tribe->getAliveKobolds());50
$drowned = [];60
$drowned = [];51
if (!empty($alive)) {61
if (!empty($alive)) {@@ -62,6 +72,9 @@ class LargeFloodEvent extends MonthEndEvent62
72
63
$drownedNames = array_map(fn($k) => $k->getName(), $drowned);73
$drownedNames = array_map(fn($k) => $k->getName(), $drowned);64
$lossText = $foodLost > 0 ? "The flood ruined {$this->tribe->foodTimeStr($foodLost)} of food." : "Food stores were battered but not fully ruined.";74
$lossText = $foodLost > 0 ? "The flood ruined {$this->tribe->foodTimeStr($foodLost)} of food." : "Food stores were battered but not fully ruined.";75
if ($harvestText !== '') {76
$lossText .= " {$harvestText}";77
}65
$drownText = empty($drownedNames)78
$drownText = empty($drownedNames)66
? "Miraculously, no one was swept away."79
? "Miraculously, no one was swept away."67
: "The waters took " . implode(', ', $drownedNames) . ", and the tribe grieved as the flood passed.";80
: "The waters took " . implode(', ', $drownedNames) . ", and the tribe grieved as the flood passed.";