| 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 TH = require("TH") |
| 22 |
local ipairs, math_floor |
| 23 |
= ipairs, math.floor |
| 24 |
|
| 25 |
-- Visually, it looks better to have the object being placed not attached to a |
| 26 |
-- tile (so that it is always on top of walls, etc.), but for debugging it can |
| 27 |
-- be useful to attach it to a tile. |
| 28 |
local ATTACH_BLUEPRINT_TO_TILE = false |
| 29 |
|
| 30 |
--! The dialog shown when placing objects. |
| 31 |
class "UIPlaceObjects" (Window) |
| 32 |
|
| 33 |
---@type UIPlaceObjects |
| 34 |
local UIPlaceObjects = _G["UIPlaceObjects"] |
| 35 |
|
| 36 |
--[[ Constructor for the class. |
| 37 |
!param ui (UI) The active ui. |
| 38 |
!param object_list (table) a list of tables with objects to place. Keys are "object", "qty" and |
| 39 |
"existing_object". The first is the object_type of the object, the second how many, and if the key |
| 40 |
"existing_object" is set it should be an already existing object that is about to be moved. |
| 41 |
In particular, if that object has a variable called current_frame then that frame will be used |
| 42 |
when drawing the object as it is being moved. |
| 43 |
]] |
| 44 |
function UIPlaceObjects:UIPlaceObjects(ui, object_list, pay_for) |
| 45 |
self:Window() |
| 46 |
|
| 47 |
object_list = object_list or {} -- Default argument |
| 48 |
|
| 49 |
local app = ui.app |
| 50 |
self.modal_class = "main" |
| 51 |
self.ui = ui |
| 52 |
self.map = app.map |
| 53 |
self.anims = app.anims |
| 54 |
self.world = app.world |
| 55 |
self.width = 186 |
| 56 |
self.height = 167 + #object_list * 29 |
| 57 |
self:setDefaultPosition(0.9, 0.1) |
| 58 |
local selected_label_color = { red = 40, green = 40, blue = 250 } |
| 59 |
self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req05V", true) |
| 60 |
self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 61 |
self.blue_font = app.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true }) |
| 62 |
self.title_text = _S.rooms_short.corridor_objects |
| 63 |
self.desc_text = _S.place_objects_window.place_objects_in_corridor |
| 64 |
|
| 65 |
self:addPanel(112, 0, 0) -- Dialog header |
| 66 |
for y = 48, 83, 7 do |
| 67 |
self:addPanel(113, 0, y) -- Desc text box |
| 68 |
end |
| 69 |
self:addPanel(114, 0, 90) -- Dialog mid-piece |
| 70 |
self:addPanel(115, 0, 100):makeButton(9, 8, 41, 42, 116, self.cancel):setSound("no4.wav"):setTooltip(_S.tooltip.place_objects_window.cancel) |
| 71 |
self.purchase_button = |
| 72 |
self:addPanel(117, 50, 100):makeButton(1, 8, 41, 42, 118, self.purchaseItems):setTooltip(_S.tooltip.place_objects_window.buy_sell) |
| 73 |
:setDisabledSprite(127):setSound("selectx.wav"):enable(false) -- Disabled purchase items button |
| 74 |
self.pickup_button = |
| 75 |
self:addPanel(119, 92, 100):makeButton(1, 8, 41, 42, 120, self.pickupItems):setTooltip(_S.tooltip.place_objects_window.pick_up) |
| 76 |
:setDisabledSprite(128):setSound("selectx.wav"):enable(false):makeToggle() -- Disabled pick up items button |
| 77 |
self.confirm_button = |
| 78 |
self:addPanel(121, 134, 100):makeButton(1, 8, 43, 42, 122, self.confirm):setTooltip(_S.tooltip.place_objects_window.confirm) |
| 79 |
:setDisabledSprite(129):enable(false):setSound("YesX.wav") -- Disabled confirm button |
| 80 |
|
| 81 |
self.list_header = self:addPanel(123, 0, 146) -- Object list header |
| 82 |
self.list_header.visible = false |
| 83 |
|
| 84 |
self.objects = {} |
| 85 |
self.object_footprint = {} |
| 86 |
self.num_slots = 0 |
| 87 |
|
| 88 |
self:addObjects(object_list, pay_for) |
| 89 |
ui:setWorldHitTest(false) |
| 90 |
UIPlaceObjects.registerKeyHandlers(self) |
| 91 |
end |
| 92 |
|
| 93 |
function UIPlaceObjects:registerKeyHandlers() |
| 94 |
self:addKeyHandler("global_cancel", self.undo) |
| 95 |
self:addKeyHandler("global_cancel_alt", self.cancel) |
| 96 |
self:addKeyHandler("ingame_sellPickedUpItem", self.sell) |
| 97 |
self:addKeyHandler("ingame_rotateobject", self.tryNextOrientation) |
| 98 |
end |
| 99 |
|
| 100 |
-- changes the window size and buttons to num_slots slots |
| 101 |
function UIPlaceObjects:resize(num_slots) |
| 102 |
if self.num_slots == num_slots then |
| 103 |
return |
| 104 |
end |
| 105 |
|
| 106 |
if num_slots == 0 then |
| 107 |
self.list_header.visible = false |
| 108 |
else |
| 109 |
self.list_header.visible = true |
| 110 |
end |
| 111 |
|
| 112 |
local function idx(i) |
| 113 |
return --[[persistable:place_objects_idx1]] function(window) |
| 114 |
if i == window.active_index then |
| 115 |
window:nextOrientation() |
| 116 |
else |
| 117 |
window.place_objects = true |
| 118 |
window:setActiveIndex(i) |
| 119 |
-- Stop picking up items when user presses object in list |
| 120 |
local edit_room_window = window.ui:getWindow(UIEditRoom) |
| 121 |
if edit_room_window and edit_room_window.in_pickup_mode then |
| 122 |
edit_room_window:stopPickupItems() |
| 123 |
end |
| 124 |
end |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
if self.num_slots < num_slots then |
| 129 |
-- change last panel |
| 130 |
if self.num_slots > 0 then |
| 131 |
local last_panel = self.panels[#self.panels - 1] |
| 132 |
last_panel.y = last_panel.y + 4 |
| 133 |
last_panel.sprite_index = 124 |
| 134 |
end |
| 135 |
|
| 136 |
-- add new panels (save last one) |
| 137 |
for i = self.num_slots + 1, num_slots - 1 do |
| 138 |
self:addPanel(124, 0, 121 + i * 29) |
| 139 |
:makeButton(15, 8, 130, 23, 125, idx(i)):setSound("selectx.wav") |
| 140 |
:preservePanel() |
| 141 |
end |
| 142 |
-- add last new panel |
| 143 |
self:addPanel(156, 0, 117 + num_slots * 29) |
| 144 |
:makeButton(15, 12, 130, 23, 125, idx(num_slots)):setSound("selectx.wav") |
| 145 |
:preservePanel() |
| 146 |
else |
| 147 |
-- remove buttons |
| 148 |
for _ = self.num_slots, num_slots + 1, -1 do |
| 149 |
-- NB: Two panels per item, the latter being a dummy for the button |
| 150 |
self.panels[#self.panels] = nil |
| 151 |
self.panels[#self.panels] = nil |
| 152 |
self.buttons[#self.buttons] = nil |
| 153 |
if num_slots > 0 then |
| 154 |
-- change appearance of last panel |
| 155 |
local last_panel = self.panels[#self.panels - 1] |
| 156 |
last_panel.y = last_panel.y - 4 |
| 157 |
last_panel.sprite_index = 156 |
| 158 |
end |
| 159 |
end |
| 160 |
end |
| 161 |
self.num_slots = num_slots |
| 162 |
self.height = 167 + (num_slots) * 29 |
| 163 |
end |
| 164 |
|
| 165 |
function UIPlaceObjects:addObjects(object_list, pay_for) |
| 166 |
self.visible = true -- even if no objects are to be placed, make the window visible again |
| 167 |
|
| 168 |
if not object_list then |
| 169 |
object_list = {} |
| 170 |
end |
| 171 |
|
| 172 |
if #object_list == 0 and #self.objects == 0 then |
| 173 |
return |
| 174 |
end |
| 175 |
|
| 176 |
-- Detect objects already existing in self.objects and increment its quantity |
| 177 |
-- rather than adding new objects lines. |
| 178 |
local new_index = 1 |
| 179 |
while true do |
| 180 |
local new_object = object_list[new_index] |
| 181 |
if not new_object then break end |
| 182 |
for _, object in ipairs(self.objects) do |
| 183 |
if new_object.qty > 0 and new_object.object.thob == object.object.thob then |
| 184 |
object.qty = object.qty + new_object.qty |
| 185 |
if pay_for then |
| 186 |
local build_cost = self.ui.hospital:getObjectBuildCost(new_object.object.id) |
| 187 |
local msg = _S.transactions.buy_object .. ": " .. object.object.name |
| 188 |
self.ui.hospital:spendMoney(new_object.qty * build_cost, msg, new_object.qty * build_cost) |
| 189 |
end |
| 190 |
-- If this is an object that has been created in the world already, add it to the |
| 191 |
-- associated list of objects to re-place. |
| 192 |
if new_object.existing_object then |
| 193 |
if not object.existing_objects then |
| 194 |
object.existing_objects = {} |
| 195 |
end |
| 196 |
-- Insert the new object in the beginning of the list so that that this object |
| 197 |
-- is the one to be placed first. (LIFO) |
| 198 |
table.insert(object.existing_objects, 1, new_object.existing_object) |
| 199 |
end |
| 200 |
table.remove(object_list, new_index) |
| 201 |
new_index = new_index - 1 |
| 202 |
break |
| 203 |
end |
| 204 |
end |
| 205 |
new_index = new_index + 1 |
| 206 |
end |
| 207 |
|
| 208 |
self.place_objects = true -- When adding objects guess we want to place objects |
| 209 |
|
| 210 |
self.object_anim = TH.animation() |
| 211 |
self.object_slave_anim = TH.animation() |
| 212 |
local total_objects = #self.objects + #object_list |
| 213 |
self:resize(total_objects) |
| 214 |
|
| 215 |
for _, object in pairs(object_list) do |
| 216 |
if object.existing_object then |
| 217 |
object.existing_objects = {object.existing_object} |
| 218 |
end |
| 219 |
self.objects[#self.objects + 1] = object |
| 220 |
|
| 221 |
if pay_for then |
| 222 |
local build_cost = self.ui.hospital:getObjectBuildCost(object.object.id) |
| 223 |
local msg = _S.transactions.buy_object .. ": " .. object.object.name |
| 224 |
self.ui.hospital:spendMoney(object.qty * build_cost, msg, object.qty * build_cost) |
| 225 |
end |
| 226 |
end |
| 227 |
|
| 228 |
-- sort list by size of object (number of tiles in the first existing orientation (usually north)) |
| 229 |
table.sort(self.objects, function(o1, o2) |
| 230 |
local orient1 = o1.object.orientations.north or o1.object.orientations.east or |
| 231 |
o1.object.orientations.south or o1.object.orientations.west |
| 232 |
local orient2 = o2.object.orientations.north or o2.object.orientations.east or |
| 233 |
o2.object.orientations.south or o2.object.orientations.west |
| 234 |
return #orient1.footprint > #orient2.footprint |
| 235 |
end) |
| 236 |
|
| 237 |
self.active_index = 0 -- avoid case of index changing from 1 to 1 |
| 238 |
self.active_hover_index = 0 |
| 239 |
self.hover_sound = nil |
| 240 |
self:setActiveIndex(1) |
| 241 |
self:onCursorWorldPositionChange(self.ui:getCursorPosition(self)) |
| 242 |
end |
| 243 |
|
| 244 |
-- precondition: self.active_index has to correspond to the object to be removed |
| 245 |
function UIPlaceObjects:removeObject(object, dont_close_if_empty, placed, move_canceled) |
| 246 |
local existing_object = object.existing_object |
| 247 |
|
| 248 |
local move_cancellation = not placed and existing_object and move_canceled |
| 249 |
local purchase_cancellation = not placed and not existing_object |
| 250 |
local sale_previously_placed = not placed and existing_object and not move_canceled |
| 251 |
|
| 252 |
if purchase_cancellation or sale_previously_placed then |
| 253 |
-- Object sell. |
| 254 |
-- The player should be reimbursed for the cost of the item, i.e. |
| 255 |
-- whether they were previously purchased. So do refund. |
| 256 |
local build_cost = self.ui.hospital:getObjectBuildCost(object.object.id) |
| 257 |
local msg = _S.transactions.sell_object .. ": " .. object.object.name |
| 258 |
self.ui.hospital:receiveMoney(build_cost, msg, build_cost) |
| 259 |
end |
| 260 |
|
| 261 |
if move_cancellation then |
| 262 |
-- Move cancelled. Place object back. Restore coordinate and orientation. |
| 263 |
self.object_cell_x = object.existing_object.tile_x |
| 264 |
self.object_cell_y = object.existing_object.tile_y |
| 265 |
self.object_orientation = object.existing_object.direction |
| 266 |
self:placeObject(nil, true) |
| 267 |
end |
| 268 |
|
| 269 |
if sale_previously_placed then |
| 270 |
-- Previously grabbed an existing object so the object was not deleted |
| 271 |
-- from its previous location, but simply made invisible. |
| 272 |
self.world:destroyEntity(object.existing_object) |
| 273 |
end |
| 274 |
|
| 275 |
object.qty = object.qty - 1 |
| 276 |
-- Prefer to remove objects not yet placed |
| 277 |
local existing_no = object.existing_objects and #object.existing_objects or 0 |
| 278 |
if existing_no > 0 then |
| 279 |
if object.qty < #object.existing_objects then |
| 280 |
-- The object is already as good as destroyed. It is only known in this list. |
| 281 |
table.remove(object.existing_objects, 1) |
| 282 |
end |
| 283 |
end |
| 284 |
if object.qty == 0 then |
| 285 |
if #self.objects == 1 then |
| 286 |
self:clearBlueprint() |
| 287 |
self.object_cell_x = nil |
| 288 |
self.object_cell_y = nil |
| 289 |
if dont_close_if_empty then |
| 290 |
self.list_header.visible = false |
| 291 |
self.place_objects = false -- No object to place |
| 292 |
else |
| 293 |
self:close(false) |
| 294 |
return |
| 295 |
end |
| 296 |
end |
| 297 |
local idx = self.active_index |
| 298 |
table.remove(self.objects, idx) |
| 299 |
self:resize(#self.objects) |
| 300 |
self.active_index = 0 -- avoid case of index changing from 1 to 1 |
| 301 |
self.active_hover_index = 0 |
| 302 |
self:setActiveIndex(1) |
| 303 |
else |
| 304 |
-- Make sure the correct frame is shown for the next object |
| 305 |
self:setOrientation(self.object_orientation) |
| 306 |
end |
| 307 |
-- Update blueprint |
| 308 |
self:setBlueprintCell(self.object_cell_x, self.object_cell_y) |
| 309 |
if object.object.id == "reception_desk" then -- Rebuild cache of reception desks |
| 310 |
self.ui.hospital:buildReceptionDesksCache() |
| 311 |
end |
| 312 |
end |
| 313 |
|
| 314 |
--! Remove all items from the menu. |
| 315 |
--!param move_canceled (bool) moving objects has been cancelled. |
| 316 |
function UIPlaceObjects:removeAllObjects(move_canceled) |
| 317 |
-- There is surely a nicer way to implement this than the current hack. Rewrite it sometime later. |
| 318 |
self:setActiveIndex(1) |
| 319 |
for _ = 1, #self.objects do |
| 320 |
for _ = 1, self.objects[1].qty do |
| 321 |
self:removeObject(self.objects[1], true, false, move_canceled) |
| 322 |
end |
| 323 |
end |
| 324 |
end |
| 325 |
|
| 326 |
function UIPlaceObjects:removeObjects(object_list) |
| 327 |
-- rewrite at some point.. |
| 328 |
if not object_list then |
| 329 |
object_list = {} |
| 330 |
end |
| 331 |
|
| 332 |
for _, o in ipairs(object_list) do |
| 333 |
for j, p in ipairs(self.objects) do |
| 334 |
if o.object.id == p.object.id then |
| 335 |
self.active_index = j |
| 336 |
for _ = 1, o.qty do |
| 337 |
self:removeObject(p, true, false) |
| 338 |
end |
| 339 |
end |
| 340 |
end |
| 341 |
end |
| 342 |
end |
| 343 |
|
| 344 |
--! Close menu. |
| 345 |
--!param move_canceled (bool) moving objects has been cancelled. |
| 346 |
function UIPlaceObjects:close(move_canceled) |
| 347 |
self.ui:tutorialStep(1, {4, 5}, 1) |
| 348 |
self:removeAllObjects(move_canceled) |
| 349 |
self:clearBlueprint() |
| 350 |
self.ui:setWorldHitTest(true) |
| 351 |
return Window.close(self) |
| 352 |
end |
| 353 |
|
| 354 |
function UIPlaceObjects:cancel() |
| 355 |
self:close(false) |
| 356 |
end |
| 357 |
|
| 358 |
--! Helper function to stop UIEditRoom going back to doors phase |
| 359 |
function UIPlaceObjects:sell() |
| 360 |
if not self.ui:getWindow(UIEditRoom) then |
| 361 |
self:close(false) |
| 362 |
end |
| 363 |
end |
| 364 |
|
| 365 |
function UIPlaceObjects:undo() |
| 366 |
self:close(true) |
| 367 |
end |
| 368 |
|
| 369 |
function UIPlaceObjects:setActiveIndex(index) |
| 370 |
if index == self.active_index or #self.objects == 0 then |
| 371 |
return |
| 372 |
end |
| 373 |
self.active_index = index |
| 374 |
|
| 375 |
local object = self.objects[self.active_index].object |
| 376 |
if object.id == "reception_desk" then |
| 377 |
self.ui:tutorialStep(1, 6, 4) |
| 378 |
else |
| 379 |
self.ui:tutorialStep(1, {4, 5}, 6) |
| 380 |
end |
| 381 |
local anims = self.anims |
| 382 |
local grey_scale = anims.Alt32_GreyScale |
| 383 |
local _, ghost = self.ui.app.gfx:getPalette("MPalette.dat") |
| 384 |
for _, anim in pairs(object.idle_animations) do |
| 385 |
anims:setAnimationGhostPalette(anim, ghost, grey_scale) |
| 386 |
end |
| 387 |
if object.slave_type then |
| 388 |
for _, anim in pairs(object.slave_type.idle_animations) do |
| 389 |
anims:setAnimationGhostPalette(anim, ghost, grey_scale) |
| 390 |
end |
| 391 |
end |
| 392 |
|
| 393 |
if object.locked_to_wall then |
| 394 |
local wx, wy, wo = self:calculateBestPlacementPosition(0, 0) |
| 395 |
self.object_cell_x, self.object_cell_y = wx, wy |
| 396 |
self:setOrientation(wo) |
| 397 |
else |
| 398 |
self.object_orientation = "west" |
| 399 |
self:nextOrientation() |
| 400 |
end |
| 401 |
end |
| 402 |
|
| 403 |
local orient_mirror = { |
| 404 |
north = "west", |
| 405 |
west = "north", |
| 406 |
east = "south", |
| 407 |
south = "east", |
| 408 |
} |
| 409 |
|
| 410 |
local orient_next = { |
| 411 |
north = "east", |
| 412 |
east = "south", |
| 413 |
south = "west", |
| 414 |
west = "north", |
| 415 |
} |
| 416 |
|
| 417 |
function UIPlaceObjects:setOrientation(orient) |
| 418 |
self.object_orientation = orient |
| 419 |
|
| 420 |
local object_data = self.objects[self.active_index] |
| 421 |
local object = object_data.object |
| 422 |
local anim = object.idle_animations[orient] |
| 423 |
local flag = 0 |
| 424 |
if not anim then |
| 425 |
anim = object.idle_animations[orient_mirror[orient]] |
| 426 |
flag = 1 |
| 427 |
end |
| 428 |
if object.orientations[orient].early_list then |
| 429 |
flag = flag + 1024 |
| 430 |
end |
| 431 |
self.object_anim:setAnimation(self.anims, anim, flag) |
| 432 |
if object.slave_type then |
| 433 |
local slave_flag = flag - (flag % 2) |
| 434 |
local slave_anim = object.slave_type.idle_animations[orient] |
| 435 |
if not slave_anim then |
| 436 |
slave_anim = object.slave_type.idle_animations[orient_mirror[orient]] |
| 437 |
slave_flag = slave_flag + 1 |
| 438 |
end |
| 439 |
self.object_slave_anim:setAnimation(self.anims, slave_anim, slave_flag) |
| 440 |
end |
| 441 |
local present_object |
| 442 |
if object_data.existing_objects and #object_data.existing_objects > 0 then |
| 443 |
present_object = object_data.existing_objects[1] |
| 444 |
end |
| 445 |
if present_object and present_object.current_frame then |
| 446 |
self.object_anim:setFrame(present_object.current_frame) |
| 447 |
end |
| 448 |
local px, py = unpack(object.orientations[orient].render_attach_position) |
| 449 |
if type(px) == "table" then |
| 450 |
px, py = unpack(px) |
| 451 |
end |
| 452 |
px, py = Map:WorldToScreen(px + 1, py + 1) |
| 453 |
px = object.orientations[orient].animation_offset[1] + px |
| 454 |
py = object.orientations[orient].animation_offset[2] + py |
| 455 |
self.object_anim:setPosition(px, py) |
| 456 |
if object.slave_type then |
| 457 |
px, py = unpack(object.slave_type.orientations[orient].render_attach_position) |
| 458 |
if type(px) == "table" then |
| 459 |
px, py = unpack(px) |
| 460 |
end |
| 461 |
local offset = object.orientations[orient].slave_position |
| 462 |
if offset then |
| 463 |
px = px + offset[1] |
| 464 |
py = py + offset[2] |
| 465 |
end |
| 466 |
px, py = Map:WorldToScreen(px + 1, py + 1) |
| 467 |
px = object.slave_type.orientations[orient].animation_offset[1] + px |
| 468 |
py = object.slave_type.orientations[orient].animation_offset[2] + py |
| 469 |
self.object_slave_anim:setPosition(px, py) |
| 470 |
end |
| 471 |
self:setBlueprintCell(self.object_cell_x, self.object_cell_y) |
| 472 |
end |
| 473 |
|
| 474 |
function UIPlaceObjects:nextOrientation() |
| 475 |
if not self.object_anim then |
| 476 |
return |
| 477 |
end |
| 478 |
local object = self.objects[self.active_index].object |
| 479 |
if object.locked_to_wall then |
| 480 |
-- Orientation is dictated by the nearest wall |
| 481 |
return |
| 482 |
end |
| 483 |
local orient = self.object_orientation |
| 484 |
repeat |
| 485 |
orient = orient_next[orient] |
| 486 |
until object.orientations[orient] |
| 487 |
self:setOrientation(orient) |
| 488 |
end |
| 489 |
|
| 490 |
function UIPlaceObjects:tryNextOrientation() |
| 491 |
if #self.objects > 0 then |
| 492 |
self.ui:playSound("swoosh.wav") |
| 493 |
self.objects[self.active_index].orientation_before = self.object_orientation |
| 494 |
self:nextOrientation() |
| 495 |
end |
| 496 |
end |
| 497 |
|
| 498 |
function UIPlaceObjects:onMouseUp(button, x, y) |
| 499 |
local repaint = Window.onMouseUp(self, button, x, y) |
| 500 |
|
| 501 |
-- We don't want to place objects because we are selecting new objects for adding in a room being built/edited |
| 502 |
-- Or the game is paused. |
| 503 |
if not self.place_objects or not self.world.user_actions_allowed then |
| 504 |
return |
| 505 |
end |
| 506 |
|
| 507 |
if button == "right" then |
| 508 |
self:tryNextOrientation() |
| 509 |
repaint = true |
| 510 |
elseif button == "left" then |
| 511 |
if #self.objects > 0 then |
| 512 |
local s = TheApp.config.ui_scale |
| 513 |
if 0 <= x and x < self.width * s and 0 <= y and y < self.height * s then -- luacheck: ignore 542 |
| 514 |
-- Click within window - do nothing |
| 515 |
elseif self.object_cell_x and self.object_cell_y then |
| 516 |
if self.object_blueprint_good then |
| 517 |
if not self.world:anyHumanoidObscuringArea(self.object_cell_x, self.object_cell_y) then |
| 518 |
self:placeObject() |
| 519 |
repaint = true |
| 520 |
end |
| 521 |
else |
| 522 |
self.ui:tutorialStep(3, {13, 15}, 14) |
| 523 |
end |
| 524 |
end |
| 525 |
end |
| 526 |
end |
| 527 |
|
| 528 |
return repaint |
| 529 |
end |
| 530 |
|
| 531 |
function UIPlaceObjects:placeObject(dont_close_if_empty, dont_remove) |
| 532 |
if not self.place_objects then |
| 533 |
-- We don't want to place objects because we are selecting new objects for adding in a room being built/edited |
| 534 |
return |
| 535 |
end |
| 536 |
|
| 537 |
local object = self.objects[self.active_index] |
| 538 |
if object.object.id == "reception_desk" then self.ui:tutorialStep(1, 4, "next") end |
| 539 |
|
| 540 |
local real_obj |
| 541 |
-- There might be an existing object that has been picked up. |
| 542 |
if object.existing_objects and #object.existing_objects > 0 then |
| 543 |
real_obj = object.existing_objects[1] |
| 544 |
table.remove(object.existing_objects, 1) |
| 545 |
end |
| 546 |
local room = self.room or self.world:getRoom(self.object_cell_x, self.object_cell_y) |
| 547 |
if real_obj then |
| 548 |
-- If there is such an object then we don't want to make a new one, but move this one instead. |
| 549 |
if real_obj.picked_up and not real_obj.th:isVisible() then |
| 550 |
-- Previously grabbed an existing object so the object was not actually |
| 551 |
-- deleted from its previous location, but simply made invisible. |
| 552 |
real_obj:setTile(nil) |
| 553 |
real_obj:setInvisible(false) |
| 554 |
real_obj:rebuildPassableCellFlags() |
| 555 |
end |
| 556 |
real_obj.picked_up = false |
| 557 |
|
| 558 |
if real_obj.orientation_before and real_obj.orientation_before ~= self.object_orientation then |
| 559 |
real_obj:initOrientation(self.object_orientation) |
| 560 |
end |
| 561 |
self.world:prepareFootprintTilesForBuild(real_obj.footprint, self.object_cell_x, self.object_cell_y) |
| 562 |
real_obj:setTile(self.object_cell_x, self.object_cell_y) |
| 563 |
self.world:objectPlaced(real_obj) |
| 564 |
if real_obj.slave then |
| 565 |
self.world:objectPlaced(real_obj.slave) |
| 566 |
end |
| 567 |
if real_obj:isMachine() then |
| 568 |
-- Machines may have some state like smoke and etc. Update it. |
| 569 |
real_obj:placed(room) |
| 570 |
end |
| 571 |
if class.is(real_obj, Machine) then |
| 572 |
real_obj:setHandymanRepairPosition(self.object_orientation) |
| 573 |
end |
| 574 |
else |
| 575 |
local object_footprint = object.object.orientations[self.object_orientation].footprint |
| 576 |
self.world:prepareFootprintTilesForBuild(object_footprint, self.object_cell_x, self.object_cell_y) |
| 577 |
real_obj = self.world:newObject(object.object.id, |
| 578 |
self.object_cell_x, self.object_cell_y, self.object_orientation) |
| 579 |
real_obj:setState(object.state) |
| 580 |
end |
| 581 |
|
| 582 |
if room then |
| 583 |
room.objects[real_obj] = true |
| 584 |
end |
| 585 |
|
| 586 |
self.ui:playSound("place_r.wav") |
| 587 |
|
| 588 |
if not dont_remove then |
| 589 |
self:removeObject(object, dont_close_if_empty, true, false) |
| 590 |
end |
| 591 |
object.orientation_before = nil |
| 592 |
|
| 593 |
return real_obj |
| 594 |
end |
| 595 |
|
| 596 |
function UIPlaceObjects:draw(canvas, x, y) |
| 597 |
if not self.visible then |
| 598 |
return -- Do nothing if dialog is not visible |
| 599 |
end |
| 600 |
-- Don't show the object if the game is paused |
| 601 |
if self.world.user_actions_allowed then |
| 602 |
if not ATTACH_BLUEPRINT_TO_TILE and self.object_cell_x and self.object_anim then |
| 603 |
local xpos, ypos = self.ui:WorldToScreen(self.object_cell_x, self.object_cell_y) |
| 604 |
local zoom = self.ui.zoom_factor |
| 605 |
if canvas:scale(zoom) then |
| 606 |
xpos = math.floor(xpos / zoom) |
| 607 |
ypos = math.floor(ypos / zoom) |
| 608 |
end |
| 609 |
self.object_anim:draw(canvas, xpos, ypos) |
| 610 |
if self.objects[self.active_index].object.slave_type then |
| 611 |
self.object_slave_anim:draw(canvas, xpos, ypos) |
| 612 |
end |
| 613 |
canvas:scale(1) |
| 614 |
end |
| 615 |
end |
| 616 |
|
| 617 |
Window.draw(self, canvas, x, y) |
| 618 |
|
| 619 |
local s = TheApp.config.ui_scale |
| 620 |
x, y = x + self.x * s, y + self.y * s |
| 621 |
self.white_font:draw(canvas, self.title_text, x + 17 * s, y + 21 * s, 153 * s, 0) |
| 622 |
self.white_font:drawWrapped(canvas, self.desc_text, x + 20 * s, y + 46 * s, 147 * s) |
| 623 |
|
| 624 |
for i, o in ipairs(self.objects) do |
| 625 |
local font = self.white_font |
| 626 |
local font_ypos = y + 136 * s + i * 29 * s |
| 627 |
|
| 628 |
if i == self.active_index then |
| 629 |
local frame_xpos = x + 20 * s |
| 630 |
local frame_ypos = y + 134 * s + i * 29 * s |
| 631 |
local frame_width = 119 * s |
| 632 |
local frame_height = 12 * s |
| 633 |
|
| 634 |
local red = canvas:mapRGB(221, 83, 0) |
| 635 |
canvas:drawRect(red, frame_xpos, frame_ypos, frame_width, s) |
| 636 |
canvas:drawRect(red, frame_xpos, frame_ypos + frame_height, frame_width + s, s) |
| 637 |
canvas:drawRect(red, frame_xpos, frame_ypos, s, frame_height) |
| 638 |
canvas:drawRect(red, frame_xpos + frame_width, frame_ypos, s, frame_height) |
| 639 |
end |
| 640 |
if i == self.active_hover_index then |
| 641 |
font = self.blue_font |
| 642 |
end |
| 643 |
font:draw(canvas, o.object.name, x + 15 * s, font_ypos, 130 * s, 0) |
| 644 |
font:draw(canvas, o.qty, x + 151 * s, font_ypos, 19 * s, 0) |
| 645 |
end |
| 646 |
end |
| 647 |
|
| 648 |
function UIPlaceObjects:onMouseMove(x, y, dx, dy) |
| 649 |
local s = TheApp.config.ui_scale |
| 650 |
local current_hover_id |
| 651 |
local header_height = 159 * s |
| 652 |
local bar_height = 29 * s |
| 653 |
local inside_objects_area = (x > 0 and x < 186 * s) and (y > header_height and y < header_height + bar_height * #self.objects) |
| 654 |
if inside_objects_area then |
| 655 |
-- Check if player hovers over a button, not between. |
| 656 |
if (header_height + y) % bar_height < 21 * s then |
| 657 |
current_hover_id = math_floor((y - header_height)/bar_height) + 1 |
| 658 |
if self.active_hover_index ~= current_hover_id then |
| 659 |
if self.hover_sound then |
| 660 |
self.ui:stopSound(self.hover_sound) |
| 661 |
end |
| 662 |
self.hover_sound = self.ui:playSound("Hlight5.wav") |
| 663 |
self.active_hover_index = current_hover_id |
| 664 |
end |
| 665 |
else |
| 666 |
self.active_hover_index = 0 |
| 667 |
end |
| 668 |
else |
| 669 |
self.active_hover_index = nil |
| 670 |
end |
| 671 |
return Window:onMouseMove(x, y, dx, dy) |
| 672 |
end |
| 673 |
|
| 674 |
function UIPlaceObjects:clearBlueprint() |
| 675 |
local map = self.map.th |
| 676 |
if self.object_anim then |
| 677 |
self.object_anim:setTile(nil) |
| 678 |
end |
| 679 |
if self.object_slave_anim then |
| 680 |
self.object_slave_anim:setTile(nil) |
| 681 |
end |
| 682 |
for _, xy in ipairs(self.object_footprint) do |
| 683 |
if xy[1] ~= 0 then |
| 684 |
map:setCell(xy[1], xy[2], 4, 0) |
| 685 |
end |
| 686 |
end |
| 687 |
end |
| 688 |
|
| 689 |
local flag_alpha75 = 256 * 8 |
| 690 |
local flag_altpal = 16 |
| 691 |
|
| 692 |
--! Method for displaying a blueprint when placing object by coordinates under the user cursor. |
| 693 |
--! Draws a blueprint and determines its color depending on whether the object can be placed |
| 694 |
--! at the given coordinates or not. A gray blueprint means placement is prohibited. |
| 695 |
--! A colored blueprint means placement is permitted. |
| 696 |
function UIPlaceObjects:setBlueprintCell(x, y) |
| 697 |
self:clearBlueprint() |
| 698 |
self.object_cell_x = x |
| 699 |
self.object_cell_y = y |
| 700 |
|
| 701 |
if not x or not y or #self.objects == 0 then |
| 702 |
self.object_footprint = {} |
| 703 |
return |
| 704 |
end |
| 705 |
|
| 706 |
local real_obj = self.objects[self.active_index] |
| 707 |
local object = real_obj.object |
| 708 |
local object_footprint = object.orientations[self.object_orientation].footprint |
| 709 |
local map = self.map.th |
| 710 |
if #object_footprint ~= #self.object_footprint then |
| 711 |
self.object_footprint = {} |
| 712 |
for i = 1, #object_footprint do |
| 713 |
self.object_footprint[i] = {} |
| 714 |
end |
| 715 |
end |
| 716 |
|
| 717 |
local room = self.room and self.room.id |
| 718 |
local valid_placement, room_id, passable_flag = self:_placementAvailableHereAndOtherObjectsDoNotInterfere( |
| 719 |
x, y, object, real_obj, object_footprint, room, map) |
| 720 |
|
| 721 |
if self.object_anim and object.class ~= "SideObject" then |
| 722 |
-- Not SideObject - object is occupying one or more tiles entirely |
| 723 |
-- (Drinks machine, plant, reception desk, room machines and etc). |
| 724 |
valid_placement = valid_placement and self:_isNonSideObjectPlacementValid(x, y, object, self.object_orientation, room_id) |
| 725 |
self:_setBlueprintPartialFlags(x, y, map, valid_placement) |
| 726 |
elseif object.class == "SideObject" then |
| 727 |
-- SideObject - an object that lives on the edge of a tile (radiator, bin, extinguisher). |
| 728 |
valid_placement = valid_placement and self:_isSideObjectPlacementValid(x, y, room_id, passable_flag, map) |
| 729 |
self:_setBlueprintPartialFlags(x, y, map, valid_placement) |
| 730 |
end |
| 731 |
end |
| 732 |
|
| 733 |
--! Function checks whether this space is available for placing an object and |
| 734 |
-- whether the space is not occupied by other objects. |
| 735 |
function UIPlaceObjects:_placementAvailableHereAndOtherObjectsDoNotInterfere( |
| 736 |
x, y, object, real_obj, object_footprint, room, map) |
| 737 |
local flags = {} |
| 738 |
local passable_flag |
| 739 |
local room_id = room |
| 740 |
local valid_placement = true |
| 741 |
local opt_tiles_blocked = 0 |
| 742 |
local world = self.ui.app.world |
| 743 |
local player_id = self.ui.hospital:getPlayerIndex() |
| 744 |
local direction = self.object_orientation |
| 745 |
local direction_parameters = Object.directionParameters() |
| 746 |
local optional_tiles = 0 |
| 747 |
for _, tile in ipairs(object_footprint) do |
| 748 |
if tile.optional then |
| 749 |
optional_tiles = optional_tiles + 1 |
| 750 |
end |
| 751 |
end |
| 752 |
|
| 753 |
-- flags for the case of moving a previously placed object to a new location |
| 754 |
local existing_object = real_obj.existing_object |
| 755 |
local same_placement = false -- same cell and same orientation |
| 756 |
local moving_existing_object = existing_object and existing_object.picked_up and |
| 757 |
not existing_object.th:isVisible() |
| 758 |
|
| 759 |
-- The given footprint tile is not usable, |
| 760 |
-- update the external 'valid_placement' variable accordingly. |
| 761 |
local function updateResult(xy) |
| 762 |
if xy.optional then |
| 763 |
opt_tiles_blocked = opt_tiles_blocked + 1 |
| 764 |
if opt_tiles_blocked >= optional_tiles then |
| 765 |
valid_placement = false |
| 766 |
end |
| 767 |
else |
| 768 |
valid_placement = false |
| 769 |
end |
| 770 |
end |
| 771 |
|
| 772 |
if moving_existing_object then |
| 773 |
-- moving a previously placed object to a new location |
| 774 |
local same_tile = existing_object.tile_x == x and existing_object.tile_y == y |
| 775 |
same_placement = same_tile and existing_object.direction == direction |
| 776 |
end |
| 777 |
|
| 778 |
if moving_existing_object and self.object_anim and object.class ~= "SideObject" then |
| 779 |
-- we placing previously 'grabbed' an existing object which actually just made invisible |
| 780 |
-- free up the space occupied by the object during the test for tiles availability |
| 781 |
existing_object:deoccupyTilesByObjectFootprintAt(existing_object.tile_x, existing_object.tile_y) |
| 782 |
end |
| 783 |
|
| 784 |
-- for each tile in object_footprint check map tile availability |
| 785 |
for i, tile in ipairs(object_footprint) do |
| 786 |
local xpos = x + tile[1] |
| 787 |
local ypos = y + tile[2] |
| 788 |
|
| 789 |
-- Check 1: Does the tile have valid map coordinates?: |
| 790 |
if not world:isOnMap(xpos, ypos) then |
| 791 |
updateResult(tile) |
| 792 |
xpos = 0 |
| 793 |
ypos = 0 |
| 794 |
else |
| 795 |
local flag = "buildable" |
| 796 |
local good_tile = 24 + flag_alpha75 |
| 797 |
local bad_tile = 67 + flag_alpha75 |
| 798 |
if tile.only_passable then |
| 799 |
flag = "passable" |
| 800 |
end |
| 801 |
if tile.only_side then |
| 802 |
-- 50 is a trash bin thob |
| 803 |
if object.thob == 50 and direction == "east" then |
| 804 |
direction = "west" |
| 805 |
end |
| 806 |
flag = direction_parameters[direction]["buildable_flag"] |
| 807 |
passable_flag = direction_parameters[direction]["passable_flag"] |
| 808 |
end |
| 809 |
|
| 810 |
-- Check 2: Is the tile in the object's allowed room?: |
| 811 |
local result = world:willObjectsFootprintTileBeWithinItsAllowedRoomIfLocatedAt(xpos, ypos, object, room_id) |
| 812 |
local is_object_allowed = result.within_room |
| 813 |
room_id = result.roomId |
| 814 |
|
| 815 |
-- Check 3: The footprint tile should either be buildable or passable, is it?: |
| 816 |
if not tile.only_side and is_object_allowed then |
| 817 |
is_object_allowed = |
| 818 |
world:isFootprintTileBuildableOrPassable(xpos, ypos, tile, object_footprint, flag, player_id) |
| 819 |
elseif is_object_allowed then |
| 820 |
is_object_allowed = (map:getCellFlags(xpos, ypos, flags)[flag] or same_placement) and |
| 821 |
(player_id == 0 or flags.owner == player_id) |
| 822 |
end |
| 823 |
|
| 824 |
-- ignore placed object tile if it is shareable |
| 825 |
if not tile.shareable and is_object_allowed then |
| 826 |
-- Check 4: only one object per tile allowed original TH |
| 827 |
-- can build on litter and unoccupied tiles and only placeable |
| 828 |
-- if not on another objects passable footprint unless that |
| 829 |
-- too is a shareable tile |
| 830 |
local objchk = map:getCellFlags(xpos, ypos, flags)["thob"] |
| 831 |
local objects_do_not_interfere = |
| 832 |
(objchk == 0) or (objchk == 62) or (objchk == 64) -- no other object, except litter/puke, ratholes |
| 833 |
local moving_existing_object_on_same_place = |
| 834 |
existing_object and (existing_object.tile_x == xpos) and (existing_object.tile_y == ypos) |
| 835 |
local tile_exclusively_passable = world:isTileExclusivelyPassable(xpos, ypos, 10) |
| 836 |
is_object_allowed = tile_exclusively_passable and |
| 837 |
(moving_existing_object_on_same_place or objects_do_not_interfere) |
| 838 |
end |
| 839 |
|
| 840 |
-- Having checked if the tile is good set its blueprint appearance flag: |
| 841 |
if is_object_allowed then |
| 842 |
if not tile.invisible then |
| 843 |
map:setCell(xpos, ypos, 4, good_tile) |
| 844 |
end |
| 845 |
else |
| 846 |
if not tile.invisible then |
| 847 |
map:setCell(xpos, ypos, 4, bad_tile) |
| 848 |
end |
| 849 |
updateResult(tile) |
| 850 |
end |
| 851 |
end |
| 852 |
self.object_footprint[i][1] = xpos |
| 853 |
self.object_footprint[i][2] = ypos |
| 854 |
end |
| 855 |
|
| 856 |
if moving_existing_object and self.object_anim and object.class ~= "SideObject" then |
| 857 |
-- we've tested the placement and now we need to re-occupy the cells. |
| 858 |
existing_object:occupyTilesByObjectFootprintAt(existing_object.tile_x, existing_object.tile_y) |
| 859 |
end |
| 860 |
|
| 861 |
return valid_placement, room_id, passable_flag |
| 862 |
end |
| 863 |
|
| 864 |
--! Function for checking the valid placement of "NonSideObject". |
| 865 |
--! param x (integer) target tile x coordinate. |
| 866 |
--! param y (integer) target tile y coordinate. |
| 867 |
--! param object (object) target SideObject. |
| 868 |
--! param object_orientation (string) footprint orientation name. |
| 869 |
--! param room_id (integer) room id if we in a build mode. |
| 870 |
--! return (bool) is this placement not going to break path finding. |
| 871 |
function UIPlaceObjects:_isNonSideObjectPlacementValid(x, y, object, object_orientation, room_id) |
| 872 |
local world = self.ui.app.world |
| 873 |
local invalid_placement |
| 874 |
|
| 875 |
if room_id > 0 and x and y then |
| 876 |
-- placing an object in a room |
| 877 |
local room = world:getRoom(x, y) |
| 878 |
if room and not room.crashed and room.door and room.door.tile_x and room.door.tile_y then |
| 879 |
-- a valid room with a door |
| 880 |
if self.ui.app.config.blocking_off_areas == 2 then |
| 881 |
-- soft placing approach |
| 882 |
if not room.is_active then |
| 883 |
-- user in a room editing mode |
| 884 |
local object_layout = object.orientations[object_orientation] |
| 885 |
invalid_placement = world:wouldNonSideObjectsNotHaveAccessToRoomDoor(x, y, room, object_layout) |
| 886 |
if not invalid_placement then |
| 887 |
-- also check that after placing the object, all usage titles of objects already placed in the room |
| 888 |
-- will remain accessible from the door room tile. |
| 889 |
invalid_placement = world:wouldObjectBreakRoomObjectsAccessToTheRoomDoor(x, y, room, object_layout, false) |
| 890 |
end |
| 891 |
else |
| 892 |
-- user not in a room editing mode. |
| 893 |
-- as we are not in a room editing mode, then there may be humanoids in the room. |
| 894 |
-- this means that placing object can block a humanoid's passage to the door. |
| 895 |
-- To prevent that case we fallback to strict placing approach. |
| 896 |
invalid_placement = world:wouldNonSideObjectBreakPathfindingIfSpawnedAt(x, y, object, object_orientation, room_id) |
| 897 |
end |
| 898 |
else |
| 899 |
-- soft placing approach disabled. So follow strict placing approach. |
| 900 |
invalid_placement = world:wouldNonSideObjectBreakPathfindingIfSpawnedAt(x, y, object, object_orientation, room_id) |
| 901 |
end |
| 902 |
else |
| 903 |
-- not a valid room with a door. Possible corner case like a transition state. |
| 904 |
invalid_placement = true |
| 905 |
end |
| 906 |
else |
| 907 |
-- placing an object outside of any room. |
| 908 |
-- Fallback to strict placing approach. |
| 909 |
invalid_placement = world:wouldNonSideObjectBreakPathfindingIfSpawnedAt(x, y, object, object_orientation, room_id) |
| 910 |
end |
| 911 |
|
| 912 |
if not invalid_placement then |
| 913 |
return true |
| 914 |
elseif self.ui.app.config.blocking_off_areas == 3 then |
| 915 |
-- all-permissive placing approach. |
| 916 |
-- This could lead to crashes, so we'll record this in the log so that during investigation |
| 917 |
-- we'll be able to know that safe placement was disabled. |
| 918 |
TheApp.world:gameLog("Blocking off areas is allowed at " .. x .. ", " .. y .. ".") |
| 919 |
return true |
| 920 |
end |
| 921 |
return false |
| 922 |
end |
| 923 |
|
| 924 |
--! Function for checking the valid placement of "SideObject". |
| 925 |
--! param x (integer) target tile x coordinate. |
| 926 |
--! param y (integer) target tile y coordinate. |
| 927 |
--! param room_id (integer) room id if we in a build mode. |
| 928 |
--! param passable_flag (string) passable flag name. orientation of an object relative to the cardinal directions. |
| 929 |
--! param map (object) map class instance. |
| 930 |
--! return (bool) is this placement not going to break path finding. |
| 931 |
function UIPlaceObjects:_isSideObjectPlacementValid(x, y, room_id, passable_flag, map) |
| 932 |
-- if we consider to place a SideObject against a wall, it is always a valid placement. |
| 933 |
-- also, for SideObject, do not change 'passable_flag according to the algorithm below in 1., |
| 934 |
-- as this will make the wall passable through. |
| 935 |
local along_wall = map:getCellFlags(x, y)[passable_flag] == false |
| 936 |
if along_wall then return true end |
| 937 |
|
| 938 |
local world = self.ui.app.world |
| 939 |
local invalid_placement |
| 940 |
|
| 941 |
-- Depending on the orientation of the side object, we will check the accessibility of the cell |
| 942 |
-- that is located just behind the edge that this side object creates with its placement |
| 943 |
local to_check_x, to_check_y = x, y |
| 944 |
if passable_flag == "travelNorth" or passable_flag == "travelSouth" then |
| 945 |
to_check_y = to_check_y + (passable_flag == "travelNorth" and -1 or 1) |
| 946 |
else |
| 947 |
to_check_x = to_check_x + (passable_flag == "travelEast" and 1 or -1) |
| 948 |
end |
| 949 |
local opposite_passable_flag = Object.getComplementaryPassableFlag(passable_flag) |
| 950 |
|
| 951 |
local function hasNoConnectingPath(x1, y1, x2, y2) |
| 952 |
if not world.pathfinder:findDistance(x1, y1, x2, y2) then |
| 953 |
-- we need to check if the failure to get the distance is due to the presence of an object in the adjacent tile |
| 954 |
if map:getCellFlags(x2, y2)["passable"] then |
| 955 |
return true |
| 956 |
end |
| 957 |
end |
| 958 |
return false |
| 959 |
end |
| 960 |
|
| 961 |
-- 1. Let's simulate it as if we've already placed the object and make tile edge unpassable |
| 962 |
-- SideObject cell |
| 963 |
local flags_main = {} |
| 964 |
flags_main[passable_flag] = false |
| 965 |
map:setCellFlags(x, y, flags_main) |
| 966 |
-- adjacent cell to SideObject |
| 967 |
local flags_adjacent = {} |
| 968 |
flags_adjacent[opposite_passable_flag] = false |
| 969 |
map:setCellFlags(to_check_x, to_check_y, flags_adjacent) |
| 970 |
|
| 971 |
-- 2. Check if the passability to objects is broken. |
| 972 |
if room_id > 0 and x and y then |
| 973 |
-- placing an object in a room |
| 974 |
local room = world:getRoom(x, y) |
| 975 |
if room and not room.crashed and room.door and room.door.tile_x and room.door.tile_y then |
| 976 |
-- a valid room with a door |
| 977 |
if self.ui.app.config.blocking_off_areas == 2 then |
| 978 |
-- soft placing approach |
| 979 |
if not room.is_active then |
| 980 |
-- user in a room editing mode |
| 981 |
invalid_placement = world:wouldObjectBreakRoomObjectsAccessToTheRoomDoor(x, y, room, nil, true) |
| 982 |
else |
| 983 |
-- user not in a room editing mode. |
| 984 |
-- as we are not in a room editing mode, then there could be humanoids in the room. |
| 985 |
-- this means that placing object can block a humanoid's passage to the door. |
| 986 |
-- to prevent that case we fallback to strict placing approach. |
| 987 |
invalid_placement = hasNoConnectingPath(x, y, to_check_x, to_check_y) |
| 988 |
end |
| 989 |
else |
| 990 |
-- follow strict placing approach. |
| 991 |
invalid_placement = hasNoConnectingPath(x, y, to_check_x, to_check_y) |
| 992 |
end |
| 993 |
else |
| 994 |
-- not a valid room with a door. Possible corner case like a transition state. |
| 995 |
invalid_placement = true |
| 996 |
end |
| 997 |
else |
| 998 |
-- placing an object outside of any room. |
| 999 |
-- Fallback to strict placing approach. |
| 1000 |
invalid_placement = hasNoConnectingPath(x, y, to_check_x, to_check_y) |
| 1001 |
end |
| 1002 |
|
| 1003 |
-- 3. Restore the original tile's passable properties, thus undoing the actions taken in step 1. |
| 1004 |
flags_main[passable_flag] = true |
| 1005 |
map:setCellFlags(x, y, flags_main) |
| 1006 |
flags_adjacent[opposite_passable_flag] = true |
| 1007 |
map:setCellFlags(to_check_x, to_check_y, flags_adjacent) |
| 1008 |
|
| 1009 |
if not invalid_placement then |
| 1010 |
return true |
| 1011 |
elseif self.ui.app.config.blocking_off_areas == 3 then |
| 1012 |
-- all-permissive placing approach. |
| 1013 |
-- This could lead to crashes, so we'll record this in the log so that during investigation |
| 1014 |
-- we'll be able to know that safe placement was disabled. |
| 1015 |
TheApp.world:gameLog("Blocking off areas is allowed at " .. x .. ", " .. y .. ".") |
| 1016 |
return true |
| 1017 |
end |
| 1018 |
return false |
| 1019 |
end |
| 1020 |
|
| 1021 |
function UIPlaceObjects:_setBlueprintPartialFlags(x, y, map, valid_placement) |
| 1022 |
if ATTACH_BLUEPRINT_TO_TILE then |
| 1023 |
self.object_anim:setTile(map, x, y, 0) |
| 1024 |
end |
| 1025 |
self.object_anim:setPartialFlag(flag_altpal, not valid_placement) |
| 1026 |
self.object_slave_anim:setPartialFlag(flag_altpal, not valid_placement) |
| 1027 |
self.object_blueprint_good = valid_placement |
| 1028 |
self.ui:tutorialStep(1, valid_placement and 5 or 4, valid_placement and 4 or 5) |
| 1029 |
end |
| 1030 |
|
| 1031 |
local function NearestPointOnLine(lx1, ly1, lx2, ly2, px, py) |
| 1032 |
-- Translate everything to make one line segment endpoint be on the origin |
| 1033 |
local lx = lx1 - lx2 |
| 1034 |
local ly = ly1 - ly2 |
| 1035 |
px = px - lx2 |
| 1036 |
py = py - ly2 |
| 1037 |
|
| 1038 |
-- Project point onto line (scale everything to make (lx, ly) be on the unit |
| 1039 |
-- circle, then dot product). |
| 1040 |
local d = (lx * px + ly * py) / (lx * lx + ly * ly) |
| 1041 |
|
| 1042 |
if d <= 0 then |
| 1043 |
return lx2, ly2 |
| 1044 |
elseif d >= 1 then |
| 1045 |
return lx1, ly1 |
| 1046 |
else |
| 1047 |
return d * lx1 + (1 - d) * lx2, d * ly1 + (1 - d) * ly2 |
| 1048 |
end |
| 1049 |
end |
| 1050 |
|
| 1051 |
function UIPlaceObjects:calculateBestPlacementPosition(x, y) |
| 1052 |
local object = self.objects[self.active_index].object |
| 1053 |
local room = self.room |
| 1054 |
local wx, wy = self.ui:ScreenToWorld(self.x + x, self.y + y) |
| 1055 |
local bestd = nil |
| 1056 |
local bestx, besty = wx, wy |
| 1057 |
local besto = self.object_orientation |
| 1058 |
if room and object.locked_to_wall then |
| 1059 |
if object.locked_to_wall.north then |
| 1060 |
local px, py = NearestPointOnLine(room.x + 0.5, room.y + 0.5, |
| 1061 |
room.x + room.width - 0.5, room.y + 0.5, wx, wy) |
| 1062 |
local d = ((px - wx)^2 + (py - wy)^2)^0.5 |
| 1063 |
if not bestd or d < bestd then |
| 1064 |
bestd, bestx, besty, besto = d, px, py, object.locked_to_wall.north |
| 1065 |
end |
| 1066 |
end |
| 1067 |
if object.locked_to_wall.west then |
| 1068 |
local px, py = NearestPointOnLine(room.x + 0.5, room.y + 0.5, |
| 1069 |
room.x + 0.5, room.y + room.height - 0.5, wx, wy) |
| 1070 |
local d = ((px - wx)^2 + (py - wy)^2)^0.5 |
| 1071 |
if not bestd or d < bestd then |
| 1072 |
bestx, besty, besto = px, py, object.locked_to_wall.west |
| 1073 |
end |
| 1074 |
end |
| 1075 |
-- TODO: East, South |
| 1076 |
end |
| 1077 |
bestx, besty = math_floor(bestx), math_floor(besty) |
| 1078 |
if bestx < 1 or besty < 1 or |
| 1079 |
bestx > self.map.width or besty > self.map.height then |
| 1080 |
bestx, besty = nil, nil |
| 1081 |
end |
| 1082 |
return bestx, besty, besto |
| 1083 |
end |
| 1084 |
|
| 1085 |
function UIPlaceObjects:onCursorWorldPositionChange(x, y) |
| 1086 |
local repaint = Window.onCursorWorldPositionChange(self, x, y) |
| 1087 |
if not self.place_objects then -- We don't want to place objects because we are selecting new objects for adding in a room being built/edited |
| 1088 |
return repaint |
| 1089 |
end |
| 1090 |
|
| 1091 |
repaint = true |
| 1092 |
if self.world.user_actions_allowed then |
| 1093 |
local wx, wy, wo = self:calculateBestPlacementPosition(x, y) |
| 1094 |
if wx ~= self.object_cell_x or wy ~= self.object_cell_y then |
| 1095 |
self:setBlueprintCell(wx, wy) |
| 1096 |
repaint = true |
| 1097 |
end |
| 1098 |
if wo ~= self.object_orientation then |
| 1099 |
self:setOrientation(wo) |
| 1100 |
repaint = true |
| 1101 |
end |
| 1102 |
else |
| 1103 |
self:clearBlueprint() |
| 1104 |
end |
| 1105 |
|
| 1106 |
return repaint |
| 1107 |
end |
| 1108 |
|
| 1109 |
function UIPlaceObjects:selectObjectType(object_type) |
| 1110 |
for i, o in ipairs(self.objects) do |
| 1111 |
if o.object.id == object_type.id then |
| 1112 |
self:setActiveIndex(i) |
| 1113 |
return |
| 1114 |
end |
| 1115 |
end |
| 1116 |
end |
| 1117 |
|
| 1118 |
function UIPlaceObjects:afterLoad(old, new) |
| 1119 |
Window.afterLoad(self, old, new) |
| 1120 |
UIPlaceObjects.registerKeyHandlers(self) |
| 1121 |
|
| 1122 |
if old < 236 then |
| 1123 |
local selected_label_color = { red = 40, green = 40, blue = 250 } |
| 1124 |
self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true }) |
| 1125 |
self.blue_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font02V", nil, nil, {ttf_color = selected_label_color, apply_ui_scale = true }) |
| 1126 |
end |
| 1127 |
end |