| 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 |
class "SeekRoomAction" (HumanoidAction) |
| 22 |
|
| 23 |
---@type SeekRoomAction |
| 24 |
local SeekRoomAction = _G["SeekRoomAction"] |
| 25 |
|
| 26 |
--! Find another room (and go to it). |
| 27 |
--!param room_type Type of the new room. |
| 28 |
function SeekRoomAction:SeekRoomAction(room_type) |
| 29 |
assert(type(room_type) == "string", "Invalid value for parameter 'room_type'") |
| 30 |
|
| 31 |
self:HumanoidAction("seek_room") |
| 32 |
self.room_type = room_type |
| 33 |
self.treatment_room = nil -- Whether the next room is a treatment room. |
| 34 |
self.diagnosis_room = nil |
| 35 |
end |
| 36 |
|
| 37 |
--! Denote that the room being looked for is a treatment room. |
| 38 |
--!return (action) self, for daisy-chaining. |
| 39 |
function SeekRoomAction:enableTreatmentRoom() |
| 40 |
self.treatment_room = true |
| 41 |
return self |
| 42 |
end |
| 43 |
|
| 44 |
--! Sets the index of the room to use from the disease's available diagnosis |
| 45 |
-- rooms |
| 46 |
--!param room (integer) key in the available diagnosis room list |
| 47 |
function SeekRoomAction:setDiagnosisRoom(room) |
| 48 |
assert(type(room) == "number", "Invalid value for parameter 'room'") |
| 49 |
|
| 50 |
self.diagnosis_room = room |
| 51 |
return self |
| 52 |
end |
| 53 |
|
| 54 |
--! Finds a relevant diagnosis/treatment room(s) for a patient |
| 55 |
--! If diagnosis room, attempt to use GP's choice first, else select any other available |
| 56 |
--! room at random. |
| 57 |
local action_seek_room_find_room = permanent"action_seek_room_find_room"( function(action, humanoid) |
| 58 |
local room_type = action.room_type |
| 59 |
if action.diagnosis_room then |
| 60 |
-- Attempt to use the GP's chosen diagnosis room first |
| 61 |
if room_type then |
| 62 |
local room = humanoid.world:findRoomNear(humanoid, room_type, nil, "advanced") |
| 63 |
if room then return room end |
| 64 |
end |
| 65 |
|
| 66 |
local diagnosis_rooms = humanoid.available_diagnosis_rooms |
| 67 |
-- Make numbers for each available diagnosis room. A random index from this list will be chosen, |
| 68 |
-- and then the corresponding room index is taken as next room. (The list decreases for each room |
| 69 |
-- missing) |
| 70 |
local rooms_to_check = {} |
| 71 |
for i=1, #diagnosis_rooms do |
| 72 |
rooms_to_check[i] = i |
| 73 |
end |
| 74 |
|
| 75 |
while #rooms_to_check > 0 do |
| 76 |
-- Choose a diagnosis room from the list at random. Note: This ignores the initial diagnosis room! |
| 77 |
local room_at_index = math.random(1,#rooms_to_check) |
| 78 |
room_type = diagnosis_rooms[rooms_to_check[room_at_index]] |
| 79 |
-- Try to find the room |
| 80 |
local room = humanoid.world:findRoomNear(humanoid, room_type, nil, "advanced") |
| 81 |
if room then |
| 82 |
-- Update action with the room to be used |
| 83 |
for k, v in pairs(diagnosis_rooms) do |
| 84 |
if room_type == v then |
| 85 |
action:setDiagnosisRoom(k) |
| 86 |
break |
| 87 |
end |
| 88 |
end |
| 89 |
action.room_type = room_type |
| 90 |
return room |
| 91 |
else |
| 92 |
-- Remove the index of this room from the list of indices available in available_diagnosis_rooms |
| 93 |
table.remove(rooms_to_check, room_at_index) |
| 94 |
-- If the room can be built, set the flag for it. |
| 95 |
local diag = humanoid.world.available_rooms[room_type] |
| 96 |
if diag and humanoid.hospital:isRoomDiscovered(diag.id) then |
| 97 |
action.diagnosis_exists = room_type |
| 98 |
end |
| 99 |
end |
| 100 |
end |
| 101 |
-- No room found. If the last room to be checked was one that is not yet discovered |
| 102 |
-- but another one exists that can be built, return to that one. |
| 103 |
if not humanoid.world.available_rooms[room_type] and action.diagnosis_exists then |
| 104 |
action.room_type = action.diagnosis_exists |
| 105 |
end |
| 106 |
return nil |
| 107 |
elseif action.treatment_room then |
| 108 |
-- Treatment rooms etc either exist, or they don't. |
| 109 |
-- However, if many rooms are needed to treat the patient they all need to be available. |
| 110 |
-- Example: ward + operating theatre. |
| 111 |
for _, rooms in pairs(humanoid.disease.treatment_rooms) do |
| 112 |
if not humanoid.world:findRoomNear(humanoid, rooms, nil, "advanced") then |
| 113 |
action.room_type_needed = rooms |
| 114 |
return nil |
| 115 |
end |
| 116 |
end |
| 117 |
end |
| 118 |
return humanoid.world:findRoomNear(humanoid, room_type, nil, "advanced") |
| 119 |
end) |
| 120 |
|
| 121 |
local action_seek_room_goto_room = permanent"action_seek_room_goto_room"( function(room, humanoid, diagnosis_room) |
| 122 |
humanoid:setMood("patient_wait", "deactivate") |
| 123 |
humanoid.waiting = nil |
| 124 |
humanoid.message_callback = nil |
| 125 |
humanoid:setNextAction(room:createEnterAction(humanoid)) |
| 126 |
humanoid.next_room_to_visit = room |
| 127 |
humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.on_my_way_to |
| 128 |
:format(room.room_info.name)) |
| 129 |
room.door:updateDynamicInfo() |
| 130 |
if not room:testStaffCriteria(room:getRequiredStaffCriteria()) then |
| 131 |
humanoid.world.dispatcher:callForStaff(room) |
| 132 |
end |
| 133 |
if diagnosis_room then |
| 134 |
-- The diagnosis room was found, remove it from the list |
| 135 |
table.remove(humanoid.available_diagnosis_rooms, diagnosis_room) |
| 136 |
end |
| 137 |
end) |
| 138 |
|
| 139 |
local function action_seek_room_no_diagnosis_room_found(action, humanoid) |
| 140 |
--If it's the VIP then there's been an explosion. Skip the exploded room |
| 141 |
if class.is(humanoid, Vip) then |
| 142 |
humanoid.waiting = 1 |
| 143 |
return |
| 144 |
end |
| 145 |
|
| 146 |
-- If none of the diagnosis rooms can be built yet, go home anyway. |
| 147 |
-- Otherwise, depending on hospital policy three things can happen: |
| 148 |
if humanoid.diagnosis_progress < humanoid.hospital.policies["send_home"] then |
| 149 |
-- Send home automatically |
| 150 |
humanoid:goHome("kicked") |
| 151 |
humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.no_diagnoses_available) |
| 152 |
|
| 153 |
elseif humanoid.diagnosis_progress < humanoid.hospital.policies["guess_cure"] or |
| 154 |
not humanoid.hospital.disease_casebook[humanoid.disease.id].discovered then |
| 155 |
-- If the disease hasn't been discovered yet it cannot be guessed, go here instead. |
| 156 |
-- Ask the player |
| 157 |
-- Wait two months before going home anyway. |
| 158 |
humanoid:setMood("patient_wait", "activate") |
| 159 |
humanoid.waiting = 60 |
| 160 |
humanoid.hospital:makeNoDiagnosisRoomFax(humanoid) |
| 161 |
humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.awaiting_decision) |
| 162 |
-- This seek_room action will be reused, return that it's valid. |
| 163 |
return true |
| 164 |
else |
| 165 |
-- Guess "type of disease" automatically. |
| 166 |
-- A patient with an undiscovered disease should never get here. |
| 167 |
assert(humanoid.hospital.disease_casebook[humanoid.disease.id].discovered) |
| 168 |
humanoid:setDiagnosed() |
| 169 |
humanoid:unregisterRoomBuildCallback(action.build_callback) |
| 170 |
humanoid:unregisterRoomRemoveCallback(action.remove_callback) |
| 171 |
humanoid:unregisterStaffChangeCallback(action.staff_change_callback) |
| 172 |
if humanoid:agreesToPay(humanoid.disease.id) then |
| 173 |
local seek_action = SeekRoomAction(humanoid.disease.treatment_rooms[1]) |
| 174 |
seek_action:enableTreatmentRoom() |
| 175 |
humanoid:queueAction(seek_action, 1) |
| 176 |
else |
| 177 |
humanoid:goHome("over_priced", humanoid.disease.id) |
| 178 |
end |
| 179 |
humanoid:finishAction() |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
local action_seek_room_interrupt = permanent"action_seek_room_interrupt"( function(action, humanoid) |
| 184 |
-- Just in case we are somehow started again: |
| 185 |
-- FIXME: This seems to be used for intermediate actions like peeing while the patient is waiting |
| 186 |
-- Unfortunately this means that if you finish the required room while the patient is peeing |
| 187 |
-- the callback does not happen, meaning that the message does not disappear. |
| 188 |
humanoid:unregisterRoomBuildCallback(action.build_callback) |
| 189 |
humanoid:unregisterRoomRemoveCallback(action.remove_callback) |
| 190 |
humanoid:unregisterStaffChangeCallback(action.staff_change_callback) |
| 191 |
action.build_callback = nil |
| 192 |
action.remove_callback = nil |
| 193 |
action.staff_change_callback = nil |
| 194 |
action.done_init = false |
| 195 |
humanoid:finishAction() |
| 196 |
end) |
| 197 |
|
| 198 |
local function action_seek_room_start(action, humanoid) |
| 199 |
if action.todo_interrupt then |
| 200 |
humanoid:finishAction() |
| 201 |
return |
| 202 |
end |
| 203 |
-- Seeking for toilets is a special case with its own action. |
| 204 |
if action.room_type == "toilets" then |
| 205 |
humanoid:queueAction(SeekToiletsAction(), 1) |
| 206 |
humanoid:finishAction() |
| 207 |
return |
| 208 |
end |
| 209 |
-- Try to find the appropriate room for the patient |
| 210 |
if not humanoid.diagnosed or action.room_type == "research" then |
| 211 |
local room = action_seek_room_find_room(action, humanoid) |
| 212 |
-- if we have the room but not the staff we shouldn't seek out the room either |
| 213 |
if room then |
| 214 |
humanoid.hospital:removeMessage(humanoid) |
| 215 |
action_seek_room_goto_room(room, humanoid, action.diagnosis_room) |
| 216 |
return |
| 217 |
end |
| 218 |
end |
| 219 |
|
| 220 |
-- check we can treat the patient - and just shortcut the processing |
| 221 |
local req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id) |
| 222 |
if humanoid.diagnosed then |
| 223 |
local room = action_seek_room_find_room(action, humanoid) |
| 224 |
-- if we have the room but not the staff we shouldn't seek out the room either |
| 225 |
if room and (not req or not action.treatment_room) then |
| 226 |
humanoid.hospital:removeMessage(humanoid) |
| 227 |
action_seek_room_goto_room(room, humanoid, action.diagnosis_room) |
| 228 |
return |
| 229 |
end |
| 230 |
end |
| 231 |
-- we can't yet treat the patient, register callbacks |
| 232 |
-- create message etc |
| 233 |
if not action.done_init then |
| 234 |
action.done_init = true |
| 235 |
action.must_happen = true |
| 236 |
|
| 237 |
local remove_callback = --[[persistable:action_seek_room_remove_callback]] function() |
| 238 |
humanoid:updateMessage("research") |
| 239 |
end -- End of remove_callback function |
| 240 |
action.remove_callback = remove_callback |
| 241 |
humanoid:registerRoomRemoveCallback(remove_callback) |
| 242 |
|
| 243 |
local build_callback |
| 244 |
|
| 245 |
local staff_change_callback |
| 246 |
staff_change_callback = --[[persistable:action_seek_room_staff_change_callback]] function(staff) |
| 247 |
-- we might have hired or fired a staff member |
| 248 |
-- technically we don't care about Receptionist or Handyman |
| 249 |
if class.is(staff, Receptionist) or class.is(staff, Handyman) then |
| 250 |
return |
| 251 |
end |
| 252 |
-- update the message either way |
| 253 |
humanoid:updateMessage("research") |
| 254 |
|
| 255 |
local room_req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id) |
| 256 |
-- only need to check if we hired someone |
| 257 |
if not staff.fired and not room_req then |
| 258 |
local room = action_seek_room_find_room(action, humanoid) |
| 259 |
if room then |
| 260 |
humanoid.hospital:removeMessage(humanoid) |
| 261 |
humanoid:unregisterRoomBuildCallback(build_callback) |
| 262 |
humanoid:unregisterRoomRemoveCallback(remove_callback) |
| 263 |
humanoid:unregisterStaffChangeCallback(staff_change_callback) |
| 264 |
action_seek_room_goto_room(room, humanoid, action.diagnosis_room) |
| 265 |
end |
| 266 |
end |
| 267 |
end -- End of staff_change_callback function |
| 268 |
action.staff_change_callback = staff_change_callback |
| 269 |
humanoid:registerStaffChangeCallback(staff_change_callback) |
| 270 |
|
| 271 |
build_callback = --[[persistable:action_seek_room_build_callback]] function(rm) |
| 272 |
-- if research room was built, message may need to be updated |
| 273 |
humanoid:updateMessage("research") |
| 274 |
|
| 275 |
local found = false |
| 276 |
if rm.room_info.id == action.room_type then |
| 277 |
found = true |
| 278 |
elseif rm.room_info.id == action.room_type_needed then |
| 279 |
-- So the room that we're going to is not actually the room we waited for to be built. |
| 280 |
-- Example: Will go to ward, but is waiting for the operating theatre. |
| 281 |
-- Clean up and start over to find the room we actually want to go to. |
| 282 |
humanoid.hospital:removeMessage(humanoid) |
| 283 |
humanoid:unregisterRoomBuildCallback(build_callback) |
| 284 |
humanoid:unregisterRoomRemoveCallback(remove_callback) |
| 285 |
humanoid:unregisterStaffChangeCallback(staff_change_callback) |
| 286 |
action.room_type_needed = nil |
| 287 |
action_seek_room_start(action, humanoid) |
| 288 |
elseif not humanoid.diagnosed then |
| 289 |
-- Waiting for a diagnosis room, we need to go through the list - unless it is gp |
| 290 |
if action.room_type ~= "gp" then |
| 291 |
for i = 1, #humanoid.available_diagnosis_rooms do |
| 292 |
if humanoid.available_diagnosis_rooms[i].id == rm.room_info.id then |
| 293 |
found = true |
| 294 |
end |
| 295 |
end |
| 296 |
end |
| 297 |
end |
| 298 |
if found then |
| 299 |
-- Don't add a "go to room" action to the patient's queue if the |
| 300 |
-- autopsy machine is about to kill them: |
| 301 |
-- don't need this as we unregistered all previous callbacks if we went to research |
| 302 |
local room_req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id) |
| 303 |
-- get required staff |
| 304 |
if not humanoid.diagnosed or (not room_req or not action.treatment_room) then |
| 305 |
action_seek_room_goto_room(rm, humanoid, action.diagnosis_room) |
| 306 |
humanoid.hospital:removeMessage(humanoid) |
| 307 |
humanoid:unregisterRoomBuildCallback(build_callback) |
| 308 |
humanoid:unregisterRoomRemoveCallback(remove_callback) |
| 309 |
humanoid:unregisterStaffChangeCallback(staff_change_callback) |
| 310 |
end |
| 311 |
end |
| 312 |
end -- End of build_callback function |
| 313 |
action.build_callback = build_callback |
| 314 |
humanoid:registerRoomBuildCallback(build_callback) |
| 315 |
|
| 316 |
action.on_interrupt = action_seek_room_interrupt |
| 317 |
end |
| 318 |
|
| 319 |
-- Things needed to get the patient to the correct room in due time are done. Now it's |
| 320 |
-- time to let the player know about it too. |
| 321 |
-- If done_walk is set the meander action that takes place after not finding any room has been |
| 322 |
-- done = nothing more to do right now. |
| 323 |
if not action.done_walk then |
| 324 |
local action_still_valid = true |
| 325 |
if not action.message_sent then |
| 326 |
-- Make a message about that something needs to be done about this patient |
| 327 |
if humanoid.diagnosed then |
| 328 |
-- Emergency patients also don't need to ask what to do, they'll just wait for the player |
| 329 |
-- to build the necessary room. |
| 330 |
if not humanoid.is_emergency then |
| 331 |
-- The patient is diagnosed, a treatment room is missing. |
| 332 |
-- It may happen that it is another room in a series which is missing. |
| 333 |
humanoid.hospital:makeNoTreatmentRoomFax(humanoid) |
| 334 |
-- Wait two months before going home anyway. |
| 335 |
humanoid.waiting = 60 |
| 336 |
humanoid:setMood("patient_wait", "activate") |
| 337 |
humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.awaiting_decision) |
| 338 |
end |
| 339 |
else |
| 340 |
if action.room_type == "gp" then |
| 341 |
-- The GP's office is a special case. |
| 342 |
humanoid:setMood("patient_wait", "activate") |
| 343 |
humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.no_gp_available) |
| 344 |
humanoid.hospital:adviseNoGPOffice() |
| 345 |
else |
| 346 |
-- No more diagnosis rooms can be found |
| 347 |
action_still_valid = action_seek_room_no_diagnosis_room_found(action, humanoid) |
| 348 |
end |
| 349 |
end |
| 350 |
end |
| 351 |
if action_still_valid then |
| 352 |
action.done_walk = true |
| 353 |
humanoid:queueAction(MeanderAction():setCount(1):setMustHappen(true), 0) |
| 354 |
end |
| 355 |
else |
| 356 |
-- Make sure the patient stands in a correct way as he/she is waiting. |
| 357 |
local direction = humanoid.last_move_direction |
| 358 |
local anims = humanoid.walk_anims |
| 359 |
if direction == "north" then |
| 360 |
humanoid:setAnimation(anims.idle_north, 0) |
| 361 |
elseif direction == "east" then |
| 362 |
humanoid:setAnimation(anims.idle_east, 0) |
| 363 |
elseif direction == "south" then |
| 364 |
humanoid:setAnimation(anims.idle_east, 1) |
| 365 |
elseif direction == "west" then |
| 366 |
humanoid:setAnimation(anims.idle_north, 1) |
| 367 |
end |
| 368 |
humanoid:setTilePositionSpeed(humanoid.tile_x, humanoid.tile_y) |
| 369 |
end |
| 370 |
end |
| 371 |
|
| 372 |
return action_seek_room_start |