Pathfinding.xs

filter: Statementstmt 95.3% 123 / 129 Branchbran 90.0% 99 / 110 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.8% 222 / 239 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/TileMap/Pathfinding/Result.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include <math.h>
8  
9 #ifndef true
10 #include <stdbool.h>
11 #endif
12  
13 typedef struct FIFO FIFO;
14 typedef struct Visited Visited;
15  
16 struct FIFO {
17 int *buffer;
18 int head;
19 int tail;
20 int count;
21 int capacity;
22 };
23  
24 struct Visited {
25 int last;
26 float value;
27 };
28  
29 11 FIFO* fifo_create (int capacity)
30 {
31 11 FIFO *f = malloc(sizeof *f);
32  
33 11 f->buffer = malloc(capacity * sizeof *(f->buffer));
34 11 f->capacity = capacity;
35 11 f->head = 0;
36 11 f->tail = 0;
37 11 f->count = 0;
38  
39 11 return f;
40 }
41  
42 11 void fifo_destroy (FIFO *f)
43 {
44 11 free(f->buffer);
45 11 free(f);
46 11 }
47  
48 290 void fifo_push (FIFO *f, int value)
49 {
50 /* NOTE: overflow is not handled - capacity must be big enough */
51  
52 290 f->buffer[f->tail] = value;
53 290 f->tail = (f->tail + 1) % f->capacity;
54 290 f->count++;
55 290 }
56  
57 250 int fifo_pop (FIFO *f)
58 {
59 /* NOTE: underflow is not handled - count must be checked manually */
60  
61 250 int value = f->buffer[f->head];
62 250 f->head = (f->head + 1) % f->capacity;
63 250 f->count--;
64  
65 250 return value;
66 }
67  
68 16 AV* do_pathfinding (float *costs, int size_x, int size_y, int x1, int y1, int x2, int y2, bool diagonal)
69 {
70 16 int size = size_x * size_y;
71 16 int start = x1 * size_y + y1;
72 16 int end = x2 * size_y + y2;
73 int i;
74 16 ▶ int i_bound = diagonal ? 8 : 4;
Branch: 1 branch
BranchTrueFalseTotal
int i_bound = diagonal ? 8 : 4; 4 12 16
75  
76 16 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 15 1 16
77 15 ▶ x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0
Branch: 3 branches
BranchTrueFalseTotal
x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 15 0 15
x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 15 0 15
x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 14 1 15
78 14 ▶ || x1 >= size_x || y1 >= size_y || x2 >= size_x || y2 >= size_y
Branch: 4 branches
BranchTrueFalseTotal
|| x1 >= size_x || y1 >= size_y || x2 >= size_x || y2 >= size_y 14 0 14
|| x1 >= size_x || y1 >= size_y || x2 >= size_x || y2 >= size_y 13 1 14
|| x1 >= size_x || y1 >= size_y || x2 >= size_x || y2 >= size_y 12 1 13
|| x1 >= size_x || y1 >= size_y || x2 >= size_x || y2 >= size_y 0 12 12
79 )
80 4 return NULL;
81  
82 12 ▶ if (start == end)
Branch: 1 branch
BranchTrueFalseTotal
if (start == end) 1 11 12
83 1 return newAV();
84  
85 11 Visited *visited = malloc(size * sizeof *visited);
86 523 ▶ for (i = 0; i < size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; ++i) { 512 11 523
87 512 visited[i].value = -1;
88 }
89  
90 11 FIFO *next = fifo_create((size_x + size_y) * 2);
91 11 AV *result = NULL;
92  
93 11 fifo_push(next, start);
94 11 visited[start].value = 0;
95  
96 int current;
97 int sides[8];
98  
99 251 ▶ while (next->count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (next->count > 0) { 250 1 251
100 250 current = fifo_pop(next);
101 250 float current_value = visited[current].value;
102  
103 250 ▶ sides[0] = current % size_y == size_y - 1 ? size : current + 1;
Branch: 1 branch
BranchTrueFalseTotal
sides[0] = current % size_y == size_y - 1 ? size : current + 1; 213 37 250
104 250 ▶ sides[1] = current % size_y == 0 ? size : current - 1;
Branch: 1 branch
BranchTrueFalseTotal
sides[1] = current % size_y == 0 ? size : current - 1; 219 31 250
105 250 sides[2] = current + size_y;
106 250 sides[3] = current - size_y;
107  
108 250 ▶ if (diagonal) {
Branch: 1 branch
BranchTrueFalseTotal
if (diagonal) { 155 95 250
109 155 ▶ sides[4] = current % size_y == size_y - 1 ? size : current + size_y + 1;
Branch: 1 branch
BranchTrueFalseTotal
sides[4] = current % size_y == size_y - 1 ? size : current + size_y + 1; 130 25 155
110 155 ▶ sides[5] = current % size_y == size_y - 1 ? size : current - size_y + 1;
Branch: 1 branch
BranchTrueFalseTotal
sides[5] = current % size_y == size_y - 1 ? size : current - size_y + 1; 130 25 155
111 155 ▶ sides[6] = current % size_y == 0 ? size : current + size_y - 1;
Branch: 1 branch
BranchTrueFalseTotal
sides[6] = current % size_y == 0 ? size : current + size_y - 1; 141 14 155
112 155 ▶ sides[7] = current % size_y == 0 ? size : current - size_y - 1;
Branch: 1 branch
BranchTrueFalseTotal
sides[7] = current % size_y == 0 ? size : current - size_y - 1; 141 14 155
113  
114 /* do not allow moving diagonally when there's an obstacle nearby */
115 155 ▶ if (sides[0] != size && sides[2] < size && (costs[sides[0]] < 0 || costs[sides[2]] < 0))
Branch: 4 branches
BranchTrueFalseTotal
if (sides[0] != size && sides[2] < size && (costs[sides[0]] < 0 || costs[sides[2]] < 0)) 130 25 155
if (sides[0] != size && sides[2] < size && (costs[sides[0]] < 0 || costs[sides[2]] < 0)) 120 10 130
if (sides[0] != size && sides[2] < size && (costs[sides[0]] < 0 || costs[sides[2]] < 0)) 110 10 120
if (sides[0] != size && sides[2] < size && (costs[sides[0]] < 0 || costs[sides[2]] < 0)) 9 101 110
116 19 sides[4] = size;
117 155 ▶ if (sides[0] != size && sides[3] >= 0 && (costs[sides[0]] < 0 || costs[sides[3]] < 0))
Branch: 4 branches
BranchTrueFalseTotal
if (sides[0] != size && sides[3] >= 0 && (costs[sides[0]] < 0 || costs[sides[3]] < 0)) 130 25 155
if (sides[0] != size && sides[3] >= 0 && (costs[sides[0]] < 0 || costs[sides[3]] < 0)) 109 21 130
if (sides[0] != size && sides[3] >= 0 && (costs[sides[0]] < 0 || costs[sides[3]] < 0)) 99 10 109
if (sides[0] != size && sides[3] >= 0 && (costs[sides[0]] < 0 || costs[sides[3]] < 0)) 5 94 99
118 15 sides[5] = size;
119 155 ▶ if (sides[1] != size && sides[2] < size && (costs[sides[1]] < 0 || costs[sides[2]] < 0))
Branch: 4 branches
BranchTrueFalseTotal
if (sides[1] != size && sides[2] < size && (costs[sides[1]] < 0 || costs[sides[2]] < 0)) 141 14 155
if (sides[1] != size && sides[2] < size && (costs[sides[1]] < 0 || costs[sides[2]] < 0)) 129 12 141
if (sides[1] != size && sides[2] < size && (costs[sides[1]] < 0 || costs[sides[2]] < 0)) 113 16 129
if (sides[1] != size && sides[2] < size && (costs[sides[1]] < 0 || costs[sides[2]] < 0)) 9 104 113
120 25 sides[6] = size;
121 155 ▶ if (sides[1] != size && sides[3] >= 0 && (costs[sides[1]] < 0 || costs[sides[3]] < 0))
Branch: 4 branches
BranchTrueFalseTotal
if (sides[1] != size && sides[3] >= 0 && (costs[sides[1]] < 0 || costs[sides[3]] < 0)) 141 14 155
if (sides[1] != size && sides[3] >= 0 && (costs[sides[1]] < 0 || costs[sides[3]] < 0)) 119 22 141
if (sides[1] != size && sides[3] >= 0 && (costs[sides[1]] < 0 || costs[sides[3]] < 0)) 101 18 119
if (sides[1] != size && sides[3] >= 0 && (costs[sides[1]] < 0 || costs[sides[3]] < 0)) 5 96 101
122 23 sides[7] = size;
123 }
124  
125 1870 ▶ for (i = 0; i < i_bound; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < i_bound; ++i) { 1620 250 1870
126 /* out of bounds */
127 1620 ▶ if (sides[i] >= size || sides[i] < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (sides[i] >= size || sides[i] < 0) 1341 279 1620
if (sides[i] >= size || sides[i] < 0) 80 1261 1341
128 359 continue;
129  
130 /* not reachable */
131 1261 ▶ if (costs[sides[i]] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (costs[sides[i]] < 0) 144 1117 1261
132 144 continue;
133  
134 1117 ▶ float new_cost = current_value + costs[sides[i]] * (i > 3 ? sqrt(2) : 1);
Branch: 1 branch
BranchTrueFalseTotal
float new_cost = current_value + costs[sides[i]] * (i > 3 ? sqrt(2) : 1); 378 739 1117
135  
136 /* already visited earlier */
137 1117 ▶ if (visited[sides[i]].value >= 0 && visited[sides[i]].value <= new_cost)
Branch: 2 branches
BranchTrueFalseTotal
if (visited[sides[i]].value >= 0 && visited[sides[i]].value <= new_cost) 849 268 1117
if (visited[sides[i]].value >= 0 && visited[sides[i]].value <= new_cost) 838 11 849
138 838 continue;
139  
140 279 visited[sides[i]].value = new_cost;
141 279 visited[sides[i]].last = current;
142  
143 279 fifo_push(next, sides[i]);
144 }
145  
146 /* found destination, backtrack to find path */
147 250 ▶ if (visited[end].value > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (visited[end].value > 0) { 10 240 250
148 10 result = newAV();
149 10 current = end;
150  
151 83 ▶ while (current != start) {
Branch: 1 branch
BranchTrueFalseTotal
while (current != start) { 73 10 83
152 73 SV *val_x = newSViv(current / size_y);
153 73 SV *val_y = newSViv(current % size_y);
154 73 av_unshift(result, 2);
155 73 ▶ if (av_store(result, 0, val_x) == NULL || av_store(result, 1, val_y) == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (av_store(result, 0, val_x) == NULL || av_store(result, 1, val_y) == NULL) { 73 0 73
if (av_store(result, 0, val_x) == NULL || av_store(result, 1, val_y) == NULL) { 0 73 73
156 0 SvREFCNT_dec(val_x);
157 0 SvREFCNT_dec(val_y);
158 0 free(visited);
159 0 fifo_destroy(next);
160 0 croak("could not store pathfinding coordinates in an AV");
161 }
162  
163 73 current = visited[current].last;
164 }
165  
166 10 break;
167 }
168 }
169  
170 11 free(visited);
171 11 fifo_destroy(next);
172  
173 11 return result;
174 }
175  
176 116 SV* get_hash_key (HV* hash, const char* key, int len)
177 {
178 116 SV **value = hv_fetch(hash, key, len, 0);
179  
180 116 ▶ if (value == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (value == NULL) return NULL; 0 116 116
181 116 return *value;
182 }
183  
184 MODULE = Game::TileMap::Pathfinding PACKAGE = Game::TileMap::Pathfinding
185  
186 PROTOTYPES: DISABLE
187  
188 void
189 _prepare(self)
190 SV *self
191 CODE:
192 13 HV *self_hash = (HV*) SvRV(self);
193 13 SV *map = get_hash_key(self_hash, "map", 3);
194 13 int size_x = SvIV(get_hash_key(self_hash, "_map_size_x", 11));
195 13 int size_y = SvIV(get_hash_key(self_hash, "_map_size_y", 11));
196  
197 13 dSP;
198  
199 int count;
200 13 float *costs = malloc(size_x * size_y * sizeof *costs);
201 int i, j;
202 93 ▶ for (i = 0; i < size_x; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size_x; ++i) { 80 13 93
203 624 ▶ for (j = 0; j < size_y; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < size_y; ++j) { 544 80 624
204 544 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 544 544
205 544 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 544 544
206 544 PUSHs(map);
207 544 mPUSHi(i);
208 544 mPUSHi(j);
209 544 PUTBACK;
210 544 count = call_method("check_can_be_accessed", G_SCALAR);
211 544 SPAGAIN;
212  
213 544 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 544 544
214 0 croak("Calling check_can_be_accessed went wrong while preparing the pathfinding");
215 }
216  
217 544 SV *result = POPs;
218 544 ▶ costs[i * size_y + j] = SvTRUE(result) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
costs[i * size_y + j] = SvTRUE(result) ? 1 : -1; 428 116 544
219 }
220 }
221  
222 13 SV *map_data = newSViv((uintptr_t) costs);
223 13 SvREADONLY_on(map_data);
224 13 hv_stores(self_hash, "_map_data", map_data);
225  
226 SV*
227 _find_path(self, x1, y1, x2, y2)
228 SV *self
229 int x1
230 int y1
231 int x2
232 int y2
233 CODE:
234 16 HV *self_hash = (HV*) SvRV(self);
235 16 int size_x = SvIV(get_hash_key(self_hash, "_map_size_x", 11));
236 16 int size_y = SvIV(get_hash_key(self_hash, "_map_size_y", 11));
237 16 bool diagonal = SvTRUE(get_hash_key(self_hash, "diagonal_movement", 17));
238  
239 16 float *costs = (float*) SvIV(get_hash_key(self_hash, "_map_data", 9));
240  
241 16 AV *result = do_pathfinding(costs, size_x, size_y, x1, y1, x2, y2, diagonal);
242 16 ▶ if (result != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (result != NULL) 11 5 16
243 11 RETVAL = newRV_inc((SV*) result);
244 else
245 5 RETVAL = &PL_sv_undef;
246 OUTPUT:
247 RETVAL
248  
249 void
250 _cleanup(self)
251 SV *self
252 CODE:
253 13 SV *value = get_hash_key((HV*) SvRV(self), "_map_data", 9);
254 13 ▶ if (value != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (value != NULL) { 13 0 13
255 13 float *costs = (float*) SvIV(value);
256 13 free(costs);
257 }
258  
« blib/lib/Game/TileMap/Pathfinding/Result.pm ↑ Summary