diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-19 15:18:43 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-19 15:18:43 (EDT) |
commit | 483d5cf42b38828e152561817769bd4155612c99 (patch) | |
tree | 65b4ef92f2d54166b51df1d59c548c26f9f94f87 /src/char | |
parent | 7a5f3b698b943423f2b365c31ca8582b7b9d8035 (diff) | |
download | mazefight-483d5cf42b38828e152561817769bd4155612c99.zip mazefight-483d5cf42b38828e152561817769bd4155612c99.tar.gz mazefight-483d5cf42b38828e152561817769bd4155612c99.tar.bz2 |
char/enemy: Add collision avoidance
Diffstat (limited to 'src/char')
-rw-r--r-- | src/char/enemy.c | 19 |
1 files changed, 19 insertions, 0 deletions
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; |