1 #include "random_event.h"
2 
3 #include "building/destruction.h"
4 #include "city/finance.h"
5 #include "city/health.h"
6 #include "city/labor.h"
7 #include "city/message.h"
8 #include "city/population.h"
9 #include "city/trade.h"
10 #include "core/config.h"
11 #include "core/random.h"
12 #include "scenario/data.h"
13 #include "scenario/property.h"
14 
15 enum {
16     EVENT_ROME_RAISES_WAGES = 1,
17     EVENT_ROME_LOWERS_WAGES = 2,
18     EVENT_LAND_TRADE_DISRUPTED = 3,
19     EVENT_LAND_SEA_DISRUPTED = 4,
20     EVENT_CONTAMINATED_WATER = 5,
21     EVENT_IRON_MINE_COLLAPSED = 6,
22     EVENT_CLAY_PIT_FLOODED = 7
23 };
24 
25 static const int RANDOM_EVENT_PROBABILITY[128] = {
26     0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 3, 0, 0, 0, 0,
27     0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 6,
28     0, 0, 2, 0, 0, 0, 7, 0, 5, 0, 0, 7, 0, 0, 0, 0,
29     0, 7, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
30     6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0,
31     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 0, 0,
32     0, 7, 0, 1, 6, 0, 0, 0, 0, 0, 2, 0, 0, 4, 0, 0,
33     0, 0, 3, 0, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0
34 };
35 
raise_wages(void)36 static void raise_wages(void)
37 {
38     if (scenario.random_events.raise_wages) {
39         if (city_labor_raise_wages_rome()) {
40             city_message_post(1, MESSAGE_ROME_RAISES_WAGES, 0, 0);
41         }
42     }
43 }
44 
lower_wages(void)45 static void lower_wages(void)
46 {
47     if (scenario.random_events.lower_wages) {
48         if (city_labor_lower_wages_rome()) {
49             city_message_post(1, MESSAGE_ROME_LOWERS_WAGES, 0, 0);
50         }
51     }
52 }
53 
disrupt_land_trade(void)54 static void disrupt_land_trade(void)
55 {
56     if (scenario.random_events.land_trade_problem) {
57         if (city_trade_has_land_trade_route()) {
58             city_trade_start_land_trade_problems(48);
59             if (scenario_property_climate() == CLIMATE_DESERT) {
60                 city_message_post(1, MESSAGE_LAND_TRADE_DISRUPTED_SANDSTORMS, 0, 0);
61             } else {
62                 city_message_post(1, MESSAGE_LAND_TRADE_DISRUPTED_LANDSLIDES, 0, 0);
63             }
64         }
65     }
66 }
67 
disrupt_sea_trade(void)68 static void disrupt_sea_trade(void)
69 {
70     if (scenario.random_events.sea_trade_problem) {
71         if (city_trade_has_sea_trade_route()) {
72             city_trade_start_sea_trade_problems(48);
73             city_message_post(1, MESSAGE_SEA_TRADE_DISRUPTED, 0, 0);
74         }
75     }
76 }
77 
contaminate_water(void)78 static void contaminate_water(void)
79 {
80     if (scenario.random_events.contaminated_water) {
81         if (city_population() > 200) {
82             int change;
83             int health_rate = city_health();
84             if (health_rate > 80) {
85                 change = -50;
86             } else if (health_rate > 60) {
87                 change = -40;
88             } else {
89                 change = -25;
90             }
91             city_health_change(change);
92             city_message_post(1, MESSAGE_CONTAMINATED_WATER, 0, 0);
93         }
94     }
95 }
96 
destroy_iron_mine(void)97 static void destroy_iron_mine(void)
98 {
99     if (scenario.random_events.iron_mine_collapse) {
100         if(config_get(CONFIG_GP_CH_RANDOM_COLLAPSES_TAKE_MONEY)) {
101             if(building_find(BUILDING_IRON_MINE)) {
102                 city_finance_process_sundry(250);
103                 city_message_post(1, MESSAGE_IRON_MINE_COLLAPED, 0, 0);
104             }
105     } else {
106             int grid_offset = building_destroy_first_of_type(BUILDING_IRON_MINE);
107             if (grid_offset) {
108                 city_message_post(1, MESSAGE_IRON_MINE_COLLAPED, 0, grid_offset);
109             }
110         }
111     }
112 }
113 
destroy_clay_pit(void)114 static void destroy_clay_pit(void)
115 {
116     if (scenario.random_events.clay_pit_flooded) {
117         if(config_get(CONFIG_GP_CH_RANDOM_COLLAPSES_TAKE_MONEY)) {
118             if(building_find(BUILDING_CLAY_PIT)) {
119                 city_finance_process_sundry(250);
120                 city_message_post(1, MESSAGE_CLAY_PIT_FLOODED, 0, 0);
121             }
122         } else {
123             int grid_offset = building_destroy_first_of_type(BUILDING_CLAY_PIT);
124             if (grid_offset) {
125                 city_message_post(1, MESSAGE_CLAY_PIT_FLOODED, 0, grid_offset);
126             }
127         }
128     }
129 }
130 
scenario_random_event_process(void)131 void scenario_random_event_process(void)
132 {
133     int event = RANDOM_EVENT_PROBABILITY[random_byte()];
134     switch (event) {
135         case EVENT_ROME_RAISES_WAGES:
136             raise_wages();
137             break;
138         case EVENT_ROME_LOWERS_WAGES:
139             lower_wages();
140             break;
141         case EVENT_LAND_TRADE_DISRUPTED:
142             disrupt_land_trade();
143             break;
144         case EVENT_LAND_SEA_DISRUPTED:
145             disrupt_sea_trade();
146             break;
147         case EVENT_CONTAMINATED_WATER:
148             contaminate_water();
149             break;
150         case EVENT_IRON_MINE_COLLAPSED:
151             destroy_iron_mine();
152             break;
153         case EVENT_CLAY_PIT_FLOODED:
154             destroy_clay_pit();
155             break;
156     }
157 }
158