~kalon33/corsix-th/master

« back to all changes in this revision

Viewing changes to CorsixTH/Lua/dialogs/edit_room.lua

  • Committer: corsixth.bot at gmail
  • Date: 2014-03-31 23:30:23 UTC
  • Revision ID: svn-v4:c39591fa-788f-11de-a72b-d90af8dea425:trunk:2687
Remove trailing whitespaces in .h, .cpp, .c and .lua files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
local TH = require "TH"
22
22
local math_floor
23
23
    = math.floor
24
 
    
 
24
 
25
25
dofile "dialogs/place_objects"
26
26
 
27
27
class "UIEditRoom" (UIPlaceObjects)
121
121
     dialog is being opened. If the room is in the objects phase and all
122
122
     required objects have been placed it will be completed. Otherwise
123
123
     it is cancelled instead.
124
 
--]]     
 
124
--]]
125
125
function UIEditRoom:verifyOrAbortRoom()
126
126
  if self.phase == "objects" and self.confirm_button.enabled then
127
127
    -- The room can be finished
237
237
      self.ui.hospital:spendMoney(cost, _S.transactions.build_room .. ": " .. self.title_text, cost)
238
238
      self.paid = true
239
239
    end
240
 
    
 
240
 
241
241
    self.world:markRoomAsBuilt(self.room)
242
242
    self.closed_cleanly = true
243
243
    -- If information dialogs are disabled, go ahead.
289
289
      if class.is(entity, Humanoid)
290
290
      and self:isHumanoidObscuringArea(entity, x1, x2, y1, y2) then
291
291
        humanoids_to_watch[entity] = true
292
 
        
 
292
 
293
293
        -- Try to make the humanoid leave the area
294
294
        local meander = entity.action_queue[2]
295
295
        if meander and meander.name == "meander" then
328
328
      end
329
329
    end
330
330
  end
331
 
  
 
331
 
332
332
  if next(humanoids_to_watch) == nil then
333
333
    -- No humanoids within the area, so continue with the room placement
334
334
    self:confirm(true)
335
335
    return
336
336
  end
337
 
  
 
337
 
338
338
  self.check_for_clear_area_timer = 10
339
339
  self.humanoids_to_watch = humanoids_to_watch
340
340
  self.ui:setDefaultCursor "sleep"
504
504
    local object = TheApp.objects[o]
505
505
    local research = self.ui.hospital.research
506
506
    local avail = research.level_config.objects[object.thob].AvailableForLevel
507
 
    if avail == 1 and (not research.research_progress[object] 
 
507
    if avail == 1 and (not research.research_progress[object]
508
508
    or research.research_progress[object].discovered) then
509
509
      -- look up current quantity
510
510
      local cur_qty = 0
513
513
          cur_qty = p.qty
514
514
        end
515
515
      end
516
 
      
 
516
 
517
517
      -- look up minimum quantity (required objects list)
518
518
      local min_qty = self.room.room_info.objects_needed[o] or 0
519
 
      
 
519
 
520
520
      -- subtract number of objects in room from minimum quantity
521
521
      for obj, _ in pairs(self.room.objects) do
522
522
        if min_qty == 0 then break end
527
527
      object_list[i] = { object = TheApp.objects[o], qty = cur_qty, min_qty = min_qty }
528
528
    end
529
529
  end
530
 
  
 
530
 
531
531
  self.ui:addWindow(UIFurnishCorridor(self.ui, object_list, self))
532
532
end
533
533
 
534
534
-- callback for item pick up button
535
535
function UIEditRoom:pickupItems()
536
 
  if self.in_pickup_mode then 
 
536
  if self.in_pickup_mode then
537
537
    self:stopPickupItems()
538
538
  else
539
539
    self.in_pickup_mode = true
586
586
  if room.door and room.door.queue then
587
587
    room.door.queue:rerouteAllPatients({name = "seek_room", room_type = room.room_info.id})
588
588
  end
589
 
  
 
589
 
590
590
  self.purchase_button:enable(false)
591
591
  self.pickup_button:enable(false)
592
 
  
 
592
 
593
593
  -- Remove any placed objects (add them to list again)
594
594
  for x = room.x, room.x + room.width - 1 do
595
595
    for y = room.y, room.y + room.height - 1 do
612
612
  if self.room.door2 then
613
613
    self.world:destroyEntity(self.room.door2)
614
614
  end
615
 
  
 
615
 
616
616
  -- backup list of objects
617
617
  self.objects_backup = {}
618
618
  for k, o in pairs(self.objects) do
619
619
    self.objects_backup[k] = { object = o.object, qty = o.qty }
620
620
  end
621
 
  
 
621
 
622
622
  UIPlaceObjects.removeAllObjects(self, true)
623
 
  
 
623
 
624
624
  -- Remove walls
625
625
  local function remove_wall_line(x, y, step_x, step_y, n_steps, layer, neigh_x, neigh_y)
626
626
    for i = 1, n_steps do
653
653
  remove_wall_line(room.x, room.y, 1, 0, room.width , 2,  0, -1)
654
654
  remove_wall_line(room.x + room.width, room.y , 0, 1, room.height, 3, 0, 0)
655
655
  remove_wall_line(room.x, room.y + room.height, 1, 0, room.width , 2, 0, 0)
656
 
  
 
656
 
657
657
  -- Reset floor tiles and flags
658
658
  self.world.map.th:unmarkRoom(room.x, room.y, room.width, room.height)
659
 
  
 
659
 
660
660
  -- Re-create blueprint
661
661
  local rect = self.blueprint_rect
662
662
  local old_w, old_h = rect.w, rect.h
663
663
  rect.w = 0
664
664
  rect.h = 0
665
665
  self:setBlueprintRect(rect.x, rect.y, old_w, old_h)
666
 
  
 
666
 
667
667
  -- We've gone all the way back to wall phase, so step forward to door phase
668
668
  self.phase = "door"
669
669
  self:enterDoorPhase()
674
674
  cellx = math_floor(cellx)
675
675
  celly = math_floor(celly)
676
676
  local rect = self.blueprint_rect
677
 
  
 
677
 
678
678
  if cellx == rect.x or cellx == rect.x - 1 or cellx == rect.x + rect.w or cellx == rect.x + rect.w - 1 or
679
679
     celly == rect.y or celly == rect.y - 1 or celly == rect.y + rect.h or celly == rect.y + rect.h - 1 then
680
680
  else
681
681
    return
682
682
  end
683
 
  
 
683
 
684
684
  -- NB: Doors and windows cannot be placed on corner tiles, hence walls of corner tiles
685
685
  -- are never returned, and the nearest non-corner wall is returned instead. If they
686
686
  -- could be placed on corner tiles, then you would have to consider the interaction of
761
761
    if cellx == rect.x then
762
762
      cellx = rect.x + 1
763
763
    elseif cellx == rect.x + rect.w - 1 then
764
 
      cellx = rect.x + rect.w - 2 
 
764
      cellx = rect.x + rect.w - 2
765
765
    end
766
766
    return cellx, rect.y + rect.h - 1, "south"
767
767
  end
772
772
function UIEditRoom:checkReachability()
773
773
  local map = self.ui.app.map.th
774
774
  local world = self.ui.app.world
775
 
  
 
775
 
776
776
  local rect = self.blueprint_rect
777
777
  local prev_x, prev_y
778
778
  local x, y = rect.x, rect.y - 1
779
779
  local flags = {}
780
 
  
 
780
 
781
781
  local function check(flag)
782
782
    if map:getCellFlags(x, y, flags).passable and flags[flag] then
783
783
      if prev_x and not world:getPathDistance(prev_x, prev_y, x, y) then
787
787
    end
788
788
    return true
789
789
  end
790
 
  
 
790
 
791
791
  while x < rect.x + rect.w do
792
792
    if not check"travelSouth" then return false end
793
793
    x = x + 1
807
807
    if not check"travelEast" then return false end
808
808
    y = y - 1
809
809
  end
810
 
  
 
810
 
811
811
  return true
812
812
end
813
813
 
819
819
      self.ui.app.map:setCellFlags(x, y, {passable = false})
820
820
    end
821
821
  end
822
 
  
 
822
 
823
823
  -- check if all adjacent tiles of the rooms are still connected
824
824
  if not self:checkReachability() then
825
825
    -- undo passable flags and go back to walls phase
829
829
    self.ui.adviser:say(_A.room_forbidden_non_reachable_parts)
830
830
    return
831
831
  end
832
 
  
 
832
 
833
833
  self.desc_text = _S.place_objects_window.place_door
834
834
  self.confirm_button:enable(false) -- Confirmation is via placing door
835
 
  
 
835
 
836
836
  -- Change the floor tiles to opaque blue
837
837
  local map = self.ui.app.map.th
838
838
  for y = self.blueprint_rect.y, self.blueprint_rect.y + self.blueprint_rect.h - 1 do
840
840
      map:setCell(x, y, 4, 24)
841
841
    end
842
842
  end
843
 
  
 
843
 
844
844
  -- Re-organise wall anims to index by x and y
845
845
  local walls = {}
846
846
  for _, wall in ipairs(self.blueprint_wall_anims) do
872
872
    self.purchase_button:enable(true)
873
873
  end
874
874
  self.pickup_button:enable(true)
875
 
  
 
875
 
876
876
  if self.objects_backup then
877
877
    self:addObjects(self.objects_backup, true)
878
878
  else
937
937
    self.cell_outline:draw(canvas, 2, x - 32, y)
938
938
    canvas:scale(1)
939
939
  end
940
 
  
 
940
 
941
941
  UIPlaceObjects.draw(self, canvas, ...)
942
942
end
943
943
 
979
979
    self.mouse_down_x = false
980
980
    self.mouse_down_y = false
981
981
  end
982
 
  
 
982
 
983
983
  if self.move_rect_x then
984
984
    self.move_rect_x = false
985
985
    self.move_rect_y = false
986
986
  end
987
 
  
 
987
 
988
988
  return UIPlaceObjects.onMouseUp(self, button, x, y)
989
989
end
990
990
 
999
999
  local map = self.ui.app.map
1000
1000
  if x + w > map.width  then w = map.width  - x end
1001
1001
  if y + h > map.height then h = map.height - y end
1002
 
  
 
1002
 
1003
1003
  if rect.x == x and rect.y == y and rect.w == w and rect.h == h then
1004
1004
    -- Nothing to do
1005
1005
    return
1006
1006
  end
1007
 
  
 
1007
 
1008
1008
  local too_small = w < self.room_type.minimum_size or h < self.room_type.minimum_size
1009
 
  
 
1009
 
1010
1010
  -- Entire update of floor tiles and wall animations done in C to replace
1011
1011
  -- several hundred calls into C with just a single call. The price for this
1012
1012
  -- is reduced flexibility. See l_map_updateblueprint in th_lua.cpp for code.
1025
1025
      self.ui:tutorialStep(3, {4, 5, 6}, 8)
1026
1026
    end
1027
1027
  end
1028
 
  
 
1028
 
1029
1029
  self.confirm_button:enable(is_valid)
1030
 
  
 
1030
 
1031
1031
  rect.x = x
1032
1032
  rect.y = y
1033
1033
  rect.w = w
1052
1052
  local orig_x = x
1053
1053
  local orig_y = y
1054
1054
  local orig_wall = wall
1055
 
  
 
1055
 
1056
1056
  -- Used to get the adjacent tiles when placing swing doors.
1057
1057
  local x_mod
1058
1058
  local y_mod
1070
1070
    y_mod = 2
1071
1071
  end
1072
1072
  local map = self.ui.app.map.th
1073
 
  
 
1073
 
1074
1074
  if self.blueprint_door.anim then
1075
1075
    if self.room_type.swing_doors then
1076
1076
      if self.blueprint_door.anim[1] then
1137
1137
    end
1138
1138
    -- If it is a swing door there are two more locations to check.
1139
1139
    if self.room_type.swing_doors then
1140
 
      if map:getCell(x, y - 1, 3) % 0x100 ~= 0 
 
1140
      if map:getCell(x, y - 1, 3) % 0x100 ~= 0
1141
1141
      or map:getCell(x, y + 1, 3) % 0x100 ~= 0 then
1142
1142
        self.blueprint_door.valid = false
1143
1143
      end
1150
1150
    end
1151
1151
    -- If it is a swing door there are two more locations to check.
1152
1152
    if self.room_type.swing_doors then
1153
 
      if map:getCell(x - 1, y, 2) % 0x100 ~= 0 
 
1153
      if map:getCell(x - 1, y, 2) % 0x100 ~= 0
1154
1154
      or map:getCell(x + 1, y, 2) % 0x100 ~= 0 then
1155
1155
        self.blueprint_door.valid = false
1156
1156
      end
1159
1159
  if self.blueprint_door.valid then
1160
1160
    -- Ensure that the door isn't being built on top of an object
1161
1161
    local flags = {}
1162
 
    local flag_names 
 
1162
    local flag_names
1163
1163
    if wall == "west" then
1164
1164
      flag_names = {"buildableNorth", "buildableSouth"}
1165
1165
    else
1195
1195
    anim:setAnimation(self.anims, 126, flags)
1196
1196
  end
1197
1197
  if self.blueprint_door.valid then
1198
 
    map:setCell(self.blueprint_door.floor_x, self.blueprint_door.floor_y, 4, 
 
1198
    map:setCell(self.blueprint_door.floor_x, self.blueprint_door.floor_y, 4,
1199
1199
      door_floor_blueprint_markers[orig_wall])
1200
1200
  end
1201
1201
end
1213
1213
  local orig_x = x
1214
1214
  local orig_y = y
1215
1215
  local orig_wall = wall
1216
 
  
 
1216
 
1217
1217
  if wall == "south" then
1218
1218
    y = y + 1
1219
1219
    wall = "north"
1221
1221
    x = x + 1
1222
1222
    wall = "west"
1223
1223
  end
1224
 
  
 
1224
 
1225
1225
  local map = self.ui.app.map.th
1226
1226
  local world = self.ui.app.world
1227
 
  
 
1227
 
1228
1228
  if self.blueprint_window.anim then
1229
1229
    self.blueprint_window.anim:setAnimation(self.anims, self.blueprint_window.old_anim,
1230
1230
      self.blueprint_window.old_flags)
1232
1232
    self.blueprint_window.anim = nil
1233
1233
    map:setCell(self.blueprint_window.floor_x, self.blueprint_window.floor_y, 4, 24)
1234
1234
  end
1235
 
  
 
1235
 
1236
1236
  local anim = x and self.blueprint_wall_anims[x][y]
1237
1237
  if anim and anim:getTag() then
1238
1238
    x, y, wall, orig_x, orig_y, orig_wall = nil
1239
1239
  end
1240
 
  
 
1240
 
1241
1241
  self.blueprint_window.x = x
1242
1242
  self.blueprint_window.y = y
1243
1243
  self.blueprint_window.wall = wall
1247
1247
  if not wall then
1248
1248
    return
1249
1249
  end
1250
 
  
 
1250
 
1251
1251
  if anim ~= self.blueprint_window.anim then
1252
1252
    self.blueprint_window.anim = anim
1253
1253
    self.blueprint_window.anim:setTag"window"
1271
1271
  end
1272
1272
  anim:setAnimation(self.anims, 130, flags)
1273
1273
  if self.blueprint_window.valid then
1274
 
    map:setCell(self.blueprint_window.floor_x, self.blueprint_window.floor_y, 4, 
 
1274
    map:setCell(self.blueprint_window.floor_x, self.blueprint_window.floor_y, 4,
1275
1275
      window_floor_blueprint_markers[orig_wall])
1276
1276
  end
1277
1277
end
1278
1278
 
1279
1279
function UIEditRoom:onCursorWorldPositionChange(x, y)
1280
1280
  local repaint = UIPlaceObjects.onCursorWorldPositionChange(self, x, y)
1281
 
  
 
1281
 
1282
1282
  local ui = self.ui
1283
 
  
 
1283
 
1284
1284
  -- Is the game paused?
1285
1285
  if not self.world.user_actions_allowed or self.confirm_dialog_open then
1286
1286
    ui:setCursor(ui.default_cursor)
1289
1289
  local wx, wy = ui:ScreenToWorld(self.x + x, self.y + y)
1290
1290
  wx = math_floor(wx)
1291
1291
  wy = math_floor(wy)
1292
 
  
 
1292
 
1293
1293
  if self.phase == "walls" then
1294
1294
    local rect = self.blueprint_rect
1295
1295
    if not self.mouse_down_x then
1312
1312
          w = (wx == rect.x),
1313
1313
          e = (wx == rect.x + rect.w - 1) and not (wx == rect.x),
1314
1314
        }
1315
 
        
 
1315
 
1316
1316
        if (self.resize_rect.w or self.resize_rect.e) and (self.resize_rect.n or self.resize_rect.s) then
1317
1317
          ui:setCursor(ui.app.gfx:loadMainCursor("nswe_arrow"))
1318
1318
        elseif self.resize_rect.w or self.resize_rect.e then
1328
1328
      self:setDoorBlueprint(cell_x, cell_y, wall)
1329
1329
    elseif self.phase == "windows" then
1330
1330
      self:setWindowBlueprint(cell_x, cell_y, wall)
1331
 
    end    
 
1331
    end
1332
1332
  end
1333
 
  
 
1333
 
1334
1334
  if self.mouse_down_x and self.move_rect then
1335
1335
    local rect = self.blueprint_rect
1336
1336
    self:setBlueprintRect(wx - self.move_rect_x, wy - self.move_rect_y, rect.w, rect.h)
1364
1364
    if y1 > y2 then y1, y2 = y2, y1 end
1365
1365
    self:setBlueprintRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1)
1366
1366
  end
1367
 
  
 
1367
 
1368
1368
  if wx ~= self.mouse_cell_x or wy ~= self.mouse_cell_y then
1369
1369
    repaint = true
1370
1370
  end
1371
1371
  self.mouse_cell_x = wx
1372
1372
  self.mouse_cell_y = wy
1373
 
  
 
1373
 
1374
1374
  return repaint
1375
1375
end
1376
1376
 
1381
1381
  for k, v in pairs(self.room.room_info.objects_needed) do
1382
1382
    needed[k] = v
1383
1383
  end
1384
 
  
 
1384
 
1385
1385
  -- subtract existing objects from the required numbers
1386
1386
  for o in pairs(self.room.objects) do
1387
1387
    local id = o.object_type.id
1392
1392
      end
1393
1393
    end
1394
1394
  end
1395
 
  
 
1395
 
1396
1396
  -- disable if there are not fulfilled requirements
1397
1397
  local confirm = not next(needed)
1398
 
  
 
1398
 
1399
1399
  if confirm then
1400
1400
    self.ui:tutorialStep(3, {13, 14}, 15)
1401
1401
  else
1402
1402
    self.ui:tutorialStep(3, {14, 15}, 13)
1403
1403
  end
1404
 
  
 
1404
 
1405
1405
  self.confirm_button:enable(confirm)
1406
1406
  return confirm
1407
1407
end