Debian Codegraph @

CorsixTH/Lua/rooms/ward.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 = "ward"
23 room.vip_must_visit = true
24 room.level_config_id = 9
25 room.class = "WardRoom"
26 room.name = _S.rooms_short.ward
27 room.tooltip = _S.tooltip.rooms.ward
28 room.long_name = _S.rooms_long.ward
29 room.objects_additional = {
30 "extinguisher",
31 "radiator",
32 "plant",
33 "desk",
34 "bin",
35 "bed" }
36 room.objects_needed = { desk = 1, bed = 1 }
37 room.build_preview_animation = 910
38 room.categories = {
39 treatment = 2,
40 diagnosis = 9,
41 }
42 room.minimum_size = 6
43 room.wall_type = "white"
44 room.floor_tile = 21
45 room.swing_doors = true
46 room.required_staff = {
47 Nurse = 1,
48 }
49
50 room.call_sound = "reqd009.wav"
51
52 class "WardRoom" (Room)
53
54 ---@type WardRoom
55 local WardRoom = _G["WardRoom"]
56
57 function WardRoom:WardRoom(...)
58 self:Room(...)
59 self.staff_member_set = {}
60 self.healing_amount = 0 -- The size of progress towards a diagnostic step
61 end
62
63 function WardRoom:roomFinished()
64 local fx, fy = self:getEntranceXY(true)
65 local objects = self.world:findAllObjectsNear(fx, fy)
66 local beds = 0
67 local desks = 0
68 for object, _ in pairs(objects) do
69 if object.object_type.id == "bed" then
70 beds = beds + 1
71 end
72 if object.object_type.id == "desk" then
73 desks = desks + 1
74 end
75 end
76 self.maximum_staff = {
77 Nurse = desks,
78 }
79 self.maximum_patients = beds
80 if self.hospital:countStaffOfCategory("Nurse", 1) == 0 then
81 self.hospital:giveAdvice({_A.room_requirements.ward_need_nurse})
82 end
83 Room.roomFinished(self)
84 end
85
86 function WardRoom:getMaximumStaffCriteria()
87 return self.maximum_staff
88 end
89
90 function WardRoom:commandEnteringStaff(humanoid)
91 self.staff_member_set[humanoid] = true
92 self:doStaffUseCycle(humanoid)
93 return Room.commandEnteringStaff(self, humanoid, true)
94 end
95
96 function WardRoom:doStaffUseCycle(humanoid)
97 local meander_time = math.random(4, 10)
98 humanoid:setNextAction(MeanderAction():setCount(meander_time))
99
100 local obj, ox, oy = self.world:findFreeObjectNearToUse(humanoid, "desk")
101 if obj then
102 obj.reserved_for = humanoid
103 humanoid:walkTo(ox, oy)
104 if obj.object_type.id == "desk" then
105 local desk_use_time = math.random(7, 14)
106 local desk_loop = --[[persistable:ward_desk_loop_callback]] function()
107 desk_use_time = desk_use_time - 1
108 if desk_use_time == 0 then
109 self:doStaffUseCycle(humanoid)
110 end
111 end
112
113 humanoid:queueAction(UseObjectAction(obj):setLoopCallback(desk_loop))
114 end
115 end
116
117 local num_meanders = math.random(2, 4)
118 local meanders_loop = --[[persistable:ward_meander_loop_callback]] function()
119 num_meanders = num_meanders - 1
120 if num_meanders == 0 then
121 self:doStaffUseCycle(humanoid)
122 end
123 end
124 humanoid:queueAction(MeanderAction():setLoopCallback(meanders_loop))
125 end
126
127 function WardRoom:updateHealingAmount()
128 local patient_count = 1
129 local nurse_factor = 0
130 for humanoid in pairs(self.humanoids) do
131 if not humanoid:isLeaving() then
132 if class.is(humanoid, Patient) then
133 patient_count = patient_count + 1
134 elseif class.is(humanoid, Nurse) and not humanoid.fired then
135 nurse_factor = nurse_factor + 0.5 + humanoid:getServiceQuality()
136 end
137 end
138 end
139 if nurse_factor > 0 then
140 -- Difficulty of healing - 10% faster on easier, 10% slower on hard mode
141 local difficulty_factor = 0.8 + self.world.map:getDifficulty() * 0.1
142 self.healing_amount = nurse_factor / math.log(patient_count) / difficulty_factor
143 else
144 self.healing_amount = 0
145 end
146 end
147
148
149 function WardRoom:commandEnteringPatient(patient)
150 local bed, pat_x, pat_y = self.world:findFreeObjectNearToUse(patient, "bed")
151 self:setStaffMembersAttribute("dealing_with_patient", nil)
152 if not bed then
153 patient:setNextAction(self:createLeaveAction())
154 patient:queueAction(self:createEnterAction(patient))
155 self.world:gameLog("Warning: A patient was called into the ward even though there are no free beds.")
156 return Room.commandEnteringPatient(self, patient)
157 end
158
159 bed.reserved_for = patient
160 -- Old callback kept for persistence in savegame version 155, April 2021
161 local --[[persistable:ward_loop_callback]] function _(action)
162 if length <= 0 then -- luacheck: ignore 113
163 action.prolonged_usage = false
164 end
165 length = length - 1 -- luacheck: ignore 111 113
166 end
167 -- New callback
168 local --[[persistable:ward_loop_callback2]] function loop_callback(action)
169 action.remaining_work = action.remaining_work - self.healing_amount
170 if action.remaining_work <= 0 then -- The patient is diagnosed or healed
171 action.prolonged_usage = false
172 end
173 end
174
175 local after_use = --[[persistable:ward_after_use]] function()
176 self:dealtWithPatient(patient)
177 end
178 patient:walkTo(pat_x, pat_y)
179 local bed_action = UseObjectAction(bed):setProlongedUsage(true)
180 :setLoopCallback(loop_callback):setAfterUse(after_use)
181 bed_action.remaining_work = math.random(150, 600)
182 patient:queueAction(bed_action)
183
184 return Room.commandEnteringPatient(self, patient)
185 end
186
187 function WardRoom:setStaffMember(staff)
188 self.staff_member_set[staff] = true
189 end
190
191 function WardRoom:setStaffMembersAttribute(attribute, value)
192 for staff_member, _ in pairs(self.staff_member_set) do
193 staff_member[attribute] = value
194 end
195 end
196
197 function WardRoom:onHumanoidLeave(humanoid)
198 self.staff_member_set[humanoid] = nil
199 Room.onHumanoidLeave(self, humanoid)
200 self:updateHealingAmount()
201 end
202
203 function WardRoom:onHumanoidEnter(humanoid)
204 Room.onHumanoidEnter(self, humanoid)
205 self:updateHealingAmount()
206 end
207
208 function WardRoom:afterLoad(old, new)
209 if old < 11 then
210 -- Make sure all three tiles outside of the door are unbuildable.
211 local door = self.door
212 local x = door.tile_x
213 local y = door.tile_y
214 local dir = door.direction
215 local map = self.world.map.th
216 local flags = {}
217
218 local function checkLocation(xpos, ypos)
219 if self.world:getRoom(xpos, ypos) or not map:getCellFlags(xpos, ypos, flags).passable then
220 local message = "Warning: An update has resolved a problem concerning " ..
221 "swing doors, but not all tiles adjacent to them could be fixed."
222 self.world.ui:addWindow(UIInformation(self.world.ui, {message}))
223 return false
224 end
225 return true
226 end
227 if dir == "west" then -- In west or east wall
228 if self.world:getRoom(x, y) == self then -- In west wall
229 if checkLocation(x - 1, y + 1) then
230 map:setCellFlags(x - 1, y + 1, {buildable = false})
231 end
232 else -- East wall
233 if checkLocation(x, y + 1) then
234 map:setCellFlags(x, y + 1, {buildable = false})
235 end
236 end
237 else -- if dir == "north", North or south wall
238 if self.world:getRoom(x, y) == self then -- In north wall
239 if checkLocation(x + 1, y - 1) then
240 map:setCellFlags(x + 1, y - 1, {buildable = false})
241 end
242 else -- South wall
243 if checkLocation(x + 1, y) then
244 map:setCellFlags(x + 1, y, {buildable = false})
245 end
246 end
247 end
248 end
249 if old < 74 then
250 -- add some new variables
251 self.staff_member_set = {}
252 self.nursecount = 0
253 -- reset any wards that already exist
254 self:roomFinished()
255 -- if there is already a nurse in the ward
256 -- make her leave so she gets counted properly
257 local nurse = self.staff_member
258 if nurse then
259 nurse:setNextAction(self:createLeaveAction())
260 nurse:queueAction(MeanderAction())
261 end
262 end
263 Room.afterLoad(self, old, new)
264 end
265
266 return room