Debian Codegraph @

CorsixTH/Lua/dialogs/fullscreen/progress_report.lua

raw
1 --[[ Copyright (c) 2010 Edvin "Lego3" Linge
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 --! Progress Report fullscreen window (check level goals, competitors and alerts).
22 class "UIProgressReport" (UIFullscreen)
23
24 ---@type UIProgressReport
25 local UIProgressReport = _G["UIProgressReport"]
26
27 function UIProgressReport:UIProgressReport(ui)
28 self:UIFullscreen(ui)
29
30 local world = self.ui.app.world
31 local gfx = ui.app.gfx
32
33 if not pcall(function()
34 local palette = gfx:getPalette("Rep01V.pal")
35
36 self.background = gfx:loadRaw("Rep01V", 640, 480, "QData", "QData", "Rep01V.pal", true)
37 self.red_font = gfx:loadFontAndSpriteTable("QData", "Font101V", false, palette, { apply_ui_scale = true })
38 self.normal_font = gfx:loadFontAndSpriteTable("QData", "Font100V", false, palette, { apply_ui_scale = true })
39 self.small_font = gfx:loadFontAndSpriteTable("QData", "Font106V", nil, nil, { apply_ui_scale = true })
40 -- Load all sprite tables needed for all goal icons
41 self.panel_sprites_table = {
42 MPointer = gfx:loadSpriteTable("Data", "MPointer"),
43 Rep02V = gfx:loadSpriteTable("QData", "Rep02V", true, palette)
44 }
45 self.panel_sprites = self.panel_sprites_table.Rep02V -- The default goals icons
46 end) then
47 ui:addWindow(UIInformation(ui, {_S.errors.dialog_missing_graphics}))
48 self:close()
49 return
50 end
51
52 self.default_button_sound = "selectx.wav"
53
54 -- Selected hospital number
55 self.selected = 1
56 local hospital = world.hospitals[self.selected]
57
58 -- Collect which criteria to show here, draw columns in draw
59 -- Add the icons for the criteria
60 local x = 263
61 local crit_data = world.endconditions:generateReportTable(hospital)
62 for _, crit_table in ipairs(crit_data) do
63 crit_table.visible = true
64 local crit_name = crit_table.name
65 local res_value = crit_table.win_value
66 local cur_value = world.endconditions:getAttribute(hospital, crit_name)
67 if crit_table.lose_value then
68 crit_table.red = true
69 res_value = crit_table.lose_value
70 end
71 -- FIXME: res_value and cure_value are depersisted as floating points, using
72 -- string.format("%.0f", x) is not suitable due to %d (num) param in _S string
73 local tooltip
74 if crit_table.two_tooltips then
75 local direction = crit_table.win_value and "over" or "under"
76 tooltip = _S.tooltip.status[direction][crit_name]
77 else
78 tooltip = _S.tooltip.status[crit_name]
79 end
80 if crit_table.formats == 2 then
81 tooltip = tooltip:format(math.floor(res_value), math.floor(cur_value))
82 elseif crit_table.formats == 3 then
83 tooltip = tooltip:format(math.floor(res_value) / 1000,
84 math.floor(cur_value) / 1000)
85 else
86 tooltip = tooltip:format(math.floor(res_value))
87 end
88 if not crit_table.icon_file then -- Icons from QData/Rep02V
89 self:addPanel(crit_table.icon, x, 240)
90 end
91 self:makeTooltip(tooltip, x, 180, x + 30, 180 + 90)
92 x = x + 30
93 end
94 self.crit_data = crit_data
95
96 self:addPanel(0, 606, 447):makeButton(0, 0, 26, 26, 8, self.close):setTooltip(_S.tooltip.status.close)
97
98 -- Own and competitor hospital buttons
99 local function btn_handler(num)
100 return --[[persistable:progress_report_hospital_button]] function()
101 self.selected = num
102 end
103 end
104 local function tooltip(num)
105 return (num == 1) and _S.tooltip.status.win_progress_own or
106 _S.tooltip.status.win_progress_other:format(world.hospitals[num].name) .. " " .. _S.misc.not_yet_implemented
107 end
108 local function make_hosp_button(num)
109 self:addPanel(0, 265, 71 + (num - 1) * 25)
110 :makeButton(0, 0, 147, 20, 9, btn_handler(num))
111 :setTooltip(tooltip(num))
112 :enable(num == 1)
113 end
114
115 for i = 1, math.min(#world.hospitals, 4) do
116 make_hosp_button(i)
117 end
118
119 self:makeTooltip(_S.tooltip.status.population_chart .. " " .. _S.misc.not_yet_implemented, 433, 64, 578, 179)
120 self:makeTooltip(_S.tooltip.status.happiness, 433, 179, 578, 209)
121 self:makeTooltip(_S.tooltip.status.thirst, 433, 209, 578, 239)
122 self:makeTooltip(_S.tooltip.status.warmth, 433, 239, 578, 270)
123
124 self.warning = self:addPanel(7, 252, 295)
125 self.warning.visible = false
126 -- TODO: 6 gray
127 end
128
129 function UIProgressReport:close()
130 UIFullscreen.close(self)
131 self.ui:getWindow(UIBottomPanel):updateButtonStates()
132 end
133
134 function UIProgressReport:drawMarkers(canvas, x, y)
135 local s = TheApp.config.ui_scale
136 local x_min = 455 * s
137 local x_max = 551 * s
138
139 local width = x_max - x_min
140 local happiness = self.ui.hospital:getAveragePatientAttribute("happiness", 0.5)
141 local thirst = 1 - self.ui.hospital:getAveragePatientAttribute("thirst", 0.5)
142 local warmth = self.ui.hospital:getAveragePatientAttribute("warmth", nil)
143 warmth = UIPatient.normaliseWarmth(warmth)
144
145 self.panel_sprites:draw(canvas, 5, math.floor(x + x_min + width * happiness), y + 193 * s, { scaleFactor = s })
146 self.panel_sprites:draw(canvas, 5, math.floor(x + x_min + width * thirst), y + 223 * s, { scaleFactor = s })
147 self.panel_sprites:draw(canvas, 5, math.floor(x + x_min + width * warmth), y + 254 * s, { scaleFactor = s })
148
149 local world = self.ui.app.world
150 if world.free_build_mode then
151 self.normal_font:drawWrapped(canvas, _S.progress_report.free_build, x + 265 * s, y + 194 * s, 150 * s, "center")
152 end
153
154 -- Possibly show warning that it's too cold, too hot, patients not happy
155 -- or if there's need to build drink machines as folks are thirsty. Only show one at a time though!
156 -- TODO the levels may need adjustment
157 local msg = self.ui.hospital.show_progress_screen_warnings
158 if warmth < 0.3 and msg == 1 then
159 self.warning.visible = true
160 self.normal_font:drawWrapped(canvas, _S.progress_report.too_cold, x + 285 * s, y + 285 * s, 285 * s)
161 elseif warmth > 0.7 and msg == 1 then
162 self.warning.visible = true
163 self.normal_font:drawWrapped(canvas, _S.progress_report.too_hot, x + 285 * s, y + 285 * s, 285 * s)
164 elseif thirst > 0.7 and msg == 2 then
165 self.warning.visible = true
166 self.normal_font:drawWrapped(canvas, _S.progress_report.more_drinks_machines, x + 285 * s, y + 285 * s, 285 * s)
167 elseif happiness < 0.8 and happiness >= 0.6 and msg == 3 then
168 self.warning.visible = true
169 self.normal_font:drawWrapped(canvas, _S.progress_report.quite_unhappy, x + 285 * s, y + 285 * s, 285 * s)
170 elseif happiness < 0.6 and msg == 3 then
171 self.warning.visible = true
172 self.normal_font:drawWrapped(canvas, _S.progress_report.very_unhappy, x + 285 * s, y + 285 * s, 285 * s)
173 else
174 self.warning.visible = false
175 end
176 end
177
178 function UIProgressReport:draw(canvas, x, y)
179 local s = TheApp.config.ui_scale
180 canvas:scale(s, "bitmap")
181 self.background:draw(canvas, self.x * s + x, self.y * s + y)
182 canvas:scale(1, "bitmap")
183 UIFullscreen.draw(self, canvas, x, y)
184
185 x, y = self.x * s + x, self.y * s + y
186 local world = self.ui.app.world
187 local hospital = world.hospitals[self.selected]
188
189 -- Names of the players playing
190 local ly = 73 * s
191 for pnum, player in ipairs(world.hospitals) do
192 local font = (pnum == self.selected) and self.red_font or self.normal_font
193 font:draw(canvas, player.name:upper(), x + 272 * s, y + ly)
194 ly = ly + 25 * s
195 end
196
197 -- Draw the vertical bars for the selected conditions
198 local lx = 270 * s
199 for _, crit_table in ipairs(self.crit_data) do
200 if crit_table.visible then
201 local sprite_offset = crit_table.red and 2 or 0
202 local crit_name = crit_table.name
203 local cur_value = world.endconditions:getAttribute(hospital, crit_name)
204 local height
205 if crit_table.red then
206 local lose = crit_table.lose_value
207 height = s + 49 * (1 - ((cur_value - lose)/(crit_table.boundary - lose))) * s
208 else
209 height = s + 49 * (cur_value/crit_table.win_value) * s
210 end
211 if height > 50 * s then height = 50 * s end
212 local result_y = 0
213 for dy = 0, height - 1, 1 do
214 self.panel_sprites:draw(canvas, 1 + sprite_offset, x + lx, y + 237 * s - dy, { scaleFactor = s })
215 result_y = result_y + 1
216 end
217 self.panel_sprites:draw(canvas, 2 + sprite_offset, x + lx, y + 237 * s - result_y, { scaleFactor = s })
218 if crit_table.icon_file then -- Icons not from QData/Rep02V
219 local icon_sprites = self.panel_sprites_table[crit_table.icon_file]
220 icon_sprites:draw(canvas, crit_table.icon, x + lx, y + 240 * s, { scaleFactor = s, flags = DrawFlags.Nearest })
221 end
222 lx = lx + 30 * s
223 end
224 end
225
226 self:drawMarkers(canvas, x, y)
227
228 self.normal_font:draw(canvas, _S.progress_report.header .. " " ..
229 (world:date():year() + 1999), x + 227 * s, y + 40 * s, 400 * s, 0)
230 self.small_font:draw(canvas, _S.progress_report.win_criteria:upper(), x + 263 * s, y + 172 * s)
231 self.small_font:draw(canvas, _S.progress_report.percentage_pop:upper() .. " " ..
232 (hospital.population * 100) .. "%", x + 450 * s, y + 65 * s)
233 end
234
235 function UIProgressReport:afterLoad(old, new)
236 if old < 206 then
237 self:close()
238 end
239
240 if old < 236 then
241 local gfx = TheApp.gfx
242 local palette = gfx:getPalette("Rep01V.pal")
243
244 self.background = gfx:loadRaw("Rep01V", 640, 480, "QData", "QData", "Rep01V.pal", true)
245 self.red_font = gfx:loadFontAndSpriteTable("QData", "Font101V", false, palette, { apply_ui_scale = true })
246 self.normal_font = gfx:loadFontAndSpriteTable("QData", "Font100V", false, palette, { apply_ui_scale = true })
247 self.small_font = gfx:loadFontAndSpriteTable("QData", "Font106V", nil, nil, { apply_ui_scale = true })
248 end
249
250 UIFullscreen.afterLoad(self, old, new)
251 end