| 1 |
--[[ Copyright (c) 2010 Sjors Gielen |
| 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 |
|
| 23 |
--! Town map fullscreen window (purchase land, set radiator levels, map overview). |
| 24 |
class "UITownMap" (UIFullscreen) |
| 25 |
|
| 26 |
---@type UITownMap |
| 27 |
local UITownMap = _G["UITownMap"] |
| 28 |
|
| 29 |
function UITownMap:UITownMap(ui) |
| 30 |
self:UIFullscreen(ui) |
| 31 |
|
| 32 |
local app = self.ui.app |
| 33 |
local gfx = app.gfx |
| 34 |
self.app = app |
| 35 |
|
| 36 |
if not pcall(function() |
| 37 |
local palette = gfx:getPalette("Town01V.pal") |
| 38 |
|
| 39 |
self.background = gfx:loadRaw("Town01V", 640, 480, "QData", "QData", "Town01V.pal", true) |
| 40 |
self.info_font = gfx:loadFontAndSpriteTable("QData", "Font34V", false, palette, { apply_ui_scale = true }) |
| 41 |
self.city_font = gfx:loadFontAndSpriteTable("QData", "Font31V", false, palette, { apply_ui_scale = true }) |
| 42 |
self.money_font = gfx:loadFontAndSpriteTable("QData", "Font05V", nil, nil, { apply_ui_scale = true }) |
| 43 |
self.panel_sprites = gfx:loadSpriteTable("QData", "Town02V", true, palette) |
| 44 |
end) then |
| 45 |
ui:addWindow(UIInformation(ui, {_S.errors.dialog_missing_graphics})) |
| 46 |
self:close() |
| 47 |
return |
| 48 |
end |
| 49 |
|
| 50 |
self.default_button_sound = "selectx.wav" |
| 51 |
self.default_buy_sound = "buy.wav" |
| 52 |
|
| 53 |
local config = self:initRuntimeConfig() |
| 54 |
|
| 55 |
-- A list of areas in the town, including the owner. |
| 56 |
-- In single player there are only bought and available areas, in multiplayer |
| 57 |
-- areas are owned by players and when a player wants to buy a piece of |
| 58 |
-- terrain, an auction is started. |
| 59 |
-- TODO display the areas, in the right color |
| 60 |
-- TODO display everything in the areas |
| 61 |
-- TODO multiplayer mode |
| 62 |
|
| 63 |
-- NB: original TH closed the town map on right click of balance button. |
| 64 |
-- This is likely a bug and we do not copy this behavior. |
| 65 |
self:addPanel(0, 30, 420):makeButton(0, 0, 200, 50, 0, self.bankManager, nil, self.bankStats):setTooltip(_S.tooltip.town_map.balance) |
| 66 |
self:addPanel(0, 594, 437):makeButton(0, 0, 26, 26, 8, self.close):setTooltip(_S.tooltip.town_map.close) |
| 67 |
self:addPanel(0, 171, 315):makeButton(0, 0, 20, 20, 6, self.increaseHeat):setTooltip(_S.tooltip.town_map.heat_inc) |
| 68 |
self:addPanel(0, 70, 314):makeButton(0, 0, 20, 20, 7, self.decreaseHeat):setTooltip(_S.tooltip.town_map.heat_dec) |
| 69 |
|
| 70 |
-- add the toggle buttons |
| 71 |
local function toggle_button(sprite, x, y, option, str) |
| 72 |
local panel = self:addPanel(sprite, x, y) |
| 73 |
local btn = panel:makeToggleButton(0, 0, 46, 46, 0, --[[persistable:town_map_config_button]] function(_, state) |
| 74 |
app.runtime_config.town_dialog[option] = state |
| 75 |
end):setTooltip(str) |
| 76 |
btn:setToggleState(config[option]) |
| 77 |
end |
| 78 |
toggle_button(1, 140, 37, "people_enabled", _S.tooltip.town_map.people) |
| 79 |
toggle_button(2, 140, 89, "plants_enabled", _S.tooltip.town_map.plants) |
| 80 |
toggle_button(3, 140, 141, "fire_ext_enabled", _S.tooltip.town_map.fire_extinguishers) |
| 81 |
toggle_button(4, 140, 193, "objects_enabled", _S.tooltip.town_map.objects) |
| 82 |
toggle_button(5, 140, 246, "radiators_enabled", _S.tooltip.town_map.radiators) |
| 83 |
|
| 84 |
self:makeTooltip(_S.tooltip.town_map.heat_level, 94, 318, 167, 331) |
| 85 |
self:makeTooltip(_S.tooltip.town_map.heating_bill, 72, 351, 167, 374) |
| 86 |
end |
| 87 |
|
| 88 |
function UITownMap:initRuntimeConfig() |
| 89 |
-- config is a *runtime* configuration list; re-instantiations of the dialog |
| 90 |
-- share the same values, but it's not saved across saves or sessions |
| 91 |
local config = self.app.runtime_config.town_dialog |
| 92 |
if config == nil then |
| 93 |
config = {} |
| 94 |
self.app.runtime_config.town_dialog = config |
| 95 |
config.people_enabled = true |
| 96 |
config.plants_enabled = true |
| 97 |
config.fire_ext_enabled = true |
| 98 |
config.objects_enabled = true |
| 99 |
config.radiators_enabled = true |
| 100 |
end |
| 101 |
return config |
| 102 |
end |
| 103 |
|
| 104 |
function UITownMap:close() |
| 105 |
UIFullscreen.close(self) |
| 106 |
self.ui:getWindow(UIBottomPanel):updateButtonStates() |
| 107 |
end |
| 108 |
|
| 109 |
local flag_cache = {} |
| 110 |
function UITownMap:onMouseMove(x, y, dx, dy) |
| 111 |
local s = TheApp.config.ui_scale |
| 112 |
local tx = math.floor((x - 227 * s) / (3 * s)) |
| 113 |
local ty = math.floor((y - 25 * s) / (3 * s)) |
| 114 |
self.hover_plot = nil |
| 115 |
local map = self.ui.hospital.world.map.th |
| 116 |
local width, height = map:size() |
| 117 |
if 0 <= tx and tx < width and 0 <= ty and ty < height then |
| 118 |
self.hover_plot = map:getCellFlags(tx + 1, ty + 1, flag_cache).parcelId |
| 119 |
end |
| 120 |
return UIFullscreen.onMouseMove(self, x, y) |
| 121 |
end |
| 122 |
|
| 123 |
function UITownMap:onMouseUp(button, x, y) |
| 124 |
local redraw = false |
| 125 |
local s = TheApp.config.ui_scale |
| 126 |
if button == "left" then |
| 127 |
local tx = math.floor((x - 227 * s) / (3 * s)) |
| 128 |
local ty = math.floor((y - 25 * s) / (3 * s)) |
| 129 |
local map = self.ui.hospital.world.map.th |
| 130 |
local width, height = map:size() |
| 131 |
if 0 <= tx and tx < width and 0 <= ty and ty < height then |
| 132 |
local plot = map:getCellFlags(tx + 1, ty + 1, flag_cache).parcelId |
| 133 |
if plot ~= 0 then |
| 134 |
if self.ui.hospital:purchasePlot(plot) then |
| 135 |
self.ui:playSound("cashreg.wav") |
| 136 |
redraw = true |
| 137 |
else |
| 138 |
self.ui:playSound("Wrong2.wav") |
| 139 |
end |
| 140 |
end |
| 141 |
end |
| 142 |
elseif button == "right" then |
| 143 |
local tx = math.floor((x - 227 * s) / (3 * s)) |
| 144 |
local ty = math.floor((y - 25 * s) / (3 * s)) |
| 145 |
local map = self.ui.hospital.world.map.th |
| 146 |
local width, height = map:size() |
| 147 |
if 0 <= tx and tx < width and 0 <= ty and ty < height then |
| 148 |
local plot = map:getCellFlags(tx + 1, ty + 1, flag_cache).parcelId |
| 149 |
if plot ~= 0 then |
| 150 |
local sx, sy = self.ui.app.map:WorldToScreen(tx, ty) |
| 151 |
self.ui:scrollMapTo(sx, sy) |
| 152 |
self:close() |
| 153 |
end |
| 154 |
end |
| 155 |
end |
| 156 |
|
| 157 |
return UIFullscreen.onMouseUp(self, button, x, y) or redraw |
| 158 |
end |
| 159 |
|
| 160 |
function UITownMap:draw(canvas, x, y) |
| 161 |
local s = TheApp.config.ui_scale |
| 162 |
canvas:scale(s, "bitmap") |
| 163 |
self.background:draw(canvas, self.x * s + x, self.y * s + y) |
| 164 |
canvas:scale(1, "bitmap") |
| 165 |
UIFullscreen.draw(self, canvas, x, y) |
| 166 |
|
| 167 |
x, y = self.x * s + x, self.y * s + y |
| 168 |
local app = self.ui.app |
| 169 |
local hospital = self.ui.hospital |
| 170 |
local world = hospital.world |
| 171 |
local map = world.map |
| 172 |
-- config is a *runtime* configuration list; re-instantiations of the dialog |
| 173 |
-- share the same values, but it's not saved across saves or sessions |
| 174 |
local config = app.runtime_config.town_dialog |
| 175 |
if not config then |
| 176 |
config = self:initRuntimeConfig() |
| 177 |
end |
| 178 |
|
| 179 |
-- We need to draw number of people, plants, fire extinguishers, other objects |
| 180 |
-- and radiators. |
| 181 |
-- NB: original TH's patient count was always 1 too big (started counting at 1) |
| 182 |
-- This is likely a bug and we do not copy this behavior. |
| 183 |
local patientcount = hospital:countPatients() |
| 184 |
local plants = hospital:countPlants() |
| 185 |
local fireext = hospital:countFireExtinguishers() |
| 186 |
local objs = hospital:countGeneralObjects() |
| 187 |
local radiators = hospital:countRadiators() |
| 188 |
|
| 189 |
self.info_font:draw(canvas, patientcount, x + 95 * s, y + 57 * s) |
| 190 |
self.info_font:draw(canvas, plants, x + 95 * s, y + 110 * s) |
| 191 |
self.info_font:draw(canvas, fireext, x + 95 * s, y + 157 * s) |
| 192 |
self.info_font:draw(canvas, objs, x + 95 * s, y + 211 * s) |
| 193 |
self.info_font:draw(canvas, radiators, x + 95 * s, y + 265 * s) |
| 194 |
|
| 195 |
-- Heating costs |
| 196 |
local heating_costs = world.free_build_mode and 0 or |
| 197 |
math.floor(((hospital.heating.radiator_heat *10)* radiators)* 7.5) |
| 198 |
self.info_font:draw(canvas, ("%8i"):format(heating_costs), x + 100 * s, y + 355 * s) |
| 199 |
|
| 200 |
-- Draw balance with temporary offset in unicode languages |
| 201 |
local offset_x, offset_y = 0, 0 |
| 202 |
if self.ui.app.gfx:drawNumbersFromUnicode() then |
| 203 |
offset_x = 4 * s |
| 204 |
offset_y = 2 * s |
| 205 |
end |
| 206 |
local balance = math.floor(hospital.balance) |
| 207 |
local i = 7 - tostring(balance):len() -- Indent balances under 100k |
| 208 |
for digit in ("%7i"):format(balance):gmatch("[-0-9]") do |
| 209 |
self.money_font:draw(canvas, digit, x + offset_x + 49 * s + i * 13 * s, y + offset_y + 431 * s) |
| 210 |
i = i + 1 |
| 211 |
end |
| 212 |
|
| 213 |
-- radiator heat |
| 214 |
local rad_max_width = 60 * s -- Radiator indicator width |
| 215 |
local rad_width = rad_max_width * hospital.heating.radiator_heat |
| 216 |
for dx = 0, rad_width, s do |
| 217 |
self.panel_sprites:draw(canvas, 9, x + 101 * s + dx, y + 319 * s, { scaleFactor = s }) |
| 218 |
end |
| 219 |
|
| 220 |
-- city name |
| 221 |
self.city_font:draw(canvas, map.level_name, x + 300 * s, y + 43 * s, 260 * s, 15 * s) |
| 222 |
|
| 223 |
local town_map_offset_x = x + 227 * s |
| 224 |
local town_map_offset_y = y + 25 * s |
| 225 |
TH.windowHelpers.townMapDraw(self, map.th, canvas, town_map_offset_x, town_map_offset_y, |
| 226 |
config.radiators_enabled, s) |
| 227 |
|
| 228 |
-- Draw entities |
| 229 |
local function draw_entities(list, color, size) |
| 230 |
for _, ent in ipairs(list) do |
| 231 |
-- 3 is the number of pixel that are used to represent one world tile in the map |
| 232 |
canvas:drawRect(color, town_map_offset_x + ent.tile_x * 3 * s - 2 * s, |
| 233 |
town_map_offset_y + ent.tile_y * 3 * s + s, size * s, size * s) |
| 234 |
end |
| 235 |
end |
| 236 |
|
| 237 |
local function draw_entities_in_hospital(list, color, size) |
| 238 |
for _, ent in ipairs(list) do |
| 239 |
local tile_x, tile_y = ent.tile_x, ent.tile_y |
| 240 |
if tile_x and hospital:isInHospital(tile_x, tile_y) then |
| 241 |
-- 3 is the number of pixel that are used to represent one world tile in the map |
| 242 |
canvas:drawRect(color, town_map_offset_x + tile_x * 3 * s - 2 * s, |
| 243 |
town_map_offset_y + tile_y * 3 * s + s, size * s, size * s) |
| 244 |
end |
| 245 |
end |
| 246 |
end |
| 247 |
|
| 248 |
--! Select entities of the world by their id. |
| 249 |
--!param id Id of the objects to select. |
| 250 |
--!return (list) The selected entities. |
| 251 |
local function get_objects_by_id(id) |
| 252 |
local ret = {} |
| 253 |
for _, obj_list in pairs(world.objects) do |
| 254 |
for _, obj in ipairs(obj_list) do |
| 255 |
if obj.object_type.id == id then |
| 256 |
table.insert(ret, obj) |
| 257 |
end |
| 258 |
end |
| 259 |
end |
| 260 |
return ret |
| 261 |
end |
| 262 |
|
| 263 |
if config.people_enabled then |
| 264 |
local staff_color = canvas:mapRGB(97, 109, 235) |
| 265 |
local patient_color = canvas:mapRGB(255, 255, 255) |
| 266 |
draw_entities_in_hospital(self.ui.hospital.staff, staff_color, 2) |
| 267 |
draw_entities_in_hospital(self.ui.hospital.patients, patient_color, 2) |
| 268 |
end |
| 269 |
|
| 270 |
if config.radiators_enabled then |
| 271 |
local radiator_color = canvas:mapRGB(255, 0, 70) |
| 272 |
draw_entities(get_objects_by_id("radiator"), radiator_color, 1) |
| 273 |
end |
| 274 |
|
| 275 |
if config.fire_ext_enabled then |
| 276 |
local fire_ext_color = canvas:mapRGB(216, 0, 0) |
| 277 |
draw_entities(get_objects_by_id("extinguisher"), fire_ext_color, 2) |
| 278 |
end |
| 279 |
|
| 280 |
if config.plants_enabled then |
| 281 |
local plant_color = canvas:mapRGB(127, 180, 73) |
| 282 |
draw_entities(get_objects_by_id("plant"), plant_color, 2) |
| 283 |
end |
| 284 |
|
| 285 |
if config.objects_enabled then |
| 286 |
local machine_list = {} |
| 287 |
for _, obj_list in pairs(world.objects) do |
| 288 |
for _, obj in ipairs(obj_list) do |
| 289 |
if obj.object_type.show_in_town_map then |
| 290 |
table.insert(machine_list, obj) |
| 291 |
end |
| 292 |
end |
| 293 |
end |
| 294 |
|
| 295 |
local machine_color = canvas:mapRGB(142, 182, 182) |
| 296 |
draw_entities(machine_list, machine_color, 3) |
| 297 |
end |
| 298 |
|
| 299 |
-- plot number, owner, area and price |
| 300 |
local plot_num = "-" |
| 301 |
local tile_count = "-" |
| 302 |
local price = "-" |
| 303 |
local owner = "-" |
| 304 |
if self.hover_plot then |
| 305 |
if self.hover_plot == 0 then |
| 306 |
price = _S.town_map.not_for_sale |
| 307 |
else |
| 308 |
tile_count = map:getParcelTileCount(self.hover_plot) |
| 309 |
local owner_num = map.th:getPlotOwner(self.hover_plot) |
| 310 |
if owner_num == 0 then |
| 311 |
owner = _S.town_map.for_sale |
| 312 |
price = world.free_build_mode and "$0" or |
| 313 |
("$" .. map:getParcelPrice(self.hover_plot)) |
| 314 |
else |
| 315 |
owner = world.hospitals[owner_num].name |
| 316 |
end |
| 317 |
plot_num = self.hover_plot |
| 318 |
end |
| 319 |
end |
| 320 |
self.city_font:draw(canvas, _S.town_map.number, x + 227 * s, y + 435 * s) |
| 321 |
self.city_font:draw(canvas, ":", x + 300 * s, y + 435 * s) |
| 322 |
self.city_font:draw(canvas, plot_num, x + 315 * s, y + 435 * s) |
| 323 |
self.city_font:draw(canvas, _S.town_map.owner, x + 227 * s, y + 450 * s) |
| 324 |
self.city_font:draw(canvas, ":", x + 300 * s, y + 450 * s) |
| 325 |
self.city_font:draw(canvas, owner, x + 315 * s, y + 450 * s) |
| 326 |
self.city_font:draw(canvas, _S.town_map.area, x + 432 * s, y + 435 * s) |
| 327 |
self.city_font:draw(canvas, ":", x + 495 * s, y + 435 * s) |
| 328 |
self.city_font:draw(canvas, tile_count, x + 515 * s, y + 435 * s) |
| 329 |
self.city_font:draw(canvas, _S.town_map.price, x + 432 * s, y + 450 * s) |
| 330 |
self.city_font:draw(canvas, ":", x + 495 * s, y + 450 * s) |
| 331 |
self.city_font:draw(canvas, price, x + 515 * s, y + 450 * s) |
| 332 |
end |
| 333 |
|
| 334 |
function UITownMap:decreaseHeat() |
| 335 |
local h = self.ui.hospital |
| 336 |
local heat = math.floor(h.heating.radiator_heat * 10 + 0.5) |
| 337 |
if not h.heating.heating_broke then |
| 338 |
heat = math.max(heat - 1, 1) |
| 339 |
h.heating.radiator_heat = heat / 10 |
| 340 |
end |
| 341 |
end |
| 342 |
|
| 343 |
function UITownMap:increaseHeat() |
| 344 |
local h = self.ui.hospital |
| 345 |
local heat = math.floor(h.heating.radiator_heat * 10 + 0.5) |
| 346 |
if not h.heating.heating_broke then |
| 347 |
heat = math.min(heat + 1, 10) |
| 348 |
h.heating.radiator_heat = heat / 10 |
| 349 |
end |
| 350 |
end |
| 351 |
|
| 352 |
function UITownMap:bankManager() |
| 353 |
local dlg = UIBankManager(self.ui) |
| 354 |
self.ui:addWindow(dlg) |
| 355 |
self.ui:getWindow(UIBottomPanel):updateButtonStates() |
| 356 |
end |
| 357 |
|
| 358 |
function UITownMap:bankStats() |
| 359 |
local dlg = UIBankManager(self.ui) |
| 360 |
dlg:showStatistics() |
| 361 |
self.ui:addWindow(dlg) |
| 362 |
self.ui:getWindow(UIBottomPanel):updateButtonStates() |
| 363 |
end |
| 364 |
|
| 365 |
function UITownMap:afterLoad(old, new) |
| 366 |
if old < 236 then |
| 367 |
local gfx = TheApp.gfx |
| 368 |
local palette = gfx:getPalette("Town01V.pal") |
| 369 |
self.background = gfx:loadRaw("Town01V", 640, 480, "QData", "QData", "Town01V.pal", true) |
| 370 |
self.info_font = gfx:loadFontAndSpriteTable("QData", "Font34V", false, palette, { apply_ui_scale = true }) |
| 371 |
self.city_font = gfx:loadFontAndSpriteTable("QData", "Font31V", false, palette, { apply_ui_scale = true }) |
| 372 |
self.panel_sprites = gfx:loadSpriteTable("QData", "Town02V", true, palette, { apply_ui_scale = true }) |
| 373 |
end |
| 374 |
|
| 375 |
UIFullscreen.afterLoad(self, old, new) |
| 376 |
end |