| 1 |
--[[ Copyright (c) 2024 Toby "tobylane" |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
--! Manages all earthquakes in the world |
| 22 |
class "Earthquake" |
| 23 |
|
| 24 |
---@type Earthquake |
| 25 |
local Earthquake = _G["Earthquake"] |
| 26 |
|
| 27 |
--[[ |
| 28 |
All fields relating to the current or next earthquake: |
| 29 |
active (boolean) If there is a current earthquake, after start_day of start_month |
| 30 |
has passed, during the warning_length, warning_period or damage timers |
| 31 |
next_planned (boolean) If the next earthquake has been planned/generated |
| 32 |
start_month (integer) The month the earthquake warning is triggered (1-12) |
| 33 |
start_day (integer) The day of the month the earthquake warning is triggered (1-31) |
| 34 |
size (integer) The amount of damage the earthquake causes (1-9) |
| 35 |
remaining_damage (integer) The amount of damage this earthquake has yet to inflict (1-9) |
| 36 |
damage_timer (integer) The number of hours until the earthquake next inflicts damage if active (0-16) |
| 37 |
warning_timer (integer) The number of hours left until the real damaging earthquake begins (0-600) |
| 38 |
current_map_earthquake (integer) The number of earthquakes that have happened on this map so far |
| 39 |
disabled (boolean) If earthquakes have been disabled by the cheat menu |
| 40 |
--]] |
| 41 |
|
| 42 |
local damage_time = 16 -- Hours between each damage caused by an earthquake |
| 43 |
local warning_period = 600 -- Hours between warning and main quakes |
| 44 |
local warning_length = 25 -- Length of early warning quake |
| 45 |
|
| 46 |
local month_length = { |
| 47 |
31, -- Jan |
| 48 |
28, -- Feb (29 in leap years, but TH doesn't have leap years) |
| 49 |
31, -- Mar |
| 50 |
30, -- Apr |
| 51 |
31, -- May |
| 52 |
30, -- Jun |
| 53 |
31, -- Jul |
| 54 |
31, -- Aug |
| 55 |
30, -- Sep |
| 56 |
31, -- Oct |
| 57 |
30, -- Nov |
| 58 |
31, -- Dec |
| 59 |
} |
| 60 |
|
| 61 |
--! Track earthquakes in the world |
| 62 |
--!param world (table) Owned by this world |
| 63 |
--!param convert (boolean) If true, world is upgraded from before this Earthquake class |
| 64 |
function Earthquake:Earthquake(world, convert) |
| 65 |
self.world = world |
| 66 |
if not convert then |
| 67 |
self.current_map_earthquake = 0 |
| 68 |
self:nextEarthquake() |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
--! Perform actions to simulate an active earthquake. |
| 73 |
function Earthquake:tick() |
| 74 |
if not self.active or self.disabled then return end |
| 75 |
local hosp = self.world:getLocalPlayerHospital() |
| 76 |
-- Check if this is the day that the earthquake is supposed to stop |
| 77 |
if self.remaining_damage == 0 then |
| 78 |
self.active = false |
| 79 |
hosp:tickEarthquake("end") |
| 80 |
-- If the earthquake measured more than 7 on the richter scale, tell the user about it |
| 81 |
if self.size > 7 then |
| 82 |
hosp:giveAdvice({_A.earthquake.ended:format(math.floor(self.size))}) |
| 83 |
end |
| 84 |
|
| 85 |
-- Set up the next earthquake date |
| 86 |
self:nextEarthquake() |
| 87 |
return |
| 88 |
end |
| 89 |
|
| 90 |
-- Start of warning quake |
| 91 |
if self.warning_timer == warning_period then |
| 92 |
self.next_planned = false |
| 93 |
hosp:tickEarthquake("warning_start") |
| 94 |
end |
| 95 |
|
| 96 |
-- End of warning quake |
| 97 |
if self.warning_timer >= warning_period - warning_length and |
| 98 |
self.warning_timer - self.world.hours_per_tick < warning_period - warning_length then |
| 99 |
hosp:tickEarthquake("end") |
| 100 |
end |
| 101 |
|
| 102 |
if self.warning_timer > 0 then |
| 103 |
self.warning_timer = self.warning_timer - self.world.hours_per_tick |
| 104 |
-- Nothing more to do during inactive warning period |
| 105 |
if self.warning_timer < warning_period - warning_length then |
| 106 |
return |
| 107 |
end |
| 108 |
|
| 109 |
-- Start of real earthquake |
| 110 |
if self.warning_timer <= 0 then |
| 111 |
hosp:tickEarthquake("main_start") |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
-- All earthquakes start and end small (small earthquakes never become |
| 116 |
-- larger), so when there has been less than 2 damage applied or only |
| 117 |
-- 2 damage remaining to be applied, move the screen with less |
| 118 |
-- intensity than otherwise. |
| 119 |
if self.remaining_damage <= 2 or |
| 120 |
self.size - self.remaining_damage <= 2 then |
| 121 |
hosp:tickEarthquake("small_damage") |
| 122 |
else |
| 123 |
hosp:tickEarthquake("large_damage") |
| 124 |
end |
| 125 |
|
| 126 |
hosp:tickEarthquake("sound") |
| 127 |
|
| 128 |
-- Do not continue to damage phase while in a warning quake |
| 129 |
if self.warning_timer > 0 then |
| 130 |
return |
| 131 |
end |
| 132 |
|
| 133 |
-- Check if damage phase should go ahead |
| 134 |
self.damage_timer = self.damage_timer - self.world.hours_per_tick |
| 135 |
if self.damage_timer > 0 then return end |
| 136 |
|
| 137 |
for _, room in pairs(self.world.rooms) do |
| 138 |
for object, _ in pairs(room.objects) do |
| 139 |
if object:isMachine() then |
| 140 |
object:earthquakeImpact(room) |
| 141 |
end |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
self.remaining_damage = self.remaining_damage - 1 |
| 146 |
self.damage_timer = self.damage_timer + damage_time |
| 147 |
|
| 148 |
-- A patient might fall over during an earthquake. |
| 149 |
local hospital = self.world:getLocalPlayerHospital() |
| 150 |
-- loop through the patients and allow the possibility for them to fall over |
| 151 |
for _, patient in ipairs(hospital.patients) do |
| 152 |
if not patient.in_room and patient.falling_anim and math.random(1,2) == 2 then |
| 153 |
patient:falling(false) |
| 154 |
end |
| 155 |
end |
| 156 |
end |
| 157 |
|
| 158 |
--! Check if it's time for an earthquake |
| 159 |
function Earthquake:onEndDay() |
| 160 |
if self.world.game_date:monthOfGame() == self.start_month and |
| 161 |
self.world.game_date:dayOfMonth() == self.start_day and |
| 162 |
not self.disabled then |
| 163 |
-- Mark the planned earthquake as ready to start in the next tick. |
| 164 |
self.active = true |
| 165 |
end |
| 166 |
end |
| 167 |
|
| 168 |
--! Called when it is time to have another earthquake |
| 169 |
function Earthquake:nextEarthquake() |
| 170 |
self.active = false |
| 171 |
|
| 172 |
local level_config = self.world.map.level_config |
| 173 |
-- Check carefully that no value that we are going to use is going to be nil |
| 174 |
if level_config.quake_control and level_config.quake_control[self.current_map_earthquake] and |
| 175 |
level_config.quake_control[self.current_map_earthquake].Severity ~= 0 then |
| 176 |
-- This map has rules to follow when making earthquakes, let's follow them |
| 177 |
local control = level_config.quake_control[self.current_map_earthquake] |
| 178 |
self.start_month = math.random(control.StartMonth, control.EndMonth) |
| 179 |
|
| 180 |
-- Month length of the start of the earthquake. From start to finish |
| 181 |
-- earthquakes do not persist for >= a month so we can wrap all days |
| 182 |
-- after the start around the month length unambiguously. |
| 183 |
local eqml = month_length[(self.start_month % 12) + 1] |
| 184 |
self.start_day = math.random(1, eqml) |
| 185 |
|
| 186 |
self.size = control.Severity |
| 187 |
self.remaining_damage = self.size |
| 188 |
self.damage_timer = damage_time |
| 189 |
self.warning_timer = warning_period |
| 190 |
self.current_map_earthquake = self.current_map_earthquake + 1 |
| 191 |
self.next_planned = true |
| 192 |
end |
| 193 |
end |
| 194 |
|
| 195 |
--! Create a small or medium quake now for the "Create Earthquake" cheat |
| 196 |
function Earthquake:createEarthquake() |
| 197 |
-- Make sure an earthquake isn't already happening, or are disabled |
| 198 |
if self.active or self.disabled then return false end |
| 199 |
|
| 200 |
self.start_day = self.world.game_date:dayOfMonth() |
| 201 |
self.start_month = self.world.game_date:monthOfGame() |
| 202 |
if not self.next_planned then |
| 203 |
-- Forcefully make an earthquake if there are none left in level file |
| 204 |
self.size = math.random(1, 6) -- Above 6 seems disastrous |
| 205 |
self.remaining_damage = self.size |
| 206 |
self.damage_timer = damage_time |
| 207 |
self.warning_timer = warning_period |
| 208 |
end |
| 209 |
end |
| 210 |
|
| 211 |
--! Check if there is a current earthquake |
| 212 |
--!return (boolean) True if there is a current earthquake |
| 213 |
function Earthquake:isActive() |
| 214 |
return self.active |
| 215 |
end |
| 216 |
|
| 217 |
--! Afterload sections before 189 were taken from World |
| 218 |
--!param old (integer) The old version of the save game. |
| 219 |
--!param new (integer) The current version of the save game format. |
| 220 |
function Earthquake:afterLoad(old, new) -- luacheck: ignore 212 keep params from parent function |
| 221 |
local old_quake = self.world.next_earthquake |
| 222 |
if old < 115 then |
| 223 |
if old_quake and old_quake.active then |
| 224 |
local rd = 0 |
| 225 |
for _, room in pairs(self.world.rooms) do |
| 226 |
for object, _ in pairs(room.objects) do |
| 227 |
if object.quake_points then |
| 228 |
rd = math.max(rd, object.quake_points) |
| 229 |
object.quake_points = nil |
| 230 |
end |
| 231 |
end |
| 232 |
end |
| 233 |
old_quake.remaining_damage = rd |
| 234 |
old_quake.damage_timer = damage_time |
| 235 |
old_quake.warning_timer = 0 |
| 236 |
end |
| 237 |
end |
| 238 |
|
| 239 |
if old < 189 then |
| 240 |
self.disabled = self.world.earthquakes_disabled -- Used in cheats |
| 241 |
if old_quake then |
| 242 |
for k, v in pairs(old_quake) do |
| 243 |
self[k] = v |
| 244 |
end |
| 245 |
else |
| 246 |
self:nextEarthquake() |
| 247 |
end |
| 248 |
end |
| 249 |
end |