Debian Codegraph @

CorsixTH/Lua/dialogs/fullscreen/research_policy.lua

raw
1 --[[ Copyright (c) 2010 M.Chalon
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 class "UIResearch" (UIFullscreen)
22
23 ---@type UIResearch
24 local UIResearch = _G["UIResearch"]
25
26 local research_categories = {
27 "cure",
28 "diagnosis",
29 "drugs",
30 "improvements",
31 "specialisation",
32 }
33
34 local col_bg = {
35 red = 24,
36 green = 24,
37 blue = 20,
38 }
39
40 function UIResearch:UIResearch(ui)
41 self:UIFullscreen(ui)
42 local gfx = ui.app.gfx
43 self.background = gfx:loadRaw("Res01V", 640, 480, "QData", "QData", "Res01V.pal", true)
44 local palette = gfx:getPalette("Res01V.pal")
45 self.panel_sprites = gfx:loadSpriteTable("QData", "Res02V", true, palette)
46 self.label_font = gfx:loadFontAndSpriteTable("QData", "Font43V", false, palette, { apply_ui_scale = true })
47 self.number_font = gfx:loadFontAndSpriteTable("QData", "Font43V", false, palette, { apply_ui_scale = true })
48 self.hospital = ui.hospital
49 self.research = ui.hospital.research
50
51 self:_playBgSound()
52
53 -- stubs for backwards compatibility
54 --luacheck: push no unused
55 local --[[persistable:research_policy_adjust]] function _1() end
56 local --[[persistable:research_less_stub]] function _2() end
57 local --[[persistable:research_more_stub]] function _3() end
58 local --[[persistable:research_policy_window_reset_bg_sound]] function _4() end
59 --luacheck: pop
60
61 -- Close button
62 self:addPanel(0, 607, 447):makeButton(0, 0, 40, 40, 4, self.close):setTooltip(_S.tooltip.research.close)
63 self.adjust_buttons = {}
64 self:updateCategories()
65
66 self.waterclk = 0
67 self.ratclk = 0
68 self.waterpanel= self:addPanel(5, 2, 312)
69 self.ratpanel= self:addPanel(13, 480, 365)
70
71 -- Add tooltips to progress of research.
72 local lx = 165
73 local ly = 60
74 for _, category in ipairs(research_categories) do
75 self:makeDynamicTooltip(--[[persistable:research_policy_research_progress_tooltip]] function()
76 local research = self.research.research_policy
77 if research[category].current and not research[category].current.dummy then
78 local required = self.research:getResearchRequired(research[category].current)
79 local available = self.research.research_progress[research[category].current].points
80 return _S.tooltip.research_policy.research_progress:format(math.round(available), required)
81 else
82 return _S.tooltip.research_policy.no_research
83 end
84 end, lx, ly, lx + 315, ly + 41)
85 ly = ly + 41
86 end
87 end
88
89 function UIResearch:updateCategories()
90 -- Buttons to increase/decrease percentages
91 local size = 40
92 local topy = 21
93 local spacing = 41
94 local c1 = 372
95 local c2 = 450
96
97 local function handler_factory(area, mode)
98 return --[[persistable:research_policy_adjust_handler]] function(window)
99 window:adjustResearch(area, mode)
100 end
101 end
102
103 --[[ Retrieves a translated string dynamically
104 !param string_name (string) The key name (e.g., "tooltip.research.cure_dec")
105 !return (string) The actual translation (e.g., "Decrease cure percentage")
106 ]]
107 local function get_localized_string(string_name)
108 local var_table = _G["_S"]
109 for token in string.gmatch(string_name, "[%w_]+") do
110 var_table = var_table[token]
111 end
112 return var_table
113 end
114
115 for i, area in ipairs(research_categories) do
116 local current = self.hospital.research.research_policy[area].current
117 if current then
118 self.adjust_buttons[area] = {
119 less = self:addPanel(0, c1, topy + i * spacing)
120 :makeRepeatButton(0, 0, size, size, 1, handler_factory(area, "less"))
121 :setTooltip(get_localized_string("tooltip.research." .. area .. "_dec")),
122 more = self:addPanel(0, c2, topy + i * spacing)
123 :makeRepeatButton(0, 0, size, size, 2, handler_factory(area, "more"))
124 :setTooltip(get_localized_string("tooltip.research." .. area .. "_inc")),
125 }
126 else
127 if self.adjust_buttons[area] then
128 self.adjust_buttons[area].less.enabled = false
129 self.adjust_buttons[area].more.enabled = false
130 end
131 self:addColourPanel(c1, topy + i * spacing, 120, 30, col_bg.red, col_bg.green, col_bg.blue)
132 end
133 end
134 end
135
136 function UIResearch:adjustResearch(area, mode)
137 local res = self.research
138 local amount = 1
139 if self.ui.app.key_modifiers.ctrl then
140 amount = amount * 20
141 elseif self.ui.app.key_modifiers.shift then
142 amount = amount * 5
143 end
144 if mode == "less" then
145 if res.research_policy[area].frac > 0 then
146 res.research_policy[area].frac = math.max(0, res.research_policy[area].frac - amount)
147 self.ui:playSound("selectx.wav")
148 else
149 self.ui:playSound("Wrong2.wav")
150 end
151 elseif mode == "more" then
152 if res.research_policy.total < 100 and res.research_policy[area].current then
153 res.research_policy[area].frac = res.research_policy[area].frac +
154 math.min(amount, 100 - res.research_policy.total)
155 self.ui:playSound("selectx.wav")
156 else
157 self.ui:playSound("Wrong2.wav")
158 end
159 end
160
161 res.research_policy.total = 0
162 for _, category in ipairs(research_categories) do
163 res.research_policy.total = res.research_policy.total + res.research_policy[category].frac
164 end
165 end
166
167 --! Construct and play this window's background sound.
168 function UIResearch:_playBgSound()
169 self.bg_sound = self.ui:playSound("Research.wav", nil, nil, -1)
170 end
171
172 function UIResearch:onTick()
173 -- sprite index for the water are between 5 and 12
174 -- We use a sub clock
175 self.waterclk = self.waterclk + 1
176 if self.waterclk > 3 then
177 self.waterclk = 0
178 self.waterpanel.sprite_index = self.waterpanel.sprite_index + 1
179 if self.waterpanel.sprite_index > 12 then
180 self.waterpanel.sprite_index = 5
181 end
182 end
183
184 -- sprite index for the rat are between 10 and 15
185 -- We use a sub clock
186 self.ratclk = self.ratclk + 1
187 if self.ratclk > 3 then
188 self.ratclk = 0
189 -- sprite index for the water are between 13 and 20
190 self.ratpanel.sprite_index = self.ratpanel.sprite_index + 1
191 if self.ratpanel.sprite_index > 20 then
192 self.ratpanel.sprite_index = 13
193 end
194 end
195
196 return UIFullscreen.onTick(self)
197 end
198
199 function UIResearch:draw(canvas, x, y)
200 local s = TheApp.config.ui_scale
201 canvas:scale(s, "bitmap")
202 self.background:draw(canvas, self.x * s + x, self.y * s + y)
203 canvas:scale(1, "bitmap")
204 UIFullscreen.draw(self, canvas, x, y)
205 x, y = self.x * s + x, self.y * s + y
206
207 local num_font = self.number_font
208 local lbl_font = self.label_font
209
210 local ytop = 28 * s
211 local spacing = 41 * s
212 local research = self.research.research_policy
213
214 for i, category in ipairs(research_categories) do
215 local ypos = y + ytop + i * spacing
216 lbl_font:draw(canvas, _S.research.categories[category], x + 170 * s, ypos)
217 if not research[category].current then
218 num_font:draw(canvas, _S.misc.done, x + 270 * s, ypos, 300 * s, 0)
219 else
220 num_font:draw(canvas, research[category].frac, x + 270 * s, ypos, 300 * s, 0)
221 end
222 -- Display research progress.
223 if research[category].current and not research[category].current.dummy then
224 local ly = ypos + 26 * s
225 local lx = x + 172 * s
226 local required = self.research:getResearchRequired(research[category].current)
227 local available = self.research.research_progress[research[category].current].points
228 local length = 290 * s * available / required
229 local dx = 0
230 while dx + 10 * s < length do
231 self.panel_sprites:draw(canvas, 3, lx + dx, ly, { scaleFactor = s })
232 dx = dx + 10 * s
233 end
234 end
235 end
236
237 num_font:draw(canvas, research.total, x + 270 * s, y + 288 * s, 300 * s, 0)
238 end
239
240 function UIResearch:close()
241 self.ui:stopSound(self.bg_sound)
242 UIFullscreen.close(self)
243 self.ui:getWindow(UIBottomPanel):updateButtonStates()
244 end
245
246 function UIResearch:afterLoad(old, new)
247 UIFullscreen.afterLoad(self, old, new)
248 if old < 26 then
249 self.adjust_buttons = {}
250 for i, area in ipairs(research_categories) do
251 self.adjust_buttons[area] = {
252 less = self.buttons[2 * i],
253 more = self.buttons[2 * i + 1],
254 }
255 end
256 end
257 if old < 236 then
258 local gfx = TheApp.gfx
259 self.background = gfx:loadRaw("Res01V", 640, 480, "QData", "QData", "Res01V.pal", true)
260 local palette = gfx:getPalette("Res01V.pal")
261 self.panel_sprites = gfx:loadSpriteTable("QData", "Res02V", true, palette)
262 self.label_font = gfx:loadFontAndSpriteTable("QData", "Font43V", false, palette, { apply_ui_scale = true })
263 self.number_font = gfx:loadFontAndSpriteTable("QData", "Font43V", false, palette, { apply_ui_scale = true })
264 end
265 self:_playBgSound()
266 end