| 1 |
--[[ Copyright (c) 2024 Toby "tobylane" |
| 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 |
-- The "Winning and Losing Conditions" section of level files uses Criteria |
| 22 |
-- numbers from the order of this table. Any Criteria numbers higher than |
| 23 |
-- the size of this table will be silently ignored. |
| 24 |
-- Icon, icon_file, formats and two_tooltips are used in the progress report dialog. |
| 25 |
local local_criteria_variable = { |
| 26 |
{name = "reputation", icon = 10, formats = 2}, |
| 27 |
{name = "balance", icon = 11, formats = 2}, |
| 28 |
{name = "percentage_cured", icon = 12, formats = 2}, |
| 29 |
{name = "num_cured" , icon = 13, formats = 2}, |
| 30 |
{name = "percentage_killed",icon = 14, formats = 2}, |
| 31 |
{name = "value", icon = 15, formats = 2}, -- Hospital value |
| 32 |
-- New criteria |
| 33 |
{name = "staff_happiness", icon = 16, icon_file = "MPointer", formats = 2, two_tooltips = true}, |
| 34 |
{name = "patient_happiness",icon = 18, icon_file = "MPointer", formats = 2, two_tooltips = true}, |
| 35 |
} |
| 36 |
|
| 37 |
-- A table of functions for fetching criteria values that cannot be measured |
| 38 |
-- directly from a hospital attribute of the same name. |
| 39 |
local get_custom_criteria = { |
| 40 |
staff_happiness = function(hospital) return 100 * hospital:getAverageStaffAttribute("happiness", 0.5) end, |
| 41 |
patient_happiness = function(hospital) return 100 * hospital:getAveragePatientAttribute("happiness", 0.5) end, |
| 42 |
} |
| 43 |
|
| 44 |
class "EndConditions" |
| 45 |
---@type EndConditions |
| 46 |
local EndConditions = _G["EndConditions"] |
| 47 |
|
| 48 |
--! Collect the end conditions for this level, if they exist |
| 49 |
--!param level_config (table) The map's level_config, containing criteria. |
| 50 |
--!param level_number (integer) The number of the map, if playing a campaign. |
| 51 |
--!param freebuild (boolean) The free build status of the world |
| 52 |
function EndConditions:EndConditions(level_config, level_number, freebuild) |
| 53 |
self.win_goals, self.lose_goals, self.highest_group = {}, {}, 0 |
| 54 |
if freebuild then return end |
| 55 |
|
| 56 |
local start, town = {} |
| 57 |
if level_number and level_config.towns[level_number] then |
| 58 |
town = level_config.towns[level_number] |
| 59 |
else |
| 60 |
town = level_config.town |
| 61 |
end |
| 62 |
start.balance = town.StartCash |
| 63 |
start.reputation = town.StartRep |
| 64 |
|
| 65 |
self:_loadGoals(level_config.win_criteria, self.win_goals, start, true) |
| 66 |
self:_loadGoals(level_config.lose_criteria, self.lose_goals, start) |
| 67 |
end |
| 68 |
|
| 69 |
--! Load conditions (goals) to win and lose from the level config, |
| 70 |
-- and store them in self.win_goals and self.lose_goals in their groups. |
| 71 |
-- Put the highest number of group of conditions in self.highest_group. |
| 72 |
-- These groups are often incomplete, possibly empty |
| 73 |
-- ie more groups of conditions that lead to loss than win. |
| 74 |
--!param criteria_tbl (table) The map's win or lose criteria. |
| 75 |
--!param goals (table) The class table to fill |
| 76 |
--!param start (table) The starting values of the hospital attributes |
| 77 |
--!param win (boolean) If the win goals are being filled this time |
| 78 |
function EndConditions:_loadGoals(criteria_tbl, goals, start, win) |
| 79 |
for _, values in pairs(criteria_tbl) do |
| 80 |
if local_criteria_variable[values.Criteria] then |
| 81 |
local crit_name = local_criteria_variable[values.Criteria].name |
| 82 |
if not goals[values.Group] then goals[values.Group] = {} end |
| 83 |
goals[values.Group][crit_name] = { |
| 84 |
name = crit_name, |
| 85 |
boundary = values.Bound, |
| 86 |
criterion = values.Criteria, |
| 87 |
max_min = values.MaxMin, |
| 88 |
icon = local_criteria_variable[values.Criteria].icon, |
| 89 |
icon_file = local_criteria_variable[values.Criteria].icon_file, |
| 90 |
formats = local_criteria_variable[values.Criteria].formats, |
| 91 |
two_tooltips = local_criteria_variable[values.Criteria].two_tooltips, |
| 92 |
start = start[crit_name] or 0, |
| 93 |
} |
| 94 |
if win then |
| 95 |
goals[values.Group][crit_name].win_value = values.Value |
| 96 |
else |
| 97 |
goals[values.Group][crit_name].lose_value = values.Value |
| 98 |
end |
| 99 |
if values.Group > self.highest_group then self.highest_group = values.Group end |
| 100 |
end |
| 101 |
end |
| 102 |
end |
| 103 |
|
| 104 |
--! Checks if the player has won or lost by meeting all of any one group. |
| 105 |
--!param hospital (Hospital) The hospital of the tests. |
| 106 |
--!return state (string) "win" or "nothing", or |
| 107 |
--!return reason (string) If the player lost, the latest criteria met |
| 108 |
--!return limit (number) If the player lost, the number limit which the player passed |
| 109 |
function EndConditions:checkEndGame(hospital) |
| 110 |
-- If there are no goals at all, do nothing. |
| 111 |
if (not self.win_goals or #self.win_goals == 0) and |
| 112 |
(not self.lose_goals or #self.lose_goals == 0) then |
| 113 |
return "nothing" |
| 114 |
end |
| 115 |
for _, tbl in pairs(self.win_goals) do |
| 116 |
local score = self:_checkWinGroup(hospital, tbl) |
| 117 |
if score == 1 and hospital.loan == 0 then return "win" end |
| 118 |
end |
| 119 |
for _, tbl in pairs(self.lose_goals) do |
| 120 |
local reason, limit = self:_checkLoseGroup(hospital, tbl) |
| 121 |
if reason then return reason, limit end |
| 122 |
end |
| 123 |
|
| 124 |
-- No win or lose group was met, or player has a loan preventing a win |
| 125 |
return "nothing" |
| 126 |
end |
| 127 |
|
| 128 |
--! Generate table for the Progress Report dialog and progress advice. |
| 129 |
--!param hospital (Hospital) The hospital of the tests. |
| 130 |
--!return report_table (table) Maximum five fields of |
| 131 |
-- lose criteria with the smallest gap between current value and boundary, |
| 132 |
-- then fill up to five with win criteria in the best group. |
| 133 |
function EndConditions:generateReportTable(hospital) |
| 134 |
local count, lose_table, key_lose_goals_table, report_table, unique_criteria_set = 0, {}, {}, {}, {} |
| 135 |
local win_group = self.win_goals[self:_findBestWinGroup(hospital)] or {} |
| 136 |
|
| 137 |
-- Collect lose criteria over the boundary |
| 138 |
for group, tbl in pairs(self.lose_goals) do |
| 139 |
lose_table[group] = self:_checkLoseGroup(hospital, tbl, true) |
| 140 |
end |
| 141 |
-- Get the most relevant of each criterion in all groups |
| 142 |
for _, group_table in pairs(lose_table) do |
| 143 |
for crit_name, crit_table in pairs(group_table) do |
| 144 |
if not key_lose_goals_table[crit_name] or key_lose_goals_table[crit_name].gap > crit_table.gap then |
| 145 |
key_lose_goals_table[crit_name] = crit_table |
| 146 |
end |
| 147 |
end |
| 148 |
end |
| 149 |
-- Move into a numbered table |
| 150 |
for _, crit_table in pairs(key_lose_goals_table) do |
| 151 |
table.insert(report_table, crit_table) |
| 152 |
count = count + 1 |
| 153 |
end |
| 154 |
-- Limit the table to five goals, ordered by progress towards meeting the lose goal |
| 155 |
if count > 5 then |
| 156 |
table.sort(report_table, function(a,b) return a.progress > b.progress end) |
| 157 |
for n = 6, #report_table do report_table[n] = nil end |
| 158 |
end |
| 159 |
|
| 160 |
-- Fill up the report table with win criteria not already present as lose criteria |
| 161 |
for _, crit in pairs(report_table) do unique_criteria_set[crit.name] = true end |
| 162 |
for i = 1, #local_criteria_variable do |
| 163 |
if count == 5 then break end |
| 164 |
local name = local_criteria_variable[i].name |
| 165 |
if win_group[name] and not unique_criteria_set[name] then |
| 166 |
count = count + 1 |
| 167 |
report_table[count] = win_group[name] |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
-- Order by criteria number, as some criteria icons shouldn't be next to each other |
| 172 |
table.sort(report_table, function(a,b) return a.criterion < b.criterion end) |
| 173 |
return report_table |
| 174 |
end |
| 175 |
|
| 176 |
--!param hospital (Hospital) The hospital of the tests. |
| 177 |
--!param lose_table (table) A group of lose conditions from level_config. |
| 178 |
--!param report (boolean) Whether a report table will be returned. |
| 179 |
--!return Losing criteria name and the limit breached, |
| 180 |
-- or if report is true, the report table. |
| 181 |
function EndConditions:_checkLoseGroup(hospital, lose_table, report) |
| 182 |
local report_table, met_count, total_count, reason, limit = {}, 0, 0 |
| 183 |
for crit_name, crit_table in pairs(lose_table) do |
| 184 |
local boundary, lose_value = crit_table.boundary, crit_table.lose_value |
| 185 |
local max_min = crit_table.max_min == 1 and 1 or -1 |
| 186 |
local measure = self:getAttribute(hospital, crit_name) |
| 187 |
if report then -- Collect the criteria that should be reported on |
| 188 |
if (measure - boundary) * max_min > 0 then |
| 189 |
report_table[crit_name] = crit_table |
| 190 |
report_table[crit_name].progress = (1 - ((measure - lose_value)/(boundary - lose_value))) |
| 191 |
end |
| 192 |
else |
| 193 |
if (measure - lose_value) * max_min > 0 then |
| 194 |
reason, limit = crit_name, lose_value |
| 195 |
met_count = met_count + 1 |
| 196 |
end |
| 197 |
end |
| 198 |
total_count = total_count + 1 |
| 199 |
end |
| 200 |
if report then return report_table end |
| 201 |
-- Have all criteria of the group been met? |
| 202 |
if met_count == total_count then |
| 203 |
-- The latest, probably only, criterion met for the lose message |
| 204 |
return reason, limit |
| 205 |
end |
| 206 |
end |
| 207 |
|
| 208 |
--!param hospital (Hospital) The hospital of the tests. |
| 209 |
--!param win_table (table) A group of win conditions from level_config |
| 210 |
--!return (number 0-1) The score of this group. 0 is no goals met, 1 is all met |
| 211 |
function EndConditions:_checkWinGroup(hospital, win_table) |
| 212 |
local met_count, total_count = 0, 0 |
| 213 |
for crit_name, crit_table in pairs(win_table) do |
| 214 |
local max_min = crit_table.max_min == 1 and 1 or -1 |
| 215 |
if (self:getAttribute(hospital, crit_name) - crit_table.win_value) * max_min >= 0 then |
| 216 |
met_count = met_count + 1 |
| 217 |
end |
| 218 |
total_count = total_count + 1 |
| 219 |
end |
| 220 |
if met_count > 0 then |
| 221 |
return met_count / total_count |
| 222 |
else return 0 |
| 223 |
end |
| 224 |
end |
| 225 |
|
| 226 |
-- Find the group of win conditions best met by the hospital. |
| 227 |
--!param hospital (Hospital) The hospital of the tests. |
| 228 |
--!return best (number) The number of the best group. |
| 229 |
function EndConditions:_findBestWinGroup(hospital) |
| 230 |
local score, best = 0, 1 |
| 231 |
for group = 1, self.highest_group do |
| 232 |
if self.win_goals[group] then |
| 233 |
local test = self:_checkWinGroup(hospital, self.win_goals[group]) |
| 234 |
if test and test > score then best = group end |
| 235 |
end |
| 236 |
end |
| 237 |
-- Return the group number of the group that is most met by the hospital |
| 238 |
return best |
| 239 |
end |
| 240 |
|
| 241 |
-- Fetch the attribute value, through the get_custom_criteria table of |
| 242 |
-- functions if there is one for this attribute. |
| 243 |
function EndConditions:getAttribute(hospital, attribute) |
| 244 |
if get_custom_criteria[attribute] then |
| 245 |
return get_custom_criteria[attribute](hospital) |
| 246 |
else |
| 247 |
return hospital[attribute] |
| 248 |
end |
| 249 |
end |