Debian Codegraph @

CorsixTH/Lua/cheats.lua

raw
1 --[[ Copyright (c) 2010 Manuel "Roujin" Wolf
2 Copyright (c) 2020 lewri
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE. --]]
21
22 corsixth.require("announcer")
23
24 local AnnouncementPriority = _G["AnnouncementPriority"]
25
26 --! A holder for all cheats in the game
27 class "Cheats"
28
29 ---@type Cheats
30 local Cheats = _G["Cheats"]
31
32 -- Cheats System
33 function Cheats:Cheats(hospital)
34 self.hospital = hospital
35 -- Cheats to appear specifically in the cheats window
36 self.cheat_list = {
37 {name = "money", func = self.cheatMoney},
38 {name = "all_research", func = self.cheatResearch},
39 {name = "emergency", func = self.cheatEmergency},
40 {name = "show_infected", func = self.cheatShowInfected},
41 {name = "epidemic", func = self.cheatEpidemic},
42 {name = "toggle_epidemic", func = self.cheatToggleEpidemic},
43 {name = "earthquake", func = self.cheatEarthquake},
44 {name = "toggle_earthquake", func = self.cheatToggleEarthquake},
45 {name = "vip", func = self.cheatVip},
46 {name = "create_patient", func = self.cheatPatient},
47 {name = "end_month", func = self.cheatMonth},
48 {name = "end_year", func = self.cheatYear},
49 {name = "lose_level", func = self.cheatLose},
50 {name = "win_level", func = self.cheatWin},
51 {name = "increase_prices", func = self.cheatIncreasePrices},
52 {name = "decrease_prices", func = self.cheatDecreasePrices},
53 {name = "reset_death_count", func = self.cheatResetDeathCount},
54 {name = "max_reputation", func = self.cheatMaxReputation},
55 {name = "repair_all_machines", func = self.cheatRepairAllMachines},
56 {name = "toggle_invulnerable_machines", func = self.cheatToggleInvulnerableMachines},
57 }
58
59 self.active_cheats = {} -- Toggle cheat status
60 --[[ Toggle-based cheats are found at bottom of file]]
61 end
62
63 --! Performs a cheat from the cheat_list (menu cheats)
64 --!param num (integer) The cheat from the cheat_list called
65 --!return true if cheat was successful, false otherwise
66 --!return message (optional string) The text message of the cheat.
67 function Cheats:performCheat(num)
68 local cheat_success, message = self.cheat_list[num].func(self)
69 cheat_success = cheat_success ~= false
70 return cheat_success and self.cheat_list[num].name ~= "lose_level", message
71 end
72
73 --! Updates the cheated status of the player, with a matching announcement
74 --!param speech (string) Optional text for the adviser to say
75 function Cheats:announceCheat(speech)
76 local announcements = self.hospital.world.cheat_announcements
77 local ui = self.hospital.world.ui
78 if announcements then
79 ui:playAnnouncement(announcements[math.random(1, #announcements)], AnnouncementPriority.Critical)
80 end
81 if speech then
82 ui.adviser:say(speech)
83 end
84 self.hospital.cheated = true
85 end
86
87 function Cheats:cheatMoney()
88 self.hospital:receiveMoney(10000, _S.transactions.cheat)
89 end
90
91 function Cheats:cheatResearch()
92 local hosp = self.hospital
93 for _, cat in ipairs({"diagnosis", "cure"}) do
94 while hosp.research.research_policy[cat].current do
95 hosp.research:discoverObject(hosp.research.research_policy[cat].current)
96 end
97 end
98 end
99
100 function Cheats:cheatEmergency()
101 local err = self.hospital:createEmergency()
102 if err == "undiscovered_disease" then
103 return false, _S.misc.cant_treat_emergency
104 elseif err == "no_heliport" then
105 return false, _S.misc.no_heliport
106 -- else 'err == nil', meaning success. The case doesn't need special handling
107 end
108 end
109
110 function Cheats:cheatToggleInfected() end -- Stub of the old name of the function below
111
112 --[[ Before an epidemic has been revealed, show/hide the infected mood icons
113 to easily distinguish the infected patients. This will show/hide icons
114 for ALL future epidemics you cannot distinguish between epidemics
115 by disease ]]--
116 function Cheats:cheatShowInfected()
117 local hosp = self.hospital
118 if hosp.future_epidemics_pool and #hosp.future_epidemics_pool > 0 then
119 for _, future_epidemic in ipairs(hosp.future_epidemics_pool) do
120 local show_mood = future_epidemic.cheat_always_show_mood
121 future_epidemic.cheat_always_show_mood = not show_mood
122 local mood_action = show_mood and "deactivate" or "activate"
123 for _, patient in ipairs(future_epidemic.infected_patients) do
124 patient:setMood("epidemy4",mood_action)
125 end
126 end
127 else
128 return false, _S.misc.epidemic_no_icon_to_toggle
129 end
130 end
131
132 --[[ Creates a new contagious patient in the hospital - potentially an epidemic]]
133 function Cheats:cheatEpidemic()
134 return self.hospital:spawnContagiousPatient()
135 end
136
137 --! Toggles the possibility of epidemics. Cancel any ongoing or preparing epidemics.
138 function Cheats:cheatToggleEpidemic()
139 local hosp, msg = self.hospital
140 if hosp.epidemics_disabled then
141 msg = _S.misc.epidemics_on
142 else
143 msg = _S.misc.epidemics_off
144 hosp:cancelEpidemics()
145 end
146 hosp.epidemics_disabled = not hosp.epidemics_disabled
147 return true, msg
148 end
149
150 function Cheats:cheatEarthquake()
151 return self.hospital.world.earthquake:createEarthquake()
152 end
153
154 --! Toggles the possibility of earthquakes
155 function Cheats:cheatToggleEarthquake()
156 local world, msg = self.hospital.world
157 if world.earthquake.disabled then
158 msg = _S.misc.earthquakes_on
159 else
160 msg = _S.misc.earthquakes_off
161 end
162 world.earthquake.disabled = not world.earthquake.disabled
163 return true, msg
164 end
165
166 function Cheats:cheatVip()
167 self.hospital:createVip()
168 end
169
170 function Cheats:cheatPatient()
171 self.hospital.world:spawnPatient()
172 end
173
174 function Cheats:cheatMonth()
175 self.hospital.world:setEndMonth()
176 end
177
178 function Cheats:cheatYear()
179 self.hospital.world:setEndYear()
180 end
181
182 function Cheats:cheatLose()
183 self.hospital.world:loseGame(1) -- TODO adjust for multiplayer
184 end
185
186 function Cheats:cheatWin()
187 self.hospital.world:winGame(1) -- TODO adjust for multiplayer
188 end
189
190 function Cheats:cheatIncreasePrices()
191 local hosp = self.hospital
192 for _, casebook in pairs(hosp.disease_casebook) do
193 local new_price = casebook.price + 0.5
194 if new_price > 2 then
195 casebook.price = 2
196 else
197 casebook.price = new_price
198 end
199 end
200 end
201
202 function Cheats:cheatDecreasePrices()
203 local hosp = self.hospital
204 for _, casebook in pairs(hosp.disease_casebook) do
205 local new_price = casebook.price - 0.5
206 if new_price < 0.5 then
207 casebook.price = 0.5
208 else
209 casebook.price = new_price
210 end
211 end
212 end
213
214 function Cheats:cheatResetDeathCount()
215 self.hospital:resetDeathCount()
216 end
217
218 function Cheats:cheatMaxReputation()
219 local hosp = self.hospital
220 hosp:unconditionalChangeReputation(hosp.reputation_max)
221 end
222
223 --! Instantly repairs all of the player's machines (regardless of condition, without decreasing strength)
224 function Cheats:cheatRepairAllMachines()
225 local world = self.hospital.world
226 local machines = world:getPlayerMachines()
227
228 for _, machine in ipairs(machines) do
229 machine:machineRepaired(machine:getRoom(), false)
230 end
231 end
232
233 --! Toggles machine invulnerability (no wear or explosions)
234 function Cheats:cheatToggleInvulnerableMachines()
235 local msg
236
237 if self:isCheatActive("invulnerable_machines") then
238 msg = _S.misc.invulnerable_machines_off
239 else
240 msg = _S.misc.invulnerable_machines_on
241 end
242
243 self.active_cheats["invulnerable_machines"] = not self.active_cheats["invulnerable_machines"]
244 return true, msg
245 end
246
247 --[[Begin toggle-based cheat functions]]
248
249 --! Enable Roujin's challenge (spawn rate cheat)
250 function Cheats:roujinOn()
251 self.active_cheats["spawn_rate_cheat"] = true
252 end
253
254 --! Disable Roujin's challenge (spawn rate cheat)
255 function Cheats:roujinOff()
256 -- Clear the current month's spawns to give the player a break
257 self.hospital.world.spawn_dates = {}
258 self.active_cheats["spawn_rate_cheat"] = nil
259 end
260
261 --! Enables no rest cheat (staff do not tire, fast movement)
262 function Cheats:noRestOn()
263 for _, staff in ipairs(self.hospital.staff) do
264 if staff:getAttribute("fatigue") then
265 staff:wake(staff:getAttribute("fatigue"))
266 end
267 end
268 self.active_cheats["no_rest_cheat"] = true
269 end
270
271 --! Disable no rest cheat (re-enable staff fatigue, and normal movement)
272 function Cheats:noRestOff()
273 self.active_cheats["no_rest_cheat"] = nil
274 end
275
276 --! Enable queue jump cheat (for nearly dead patients)
277 function Cheats:queueJumpOn()
278 self.active_cheats.queuejump = true
279 end
280
281 --! Disable queue jump cheat (so that nearly dead patients queue regularly)
282 function Cheats:queueJumpOff()
283 self.active_cheats.queuejump = false
284 end
285
286 --! Enable super doctors cheat (all doctors for hire have maximum skills)
287 function Cheats:superDoctorOn()
288 self.active_cheats.super_doctor = true
289 self.hospital.world:makeAvailableStaff(self.hospital.world.game_date:monthOfGame())
290 end
291
292 --! Disable super doctors cheat (immediately return to the normal doctors available in the for hire dialog)
293 function Cheats:superDoctorOff()
294 self.active_cheats.super_doctor = false
295 self.hospital.world:makeAvailableStaff(self.hospital.world.game_date:monthOfGame())
296 end
297
298 --[[End toggle-based cheat functions]]
299
300 --[[ The toggle_cheats list (code operated cheats)
301 These cheats do not appear in the Cheat UI menu, and are only accessed from fax code inputs currently.
302 They contain adviser strings, so cannot be persisted.
303 ]]--
304 local toggle_cheats = {
305 ["spawn_rate_cheat"] = {
306 enable = Cheats.roujinOn,
307 disable = Cheats.roujinOff,
308 enableAnnouncement = _A.cheats.roujin_on_cheat,
309 disableAnnouncement = _A.cheats.roujin_off_cheat,
310 lower = 27868.3,
311 upper = 27868.4,
312 },
313 ["no_rest_cheat"] = {
314 enable = Cheats.noRestOn,
315 disable = Cheats.noRestOff,
316 enableAnnouncement = _A.cheats.norest_on_cheat,
317 disableAnnouncement = _A.cheats.norest_off_cheat,
318 lower = 185.5,
319 upper = 185.6,
320 },
321 queuejump = {
322 enable = Cheats.queueJumpOn,
323 disable = Cheats.queueJumpOff,
324 enableAnnouncement = _A.cheats.queuejump_on_cheat,
325 disableAnnouncement = _A.cheats.queuejump_off_cheat,
326 lower = 200.5,
327 upper = 200.6,
328 },
329 super_doctor = {
330 enable = Cheats.superDoctorOn,
331 disable = Cheats.superDoctorOff,
332 enableAnnouncement = _A.cheats.superdoctor_on_cheat,
333 disableAnnouncement = _A.cheats.superdoctor_off_cheat,
334 lower = 301.5,
335 upper = 301.6,
336 },
337 }
338
339 --! Checks if a toggle cheat is activated
340 --!param name (string) Name of cheat to check
341 --!return Returns true if active
342 function Cheats:isCheatActive(name)
343 return self.active_cheats[name]
344 end
345
346 --! Checks the obfuscated cheat code for a match and executes it
347 --!param num (number) The obfuscated cheat value
348 --!return Returns name of the cheat executed from the lookup table, or nil
349 function Cheats:processCheatCode(num)
350 local cheat_list = toggle_cheats
351 for name, data in pairs(cheat_list) do
352 if data.lower < num and data.upper > num then
353 self:toggleCheat(name)
354 return name
355 end
356 end
357 return -- cheat not found
358 end
359
360
361 --! Performs a cheat from fax_cheats
362 --!param name (string) The cheat called from the list
363 function Cheats:toggleCheat(name)
364 local ui = self.hospital.world.ui
365 local cheat_list = toggle_cheats
366 local cheat = cheat_list[name]
367 local cheatWindow = ui:getWindow(UICheats)
368 local speech
369 if not self:isCheatActive(name) then
370 cheat.enable(self)
371 speech = cheat.enableAnnouncement
372 self:announceCheat(speech)
373 else
374 cheat.disable(self)
375 speech = cheat.disableAnnouncement
376 self:announceCheat(speech)
377 end
378 -- If a cheats window is open, make sure the UI is updated
379 if cheatWindow then
380 cheatWindow:updateCheatedStatus()
381 end
382 end