Debian Codegraph @

CorsixTH/Lua/rooms/gp.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 local room = {}
22 room.id = "gp"
23 room.vip_must_visit = false
24 room.level_config_id = 7
25 room.class = "GPRoom"
26 room.name = _S.rooms_short.gps_office
27 room.long_name = _S.rooms_long.gps_office
28 room.tooltip = _S.tooltip.rooms.gps_office
29 room.objects_additional = { "extinguisher", "radiator", "plant", "bin" }
30 room.objects_needed = { desk = 1, cabinet = 1, chair = 1 }
31 room.build_preview_animation = 900
32 room.categories = {
33 diagnosis = 1,
34 }
35 room.minimum_size = 4
36 room.wall_type = "white"
37 room.floor_tile = 18
38 room.required_staff = {
39 Doctor = 1,
40 }
41 room.maximum_staff = room.required_staff
42 room.call_sound = "reqd008.wav"
43
44 class "GPRoom" (Room)
45
46 ---@type GPRoom
47 local GPRoom = _G["GPRoom"]
48
49 function GPRoom:GPRoom(...)
50 self:Room(...)
51 end
52
53 function GPRoom:doStaffUseCycle(humanoid)
54 local obj, ox, oy = self.world:findObjectNear(humanoid, "cabinet")
55 humanoid:walkTo(ox, oy)
56 humanoid:queueAction(UseObjectAction(obj))
57
58 obj, ox, oy = self.world:findObjectNear(humanoid, "desk")
59 humanoid:queueAction(WalkAction(ox, oy))
60
61 -- A skilled doctor requires less time at the desk to diagnose the patient
62 local inv_skill = 1 - humanoid.profile.skill
63 local desk_use_time = math.random(math.floor(3 + 5 * inv_skill),
64 math.ceil (8 + 10 * inv_skill))
65 local gp_loop_callback = --[[persistable:gp_loop_callback]] function()
66 desk_use_time = desk_use_time - 1
67 if desk_use_time == 0 then
68 -- Consultants who aren't tired might not need to stretch their legs
69 -- to remain alert, so might just remain at the desk and deal with the
70 -- next patient quicker.
71 if humanoid.profile.is_consultant and
72 math.random() >= humanoid:getAttribute("fatigue") then
73 desk_use_time = math.random(7, 14)
74 else
75 self:doStaffUseCycle(humanoid)
76 end
77
78 local patient = self:getPatient()
79 if patient then
80 if math.random() <= (0.7 + 0.3 * humanoid.profile.skill) or self.max_times <= 0 then
81 if patient.user_of and not class.is(patient.user_of, Door) then
82 self:dealtWithPatient(patient)
83 end
84 else
85 self.max_times = self.max_times - 1
86 end
87 end
88 end
89 end
90
91 humanoid:queueAction(UseObjectAction(obj):setLoopCallback(gp_loop_callback))
92 end
93
94 function GPRoom:commandEnteringStaff(humanoid)
95 self.staff_member = humanoid
96 self:doStaffUseCycle(humanoid)
97 return Room.commandEnteringStaff(self, humanoid, true)
98 end
99
100 function GPRoom:commandEnteringPatient(humanoid)
101 local obj, ox, oy = self.world:findObjectNear(humanoid, "chair")
102 humanoid:walkTo(ox, oy)
103 humanoid:queueAction(UseObjectAction(obj))
104 self.max_times = 3
105 return Room.commandEnteringPatient(self, humanoid)
106 end
107
108 function GPRoom:shouldHavePatientReenter(patient)
109 return not patient.diagnosed and Room.shouldHavePatientReenter(self, patient)
110 end
111
112 function GPRoom:dealtWithPatient(patient)
113 patient = patient or self:getPatient()
114
115 -- If patients are slow to leave the chair, and staff are quick in their
116 -- usage cycle, then dealtWithPatient() might get called twice for the
117 -- same patient, in which case the second call must be ignored (otherwise
118 -- if the first call resulted in the patient being diagnosed, the following
119 -- logic would cause the patient to leave the room and stand indefinitely).
120 if patient == self.just_dealt_with then
121 return
122 else
123 self.just_dealt_with = patient
124 end
125
126 patient:setNextAction(self:createLeaveAction())
127 patient:addToTreatmentHistory(self.room_info)
128
129 -- If the patient got sent to the wrong room and needs telling where
130 -- to go next - this happens when a disease changes for an epidemic
131 if patient.needs_redirecting then
132 self:sendPatientToNextDiagnosisRoom(patient)
133 patient.needs_redirecting = false
134 elseif patient.disease and not patient.diagnosed then
135 self.hospital:receiveMoneyForTreatment(patient)
136 patient:completeDiagnosticStep(self)
137 local no_need_diagnose_further =
138 patient.diagnosis_progress >= self.hospital.policies["stop_procedure"]
139 local cant_diagnose_further_but_can_set_diagnosis =
140 (patient.diagnosis_progress >= 1.0) and (not patient:hasMoreDiagnosisRoomsAvailable())
141 if no_need_diagnose_further or cant_diagnose_further_but_can_set_diagnosis then
142 patient:setDiagnosed()
143 if patient:agreesToPay(patient.disease.id) then
144 patient:queueAction(SeekRoomAction(patient.disease.treatment_rooms[1]):enableTreatmentRoom())
145 else
146 patient:goHome("over_priced", patient.disease.id)
147 end
148
149 self.staff_member:setMood("idea3", "activate") -- Show the light bulb over the doctor
150 -- Check if this disease has just been discovered
151 if not self.hospital.disease_casebook[patient.disease.id].discovered then
152 self.hospital.research:discoverDisease(patient.disease)
153 end
154 else
155 self:sendPatientToNextDiagnosisRoom(patient)
156 end
157 else
158 patient:queueAction(MeanderAction():setCount(2))
159 patient:queueAction(IdleAction())
160 end
161
162 if self.staff_member then
163 self:setStaffMembersAttribute("dealing_with_patient", false)
164 end
165 end
166
167 function GPRoom:sendPatientToNextDiagnosisRoom(patient)
168 if not patient:hasMoreDiagnosisRoomsAvailable() then
169 -- The very rare case where the patient has visited all his/her possible diagnosis rooms
170 -- There's not much to do then... Send home
171 patient:goHome("kicked")
172 patient:setDynamicInfoText(_S.dynamic_info.patient.actions.no_diagnoses_available)
173 else
174 self.staff_member:setMood("reflexion", "activate") -- Show the uncertainty mood over the doctor
175 local next_room_id = math.random(1, #patient.available_diagnosis_rooms)
176 local next_room = patient.available_diagnosis_rooms[next_room_id]
177 if patient:agreesToPay("diag_" .. next_room) then
178 patient:queueAction(SeekRoomAction(next_room):setDiagnosisRoom(next_room_id))
179 else
180 patient:goHome("over_priced", "diag_" .. next_room)
181 end
182 end
183 end
184
185 function GPRoom:onHumanoidLeave(humanoid)
186 -- Reset moods when either the patient or the doctor leaves the room.
187 if not class.is(humanoid, Handyman) then
188 for staff, _ in pairs(self.humanoids) do
189 staff:setMood("idea3", "deactivate")
190 staff:setMood("reflexion", "deactivate")
191 end
192 end
193 if self.staff_member == humanoid then
194 self.staff_member = nil
195 end
196 if self.just_dealt_with == humanoid then
197 self.just_dealt_with = nil
198 end
199 Room.onHumanoidLeave(self, humanoid)
200 end
201
202 function GPRoom:roomFinished()
203 if self.hospital:countStaffOfCategory("Doctor") == 0 and
204 not self.world.ui.start_tutorial then
205 self.hospital:giveAdvice({_A.room_requirements.gps_office_need_doctor})
206 end
207 return Room.roomFinished(self)
208 end
209
210 return room