Debian Codegraph @

CorsixTH/Lua/humanoid_actions/seek_reception.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 "SeekReceptionAction" (HumanoidAction)
22
23 ---@type SeekReceptionAction
24 local SeekReceptionAction = _G["SeekReceptionAction"]
25
26 -- Distance from queue that patient can join a reception desk queue.
27 local can_join_queue_distance = 6
28
29 function SeekReceptionAction:SeekReceptionAction()
30 self:HumanoidAction("seek_reception")
31 end
32
33 --! Can the humanoid join the reception queue at the given position?
34 --!param humanoid (Humanoid) Humanoid going to the reception.
35 --!param x (int) X coordinate of the tile to consider.
36 --!param y (int) Y coordinate of the tile to consider.
37 --!param dist (int) Distance of the tile to the reception.
38 --!return (bool) Whether the humanoid can start queueing.
39 local function can_join_queue_at(humanoid, x, y, dist)
40 local flag_cache = humanoid.world.map.th:getCellFlags(x, y)
41 return flag_cache.hospital and not flag_cache.room and
42 dist <= can_join_queue_distance and
43 flag_cache.owner == humanoid.hospital:getPlayerIndex()
44 end
45
46 --! Trim the last part of the path when it is possible to join the reception queue.
47 --!param humanoid Humanoid that will walk the path.
48 --!param path_x (array of int) X coordinates of the path, modified in-place.
49 --!param path_y (array of int) Y coordinates of the path, modified in-place.
50 local function trimQueuingTail(humanoid, path_x, path_y)
51 local pathindex = #path_x
52 for i = pathindex - 1, 2, -1 do
53 if can_join_queue_at(humanoid, path_x[i], path_y[i], pathindex - i) then
54 path_x[i + 1] = nil
55 path_y[i + 1] = nil
56 else
57 break
58 end
59 end
60 return
61 end
62
63 -- Start of the seek-reception action.
64 local function action_seek_reception_start(action, humanoid)
65 local world = humanoid.world
66 local best_desk
67 local score
68 local best_distance_from_desk
69
70 assert(humanoid.hospital, "humanoid must be associated with a hospital to seek reception")
71
72 -- Go through all receptions desks.
73 for _, desk in ipairs(humanoid.hospital:getReceptionDesks(true)) do
74 -- Ok, so we found one with staff at it or on the way.
75 -- Is this one better than the last one?
76 -- A lower score is better.
77 -- First find out where the usage tile is.
78 local orientation = desk.object_type.orientations[desk.direction]
79 local x = desk.tile_x + orientation.use_position[1]
80 local y = desk.tile_y + orientation.use_position[2]
81
82 local distance_from_desk = humanoid.world:getPathDistance(humanoid.tile_x, humanoid.tile_y, x, y)
83 if distance_from_desk then
84 local this_score = distance_from_desk + desk:getUsageScore()
85 if not score or this_score < score then
86 -- It is better, or the first one!
87 score = this_score
88 best_desk = desk
89 best_distance_from_desk = distance_from_desk
90 end
91 end
92 end
93 if best_desk then
94 -- We found a desk to go to!
95 local orientation = best_desk.object_type.orientations[best_desk.direction]
96 local x = best_desk.tile_x + orientation.use_position[1]
97 local y = best_desk.tile_y + orientation.use_position[2]
98
99 -- TODO: Make 'else' method work for all humanoids
100 if class.is(humanoid, Vip) or class.is(humanoid, Inspector) then
101 humanoid:updateDynamicInfo(_S.dynamic_info.patient.actions.on_my_way_to
102 :format(best_desk.object_type.name))
103
104 else
105 humanoid:setDynamicInfoText(_S.dynamic_info.patient.actions.on_my_way_to
106 :format(best_desk.object_type.name))
107 end
108 humanoid.waiting = nil
109
110 -- We don't want patients which have just spawned to be joining the queue
111 -- immediately, so walk them closer to the desk before joining the queue
112 if can_join_queue_at(humanoid, humanoid.tile_x, humanoid.tile_y, best_distance_from_desk) then
113 local face_x, face_y = best_desk:getSecondaryUsageTile()
114 humanoid:setNextAction(QueueAction(x, y, best_desk.queue):setMustHappen(action.must_happen)
115 :setFaceDirection(face_x, face_y))
116 else
117 local walk = WalkAction(x, y, true):setMustHappen(action.must_happen)
118 humanoid:queueAction(walk, 0)
119
120 trimQueuingTail(humanoid, walk.path_x, walk.path_y)
121 walk.x = walk.path_x[#walk.path_x]
122 walk.y = walk.path_y[#walk.path_x]
123 end
124 humanoid.hospital:msgMultiReceptionDesks()
125
126 else
127 -- No reception desk found. One will probably be built soon, somewhere in
128 -- the hospital, so either walk to the hospital, or walk around the hospital.
129 local procrastination
130 if humanoid.hospital:isInHospital(humanoid.tile_x, humanoid.tile_y) then
131 procrastination = MeanderAction():setCount(1):setMustHappen(action.must_happen)
132 if not humanoid.waiting then
133 -- Eventually people are going to get bored and leave.
134 humanoid.waiting = 5
135 end
136 else
137 local _, hosp_x, hosp_y = world.pathfinder:isReachableFromHospital(humanoid.tile_x, humanoid.tile_y)
138 procrastination = WalkAction(hosp_x, hosp_y):setMustHappen(action.must_happen)
139 end
140 humanoid:queueAction(procrastination, 0)
141 end
142 end
143
144 return action_seek_reception_start