Debian Codegraph @

CorsixTH/Lua/humanoid_actions/walk.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 "WalkAction" (HumanoidAction)
22
23 ---@type WalkAction
24 local WalkAction = _G["WalkAction"]
25
26 --! Action to walk to a given position.
27 --!param x (int) X coordinate of the destination tile.
28 --!param y (int) Y coordinate of the destination tile.
29 --!param trimmed (boolean) Optional parameter to finish action on re-route
30 function WalkAction:WalkAction(x, y, trimmed)
31 assert(type(x) == "number", "Invalid value for parameter 'x'")
32 assert(type(y) == "number", "Invalid value for parameter 'y'")
33
34 self:HumanoidAction("walk")
35 self.x = x
36 self.y = y
37 self.trimmed = trimmed
38 self.truncate_only_on_high_priority = false
39 self.walking_to_vaccinate = false -- Nurse walking with the intention to vaccinate
40 self.is_entering = false -- Whether the walk enters a room.
41 end
42
43 function WalkAction:truncateOnHighPriority()
44 self.truncate_only_on_high_priority = true
45 return self
46 end
47
48 --! Nurse is walking with the intention to vaccinate.
49 --!return (action) self, for daisy-chaining.
50 function WalkAction:enableWalkingToVaccinate()
51 self.walking_to_vaccinate = true
52 return self
53 end
54
55 --! Set a flag whether the walk enters a room.
56 --!param entering (bool) If set or nil, set the flag of entering the room.
57 --!return (action) self, for daisy-chaining.
58 function WalkAction:setIsEntering(entering)
59 assert(type(entering) == "boolean", "Invalid value for parameter 'entering'")
60
61 self.is_entering = entering
62 return self
63 end
64
65 local action_walk_interrupt
66 action_walk_interrupt = permanent"action_walk_interrupt"( function(action, humanoid, high_priority)
67 if action.truncate_only_on_high_priority and not high_priority then
68 action.on_interrupt = action_walk_interrupt
69 return
70 end
71
72 -- Truncate the remainder of the path
73 for j = #action.path_x, action.path_index + 1, -1 do
74 action.path_x[j] = nil
75 action.path_y[j] = nil
76 end
77 -- Unreserve any door which we had reserved unless specifically told not to.
78 if not action.keep_reserved then
79 local door = action.reserve_on_resume
80 if door and (door.reserved_for == humanoid or class.is(humanoid, Vip)) then -- "or class.is(humanoid, Vip)" is added as a temporary fix
81 -- TODO: find the cause of the "VIP bug", why does the door not get unreserved sometimes when the VIP has looked into a room? See issue 1025
82 door.reserved_for = nil
83 door:getRoom():tryAdvanceQueue()
84 end
85
86 -- guarding with the action.keep_reserved check as we don't want to unexpect
87 -- if we are just interrupting but resume the walk action afterwards
88 humanoid:unexpectFromRoom(humanoid.world:getRoom(action.x, action.y))
89 else
90 -- This flag can be used only once at a time.
91 action.keep_reserved = nil
92 end
93
94 -- Terminate immediately if high-priority
95 if high_priority then
96 local timer_function = humanoid.timer_function
97 humanoid:setTimer(nil)
98 timer_function(humanoid)
99 end
100
101 if action.walking_to_vaccinate then
102 local hospital = humanoid.hospital or humanoid.last_hospital
103 local epidemic = hospital.epidemic
104 if epidemic then
105 epidemic:interruptVaccinationActions(humanoid)
106 end
107 end
108 end)
109
110 local flag_flip_h = 1
111
112 local navigateDoor
113
114 local function action_walk_raw(humanoid, x1, y1, x2, y2, map, timer_fn)
115 -- The variables below must always make up factor*quantity = 8 or the
116 -- animation glitches
117 -- Factor must also be able to multiply by 2 to become an integer
118 -- Quantity must always be an integer
119 local factor = 1
120 local quantity = 8
121 if humanoid.speed and humanoid.speed == "fast" then
122 factor = 2
123 quantity = 4
124 end
125
126 local anims = humanoid.walk_anims
127 local world = humanoid.world
128 local notify_object = world:getObjectToNotifyOfOccupants(x2, y2)
129 if notify_object then
130 notify_object:onOccupantChange(1)
131 end
132 notify_object = world:getObjectToNotifyOfOccupants(x1, y1)
133 if notify_object then
134 notify_object:onOccupantChange(-1)
135 end
136 if x1 ~= x2 then
137 if x1 < x2 then
138 if map and map:getCellFlags(x2, y2).doorWest then
139 return navigateDoor(humanoid, x1, y1, "east")
140 else
141 humanoid.last_move_direction = "east"
142 humanoid:setAnimation(anims.walk_east)
143 humanoid:setTilePositionSpeed(x2, y2, -32, -16, 4*factor, 2*factor)
144 end
145 else
146 if map and map:getCellFlags(x1, y1).doorWest then
147 return navigateDoor(humanoid, x1, y1, "west")
148 else
149 humanoid.last_move_direction = "west"
150 humanoid:setAnimation(anims.walk_north, flag_flip_h)
151 humanoid:setTilePositionSpeed(x1, y1, 0, 0, -4*factor, -2*factor)
152 end
153 end
154 else
155 if y1 < y2 then
156 if map and map:getCellFlags(x2, y2).doorNorth then
157 return navigateDoor(humanoid, x1, y1, "south")
158 else
159 humanoid.last_move_direction = "south"
160 humanoid:setAnimation(anims.walk_east, flag_flip_h)
161 humanoid:setTilePositionSpeed(x2, y2, 32, -16, -4*factor, 2*factor)
162 end
163 else
164 if map and map:getCellFlags(x1, y1).doorNorth then
165 return navigateDoor(humanoid, x1, y1, "north")
166 else
167 humanoid.last_move_direction = "north"
168 humanoid:setAnimation(anims.walk_north)
169 humanoid:setTilePositionSpeed(x1, y1, 0, 0, 4*factor, -2*factor)
170 end
171 end
172 end
173 humanoid:setTimer(quantity, timer_fn)
174 end
175
176 local flags_here, flags_there = {}, {}
177 local action_walk_tick; action_walk_tick = permanent"action_walk_tick"( function(humanoid)
178 local action = humanoid:getCurrentAction()
179 local path_x = action.path_x
180 local path_y = action.path_y
181 local path_index = action.path_index
182 local check_doors = not not humanoid.door_anims
183 local x1, y1 = path_x[path_index ], path_y[path_index ]
184 local x2, y2 = path_x[path_index+1], path_y[path_index+1]
185
186 if not x2 then
187 -- Arrival at final tile
188 humanoid:setTilePositionSpeed(x1, y1)
189 if action.on_next_tile_set then
190 action.on_next_tile_set()
191 end
192 humanoid:finishAction(action)
193 return
194 end
195
196 -- Make sure that the next tile hasn't somehow become impassable since our
197 -- route was determined
198 local map = humanoid.world.map.th
199 map:getCellFlags(x1, y1, flags_here)
200 map:getCellFlags(x2, y2, flags_there)
201 local recalc_route = not flags_there.passable and flags_here.passable
202 -- Also make sure that a room hasn't unexpectedly been built on top of the
203 -- path since the route was calculated.
204 if not recalc_route and flags_here.roomId ~= flags_there.roomId then
205 -- if going to the corridor we don't care as we are at the door
206 -- as this will be false we won't bother checking for rerouting
207 recalc_route = flags_there.room
208 local door = TheApp.objects.door.id
209 local door2 = TheApp.objects.swing_door_right.id
210 local doorcheck = {[door] = true, [door2] = true}
211 -- but we should see if this is the same room id we want to go to and cancel the reroute
212 -- ensure we still have a door on this route
213 if recalc_route and (humanoid.world:getObject(x1, y1, doorcheck) or
214 humanoid.world:getObject(x2, y2, doorcheck)) and -- is there any door
215 map:getCellFlags(path_x[#path_x], path_y[#path_y]).roomId ==
216 flags_there.roomId then
217 -- A walk including a trimmed path could have the last tile inside a room, and
218 -- might need a new route completely (e.g. seek reception)
219 recalc_route = action.trimmed
220 end
221 end
222 if recalc_route then
223 if map:getCellFlags(x1, y1).passable then
224 humanoid:setTilePositionSpeed(x1, y1)
225 if action.on_next_tile_set then
226 action.on_next_tile_set()
227 end
228 if action.trimmed then -- request new route
229 humanoid:finishAction(action)
230 return
231 end
232 return action:on_restart(humanoid)
233 end
234 end
235
236 -- on_next_tile_set can be set in the call to action_walk_raw, but it is
237 -- then to be called AFTER the next raw walk or tile set, which is why we
238 -- remember the previous value and call that, rather than call the new value.
239 local on_next_tile_set = action.on_next_tile_set
240 action_walk_raw(humanoid, x1, y1, x2, y2, check_doors and map, action_walk_tick)
241 action.path_index = path_index + 1
242 if on_next_tile_set then
243 on_next_tile_set()
244 end
245 end)
246
247 -- This is a slight hack, but is the easiest way to make walk functionality
248 -- available to other actions which want to do low-level walk operations.
249 strict_declare_global "HumanoidRawWalk"
250 HumanoidRawWalk = action_walk_raw
251
252 local action_walk_tick_door = permanent"action_walk_tick_door"( function(humanoid)
253 local door = humanoid.user_of
254 if not class.is(door, SwingDoor) then
255 -- The doors will need to finish swinging before another humanoid can walk through.
256 door:removeUser(humanoid)
257 end
258 humanoid.user_of = nil
259 return action_walk_tick(humanoid)
260 end)
261
262 navigateDoor = function(humanoid, x1, y1, dir)
263 local action = humanoid:getCurrentAction()
264 local dx = x1
265 local dy = y1
266 if dir == "east" then
267 dx = dx + 1
268 elseif dir == "south" then
269 dy = dy + 1
270 end
271 local swinging = false
272 local door = humanoid.world:getObject(dx, dy, "door")
273 if not door then
274 swinging = true
275 door = humanoid.world:getObject(dx, dy, "swing_door_right")
276 end
277 door.queue:unexpect(humanoid)
278 door:updateDynamicInfo()
279 local room = door:getRoom()
280 local is_entering_room = room and humanoid:getRoom() ~= room
281
282 if class.is(humanoid, Staff) and is_entering_room and
283 not class.is(humanoid, Handyman) then
284 -- A member of staff is entering, but is maybe no longer needed
285 -- in this room?
286 if not room.is_active or not room:staffFitsInRoom(humanoid) then
287 humanoid:queueAction(IdleAction(), 0)
288 humanoid:setTilePositionSpeed(x1, y1)
289 humanoid:setNextAction(IdleAction():setCount(10), 0)
290 humanoid:queueAction(MeanderAction())
291 if door.reserved_for == humanoid then
292 door.reserved_for = nil
293 room:tryAdvanceQueue()
294 end
295 return
296 end
297 end
298 if door.user or (door.reserved_for and door.reserved_for ~= humanoid) or
299 (is_entering_room and not room:canHumanoidEnter(humanoid)) then
300 local queue = door.queue
301 if door.reserved_for == humanoid then
302 door.reserved_for = nil
303 room:tryAdvanceQueue()
304 end
305 humanoid:setTilePositionSpeed(x1, y1)
306 local action_index = 0
307 if is_entering_room and queue:size() == 0 and not room:getPatient() and
308 not door.user and not door.reserved_for and humanoid.should_knock_on_doors and
309 room.room_info.required_staff and not swinging then
310 humanoid:queueAction(KnockDoorAction(humanoid, door, dir), action_index)
311 action_index = action_index + 1
312 end
313 -- a doctor/nurse answering a call but not yet left the room will not ever be leaving
314 -- if they happen to queue when leaving the room, humanoid:isLeaving will not reflect
315 -- the true state, so just use the is_entering_room fact instead
316 humanoid:queueAction(QueueAction(x1, y1, queue):setIsLeaving(not is_entering_room)
317 :setReserveWhenDone(door), action_index)
318 action.must_happen = action.saved_must_happen
319 action.reserve_on_resume = door
320 return
321 end
322 if action.reserve_on_resume then
323 assert(action.reserve_on_resume == door)
324 action.reserve_on_resume = nil
325 elseif is_entering_room and not action.done_knock and humanoid.should_knock_on_doors and
326 room.room_info.required_staff and not swinging then
327 humanoid:setTilePositionSpeed(x1, y1)
328 humanoid:queueAction(KnockDoorAction(humanoid, door, dir), 0)
329 action.reserve_on_resume = door
330 action.done_knock = true
331 return
332 end
333
334 local to_x, to_y
335 local anims = humanoid.door_anims
336 if not anims.leaving or not anims.entering then
337 local from_rm, to_rm = room.room_info.id, "corridor"
338 if is_entering_room then
339 from_rm, to_rm = to_rm, from_rm
340 end
341 error(("Humanoid (%s) without door animations trying to walk through "..
342 "door (from %s to %s)"):format(humanoid.humanoid_class, from_rm, to_rm))
343 end
344 humanoid:setTilePositionSpeed(dx, dy)
345 humanoid.user_of = door
346 door:setUser(humanoid)
347 local entering = swinging and anims.entering_swing or anims.entering
348 local leaving = swinging and anims.leaving_swing or anims.leaving
349
350 local duration, direction
351 if dir == "north" then
352 humanoid:setAnimation(leaving)
353 duration = TheApp.animation_manager:getAnimLength(leaving)
354 to_x, to_y = dx, dy - 1
355 direction = "in"
356
357 elseif dir == "west" then
358 humanoid:setAnimation(leaving, flag_flip_h)
359 duration = TheApp.animation_manager:getAnimLength(leaving)
360 to_x, to_y = dx - 1, dy
361 direction = "in"
362
363 elseif dir == "east" then
364 humanoid:setAnimation(entering)
365 duration = TheApp.animation_manager:getAnimLength(entering)
366 to_x, to_y = dx, dy
367 direction = "out"
368
369 elseif dir == "south" then
370 humanoid:setAnimation(entering, flag_flip_h)
371 duration = TheApp.animation_manager:getAnimLength(entering)
372 to_x, to_y = dx, dy
373 direction = "out"
374 end
375
376 humanoid.last_move_direction = dir
377 if swinging then
378 door:swingDoors(direction, duration)
379 end
380
381 -- We want to notify the rooms on either side of the door that the humanoid
382 -- has entered / left, but we want to do this AFTER the humanoid has gone
383 -- through the door (so that their tile position reflects the room which they
384 -- are now in).
385 local --[[persistable:action_walk_on_next_tile_set]] function on_next_tile_set()
386 if action.on_next_tile_set == on_next_tile_set then
387 action.on_next_tile_set = nil
388 end
389 local rm = humanoid.world:getRoom(x1, y1)
390 if rm then
391 rm:onHumanoidLeave(humanoid)
392 end
393 rm = humanoid.world:getRoom(to_x, to_y)
394 if rm then
395 rm:onHumanoidEnter(humanoid)
396 end
397 end
398 action.on_next_tile_set = on_next_tile_set
399
400 if is_entering_room then
401 humanoid.in_room = room
402 end
403
404 action.path_index = action.path_index + 1
405 humanoid:setTimer(duration, action_walk_tick_door)
406 end
407
408 local function action_walk_start(action, humanoid)
409 -- Possible future optimisation: when walking from somewhere inside the hospital
410 -- to somewhere outside the hospital (or from one building to another?), do
411 -- pathfinding in two steps, with the building door as a middle node
412 local path_x, path_y = humanoid.world:getPath(humanoid.tile_x, humanoid.tile_y, action.x, action.y)
413 if not path_x or #path_x == 1 then
414 -- Finishing an action from within the start handler is a very bad idea, as
415 -- it is normal when ordering several actions to setNextAction the first
416 -- one, then queueAction the rest. If the first starts straight away, and
417 -- then finishes straight away, then the humanoid is left with an empty
418 -- action queue. Hence we wait one tick before finishing. We still need to
419 -- set the humanoid animation / position though, which is delegated to the
420 -- idle action (if this wasn't done, then the previous animation would be
421 -- used, which might involve an object).
422 TheApp.humanoid_actions.idle(action, humanoid)
423 humanoid:setTimer(1, humanoid.finishAction)
424 return
425 end
426 action.path_x = path_x
427 action.path_y = path_y
428 action.path_index = 1
429 if not action.no_truncate then
430 action.on_interrupt = action_walk_interrupt
431 end
432 if action.on_restart ~= nil then
433 action.saved_must_happen = action.must_happen
434 end
435 action.on_restart = action_walk_start
436 action.must_happen = true
437 if action.reserve_on_resume and not action.todo_interrupt then
438 action.reserve_on_resume.reserved_for = humanoid
439 end
440
441 return action_walk_tick(humanoid)
442 end
443
444 return action_walk_start