-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.c
460 lines (375 loc) · 11.3 KB
/
bridge.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "bridge.h"
struct _bridge {
isla *point[2];
int written;
list *probi_list;
};
bridge *create_bridge(isla *point_one, isla *point_two)
{
bridge *new_bridge = NULL;
new_bridge = (bridge *)malloc(sizeof(bridge));
if( new_bridge == NULL )
memory_error("Unable to create structure bridge");
new_bridge->point[0] = point_one;
new_bridge->point[1] = point_two;
new_bridge->probi_list = create_list();
new_bridge->written = 0;
return new_bridge;
}
void set_bridge_written(bridge *got_bridge, int value)
{
got_bridge->written = value;
return;
}
bool get_bridge_written(bridge *got_bridge)
{
return got_bridge->written;
}
isla *get_points(bridge *got_bridge, int index)
{
return got_bridge->point[index];
}
list *get_bridge_probi_list(bridge *got_bridge)
{
return got_bridge->probi_list;
}
int get_isla_x_from_bridge(bridge *got_bridge, int i)
{
return get_x(get_isla_pos(get_points(got_bridge, i)));
}
int get_isla_y_from_bridge(bridge *got_bridge, int i)
{
return get_y(get_isla_pos(get_points(got_bridge, i)));
}
void print_bridge(item got_item)
{
bridge* got_bridge = (bridge *)got_item;
fprintf(DEBUG_LOC,
KMAG "Isla 1 name:" RESET " %d, "
KMAG "Isla 2 name:" RESET " %d \n",
get_isla_name(get_points(got_bridge, 0)), get_isla_name(get_points(got_bridge, 1)));
#ifdef PROBI_DEBUG
if(got_bridge->probi_list != NULL)
print_list(got_bridge->probi_list, print_bridge);
#endif
return;
}
/* Just to know if there is any bridge in an isla direction */
bridge *is_bridge(isla *new_isla, int dir)
{
bridge *got_bridge = NULL;
got_bridge = (bridge *)get_isla_used_bridge(new_isla, dir, 0);
if(got_bridge == NULL)
got_bridge = get_isla_used_bridge(new_isla, dir, 1);
return got_bridge;
}
void free_bridge(item got_item)
{
bridge *got_bridge = (bridge *)got_item;
if(get_head(got_bridge->probi_list) != NULL)
{
free_connected_nodes(get_head(got_bridge->probi_list), free_bridge);
}
free(got_bridge->probi_list);
free(got_bridge);
return;
}
int get_isla_n_bridges(isla *new_isla, int dir)
{
int bridge_counter = 0, i = 0;
bridge *new_bridge = NULL;
for(i = 0; i < 2; i++)
{
new_bridge = (bridge*) get_isla_used_bridge(new_isla, dir, i);
if(new_bridge != NULL)
{
bridge_counter ++;
}
}
return bridge_counter;
}
/*find the next isla on a row*/
isla* find_next_isla_x(map *got_map, int x, int static_y, int x_max)
{
isla *new = NULL;
while(new == NULL && x <= x_max)
{
new = get_tile(got_map, x, static_y);
x++;
}
return new;
}
/*find the next isla on a column*/
isla* find_next_isla_y(map *got_map, int static_x, int y, int y_max)
{
isla *new = NULL;
while(new == NULL && y <= y_max)
{
new = get_tile(got_map, static_x, y);
y++;
}
return new;
}
/*sets all adjacent islas in a column*/
void find_adj_y(isla* active_row_node, map *got_map)
{
isla *new = NULL, *new_next = NULL;
int y_max, static_x, y;
new = active_row_node; /* new gets the first isla on the column */
y_max = get_y_max(got_map);
static_x = get_x(get_isla_pos(active_row_node)); /* gets which column we are working on */
y = get_y(get_isla_pos(new)); /* gets the row from which we are working on */
new_next = find_next_isla_y(got_map, static_x, y+1, y_max); /* gets the next isla in that column */
if(new_next != NULL) /* if an isla is actually found, it's an adjacent */
{
set_isla_adj(new, new_next, 1);
set_isla_adj(new_next, new, 0);
}
}
void find_adj(map* got_map)
{
isla *new = NULL, *new_next = NULL;
int y = 1, y_max, x = 1, x_max;
y_max = get_y_max(got_map);
x_max = get_x_max(got_map);
while(y <= y_max) /* check till last row */
{
new = find_next_isla_x(got_map, 1, y, x_max);
while(new != NULL) /* check till last cloumn */
{
y = get_y(get_isla_pos(new));
x = get_x(get_isla_pos(new));
new_next = find_next_isla_x(got_map, x+1, y, x_max);
if(new_next != NULL) /* if the row has more islas */
{
set_isla_adj(new, new_next, 2);
set_isla_adj(new_next, new, 3);
}
find_adj_y(new, got_map); /* find islas in all column now */
new = new_next;
}
y++;
}
}
void print_adj(list* isla_list)
{
isla* new;
node* node;
int i = 0;
node = get_head(isla_list);
while(node != NULL)
{
new = get_node_item(node);
fprintf(DEBUG_LOC, KGRN "Isla: %d - " RESET, get_isla_name(new));
for(i=0; i<4; i++)
{
if(get_isla_adj(new, i) != NULL)
{
fprintf(DEBUG_LOC, KBLU "Adj %d:" RESET KGRN "%d " KNRM, i, get_isla_name(get_isla_adj(new, i)));
}
else
{
fprintf(DEBUG_LOC, KBLU "Adj %d:" RESET KGRN "X " KNRM, i);
}
}
printf("\n");
node = get_next_node(node);
}
}
bool is_cross_vertical(int islas_x, int isla_a_y, int isla_b_y, bridge *new_bridge)
{
int min_isla = 0, max_isla = 0;
if(get_isla_y_from_bridge(new_bridge, 0) != get_isla_y_from_bridge(new_bridge, 1))
return FALSE;
if(isla_a_y > isla_b_y)
{
min_isla = isla_b_y;
max_isla = isla_a_y;
}
else
{
min_isla = isla_a_y;
max_isla = isla_b_y;
}
if((get_isla_x_from_bridge(new_bridge, 0) < islas_x
&& get_isla_x_from_bridge(new_bridge, 1) > islas_x) ||
(get_isla_x_from_bridge(new_bridge, 1) < islas_x
&& get_isla_x_from_bridge(new_bridge, 0) > islas_x))
{
if(min_isla < get_isla_y_from_bridge(new_bridge, 0)
&& get_isla_y_from_bridge(new_bridge, 0) < max_isla)
{
return TRUE;
}
}
return FALSE;
}
bool is_cross_horizontal(int islas_y, int isla_a_x, int isla_b_x, bridge *new_bridge)
{
int min_isla = 0, max_isla = 0;
if(get_isla_x_from_bridge(new_bridge, 0) != get_isla_x_from_bridge(new_bridge, 1))
return FALSE;
if(isla_a_x > isla_b_x)
{
min_isla = isla_b_x;
max_isla = isla_a_x;
}
else
{
min_isla = isla_a_x;
max_isla = isla_b_x;
}
if((get_isla_y_from_bridge(new_bridge, 0) < islas_y
&& get_isla_y_from_bridge(new_bridge, 1) > islas_y) ||
(get_isla_y_from_bridge(new_bridge, 1) < islas_y
&& get_isla_y_from_bridge(new_bridge, 0) > islas_y))
{
if(min_isla < get_isla_x_from_bridge(new_bridge, 0)
&& get_isla_x_from_bridge(new_bridge, 0) < max_isla)
{
return TRUE;
}
}
return FALSE;
}
bool crossed_fire(isla* isla_a, isla* isla_b, stack *got_stack)
{
int a_x, a_y, b_x, b_y;
bridge *new_bridge = NULL;
node *new_node = NULL;
new_node = get_stack_head(got_stack);
while(new_node != NULL)
{
new_bridge = get_node_item(new_node);
a_x = get_x(get_isla_pos(isla_a));
a_y = get_y(get_isla_pos(isla_a));
b_x = get_x(get_isla_pos(isla_b));
b_y = get_y(get_isla_pos(isla_b));
/* Asking for a vertical bridge possibility */
if(a_x == b_x)
{
if(is_cross_vertical(a_x, a_y, b_y, new_bridge))
return TRUE;
}
else
{
if(is_cross_horizontal(a_y, a_x, b_x, new_bridge))
return TRUE;
}
new_node = get_next_node(new_node);
}
return FALSE;
}
int get_numberof_bridges(list *isla_list)
{
isla *new_isla;
node *new_node;
int n = 0;
new_node = get_head(isla_list);
while(new_node != NULL)
{
new_isla = get_node_item(new_node);
n += get_isla_bridges_avb(new_isla);
new_node = get_next_node(new_node);
}
return n/2;
}
int get_numberof_bridges_avb(list *isla_list)
{
isla *new_isla;
node *new_node;
int n = 0;
new_node = get_head(isla_list);
while(new_node != NULL)
{
new_isla = get_node_item(new_node);
n += get_isla_bridge_s_avb(new_isla);
new_node = get_next_node(new_node);
}
return n/2;
}
void free_isla(item got_item)
{
isla *got_isla = (isla *)got_item;
isla *adj_isla = NULL;
bridge *active_bridge;
int dir = 0, index = 0;
/* O say can you see
* by the dawn's early light*/
free_pos(get_isla_pos(got_isla));
for(dir = 0; dir < 4; dir++)
{
index = 0;
while(index <= 1)
{
active_bridge = get_isla_used_bridge(got_isla, dir, index);
if(active_bridge != NULL)
{
adj_isla = get_points(active_bridge, 0);
if(get_isla_name(adj_isla) == get_isla_name(got_isla))
{
adj_isla = get_points(active_bridge, 1);
}
set_isla_used_bridge(adj_isla, get_opposite_dir(dir), index, NULL);
free_bridge(active_bridge);
}
index++;
}
}
/* Bring democracy to the pointer */
free(got_isla);
return;
}
bridge *connect_islas(isla *isla_a, isla *isla_b, int dir)
{
bridge *got_bridge = NULL;
if(get_isla_used_bridge(isla_a, dir, 0) == NULL)
{
got_bridge = create_bridge(isla_a, isla_b);
set_isla_used_bridge(isla_a, dir, 0, (bridge *)got_bridge);
set_isla_used_bridge(isla_b, get_opposite_dir(dir), 0, (bridge *)got_bridge);
}
else if(get_isla_used_bridge(isla_a, dir, 1) == NULL)
{
got_bridge = create_bridge(isla_a, isla_b);
set_isla_used_bridge(isla_a, dir, 1, (bridge *)got_bridge);
set_isla_used_bridge(isla_b, get_opposite_dir(dir), 1, (bridge *)got_bridge);
}
/* Decrease available connections in islas */
dec_isla_bridge_s_avb(isla_a);
dec_isla_bridge_s_avb(isla_b);
return got_bridge;
}
/* A function that detects if the map is already fucked up or not */
bool initial_fuck_up(list *isla_list)
{
isla *new_isla, *_adj;
node *new_node;
int n_total_bridges = 0, i = 0;
new_node = get_head(isla_list);
while(new_node != NULL)
{
new_isla = get_node_item(new_node);
/*
* check if the sum of bridges from adj islas are less
* than the number of bridges of the isla being tested
*/
for(i = 0; i < 4; i++)
{
_adj = get_isla_adj(new_isla, i);
if(_adj != NULL)
{
if(get_isla_bridges_avb(_adj) >= 2)
n_total_bridges += 2;
else if(get_isla_bridges_avb(_adj) == 1)
n_total_bridges += 1;
}
}
/* if there aren't enough bridges for an isla */
if(n_total_bridges < get_isla_bridges_avb(new_isla))
return TRUE;
n_total_bridges = 0; i = 0;
new_node = get_next_node(new_node);
}
return FALSE;
}