/
seek_room.lua
422 lines (392 loc) · 17.9 KB
/
seek_room.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
--[[ Copyright (c) 2009 Peter "Corsix" Cawley
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
class "SeekRoomAction" (HumanoidAction)
---@type SeekRoomAction
local SeekRoomAction = _G["SeekRoomAction"]
--! Find another room (and go to it).
--!param room_type Type of the new room.
function SeekRoomAction:SeekRoomAction(room_type)
assert(type(room_type) == "string", "Invalid value for parameter 'room_type'")
self:HumanoidAction("seek_room")
self.room_type = room_type
self.treatment_room = nil -- Whether the next room is a treatment room.
self.diagnosis_room = nil
end
--! Denote that the room being looked for is a treatment room.
--!return (action) self, for daisy-chaining.
function SeekRoomAction:enableTreatmentRoom()
self.treatment_room = true
return self
end
function SeekRoomAction:setDiagnosisRoom(room)
assert(type(room) == "number", "Invalid value for parameter 'room'")
self.diagnosis_room = room
return self
end
local action_seek_room_find_room = permanent"action_seek_room_find_room"( function(action, humanoid)
local room_type = action.room_type
if action.diagnosis_room then
local tried_rooms = 0
-- Make numbers for each available diagnosis room. A random index from this list will be chosen,
-- and then the corresponding room index is taken as next room. (The list decrease for each room
-- missing)
local available_rooms = {}
local room_at_index
for i=1, #humanoid.available_diagnosis_rooms do
available_rooms[i] = i
end
while tried_rooms < #humanoid.available_diagnosis_rooms do
-- Choose a diagnosis room from the list at random. Note: This ignores the initial diagnosis room!
room_at_index = math.random(1,#available_rooms)
room_type = humanoid.available_diagnosis_rooms[available_rooms[room_at_index]]
-- Try to find the room
local room = humanoid.world:findRoomNear(humanoid, room_type, nil, "advanced")
if room then
return room
else
tried_rooms = tried_rooms + 1
-- Remove the index of this room from the list of indeces available in available_diagnosis_rooms
table.remove(available_rooms, room_at_index)
-- If the room can be built, set the flag for it.
local diag = humanoid.world.available_rooms[room_type]
if diag and humanoid.hospital.discovered_rooms[diag] then
action.diagnosis_exists = room_type
end
end
end
-- No room found. If the last room to be checked was one that is not yet discovered
-- but another one exists that can be built, return to that one.
if not humanoid.world.available_rooms[room_type] and action.diagnosis_exists then
action.room_type = action.diagnosis_exists
end
return nil
elseif action.treatment_room then
-- Treatment rooms etc either exist, or they don't.
-- However, if many rooms are needed to treat the patient they all need to be available.
-- Example: ward + operating theatre.
for _, rooms in pairs(humanoid.disease.treatment_rooms) do
if not humanoid.world:findRoomNear(humanoid, rooms, nil, "advanced") then
action.room_type_needed = rooms
return nil
end
end
end
return humanoid.world:findRoomNear(humanoid, room_type, nil, "advanced")
end)
local action_seek_room_goto_room = permanent"action_seek_room_goto_room"( function(room, humanoid, diagnosis_room)
humanoid:setMood("patient_wait", "deactivate")
humanoid.waiting = nil
humanoid.message_callback = nil
humanoid:setNextAction(room:createEnterAction(humanoid))
humanoid.next_room_to_visit = room
humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.on_my_way_to
:format(room.room_info.name))
room.door.queue:expect(humanoid)
room.door:updateDynamicInfo()
if not room:testStaffCriteria(room:getRequiredStaffCriteria()) then
humanoid.world.dispatcher:callForStaff(room)
end
if diagnosis_room then
-- The diagnosis room was found, remove it from the list
table.remove(humanoid.available_diagnosis_rooms, diagnosis_room)
end
end)
local function action_seek_room_no_treatment_room_found(room_type, humanoid)
-- Emergency patients also don't need to ask what to do, they'll just wait for the player
-- to build the necessary room.
if humanoid.is_emergency then
return
end
-- Wait two months before going home anyway.
humanoid.waiting = 60
-- Can this room be built right now? What is then missing?
local output_text = strings.can_not_cure
-- TODO: can we really assert this? Or should we just make the patient go home?
--local room = assert(humanoid.world.available_rooms[room_type], "room " .. room_type .. " not available")
local req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id)
local research_enabled = false
if req then
local strings = _S.fax.disease_discovered_patient_choice
research_enabled = humanoid.hospital:hasRoomOfType("research") and humanoid.hospital:hasStaffOfCategory("Researcher")
if #req.rooms == 1 then
local room_name, required_staff, staff_name = humanoid.world:getRoomNameAndRequiredStaffName(req.rooms[1])
if req.staff[required_staff] or 0 > 0 then
output_text = strings.need_to_build_and_employ:format(room_name, staff_name)
else
output_text = strings.need_to_build:format(room_name)
end
elseif #req.rooms == 0 and next(req.staff) then
local staffclass_to_string = {
Nurse = _S.staff_title.nurse,
Doctor = _S.staff_title.doctor,
Surgeon = _S.staff_title.surgeon,
Psychiatrist = _S.staff_title.psychiatrist,
}
output_text = strings.need_to_employ:format(staffclass_to_string[next(req.staff)])
end
end
local message = {
{text = strings.disease_name:format(humanoid.disease.name)},
{text = " "},
{text = output_text},
{text = strings.what_to_do_question},
choices = {
{text = strings.choices.send_home, choice = "send_home"},
{text = strings.choices.wait, choice = "wait"},
{text = strings.choices.research, choice = "research", enabled = research_enabled},
},
}
-- Ok, send the message in all channels.
TheApp.ui.bottom_panel:queueMessage("information", message, humanoid)
humanoid:setMood("patient_wait", "activate")
humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.awaiting_decision)
end
local function action_seek_room_no_diagnosis_room_found(action, humanoid)
--If it's the VIP then there's been an explosion. Skip the exploded room
if humanoid.humanoid_class == "VIP" then
humanoid.waiting = 1
return
end
-- If none of the diagnosis rooms can be built yet, go home anyway.
-- Otherwise, depending on hospital policy three things can happen:
if humanoid.diagnosis_progress < humanoid.hospital.policies["send_home"] then
-- Send home automatically
humanoid:goHome("kicked")
humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.no_diagnoses_available)
elseif humanoid.diagnosis_progress < humanoid.hospital.policies["guess_cure"] or
not humanoid.hospital.disease_casebook[humanoid.disease.id].discovered then
-- If the disease hasn't been discovered yet it cannot be guessed, go here instead.
-- Ask the player
-- Wait two months before going home anyway.
humanoid:setMood("patient_wait", "activate")
humanoid.waiting = 60
local guess_enabled = humanoid.hospital.disease_casebook[humanoid.disease.id].discovered
local message = {
{text = _S.fax.diagnosis_failed.situation},
{text = " "},
{text = _S.fax.diagnosis_failed.what_to_do_question},
choices = {
{text = _S.fax.diagnosis_failed.choices.send_home, choice = "send_home"},
{text = _S.fax.diagnosis_failed.choices.take_chance, choice = "guess_cure", enabled = guess_enabled},
{text = _S.fax.diagnosis_failed.choices.wait, choice = "wait"},
},
}
if guess_enabled then
table.insert(message, 3, {text = _S.fax.diagnosis_failed.partial_diagnosis_percentage_name
:format(math.round(humanoid.diagnosis_progress*100), humanoid.disease.name)})
end
TheApp.ui.bottom_panel:queueMessage("information", message, humanoid)
humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.awaiting_decision)
-- This seek_room action will be reused, return that it's valid.
return true
else
-- Guess "type of disease" automatically.
-- A patient with an undiscovered disease should never get here.
assert(humanoid.hospital.disease_casebook[humanoid.disease.id].discovered)
humanoid:setDiagnosed()
humanoid:unregisterRoomBuildCallback(action.build_callback)
humanoid:unregisterRoomRemoveCallback(action.remove_callback)
humanoid:unregisterStaffCallback(action.staff_callback)
if humanoid:agreesToPay(humanoid.disease.id) then
humanoid:queueAction({
name = "seek_room",
room_type = humanoid.disease.treatment_rooms[1],
treatment_room = true,
}, 1)
else
humanoid:goHome("over_priced", humanoid.disease.id)
end
humanoid:finishAction()
end
end
local action_seek_room_interrupt = permanent"action_seek_room_interrupt"( function(action, humanoid)
humanoid:setMood("patient_wait", "deactivate")
-- Just in case we are somehow started again:
-- FIXME: This seems to be used for intermediate actions like peeing while the patient is waiting
-- Unfortunately this means that if you finish the required room while the patient is peeing
-- the callback does not happen, meaning that the message does not disappear.
humanoid:unregisterRoomBuildCallback(action.build_callback)
humanoid:unregisterRoomRemoveCallback(action.remove_callback)
humanoid:unregisterStaffCallback(action.staff_callback)
action.build_callback = nil
action.remove_callback = nil
action.staff_callback = nil
action.done_init = false
humanoid:finishAction()
end)
local function action_seek_room_start(action, humanoid)
if action.todo_interrupt then
humanoid:finishAction()
return
end
-- Seeking for toilets is a special case with its own action.
if action.room_type == "toilets" then
humanoid:queueAction(SeekToiletsAction(), 1)
humanoid:finishAction()
return
end
-- Tries to find the room, if it is a diagnosis room, try to find any room in the diagnosis list.
if not humanoid.diagnosed or action.room_type == "research" then
local room = action_seek_room_find_room(action, humanoid)
-- if we have the room but not the staff we shouldn't seek out the room either
if room then
if humanoid.message then
TheApp.ui.bottom_panel:removeMessage(humanoid)
end
action_seek_room_goto_room(room, humanoid, action.diagnosis_room)
return
end
end
-- check we can treat the patient - and just shortcut the processing
local req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id)
if humanoid.diagnosed and not req then
local room = action_seek_room_find_room(action, humanoid)
-- if we have the room but not the staff we shouldn't seek out the room either
if room then
if humanoid.message then
TheApp.ui.bottom_panel:removeMessage(humanoid)
end
action_seek_room_goto_room(room, humanoid, action.diagnosis_room)
return
end
end
-- we can't yet treat the patient, register callbacks
-- create message etc
if not action.done_init then
action.done_init = true
action.must_happen = true
local remove_callback = --[[persistable:action_seek_room_remove_callback]] function(rm)
humanoid:updateMessage("research")
end -- End of remove_callback function
action.remove_callback = remove_callback
humanoid:registerRoomRemoveCallback(remove_callback)
local build_callback
local staff_callback
staff_callback = --[[persistable:action_seek_room_staff_callback]] function(staff)
-- we might have hired or fired a staff member
-- technically we don't care about Receptionist or Handyman
if staff then
if staff.humanoid_class == "Receptionist" or staff.humanoid_class == "Handyman" then
return
end
end
-- update the message either way
humanoid:updateMessage("research")
local room_req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id)
if not room_req then
local room = action_seek_room_find_room(action, humanoid)
if room then
TheApp.ui.bottom_panel:removeMessage(humanoid)
humanoid:unregisterRoomBuildCallback(build_callback)
humanoid:unregisterRoomRemoveCallback(remove_callback)
humanoid:unregisterStaffCallback(staff_callback)
action_seek_room_goto_room(room, humanoid, action.diagnosis_room)
end
end
end -- End of staff_callback function
action.staff_callback = staff_callback
humanoid:registerStaffCallback(staff_callback)
build_callback = --[[persistable:action_seek_room_build_callback]] function(rm)
-- if research room was built, message may need to be updated
humanoid:updateMessage("research")
local found = false
if rm.room_info.id == action.room_type then
found = true
elseif rm.room_info.id == action.room_type_needed then
-- So the room that we're going to is not actually the room we waited for to be built.
-- Example: Will go to ward, but is waiting for the operating theatre.
-- Clean up and start over to find the room we actually want to go to.
TheApp.ui.bottom_panel:removeMessage(humanoid)
humanoid:unregisterRoomBuildCallback(build_callback)
humanoid:unregisterRoomRemoveCallback(remove_callback)
humanoid:unregisterStaffCallback(staff_callback)
action.room_type_needed = nil
action_seek_room_start(action, humanoid)
elseif not humanoid.diagnosed then
-- Waiting for a diagnosis room, we need to go through the list - unless it is gp
if action.room_type ~= "gp" then
for i = 1, #humanoid.available_diagnosis_rooms do
if humanoid.available_diagnosis_rooms[i].id == rm.room_info.id then
found = true
end
end
end
end
if found then
-- Don't add a "go to room" action to the patient's queue if the
-- autopsy machine is about to kill them:
-- don't need this as we unregistered all previous callbacks if we went to research
local room_req = humanoid.hospital:checkDiseaseRequirements(humanoid.disease.id)
-- get required staff
if not room_req then
action_seek_room_goto_room(rm, humanoid, action.diagnosis_room)
TheApp.ui.bottom_panel:removeMessage(humanoid)
humanoid:unregisterRoomBuildCallback(build_callback)
humanoid:unregisterRoomRemoveCallback(remove_callback)
humanoid:unregisterStaffCallback(staff_callback)
end
end
end -- End of build_callback function
action.build_callback = build_callback
humanoid:registerRoomBuildCallback(build_callback)
action.on_interrupt = action_seek_room_interrupt
end
-- Things needed to get the patient to the correct room in due time are done. Now it's
-- time to let the player know about it too.
-- If done_walk is set the meander action that takes place after not finding any room has been
-- done = nothing more to do right now.
if not action.done_walk then
local action_still_valid = true
if not action.message_sent then
-- Make a message about that something needs to be done about this patient
if humanoid.diagnosed then
-- The patient is diagnosed, a treatment room is missing.
-- It may happen that it is another room in a series which is missing.
local room_to_find = action.room_type_needed and action.room_type_needed or action.room_type
action_seek_room_no_treatment_room_found(room_to_find, humanoid)
else
-- No more diagnosis rooms can be found
-- The GP's office is a special case. TODO: Make a custom message anyway?
if action.room_type == "gp" then
humanoid:setMood("patient_wait", "activate")
humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.no_gp_available)
else
action_still_valid = action_seek_room_no_diagnosis_room_found(action, humanoid)
end
end
end
if action_still_valid then
action.done_walk = true
humanoid:queueAction(MeanderAction():setCount(1):setMustHappen(true), 0)
end
else
-- Make sure the patient stands in a correct way as he/she is waiting.
local direction = humanoid.last_move_direction
local anims = humanoid.walk_anims
if direction == "north" then
humanoid:setAnimation(anims.idle_north, 0)
elseif direction == "east" then
humanoid:setAnimation(anims.idle_east, 0)
elseif direction == "south" then
humanoid:setAnimation(anims.idle_east, 1)
elseif direction == "west" then
humanoid:setAnimation(anims.idle_north, 1)
end
humanoid:setTilePositionSpeed(humanoid.tile_x, humanoid.tile_y)
end
end
return action_seek_room_start