| 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 |
corsixth.require("announcer") |
| 22 |
|
| 23 |
local AnnouncementPriority = _G["AnnouncementPriority"] |
| 24 |
|
| 25 |
--! A Doctor, Nurse, Receptionist, Handyman, or Surgeon |
| 26 |
class "Staff" (Humanoid) |
| 27 |
|
| 28 |
---@type Staff |
| 29 |
local Staff = _G["Staff"] |
| 30 |
|
| 31 |
--!param ... Arguments to base class constructor. |
| 32 |
function Staff:Staff(...) |
| 33 |
self:Humanoid(...) |
| 34 |
self.hover_cursor = TheApp.gfx:loadMainCursor("staff") |
| 35 |
self.parcelNr = 0 |
| 36 |
self.leave_sounds = {} |
| 37 |
self.leave_priority = AnnouncementPriority.High |
| 38 |
end |
| 39 |
|
| 40 |
--! Handle daily adjustments to staff. |
| 41 |
--!return (boolean) Whether the caller should continue processing |
| 42 |
function Staff:tickDay() |
| 43 |
if not Humanoid.tickDay(self) then |
| 44 |
return false |
| 45 |
end |
| 46 |
if class.is(self, Receptionist) then return true end |
| 47 |
-- Pay too low --> unhappy |
| 48 |
-- Pay too high --> happy |
| 49 |
local fair_wage = self.profile:getFairWage() |
| 50 |
local wage = self.profile.wage |
| 51 |
self:changeAttribute("happiness", 0.05 * (wage - fair_wage) / (fair_wage ~= 0 and fair_wage or 1)) |
| 52 |
|
| 53 |
-- if you overwork your Dr's then there is a chance that they can go crazy |
| 54 |
-- when this happens, find him and get him to rest straight away |
| 55 |
if self:isVeryTired() or not self:isResting() then |
| 56 |
-- Working when you should be taking a break will make you unhappy |
| 57 |
if self:getAttribute("fatigue") >= self.hospital.policies["goto_staffroom"] then |
| 58 |
self:changeAttribute("happiness", -0.02) |
| 59 |
end |
| 60 |
-- You will also start to become unhappy as you become tired |
| 61 |
if self:getAttribute("fatigue") >= 0.5 then |
| 62 |
self:changeAttribute("happiness", -0.01) |
| 63 |
end |
| 64 |
else -- You are resting, and no longer very tired. Things can only get better! |
| 65 |
self:setMood("tired", "deactivate") |
| 66 |
self:changeAttribute("happiness", 0.006) |
| 67 |
end |
| 68 |
|
| 69 |
-- It is nice to see plants, but dead plants make you unhappy |
| 70 |
local plant = getRandomEntryFromArray(self:findObjectsInSquare(2, "plant")) |
| 71 |
if plant then |
| 72 |
self:changeAttribute("happiness", -0.003 + (plant:isPleasingFactor() * 0.001)) |
| 73 |
end |
| 74 |
|
| 75 |
-- Seeing various nearby objects boost your happiness, some more than others |
| 76 |
local good_objects = { |
| 77 |
["extinguisher"] = 0.002, -- Makes you feel safe |
| 78 |
["bin"] = 0.001, |
| 79 |
["bookcase"] = 0.003, |
| 80 |
["skeleton"] = 0.002, |
| 81 |
["tv"] = 0.0005, |
| 82 |
} |
| 83 |
|
| 84 |
-- Construct an array with the object names. |
| 85 |
local happy_objects = {} |
| 86 |
for name, _ in pairs(good_objects) do happy_objects[#happy_objects + 1] = name end |
| 87 |
|
| 88 |
-- Look what's around the humanoid, and adapt the happiness. |
| 89 |
happy_objects = self:findObjectsInSquare(2, happy_objects) |
| 90 |
for obj_name, happiness_score in pairs(good_objects) do |
| 91 |
self:changeAttribute("happiness", #happy_objects[obj_name] * happiness_score) |
| 92 |
end |
| 93 |
|
| 94 |
-- List of positive rest activities and their happiness effect |
| 95 |
local recreation = { |
| 96 |
["video_game"] = 0.08, |
| 97 |
["pool_table"] = 0.074, |
| 98 |
["sofa"] = 0.05, |
| 99 |
} |
| 100 |
-- Being able to rest from work and play the video game or pool will make you happy |
| 101 |
if self:getCurrentAction().name == "use_object" then |
| 102 |
local happiness = recreation[self:getCurrentAction().object.object_type.id] |
| 103 |
if happiness then self:changeAttribute("happiness", happiness) end |
| 104 |
end |
| 105 |
|
| 106 |
local room = self:getRoom() |
| 107 |
if room then |
| 108 |
self:changeAttribute("happiness", room.happiness_factor) |
| 109 |
end |
| 110 |
|
| 111 |
return true |
| 112 |
end |
| 113 |
|
| 114 |
function Staff:tick() |
| 115 |
Entity.tick(self) |
| 116 |
-- don't do anything if they're fired or picked up or have no hospital |
| 117 |
if self.fired or self.pickup or not self.hospital or self.dead then |
| 118 |
return |
| 119 |
end |
| 120 |
|
| 121 |
-- check if we need to use the staff room and go there if so |
| 122 |
self:checkIfNeedRest() |
| 123 |
-- check if staff has been waiting too long for a raise |
| 124 |
self:checkIfWaitedTooLongForRaise() |
| 125 |
|
| 126 |
-- Decide whether the staff member should be tiring and tire them |
| 127 |
if self:isTiring() then |
| 128 |
self:tire(0.000090) |
| 129 |
self:changeAttribute("happiness", -0.00002) |
| 130 |
end |
| 131 |
|
| 132 |
-- Make staff members request a raise if they are very unhappy |
| 133 |
if not self.world.debug_disable_salary_raise and self:getAttribute("happiness") < 0.1 then |
| 134 |
if not self.timer_until_raise then |
| 135 |
self.timer_until_raise = 200 |
| 136 |
end |
| 137 |
if self.timer_until_raise == 0 then |
| 138 |
self:requestRaise() |
| 139 |
else |
| 140 |
self.timer_until_raise = self.timer_until_raise - 1 |
| 141 |
end |
| 142 |
else |
| 143 |
self.timer_until_raise = nil |
| 144 |
end |
| 145 |
|
| 146 |
-- seeing litter will make you unhappy. If it is pee or puke it is worse |
| 147 |
for _, litter in ipairs(self:findObjectsInSquare(2, "litter")) do |
| 148 |
if litter:anyLitter() then |
| 149 |
self:changeAttribute("happiness", -0.0002) |
| 150 |
else |
| 151 |
self:changeAttribute("happiness", -0.0004) |
| 152 |
end |
| 153 |
end |
| 154 |
self:updateSpeed() |
| 155 |
end |
| 156 |
|
| 157 |
function Staff:checkIfWaitedTooLongForRaise() |
| 158 |
if not self.quitting_in then return end |
| 159 |
self.quitting_in = self.quitting_in - 1 |
| 160 |
|
| 161 |
local is_waiting_time_is_up = self.quitting_in < 0 |
| 162 |
if is_waiting_time_is_up then |
| 163 |
local rise_windows = self.world.ui:getWindows(UIStaffRise) |
| 164 |
local staff_rise_window = nil |
| 165 |
self.quitting_in = nil |
| 166 |
self.hospital:removeMessage(self) |
| 167 |
|
| 168 |
-- We go through all "requesting rise" windows open |
| 169 |
-- to close one of them if open when request resolved. |
| 170 |
for i = 1, #rise_windows do |
| 171 |
if rise_windows[i].staff == self then |
| 172 |
staff_rise_window = rise_windows[i] |
| 173 |
break |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
-- If the hospital policy is set to automatically grant wage increases, grant |
| 178 |
-- the requested raise instead of firing the staff member |
| 179 |
if self.hospital.policies.grant_wage_increase then |
| 180 |
if staff_rise_window then -- if rise window open |
| 181 |
staff_rise_window:increaseSalary() -- close window and raise |
| 182 |
else |
| 183 |
local rise_amount = self.profile:getRiseAmount() |
| 184 |
self:increaseWage(rise_amount) |
| 185 |
end |
| 186 |
-- else: The staff member is sacked |
| 187 |
else |
| 188 |
if staff_rise_window then |
| 189 |
staff_rise_window:fireStaff() -- close window and fire |
| 190 |
else |
| 191 |
self:fire() |
| 192 |
end |
| 193 |
end |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
function Staff:isTiring() |
| 198 |
local tiring = true |
| 199 |
|
| 200 |
local room = self:getRoom() |
| 201 |
-- Being in a staff room is actually quite refreshing, as long as you're not a handyman watering plants. |
| 202 |
if room then |
| 203 |
if room.room_info.id == "staff_room" and not self.on_call then |
| 204 |
tiring = false |
| 205 |
end |
| 206 |
elseif not class.is(self, Handyman) then |
| 207 |
tiring = false |
| 208 |
end |
| 209 |
|
| 210 |
-- Picking staff members up doesn't tire them, it just tires the player. |
| 211 |
if self:getCurrentAction().name == "pickup" then |
| 212 |
tiring = false |
| 213 |
end |
| 214 |
|
| 215 |
return tiring |
| 216 |
end |
| 217 |
|
| 218 |
function Staff:isResting() |
| 219 |
local room = self:getRoom() |
| 220 |
|
| 221 |
if room and room.room_info.id == "staff_room" and not self.on_call then |
| 222 |
return true |
| 223 |
else |
| 224 |
return false |
| 225 |
end |
| 226 |
end |
| 227 |
|
| 228 |
--! Destroys any raise request window that may be queued |
| 229 |
function Staff:removeQueuedStaffMessage() |
| 230 |
self.hospital:removeMessage(self) |
| 231 |
end |
| 232 |
|
| 233 |
-- Immediately terminate the staff member's employment. |
| 234 |
function Staff:fire() |
| 235 |
if self.fired then |
| 236 |
return |
| 237 |
end |
| 238 |
|
| 239 |
-- Ensure that there are no inspection windows open for this staff member. |
| 240 |
local staff_window = self.world.ui:getWindow(UIStaff) |
| 241 |
if staff_window and staff_window.staff == self then |
| 242 |
staff_window:close() |
| 243 |
end |
| 244 |
self:removeQueuedStaffMessage() |
| 245 |
self.hospital:spendMoney(self.profile.wage, _S.transactions.severance .. ": " |
| 246 |
.. self.profile:getFullName()) |
| 247 |
self.world.ui:playSound("sack.wav") |
| 248 |
self:setMood("exit", "activate") |
| 249 |
self:setDynamicInfoText(_S.dynamic_info.staff.actions.fired) |
| 250 |
self.fired = true |
| 251 |
self.going_home = true |
| 252 |
self.hospital:changeReputation("kicked") |
| 253 |
self:despawn() |
| 254 |
self.hover_cursor = nil |
| 255 |
self.hospital:announceStaffLeave(self) |
| 256 |
-- Unregister any build callbacks or messages. |
| 257 |
self:unregisterCallbacks() |
| 258 |
-- Update the staff management window if it is open. |
| 259 |
local window = self.world.ui:getWindow(UIStaffManagement) |
| 260 |
if window then |
| 261 |
window:updateStaffList(self) |
| 262 |
end |
| 263 |
end |
| 264 |
|
| 265 |
function Staff:die() |
| 266 |
self:removeQueuedStaffMessage() |
| 267 |
-- Update the staff management screen (if present) accordingly |
| 268 |
local window = self.world.ui:getWindow(UIStaffManagement) |
| 269 |
if window then |
| 270 |
window:updateStaffList(self) |
| 271 |
end |
| 272 |
self.dead = true |
| 273 |
end |
| 274 |
|
| 275 |
-- Despawns the staff member and removes them from the hospital |
| 276 |
function Staff:despawn() |
| 277 |
self.hospital:removeStaff(self) |
| 278 |
Humanoid.despawn(self) |
| 279 |
end |
| 280 |
|
| 281 |
-- Function which is called when the user clicks on the staff member. |
| 282 |
-- Responsible for opening a staff information dialog on left click and picking |
| 283 |
-- up the staff member on right click. |
| 284 |
--!param ui (GameUI) The UI which the user in question is using. |
| 285 |
--!param button (string) One of: "left", "middle", "right". |
| 286 |
function Staff:onClick(ui, button) |
| 287 |
if self.fired then |
| 288 |
return |
| 289 |
end |
| 290 |
|
| 291 |
if button == "left" then |
| 292 |
if self.message_callback then |
| 293 |
self:message_callback() |
| 294 |
else |
| 295 |
ui:addWindow(UIStaff(ui, self)) |
| 296 |
end |
| 297 |
elseif button == "right" then |
| 298 |
self:setPickup(ui, nil) |
| 299 |
end |
| 300 |
Humanoid.onClick(self, ui, button) |
| 301 |
end |
| 302 |
|
| 303 |
function Staff:dump() |
| 304 |
print("-----------------------------------") |
| 305 |
if self.on_call then |
| 306 |
print("On call: ") |
| 307 |
CallsDispatcher.dumpCall(self.on_call) |
| 308 |
else |
| 309 |
print('On call: no') |
| 310 |
end |
| 311 |
print("Busy: ", (self:isIdle() and "idle" or "busy") .. (self.pickup and " and picked up" or '')) |
| 312 |
if self.going_to_staffroom then print("Going to staffroom") end |
| 313 |
if self.last_room then |
| 314 |
print("Last room: ", self.last_room.room_info.id .. '@' .. self.last_room.x ..','.. self.last_room.y) |
| 315 |
end |
| 316 |
|
| 317 |
Humanoid.dump(self) |
| 318 |
end |
| 319 |
|
| 320 |
function Staff:setProfile(profile) |
| 321 |
self.profile = profile |
| 322 |
self:setType(profile.humanoid_class) |
| 323 |
self.attributes["fatigue"] = 0 |
| 324 |
self:setLayer(5, profile.layer5) |
| 325 |
self.waiting_for_staffroom = false -- Staff member has detected there is no staff room to rest. |
| 326 |
end |
| 327 |
|
| 328 |
-- Function for increasing fatigue. Fatigue can be between 0 and 1, |
| 329 |
-- so amounts here should be appropriately small comma values. |
| 330 |
function Staff:tire(amount) |
| 331 |
-- The no rest cheat overrides tiring effects |
| 332 |
if not self.hospital.hosp_cheats:isCheatActive("no_rest_cheat") then |
| 333 |
self:changeAttribute("fatigue", amount) |
| 334 |
end |
| 335 |
self:updateDynamicInfo() |
| 336 |
end |
| 337 |
|
| 338 |
-- Function for decreasing fatigue. Fatigue can be between 0 and 1, |
| 339 |
-- so amounts here should be appropriately small comma values. |
| 340 |
function Staff:wake(amount) |
| 341 |
self:changeAttribute("fatigue", -amount) |
| 342 |
self:updateDynamicInfo() |
| 343 |
end |
| 344 |
|
| 345 |
-- Update the movement speed |
| 346 |
function Staff:updateSpeed() |
| 347 |
local level = 2 |
| 348 |
if self.profile.is_junior then |
| 349 |
level = 1 |
| 350 |
elseif self.profile.is_consultant then |
| 351 |
level = 3 |
| 352 |
end |
| 353 |
local room = self:getRoom() |
| 354 |
if room and room.room_info.id == "training" and self:fulfillsCriterion("Doctor") then |
| 355 |
level = 1 |
| 356 |
elseif self.hospital and self.hospital.hosp_cheats:isCheatActive("no_rest_cheat") then |
| 357 |
level = 3 -- Cheat makes everyone speedy |
| 358 |
elseif not class.is(self, Receptionist) then |
| 359 |
if self:isCrackUpTired() then |
| 360 |
level = level - 2 |
| 361 |
elseif self:isVeryTired() then |
| 362 |
level = level - 1 |
| 363 |
end |
| 364 |
end |
| 365 |
if level >= 3 then |
| 366 |
self.speed = "fast" |
| 367 |
self.slow_animation = false |
| 368 |
elseif level <= 1 then |
| 369 |
self.speed = "slow" |
| 370 |
self.slow_animation = true |
| 371 |
else |
| 372 |
self.speed = "normal" |
| 373 |
self.slow_animation = false |
| 374 |
end |
| 375 |
end |
| 376 |
|
| 377 |
-- Check if fatigue is over a certain level (decided by the hospital policy), |
| 378 |
-- and go to the StaffRoom if it is. |
| 379 |
function Staff:checkIfNeedRest() |
| 380 |
-- Only when the staff member is very tired should the icon emerge. Unhappiness will also escalate |
| 381 |
if self:isVeryTired() then |
| 382 |
self:setMood("tired", "activate") |
| 383 |
self:changeAttribute("happiness", -0.0002) |
| 384 |
end |
| 385 |
-- If above the policy threshold, go to the staff room. |
| 386 |
if self:getAttribute("fatigue") >= self.hospital.policies["goto_staffroom"] and |
| 387 |
not class.is(self:getRoom(), StaffRoom) then |
| 388 |
-- The staff will get unhappy if there is no staffroom to rest in. |
| 389 |
if self.waiting_for_staffroom then |
| 390 |
self:changeAttribute("happiness", -0.001) |
| 391 |
end |
| 392 |
|
| 393 |
local room = self:getRoom() |
| 394 |
-- Nurses can leave a ward with patients remaining in their beds, |
| 395 |
-- nurses in other rooms and other staff must leave after all patients exit |
| 396 |
local may_leave = room and (room.room_info.id == "ward" or not room:getPatient()) |
| 397 |
if (self.staffroom_needed and (may_leave or not room)) or |
| 398 |
(room and self.going_to_staffroom) then |
| 399 |
if self:getCurrentAction().name ~= "walk" and self:getCurrentAction().name ~= "queue" then |
| 400 |
self.staffroom_needed = nil |
| 401 |
self:goToStaffRoom() |
| 402 |
end |
| 403 |
end |
| 404 |
|
| 405 |
-- Abort if waiting for a staffroom to be built, waiting for the patient to leave, |
| 406 |
-- already going to staffroom or being picked up |
| 407 |
if self.waiting_for_staffroom or self.staffroom_needed or |
| 408 |
self.going_to_staffroom or self.pickup then |
| 409 |
return |
| 410 |
end |
| 411 |
|
| 412 |
-- If no staff room exists, prevent further checks until one is built |
| 413 |
if not self.world:findRoomNear(self, "staff_room") then |
| 414 |
self.waiting_for_staffroom = true -- notifyNewRoom resets it when a staff room gets built. |
| 415 |
return |
| 416 |
end |
| 417 |
|
| 418 |
if not class.is(self, Handyman) and room and room:getPatient() then |
| 419 |
-- If occupied by patient, staff will go to the staffroom after the patient left. |
| 420 |
self.staffroom_needed = true |
| 421 |
else |
| 422 |
self:goToStaffRoom() |
| 423 |
end |
| 424 |
end |
| 425 |
end |
| 426 |
|
| 427 |
function Staff:notifyNewRoom(room) |
| 428 |
if room.room_info.id == "staff_room" then |
| 429 |
self.waiting_for_staffroom = false |
| 430 |
end |
| 431 |
end |
| 432 |
|
| 433 |
function Staff:goToStaffRoom() |
| 434 |
-- NB: going_to_staffroom set if (and only if) a seek_staffroom action is in the action_queue |
| 435 |
self.going_to_staffroom = true |
| 436 |
|
| 437 |
local room = self:getRoom() |
| 438 |
if room then |
| 439 |
room.staff_leaving = true |
| 440 |
self:setNextAction(room:createLeaveAction()) |
| 441 |
self:queueAction(SeekStaffRoomAction()) |
| 442 |
else |
| 443 |
self:setNextAction(SeekStaffRoomAction()) |
| 444 |
end |
| 445 |
end |
| 446 |
|
| 447 |
-- Function to set pickup action on staff. Pickup action can be deferred. |
| 448 |
function Staff:setPickup(ui, window_to_close) |
| 449 |
if not self.pickup then -- check if we already added pickup Action in actions queue |
| 450 |
self.pickup = true |
| 451 |
local pickup_action = PickupAction(ui) |
| 452 |
if window_to_close then -- if we want to close some window after pickup happened |
| 453 |
pickup_action = pickup_action:setTodoClose(window_to_close) |
| 454 |
end |
| 455 |
self:setNextAction(pickup_action, true) |
| 456 |
end |
| 457 |
end |
| 458 |
|
| 459 |
function Staff:onPickup() |
| 460 |
self:setDynamicInfoText("") |
| 461 |
-- picking up staff was not canceling moods in all cases see issue 1642 |
| 462 |
-- as you would expect room:onHumanoidLeave(humanoid) to clear them! |
| 463 |
self:setMood("idea3", "deactivate") |
| 464 |
self:setMood("reflexion", "deactivate") |
| 465 |
end |
| 466 |
|
| 467 |
function Staff:onPlaceInCorridor() |
| 468 |
local world = self.world |
| 469 |
local notify_object = world:getObjectToNotifyOfOccupants(self.tile_x, self.tile_y) |
| 470 |
if notify_object then |
| 471 |
notify_object:onOccupantChange(1) |
| 472 |
end |
| 473 |
-- Assume that if the player puts someone in the corridor they don't want the |
| 474 |
-- staff member to primarily return to his/her old room. |
| 475 |
self.last_room = nil |
| 476 |
self:updateSpeed() |
| 477 |
self:setNextAction(MeanderAction()) |
| 478 |
end |
| 479 |
|
| 480 |
-- Sets the Hospital for a member of staff |
| 481 |
--!param hospital (Hospital) - hospital to assign to member of staff |
| 482 |
function Staff:setHospital(hospital) |
| 483 |
Humanoid.setHospital(self, hospital) |
| 484 |
self:updateDynamicInfo() |
| 485 |
end |
| 486 |
|
| 487 |
-- Helper function to decide if Staff fulfills a criterion |
| 488 |
-- (one of "Doctor", "Nurse", "Psychiatrist", "Surgeon", "Researcher" and "Handyman", "Receptionist", "Junior", "Consultant") |
| 489 |
function Staff:fulfillsCriterion(criterion) |
| 490 |
return false |
| 491 |
end |
| 492 |
|
| 493 |
function Staff:adviseWrongPersonForThisRoom() |
| 494 |
local room = self:getRoom() |
| 495 |
local room_name = room.room_info.long_name |
| 496 |
local required = (room.room_info.maximum_staff or room.room_info.required_staff) |
| 497 |
if required then |
| 498 |
if required.Nurse then |
| 499 |
self.hospital:giveAdvice({ _A.staff_place_advice.only_nurses_in_room:format(room_name) }) |
| 500 |
elseif required.Surgeon then |
| 501 |
self.hospital:giveAdvice({ _A.staff_place_advice.only_surgeons }) |
| 502 |
elseif required.Researcher then |
| 503 |
self.hospital:giveAdvice({ _A.staff_place_advice.only_researchers }) |
| 504 |
elseif required.Psychiatrist then |
| 505 |
self.hospital:giveAdvice({ _A.staff_place_advice.only_psychiatrists }) |
| 506 |
else |
| 507 |
self.hospital:giveAdvice({ _A.staff_place_advice.only_doctors_in_room:format(room_name) }) |
| 508 |
end |
| 509 |
end |
| 510 |
end |
| 511 |
|
| 512 |
--! Check whether staff are meandering |
| 513 |
--!return true if staff currently has a meander action |
| 514 |
function Staff:isMeandering() |
| 515 |
if #self.action_queue < 2 then return false end |
| 516 |
|
| 517 |
-- "meander" action always insert "move" or "idle" action before itself. |
| 518 |
-- so when humanoid "meandering" his action queue usually looks like: |
| 519 |
-- [1 idle, 2 meander] or [1 walk, 2 meander]. |
| 520 |
local idle_is_first = self.action_queue[1].name == "idle" |
| 521 |
local walk_is_first = self.action_queue[1].name == "walk" |
| 522 |
local meander_is_second = self.action_queue[2].name == "meander" |
| 523 |
|
| 524 |
return (idle_is_first or walk_is_first) and meander_is_second |
| 525 |
end |
| 526 |
|
| 527 |
-- Function to decide if staff currently has nothing to do and can be called to a room where they're needed |
| 528 |
function Staff:isIdle() |
| 529 |
-- Make sure we're not in an undesired state |
| 530 |
if not self.hospital or self.fired then |
| 531 |
return false |
| 532 |
end |
| 533 |
|
| 534 |
if self.on_call or self.pickup or self.going_to_staffroom or self.staffroom_needed then |
| 535 |
return false |
| 536 |
end |
| 537 |
|
| 538 |
-- if they are using a door they are not idle, this stops doctors being considered for staff selection |
| 539 |
-- for rooms they have not completely left yet, fixes issue 810 |
| 540 |
if self.user_of and self.user_of.object_type.id == "door" then |
| 541 |
return false |
| 542 |
end |
| 543 |
|
| 544 |
local room = self:getRoom() |
| 545 |
if room then |
| 546 |
-- in special rooms, never |
| 547 |
if room.room_info.id == "staff_room" or room.room_info.id == "research" or |
| 548 |
room.room_info.id == "training" then |
| 549 |
return false |
| 550 |
end |
| 551 |
|
| 552 |
-- For handyman - just check the on_call flag |
| 553 |
if class.is(self, Handyman) then return not self.on_call end |
| 554 |
|
| 555 |
-- For other staff... |
| 556 |
-- Staff member might be leaving |
| 557 |
if self:getCurrentAction().is_leaving then return false end |
| 558 |
|
| 559 |
-- in regular rooms (diagnosis / treatment), if no patient is in sight |
| 560 |
-- or if the only one in sight is actually leaving. |
| 561 |
return not room:isRoomInDemand() |
| 562 |
else |
| 563 |
-- In the corridor and not on_call (e.g. watering or going to room). |
| 564 |
if class.is(self, Handyman) then return not self.on_call end |
| 565 |
|
| 566 |
-- The staff is free, unless going back to the training/research. |
| 567 |
room = self.last_room |
| 568 |
if room and (room.room_info.id == "training" or room.room_info.id == "research") then |
| 569 |
return false |
| 570 |
end |
| 571 |
|
| 572 |
return true |
| 573 |
end |
| 574 |
end |
| 575 |
|
| 576 |
-- Makes the staff member request a raise of 10%, or a wage exactly in the middle of their current and a fair one, whichever is more. |
| 577 |
function Staff:requestRaise() |
| 578 |
assert(self.hospital, "A staff member asked for a pay rise who doesn't belong to a hospital!") |
| 579 |
local max_salary = self.world.map.level_config.payroll.MaxSalary |
| 580 |
-- Never request a raise if already at max salary |
| 581 |
if self.profile.wage >= max_salary then |
| 582 |
self.timer_until_raise = nil |
| 583 |
return |
| 584 |
end |
| 585 |
-- Check whether there is already a request for raise. |
| 586 |
if not self:isMoodActive("pay_rise") then |
| 587 |
local amount = math.floor(math.max(self.profile.wage * 1.1, (self.profile:getFairWage() + self.profile.wage) / 2) - self.profile.wage) |
| 588 |
-- Don't ask over the salary cap |
| 589 |
if self.profile.wage + amount > max_salary then |
| 590 |
amount = max_salary - self.profile.wage |
| 591 |
end |
| 592 |
-- At least for now, staff are timid, and only ask for raises 1/5th of the time |
| 593 |
if math.random(1, 5) ~= 1 or amount <= 0 then |
| 594 |
self.timer_until_raise = nil |
| 595 |
return -- too timid |
| 596 |
end |
| 597 |
-- The staff member can now successfully ask for a raise |
| 598 |
self.hospital:makeRaiseRequest(amount, self) |
| 599 |
self.quitting_in = 25*30 + math.random(0, 50) -- Time until the staff members quits anyway |
| 600 |
self:setMood("pay_rise", "activate") |
| 601 |
end |
| 602 |
end |
| 603 |
|
| 604 |
-- Increases the wage of the staff member. Also increases happiness and clears |
| 605 |
-- any request raise dialogs. |
| 606 |
--!param amount (integer) The amount, in game dollars per month, to increase |
| 607 |
-- the salary by. |
| 608 |
--!return (bool) Whether the wage actually increased |
| 609 |
function Staff:increaseWage(amount) |
| 610 |
-- Are we already paid the maximum? |
| 611 |
local max_salary = self.world.map.level_config.payroll.MaxSalary |
| 612 |
local wage_raised = true |
| 613 |
if self.profile.wage >= max_salary then |
| 614 |
wage_raised = false -- Already at max salary |
| 615 |
else |
| 616 |
local new_wage = self.profile.wage + amount |
| 617 |
if self.profile.wage + amount > max_salary then |
| 618 |
-- Maximum salary hit. The staff member will never be unhappy again |
| 619 |
new_wage = max_salary |
| 620 |
end |
| 621 |
-- Apply new salary |
| 622 |
self.profile.wage = new_wage |
| 623 |
self.world.ui:playSound("bonusal2.wav") |
| 624 |
end |
| 625 |
-- Reset |
| 626 |
self:setMood("pay_rise", "deactivate") |
| 627 |
self:changeAttribute("happiness", 0.99) |
| 628 |
return wage_raised |
| 629 |
end |
| 630 |
|
| 631 |
--! Sets dynamic info text before the dynamic info update |
| 632 |
--!param text (string) the string to append |
| 633 |
function Staff:setDynamicInfoText(text) |
| 634 |
self.dynamic_text = text |
| 635 |
self:updateDynamicInfo() |
| 636 |
end |
| 637 |
|
| 638 |
--! Updates a staff member's dynamic info |
| 639 |
function Staff:updateDynamicInfo() |
| 640 |
local dynamic_text = self.dynamic_text or "" |
| 641 |
local fatigue_text = _S.dynamic_info.staff.tiredness |
| 642 |
if class.is(self, Receptionist) then |
| 643 |
fatigue_text = nil |
| 644 |
else |
| 645 |
self:setDynamicInfo('progress', self:getAttribute("fatigue")) |
| 646 |
end |
| 647 |
self:setDynamicInfo('text', { |
| 648 |
self.profile.profession, |
| 649 |
dynamic_text, |
| 650 |
fatigue_text, |
| 651 |
}) |
| 652 |
if self.hospital then |
| 653 |
self:setDynamicInfo('dividers', {self.hospital.policies["goto_staffroom"]}) |
| 654 |
end |
| 655 |
end |
| 656 |
|
| 657 |
function Staff:onDestroy() |
| 658 |
-- Remove any message related to the staff member. |
| 659 |
if self.message_callback then |
| 660 |
self:message_callback(true) |
| 661 |
self.message_callback = nil |
| 662 |
end |
| 663 |
Humanoid.onDestroy(self) |
| 664 |
end |
| 665 |
|
| 666 |
function Staff:afterLoad(old, new) |
| 667 |
-- Usage of going_to_staffroom flag changed slightly, so unset it. |
| 668 |
-- (should be safe even if someone is actually going to staffroom) |
| 669 |
if old < 27 and new >= 27 then |
| 670 |
self.going_to_staffroom = nil |
| 671 |
end |
| 672 |
|
| 673 |
if old < 29 and new >= 29 then |
| 674 |
-- Handymen could have "staffroom_needed" flag set due to a bug, unset it. |
| 675 |
if class.is(self, Handyman) then |
| 676 |
self.staffroom_needed = nil |
| 677 |
end |
| 678 |
end |
| 679 |
|
| 680 |
if old < 64 and new >= 64 then |
| 681 |
-- added reference to world for staff profiles |
| 682 |
self.profile.world = self.world |
| 683 |
end |
| 684 |
if old < 68 and new >= 68 then |
| 685 |
if not class.is(self, Receptionist) and |
| 686 |
self:getAttribute("fatigue") >= self.hospital.policies["goto_staffroom"] then |
| 687 |
self:goToStaffRoom() |
| 688 |
self.going_to_staffroom = true |
| 689 |
end |
| 690 |
end |
| 691 |
|
| 692 |
if old < 121 and new >= 121 then |
| 693 |
if class.is(self, Handyman) and self.user_of and self.user_of.object_type.class == "Litter" then |
| 694 |
local litter = self.user_of |
| 695 |
local hospital = self.world:getHospital(litter.tile_x, litter.tile_y) |
| 696 |
local taskIndex = hospital:getIndexOfTask(litter.tile_x, litter.tile_y, "cleaning", litter) |
| 697 |
hospital:removeHandymanTask(taskIndex, "cleaning") |
| 698 |
end |
| 699 |
end |
| 700 |
|
| 701 |
if old < 133 and new >= 133 then |
| 702 |
if class.is(self, Handyman) then |
| 703 |
setmetatable(self, Handyman._metatable) |
| 704 |
elseif class.is(self, Receptionist) then |
| 705 |
setmetatable(self, Receptionist._metatable) |
| 706 |
elseif class.is(self, Nurse) then |
| 707 |
setmetatable(self, Nurse._metatable) |
| 708 |
else |
| 709 |
setmetatable(self, Doctor._metatable) |
| 710 |
end |
| 711 |
|
| 712 |
-- The new class's afterLoad calls staff.afterLoad, so to avoid |
| 713 |
-- infinite recursion we need to finish afterLoad actions up to |
| 714 |
-- this version, and then pretend we are starting from this |
| 715 |
-- version to avoid running them a second time. |
| 716 |
Humanoid.afterLoad(self, old, 133) |
| 717 |
self:afterLoad(133, new) |
| 718 |
return |
| 719 |
end |
| 720 |
|
| 721 |
if old < 213 then |
| 722 |
self.mood_marker = 2 |
| 723 |
end |
| 724 |
|
| 725 |
if old < 250 then |
| 726 |
-- Staff members picked up and moved to rest didn't reset the |
| 727 |
-- staffroom_needed flag. If a staff member genuinely needed it they would |
| 728 |
-- get it back again next tick. |
| 729 |
if self:getAttribute("fatigue") < self.hospital.policies["goto_staffroom"] then |
| 730 |
self.staffroom_needed = nil |
| 731 |
self.going_to_staffroom = nil -- also clear for consistency |
| 732 |
end |
| 733 |
end |
| 734 |
|
| 735 |
self:updateDynamicInfo() |
| 736 |
Humanoid.afterLoad(self, old, new) |
| 737 |
end |
| 738 |
|
| 739 |
--! Estimate staff service quality based on skills, restfulness (inverse of fatigue) and happiness. |
| 740 |
--!return (float) between [0-1] indicating quality of the service. |
| 741 |
function Staff:getServiceQuality() |
| 742 |
-- weights |
| 743 |
local skill_weight = 0.7 |
| 744 |
local restfulness_weight = 0.2 |
| 745 |
local happiness_weight = 0.1 |
| 746 |
|
| 747 |
local weighted_skill = skill_weight * self.profile.skill |
| 748 |
-- Less fatigue is better |
| 749 |
local weighted_restfulness = restfulness_weight * (1 - self:getAttribute("fatigue")) |
| 750 |
local weighted_happiness = happiness_weight * self:getAttribute("happiness") |
| 751 |
|
| 752 |
return weighted_skill + weighted_restfulness + weighted_happiness |
| 753 |
end |
| 754 |
|
| 755 |
--[[ Return string representation |
| 756 |
!return (string) |
| 757 |
]] |
| 758 |
function Staff:tostring() |
| 759 |
return Humanoid.tostring(self) |
| 760 |
end |
| 761 |
|
| 762 |
--! Judge tiredness based on the level config (which has a default of 700) |
| 763 |
--!return (boolean) Is staff member very tired? |
| 764 |
function Staff:isVeryTired() |
| 765 |
return self:getAttribute("fatigue") * 1000 >= self.world.map.level_config.gbv.VeryTired |
| 766 |
end |
| 767 |
|
| 768 |
--! Judge crack up tiredness based on the level config (which has a default of 800) |
| 769 |
--!return (boolean) Is staff member very tired? |
| 770 |
function Staff:isCrackUpTired() |
| 771 |
return self:getAttribute("fatigue") * 1000 >= self.world.map.level_config.gbv.CrackUpTired |
| 772 |
end |
| 773 |
|
| 774 |
-- Dummy callback for savegame compatibility |
| 775 |
local callbackNewRoom = --[[persistable:staff_build_staff_room_callback]] function() end |