Debian Codegraph @

CorsixTH/Lua/rooms/operating_theatre.lua

raw
1 --[[ Copyright (c) 2010 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 = "operating_theatre"
23 room.vip_must_visit = true
24 room.level_config_id = 10
25 room.class = "OperatingTheatreRoom"
26 room.name = _S.rooms_short.operating_theatre
27 room.long_name = _S.rooms_long.operating_theatre
28 room.tooltip = _S.tooltip.rooms.operating_theatre
29 room.objects_additional = { "extinguisher", "radiator", "plant", "bin" }
30 room.objects_needed = {
31 operating_table = 1,
32 surgeon_screen = 1,
33 op_sink1 = 1,
34 x_ray_viewer = 1
35 }
36 room.build_preview_animation = 5080
37 room.categories = {
38 treatment = 3,
39 }
40 room.minimum_size = 6
41 room.wall_type = "white"
42 room.floor_tile = 21
43 room.swing_doors = true
44 room.required_staff = {
45 Surgeon = 2,
46 }
47 room.call_sound = "reqd010.wav" -- TODO: There is also an unused sound
48 -- "Another surgeon needed [...]", reqd011.wav
49 -- room.handyman_call_sound = "maint007.wav" TODO: No sound for this room?
50
51 class "OperatingTheatreRoom" (Room)
52
53 ---@type OperatingTheatreRoom
54 local OperatingTheatreRoom = _G["OperatingTheatreRoom"]
55
56 function OperatingTheatreRoom:OperatingTheatreRoom(...)
57 self:Room(...)
58 self.staff_member_set = {}
59 end
60
61 function OperatingTheatreRoom:roomFinished()
62 -- Find the X-ray viewer
63 local fx, fy = self:getEntranceXY(true)
64 local objects = self.world:findAllObjectsNear(fx, fy)
65 for object in pairs(objects) do
66 local id = object.object_type.id
67 if id == "x_ray_viewer" then
68 self[id] = object
69 end
70 end
71 -- Tell the player what is missing, if anything.
72 if self.hospital:countRoomOfType("ward", 1) == 0 then
73 self.hospital:giveAdvice({_A.room_requirements.op_need_ward})
74 end
75 local numSurgeons = self.hospital:countStaffOfCategory("Surgeon", 2)
76 if numSurgeons == 0 then
77 self.hospital:giveAdvice({_A.room_requirements.op_need_two_surgeons})
78 elseif numSurgeons == 1 then
79 self.hospital:giveAdvice({_A.room_requirements.op_need_another_surgeon})
80 end
81 return Room.roomFinished(self)
82 end
83
84 local function wait_for_object(humanoid, obj, must_happen)
85 assert(type(must_happen) == "boolean", "must happen must be true or false")
86
87 local loop_callback_wait = --[[persistable:operatring_theatre_wait]] function(action)
88 if action.todo_interrupt or not obj.user then
89 humanoid:finishAction(action)
90 else
91 humanoid:queueAction(IdleAction():setCount(5):setMustHappen(true), 0)
92 end
93 end
94
95 return IdleAction():setMustHappen(must_happen):setLoopCallback(loop_callback_wait)
96 end
97
98 --! Builds the second operation action (i.e. with the surgeon whose we
99 --! see the back). Called either when the operation starts or when the
100 --! operation is resumed after interruption caused by the picking up of
101 --! the second surgeon.
102 --! Note: Must be part of OperatingTheatreRoom and not a local function
103 --! because of the use in the persisted callback function operation_standby.
104 --!param multi_use (action): the first operation action (built with via buildTableAction1()).
105 --!param operation_table_b (OperatingTable): slave object representing the operation table.
106 function OperatingTheatreRoom._buildTableAction2(multi_use, operation_table_b)
107 -- number of cutting and sewing animation
108 local num_loops = math.random(2, 5)
109
110 local loop_callback_use_object = --[[persistable:operatring_theatre_use_callback]] function(action)
111 num_loops = num_loops - 1
112 if num_loops <= 0 then
113 action.prolonged_usage = false
114 end
115 end
116
117 local after_use_use_object = --[[persistable:operatring_theatre_after_use]] function()
118 multi_use.prolonged_usage = false
119 end
120
121 return UseObjectAction(operation_table_b)
122 :setLoopCallback(loop_callback_use_object)
123 :setAfterUse(after_use_use_object)
124 :setMustHappen(true)
125 :disableTruncate()
126 :setUninterruptible(true)
127 end
128
129 function OperatingTheatreRoom:commandEnteringStaff(staff)
130 -- Put surgeon outfit on
131 local screen, screen_x, screen_y = self.world:findObjectNear(staff, "surgeon_screen")
132 staff:walkTo(screen_x, screen_y)
133 staff:queueAction(wait_for_object(staff, screen, false))
134 staff:queueAction(UseScreenAction(screen))
135
136 self.staff_member_set[staff] = true
137
138 -- Wait around for patients
139 local loop_callback_more_patients = --[[persistable:operatring_theatre_after_surgeon_clothes_on]] function()
140 self.staff_member_set[staff] = "ready"
141 self:tryAdvanceQueue()
142 end
143
144 staff:queueAction(MeanderAction()
145 :setMustHappen(true)
146 :setLoopCallback(loop_callback_more_patients))
147
148 -- Ensure that surgeons turn back into doctors when they leave
149 staff:queueAction(WalkAction(screen_x, screen_y)
150 :setMustHappen(true)
151 :setIsLeaving(true)
152 :truncateOnHighPriority())
153 staff:queueAction(UseScreenAction(screen)
154 :setMustHappen(true)
155 :setIsLeaving(true))
156
157 return Room.commandEnteringStaff(self, staff, true)
158 end
159
160 function OperatingTheatreRoom:setStaffMembersAttribute(attribute, value)
161 for staff_member, _ in pairs(self.staff_member_set) do
162 staff_member[attribute] = value
163 end
164 end
165
166 --! Builds the first operation action (i.e. with the surgeon whose we see the front).
167 --!param surgeon1 (Staff): the surgeon who does this operation action. He must
168 --! be the same as the surgeon who gets the action on his queue.
169 --!param patient (Patient): the patient to be operated.
170 --!param operation_table (OperatingTable): master object representing
171 --! the operation table.
172 function OperatingTheatreRoom:_buildTableAction1(surgeon1, patient, operation_table)
173 local loop_callback_multi_use = --[[persistable:operatring_theatre_multi_use_callback]] function()
174 -- dirty hack to make the truncated animation work
175 surgeon1.animation_idx = nil
176 end
177
178 local after_use_table = --[[persistable:operatring_theatre_table_after_use]] function()
179 self:dealtWithPatient(patient)
180 -- Tell the patient that it's time to leave, but only if the first action
181 -- is really an idle action.
182 if patient:getCurrentAction().name == "idle" then
183 patient:finishAction()
184 end
185 end
186
187 return MultiUseObjectAction(operation_table, patient)
188 :setProlongedUsage(true)
189 :setLoopCallback(loop_callback_multi_use)
190 :setAfterUse(after_use_table)
191 :setMustHappen(true)
192 :disableTruncate()
193 end
194
195 --! Sends the surgeon to the nearest operation sink ("op_sink1")
196 --! and makes him wash his hands
197 --!param before_other_actions (boolean): If true, add the actions at the front the action queue.
198 --! Add the actions at the end of the queue otherwise.
199 --! Default value is true.
200 function OperatingTheatreRoom:queueWashHands(surgeon, before_other_actions)
201 local sink, sink_x, sink_y = self.world:findObjectNear(surgeon, "op_sink1")
202 local walk = WalkAction(sink_x, sink_y)
203 :setMustHappen(true)
204 :disableTruncate()
205 local wait = wait_for_object(surgeon, sink, true)
206 :setMustHappen(true)
207 :disableTruncate()
208 :setUninterruptible(true)
209 local wash = UseObjectAction(sink)
210 :setMustHappen(true)
211 :disableTruncate()
212 :setUninterruptible(true)
213
214 for pos, action in pairs({walk, wait, wash}) do
215 if (before_other_actions) then
216 surgeon:queueAction(action, pos)
217 else
218 surgeon:queueAction(action)
219 end
220 end
221 end
222
223 --! Turn on/off x-ray viewer - if it's been found
224 --!param turn_on (boolean): true to switch on and false to switch off
225 function OperatingTheatreRoom:setXRayOn(turn_on)
226 if self.x_ray_viewer then
227 self.x_ray_viewer:setLayer(11, (turn_on and 2 or 0))
228 end
229 end
230
231 function OperatingTheatreRoom:commandEnteringPatient(patient)
232 -- Turn on x-ray viewer
233 self:setXRayOn(true)
234
235 -- Identify the staff
236 local surgeon1 = next(self.staff_member_set)
237 local surgeon2 = next(self.staff_member_set, surgeon1)
238 assert(surgeon1 and surgeon2, "Not enough staff in operating theatre")
239
240 local screen, sx, sy = self.world:findObjectNear(patient, "surgeon_screen")
241 -- Patient walk to surgeon screen
242 patient:setNextAction(WalkAction(sx, sy)
243 :setMustHappen(true)
244 :disableTruncate())
245 -- Patient changes into surgical gown
246 patient:queueAction(UseScreenAction(screen)
247 :setMustHappen(true)
248 :disableTruncate())
249
250 -- Meanwhile, surgeons wash their hands
251 -- TODO: They sometimes overlap each other when doing that. Can we avoid that?
252 -- TODO: The "op_sink2" animation is missing to fix that.
253 self:queueWashHands(surgeon1, true)
254 self:queueWashHands(surgeon2, true)
255
256 local num_ready = {0}
257 ----- BEGIN Save game compatibility -----
258 -- These function are merely for save game compatibility.
259 -- And they does not participate in the current game logic.
260 -- Do not move or edit
261 local --[[persistable:operatring_theatre_wait_for_ready]] function wait_for_ready(action)
262 action.on_interrupt = nil
263 if not action.done_ready then
264 num_ready[1] = num_ready[1] + 1
265 action.done_ready = true
266 end
267 if num_ready[1] == 3 then
268 surgeon1:finishAction()
269 surgeon2:finishAction()
270 end
271 end
272 local after_use = --[[persistable:operatring_theatre_after_multi_use]] function()
273 self:dealtWithPatient(patient)
274 patient:finishAction()
275 end
276 ----- END Save game compatibility -----
277
278 local --[[persistable:operatring_theatre_operation_standby]] function operation_standby(action, humanoid)
279 action.on_interrupt = nil
280 if not action.done_ready then
281 num_ready[1] = num_ready[1] + 1
282 action.done_ready = true
283 end
284 if self.staff_member_set[humanoid] and self.staff_member_set[humanoid] == "abort" then
285 humanoid:finishAction()
286 elseif num_ready[1] == 3 then
287 -- Only if everyone (2 Surgeons and Patient) ready, we schedule the operation action
288 local obj, _, _ = self.world:findObjectNear(surgeon1, "operating_table")
289
290 local table_action1 = self:_buildTableAction1(surgeon1, patient, obj)
291 surgeon1:queueAction(table_action1, 1)
292
293 obj, _, _ = self.world:findObjectNear(surgeon2, "operating_table_b")
294 local table_action2 = self._buildTableAction2(table_action1, obj)
295 surgeon2:queueAction(table_action2, 1)
296
297 -- Kick off
298 surgeon1:finishAction()
299 surgeon2:finishAction()
300 end
301 end
302
303 ---- Everyone standby...and sync start the operation
304 --
305 -- first surgeon walk over to the operating table to the patient side (surgeon face to camera)
306 local obj, ox, oy = self.world:findObjectNear(surgeon1, "operating_table")
307 surgeon1:queueAction(WalkAction(ox, oy)
308 :setMustHappen(true)
309 :disableTruncate(), 4)
310 surgeon1:queueAction(IdleAction()
311 :setLoopCallback(operation_standby)
312 :setMustHappen(true)
313 :disableTruncate()
314 :setUninterruptible(true), 5)
315
316 -- Patient walk to the side of the operating table
317 ox, oy = obj:getSecondaryUsageTile()
318 patient:queueAction(WalkAction(ox, oy)
319 :setMustHappen(true)
320 :disableTruncate())
321 patient:queueAction(IdleAction()
322 :setLoopCallback(operation_standby)
323 :setMustHappen(true))
324
325 -- Patient changes out of the gown afterwards
326 patient:queueAction(WalkAction(sx, sy)
327 :setMustHappen(true)
328 :disableTruncate())
329 patient:queueAction(UseScreenAction(screen)
330 :setMustHappen(true))
331
332 -- Meanwhile, second surgeon walks over to other side of operating table (surgeon back to camera)
333 local _
334 _, ox, oy = self.world:findObjectNear(surgeon1, "operating_table_b")
335 surgeon2:queueAction(WalkAction(ox, oy):setMustHappen(true)
336 :disableTruncate(), 4)
337 surgeon2:queueAction(IdleAction()
338 :setLoopCallback(operation_standby)
339 :setMustHappen(true)
340 :disableTruncate()
341 :setUninterruptible(true), 5)
342
343 return Room.commandEnteringPatient(self, patient)
344 end
345
346 function OperatingTheatreRoom:onHumanoidLeave(humanoid)
347 self.staff_member_set[humanoid] = nil
348
349 if class.is(humanoid, Patient) then
350 -- Turn off x-ray viewer
351 -- (FIXME: would be better when patient dress back?)
352 self:setXRayOn(false)
353
354 local surgeon1 = next(self.staff_member_set)
355 local surgeon2 = next(self.staff_member_set, surgeon1)
356 if surgeon1 then
357 self.staff_member_set[surgeon1] = "abort"
358 end
359 if surgeon2 then
360 self.staff_member_set[surgeon2] = "abort"
361 end
362 end
363 return Room.onHumanoidLeave(self, humanoid)
364 end
365
366 function OperatingTheatreRoom:canHumanoidEnter(humanoid)
367 local can = Room.canHumanoidEnter(self, humanoid)
368 if can and class.is(humanoid, Patient) then
369 -- Patients can only enter once all doctors are in surgeon clothes
370 for staff, is_ready in pairs(self.staff_member_set) do
371 if not staff:isType("Surgeon") or is_ready ~= "ready" then
372 return false
373 end
374 end
375 end
376 return can
377 end
378
379 return room