-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.php
2097 lines (1714 loc) · 64.4 KB
/
objects.php
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
<?php
/***********************************************
Zurich Water Game: internet version
Author: Nigel Gilbert and the FIRMA project
--------------------------------------------
objects.php
This file contains the class definitions for
the objects of the game (players, resources
etc.)
Version 1.0 6 August 2001
Version 1.1 1 September 2001
Version 2.0 28 Occtober 2001
Version 2.1 7 April 2002
**********************************************/
/*============================================
An object to represent the environment
============================================*/
Class Zurich {
/*the parameters that describe the current state */
var $water_demand;
var $water_supply;
var $water_price;
var $water_quality;
var $political_popularity;
var $lake_quality;
var $env_awareness;
var $max_water_quality; /* records states during a turn */
var $max_lake_quality;
var $min_lake_quality;
/* some convenient variables for global use */
var $players; // array of the objects repesenting the players
var $clock; // the time of day, (as a Unix timestamp)
var $time; // the time of day (as a string)
var $action_count; // total number of actions any player has made
var $email_notify_time; // time when last email was sent to players
var $actsperyear; // number of actions between year ends (excludes softbot actions)
var $action_gap; // minimum period between actions for each player (in secs.)
var $turn; // count of years since the game began
function zurich() {
/* constructor function; populates the environment with players and
resources */
/* the player constructors give the players their initial resources */
$this->players['water_utility'] =
new Water_utility('water_utility', 'Water Utility');
$this->players['waste_water_utility'] =
new Waste_Water_utility('waste_water_utility', 'Waste Water Utility');
$this->players['housing_assoc_1'] =
new Housing_association('housing_assoc_1', 'Housing Association 1');
$this->players['housing_assoc_2'] =
new Housing_association('housing_assoc_2', 'Housing Association 2');
$this->players['manufacturer_1'] =
new Manufacturer('manufacturer_1', 'Manufacturer 1');
$this->players['manufacturer_2'] =
new Manufacturer('manufacturer_2', 'Manufacturer 2');
$this->players['politician'] =
new Politician('politician', 'Politician');
$this->players['bank'] = new Bank('bank', 'Bank');
/* initial environmental state */
$this->water_supply = 0; // set in measure_water_indicators()
$this->water_demand = 0; // set in measure_water_indicators()
$this->water_price = 5;
$this->water_quality = 8;
$this->political_popularity = 5;
$this->lake_quality = 6;
$this->env_awareness = 5;
$this->set_time();
$this->turn = 0;
$this->action_count = 1;
$this->max_water_quality = 10;
$this->min_water_quality = 0;
$this->min_lake_quality = 9999;
/* others */
$this->actsperyear = 5;
$this->action_gap = 1 * 60; // 1 minutes
}
function set_time($time = 0) {
/* sets the time of day to supplied time (secs from the beginning of Unix time)
or now */
if (!$time) $time = time();
$this->clock = $time;
$this->time = date('r', $this->clock);
}
function total_demand() {
/* returns the total demand for water by all the houses
owned by housing associations */
return $this->players['housing_assoc_1']->water_demand() +
$this->players['housing_assoc_2']->water_demand();
}
function total_supply() {
/* returns the total supply of water provided by all the
reservoirs owned by the water utility */
return $this->players['water_utility']->water_supply();
}
function lake_water_quality() {
/* return the lake water quality indicator */
return round(($this->total_demand() *
$this->players['waste_water_utility']->filter_level)/30);
}
function measure_water_indicators() {
/* sets the global environmental indicators */
$this->water_demand = $this->total_demand();
$this->water_supply = $this->total_supply();
$this->lake_quality = $this->lake_water_quality();
if ($this->water_quality > $this->max_water_quality)
$this->max_water_quality = $this->water_quality;
if ($this->water_quality < $this->min_water_quality)
$this->min_water_quality = $this->water_quality;
if ($this->lake_quality < $this->min_lake_quality)
$this->min_lake_quality = $this->lake_quality;
}
function change_water_quality($delta) {
/* increase the water quality index by $delta, but guard against
the index going out of range. Return actual change */
$new_amount = $delta + $this->water_quality;
If ($new_amount > 10) $new_amount = 10;
if ($new_amount < 0) $new_amount = 0;
$delta = $new_amount - $this->water_quality;
$this->water_quality = $new_amount;
return $delta;
}
function end_of_turn() {
/* for each player:
1 apply strategy rules;
3 pay maintenance on resources to bank; note order reversed! 14/9/02 to avoid getting into debt unnecessarily
2 collect income;
4 add 1 to the age of each resource.
Note the tricky semantics of PHP here. foreach works on a *copy*
of the array in something like foreach ($this_players as $p). Hence
if you use this, and try to advance the age of the resources in $p,
you'll actually be changing an anonymous *copy* of the resources!
Oh for lisp which has none of this bother! */
log_act("**It is the end of the year and the bills are due:", "", 10);
$this->measure_water_indicators();
apply_system_rules();
foreach (array_keys($this->players) as $k) {
if ($k) $this->players[$k]->income();
}
foreach (array_keys($this->players) as $k) {
if ($k) $this->players[$k]->maintenance();
}
foreach (array_keys($this->players) as $k) {
if ($k) $this->players[$k]->age_resources(1);
}
$this->turn++;
}
}
/*============================================
Resource class definitions
============================================*/
Class Resource {
var $maintenance_pt; // maintenance charge per turn
var $income_pt; // fixed income per turn
var $age; // age of this resource in years
var $max_age; // max age of the resource before it stops
// functioning
function make_older($change) {
/* age the resource by $change */
$this->age += $change;
}
}
/*-------------------------------------------
Factory class definitions
-------------------------------------------*/
Class Factory extends Resource {
/* either a normal or a water saving factory. To change from one to
the other, use change_type(). Initially, a normal factory */
var $building_cost;
var $change_of_use_cost;
var $repair;
var $type;
function factory() {
/* constructor: set initial values of parameters for a normal factory */
$this->building_cost = 800;
$this->change_of_use_cost = 600;
$this->repair_cost = 500;
$this->maintenance = 75;
$this->max_age = 8;
$this->type = 'normal';
$this->age = mt_rand(0,$this->max_age - 1); // start with an age between 0 and max_age
}
function change_type() {
/* change the type of this factory from Normal to one that makes Water saving
technology, or vice versa. Returns new type */
if ($this->type == 'normal') { // change to water saving technology
$this->building_cost = 1200;
$this->change_of_use_cost = 600;
$this->repair_cost = 500;
$this->maintenance = 75;
$this->type = 'water saving';
}
else { // change to normal technology
$this->building_cost = 800;
$this->change_of_use_cost = 600;
$this->repair_cost = 600;
$this->maintenance = 75;
$this->type = 'normal';
}
$this->age = 0; // reset age to zero
return $this->type;
}
}
/*-------------------------------------------
House class definitions
-------------------------------------------*/
Class House extends Resource {
var $type;
function house() {
/* constructor */
$this->maintenance_pt = 25;
$this->income_pt = 175;
$this->max_age = 8;
$this->age = mt_rand(0, $this->max_age - 1);
$this->type = 'normal';
}
function water_demand() {
/* return the amount of water used by this house */
global $zurich;
switch(floor($zurich->env_awareness / 4)) {
case 0: // awareness 0 - 3
return ($this->type == 'normal' ? 8 : 5);
case 1: // awareness 4 - 7
return ($this->type == 'normal' ? 6 : 4);
case 2: // awareness 8 - 10
return ($this->type == 'normal' ? 4 : 2);
default:
echo "Env. awareness out of range:$zurich->env_awareness!";
}
}
function fit_sanitary_system($type) {
/* change the type of the house to $type and reset age */
$this->type = $type;
$this->age = 0;
}
}
/*-------------------------------------------
Reservoir class definitions
-------------------------------------------*/
Class Reservoir extends Resource {
var $building_cost;
var $repair_cost;
function reservoir() {
/* constructor */
$this->building_cost = 1500;
$this->repair_cost = 500;
$this->maintenance_pt = 30;
$this->max_age = 10;
$this->age = mt_rand(0, $this->max_age - 1);
}
}
/*============================================
Player class definitions
============================================*/
Class Player {
var $id; // short identifier for this player
var $name; // the literal name of this player as a string
var $account; // current cash balance of this player
var $loggedin; // a human is playing this role
var $email; // email address of this role's player
var $realname; // name of this role's player
var $last_time; // the time when the last action was carried out by the player
var $last_refresh; // the time when the player's page was last downloaded from the server
var $paid_off_debts; // player has paid off borrowings during the turn
var $rating_weight; // array of this player's chosen weights for the 5 rating criteria
function id_to_name($id) {
/* given a player id, returns the literal name */
global $zurich;
return $zurich->players[$id]->name;
}
function &id_to_obj($id) {
/* returns a reference to the object with identifier id */
global $zurich;
return $zurich->players[$id];
}
function rating() {
/* Return my current rating score. Answer should be in range 0 to 10.
First normalise weights so that their sum is one. Then work out each component score,
and multiply by the corresponding normalised weight */
global $zurich;
$weight_sum = 0 ;
for ($r = 0; $r < 5; $r++) {
$weight_sum += $this->rating_weight[$r];
}
$rating = 0;
for ($r = 0; $r < 5; $r++) {
/* get this player's score for each component of the rating */
switch ($r) {
case 0: /* supply > demand */
$score = ($zurich->water_supply >= $zurich->water_demand ? 10 : 0); break;
case 1: /* water price is low */
switch (true) {
case ($zurich->water_price <= 1): $score = 10; break;
case ($zurich->water_price <= 3): $score = 5; break;
case ($zurich->water_price <= 5): $score = 1; break;
default: $score = 0;
}
break;
case 2: /* political popularity is high */
switch (true) {
case ($zurich->political_popularity >= 9): $score = 10; break;
case ($zurich->political_popularity >= 7): $score = 5; break;
case ($zurich->political_popularity >= 5): $score = 1; break;
default: $score = 0;
}
break;
case 3: /* lake water quality is high */
switch (true) {
case ($zurich->lake_quality >= 9): $score = 10; break;
case ($zurich->lake_quality >= 7): $score = 5; break;
case ($zurich->lake_quality >= 5): $score = 1; break;
default: $score = 0;
}
break;
case 4: /* profit is high */
$score = $this->account / 200;
if ($score > 10) $score = 10;
break;
default: alert("Unknown rating in Player->rating");
}
$rating += $this->rating_weight[$r] * $score;
# echo "component = $r rating_weight = " . $this->rating_weight[$r] . " weight_sum= $weight_sum" .
# " score= $score rating= $rating answer = " . $rating/$weight_sum . "<P>";
}
return round($rating/$weight_sum);
}
function benefit($whom, $amount) {
/* receive some money from $whom (an id) */
$this->account += $amount;
log_and_confirm("received " . CURRSYM . "$amount from " .
$this->id_to_name($whom), $this->id, 0);
return $amount;
}
function pay($whom, $amount) {
/* pay someone (id given in $whom) the $amount. First check that they
have enough money. If not, if they are logged in, return 0, otherwise
borrow from the bank; if they have, return the amount paid. */
if ($this->account - $amount < 0) {
if ($this->loggedin) return 0;
$this->overdraw($amount - $this->account);
}
$this->account -= $amount;
log_and_confirm("paid " . CURRSYM . "$amount to " .
$this->id_to_name($whom), $this->id, 0);
$whom_obj =& $this->id_to_obj($whom);
return $whom_obj->benefit($this->id, $amount);
}
function cant_pay($amount) {
/* player has attempted to pay out more than is available in their account;
complain */
global $player;
if ($this->id == $player) {
/* this will normally be the case, but when the bank forces
an end of year, $playobj is the bank, and $this is the
player paying maintenance etc. */
echo "You have tried to pay out more than is in your
current account. The bank will not allow unplanned overdrafts.
You will have to arrange to borrow from the bank.
Action cancelled.";
}
}
function must_pay($whom, $amount) {
/* pay someone (id given in $whom) the $amount. If there is not that
much in the account, first borrow sufficient from the bank. */
if ($this->account - $amount < 0) {
/* must borrow; borrow amount rounded up to nearest 100 */
$loan = $this->overdraw($amount - $this->account);
/* leave a warning for the player to collect next go */
$this->overdraft_note($loan);
}
$this->account -= $amount;
log_and_confirm("paid " . CURRSYM . "$amount to " . $this->id_to_name($whom), $this->id, 0);
$whom_obj =& $this->id_to_obj($whom);
return $whom_obj->benefit($this->id, $amount);
}
function overdraft_note($loan) {
/* note the overdraft for when the user logs in. If there is already an overdraft,
just update the existing record */
$query = new Query("SELECT param1 FROM requests
WHERE requestor='$this->id' and request='overdraft'");
if ($query->next_rec()) {
$new_loan = $loan + $query->field('param1');
db_write("UPDATE requests SET param1= '$new_loan'
WHERE requestor='$this->id' and request='overdraft'");
}
else {
$this->request($this->id, 'overdraft', $loan);
}
}
function overdraw($amount) {
/* silently borrow an amount from the bank. Return amount
borrowed (rounded up to nearest 100 from $amount) */
$loan = 100 * ceil($amount/100);
$this->account += $loan;
$bank_obj =& $this->id_to_obj('bank');
$bank_obj->lend($this->id, $loan);
log_and_confirm("borrowed " . CURRSYM . "$loan from the bank", $this->id, 2);
return $loan;
}
function overdraft($id, $requestor, $amount) {
/* warn user that an overdraft has had to be arranged */
echo "You did not have enough in your account to pay the
bills that were due for maintenance etc. The Bank has therefore
automatically provided you with overdraft facilities and lent you
the " . CURRSYM . "$amount that you needed.";
$this->delete_request($id);
}
function play() {
/* method to be over-ridden by each player role!
Runs the player strategy while the real player is not
logged in */
log_and_confirm("Running softbot", $this->id, 1);
/* by default, pick a chance card */
$this->pick_chance_card();
}
/* generic actions */
function advertise($state = 0) {
/* advertise to (hopefully) improve the given environmental state */
global $zurich;
if (!$state) $state = get_param('state');
$cost = 500;
if (!$this->pay('bank', $cost)) {
$this->cant_pay($cost);
return;
};
$delta = dice_up($zurich->$state, -1, 3);
$zurich->$state += $delta;
$state_name = state_to_name($state);
if ($delta) {
log_and_confirm("spent " . CURRSYM . "$cost on advertising resulting in " .
($delta >= 0 ? "increasing " : "decreasing ") .
"$state_name by " . abs($delta),
$this->id, 3);
}
else {
log_and_confirm("spent " . CURRSYM .
"$cost on advertising to raise $state_name, but to no effect!",
$this->id, 3);
}
}
function borrow_from_bank() {
/* increase my account by the amount borrowed */
$amount = get_param('amount');
if ($amount == 0) {
echo "You must say how much you want to borrow.";
return;
}
log_and_confirm("borrowed " . CURRSYM .
"$amount from the bank at an interest rate of " .
100 * $this->interest_rate() . "%", $this->id, 3);
$this->account += $amount;
$bank_obj =& $this->id_to_obj('bank');
$bank_obj->lend($this->id, $amount);
}
function interest_rate() {
/* return the current interest rate */
global $zurich;
return $zurich->players['bank']->interest_rate;
}
function pay_interest($interest) {
/* pay interest on borrowings to the bank */
$this->must_pay('bank', $interest);
$this->warn_player("paid " . CURRSYM . "$interest in interest on the overdraft to the bank", $this->id, 2);
}
function repay_bank() {
/* repay the bank for a loan (may be partial repayment)
No check for overpayment */
$amount = get_param('repayment');
if (!$this->pay('bank', $amount)) {
$this->cant_pay($amount);
return;
}
$bank_obj =& $this->id_to_obj('bank');
$bank_obj->repay($this->id, $amount);
log_and_confirm("repaid " . CURRSYM . "$amount of borrowings to the bank",
$this->id, 3);
/* if the debt has been paid off, note this for system rules */
if (!$this->debtor()) $this->paid_off_debts = TRUE;
}
function debtor() {
/* returns amount borrowed if this player owes money to the bank */
$bank_obj =& $this->id_to_obj('bank');
if (isset($bank_obj->debtors[$this->id]))
$debt = $bank_obj->debtors[$this->id];
else $debt = 0;
return ($debt > 0 ? $debt : 0);
}
function pick_chance_card() {
/* pick and carry out a chance card (actually an event,
since strategy cards are not implemented), with a probability of 1 in 5 */
if (mt_rand(0, 100) < 20) event();
}
function warn_player($text, $who = "", $loglevel = "") {
/* if player is logged in, display text on the screen,
or if not logged in, leave the text in the database
for display when the player does log in.
If loglevel is given, also copy text to the log */
global $player;
if ($loglevel) log_act($text, $who, $loglevel);
if ($who != 'Event') $text = "You " . $text;
if ($this->id == $player and $this->loggedin) echo "$text<BR>";
else $this->request($this->id, 'warning', $text);
}
function warning($id, $requestor, $text) {
/* get here if a warning has been stored away for when the player
logs in. Display the warning */
echo "$text<BR>";
$this->delete_request($id);
}
function request($who, $what, $arg1, $arg2="") {
/* deposit a request in the queue, for another player or myself
(if $who == $player) when I log in, to pick up */
global $zurich;
/* to prevent duplicate requests, delete any existing request that has the same parameters as this one */
db_write("DELETE FROM requests
WHERE requestor='$this->id'
AND requestee='$who' AND request='$what' AND param1='$arg1'");
db_write("INSERT INTO requests (time, requestor, requestee, request, param1, param2)
VALUES('$zurich->time', '$this->id', '$who', '$what', '$arg1', '$arg2')");
}
function delete_request($id) {
/* delete a request from the queue because it has been completed */
db_write("DELETE FROM requests WHERE id='$id'");
}
}
/*-------------------------------------------
Water utility class definitions
-------------------------------------------*/
Class Water_utility extends Player {
var $reservoirs; // list of open reservoirs I own;
function water_utility($id, $name) {
/* constructor. Give the water utility 5 reservoirs to start */
$this->id = $id;
$this->name = $name;
for ($r = 0; $r < 5; $r++) {
$this->reservoirs[chr(ord('a') + $r)] = new Reservoir();
}
$this->account = 1000;
/* default rating weights */
$this->rating_weight = array(3, 3, 2, 1, 3);
}
function water_supply() {
/* returns the water supplied by my reservoirs
(amount of water per reservoir is 20 units) */
$supply = 0;
foreach ($this->reservoirs as $r) {
if ($r->age < $r->max_age) $supply += 20;
}
return $supply;
}
function age_resources($change) {
/* make the reservoirs $change years older */
foreach ($this->reservoirs as $k=>$r) {
$this->reservoirs[$k]->make_older($change);
if ($r->age == $r->max_age - 2) {
$this->warn_player("Reservoir $k is near to the end of its life", 'Event', 3);
}
if ($r->age == $r->max_age) {
$this->warn_player("Reservoir $k was closed because it was at the end of its life",
'Event', 3);
unset($this->reservoirs[$k]);
}
}
}
function maintenance() {
/* pay maintenance on reservoirs to the bank */
$fee = 0;
foreach ($this->reservoirs as $r) {
$fee += $r->maintenance_pt;
}
if ($fee) {
$this->must_pay('bank', $fee);
$this->warn_player("paid maintenance fee of " . CURRSYM . "$fee on reservoirs", $this->id, 2);
}
}
function income() {
/* nothing to do */
}
function oldest() {
/* return the index of the oldest reservoir or -1 if there are none*/
$age_of_oldest = -1;
while (list ($key, $r) = each($this->reservoirs)) {
if ($r->age > $age_of_oldest) {
$age_of_oldest = $r->age;
$oldest = $key;
}
}
return $oldest;
}
function decrease_capacity () {
/* close the specified or oldest reservoir,
and update water supply scale*/
global $zurich;
$key = get_param('close_res_number');
if(!$key) $key = $this->oldest();
if ($key) {
log_and_confirm("reduced capacity by closing a reservoir", $this->id, 3);
unset($this->reservoirs[$key]);
}
else {
log_act("No reservoir to close!", $this->id, 3);
}
}
function increase_capacity () {
/* add an extra reservoir to the list of reservoirs */
global $zurich;
$new_reservoir = new Reservoir();
$new_reservoir->age = 0;
$cost = $new_reservoir->building_cost;
if (!$this->pay('bank', $cost)) {
$this->cant_pay($cost);
return;
}
$new_key = 'a';
while ($this->factories[$new_key]) {
$new_key = chr(ord($new_key) + 1);
}
$this->reservoirs[$new_key] = $new_reservoir;
log_and_confirm("increased capacity by building reservoir \"$new_key\"", $this->id, 3);
}
function repair_reservoir() {
/* repair the specified or oldest reservoir, and reset its age to 0 */
$key = get_param('repair_res_number');
if(!$key) $key = $this->oldest();
if ($key) {
$cost = $this->reservoirs[$key]->repair_cost;
if (!$this->pay('bank', $cost)) {
$this->cant_pay($cost);
return;
}
$this->reservoirs[$key]->age = 0;
log_and_confirm("repaired reservoir \"$key\"", $this->id, 3);
}
}
function improve_water_quality () {
/* improve the water quality by a random amount */
global $zurich;
$delta = mt_rand(1,3);
$cost = 1000;
if (!$this->pay('bank', $cost)) {
$this->cant_pay($cost);
return;
}
$real_delta = $zurich->change_water_quality($delta);
log_and_confirm("improved the water quality by +$real_delta at a cost of " .
CURRSYM . "$cost", $this->id, 3);
}
function petition_politician ($price=0) {
/* make a request to the politician for a change in water price */
if (!$price) $price = get_param('price');
$this->request('politician','request_price_change', $price);
log_and_confirm("asked the Politician to change the water price to " . CURRSYM . "$price", $this->id, 3);
}
function price_change_approved($id, $requestor, $amount, $ignore) {
/* tell player the price change was approved */
global $zurich;
if ($amount <= 0) $amount = 1;
if ($amount > 100) $amount = 10;
$change = $amount - $zurich->water_price;
$zurich->water_price = $amount;
echo "Your request for a water price change to " . CURRSYM . "$amount has been approved\n";
log_and_confirm("changed the price of water to " . CURRSYM . "$amount", $this->id, 3);
$this->delete_request($id);
}
function price_change_disapproved($id, $requestor, $amount, $ignore) {
/* tell player the price change was not approved */
echo "Your request for a water price change
to " . CURRSYM . "$amount has NOT been approved<P>";
$this->delete_request($id);
}
function play() {
/* carry out default strategy while human player is not around:
if water_supply >= 1.5 * water_demand then close a reservoir
else if oldest reservoir is max_age or older then repair a reservoir
else if water quality < 5 then improve water quality
else if water demand > 0.8 * water supply then advertise environment
else if borrowing money then ask for water price increase
else take a chance card */
global $zurich;
/* get any outstanding requests and deal with them */
$query = new Query("SELECT id, request, requestor, param1, param2 FROM requests
WHERE requestee='water_utility'
ORDER BY time DESC");
while ($query->next_rec()) {
$id = $query->field('id');
$request = $query->field('request');
$requestor = $query->field('requestor');
$amount = $query->field('param1');
if ($request == 'price_change_approved') {
/* price change */
$zurich->water_price = $amount;
log_and_confirm("changed the price of water to " . CURRSYM . "$amount.",
$this->id, 3);
}
/* do nothing with price change disapprovals */
$this->delete_request($id);
}
if ($zurich->water_supply >= 1.5 * $zurich->water_demand) {
$this->decrease_capacity();
return;
}
$oldest_id = $this->oldest();
if ($oldest_id and $this->reservoirs[$oldest_id]->age >=
$this->reservoirs[$oldest_id]->max_age) {
$this->repair_reservoir();
return;
}
if ($zurich->water_quality < 5) {
$this->improve_water_quality();
return;
}
if ($zurich->water_demand > 0.8 * $zurich->water_supply and
$zurich->env_awareness < 8) {
$this->advertise('env_awareness');
return;
}
if ($this->debtor() and $zurich->water_price < 10) {
$this->petition_politician($zurich->water_price + 1);
return;
}
$this->pick_chance_card();
}
}
/*-------------------------------------------
Waste Water Utility class definitions
-------------------------------------------*/
Class Waste_Water_utility extends Player {
var $filter_level; /* current level of the filter system
(1 = mechanical;
2 = plus nutrient
3 = plus biological) */
function waste_water_utility($id, $name) {
/* constructor. Start with mechanical only */
$this->id = $id;
$this->name = $name;
$this->filter_level = 1;
$this->account = 1000;
$this->rating_weight = array(2, 2, 2, 3, 3);
}
function age_resources($change) {
/* nothing to do */
}
function maintenance() {
/* pay the cost of filtration, depending on the type in use */
switch ($this->filter_level) {
case 1: $cost = 100; break;
case 2: $cost = 200; break;
case 3: $cost = 300; break;
}
$this->must_pay('bank', $cost);
$this->warn_player("paid the maintenance cost of filtration (" . CURRSYM . "$cost)", $this->id, 2);
}
function income() {
/* income comes from the housing associations, as a flat fee per house,
plus a fee per unit of water used */
/* nothing to do here - payments are made by HAs */
}
function flush_pipes() {
/* flush the pipes because they are filthy */
$pipe_flush_cost = 500;
$this->must_pay('bank', $pipe_flush_cost);
log_and_confirm("flushed waste pipes to clean them at a cost of " . CURRSYM . "$pipe_flush_cost",
$this->id, 2);
}
function change_filter_level($new_quality = 0) {
/* change the filtration quality */
if(!$new_quality) $new_quality = get_param('filter_quality');
$this->filter_level = $new_quality;
switch ($this->filter_level) {
case 1: $filter_type = "Mechanical"; break;
case 2: $filter_type = "Mechanical and Nutrient"; break;
case 3: $filter_type = "Mechanical, Nutrient and Biological"; break;
}
log_and_confirm("changed the filtration type to $filter_type", $this->id, 3);
}
function play() {
/* carry out default strategy while human player is not around:
Change filtration level according to level of current account
else take a chance card */
global $zurich;
if ($this->account > 1000) $filtration = 3;
elseif ($this->account > 700) $filtration = 2;
else $filtration = 1;
if ($filtration != $this->filter_level) {
$this->change_filter_level($filtration);
return;
}
$this->pick_chance_card();
}
}
/*-------------------------------------------
Housing Association class definitions