forked from bvschaik/julius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroad_access.c
295 lines (270 loc) · 10.1 KB
/
road_access.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
#include "road_access.h"
#include "building/building.h"
#include "city/map.h"
#include "map/building.h"
#include "map/grid.h"
#include "map/road_network.h"
#include "map/routing.h"
#include "map/routing_terrain.h"
#include "map/terrain.h"
static void find_minimum_road_tile(int x, int y, int size, int *min_value, int *min_grid_offset)
{
int base_offset = map_grid_offset(x, y);
for (const int *tile_delta = map_grid_adjacent_offsets(size); *tile_delta; tile_delta++) {
int grid_offset = base_offset + *tile_delta;
if (!map_terrain_is(grid_offset, TERRAIN_BUILDING) ||
building_get(map_building_at(grid_offset))->type != BUILDING_GATEHOUSE) {
if (map_terrain_is(grid_offset, TERRAIN_ROAD)) {
int road_index = city_map_road_network_index(map_road_network_get(grid_offset));
if (road_index < *min_value) {
*min_value = road_index;
*min_grid_offset = grid_offset;
}
}
}
}
}
int map_has_road_access(int x, int y, int size, map_point *road)
{
int min_value = 12;
int min_grid_offset = map_grid_offset(x, y);
find_minimum_road_tile(x, y, size, &min_value, &min_grid_offset);
if (min_value < 12) {
if (road) {
map_point_store_result(map_grid_offset_to_x(min_grid_offset), map_grid_offset_to_y(min_grid_offset), road);
}
return 1;
}
return 0;
}
int map_has_road_access_hippodrome(int x, int y, map_point *road)
{
int min_value = 12;
int min_grid_offset = map_grid_offset(x, y);
find_minimum_road_tile(x, y, 5, &min_value, &min_grid_offset);
find_minimum_road_tile(x + 5, y, 5, &min_value, &min_grid_offset);
find_minimum_road_tile(x + 10, y, 5, &min_value, &min_grid_offset);
if (min_value < 12) {
if (road) {
map_point_store_result(map_grid_offset_to_x(min_grid_offset), map_grid_offset_to_y(min_grid_offset), road);
}
return 1;
}
return 0;
}
int map_has_road_access_granary(int x, int y, map_point *road)
{
int rx = -1, ry = -1;
if (map_terrain_is(map_grid_offset(x + 1, y - 1), TERRAIN_ROAD)) {
rx = x + 1;
ry = y - 1;
} else if (map_terrain_is(map_grid_offset(x + 3, y + 1), TERRAIN_ROAD)) {
rx = x + 3;
ry = y + 1;
} else if (map_terrain_is(map_grid_offset(x + 1, y + 3), TERRAIN_ROAD)) {
rx = x + 1;
ry = y + 3;
} else if (map_terrain_is(map_grid_offset(x - 1, y + 1), TERRAIN_ROAD)) {
rx = x - 1;
ry = y + 1;
}
if (rx >= 0 && ry >= 0) {
if (road) {
map_point_store_result(rx, ry, road);
}
return 1;
}
return 0;
}
static int road_within_radius(int x, int y, int size, int radius, int *x_road, int *y_road)
{
int x_min, y_min, x_max, y_max;
map_grid_get_area(x, y, size, radius, &x_min, &y_min, &x_max, &y_max);
for (int yy = y_min; yy <= y_max; yy++) {
for (int xx = x_min; xx <= x_max; xx++) {
if (map_terrain_is(map_grid_offset(xx, yy), TERRAIN_ROAD)) {
if (x_road && y_road) {
*x_road = xx;
*y_road = yy;
}
return 1;
}
}
}
return 0;
}
int map_closest_road_within_radius(int x, int y, int size, int radius, int *x_road, int *y_road)
{
for (int r = 1; r <= radius; r++) {
if (road_within_radius(x, y, size, r, x_road, y_road)) {
return 1;
}
}
return 0;
}
static int reachable_road_within_radius(int x, int y, int size, int radius, int *x_road, int *y_road)
{
int x_min, y_min, x_max, y_max;
map_grid_get_area(x, y, size, radius, &x_min, &y_min, &x_max, &y_max);
for (int yy = y_min; yy <= y_max; yy++) {
for (int xx = x_min; xx <= x_max; xx++) {
int grid_offset = map_grid_offset(xx, yy);
if (map_terrain_is(grid_offset, TERRAIN_ROAD)) {
if (map_routing_distance(grid_offset) > 0) {
if (x_road && y_road) {
*x_road = xx;
*y_road = yy;
}
return 1;
}
}
}
}
return 0;
}
int map_closest_reachable_road_within_radius(int x, int y, int size, int radius, int *x_road, int *y_road)
{
for (int r = 1; r <= radius; r++) {
if (reachable_road_within_radius(x, y, size, r, x_road, y_road)) {
return 1;
}
}
return 0;
}
int map_road_to_largest_network(int x, int y, int size, int *x_road, int *y_road)
{
int min_index = 12;
int min_grid_offset = -1;
int base_offset = map_grid_offset(x, y);
for (const int *tile_delta = map_grid_adjacent_offsets(size); *tile_delta; tile_delta++) {
int grid_offset = base_offset + *tile_delta;
if (map_terrain_is(grid_offset, TERRAIN_ROAD) && map_routing_distance(grid_offset) > 0) {
int index = city_map_road_network_index(map_road_network_get(grid_offset));
if (index < min_index) {
min_index = index;
min_grid_offset = grid_offset;
}
}
}
if (min_index < 12) {
*x_road = map_grid_offset_to_x(min_grid_offset);
*y_road = map_grid_offset_to_y(min_grid_offset);
return min_grid_offset;
}
int min_dist = 100000;
min_grid_offset = -1;
for (const int *tile_delta = map_grid_adjacent_offsets(size); *tile_delta; tile_delta++) {
int grid_offset = base_offset + *tile_delta;
int dist = map_routing_distance(grid_offset);
if (dist > 0 && dist < min_dist) {
min_dist = dist;
min_grid_offset = grid_offset;
}
}
if (min_grid_offset >= 0) {
*x_road = map_grid_offset_to_x(min_grid_offset);
*y_road = map_grid_offset_to_y(min_grid_offset);
return min_grid_offset;
}
return -1;
}
static void check_road_to_largest_network_hippodrome(int x, int y, int *min_index, int *min_grid_offset)
{
int base_offset = map_grid_offset(x, y);
for (const int *tile_delta = map_grid_adjacent_offsets(5); *tile_delta; tile_delta++) {
int grid_offset = base_offset + *tile_delta;
if (map_terrain_is(grid_offset, TERRAIN_ROAD) && map_routing_distance(grid_offset) > 0) {
int index = city_map_road_network_index(map_road_network_get(grid_offset));
if (index < *min_index) {
*min_index = index;
*min_grid_offset = grid_offset;
}
}
}
}
static void check_min_dist_hippodrome(
int base_offset, int x_offset, int *min_dist, int *min_grid_offset, int *min_x_offset)
{
for (const int *tile_delta = map_grid_adjacent_offsets(5); *tile_delta; tile_delta++) {
int grid_offset = base_offset + *tile_delta;
int dist = map_routing_distance(grid_offset);
if (dist > 0 && dist < *min_dist) {
*min_dist = dist;
*min_grid_offset = grid_offset;
*min_x_offset = x_offset;
}
}
}
int map_road_to_largest_network_hippodrome(int x, int y, int *x_road, int *y_road)
{
int min_index = 12;
int min_grid_offset = -1;
check_road_to_largest_network_hippodrome(x, y, &min_index, &min_grid_offset);
check_road_to_largest_network_hippodrome(x + 5, y, &min_index, &min_grid_offset);
check_road_to_largest_network_hippodrome(x + 10, y, &min_index, &min_grid_offset);
if (min_index < 12) {
*x_road = map_grid_offset_to_x(min_grid_offset);
*y_road = map_grid_offset_to_y(min_grid_offset);
return min_grid_offset;
}
int min_dist = 100000;
min_grid_offset = -1;
int min_x_offset = -1;
check_min_dist_hippodrome(map_grid_offset(x, y), 0, &min_dist, &min_grid_offset, &min_x_offset);
check_min_dist_hippodrome(map_grid_offset(x + 5, y), 5, &min_dist, &min_grid_offset, &min_x_offset);
check_min_dist_hippodrome(map_grid_offset(x + 10, y), 10, &min_dist, &min_grid_offset, &min_x_offset);
if (min_grid_offset >= 0) {
*x_road = map_grid_offset_to_x(min_grid_offset) + min_x_offset;
*y_road = map_grid_offset_to_y(min_grid_offset);
return min_grid_offset + min_x_offset;
}
return -1;
}
static int terrain_is_road_like(int grid_offset)
{
return map_terrain_is(grid_offset, TERRAIN_ROAD | TERRAIN_ACCESS_RAMP) ? 1 : 0;
}
static int get_adjacent_road_tile_for_roaming(int grid_offset)
{
int is_road = terrain_is_road_like(grid_offset);
if (map_terrain_is(grid_offset, TERRAIN_BUILDING)) {
building *b = building_get(map_building_at(grid_offset));
if (b->type == BUILDING_GATEHOUSE) {
is_road = 0;
} else if (b->type == BUILDING_GRANARY) {
if (map_routing_citizen_is_road(grid_offset)) {
is_road = 1;
}
}
}
return is_road;
}
int map_get_adjacent_road_tiles_for_roaming(int grid_offset, int *road_tiles)
{
road_tiles[1] = road_tiles[3] = road_tiles[5] = road_tiles[7] = 0;
road_tiles[0] = get_adjacent_road_tile_for_roaming(grid_offset + map_grid_delta(0, -1));
road_tiles[2] = get_adjacent_road_tile_for_roaming(grid_offset + map_grid_delta(1, 0));
road_tiles[4] = get_adjacent_road_tile_for_roaming(grid_offset + map_grid_delta(0, 1));
road_tiles[6] = get_adjacent_road_tile_for_roaming(grid_offset + map_grid_delta(-1, 0));
return road_tiles[0] + road_tiles[2] + road_tiles[4] + road_tiles[6];
}
int map_get_diagonal_road_tiles_for_roaming(int grid_offset, int *road_tiles)
{
road_tiles[1] = terrain_is_road_like(grid_offset + map_grid_delta(1, -1));
road_tiles[3] = terrain_is_road_like(grid_offset + map_grid_delta(1, 1));
road_tiles[5] = terrain_is_road_like(grid_offset + map_grid_delta(-1, 1));
road_tiles[7] = terrain_is_road_like(grid_offset + map_grid_delta(-1, -1));
int max_stretch = 0;
int stretch = 0;
for (int i = 0; i < 16; i++) {
if (road_tiles[i % 8]) {
stretch++;
if (stretch > max_stretch) {
max_stretch = stretch;
}
} else {
stretch = 0;
}
}
return max_stretch;
}