Debian Codegraph @

CorsixTH/Lua/rooms/training.lua

raw
1 --[[ Copyright (c) 2010 Justin Pasher
2 Copyright (c) 2023 lewri
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE. --]]
21
22 local room = {}
23 room.id = "training"
24 room.vip_must_visit = false
25 room.level_config_id = 22
26 room.class = "TrainingRoom"
27 room.long_name = _S.rooms_long.training_room
28 room.name = _S.rooms_short.training_room
29 room.tooltip = _S.tooltip.rooms.training_room
30 room.objects_additional = { "extinguisher", "radiator", "plant", "bin", "lecture_chair", "bookcase", "skeleton" }
31 room.objects_needed = { lecture_chair = 1, projector = 1 }
32 room.build_preview_animation = 5086
33 room.categories = {
34 facilities = 4,
35 }
36 room.minimum_size = 4
37 room.wall_type = "green"
38 room.floor_tile = 17
39 room.has_no_queue_dialog = true
40
41 class "TrainingRoom" (Room)
42
43 ---@type TrainingRoom
44 local TrainingRoom = _G["TrainingRoom"]
45
46 function TrainingRoom:TrainingRoom(...)
47 self:Room(...)
48 end
49
50 function TrainingRoom:roomFinished()
51 -- Training rate and max occupancy based on objects in the room
52 local fx, fy = self:getEntranceXY(true)
53 local objects = self.world:findAllObjectsNear(fx, fy)
54 local chairs = 0
55 for object, _ in pairs(objects) do
56 if object.object_type.id == "lecture_chair" then
57 chairs = chairs + 1
58 end
59 end
60 -- Total staff occupancy: number of lecture chairs plus projector
61 self.maximum_staff = { Doctor = chairs + 1 }
62 self.training_factor = self:calculateTrainingFactor(objects)
63
64 -- Also tell the player if he/she doesn't have a consultant yet.
65 if self.hospital:countStaffOfCategory("Consultant", 1) == 0 then
66 self.hospital:giveAdvice({_A.room_requirements.training_room_need_consultant})
67 end
68 Room.roomFinished(self)
69 end
70
71 --! Determine the training factor of this room for teaching specialisms
72 --!param objects (table) the list of objects in this room
73 --!return total (number) The final training factor
74 function TrainingRoom:calculateTrainingFactor(objects)
75 -- Tally relevant objects that affect training
76 local function countTrainingObjects()
77 local counts = { projector = 0, skeleton = 0, bookcase = 0 }
78 for object, _ in pairs(objects) do
79 local obj_id = object.object_type.id
80 counts[obj_id] = counts[obj_id] and counts[obj_id] + 1 or nil
81 end
82 return counts
83 end
84 -- Work out total training factor using two averaging methods, choosing the best one
85 -- param factors(table) An array containing a count of each object and its raw
86 -- training value
87 -- returns the total effect
88 local function calculateTotalEffect(factors)
89 assert(factors,
90 "Unable to determine the training factor of a training room! " ..
91 "Reason: No inputs given")
92 local total_objects = 0
93 local total_value = 0
94 local num_factors = #factors
95 for _, factor in ipairs (factors) do
96 total_objects = total_objects + factor.count
97 -- Check we have at least one of this object and action accordingly
98 local multiplier = factor.count > 0 and (math.log(factor.count) + 1) or 0
99 total_value = total_value + (multiplier * factor.value)
100 end
101 assert(total_value > 0 and total_objects > 0,
102 "Unable to determine the training factor of a training room! " ..
103 "Reason: total_value and total_objects are both 0")
104 local average_1 = total_value / num_factors
105 local average_2 = total_value / total_objects
106 return math.max(average_1, average_2)
107 end
108
109 -- Object values and training rate set in level config
110 local level_config = self.world.map.level_config
111 local training_objects = countTrainingObjects()
112 -- Consolidate elements of training factor to an array
113 local training_value = level_config.gbv.TrainingValue
114 local training_factors = {
115 {count = training_objects.projector, value = training_value[0]},
116 {count = training_objects.skeleton, value = training_value[1]},
117 {count = training_objects.bookcase, value = training_value[2]},
118 }
119 return calculateTotalEffect(training_factors)
120 end
121
122 function TrainingRoom:getStaffCount()
123 local count = 0
124 for humanoid in pairs(self.humanoids) do
125 if class.is(humanoid, Staff) then
126 count = count + 1
127 end
128 end
129 return count
130 end
131
132 function TrainingRoom:testStaffCriteria(criteria, extra_humanoid)
133 if extra_humanoid and extra_humanoid.profile and
134 extra_humanoid.profile.is_consultant and self.staff_member then
135 -- Training room can only have one consultant
136 return false
137 end
138 return Room.testStaffCriteria(self, criteria, extra_humanoid)
139 end
140
141 function TrainingRoom:getTrainingFactor()
142 return self.training_factor
143 end
144
145 function TrainingRoom:getMaximumStaffCriteria()
146 return self.maximum_staff
147 end
148
149 function TrainingRoom:doStaffUseCycle(humanoid)
150 local projector, ox, oy = self.world:findObjectNear(humanoid, "projector")
151 humanoid:queueAction(WalkAction(ox, oy))
152 local projector_use_time = math.random(6,20)
153
154 local loop_callback_training = --[[persistable:training_loop_callback]] function()
155 projector_use_time = projector_use_time - 1
156 if projector_use_time == 0 then
157 local skeleton, sox, soy = self.world:findFreeObjectNearToUse(humanoid, "skeleton", "near")
158 local bookcase, box, boy = self.world:findFreeObjectNearToUse(humanoid, "bookcase", "near")
159 if math.random(0, 1) == 0 and bookcase then skeleton = nil end -- choose bookcase or skeleton
160 if skeleton then
161 humanoid:walkTo(sox, soy)
162 for _ = 1, math.random(3, 10) do
163 humanoid:queueAction(UseObjectAction(skeleton))
164 end
165 elseif bookcase then
166 humanoid:walkTo(box, boy)
167 for _ = 1, math.random(3, 10) do
168 humanoid:queueAction(UseObjectAction(bookcase))
169 end
170 end
171 -- go back to the projector
172 self:doStaffUseCycle(humanoid)
173 elseif projector_use_time < 0 then
174 -- reset variable to avoid potential overflow (over a VERY long
175 -- period of time)
176 projector_use_time = 0
177 end
178 end
179
180 humanoid:queueAction(UseObjectAction(projector):setLoopCallback(loop_callback_training))
181 end
182
183 function TrainingRoom:onHumanoidEnter(humanoid)
184 if not class.is(humanoid, Doctor) then
185 -- use default behavior for staff other than doctors
186 return Room.onHumanoidEnter(self, humanoid)
187 end
188
189 assert(not self.humanoids[humanoid], "Humanoid entering a room that they are already in")
190 humanoid.in_room = self
191 humanoid.last_room = self -- Remember where the staff was for them to come back after staffroom rest
192
193 humanoid:setCallCompleted()
194 self:commandEnteringStaff(humanoid)
195 self.humanoids[humanoid] = true
196 self:tryAdvanceQueue()
197 end
198
199 function TrainingRoom:commandEnteringStaff(humanoid)
200 local obj, ox, oy
201 local profile = humanoid.profile
202
203 if class.is(humanoid, Doctor) then
204 if profile.is_consultant then
205 -- Consultant entered/placed in room.
206 obj, ox, oy = self.world:findObjectNear(humanoid, "projector")
207 local projector = obj
208 -- Check if another consultant is teaching the lecture
209 if self.staff_member then
210 -- Release projector and dismiss current lector
211 projector.reserved_for = nil
212 self.staff_member:setNextAction(self:createLeaveAction(), true)
213 self.staff_member:queueAction(MeanderAction())
214 end
215 -- Start lecture with entered consultant
216 projector.reserved_for = humanoid
217 humanoid:walkTo(ox, oy)
218 self:doStaffUseCycle(humanoid)
219 self:setStaffMember(humanoid)
220 else
221 -- Student entered/placed in room
222 obj, ox, oy = self.world:findFreeObjectNearToUse(humanoid, "lecture_chair")
223 local lecture_chair = obj
224 -- If any chair available
225 if lecture_chair then
226 -- Student occupy chair
227 lecture_chair.reserved_for = humanoid
228 humanoid:walkTo(ox, oy)
229 humanoid:queueAction(UseObjectAction(obj))
230 humanoid:queueAction(MeanderAction())
231 else
232 -- Student leave room
233 self.hospital:giveAdvice({_A.staff_place_advice.not_enough_lecture_chairs})
234 humanoid:setNextAction(self:createLeaveAction())
235 humanoid:queueAction(MeanderAction())
236 humanoid.last_room = nil -- Prevent Doctor returning to this room automatically
237 end
238 end
239 elseif not class.is(humanoid, Handyman) then
240 self.hospital:giveAdvice({_A.staff_place_advice.only_doctors_in_room
241 :format(_S.rooms_long.training_room)})
242 humanoid:setNextAction(self:createLeaveAction())
243 humanoid:queueAction(MeanderAction())
244 return
245 end
246
247 return Room.commandEnteringStaff(self, humanoid, true)
248 end
249
250 function TrainingRoom:onHumanoidLeave(humanoid)
251 if class.is(humanoid, Doctor) then
252 -- unreserve whatever it was they we using
253 local fx, fy = self:getEntranceXY(true)
254 local objects = self.world:findAllObjectsNear(fx,fy)
255 for object, _ in pairs(objects) do
256 if object.reserved_for == humanoid then
257 object:removeReservedUser()
258 end
259 end
260
261 end
262
263 Room.onHumanoidLeave(self, humanoid)
264 end
265
266 function TrainingRoom:afterLoad(old, new)
267 if old < 234 then
268 -- Calculate the new training factor, unless we're currently editing the room
269 if self.built then
270 local fx, fy = self:getEntranceXY(true)
271 local objects = self.world:findAllObjectsNear(fx, fy)
272 self.training_factor = self:calculateTrainingFactor(objects)
273 end
274 end
275 Room.afterLoad(self, old, new)
276 end
277
278 return room