Revision 309
finally done with zombies!
ent.c | ||
---|---|---|
216 | 216 |
basic_sprite_t *bsp_background; |
217 | 217 |
sprite_t *background; |
218 | 218 |
uint8_t *collide; |
219 |
int *x_prev; |
|
220 |
int *y_prev; |
|
219 |
uint8_t *collide_gunner; |
|
220 |
int32_t *prev; |
|
221 |
uint8_t *visited; |
|
221 | 222 |
}; |
223 |
static int (map_collides_gunner_pos)(const map_t *p, double shooter_x, double shooter_y, double radius) { |
|
224 |
for (double x = -radius; x <= radius; x += 1) { |
|
225 |
double y1 = sqrt(radius*radius - x*x); |
|
226 |
double y2 = -y1; |
|
227 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
|
228 |
} |
|
229 |
return 0; |
|
230 |
} |
|
222 | 231 |
map_t* (map_ctor)(const char **background, const char **collide){ |
223 | 232 |
map_t *ret = malloc(sizeof(map_t)); |
224 | 233 |
if(ret == NULL) return NULL; |
... | ... | |
226 | 235 |
ret->bsp_background = NULL; |
227 | 236 |
ret->background = NULL; |
228 | 237 |
ret->collide = NULL; |
229 |
ret->x_prev = NULL;
|
|
230 |
ret->y_prev = NULL;
|
|
238 |
ret->prev = NULL;
|
|
239 |
ret->visited = NULL;
|
|
231 | 240 |
|
232 | 241 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
233 | 242 |
ret->background = sprite_ctor(ret->bsp_background); |
... | ... | |
246 | 255 |
} |
247 | 256 |
basic_sprite_dtor(bsp_collide); |
248 | 257 |
|
249 |
ret->x_prev = malloc(W*H*sizeof(int)); |
|
250 |
ret->y_prev = malloc(W*H*sizeof(int)); |
|
258 |
ret->collide_gunner = malloc(W*H*sizeof(uint8_t)); |
|
259 |
if(ret->collide_gunner == NULL){ map_dtor(ret); return NULL; } |
|
260 |
for(size_t i = 0; i < W*H; ++i){ |
|
261 |
int16_t x = i%W, y = i/W; |
|
262 |
ret->collide_gunner[i] = (map_collides_gunner_pos(ret, x, y, 36) ? 1 : 0); |
|
263 |
} |
|
251 | 264 |
|
252 |
if(ret->x_prev == NULL || ret->y_prev == NULL){ |
|
265 |
ret->prev = malloc(W*H*sizeof(int32_t)); |
|
266 |
ret->visited = malloc(W*H*sizeof(uint8_t)); |
|
267 |
|
|
268 |
if(ret->prev == NULL || ret->visited == NULL){ |
|
253 | 269 |
map_dtor(ret); |
254 | 270 |
return NULL; |
255 | 271 |
} |
... | ... | |
260 | 276 |
sprite_dtor(p->background); |
261 | 277 |
basic_sprite_dtor(p->bsp_background); |
262 | 278 |
free(p->collide); |
263 |
free(p->x_prev);
|
|
264 |
free(p->y_prev);
|
|
279 |
free(p->prev); |
|
280 |
free(p->visited);
|
|
265 | 281 |
free(p); |
266 | 282 |
} |
267 | 283 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
... | ... | |
275 | 291 |
uint32_t pos = x_ + y_*w; |
276 | 292 |
return p->collide[pos]; |
277 | 293 |
} |
278 |
int (map_make_dijkstra)(map_t *p, uint16_t x, uint16_t y){ |
|
294 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
|
295 |
double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
|
296 |
return map_collides_gunner_pos(p, gunner_get_x(shooter), gunner_get_y(shooter), radius); |
|
297 |
} |
|
298 |
int (map_make_dijkstra)(map_t *p, int16_t x, int16_t y){ |
|
299 |
|
|
279 | 300 |
const uint16_t W = basic_sprite_get_w(p->bsp_background), |
280 | 301 |
H = basic_sprite_get_h(p->bsp_background); |
281 |
for(size_t i = 0; i < W*H; ++i) |
|
282 |
p->x_prev[i] = p->y_prev[i] = -1; |
|
302 |
|
|
303 |
static uint8_t first_time = true; |
|
304 |
if(first_time){ |
|
305 |
for(size_t i = 0; i < W*H; ++i) p->prev[i] = -1; |
|
306 |
first_time = false; |
|
307 |
} |
|
283 | 308 |
/// ACTUAL DIJKSTRA |
284 | 309 |
queue_t *q = queue_ctor(); |
310 |
|
|
311 |
memset(p->visited, false, W*H*sizeof(uint8_t)); |
|
312 |
|
|
285 | 313 |
int *ptr; |
286 |
p->x_prev[y*W+x] = x; p->x_prev[y*W+x] = y; |
|
287 |
ptr = malloc(sizeof(int)); *ptr = y*W+x; queue_push(q, ptr); |
|
314 |
int32_t c, pos; |
|
315 |
c = y*W+x; p->prev[c] = c; ptr = malloc(sizeof(int)); *ptr = c; queue_push(q, ptr); |
|
316 |
|
|
288 | 317 |
while(!queue_empty(q)){ |
289 |
int c = *(int*)list_node_val(queue_top(q)); free(queue_top(q)); queue_pop(q); |
|
290 |
if(p->collide[c]){ p->x_prev[c] = p->y_prev[c] = -1; continue; } |
|
291 |
uint16_t x = c%W, y = c/W; |
|
292 |
if(0 <= x-1){ int pos = y*W+(x-1); if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
293 |
if(x+1 < W ){ int pos = y*W+(x+1); if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
294 |
if(0 <= y-1){ int pos = (y-1)*W+x; if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
295 |
if(y+1 < H ){ int pos = (y+1)*W+x; if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
318 |
c = *(int*)queue_top(q); free(queue_top(q)); queue_pop(q); |
|
319 |
x = c%W, y = c/W; |
|
320 |
if(p->visited[c]) continue; |
|
321 |
p->visited[c] = true; |
|
322 |
if(p->collide_gunner[c]) continue; |
|
323 |
if(0 <= x-1){ pos = y*W+(x-1); if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
324 |
if(x+1 < W ){ pos = y*W+(x+1); if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
325 |
if(0 <= y-1){ pos = (y-1)*W+x; if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
326 |
if(y+1 < H ){ pos = (y+1)*W+x; if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
|
296 | 327 |
} |
297 |
return SUCCESS; |
|
298 |
} |
|
299 | 328 |
|
329 |
queue_dtor(q); |
|
300 | 330 |
|
301 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
|
302 |
double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
|
303 |
double shooter_x = gunner_get_x(shooter); |
|
304 |
double shooter_y = gunner_get_y(shooter); |
|
305 |
for (double x = -radius; x < radius; x += 1) { |
|
306 |
double y1 = sqrt(radius*radius - x*x); |
|
307 |
double y2 = -y1; |
|
308 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
|
309 |
} |
|
310 |
return 0; |
|
331 |
return SUCCESS; |
|
311 | 332 |
} |
333 |
int (map_where_to_follow)(const map_t *p, float x, float y, float *theta){ |
|
334 |
const uint16_t W = basic_sprite_get_w(p->bsp_background); |
|
335 |
int x_ = x, y_ = y; |
|
336 |
int pos = y_*W+x_; |
|
337 |
//printf("Is in %d,%d\n", x_, y_); |
|
338 |
int newx = p->prev[pos]%W, newy = p->prev[pos]/W; |
|
339 |
//printf("from %d,%d to %d,%d\n", x_, y_, newx, newy); |
|
340 |
*theta = atan2(-(newy-y_), newx-x_); |
|
341 |
return SUCCESS; |
|
342 |
} |
|
312 | 343 |
|
313 | 344 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
314 | 345 |
double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
Also available in: Unified diff