Debian Codegraph @

CorsixTH/Lua/humanoid_actions/vaccinate.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 class "VaccinateAction" (HumanoidAction)
22
23 ---@type VaccinateAction
24 local VaccinateAction = _G["VaccinateAction"]
25
26 -- Vaccinate action constructor for a nurse.
27 --!param patient (Patient) Patient to vaccinate.
28 --!param fee Amount of money to pay.
29 function VaccinateAction:VaccinateAction(patient, fee)
30 assert(class.is(patient, Patient), "Invalid value for parameter 'patient'")
31 assert(type(fee) == "number", "Invalid value for parameter 'fee'")
32
33 self:HumanoidAction("vaccinate")
34 self.patient = patient -- Patient to vaccinate.
35 self.vaccination_fee = fee -- Money to get from the patient.
36 self:setMustHappen(true)
37 end
38
39 local is_in_adjacent_square = permanent"vacc_adjacent_square"(
40 function (patient,nurse)
41 local x1, y1 = patient.tile_x, patient.tile_y
42 local x2, y2 = nurse.tile_x, nurse.tile_y
43
44 if not x1 or not x2 or not y1 or not y2 then
45 return false
46 end
47
48 -- Determine if they are in an adjacent square
49 local x_diff = math.abs(x1-x2)
50 local y_diff = math.abs(y1-y2)
51 if (x_diff + y_diff == 1) then
52 -- And neither of them are in a room so must be outside
53 return (not patient:getRoom() and not nurse:getRoom())
54 end
55 end)
56
57 local find_face_direction = permanent"vacc_find_face_direction"(
58 function(nurse, patient)
59 local nx, ny = nurse.tile_x, nurse.tile_y
60 local px, py = patient.tile_x, patient.tile_y
61
62 local x_diff = px - nx
63 local y_diff = py - ny
64
65 if x_diff == 1 then
66 return "east"
67 elseif x_diff == -1 then
68 return "west"
69 elseif y_diff == -1 then
70 return "north"
71 elseif y_diff == 1 then
72 return "south"
73 end
74 end)
75
76
77 local interrupt_vaccination = permanent"action_interrupt_vaccination"(
78 function(_, humanoid)
79 local epidemic = humanoid.hospital.epidemic
80 epidemic:interruptVaccinationActions(humanoid)
81 humanoid:setTimer(1, humanoid.finishAction)
82 end)
83
84
85 local function vaccinate(action, nurse)
86 assert(class.is(nurse, Nurse))
87
88 local patient = action.patient
89
90 local perform_vaccination = --[[persistable:action_perform_vaccination]] function()
91 -- Check if they STILL are in an adjacent square
92 if is_in_adjacent_square(nurse, patient) then
93 CallsDispatcher.queueCallCheckpointAction(nurse)
94 nurse:queueAction(AnswerCallAction())
95 patient:setVaccinatedStatus()
96 patient.hospital:spendMoney(action.vaccination_fee, _S.transactions.vaccination)
97 patient:updateDynamicInfo()
98 else
99 patient:removeVaccinationCandidateStatus()
100 -- Drop it they may not even be the vacc candidate anymore
101 CallsDispatcher.queueCallCheckpointAction(nurse)
102 nurse:queueAction(AnswerCallAction())
103 patient.reserved_for = nil
104 end
105 end
106
107 if is_in_adjacent_square(nurse,patient) then
108 local face_direction = find_face_direction(nurse,patient)
109 nurse:queueAction(IdleAction():setDirection(face_direction):setCount(5)
110 :setAfterUse(perform_vaccination):setOnInterrupt(interrupt_vaccination):setMustHappen(true))
111
112 else
113 patient:removeVaccinationCandidateStatus()
114 nurse:setCallCompleted()
115 patient.reserved_for = nil
116 nurse:queueAction(MeanderAction())
117 end
118 nurse:finishAction()
119 end
120
121 return vaccinate
122