Debian Codegraph @

CorsixTH/Lua/staff_profile.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 class "StaffProfile"
22
23 ---@type StaffProfile
24 local StaffProfile = _G["StaffProfile"]
25
26 function StaffProfile:StaffProfile(world, humanoid_class, local_string)
27 self.world = world
28 self.humanoid_class = humanoid_class
29 self.name = "Initialised"
30 self.initial = "UN"
31 self.name_seed = math.random(1, 1009)
32 self.name_lang = self.world.app.config.language
33 self.wage = 0
34 self.skill = 0 -- [0.0, 1.0]
35 self.layer5 = 2
36 self.attention_to_detail = math.random()
37 self.profession = local_string
38 end
39
40 function StaffProfile:setDoctorAbilities(psychiatrist, surgeon, researcher, junior, consultant)
41 self.is_psychiatrist = psychiatrist
42 self.is_surgeon = surgeon
43 self.is_researcher = researcher
44 self.is_junior = junior
45 self.is_consultant = consultant
46 end
47
48 function StaffProfile:initDoctor(psychiatrist, surgeon, researcher, junior, consultant, skill)
49 self:setDoctorAbilities(psychiatrist, surgeon, researcher, junior, consultant)
50 self:init(skill)
51 end
52
53 function StaffProfile:init(skill)
54 self:setSkill(skill)
55 self.wage = self:getFairWage()
56 self:randomiseOrganical()
57 end
58
59 function StaffProfile:setSkill(skill)
60 self.skill = skill
61 self:parseSkillLevel()
62 end
63
64 local name_parts = {_S.humanoid_name_starts, _S.humanoid_name_ends}
65
66 local function shuffle(t)
67 local r = {}
68 local i = {}
69 for k in ipairs(t) do
70 r[k] = math.random()
71 i[k] = k
72 end
73 table.sort(i, function(x, y) return r[x] < r[y] end)
74 for k in ipairs(t) do
75 r[k] = t[i[k]]
76 end
77 return r
78 end
79
80 local function our_concat(t)
81 -- The standard table.concat function doesn't like our userdata strings :(
82 local result = ""
83 for _, s in ipairs(t) do
84 result = result .. s
85 end
86 return result
87 end
88
89 function StaffProfile:randomise(month)
90 local level_config = self.world.map.level_config
91
92 -- decide general skill for all staff
93 self.skill = math.random()
94 --self.skill_level_modifier = math.random(-50, 50) / 1000 -- [-0.05, +0.05]
95
96 if self:isType("Doctor") then
97 local jr_limit = level_config.gbv.DoctorThreshold / 1000
98 local cons_limit = level_config.gbv.ConsultantThreshold / 1000
99
100 if self.world:getLocalPlayerHospital().hosp_cheats:isCheatActive("super_doctor") then
101 self:initDoctor(1, 1, 1, false, true, math.random(cons_limit * 1000, 1000) / 1000)
102 else
103 -- find the correct config line (based on month) for generation of the doctor
104 local i = 0
105 while i < #level_config.staff_levels and
106 level_config.staff_levels[i+1].Month <= month do
107 i = i+1
108 end
109
110 -- list of level_config values and the corresponding staff modifiers, plus the value set for "no"
111 local mods = {
112 {"ShrkRate", "is_psychiatrist", 0},
113 {"SurgRate", "is_surgeon", 0},
114 {"RschRate", "is_researcher", 0},
115 {"JrRate", "is_junior", nil},
116 {"ConsRate", "is_consultant", nil},
117 }
118
119 -- The following assumes ascending month order of the staff_levels table.
120 -- TODO don't assume this but sort when loading map config
121 for _, m in ipairs(mods) do
122 local rate
123 local ind = i
124 while not rate do
125 assert(ind >= 0, "Staff modifier " .. m[1] .. " not existent (should at least be given by base_config).")
126 rate = level_config.staff_levels[ind][m[1]]
127 ind = ind - 1
128 end
129 -- 0 means none. Other values x mean "one in x"; thus 1 means "one in one" aka "all"
130 rate = (rate == 0) and 0 or 1 / rate
131 self[m[2]] = math.random() < rate and 1.0 or m[3]
132 end
133
134 -- is_consultant is forced to nil if is_junior is already 1
135 self.is_consultant = not self.is_junior and self.is_consultant or nil
136
137 -- put the doctor in the right skill level box
138 if self.is_junior then
139 self.skill = jr_limit * self.skill
140 elseif self.is_consultant then
141 self.skill = cons_limit + ((1 - cons_limit) * self.skill)
142 else
143 self.skill = jr_limit + ((cons_limit - jr_limit) * self.skill)
144 end
145 end
146 end
147 self.wage = self:getFairWage()
148 self:parseSkillLevel()
149 self:randomiseOrganical()
150 end
151
152 --! Give the staff member some personal properties.
153 function StaffProfile:randomiseOrganical()
154 local initials = staff_initials_cache.initials
155 local num = self.name_seed % #initials
156 self.initial = initials[num]
157 self.name = ""
158 for _, part_table in ipairs(name_parts) do
159 self.name = self.name .. part_table.__random
160 end
161
162 local desc_table1, desc_table2
163 if self.skill < 0.55 then
164 desc_table1 = _S.staff_descriptions.bad
165 desc_table2 = _S.staff_descriptions.misc
166 else
167 desc_table1 = _S.staff_descriptions.good
168 desc_table2 = _S.staff_descriptions.misc
169 end
170 local descs = {desc_table1.__random,
171 desc_table2.__random}
172 while #our_concat(descs) > 96 do
173 descs[#descs] = nil
174 end
175 self.desc = our_concat(shuffle(descs))
176 if self:isType("Doctor") then
177 self.is_black = math.random(0, 1) == 0
178 if self.is_black then
179 self.hair_index = math.random(5, 9)
180 self.face_index = math.random(5, 9)
181 self.chin_index = math.random(5, 9)
182 self.layer5 = 4
183 else
184 self.hair_index = math.random(0, 4)
185 self.face_index = math.random(0, 4)
186 self.chin_index = math.random(0, 4)
187 self.layer5 = 2
188 end
189 elseif self:isType("Nurse") then
190 self.hair_index = math.random(10, 12)
191 self.face_index = math.random(10, 12)
192 self.chin_index = math.random(10, 12)
193 elseif self:isType("Receptionist") then
194 self.hair_index = math.random(13, 14)
195 self.face_index = math.random(13, 14)
196 self.chin_index = math.random(13, 14)
197 elseif self:isType("Handyman") then
198 self.hair_index = math.random(15, 17)
199 self.face_index = math.random(15, 17)
200 self.chin_index = math.random(15, 17)
201 end
202 end
203
204 function StaffProfile:drawFace(canvas, x, y, parts_bitmap, scale)
205 scale = scale or 1
206 canvas:scale(scale, "bitmap")
207 parts_bitmap:draw(canvas, x, y , 0, self.hair_index * 29, 65, 29)
208 parts_bitmap:draw(canvas, x, y + 29 * scale, 0, 522 + self.face_index * 24, 65, 24)
209 parts_bitmap:draw(canvas, x, y + 53 * scale, 0, 954 + self.chin_index * 22, 65, 22)
210 canvas:scale(1, "bitmap")
211 end
212
213 -- Update junior and consultant status
214 function StaffProfile:parseSkillLevel()
215 local level_config = self.world.map.level_config
216
217 local junior_skill = level_config.gbv.DoctorThreshold / 1000
218 self.is_junior = self.skill <= junior_skill and 1 or nil
219
220 local consultant_skill = level_config.gbv.ConsultantThreshold / 1000
221 self.is_consultant = self.skill >= consultant_skill and 1 or nil
222 end
223
224 local conf_id = {
225 Nurse = 0,
226 Doctor = 1,
227 Handyman = 2,
228 Receptionist = 3,
229 }
230
231 local ability_conf_id = {
232 is_junior = 3,
233 is_doctor = 4,
234 is_surgeon = 5,
235 is_psychiatrist = 6,
236 is_consultant = 7,
237 is_researcher = 8,
238 }
239
240 function StaffProfile:getFairWage()
241 if self.world.free_build_mode then
242 return 0
243 end
244
245 local level_config = self.world.map.level_config
246 local wage = level_config.staff[conf_id[self.humanoid_class]].MinSalary
247 wage = wage + self.skill * 1000 / level_config.gbv.SalaryAbilityDivisor
248 if self:isType("Doctor") then
249 for name, id in pairs(ability_conf_id) do
250 if self[name] == 1 then
251 wage = wage + level_config.gbv.SalaryAdd[id]
252 end
253 end
254 if not self.is_junior and not self.is_consultant then
255 wage = wage + level_config.gbv.SalaryAdd[ability_conf_id.is_doctor]
256 end
257 end
258 return math.max(math.floor(wage), level_config.staff[conf_id[self.humanoid_class]].MinSalary)
259 end
260
261 function StaffProfile:getRiseAmount()
262 local rise_amount = math.floor(math.max(self.wage * 1.1, (self:getFairWage(self.world) + self.wage) / 2) - self.wage)
263 return rise_amount
264 end
265
266 function StaffProfile:getFullName()
267 return self.initial .. ". " .. self.name
268 end
269
270 --! Translate the staff class to its translated text.
271 --!param staff_class Class of the staff to translate.
272 --!return The translated name.
273 function StaffProfile.translateStaffClass(staff_class)
274 local staffclass_to_string = {
275 Nurse = _S.staff_title.nurse,
276 Doctor = _S.staff_title.doctor,
277 Surgeon = _S.staff_title.surgeon,
278 Psychiatrist = _S.staff_title.psychiatrist,
279 Researcher = _S.staff_title.researcher,
280 }
281 return staffclass_to_string[staff_class]
282 end
283
284 function StaffProfile:isType(humanoid_class)
285 return self.humanoid_class == humanoid_class
286 end