From 483d5cf42b38828e152561817769bd4155612c99 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 19 Aug 2021 15:18:43 -0400 Subject: char/enemy: Add collision avoidance --- (limited to 'src/char') diff --git a/src/char/enemy.c b/src/char/enemy.c index b8836fb..0644ed3 100644 --- a/src/char/enemy.c +++ b/src/char/enemy.c @@ -39,6 +39,7 @@ _mf_enemy_update(struct mf_char *c __attribute__((__unused__))) static int _mf_enemy_step(struct mf_char *c) { + struct mf_enemy *e = (struct mf_enemy *) c; enum _mf_char_dir dirs[4] = {MF_CHAR_DIR_U_, MF_CHAR_DIR_D_, MF_CHAR_DIR_L_, MF_CHAR_DIR_R_}; int i; @@ -84,9 +85,27 @@ _mf_enemy_step(struct mf_char *c) /* Wall ahead; don't go this direction. */ continue; } + for (j = 0; j < e->num_allies; ++j) { + if (e->allies[j] == c) { + /* Found self */ + continue; + } + if (e->allies[j]->cur_x == c->cur_x + dx && + e->allies[j]->cur_y == c->cur_y + dy) { + /* Another enemy currently in this direction */ + goto next_dir; + } + if (e->allies[j]->new_x == c->cur_x + dx && + e->allies[j]->new_y == c->cur_y + dy) { + /* Another enemy moving in this direction */ + goto next_dir; + } + } /* Move */ c->new_dir = dirs[i]; return 0; + next_dir: + continue; } return 0; -- cgit v0.9.1