Tweak liking thresholds. Required interest rate is higher to discourage flirt grinding.
[rogue-pphs.git] / chase.c
1 /*
2 * Code for one object to chase another
3 *
4 * @(#)chase.c 3.17 (Berkeley) 6/15/81
5 *
6 * Rogue: Exploring the Cavern of Cuties
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include "curses.h"
14 #include "rogue.h"
15
16 coord ch_ret; /* Where chasing takes you */
17
18 /*
19 * runners:
20 * Make all the running monsters move.
21 */
22
23 runners()
24 {
25 register struct linked_list *item;
26 register struct thing *tp;
27
28 for (item = mlist; item != NULL;)
29 {
30 tp = (struct thing *) ldata(item);
31 item = next(item);
32 if (off(*tp, ISHELD) && on(*tp, ISRUN))
33 {
34 if (off(*tp, ISSLOW) || tp->t_turn)
35 if (do_chase(tp) == -1)
36 continue;
37 if (on(*tp, ISHASTE))
38 if (do_chase(tp) == -1)
39 continue;
40 tp->t_turn ^= TRUE;
41 }
42 }
43 }
44
45 /*
46 * do_chase:
47 * Make one thing chase another.
48 */
49
50 do_chase(th)
51 register struct thing *th;
52 {
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 */
56 register char sch;
57 coord this; /* Temporary destination for chaser */
58
59 rer = roomin(&th->t_pos); /* Find room of chaser */
60 ree = roomin(th->t_dest); /* Find room of chasee */
61 /*
62 * We don't count doors as inside rooms for this routine
63 */
64 if (mvwinch(stdscr, th->t_pos.y, th->t_pos.x) == DOOR)
65 rer = NULL;
66 this = *th->t_dest;
67 /*
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.
71 */
72 if (rer != NULL && rer != ree)
73 for (i = 0; i < rer->r_nexits; i++) /* loop through doors */
74 {
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 */
78 {
79 this = rer->r_exit[i];
80 mindist = dist;
81 }
82 }
83 /*
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
86 * or stop running
87 */
88 if (!chase(th, &this))
89 {
90 if (ce(this, hero))
91 {
92 return( attack(th) );
93 }
94 else if (th->t_type != 'F')
95 stoprun = TRUE;
96 }
97 else if (th->t_type == 'F')
98 return(0);
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))
104 th->t_oldch = ' ';
105 else
106 th->t_oldch = sch;
107
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);
112 th->t_pos = ch_ret;
113 /*
114 * And stop running if need be
115 */
116 if (stoprun && ce(th->t_pos, *(th->t_dest)))
117 th->t_flags &= ~ISRUN;
118
119 return(0);
120 }
121
122 /*
123 * runto:
124 * Set a mosnter running after something
125 * or stop it from running (for when it dies)
126 */
127
128 runto(runner, spot)
129 register coord *runner;
130 coord *spot;
131 {
132 register struct linked_list *item;
133 register struct thing *tp;
134
135 /*
136 * If we couldn't find him, something is funny
137 */
138 if ((item = find_mons(runner->y, runner->x)) == NULL)
139 {
140 msg("CHASER '%s'", unctrl(winat(runner->y, runner->x)));
141 return;
142 }
143 tp = (struct thing *) ldata(item);
144 /*
145 * Start the beastie running
146 */
147 tp->t_dest = spot;
148 tp->t_flags |= ISRUN;
149 tp->t_flags &= ~ISHELD;
150 }
151
152 /*
153 * chase:
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.
157 */
158
159 chase(tp, ee)
160 struct thing *tp;
161 coord *ee;
162 {
163 register int x, y;
164 register int dist, thisdist;
165 register struct linked_list *item;
166 register struct object *obj;
167 register coord *er = &tp->t_pos;
168 register char ch;
169
170 /*
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
174 */
175 if ((on(*tp, ISHUH) && rnd(10) < 8) || (tp->t_type == 'I' && rnd(100) < 20)
176 || (tp->t_type == 'B' && rnd(100) < 50))
177 {
178 /*
179 * get a valid random move
180 */
181 ch_ret = *rndmove(tp);
182 dist = DISTANCE(ch_ret.y, ch_ret.x, ee->y, ee->x);
183 /*
184 * Small chance that it will become un-confused
185 */
186 if (rnd(1000) < 50)
187 tp->t_flags &= ~ISHUH;
188 }
189 /*
190 * Otherwise, find the empty spot next to the chaser that is
191 * closest to the chasee.
192 */
193 else
194 {
195 register int ey, ex;
196 /*
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.
199 */
200 dist = DISTANCE(er->y, er->x, ee->y, ee->x);
201 ch_ret = *er;
202
203 ey = er->y + 1;
204 ex = er->x + 1;
205 for (x = er->x - 1; x <= ex; x++)
206 for (y = er->y - 1; y <= ey; y++)
207 {
208 coord tryp;
209
210 tryp.x = x;
211 tryp.y = y;
212 if (!diag_ok(er, &tryp))
213 continue;
214 ch = winat(y, x);
215 if (step_ok(ch))
216 {
217 /*
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.
220 */
221 if (ch == SCROLL)
222 {
223 for (item = lvl_obj; item != NULL; item = next(item))
224 {
225 obj = (struct object *) ldata(item);
226 if (y == obj->o_pos.y && x == obj->o_pos.x)
227 break;
228 }
229 if (item != NULL && obj->o_which == S_SCARE)
230 continue;
231 }
232 /*
233 * If we didn't find any scrolls at this place or it
234 * wasn't a scare scroll, then this place counts
235 */
236 thisdist = DISTANCE(y, x, ee->y, ee->x);
237 if (thisdist < dist)
238 {
239 ch_ret = tryp;
240 dist = thisdist;
241 }
242 }
243 }
244 }
245 return (dist != 0);
246 }
247
248 /*
249 * roomin:
250 * Find what room some coordinates are in. NULL means they aren't
251 * in any room.
252 */
253
254 struct room *
255 roomin(cp)
256 register coord *cp;
257 {
258 register struct room *rp;
259
260 for (rp = rooms; rp <= &rooms[MAXROOMS-1]; rp++)
261 if (inroom(rp, cp))
262 return rp;
263 return NULL;
264 }
265
266 /*
267 * find_mons:
268 * Find the monster from his corrdinates
269 */
270
271 struct linked_list *
272 find_mons(y, x)
273 register int y;
274 int x;
275 {
276 register struct linked_list *item;
277 register struct thing *th;
278
279 for (item = mlist; item != NULL; item = next(item))
280 {
281 th = (struct thing *) ldata(item);
282 if (th->t_pos.y == y && th->t_pos.x == x)
283 return item;
284 }
285 return NULL;
286 }
287
288 /*
289 * diag_ok:
290 * Check to see if the move is legal if it is diagonal
291 */
292
293 diag_ok(sp, ep)
294 register coord *sp, *ep;
295 {
296 if (ep->x == sp->x || ep->y == sp->y)
297 return TRUE;
298 return (step_ok(mvinch(ep->y, sp->x)) && step_ok(mvinch(sp->y, ep->x)));
299 }
300
301 /*
302 * cansee:
303 * returns true if the hero can see a certain coordinate.
304 */
305
306 cansee(y, x)
307 register int y, x;
308 {
309 register struct room *rer;
310 coord tp;
311
312 if (on(player, ISBLIND))
313 return FALSE;
314 tp.y = y;
315 tp.x = x;
316 rer = roomin(&tp);
317 /*
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.
320 */
321 return (rer != NULL && rer == roomin(&hero) && !(rer->r_flags&ISDARK)) ||
322 DISTANCE(y, x, hero.y, hero.x) < 3;
323 }