| 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 math_floor |
| 22 |
= math.floor |
| 23 |
local TH = require("TH") |
| 24 |
|
| 25 |
--! Invisible window which handles placing a `Staff` member in the world. |
| 26 |
class "UIPlaceStaff" (Window) |
| 27 |
|
| 28 |
---@type UIPlaceStaff |
| 29 |
local UIPlaceStaff = _G["UIPlaceStaff"] |
| 30 |
|
| 31 |
function UIPlaceStaff:UIPlaceStaff(ui, profile, x, y) |
| 32 |
self.ui = ui |
| 33 |
self.world = ui.app.world |
| 34 |
self.modal_class = "main" |
| 35 |
self.esc_closes = true |
| 36 |
if class.is(profile, Staff) then |
| 37 |
self.staff = profile |
| 38 |
profile = profile.profile |
| 39 |
end |
| 40 |
self.profile = profile |
| 41 |
-- The receptionist has no door animations, and hence would not |
| 42 |
-- be able to leave a room if placed in one. |
| 43 |
self.allow_in_rooms = not profile:isType("Receptionist") |
| 44 |
self.anim = TH.animation() |
| 45 |
self.anim:setLayer(5, profile.layer5) |
| 46 |
local idle_anim = Humanoid.getIdleAnimation(profile.humanoid_class) |
| 47 |
self.anim:setAnimation(self.world.anims, idle_anim) |
| 48 |
local _, ghost = ui.app.gfx:getPalette("MPalette.dat") |
| 49 |
local grey_scale = self.world.anims.Alt32_GreyScale |
| 50 |
self.world.anims:setAnimationGhostPalette(idle_anim, ghost, grey_scale) |
| 51 |
self:onCursorWorldPositionChange(x, y) |
| 52 |
self:Window() |
| 53 |
end |
| 54 |
|
| 55 |
function UIPlaceStaff:close() |
| 56 |
local employed_staff = self.staff |
| 57 |
local play_placement_sound = true |
| 58 |
if employed_staff then |
| 59 |
employed_staff.pickup = false |
| 60 |
employed_staff.staffroom_needed = nil |
| 61 |
employed_staff.going_to_staffroom = nil |
| 62 |
employed_staff:getCurrentAction().window = nil |
| 63 |
local room = self.world:getRoom(employed_staff.tile_x, employed_staff.tile_y) |
| 64 |
if room and room == employed_staff.last_room and room.crashed then |
| 65 |
employed_staff:die() |
| 66 |
employed_staff:despawn() |
| 67 |
self.world:destroyEntity(employed_staff) |
| 68 |
else |
| 69 |
employed_staff:setNextAction(MeanderAction()) |
| 70 |
end |
| 71 |
elseif self.profile then -- Cancel hiring newcomer |
| 72 |
self.ui:tutorialStep(2, {6, 7}, 1) |
| 73 |
self.ui:tutorialStep(4, {4, 5}, 1) |
| 74 |
-- Return the profile to the available staff list |
| 75 |
local staff_pool = self.world.available_staff[self.profile.humanoid_class] |
| 76 |
staff_pool[#staff_pool + 1] = self.profile |
| 77 |
play_placement_sound = false |
| 78 |
end |
| 79 |
if play_placement_sound then |
| 80 |
self.ui:playSound("plac_st2.wav") |
| 81 |
end |
| 82 |
Window.close(self) |
| 83 |
end |
| 84 |
|
| 85 |
function UIPlaceStaff:onCursorWorldPositionChange(x, y) |
| 86 |
x, y = self.ui:ScreenToWorld(x, y + 14) |
| 87 |
self.tile_x = math_floor(x) |
| 88 |
self.tile_y = math_floor(y) |
| 89 |
end |
| 90 |
|
| 91 |
local flag_cache = {} |
| 92 |
local flag_altpal = 16 |
| 93 |
--! Private function. Is the staff member (self) permitted |
| 94 |
-- to be placed where the user is hovering or clicking? |
| 95 |
function UIPlaceStaff:_isValidStaffPlacement() |
| 96 |
local world, x, y = self.world, self.tile_x, self.tile_y |
| 97 |
world.map.th:getCellFlags(x, y, flag_cache) |
| 98 |
-- Is the tile inside the player's hospital building? |
| 99 |
if not (self.ui.hospital:getPlayerIndex() == flag_cache.owner and |
| 100 |
flag_cache.hospital) then |
| 101 |
return false |
| 102 |
end |
| 103 |
local room = world:getRoom(x, y) |
| 104 |
-- On a tile humanoids can walk on? |
| 105 |
local walkable = flag_cache.passable |
| 106 |
if room then |
| 107 |
if room.door and room.door.tile_x and room.door.tile_y then |
| 108 |
-- Check that the door is accessible from this tile. Otherwise, forbid placement. |
| 109 |
local reachable = world.pathfinder:findDistance(x, y, room.door.tile_x, room.door.tile_y) |
| 110 |
walkable = walkable and not room.crashed and reachable |
| 111 |
else |
| 112 |
walkable = false |
| 113 |
end |
| 114 |
end |
| 115 |
-- and in an area the regular staff (doctors, nurses and handymen) can go? |
| 116 |
local staffable = (self.allow_in_rooms or flag_cache.roomId == 0) |
| 117 |
-- Or is it a receptionist placed on an unstaffed reception desk? |
| 118 |
local reception = false |
| 119 |
if self.profile:isType("Receptionist") then |
| 120 |
local desk = world:getObject(x, y, "reception_desk", true) or |
| 121 |
world:findObjectNear(self, "reception_desk", 0, true) |
| 122 |
reception = desk and not desk.receptionist |
| 123 |
end |
| 124 |
return (walkable and staffable) or reception |
| 125 |
end |
| 126 |
|
| 127 |
function UIPlaceStaff:draw(canvas) |
| 128 |
if self.world.user_actions_allowed then |
| 129 |
local valid = self:_isValidStaffPlacement() |
| 130 |
self.anim:setFlag(valid and 0 or flag_altpal) |
| 131 |
local zoom = self.ui.zoom_factor |
| 132 |
if canvas:scale(zoom) then |
| 133 |
local x, y = self.ui:WorldToScreen(self.tile_x, self.tile_y) |
| 134 |
self.anim:draw(canvas, math.floor(x / zoom), math.floor(y / zoom)) |
| 135 |
canvas:scale(1) |
| 136 |
else |
| 137 |
self.anim:draw(canvas, self.ui:WorldToScreen(self.tile_x, self.tile_y)) |
| 138 |
end |
| 139 |
self.ui:tutorialStep(2, valid and 7 or 6, valid and 6 or 7) |
| 140 |
self.ui:tutorialStep(4, valid and 5 or 4, valid and 4 or 5) |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
function UIPlaceStaff:onMouseUp(button, x, y) |
| 145 |
if self.world.user_actions_allowed then |
| 146 |
if button == "right" then |
| 147 |
self:close() |
| 148 |
return true |
| 149 |
elseif button == "left" then |
| 150 |
self:onMouseMove(x, y) |
| 151 |
if self:_isValidStaffPlacement() then |
| 152 |
if self.staff then |
| 153 |
self.staff:setTile(self.tile_x, self.tile_y) |
| 154 |
else |
| 155 |
local entity = self.world:newEntity(self.profile.humanoid_class, 2, 2) |
| 156 |
entity:setProfile(self.profile) |
| 157 |
self.profile = nil |
| 158 |
entity:setTile(self.tile_x, self.tile_y) |
| 159 |
self.ui.hospital:addStaff(entity) |
| 160 |
entity:setHospital(self.ui.hospital) |
| 161 |
local entity_room = entity:getRoom() |
| 162 |
if entity_room then |
| 163 |
entity_room:onHumanoidEnter(entity) |
| 164 |
else |
| 165 |
entity:onPlaceInCorridor() |
| 166 |
end |
| 167 |
self.ui:tutorialStep(2, 6, "next") |
| 168 |
self.ui:tutorialStep(4, 4, "next") |
| 169 |
-- Update the staff management window if it is open. |
| 170 |
local window = self.world.ui:getWindow(UIStaffManagement) |
| 171 |
if window then |
| 172 |
window:updateStaffList(self) |
| 173 |
end |
| 174 |
end |
| 175 |
self:close() |
| 176 |
return true |
| 177 |
else |
| 178 |
self.ui.adviser:say(_A.placement_info.staff_cannot_place) |
| 179 |
return true |
| 180 |
end |
| 181 |
end |
| 182 |
end |
| 183 |
end |