| 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 |
corsixth.require("dialogs.place_objects") |
| 22 |
|
| 23 |
class "UIEditRoom" (UIPlaceObjects) |
| 24 |
|
| 25 |
---@type UIEditRoom |
| 26 |
local UIEditRoom = _G["UIEditRoom"] |
| 27 |
|
| 28 |
function UIEditRoom:UIEditRoom(ui, room_type) |
| 29 |
|
| 30 |
-- NB: UIEditRoom:onCursorWorldPositionChange is called by the UIPlaceObjects |
| 31 |
-- constructor, hence the initialisation of required fields prior to the call. |
| 32 |
self.UIPlaceObjects(self, ui) |
| 33 |
|
| 34 |
local app = ui.app |
| 35 |
local blue_red_swap = self.anims.Alt32_BlueRedSwap |
| 36 |
-- Set alt palette on wall blueprint to make it red |
| 37 |
self.anims:setAnimationGhostPalette(124, app.gfx:loadGhost("QData", "Ghost1.dat", 6), blue_red_swap) |
| 38 |
-- Set on door and window blueprints too |
| 39 |
self.anims:setAnimationGhostPalette(126, app.gfx:loadGhost("QData", "Ghost1.dat", 6), blue_red_swap) |
| 40 |
self.anims:setAnimationGhostPalette(130, app.gfx:loadGhost("QData", "Ghost1.dat", 6), blue_red_swap) |
| 41 |
self.cell_outline = TheApp.gfx:loadSpriteTable("Bitmap", "aux_ui", true) |
| 42 |
if not room_type.room_info then |
| 43 |
self.blueprint_rect = { |
| 44 |
x = 1, |
| 45 |
y = 1, |
| 46 |
w = 0, |
| 47 |
h = 0, |
| 48 |
} |
| 49 |
if room_type.swing_doors then |
| 50 |
self.blueprint_door = {anim = {}, old_anim = {}, old_flags = {}} |
| 51 |
else |
| 52 |
self.blueprint_door = {} |
| 53 |
end |
| 54 |
self.phase = "walls" --> "door" --> "windows" --> "clear_area" --> "objects" --> "closed" |
| 55 |
self.room_type = room_type |
| 56 |
self.title_text = room_type.name |
| 57 |
self.desc_text = _S.place_objects_window.drag_blueprint |
| 58 |
else |
| 59 |
self.phase = "objects" |
| 60 |
self.room_type = room_type.room_info |
| 61 |
self.title_text = room_type.room_info.name |
| 62 |
self.room = room_type |
| 63 |
self.desc_text = _S.place_objects_window.confirm_or_buy_objects |
| 64 |
self.paid = true |
| 65 |
self.blueprint_rect = { |
| 66 |
x = room_type.x, |
| 67 |
y = room_type.y, |
| 68 |
w = room_type.width, |
| 69 |
h = room_type.height, |
| 70 |
} |
| 71 |
if room_type.room_info.swing_doors then |
| 72 |
self.blueprint_door = {anim = {}, old_anim = {}, old_flags = {}} |
| 73 |
else |
| 74 |
self.blueprint_door = {} |
| 75 |
end |
| 76 |
self.ui:setWorldHitTest(self.room) |
| 77 |
self.pickup_button:enable(true) |
| 78 |
self.purchase_button:enable(true) |
| 79 |
self:checkEnableConfirm() |
| 80 |
end |
| 81 |
|
| 82 |
self.blueprint_wall_anims = {} |
| 83 |
self.blueprint_window = {} |
| 84 |
|
| 85 |
self.mouse_down_x = false |
| 86 |
self.mouse_down_y = false |
| 87 |
self.mouse_cell_x = 0 |
| 88 |
self.mouse_cell_y = 0 |
| 89 |
|
| 90 |
self:registerKeyHandlers() |
| 91 |
end |
| 92 |
|
| 93 |
function UIEditRoom:registerKeyHandlers() |
| 94 |
self:addKeyHandler("global_confirm", self.confirm) -- UIPlaceObjects does not need this |
| 95 |
self:addKeyHandler("global_confirm_alt", self.confirm) |
| 96 |
end |
| 97 |
|
| 98 |
function UIEditRoom:close(...) |
| 99 |
if self.phase == "objects" and self.confirm_button.enabled then |
| 100 |
if not self.closed_cleanly then |
| 101 |
self:confirm(true) |
| 102 |
end |
| 103 |
else |
| 104 |
while self.phase ~= "walls" do |
| 105 |
self:cancel() |
| 106 |
end |
| 107 |
end |
| 108 |
for k, obj in pairs(self.blueprint_wall_anims) do |
| 109 |
if obj.setTile then |
| 110 |
obj:setTile(nil) |
| 111 |
else |
| 112 |
for _, anim in pairs(obj) do |
| 113 |
anim:setTile(nil) |
| 114 |
end |
| 115 |
end |
| 116 |
self.blueprint_wall_anims[k] = nil |
| 117 |
end |
| 118 |
self.phase = "closed" |
| 119 |
-- No longer editing a room |
| 120 |
self.ui.edit_room = false |
| 121 |
self:setBlueprintRect(1, 1, 0, 0) |
| 122 |
self.ui:tutorialStep(3, {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 1) -- not all of these links may be needed, but to be safe... |
| 123 |
return UIPlaceObjects.close(self, ...) |
| 124 |
end |
| 125 |
|
| 126 |
--[[ Called when building/editing of the room is about to stop because another |
| 127 |
dialog is being opened. If the room is in the objects phase and all |
| 128 |
required objects have been placed it will be completed. Otherwise |
| 129 |
it is cancelled instead. |
| 130 |
--]] |
| 131 |
function UIEditRoom:verifyOrAbortRoom() |
| 132 |
if self.phase == "objects" and self.confirm_button.enabled then |
| 133 |
-- The room can be finished |
| 134 |
self:close() |
| 135 |
else |
| 136 |
-- The room will have to be cancelled |
| 137 |
self:abortRoom() |
| 138 |
end |
| 139 |
end |
| 140 |
|
| 141 |
function UIEditRoom:abortRoom() |
| 142 |
if self.paid then |
| 143 |
-- Return half the cost. |
| 144 |
local progress = self.ui.hospital.research.research_progress |
| 145 |
local cost = math.floor(progress[self.room.room_info].build_cost/2) |
| 146 |
-- TODO: Return also the cost for additional objects. |
| 147 |
|
| 148 |
-- Decrease the hospital value by the whole room build cost |
| 149 |
local valueChange = progress[self.room.room_info].build_cost |
| 150 |
for obj, num in pairs(self.room.room_info.objects_needed) do |
| 151 |
-- Get how much this item costs. |
| 152 |
local obj_cost = self.ui.hospital:getObjectBuildCost(obj) |
| 153 |
cost = cost - math.floor(obj_cost)/2 |
| 154 |
valueChange = valueChange - num * obj_cost |
| 155 |
end |
| 156 |
self.ui.hospital:receiveMoney(cost, _S.transactions.sell_object, valueChange) |
| 157 |
end |
| 158 |
-- Close the dialog |
| 159 |
self:close() |
| 160 |
-- Finally remove the room from the world (close() needs the reference) |
| 161 |
if self.room then |
| 162 |
self.world.rooms[self.room.id] = nil |
| 163 |
end |
| 164 |
end |
| 165 |
|
| 166 |
function UIEditRoom:cancel() |
| 167 |
if self.confirm_dialog_open then |
| 168 |
-- Don't do anything as long as the confirm dialog is open. |
| 169 |
return |
| 170 |
end |
| 171 |
if self.phase == "walls" then |
| 172 |
if self.paid then |
| 173 |
-- While the confirmation window is open, don't allow the player to click the confirm button. |
| 174 |
local confirm_button_state = self.confirm_button.enabled |
| 175 |
self.confirm_button:enable(false) |
| 176 |
self.confirm_dialog_open = true |
| 177 |
-- Ask if the user really wish to sell this room |
| 178 |
self.ui:addWindow(UIConfirmDialog(self.ui, false, |
| 179 |
_S.confirmation.delete_room, |
| 180 |
--[[persistable:delete_room_confirm_dialog]]function() |
| 181 |
self:abortRoom() |
| 182 |
end, |
| 183 |
--[[persistable:delete_room_confirm_dialog_cancel]]function() |
| 184 |
self.confirm_button:enable(confirm_button_state) |
| 185 |
self.confirm_dialog_open = nil |
| 186 |
end |
| 187 |
)) |
| 188 |
else |
| 189 |
self:abortRoom() |
| 190 |
end |
| 191 |
self.ui:setCursor(self.ui.default_cursor) |
| 192 |
elseif self.phase == "objects" then |
| 193 |
self:stopPickupItems() |
| 194 |
self:returnToDoorPhase() |
| 195 |
self.world:resetSideObjects() |
| 196 |
else |
| 197 |
if self.phase == "clear_area" then |
| 198 |
self.ui:setDefaultCursor(nil) |
| 199 |
self.check_for_clear_area_timer = nil |
| 200 |
self.humanoids_to_watch = nil |
| 201 |
end |
| 202 |
self.phase = "walls" |
| 203 |
self:returnToWallPhase() |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
function UIEditRoom:confirm(force) |
| 208 |
-- double check if confirm is allowed (for being called e.g. by hotkey) |
| 209 |
if not force and not self.confirm_button.enabled then |
| 210 |
return |
| 211 |
end |
| 212 |
self:stopPickupItems() |
| 213 |
|
| 214 |
if self.phase == "walls" then |
| 215 |
self.mouse_down_x = false |
| 216 |
self.mouse_down_y = false |
| 217 |
self.move_rect_x = false |
| 218 |
self.move_rect_y = false |
| 219 |
self.phase = "door" |
| 220 |
self:enterDoorPhase() |
| 221 |
elseif self.phase == "door" then |
| 222 |
self.phase = "windows" |
| 223 |
self:enterWindowsPhase() |
| 224 |
elseif self.phase == "windows" then |
| 225 |
self.phase = "clear_area" |
| 226 |
self:clearArea() |
| 227 |
elseif self.phase == "clear_area" then |
| 228 |
self.ui:setDefaultCursor(nil) |
| 229 |
self.phase = "objects" |
| 230 |
self:finishRoom() |
| 231 |
self.world:resetSideObjects() |
| 232 |
self:enterObjectsPhase() |
| 233 |
else |
| 234 |
-- Pay for room (subtract cost of needed objects, which were already paid for) |
| 235 |
if not self.paid then |
| 236 |
local progress = self.ui.hospital.research.research_progress |
| 237 |
local cost = progress[self.room.room_info].build_cost |
| 238 |
for obj, num in pairs(self.room.room_info.objects_needed) do |
| 239 |
-- Get how much this item costs. |
| 240 |
local obj_cost = self.ui.hospital:getObjectBuildCost(obj) |
| 241 |
cost = cost - num * obj_cost |
| 242 |
end |
| 243 |
self.ui.hospital:spendMoney(cost, _S.transactions.build_room .. ": " .. self.title_text, cost) |
| 244 |
self.paid = true |
| 245 |
end |
| 246 |
|
| 247 |
self.world:markRoomAsBuilt(self.room) |
| 248 |
self.closed_cleanly = true |
| 249 |
-- If information dialogs are disabled, go ahead. |
| 250 |
if self.ui:getWindow(UIInformation) then |
| 251 |
self.ui:tutorialStep(3, 15, 16) |
| 252 |
else |
| 253 |
self.ui:tutorialStep(3, 15, "next") |
| 254 |
end |
| 255 |
self:close() |
| 256 |
end |
| 257 |
end |
| 258 |
|
| 259 |
function UIEditRoom:clearArea() |
| 260 |
self.confirm_button:enable(false) |
| 261 |
local rect = self.blueprint_rect |
| 262 |
local world = self.ui.app.world |
| 263 |
world:clearCaches() -- To invalidate idle tiles in case we need to move people |
| 264 |
local humanoids_to_watch = {} |
| 265 |
do |
| 266 |
local x1 = rect.x - 1 |
| 267 |
local x2 = rect.x + rect.w |
| 268 |
local y1 = rect.y - 1 |
| 269 |
local y2 = rect.y + rect.h |
| 270 |
for _, entity in ipairs(world.entities) do |
| 271 |
if class.is(entity, Humanoid) and |
| 272 |
entity:isObscuringArea(x1, x2, y1, y2) then |
| 273 |
humanoids_to_watch[entity] = true |
| 274 |
|
| 275 |
-- Try to make the humanoid leave the area |
| 276 |
local current_action = entity:getCurrentAction() |
| 277 |
local meander = entity.action_queue[2] |
| 278 |
if meander and meander.name == "meander" then |
| 279 |
-- Interrupt the idle or walk, which will cause a new meander target |
| 280 |
-- to be chosen, which will be outside the blueprint rectangle |
| 281 |
meander.can_idle = false |
| 282 |
local on_interrupt = current_action.on_interrupt |
| 283 |
if on_interrupt then |
| 284 |
current_action.on_interrupt = nil |
| 285 |
on_interrupt(current_action, entity) |
| 286 |
end |
| 287 |
elseif current_action.name == "seek_room" or (meander and meander.name == "seek_room") then |
| 288 |
-- Make sure that the humanoid doesn't stand idle waiting within the blueprint |
| 289 |
if current_action.name == "seek_room" then |
| 290 |
entity:queueAction(MeanderAction():setCount(1):setMustHappen(true), 0) |
| 291 |
else |
| 292 |
meander.done_walk = false |
| 293 |
end |
| 294 |
else |
| 295 |
-- Look for a queue action and re-arrange the people in it, which |
| 296 |
-- should cause anyone queueing within the blueprint to move |
| 297 |
for _, action in ipairs(entity.action_queue) do |
| 298 |
if action.name == "queue" then |
| 299 |
for _, humanoid in ipairs(action.queue) do |
| 300 |
local callbacks = action.queue.callbacks[humanoid] |
| 301 |
if callbacks then |
| 302 |
callbacks:onChangeQueuePosition(humanoid) |
| 303 |
end |
| 304 |
end |
| 305 |
break |
| 306 |
end |
| 307 |
end |
| 308 |
-- TODO: Consider any other actions which might be causing the |
| 309 |
-- humanoid to be staying within the rectangle for a long time. |
| 310 |
end |
| 311 |
end |
| 312 |
end |
| 313 |
end |
| 314 |
|
| 315 |
if next(humanoids_to_watch) == nil then |
| 316 |
-- No humanoids within the area, so continue with the room placement |
| 317 |
self:confirm(true) |
| 318 |
return |
| 319 |
end |
| 320 |
|
| 321 |
self.check_for_clear_area_timer = 10 |
| 322 |
self.humanoids_to_watch = humanoids_to_watch |
| 323 |
self.ui:setCursor(self.ui.waiting_cursor) |
| 324 |
end |
| 325 |
|
| 326 |
function UIEditRoom:onTick() |
| 327 |
UIFurnishCorridor.onTick(self) |
| 328 |
if self.check_for_clear_area_timer then |
| 329 |
self.check_for_clear_area_timer = self.check_for_clear_area_timer - 1 |
| 330 |
if self.check_for_clear_area_timer == 0 then |
| 331 |
local rect = self.blueprint_rect |
| 332 |
local x1 = rect.x - 1 |
| 333 |
local x2 = rect.x + rect.w |
| 334 |
local y1 = rect.y - 1 |
| 335 |
local y2 = rect.y + rect.h |
| 336 |
for humanoid in pairs(self.humanoids_to_watch) do |
| 337 |
-- The person might be dying (this check should probably be moved into |
| 338 |
-- isHumanoidObscuringArea, but I don't want to change too much right |
| 339 |
-- before a release). |
| 340 |
if humanoid:getCurrentAction().name == "die" then |
| 341 |
if not humanoid.hospital then |
| 342 |
self.humanoids_to_watch[humanoid] = nil |
| 343 |
end |
| 344 |
elseif not humanoid:isObscuringArea(x1, x2, y1, y2) then |
| 345 |
self.humanoids_to_watch[humanoid] = nil |
| 346 |
end |
| 347 |
end |
| 348 |
if next(self.humanoids_to_watch) then |
| 349 |
self.check_for_clear_area_timer = 10 |
| 350 |
else |
| 351 |
self.check_for_clear_area_timer = nil |
| 352 |
self.humanoids_to_watch = nil |
| 353 |
self:confirm(true) |
| 354 |
end |
| 355 |
end |
| 356 |
end |
| 357 |
end |
| 358 |
|
| 359 |
function UIEditRoom:finishRoom() |
| 360 |
local room_type = self.room_type |
| 361 |
local wall_type = self.ui.app.walls[room_type.wall_type] |
| 362 |
local world = self.ui.app.world |
| 363 |
local map = self.ui.app.map.th |
| 364 |
local rect = self.blueprint_rect |
| 365 |
local door, door2 |
| 366 |
-- Add the transparency flag if it is set. |
| 367 |
local flag = 0 |
| 368 |
if self.ui.transparent_walls then |
| 369 |
flag = 1024 |
| 370 |
end |
| 371 |
local function check_external_window(x, y, layer) |
| 372 |
-- Test it is a wall, but not spr_num. |
| 373 |
local function is_wall_and_not(wall_x, wall_y, spr_num) |
| 374 |
return map:getCell(wall_x, wall_y, layer) ~= 0 |
| 375 |
and map:getCell(wall_x, wall_y, layer) ~= spr_num |
| 376 |
end |
| 377 |
-- Test that the wall is either spr_num1 or spr_num2. |
| 378 |
local function is_one_of_wall(wall_x, wall_y, spr_num1, spr_num2) |
| 379 |
return map:getCell(wall_x, wall_y, layer) == spr_num1 |
| 380 |
or map:getCell(wall_x, wall_y, layer) == spr_num2 |
| 381 |
end |
| 382 |
|
| 383 |
|
| 384 |
-- If a wall is built which is normal to an external window, then said |
| 385 |
-- window needs to be removed, otherwise it looks odd. |
| 386 |
local block = map:getCell(x, y, layer) |
| 387 |
local dir = world:getWallDirFromBlockId(block) |
| 388 |
local tiles = self.ui.app.walls.external[world:getWallSetFromBlockId(block)] |
| 389 |
|
| 390 |
-- Dispatch on the wall tiles at (x, y) |
| 391 |
if dir == "north_window_1" then |
| 392 |
if x ~= rect.x then |
| 393 |
map:setCell(x, y, layer, flag + tiles.north) |
| 394 |
if map:getCell(x + 1, y, layer) ~= 0 then |
| 395 |
map:setCell(x + 1, y, layer, flag + tiles.north) |
| 396 |
end |
| 397 |
end |
| 398 |
|
| 399 |
elseif dir == "north_window_2" then |
| 400 |
if x == rect.x then |
| 401 |
if map:getCell(x - 1, y, layer) ~= 0 then |
| 402 |
map:setCell(x - 1, y, layer, flag + tiles.north) |
| 403 |
end |
| 404 |
map:setCell(x, y, layer, flag + tiles.north) |
| 405 |
end |
| 406 |
|
| 407 |
elseif dir == "west_window_1" then |
| 408 |
if y == rect.y then |
| 409 |
map:setCell(x, y, layer, flag + tiles.west) |
| 410 |
if map:getCell(x, y - 1, layer) ~= 0 then |
| 411 |
map:setCell(x, y - 1, layer, flag + tiles.west) |
| 412 |
end |
| 413 |
end |
| 414 |
|
| 415 |
elseif dir == "west_window_2" then |
| 416 |
if y ~= rect.y then |
| 417 |
if map:getCell(x, y + 1, layer) ~= 0 then |
| 418 |
map:setCell(x, y + 1, layer, flag + tiles.west) |
| 419 |
end |
| 420 |
map:setCell(x, y, layer, flag + tiles.west) |
| 421 |
end |
| 422 |
|
| 423 |
elseif dir == "north_window_long" then |
| 424 |
if x == rect.x then |
| 425 |
map:setCell(x, y, layer, flag + tiles.north_window_1) |
| 426 |
-- Check if the window_long continues. |
| 427 |
-- If yes, replace the window_long with window_n. |
| 428 |
-- if no, replace it with the wall. |
| 429 |
if is_wall_and_not(x - 1, y, tiles.north_window_2) then |
| 430 |
if is_one_of_wall(x - 2, y, |
| 431 |
tiles.north_window_long, tiles.north_window_1) then |
| 432 |
map:setCell(x - 1, y, layer, flag + tiles.north_window_2) |
| 433 |
else |
| 434 |
map:setCell(x - 1, y, layer, flag + tiles.north) |
| 435 |
end |
| 436 |
end |
| 437 |
else |
| 438 |
map:setCell(x, y, layer, flag + tiles.north_window_2) |
| 439 |
if is_wall_and_not(x + 1, y, tiles.north_window_1) then |
| 440 |
if is_one_of_wall(x + 2, y, |
| 441 |
tiles.north_window_long, tiles.north_window_2) then |
| 442 |
map:setCell(x + 1, y, layer, flag + tiles.north_window_1) |
| 443 |
else |
| 444 |
map:setCell(x + 1, y, layer, flag + tiles.north) |
| 445 |
end |
| 446 |
end |
| 447 |
end |
| 448 |
|
| 449 |
elseif dir == "west_window_long" then |
| 450 |
if y == rect.y then |
| 451 |
map:setCell(x, y, layer, flag + tiles.west_window_2) |
| 452 |
if is_wall_and_not(x, y - 1, tiles.west_window_1) then |
| 453 |
if is_one_of_wall(x, y - 2, |
| 454 |
tiles.west_window_long, tiles.west_window_2) then |
| 455 |
map:setCell(x, y - 1, layer, flag + tiles.west_window_1) |
| 456 |
else |
| 457 |
map:setCell(x, y - 1, layer, flag + tiles.west) |
| 458 |
end |
| 459 |
end |
| 460 |
else |
| 461 |
map:setCell(x, y, layer, flag + tiles.west_window_1) |
| 462 |
if is_wall_and_not(x, y + 1, tiles.west_window_2) then |
| 463 |
if is_one_of_wall(x, y + 2, |
| 464 |
tiles.west_window_long, tiles.west_window_1) then |
| 465 |
map:setCell(x, y + 1, layer, flag + tiles.west_window_2) |
| 466 |
else |
| 467 |
map:setCell(x, y + 1, layer, flag + tiles.west) |
| 468 |
end |
| 469 |
end |
| 470 |
end |
| 471 |
end |
| 472 |
end |
| 473 |
|
| 474 |
for x, obj in pairs(self.blueprint_wall_anims) do |
| 475 |
for y, anim in pairs(obj) do |
| 476 |
if x == rect.x and y == rect.y then |
| 477 |
local _, east, north = map:getCell(x, y) |
| 478 |
if world:getWallIdFromBlockId(east) == "external" then |
| 479 |
check_external_window(x, y, 2) |
| 480 |
elseif world:getWallSetFromBlockId(east) == "window_tiles" then |
| 481 |
map:setCell(x, y, 2, flag + wall_type.window_tiles.north) |
| 482 |
else |
| 483 |
map:setCell(x, y, 2, flag + wall_type.inside_tiles.north) |
| 484 |
end |
| 485 |
if world:getWallIdFromBlockId(north) == "external" then |
| 486 |
check_external_window(x, y, 3) |
| 487 |
elseif world:getWallSetFromBlockId(north) == "window_tiles" then |
| 488 |
map:setCell(x, y, 3, flag + wall_type.window_tiles.west) |
| 489 |
else |
| 490 |
map:setCell(x, y, 3, flag + wall_type.inside_tiles.west) |
| 491 |
end |
| 492 |
else |
| 493 |
repeat |
| 494 |
local tiles = "outside_tiles" |
| 495 |
local tag = anim:getTag() |
| 496 |
if tag == "nothing" or tag == "swing_slave" then |
| 497 |
tiles = "swing_outside_tiles" |
| 498 |
end |
| 499 |
local dir = (anim:getFlag() % 2 == 1) and "west" or "north" |
| 500 |
local layer = dir == "north" and 2 or 3 |
| 501 |
if world:getWallIdFromBlockId(map:getCell(x, y, layer)) == "external" then |
| 502 |
if layer == 2 then |
| 503 |
if (x == rect.x or x == rect.x + rect.w - 1) and (y == rect.y or y == rect.y + rect.h) then |
| 504 |
check_external_window(x, y, 2) |
| 505 |
end |
| 506 |
else |
| 507 |
if (x == rect.x or x == rect.x + rect.w) and (y == rect.y or y == rect.y + rect.h - 1) then |
| 508 |
check_external_window(x, y, 3) |
| 509 |
end |
| 510 |
end |
| 511 |
break |
| 512 |
end |
| 513 |
local existing = world:getWallSetFromBlockId(map:getCell(x, y, layer)) |
| 514 |
if rect.x <= x and x < rect.x + rect.w and rect.y <= y and y < rect.y + rect.h then |
| 515 |
if tag == "nothing" or tag == "swing_slave" then |
| 516 |
tiles = "swing_inside_tiles" |
| 517 |
else |
| 518 |
tiles = "inside_tiles" |
| 519 |
end |
| 520 |
elseif existing then |
| 521 |
break |
| 522 |
end |
| 523 |
if tag == "window" or existing == "window_tiles" then |
| 524 |
tiles = "window_tiles" |
| 525 |
end |
| 526 |
local suffixes = {"_left", "_right"} |
| 527 |
local num = dir == "north" and 1 or 0 |
| 528 |
if tag == "door" then |
| 529 |
door = world:newObject("door", x, y, dir) |
| 530 |
elseif tag == "swing_master" then |
| 531 |
door = world:newObject("swing_door_right", x, y, dir) |
| 532 |
elseif tag == "swing_slave" then |
| 533 |
door2 = world:newObject("swing_door_left", x, y, dir) |
| 534 |
map:setCell(x, y, layer, flag + wall_type[tiles][dir .. suffixes[2 - num]]) |
| 535 |
elseif tag == "nothing" then |
| 536 |
map:setCell(x, y, layer, flag + wall_type[tiles][dir .. suffixes[num + 1]]) |
| 537 |
else |
| 538 |
map:setCell(x, y, layer, flag + wall_type[tiles][dir]) |
| 539 |
end |
| 540 |
until true |
| 541 |
end |
| 542 |
anim:setTile(nil) |
| 543 |
end |
| 544 |
self.blueprint_wall_anims[x] = nil |
| 545 |
end |
| 546 |
-- If there is already a room, e.g. it is being moved, don't make a new one. |
| 547 |
if self.room then |
| 548 |
self.room:initRoom(rect.x, rect.y, rect.w, rect.h, door, door2) |
| 549 |
else |
| 550 |
self.room = self.world:newRoom(rect.x, rect.y, rect.w, rect.h, room_type, door, door2) |
| 551 |
end |
| 552 |
end |
| 553 |
|
| 554 |
function UIEditRoom:purchaseItems() |
| 555 |
self.visible = false |
| 556 |
self.place_objects = false |
| 557 |
self:stopPickupItems() |
| 558 |
|
| 559 |
local cfg_objects = self.world.map.level_config.objects |
| 560 |
local research = self.ui.hospital.research |
| 561 |
|
| 562 |
local object_list = {} -- transform set to list |
| 563 |
for i, o in ipairs(self.room.room_info.objects_additional) do |
| 564 |
-- Don't show the object if it hasn't been researched yet. |
| 565 |
local object = TheApp.objects[o] |
| 566 |
local avail = cfg_objects[object.thob].AvailableForLevel |
| 567 |
if avail == 1 and (not research.research_progress[object] or |
| 568 |
research.research_progress[object].discovered) then |
| 569 |
-- look up current quantity |
| 570 |
local cur_qty = 0 |
| 571 |
for _, p in ipairs(self.objects) do |
| 572 |
if p.object.id == o then |
| 573 |
cur_qty = p.qty |
| 574 |
end |
| 575 |
end |
| 576 |
|
| 577 |
-- look up minimum quantity (required objects list) |
| 578 |
local min_qty = self.room.room_info.objects_needed[o] or 0 |
| 579 |
|
| 580 |
-- subtract number of objects in room from minimum quantity |
| 581 |
for obj, _ in pairs(self.room.objects) do |
| 582 |
if min_qty == 0 then break end |
| 583 |
if obj.object_type.id == o then |
| 584 |
min_qty = min_qty - 1 |
| 585 |
end |
| 586 |
end |
| 587 |
object_list[i] = { object = TheApp.objects[o], qty = cur_qty, min_qty = min_qty } |
| 588 |
end |
| 589 |
end |
| 590 |
|
| 591 |
self.ui:addWindow(UIFurnishCorridor(self.ui, object_list, self)) |
| 592 |
end |
| 593 |
|
| 594 |
-- callback for item pick up button |
| 595 |
function UIEditRoom:pickupItems() |
| 596 |
if self.in_pickup_mode then |
| 597 |
self:stopPickupItems() |
| 598 |
else |
| 599 |
self.in_pickup_mode = true |
| 600 |
self.ui:setCursor(self.ui.grab_cursor) |
| 601 |
self.place_objects = false |
| 602 |
self.active_index = 0 |
| 603 |
self.object_cell_x = nil |
| 604 |
self.object_cell_y = nil |
| 605 |
self:clearBlueprint() |
| 606 |
end |
| 607 |
end |
| 608 |
|
| 609 |
function UIEditRoom:stopPickupItems() |
| 610 |
if self.in_pickup_mode then |
| 611 |
self.in_pickup_mode = false |
| 612 |
self.ui:setCursor(self.ui.default_cursor) |
| 613 |
self.pickup_button:setToggleState(false) |
| 614 |
end |
| 615 |
end |
| 616 |
|
| 617 |
function UIEditRoom:returnToWallPhase(early) |
| 618 |
self.ui:tutorialStep(3, {9, 10, 11, 12}, 4) |
| 619 |
if not early then |
| 620 |
self.desc_text = _S.place_objects_window.drag_blueprint |
| 621 |
self.confirm_button:enable(true) |
| 622 |
for k, obj in pairs(self.blueprint_wall_anims) do |
| 623 |
for _, anim in pairs(obj) do |
| 624 |
anim:setTile(nil) |
| 625 |
end |
| 626 |
self.blueprint_wall_anims[k] = nil |
| 627 |
end |
| 628 |
end |
| 629 |
local rect = self.blueprint_rect |
| 630 |
local x, y, w, h = rect.x, rect.y, rect.w, rect.h |
| 631 |
self:setBlueprintRect(1, 1, 0, 0) |
| 632 |
self:setBlueprintRect(x, y, w, h) |
| 633 |
if self.room_type.swing_doors then |
| 634 |
self.blueprint_door = {anim = {}, old_anim = {}, old_flags = {}} |
| 635 |
else |
| 636 |
self.blueprint_door = {} |
| 637 |
end |
| 638 |
end |
| 639 |
|
| 640 |
-- Remove walls |
| 641 |
function UIEditRoom:_remove_wall_line(x, y, step_x, step_y, n_steps, layer, neigh_x, neigh_y, world) |
| 642 |
local map = world.map.th |
| 643 |
for _ = 1, n_steps do |
| 644 |
local existing = map:getCell(x, y, layer) |
| 645 |
-- Possibly add transparency. |
| 646 |
local flag = 0 |
| 647 |
if world.ui.transparent_walls then |
| 648 |
flag = 1024 |
| 649 |
end |
| 650 |
if world:getWallIdFromBlockId(existing) ~= "external" then |
| 651 |
local neighbour = world:getRoom(x + neigh_x, y + neigh_y) |
| 652 |
if neighbour then |
| 653 |
if neigh_x ~= 0 or neigh_y ~= 0 then |
| 654 |
local set = world:getWallSetFromBlockId(existing) |
| 655 |
local dir = world:getWallDirFromBlockId(existing) |
| 656 |
if set == "inside_tiles" then |
| 657 |
set = "outside_tiles" |
| 658 |
end |
| 659 |
map:setCell(x, y, layer, flag + world.wall_types[neighbour.room_info.wall_type][set][dir]) |
| 660 |
end |
| 661 |
else |
| 662 |
map:setCell(x, y, layer, flag) |
| 663 |
end |
| 664 |
end |
| 665 |
x = x + step_x |
| 666 |
y = y + step_y |
| 667 |
end |
| 668 |
end |
| 669 |
|
| 670 |
function UIEditRoom:removeRoom(save_objects, room, world) |
| 671 |
-- Remove any placed objects (add them to list again) when save_objects is true |
| 672 |
for x = room.x, room.x + room.width - 1 do |
| 673 |
for y = room.y, room.y + room.height - 1 do |
| 674 |
while true do |
| 675 |
-- get litter then any other object |
| 676 |
local obj = world:getObject(x, y, 'litter') or world:getObject(x, y) |
| 677 |
-- but we need to ignore doors |
| 678 |
if not obj or obj == room.door or class.is(obj, SwingDoor) then |
| 679 |
break |
| 680 |
end |
| 681 |
if obj.object_type.id == "litter" then -- Silently remove litter from the world. |
| 682 |
obj:remove() |
| 683 |
else |
| 684 |
if save_objects then |
| 685 |
local obj_state = obj:getState() |
| 686 |
world:destroyEntity(obj) |
| 687 |
if not obj.master then |
| 688 |
self:addObjects({{ |
| 689 |
object = TheApp.objects[obj.object_type.id], |
| 690 |
state = obj_state, |
| 691 |
qty = 1 |
| 692 |
}}) |
| 693 |
end |
| 694 |
else |
| 695 |
-- just destroy it |
| 696 |
world:destroyEntity(obj) |
| 697 |
end |
| 698 |
end |
| 699 |
end |
| 700 |
end |
| 701 |
end |
| 702 |
|
| 703 |
-- now doors |
| 704 |
world:destroyEntity(room.door) |
| 705 |
if room.door2 then |
| 706 |
world:destroyEntity(room.door2) |
| 707 |
end |
| 708 |
|
| 709 |
if save_objects then |
| 710 |
-- backup list of objects |
| 711 |
self.objects_backup = {} |
| 712 |
for k, o in pairs(self.objects) do |
| 713 |
self.objects_backup[k] = { object = o.object, qty = o.qty, state = o.state } |
| 714 |
end |
| 715 |
|
| 716 |
UIPlaceObjects.removeAllObjects(self) |
| 717 |
end |
| 718 |
|
| 719 |
self:_remove_wall_line(room.x, room.y, 0, 1, room.height, 3, -1, 0, world) |
| 720 |
self:_remove_wall_line(room.x, room.y, 1, 0, room.width , 2, 0, -1, world) |
| 721 |
self:_remove_wall_line(room.x + room.width, room.y , 0, 1, room.height, 3, 0, 0, world) |
| 722 |
self:_remove_wall_line(room.x, room.y + room.height, 1, 0, room.width , 2, 0, 0, world) |
| 723 |
|
| 724 |
-- Reset floor tiles and flags |
| 725 |
world.map.th:unmarkRoom(room.x, room.y, room.width, room.height) |
| 726 |
end |
| 727 |
|
| 728 |
function UIEditRoom:returnToDoorPhase() |
| 729 |
self.ui:tutorialStep(3, {13, 14, 15}, 9) |
| 730 |
local room = self.room |
| 731 |
room.built = false |
| 732 |
if room.door and room.door.queue then |
| 733 |
room.door.queue:rerouteAllPatients(room.room_info.id) |
| 734 |
end |
| 735 |
|
| 736 |
self.purchase_button:enable(false) |
| 737 |
self.pickup_button:enable(false) |
| 738 |
|
| 739 |
self:removeRoom(true, room, self.world) |
| 740 |
|
| 741 |
-- Re-create blueprint |
| 742 |
local rect = self.blueprint_rect |
| 743 |
local old_w, old_h = rect.w, rect.h |
| 744 |
rect.w = 0 |
| 745 |
rect.h = 0 |
| 746 |
self:setBlueprintRect(rect.x, rect.y, old_w, old_h) |
| 747 |
|
| 748 |
-- We've gone all the way back to wall phase, so step forward to door phase |
| 749 |
self.phase = "door" |
| 750 |
self:enterDoorPhase() |
| 751 |
end |
| 752 |
|
| 753 |
function UIEditRoom:screenToWall(x, y) |
| 754 |
local cellx, celly = self.ui:ScreenToWorld(x, y) |
| 755 |
cellx = math.floor(cellx) |
| 756 |
celly = math.floor(celly) |
| 757 |
local rect = self.blueprint_rect |
| 758 |
|
| 759 |
if cellx == rect.x or cellx == rect.x - 1 or cellx == rect.x + rect.w or cellx == rect.x + rect.w - 1 or |
| 760 |
celly == rect.y or celly == rect.y - 1 or celly == rect.y + rect.h or |
| 761 |
celly == rect.y + rect.h - 1 then -- luacheck: ignore 542 |
| 762 |
else |
| 763 |
return |
| 764 |
end |
| 765 |
|
| 766 |
-- NB: Doors and windows cannot be placed on corner tiles, hence walls of corner tiles |
| 767 |
-- are never returned, and the nearest non-corner wall is returned instead. If they |
| 768 |
-- could be placed on corner tiles, then you would have to consider the interaction of |
| 769 |
-- wall shadows with windows and doors, amongst other things. |
| 770 |
-- Swing doors are allowed to be adjacent everywhere except the top corner. |
| 771 |
local modifier = 0 |
| 772 |
local swinging = false |
| 773 |
if self.room_type.swing_doors and self.phase == "door" then |
| 774 |
modifier = 1 |
| 775 |
swinging = true |
| 776 |
end |
| 777 |
if cellx == rect.x and celly == rect.y then |
| 778 |
-- top corner |
| 779 |
local x_, _ = self.ui:WorldToScreen(cellx, celly) |
| 780 |
if x >= x_ then |
| 781 |
-- correctly reflects (at least origin version) of TH. |
| 782 |
-- Swing doors in top corner to the east, actually skip another tile |
| 783 |
if swinging then |
| 784 |
return cellx + 2 + modifier, celly, "north" |
| 785 |
else |
| 786 |
return cellx + 1 + modifier, celly, "north" |
| 787 |
end |
| 788 |
else |
| 789 |
return cellx, celly + 1 + modifier, "west" |
| 790 |
end |
| 791 |
elseif cellx == rect.x + rect.w - 1 and celly == rect.y + rect.h - 1 then |
| 792 |
-- bottom corner |
| 793 |
local x_, _ = self.ui:WorldToScreen(cellx, celly) |
| 794 |
if x >= x_ then |
| 795 |
return cellx, celly - 1, "east" |
| 796 |
else |
| 797 |
return cellx - 1, celly, "south" |
| 798 |
end |
| 799 |
elseif cellx == rect.x and celly == rect.y + rect.h - 1 then |
| 800 |
-- left corner |
| 801 |
local _, y_ = self.ui:WorldToScreen(cellx, celly) |
| 802 |
if y >= y_ + 16 then |
| 803 |
if swinging and cellx <= rect.x + 2 then |
| 804 |
return cellx + 1 + modifier, celly, "south" |
| 805 |
else |
| 806 |
return cellx + 1, celly, "south" |
| 807 |
end |
| 808 |
else |
| 809 |
return cellx, celly - 1, "west" |
| 810 |
end |
| 811 |
elseif cellx == rect.x + rect.w - 1 and celly == rect.y then |
| 812 |
-- right corner |
| 813 |
local _, y_ = self.ui:WorldToScreen(cellx, celly) |
| 814 |
if y >= y_ + 16 then |
| 815 |
if swinging and celly <= rect.y + 2 then |
| 816 |
return cellx, celly + 1 + modifier, "east" |
| 817 |
else |
| 818 |
return cellx, celly + 1, "east" |
| 819 |
end |
| 820 |
else |
| 821 |
return cellx - 1, celly, "north" |
| 822 |
end |
| 823 |
elseif (cellx == rect.x - 1 or cellx == rect.x) and rect.y <= celly and celly < rect.y + rect.h then |
| 824 |
-- west edge |
| 825 |
if celly == rect.y then |
| 826 |
celly = rect.y + 1 |
| 827 |
elseif celly == rect.y + rect.h - 1 then |
| 828 |
celly = rect.y + rect.h - 2 |
| 829 |
end |
| 830 |
if swinging and celly <= rect.y + 1 then |
| 831 |
celly = celly + 1 |
| 832 |
end |
| 833 |
return rect.x, celly, "west" |
| 834 |
elseif (celly == rect.y - 1 or celly == rect.y) and rect.x <= cellx and cellx < rect.x + rect.w then |
| 835 |
-- north edge |
| 836 |
-- correctly reflects (at least origin version) of TH. |
| 837 |
-- Swing doors in top corner to the east, actually skip another tile |
| 838 |
if swinging and cellx <= rect.x + 2 then |
| 839 |
cellx = rect.x + 3 |
| 840 |
elseif cellx == rect.x then |
| 841 |
cellx = rect.x + 1 + modifier |
| 842 |
elseif cellx == rect.x + rect.w - 1 then |
| 843 |
cellx = rect.x + rect.w - 2 |
| 844 |
end |
| 845 |
return cellx, rect.y, "north" |
| 846 |
elseif (cellx == rect.x + rect.w or cellx == rect.x + rect.w - 1) and |
| 847 |
rect.y <= celly and celly < rect.y + rect.h then |
| 848 |
-- east edge |
| 849 |
if swinging and celly <= rect.y + 1 then |
| 850 |
celly = rect.y + 2 |
| 851 |
elseif celly == rect.y then |
| 852 |
celly = rect.y + 1 |
| 853 |
elseif celly == rect.y + rect.h - 1 then |
| 854 |
celly = rect.y + rect.h - 2 |
| 855 |
end |
| 856 |
return rect.x + rect.w - 1, celly, "east" |
| 857 |
elseif (celly == rect.y + rect.h or celly == rect.y + rect.h - 1) and |
| 858 |
rect.x <= cellx and cellx < rect.x + rect.w then |
| 859 |
-- south edge |
| 860 |
if swinging and cellx <= rect.x + 1 then |
| 861 |
cellx = rect.x + 2 |
| 862 |
elseif cellx == rect.x then |
| 863 |
cellx = rect.x + 1 |
| 864 |
elseif cellx == rect.x + rect.w - 1 then |
| 865 |
cellx = rect.x + rect.w - 2 |
| 866 |
end |
| 867 |
return cellx, rect.y + rect.h - 1, "south" |
| 868 |
end |
| 869 |
end |
| 870 |
|
| 871 |
-- Function to check if the tiles adjacent to the room are still reachable from each other. |
| 872 |
-- NB: the passable flags of the room have to be set to false already before calling this function |
| 873 |
function UIEditRoom:checkReachability() |
| 874 |
local map = self.ui.app.map.th |
| 875 |
local world = self.ui.app.world |
| 876 |
|
| 877 |
local rect = self.blueprint_rect |
| 878 |
local prev_x, prev_y |
| 879 |
local x, y = rect.x, rect.y - 1 |
| 880 |
local flags = {} |
| 881 |
|
| 882 |
local function check(flag) |
| 883 |
if map:getCellFlags(x, y, flags).passable and flags[flag] then |
| 884 |
if prev_x and not world:getPathDistance(prev_x, prev_y, x, y) then |
| 885 |
return false |
| 886 |
end |
| 887 |
prev_x, prev_y = x, y |
| 888 |
end |
| 889 |
return true |
| 890 |
end |
| 891 |
|
| 892 |
while x < rect.x + rect.w do |
| 893 |
if not check("travelSouth") then return false end |
| 894 |
x = x + 1 |
| 895 |
end |
| 896 |
y = y + 1 |
| 897 |
while y < rect.y + rect.h do |
| 898 |
if not check("travelWest") then return false end |
| 899 |
y = y + 1 |
| 900 |
end |
| 901 |
x = x - 1 |
| 902 |
while x >= rect.x do |
| 903 |
if not check("travelNorth") then return false end |
| 904 |
x = x - 1 |
| 905 |
end |
| 906 |
y = y - 1 |
| 907 |
while y >= rect.y do |
| 908 |
if not check("travelEast") then return false end |
| 909 |
y = y - 1 |
| 910 |
end |
| 911 |
|
| 912 |
return true |
| 913 |
end |
| 914 |
|
| 915 |
function UIEditRoom:enterDoorPhase() |
| 916 |
self.ui:tutorialStep(3, 8, 9) |
| 917 |
-- make tiles impassable |
| 918 |
for y = self.blueprint_rect.y, self.blueprint_rect.y + self.blueprint_rect.h - 1 do |
| 919 |
for x = self.blueprint_rect.x, self.blueprint_rect.x + self.blueprint_rect.w - 1 do |
| 920 |
self.ui.app.map:setCellFlags(x, y, {passable = false}) |
| 921 |
end |
| 922 |
end |
| 923 |
|
| 924 |
-- check if all adjacent tiles of the rooms are still connected |
| 925 |
if not self:checkReachability() then |
| 926 |
if self.ui.app.config.blocking_off_areas == 3 then |
| 927 |
-- all-permissive placing approach |
| 928 |
-- This could lead to crashes, so we'll record this in the log so that during investigation |
| 929 |
-- we'll be able to know that safe placement was disabled. |
| 930 |
TheApp.world:gameLog("Blocking off areas is allowed with room " .. self.blueprint_rect.x .. ", " .. self.blueprint_rect.y .. ".") |
| 931 |
else |
| 932 |
-- undo passable flags and go back to walls phase |
| 933 |
self.phase = "walls" |
| 934 |
self:returnToWallPhase(true) |
| 935 |
self.ui:playSound("wrong2.wav") |
| 936 |
self.ui.adviser:say(_A.room_forbidden_non_reachable_parts) |
| 937 |
return |
| 938 |
end |
| 939 |
end |
| 940 |
|
| 941 |
self.desc_text = _S.place_objects_window.place_door |
| 942 |
self.confirm_button:enable(false) -- Confirmation is via placing door |
| 943 |
|
| 944 |
-- Change the floor tiles to opaque blue |
| 945 |
local map = self.ui.app.map.th |
| 946 |
for y = self.blueprint_rect.y, self.blueprint_rect.y + self.blueprint_rect.h - 1 do |
| 947 |
for x = self.blueprint_rect.x, self.blueprint_rect.x + self.blueprint_rect.w - 1 do |
| 948 |
map:setCell(x, y, 4, 24) |
| 949 |
end |
| 950 |
end |
| 951 |
|
| 952 |
-- Re-organise wall anims to index by x and y |
| 953 |
local walls = {} |
| 954 |
for _, wall in ipairs(self.blueprint_wall_anims) do |
| 955 |
local _, x, y = wall:getTile() |
| 956 |
if not walls[x] then |
| 957 |
walls[x] = {} |
| 958 |
end |
| 959 |
walls[x][y] = wall |
| 960 |
end |
| 961 |
self.blueprint_wall_anims = walls |
| 962 |
end |
| 963 |
|
| 964 |
function UIEditRoom:enterWindowsPhase() |
| 965 |
self.ui:tutorialStep(3, {9, 10}, 11) |
| 966 |
self.desc_text = _S.place_objects_window.place_windows |
| 967 |
self.confirm_button:enable(true) |
| 968 |
end |
| 969 |
|
| 970 |
function UIEditRoom:enterObjectsPhase() |
| 971 |
self.ui:setCursor(self.ui.default_cursor) |
| 972 |
self.ui:tutorialStep(3, {11, 12}, 13) |
| 973 |
self.ui:setWorldHitTest(self.room) |
| 974 |
local confirm = self:checkEnableConfirm() |
| 975 |
if #self.room.room_info.objects_additional == 0 and confirm then |
| 976 |
self:confirm(true) |
| 977 |
return |
| 978 |
end |
| 979 |
self.desc_text = _S.place_objects_window.confirm_or_buy_objects |
| 980 |
if #self.room.room_info.objects_additional > 0 then |
| 981 |
self.purchase_button:enable(true) |
| 982 |
end |
| 983 |
self.pickup_button:enable(true) |
| 984 |
|
| 985 |
if self.objects_backup then |
| 986 |
self:addObjects(self.objects_backup, true) |
| 987 |
else |
| 988 |
local room_objects = self.room.room_info.objects_needed |
| 989 |
if TheApp.config.enable_avg_contents then |
| 990 |
room_objects = self:computeAverageContents() |
| 991 |
end |
| 992 |
local object_list = {} -- transform set to list |
| 993 |
for o, num in pairs(room_objects) do |
| 994 |
if num > 0 then |
| 995 |
object_list[#object_list + 1] = { object = TheApp.objects[o], qty = num } |
| 996 |
end |
| 997 |
end |
| 998 |
self:addObjects(object_list, true) |
| 999 |
end |
| 1000 |
end |
| 1001 |
|
| 1002 |
-- Decide contents of the new room based on average content of previous built rooms |
| 1003 |
-- of the same type. |
| 1004 |
function UIEditRoom:computeAverageContents() |
| 1005 |
local average_objects = {} -- what does the average room of this type contain? |
| 1006 |
local room_count = 0 |
| 1007 |
for _, room in pairs(self.world.rooms) do |
| 1008 |
if room and room.built and not room.crashed and |
| 1009 |
room.hospital == self.ui.hospital and room.room_info == self.room_type then |
| 1010 |
room_count = room_count + 1 |
| 1011 |
for obj, _ in pairs(room.objects) do |
| 1012 |
average_objects[obj.object_type.id] = (average_objects[obj.object_type.id] or 0) + 1 |
| 1013 |
end |
| 1014 |
end |
| 1015 |
end |
| 1016 |
-- Ensure room contents is within the boundaries of objects_needed and objects_additional. |
| 1017 |
local objects_needed = self.room.room_info.objects_needed |
| 1018 |
local additional_objects = {} -- Reversed mapping |
| 1019 |
for _, obj in pairs(self.room.room_info.objects_additional) do |
| 1020 |
additional_objects[obj] = 1 |
| 1021 |
end |
| 1022 |
for id, count in pairs(average_objects) do |
| 1023 |
count = math.floor(count / room_count + 0.5) |
| 1024 |
if additional_objects[id] == nil and objects_needed[id] == nil then |
| 1025 |
count = 0 |
| 1026 |
end |
| 1027 |
average_objects[id] = count |
| 1028 |
end |
| 1029 |
for id, count in pairs(objects_needed) do |
| 1030 |
if (average_objects[id] or 0) < count then |
| 1031 |
average_objects[id] = count |
| 1032 |
end |
| 1033 |
end |
| 1034 |
return average_objects |
| 1035 |
end |
| 1036 |
|
| 1037 |
function UIEditRoom:draw(canvas, ...) |
| 1038 |
if self.world.user_actions_allowed then |
| 1039 |
local ui = self.ui |
| 1040 |
local x, y = ui:WorldToScreen(self.mouse_cell_x, self.mouse_cell_y) |
| 1041 |
local zoom = self.ui.zoom_factor |
| 1042 |
if canvas:scale(zoom) then |
| 1043 |
x = math.floor(x / zoom) |
| 1044 |
y = math.floor(y / zoom) |
| 1045 |
end |
| 1046 |
self.cell_outline:draw(canvas, 2, x - 32, y) |
| 1047 |
canvas:scale(1) |
| 1048 |
end |
| 1049 |
|
| 1050 |
UIPlaceObjects.draw(self, canvas, ...) |
| 1051 |
end |
| 1052 |
|
| 1053 |
--25 north wall, west |
| 1054 |
--26 north wall, east |
| 1055 |
--27 east wall, south |
| 1056 |
--28 east wall, north |
| 1057 |
--29 south wall, west |
| 1058 |
--30 south wall, east |
| 1059 |
--31 west wall, south |
| 1060 |
--32 west wall, north |
| 1061 |
-- single door blue print values matching TH |
| 1062 |
local door_floor_blueprint_markers = { |
| 1063 |
north = 26, |
| 1064 |
east = 27, |
| 1065 |
south = 30, |
| 1066 |
west = 31 |
| 1067 |
} |
| 1068 |
|
| 1069 |
local window_floor_blueprint_markers = { |
| 1070 |
north = 33, |
| 1071 |
east = 34, |
| 1072 |
south = 35, |
| 1073 |
west = 36, |
| 1074 |
} |
| 1075 |
|
| 1076 |
function UIEditRoom:onLeftButtonDown(x, y) |
| 1077 |
local s = TheApp.config.ui_scale |
| 1078 |
if self.phase == "walls" then |
| 1079 |
if 0 <= x and x < self.width * s and 0 <= y and y < self.height * s then -- luacheck: ignore 542 |
| 1080 |
else |
| 1081 |
local mouse_x, mouse_y = self.ui:ScreenToWorld(self.x * s + x, self.y * s + y) |
| 1082 |
self.mouse_down_x = math.floor(mouse_x) |
| 1083 |
self.mouse_down_y = math.floor(mouse_y) |
| 1084 |
if self.move_rect then |
| 1085 |
self.move_rect_x = self.mouse_down_x - self.blueprint_rect.x |
| 1086 |
self.move_rect_y = self.mouse_down_y - self.blueprint_rect.y |
| 1087 |
elseif not self.resize_rect then |
| 1088 |
self:setBlueprintRect(self.mouse_down_x, self.mouse_down_y, 1, 1) |
| 1089 |
end |
| 1090 |
end |
| 1091 |
elseif self.phase == "door" then |
| 1092 |
if self.blueprint_door.valid then |
| 1093 |
self.ui:playSound("buildclk.wav") |
| 1094 |
self:confirm(true) |
| 1095 |
else |
| 1096 |
self.ui:tutorialStep(3, 9, 10) |
| 1097 |
end |
| 1098 |
elseif self.phase == "windows" then |
| 1099 |
self:placeWindowBlueprint() |
| 1100 |
end |
| 1101 |
end |
| 1102 |
|
| 1103 |
--! Attempt to remove a window placed on the room blueprints |
| 1104 |
--!param x coordinate |
| 1105 |
--!param y coordinate |
| 1106 |
function UIEditRoom:tryRemoveWindowFromWall(x, y) |
| 1107 |
local cell_x, cell_y, wall_dir = self:screenToWall(self.x + x, self.y + y) |
| 1108 |
if not cell_x then |
| 1109 |
return |
| 1110 |
end |
| 1111 |
|
| 1112 |
local map = TheApp.map.th |
| 1113 |
local cell_flag = map:getCell(cell_x, cell_y, 4) |
| 1114 |
if cell_flag == window_floor_blueprint_markers[wall_dir] then -- right click on a wall with a window |
| 1115 |
local wall_x, wall_y = cell_x, cell_y |
| 1116 |
local direction_flag = (wall_dir == "south" or wall_dir == "north") and 0 or 1 |
| 1117 |
if wall_dir == "south" then |
| 1118 |
wall_y = cell_y + 1 |
| 1119 |
elseif wall_dir == "east" then |
| 1120 |
wall_x = cell_x + 1 |
| 1121 |
end |
| 1122 |
|
| 1123 |
local anim = self.blueprint_wall_anims[wall_x][wall_y] |
| 1124 |
if anim then |
| 1125 |
-- reset tile to basic floor/wall |
| 1126 |
map:setCell(cell_x, cell_y, 4, 24) |
| 1127 |
anim:setAnimation(self.anims, 120, direction_flag) |
| 1128 |
anim:setTag(nil) |
| 1129 |
self.ui:playSound("de_build.wav") |
| 1130 |
end |
| 1131 |
end |
| 1132 |
end |
| 1133 |
|
| 1134 |
function UIEditRoom:onMouseDown(button, x, y) |
| 1135 |
if self.world.user_actions_allowed and not self.confirm_dialog_open then |
| 1136 |
if button == "left" then |
| 1137 |
self:onLeftButtonDown(x, y) |
| 1138 |
elseif button == "right" then |
| 1139 |
if self.phase == "windows" then |
| 1140 |
self:tryRemoveWindowFromWall(x, y) |
| 1141 |
end |
| 1142 |
end |
| 1143 |
end |
| 1144 |
return UIPlaceObjects.onMouseDown(self, button, x, y) or true |
| 1145 |
end |
| 1146 |
|
| 1147 |
function UIEditRoom:onMouseUp(button, x, y) |
| 1148 |
if self.mouse_down_x then |
| 1149 |
self.mouse_down_x = false |
| 1150 |
self.mouse_down_y = false |
| 1151 |
end |
| 1152 |
|
| 1153 |
if self.move_rect_x then |
| 1154 |
self.move_rect_x = false |
| 1155 |
self.move_rect_y = false |
| 1156 |
end |
| 1157 |
|
| 1158 |
return UIPlaceObjects.onMouseUp(self, button, x, y) |
| 1159 |
end |
| 1160 |
|
| 1161 |
function UIEditRoom:onMouseMove(x, y, dx, dy) |
| 1162 |
if self.in_pickup_mode then |
| 1163 |
self.ui:setCursor(self.ui.app.gfx:loadMainCursor("grab")) |
| 1164 |
end |
| 1165 |
end |
| 1166 |
|
| 1167 |
function UIEditRoom:setBlueprintRect(x, y, w, h) |
| 1168 |
local rect = self.blueprint_rect |
| 1169 |
local map = self.ui.app.map |
| 1170 |
if x < 1 then x = 1 end |
| 1171 |
if y < 1 then y = 1 end |
| 1172 |
if x + w > map.width then w = map.width - x end |
| 1173 |
if y + h > map.height then h = map.height - y end |
| 1174 |
|
| 1175 |
if rect.x == x and rect.y == y and rect.w == w and rect.h == h then |
| 1176 |
-- Nothing to do |
| 1177 |
return |
| 1178 |
end |
| 1179 |
|
| 1180 |
local too_small = w < self.room_type.minimum_size or h < self.room_type.minimum_size |
| 1181 |
local player_id = self.ui.hospital:getPlayerIndex() |
| 1182 |
|
| 1183 |
-- Entire update of floor tiles and wall animations done in C to replace |
| 1184 |
-- several hundred calls into C with just a single call. The price for this |
| 1185 |
-- is reduced flexibility. See l_map_updateblueprint in th_lua.cpp for code. |
| 1186 |
local is_valid = map.th:updateRoomBlueprint(rect.x, rect.y, rect.w, rect.h, |
| 1187 |
x, y, w, h, player_id, self.blueprint_wall_anims, self.anims, too_small) |
| 1188 |
|
| 1189 |
-- NB: due to the unflexibility, tutorial step "too small AND invalid position" (3.7) |
| 1190 |
-- is currently unusable, as it's not possible to determine if the position would |
| 1191 |
-- have been invalid even if it weren't too small. |
| 1192 |
if self.phase ~= "closed" then |
| 1193 |
if too_small then |
| 1194 |
self.ui:tutorialStep(3, {4, 5, 8}, 6) |
| 1195 |
elseif not is_valid then |
| 1196 |
self.ui:tutorialStep(3, {4, 6, 8}, 5) |
| 1197 |
else |
| 1198 |
self.ui:tutorialStep(3, {4, 5, 6}, 8) |
| 1199 |
end |
| 1200 |
end |
| 1201 |
|
| 1202 |
self.confirm_button:enable(is_valid) |
| 1203 |
|
| 1204 |
rect.x = x |
| 1205 |
rect.y = y |
| 1206 |
rect.w = w |
| 1207 |
rect.h = h |
| 1208 |
end |
| 1209 |
|
| 1210 |
--! Check walls for having room for the door |
| 1211 |
--!param x (int) X tile position of the door. |
| 1212 |
--!param y (int) Y tile position of the door. |
| 1213 |
--!param wall (string) Name of the wall (either 'north' or 'west'). |
| 1214 |
--!param has_swingdoor (boolean) Whether the room has a normal door (false) or a swing door (true) as entrance. |
| 1215 |
--!return (int) bit flags indicating invalid tile position using 1 based power of 2 as this works with ipairs |
| 1216 |
--! values returned are the enumeration of |
| 1217 |
--! 4 = centre door in swing door or for single door the value can just be non-zero but uses the same bit of code |
| 1218 |
--! 2 (door section closer to top of screen) - smaller x or y |
| 1219 |
--! 8 (door section closer to bottom of screen) - larger x or y |
| 1220 |
local function checkDoorWalls(x, y, wall, has_swingdoor) |
| 1221 |
local th = TheApp.map.th |
| 1222 |
|
| 1223 |
local dx, dy, wall_num |
| 1224 |
if wall == "west" then |
| 1225 |
wall_num = 3 |
| 1226 |
dx = 0 |
| 1227 |
dy = 1 |
| 1228 |
else |
| 1229 |
wall_num = 2 |
| 1230 |
dx = 1 |
| 1231 |
dy = 0 |
| 1232 |
end |
| 1233 |
local invalid_tile = 0 |
| 1234 |
if th:getCell(x, y, wall_num) % 0x100 ~= 0 then |
| 1235 |
invalid_tile = 4 |
| 1236 |
end |
| 1237 |
|
| 1238 |
-- If it is a swing door there are two more locations to check. |
| 1239 |
if has_swingdoor then |
| 1240 |
if th:getCell(x - dx, y - dy, wall_num) % 0x100 ~= 0 then |
| 1241 |
invalid_tile = invalid_tile + 2 |
| 1242 |
end |
| 1243 |
if th:getCell(x + dx, y + dy, wall_num) % 0x100 ~= 0 then |
| 1244 |
invalid_tile = invalid_tile + 8 |
| 1245 |
end |
| 1246 |
end |
| 1247 |
return invalid_tile |
| 1248 |
end |
| 1249 |
|
| 1250 |
--! Check whether the given tile can function as a door entry/exit tile. |
| 1251 |
--!param xpos (int) X position of the tile. |
| 1252 |
--!param ypos (int) Y position of the tile. |
| 1253 |
--!param player_id (int) Player id owning the hospital. |
| 1254 |
--!param world - reference to world object instance |
| 1255 |
--!return (boolean) whether the tile is considered to be valid. |
| 1256 |
local function validDoorTile(xpos, ypos, player_id, world) |
| 1257 |
local th = TheApp.map.th |
| 1258 |
local tile_flags = th:getCellFlags(xpos, ypos) |
| 1259 |
-- check own it |
| 1260 |
if tile_flags.owner ~= player_id then return false end |
| 1261 |
-- any object will cause it to be blocked (ignore litter) |
| 1262 |
if tile_flags.thob ~= 0 and tile_flags.thob ~= 62 then return false end |
| 1263 |
-- check if its passable that no object footprint blocks it |
| 1264 |
if tile_flags.passable then return world:isTileExclusivelyPassable(xpos, ypos, 1) end |
| 1265 |
return true |
| 1266 |
end |
| 1267 |
|
| 1268 |
--! Calculate position offsets and door blueprint wall values |
| 1269 |
--! param x (int) doors blueprint x value |
| 1270 |
--! param y (int) doors blueprint y value |
| 1271 |
--! param wall (string) original wall orientation |
| 1272 |
--! return x (int) updated x value |
| 1273 |
--! return y (int) updated y value |
| 1274 |
--! return x_mod (int) offset value to apply to tile count to determine relative position |
| 1275 |
--! return y_mod (int) offset value to apply to tile count to determine relatitve position |
| 1276 |
--! return wall (string) wall orientation style (only 2 styles) |
| 1277 |
local function doorWallOffsetCalculations(x, y, wall) |
| 1278 |
local x_mod |
| 1279 |
local y_mod |
| 1280 |
if wall == "south" then |
| 1281 |
y = y + 1 |
| 1282 |
wall = "north" |
| 1283 |
x_mod = 2 |
| 1284 |
elseif wall == "east" then |
| 1285 |
x = x + 1 |
| 1286 |
wall = "west" |
| 1287 |
y_mod = 2 |
| 1288 |
elseif wall == "north" then |
| 1289 |
x_mod = 2 |
| 1290 |
else |
| 1291 |
y_mod = 2 |
| 1292 |
end |
| 1293 |
return x, y, x_mod, y_mod, wall |
| 1294 |
end |
| 1295 |
|
| 1296 |
function UIEditRoom:setDoorBlueprint(orig_x, orig_y, orig_wall) |
| 1297 |
local x, y, x_mod, y_mod, wall = doorWallOffsetCalculations(orig_x, orig_y, orig_wall) |
| 1298 |
local map = TheApp.map.th |
| 1299 |
|
| 1300 |
if self.blueprint_door.anim then |
| 1301 |
if self.room_type.swing_doors then |
| 1302 |
if self.blueprint_door.anim[1] then |
| 1303 |
-- retrieve the old door position details to reset the blue print |
| 1304 |
local oldx, oldy |
| 1305 |
local _, _, oldx_mod, oldy_mod, _ = doorWallOffsetCalculations(self.blueprint_door.floor_x, |
| 1306 |
self.blueprint_door.floor_y, self.blueprint_door.wall) |
| 1307 |
-- If we're dealing with swing doors the anim variable is actually a table with three |
| 1308 |
-- identical "doors". |
| 1309 |
for i, anim in ipairs(self.blueprint_door.anim) do |
| 1310 |
anim:setAnimation(self.anims, self.blueprint_door.old_anim[i], |
| 1311 |
self.blueprint_door.old_flags[i]) |
| 1312 |
anim:setTag(nil) |
| 1313 |
self.blueprint_door.anim[i] = nil |
| 1314 |
oldx = oldx_mod and self.blueprint_door.floor_x + (i - oldx_mod) or self.blueprint_door.floor_x |
| 1315 |
oldy = oldy_mod and self.blueprint_door.floor_y + (i - oldy_mod) or self.blueprint_door.floor_y |
| 1316 |
map:setCell(oldx, oldy, 4, 24) |
| 1317 |
end |
| 1318 |
end |
| 1319 |
else |
| 1320 |
self.blueprint_door.anim:setAnimation(self.anims, self.blueprint_door.old_anim, |
| 1321 |
self.blueprint_door.old_flags) |
| 1322 |
self.blueprint_door.anim:setTag(nil) |
| 1323 |
self.blueprint_door.anim = nil |
| 1324 |
map:setCell(self.blueprint_door.floor_x, self.blueprint_door.floor_y, 4, 24) |
| 1325 |
end |
| 1326 |
end |
| 1327 |
self.blueprint_door.x = x |
| 1328 |
self.blueprint_door.y = y |
| 1329 |
self.blueprint_door.wall = wall |
| 1330 |
self.blueprint_door.floor_x = orig_x |
| 1331 |
self.blueprint_door.floor_y = orig_y |
| 1332 |
self.blueprint_door.valid = false |
| 1333 |
if not wall then |
| 1334 |
return |
| 1335 |
end |
| 1336 |
local anim = self.blueprint_wall_anims[x][y] |
| 1337 |
if self.room_type.swing_doors then |
| 1338 |
anim = {} |
| 1339 |
local types = {"swing_slave", "swing_master", "nothing"} |
| 1340 |
for i = 1, 3 do |
| 1341 |
local x1 = x_mod and (x + (i - x_mod)) or x |
| 1342 |
local y1 = y_mod and (y + (i - y_mod)) or y |
| 1343 |
anim[i] = self.blueprint_wall_anims[x1][y1] |
| 1344 |
if anim[i] ~= self.blueprint_door.anim[i] then |
| 1345 |
self.blueprint_door.anim[i] = anim[i] |
| 1346 |
self.blueprint_door.anim[i]:setTag(types[i]) |
| 1347 |
self.blueprint_door.old_anim[i] = anim[i]:getAnimation() |
| 1348 |
self.blueprint_door.old_flags[i] = anim[i]:getFlag() |
| 1349 |
end |
| 1350 |
end |
| 1351 |
else |
| 1352 |
if anim ~= self.blueprint_door.anim then |
| 1353 |
self.blueprint_door.anim = anim |
| 1354 |
self.blueprint_door.anim:setTag("door") |
| 1355 |
self.blueprint_door.old_anim = anim:getAnimation() |
| 1356 |
self.blueprint_door.old_flags = anim:getFlag() |
| 1357 |
end |
| 1358 |
end |
| 1359 |
|
| 1360 |
local flags |
| 1361 |
local x2, y2 = x, y |
| 1362 |
if wall == "west" then |
| 1363 |
flags = 1 |
| 1364 |
x2 = x2 - 1 |
| 1365 |
else--if wall == "north" then |
| 1366 |
flags = 0 |
| 1367 |
y2 = y2 - 1 |
| 1368 |
end |
| 1369 |
local world = self.ui.app.world |
| 1370 |
-- invalid_tile used to select the individual blueprint that is blocked |
| 1371 |
local invalid_tile = checkDoorWalls(x, y, wall, self.room_type.swing_doors) |
| 1372 |
-- Ensure that the door isn't being built on top of an object |
| 1373 |
local player_id = self.ui.hospital:getPlayerIndex() |
| 1374 |
if not validDoorTile(x, y, player_id, world) or |
| 1375 |
not validDoorTile(x2, y2, player_id, world) then |
| 1376 |
invalid_tile = bitOr(invalid_tile, 4) |
| 1377 |
end |
| 1378 |
-- If we're making swing doors two more tiles need to be checked. |
| 1379 |
if self.room_type.swing_doors then |
| 1380 |
local dx = x_mod and 1 or 0 |
| 1381 |
local dy = y_mod and 1 or 0 |
| 1382 |
if not validDoorTile(x + dx, y + dy, player_id, world) or |
| 1383 |
not validDoorTile(x2 + dx, y2 + dy, player_id, world) then |
| 1384 |
invalid_tile = bitOr(invalid_tile, 8) |
| 1385 |
end |
| 1386 |
if not validDoorTile(x - dx, y - dy, player_id, world) or |
| 1387 |
not validDoorTile(x2 - dx, y2 - dy, player_id, world) then |
| 1388 |
invalid_tile = bitOr(invalid_tile, 2) |
| 1389 |
end |
| 1390 |
end |
| 1391 |
|
| 1392 |
self.blueprint_door.valid = (invalid_tile == 0) |
| 1393 |
|
| 1394 |
if self.room_type.swing_doors then |
| 1395 |
for i, animation in ipairs(anim) do |
| 1396 |
-- calculation here to flag blocked blueprint tiles on swing doors for each door tile |
| 1397 |
animation:setAnimation(self.anims, 126, flags + (hasBit(invalid_tile, i) and 1 or 0) * 16) |
| 1398 |
end |
| 1399 |
else |
| 1400 |
anim:setAnimation(self.anims, 126, flags + (invalid_tile ~= 0 and 1 or 0) * 16) |
| 1401 |
end |
| 1402 |
if self.room_type.swing_doors then |
| 1403 |
flags = door_floor_blueprint_markers[orig_wall] |
| 1404 |
local dirfix = orig_wall == "east" |
| 1405 |
flags = dirfix and flags + 1 or flags |
| 1406 |
for i = 1, 3 do |
| 1407 |
local x1 = x_mod and orig_x + i - x_mod or orig_x |
| 1408 |
local y1 = y_mod and orig_y + i - y_mod or orig_y |
| 1409 |
if (i == 2) then |
| 1410 |
map:setCell(x1, y1, 4, 24) |
| 1411 |
else |
| 1412 |
if dirfix then |
| 1413 |
map:setCell(x1, y1, 4, i < 2 and flags or flags - 1) |
| 1414 |
else |
| 1415 |
map:setCell(x1, y1, 4, i > 2 and flags or flags - 1) |
| 1416 |
end |
| 1417 |
end |
| 1418 |
end |
| 1419 |
else |
| 1420 |
map:setCell(self.blueprint_door.floor_x, self.blueprint_door.floor_y, 4, |
| 1421 |
door_floor_blueprint_markers[orig_wall]) |
| 1422 |
end |
| 1423 |
end |
| 1424 |
|
| 1425 |
function UIEditRoom:placeWindowBlueprint() |
| 1426 |
if self.blueprint_window.anim and self.blueprint_window.valid then |
| 1427 |
self.blueprint_window = {} |
| 1428 |
self.ui:playSound("buildclk.wav") |
| 1429 |
elseif self.blueprint_window.anim and not self.blueprint_window.valid then |
| 1430 |
self.ui:tutorialStep(3, 11, 12) |
| 1431 |
end |
| 1432 |
end |
| 1433 |
|
| 1434 |
function UIEditRoom:setWindowBlueprint(orig_x, orig_y, orig_wall) |
| 1435 |
local x = orig_x |
| 1436 |
local y = orig_y |
| 1437 |
local wall = orig_wall |
| 1438 |
|
| 1439 |
if wall == "south" then |
| 1440 |
y = y + 1 |
| 1441 |
wall = "north" |
| 1442 |
elseif wall == "east" then |
| 1443 |
x = x + 1 |
| 1444 |
wall = "west" |
| 1445 |
end |
| 1446 |
|
| 1447 |
local map = self.ui.app.map.th |
| 1448 |
local world = self.ui.app.world |
| 1449 |
|
| 1450 |
if self.blueprint_window.anim then |
| 1451 |
self.blueprint_window.anim:setAnimation(self.anims, self.blueprint_window.old_anim, |
| 1452 |
self.blueprint_window.old_flags) |
| 1453 |
self.blueprint_window.anim:setTag(nil) |
| 1454 |
self.blueprint_window.anim = nil |
| 1455 |
map:setCell(self.blueprint_window.floor_x, self.blueprint_window.floor_y, 4, 24) |
| 1456 |
end |
| 1457 |
|
| 1458 |
local anim = x and self.blueprint_wall_anims[x][y] |
| 1459 |
if anim and anim:getTag() then |
| 1460 |
x, y, wall = nil, nil, nil |
| 1461 |
orig_x, orig_y, orig_wall = nil, nil, nil |
| 1462 |
end |
| 1463 |
|
| 1464 |
self.blueprint_window.x = x |
| 1465 |
self.blueprint_window.y = y |
| 1466 |
self.blueprint_window.wall = wall |
| 1467 |
self.blueprint_window.floor_x = orig_x |
| 1468 |
self.blueprint_window.floor_y = orig_y |
| 1469 |
self.blueprint_window.valid = false |
| 1470 |
if not wall then |
| 1471 |
return |
| 1472 |
end |
| 1473 |
|
| 1474 |
if anim ~= self.blueprint_window.anim then |
| 1475 |
self.blueprint_window.anim = anim |
| 1476 |
self.blueprint_window.anim:setTag("window") |
| 1477 |
self.blueprint_window.old_anim = anim:getAnimation() |
| 1478 |
self.blueprint_window.old_flags = anim:getFlag() |
| 1479 |
end |
| 1480 |
self.blueprint_window.valid = true |
| 1481 |
local flags |
| 1482 |
if wall == "west" then |
| 1483 |
flags = 1 |
| 1484 |
if world:getWallIdFromBlockId(map:getCell(x, y, 3)) then |
| 1485 |
self.blueprint_window.valid = false |
| 1486 |
flags = flags + 16 |
| 1487 |
end |
| 1488 |
else--if wall == "north" then |
| 1489 |
flags = 0 |
| 1490 |
if world:getWallIdFromBlockId(map:getCell(x, y, 2)) then |
| 1491 |
self.blueprint_window.valid = false |
| 1492 |
flags = flags + 16 |
| 1493 |
end |
| 1494 |
end |
| 1495 |
anim:setAnimation(self.anims, 130, flags) |
| 1496 |
if self.blueprint_window.valid then |
| 1497 |
map:setCell(self.blueprint_window.floor_x, self.blueprint_window.floor_y, 4, |
| 1498 |
window_floor_blueprint_markers[orig_wall]) |
| 1499 |
end |
| 1500 |
end |
| 1501 |
|
| 1502 |
function UIEditRoom:onCursorWorldPositionChange(x, y) |
| 1503 |
local repaint = UIPlaceObjects.onCursorWorldPositionChange(self, x, y) |
| 1504 |
|
| 1505 |
local ui = self.ui |
| 1506 |
|
| 1507 |
-- Is the game paused? |
| 1508 |
if not self.world.user_actions_allowed or self.confirm_dialog_open then |
| 1509 |
ui:setCursor(ui.default_cursor) |
| 1510 |
return |
| 1511 |
end |
| 1512 |
local wx, wy = ui:ScreenToWorld(self.x + x, self.y + y) |
| 1513 |
wx = math.floor(wx) |
| 1514 |
wy = math.floor(wy) |
| 1515 |
|
| 1516 |
if self.phase == "walls" then |
| 1517 |
local rect = self.blueprint_rect |
| 1518 |
if not self.mouse_down_x then |
| 1519 |
if wx > rect.x and wx < rect.x + rect.w - 1 and wy > rect.y and wy < rect.y + rect.h - 1 then |
| 1520 |
-- inside blueprint, non-border -> move blueprint |
| 1521 |
ui:setCursor(ui.app.gfx:loadMainCursor("move_room")) |
| 1522 |
self.move_rect = true |
| 1523 |
self.resize_rect = false |
| 1524 |
elseif wx < rect.x or wx >= rect.x + rect.w or wy < rect.y or wy >= rect.y + rect.h then |
| 1525 |
-- outside blueprint |
| 1526 |
ui:setCursor(ui.app.gfx:loadMainCursor("resize_room")) |
| 1527 |
self.move_rect = false |
| 1528 |
self.resize_rect = false |
| 1529 |
else |
| 1530 |
-- inside blueprint, at border -> resize blueprint |
| 1531 |
self.move_rect = false |
| 1532 |
self.resize_rect = { |
| 1533 |
n = (wy == rect.y), |
| 1534 |
s = (wy == rect.y + rect.h - 1) and (wy ~= rect.y), |
| 1535 |
w = (wx == rect.x), |
| 1536 |
e = (wx == rect.x + rect.w - 1) and (wx ~= rect.x), |
| 1537 |
} |
| 1538 |
|
| 1539 |
if (self.resize_rect.w or self.resize_rect.e) and (self.resize_rect.n or self.resize_rect.s) then |
| 1540 |
ui:setCursor(ui.app.gfx:loadMainCursor("nswe_arrow")) |
| 1541 |
elseif self.resize_rect.w or self.resize_rect.e then |
| 1542 |
ui:setCursor(ui.app.gfx:loadMainCursor("we_arrow")) |
| 1543 |
else |
| 1544 |
ui:setCursor(ui.app.gfx:loadMainCursor("ns_arrow")) |
| 1545 |
end |
| 1546 |
end |
| 1547 |
end |
| 1548 |
else |
| 1549 |
if self.phase ~= "clear_area" and self.phase ~= "objects" then |
| 1550 |
ui:setCursor(ui.app.gfx:loadMainCursor("resize_room")) |
| 1551 |
end |
| 1552 |
local cell_x, cell_y, wall = self:screenToWall(self.x + x, self.y + y) |
| 1553 |
if self.phase == "door" then |
| 1554 |
self:setDoorBlueprint(cell_x, cell_y, wall) |
| 1555 |
elseif self.phase == "windows" then |
| 1556 |
self:setWindowBlueprint(cell_x, cell_y, wall) |
| 1557 |
end |
| 1558 |
end |
| 1559 |
|
| 1560 |
if self.mouse_down_x and self.move_rect then |
| 1561 |
local rect = self.blueprint_rect |
| 1562 |
self:setBlueprintRect(wx - self.move_rect_x, wy - self.move_rect_y, rect.w, rect.h) |
| 1563 |
elseif self.mouse_down_x and self.resize_rect then |
| 1564 |
local rect = self.blueprint_rect |
| 1565 |
local x1, y1, x2, y2 = rect.x, rect.y, rect.x + rect.w - 1, rect.y + rect.h - 1 |
| 1566 |
if self.resize_rect.w then |
| 1567 |
x1 = wx |
| 1568 |
elseif self.resize_rect.e then |
| 1569 |
x2 = wx |
| 1570 |
end |
| 1571 |
if self.resize_rect.n then |
| 1572 |
y1 = wy |
| 1573 |
elseif self.resize_rect.s then |
| 1574 |
y2 = wy |
| 1575 |
end |
| 1576 |
|
| 1577 |
if x1 > x2 then |
| 1578 |
x1, x2 = x2, x1 |
| 1579 |
self.resize_rect.w, self.resize_rect.e = self.resize_rect.e, self.resize_rect.w |
| 1580 |
end |
| 1581 |
if y1 > y2 then |
| 1582 |
y1, y2 = y2, y1 |
| 1583 |
self.resize_rect.n, self.resize_rect.s = self.resize_rect.s, self.resize_rect.n |
| 1584 |
end |
| 1585 |
self:setBlueprintRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1) |
| 1586 |
elseif self.mouse_down_x then |
| 1587 |
local x1, x2 = self.mouse_down_x, wx |
| 1588 |
local y1, y2 = self.mouse_down_y, wy |
| 1589 |
if x1 > x2 then x1, x2 = x2, x1 end |
| 1590 |
if y1 > y2 then y1, y2 = y2, y1 end |
| 1591 |
self:setBlueprintRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1) |
| 1592 |
end |
| 1593 |
|
| 1594 |
if wx ~= self.mouse_cell_x or wy ~= self.mouse_cell_y then |
| 1595 |
repaint = true |
| 1596 |
end |
| 1597 |
self.mouse_cell_x = wx |
| 1598 |
self.mouse_cell_y = wy |
| 1599 |
|
| 1600 |
return repaint |
| 1601 |
end |
| 1602 |
|
| 1603 |
-- checks if all required objects are placed, and enables/disables the confirm button accordingly. |
| 1604 |
-- also returns the new state of the confirm button |
| 1605 |
function UIEditRoom:checkEnableConfirm() |
| 1606 |
local needed = {} -- copy list of required objects |
| 1607 |
for k, v in pairs(self.room.room_info.objects_needed) do |
| 1608 |
needed[k] = v |
| 1609 |
end |
| 1610 |
|
| 1611 |
-- subtract existing objects from the required numbers |
| 1612 |
for o in pairs(self.room.objects) do |
| 1613 |
local id = o.object_type.id |
| 1614 |
if needed[id] then |
| 1615 |
needed[id] = needed[id] - 1 |
| 1616 |
if needed[id] == 0 then |
| 1617 |
needed[id] = nil |
| 1618 |
end |
| 1619 |
end |
| 1620 |
end |
| 1621 |
|
| 1622 |
-- disable if there are not fulfilled requirements |
| 1623 |
local confirm = not next(needed) |
| 1624 |
|
| 1625 |
if confirm then |
| 1626 |
self.ui:tutorialStep(3, {13, 14}, 15) |
| 1627 |
else |
| 1628 |
self.ui:tutorialStep(3, {14, 15}, 13) |
| 1629 |
end |
| 1630 |
|
| 1631 |
self.confirm_button:enable(confirm) |
| 1632 |
return confirm |
| 1633 |
end |
| 1634 |
|
| 1635 |
function UIEditRoom:placeObject() |
| 1636 |
local obj = UIPlaceObjects.placeObject(self, true) |
| 1637 |
if obj then |
| 1638 |
self:checkEnableConfirm() |
| 1639 |
end |
| 1640 |
end |
| 1641 |
|
| 1642 |
function UIEditRoom:afterLoad(old, new) |
| 1643 |
if old < 172 then |
| 1644 |
-- reverts change in 171 where walls were moved into edit_room, its original |
| 1645 |
-- afterload was removed due to crashes |
| 1646 |
self.wall_types = nil |
| 1647 |
self.wall_id_by_block_id = nil |
| 1648 |
self.wall_set_by_block_id = nil |
| 1649 |
self.wall_dir_by_block_id = nil |
| 1650 |
end |
| 1651 |
|
| 1652 |
UIPlaceObjects.afterLoad(self, old, new) |
| 1653 |
self:registerKeyHandlers() |
| 1654 |
end |