| 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 |
--! Manages a list of `Humanoid`s which are waiting to use an `Object`. |
| 22 |
--! A queue stores a list of humanoids waiting to use an object. |
| 23 |
-- For a reception desk, this is as expected. |
| 24 |
-- For a room, the queue is for the door, not the room. Hence the queue stores |
| 25 |
-- the list of patients waiting to enter (the traditional queue for the room), |
| 26 |
-- the list of staff waiting to enter (because of the door being in use for |
| 27 |
-- example), the list of staff and patients waiting to leave (again due to door |
| 28 |
-- being in use). |
| 29 |
-- Queues are currently implemented as normal Lua arrays, but you should access |
| 30 |
-- a queue via its methods rather than directly. |
| 31 |
class "Queue" |
| 32 |
|
| 33 |
---@type Queue |
| 34 |
local Queue = _G["Queue"] |
| 35 |
|
| 36 |
--! Constructor of a queue. |
| 37 |
function Queue:Queue() |
| 38 |
-- Number of real patients that can be observed in the door queue interface. |
| 39 |
self.reported_size = 0 |
| 40 |
-- Expected humanoids |
| 41 |
self.expected = {} |
| 42 |
self.callbacks = {} |
| 43 |
-- Number of expected patients |
| 44 |
self.expected_count = 0 |
| 45 |
self.visitor_count = 0 |
| 46 |
-- Maximum queue length (default value) |
| 47 |
self.max_size = 6 |
| 48 |
self.bench_threshold = 0 |
| 49 |
end |
| 50 |
|
| 51 |
--! A humanoid is expected in a queue. |
| 52 |
--!param humanoid - any humanoid expected for the room |
| 53 |
--!param callback - register a callback for when queue is 'destroyed' |
| 54 |
function Queue:expect(humanoid, callback) |
| 55 |
if not self.expected[humanoid] then |
| 56 |
self.expected[humanoid] = callback |
| 57 |
-- only count patients in the expected count |
| 58 |
if class.is(humanoid, Patient) then |
| 59 |
self.expected_count = self.expected_count + 1 |
| 60 |
end |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
--! A humanoid is canceled as expected in a queue. |
| 65 |
--!param humanoid - Humanoid that is not coming to this queue. |
| 66 |
function Queue:unexpect(humanoid) |
| 67 |
if self.expected[humanoid] then |
| 68 |
self.expected[humanoid] = nil |
| 69 |
-- only count patients in the expected count |
| 70 |
if class.is(humanoid, Patient) then |
| 71 |
self.expected_count = self.expected_count - 1 |
| 72 |
end |
| 73 |
end |
| 74 |
end |
| 75 |
|
| 76 |
--! Lower the max queue length. |
| 77 |
--!param amount (int) Decrement length value of the queue. |
| 78 |
function Queue:decreaseMaxSize(amount) |
| 79 |
self.max_size = math.max(0, self.max_size - amount) |
| 80 |
end |
| 81 |
|
| 82 |
--! Increase max queue length. |
| 83 |
--!param amount (int) Increment length value of the queue. |
| 84 |
function Queue:increaseMaxSize(amount) |
| 85 |
self.max_size = math.min(30, self.max_size + amount) |
| 86 |
end |
| 87 |
|
| 88 |
function Queue:setBenchThreshold(standing_count) |
| 89 |
self.bench_threshold = standing_count |
| 90 |
end |
| 91 |
|
| 92 |
--! Set max queue length. |
| 93 |
--!param queue_count (int) New max queue length to set. |
| 94 |
function Queue:setMaxQueue(queue_count) |
| 95 |
self.max_size = queue_count |
| 96 |
end |
| 97 |
|
| 98 |
--! Total size of the queue, which are various people wanting in or out of the room. |
| 99 |
--! For a true patient queue count, use Queue:reportedSize. |
| 100 |
--!return (int) Number of various people in the queue. |
| 101 |
function Queue:size() |
| 102 |
-- Remember, the size includes people waiting to leave and staff waiting to enter |
| 103 |
-- For just the patients waiting to be served, use Queue:reportedSize() |
| 104 |
-- Most of the time, size() == reportedSize(), so it won't be immediately obvious |
| 105 |
-- if you're using the wrong method, but from time to time, staff or exiting |
| 106 |
-- patients will be in the queue, at which point the sizes will differ. |
| 107 |
return #self |
| 108 |
end |
| 109 |
|
| 110 |
--! Retrieve whether the queue is full. |
| 111 |
--!return (boolean) Whether the queue is full. |
| 112 |
function Queue:isFull() |
| 113 |
return self:size() >= self.max_size |
| 114 |
end |
| 115 |
|
| 116 |
--! Get the number of real patients in the queue. |
| 117 |
--!return (int) Number of real patients in the queue. |
| 118 |
function Queue:reportedSize() |
| 119 |
return self.reported_size |
| 120 |
end |
| 121 |
|
| 122 |
--! Get the number of expected patients. |
| 123 |
--!return (int) Number of expected patients (in the near future). |
| 124 |
function Queue:expectedSize() |
| 125 |
return self.expected_count |
| 126 |
end |
| 127 |
|
| 128 |
--! Check if the queue has an emergency patient. |
| 129 |
--!return (boolean) Whether an emergency patient was found in the queue. |
| 130 |
function Queue:hasEmergencyPatient() |
| 131 |
for _, humanoid in ipairs(self) do |
| 132 |
if humanoid.is_emergency then |
| 133 |
return true |
| 134 |
end |
| 135 |
end |
| 136 |
return false |
| 137 |
end |
| 138 |
|
| 139 |
--! Retrieve the total number of queued and expected patients. |
| 140 |
--return (int) Number of patients. |
| 141 |
function Queue:patientSize() |
| 142 |
return self.reported_size + self.expected_count |
| 143 |
end |
| 144 |
|
| 145 |
--! Get the 'index' real patient. |
| 146 |
--!param index (int) Index of the patient to retrieve (runs up to Queue:reportedSize). |
| 147 |
--!return Patient at the queried point in the queue. |
| 148 |
function Queue:reportedHumanoid(index) |
| 149 |
return self[self:size() - self.reported_size + index] |
| 150 |
end |
| 151 |
|
| 152 |
function Queue:setPriorityForSameRoom(entity) |
| 153 |
self.same_room_priority = entity |
| 154 |
end |
| 155 |
|
| 156 |
local function _isLeaving(queue, humanoid) |
| 157 |
return queue.same_room_priority and queue.same_room_priority:getRoom() == humanoid:getRoom() |
| 158 |
end |
| 159 |
|
| 160 |
--! Get queue priority for given humanoid |
| 161 |
--! Humanoids with priority lower that 'reported_priority_threshold' |
| 162 |
-- are not displayed in the door queue interface. |
| 163 |
--!param queue (object) target Queue. |
| 164 |
--!param humanoid (object) Humanoid for which we're asking the priority. |
| 165 |
--return (int) priority. Lower value means more significant priority. |
| 166 |
local function _getHumanoidQueuePriority(queue, humanoid) |
| 167 |
if _isLeaving(queue, humanoid) then |
| 168 |
-- If humanoid is leaving they have the highest priority |
| 169 |
-- To prevent cases like #873 |
| 170 |
return 1 |
| 171 |
elseif class.is(humanoid, Staff) then |
| 172 |
-- Next are staff members |
| 173 |
return 2 |
| 174 |
elseif class.is(humanoid, Vip) or class.is(humanoid, Inspector) then |
| 175 |
-- Next is a Vip and other hospital guests. |
| 176 |
-- Who are served instantly and do not delay others. |
| 177 |
return 3 |
| 178 |
elseif humanoid.is_emergency then |
| 179 |
-- Next are Emergency patients |
| 180 |
return 4 |
| 181 |
elseif humanoid.hospital.hosp_cheats:isCheatActive("queuejump") and humanoid:getAttribute("health") < 0.10 then |
| 182 |
-- If the queue jump cheat is enabled, let these nearly dead patients move ahead |
| 183 |
return 5 |
| 184 |
else |
| 185 |
-- All other regular patients receive this priority |
| 186 |
return 6 |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
--! Determines whether Humanoid priority is sufficient |
| 191 |
-- to display this Humanoid in door queue interface. |
| 192 |
--!param priority (int) humanoid priority. |
| 193 |
--return (bool) true if patient should be displayed. |
| 194 |
local function _shouldDisplayInDoorQueueInterface(priority) |
| 195 |
local reported_priority_threshold = 3 |
| 196 |
return priority > reported_priority_threshold |
| 197 |
end |
| 198 |
|
| 199 |
--! Insert the humanoid at the proper spot in the queue. |
| 200 |
--!param humanoid (object) Humanoid we want to put in the queue. |
| 201 |
function Queue:push(humanoid, callbacks_on) |
| 202 |
-- The queue is ordered on decreasing priority of the the humanoids for using the door. |
| 203 |
-- Higher priority is a smaller number. |
| 204 |
-- Starting from the back of the queue, and moving towards the front of the queue until |
| 205 |
-- the humanoid in front has an equal or higher priority (an equal of lower number). |
| 206 |
local index = self:size() + 1 |
| 207 |
local priority = _getHumanoidQueuePriority(self, humanoid) |
| 208 |
|
| 209 |
-- Calculate under what 'index' we should insert new humanoid into the queue |
| 210 |
while index > 1 do |
| 211 |
-- Abort the loop when the humanoid before it has equal or higher priority. |
| 212 |
if _getHumanoidQueuePriority(self, self[index - 1]) <= priority then |
| 213 |
break |
| 214 |
end |
| 215 |
index = index - 1 |
| 216 |
end |
| 217 |
|
| 218 |
if _shouldDisplayInDoorQueueInterface(priority) then |
| 219 |
self.reported_size = self.reported_size + 1 |
| 220 |
end |
| 221 |
|
| 222 |
self.callbacks[humanoid] = callbacks_on |
| 223 |
table.insert(self, index, humanoid) |
| 224 |
for i = index + 1, self:size() do |
| 225 |
local queued_humanoid = self[i] |
| 226 |
local callbacks = self.callbacks[queued_humanoid] |
| 227 |
if callbacks then |
| 228 |
callbacks:onChangeQueuePosition(queued_humanoid) |
| 229 |
end |
| 230 |
end |
| 231 |
end |
| 232 |
|
| 233 |
--! Get the first person in the queue (queue is not changed). |
| 234 |
--! Note that first person may not be a patient, use Queue:reportedHumanoid to get patients |
| 235 |
--!return First person in the queue. |
| 236 |
function Queue:front() |
| 237 |
return self[1] |
| 238 |
end |
| 239 |
|
| 240 |
--! Get the last person in the queue (queue is not changed). |
| 241 |
--!return Last person in the queue. |
| 242 |
function Queue:back() |
| 243 |
return self[self:size()] |
| 244 |
end |
| 245 |
|
| 246 |
--! Pop first person from the queue. |
| 247 |
--! Note that first person may not be a patient, use Queue:reportedHumanoid to get patients |
| 248 |
--!return First person in the queue. |
| 249 |
function Queue:pop() |
| 250 |
if self.reported_size == self:size() then |
| 251 |
self.reported_size = self.reported_size - 1 |
| 252 |
end |
| 253 |
local oldfront = self[1] |
| 254 |
table.remove(self, 1) |
| 255 |
oldfront:setMood("queue", "deactivate") |
| 256 |
local callbacks = self.callbacks[oldfront] |
| 257 |
if callbacks then |
| 258 |
callbacks:onLeaveQueue(oldfront) |
| 259 |
end |
| 260 |
self.callbacks[oldfront] = nil |
| 261 |
for _, humanoid in ipairs(self) do |
| 262 |
callbacks = self.callbacks[humanoid] |
| 263 |
if callbacks then |
| 264 |
callbacks:onChangeQueuePosition(humanoid) |
| 265 |
end |
| 266 |
end |
| 267 |
return oldfront |
| 268 |
end |
| 269 |
|
| 270 |
--! Remove person from the queue by index number. |
| 271 |
--! Note that the person may not be a patient. |
| 272 |
--!param index (jnt) Index in the queue of the person to remove. |
| 273 |
--!return The removed person. |
| 274 |
function Queue:remove(index) |
| 275 |
if self[index] == nil then |
| 276 |
return |
| 277 |
end |
| 278 |
local value = self[index] |
| 279 |
if index > self:size() - self.reported_size then |
| 280 |
self.reported_size = self.reported_size - 1 |
| 281 |
end |
| 282 |
value:setMood("queue", "deactivate") |
| 283 |
table.remove(self, index) |
| 284 |
self.callbacks[value] = nil |
| 285 |
for i = self:size(), index, -1 do |
| 286 |
local humanoid = self[i] |
| 287 |
if humanoid.onAdvanceQueue then |
| 288 |
humanoid:onAdvanceQueue(self, i - 1) |
| 289 |
end |
| 290 |
end |
| 291 |
return value |
| 292 |
end |
| 293 |
|
| 294 |
--! Remove a person by value from the queue. |
| 295 |
--!param value Person to remove. |
| 296 |
--!return Whether the person could be found (and was removed). |
| 297 |
function Queue:removeValue(value) |
| 298 |
for i = 1, self:size() do |
| 299 |
if self[i] == value then |
| 300 |
self:remove(i) |
| 301 |
return true |
| 302 |
end |
| 303 |
end |
| 304 |
return false |
| 305 |
end |
| 306 |
|
| 307 |
--! Move the person at position 'index' to position 'new_index'. |
| 308 |
--! Persons between 'index' and 'new_index' move one place to 'index'. |
| 309 |
--!param index (int) Index number of the person to move. |
| 310 |
--!param new_index (int) Destination of the person being moved. |
| 311 |
function Queue:move(index, new_index) |
| 312 |
if self[index] == nil or self[new_index] == nil or index == new_index then |
| 313 |
return |
| 314 |
end |
| 315 |
|
| 316 |
local i |
| 317 |
if new_index < index then |
| 318 |
i = -1 |
| 319 |
else |
| 320 |
i = 1 |
| 321 |
end |
| 322 |
|
| 323 |
while new_index ~= index do |
| 324 |
local temp = self[index + i] |
| 325 |
self[index + i] = self[index] |
| 326 |
self[index] = temp |
| 327 |
index = index + i |
| 328 |
end |
| 329 |
end |
| 330 |
|
| 331 |
--! Move an entering patient in the queue at position 'index' to position 'new_index'. |
| 332 |
--! Persons between 'index' and 'new_index' move one place to 'index'. |
| 333 |
--! Values are relative to the reported humanoids in the queue |
| 334 |
--!param index (int) Index number of the person to move. |
| 335 |
--!param new_index (int) Destination of the person being moved. |
| 336 |
--!param new_index (string) 'front' or 'back' as relative markers |
| 337 |
function Queue:movePatient(index, new_index) |
| 338 |
local first_patient_index = self:size() - self:reportedSize() + 1 |
| 339 |
if type(new_index) == "string" then |
| 340 |
if new_index == 'front' then |
| 341 |
new_index = first_patient_index |
| 342 |
else |
| 343 |
new_index = self:size() |
| 344 |
end |
| 345 |
else |
| 346 |
new_index = first_patient_index + new_index |
| 347 |
end |
| 348 |
self:move(first_patient_index + index - 1, new_index) |
| 349 |
end |
| 350 |
|
| 351 |
--! Called when reception desk is destroyed, or when a room is destroyed from a crashed machine. |
| 352 |
--!param room_id Id of the room to reroute to, 'nil' for reception. |
| 353 |
function Queue:rerouteAllPatients(room_id) |
| 354 |
for _, humanoid in ipairs(self) do |
| 355 |
-- check by class type as staff/vips shouldn't get a SeekRoomAction |
| 356 |
if class.is(humanoid, Patient) then |
| 357 |
-- slight delay so the desk is really destroyed before rerouting |
| 358 |
humanoid:setNextAction(IdleAction():setCount(1)) |
| 359 |
|
| 360 |
local action |
| 361 |
if room_id then |
| 362 |
action = SeekRoomAction(room_id) |
| 363 |
else |
| 364 |
action = SeekReceptionAction() |
| 365 |
end |
| 366 |
humanoid:queueAction(action) |
| 367 |
|
| 368 |
elseif class.is(humanoid, Staff) then |
| 369 |
-- likewise believe we need action here to stop |
| 370 |
humanoid:setNextAction(IdleAction():setCount(1)) |
| 371 |
humanoid:queueAction(MeanderAction()) |
| 372 |
else |
| 373 |
-- other humanoids don't enter rooms |
| 374 |
humanoid:setNextAction(MeanderAction()) |
| 375 |
end |
| 376 |
end |
| 377 |
for humanoid, callback in pairs(self.expected) do |
| 378 |
-- call the callback if registered as door is closing |
| 379 |
if callback then |
| 380 |
callback.callback() |
| 381 |
end |
| 382 |
self:unexpect(humanoid) |
| 383 |
end |
| 384 |
end |