Debian Codegraph @

CorsixTH/Lua/dialogs/staff_dialog.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 -- Test for hit within the view circle
22 local --[[persistable:staff_dialog_is_in_view_circle]] function is_in_view_circle(x, y, is_handyman)
23 local s = TheApp.config.ui_scale
24 local circle_center_y = is_handyman and 276 * s or 248 * s
25 return (x - 55 * s)^2 + (y - circle_center_y)^2 < (39 * s)^2
26 end
27
28 --! Individual staff information dialog
29 class "UIStaff" (Window)
30
31 ---@type UIStaff
32 local UIStaff = _G["UIStaff"]
33
34 --! Callback function for handyman to change his parcel.
35 function UIStaff:changeParcel()
36 local index = 0
37 for i, v in ipairs(self.staff.hospital.ownedPlots) do
38 if v == self.staff.parcelNr then
39 index = i
40 break
41 end
42 end
43 if not self.staff.hospital.ownedPlots[index + 1] then
44 self.staff.parcelNr = 0
45 else
46 self.staff.parcelNr = self.staff.hospital.ownedPlots[index + 1]
47 end
48 end
49
50 function UIStaff:getParcelText()
51 if not self.staff.parcelNr then
52 self.staff.parcelNr = 0
53 end
54 if self.staff.parcelNr == 0 then
55 return _S.handyman_window.all_parcels --"All parcels"
56 else
57 return _S.handyman_window.parcel .. " " .. self.staff.parcelNr
58 end
59 end
60
61 function UIStaff:UIStaff(ui, staff)
62 self:Window()
63
64 local app = ui.app
65 local profile = staff.profile
66 self.esc_closes = true
67 self.staff = staff
68 self.ui = ui
69 self.modal_class = "humanoid_info"
70 self.width = 220
71 if class.is(staff, Handyman) then
72 self.height = 332
73 else
74 self.height = 304
75 end
76 self:setDefaultPosition(-20, 30)
77 self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req01V", true)
78 self.white_font = app.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
79 self.face_parts = app.gfx:loadRaw("Face01V", 65, 1350, nil, "Data", "MPalette.dat", false, { flags = DrawFlags.Nearest })
80
81 self:addPanel(297, 15, 0) -- Dialog header
82 for y = 51, 121, 10 do
83 self:addPanel(298, 15, y) -- Dialog background
84 end
85 self:addPanel(299, 104, 50) -- Happiness
86 self:addPanel(300, 105, 82) -- Tiredness
87 self:addPanel(301, 15, 114) -- Skills/Abilities
88 self:addColourPanel(35, 51, 71, 81, 208, 252, 252):makeButton(0, 0, 71, 81, nil, self.openStaffManagement):setTooltip(_S.tooltip.staff_window.face) -- Portrait background
89
90 if class.is(staff, Handyman) then
91 self:addPanel(311, 15, 131) -- Tasks top
92 for y = 149, 184, 5 do
93 self:addPanel(312, 15, y) -- Tasks buttons
94 end
95 self:addPanel(302, 5, 205) -- View circle top/Wage
96 self:addPanel(313, 15, 189) -- Tasks bottom
97 self:addPanel(314, 37, 145):makeRepeatButton(0, 0, 49, 48, 315, self.doMoreCleaning):setTooltip(_S.tooltip.handyman_window.prio_litter):setSound("selectx.wav")
98 self:addPanel(316, 92, 145):makeRepeatButton(0, 0, 49, 48, 317, self.doMoreWatering):setTooltip(_S.tooltip.handyman_window.prio_plants):setSound("selectx.wav")
99 self:addPanel(318, 148, 145):makeRepeatButton(0, 0, 49, 48, 319, self.doMoreRepairing):setTooltip(_S.tooltip.handyman_window.prio_machines):setSound("selectx.wav")
100 self:addPanel(240, 21, 210):makeButton(0, 0, 73, 30, 240, self.changeParcel):setTooltip(_S.tooltip.handyman_window.parcel_select):setSound("selectx.wav")
101 self:addPanel(303, 0, 253) -- View circle midpiece
102 self:addPanel(304, 6, 302) -- View circle bottom
103 self:addPanel(307, 106, 253):makeButton(0, 0, 50, 50, 308, self.fireStaff):setTooltip(_S.tooltip.staff_window.sack):setSound("selectx.wav")
104 self:addPanel(309, 164, 253):makeButton(0, 0, 37, 50, 310, self.pickupStaff):setTooltip(_S.tooltip.staff_window.pick_up)
105 else
106 self:addPanel(302, 5, 178) -- View circle top/Wage
107 self:addPanel(303, 0, 226) -- View circle midpiece
108 self:addPanel(304, 6, 274) -- View circle bottom
109 if not class.is(staff, Doctor) then
110 self:addColourPanel(32, 141, 171, 39, 85, 202, 219) -- Hides Skills
111 end
112 self:addPanel(307, 106, 226):makeButton(0, 0, 50, 50, 308, self.fireStaff):setTooltip(_S.tooltip.staff_window.sack):setSound("selectx.wav")
113 self:addPanel(309, 164, 226):makeButton(0, 0, 37, 50, 310, self.pickupStaff):setTooltip(_S.tooltip.staff_window.pick_up)
114 end
115
116 self:addPanel(305, 178, 18):makeButton(0, 0, 24, 24, 306, self.close):setTooltip(_S.tooltip.staff_window.close)
117
118 self:makeTooltip(_S.tooltip.staff_window.name, 33, 19, 172, 42)
119 self:makeTooltip(_S.tooltip.staff_window.happiness, 113, 49, 204, 74)
120 self:makeTooltip(_S.tooltip.staff_window.tiredness, 113, 74, 204, 109)
121 self:makeTooltip(_S.tooltip.staff_window.ability, 113, 109, 204, 134)
122
123 if class.is(staff, Doctor) then
124 self:makeTooltip(_S.tooltip.staff_window.doctor_seniority, 30, 141, 111, 182)
125 self:makeTooltip(_S.tooltip.staff_window.skills, 111, 146, 141, 179)
126
127 local skill_to_string = {
128 is_surgeon = _S.tooltip.staff_window.surgeon,
129 is_psychiatrist = _S.tooltip.staff_window.psychiatrist,
130 is_researcher = _S.tooltip.staff_window.researcher,
131 }
132 local --[[persistable:staff_dialog_skill_tooltip_template]] function skill_tooltip(skill)
133 return --[[persistable:staff_dialog_skill_tooltip]] function()
134 if profile[skill] >= 1.0 then
135 return skill_to_string[skill]
136 end
137 end
138 end
139
140 self:makeDynamicTooltip(skill_tooltip("is_surgeon"), 143, 148, 155, 177)
141 self:makeDynamicTooltip(skill_tooltip("is_psychiatrist"), 155, 148, 177, 177)
142 self:makeDynamicTooltip(skill_tooltip("is_researcher"), 177, 148, 202, 177)
143 end
144
145 -- window for handyman is slightly different
146 local offset = class.is(staff, Handyman) and 27 or 0
147
148 self:makeTooltip(_S.tooltip.staff_window.salary, 90, 191 + offset, 204, 214 + offset)
149 -- Non-rectangular tooltip has to be realized with dynamic tooltip at the moment
150 self:makeDynamicTooltip(--[[persistable:staff_dialog_center_tooltip]]function(x, y)
151 if is_in_view_circle(x, y, profile:isType("Handyman")) then
152 return _S.tooltip.staff_window.center_view
153 end
154 end, 17, 211 + offset, 92, 286 + offset)
155
156 end
157
158 function UIStaff:getStaffPosition(dx, dy)
159 local staff = self.staff
160 local x, y = self.ui.app.map:WorldToScreen(staff.tile_x, staff.tile_y)
161 local px, py = staff.th:getSecondaryMarker()
162 return x + px - (dx or 0), y + py - (dy or 0)
163 end
164
165 function UIStaff:draw(canvas, x_, y_)
166 local s = TheApp.config.ui_scale
167 local x, y = self.x * s + x_, self.y * s + y_
168
169 local px, py = self:getStaffPosition(37, 61)
170 canvas:scale(s)
171 self.ui.app.map:draw(canvas, px, py, 75, 75, math.floor(x / s) + 17, math.floor(y / s) + self.height - 93)
172 canvas:scale(1)
173 Window.draw(self, canvas, x_, y_)
174
175 local profile = self.staff.profile
176 local font = self.white_font
177
178 font:draw(canvas, profile:getFullName(), x + 42 * s, y + 28 * s) -- Name
179 if class.is(self.staff, Handyman)then
180 font:draw(canvas, "$" .. profile.wage, x + 135 * s, y + 225 * s) -- Wage
181 font:draw(canvas, self:getParcelText(), x + 35 * s, y + 215 * s, 50 * s, 0)
182 -- The concentration areas
183 local cleaning_width = math.floor(self.staff:getAttribute("cleaning") * 40 * s + 0.5)
184 local watering_width = math.floor(self.staff:getAttribute("watering") * 40 * s + 0.5)
185 local repairing_width = math.floor(self.staff:getAttribute("repairing") * 40 * s + 0.5)
186 if cleaning_width ~= 0 then
187 for dx = 0, cleaning_width - 1, s do
188 self.panel_sprites:draw(canvas, 351, x + 43 * s + dx, y + 200 * s, { scaleFactor = s })
189 end
190 end
191 if watering_width ~= 0 then
192 for dx = 0, watering_width - 1 do
193 self.panel_sprites:draw(canvas, 351, x + 99 * s + dx, y + 200 * s, { scaleFactor = s })
194 end
195 end
196 if repairing_width ~= 0 then
197 for dx = 0, repairing_width - 1 do
198 self.panel_sprites:draw(canvas, 351, x + 155 * s + dx, y + 200 * s, { scaleFactor = s })
199 end
200 end
201 else
202 font:draw(canvas, "$" .. profile.wage, x + 135 * s, y + 198 * s) -- Wage
203 end
204
205 if self.staff:getAttribute("happiness") then
206 local happiness_bar_width = math.floor(self.staff:getAttribute("happiness") * 40 * s + 0.5)
207 if happiness_bar_width ~= 0 then
208 for dx = 0, happiness_bar_width - 1, s do
209 self.panel_sprites:draw(canvas, 348, x + 139 * s + dx, y + 56 * s, { scaleFactor = s })
210 end
211 end
212 end
213
214 local fatigue_bar_width = 40.5
215 if self.staff:getAttribute("fatigue") then
216 fatigue_bar_width = math.floor((1 - self.staff:getAttribute("fatigue")) * 40 * s + 0.5)
217 end
218 if fatigue_bar_width ~= 0 then
219 for dx = 0, fatigue_bar_width - 1, s do
220 self.panel_sprites:draw(canvas, 349, x + 139 * s + dx, y + 89 * s, { scaleFactor = s })
221 end
222 end
223
224 local skill_bar_width = math.floor(profile.skill * 40 * s + 0.5)
225 if skill_bar_width ~= 0 then
226 for dx = 0, skill_bar_width - 1, s do
227 self.panel_sprites:draw(canvas, 350, x + 139 * s + dx, y + 120 * s, { scaleFactor = s })
228 end
229 end
230
231 if class.is(self.staff, Doctor) then
232 -- Junior / Doctor / Consultant marker
233 if profile.is_junior then
234 self.panel_sprites:draw(canvas, 347, x + 38 * s, y + 173 * s, { scaleFactor = s })
235 elseif profile.is_consultant then
236 self.panel_sprites:draw(canvas, 347, x + 89 * s, y + 173 * s, { scaleFactor = s })
237 else
238 self.panel_sprites:draw(canvas, 347, x + 60 * s, y + 173 * s, { scaleFactor = s })
239 end
240 -- Ability markers
241 if profile.is_surgeon >= 1.0 then
242 self.panel_sprites:draw(canvas, 344, x + 144 * s, y + 148 * s, { scaleFactor = s })
243 end
244 if profile.is_psychiatrist >= 1.0 then
245 self.panel_sprites:draw(canvas, 345, x + 155 * s, y + 154 * s, { scaleFactor = s })
246 end
247 if profile.is_researcher >= 1.0 then
248 self.panel_sprites:draw(canvas, 346, x + 178 * s, y + 153 * s, { scaleFactor = s })
249 end
250 end
251
252 profile:drawFace(canvas, x + 38 * s, y + 54 * s, self.face_parts, s) -- Portrait
253 end
254
255 function UIStaff:onMouseDown(button, x, y)
256 self.do_scroll = button == "left" and is_in_view_circle(x, y, class.is(self.staff, Handyman))
257 return Window.onMouseDown(self, button, x, y)
258 end
259
260 function UIStaff:onMouseUp(button, x, y)
261 local ui = self.ui
262 if button == "left" then
263 self.do_scroll = false
264 end
265 local repaint = Window.onMouseUp(self, button, x, y)
266 local s = TheApp.config.ui_scale
267 -- Test for hit within the view circle and name box
268 local hit_namebox = x > self.tooltip_regions[1].x * s and x < self.tooltip_regions[1].r * s
269 and y > self.tooltip_regions[1].y * s and y < self.tooltip_regions[1].b * s
270 if button == "right" and is_in_view_circle(x, y, class.is(self.staff, Handyman))
271 or button == "right" and hit_namebox then
272 -- Right click goes to the next staff member of the same category (NB: Surgeon in same Category as Doctor)
273 local staff_index = nil
274 for i, staff in ipairs(ui.hospital.staff) do
275 if staff_index and staff.profile:isType(self.staff.profile.humanoid_class) then
276 ui:addWindow(UIStaff(ui, staff))
277 if hit_namebox then
278 local sx, sy = ui.app.map:WorldToScreen(staff.tile_x, staff.tile_y)
279 local dx, dy = staff.th:getPosition()
280 ui:scrollMapTo(sx + dx, sy + dy)
281 end
282 return false
283 end
284 if staff == self.staff then
285 staff_index = i
286 end
287 end
288 -- Try again from beginning of list until staff_index
289 for i = 1, staff_index - 1 do
290 local staff = ui.hospital.staff[i]
291 if staff.profile:isType(self.staff.profile.humanoid_class) then
292 ui:addWindow(UIStaff(ui, staff))
293 if hit_namebox then
294 local sx, sy = ui.app.map:WorldToScreen(staff.tile_x, staff.tile_y)
295 local dx, dy = staff.th:getPosition()
296 ui:scrollMapTo(sx + dx, sy + dy)
297 end
298 return false
299 end
300 end
301 end
302 return repaint
303 end
304
305 function UIStaff:onMouseMove(x, y, dx, dy)
306 self.do_scroll = self.do_scroll and is_in_view_circle(x, y, class.is(self.staff, Handyman))
307 return Window.onMouseMove(self, x, y, dx, dy)
308 end
309
310 function UIStaff:onTick()
311 if self.do_scroll then
312 local ui = self.ui
313 local staff = self.staff
314 local sx, sy = ui.app.map:WorldToScreen(staff.tile_x, staff.tile_y)
315 local dx, dy = staff.th:getPosition()
316 ui:scrollMapTo(sx + dx, sy + dy)
317 end
318 return Window.onTick(self)
319 end
320
321 function UIStaff:pickupStaff()
322 self.staff:setPickup(self.ui, self)
323 end
324
325 function UIStaff:fireStaff()
326 self.ui:addWindow(UIConfirmDialog(self.ui, false, _S.confirmation.sack_staff, --[[persistable:staff_dialog_confirm_sack]] function()
327 self.staff:fire()
328 end))
329 end
330
331
332 --! Function to balance 'cleaning','watering', and 'repairing', where
333 --! one of them is increased, and the other two are decreased.
334 --!param increased Attribute to increase.
335 function UIStaff:changeHandymanAttributes(increased)
336 if not self.staff:getAttribute(increased) then
337 return
338 end
339
340 -- Show a helpful message if this dialog hasn't been opened yet
341 if not self.ui.hospital.handyman_popup then
342 self.ui.adviser:say(_A.information.handyman_adjust)
343 self.ui.hospital.handyman_popup = true
344 end
345
346 local incr_value = 0.1 -- Increase of 'increased'
347 local smallest_decr = 0.05 -- Smallest decrement that can be performed.
348 local decr_attrs = {}
349
350 local attributes = {"cleaning", "watering", "repairing"}
351 for _, attr in ipairs(attributes) do
352 if attr == increased then
353 -- Adding too much is not a problem, it gets clipped to 1.
354 self.staff:changeAttribute(attr, incr_value)
355 if self.staff:getAttribute(attr) == 1 then
356 incr_value = 2.0 -- Doing 'increased' 100%, set other attributes to 0.
357 end
358 else
359 decr_attrs[#decr_attrs + 1] = attr
360 smallest_decr = math.min(smallest_decr, self.staff:getAttribute(attr))
361 end
362 end
363 assert(#decr_attrs == 2)
364
365 -- The decreasing attributes should together decrease '-incr_value', but one
366 -- or both may be smaller than '-incr_value / 2'.
367 -- Compensate by subtracting the biggest value from both.
368 local decr_value = incr_value - smallest_decr
369 for _, attr in ipairs(decr_attrs) do
370 -- Subtracting too much is not a problem, it gets clipped to 0.
371 self.staff:changeAttribute(attr, -decr_value)
372 end
373 end
374
375 --! UI callback function to increase 'cleaning' (wiping litter).
376 function UIStaff:doMoreCleaning()
377 self:changeHandymanAttributes("cleaning")
378 end
379
380 --! UI callback function to increase 'watering' (plants).
381 function UIStaff:doMoreWatering()
382 self:changeHandymanAttributes("watering")
383 end
384
385 --! UI callback function to increase 'repairing' (machines).
386 function UIStaff:doMoreRepairing()
387 self:changeHandymanAttributes("repairing")
388 end
389
390 function UIStaff:openStaffManagement()
391 local dlg = UIStaffManagement(self.ui)
392 -- Make sure that the dialog managed to create itself properly.
393 -- For example, if using the demo files closed will be true because the dialog could not be loaded.
394 if not dlg.closed then
395 dlg:selectStaff(self.staff)
396 self.ui:addWindow(dlg)
397 self:close()
398 end
399 end
400
401 function UIStaff:hitTest(x, y)
402 return Window.hitTest(self, x, y) or is_in_view_circle(x, y, class.is(self.staff, Handyman))
403 end
404
405 function UIStaff:afterLoad(old, new)
406 if old < 205 then
407 self:close()
408 end
409 Window.afterLoad(self, old, new)
410 if old < 236 then
411 self.white_font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font01V", nil, nil, { apply_ui_scale = true })
412 self.face_parts = TheApp.gfx:loadRaw("Face01V", 65, 1350, nil, "Data", "MPalette.dat", false, { flags = DrawFlags.Nearest })
413 end
414 end