Debian Codegraph @

CorsixTH/Lua/dialogs/fullscreen/fax.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
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 corsixth.require("announcer")
22
23 local AnnouncementPriority = _G["AnnouncementPriority"]
24
25 class "UIFax" (UIFullscreen)
26
27 ---@type UIFax
28 local UIFax = _G["UIFax"]
29
30 function UIFax:UIFax(ui, icon)
31 self:UIFullscreen(ui)
32 local gfx = ui.app.gfx
33 self.background = gfx:loadRaw("Fax01V", 640, 480, "QData", "QData", "Fax01V.pal", true)
34 local palette = gfx:getPalette("Fax01V.pal")
35 self.panel_sprites = gfx:loadSpriteTable("QData", "Fax02V", true, palette)
36 self.fax_font = gfx:loadFontAndSpriteTable("QData", "Font51V", false, palette, { apply_ui_scale = true })
37 self.icon = icon
38 self.message = icon.message or {}
39 self.owner = icon.owner
40
41 self.code = ""
42
43 -- Add choice buttons
44 local choices = self.message.choices
45 self.choice_buttons = {}
46 local orig_y = 175
47 if choices then
48 for i = 1, #choices do
49 local y = orig_y + ((i-1) + (3-#choices)) * 48
50 local choice = choices[i].choice
51 -- NB: both nil and true result in enabled; also handle old "disabled" choice
52 local enabled = (choices[i].enabled ~= false) and (choice ~= "disabled")
53 local --[[persistable:fax_choice_button]] function callback()
54 self:choice(i)
55 end
56 self.choice_buttons[i] = self:addPanel(17, 492, y, nil, nil, true, DrawFlags.Nearest)
57 :makeButton(0, 0, 43, 43, 18, callback)
58 :setDisabledSprite(19)
59 :enable(enabled)
60 end
61 end
62
63 -- Close button
64 self:addPanel(0, 598, 440):makeButton(0, 0, 26, 26, 16, self.close):setTooltip(_S.tooltip.fax.close)
65
66 self:addPanel(0, 471, 349):makeButton(0, 0, 87, 20, 14, self.cancel) -- Cancel code button
67 self:addPanel(0, 474, 372):makeButton(0, 0, 91, 27, 15, self.validate) -- Validate code button
68
69 self:addPanel(0, 168, 348):makeButton(0, 0, 43, 10, 1, self.correct) -- Correction button
70
71 local function button(char)
72 return --[[persistable:fax_button]] function() self:appendNumber(char) end
73 end
74
75 self:addPanel(0, 220, 348):makeButton(0, 0, 43, 10, 2, button("1")):setSound("Fax_1.wav")
76 self:addPanel(0, 272, 348):makeButton(0, 0, 44, 10, 3, button("2")):setSound("Fax_2.wav")
77 self:addPanel(0, 327, 348):makeButton(0, 0, 43, 10, 4, button("3")):setSound("Fax_3.wav")
78
79 self:addPanel(0, 219, 358):makeButton(0, 0, 44, 10, 5, button("4")):setSound("Fax_4.wav")
80 self:addPanel(0, 272, 358):makeButton(0, 0, 43, 10, 6, button("5")):setSound("Fax_5.wav")
81 self:addPanel(0, 326, 358):makeButton(0, 0, 44, 10, 7, button("6")):setSound("Fax_6.wav")
82
83 self:addPanel(0, 218, 370):makeButton(0, 0, 44, 11, 8, button("7")):setSound("Fax_7.wav")
84 self:addPanel(0, 271, 370):makeButton(0, 0, 44, 11, 9, button("8")):setSound("Fax_8.wav")
85 self:addPanel(0, 326, 370):makeButton(0, 0, 44, 11, 10, button("9")):setSound("Fax_9.wav")
86
87 self:addPanel(0, 217, 382):makeButton(0, 0, 45, 12, 11, button("*"))
88 self:addPanel(0, 271, 382):makeButton(0, 0, 44, 11, 12, button("0")):setSound("Fax_0.wav")
89 self:addPanel(0, 326, 382):makeButton(0, 0, 44, 11, 13, button("#"))
90 end
91
92 -- Faxes pause the game
93 function UIFax:mustPause()
94 return true
95 end
96
97 function UIFax:updateChoices()
98 local choices = self.message.choices
99 for i, button in ipairs(self.choice_buttons) do
100 -- NB: both nil and true result in enabled; also handle old "disabled" choice
101 local enabled = (choices[i].enabled ~= false) and (choices[i].choice ~= "disabled")
102 button:enable(enabled)
103 end
104 end
105
106 function UIFax:draw(canvas, x, y)
107 local s = TheApp.config.ui_scale
108 canvas:scale(s, "bitmap")
109 self.background:draw(canvas, self.x * s + x, self.y * s + y)
110 canvas:scale(1, "bitmap")
111 UIFullscreen.draw(self, canvas, x, y)
112 x, y = self.x * s + x, self.y * s + y
113
114 if self.message then
115 local last_y = y + 40 * s
116 for _, message in ipairs(self.message) do
117 last_y = self.fax_font:drawWrapped(canvas, message.text, x + 190 * s,
118 last_y + (message.offset or 0), 360 * s,
119 "center")
120 end
121 local choices = self.message.choices
122 if choices then
123 local orig_y = y + 190 * s
124 for i = 1, #choices do
125 last_y = orig_y + ((i - 1) + (3 - #choices)) * 48 * s
126 self.fax_font:drawWrapped(canvas, choices[i].text, x + 190 * s,
127 last_y + (choices[i].offset or 0), 300 * s)
128 end
129 end
130 end
131 end
132
133 --A choice was made for the fax.
134 --!param choice_number (integer) Number of the choice
135 function UIFax:choice(choice_number)
136 local choices = self.message.choices
137 local choice, additionalInfo
138 if choices and choice_number >= 1 and choice_number <= #choices then
139 choice = choices[choice_number].choice
140 additionalInfo = choices[choice_number].additionalInfo
141 else
142 choice = "disabled"
143 additionalInfo = nil
144 end
145
146 local owner = self.owner
147 if owner and owner.humanoid_class then
148 local humanoid = owner
149 -- A choice was made, the patient is no longer waiting for a decision
150 humanoid:setMood("patient_wait", "deactivate")
151 humanoid.message_callback = nil
152 if choice == "send_home" then
153 humanoid:goHome("kicked")
154 if humanoid.diagnosed then
155 -- No treatment rooms
156 humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.no_treatment_available)
157 else
158 -- No diagnosis rooms
159 humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.no_diagnoses_available)
160 end
161 elseif choice == "wait" then
162 -- Wait two months before going home
163 humanoid.waiting = 60
164 if humanoid.diagnosed then
165 -- Waiting for treatment room
166 humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.waiting_for_treatment_rooms)
167 else
168 -- Waiting for diagnosis room
169 humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.waiting_for_diagnosis_rooms)
170 end
171 elseif choice == "guess_cure" then
172 humanoid:setDiagnosed()
173 if humanoid:agreesToPay(humanoid.disease.id) then
174 humanoid:setNextAction(SeekRoomAction(humanoid.disease.treatment_rooms[1]):enableTreatmentRoom())
175 else
176 humanoid:goHome("over_priced", humanoid.disease.id)
177 end
178 elseif choice == "research" then
179 humanoid:unregisterCallbacks()
180 humanoid:setMood("idea", "activate")
181 humanoid:setNextAction(SeekRoomAction("research"))
182 end
183 end
184 local vip_ignores_refusal = math.random(1, 2)
185 if choice == "accept_emergency" then
186 self.ui.app.world:newObject("helicopter", "north")
187 self.ui:addWindow(UIWatch(self.ui, "emergency"))
188 elseif choice == "refuse_emergency" then
189 self.ui.app.world:nextEmergency()
190 -- VIP may choose to visit anyway if he is refused too often
191 elseif (self.ui.hospital.vip_declined > 2 and vip_ignores_refusal == 2) and choice == "refuse_vip" then
192 self.ui.hospital.num_vips = self.ui.hospital.num_vips + 1
193 self.ui.app.world:spawnVIP(additionalInfo.name)
194 self.ui.hospital.vip_declined = 0
195 elseif choice == "refuse_vip" then
196 self.ui.app.world:nextVip() -- don't start an inspection
197 self.ui.hospital.vip_declined = self.ui.hospital.vip_declined + 1
198 elseif choice == "accept_vip" then
199 self.ui.hospital.num_vips = self.ui.hospital.num_vips + 1
200 self.ui.app.world:spawnVIP(additionalInfo.name)
201 elseif choice == "declare_epidemic" then
202 local epidemic = self.ui.hospital.epidemic
203 if epidemic then
204 epidemic:resolveDeclaration()
205 end
206 elseif choice == "cover_up_epidemic" then
207 local epidemic = self.ui.hospital.epidemic
208 if epidemic then
209 epidemic:startCoverUp()
210 end
211 elseif choice == "accept_new_level" then
212 -- Set the new salary.
213 self.ui.hospital.player_salary = self.ui.hospital.salary_offer
214 if tonumber(self.ui.app.world.map.level_number) then
215 local next_level = self.ui.app.world.map.level_number + 1
216 if self.ui.app:loadLevel(next_level, self.ui.app.map.difficulty, nil, nil,
217 nil, nil, _S.errors.load_level_prefix) then
218 self.ui.app.moviePlayer:playAdvanceMovie(next_level)
219 end
220 else
221 local campaign_info = self.ui.app.world.campaign_info
222 for i, level in ipairs(campaign_info.levels) do
223 local filename = self.ui.app.world.map.level_filename or self.ui.app.world.map.level_number
224 if filename == level then
225 local level_info, _ = self.ui.app:readLevelFile(campaign_info.levels[i + 1], campaign_info.folder)
226 if level_info then
227 self.ui.app:loadLevel(level_info.path, nil, level_info.name,
228 level_info.map_file, level_info.briefing, nil, _S.errors.load_level_prefix, campaign_info)
229 if campaign_info.movie then
230 local n = math.max(1, 12 - #campaign_info.levels + i)
231 self.ui.app.moviePlayer:playAdvanceMovie(n)
232 end
233 break
234 end
235 end
236 end
237 end
238 elseif choice == "return_to_main_menu" then
239 self.ui.app.moviePlayer:playWinMovie()
240 self.ui.app:loadMainMenu()
241 elseif choice == "stay_on_level" then
242 self.ui.hospital.win_declined = true
243 end
244 self.icon:removeMessage()
245 self:close()
246 end
247
248 function UIFax:cancel()
249 self.code = ""
250 end
251
252 function UIFax:correct()
253 if self.code ~= "" then
254 self.code = string.sub(self.code, 1, -2) --Remove last character
255 end
256 end
257
258 function UIFax:validate()
259 if self.code == "" then
260 return
261 end
262 local code = self.code
263 self.code = ""
264 local code_n = (tonumber(code) or 0) / 10^5
265 local x = math.abs((code_n ^ 5.00001 - code_n ^ 5) * 10^5 - code_n ^ 5)
266 local cheats = self.ui.hospital.hosp_cheats
267 print("Code typed on fax:", code)
268 if code == "24328" then
269 -- Original game cheat code
270 self.ui.adviser:say(_A.cheats.th_cheat)
271 self.ui:addWindow(UICheats(self.ui))
272 elseif code == "112" then
273 -- simple, unobfuscated cheat for everyone :)
274 -- not that critical, but we want to make to make sure it's played fairly soon
275 self.ui:playAnnouncement("rand*.wav", AnnouncementPriority.Critical)
276
277 -- Pass cheat code to the Cheats system to handle
278 elseif not cheats:processCheatCode(x) then
279 -- no valid cheat code entered
280 self.ui:playSound("fax_no.wav")
281 return
282 -- else Cheat executed, nothing to do here
283 end
284 self.ui:playSound("fax_yes.wav")
285
286 -- TODO: Other cheats (preferably with slight obfuscation, as above)
287 end
288
289 function UIFax:appendNumber(number)
290 self.code = self.code .. number
291 end
292
293 function UIFax:close()
294 self.icon.fax = nil
295 self.icon:adjustToggle()
296 UIFullscreen.close(self)
297 end
298
299 function UIFax:afterLoad(old, new)
300 if old < 236 then
301 local gfx = TheApp.gfx
302 self.background = gfx:loadRaw("Fax01V", 640, 480, "QData", "QData", "Fax01V.pal", true)
303 local palette = gfx:getPalette("Fax01V.pal")
304 self.panel_sprites = gfx:loadSpriteTable("QData", "Fax02V", true, palette)
305 self.fax_font = gfx:loadFontAndSpriteTable("QData", "Font51V", false, palette, { apply_ui_scale = true })
306 end
307 UIFullscreen.afterLoad(self, old, new)
308 if old < 59 then
309 -- self.choice_buttons added, changes to disabled buttons.
310 -- Since it's hard to add retroactively, just close any opened fax window.
311 self:close()
312 end
313 end