Debian Codegraph @

CorsixTH/Lua/objects/door.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 = "door"
23 object.thob = 3
24 object.name = _S.object.door
25 object.tooltip = _S.tooltip.objects.door
26 object.class = "Door"
27 object.ticks = false
28 object.idle_animations = {
29 north = 104,
30 west = 106,
31 }
32
33 corsixth.require("queue")
34
35 class "Door" (Object)
36
37 ---@type Door
38 local Door = _G["Door"]
39
40 function Door:Door(...)
41 self:Object(...)
42 self.queue = Queue()
43 -- Priority should be given to humanoids already inside the door's room, as
44 -- otherwise we could end up in a situation where a patient trying to leave
45 -- a room is in the queue behind a patient trying to enter, and the entering
46 -- one cannot enter until the leaving one has left, etc.
47 self.queue:setPriorityForSameRoom(self)
48 self.hover_cursor = TheApp.gfx:loadMainCursor("queue")
49 -- self.user = "locked" -- prevents doors from being used (debug aid)
50 end
51
52 --! Updates room new door with values from previous door.
53 -- New door is typically result of room editing.
54 --!param room (Room) The room this door belongs to.
55 --!param old_door (Door) Previous door of this room.
56 function Door:setupDoor(room, old_door)
57 if old_door and old_door.queue then
58 -- for room edit case update new queue with values from old queue
59 self.queue.visitor_count = old_door.queue.visitor_count
60 self.queue.max_size = old_door.queue.max_size
61 end
62 self.room = room
63 end
64
65 function Door:getRoom()
66 return self.room
67 end
68
69 --! Updates a door's dynamic info
70 function Door:updateDynamicInfo()
71 if self.room and self.queue then
72 if not self.room:hasQueueDialog() then
73 self:setDynamicInfo('text', {
74 self.room.room_info.name
75 })
76 else
77 self:setDynamicInfo('text', {
78 self.room.room_info.name,
79 _S.dynamic_info.object.queue_size:format(self.queue:reportedSize()),
80 _S.dynamic_info.object.queue_expected:format(self.queue:expectedSize())
81 })
82 end
83 end
84 end
85
86 function Door:onClick(ui, button)
87 local room = self:getRoom()
88 if button == "left" and room:hasQueueDialog() and self.queue then
89 local queue_window = UIQueue(ui, self.queue)
90 ui:addWindow(queue_window)
91 end
92 end
93
94 local door_flag_name = {
95 north = {"doorNorth", "tallNorth"},
96 west = {"doorWest", "tallWest"},
97 }
98
99 function Door:setTile(x, y)
100 local map = self.world.map
101 local flag_names = door_flag_name[self.direction]
102 local todo_cleanup
103 if self.tile_x then
104 -- NB: unsetting buildable flag has to be done AFTER removing the object
105 -- from its former tile in Object.setTile
106 todo_cleanup = self:getWalkableTiles()
107 map:setCellFlags(self.tile_x, self.tile_y, {
108 [flag_names[1]] = false,
109 [flag_names[2]] = false,
110 })
111 for _, xy in ipairs(self:getWalkableTiles()) do
112 map:setCellFlags(xy[1], xy[2], {doNotIdle = false})
113 end
114 end
115 Object.setTile(self, x, y)
116 if todo_cleanup then
117 -- passable tiles can "belong" to multiple objects, so we have to check that
118 -- assumption: no object defines a passable tile further than 10 tiles away from its origin
119 for _, xy in ipairs(todo_cleanup) do
120 if not self.world:isTilePartOfNearbyObject(xy[1], xy[2], 10) then
121 map:setCellFlags(xy[1], xy[2], {buildable = true})
122 end
123 end
124 end
125 if x then
126 map:setCellFlags(x, y, {
127 [flag_names[1]] = true,
128 [flag_names[2]] = true,
129 })
130 for _, xy in ipairs(self:getWalkableTiles()) do
131 map:setCellFlags(xy[1], xy[2], {buildable = false, doNotIdle = true})
132 end
133 end
134 map.th:updateShadows()
135 return self
136 end
137
138 function Door:getWalkableTiles()
139 local x, y = self.tile_x, self.tile_y
140 if self.direction == "west" then
141 x = x - 1
142 else
143 y = y - 1
144 end
145 return { {self.tile_x, self.tile_y}, {x, y} }
146 end
147
148 local flag_early_list = 1024
149
150 function Door:setAnimation(animation, flags)
151 flags = (flags or 0)
152 if self.direction == "north" then
153 flags = flags + flag_early_list
154 end
155 return Object.setAnimation(self, animation, flags)
156 end
157
158 function Door:closeDoor()
159 if self.queue then
160 self.queue:rerouteAllPatients(self:getRoom().room_info.id)
161 self.queue = nil
162 end
163 self:clearDynamicInfo(nil)
164 self.hover_cursor = nil
165 end
166
167 function Door:getDrawingLayer()
168 return DrawingLayers.Door
169 end
170
171 function Door:checkForDeadlock()
172 -- In an ideal world, deadlocks should not occur, as they indicate errors in
173 -- some logic elsewhere. From a practical point of view, we should check for
174 -- deadlocks from time to time and attempt to fix them.
175 if self.queue and self.reserved_for then
176 -- If the door is reserved for someone, then that person should either be
177 -- at the front of the queue, or not in any queues at all.
178 for _, action in ipairs(self.reserved_for.action_queue) do
179 if action.name == "queue" then
180 if action.queue ~= self.queue or self.queue[1] ~= self.reserved_for then
181 self.world:gameLog("Warning: Trying to resolve door deadlock at ("
182 .. tostring(self.tile_x) .. ", " .. tostring(self.tile_y) .. ")")
183 self.reserved_for = nil
184 self:getRoom():tryAdvanceQueue()
185 end
186 break
187 end
188 end
189 end
190 end
191
192 function Door:afterLoad(old, new)
193 if old < 57 then
194 local flags_to_set
195 local map = self.world.map
196 if self.direction == "west" then
197 flags_to_set = {buildableNorth = false, buildableSouth = false}
198 map:setCellFlags(self.tile_x, self.tile_y, flags_to_set)
199 map:setCellFlags(self.tile_x - 1, self.tile_y, flags_to_set)
200 else
201 flags_to_set = {buildableEast = false, buildableWest = false}
202 map:setCellFlags(self.tile_x, self.tile_y, flags_to_set)
203 map:setCellFlags(self.tile_x, self.tile_y - 1, flags_to_set)
204 end
205 end
206 self:updateDynamicInfo()
207 Object.afterLoad(self, old, new)
208 end
209 return object