Debian Codegraph @

CorsixTH/Lua/map.lua

raw
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 --! Lua extensions to the C++ THMap class
22 class "Map"
23
24 ---@type Map
25 local Map = _G["Map"]
26
27 local math_floor, tostring, table_concat
28 = math.floor, tostring, table.concat
29 local thMap = require("TH").map
30
31 function Map:Map(app)
32 self.width = false
33 self.height = false
34 self.th = thMap()
35 self.app = app
36 self.debug_text = false
37 self.debug_flags = false
38 self.debug_font = false
39 self.debug_tick_timer = 1
40 self:setTemperatureDisplayMethod(app.config.warmth_colors_display_default)
41
42 -- Difficulty of the level (string) "easy", "full", "hard".
43 -- Use map:getDifficulty() to query the value.
44 self.difficulty = nil
45 end
46
47 local flag_cache = {}
48
49 --! Get the value of the given flag from the tile at x, y in the map.
50 --!param x (int) Horizontal position of the tile to query in the map.
51 --!param x (int) Vertical position of the tile to query in the map.
52 --!param flag (string) Name of the queried flag.
53 --!return (?) value of the queried flag.
54 function Map:getCellFlag(x, y, flag)
55 return self.th:getCellFlags(math.floor(x), math.floor(y), flag_cache)[flag]
56 end
57
58 --! Get the ID of the room of the tile at x, y in the map.
59 --!param x (int) Horizontal position of the tile to query in the map.
60 --!param x (int) Vertical position of the tile to query in the map.
61 --!return ID of the room at the queried tile.
62 function Map:getRoomId(x, y)
63 return self.th:getRoomId(math.floor(x), math.floor(y))
64 end
65
66 function Map:setPlayerCount(count)
67 self.th:setPlayerCount(count)
68 end
69
70 function Map:getPlayerCount(count)
71 return self.th:getPlayerCount(count)
72 end
73
74 --! Set the camera tile for the given player on the map
75 --!param x (int) Horizontal position of tile to set camera on
76 --!param y (int) Vertical position of the tile to set the camera on
77 --!param player (int) Player number (1-4)
78 function Map:setCameraTile(x, y, player)
79 self.th:setCameraTile(x, y, player)
80 end
81
82 --! Set the heliport tile for the given player on the map
83 --!param x (int) Horizontal position of tile to set heliport on
84 --!param y (int) Vertical position of the tile to set the heliport on
85 --!param player (int) Player number (1-4)
86 function Map:setHeliportTile(x, y, player)
87 self.th:setHeliportTile(x, y, player)
88 end
89
90 --! Set how to display the room temperature in the hospital map.
91 --!param method (int) Way of displaying the temperature. See also THMapTemperatureDisplay enum.
92 --! 1=red gradients, 2=blue/green/red colour shifts, 3=yellow/orange/red colour shifts
93 function Map:setTemperatureDisplayMethod(method)
94 if method ~= 1 and method ~= 2 and method ~= 3 then
95 method = 1
96 end
97 self.temperature_display_method = method
98 self.app.config.warmth_colors_display_default = method
99 self.th:setTemperatureDisplay(method)
100 end
101
102 --! Copy the temperature display method from the Lua data, if available, else use the default.
103 function Map:registerTemperatureDisplayMethod()
104 if not self.temperature_display_method then
105 self:setTemperatureDisplayMethod(self.app.config.warmth_colors_display_default)
106 end
107 self.th:setTemperatureDisplay(self.temperature_display_method)
108 end
109
110 -- Convert between world coordinates and screen coordinates
111 -- World coordinates are (at least for standard maps) in the range [1, 128)
112 -- for both x and y, with the floor of the values giving the cell index.
113 -- Screen coordinates are pixels relative to the map origin - NOT relative to
114 -- the top-left corner of the screen (use UI:WorldToScreen and UI:ScreenToWorld
115 -- for this).
116
117 function Map:WorldToScreen(x, y)
118 if x == nil then x = 0 end
119 if y == nil then y = 0 end
120
121 -- Adjust origin from (1, 1) to (0, 0) and then linear transform by matrix:
122 -- 32 -32
123 -- 16 16
124 return 32 * (x - y), 16 * (x + y - 2)
125 end
126
127 function Map:ScreenToWorld(x, y)
128 -- Transform by matrix: (inverse of the WorldToScreen matrix)
129 -- 1/64 1/32
130 -- -1/64 1/32
131 -- And then adjust origin from (0, 0) to (1, 1)
132 y = y / 32 + 1
133 x = x / 64
134 local tile_x, tile_y = y + x, y - x
135 if self.width ~= nil and self.height ~= nil then
136 if tile_x < 1 then tile_x = 1 end
137 if tile_x > self.width then tile_x = self.width end
138 if tile_y < 1 then tile_y = 1 end
139 if tile_y > self.height then tile_y = self.height end
140 end
141 return tile_x, tile_y
142 end
143
144 local function bits(n)
145 local vals = {}
146 local m = 256
147 while m >= 1 do
148 if n >= m then
149 vals[#vals + 1] = m
150 n = n - m
151 end
152 m = m / 2
153 end
154 if vals[1] then
155 return unpack(vals)
156 else
157 return 0
158 end
159 end
160
161 --[[! Loads the specified level. If a string is passed it looks for the file with the same name
162 in the "Levels" and/or "Campaigns" folder of CorsixTH, if it is a number it tries to load
163 that level from the original game.
164 !param level (string or int) The name (or number) of the level to load. If this is a number the game assumes
165 the original game levels are considered.
166 !param level_name (string) The name of the actual map/area/hospital as written in the config file.
167 !param map_file (string) The path to the map file as supplied by the config file.
168 !param level_intro (string) If loading a custom level this message will be shown as soon as the level
169 has been loaded.
170 !return objects (table) The loaded map objects if successfully loaded
171 !return errors (string) A localised error message if objects were not successfully loaded
172 ]]
173 function Map:load(level, difficulty, level_name, map_file, level_intro, map_editor)
174 local objects
175 if not difficulty then
176 difficulty = "full"
177 end
178
179 -- Load CorsixTH base configuration for all levels.
180 local base_config = loadfile(self.app:getFullPath({"Lua", "base_config.lua"}))
181 if not base_config then
182 return nil, _S.errors.missing_corsixth_file:format("base_config.lua")
183 end
184 base_config = base_config()
185 assert(base_config, "No base config has been loaded!")
186
187 if type(level) == "number" then
188 -- Playing the original campaign.
189 local errors, data, _, result
190 self.level_number = level
191 data, errors = self:getRawData(map_file)
192 if data then
193 _, objects = self.th:load(data)
194 else
195 return nil, errors
196 end
197 self.level_name = _S.level_names[level]:upper()
198 -- Check if we're using the demo files. If we are, that special config should be loaded.
199 if self.app.using_demo_files then
200 -- Try to load our own configuration file for the demo.
201 local demo_path = self.app:getFullPath({"Levels", "demo.level"})
202 errors, result = self:loadMapConfig(demo_path, base_config, true)
203 if errors then
204 return nil, _S.errors.missing_corsixth_file:format("demo.level")
205 end
206 self.difficulty = "full"
207 self.level_config = result
208 else
209 errors, result = self:_loadOriginalCampaignLevel(difficulty, level, base_config)
210 if errors then
211 return nil, errors
212 end
213 self.level_config = result
214 end
215 elseif map_editor then
216 local _
217 -- We're being fed data by the map editor.
218 self.level_name = "MAP EDITOR"
219 self.level_number = "MAP EDITOR"
220 if level == "" then
221 _, objects = self.th:loadBlank()
222 else
223 local data, errors = self:getRawData(level)
224 if data then
225 _, objects = self.th:load(data)
226 else
227 return nil, errors
228 end
229 end
230 self.level_config = base_config
231 else
232 local _, result
233 -- We're loading a custom level.
234 self.level_name = level_name
235 self.level_intro = level_intro
236 self.level_number = level
237 self.level_filename = level:match(".+" .. package.config:sub(1, 1) .. "(.+)$")
238 self.map_file = map_file
239 local data, errors = self:getRawData(map_file)
240 if data then
241 _, objects = self.th:load(data)
242 else
243 return nil, errors
244 end
245 errors, result = self:loadMapConfig(level, base_config, true)
246 if errors then
247 return nil, errors
248 end
249 self.level_config = result
250 end
251
252 self.width, self.height = self.th:size()
253
254 self.parcelTileCounts = {}
255 for plot = 1, self.th:getPlotCount() do
256 self.parcelTileCounts[plot] = self.th:getParcelTileCount(plot)
257 if not map_editor then
258 self:setPlotOwner(plot, plot <= self.th:getPlayerCount() and plot or 0)
259 end
260 end
261
262 self:_fixTiles()
263 return objects
264 end
265
266 -- A set of the TH campaign levels with additional CorsixTH config files, found in CorsixTH/Levels.
267 local additional_config = list_to_set({"05", "07", "08", 11, 12})
268
269 --! Load further level configurations for the main campaign.
270 --!param difficulty (string)
271 --!param level_number (integer)
272 --!param config (table) The level config created so far
273 --!return error (string) Localised error message if error happens
274 --!return config (table) Level config, if loaded successfully
275 function Map:_loadOriginalCampaignLevel(difficulty, level_number, config)
276 local errors
277 if level_number < 10 then
278 level_number = "0" .. level_number
279 end
280 -- Add TH's base config if possible, otherwise our own config,
281 -- which roughly corresponds to "full".
282 local filename_diff = difficulty .. "00.SAM"
283 errors, config = self:loadMapConfig(filename_diff, config)
284 if errors then
285 return _S.errors.missing_th_data_file:format(filename_diff)
286 end
287 self.difficulty = difficulty
288 -- Load the Theme Hospital configuration for this difficulty and level
289 local filename_th_data = difficulty .. level_number .. ".SAM"
290 errors, config = self:loadMapConfig(filename_th_data, config)
291 if errors then
292 return _S.errors.missing_th_data_file:format(filename_th_data)
293 end
294 if additional_config[level_number] then
295 -- Load additional CorsixTH config per level.
296 local filename_cth = "original" .. level_number .. ".level"
297 local level_path = self.app:getFullPath({"Levels", filename_cth})
298 errors, config = self:loadMapConfig(level_path, config, true)
299 if errors then
300 return _S.errors.missing_corsixth_file:format(filename_cth)
301 end
302 end
303 return nil, config
304 end
305
306 --! Get the difficulty of the level. Custom levels and campaign always have medium difficulty.
307 --!return (int) difficulty of the level, 1=easy, 2=medium, 3=hard.
308 function Map:getDifficulty()
309 if self.difficulty == "easy" then return 1 end
310 if self.difficulty == "hard" then return 3 end
311 return 2
312 end
313
314 --[[! Sets the plot owner of the given plot number to the given new owner. Makes sure
315 that any room adjacent to the new plot have walls in all directions after the purchase.
316 !param plot_number (int) Number of the plot to change owner of. Plot 0 is the outside and
317 should never change owner.
318 !param new_owner (int) The player number that should own plot_number. 0 means no owner.
319 ]]
320 function Map:setPlotOwner(plot_number, new_owner)
321 local split_tiles = self.th:setPlotOwner(plot_number, new_owner)
322 for _, coordinates in ipairs(split_tiles) do
323 local x = coordinates[1]
324 local y = coordinates[2]
325
326 local _, north_wall, west_wall = self.th:getCell(x, y)
327 local cell_flags = self.th:getCellFlags(x, y)
328 local north_cell_flags = self.th:getCellFlags(x, y - 1)
329 local west_cell_flags = self.th:getCellFlags(x - 1, y)
330 local wall_dirs = {
331 south = { layer = north_wall,
332 block_id = 2,
333 room_cell_flags = cell_flags,
334 adj_cell_flags = north_cell_flags,
335 tile_cat = 'inside_tiles',
336 wall_dir = 'north'
337 },
338 north = { layer = north_wall,
339 block_id = 2,
340 room_cell_flags = north_cell_flags,
341 adj_cell_flags = cell_flags,
342 tile_cat = 'outside_tiles',
343 wall_dir = 'north'
344 },
345 east = { layer = west_wall,
346 block_id = 3,
347 room_cell_flags = cell_flags,
348 adj_cell_flags = west_cell_flags,
349 tile_cat = 'inside_tiles',
350 wall_dir = 'west'
351 },
352 west = { layer = west_wall,
353 block_id = 3,
354 room_cell_flags = west_cell_flags,
355 adj_cell_flags = cell_flags,
356 tile_cat = 'outside_tiles',
357 wall_dir = 'west'
358 },
359 }
360 for _, dir in pairs(wall_dirs) do
361 if dir.layer == 0 and dir.room_cell_flags.roomId ~= 0 and
362 dir.room_cell_flags.parcelId ~= dir.adj_cell_flags.parcelId then
363 local room = self.app.world.rooms[dir.room_cell_flags.roomId]
364 local wall_type = self.app.walls[room.room_info.wall_type][dir.tile_cat][dir.wall_dir]
365 self.th:setCell(x, y, dir.block_id, wall_type)
366 end
367 end
368 end
369 -- Refresh the map's paths and flags
370 self.th:updatePathfinding()
371 self:_fixTiles()
372 end
373
374 --[[! Saves the map to a .map file
375 !param filename (string) Name of the file to save the map in
376 ]]
377 function Map:save(filename)
378 self.th:save(filename)
379 end
380
381 -- A set of the level difficulties. Full is default
382 local correct_difficulties = list_to_set({"easy", "full", "hard"})
383
384 --[[! Loads map configurations from files.
385 !param filename (string) The absolute path to the config file to load
386 !param config (string) If a base config already exists and only some values should be overridden
387 this is the base config
388 !param custom If true The configuration file is searched for where filename points, otherwise
389 it is assumed that we're looking in the theme_hospital_install path.
390 !return error The specific localised error message, if error occurred
391 !return config The successfully loaded config
392 ]]
393 function Map:loadMapConfig(filename, config, custom)
394 local function iterator()
395 if custom then
396 return io.lines(filename)
397 else
398 return self.app.fs:readContents("Levels", filename):gmatch"[^\r\n]+"
399 end
400 end
401 if filename and (self.app.fs:readContents("Levels", filename) or io.open(filename)) then
402 for line in iterator() do
403 if line:sub(1, 1) == "#" then
404 local parts = {}
405 local nkeys = 0
406 for part in line:gmatch("%.?[-?a-zA-Z0-9%[_%]]+") do
407 if part:sub(1, 1) == "." and #parts == nkeys + 1 then
408 nkeys = nkeys + 1
409 end
410 parts[#parts + 1] = part
411 end
412 if nkeys == 0 then
413 parts[3] = parts[2]
414 parts[2] = ".Value"
415 nkeys = 1
416 end
417 for i = 2, nkeys + 1 do
418 local key = parts[1] .. parts[i]
419 local t, n
420 for match in key:gmatch("[^.%[%]]+") do
421 local name = tonumber(match) or match
422 if t then
423 if not t[n] then
424 t[n] = {}
425 end
426 t = t[n]
427 else
428 t = config
429 end
430 n = name
431 end
432 t[n] = tonumber(parts[nkeys + i]) or parts[nkeys + i]
433 end
434 end
435 end
436
437 -- If the level file overlay field gives a difficulty and number (from the TH campaign),
438 -- try to load that on top of the config loaded so far.
439 if config.overlay then
440 local difficulty, level_number = config.overlay.difficulty, config.overlay.level_number
441 local errors
442 config.overlay = nil -- Prevent recursive loop
443
444 if difficulty and level_number then
445 -- Validate the difficulty and level number
446 if not correct_difficulties[difficulty] then
447 return _S.errors.overlay.incorrect_difficulty .. difficulty
448 end
449 if type(level_number) ~= "number" or level_number < 1 or level_number > 12 then
450 return _S.errors.overlay.incorrect_level_number .. level_number
451 end
452 -- Load difficulty and level from Theme Hospital and CorsixTH configs
453 errors, config = self:_loadOriginalCampaignLevel(difficulty, level_number, config)
454 if errors then
455 return errors
456 end
457 else
458 return _S.errors.overlay.missing_setting
459 end
460 end
461 return nil, config
462 else
463 return _S.errors.missing_level_file
464 end
465 end
466
467 local temp_debug_text
468 local temp_debug_flags
469 local temp_updateDebugOverlay
470 local temp_thData
471
472 -- Keep debug information in temporary local vars, do not save them
473 function Map:prepareForSave()
474 temp_debug_text = self.debug_text
475 self.debug_text = false
476 temp_debug_flags = self.debug_flags
477 self.debug_flags = false
478 temp_updateDebugOverlay = self.updateDebugOverlay
479 self.updateDebugOverlay = nil
480 temp_thData = self.thData
481 self.thData = nil
482 end
483
484 -- Restore the temporarily stored debug information after saving
485 function Map:afterSave()
486 self.debug_text = temp_debug_text
487 temp_debug_text = nil
488 self.debug_flags = temp_debug_flags
489 temp_debug_flags = nil
490 self.updateDebugOverlay = temp_updateDebugOverlay
491 temp_updateDebugOverlay = nil
492 self.thData = temp_thData
493 temp_thData = nil
494 end
495
496 function Map:clearDebugText()
497 self.debug_text = false
498 self.debug_flags = false
499 self.updateDebugOverlay = nil
500 end
501
502 function Map:getRawData(map_file)
503 if not self.thData then
504 local data, errors
505 if not map_file then
506 data, errors = self.app:readDataFile("Levels", "Level.L".. self.level_number)
507 else
508 data, errors = self.app:readDataFile("Levels", map_file)
509 end
510 if data then
511 self.thData = data
512 else
513 return nil, errors
514 end
515 end
516 return self.thData
517 end
518
519 function Map:updateDebugOverlayFlags()
520 for x = 1, self.width do
521 for y = 1, self.height do
522 local xy = (y - 1) * self.width + x - 1
523 self.th:getCellFlags(x, y, self.debug_flags[xy])
524 end
525 end
526 end
527
528 function Map:updateDebugOverlayHeat()
529 for x = 1, self.width do
530 for y = 1, self.height do
531 local xy = (y - 1) * self.width + x - 1
532 self.debug_text[xy] = ("%02.1f"):format(self.th:getCellTemperature(x, y) * 50)
533 end
534 end
535 end
536
537 function Map:updateDebugOverlayParcels()
538 for x = 1, self.width do
539 for y = 1, self.height do
540 local xy = (y - 1) * self.width + x - 1
541 self.debug_text[xy] = self.th:getCellFlags(x, y).parcelId
542 if self.debug_text[xy] == 0 then
543 self.debug_text[xy] = ''
544 end
545 end
546 end
547 end
548
549 function Map:updateDebugOverlayCamera()
550 for x = 1, self.width do
551 for y = 1, self.height do
552 local xy = (y - 1) * self.width + x - 1
553 self.debug_text[xy] = ''
554 end
555 end
556 for p = 1, self.th:getPlayerCount() do
557 local x, y = self.th:getCameraTile(p)
558 if x and y then
559 local xy = (y - 1) * self.width + x - 1
560 self.debug_text[xy] = 'C'..p
561 end
562 end
563 end
564
565 function Map:updateDebugOverlayHeliport()
566 for x = 1, self.width do
567 for y = 1, self.height do
568 local xy = (y - 1) * self.width + x - 1
569 self.debug_text[xy] = ''
570 end
571 end
572 for p = 1, self.th:getPlayerCount() do
573 local x, y = self.th:getHeliportTile(p)
574 if x and y then
575 local xy = (y - 1) * self.width + x - 1
576 self.debug_text[xy] = 'H'..p
577 end
578 end
579 end
580
581 -- If both 'first' and 'last' are numbers, they are indices starting at 0.
582 function Map:loadDebugText(first, last, bits_)
583 self.debug_text = false
584 self.debug_flags = false
585 self.updateDebugOverlay = nil
586 if first == "flags" then
587 self.debug_flags = {}
588 for x = 1, self.width do
589 for y = 1, self.height do
590 local xy = (y - 1) * self.width + x - 1
591 self.debug_flags[xy] = {}
592 end
593 end
594 self.updateDebugOverlay = self.updateDebugOverlayFlags
595 self:updateDebugOverlay()
596 elseif first == "positions" then
597 self.debug_text = {}
598 for x = 1, self.width do
599 for y = 1, self.height do
600 local xy = (y - 1) * self.width + x - 1
601 self.debug_text[xy] = x .. "," .. y
602 end
603 end
604 elseif first == "heat" then
605 self.debug_text = {}
606 self.updateDebugOverlay = self.updateDebugOverlayHeat
607 self:updateDebugOverlay()
608 elseif first == "parcel" then
609 self.debug_text = {}
610 self.updateDebugOverlay = self.updateDebugOverlayParcels
611 self:updateDebugOverlay()
612 elseif first == "camera" then
613 self.debug_text = {}
614 self.updateDebugOverlay = self.updateDebugOverlayCamera
615 self:updateDebugOverlay()
616 elseif first == "heliport" then
617 self.debug_text = {}
618 self.updateDebugOverlay = self.updateDebugOverlayHeliport
619 self:updateDebugOverlay()
620 else
621 for x = 1, self.width do
622 for y = 1, self.height do
623 local raw = self.th:getCellRaw(x, y)
624 if bits_ then
625 self:setDebugText(x, y, bits(raw:byte(first + 1, last + 1)))
626 else
627 self:setDebugText(x, y, raw:byte(first + 1, last + 1))
628 end
629 end
630 end
631 end
632 end
633
634 function Map:onTick()
635 if self.debug_tick_timer == 1 then
636 if self.updateDebugOverlay then
637 self:updateDebugOverlay()
638 end
639 self.debug_tick_timer = 10
640 else
641 self.debug_tick_timer = self.debug_tick_timer - 1
642 end
643 end
644
645 --! Set the sprites to be used by the map.
646 --!param blocks (object) Sprite sheet for the map.
647 function Map:setBlocks(blocks)
648 self.blocks = blocks
649 self.th:setSheet(blocks)
650 end
651
652 function Map:setCellFlags(...)
653 self.th:setCellFlags(...)
654 end
655
656 function Map:setDebugFont(font)
657 self.debug_font = font
658 self.cell_outline = TheApp.gfx:loadSpriteTable("Bitmap", "aux_ui", true)
659 end
660
661 function Map:setDebugText(x, y, msg, ...)
662 if not self.debug_text then
663 self.debug_text = {}
664 end
665 local text
666 if ... then
667 text = {msg, ...}
668 for i, v in ipairs(text) do
669 text[i] = tostring(v)
670 end
671 text = table_concat(text, ",")
672 else
673 text = msg ~= 0 and msg or nil
674 end
675 self.debug_text[(y - 1) * self.width + x - 1] = text
676 end
677
678 function Map:_drawDebugTiles(canvas, x, y, xpos, ypos)
679 local xy = y * self.width + x
680 if self.debug_flags then
681 local flags = self.debug_flags[xy]
682 if flags.passable then
683 self.cell_outline:draw(canvas, 3, xpos, ypos)
684 end
685 if flags.hospital then
686 self.cell_outline:draw(canvas, 8, xpos, ypos)
687 end
688 if flags.buildable then
689 self.cell_outline:draw(canvas, 9, xpos, ypos)
690 end
691 if flags.travelNorth and self.debug_flags[xy - self.width].passable then
692 self.cell_outline:draw(canvas, 4, xpos, ypos)
693 end
694 if flags.travelEast and self.debug_flags[xy + 1].passable then
695 self.cell_outline:draw(canvas, 5, xpos, ypos)
696 end
697 if flags.travelSouth and self.debug_flags[xy + self.width].passable then
698 self.cell_outline:draw(canvas, 6, xpos, ypos)
699 end
700 if flags.travelWest and self.debug_flags[xy - 1].passable then
701 self.cell_outline:draw(canvas, 7, xpos, ypos)
702 end
703 if flags.thob ~= 0 then
704 self.debug_font:draw(canvas, "T"..flags.thob, xpos, ypos, 64, 16)
705 end
706 if flags.roomId ~= 0 then
707 self.debug_font:draw(canvas, "R"..flags.roomId, xpos, ypos + 16, 64, 16)
708 end
709 else
710 local msg = self.debug_text[xy]
711 if msg and msg ~= "" then
712 self.cell_outline:draw(canvas, 2, xpos, ypos)
713 self.debug_font:draw(canvas, msg, xpos, ypos, 64, 32)
714 end
715 end
716 end
717
718 --! Draws the rectangle of the map given by (sx, sy, sw, sh) at position (dx, dy) on the canvas
719 --!param canvas
720 --!param sx Horizontal start position at the screen.
721 --!param sy Vertical start position at the screen.
722 --!param sw (int) Width of the screen.
723 --!param sh (int) Height of the screen.
724 --!param dx (jnt) Horizontal destination at the canvas.
725 --!param dy (int) Vertical destination at the canvas.
726 --]]
727 function Map:draw(canvas, sx, sy, sw, sh, dx, dy)
728 -- All the heavy work is done by C code:
729 self.th:draw(canvas, sx, sy, sw, sh, dx, dy)
730
731 if not self.debug_font or not (self.debug_text or self.debug_flags) then return end
732 -- Draw any debug overlays
733 local startX = 0
734 local startY = math_floor((sy - 32) / 16)
735 if startY < 0 then
736 startY = 0
737 elseif startY >= self.height then
738 startX = startY - self.height + 1
739 startY = self.height - 1
740 if startX >= self.width then
741 startX = self.width - 1
742 end
743 end
744 local baseX = startX
745 local baseY = startY
746 while true do -- TODO: replace this with a scanline
747 local x = baseX
748 local y = baseY
749 local screenX = 32 * (x - y) - sx
750 local screenY = 16 * (x + y) - sy
751 local ypos = dy + screenY
752 if screenY >= sh + 70 then
753 break
754 elseif screenY > -32 then
755 repeat
756 if screenX >= -32 then
757 if screenX < sw + 32 then
758 self:_drawDebugTiles(canvas, x, y, dx + screenX - 32, ypos)
759 else
760 break
761 end
762 end
763 x = x + 1
764 y = y - 1
765 screenX = screenX + 64
766 until y < 0 or x >= self.width
767 end
768 if baseY == self.height - 1 then
769 baseX = baseX + 1
770 if baseX == self.width then
771 break
772 end
773 else
774 baseY = baseY + 1
775 end
776 end
777 end
778
779 --! Get the price of a parcel
780 --!param parcel (int) Parcel number being queried.
781 --!return Price of the queried parcel.
782 function Map:getParcelPrice(parcel)
783 local conf = self.level_config
784 conf = conf and conf.gbv
785 conf = conf and conf.LandCostPerTile
786 return self:getParcelTileCount(parcel) * (conf or 25)
787 end
788
789 --! Get the number of tiles in a parcel.
790 --!param parcel (int) Parcel number being queried.
791 --!return Number of tiles in the queried parcel.
792 function Map:getParcelTileCount(parcel)
793 return self.parcelTileCounts[parcel] or 0
794 end
795
796 --! Expandable private function to fix tile issues on loading a map.
797 --! Each fix should be rendered in a local function.
798 function Map:_fixTiles()
799 local function isOriginalCampaign()
800 return type(self.level_number) == "number" and not self.map_file
801 end
802
803 -- Fix the original campaign's outdoor tiles from being marked with bad flags
804 local function fixOutdoorTiles()
805 local function unsetFlags(cell, x, y, allowed_set, flags_to_set)
806 if not allowed_set[cell] then
807 self:setCellFlags(x, y, flags_to_set)
808 end
809 end
810
811 -- Only buildings should be part of the hospital.
812 local hosp_tiles = list_to_set( {
813 17, 70, 18, 19, 23, 16, 21, 22, 66, 76, 20 -- indoor tiles
814 } )
815 local non_hosp_flags = { hospital = false }
816
817 -- Only indoor and certain outdoor tiles should be transverable.
818 local pathable_tiles = list_to_set( {
819 17, 70, 18, 19, 23, 16, 21, 22, 66, 76, 20, -- indoor tiles
820 4, 15, 5, -- concrete paths and helipad
821 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58 -- road tiles
822 } )
823 -- Also make ineligible outdoor tiles non-buildable, which sometimes got set in
824 -- the original campaign.
825 local non_path_flags = {
826 passable = false, buildable = false, buildableNorth = false,
827 buildableEast = false, buildableSouth = false, buildableWest = false
828 }
829
830 for x=1, self.width do
831 for y=1, self.height do
832 local cell = self.th:getCell(x, y, 1)
833 unsetFlags(cell, x, y, hosp_tiles, non_hosp_flags)
834 unsetFlags(cell, x, y, pathable_tiles, non_path_flags)
835 end
836 end
837 end
838
839 -- Fix the original level 6 map
840 local function fixLevel6Flags()
841 if self.level_number ~= 6 then return end
842 self:setCellFlags(56, 71, {
843 hospital = true, buildable = true,
844 buildableNorth = true, buildableSouth = true,
845 buildableEast = true, buildableWest = true
846 })
847 self:setCellFlags(58, 72, {passable = false})
848 end
849
850 if isOriginalCampaign() then
851 fixOutdoorTiles()
852 fixLevel6Flags()
853 end
854 end
855
856 function Map:afterLoad(old, new)
857 if old < 6 then
858 self.parcelTileCounts = {}
859 for plot = 1,self.th:getPlotCount() do
860 self.parcelTileCounts[plot] = self.th:getParcelTileCount(plot)
861 end
862 end
863 if old < 18 then
864 self.difficulty = "full"
865 end
866 if old < 44 then
867 self.level_config.expertise[2].MaxDiagDiff = 700
868 self.level_config.expertise[3].MaxDiagDiff = 250
869 self.level_config.expertise[4].MaxDiagDiff = 250
870 self.level_config.expertise[5].MaxDiagDiff = 250
871 self.level_config.expertise[6].MaxDiagDiff = 250
872 self.level_config.expertise[7].MaxDiagDiff = 250
873 self.level_config.expertise[8].MaxDiagDiff = 350
874 self.level_config.expertise[9].MaxDiagDiff = 250
875 self.level_config.expertise[10].MaxDiagDiff = 250
876 self.level_config.expertise[11].MaxDiagDiff = 700
877 self.level_config.expertise[12].MaxDiagDiff = 1000
878 self.level_config.expertise[13].MaxDiagDiff = 700
879 self.level_config.expertise[14].MaxDiagDiff = 400
880 self.level_config.expertise[15].MaxDiagDiff = 350
881 self.level_config.expertise[16].MaxDiagDiff = 350
882 self.level_config.expertise[17].MaxDiagDiff = 1000
883 self.level_config.expertise[18].MaxDiagDiff = 350
884 self.level_config.expertise[19].MaxDiagDiff = 700
885 self.level_config.expertise[20].MaxDiagDiff = 700
886 self.level_config.expertise[21].MaxDiagDiff = 700
887 self.level_config.expertise[22].MaxDiagDiff = 350
888 self.level_config.expertise[23].MaxDiagDiff = 350
889 self.level_config.expertise[24].MaxDiagDiff = 700
890 self.level_config.expertise[25].MaxDiagDiff = 700
891 self.level_config.expertise[26].MaxDiagDiff = 700
892 self.level_config.expertise[27].MaxDiagDiff = 350
893 self.level_config.expertise[28].MaxDiagDiff = 700
894 self.level_config.expertise[29].MaxDiagDiff = 1000
895 self.level_config.expertise[30].MaxDiagDiff = 700
896 self.level_config.expertise[31].MaxDiagDiff = 1000
897 self.level_config.expertise[32].MaxDiagDiff = 700
898 self.level_config.expertise[33].MaxDiagDiff = 1000
899 self.level_config.expertise[34].MaxDiagDiff = 700
900 self.level_config.expertise[35].MaxDiagDiff = 700
901 end
902 if old < 57 then
903 local flags_to_set = {buildableNorth = true, buildableSouth = true, buildableWest = true, buildableEast = true}
904 for x = 1, self.width do
905 for y = 1, self.height do
906 self:setCellFlags(x, y, flags_to_set)
907 end
908 end
909 end
910 if old < 120 then
911 -- Issue #1105 update pathfinding (rebuild walls) potentially broken by side object placement
912 self.th:updatePathfinding()
913 end
914 if old < 161 then
915 -- Permanently fix the 0.65 trophy bug (#2004)
916 -- make sure we erase the hofix variable too
917 self.hotfix1 = nil
918 self.level_config.awards_trophies.TrophyAllCuredBonus = 20000
919 self.level_config.awards_trophies.AllCuresBonus = 5000
920 end
921 if old < 164 then
922 -- New feature, by default non-visual illnesses were always available
923 -- at the start
924 self.level_config.non_visuals_available = {
925 [0] = {Value = 0}, -- I_UNCOMMON_COLD
926 {Value = 0}, -- I_BROKEN_WIND
927 {Value = 0}, -- I_SPARE_RIBS
928 {Value = 0}, -- I_KIDNEY_BEANS
929 {Value = 0}, -- I_BROKEN_HEART
930 {Value = 0}, -- I_RUPTURED_NODULES
931 {Value = 0}, -- I_MULTIPLE_TV_PERSONALITIES
932 {Value = 0}, -- I_INFECTIOUS_LAUGHTER
933 {Value = 0}, -- I_CORRUGATED_ANKLES
934 {Value = 0}, -- I_CHRONIC_NOSEHAIR
935 {Value = 0}, -- I_3RD_DEGREE_SIDEBURNS
936 {Value = 0}, -- I_FAKE_BLOOD
937 {Value = 0}, -- I_GASTRIC_EJECTIONS
938 {Value = 0}, -- I_THE_SQUITS
939 {Value = 0}, -- I_IRON_LUNGS
940 {Value = 0}, -- I_SWEATY_PALMS
941 {Value = 0}, -- I_HEAPED_PILES
942 {Value = 0}, -- I_GUT_ROT
943 {Value = 0}, -- I_GOLF_STONES
944 {Value = 0}, -- I_UNEXPECTED_SWELLING
945 }
946 end
947 if old < 175 then
948 -- Set max salary value for existing saves
949 self.level_config.payroll = {
950 MaxSalary = 2000,
951 }
952 end
953 if old < 187 then
954 local gbv = self.level_config.gbv
955 gbv.Tired = gbv.Tired or 600
956 gbv.VeryTired = gbv.VeryTired or 700
957 gbv.CrackUpTired = gbv.CrackUpTired or 800
958 end
959 if old < 209 then
960 self.level_config.gbv.SodaPrice = 20
961 end
962 if old < 217 then
963 self:_fixTiles()
964 end
965 end