| 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 "PickupAction" (HumanoidAction) |
| 22 |
|
| 23 |
---@type PickupAction |
| 24 |
local PickupAction = _G["PickupAction"] |
| 25 |
|
| 26 |
-- Construct a pick-up action |
| 27 |
--!param ui User interface |
| 28 |
function PickupAction:PickupAction(ui) |
| 29 |
assert(class.is(ui, UI), "Invalid value for parameter 'ui'") |
| 30 |
|
| 31 |
self:HumanoidAction("pickup") |
| 32 |
self.ui = ui |
| 33 |
self.todo_close = nil |
| 34 |
self:setMustHappen(true) |
| 35 |
end |
| 36 |
|
| 37 |
function PickupAction:setTodoClose(dialog) |
| 38 |
assert(class.is(dialog, Window), "Invalid value for parameter 'dialog'") |
| 39 |
|
| 40 |
self.todo_close = dialog |
| 41 |
return self |
| 42 |
end |
| 43 |
|
| 44 |
local action_pickup_interrupt = permanent"action_pickup_interrupt"( function(action, humanoid) |
| 45 |
if action.window then |
| 46 |
action.window:close() |
| 47 |
end |
| 48 |
humanoid.th:makeVisible() |
| 49 |
local room = humanoid:getRoom() |
| 50 |
if room and room.crashed then |
| 51 |
action.ui:setDefaultCursor(nil) |
| 52 |
return |
| 53 |
elseif room then |
| 54 |
room:onHumanoidEnter(humanoid) |
| 55 |
else |
| 56 |
humanoid:onPlaceInCorridor() |
| 57 |
end |
| 58 |
humanoid:finishAction() |
| 59 |
action.ui:setDefaultCursor(nil) |
| 60 |
end) |
| 61 |
|
| 62 |
local action_pickup_dont_interrupt = permanent"action_pickup_dont_interrupt"( function(action) |
| 63 |
action.on_interrupt = action_pickup_interrupt |
| 64 |
end) |
| 65 |
|
| 66 |
local function action_pickup_start(action, humanoid) |
| 67 |
humanoid.dealing_with_patient = nil |
| 68 |
if action.todo_close then |
| 69 |
action.todo_close:close() |
| 70 |
end |
| 71 |
if class.is(humanoid, Staff) then |
| 72 |
humanoid:onPickup() |
| 73 |
end |
| 74 |
humanoid:setSpeed(0, 0) |
| 75 |
humanoid.th:makeInvisible() |
| 76 |
local room = humanoid:getRoom() |
| 77 |
if room then |
| 78 |
room:onHumanoidLeave(humanoid) |
| 79 |
end |
| 80 |
action.must_happen = true |
| 81 |
if action.todo_interrupt and action.todo_interrupt ~= "high" then |
| 82 |
-- If you pick up a staff member as they walk through a door, then the walk |
| 83 |
-- action will be given a high priority interrupt, and hence immediately |
| 84 |
-- dump the staff member in the room, at which point the room will command |
| 85 |
-- the entering staff, sending this pick up action a normal interrupt. We |
| 86 |
-- will completely ignore that, as the user's wish to pick up the staff is |
| 87 |
-- more important than the room's wish to command the staff. |
| 88 |
-- action_pickup_dont_interrupt will then set the interrupt handler back to |
| 89 |
-- normal, as that is called when the staff member is placed down again. |
| 90 |
action.on_interrupt = action_pickup_dont_interrupt |
| 91 |
else |
| 92 |
action.on_interrupt = action_pickup_interrupt |
| 93 |
end |
| 94 |
local ui = action.ui |
| 95 |
action.window = UIPlaceStaff(ui, humanoid, ui.cursor_x, ui.cursor_y) |
| 96 |
ui:addWindow(action.window) |
| 97 |
ui:playSound("pickup.wav") |
| 98 |
ui:setDefaultCursor(ui.grab_cursor) |
| 99 |
end |
| 100 |
|
| 101 |
return action_pickup_start |