2 * Code for one object to chase another
4 * @(#)chase.c 3.17 (Berkeley) 6/15/81
6 * Rogue: Exploring the Cavern of Cuties
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
10 * See the file LICENSE.TXT for full copyright and licensing information.
16 coord ch_ret
; /* Where chasing takes you */
20 * Make all the running monsters move.
25 register struct linked_list
*item
;
26 register struct thing
*tp
;
28 for (item
= mlist
; item
!= NULL
;)
30 tp
= (struct thing
*) ldata(item
);
32 if (off(*tp
, ISHELD
) && on(*tp
, ISRUN
))
34 if (off(*tp
, ISSLOW
) || tp
->t_turn
)
35 if (do_chase(tp
) == -1)
38 if (do_chase(tp
) == -1)
47 * Make one thing chase another.
51 register struct thing
*th
;
53 register struct room
*rer
, *ree
; /* room of chaser, room of chasee */
54 register int mindist
= 32767, i
, dist
;
55 register bool stoprun
= FALSE
; /* TRUE means we are there */
57 coord
this; /* Temporary destination for chaser */
59 rer
= roomin(&th
->t_pos
); /* Find room of chaser */
60 ree
= roomin(th
->t_dest
); /* Find room of chasee */
62 * We don't count doors as inside rooms for this routine
64 if (mvwinch(stdscr
, th
->t_pos
.y
, th
->t_pos
.x
) == DOOR
)
68 * If the object of our desire is in a different room,
69 * than we are and we ar not in a corridor, run to the
70 * door nearest to our goal.
72 if (rer
!= NULL
&& rer
!= ree
)
73 for (i
= 0; i
< rer
->r_nexits
; i
++) /* loop through doors */
75 dist
= DISTANCE(th
->t_dest
->y
, th
->t_dest
->x
,
76 rer
->r_exit
[i
].y
, rer
->r_exit
[i
].x
);
77 if (dist
< mindist
) /* minimize distance */
79 this = rer
->r_exit
[i
];
84 * this now contains what we want to run to this time
85 * so we run to it. If we hit it we either want to fight it
88 if (!chase(th
, &this))
94 else if (th
->t_type
!= 'F')
97 else if (th
->t_type
== 'F')
99 mvwaddch(cw
, th
->t_pos
.y
, th
->t_pos
.x
, th
->t_oldch
);
100 sch
= mvwinch(cw
, ch_ret
.y
, ch_ret
.x
);
101 if (rer
!= NULL
&& (rer
->r_flags
& ISDARK
) && sch
== FLOOR
102 && DISTANCE(ch_ret
.y
, ch_ret
.x
, th
->t_pos
.y
, th
->t_pos
.x
) < 3
103 && off(player
, ISBLIND
))
108 if (cansee(unc(ch_ret
)) && !on(*th
, ISINVIS
))
109 mvwaddch(cw
, ch_ret
.y
, ch_ret
.x
, th
->t_type
);
110 mvwaddch(mw
, th
->t_pos
.y
, th
->t_pos
.x
, ' ');
111 mvwaddch(mw
, ch_ret
.y
, ch_ret
.x
, th
->t_type
);
114 * And stop running if need be
116 if (stoprun
&& ce(th
->t_pos
, *(th
->t_dest
)))
117 th
->t_flags
&= ~ISRUN
;
124 * Set a mosnter running after something
125 * or stop it from running (for when it dies)
129 register coord
*runner
;
132 register struct linked_list
*item
;
133 register struct thing
*tp
;
136 * If we couldn't find him, something is funny
138 if ((item
= find_mons(runner
->y
, runner
->x
)) == NULL
)
140 msg("CHASER '%s'", unctrl(winat(runner
->y
, runner
->x
)));
143 tp
= (struct thing
*) ldata(item
);
145 * Start the beastie running
148 tp
->t_flags
|= ISRUN
;
149 tp
->t_flags
&= ~ISHELD
;
154 * Find the spot for the chaser(er) to move closer to the
155 * chasee(ee). Returns TRUE if we want to keep on chasing later
156 * FALSE if we reach the goal.
164 register int dist
, thisdist
;
165 register struct linked_list
*item
;
166 register struct object
*obj
;
167 register coord
*er
= &tp
->t_pos
;
171 * If the thing is confused, let it move randomly. Invisible
172 * Stalkers are slightly confused all of the time, and bats are
173 * quite confused all the time
175 if ((on(*tp
, ISHUH
) && rnd(10) < 8) || (tp
->t_type
== 'I' && rnd(100) < 20)
176 || (tp
->t_type
== 'B' && rnd(100) < 50))
179 * get a valid random move
181 ch_ret
= *rndmove(tp
);
182 dist
= DISTANCE(ch_ret
.y
, ch_ret
.x
, ee
->y
, ee
->x
);
184 * Small chance that it will become un-confused
187 tp
->t_flags
&= ~ISHUH
;
190 * Otherwise, find the empty spot next to the chaser that is
191 * closest to the chasee.
197 * This will eventually hold where we move to get closer
198 * If we can't find an empty spot, we stay where we are.
200 dist
= DISTANCE(er
->y
, er
->x
, ee
->y
, ee
->x
);
205 for (x
= er
->x
- 1; x
<= ex
; x
++)
206 for (y
= er
->y
- 1; y
<= ey
; y
++)
212 if (!diag_ok(er
, &tryp
))
218 * If it is a scroll, it might be a scare monster scroll
219 * so we need to look it up to see what type it is.
223 for (item
= lvl_obj
; item
!= NULL
; item
= next(item
))
225 obj
= (struct object
*) ldata(item
);
226 if (y
== obj
->o_pos
.y
&& x
== obj
->o_pos
.x
)
229 if (item
!= NULL
&& obj
->o_which
== S_SCARE
)
233 * If we didn't find any scrolls at this place or it
234 * wasn't a scare scroll, then this place counts
236 thisdist
= DISTANCE(y
, x
, ee
->y
, ee
->x
);
250 * Find what room some coordinates are in. NULL means they aren't
258 register struct room
*rp
;
260 for (rp
= rooms
; rp
<= &rooms
[MAXROOMS
-1]; rp
++)
268 * Find the monster from his corrdinates
276 register struct linked_list
*item
;
277 register struct thing
*th
;
279 for (item
= mlist
; item
!= NULL
; item
= next(item
))
281 th
= (struct thing
*) ldata(item
);
282 if (th
->t_pos
.y
== y
&& th
->t_pos
.x
== x
)
290 * Check to see if the move is legal if it is diagonal
294 register coord
*sp
, *ep
;
296 if (ep
->x
== sp
->x
|| ep
->y
== sp
->y
)
298 return (step_ok(mvinch(ep
->y
, sp
->x
)) && step_ok(mvinch(sp
->y
, ep
->x
)));
303 * returns true if the hero can see a certain coordinate.
309 register struct room
*rer
;
312 if (on(player
, ISBLIND
))
318 * We can only see if the hero in the same room as
319 * the coordinate and the room is lit or if it is close.
321 return (rer
!= NULL
&& rer
== roomin(&hero
) && !(rer
->r_flags
&ISDARK
)) ||
322 DISTANCE(y
, x
, hero
.y
, hero
.x
) < 3;