-
Notifications
You must be signed in to change notification settings - Fork 354
/
hospital.lua
2498 lines (2240 loc) · 88.2 KB
/
hospital.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[ Copyright (c) 2009 Peter "Corsix" Cawley
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
corsixth.require("announcer")
class "Hospital"
---@type Hospital
local Hospital = _G["Hospital"]
function Hospital:Hospital(world, avail_rooms, name)
self.world = world
local level_config = world.map.level_config
local level = world.map.level_number
local balance, interest_rate_numerator, reputation, overdraft_differential_numerator
if level_config.towns and level_config.towns[level] then
balance = level_config.towns[level].StartCash
interest_rate_numerator = level_config.towns[level].InterestRate
reputation = level_config.towns[level].StartRep
overdraft_differential_numerator = level_config.towns[level].OverdraftDiff
elseif level_config.town then
balance = level_config.town.StartCash
interest_rate_numerator = level_config.town.InterestRate
reputation = level_config.town.StartRep
overdraft_differential_numerator = level_config.town.OverdraftDiff
end
self.name = name or "PLAYER"
-- When playing in free build mode you don't care about money.
self.balance = not world.free_build_mode and balance or 0
self.loan = 0
self.acc_loan_interest = 0
self.acc_research_cost = 0
self.acc_overdraft = 0
self.acc_heating = 0
-- The sum of all material values (tiles, rooms, objects).
-- Initial value: hospital tile count * tile value + 20000
self.value = world.map:getParcelPrice(self:getPlayerIndex()) + 20000
self.player_salary = 10000
self.salary_offer = 0
self.handymanTasks = {}
-- Represents the "active" epidemic non-nil if
-- an epidemic is happening currently in the hospital
-- Only one epidemic is ever "active"
self.epidemic = nil
-- The pool of epidemics which may happen in the future.
-- Epidemic in this table continue in the background its
-- patients infecting each other. Epidemics are chosen from
-- this pool to become the "active" epidemic
self.future_epidemics_pool = {}
-- How many epidemics can exist simultaneously counting current and future
-- epidemics. If epidemic_limit = 1 then only one epidemic can exist at a
-- time either in the futures pool or as a current epidemic.
self.concurrent_epidemic_limit = level_config.gbv.EpidemicConcurrentLimit or 1
-- Initial values
self.interest_rate = interest_rate_numerator / 10000
self.inflation_rate = 0.045
self.overdraft_interest_rate = self.interest_rate + overdraft_differential_numerator / 10000
self.salary_incr = level_config.gbv.ScoreMaxInc or 300
self.sal_min = level_config.gbv.ScoreMaxInc / 6 or 50
self.reputation_min = 0
self.reputation_max = 1000
self.reputation = math.min(math.max(reputation, self.reputation_min), self.reputation_max)
local difficulty = self.world.map:getDifficulty()
-- Price distortion level under which the patients might consider the
-- treatment to be under-priced.
local under_priced_thresholds = {-0.3, -0.4, -0.5}
self.under_priced_threshold = under_priced_thresholds[difficulty]
-- Price distortion level over which the patients might consider the
-- treatment to be over-priced.
local over_priced_thresholds = {0.4, 0.3, 0.2}
self.over_priced_threshold = over_priced_thresholds[difficulty]
-- (int) Number of days until the next heating or vomit wave disaster.
-- TODO: Implement the vomit wave.
self.disasterless_days = self:daysUntilNextDisaster()
-- Heating system variables.
self.heating = {
radiator_heat = 0.5, -- (float) [0..1] fraction of heating by a radiator.
saved_radiator_heat = nil, -- (float) Saved radiator heat when boiler has broken down.
boiler_repair_count = nil, -- (int) Number of items to repair.
heating_broke = false -- (bool) Whether the heating system is broken down currently.
}
-- Number of tile objects of each category in the hospital.
self.tile_object_counts = {
extinguisher = 0,
radiator = 0,
plant = 0,
reception_desk = 0,
bench = 0,
general = 0,
}
self.num_visitors = 0 -- Counts patients only
self.num_deaths = 0
self.num_deaths_this_year = 0
self.num_cured = 0
self.not_cured = 0
self.num_explosions = 0
self.vip_declined = 0
self.num_vips = 0 -- used to check if it's the user's first vip
self.percentage_cured = 0
self.percentage_killed = 0
self.population = 0.25 -- TODO: Percentage showing how much of
-- the total population that goes to the player's hospital,
-- used for one of the goals. Change when competitors are there.
-- Statistics used in the graph dialog. Each entry is the month, inside it
-- is "money in", "money out", wages, balance, visitors, cures, deaths, reputation
-- statistic[i] shows what the values were when going from month i - 1 to i.
self.statistics = {
{
money_in = 0,
money_out = 0,
wages = 0,
balance = balance,
visitors = 0,
cures = 0,
deaths = 0,
reputation = self.reputation
}
}
self.money_in = 0
self.money_out = 0
-- Other statistics, back to zero each year
self.has_impressive_reputation = true
self:unconditionalChangeReputation(0) -- Reset self.has_impressive_reputation
self.sodas_sold = 0
self.num_vips_ty = 0 -- used to count how many VIP visits in the year for an award
self.pleased_vips_ty = 0
self.num_cured_ty = 0
self.not_cured_ty = 0
self.num_visitors_ty = 0
self.ownedPlots = {1} -- Plots owned by the hospital
self.ratholes = {} -- List of table {x, y, wall, parcel, optional object} for ratholes in the hospital corridors.
self.is_in_world = true -- Whether the hospital is in this world (AI hospitals are not)
self.opened = false -- Whether the hospital is opened (timer was cleared or ended).
self.transactions = {}
self.staff = {}
self.patients = {}
self.debug_patients = {} -- right-click-commandable patients for testing
self.disease_casebook = {}
self.discovered_diseases = {} -- a list
-- Create a cheats instance for each hospital. AIHospitals don't know how to cheat (we hope!)
self.hosp_cheats = Cheats(self)
-- Make a table containing available rooms for the level, and its discovery status (room, discovery_status)
-- Index is the room's id e.g. gp
self.room_discoveries = {}
for _, avail_room in ipairs(avail_rooms) do
self.room_discoveries[avail_room.room.id] = {
room = avail_room.room,
is_discovered = avail_room.is_discovered or false
}
end
local gbv = level_config.gbv
self.policies = {
staff_allowed_to_move = true,
send_home = 0.1,
guess_cure = 0.9,
stop_procedure = 1, -- Note that this is between 1 and 2 ( = 100% - 200%)
goto_staffroom = gbv.Tired / 1000, -- Defaults to 0.6 on normal difficulty
grant_wage_increase = TheApp.config.grant_wage_increase,
}
-- Semi-randomly select three insurance companies to use, only different by name right now.
-- The companies in the first quarter of the list are more likely to be selected
self.insurance = {}
-- Make a local writeable copy table of the translated company names.
local companies = {}
for no, local_name in ipairs(_S.insurance_companies) do
companies[no] = local_name
end
while #self.insurance < 3 and #companies > 0 do
local num = math.random(1, 2) == 1 and math.random(1, math.ceil(#companies / 4)) or
math.random(1, #companies)
self.insurance[#self.insurance + 1] = companies[num]
table.remove(companies, num)
end
-- A list of how much each insurance company owes you. The first entry for
-- each company is the current month's dept, the second the previous
-- month and the third the month before that.
-- All payment that goes through an insurance company a given month is paid two
-- months later. For example diagnoses in April are paid the 1st of July
self.insurance_balance = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
-- Initialize diseases
local diseases = TheApp.diseases
local expertise = level_config.expertise
for _, disease in ipairs(diseases) do
local disease_available = true
local drug_effectiveness = 95
local drug = disease.treatment_rooms and disease.treatment_rooms[1] == "pharmacy" or nil
local drug_cost = 100
if expertise then
disease_available = expertise[disease.expertise_id].Known == 1
-- This means that the config is available
drug_effectiveness = gbv.StartRating
drug_cost = gbv.StartCost
end
if world.available_diseases[disease.id] or disease.pseudo then
local info = {
reputation = not disease.pseudo and 500 or nil,
price = 1.0, -- user-set multiplier between 0.5 and 2.0
money_earned = 0,
recoveries = 0,
fatalities = 0,
turned_away = 0,
disease = disease,
discovered = disease_available,
concentrate_research = false,
cure_effectiveness = drug and drug_effectiveness or 100,
-- This will only work as long as there's only one treatment room.
drug = drug,
drug_cost = drug and drug_cost,
psychiatrist = disease.treatment_rooms and disease.treatment_rooms[1] == "psych" or nil,
machine = disease.requires_machine,
-- This assumes we always have the ward then the operating_theatre as treatment rooms.
surgeon = disease.treatment_rooms and disease.treatment_rooms[2] == "operating_theatre" or nil,
researcher = disease.requires_researcher, -- TODO: Fix when aliens are in the game.
pseudo = disease.pseudo, -- Diagnosis is pseudo
}
self.disease_casebook[disease.id] = info
if disease_available and not disease.pseudo then
self.discovered_diseases[#self.discovered_diseases + 1] = disease.id
end
end
end
self.research = ResearchDepartment(self)
-- Initialize build cost for all available rooms.
for _, avail_room in ipairs(avail_rooms) do
self.research.research_progress[avail_room.room] = {
-- In free build mode, everything is freely available.
build_cost = not self.free_build_mode and avail_room.build_cost or 0,
}
end
end
--! Checks if a room has been discovered
--!param room_id (string) The name of the room
--!return (boolean) true if discovered, otherwise false
function Hospital:isRoomDiscovered(room_id)
return self.room_discoveries[room_id].is_discovered
end
--! Update the loaded game with version 'old' to the version 'new'.
--!param old Version of the loaded game.
--!param new Version of the code being executed.
function Hospital:afterLoad(old, new)
if old < 8 then
-- The list of discovered rooms was not saved. The best we can do is make everything
-- discovered which is available for the level.
self.discovered_rooms = {}
for _, room in ipairs(self.world.available_rooms) do
self.discovered_rooms[room] = true
end
end
if old < 9 then
-- Initial opening added
self.opened = true
end
if old < 14 then
-- NOTE: This will no longer work, but cluttering the code with stub functions
-- for this "old" compatibility, is it necessary?
self:_initResearch()
end
if old < 19 then
-- The statistics on the current map will be wrong, but it's better than nothing.
self.num_visitors = 0
self.player_salary = 10000
self.sodas_sold = 0
end
if old < 20 then
-- New variables
self.acc_loan_interest = 0
self.acc_research_cost = 0
-- Go through all diseases and remove individual reputation for diagnoses
for _, disease in pairs(self.disease_casebook) do
if disease.pseudo == true then
disease.reputation = nil
end
end
-- Go through all possible rooms and add those not researched to the research list.
self.research_rooms = {}
local next_diag = nil
local next_cure = nil
for _, room in ipairs(self.world.available_rooms) do
if not self.discovered_rooms[room] then
self.research_rooms[room] = 0
if room.categories.diagnosis then
next_diag = room
else
next_cure = room
end
end
end
-- Go through all rooms and find if a research department has been built
-- Also check for training rooms where the training_factor needs to be set
for _, room in pairs(self.world.rooms) do
if room.room_info.id == "research" then
self.research_dep_built = true
elseif room.room_info.id == "training" then
-- A standard value to keep things going
room.training_factor = 5
end
end
-- Redefine the research table
self.research = {
improvements = {frac = 20, points = 0, current = "inflation"},
drugs = {frac = 20, points = 0, current = "invisibility"},
diagnosis = {frac = 20, points = 0, current = next_diag},
cure = {frac = 20, points = 0, current = next_cure},
specialisation = {frac = 20, points = 0, current = "special"},
global = 100,
}
self.discover_autopsy_risk = 10
end
if old < 24 then
-- New variables
self.salary_incr = 300
self.sal_min = 50
self.salary_offer = 0
end
if old < 25 then
self.insurance_balance = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
self.num_deaths_this_year = 0
end
if old < 30 then
if self.emergency then
self.emergency.percentage = 0.75
end
end
if old < 33 then
-- Research has been revamped and expanded. Drugs and improvements
-- will work fine, but room research needs to be rebuilt.
local research = ResearchDepartment(self)
self.undiscovered_rooms = {}
local cure, diagnosis
local cfg_objects = self.world.map.level_config.objects
for _, room in ipairs(self.world.available_rooms) do
-- If a room is discovered, make sure its objects are also
-- discovered, otherwise add it to the undiscovered list.
if self.discovered_rooms[room] then
for name, _ in pairs(room.objects_needed) do
local object = TheApp.objects[name]
if cfg_objects[object.thob] and cfg_objects[object.thob].AvailableForLevel == 1 and
object.research_category and not research.research_progress[object].discovered then
local progress = self.research_rooms[room]
if self.research.cure.current == room or self.research.diagnosis.current == room then
progress = progress + self.research.diagnosis.points
end
research.research_progress[object].discovered = true
research.research_progress[object].points = progress
end
end
else
self.undiscovered_rooms[room] = true
if not cure or not diagnosis then
for name, _ in pairs(room.objects_needed) do
local object = TheApp.objects[name]
if cfg_objects[object.thob] and cfg_objects[object.thob].AvailableForLevel == 1 and
object.research_category and not research.research_progress[object].discovered then
if object.research_category == "cure" then
cure = object
elseif object.research_category == "diagnosis" then
diagnosis = object
end
end
end
end
end
end
local policy = research.research_policy
if cure then
policy.cure.current = cure
else
policy.global = policy.global - policy.cure.frac
policy.cure.frac = 0
end
if diagnosis then
research.research_policy.diagnosis.current = diagnosis
else
policy.global = policy.global - policy.diagnosis.frac
policy.diagnosis.frac = 0
end
self.research = research
self.research_rooms = nil
-- Cost of rooms has also been changed
local rooms = self.world.map.level_config.rooms
if not rooms then
-- Add the new variables manually.
rooms = {
[7] = {Cost = 2280}, -- GP_OFFICE
[8] = {Cost = 2270}, -- PSYCHO
[9] = {Cost = 1700}, -- WARD
[10] = {Cost = 2250}, -- OP_THEATRE
[11] = {Cost = 500}, -- PHARMACY
[12] = {Cost = 470}, -- CARDIO
[13] = {Cost = 3970}, -- SCANNER
[14] = {Cost = 2000}, -- ULTRASCAN
[15] = {Cost = 3000}, -- BLOOD_MACHINE
[16] = {Cost = 2000}, -- XRAY
[17] = {Cost = 1500}, -- INFLATOR
[18] = {Cost = 7000}, -- ALIEN
[19] = {Cost = 500}, -- HAIR_RESTORER
[20] = {Cost = 1500}, -- SLACK_TONGUE
[21] = {Cost = 500}, -- FRACTURE
[22] = {Cost = 1850}, -- TRAINING
[23] = {Cost = 500}, -- ELECTRO
[24] = {Cost = 4500}, -- JELLY_VAT
[25] = {Cost = 1350}, -- STAFF ROOM
[26] = {Cost = 5}, -- TV ??
[27] = {Cost = 720}, -- GENERAL_DIAG
[28] = {Cost = 800}, -- RESEARCH
[29] = {Cost = 1170}, -- TOILETS
[30] = {Cost = 5500}, -- DECON_SHOWER
}
self.world.map.level_config.rooms = rooms
end
for _, room in ipairs(TheApp.rooms) do
-- Sum up the build cost of the room
local build_cost = rooms[room.level_config_id].Cost
for name, no in pairs(room.objects_needed) do
-- Add cost for this object.
build_cost = build_cost + cfg_objects[TheApp.objects[name].thob].StartCost * no
end
-- Now define the total build cost for the room.
room.build_cost = build_cost
end
end
if old < 34 then
-- New variable
self.acc_overdraft = 0
end
if old < 35 then
-- Define build costs for rooms once again.
local cfg_objects = self.world.map.level_config.objects
local cfg_rooms = self.world.map.level_config.rooms
for _, room in ipairs(TheApp.rooms) do
-- Sum up the build cost of the room
local build_cost = cfg_rooms[room.level_config_id].Cost
for name, no in pairs(room.objects_needed) do
-- Add cost for this object.
build_cost = build_cost + cfg_objects[TheApp.objects[name].thob].StartCost * no
end
-- Now define the total build cost for the room.
self.research.research_progress[room] = {
build_cost = build_cost,
}
end
end
if old < 39 then
self.acc_heating = 0
end
if old < 40 then
self.statistics = {}
self.money_in = 0
self.money_out = 0
end
if old < 41 then
self.boiler_can_break = true
end
if old < 45 then
self.num_explosions = 0
self.num_vips = 0
end
if old < 50 then
self.num_vips_ty = 0
self.pleased_vips_ty = 0
self.num_cured_ty = 0
self.not_cured_ty = 0
self.num_visitors_ty = 0
end
if old < 52 then
self:initOwnedPlots()
self.handymanTasks = {}
end
if old < 54 then
local current = self.research.research_policy.specialisation.current
if current and not current.dummy and not current.thob and not current.drug then
for _, disease_entry in pairs(self.disease_casebook) do
if disease_entry.concentrate_research then
self.research:concentrateResearch(disease_entry.disease.id)
self.research:concentrateResearch(disease_entry.disease.id)
end
end
end
end
if old < 56 then
self.research_dep_built = false
end
if old < 84 then
self.vip_declined = 0
end
if old < 88 then
self.future_epidemics_pool = {}
self.concurrent_epidemic_limit = self.world.map.level_config.gbv.EpidemicConcurrentLimit or 1
end
if old < 107 then
self.reception_desks = nil
end
if old < 109 then
-- price distortion
self.under_priced_threshold = -0.4
self.over_priced_threshold = 0.3
end
if old < 111 then
self.initial_grace = nil
end
if old < 114 then
self.ratholes = {}
end
if old < 131 then
self.autopsy_discovered = nil
self.discover_autopsy_risk = nil
end
if old < 140 then
self.has_impressive_reputation = self.reputation_above_threshold and true or false
self.reputation_above_threshold = nil
self:unconditionalChangeReputation(0) -- Setup 'has_impressive_reputation'
end
if old < 142 then
self.disasterless_days = self:daysUntilNextDisaster()
self.heating = {
radiator_heat = self.radiator_heat or 0.5,
saved_radiator_heat = self.curr_setting or 0.5,
boiler_repair_count = self.boiler_countdown or 0,
heating_broke = self.heating_broke or false
}
self.radiator_heat = nil
self.curr_setting = nil
self.boiler_countdown = nil
self.boiler_can_break = nil -- Equivalent to self.opened.
self.heating_broke = nil
end
if old < 143 and new >= 143 then
if self:isPlayerHospital() then
setmetatable(self, PlayerHospital._metatable)
end
-- To avoid recursion, apply the remaining changes asif the game was
-- started from version 143.
self:afterLoad(143, new)
return
end
if old < 146 then
self.staff_room_msg = nil
self.toilet_msg = nil
self.bench_msg = nil
self.warmth_msg = nil
self.thirst_msg = nil
self.seating_warning = nil
self.cash_msg = nil
end
if old < 147 then
self.patientcount = nil
self.receptionist_msg = nil
end
if old < 148 then
self.msg_counter = nil
end
if old < 152 then
-- If old save has an emergency fax, or emergency active, of an undiscovered disease
-- make the disease discovered to prevent crashing (see #1754, #1799)
local em = self.emergency
if em and not self.disease_casebook[em.disease.id].discovered then
self.research:discoverDisease(em.disease)
end
end
if old < 154 then
-- We now use one table for our room discovery
self.room_discoveries = {}
-- Get the level start available rooms
local avail_rooms = self.world:getAvailableRooms()
for _, avail_room in ipairs(avail_rooms) do
self.room_discoveries[avail_room.room.id] = {
room = avail_room.room,
is_discovered = avail_room.is_discovered or false
}
end
-- Has the player discovered rooms since?
for _, room_new in pairs(self.room_discoveries) do
if self.discovered_rooms[room_new.room] then -- old system used rooms as keys
room_new.is_discovered = true
end
end
-- Clear old variables
self.discovered_rooms = nil
self.undiscovered_rooms = nil
end
if old < 155 then
self.overdraft_interest_rate = self.interest_rate + 0.02
end
if old < 159 then
self.receptionist_msg = nil
end
if old < 167 then
-- Count tile objects of this hospital from scratch.
self.tile_object_counts = {
extinguisher = 0,
radiator = 0,
plant = 0,
reception_desk = 0,
bench = 0,
general = 0,
}
local width, height = self.world.map.th:size()
for x = 1, width do
for y = 1, height do
local objects = self.world:getObjects(x, y)
if objects then
for _, object in ipairs(objects) do
if object.hospital == self then
self:addTileObject(object.object_type.count_category)
end
end
end
end
end
end
if old < 169 then
-- AIHospital previously did not have a cheats instance, now it should
if not self.hosp_cheats then self.hosp_cheats = Cheats(self) end
-- Move toggle cheat triggers to Cheats
self.hosp_cheats.active_cheats = {}
self.hosp_cheats.active_cheats["spawn_rate_cheat"] = self.spawn_rate_cheat
self.spawn_rate_cheat = nil
end
-- Update other objects in the hospital (added in version 106).
if self.epidemic then self.epidemic.afterLoad(old, new) end
for _, future_epidemic in ipairs(self.future_epidemics_pool) do
future_epidemic.afterLoad(old, new)
end
self.research.afterLoad(old, new)
end
--! Count the number of patients in the hospital.
--!param max_count (optional integer) If provided, non-negative maximum count to return.
--!return The number of patients in the hospital, at most max_count is returned if provided.
function Hospital:countPatients(max_count)
local count = 0
for _, patient in ipairs(self.patients) do
-- Only count patients that are in the hospital.
local tx, ty = patient.tile_x, patient.tile_y
if tx and ty and self:isInHospital(tx, ty) then
count = count + 1
if max_count ~= nil and count >= max_count then break end
end
end
return count
end
--! Count number of sitting and standing patients in the hospital.
--!return (integer, integer) Number of sitting and number of standing patient in the hospital.
function Hospital:countSittingStanding()
local numberSitting = 0
local numberStanding = 0
for _, patient in ipairs(self.patients) do
local pat_action = patient:getCurrentAction()
if pat_action.name == "idle" and not patient:getRoom() then
numberStanding = numberStanding + 1
elseif pat_action.name == "use_object" and pat_action.object.object_type.id == "bench" then
numberSitting = numberSitting + 1
end
end
return numberSitting, numberStanding
end
--! Called each tick, also called 'hours'. Check hours_per_day in
--! date.lua to see how many times per day this is.
function Hospital:tick()
-- Add some random background sounds, ringing phones, coughing, belching etc.
--
-- TODO: Background noises of other hospitals are heard in multi-player,
-- TODO: decide where this should go.
if math.random(1, 100) == 3 then
-- Wait until there are some patients in the hospital and a room, otherwise you
-- will wonder who is coughing or who is the receptionist telephoning!
-- Opted for gp as you can't run the hospital without one.
if self:countRoomOfType("gp", 1) > 0 and self:countPatients(3) > 2 then
local sounds = {
"ispot001.wav", "ispot002.wav", "ispot003.wav", "ispot004.wav",
"ispot005.wav", "ispot006.wav", "ispot007.wav", "ispot008.wav",
"ispot009.wav", "ispot010.wav", "ispot011.wav", "ispot012.wav",
"ispot013.wav", "ispot014.wav", "ispot015.wav", "ispot016.wav",
"ispot017.wav", "ispot018.wav", "ispot019.wav", "ispot020.wav",
"ispot021.wav", "ispot022.wav", "ispot023.wav", "ispot024.wav",
"ispot025.wav"
} -- ispot026 and ispot027 are both toilet related sounds.
local sound_to_play = sounds[math.random(1, #sounds)]
if TheApp.audio:soundExists(sound_to_play) then
self.world.ui:playSound(sound_to_play)
end
end
end
self:manageEpidemics()
end
function Hospital:purchasePlot(plot_number)
local map = self.world.map
if map.th:isParcelPurchasable(plot_number, self:getPlayerIndex()) then
local cost = not self.world.free_build_mode and map:getParcelPrice(plot_number) or 0
if cost <= self.balance then
self.world:setPlotOwner(plot_number, self:getPlayerIndex())
table.insert(self.ownedPlots, plot_number)
-- Also make sure to apply transparency to the new walls, if required.
self.world.ui:applyTransparency()
self:spendMoney(cost, _S.transactions.buy_land, cost)
return true
else
-- Give visual warning that player doesn't have enough $ to build
self:adviseCannotAffordPlot()
end
end
return false
end
function Hospital:getPlayerIndex()
-- TODO: In multiplayer, return 2 or 3 or 4
return 1
end
--! Returns the heliport x and y coordinates or nil if none exist.
--!return (pair of integers, or nil) The x,y position of the tile with the heliport, if it exists.
function Hospital:getHeliportPosition()
local x, y = self.world.map.th:getHeliportTile(self:getPlayerIndex())
-- NB: Level 2 has a heliport tile set, but no heliport, so we ensure that
-- the specified tile is suitable by checking the spawn tile for
-- passability.
if y > 0 and self.world.map:getCellFlag(x, y, "passable") then
return x, y
end
end
--! Returns the tile on which patients should spawn when getting out of the helicopter.
--!return (pair of integers, or nil) The x,y position to use for spawning emergency patients from the heliport, if available.
function Hospital:getHeliportSpawnPosition()
local x, y = self:getHeliportPosition()
if x and y then
return x, y - 1
end
end
--[[ Test if a given map tile is part of this hospital.
!param x (integer) The 1-based X coordinate of the tile to test.
!param y (integer) The 1-based Y coordinate of the tile to test.
]]
function Hospital:isInHospital(x, y)
local flags = self.world.map.th:getCellFlags(x, y)
return flags.hospital and flags.owner == self:getPlayerIndex()
end
--! Decide how many days the hospital functions within specification.
--!return (int) Number of disaster-free days in the hospital.
function Hospital:daysUntilNextDisaster()
local disaster_free_days = {300, 200, 150}
-- Original doesn't use random, see Github #490.
return disaster_free_days[self.world.map:getDifficulty()] + math.random(1, 21) - 11
end
--! Boiler should break down.
--!param broken_heat (0 or 1) Amount of heat to output due to being broken.
function Hospital:boilerBreakdown(broken_heat)
local heat_vars = self.heating
if not self.opened then return end -- Boiler cannot break if hospital is closed.
if heat_vars.heating_broke then return end -- Still broken, don't break it again.
local num_radiators = self:countRadiators()
if num_radiators == 0 then return end -- No radiators, don't bother to break the boiler.
local num_handyman = self:countStaffOfCategory("Handyman")
if num_radiators <= 8 * num_handyman then return end -- Enough handyman to maintain the heating system.
heat_vars.saved_radiator_heat = heat_vars.radiator_heat
heat_vars.radiator_heat = broken_heat
heat_vars.boiler_repair_count = math.random(10, 30)
heat_vars.heating_broke = true
-- Warn the player of the boiler's breakdown
self:adviseBoilerBreakdown(broken_heat)
end
--! Boiler broke down and work is done to get it fixed.
function Hospital:_fixBoiler()
local heat_vars = self.heating
if not heat_vars.heating_broke then return end -- Not broken, done!
-- Repair the boiler or radiators, more handy men speeds up repair, see also github #490
local num_radiators = self:countRadiators()
local num_handyman = self:countStaffOfCategory("Handyman")
if num_radiators < 5 * num_handyman then
heat_vars.boiler_repair_count = heat_vars.boiler_repair_count - 3
elseif num_radiators < 8 * num_handyman then
heat_vars.boiler_repair_count = heat_vars.boiler_repair_count - 2
else
heat_vars.boiler_repair_count = heat_vars.boiler_repair_count - 1
end
if heat_vars.boiler_repair_count <= 0 then
-- It's fixed, restore previous settings.
heat_vars.radiator_heat = heat_vars.saved_radiator_heat
heat_vars.heating_broke = false
if num_radiators > 0 then
-- Only tell the player about fix if there is at least one radiator.
self:giveAdvice({_A.boiler_issue.resolved})
end
end
end
--! Daily update of the ratholes.
--!param self (Hospital) hospital being updated.
local function dailyUpdateRatholes(self)
local map = self.world.map
local th = map.th
local wanted_holes = math.round(th:getLitterFraction(self:getPlayerIndex()) * 200)
if #self.ratholes < wanted_holes then -- Not enough holes, find a new spot
-- Try to find a wall in a corridor, and add it if possible.
-- Each iteration does a few probes at a random position, most tries will
-- fail on not being on a free (non-built) tile in a corridor with a wall
-- in the right hospital.
-- Doing more iterations speeds up finding a suitable location, less
-- iterations is reduces needed processor time.
-- "6 + 2 * difference" is an arbitrary value that seems to work nicely, 12
-- is an arbitrary upper limit on the number of tries.
for _ = 1, math.min(12, 6 + 2 * (wanted_holes - #self.ratholes)) do
local x = math.random(1, map.width)
local y = math.random(1, map.height)
local flags = th:getCellFlags(x, y)
if self:isInHospital(x, y) and flags.roomId == 0 and flags.buildable then
local walls = self:getWallsAround(x, y)
if #walls > 0 then
-- Found a wall, check it for not being used.
local wall = walls[math.random(1, #walls)]
local found = false
for _, hole in ipairs(self.ratholes) do
if hole.x == x and hole.y == y and hole.wall == wall.wall then
found = true
break
end
end
if not found then
self:addRathole(x, y, wall.wall, wall.parcel)
break
end
end
end
end
end
end
-- Called at the end of each day.
function Hospital:onEndDay()
local pay_this = self.loan*self.interest_rate/365 -- No leap years
self.acc_loan_interest = self.acc_loan_interest + pay_this
self.research:researchCost()
self.show_progress_screen_warnings = math.random(1, 3) -- used in progress report to limit warnings
if self.balance < 0 then
local overdraft_interest = self.overdraft_interest_rate
local overdraft = math.abs(self.balance)
local overdraft_payment = (overdraft*overdraft_interest)/365
self.acc_overdraft = self.acc_overdraft + overdraft_payment
end
-- Count receptionists.
self.receptionist_count = 0
for _, staff in ipairs(self.staff) do
if staff.humanoid_class == "Receptionist" then
self.receptionist_count = self.receptionist_count + 1
end
end
self:_fixBoiler() -- Boiler always needs work (especially if broken).
-- Do we have a disaster?
self.disasterless_days = self.disasterless_days - 1
if self.disasterless_days <= 0 then
self.disasterless_days = self:daysUntilNextDisaster()
local disaster_type = math.random(1, 3) -- TODO: Set to 3 until the vomit wave is implemented.
-- disaster_type == 1 is for skipping the disaster, nothing happens.
if disaster_type == 2 then
self:boilerBreakdown(1) -- max heat
elseif disaster_type == 3 then
self:boilerBreakdown(0) -- min heat
end
-- TODO: Implement vomit wave disaster for disaster_type == 4
end
-- Calculate heating cost daily. Divide the monthly cost by the number of days in that month
local num_radiators = self:countRadiators()
local heating_costs = (self.heating.radiator_heat * 10 * num_radiators * 7.50) / self.world:date():lastDayOfMonth()
self.acc_heating = self.acc_heating + heating_costs
if self:isPlayerHospital() then dailyUpdateRatholes(self) end
end
-- Called at the end of each month.
function Hospital:onEndMonth()
-- Spend wages
local wages = 0
for _, staff in ipairs(self.staff) do
wages = wages + staff.profile.wage
end
if wages ~= 0 then
self:spendMoney(wages, _S.transactions.wages)
end
-- Pay heating costs
if math.round(self.acc_heating) > 0 then
self:spendMoney(math.round(self.acc_heating), _S.transactions.heating)
self.acc_heating = 0
end
-- Pay interest on loans
if math.round(self.acc_loan_interest) > 0 then
self:spendMoney(math.round(self.acc_loan_interest), _S.transactions.loan_interest)
self.acc_loan_interest = 0
end
-- Pay overdraft charges
if math.round(self.acc_overdraft) > 0 then
self:spendMoney(math.round(self.acc_overdraft), _S.transactions.overdraft)
self.acc_overdraft = 0
end
-- Pay research costs
if math.round(self.acc_research_cost) > 0 then
self:spendMoney(math.round(self.acc_research_cost), _S.transactions.research)
self.acc_research_cost = 0
end
-- add to score each month
-- rate varies on some performance factors i.e. reputation above 500 increases the score
-- and the number of deaths will reduce the score.
local sal_inc = self.salary_incr / 10
local sal_mult = (self.reputation - 500) / (self.num_deaths + 1) -- added 1 so that you don't divide by 0
local month_incr = sal_inc + sal_mult
-- To ensure that you can't receive less than 50 or
-- more than 300 per month
if month_incr < self.sal_min then
month_incr = self.sal_min