Debian Codegraph @

CorsixTH/Lua/humanoid_actions/queue.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 class "QueueAction" (HumanoidAction)
22
23 ---@type QueueAction
24 local QueueAction = _G["QueueAction"]
25
26 --! Queue for something (door or reception desk).
27 --!param x X position of the queue.
28 --!param y Y position of the queue.
29 --!param queue (Queue) Queue to join
30 function QueueAction:QueueAction(x, y, queue)
31 assert(type(x) == "number", "Invalid value for parameter 'x'")
32 assert(type(y) == "number", "Invalid value for parameter 'y'")
33 assert(class.is(queue, Queue), "Invalid value for parameter 'queue'")
34
35 self:HumanoidAction("queue")
36 self.x = x -- Position of the queue(?)
37 self.y = y
38 self.face_x = nil -- Tile to turn the face to.
39 self.face_y = nil
40 self.queue = queue
41 self.reserve_when_done = nil -- Object to reserve when leaving the queue.
42 end
43
44 --! Set the object to reserve when queueing is done.
45 --!param door (object) Object to reserve when leaving the queue.
46 --!return (action) self, for daisy-chaining.
47 function QueueAction:setReserveWhenDone(door)
48 assert(class.is(door, Door), "Invalid value for parameter 'door'")
49
50 self.reserve_when_done = door
51 return self
52 end
53
54 --! Set the tile to face.
55 --!param face_x (int) X coordinate of the tile to face.
56 --!param face_y (int) Y coordinate of the tile to face.
57 --!return (action) self, for daisy-chaining.
58 function QueueAction:setFaceDirection(face_x, face_y)
59 assert(type(face_x) == "number", "Invalid value for parameter 'face_x'")
60 assert(type(face_y) == "number", "Invalid value for parameter 'face_y'")
61
62 self.face_x = face_x
63 self.face_y = face_y
64 return self
65 end
66
67 local function get_direction(x, y, facing_x, facing_y)
68 if facing_y < y then
69 return "north"
70 elseif facing_y > y then
71 return "south"
72 end
73 if facing_x > x then
74 return "east"
75 elseif facing_x < x then
76 return "west"
77 end
78 end
79
80 local function interrupt_head(humanoid, n)
81 while n > 1 do
82 local action = humanoid.action_queue[n]
83 if action.name == "use_object" then
84 -- Pull object usages out of the queue
85 if action.object and action.object:isReservedFor(humanoid) then
86 action.object:removeReservedUser(humanoid)
87 end
88 table.remove(humanoid.action_queue, n)
89 else
90 -- Mark other actions as needing interruption
91 assert(action.must_happen)
92 action.todo_interrupt = true
93 end
94 n = n - 1
95 end
96
97 local action = humanoid.action_queue[n]
98 assert(action.must_happen)
99 local on_interrupt = action.on_interrupt
100 if on_interrupt then
101 action.on_interrupt = nil
102 on_interrupt(action, humanoid)
103 end
104 end
105
106 local function action_queue_find_idle(action, humanoid)
107 local found_any = false
108 for i, current_action in ipairs(humanoid.action_queue) do
109 if current_action.name == "idle" then
110 found_any = true
111 if humanoid.action_queue[i + 1] == action then
112 return i
113 end
114 end
115 end
116 if found_any then
117 print("Warning: Proper idle not in action_queue")
118 end
119 return -1
120 end
121
122 local function action_queue_find_drink_action(action, humanoid)
123 local found_any = false
124 for i, current_action in ipairs(humanoid.action_queue) do
125 if current_action.name == "use_object" and current_action.object.object_type.id == "drinks_machine" then
126 found_any = true
127 if humanoid.action_queue[i + 1] == action then
128 return i
129 end
130 end
131 end
132 if found_any then
133 error("Proper drink action not in action_queue")
134 else
135 return -1
136 end
137 end
138
139 local action_queue_leave_bench -- Defined below the next function.
140
141 -- Finish standing includes currently going to the drinks machine.
142 local function action_queue_finish_standing(action, humanoid)
143 local index = action_queue_find_idle(action, humanoid)
144 if index == -1 then
145 -- Maybe going to the drinks machine?
146 index = action_queue_find_drink_action(action, humanoid)
147 if index == -1 then
148 -- Attempt to recover by assuming the person is sitting down.
149 print("Warning: Idle not in action_queue")
150 if humanoid:getCurrentAction().name == "use_object" then
151 -- It is likely that the person is sitting down.
152 return action_queue_leave_bench(action, humanoid)
153 else
154 error("This person seems to neither be standing nor sitting?!")
155 end
156 end
157 end
158 interrupt_head(humanoid, index)
159 index = index + 1
160 while index >= 1 do
161 local current_action = humanoid.action_queue[index]
162 if current_action == action then
163 return index - 1
164 end
165 index = index - 1
166 end
167 error("Queue action not in action_queue")
168 end
169
170 action_queue_leave_bench = function(action, humanoid)
171 local index
172 for i, current_action in ipairs(humanoid.action_queue) do
173 -- Check to see that we haven't
174 -- gotten to the actual queue action yet.
175 -- Instead of crashing if we have, try with the assumption
176 -- that we're actually standing.
177 if current_action == action then
178 print("Warning: A patient supposedly sitting down was not," ..
179 " trying to recover assuming he/she is standing.")
180 return action_queue_finish_standing(action, humanoid)
181 end
182 if current_action.name == "use_object" then
183 if humanoid.action_queue[i + 1] == action then
184 interrupt_head(humanoid, i)
185 index = i
186 break
187 end
188 end
189 end
190 index = index + 1
191 while index >= 1 do
192 local current_action = humanoid.action_queue[index]
193 if current_action == action then
194 return index - 1
195 end
196 index = index - 1
197 end
198 error("Queue action not in action_queue")
199 end
200
201 local action_queue_on_change_position = permanent"action_queue_on_change_position"( function(action, humanoid)
202 -- Only proceed with this handler if the patient is still in the queue
203 if not action.is_in_queue then
204 return
205 end
206
207 -- Find out if we have to be standing up - considering humanoid_class covers both health inspector and VIP
208 local must_stand = not class.is(humanoid, Patient) or action.is_leaving or
209 (humanoid.disease and humanoid.disease.must_stand)
210 local queue = action.queue
211 if not must_stand then
212 for i = 1, queue.bench_threshold do
213 if queue:reportedHumanoid(i) == humanoid then
214 must_stand = true
215 break
216 end
217 end
218 end
219
220 -- Maybe we can sit down?
221 if not must_stand then
222 -- Where are we in relation to door/reception desk (goal).
223 -- Specify the distance we no longer look for a bench from our goal tile,
224 -- and the maximum distance we will look for one too.
225 local DISTANCE_THRESHOLD, MAX_DISTANCE = 4, 10
226 if action.current_bench_distance and
227 action.current_bench_distance <= DISTANCE_THRESHOLD then
228 return
229 end
230 local hum_x, hum_y = humanoid.tile_x, humanoid.tile_y
231 local goal_x, goal_y = action.x, action.y
232
233 local distance_from_goal = humanoid.world:getPathDistance(hum_x, hum_y,
234 goal_x, goal_y)
235 if not distance_from_goal then
236 error("Patient in a queue has no calculated distance from themselves to their target door/desk!")
237 end
238
239 -- Look for a free bench that is closer to the goal. If we are standing
240 -- expand the search radius to increase the chance of getting a seat.
241 local bench_look_distance = not action:isStanding() and
242 math.max(2 * distance_from_goal / 3, DISTANCE_THRESHOLD) or MAX_DISTANCE
243
244 local bench, bx, by, new_dist = humanoid.world:getFreeBench(goal_x, goal_y, bench_look_distance)
245 if bench then
246 local num_actions_prior
247 if action:isStanding() then
248 num_actions_prior = action_queue_finish_standing(action, humanoid)
249 else
250 num_actions_prior = action_queue_leave_bench(action, humanoid)
251 end
252 action.current_bench_distance = new_dist
253 humanoid:queueAction(WalkAction(bx, by):setMustHappen(true), num_actions_prior)
254 humanoid:queueAction(UseObjectAction(bench):setMustHappen(true), num_actions_prior + 1)
255 bench.reserved_for = humanoid
256 return
257 elseif not action:isStanding() then
258 -- Already sitting down, so nothing to do.
259 return
260 end
261 end
262
263 -- Stand up in the correct position in the queue
264 local standing_index = 0
265 local our_room = humanoid:getRoom()
266 for _, person in ipairs(queue) do
267 if person == humanoid then
268 break
269 end
270 if queue.callbacks[person]:isStanding() and person:getRoom() == our_room then
271 standing_index = standing_index + 1
272 end
273 end
274 local ix, iy = humanoid.world:getIdleTile(action.x, action.y, standing_index)
275 assert(ix and iy)
276 local facing_x, facing_y
277 if standing_index == 0 then
278 facing_x, facing_y = action.face_x or action.x, action.face_y or action.y
279 else
280 facing_x, facing_y = humanoid.world:getIdleTile(action.x, action.y, standing_index - 1)
281 end
282 assert(facing_x and facing_y)
283 local idle_direction = get_direction(ix, iy, facing_x, facing_y)
284 if action:isStanding() then
285 local idle_index = action_queue_find_idle(action, humanoid)
286 if idle_index == -1 then
287 idle_index = action_queue_find_drink_action(action, humanoid)
288 if idle_index ~= -1 then
289 -- Going to get a drink. Do nothing since it will be fixed after getting the drink.
290 return
291 else
292 error("Could not find an idle or drink action when trying to stand in line.")
293 end
294 end
295 humanoid.action_queue[idle_index].direction = idle_direction
296 humanoid:queueAction(WalkAction(ix, iy):setMustHappen(true):setIsLeaving(humanoid:isLeaving()), idle_index - 1)
297 else
298 action.current_bench_distance = nil
299 local num_actions_prior = action_queue_leave_bench(action, humanoid)
300 humanoid:queueAction(WalkAction(ix, iy):setMustHappen(true), num_actions_prior)
301 humanoid:queueAction(IdleAction():setDirection(idle_direction):setMustHappen(true),
302 num_actions_prior + 1)
303 end
304 end)
305
306 local action_queue_is_standing = permanent"action_queue_is_standing"( function(action)
307 return not action.current_bench_distance
308 end)
309
310 local action_queue_on_leave = permanent"action_queue_on_leave"( function(action, humanoid)
311 action.is_in_queue = false
312 if action.reserve_when_done then
313 action.reserve_when_done:addReservedUser(humanoid)
314 end
315 for i, current_action in ipairs(humanoid.action_queue) do
316 if current_action == action then
317 interrupt_head(humanoid, i)
318 return
319 end
320 end
321 error("Queue action not in action_queue")
322 end)
323
324 -- While queueing one could get thirsty.
325 local action_queue_get_soda = permanent"action_queue_get_soda"(
326 function(action, humanoid, machine, mx, my, fun_after_use)
327 local num_actions_prior
328 if action:isStanding() then
329 num_actions_prior = action_queue_finish_standing(action, humanoid)
330 else
331 num_actions_prior = action_queue_leave_bench(action, humanoid)
332 end
333
334 -- Callback function used after the drinks machine has been used.
335 local --[[persistable:action_queue_get_soda_after_use]] function after_use()
336 fun_after_use() -- Defined in patient:tickDay
337 -- If the patient is still in the queue, insert an idle action so that
338 -- change_position can do its work.
339 -- Note that it is inserted after the currently executing use_object action.
340 if action.is_in_queue and not humanoid.going_home then
341 humanoid:queueAction(IdleAction():setMustHappen(true), 1)
342 action_queue_on_change_position(action, humanoid)
343 end
344 end
345
346 -- Walk to the machine and then use it.
347 humanoid:queueAction(WalkAction(mx, my):setMustHappen(true), num_actions_prior)
348 humanoid:queueAction(UseObjectAction(machine):setAfterUse(after_use)
349 :setMustHappen(true), num_actions_prior + 1)
350 machine:addReservedUser(humanoid)
351
352 -- Make sure no one thinks we're sitting down anymore.
353 action.current_bench_distance = nil
354 end)
355
356 local action_queue_interrupt = permanent"action_queue_interrupt"( function(action, humanoid)
357 if action.is_in_queue then
358 action.queue:removeValue(humanoid)
359 if action.reserve_when_done then
360 action.reserve_when_done:updateDynamicInfo()
361 end
362 end
363 if action.reserve_when_done then
364 if action.reserve_when_done:isReservedFor(humanoid) then
365 action.reserve_when_done:removeReservedUser(humanoid)
366 end
367 end
368 humanoid:finishAction()
369 end)
370
371 local function action_queue_start(action, humanoid)
372 local queue = action.queue
373
374 if action.done_init then
375 return
376 end
377 action.done_init = true
378 action.must_happen = true
379 action.on_interrupt = action_queue_interrupt
380 action.onChangeQueuePosition = action_queue_on_change_position
381 action.onLeaveQueue = action_queue_on_leave
382 action.onGetSoda = action_queue_get_soda
383 action.isStanding = action_queue_is_standing
384
385 action.is_in_queue = true
386 queue:unexpect(humanoid)
387 queue:push(humanoid, action)
388
389 local door = action.reserve_when_done
390 if door then
391 door:updateDynamicInfo()
392 if class.is(humanoid, Patient) then
393 humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.queueing_for:format(door.room.room_info.name))
394 end
395 end
396 humanoid:queueAction(IdleAction():setMustHappen(true):setIsLeaving(humanoid:isLeaving()), 0)
397 action:onChangeQueuePosition(humanoid)
398
399 if queue.same_room_priority then
400 queue.same_room_priority:getRoom():tryAdvanceQueue()
401 end
402 end
403
404 return action_queue_start