1 #include "culture.h"
2 
3 #include "building/building.h"
4 #include "building/count.h"
5 #include "city/constants.h"
6 #include "city/data_private.h"
7 #include "city/entertainment.h"
8 #include "city/festival.h"
9 #include "city/population.h"
10 #include "core/calc.h"
11 
12 static struct {
13     int theater;
14     int amphitheater;
15     int colosseum;
16     int hippodrome;
17     int hospital;
18     int school;
19     int academy;
20     int library;
21     int religion[5];
22     int oracle;
23 } coverage;
24 
city_culture_coverage_theater(void)25 int city_culture_coverage_theater(void)
26 {
27     return coverage.theater;
28 }
29 
city_culture_coverage_amphitheater(void)30 int city_culture_coverage_amphitheater(void)
31 {
32     return coverage.amphitheater;
33 }
34 
city_culture_coverage_colosseum(void)35 int city_culture_coverage_colosseum(void)
36 {
37     return coverage.colosseum;
38 }
39 
city_culture_coverage_hippodrome(void)40 int city_culture_coverage_hippodrome(void)
41 {
42     return coverage.hippodrome;
43 }
44 
city_culture_coverage_average_entertainment(void)45 int city_culture_coverage_average_entertainment(void)
46 {
47     return (coverage.hippodrome + coverage.colosseum + coverage.amphitheater + coverage.theater) / 4;
48 }
49 
city_culture_coverage_religion(god_type god)50 int city_culture_coverage_religion(god_type god)
51 {
52     return coverage.religion[god];
53 }
54 
city_culture_coverage_school(void)55 int city_culture_coverage_school(void)
56 {
57     return coverage.school;
58 }
59 
city_culture_coverage_library(void)60 int city_culture_coverage_library(void)
61 {
62     return coverage.library;
63 }
64 
city_culture_coverage_academy(void)65 int city_culture_coverage_academy(void)
66 {
67     return coverage.academy;
68 }
69 
city_culture_coverage_hospital(void)70 int city_culture_coverage_hospital(void)
71 {
72     return coverage.hospital;
73 }
74 
city_culture_average_education(void)75 int city_culture_average_education(void)
76 {
77     return city_data.culture.average_education;
78 }
79 
city_culture_average_entertainment(void)80 int city_culture_average_entertainment(void)
81 {
82     return city_data.culture.average_entertainment;
83 }
84 
city_culture_average_health(void)85 int city_culture_average_health(void)
86 {
87     return city_data.culture.average_health;
88 }
89 
top(int input)90 static int top(int input)
91 {
92     return input > 100 ? 100 : input;
93 }
94 
city_culture_update_coverage(void)95 void city_culture_update_coverage(void)
96 {
97     int population = city_data.population.population;
98 
99     // entertainment
100     coverage.theater = top(calc_percentage(500 * building_count_active(BUILDING_THEATER), population));
101     coverage.amphitheater = top(calc_percentage(800 * building_count_active(BUILDING_AMPHITHEATER), population));
102     coverage.colosseum = top(calc_percentage(1500 * building_count_active(BUILDING_COLOSSEUM), population));
103     if (building_count_active(BUILDING_HIPPODROME) <= 0) {
104         coverage.hippodrome = 0;
105     } else {
106         coverage.hippodrome = 100;
107     }
108 
109     // religion
110     int oracles = building_count_total(BUILDING_ORACLE);
111     coverage.religion[GOD_CERES] = top(calc_percentage(
112             500 * oracles +
113             750 * building_count_active(BUILDING_SMALL_TEMPLE_CERES) +
114             1500 * building_count_active(BUILDING_LARGE_TEMPLE_CERES),
115         population));
116     coverage.religion[GOD_NEPTUNE] = top(calc_percentage(
117             500 * oracles +
118             750 * building_count_active(BUILDING_SMALL_TEMPLE_NEPTUNE) +
119             1500 * building_count_active(BUILDING_LARGE_TEMPLE_NEPTUNE),
120         population));
121     coverage.religion[GOD_MERCURY] = top(calc_percentage(
122             500 * oracles +
123             750 * building_count_active(BUILDING_SMALL_TEMPLE_MERCURY) +
124             1500 * building_count_active(BUILDING_LARGE_TEMPLE_MERCURY),
125         population));
126     coverage.religion[GOD_MARS] = top(calc_percentage(
127             500 * oracles +
128             750 * building_count_active(BUILDING_SMALL_TEMPLE_MARS) +
129             1500 * building_count_active(BUILDING_LARGE_TEMPLE_MARS),
130         population));
131     coverage.religion[GOD_VENUS] = top(calc_percentage(
132             500 * oracles +
133             750 * building_count_active(BUILDING_SMALL_TEMPLE_VENUS) +
134             1500 * building_count_active(BUILDING_LARGE_TEMPLE_VENUS),
135         population));
136     coverage.oracle = top(calc_percentage(500 * oracles, population));
137 
138     city_data.culture.religion_coverage =
139         coverage.religion[GOD_CERES] +
140         coverage.religion[GOD_NEPTUNE] +
141         coverage.religion[GOD_MERCURY] +
142         coverage.religion[GOD_MARS] +
143         coverage.religion[GOD_VENUS];
144     city_data.culture.religion_coverage /= 5;
145 
146     // education
147     city_population_calculate_educational_age();
148 
149     coverage.school = top(calc_percentage(
150         75 * building_count_active(BUILDING_SCHOOL), city_population_school_age()));
151     coverage.library = top(calc_percentage(
152         800 * building_count_active(BUILDING_LIBRARY), population));
153     coverage.academy = top(calc_percentage(
154         100 * building_count_active(BUILDING_ACADEMY), city_population_academy_age()));
155 
156     // health
157     coverage.hospital = top(calc_percentage(
158         1000 * building_count_active(BUILDING_HOSPITAL), population));
159 }
160 
city_culture_calculate(void)161 void city_culture_calculate(void)
162 {
163     city_data.culture.average_entertainment = 0;
164     city_data.culture.average_religion = 0;
165     city_data.culture.average_education = 0;
166     city_data.culture.average_health = 0;
167 
168     int num_houses = 0;
169     for (int i = 1; i < MAX_BUILDINGS; i++) {
170         building *b = building_get(i);
171         if (b->state == BUILDING_STATE_IN_USE && b->house_size) {
172             num_houses++;
173             city_data.culture.average_entertainment += b->data.house.entertainment;
174             city_data.culture.average_religion += b->data.house.num_gods;
175             city_data.culture.average_education += b->data.house.education;
176             city_data.culture.average_health += b->data.house.health;
177         }
178     }
179     if (num_houses) {
180         city_data.culture.average_entertainment /= num_houses;
181         city_data.culture.average_religion /= num_houses;
182         city_data.culture.average_education /= num_houses;
183         city_data.culture.average_health /= num_houses;
184     }
185 
186     city_entertainment_calculate_shows();
187     city_festival_calculate_costs();
188 }
189 
city_culture_save_state(buffer * buf)190 void city_culture_save_state(buffer *buf)
191 {
192     // Yes, hospital is saved twice
193     buffer_write_i32(buf, coverage.theater);
194     buffer_write_i32(buf, coverage.amphitheater);
195     buffer_write_i32(buf, coverage.colosseum);
196     buffer_write_i32(buf, coverage.hospital);
197     buffer_write_i32(buf, coverage.hippodrome);
198     for (int i = GOD_CERES; i <= GOD_VENUS; i++) {
199         buffer_write_i32(buf, coverage.religion[i]);
200     }
201     buffer_write_i32(buf, coverage.oracle);
202     buffer_write_i32(buf, coverage.school);
203     buffer_write_i32(buf, coverage.library);
204     buffer_write_i32(buf, coverage.academy);
205     buffer_write_i32(buf, coverage.hospital);
206 }
207 
city_culture_load_state(buffer * buf)208 void city_culture_load_state(buffer *buf)
209 {
210     // Yes, hospital is saved twice
211     coverage.theater = buffer_read_i32(buf);
212     coverage.amphitheater = buffer_read_i32(buf);
213     coverage.colosseum = buffer_read_i32(buf);
214     coverage.hospital = buffer_read_i32(buf);
215     coverage.hippodrome = buffer_read_i32(buf);
216     for (int i = GOD_CERES; i <= GOD_VENUS; i++) {
217         coverage.religion[i] = buffer_read_i32(buf);
218     }
219     coverage.oracle = buffer_read_i32(buf);
220     coverage.school = buffer_read_i32(buf);
221     coverage.library = buffer_read_i32(buf);
222     coverage.academy = buffer_read_i32(buf);
223     coverage.hospital = buffer_read_i32(buf);
224 }
225