-
Notifications
You must be signed in to change notification settings - Fork 2
/
randomEvents.php
179 lines (147 loc) · 4.72 KB
/
randomEvents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
* randomEvents.php
*
* Generates random events during travel.
*
* Inputs:
* - Query Param - startLocation - The number of miles before traveling today
* - Query Param - endLocation - The number of miles after traveling today
*/
include "classes.php";
session_start();
include "scoresAndDeaths.php";
$maxEvents = 3; # For fairness, make sure no more than 3 events happen at the same time
$events = array();
$start = intval($_GET["startLocation"]);
$end = intval($_GET["endLocation"]);
# Weather changes
$month = $_SESSION["playerJourney"]->_month;
# It's more likely to rain in spring & fall
if (( $month >= 2 && $month < 5 ) || ( $month >= 8 && $month < 11))
{
$rainMultiplier = 1.5;
}
else
{
$rainMultiplier = 1;
}
# The closer it is to summer, the more likely it'll be that it's hot
$summerDistance = abs( 5 - $month );
$rainProbability = rand(1, 100) * $rainMultiplier;
$hotProbability = rand(1, 100) * (5 - $summerDistance) / 2.5;
$coldProbability = rand(1, 100) * $summerDistance / 2.5;
$sunnyProbability = rand(1,100);
$newWeather = "";
$highestProb = -1;
$weatherProbabilities = array("rainy" => $rainProbability, "hot" => $hotProbability,
"cold" => $coldProbability, "sunny" => $sunnyProbability);
foreach ($weatherProbabilities as $weatherKey => $weatherValue)
{
if ($weatherValue > $highestProb)
{
$newWeather = $weatherKey;
$highestProb = $weatherValue;
}
}
switch($newWeather)
{
case "rainy":
$slowFactor = 1.25;
break;
case "cold":
$slowFactor = 2.0;
break;
default:
$slowFactor = 1.0;
break;
}
if ($newWeather != $_SESSION["playerJourney"]->_weather)
{
$_SESSION["playerJourney"]->_weather = $newWeather;
$maxEvents--;
$events["weather_change"] =
array("weather" => $newWeather, "slow_factor" => $slowFactor);
}
# Thieves stealing
if ($maxEvents > 0)
{
$thiefSteal = rand(1, 50);
if ($thiefSteal == 50)
{
$maxEvents--;
$itemStolen = rand(0, 7);
$maxStolen = array(50, 100, 30, 4, 1, 1, 1, 3);
$amountStolen = rand(1, $maxStolen[$itemStolen]);
$amountStolen = -$_SESSION["playParty"]->_supplies->setItem($itemStolen, -$amountStolen);
$events["thief_steal"] = array("id" => $itemStolen, "qty" => $amountStolen);
}
}
# Wagon parts breaking
if ($maxEvents > 0)
{
$partBreaks = rand(1, 300);
if ($partBreaks == 300)
{
$maxEvents--;
$brokenPart = rand(0,2);
$_SESSION["playParty"]->_supplies->setItem(4 + $brokenPart, -1);
$events["wagon_broke"] = array("wagon_part", $brokenPart);
}
}
# Finding a wagon
if ($maxEvents > 0)
{
$findWagon = rand(1, 50);
if ($findWagon == 50)
{
$maxEvents--;
$foundItem = rand(0, 7);
$maxFound = array(50, 50, 30, 4, 1, 1, 1, 1);
$amountFound = rand(1, $maxFound[$foundItem]);
$_SESSION["playParty"]->_supplies->setItem($foundItem, $amountFound);
$events["find_wagon"] = array("id" => $foundItem, "qty" => $amountFound);
}
}
# Grave marker
$db = new OregonTrailDatabase("eritte2", "eritte2");
if ($db->connect())
{
$graveStone = $db->getDeath($start, $end);
if ($graveStone)
{
$events["grave_marker"] = $graveStone;
}
}
# Getting sick
if ($maxEvents > 0)
{
# Higher paces are detrimental to your health
$paceMultiplier = ($_SESSION["playerJourney"]->_speed - 10) / 10;
# Bad weather + poor clothes choices = worse health
$weatherMultiplier = $_SESSION["playerJourney"]->_weather == "sunny"? 1 : 1.5 +
($_SESSION["playParty"]->_livingMembers * 2 - $_SESSION["playParty"]->_supplies->_clothes);
$sick_chance = rand(1,50) * ($paceMultiplier + $weatherMultiplier);
# You get sick if your sick chance is greater than 199
if ($sick_chance > 199)
{
$lowestHealth = 150;
$personIndex = -1;
# Person with the lowest health gets sick
foreach ($_SESSION["playParty"]->_members as $index => $member)
{
if ($member->_alive && $member->_health < $lowestHealth)
{
$personIndex = $index;
$lowestHealth = $member->_health;
}
}
$sickNames = array("cholera", "dysentery", "common cold", "influenza", "diptheria", "measles", "typhoid fever", "cancer");
$sickName = rand(0, count($sickNames) - 1);
$sickDamage = $paceMultiplier * $weatherMultiplier * 2;
$_SESSION["playParty"]->_members[$personIndex]->catchCold($sickNames[$sickName], $sickDamage);
$events["got_sick"] = array("party_member" => $_SESSION["playParty"]->_members[$personIndex]->_name,
"sick_name" => $sickNames[$sickName]);
}
}
echo json_encode($events);