-
Notifications
You must be signed in to change notification settings - Fork 354
/
edit_room.lua
1569 lines (1452 loc) · 52.1 KB
/
edit_room.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[ Copyright (c) 2009 Peter "Corsix" Cawley
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
corsixth.require("dialogs.place_objects")
class "UIEditRoom" (UIPlaceObjects)
---@type UIEditRoom
local UIEditRoom = _G["UIEditRoom"]
function UIEditRoom:UIEditRoom(ui, room_type)
-- NB: UIEditRoom:onCursorWorldPositionChange is called by the UIPlaceObjects
-- constructor, hence the initialisation of required fields prior to the call.
self.UIPlaceObjects(self, ui)
local app = ui.app
local blue_red_swap = self.anims.Alt32_BlueRedSwap
-- Set alt palette on wall blueprint to make it red
self.anims:setAnimationGhostPalette(124, app.gfx:loadGhost("QData", "Ghost1.dat", 6), blue_red_swap)
-- Set on door and window blueprints too
self.anims:setAnimationGhostPalette(126, app.gfx:loadGhost("QData", "Ghost1.dat", 6), blue_red_swap)
self.anims:setAnimationGhostPalette(130, app.gfx:loadGhost("QData", "Ghost1.dat", 6), blue_red_swap)
self.cell_outline = TheApp.gfx:loadSpriteTable("Bitmap", "aux_ui", true)
if not room_type.room_info then
self.blueprint_rect = {
x = 1,
y = 1,
w = 0,
h = 0,
}
if room_type.swing_doors then
self.blueprint_door = {anim = {}, old_anim = {}, old_flags = {}}
else
self.blueprint_door = {}
end
self.phase = "walls" --> "door" --> "windows" --> "clear_area" --> "objects" --> "closed"
self.room_type = room_type
self.title_text = room_type.name
self.desc_text = _S.place_objects_window.drag_blueprint
else
self.phase = "objects"
self.room_type = room_type.room_info
self.title_text = room_type.room_info.name
self.room = room_type
self.desc_text = _S.place_objects_window.confirm_or_buy_objects
self.paid = true
self.blueprint_rect = {
x = room_type.x,
y = room_type.y,
w = room_type.width,
h = room_type.height,
}
if room_type.room_info.swing_doors then
self.blueprint_door = {anim = {}, old_anim = {}, old_flags = {}}
else
self.blueprint_door = {}
end
self.ui:setWorldHitTest(self.room)
self.pickup_button:enable(true)
self.purchase_button:enable(true)
self:checkEnableConfirm()
end
self.blueprint_wall_anims = {}
self.blueprint_window = {}
self.mouse_down_x = false
self.mouse_down_y = false
self.mouse_cell_x = 0
self.mouse_cell_y = 0
self:registerKeyHandlers()
end
function UIEditRoom:registerKeyHandlers()
self:addKeyHandler("global_confirm", self.confirm) -- UIPlaceObjects does not need this
self:addKeyHandler("global_confirm_alt", self.confirm)
end
function UIEditRoom:close(...)
if self.phase == "objects" and self.confirm_button.enabled then
if not self.closed_cleanly then
self:confirm(true)
end
else
while self.phase ~= "walls" do
self:cancel()
end
end
for k, obj in pairs(self.blueprint_wall_anims) do
if obj.setTile then
obj:setTile(nil)
else
for _, anim in pairs(obj) do
anim:setTile(nil)
end
end
self.blueprint_wall_anims[k] = nil
end
self.phase = "closed"
-- No longer editing a room
self.ui.edit_room = false
self:setBlueprintRect(1, 1, 0, 0)
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...
return UIPlaceObjects.close(self, ...)
end
--[[ Called when building/editing of the room is about to stop because another
dialog is being opened. If the room is in the objects phase and all
required objects have been placed it will be completed. Otherwise
it is cancelled instead.
--]]
function UIEditRoom:verifyOrAbortRoom()
if self.phase == "objects" and self.confirm_button.enabled then
-- The room can be finished
self:close()
else
-- The room will have to be cancelled
self:abortRoom()
end
end
function UIEditRoom:abortRoom()
if self.paid then
-- Return half the cost.
local progress = self.ui.hospital.research.research_progress
local cost = math.floor(progress[self.room.room_info].build_cost/2)
-- TODO: Return also the cost for additional objects.
-- Decrease the hospital value by the whole room build cost
local valueChange = progress[self.room.room_info].build_cost
for obj, num in pairs(self.room.room_info.objects_needed) do
-- Get how much this item costs.
local obj_cost = self.ui.hospital:getObjectBuildCost(obj)
cost = cost - math.floor(obj_cost)/2
valueChange = valueChange - num * obj_cost
end
self.ui.hospital:receiveMoney(cost, _S.transactions.sell_object, valueChange)
end
-- Close the dialog
self:close()
-- Finally remove the room from the world (close() needs the reference)
if self.room then
self.world.rooms[self.room.id] = nil
end
end
function UIEditRoom:cancel()
if self.confirm_dialog_open then
-- Don't do anything as long as the confirm dialog is open.
return
end
if self.phase == "walls" then
if self.paid then
-- While the confirmation window is open, don't allow the player to click the confirm button.
local confirm_button_state = self.confirm_button.enabled
self.confirm_button:enable(false)
self.confirm_dialog_open = true
-- Ask if the user really wish to sell this room
self.ui:addWindow(UIConfirmDialog(self.ui, false,
_S.confirmation.delete_room,
--[[persistable:delete_room_confirm_dialog]]function()
self:abortRoom()
end,
--[[persistable:delete_room_confirm_dialog_cancel]]function()
self.confirm_button:enable(confirm_button_state)
self.confirm_dialog_open = nil
end
))
else
self:abortRoom()
end
self.ui:setCursor(self.ui.default_cursor)
elseif self.phase == "objects" then
self:stopPickupItems()
self:returnToDoorPhase()
self.world:resetSideObjects()
else
if self.phase == "clear_area" then
self.ui:setDefaultCursor(nil)
self.check_for_clear_area_timer = nil
self.humanoids_to_watch = nil
end
self.phase = "walls"
self:returnToWallPhase()
end
end
function UIEditRoom:confirm(force)
-- double check if confirm is allowed (for being called e.g. by hotkey)
if not force and not self.confirm_button.enabled then
return
end
self:stopPickupItems()
if self.phase == "walls" then
self.mouse_down_x = false
self.mouse_down_y = false
self.move_rect_x = false
self.move_rect_y = false
self.phase = "door"
self:enterDoorPhase()
elseif self.phase == "door" then
self.phase = "windows"
self:enterWindowsPhase()
elseif self.phase == "windows" then
self.phase = "clear_area"
self:clearArea()
elseif self.phase == "clear_area" then
self.ui:setDefaultCursor(nil)
self.phase = "objects"
self:finishRoom()
self.world:resetSideObjects()
self:enterObjectsPhase()
else
-- Pay for room (subtract cost of needed objects, which were already paid for)
if not self.paid then
local progress = self.ui.hospital.research.research_progress
local cost = progress[self.room.room_info].build_cost
for obj, num in pairs(self.room.room_info.objects_needed) do
-- Get how much this item costs.
local obj_cost = self.ui.hospital:getObjectBuildCost(obj)
cost = cost - num * obj_cost
end
self.ui.hospital:spendMoney(cost, _S.transactions.build_room .. ": " .. self.title_text, cost)
self.paid = true
end
self.world:markRoomAsBuilt(self.room)
self.closed_cleanly = true
-- If information dialogs are disabled, go ahead.
if self.ui:getWindow(UIInformation) then
self.ui:tutorialStep(3, 15, 16)
else
self.ui:tutorialStep(3, 15, "next")
end
self:close()
end
end
local function isHumanoidObscuringArea(humanoid, x1, x2, y1, y2)
if humanoid.tile_x then
if x1 <= humanoid.tile_x and humanoid.tile_x <= x2 and
y1 <= humanoid.tile_y and humanoid.tile_y <= y2 then
if (x1 == humanoid.tile_x or x2 == humanoid.tile_x) or
(y1 == humanoid.tile_y or y2 == humanoid.tile_y) then
-- Humanoid not in the rectangle, but might be walking into it
local action = humanoid:getCurrentAction()
if action.name ~= "walk" then
return false
end
if action.path_x then -- in a (rare) special case, path_x is nil (see action_walk_start)
local next_x = action.path_x[action.path_index]
local next_y = action.path_y[action.path_index]
if x1 >= next_x or next_x >= x2 or y1 >= next_y or next_y >= y2 then
return false
end
end
end
return true
end
end
return false
end
function UIEditRoom:clearArea()
self.confirm_button:enable(false)
local rect = self.blueprint_rect
local world = self.ui.app.world
world:clearCaches() -- To invalidate idle tiles in case we need to move people
local humanoids_to_watch = setmetatable({}, {__mode = "k"})
do
local x1 = rect.x - 1
local x2 = rect.x + rect.w
local y1 = rect.y - 1
local y2 = rect.y + rect.h
for _, entity in ipairs(world.entities) do
if class.is(entity, Humanoid) and
isHumanoidObscuringArea(entity, x1, x2, y1, y2) then
humanoids_to_watch[entity] = true
-- Try to make the humanoid leave the area
local current_action = entity:getCurrentAction()
local meander = entity.action_queue[2]
if meander and meander.name == "meander" then
-- Interrupt the idle or walk, which will cause a new meander target
-- to be chosen, which will be outside the blueprint rectangle
meander.can_idle = false
local on_interrupt = current_action.on_interrupt
if on_interrupt then
current_action.on_interrupt = nil
on_interrupt(current_action, entity)
end
elseif current_action.name == "seek_room" or (meander and meander.name == "seek_room") then
-- Make sure that the humanoid doesn't stand idle waiting within the blueprint
if current_action.name == "seek_room" then
entity:queueAction(MeanderAction():setCount(1):setMustHappen(true), 0)
else
meander.done_walk = false
end
else
-- Look for a queue action and re-arrange the people in it, which
-- should cause anyone queueing within the blueprint to move
for _, action in ipairs(entity.action_queue) do
if action.name == "queue" then
for _, humanoid in ipairs(action.queue) do
local callbacks = action.queue.callbacks[humanoid]
if callbacks then
callbacks:onChangeQueuePosition(humanoid)
end
end
break
end
end
-- TODO: Consider any other actions which might be causing the
-- humanoid to be staying within the rectangle for a long time.
end
end
end
end
if next(humanoids_to_watch) == nil then
-- No humanoids within the area, so continue with the room placement
self:confirm(true)
return
end
self.check_for_clear_area_timer = 10
self.humanoids_to_watch = humanoids_to_watch
self.ui:setCursor(self.ui.waiting_cursor)
end
function UIEditRoom:onTick()
UIFurnishCorridor.onTick(self)
if self.check_for_clear_area_timer then
self.check_for_clear_area_timer = self.check_for_clear_area_timer - 1
if self.check_for_clear_area_timer == 0 then
local rect = self.blueprint_rect
local x1 = rect.x - 1
local x2 = rect.x + rect.w
local y1 = rect.y - 1
local y2 = rect.y + rect.h
for humanoid in pairs(self.humanoids_to_watch) do
-- The person might be dying (this check should probably be moved into
-- isHumanoidObscuringArea, but I don't want to change too much right
-- before a release).
if humanoid:getCurrentAction().name == "die" then
if not humanoid.hospital then
self.humanoids_to_watch[humanoid] = nil
end
elseif not isHumanoidObscuringArea(humanoid, x1, x2, y1, y2) then
self.humanoids_to_watch[humanoid] = nil
end
end
if next(self.humanoids_to_watch) then
self.check_for_clear_area_timer = 10
else
self.check_for_clear_area_timer = nil
self.humanoids_to_watch = nil
self:confirm(true)
end
end
end
end
function UIEditRoom:finishRoom()
local room_type = self.room_type
local wall_type = self.ui.app.walls[room_type.wall_type]
local world = self.ui.app.world
local map = self.ui.app.map.th
local rect = self.blueprint_rect
local door, door2
-- Add the transparency flag if it is set.
local flag = 0
if self.ui.transparent_walls then
flag = 1024
end
local function check_external_window(x, y, layer)
-- If a wall is built which is normal to an external window, then said
-- window needs to be removed, otherwise it looks odd (see issue #59).
local block = map:getCell(x, y, layer)
local dir = world:getWallDirFromBlockId(block)
local tiles = self.ui.app.walls.external[world:getWallSetFromBlockId(block)]
if dir == "north_window_1" then
if x ~= rect.x then
map:setCell(x, y, layer, flag + tiles.north)
if map:getCell(x + 1, y, layer) ~= 0 then
map:setCell(x + 1, y, layer, flag + tiles.north)
end
end
elseif dir == "north_window_2" then
if x == rect.x then
if map:getCell(x - 1, y, layer) ~= 0 then
map:setCell(x - 1, y, layer, flag + tiles.north)
end
map:setCell(x, y, layer, flag + tiles.north)
end
elseif dir == "west_window_1" then
if y == rect.y then
map:setCell(x, y, layer, flag + tiles.west)
if map:getCell(x, y - 1, layer) ~= 0 then
map:setCell(x, y - 1, layer, flag + tiles.west)
end
end
elseif dir == "west_window_2" then
if y ~= rect.y then
if map:getCell(x, y + 1, layer) ~= 0 then
map:setCell(x, y + 1, layer, flag + tiles.west)
end
map:setCell(x, y, layer, flag + tiles.west)
end
end
end
for x, obj in pairs(self.blueprint_wall_anims) do
for y, anim in pairs(obj) do
if x == rect.x and y == rect.y then
local _, east, north = map:getCell(x, y)
if world:getWallIdFromBlockId(east) == "external" then
check_external_window(x, y, 2)
elseif world:getWallSetFromBlockId(east) == "window_tiles" then
map:setCell(x, y, 2, flag + wall_type.window_tiles.north)
else
map:setCell(x, y, 2, flag + wall_type.inside_tiles.north)
end
if world:getWallIdFromBlockId(north) == "external" then
check_external_window(x, y, 3)
elseif world:getWallSetFromBlockId(north) == "window_tiles" then
map:setCell(x, y, 3, flag + wall_type.window_tiles.west)
else
map:setCell(x, y, 3, flag + wall_type.inside_tiles.west)
end
else
repeat
local tiles = "outside_tiles"
local tag = anim:getTag()
if tag == "nothing" or tag == "swing_slave" then
tiles = "swing_outside_tiles"
end
local dir = (anim:getFlag() % 2 == 1) and "west" or "north"
local layer = dir == "north" and 2 or 3
if world:getWallIdFromBlockId(map:getCell(x, y, layer)) == "external" then
if layer == 2 then
if (x == rect.x or x == rect.x + rect.w - 1) and (y == rect.y or y == rect.y + rect.h) then
check_external_window(x, y, 2)
end
else
if (x == rect.x or x == rect.x + rect.w) and (y == rect.y or y == rect.y + rect.h - 1) then
check_external_window(x, y, 3)
end
end
break
end
local existing = world:getWallSetFromBlockId(map:getCell(x, y, layer))
if rect.x <= x and x < rect.x + rect.w and rect.y <= y and y < rect.y + rect.h then
if tag == "nothing" or tag == "swing_slave" then
tiles = "swing_inside_tiles"
else
tiles = "inside_tiles"
end
elseif existing then
break
end
if tag == "window" or existing == "window_tiles" then
tiles = "window_tiles"
end
local suffixes = {"_left", "_right"}
local num = dir == "north" and 1 or 0
if tag == "door" then
door = world:newObject("door", x, y, dir)
elseif tag == "swing_master" then
door = world:newObject("swing_door_right", x, y, dir)
elseif tag == "swing_slave" then
door2 = world:newObject("swing_door_left", x, y, dir)
map:setCell(x, y, layer, flag + wall_type[tiles][dir .. suffixes[2 - num]])
elseif tag == "nothing" then
map:setCell(x, y, layer, flag + wall_type[tiles][dir .. suffixes[num + 1]])
else
map:setCell(x, y, layer, flag + wall_type[tiles][dir])
end
until true
end
anim:setTile(nil)
end
self.blueprint_wall_anims[x] = nil
end
-- If there is already a room, e.g. it is being moved, don't make a new one.
if self.room then
self.room:initRoom(rect.x, rect.y, rect.w, rect.h, door, door2)
else
self.room = self.world:newRoom(rect.x, rect.y, rect.w, rect.h, room_type, door, door2)
end
end
function UIEditRoom:purchaseItems()
self.visible = false
self.place_objects = false
self:stopPickupItems()
local cfg_objects = self.world.map.level_config.objects
local research = self.ui.hospital.research
local object_list = {} -- transform set to list
for i, o in ipairs(self.room.room_info.objects_additional) do
-- Don't show the object if it hasn't been researched yet.
local object = TheApp.objects[o]
local avail = cfg_objects[object.thob].AvailableForLevel
if avail == 1 and (not research.research_progress[object] or
research.research_progress[object].discovered) then
-- look up current quantity
local cur_qty = 0
for _, p in ipairs(self.objects) do
if p.object.id == o then
cur_qty = p.qty
end
end
-- look up minimum quantity (required objects list)
local min_qty = self.room.room_info.objects_needed[o] or 0
-- subtract number of objects in room from minimum quantity
for obj, _ in pairs(self.room.objects) do
if min_qty == 0 then break end
if obj.object_type.id == o then
min_qty = min_qty - 1
end
end
object_list[i] = { object = TheApp.objects[o], qty = cur_qty, min_qty = min_qty }
end
end
self.ui:addWindow(UIFurnishCorridor(self.ui, object_list, self))
end
-- callback for item pick up button
function UIEditRoom:pickupItems()
if self.in_pickup_mode then
self:stopPickupItems()
else
self.in_pickup_mode = true
self.ui:setCursor(self.ui.grab_cursor)
self.place_objects = false
self.active_index = 0
self.object_cell_x = nil
self.object_cell_y = nil
self:clearBlueprint()
end
end
function UIEditRoom:stopPickupItems()
if self.in_pickup_mode then
self.in_pickup_mode = false
self.ui:setCursor(self.ui.default_cursor)
self.pickup_button:setToggleState(false)
end
end
function UIEditRoom:returnToWallPhase(early)
self.ui:tutorialStep(3, {9, 10, 11, 12}, 4)
if not early then
self.desc_text = _S.place_objects_window.drag_blueprint
self.confirm_button:enable(true)
for k, obj in pairs(self.blueprint_wall_anims) do
for _, anim in pairs(obj) do
anim:setTile(nil)
end
self.blueprint_wall_anims[k] = nil
end
end
local rect = self.blueprint_rect
local x, y, w, h = rect.x, rect.y, rect.w, rect.h
self:setBlueprintRect(1, 1, 0, 0)
self:setBlueprintRect(x, y, w, h)
if self.room_type.swing_doors then
self.blueprint_door = {anim = {}, old_anim = {}, old_flags = {}}
else
self.blueprint_door = {}
end
end
-- Remove walls
function UIEditRoom:_remove_wall_line(x, y, step_x, step_y, n_steps, layer, neigh_x, neigh_y, world)
local map = world.map.th
for _ = 1, n_steps do
local existing = map:getCell(x, y, layer)
-- Possibly add transparency.
local flag = 0
if world.ui.transparent_walls then
flag = 1024
end
if world:getWallIdFromBlockId(existing) ~= "external" then
local neighbour = world:getRoom(x + neigh_x, y + neigh_y)
if neighbour then
if neigh_x ~= 0 or neigh_y ~= 0 then
local set = world:getWallSetFromBlockId(existing)
local dir = world:getWallDirFromBlockId(existing)
if set == "inside_tiles" then
set = "outside_tiles"
end
map:setCell(x, y, layer, flag + world.wall_types[neighbour.room_info.wall_type][set][dir])
end
else
map:setCell(x, y, layer, flag)
end
end
x = x + step_x
y = y + step_y
end
end
function UIEditRoom:removeRoom(save_objects, room, world)
-- Remove any placed objects (add them to list again) when save_objects is true
for x = room.x, room.x + room.width - 1 do
for y = room.y, room.y + room.height - 1 do
while true do
-- get litter then any other object
local obj = world:getObject(x, y, 'litter') or world:getObject(x, y)
-- but we need to ignore doors
if not obj or obj == room.door or class.is(obj, SwingDoor) then
break
end
if obj.object_type.id == "litter" then -- Silently remove litter from the world.
obj:remove()
else
if save_objects then
local obj_state = obj:getState()
world:destroyEntity(obj)
if not obj.master then
self:addObjects({{
object = TheApp.objects[obj.object_type.id],
state = obj_state,
qty = 1
}})
end
else
-- just destroy it
world:destroyEntity(obj)
end
end
end
end
end
-- now doors
world:destroyEntity(room.door)
if room.door2 then
world:destroyEntity(room.door2)
end
if save_objects then
-- backup list of objects
self.objects_backup = {}
for k, o in pairs(self.objects) do
self.objects_backup[k] = { object = o.object, qty = o.qty, state = o.state }
end
UIPlaceObjects.removeAllObjects(self, true)
end
self:_remove_wall_line(room.x, room.y, 0, 1, room.height, 3, -1, 0, world)
self:_remove_wall_line(room.x, room.y, 1, 0, room.width , 2, 0, -1, world)
self:_remove_wall_line(room.x + room.width, room.y , 0, 1, room.height, 3, 0, 0, world)
self:_remove_wall_line(room.x, room.y + room.height, 1, 0, room.width , 2, 0, 0, world)
-- Reset floor tiles and flags
world.map.th:unmarkRoom(room.x, room.y, room.width, room.height)
end
function UIEditRoom:returnToDoorPhase()
self.ui:tutorialStep(3, {13, 14, 15}, 9)
local room = self.room
room.built = false
if room.door and room.door.queue then
room.door.queue:rerouteAllPatients(room.room_info.id)
end
self.purchase_button:enable(false)
self.pickup_button:enable(false)
self:removeRoom(true, room, self.world)
-- Re-create blueprint
local rect = self.blueprint_rect
local old_w, old_h = rect.w, rect.h
rect.w = 0
rect.h = 0
self:setBlueprintRect(rect.x, rect.y, old_w, old_h)
-- We've gone all the way back to wall phase, so step forward to door phase
self.phase = "door"
self:enterDoorPhase()
end
function UIEditRoom:screenToWall(x, y)
local cellx, celly = self.ui:ScreenToWorld(x, y)
cellx = math.floor(cellx)
celly = math.floor(celly)
local rect = self.blueprint_rect
if cellx == rect.x or cellx == rect.x - 1 or cellx == rect.x + rect.w or cellx == rect.x + rect.w - 1 or
celly == rect.y or celly == rect.y - 1 or celly == rect.y + rect.h or
celly == rect.y + rect.h - 1 then -- luacheck: ignore 542
else
return
end
-- NB: Doors and windows cannot be placed on corner tiles, hence walls of corner tiles
-- are never returned, and the nearest non-corner wall is returned instead. If they
-- could be placed on corner tiles, then you would have to consider the interaction of
-- wall shadows with windows and doors, amongst other things.
-- Swing doors are allowed to be adjacent everywhere except the top corner.
local modifier = 0
local swinging = false
if self.room_type.swing_doors and self.phase == "door" then
modifier = 1
swinging = true
end
if cellx == rect.x and celly == rect.y then
-- top corner
local x_, _ = self.ui:WorldToScreen(cellx, celly)
if x >= x_ then
-- correctly reflects (at least origin version) of TH.
-- Swing doors in top corner to the east, actually skip another tile
if swinging then
return cellx + 2 + modifier, celly, "north"
else
return cellx + 1 + modifier, celly, "north"
end
else
return cellx, celly + 1 + modifier, "west"
end
elseif cellx == rect.x + rect.w - 1 and celly == rect.y + rect.h - 1 then
-- bottom corner
local x_, _ = self.ui:WorldToScreen(cellx, celly)
if x >= x_ then
return cellx, celly - 1, "east"
else
return cellx - 1, celly, "south"
end
elseif cellx == rect.x and celly == rect.y + rect.h - 1 then
-- left corner
local _, y_ = self.ui:WorldToScreen(cellx, celly)
if y >= y_ + 16 then
if swinging and cellx <= rect.x + 2 then
return cellx + 1 + modifier, celly, "south"
else
return cellx + 1, celly, "south"
end
else
return cellx, celly - 1, "west"
end
elseif cellx == rect.x + rect.w - 1 and celly == rect.y then
-- right corner
local _, y_ = self.ui:WorldToScreen(cellx, celly)
if y >= y_ + 16 then
if swinging and celly <= rect.y + 2 then
return cellx, celly + 1 + modifier, "east"
else
return cellx, celly + 1, "east"
end
else
return cellx - 1, celly, "north"
end
elseif (cellx == rect.x - 1 or cellx == rect.x) and rect.y <= celly and celly < rect.y + rect.h then
-- west edge
if celly == rect.y then
celly = rect.y + 1
elseif celly == rect.y + rect.h - 1 then
celly = rect.y + rect.h - 2
end
if swinging and celly <= rect.y + 1 then
celly = celly + 1
end
return rect.x, celly, "west"
elseif (celly == rect.y - 1 or celly == rect.y) and rect.x <= cellx and cellx < rect.x + rect.w then
-- north edge
-- correctly reflects (at least origin version) of TH.
-- Swing doors in top corner to the east, actually skip another tile
if swinging and cellx <= rect.x + 2 then
cellx = rect.x + 3
elseif cellx == rect.x then
cellx = rect.x + 1 + modifier
elseif cellx == rect.x + rect.w - 1 then
cellx = rect.x + rect.w - 2
end
return cellx, rect.y, "north"
elseif (cellx == rect.x + rect.w or cellx == rect.x + rect.w - 1) and
rect.y <= celly and celly < rect.y + rect.h then
-- east edge
if swinging and celly <= rect.y + 1 then
celly = rect.y + 2
elseif celly == rect.y then
celly = rect.y + 1
elseif celly == rect.y + rect.h - 1 then
celly = rect.y + rect.h - 2
end
return rect.x + rect.w - 1, celly, "east"
elseif (celly == rect.y + rect.h or celly == rect.y + rect.h - 1) and
rect.x <= cellx and cellx < rect.x + rect.w then
-- south edge
if swinging and cellx <= rect.x + 1 then
cellx = rect.x + 2
elseif cellx == rect.x then
cellx = rect.x + 1
elseif cellx == rect.x + rect.w - 1 then
cellx = rect.x + rect.w - 2
end
return cellx, rect.y + rect.h - 1, "south"
end
end
-- Function to check if the tiles adjacent to the room are still reachable from each other.
-- NB: the passable flags of the room have to be set to false already before calling this function
function UIEditRoom:checkReachability()
local map = self.ui.app.map.th
local world = self.ui.app.world
local rect = self.blueprint_rect
local prev_x, prev_y
local x, y = rect.x, rect.y - 1
local flags = {}
local function check(flag)
if map:getCellFlags(x, y, flags).passable and flags[flag] then
if prev_x and not world:getPathDistance(prev_x, prev_y, x, y) then
return false
end
prev_x, prev_y = x, y
end
return true
end
while x < rect.x + rect.w do
if not check("travelSouth") then return false end
x = x + 1
end
y = y + 1
while y < rect.y + rect.h do
if not check("travelWest") then return false end
y = y + 1
end
x = x - 1
while x >= rect.x do
if not check("travelNorth") then return false end
x = x - 1
end
y = y - 1
while y >= rect.y do
if not check("travelEast") then return false end
y = y - 1
end
return true
end
function UIEditRoom:enterDoorPhase()
self.ui:tutorialStep(3, 8, 9)
-- make tiles impassable
for y = self.blueprint_rect.y, self.blueprint_rect.y + self.blueprint_rect.h - 1 do
for x = self.blueprint_rect.x, self.blueprint_rect.x + self.blueprint_rect.w - 1 do
self.ui.app.map:setCellFlags(x, y, {passable = false})
end
end
-- check if all adjacent tiles of the rooms are still connected
if not self:checkReachability() then
if self.ui.app.config.allow_blocking_off_areas then
print("Blocking off areas is allowed with room " .. self.blueprint_rect.x .. ", " .. self.blueprint_rect.y .. ".")
else
-- undo passable flags and go back to walls phase
self.phase = "walls"
self:returnToWallPhase(true)
self.ui:playSound("wrong2.wav")
self.ui.adviser:say(_A.room_forbidden_non_reachable_parts)
return
end
end
self.desc_text = _S.place_objects_window.place_door
self.confirm_button:enable(false) -- Confirmation is via placing door
-- Change the floor tiles to opaque blue
local map = self.ui.app.map.th
for y = self.blueprint_rect.y, self.blueprint_rect.y + self.blueprint_rect.h - 1 do
for x = self.blueprint_rect.x, self.blueprint_rect.x + self.blueprint_rect.w - 1 do
map:setCell(x, y, 4, 24)
end
end
-- Re-organise wall anims to index by x and y
local walls = {}
for _, wall in ipairs(self.blueprint_wall_anims) do
local _, x, y = wall:getTile()
if not walls[x] then
walls[x] = {}
end
walls[x][y] = wall
end
self.blueprint_wall_anims = walls
end
function UIEditRoom:enterWindowsPhase()
self.ui:tutorialStep(3, {9, 10}, 11)
self.desc_text = _S.place_objects_window.place_windows
self.confirm_button:enable(true)
end
function UIEditRoom:enterObjectsPhase()
self.ui:setCursor(self.ui.default_cursor)
self.ui:tutorialStep(3, {11, 12}, 13)
self.ui:setWorldHitTest(self.room)
local confirm = self:checkEnableConfirm()
if #self.room.room_info.objects_additional == 0 and confirm then
self:confirm(true)
return
end
self.desc_text = _S.place_objects_window.confirm_or_buy_objects
if #self.room.room_info.objects_additional > 0 then
self.purchase_button:enable(true)
end
self.pickup_button:enable(true)
if self.objects_backup then
self:addObjects(self.objects_backup, true)
else
local room_objects = self.room.room_info.objects_needed
if TheApp.config.enable_avg_contents then
room_objects = self:computeAverageContents()
end
local object_list = {} -- transform set to list
for o, num in pairs(room_objects) do
if num > 0 then
object_list[#object_list + 1] = { object = TheApp.objects[o], qty = num }
end
end
self:addObjects(object_list, true)
end
end
-- Decide contents of the new room based on average content of previous built rooms
-- of the same type.
function UIEditRoom:computeAverageContents()
local average_objects = {} -- what does the average room of this type contain?
local room_count = 0
for _, room in pairs(self.world.rooms) do
if room and room.built and not room.crashed and
room.hospital == self.ui.hospital and room.room_info == self.room_type then
room_count = room_count + 1
for obj, _ in pairs(room.objects) do
average_objects[obj.object_type.id] = (average_objects[obj.object_type.id] or 0) + 1
end
end
end
-- Ensure room contents is within the boundaries of objects_needed and objects_additional.
local objects_needed = self.room.room_info.objects_needed
local additional_objects = {} -- Reversed mapping
for _, obj in pairs(self.room.room_info.objects_additional) do
additional_objects[obj] = 1
end
for id, count in pairs(average_objects) do
count = math.floor(count / room_count + 0.5)
if additional_objects[id] == nil and objects_needed[id] == nil then
count = 0
end
average_objects[id] = count
end
for id, count in pairs(objects_needed) do
if (average_objects[id] or 0) < count then
average_objects[id] = count
end
end
return average_objects
end
function UIEditRoom:draw(canvas, ...)
if self.world.user_actions_allowed then
local ui = self.ui
local x, y = ui:WorldToScreen(self.mouse_cell_x, self.mouse_cell_y)
local zoom = self.ui.zoom_factor
if canvas:scale(zoom) then
x = math.floor(x / zoom)
y = math.floor(y / zoom)
end