Debian Codegraph @

CorsixTH/Lua/epidemic.lua

raw
1 --[[ Copyright (c) 2013 William "sadger" Gatens
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 "Epidemic"
26
27 ---@type Epidemic
28 local Epidemic = _G["Epidemic"]
29
30 --[[Manages the epidemics that occur in hospitals. Generally, any epidemic
31 logic that happens outside this class will call functions contained here.]]
32 function Epidemic:Epidemic(hospital, contagious_patient)
33 self.hospital = hospital
34 self.world = self.hospital.world
35
36 -- Epidemic participants
37 self.infected_patients = {}
38
39 -- The contagious disease the epidemic is based around
40 self.disease = contagious_patient.disease
41
42 -- Can the epidemic be revealed to the player
43 self.ready_to_reveal = false
44
45 -- Is the epidemic revealed to the player?
46 self.revealed = false
47
48 -- Various values for the different outcomes - used when result fax is sent
49 self.declare_fine = 0
50 self.reputation_hit = 0
51 self.coverup_fine = 0
52 self.compensation = 0
53
54 -- Is the epidemic bad enough to deserve the whole hospital to be evacuated
55 self.will_be_evacuated = false
56
57 -- Fax sent when the result of the cover up is revealed to the player
58 self.cover_up_result_fax = {}
59
60 -- Set if the user choses the cover up option instead of declaring
61 self.coverup_selected = false
62
63 --Cover up timer and amount of intervals the timer has
64 self.timer = nil
65 self.countdown_intervals = 0
66
67 -- Vaccination mode is activated when the icon on the timer
68 -- is clicked - used to determine what the cursor should look like
69 self.vaccination_mode_active = false
70
71 -- For Cheat - Show the contagious icon even before the epidemic is revealed?
72 self.cheat_always_show_mood = false
73
74 -- Number of times an infected patients has successfully infected another
75 self.total_infections = 0
76 -- Number of times any infected patient has tried to infected another - successful or not
77 self.attempted_infections = 0
78
79 local level_config = self.world.map.level_config
80 -- How often is the epidemic disease passed on? Represents a percentage
81 -- spread_factor% of the time a disease is passed on to a suitable target
82 self.spread_factor = level_config.gbv.ContagiousSpreadFactor or 25
83 -- How many people still infected and not cure causes the player to take a reputation hit as well as a fine
84 -- has no effect if more than evacuation_minimum - hospital is evacuated anyway
85 self.reputation_loss_minimum = level_config.gbv.EpidemicRepLossMinimum or 5
86 -- How many people need to still be infected and not cure to cause the
87 -- inspector to evacuate the hospital
88 self.evacuation_minimum = level_config.gbv.EpidemicEvacMinimum or 10
89
90 -- The health inspector who reveals the result of the epidemic
91 self.inspector = nil
92
93 self:addContagiousPatient(contagious_patient)
94 -- Mark all the patients currently in the hospital as passed reception - for compatibility
95 self:markPatientsAsPassedReception()
96 end
97
98 --[[ The epidemic tick - currently the same rate as the hospital's tick but
99 not necessary dependent on it - could potentially be reduced for performance.]]
100 function Epidemic:tick()
101 self:infectOtherPatients()
102 self:checkIfReadyToReveal()
103 self:showAppropriateAdviceMessages()
104 self:tryAnnounceInspector()
105 self:markedPatientsCallForVaccination()
106 self:checkInfectedLeftHospital()
107 self:checkNoInfectedPatients()
108 self:checkPatientsForRemoval()
109 end
110
111 --[[ Adds a new patient to the epidemic who is actively contagious: infected but
112 not vaccinated or cured]]
113 function Epidemic:addContagiousPatient(patient)
114 patient.infected = true
115 patient:updateDynamicInfo()
116 self.infected_patients[#self.infected_patients + 1] = patient
117 if self.coverup_selected or self.cheat_always_show_mood then
118 patient:setInfectedStatus()
119 end
120 end
121
122 --[[ Goes through all infected patients checking if there are any other patients
123 in adjacent squares that can be infected, and if so infects them turning
124 them into an infected patient too. ]]
125 function Epidemic:infectOtherPatients()
126 --[[ Can an infected patient infect another patient - taking into account
127 spread factor as defined in the configuration. Patients must be both in the
128 corridors or in the same room - don't infect through walls.
129 @param infector (Patient) already infected patient who want to infect
130 @param victim (Patient) target to possibly infect
131 @return true if patient can infect other, false otherwise (boolean) ]]
132 local function canInfectOther(infector, victim)
133 -- Check is patient-Infector is not infectious.
134 if infector.cured or infector.vaccinated then return false end
135
136 -- Don't allow infection outside the hospital grounds
137 -- Also check both patients to prevent infecting through outer walls
138 local ppx, ppy = infector.tile_x, infector.tile_y
139 if ppx and ppy and not self.hospital:isInHospital(ppx, ppy) then return false end
140 local opx, opy = victim.tile_x, victim.tile_y
141 if opx and opy and not self.hospital:isInHospital(opx, opy) then return false end
142
143 -- 'victim' is already infected or is going home.
144 if victim.infected or victim.cured or victim.vaccinated then return false end
145 -- Don't infect victim if it is already under another attempt to be infected
146 if victim.under_infection_attempt then return false end
147 -- Don't infect emergencies.
148 if victim.is_emergency then return false end
149
150 -- If the victim patient has a different disease OR the victim patient's
151 -- disease cannot be changed.
152 if infector.disease ~= victim.disease and
153 (not victim.disease.contagious or victim.diagnosed) then return false end
154
155 -- Only infect if both are in the same room.
156 return infector:getRoom() == victim:getRoom()
157 end
158
159 local function infect_other(infector, victim)
160 if infector.disease ~= victim.disease then
161 victim:changeDisease(infector.disease)
162 end
163 self:addContagiousPatient(victim)
164 self.total_infections = self.total_infections + 1
165 end
166
167 -- Scale the chance of spreading the disease infecting spread_factor%
168 -- of patients results in too large an epidemic typically so infect
169 -- spread_factor/spread_scale_factor patients.
170 -- This scale factor is purely for balance purposes, the larger the
171 -- value the less spreading the epidemics will be, all relative to spread_factor.
172 -- Must be > 0.
173 local spread_scale_factor = 200
174
175 -- Go through all infected patients making the check if they can infect
176 -- and making any patient they can infect contagious too.
177 local entity_map = self.world.entity_map
178 if entity_map then
179 for _, infector in ipairs(self.infected_patients) do
180 local adjacent_patients =
181 entity_map:getPatientsInAdjacentSquares(infector.tile_x, infector.tile_y)
182 for _, potential_victim in ipairs(adjacent_patients) do
183 if canInfectOther(infector, potential_victim) then
184 potential_victim.under_infection_attempt = true
185 self.attempted_infections = self.attempted_infections + 1
186 if (self.total_infections / self.attempted_infections) <
187 (self.spread_factor / spread_scale_factor) then
188 infect_other(infector, potential_victim)
189 end
190 end
191 end
192 end
193 end
194
195 end
196
197 --[[ The epidemic is ready to be revealed to the player if any infected patient
198 is fully diagnosed. N.B. because multiple epidemics are queued to become
199 the "active" one, being ready to reveal does NOT guarantee an epidemic
200 WILL be revealed to the player and may even terminate before they are even
201 aware it existed. ]]
202 function Epidemic:checkIfReadyToReveal()
203 if self.ready_to_reveal then return end
204
205 for _, infected_patient in ipairs(self.infected_patients) do
206 if infected_patient.diagnosed then
207 self.ready_to_reveal = true
208 break
209 end
210 end
211 end
212
213 --[[ Show the player the have an epidemic - send a fax
214 This happens when the epidemic is chosen to the be
215 the "active" epidemic out of all the queued ones.]]
216 function Epidemic:revealEpidemic()
217 assert(self.ready_to_reveal)
218 self.revealed = true
219 self:sendInitialFax()
220 self:announceStartOfEpidemic()
221 end
222
223 --[[ Plays the announcement for the start of the epidemic ]]
224 function Epidemic:announceStartOfEpidemic()
225 local announcements = {"EPID001.wav", "EPID002.wav", "EPID003.wav", "EPID004.wav"}
226 if self.hospital:isPlayerHospital() then
227 self.world.ui:playAnnouncement(announcements[math.random(1, #announcements)], AnnouncementPriority.Critical)
228 end
229 end
230
231
232 --[[ Plays the announcement for the end of the epidemic ]]
233 function Epidemic:announceEndOfEpidemic()
234 local announcements = {"EPID005.wav", "EPID006.wav", "EPID007.wav", "EPID008.wav"}
235 if self.hospital:isPlayerHospital() then
236 self.world.ui:playAnnouncement(announcements[math.random(1, #announcements)], AnnouncementPriority.Critical)
237 end
238 end
239
240 --[[ Check for conditions that one of the infected and not cured
241 patient leaved the hospital. This discover epidemic to public instantly.
242 So cover up must end earlier than the length of the timer.]]
243 function Epidemic:checkInfectedLeftHospital()
244 if not self:_isCoverUpActive() then return end
245
246 -- Check whether a patient has left.
247 for _, infected_patient in ipairs(self.infected_patients) do
248 local px, py = infected_patient.tile_x, infected_patient.tile_y
249 -- If leaving and no longer in the hospital.
250 if (infected_patient.going_home or infected_patient.going_to_die) and not infected_patient.cured and
251 px and py and not self.hospital:isInHospital(px,py) then
252 -- Patient escaped from the hospital, discovery is inevitable.
253 self:finishCoverUp()
254 return
255 end
256 end
257 end
258
259 --[[ Check for conditions that no any uncured infected patients
260 left in hospital. If so then cover up must end earlier than the
261 length of the timer.]]
262 function Epidemic:checkNoInfectedPatients()
263 if not self:_isCoverUpActive() then return end
264
265 if self:countInfectedPatients() == 0 then
266 self:finishCoverUp()
267 end
268 end
269
270 --[[Remove any patients which were already on their way out or died before the
271 epidemic was started to be fair on the players so they don't instantly fail.
272 Additionally if any patients die during an epidemic we also remove them,
273 otherwise a player may never win the epidemic in such a case.]]
274 function Epidemic:checkPatientsForRemoval()
275 for i = #self.infected_patients, 1, -1 do
276 local infected_patient = self.infected_patients[i]
277 if (not self.coverup_selected and (infected_patient.going_home or infected_patient.going_to_die)) or
278 infected_patient.dead or infected_patient.tile_x == nil then
279 table.remove(self.infected_patients,i)
280 end
281 end
282 end
283
284 --[[ Toggle the vaccination mode changes how the cursor interacts with
285 the hospital. Toggled by pressing the button on the watch
286 (@see UIWatch:UIWatch) ]]
287 function Epidemic:toggleVaccinationMode()
288 self.vaccination_mode_active = not self.vaccination_mode_active
289 self:_updateVaccinationCursor()
290 end
291
292 --[[ Turn off vaccination cursor mode ]]
293 function Epidemic:turnOffVaccinationMode()
294 self.vaccination_mode_active = false
295 self:_updateVaccinationCursor()
296 end
297
298 --[[ Update how the cursor interacts with the hospital. ]]
299 function Epidemic:_updateVaccinationCursor()
300 local cursor = self.vaccination_mode_active and "epidemic_hover" or "default"
301 self.world.ui:setCursor(self.world.ui.app.gfx:loadMainCursor(cursor))
302 end
303
304 --[[ Show a patient is able to be vaccinated, this is shown to the player by
305 changing the icon, once a player has been marked for vaccination they may
306 possibly become a vaccination candidate. Marking is done by clicking
307 the player and can only happen during a cover up. ]]
308 function Epidemic:markForVaccination(patient)
309 if patient.infected and not patient.vaccinated and
310 not patient.marked_for_vaccination then
311 patient:setToReadyForVaccinationStatus()
312 patient.hospital:playSound("vaccin.wav")
313 end
314 end
315
316 --[[ Counts the number of patients that have been infected that are still
317 -- infected
318 -- @return infected_count (Integer) the number of patients still infected.]]
319 function Epidemic:countInfectedPatients()
320 local infected_count = 0
321 for _, patient in pairs(self.infected_patients) do
322 if patient.infected and not patient.cured then
323 infected_count = infected_count + 1
324 end
325 end
326 return infected_count
327 end
328
329 --[[ Sends the initial fax to the player when the epidemic is revealed.]]
330 function Epidemic:sendInitialFax()
331 local num_infected = self:countInfectedPatients()
332 --Save it in a global variable so we can apply the fine in the declare function
333 self.declare_fine = self:calculateInfectedFine(num_infected)
334
335 local message = {
336 {text = _S.fax.epidemic.disease_name:format(self.disease.name)},
337 {text = _S.fax.epidemic.declare_explanation_fine:format(self.declare_fine)},
338 {text = _S.fax.epidemic.cover_up_explanation_1},
339 {text = _S.fax.epidemic.cover_up_explanation_2},
340 choices = {
341 {text = _S.fax.epidemic.choices.declare, choice = "declare_epidemic"},
342 {text = _S.fax.epidemic.choices.cover_up, choice = "cover_up_epidemic"},
343 },
344 }
345 self.world.ui.bottom_panel:queueMessage("epidemy", message, self, 24*20,2)
346 end
347
348 --[[ Calculate the fine for having a given number of infected patients
349 --Used to determine the initial declaration fine as the cover up fine.
350 --@param infected_count (Integer) number of patients still infected
351 --@return fine (Integer) the fine amount ]]
352 function Epidemic:calculateInfectedFine(infected_count)
353 local level_config = self.world.map.level_config
354 local fine_per_infected = level_config.gbv.EpidemicFine or 2000
355 return math.max(2000, infected_count * fine_per_infected)
356 end
357
358 --[[ Gets the amount of reputation to add/remove from the player
359 based on a given fine. Reputation gain/loss isn't specified
360 in the configs so we use a percentage of the fine as a base
361 value with extra being gained/lost for specific circumstances.
362 @param fine_amount (Integer) amount the player will be fined
363 @return reputation hit (Integer) reputation to be deducted relative to fine]]
364 local function getBaseReputationFromFine(fine_amount)
365 return math.round(fine_amount / 100)
366 end
367
368 --[[ When the player chooses to declare the epidemic instead of trying
369 to cover up it from the initial faxes - ends the epidemic immediately
370 after applying fine.]]
371 function Epidemic:resolveDeclaration()
372 self:clearAllInfectedPatients()
373
374 --No fax for declaration just apply fines and rep hit
375 self.hospital:spendMoney(self.declare_fine, _S.transactions.epidemy_fine)
376 local reputation_hit = getBaseReputationFromFine(self.declare_fine)
377 self.hospital.reputation = self.hospital.reputation - reputation_hit
378 self.hospital.epidemic = nil
379 end
380
381 --[[ Remove all infected patients by vaccinating from the hospital and clear
382 any epidemic-specific icons from their heads.]]
383 function Epidemic:clearAllInfectedPatients()
384 for _, infected_patient in ipairs(self.infected_patients) do
385 -- Remove any vaccination calls still open
386 infected_patient:removeVaccinationCandidateStatus()
387 self.world.dispatcher:dropFromQueue(infected_patient)
388 infected_patient.vaccinated = true
389 infected_patient:removeAnyEpidemicStatus()
390 end
391 end
392
393
394 --[[ When the player chooses to begin the cover up over declaring from the
395 initial fax (@see sendInitialFax) ]]
396 function Epidemic:startCoverUp()
397 self.timer = UIWatch(self.world.ui, "epidemic")
398 self.countdown_intervals = self.timer.open_timer
399 self.world.ui:addWindow(self.timer)
400 -- last chance clean up as entities might have ticked and changed state
401 self:checkPatientsForRemoval()
402 self.coverup_selected = true
403 --Set the mood icon for all infected patients
404 for _, infected_patient in ipairs(self.infected_patients) do
405 infected_patient:updateDynamicInfo()
406 infected_patient:setInfectedStatus()
407 end
408 end
409
410 --[[ Ends the cover up epidemic stage ]]
411 function Epidemic:finishCoverUp()
412 if not self.inspector then
413 self:spawnInspector()
414 end
415
416 self.timer:close()
417
418 -- Turn vaccination mode off if enabled
419 self:turnOffVaccinationMode()
420 end
421
422 --[[ Inspector had arrived at reception desk. Check if
423 any infected still in hospital and determine final verdict.]]
424 function Epidemic:handleInspectorArrival()
425 local still_infected = self:countInfectedPatients()
426 self:determineFaxAndFines(still_infected)
427 self:clearAllInfectedPatients()
428 self:applyOutcome()
429 end
430
431 --[[ Epidemic timer time is up ]]
432 function Epidemic:coverUpTimeIsUp()
433 self:finishCoverUp()
434 end
435
436 --[[ Calculates the contents of the fax and the appropriate fines based on the
437 result of a cover up, results are stored globally to the class to be applied later.
438 @param still_infected (Integer) the number of patients still infected]]
439 function Epidemic:determineFaxAndFines(still_infected)
440
441 -- Losing text
442 local fail_text_1 = _S.fax.epidemic_result.failed.part_1_name:format(self.disease.name)
443 local fail_text_2 = _S.fax.epidemic_result.failed.part_2
444 local close_option = {text = _S.fax.epidemic_result.close_text, choice = "close"}
445
446 -- Losing fine (if epidemic is "lost")
447 self.coverup_fine = self:calculateInfectedFine(still_infected)
448
449 if still_infected == 0 then
450 -- Compensation fine (if epidemic is "won")
451 local level_config = self.world.map.level_config
452 local compensation_low_value = level_config.gbv.EpidemicCompLo or 1000
453 local compensation_high_value = level_config.gbv.EpidemicCompHi or 5000
454 self.compensation = math.random(compensation_low_value,compensation_high_value)
455
456 self.cover_up_result_fax = {
457 {text = _S.fax.epidemic_result.succeeded.part_1_name:format(self.disease.name)},
458 {text = _S.fax.epidemic_result.succeeded.part_2},
459 {text = _S.fax.epidemic_result.compensation_amount:format(self.compensation)},
460 choices = {close_option}
461 }
462 elseif still_infected < self.reputation_loss_minimum and still_infected < self.evacuation_minimum then
463 self.cover_up_result_fax = {
464 {text = fail_text_1},
465 {text = fail_text_2},
466 {text = _S.fax.epidemic_result.fine_amount:format(self.coverup_fine)},
467 choices = {close_option}
468 }
469 elseif still_infected >= self.reputation_loss_minimum and still_infected < self.evacuation_minimum then
470 self.cover_up_result_fax = {
471 {text = fail_text_1},
472 {text = fail_text_2},
473 {text = _S.fax.epidemic_result.rep_loss_fine_amount:format(self.coverup_fine)},
474 choices = {close_option}
475 }
476 else
477 self.will_be_evacuated = true
478 self.cover_up_result_fax = {
479 {text = fail_text_1},
480 {text = fail_text_2},
481 {text = _S.fax.epidemic_result.hospital_evacuated},
482 choices = {close_option}
483 }
484 end
485 end
486
487 --[[ Apply the compensation or fines where appropriate to the player as
488 determined when the cover up was completed (@see finishCoverUp) ]]
489 function Epidemic:applyOutcome()
490 -- If there is no compensation to apply the epidemic has been failed
491 if self.compensation == 0 then
492 if self.will_be_evacuated then
493 self.reputation_hit = math.round(self.hospital.reputation * (1/3))
494 self:evacuateHospital()
495 else
496 self.reputation_hit = getBaseReputationFromFine(self.coverup_fine)
497 end
498 -- Apply fine and reputation hit
499 self.hospital:spendMoney(self.coverup_fine,_S.transactions.epidemy_coverup_fine)
500 self.hospital.reputation = self.hospital.reputation - self.reputation_hit
501 else
502 self.hospital:receiveMoney(self.compensation, _S.transactions.compensation)
503 end
504 -- Finally send the fax confirming the outcome
505 self:sendResultFax()
506 --Remove epidemic from hospital so another epidemic may be assigned
507 self.hospital.epidemic = nil
508 end
509
510 --[[ For compatibility. Mark all the current patients who are in the hospital
511 --but not in the reception queues as being "passed reception" used to decide
512 --who is evacuated @see evacuateHospital. For new patients they are marked
513 --as they leave the reception desks.]]
514 function Epidemic:markPatientsAsPassedReception()
515 local queuing_patients = {}
516 for _, desk in ipairs(self.hospital:getReceptionDesks()) do
517 for _, patient in ipairs(desk.queue) do
518 -- Use patient as map key to speed up lookup
519 queuing_patients[patient] = true
520 end
521 end
522
523 for _, patient in ipairs(self.hospital.patients) do
524 -- Patient is not queuing for reception
525 local px, py = patient.tile_x, patient.tile_y
526 if px and py and self.hospital:isInHospital(px,py) and queuing_patients[patient] == nil then
527 patient.has_passed_reception = true
528 end
529 end
530 end
531
532 --[[ Forces evacuation of the hospital - it makes ALL patients leave and storm out. ]]
533 function Epidemic:evacuateHospital()
534 for _, patient in ipairs(self.hospital.patients) do
535 if patient.has_passed_reception and
536 not patient.going_home and
537 not patient.going_to_die then
538 patient:goHome("evacuated")
539 end
540 end
541 end
542
543 --[[ Send the results of the cover up to the player - will be a
544 success/compensation or fail/fines + reputation hit]]
545 function Epidemic:sendResultFax()
546 self.world.ui.bottom_panel:queueMessage("report", self.cover_up_result_fax, nil, 24*20, 1)
547 self:announceEndOfEpidemic()
548 end
549
550 --[[ Spawns the inspector who will walk to the reception desk. ]]
551 function Epidemic:spawnInspector()
552 self.world.ui.adviser:say(_A.information.epidemic_health_inspector)
553 local inspector = self.world:newEntity("Inspector", 2, 2)
554 self.inspector = inspector
555 inspector:setType("Inspector")
556
557 local spawn_point = self.world.spawn_points[math.random(1, #self.world.spawn_points)]
558 inspector:setNextAction(SpawnAction("spawn", spawn_point))
559 inspector:setHospital(self.hospital)
560 inspector:queueAction(SeekReceptionAction())
561 end
562
563 --[[ Private function to check if a inspector already spawned.
564 @return (boolean) true if spawned already, false if not]]
565 function Epidemic:_inspectorSpawned()
566 return self.inspector ~= nil
567 end
568
569 --[[ Private function to check if a cover up is in progress
570 @return (boolean) true if cover up in progress, false if not]]
571 function Epidemic:_isCoverUpActive()
572 return self.coverup_selected and not self:_inspectorSpawned()
573 end
574
575 --[[ Is the patient "still" either idle queuing or sitting on a bench
576 Typically this is used to determine if a patient can be vaccinated
577 @param patient (Patient) the patient we wish to determine if they are static.]]
578 local function is_static(patient)
579 local action = patient:getCurrentAction()
580 return action.name == "queue" or action.name == "idle" or action.name == "seek_room" or
581 (action.name == "use_object" and action.object.object_type.id == "bench")
582 end
583
584 --[[ During a cover up every patient marked for vaccination (clicked)
585 creates a call to be vaccinated by a nurse - patients must also be static
586 (seated or standing queuing) ]]
587 function Epidemic:markedPatientsCallForVaccination()
588 for _, infected_patient in ipairs(self.infected_patients) do
589 if infected_patient.marked_for_vaccination and
590 not infected_patient.reserved_for and is_static(infected_patient) then
591 self.world.dispatcher:callNurseForVaccination(infected_patient)
592 end
593 end
594 end
595
596 --[[ In response to a vaccination call by the vaccination candidate
597 (@see makeVaccinationCandidateCallForNurse) perform the vaccination
598 actions or deal with call if unable to vaccinate.
599 @param patient (Patient) the patient who make the original call
600 @param nurse (Nurse) the nurse attempting to vaccinate the patient]]
601 function Epidemic:createVaccinationActions(patient,nurse)
602 patient.reserved_for = nurse
603 -- Check square is reachable first
604 local x,y = self:getBestVaccinationTile(nurse,patient)
605 -- If unreachable patient keep the call open for now
606 if not x or not y then
607 nurse:setCallCompleted()
608 patient.reserved_for = nil
609 nurse:setNextAction(MeanderAction())
610 patient:removeVaccinationCandidateStatus()
611 else
612 -- Give selected patient the cursor with the arrow once they are next
613 -- in line for vaccination i.e. call assigned
614 patient:giveVaccinationCandidateStatus()
615 local level_config = self.world.map.level_config
616 local fee = level_config.gbv.VacCost or 50
617 nurse:setDynamicInfoText(_S.dynamic_info.staff.actions.vaccine)
618 nurse:setNextAction(WalkAction(x, y):setMustHappen(true):enableWalkingToVaccinate())
619 nurse:queueAction(VaccinateAction(patient, fee))
620 end
621 end
622
623
624 --[[Find the best tile to stand on to vaccinate a patient
625 @param nurse (Nurse) the nurse performing the vaccination
626 @param patient (Patient) the patient to be vaccinated
627 @return best_x,best_y (Integer,nil) the best tiles to vaccinate from.]]
628 function Epidemic:getBestVaccinationTile(nurse, patient)
629 local px, py = patient.tile_x, patient.tile_y
630 -- If the patient is using a bench the best tile to use is
631 -- directly in front of them
632 local action = patient:getCurrentAction()
633 if action.name == "use_object" then
634 local object_in_use = action.object
635 if object_in_use.object_type.id == "bench" then
636 local direction = object_in_use.direction
637 if direction == "north" then
638 return px, py - 1
639
640 elseif direction == "south" then
641 return px, py + 1
642
643 elseif direction == "east" then
644 return px + 1, py
645
646 elseif direction == "west" then
647 return px - 1, py
648 end
649 end
650 end
651
652 -- General usage tile finder, used in the other cases
653 -- when the patient isn't sitting on a bench
654
655 -- Location of the nurse
656 local nx, ny = nurse.tile_x, nurse.tile_y
657
658 local best_x, best_y = nil
659 local shortest_distance = nil
660 local free_tiles = self.world.entity_map:getAdjacentFreeTiles(px, py)
661
662 for _, coord in ipairs(free_tiles) do
663 local x = coord['x']
664 local y = coord['y']
665
666 local distance = self.world:getPathDistance(nx, ny, x, y)
667 -- If the tile is reachable for the nurse
668 if distance then
669 -- If the tile is closer then it's a better choice
670 if not shortest_distance or distance < shortest_distance then
671 shortest_distance = distance
672 best_x, best_y = x, y
673 end
674 end
675 end
676 return best_x, best_y
677 end
678
679 --[[When the nurse is interrupted unreserve the patient and unassign the call.
680 @param nurse (Nurse) the nurse whose vaccination actions we are interrupting]]
681 function Epidemic:interruptVaccinationActions(nurse)
682 assert(class.is(nurse, Nurse))
683 local call = nurse.on_call
684 if call then
685 local patient = call.object
686 if patient and patient.vaccination_candidate and not patient.vaccinated then
687 patient:removeVaccinationCandidateStatus()
688 end
689 call.object.reserved_for = nil
690 call.assigned = nil
691 nurse.on_call = nil
692 end
693 end
694
695 --[[ Make the advisor show appropriate messages under certain
696 conditions of the epidemic.]]
697 function Epidemic:showAppropriateAdviceMessages()
698 if not self:_isCoverUpActive() then return end
699
700 if self.countdown_intervals then
701 if not self.has_said_hurry_up and self:countInfectedPatients() > 0 and
702 -- If only 1/4 of the countdown_intervals remaining on the timer
703 self.timer.open_timer == math.floor(self.countdown_intervals * 1 / 4) then
704 self.world.ui.adviser:say(_A.epidemic.hurry_up)
705 self.has_said_hurry_up = true
706
707 -- Wait until at least 1/4 of the countdown_intervals has expired before giving
708 -- this warning so it doesn't happen straight away
709 elseif self.timer.open_timer <= math.floor(self.countdown_intervals * 3 / 4) and
710 not self.has_said_serious and self:countInfectedPatients() > 10 then
711 self.world.ui.adviser:say(_A.epidemic.serious_warning)
712 self.has_said_serious = true
713 end
714 end
715 end
716
717 --[[ Are no infected patients, cured or still infected in the hospital?
718 @returns true if so, false otherwise. (boolean) ]]
719 function Epidemic:hasNoInfectedPatients()
720 return #self.infected_patients == 0
721 end
722
723 function Epidemic:tryAnnounceInspector()
724 if not self.coverup_selected then return end
725 if not self:_inspectorSpawned() then return end
726
727 local inspector = self.inspector
728 if not inspector.has_been_announced and
729 self.hospital:isInHospital(inspector.tile_x, inspector.tile_y) then
730 inspector:announce()
731 inspector.has_been_announced = true
732 end
733 end
734
735 --[[ For Cheat - Cancel the epidemic. ]]
736 function Epidemic:cancelEpidemic()
737 -- Remove init epidemic fax
738 self.world.ui.bottom_panel:deleteMessage(self)
739 -- Turn vaccination mode off if enabled
740 self:turnOffVaccinationMode()
741 -- Remove epidemic timer
742 if self.timer ~= nil then
743 self.timer:close()
744 end
745 -- Send inspector home if spawned
746 if self.inspector ~= nil then
747 self.inspector:goHome()
748 end
749 -- Clear epidemic patients
750 self:clearAllInfectedPatients()
751 self.infected_patients = {}
752 end
753
754 function Epidemic:afterLoad(old, new)
755 if old < 106 then
756 self.level_config = nil
757 end
758 if old < 212 then
759 self.coverup_selected = self.coverup_in_progress
760 self.coverup_in_progress = nil
761 end
762 end