-
Notifications
You must be signed in to change notification settings - Fork 354
/
town_map.lua
369 lines (323 loc) · 13.5 KB
/
town_map.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
--[[ Copyright (c) 2010 Sjors Gielen
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. --]]
local TH = require("TH")
--! Town map fullscreen window (purchase land, set radiator levels, map overview).
class "UITownMap" (UIFullscreen)
---@type UITownMap
local UITownMap = _G["UITownMap"]
function UITownMap:UITownMap(ui)
self:UIFullscreen(ui)
local app = self.ui.app
local gfx = app.gfx
self.app = app
if not pcall(function()
local palette = gfx:loadPalette("QData", "Town01V.pal", true)
self.background = gfx:loadRaw("Town01V", 640, 480, "QData", "QData", "Town01V.pal", true)
self.info_font = gfx:loadFont("QData", "Font34V", false, palette)
self.city_font = gfx:loadFont("QData", "Font31V", false, palette)
self.money_font = gfx:loadFont("QData", "Font05V")
self.panel_sprites = gfx:loadSpriteTable("QData", "Town02V", true, palette)
end) then
ui:addWindow(UIInformation(ui, {_S.errors.dialog_missing_graphics}))
self:close()
return
end
self.default_button_sound = "selectx.wav"
self.default_buy_sound = "buy.wav"
local config = self:initRuntimeConfig()
-- A list of areas in the town, including the owner.
-- In single player there are only bought and available areas, in multiplayer
-- areas are owned by players and when a player wants to buy a piece of
-- terrain, an auction is started.
-- TODO display the areas, in the right color
-- TODO display everything in the areas
-- TODO multiplayer mode
-- NB: original TH closed the town map on right click of balance button.
-- This is likely a bug and we do not copy this behavior.
self:addPanel(0, 30, 420):makeButton(0, 0, 200, 50, 0, self.bankManager, nil, self.bankStats):setTooltip(_S.tooltip.town_map.balance)
self:addPanel(0, 594, 437):makeButton(0, 0, 26, 26, 8, self.close):setTooltip(_S.tooltip.town_map.close)
self:addPanel(0, 171, 315):makeButton(0, 0, 20, 20, 6, self.increaseHeat):setTooltip(_S.tooltip.town_map.heat_inc)
self:addPanel(0, 70, 314):makeButton(0, 0, 20, 20, 7, self.decreaseHeat):setTooltip(_S.tooltip.town_map.heat_dec)
-- add the toggle buttons
local function toggle_button(sprite, x, y, option, str)
local panel = self:addPanel(sprite, x, y)
local btn = panel:makeToggleButton(0, 0, 46, 46, 0, --[[persistable:town_map_config_button]] function(_, state)
app.runtime_config.town_dialog[option] = state
end):setTooltip(str)
btn:setToggleState(config[option])
end
toggle_button(1, 140, 37, "people_enabled", _S.tooltip.town_map.people)
toggle_button(2, 140, 89, "plants_enabled", _S.tooltip.town_map.plants)
toggle_button(3, 140, 141, "fire_ext_enabled", _S.tooltip.town_map.fire_extinguishers)
toggle_button(4, 140, 193, "objects_enabled", _S.tooltip.town_map.objects)
toggle_button(5, 140, 246, "radiators_enabled", _S.tooltip.town_map.radiators)
self:makeTooltip(_S.tooltip.town_map.heat_level, 94, 318, 167, 331)
self:makeTooltip(_S.tooltip.town_map.heating_bill, 72, 351, 167, 374)
end
function UITownMap:initRuntimeConfig()
-- config is a *runtime* configuration list; re-instantiations of the dialog
-- share the same values, but it's not saved across saves or sessions
local config = self.app.runtime_config.town_dialog
if config == nil then
config = {}
self.app.runtime_config.town_dialog = config
config.people_enabled = true
config.plants_enabled = true
config.fire_ext_enabled = true
config.objects_enabled = true
config.radiators_enabled = true
end
return config
end
function UITownMap:close()
UIFullscreen.close(self)
self.ui:getWindow(UIBottomPanel):updateButtonStates()
end
local flag_cache = {}
function UITownMap:onMouseMove(x, y, dx, dy)
local tx = math.floor((x - 227) / 3)
local ty = math.floor((y - 25) / 3)
self.hover_plot = nil
local map = self.ui.hospital.world.map.th
local width, height = map:size()
if 0 <= tx and tx < width and 0 <= ty and ty < height then
self.hover_plot = map:getCellFlags(tx + 1, ty + 1, flag_cache).parcelId
end
return UIFullscreen.onMouseMove(self, x, y)
end
function UITownMap:onMouseUp(button, x, y)
local redraw = false
if button == "left" then
local tx = math.floor((x - 227) / 3)
local ty = math.floor((y - 25) / 3)
local map = self.ui.hospital.world.map.th
local width, height = map:size()
if 0 <= tx and tx < width and 0 <= ty and ty < height then
local plot = map:getCellFlags(tx + 1, ty + 1, flag_cache).parcelId
if plot ~= 0 then
if self.ui.hospital:purchasePlot(plot) then
self.ui:playSound("cashreg.wav")
redraw = true
else
self.ui:playSound("Wrong2.wav")
end
end
end
elseif button == "right" then
local tx = math.floor((x - 227) / 3)
local ty = math.floor((y - 25) / 3)
local map = self.ui.hospital.world.map.th
local width, height = map:size()
if 0 <= tx and tx < width and 0 <= ty and ty < height then
local plot = map:getCellFlags(tx + 1, ty + 1, flag_cache).parcelId
if plot ~= 0 then
local sx, sy = self.ui.app.map:WorldToScreen(tx, ty)
self.ui:scrollMapTo(sx, sy)
self:close()
end
end
end
return UIFullscreen.onMouseUp(self, button, x, y) or redraw
end
function UITownMap:draw(canvas, x, y)
self.background:draw(canvas, self.x + x, self.y + y)
UIFullscreen.draw(self, canvas, x, y)
x, y = self.x + x, self.y + y
local app = self.ui.app
local hospital = self.ui.hospital
local world = hospital.world
local map = world.map
-- config is a *runtime* configuration list; re-instantiations of the dialog
-- share the same values, but it's not saved across saves or sessions
local config = app.runtime_config.town_dialog
if not config then
config = self:initRuntimeConfig()
end
-- We need to draw number of people, plants, fire extinguishers, other objects
-- and radiators.
-- NB: original TH's patient count was always 1 too big (started counting at 1)
-- This is likely a bug and we do not copy this behavior.
local patientcount = hospital:countPatients()
local plants = hospital:countPlants()
local fireext = hospital:countFireExtinguishers()
local objs = hospital:countGeneralObjects()
local radiators = hospital:countRadiators()
self.info_font:draw(canvas, patientcount, x + 95, y + 57)
self.info_font:draw(canvas, plants, x + 95, y + 110)
self.info_font:draw(canvas, fireext, x + 95, y + 157)
self.info_font:draw(canvas, objs, x + 95, y + 211)
self.info_font:draw(canvas, radiators, x + 95, y + 265)
-- Heating costs
local heating_costs = math.floor(((hospital.heating.radiator_heat *10)* radiators)* 7.5)
self.info_font:draw(canvas, ("%8i"):format(heating_costs), x + 100, y + 355)
-- Draw balance with temporary offset in unicode languages
local offset_x, offset_y = 0, 0
if self.ui.app.gfx.language_font then
offset_x = 4
offset_y = 2
end
local balance = math.floor(hospital.balance)
local i = 7 - tostring(balance):len() -- Indent balances under 100k
for digit in ("%7i"):format(balance):gmatch("[-0-9]") do
self.money_font:draw(canvas, digit, x + offset_x + 49 + i * 13, y + offset_y + 431)
i = i + 1
end
-- radiator heat
local rad_max_width = 60 -- Radiator indicator width
local rad_width = rad_max_width * hospital.heating.radiator_heat
for dx = 0, rad_width do
self.panel_sprites:draw(canvas, 9, x + 101 + dx, y + 319)
end
-- city name
self.city_font:draw(canvas, map.level_name, x + 300, y + 43, 260, 15)
local town_map_offset_x = x + 227
local town_map_offset_y = y + 25
TH.windowHelpers.townMapDraw(self, map.th, canvas, town_map_offset_x, town_map_offset_y,
config.radiators_enabled)
-- Draw entities
local function draw_entities(list, color, size)
for _, ent in ipairs(list) do
-- 3 is the number of pixel that are used to represent one world tile in the map
canvas:drawRect(color, town_map_offset_x + ent.tile_x * 3 - 2,
town_map_offset_y + ent.tile_y * 3 + 1, size, size)
end
end
local function draw_entities_in_hospital(list, color, size)
for _, ent in ipairs(list) do
local tile_x, tile_y = ent.tile_x, ent.tile_y
if tile_x and hospital:isInHospital(tile_x, tile_y) then
-- 3 is the number of pixel that are used to represent one world tile in the map
canvas:drawRect(color, town_map_offset_x + tile_x * 3 - 2,
town_map_offset_y + tile_y * 3 + 1, size, size)
end
end
end
--! Select entities of the world by their id.
--!param id Id of the objects to select.
--!return (list) The selected entities.
local function get_objects_by_id(id)
local ret = {}
for _, obj_list in pairs(world.objects) do
for _, obj in ipairs(obj_list) do
if obj.object_type.id == id then
table.insert(ret, obj)
end
end
end
return ret
end
if config.people_enabled then
local staff_color = canvas:mapRGB(97, 109, 235)
local patient_color = canvas:mapRGB(255, 255, 255)
draw_entities_in_hospital(self.ui.hospital.staff, staff_color, 2)
draw_entities_in_hospital(self.ui.hospital.patients, patient_color, 2)
end
if config.radiators_enabled then
local radiator_color = canvas:mapRGB(255, 0, 70)
draw_entities(get_objects_by_id("radiator"), radiator_color, 1)
end
if config.fire_ext_enabled then
local fire_ext_color = canvas:mapRGB(216, 0, 0)
draw_entities(get_objects_by_id("extinguisher"), fire_ext_color, 2)
end
if config.plants_enabled then
local plant_color = canvas:mapRGB(127, 180, 73)
draw_entities(get_objects_by_id("plant"), plant_color, 2)
end
if config.objects_enabled then
local machine_list = {}
for _, obj_list in pairs(world.objects) do
for _, obj in ipairs(obj_list) do
if obj.object_type.show_in_town_map then
table.insert(machine_list, obj)
end
end
end
local machine_color = canvas:mapRGB(142, 182, 182)
draw_entities(machine_list, machine_color, 3)
end
-- plot number, owner, area and price
local plot_num = "-"
local tile_count = "-"
local price = "-"
local owner = "-"
if self.hover_plot then
if self.hover_plot == 0 then
price = _S.town_map.not_for_sale
else
tile_count = map:getParcelTileCount(self.hover_plot)
local owner_num = map.th:getPlotOwner(self.hover_plot)
if owner_num == 0 then
owner = _S.town_map.for_sale
price = "$" .. map:getParcelPrice(self.hover_plot)
else
owner = world.hospitals[owner_num].name
end
plot_num = self.hover_plot
end
end
self.city_font:draw(canvas, _S.town_map.number, x + 227, y + 435)
self.city_font:draw(canvas, ":", x + 300, y + 435)
self.city_font:draw(canvas, plot_num, x + 315, y + 435)
self.city_font:draw(canvas, _S.town_map.owner, x + 227, y + 450)
self.city_font:draw(canvas, ":", x + 300, y + 450)
self.city_font:draw(canvas, owner, x + 315, y + 450)
self.city_font:draw(canvas, _S.town_map.area, x + 432, y + 435)
self.city_font:draw(canvas, ":", x + 495, y + 435)
self.city_font:draw(canvas, tile_count, x + 515, y + 435)
self.city_font:draw(canvas, _S.town_map.price, x + 432, y + 450)
self.city_font:draw(canvas, ":", x + 495, y + 450)
self.city_font:draw(canvas, price, x + 515, y + 450)
end
function UITownMap:decreaseHeat()
local h = self.ui.hospital
local heat = math.floor(h.heating.radiator_heat * 10 + 0.5)
if not h.heating_broke then
heat = math.max(heat - 1, 1)
h.heating.radiator_heat = heat / 10
end
end
function UITownMap:increaseHeat()
local h = self.ui.hospital
local heat = math.floor(h.heating.radiator_heat * 10 + 0.5)
if not h.heating_broke then
heat = math.min(heat + 1, 10)
h.heating.radiator_heat = heat / 10
end
end
function UITownMap:bankManager()
local dlg = UIBankManager(self.ui)
self.ui:addWindow(dlg)
self.ui:getWindow(UIBottomPanel):updateButtonStates()
end
function UITownMap:bankStats()
local dlg = UIBankManager(self.ui)
dlg:showStatistics()
self.ui:addWindow(dlg)
self.ui:getWindow(UIBottomPanel):updateButtonStates()
end
function UITownMap:afterLoad(old, new)
if old < 179 then
local gfx = TheApp.gfx
local palette = gfx:loadPalette("QData", "Town01V.pal", true)
self.background = gfx:loadRaw("Town01V", 640, 480, "QData", "QData", "Town01V.pal", true)
self.info_font = gfx:loadFont("QData", "Font34V", false, palette)
self.city_font = gfx:loadFont("QData", "Font31V", false, palette)
self.panel_sprites = gfx:loadSpriteTable("QData", "Town02V", true, palette)
end
UIFullscreen.afterLoad(self, old, new)
end