forked from bvschaik/julius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilding_tiles.c
316 lines (300 loc) · 10.4 KB
/
building_tiles.c
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
#include "building_tiles.h"
#include "building/building.h"
#include "building/industry.h"
#include "city/view.h"
#include "core/direction.h"
#include "core/image.h"
#include "map/aqueduct.h"
#include "map/bridge.h"
#include "map/building.h"
#include "map/figure.h"
#include "map/grid.h"
#include "map/image.h"
#include "map/property.h"
#include "map/random.h"
#include "map/sprite.h"
#include "map/terrain.h"
#include "map/tiles.h"
void map_building_tiles_add(int building_id, int x, int y, int size, int image_id, int terrain)
{
if (!map_grid_is_inside(x, y, size)) {
return;
}
int x_leftmost, y_leftmost;
switch (city_view_orientation()) {
case DIR_0_TOP:
x_leftmost = 0;
y_leftmost = size - 1;
break;
case DIR_2_RIGHT:
x_leftmost = y_leftmost = 0;
break;
case DIR_4_BOTTOM:
x_leftmost = size - 1;
y_leftmost = 0;
break;
case DIR_6_LEFT:
x_leftmost = y_leftmost = size - 1;
break;
default:
return;
}
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
map_terrain_add(grid_offset, terrain);
map_building_set(grid_offset, building_id);
map_property_clear_constructing(grid_offset);
map_property_set_multi_tile_size(grid_offset, size);
map_image_set(grid_offset, image_id);
map_property_set_multi_tile_xy(grid_offset, dx, dy,
dx == x_leftmost && dy == y_leftmost);
}
}
}
static void set_crop_tile(int building_id, int x, int y, int dx, int dy, int crop_image_id, int growth)
{
int grid_offset = map_grid_offset(x + dx, y + dy);
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
map_terrain_add(grid_offset, TERRAIN_BUILDING);
map_building_set(grid_offset, building_id);
map_property_clear_constructing(grid_offset);
map_property_set_multi_tile_xy(grid_offset, dx, dy, 1);
map_image_set(grid_offset, crop_image_id + (growth < 4 ? growth : 4));
}
void map_building_tiles_add_farm(int building_id, int x, int y, int crop_image_id, int progress)
{
if (!map_grid_is_inside(x, y, 3)) {
return;
}
// farmhouse
int x_leftmost, y_leftmost;
switch (city_view_orientation()) {
case DIR_0_TOP:
x_leftmost = 0;
y_leftmost = 1;
break;
case DIR_2_RIGHT:
x_leftmost = 0;
y_leftmost = 0;
break;
case DIR_4_BOTTOM:
x_leftmost = 1;
y_leftmost = 0;
break;
case DIR_6_LEFT:
x_leftmost = 1;
y_leftmost = 1;
break;
default:
return;
}
for (int dy = 0; dy < 2; dy++) {
for (int dx = 0; dx < 2; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
map_terrain_add(grid_offset, TERRAIN_BUILDING);
map_building_set(grid_offset, building_id);
map_property_clear_constructing(grid_offset);
map_property_set_multi_tile_size(grid_offset, 2);
map_image_set(grid_offset, image_group(GROUP_BUILDING_FARM_HOUSE));
map_property_set_multi_tile_xy(grid_offset, dx, dy,
dx == x_leftmost && dy == y_leftmost);
}
}
// crop tile 1
int growth = progress / 10;
set_crop_tile(building_id, x, y, 0, 2, crop_image_id, growth);
// crop tile 2
growth -= 4;
if (growth < 0) {
growth = 0;
}
set_crop_tile(building_id, x, y, 1, 2, crop_image_id, growth);
// crop tile 3
growth -= 4;
if (growth < 0) {
growth = 0;
}
set_crop_tile(building_id, x, y, 2, 2, crop_image_id, growth);
// crop tile 4
growth -= 4;
if (growth < 0) {
growth = 0;
}
set_crop_tile(building_id, x, y, 2, 1, crop_image_id, growth);
// crop tile 5
growth -= 4;
if (growth < 0) {
growth = 0;
}
set_crop_tile(building_id, x, y, 2, 0, crop_image_id, growth);
}
int map_building_tiles_add_aqueduct(int x, int y)
{
int grid_offset = map_grid_offset(x,y);
map_terrain_add(grid_offset, TERRAIN_AQUEDUCT);
map_property_clear_constructing(grid_offset);
return 1;
}
static int north_tile_grid_offset(int x, int y, int *size)
{
int grid_offset = map_grid_offset(x, y);
*size = map_property_multi_tile_size(grid_offset);
for (int i = 0; i < *size && map_property_multi_tile_x(grid_offset); i++) {
grid_offset += map_grid_delta(-1, 0);
}
for (int i = 0; i < *size && map_property_multi_tile_y(grid_offset); i++) {
grid_offset += map_grid_delta(0, -1);
}
return grid_offset;
}
void map_building_tiles_remove(int building_id, int x, int y)
{
if (!map_grid_is_inside(x, y, 1)) {
return;
}
int size;
int base_grid_offset = north_tile_grid_offset(x, y, &size);
x = map_grid_offset_to_x(base_grid_offset);
y = map_grid_offset_to_y(base_grid_offset);
if (map_terrain_get(base_grid_offset) == TERRAIN_ROCK) {
return;
}
building *b = building_get(building_id);
if (building_id && building_is_farm(b->type)) {
size = 3;
}
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
if (building_id && map_building_at(grid_offset) != building_id) {
continue;
}
if (building_id && b->type != BUILDING_BURNING_RUIN) {
map_set_rubble_building_type(grid_offset, b->type);
}
map_property_clear_constructing(grid_offset);
map_property_set_multi_tile_size(grid_offset, 1);
map_property_clear_multi_tile_xy(grid_offset);
map_property_mark_draw_tile(grid_offset);
map_aqueduct_set(grid_offset, 0);
map_building_set(grid_offset, 0);
map_building_damage_clear(grid_offset);
map_sprite_clear_tile(grid_offset);
if (map_terrain_is(grid_offset, TERRAIN_WATER)) {
map_terrain_set(grid_offset, TERRAIN_WATER); // clear other flags
map_tiles_set_water(x + dx, y + dy);
} else {
map_image_set(grid_offset,
image_group(GROUP_TERRAIN_UGLY_GRASS) +
(map_random_get(grid_offset) & 7));
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
}
}
}
map_tiles_update_region_empty_land(x, y, x + size, y + size);
map_tiles_update_region_meadow(x, y, x + size, y + size);
map_tiles_update_region_rubble(x, y, x + size, y + size);
}
void map_building_tiles_set_rubble(int building_id, int x, int y, int size)
{
if (!map_grid_is_inside(x, y, size)) {
return;
}
building *b = building_get(building_id);
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
if (map_building_at(grid_offset) != building_id) {
continue;
}
if (building_id && building_get(map_building_at(grid_offset))->type != BUILDING_BURNING_RUIN) {
map_set_rubble_building_type(grid_offset, b->type);
} else if (!building_id && map_terrain_get(grid_offset) & TERRAIN_WALL) {
map_set_rubble_building_type(grid_offset, BUILDING_WALL);
}
map_property_clear_constructing(grid_offset);
map_property_set_multi_tile_size(grid_offset, 1);
map_aqueduct_set(grid_offset, 0);
map_building_set(grid_offset, 0);
map_building_damage_clear(grid_offset);
map_sprite_clear_tile(grid_offset);
map_property_set_multi_tile_xy(grid_offset, 0, 0, 1);
if (map_terrain_is(grid_offset, TERRAIN_WATER)) {
map_terrain_set(grid_offset, TERRAIN_WATER); // clear other flags
map_tiles_set_water(x + dx, y + dy);
} else {
map_terrain_remove(grid_offset, TERRAIN_CLEARABLE);
map_terrain_add(grid_offset, TERRAIN_RUBBLE);
map_image_set(grid_offset, image_group(GROUP_TERRAIN_RUBBLE) + (map_random_get(grid_offset) & 7));
}
}
}
}
static void adjust_to_absolute_xy(int *x, int *y, int size)
{
switch (city_view_orientation()) {
case DIR_2_RIGHT:
*x = *x - size + 1;
break;
case DIR_4_BOTTOM:
*x = *x - size + 1;
// fall-through
case DIR_6_LEFT:
*y = *y - size + 1;
break;
}
}
int map_building_tiles_mark_construction(int x, int y, int size, int terrain, int absolute_xy)
{
if (!absolute_xy) {
adjust_to_absolute_xy(&x, &y, size);
}
if (!map_grid_is_inside(x, y, size)) {
return 0;
}
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
if (map_terrain_is(grid_offset, terrain & TERRAIN_NOT_CLEAR) || map_has_figure_at(grid_offset)) {
return 0;
}
}
}
// mark as being constructed
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
map_property_mark_constructing(grid_offset);
}
}
return 1;
}
void map_building_tiles_mark_deleting(int grid_offset)
{
int building_id = map_building_at(grid_offset);
if (!building_id) {
map_bridge_remove(grid_offset, 1);
} else {
grid_offset = building_main(building_get(building_id))->grid_offset;
}
map_property_mark_deleted(grid_offset);
}
int map_building_tiles_are_clear(int x, int y, int size, int terrain)
{
adjust_to_absolute_xy(&x, &y, size);
if (!map_grid_is_inside(x, y, size)) {
return 0;
}
for (int dy = 0; dy < size; dy++) {
for (int dx = 0; dx < size; dx++) {
int grid_offset = map_grid_offset(x + dx, y + dy);
if (map_terrain_is(grid_offset, terrain & TERRAIN_NOT_CLEAR)) {
return 0;
}
}
}
return 1;
}