| 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 |
local TH = require("TH") |
| 22 |
|
| 23 |
-- Test for hit within the view circle |
| 24 |
local --[[persistable:patient_window_is_in_view_circle]] function is_in_view_circle(x, y) |
| 25 |
local s = TheApp.config.ui_scale |
| 26 |
return (x - 55 * s)^2 + (y - 254 * s)^2 < (39 * s)^2 |
| 27 |
end |
| 28 |
|
| 29 |
--! Individual patient information dialog |
| 30 |
class "UIPatient" (Window) |
| 31 |
|
| 32 |
---@type UIPatient |
| 33 |
local UIPatient = _G["UIPatient"] |
| 34 |
|
| 35 |
function UIPatient:UIPatient(ui, patient) |
| 36 |
self:Window() |
| 37 |
|
| 38 |
local app = ui.app |
| 39 |
self.esc_closes = true |
| 40 |
self.ui = ui |
| 41 |
self.modal_class = "humanoid_info" |
| 42 |
self.width = 191 |
| 43 |
self.height = 310 |
| 44 |
self:setDefaultPosition(-20, 30) |
| 45 |
self.panel_sprites = app.gfx:loadSpriteTable("QData", "Req02V", true) |
| 46 |
self.font = app.gfx:loadFontAndSpriteTable("QData", "Font74V", nil, nil, { apply_ui_scale = true }) -- Font used in the treatment history |
| 47 |
self.patient = patient |
| 48 |
self.visible_diamond = ui:makeVisibleDiamond(75, 76) |
| 49 |
|
| 50 |
self:addPanel(320, 15, 0) -- Graph top |
| 51 |
self:addPanel(321, 15, 61) -- Graph bottom |
| 52 |
|
| 53 |
self.history_panel = self:addColourPanel(36, 22, 99, 88, 223, 223, 223) -- Treatment history background |
| 54 |
self.history_panel:makeButton(0, 0, 99, 88, nil, --[[persistable:patient_toggle_history]] function() |
| 55 |
self.history_panel.visible = not self.history_panel.visible |
| 56 |
end):setTooltip(_S.tooltip.patient_window.graph):setSound("selectx.wav") -- Treatment history toggle |
| 57 |
self.history_panel.visible = false -- Hide the treatment history at start |
| 58 |
|
| 59 |
self:addPanel(322, 15, 126) -- Happiness / thirst / temperature sliders |
| 60 |
self:addPanel(323, 0, 201) -- View circle top |
| 61 |
self:addPanel(324, 0, 254) -- View circle bottom |
| 62 |
self:addPanel(325, 147, 21):makeButton(0, 0, 24, 24, 326, self.close):setTooltip(_S.tooltip.patient_window.close) |
| 63 |
|
| 64 |
-- If the patient has been diagnosed the "guess cure" button is not visible and |
| 65 |
-- if the patient is going home it is not possible to kick him/her anymore. |
| 66 |
self:addPanel(411, 14 + 132, 61 + 19):makeButton(0, 0, 25, 31, 412, self.viewQueue):setTooltip(_S.tooltip.patient_window.queue) |
| 67 |
|
| 68 |
-- Initialize all buttons and blankers, then call the update function which decides what to show. |
| 69 |
-- Show the drug casebook only after the disease has been diagnosed. |
| 70 |
self.disease_button = self:addPanel(329, 14 + 117, 61 + 107) |
| 71 |
:makeButton(0, 0, 38, 38, 330, self.viewDiseases):setTooltip(_S.tooltip.patient_window.casebook) |
| 72 |
self.disease_blanker = self:addColourPanel(14 + 115, 61 + 105, 45, 45, 113, 117, 170) |
| 73 |
|
| 74 |
self.home_button = self:addPanel(331, 14 + 95, 61 + 158):makeButton(0, 0, 60, 60, 332, self.goHome):setTooltip(_S.tooltip.patient_window.send_home) |
| 75 |
self.home_blanker = self:addColourPanel(14 + 93, 61 + 156, 67, 67, 113, 117, 170) |
| 76 |
|
| 77 |
self.guess_button = self:addPanel(413, 14 + 117, 61 + 58):makeButton(0, 0, 38, 38, 414, self.guessDisease):setTooltip(_S.tooltip.patient_window.abort_diagnosis) |
| 78 |
self.guess_blanker = self:addColourPanel(14 + 115, 61 + 56, 45, 45, 113, 117, 170) |
| 79 |
|
| 80 |
-- Set correct initial visibility/enabledness of the three buttons and their blankers |
| 81 |
self:updateInformation() |
| 82 |
|
| 83 |
self:makeTooltip(_S.tooltip.patient_window.happiness, 33, 117, 124, 141) |
| 84 |
self:makeTooltip(_S.tooltip.patient_window.thirst, 33, 141, 124, 169) |
| 85 |
self:makeTooltip(_S.tooltip.patient_window.warmth, 33, 169, 124, 203) |
| 86 |
|
| 87 |
-- Non-rectangular tooltip has to be realized with dynamic tooltip at the moment |
| 88 |
self:makeDynamicTooltip(--[[persistable:patient_window_center_tooltip]]function(x, y) |
| 89 |
if is_in_view_circle(x, y) then |
| 90 |
return _S.tooltip.patient_window.center_view |
| 91 |
end |
| 92 |
end, 17, 216, 92, 292) |
| 93 |
|
| 94 |
self:registerKeyHandlers() |
| 95 |
end |
| 96 |
|
| 97 |
function UIPatient:registerKeyHandlers() |
| 98 |
-- Always add this because of a race condition if the user clicks a patient |
| 99 |
-- that's already going home, then clicks another, the handler is left empty. Bad. |
| 100 |
-- Just do a going_home check when called. |
| 101 |
self:addKeyHandler("ingame_patient_gohome", self.goHome) |
| 102 |
end |
| 103 |
|
| 104 |
--! Normalise warmth of a patient. |
| 105 |
--!param warmth (number or nil) If given, the fraction of warmth of the patient. |
| 106 |
--!return (float) Normalized warmth level. |
| 107 |
function UIPatient.normaliseWarmth(warmth) |
| 108 |
if not warmth then return 0.5 end -- Return 1/2 if unknown. |
| 109 |
|
| 110 |
if warmth < 0.08 then |
| 111 |
warmth = 0 |
| 112 |
elseif warmth > 0.50 then |
| 113 |
warmth = 1 |
| 114 |
else |
| 115 |
warmth = (warmth - 0.08) / (0.50 - 0.08) |
| 116 |
end |
| 117 |
return warmth |
| 118 |
end |
| 119 |
|
| 120 |
--! Draw a bar in the patient dialogue window. |
| 121 |
--!param canvas Canvas to draw at. |
| 122 |
--!param sprite The sprite to draw |
| 123 |
--!param ybase Vertical base position. |
| 124 |
--!param xpos Horizontal offset. |
| 125 |
--!param ypos Vertical offset. |
| 126 |
--!param value Fraction to draw. |
| 127 |
function UIPatient:drawBar(canvas, sprite, ybase, xpos, ypos, value) |
| 128 |
local s = TheApp.config.ui_scale |
| 129 |
local width = math.floor(value * 40 + 0.5) |
| 130 |
for dx = 0, width - 1 do |
| 131 |
self.panel_sprites:draw(canvas, sprite, xpos + 58 * s + dx * s, ypos + ybase * s, { scaleFactor = s }) |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
function UIPatient:draw(canvas, x_, y_) |
| 136 |
local s = TheApp.config.ui_scale |
| 137 |
local x, y = self.x * s + x_, self.y * s + y_ |
| 138 |
local map = self.ui.app.map |
| 139 |
local patient = self.patient |
| 140 |
-- If the patient has just despawned, then it will have no tile, hence |
| 141 |
-- making it impossible to render said patient. In this case, the dialog |
| 142 |
-- should close. Note that it is slightly better to close the dialog during |
| 143 |
-- the draw callback rather than the tick callback, as doing so in the tick |
| 144 |
-- callback would be removing from the window list while said list is being |
| 145 |
-- iterated, causing the next window in the list to miss it's tick (rendering |
| 146 |
-- is done via a reverse traversal, which does not suffer this problem). |
| 147 |
if not patient.tile_x then |
| 148 |
self:close() |
| 149 |
return |
| 150 |
end |
| 151 |
local px, py = map:WorldToScreen(patient.tile_x, patient.tile_y) |
| 152 |
local dx, dy = patient.th:getPrimaryMarker() |
| 153 |
px = px + dx - 37 |
| 154 |
py = py + dy - 61 |
| 155 |
-- If the patient is spawning or despawning, or just on the map edge, then |
| 156 |
-- the rendering point needs adjustment to keep the rendered region entirely |
| 157 |
-- within the map (this situation doesn't occur very often, but we need to |
| 158 |
-- handle it properly when it does occur). |
| 159 |
px, py = self.ui.limitPointToDiamond(px, py, self.visible_diamond, true) |
| 160 |
canvas:scale(s) |
| 161 |
self.ui.app.map:draw(canvas, px, py, 75, 76, math.floor(x / s) + 17, math.floor(y / s) + 216) |
| 162 |
canvas:scale(1) |
| 163 |
Window.draw(self, canvas, x_, y_) |
| 164 |
|
| 165 |
-- The patient bars (happiness, thirst, and warmth). |
| 166 |
local warmth = self.normaliseWarmth(patient:getAttribute("warmth")) |
| 167 |
|
| 168 |
self:drawBar(canvas, 348, 126, x, y, patient:getAttribute("happiness", 0.5)) |
| 169 |
self:drawBar(canvas, 351, 154, x, y, 1 - (patient:getAttribute("thirst", 0.5))) |
| 170 |
self:drawBar(canvas, 349, 183, x, y, warmth) |
| 171 |
|
| 172 |
if self.history_panel.visible then |
| 173 |
self:drawTreatmentHistory(canvas, x + 40 * s, y + 25 * s) |
| 174 |
elseif patient.health_history then |
| 175 |
self:drawHealthHistory(canvas, x, y) |
| 176 |
end |
| 177 |
end |
| 178 |
|
| 179 |
--! List the treatments that were performed on the patient. |
| 180 |
-- This text is always capitalised. |
| 181 |
--!param canvas Destination to draw on. |
| 182 |
--!param x (int) X position of the top of the list. |
| 183 |
--!param y (int) Y position of the top of the list. |
| 184 |
function UIPatient:drawTreatmentHistory(canvas, x, y) |
| 185 |
local s = TheApp.config.ui_scale |
| 186 |
for _, room in ipairs(self.patient.treatment_history) do |
| 187 |
y = self.font:drawWrapped(canvas, room:upper(), x, y, 95 * s) |
| 188 |
end |
| 189 |
end |
| 190 |
|
| 191 |
--! Draw the health graph of the patient. |
| 192 |
--!param canvas Destination to draw on. |
| 193 |
--!param x (int) X position of the top-left of the graph. |
| 194 |
--!param y (int) Y position of the top-left of the graph. |
| 195 |
function UIPatient:drawHealthHistory(canvas, x, y) |
| 196 |
local s = TheApp.config.ui_scale |
| 197 |
|
| 198 |
-- Sizes and positions of the graph in the window. |
| 199 |
local hor_length = 76 * s |
| 200 |
local vert_length = 70 * s |
| 201 |
local startx = 58 * s |
| 202 |
local starty = 27 * s |
| 203 |
|
| 204 |
-- Health history information. |
| 205 |
local hh = self.patient.health_history |
| 206 |
local index = hh["last"] |
| 207 |
local size = hh["size"] |
| 208 |
|
| 209 |
local dx = hor_length / size |
| 210 |
local line = nil -- Make a line the first time we find a non-nil value. |
| 211 |
for _ = 1, size do |
| 212 |
index = (index == size) and 1 or (index + 1) |
| 213 |
if hh[index] then |
| 214 |
local posy = starty + (1.0 - hh[index]) * vert_length |
| 215 |
|
| 216 |
if not line then |
| 217 |
line = TH.line() |
| 218 |
line:setWidth(2 * s) |
| 219 |
line:setColour(200, 55, 30, 255) |
| 220 |
line:moveTo(startx, posy) |
| 221 |
else |
| 222 |
line:lineTo(startx, posy) |
| 223 |
end |
| 224 |
end |
| 225 |
startx = startx + dx |
| 226 |
end |
| 227 |
|
| 228 |
if line then line:draw(canvas, x, y) end |
| 229 |
end |
| 230 |
|
| 231 |
function UIPatient:onMouseDown(button, x, y) |
| 232 |
self.do_scroll = button == "left" and is_in_view_circle(x, y) |
| 233 |
return Window.onMouseDown(self, button, x, y) |
| 234 |
end |
| 235 |
|
| 236 |
function UIPatient:onMouseUp(button, x, y) |
| 237 |
local ui = self.ui |
| 238 |
if button == "left" then |
| 239 |
self.do_scroll = false |
| 240 |
end |
| 241 |
local repaint = Window.onMouseUp(self, button, x, y) |
| 242 |
if button == "right" and is_in_view_circle(x, y) then |
| 243 |
-- Right click goes to the next patient |
| 244 |
local patient_index = nil |
| 245 |
for i, patient in ipairs(ui.hospital.patients) do |
| 246 |
if patient == self.patient then |
| 247 |
patient_index = i |
| 248 |
break |
| 249 |
end |
| 250 |
end |
| 251 |
patient_index = (patient_index or 0) + 1 |
| 252 |
local patient = ui.hospital.patients[patient_index] or ui.hospital.patients[1] |
| 253 |
if patient then |
| 254 |
ui:addWindow(UIPatient(ui, patient)) |
| 255 |
return false |
| 256 |
end |
| 257 |
end |
| 258 |
return repaint |
| 259 |
end |
| 260 |
|
| 261 |
function UIPatient:onMouseMove(x, y, dx, dy) |
| 262 |
self.do_scroll = self.do_scroll and is_in_view_circle(x, y) |
| 263 |
return Window.onMouseMove(self, x, y, dx, dy) |
| 264 |
end |
| 265 |
|
| 266 |
--[[! Scrolls the map to the position of the patient which this dialog belongs to ]] |
| 267 |
function UIPatient:scrollToPatient() |
| 268 |
local ui = self.ui |
| 269 |
local patient = self.patient |
| 270 |
local px, py = ui.app.map:WorldToScreen(patient.tile_x, patient.tile_y) |
| 271 |
local dx, dy = patient.th:getPosition() |
| 272 |
ui:scrollMapTo(px + dx, py + dy) |
| 273 |
end |
| 274 |
|
| 275 |
function UIPatient:onTick() |
| 276 |
if self.do_scroll then |
| 277 |
self:scrollToPatient() |
| 278 |
end |
| 279 |
return Window.onTick(self) |
| 280 |
end |
| 281 |
|
| 282 |
--! Go through the buttons in the patient dialog to check whether they should be visible |
| 283 |
function UIPatient:updateInformation() |
| 284 |
local patient = self.patient |
| 285 |
-- Show casebook? |
| 286 |
-- Debug patients can exist before a disease is discovered (i.e. no casebook entry) |
| 287 |
-- so make sure we guard against this. |
| 288 |
local diag = patient.diagnosed |
| 289 |
local disc = patient.hospital.disease_casebook[patient.disease.id].discovered |
| 290 |
local show_dis_btn = diag and disc |
| 291 |
self.disease_button.enabled = show_dis_btn |
| 292 |
self.disease_button.visible = show_dis_btn |
| 293 |
self.disease_blanker.visible = not show_dis_btn |
| 294 |
|
| 295 |
-- Show go home? |
| 296 |
local show_home_btn = not patient.going_home and not patient.going_to_die |
| 297 |
self.home_button.enabled = show_home_btn |
| 298 |
self.home_button.visible = show_home_btn |
| 299 |
self.home_blanker.visible = not show_home_btn |
| 300 |
|
| 301 |
-- Show guess cure? |
| 302 |
local show_guess_btn = not (patient.is_debug or |
| 303 |
patient.diagnosis_progress == 0 or |
| 304 |
patient.diagnosed or |
| 305 |
patient.going_to_die or |
| 306 |
patient.going_home) |
| 307 |
self.guess_button.enabled = show_guess_btn |
| 308 |
self.guess_button.visible = show_guess_btn |
| 309 |
self.guess_blanker.visible = not show_guess_btn |
| 310 |
end |
| 311 |
|
| 312 |
function UIPatient:viewQueue() |
| 313 |
for _, action in ipairs(self.patient.action_queue) do |
| 314 |
if action.name == "queue" then |
| 315 |
self.ui:addWindow(UIQueue(self.ui, action.queue)) |
| 316 |
self.ui:playSound("selectx.wav") |
| 317 |
return |
| 318 |
end |
| 319 |
end |
| 320 |
self.ui:playSound("wrong2.wav") |
| 321 |
end |
| 322 |
|
| 323 |
function UIPatient:goHome() |
| 324 |
if self.patient.going_home then |
| 325 |
return |
| 326 |
end |
| 327 |
self:close() |
| 328 |
self.patient:playSound("sack.wav") |
| 329 |
self.patient:goHome("kicked") |
| 330 |
self.patient:setDynamicInfoText(_S.dynamic_info.patient.actions.sent_home) |
| 331 |
end |
| 332 |
|
| 333 |
function UIPatient:viewDiseases() |
| 334 |
local dlg = UICasebook(self.ui, self.patient.diagnosed and self.patient.disease.id or nil) |
| 335 |
self.ui:addWindow(dlg) |
| 336 |
end |
| 337 |
|
| 338 |
function UIPatient:guessDisease() |
| 339 |
local patient = self.patient |
| 340 |
-- NB: the first line of conditions should already be ruled out by button being disabled, but just in case |
| 341 |
if patient.is_debug or patient.diagnosis_progress == 0 or patient.diagnosed or |
| 342 |
patient.going_home or patient.going_to_die or patient:getRoom() or |
| 343 |
not patient.hospital.disease_casebook[patient.disease.id].discovered then |
| 344 |
self.ui:playSound("wrong2.wav") |
| 345 |
return |
| 346 |
end |
| 347 |
patient:setDiagnosed() |
| 348 |
if patient:agreesToPay(patient.disease.id) then |
| 349 |
patient:setNextAction(SeekRoomAction(patient.disease.treatment_rooms[1]):enableTreatmentRoom(), 1) |
| 350 |
else |
| 351 |
patient:goHome("over_priced") |
| 352 |
end |
| 353 |
end |
| 354 |
|
| 355 |
function UIPatient:hitTest(x, y) |
| 356 |
return Window.hitTest(self, x, y) or is_in_view_circle(x, y) |
| 357 |
end |
| 358 |
|
| 359 |
function UIPatient:afterLoad(old, new) |
| 360 |
Window.afterLoad(self, old, new) |
| 361 |
self:registerKeyHandlers() |
| 362 |
|
| 363 |
if old < 236 then |
| 364 |
self.font = TheApp.gfx:loadFontAndSpriteTable("QData", "Font74V", nil, nil, { apply_ui_scale = true }) |
| 365 |
end |
| 366 |
end |