Debian Codegraph @

CorsixTH/Lua/objects/reception_desk.lua

raw
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 local object = {}
22 object.id = "reception_desk"
23 object.thob = 11
24 object.class = "ReceptionDesk"
25 object.name = _S.object.reception_desk
26 object.tooltip = _S.tooltip.objects.reception_desk
27 object.ticks = true
28 object.corridor_object = 1
29 object.build_preview_animation = 5060
30 object.idle_animations = {
31 north = 2062,
32 east = 2064,
33 }
34 object.orientations = {
35 north = {
36 footprint = { {0, 0}, {0, -1, only_passable = true},
37 {0, 1, only_passable = true}, {1, 0, need_north_side = true, need_south_side = true},
38 {-1, 0, need_north_side = true, need_south_side = true}
39 },
40 use_position = {0, -1},
41 use_position_secondary = {0, 1},
42 },
43 east = {
44 footprint = { {0, 0}, {0, -1, need_west_side = true, need_east_side = true},
45 {0, 1, need_west_side = true, need_east_side = true}, {1, 0, only_passable = true},
46 {-1, 0, only_passable = true}
47 },
48 use_position = {1, 0},
49 use_position_secondary = {-1, 0},
50 },
51 south = {
52 footprint = { {0, 0}, {0, -1, only_passable = true}, {0, 1, only_passable = true},
53 {1, 0, need_north_side = true, need_south_side = true},
54 {-1, 0, need_north_side = true, need_south_side = true}
55 },
56 use_position = {0, 1},
57 use_position_secondary = {0, -1},
58 },
59 west = {
60 footprint = { {0, 0}, {0, -1, need_west_side = true, need_east_side = true},
61 {0, 1, need_west_side = true, need_east_side = true},
62 {1, 0, only_passable = true}, {-1, 0, only_passable = true}
63 },
64 use_position = {-1, 0},
65 use_position_secondary = {1, 0},
66 },
67 }
68
69 corsixth.require("queue")
70
71 class "ReceptionDesk" (Object)
72
73 ---@type ReceptionDesk
74 local ReceptionDesk = _G["ReceptionDesk"]
75
76 function ReceptionDesk:ReceptionDesk(...)
77 self:Object(...)
78 self.queue = Queue()
79 self.queue:setBenchThreshold(3) -- Keep 3 people standing in the queue even if there are benches
80 self.queue:setMaxQueue(20) -- larger queues for reception desk
81 self.hover_cursor = TheApp.gfx:loadMainCursor("queue")
82 self.queue_advance_timer = 0
83 end
84
85 function ReceptionDesk:onClick(ui, button)
86 if button == "left" then
87 local queue_window = UIQueue(ui, self.queue)
88 ui:addWindow(queue_window)
89 else
90 return Object.onClick(self, ui, button)
91 end
92 end
93
94 function ReceptionDesk:tick()
95 --! Handle a patient at the reception desk.
96 --!param patient The patient at the front of queue.
97 --!param is_new (boolean) Have they just entered the hospital?
98 --!return action taken by patient
99 local function handlePatient(patient, is_new)
100 if not is_new then
101 -- Patient redirected to reception, send them on their way
102 local id = patient.next_room_to_visit.room_info.id
103 return patient:queueAction(SeekRoomAction(id))
104 end
105
106 --else: a new patient has arrived in the hospital
107 if patient:agreesToPay("diag_gp") then
108 return patient:queueAction(SeekRoomAction("gp"))
109 end
110 --Patient goes home, too costly
111 return patient:goHome("over_priced", "diag_gp")
112 end
113
114 -- Sends the VIP on their way.
115 local function handleVIP(vip)
116 vip:queueAction(IdleAction())
117 vip.waiting = 1
118 end
119
120 -- Let the Inspector conclude the epidemic, then send them on their way.
121 local function handleInspector(inspector)
122 if inspector.going_home then return end
123 local epidemic = self.hospital.epidemic
124 if epidemic then
125 epidemic:handleInspectorArrival()
126 inspector:goHome()
127 end
128 end
129
130 --! Advance the front humanoid of the queue.
131 --!param humanoid (entity) NPC to be checked/advanced
132 --!return (boolean) true if ready to be dealt with, else false.
133 local function advanceQueue(humanoid)
134 if humanoid:getCurrentAction().name == "idle" then
135 self.queue_advance_timer = self.queue_advance_timer + 1
136 end
137 return self.queue_advance_timer >= 4 + Date.hoursPerDay() * (1.0 - self.receptionist.profile.skill)
138 end
139
140 -- Start of the ReceptionDesk:tick function.
141 local front_humanoid = self.queue:front()
142 local reset_timer = true
143 if self.receptionist and front_humanoid then
144 if not advanceQueue(front_humanoid) then
145 -- Not ready to deal with humanoid, continue waiting
146 reset_timer = false
147 elseif class.is(front_humanoid, Patient) then
148 handlePatient(front_humanoid, not front_humanoid.next_room_to_visit)
149 elseif class.is(front_humanoid, Inspector) then
150 handleInspector(front_humanoid)
151 elseif class.is(front_humanoid, Vip) then
152 handleVIP(front_humanoid)
153 else
154 error("Unexpected humanoid " .. class.type(front_humanoid) .. " at reception desk.")
155 end
156 if reset_timer then
157 -- Humanoid dealt with, call the next one
158 self.queue:pop()
159 self.queue.visitor_count = self.queue.visitor_count + 1
160 front_humanoid.has_passed_reception = true
161 end
162 end
163 if reset_timer then
164 self.queue_advance_timer = 0
165 end
166 return Object.tick(self)
167 end
168
169 --! Reception desk looks for a receptionist.
170 --!return (boolean) Desk has a receptionist attached to it (may still be on her way to the desk).
171 function ReceptionDesk:checkForNearbyStaff()
172 if self.receptionist or self.reserved_for then
173 -- Already got staff, or a staff member is on the way
174 return true
175 end
176
177 local nearest_staff, nearest_d
178 local use_x, use_y = self:getSecondaryUsageTile()
179 for _, entity in ipairs(self.world.entities) do
180 if class.is(entity, Receptionist) and not entity.associated_desk and not entity.fired then
181 local distance = self.world.pathfinder:findDistance(entity.tile_x, entity.tile_y, use_x, use_y)
182 if not nearest_d or distance < nearest_d then
183 nearest_staff = entity
184 nearest_d = distance
185 end
186 end
187 end
188 if not nearest_staff then
189 return false
190 end
191
192 self:occupy(nearest_staff)
193 return true
194 end
195
196 -- how many tiles further are we willing to walk for 1 person fewer in the queue
197 local tile_factor = 10
198 function ReceptionDesk:getUsageScore()
199 local score = self.queue:patientSize() * tile_factor
200 -- Add constant penalty if queue is full
201 if self.queue:isFull() then
202 score = score + 1000
203 end
204 return score
205 end
206
207 function ReceptionDesk:setTile(x, y)
208 Object.setTile(self, x, y)
209 if x and y then
210 self:checkForNearbyStaff()
211 self.hospital:buildReceptionDesksCache()
212 end
213 return self
214 end
215
216 function ReceptionDesk:onPickUp()
217 self.hospital:buildReceptionDesksCache()
218 Object.onPickUp(self)
219 end
220
221 function ReceptionDesk:resetUsageAndReservaton()
222 self.being_destroyed = true -- temporary flag for receptionist (re-)routing logic
223
224 local receptionist = self.receptionist or self.reserved_for
225 if receptionist then
226 receptionist:handleRemovedObject(self)
227 self.receptionist = nil
228 self.reserved_for = nil
229
230 -- Find a new reception desk for the receptionist
231 self.world:findObjectNear(receptionist, "reception_desk", nil, false,
232 function(x, y)
233 local obj = self.world:getObject(x, y, "reception_desk")
234 -- Make sure we are not selecting the same desk again
235 if obj and obj ~= self then
236 return obj:occupy(receptionist)
237 end
238 end)
239 end
240 self.queue:rerouteAllPatients(nil)
241
242 self.being_destroyed = nil
243 Object.resetUsageAndReservaton(self)
244 end
245
246 --[[ Create orders for the specified receptionist to walk to and then staff the reception desk,
247 if not already staffed or someone is on the way
248 !param receptionist (Staff) the receptionist to occupy this desk
249 !return true if the receptionist was ordered to the desk
250 ]]
251 function ReceptionDesk:occupy(receptionist)
252 if not self.receptionist and not self.reserved_for then
253 self.reserved_for = receptionist
254 receptionist.associated_desk = self
255
256 local use_x, use_y = self:getSecondaryUsageTile()
257 receptionist:setNextAction(WalkAction(use_x, use_y):setMustHappen(true))
258 receptionist:queueAction(StaffReceptionAction(self))
259 return true
260 end
261 end
262
263 return object