Debian Codegraph @

CorsixTH/Lua/entities/humanoids/vip.lua

raw
1 --[[ Copyright (c) 2011 John Pirie
2 Copyright (c) 2020 lewri
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE. --]]
21
22 --[[
23 ---------------------------------- VIP Rating System --------------------------------
24 Vip rating is calculated between 0 and 15
25 If rating exceeds values, it will be capped as necessary
26 1. LITTER OBJECTS
27 - a. General litter and vomit 'litter' is counted
28 - b. At end of visit, if number <=10 award -1
29 - c. Otherwise, award +1
30 2. STAFF TIREDNESS
31 - a. If staff <=1, award +4 and skip other checks
32 - b. If any staff member very tired (over 0.7), award 2
33 - c. If average staff tiredness over tired (0.5), award 1
34 - d. Else award -1
35 3. PATIENTS
36 - a. If average patient health >= 0.2, award -1
37 - b. If average patient health < 0.2, award +1
38 - c. Assess patient warmth, too hot/cold award +2, perfect -1
39 - d. Average happiness, award 3 if <0.2; 2 if <0.4, 1 if <0.6, 0 if <0.8, else -1
40 - e. Check if anyone has died during visit, punish based on severity
41 - f. Check how many patients are cured vs. all patients, award based on %
42 - g. Check seating. Award -1 for more seated than standing, else +1
43 - h. Check maximum queue size. Award based on the longest queue
44 4. DOCTORS
45 - a. If no doctors, award +4 and skip other checks
46 - b. If more than half doctors are consultants, award -2
47 - c. If more than half doctors are juniors, award +2
48 5. ROOMS
49 - a. If there are no active rooms, award +4
50 - b. If rooms not crashed (exploded) <3, award +1
51 - c. Get average room evaluation score, award based on average level
52 -------------------------------------------------------------------------------------
53 --]]
54
55 --[[ initialisation --]]
56 corsixth.require("utility")
57
58
59 --! A `Vip` who is in the hospital to evaluate the hospital and produce a report
60 class "Vip" (Humanoid)
61
62 ---@type Vip
63 local Vip = _G["Vip"]
64
65 function Vip:Vip(...)
66 self:Humanoid(...)
67 self.hover_cursor = TheApp.gfx:loadMainCursor("default")
68 self.action_string = ""
69 self.name = ""
70 self.announced = false
71 --First we should generate an initial VIP rating
72 self.vip_rating = 12 - math.random(0,5)
73
74 self.cash_reward = 0
75 self.rep_reward = 0
76 self.vip_message = 0
77 self.enter_deaths = 0
78 self.enter_visitors = 0
79 -- patients in the hospital when VIP arrives
80 self.enter_patients = 0
81 self.enter_cures = 0
82 self.num_vomit_noninducing = 0
83 self.num_vomit_inducing = 0
84 self.found_vomit = {}
85 self.num_visited_rooms = 0
86 self.room_eval = 0
87 -- sets the chance VIP visits each room, default is 50% or 1/2. For every 40 rooms in a hospital over 79 we increase n by 1 and chance is 1/n+1
88 self.room_visit_chance = 1
89 self.waiting = 0
90 self.slow_animation = true
91 end
92
93 --[[--VIP while on premises--]]
94 function Vip:tickDay()
95 -- for the vip
96 if self.waiting then
97 self.waiting = self.waiting - 1
98 if self.waiting == 0 then
99 if #self.world.rooms == 0 then
100 -- No rooms have been built yet
101 self:goHome()
102 end
103 self:getNextRoom()
104 self.waiting = nil
105 end
106 end
107 if not self.going_home then
108 self.world:findObjectNear(self, "litter", 8, function(x, y)
109 local litter = self.world:getObject(x, y, "litter")
110 if not litter then
111 return
112 end
113
114 local alreadyFound = false
115 for i=1, (self.num_vomit_noninducing + self.num_vomit_inducing) do
116 if self.found_vomit[i] == litter then
117 alreadyFound = true
118 break
119 end
120 end
121
122 self.found_vomit[(self.num_vomit_noninducing + self.num_vomit_inducing + 1)] = litter
123
124 if not alreadyFound then
125 if litter:anyLitter() then
126 self.num_vomit_noninducing = self.num_vomit_noninducing + 1
127 else
128 self.num_vomit_inducing = self.num_vomit_inducing + 1
129 end
130 end
131 end)
132 end
133 return Humanoid.tickDay(self)
134 end
135
136 function Vip:getNextRoom()
137 -- Find out the next room to visit.
138 self.next_room_no, self.next_room = next(self.world.rooms, self.next_room_no)
139 if self.next_room == nil then
140 return self:setNextAction(VipGoToNextRoomAction())
141 end
142 -- Always visit the first room.
143 if self.num_visited_rooms > 0 then
144 local roll_to_visit = math.random(0, self.room_visit_chance)
145 -- Run a loop until roll_to_visit passes, or we hit a room the VIP must visit
146 while roll_to_visit ~= self.room_visit_chance and not self.next_room.room_info.vip_must_visit do
147 self.next_room_no, self.next_room = next(self.world.rooms, self.next_room_no) -- get new room
148 if self.next_room == nil then
149 -- We ran out of rooms, time to leave
150 break
151 end
152 roll_to_visit = math.random(0, self.room_visit_chance)
153 end
154 end
155 -- Make sure room is active. If not, always visit the next available room.
156 while self.next_room and not self.next_room.is_active do
157 self.next_room_no, self.next_room = next(self.world.rooms, self.next_room_no)
158 end
159 self:setNextAction(VipGoToNextRoomAction())
160 end
161
162 -- display the VIP name in the info box
163 function Vip:updateDynamicInfo(action_string)
164 self:setDynamicInfo('text', {self.name})
165 end
166
167 -- Disable showing VIP mood on hover
168 function Vip:setIfHoverMoodsVisible()
169 self.hover_moods = false
170 end
171
172 --[[--VIP is leaving--]]
173 function Vip:goHome()
174 if self.going_home then
175 return
176 end
177
178 self:unregisterCallbacks()
179 -- Set the rating.
180 self:setVIPRating()
181
182 self.going_home = true
183 -- save self.hospital so we can reference it in self:onDestroy
184 self.last_hospital = self.hospital
185 self:despawn()
186 end
187
188 function Vip:evaluateRoom()
189 local room_extinguisher = 0
190 local room_plant = 0
191 local room_bin = 0
192 -- Another room visited.
193 self.num_visited_rooms = self.num_visited_rooms + 1
194 local room = self.next_room
195 -- If the player is about to kill a live patient for research, punish hard
196 if room.room_info.id == "research" then
197 if room:getPatient() then
198 self.vip_rating = self.vip_rating + 6
199 end
200 end
201 -- Evaluate the room we're currently looking at
202 for object, _ in pairs(room.objects) do
203 if object.object_type.id == "extinguisher" and room_extinguisher == 0 then
204 self.room_eval = self.room_eval + 1
205 -- Only count this object type once
206 room_extinguisher = 1
207 elseif object.object_type.id == "plant" then
208 room_plant = object:isDying() and room_plant - 1 or room_plant + 1
209 elseif object.object_type.id == "bin" and room_bin == 0 then
210 self.room_eval = self.room_eval + 1
211 -- Only count this object type once
212 room_bin = 1
213 elseif object:isMachine() then
214 self.room_eval = object:isBreaking() and self.room_eval - 1 or self.room_eval + 1
215 end
216 end
217 -- Check whether we had more good or bad plants
218 if room_plant < 0 then
219 self.room_eval = self.room_eval - 1
220 elseif room_plant > 0 then
221 self.room_eval = self.room_eval + 1
222 end
223 self:getNextRoom()
224 end
225
226 --[[--VIP has left--]]
227 -- Called when the vip is out of the hospital grounds
228 function Vip:onDestroy()
229 if self.world.free_build_mode then
230 self.last_hospital:unconditionalChangeReputation(20)
231 elseif self.vip_rating <= 7 then
232 self.last_hospital:receiveMoney(self.cash_reward, _S.transactions.vip_award)
233 self.last_hospital:unconditionalChangeReputation(self.rep_reward)
234 self.last_hospital.pleased_vips_ty = self.last_hospital.pleased_vips_ty + 1
235 else
236 self.last_hospital:unconditionalChangeReputation(self.rep_reward)
237 end
238
239 self.last_hospital:makeVipEndFax(self.vip_rating, self.name, self.cash_reward, self.vip_message)
240 self.world:nextVip()
241
242 return Humanoid.onDestroy(self)
243 end
244
245 function Vip:setVIPRating()
246 -- First do room code for later
247 local count_rooms = 0
248 local max_queue = 0
249 for _, room in pairs(self.world.rooms) do
250 if not room.crashed then
251 count_rooms = count_rooms + 1
252 end
253 if room.door.queue then
254 max_queue = max_queue < room.door.queue:size() and room.door.queue:size() or max_queue
255 end
256 end
257
258 --[[-- Group factor 1: Litter--]]
259 if (self.num_vomit_noninducing + self.num_vomit_inducing) <= 10 then
260 self.vip_rating = self.vip_rating - 1
261 else
262 self.vip_rating = self.vip_rating + 1
263 end
264
265 --[[-- Group factor 2: Staff tiredness--]]
266 -- First get staff members
267 local count_staff = #self.hospital.staff
268 if count_staff > 1 then
269 -- Loop through staff tiredness, if any above verytired, break loop
270 for _, staff in ipairs(self.hospital.staff) do
271 if staff:isVeryTired() then
272 self.vip_rating = self.vip_rating + 2
273 break
274 end
275 end
276 -- Average all staff tiredness
277 local avg_tired = self.hospital:getAverageStaffAttribute("fatigue", 1)
278 if avg_tired >= 0.5 then
279 self.vip_rating = self.vip_rating + 1
280 else
281 self.vip_rating = self.vip_rating - 1
282 end
283 else
284 -- Penalise where there is one or no staff
285 self.vip_rating = self.vip_rating + 4
286 end
287
288 --[[-- Group factor 3: Patients--]]
289 -- First check we had patients this visit
290 local patients_this_visit = self.enter_patients + self.hospital.num_visitors - self.enter_visitors
291 if patients_this_visit > 0 then
292 -- Average all patient health
293 local avg_health = self.hospital:getAveragePatientAttribute("health", 0.19)
294 if avg_health >= 0.2 then
295 self.vip_rating = self.vip_rating - 1
296 else
297 self.vip_rating = self.vip_rating + 1
298 end
299
300 -- Get patient warmth
301 local avg_warmth = self.hospital:getAveragePatientAttribute("warmth", nil)
302 -- Punish if too cold/hot
303 if avg_warmth then
304 local patients_warmth_ratio_rangemap = {
305 {upper = 0.22, value = 2},
306 {upper = 0.36, value = -1},
307 {value = 2}
308 }
309 self.vip_rating = self.vip_rating + rangeMapLookup(avg_warmth, patients_warmth_ratio_rangemap)
310 end
311
312 -- Check average patient happiness
313 local avg_happiness = self.hospital:getAveragePatientAttribute("happiness", nil)
314 if avg_happiness then
315 local patients_happy_ratio_rangemap = {
316 {upper = 0.20, value = 3},
317 {upper = 0.40, value = 2},
318 {upper = 0.60, value = 1},
319 {upper = 0.80, value = 0},
320 {value = -1}
321 }
322 self.vip_rating = self.vip_rating + rangeMapLookup(avg_happiness, patients_happy_ratio_rangemap)
323 end
324
325 -- Check the visitor to patient death ratio
326 local death_diff = self.hospital.num_deaths - self.enter_deaths
327 if death_diff ~= 0 then -- no deaths are good, but also expected
328 local death_ratio = patients_this_visit / death_diff
329 local death_ratio_rangemap = {
330 {upper = 2, value = 4},
331 {upper = 4, value = 3},
332 {upper = 8, value = 2},
333 {upper = 12, value = 1},
334 {value = 0}
335 }
336 self.vip_rating = self.vip_rating + rangeMapLookup(death_ratio, death_ratio_rangemap)
337 end
338
339 -- Check the visitor to patient cure ratio
340 local cure_diff = self.hospital.num_cured - self.enter_cures
341 if cure_diff ~= 0 then -- no cures are bad
342 local cure_ratio = patients_this_visit / cure_diff
343 local cure_ratio_rangemap = {
344 {upper = 2, value = -1},
345 {upper = 3, value = 0},
346 {upper = 4, value = 1},
347 {upper = 5, value = 2},
348 {value = 3}
349 }
350 self.vip_rating = self.vip_rating + rangeMapLookup(cure_ratio, cure_ratio_rangemap)
351 else
352 self.vip_rating = self.vip_rating + 3
353 end
354
355 -- Check the seating : standing ratio of waiting patients
356 local sum_sitting, sum_standing = self.hospital:countSittingStanding()
357 if (sum_sitting + sum_standing) ~= 0 then
358 if sum_sitting >= sum_standing or sum_standing == 0 then
359 self.vip_rating = self.vip_rating - 1
360 else
361 self.vip_rating = self.vip_rating + 1
362 end
363 end
364
365 -- Check the maximum queue length
366 if max_queue == 0 then
367 self.vip_rating = self.vip_rating - 1
368 else
369 local queue_ratio_rangemap = {
370 {upper = 3, value = -1},
371 {upper = 6, value = 0},
372 {upper = 9, value = 1},
373 {value = 2}
374 }
375 self.vip_rating = self.vip_rating + rangeMapLookup(max_queue, queue_ratio_rangemap)
376 end
377 end
378
379 --[[--Group factor 4: Doctor ratios--]]
380 -- First get all doctors
381 local num_docs = self.hospital:countStaffOfCategory("Doctor")
382 -- No doctors are bad
383 if num_docs == 0 then
384 self.vip_rating = self.vip_rating + 4
385 else
386 -- Count num. consultants, num. juniors
387 local num_cons = self.hospital:countStaffOfCategory("Consultant")
388 local num_junior = self.hospital:countStaffOfCategory("Junior")
389
390 -- Check consultant and junior proportions
391 if num_cons / num_docs > 0.5 then
392 self.vip_rating = self.vip_rating - 1
393 elseif num_junior / num_docs > 0.5 then
394 self.vip_rating = self.vip_rating + 1
395 end
396 end
397
398 --[[--Group factor 5: Rooms--]]
399 -- Low room numbers incur a penalty
400 if count_rooms < 1 then
401 self.vip_rating = self.vip_rating + 4
402 else
403 if count_rooms < 3 then
404 self.vip_rating = self.vip_rating + 1
405 end
406 -- Room decor average
407 local avg_room_eval = self.room_eval / self.num_visited_rooms
408 local room_eval_rangemap = {
409 {upper = 1.5, value = 3},
410 {upper = 2, value = 1},
411 {upper = 3, value = 0},
412 {value = -1}
413 }
414 if self.num_visited_rooms ~= 0 then
415 self.vip_rating = self.vip_rating + rangeMapLookup(avg_room_eval, room_eval_rangemap)
416 end
417 end
418
419 --[[--Finalise score--]]
420 -- Documented rewards
421 local rewards = {
422 [1] = 4000,
423 [2] = 2000,
424 [3] = 1500,
425 [4] = 1200,
426 [5] = 800,
427 [6] = 400,
428 [7] = 200,
429 [8] = 0,
430 }
431 -- Documented reps
432 local rep_change = {
433 [1] = 50,
434 [2] = 45,
435 [3] = 40,
436 [4] = 35,
437 [5] = 30,
438 [6] = 25,
439 [7] = 20,
440 [8] = 15,
441 [9] = 10,
442 [10] = 5,
443 [11] = -5,
444 [12] = -10,
445 [13] = -15,
446 [14] = -20,
447 [15] = -25,
448 }
449 -- Set rewards
450 self.vip_rating = self.vip_rating > 15 and 15 or self.vip_rating
451 self.vip_rating = self.vip_rating < 1 and 1 or self.vip_rating
452 self.cash_reward = rewards[self.vip_rating] or 0
453 self.rep_reward = rep_change[self.vip_rating]
454 self.vip_message = self.vip_rating
455 self.hospital.num_vips_ty = self.hospital.num_vips_ty + 1
456 end
457
458 function Vip:afterLoad(old, new)
459 if old < 50 then
460 self.num_visited_rooms = 0
461 self:setNextAction(IdleAction())
462 self.waiting = 1
463 for _, room in pairs(self.world.rooms) do
464 if room.door.reserved_for == self then
465 room.door.reserved_for = nil
466 room:tryAdvanceQueue()
467 end
468 end
469 end
470 if old < 79 then
471 self.name = self.hospital.visitingVIP
472 end
473 if old < 144 then
474 self.vip_rating = 12 - math.random(0,5)
475 -- Make sure we only rate rooms from now on if a VIP was visiting
476 self.room_eval = 0
477 self.num_visited_rooms = 0
478 -- VIP's room visit chance is 50% if total rooms in hospital is less than 80 (makes a math.random with 0 and 1 possibilities).
479 -- Else decided by total rooms / 40 (0, 1, 2 [33%]; 0, 1, 2, 3 [25%] etc)
480 local rooms_threshold = 79
481 if #self.world.rooms > rooms_threshold then
482 self.room_visit_chance = math.floor(#self.world.rooms / 40)
483 else
484 self.room_visit_chance = 1
485 end
486 -- If our hospital has more patients than counted visitors adjust enter_patients
487 self.enter_patients = #self.hospital.patients + self.enter_visitors - self.hospital.num_visitors
488 if self.enter_patients < 0 then
489 self.enter_patients = 0
490 end
491 if self.going_home then
492 --award max from old code if VIP leaving
493 self.vip_rating = 2
494 self.cash_reward = 2000
495 self.rep_reward = 45
496 self.vip_message = 2
497 end
498 for i, action in ipairs(self.action_queue) do
499 if action.name == 'idle' and action.loop_callback and (self.waiting > 1 or i > 1) then
500 action:setCount(50):setAfterUse(action.loop_callback)
501 action.loop_callback = nil
502 self.waiting = nil
503 if i == 1 then
504 self:queueAction(action, 1)
505 self:finishAction()
506 end
507 end
508 end
509 end
510
511 if old < 207 then
512 self.slow_animation = true
513 end
514
515 if old < 213 then
516 self.mood_marker = 2
517 end
518
519 self:updateDynamicInfo()
520 Humanoid.afterLoad(self, old, new)
521 end