Debian Codegraph @

CorsixTH/Lua/entities/humanoids/staff/doctor.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
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 corsixth.require("announcer")
23 corsixth.require("entities.humanoids.staff")
24
25 local AnnouncementPriority = _G["AnnouncementPriority"]
26
27 --! A Doctor (Researcher,Surgan,Psychologist)
28 class "Doctor" (Staff)
29
30 ---@type Doctor
31 local Doctor = _G["Doctor"]
32
33 --!param ... Arguments to base class constructor.
34 function Doctor:Doctor(...)
35 self:Staff(...)
36 self.leave_sounds = {"sack001.wav", "sack002.wav", "sack003.wav"}
37 end
38
39 function Doctor:tickDay()
40 if not Staff.tickDay(self) then
41 return false
42 end
43
44 -- if you overwork your Dr's then there is a chance that they can go crazy
45 -- when this happens, find him and get him to rest straight away
46 if not self:isVeryTired() then
47 if self:isResting() then
48 self:setCrazy(false)
49 end
50 else
51 -- doctor can go crazy if they're too tired
52 if math.random(1, 300) == 1 then
53 self:setCrazy(true)
54 end
55 end
56
57 -- is self researcher in research room?
58 if self:isResearching() then
59 self.hospital.research:addResearchPoints(1550 + 1000 * self.profile.skill)
60 -- Are we learning new skills today?
61 elseif self:isLearning() then
62 local room = self:getRoom()
63 local consultant = room.staff_member
64 local room_factor = room:getTrainingFactor()
65 -- When counting room occupancy, we must subtract 1 for the consultant in the room.
66 -- Currently, handymen/newly trained consultants also get counted here as they
67 -- could be considered distracting to current trainees.
68 local student_count = room:getStaffCount() - 1
69 self:trainSkills(consultant, room_factor, student_count)
70 end
71 end
72
73 function Doctor:tick()
74 Staff.tick(self)
75 -- don't do anything if they're fired or picked up or have no hospital
76 if self.fired or self.pickup or not self.hospital or self.dead then
77 return
78 end
79
80 -- if doctor is in a room and they're using an object
81 -- then their skill level will increase _slowly_ over time
82 if self:isLearningOnTheJob() then
83 self:updateSkill("skill", 0.000003)
84 end
85 end
86
87 -- Determine if the staff member should contribute to research
88 function Doctor:isResearching()
89 local room = self:getRoom()
90
91 -- Doctor is in research lab, is qualified, and is not leaving the hospital.
92 return room and room.room_info.id == "research" and
93 self.profile.is_researcher >= 1.0 and self.hospital
94 end
95
96 -- Determine if the staff member should increase their skills
97 function Doctor:isLearning()
98 local room = self:getRoom()
99 -- Check that the doctor is a trainee (currently in a chair and not a consultant) in
100 -- a training room.
101 if self.profile.is_consultant then return false end
102 return room and room.room_info.id == "training" and room.staff_member and
103 self:getCurrentAction().name == "use_object" and
104 self:getCurrentAction().object.object_type.id == "lecture_chair"
105 end
106
107 function Doctor:isLearningOnTheJob()
108 local room = self:getRoom()
109
110 -- Doctor is in room but not training room, staff room, or toilets; is a doctor; and is using something
111 return room and room.room_info.id ~= "training" and
112 room.room_info.id ~= "staff_room" and room.room_info.id ~= "toilets" and
113 self:getCurrentAction().name == "use_object"
114 end
115
116 function Doctor:setProfile(profile)
117 Staff.setProfile(self, profile)
118 self:updateStaffTitle()
119 end
120
121 --! Assign skill points
122 --!param trait (string) The trait of focus
123 --!param amount (number) The amount granted
124 function Doctor:updateSkill(trait, amount)
125 local old_profile = {
126 is_junior = self.profile.is_junior,
127 is_consultant = self.profile.is_consultant
128 }
129
130 -- don't push further when they are already at 100%+
131 if self.profile[trait] >= 1.0 then
132 return
133 end
134
135 self.profile[trait] = self.profile[trait] + amount
136 if self.profile[trait] >= 1.0 then
137 self.profile[trait] = 1.0
138 local is = trait:match("^is_(.*)")
139 if is == "surgeon" or is == "psychiatrist" or is == "researcher" then
140 self.hospital:giveAdvice({ _A.information.promotion_to_specialist:format(_S.staff_title[is]) })
141 -- patients might we waiting for a doctor with this skill, notify them
142 self.hospital:notifyOfStaffChange(self)
143 end
144 self:updateStaffTitle()
145 end
146
147 if trait == "skill" then
148 self.profile:parseSkillLevel()
149
150 if old_profile.is_junior and not self.profile.is_junior then
151 self.hospital:giveAdvice({ _A.information.promotion_to_doctor })
152 self:updateStaffTitle()
153 elseif not old_profile.is_consultant and self.profile.is_consultant then
154 self.hospital:giveAdvice({ _A.information.promotion_to_consultant })
155 if self:getRoom().room_info.id == "training" then
156 self:setNextAction(self:getRoom():createLeaveAction())
157 self:queueAction(MeanderAction())
158 self.last_room = nil -- Consultant no longer needs to return to this room
159 end
160 self:updateStaffTitle()
161 end
162 end
163 end
164
165 --! Confirm doctor has specialism
166 --!param trait (string) what to check (in is_xxx format)
167 --!return true if specialised
168 function Doctor:hasSpecialism(trait)
169 local specialisms = {
170 ["is_surgeon"] = self.profile.is_surgeon >= 1,
171 ["is_psychiatrist"] = self.profile.is_psychiatrist >= 1,
172 ["is_researcher"] = self.profile.is_researcher >= 1,
173 }
174 return specialisms[trait]
175 end
176
177 --! Generate the performance factor of the training room
178 --!param doc_atd (number) Attention to detail of student
179 --!param con_skill (number) Skill level of consultant
180 --!param class_size (number) Total students in the training room
181 --!return calculated performance
182 function Doctor._calculateTrainingPerformance(doc_atd, con_skill, class_size)
183 -- Clamp a performance modifier's impact between 90% and 110%
184 local function clamp(value)
185 local base, range = 0.9, 0.2 -- specify the +-10% variability
186 local factor = value * range
187 return base + factor
188 end
189
190 local performers = {
191 clamp(doc_atd), -- doctor focus
192 clamp(con_skill * con_skill), -- teacher ability. ~1.0 modifier at 0.75 skill
193 math.t_random(0.9, 1, 1.1), -- Allow some randomness
194 }
195 -- Average the three performance modifiers to generate a single performance factor
196 local p_impact = 0
197 for i = 1, #performers do
198 p_impact = p_impact + performers[i]
199 end
200 p_impact = p_impact / #performers
201
202 -- Class size impacts performance when there are more than 3 students at rate 2/n-1
203 local c_impact = class_size >= 3 and 2 / (class_size - 1) or 1
204
205 return p_impact * c_impact
206 end
207
208 --! Calculates the amount of skill to learn towards becoming a doctor/consultant and
209 --! any amount towards a specialist skill being taught
210 --! There are detailed explanations on the logic to these calculations in the Wiki
211 --!param consultant (entity) The person teaching the student
212 --!param room_factor (number) What score training objects in the room give
213 --!param student_count (number) Total number of students present
214 function Doctor:trainSkills(consultant, room_factor, student_count)
215 local level_config = self.world.map.level_config
216 local general_factor = level_config.gbv.TrainingRate
217
218 local MIN_LEARN_VALUE = 0.001 -- Doctor always advances at least this amount
219 local g_scale = 10000 -- scales generalist calculation
220 local s_scale = 12 -- scales specialism calculation
221
222 local function countSkillsTaught()
223 local num_skills = 1 -- General skill is always counted
224 local skills = {"is_surgeon", "is_psychiatrist", "is_researcher"}
225 for _, skill in pairs(skills) do
226 if consultant:hasSpecialism(skill) and not self:hasSpecialism(skill) then
227 num_skills = num_skills + 1
228 end
229 end
230 return num_skills
231 end
232
233 local skills_factor = 1 / countSkillsTaught()
234 local class_performance = self._calculateTrainingPerformance(
235 self.profile.attention_to_detail,
236 consultant.profile.skill,
237 student_count)
238
239 -- Calculate the simpler, generalist skill, which is always awarded while training.
240 local g_train = (general_factor * skills_factor * class_performance) / g_scale
241 self:updateSkill("skill", math.max(g_train, MIN_LEARN_VALUE))
242
243 -- Now begin calculations for specialist skills
244 local thresholds = {
245 ["is_surgeon"] = level_config.gbv.AbilityThreshold[0],
246 ["is_psychiatrist"] = level_config.gbv.AbilityThreshold[1],
247 ["is_researcher"] = level_config.gbv.AbilityThreshold[2],
248 }
249
250 for name, threshold in pairs(thresholds) do
251 if consultant:hasSpecialism(name) and not self:hasSpecialism(name) then
252 local base = room_factor / threshold
253 local s_train = (base * skills_factor * class_performance) / s_scale
254 self:updateSkill(name, math.max(s_train, MIN_LEARN_VALUE))
255 end
256 end
257 end
258
259 function Doctor:updateStaffTitle()
260 local profile = self.profile
261 local professions = ""
262 local number = 0
263 if profile.is_junior then
264 professions = _S.staff_title.junior .. " "
265 number = 1
266 elseif profile.is_consultant then
267 professions = _S.staff_title.consultant .. " "
268 number = 1
269 end
270 if profile.is_researcher >= 1.0 then
271 professions = professions .. _S.staff_title.researcher .. " "
272 number = number + 1
273 end
274 if profile.is_surgeon >= 1.0 then
275 professions = professions .. _S.staff_title.surgeon .. " "
276 number = number + 1
277 end
278 if profile.is_psychiatrist >= 1.0 then
279 if number < 3 then
280 professions = professions .. _S.staff_title.psychiatrist
281 else
282 professions = professions .. _S.dynamic_info.staff.psychiatrist_abbrev
283 end
284 end
285
286 if professions == "" then
287 professions = _S.staff_title.doctor
288 end
289 self.profile.profession = professions
290 end
291
292 function Doctor:setCrazy(crazy)
293 if crazy then
294 -- make doctor crazy
295 if not self.is_crazy then
296 self:setLayer(5, self.profile.layer5 + 4)
297 self.hospital:giveAdvice({_A.warnings.doctor_crazy_overwork})
298 self.is_crazy = true
299 end
300 else
301 -- make doctor sane
302 if self.is_crazy then
303 if self.layers[5] >= 5 then
304 self:setLayer(5, self.layers[5] - 4)
305 self.is_crazy = false
306 end
307 end
308 end
309 end
310
311 function Doctor:onPickup()
312 Staff.onPickup(self)
313 self:resetSurgeonState()
314 end
315
316 -- Function resets "Surgeon" state for Doctor.
317 -- Useful for case when on picking up Doctor from Operating Theatre
318 -- we need to switching him back to regular clothes and type.
319 function Doctor:resetSurgeonState()
320 if self:isType("Surgeon") then
321 self:setType("Doctor")
322 end
323 end
324
325 local profile_attributes = {
326 Psychiatrist = "is_psychiatrist",
327 Surgeon = "is_surgeon",
328 Researcher = "is_researcher",
329 Junior = "is_junior",
330 Consultant = "is_consultant",
331 }
332
333 -- Helper function to decide if Staff fulfills a criterion
334 -- (one of "Doctor", "Nurse", "Psychiatrist", "Surgeon", "Researcher" and "Handyman", "Receptionist", "Junior", "Consultant")
335 function Doctor:fulfillsCriterion(criterion)
336 if criterion == "Doctor" then
337 return true
338 end
339 if self.profile and self.profile[profile_attributes[criterion]] == 1.0 then
340 return true
341 end
342 return false
343 end
344
345 function Doctor:adviseWrongPersonForThisRoom()
346 local room = self:getRoom()
347 local room_name = room.room_info.long_name
348 if room.room_info.id == "toilets" then
349 self.hospital:giveAdvice({ _A.staff_place_advice.doctors_cannot_work_in_room:format(room_name) })
350 elseif room.room_info.id == "training" then
351 self.hospital:giveAdvice({ _A.staff_place_advice.doctors_cannot_work_in_room:format(room_name) })
352 else
353 Staff.adviseWrongPersonForThisRoom(self)
354 end
355 end
356
357 function Doctor:afterLoad(old, new)
358 if old < 163 then
359 self.leave_priority = AnnouncementPriority.High
360 self.leave_sounds = {"sack001.wav", "sack002.wav", "sack003.wav"}
361 end
362 Staff.afterLoad(self, old, new)
363 end
364
365 --[[ Return string representation
366 ! Adds Doctor statistics for a "Doctor" object
367 !return (string)
368 ]]
369 function Doctor:tostring()
370 local result = Humanoid.tostring(self)
371 result = result .. string.format("\nSkills: (%.3f) Surgeon (%.3f) Psych (%.3f) Researcher (%.3f)",
372 self.profile.skill or 0,
373 self.profile.is_surgeon or 0,
374 self.profile.is_psychiatrist or 0,
375 self.profile.is_researcher or 0)
376 return result
377 end