-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHASE.C
429 lines (404 loc) · 10.6 KB
/
CHASE.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Code for one creature to chase another
*
* chase.c 1.32 (A.I. Design) 12/12/84
*/
#include "rogue.h"
#include "curses.h"
#define DRAGONSHOT 5 /* one chance in DRAGONSHOT that a dragon will flame */
coord ch_ret; /* Where chasing takes you */
/*
* runners:
* Make all the running monsters move.
*/
runners()
{
register THING *tp;
register int dist;
for (tp = mlist; tp != NULL; tp = next(tp)) {
if (!on(*tp, ISHELD) && on(*tp, ISRUN)) {
dist = DISTANCE(hero.y, hero.x, tp->t_pos.y, tp->t_pos.x);
if (!(on(*tp, ISSLOW) || (tp->t_type == 'S' && dist > 3)) || tp->t_turn)
do_chase(tp);
if (on(*tp, ISHASTE))
do_chase(tp);
dist = DISTANCE(hero.y, hero.x, tp->t_pos.y, tp->t_pos.x);
if (on(*tp, ISFLY) && dist > 3)
do_chase(tp);
tp->t_turn ^= TRUE;
}
}
}
/*
* do_chase:
* Make one thing chase another.
*/
do_chase(th)
THING *th;
{
int mindist = 32767, i, dist;
byte sch;
bool door;
register THING *obj;
struct room *oroom;
register struct room *rer, *ree; /* room of chaser, room of chasee */
coord this; /* Temporary destination for chaser */
rer = th->t_room; /* Find room of chaser */
if (on(*th, ISGREED) && rer->r_goldval == 0)
th->t_dest = &hero; /* If gold has been taken, run after hero */
ree = proom;
if (th->t_dest != &hero) /* Find room of chasee */
ree = roomin(th->t_dest);
if (ree == NULL)
return;
/*
* We don't count doors as inside rooms for this routine
*/
door = (chat(th->t_pos.y, th->t_pos.x) == DOOR);
/*
* If the object of our desire is in a different room,
* and we are not in a maze, run to the door nearest to
* our goal.
*/
over:
if (rer != ree && (rer->r_flags & ISMAZE) == 0)
{
for (i = 0; i < rer->r_nexits; i++) { /* loop through doors */
dist = DISTANCE(th->t_dest->y, th->t_dest->x,rer->r_exit[i].y, rer->r_exit[i].x);
if (dist < mindist) {
this = rer->r_exit[i];
mindist = dist;
}
}
if (door) {
rer = &passages[flat(th->t_pos.y, th->t_pos.x) & F_PNUM];
door = FALSE;
goto over;
}
} else {
this = *th->t_dest;
/*
* For monsters which can fire bolts at the poor hero, we check to
* see if (a) the hero in on a straight line from it, and (b) that
* it is within shooting distance, but outside of striking range.
*/
if ((th->t_type == 'D' || th->t_type == 'I')
&& (th->t_pos.y == hero.y || th->t_pos.x == hero.x
|| abs(th->t_pos.y - hero.y) == abs(th->t_pos.x - hero.x))
&& ((dist=DISTANCE(th->t_pos.y, th->t_pos.x, hero.y, hero.x)) > 2
&& dist <= BOLT_LENGTH * BOLT_LENGTH)
&& !on(*th, ISCANC) && rnd(DRAGONSHOT) == 0)
{
running = FALSE;
delta.y = sign(hero.y - th->t_pos.y);
delta.x = sign(hero.x - th->t_pos.x);
fire_bolt(&th->t_pos,&delta,th->t_type == 'D' ? "flame" : "frost");
return;
}
}
/*
* This now contains what we want to run to this time
* so we run to it. If we hit it we either want to fight it
* or stop running
*/
chase(th, &this);
if (ce(ch_ret, hero)) {
attack(th);
return;
} else if (ce(ch_ret, *th->t_dest)) {
for (obj = lvl_obj; obj != NULL; obj = next(obj))
if (th->t_dest == &obj->o_pos) {
byte oldchar;
detach(lvl_obj, obj);
attach(th->t_pack, obj);
oldchar = chat(obj->o_pos.y, obj->o_pos.x) =
(th->t_room->r_flags & ISGONE) ? PASSAGE : FLOOR;
if (cansee(obj->o_pos.y, obj->o_pos.x))
mvaddch(obj->o_pos.y, obj->o_pos.x, oldchar);
th->t_dest = find_dest(th);
break;
}
}
if (th->t_type == 'F')
return;
/*
* If the chasing thing moved, update the screen
*/
if (th->t_oldch != '@') {
if (th->t_oldch == ' ' && cansee(th->t_pos.y, th->t_pos.x)
&& _level[INDEX(th->t_pos.y,th->t_pos.x)] == FLOOR)
mvaddch(th->t_pos.y, th->t_pos.x, FLOOR);
else if (th->t_oldch == FLOOR && !cansee(th->t_pos.y, th->t_pos.x)
&& !on(player, SEEMONST))
mvaddch(th->t_pos.y, th->t_pos.x, ' ');
else
mvaddch(th->t_pos.y, th->t_pos.x, th->t_oldch);
}
oroom = th->t_room;
if (!ce(ch_ret, th->t_pos))
{
if ((th->t_room = roomin(&ch_ret)) == NULL) {
th->t_room = oroom;
return;
}
if (oroom != th->t_room)
th->t_dest = find_dest(th);
th->t_pos = ch_ret;
}
if (see_monst(th)) {
if (flat(ch_ret.y,ch_ret.x) & F_PASS)
standout();
th->t_oldch = mvinch(ch_ret.y, ch_ret.x);
mvaddch(ch_ret.y, ch_ret.x, th->t_disguise);
}
else if (on(player, SEEMONST))
{
standout();
th->t_oldch = mvinch(ch_ret.y, ch_ret.x);
mvaddch(ch_ret.y, ch_ret.x, th->t_type);
}
else
th->t_oldch = '@';
if (th->t_oldch == FLOOR && oroom->r_flags & ISDARK)
th->t_oldch = ' ';
standend();
}
/*
* see_monst:
* Return TRUE if the hero can see the monster
*/
see_monst(mp)
register THING *mp;
{
if (on(player, ISBLIND))
return FALSE;
if (on(*mp, ISINVIS) && !on(player, CANSEE))
return FALSE;
if (DISTANCE(mp->t_pos.y, mp->t_pos.x, hero.y, hero.x) >= LAMPDIST &&
((mp->t_room != proom || (mp->t_room->r_flags & ISDARK) ||
(mp->t_room->r_flags & ISMAZE))))
return FALSE;
/*
* If we are seeing the enemy of a vorpally enchanted weapon for the first
* time, give the player a hint as to what that weapon is good for.
*/
if (cur_weapon != NULL && mp->t_type == cur_weapon->o_enemy
&& ((cur_weapon->o_flags & DIDFLASH) == 0))
{
cur_weapon->o_flags |= DIDFLASH;
msg(flash, w_names[cur_weapon->o_which], terse || expert ? "" : intense);
}
return TRUE;
}
/*
* start_run:
* Set a monster running after something or stop it from running
* (for when it dies)
*/
start_run(runner)
register coord *runner;
{
register THING *tp;
/*
* If we couldn't find him, something is funny
*/
tp = moat(runner->y, runner->x);
if (tp != NULL) {
/*
* Start the beastie running
*/
tp->t_flags |= ISRUN;
tp->t_flags &= ~ISHELD;
tp->t_dest = find_dest(tp);
}
#ifdef DEBUG
else
debug("start_run: moat == NULL ???");
#endif DEBUG
}
/*
* chase:
* Find the spot for the chaser(er) to move closer to the
* chasee(ee). Returns TRUE if we want to keep on chasing later
* FALSE if we reach the goal.
*/
chase(tp, ee)
THING *tp;
coord *ee;
{
register int x, y;
int dist, thisdist;
register THING *obj;
coord *er;
byte ch;
int plcnt = 1;
er = &tp->t_pos;
/*
* If the thing is confused, let it move randomly. Phantoms
* are slightly confused all of the time, and bats are
* quite confused all the time
*/
if ((on(*tp, ISHUH) && rnd(5) != 0) || (tp->t_type == 'P' && rnd(5) == 0)
|| (tp->t_type == 'B' && rnd(2) == 0))
{
/*
* get a valid random move
*/
rndmove(tp,&ch_ret);
dist = DISTANCE(ch_ret.y, ch_ret.x, ee->y, ee->x);
/*
* Small chance that it will become un-confused
*/
if (rnd(30) == 17)
tp->t_flags &= ~ISHUH;
}
/*
* Otherwise, find the empty spot next to the chaser that is
* closest to the chasee.
*/
else
{
register int ey, ex;
/*
* This will eventually hold where we move to get closer
* If we can't find an empty spot, we stay where we are.
*/
dist = DISTANCE(er->y, er->x, ee->y, ee->x);
ch_ret = *er;
ey = er->y + 1;
ex = er->x + 1;
for (x = er->x - 1; x <= ex; x++)
{
for (y = er->y - 1; y <= ey; y++)
{
coord tryp;
tryp.x = x;
tryp.y = y;
if (offmap(y, x) || !diag_ok(er, &tryp))
continue;
ch = winat(y, x);
if (step_ok(ch))
{
/*
* If it is a scroll, it might be a scare monster scroll
* so we need to look it up to see what type it is.
*/
if (ch == SCROLL)
{
for (obj = lvl_obj; obj != NULL; obj = next(obj))
{
if (y == obj->o_pos.y && x == obj->o_pos.x)
break;
}
if (obj != NULL && obj->o_which == S_SCARE)
continue;
}
/*
* If we didn't find any scrolls at this place or it
* wasn't a scare scroll, then this place counts
*/
thisdist = DISTANCE(y, x, ee->y, ee->x);
if (thisdist < dist)
{
plcnt = 1;
ch_ret = tryp;
dist = thisdist;
}
else if (thisdist == dist && rnd(++plcnt) == 0)
{
ch_ret = tryp;
dist = thisdist;
}
}
}
}
}
}
/*
* roomin:
* Find what room some coordinates are in. NULL means they aren't
* in any room.
*/
struct room *
roomin(cp)
register coord *cp;
{
register struct room *rp;
register byte *fp;
for (rp = rooms; rp <= &rooms[MAXROOMS-1]; rp++)
if (cp->x < rp->r_pos.x + rp->r_max.x && rp->r_pos.x <= cp->x
&& cp->y < rp->r_pos.y + rp->r_max.y && rp->r_pos.y <= cp->y)
return rp;
fp = &flat(cp->y, cp->x);
if (*fp & F_PASS)
return &passages[*fp & F_PNUM];
#ifdef DEBUG
debug("in some bizarre place (%d, %d)", unc(*cp));
#endif DEBUG
bailout++;
return NULL;
}
/*
* diag_ok:
* Check to see if the move is legal if it is diagonal
*/
diag_ok(sp, ep)
register coord *sp, *ep;
{
if (ep->x == sp->x || ep->y == sp->y)
return TRUE;
return (step_ok(chat(ep->y, sp->x)) && step_ok(chat(sp->y, ep->x)));
}
/*
* cansee:
* Returns true if the hero can see a certain coordinate.
*/
cansee(y, x)
register int y, x;
{
register struct room *rer;
coord tp;
if (on(player, ISBLIND))
return FALSE;
if (DISTANCE(y, x, hero.y, hero.x) < LAMPDIST)
return TRUE;
/*
* We can only see if the hero in the same room as
* the coordinate and the room is lit or if it is close.
*/
tp.y = y;
tp.x = x;
rer = roomin(&tp);
return (rer == proom && !(rer->r_flags & ISDARK));
}
/*
* find_dest:
* find the proper destination for the monster
*/
coord *
find_dest(tp)
register THING *tp;
{
register THING *obj;
register register int prob;
register struct room *rp;
if ((prob = monsters[tp->t_type - 'A'].m_carry) <= 0 || tp->t_room == proom
|| see_monst(tp))
return &hero;
rp = tp->t_room;
for (obj = lvl_obj; obj != NULL; obj = next(obj))
{
if (obj->o_type == SCROLL && obj->o_which == S_SCARE)
continue;
if (roomin(&obj->o_pos) == rp && rnd(100) < prob)
{
for (tp = mlist; tp != NULL; tp = next(tp))
if (tp->t_dest == &obj->o_pos)
break;
if (tp == NULL)
return &obj->o_pos;
}
}
return &hero;
}