From 94c1e94d2002953ae898a8d88e02e8cd76d45bdc Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Sat, 16 Jul 2022 18:43:22 +0300 Subject: [PATCH] Move mtrack push and clear to separate functions --- include/extern.h | 2 ++ src/dog.c | 2 +- src/dogmove.c | 5 +---- src/makemon.c | 2 +- src/mon.c | 5 ++--- src/monmove.c | 27 ++++++++++++++++++++------- src/teleport.c | 2 +- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/include/extern.h b/include/extern.h index 604ac1ca6..c3adf532c 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1641,6 +1641,8 @@ extern boolean resist_conflict(struct monst *); extern boolean itsstuck(struct monst *); extern boolean mb_trapped(struct monst *, boolean); +extern void mon_track_add(struct monst *, coordxy, coordxy); +extern void mon_track_clear(struct monst *); extern boolean monhaskey(struct monst *, boolean); extern void mon_regen(struct monst *, boolean); extern int dochugw(struct monst *, boolean); diff --git a/src/dog.c b/src/dog.c index 64b06b8c0..9ed76097e 100644 --- a/src/dog.c +++ b/src/dog.c @@ -357,7 +357,7 @@ mon_arrive(struct monst *mtmp, int when) ylocale = mtmp->mtrack[1].y; fromdlev.dnum = mtmp->mtrack[2].x; fromdlev.dlevel = mtmp->mtrack[2].y; - memset(mtmp->mtrack, 0, sizeof mtmp->mtrack); + mon_track_clear(mtmp); if (mtmp == u.usteed) return; /* don't place steed on the map */ diff --git a/src/dogmove.c b/src/dogmove.c index b774490d5..8b746c7a4 100644 --- a/src/dogmove.c +++ b/src/dogmove.c @@ -1278,10 +1278,7 @@ dog_move(register struct monst *mtmp, pline("%s %s reluctantly over %s.", noit_Monnam(mtmp), vtense((char *) 0, locomotion(mtmp->data, "step")), what); } - for (j = MTSZ - 1; j > 0; j--) - mtmp->mtrack[j] = mtmp->mtrack[j - 1]; - mtmp->mtrack[0].x = omx; - mtmp->mtrack[0].y = omy; + mon_track_add(mtmp, omx, omy); /* We have to know if the pet's going to do a combined eat and * move before moving it, but it can't eat until after being * moved. Thus the do_eat flag. diff --git a/src/makemon.c b/src/makemon.c index 531a9b99e..de0e77297 100644 --- a/src/makemon.c +++ b/src/makemon.c @@ -871,7 +871,7 @@ clone_mon(struct monst *mon, /* ms->isminion handled below */ /* clone shouldn't be reluctant to move on spots 'parent' just moved on */ - (void) memset((genericptr_t) m2->mtrack, 0, sizeof m2->mtrack); + mon_track_clear(m2); place_monster(m2, m2->mx, m2->my); if (emits_light(m2->data)) diff --git a/src/mon.c b/src/mon.c index 517054ff4..d7f6e9c8b 100644 --- a/src/mon.c +++ b/src/mon.c @@ -3788,9 +3788,8 @@ wake_nearto(coordxy x, coordxy y, int distance) if (mtmp->mtame) { if (!mtmp->isminion) EDOG(mtmp)->whistletime = g.moves; - /* Clear mtrack. This is to fix up a pet who is - stuck "fleeing" its master. */ - memset(mtmp->mtrack, 0, sizeof mtmp->mtrack); + /* Fix up a pet who is stuck "fleeing" its master */ + mon_track_clear(mtmp); } } } diff --git a/src/monmove.c b/src/monmove.c index cb9eec9e6..c2c8f3478 100644 --- a/src/monmove.c +++ b/src/monmove.c @@ -48,6 +48,24 @@ mb_trapped(struct monst *mtmp, boolean canseeit) return FALSE; } +/* push coordinate x,y to mtrack, making monster remember where it was */ +void +mon_track_add(struct monst *mtmp, coordxy x, coordxy y) +{ + int j; + + for (j = MTSZ - 1; j > 0; j--) + mtmp->mtrack[j] = mtmp->mtrack[j - 1]; + mtmp->mtrack[0].x = x; + mtmp->mtrack[0].y = y; +} + +void +mon_track_clear(struct monst *mtmp) +{ + memset(mtmp->mtrack, 0, sizeof(mtmp->mtrack)); +} + /* check whether a monster is carrying a locking/unlocking tool */ boolean monhaskey( @@ -394,7 +412,7 @@ monflee( mtmp->mflee = 1; } /* ignore recently-stepped spaces when made to flee */ - memset(mtmp->mtrack, 0, sizeof(mtmp->mtrack)); + mon_track_clear(mtmp); } static void @@ -1373,8 +1391,6 @@ m_move(register struct monst* mtmp, register int after) } if (mmoved) { - register int j; - if (mmoved == MMOVE_MOVED && (u.ux != nix || u.uy != niy) && itsstuck(mtmp)) return MMOVE_DONE; @@ -1439,10 +1455,7 @@ m_move(register struct monst* mtmp, register int after) maybe_unhide_at(mtmp->mx, mtmp->my); - for (j = MTSZ - 1; j > 0; j--) - mtmp->mtrack[j] = mtmp->mtrack[j - 1]; - mtmp->mtrack[0].x = omx; - mtmp->mtrack[0].y = omy; + mon_track_add(mtmp, omx, omy); } else { if (is_unicorn(ptr) && rn2(2) && !tele_restrict(mtmp)) { (void) rloc(mtmp, RLOC_MSG); diff --git a/src/teleport.c b/src/teleport.c index 35f9cd09c..4374d0e3c 100644 --- a/src/teleport.c +++ b/src/teleport.c @@ -1313,7 +1313,7 @@ rloc_to_core( } } - memset(mtmp->mtrack, 0, sizeof mtmp->mtrack); + mon_track_clear(mtmp); place_monster(mtmp, x, y); /* put monster down */ update_monster_region(mtmp); -- 2.50.1