1 #include "migration.h"
2 
3 #include "building/house_population.h"
4 #include "building/monument.h"
5 #include "city/data_private.h"
6 #include "city/figures.h"
7 #include "city/message.h"
8 #include "core/calc.h"
9 #include "game/tutorial.h"
10 
update_status(void)11 static void update_status(void)
12 {
13     if (city_data.sentiment.value > 70) {
14         city_data.migration.percentage = 100;
15     } else if (city_data.sentiment.value > 60) {
16         city_data.migration.percentage = 75;
17     } else if (city_data.sentiment.value >= 50) {
18         city_data.migration.percentage = 50;
19     } else if (city_data.sentiment.value > 40) {
20         city_data.migration.percentage = 0;
21     } else if (city_data.sentiment.value > 30) {
22         city_data.migration.percentage = -10;
23     } else if (city_data.sentiment.value > 20) {
24         city_data.migration.percentage = -25;
25     } else {
26         city_data.migration.percentage = -50;
27     }
28 
29     city_data.migration.immigration_amount_per_batch = 0;
30     city_data.migration.emigration_amount_per_batch = 0;
31 
32     int population_cap = tutorial_get_population_cap(200000);
33     if (city_data.population.population >= population_cap) {
34         city_data.migration.percentage = 0;
35         return;
36     }
37     // war scares immigrants away
38     if (city_figures_total_invading_enemies() > 3 &&
39         city_data.migration.percentage > 0) {
40         city_data.migration.percentage = 0;
41         return;
42     }
43     if (city_data.migration.percentage > 0) {
44         // immigration
45         if (city_data.migration.emigration_duration) {
46             city_data.migration.emigration_duration--;
47         } else {
48             int migration_size = 12;
49             city_data.migration.immigration_amount_per_batch =
50                 calc_adjust_with_percentage(migration_size, city_data.migration.percentage);
51             city_data.migration.immigration_duration = 2;
52         }
53     } else if (city_data.migration.percentage < 0) {
54         // emigration
55         if (city_data.migration.immigration_duration) {
56             city_data.migration.immigration_duration--;
57         } else if (city_data.population.population > 100) {
58             city_data.migration.emigration_amount_per_batch =
59                 calc_adjust_with_percentage(12, -city_data.migration.percentage);
60             city_data.migration.emigration_duration = 2;
61         }
62     }
63 }
64 
create_immigrants(int num_people)65 static void create_immigrants(int num_people)
66 {
67     int immigrated = house_population_create_immigrants(num_people);
68     city_data.migration.immigrated_today += immigrated;
69     city_data.migration.newcomers += city_data.migration.immigrated_today;
70     if (immigrated == 0) {
71         city_data.migration.refused_immigrants_today += num_people;
72     }
73 }
74 
create_emigrants(int num_people)75 static void create_emigrants(int num_people)
76 {
77     city_data.migration.emigrated_today += house_population_create_emigrants(num_people);
78 }
79 
create_migrants(void)80 static void create_migrants(void)
81 {
82     city_data.migration.immigrated_today = 0;
83     city_data.migration.emigrated_today = 0;
84     city_data.migration.refused_immigrants_today = 0;
85 
86     if (city_data.migration.immigration_amount_per_batch > 0) {
87         if (city_data.migration.immigration_amount_per_batch >= 4) {
88             create_immigrants(city_data.migration.immigration_amount_per_batch);
89         } else if (city_data.migration.immigration_amount_per_batch
90             + city_data.migration.immigration_queue_size >= 4) {
91             create_immigrants(city_data.migration.immigration_amount_per_batch
92                 + city_data.migration.immigration_queue_size);
93             city_data.migration.immigration_queue_size = 0;
94         } else {
95             // queue them for next round
96             city_data.migration.immigration_queue_size += city_data.migration.immigration_amount_per_batch;
97         }
98     }
99     if (city_data.migration.emigration_amount_per_batch > 0) {
100         if (city_data.migration.emigration_amount_per_batch >= 4) {
101             create_emigrants(city_data.migration.emigration_amount_per_batch);
102         } else if (city_data.migration.emigration_amount_per_batch + city_data.migration.emigration_queue_size >= 4) {
103             create_emigrants(city_data.migration.emigration_amount_per_batch
104                 + city_data.migration.emigration_queue_size);
105             city_data.migration.emigration_queue_size = 0;
106             if (!city_data.migration.emigration_message_shown) {
107                 city_data.migration.emigration_message_shown = 1;
108                 city_message_post(1, MESSAGE_EMIGRATION, 0, 0);
109             }
110         } else {
111             // queue them for next round
112             city_data.migration.emigration_queue_size += city_data.migration.emigration_amount_per_batch;
113         }
114     }
115     city_data.migration.immigration_amount_per_batch = 0;
116     city_data.migration.emigration_amount_per_batch = 0;
117 }
118 
city_migration_update(void)119 void city_migration_update(void)
120 {
121     update_status();
122     create_migrants();
123 }
124 
city_migration_determine_no_immigration_cause(void)125 void city_migration_determine_no_immigration_cause(void)
126 {
127     switch (city_data.sentiment.low_mood_cause) {
128         case LOW_MOOD_CAUSE_NO_FOOD:
129             city_data.migration.no_immigration_cause = NO_IMMIGRATION_NO_FOOD;
130             break;
131         case LOW_MOOD_CAUSE_NO_JOBS:
132             city_data.migration.no_immigration_cause = NO_IMMIGRATION_NO_JOBS;
133             break;
134         case LOW_MOOD_CAUSE_HIGH_TAXES:
135             city_data.migration.no_immigration_cause = NO_IMMIGRATION_HIGH_TAXES;
136             break;
137         case LOW_MOOD_CAUSE_LOW_WAGES:
138             city_data.migration.no_immigration_cause = NO_IMMIGRATION_LOW_WAGES;
139             break;
140         case LOW_MOOD_CAUSE_MANY_TENTS:
141             city_data.migration.no_immigration_cause = NO_IMMIGRATION_MANY_TENTS;
142             break;
143         case LOW_MOOD_CAUSE_SQUALOR:
144             city_data.migration.no_immigration_cause = NO_IMMIGRATION_SQUALOR;
145             break;
146         default:
147             city_data.migration.no_immigration_cause = NO_IMMIGRATION_LOW_MOOD;
148             break;
149     }
150 }
151 
city_migration_no_immigration_cause(void)152 int city_migration_no_immigration_cause(void)
153 {
154     return city_data.migration.no_immigration_cause;
155 }
156 
city_migration_no_room_for_immigrants(void)157 int city_migration_no_room_for_immigrants(void)
158 {
159     return city_data.migration.refused_immigrants_today || city_data.population.room_in_houses <= 0;
160 }
161 
city_migration_percentage(void)162 int city_migration_percentage(void)
163 {
164     return city_data.migration.percentage;
165 }
166 
city_migration_newcomers(void)167 int city_migration_newcomers(void)
168 {
169     return city_data.migration.newcomers;
170 }
171 
city_migration_reset_newcomers(void)172 void city_migration_reset_newcomers(void)
173 {
174     city_data.migration.newcomers = 0;
175 }
176