-
Notifications
You must be signed in to change notification settings - Fork 3
/
movem.c
598 lines (533 loc) · 18.4 KB
/
movem.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* movem.c (move monster)
*
* movemonst() Routine to move the monsters toward the player
* build_proximity_ripple() Build proximity ripple for smart monster move
* move_scared() Move scared monsters
* move_smart() Move smart monsters
* move_dumb() Move dumb monsters
* mmove(x,y,xd,yd) Function to actually perform the monster movement
*/
#include <stdlib.h>
#include "larncons.h"
#include "larndata.h"
#include "larnfunc.h"
static void build_proximity_ripple(void);
static void move_scared(int, int);
static void move_smart(int, int);
static void move_dumb(int, int);
static void mmove(int, int, int, int);
#if 0
# define IDISTNORM 8 /* was 17 - dgk */
# define IDISTAGGR 20 /* was 40 - dgk */
#endif
# define IDISTNORM 17 /* was 17 - dgk */
# define IDISTAGGR 40 /* was 40 - dgk */
static short w1x[9],w1y[9];
static int tmp1,tmp2,tmp3,tmp4,distance;
/* list of monsters to move */
static struct foo { char x ; char y; char smart; } movelist[250] ;
/*
* movemonst() Routine to move the monsters toward the player
*
* This routine has the responsibility to determine which monsters are to
* move, and call movemt() to do the move.
* Returns no value.
*/
void movemonst(void)
{
int i, j, movecnt=0, smart_count, min_int;
if (c[HOLDMONST]) return; /* no action if monsters are held */
if (c[AGGRAVATE]) /* determine window of monsters to move */
{
tmp1=playery-5; tmp2=playery+6; tmp3=playerx-10; tmp4=playerx+11;
distance=IDISTAGGR; /* depth of intelligent monster movement */
}
else
{
tmp1=playery-3; tmp2=playery+4; tmp3=playerx-5; tmp4=playerx+6;
distance=IDISTNORM; /* depth of intelligent monster movement */
}
if (level == 0) /* if on outside level monsters can move in perimeter */
{
if (tmp1 < 0) tmp1=0; if (tmp2 > MAXY) tmp2=MAXY;
if (tmp3 < 0) tmp3=0; if (tmp4 > MAXX) tmp4=MAXX;
}
else /* if in a dungeon monsters can't be on the perimeter (wall there) */
{
if (tmp1 < 1) tmp1=1; if (tmp2 > MAXY-1) tmp2=MAXY-1;
if (tmp3 < 1) tmp3=1; if (tmp4 > MAXX-1) tmp4=MAXX-1;
}
/* We now have a window in which to move monsters. First find all
monsters in the window, then decide whether or not to move them.
Its faster that way since the size of the window is usually larger
than the # of monsters in that window.
Find all monsters in the window. The only time a monster cannot
move is if: monsters are not aggrevated, AND player is stealthed,
AND the monster is asleep due to stealth. Split into two
separate loops in order to simplify the if statement inside the
loop for the most common case.
Also count # of smart monsters.
*/
smart_count = 0 ;
min_int = 10 - c[HARDGAME] ; /* minimum monster intelligence to move smart */
if ( c[AGGRAVATE] || !c[STEALTH] )
{
for ( j = tmp1 ; j < tmp2 ; j++ )
for ( i = tmp3 ; i < tmp4 ; i++ )
if (mitem[i][j])
{
movelist[movecnt].x = i;
movelist[movecnt].y = j ;
if ( monster[mitem[i][j]].intelligence > min_int )
{
movelist[movecnt].smart = TRUE ;
smart_count++;
}
else
movelist[movecnt].smart = FALSE ;
movecnt++;
}
}
else
{
for ( j = tmp1; j < tmp2 ; j++ )
for ( i = tmp3 ; i < tmp4 ; i++ )
if ( mitem[i][j] && stealth[i][j] ) /* stealth[x][y] = 1 when AWAKE! */
{
movelist[movecnt].x = i;
movelist[movecnt].y = j ;
if ( monster[mitem[i][j]].intelligence > min_int )
{
movelist[movecnt].smart = TRUE ;
smart_count++;
}
else
movelist[movecnt].smart = FALSE ;
movecnt++;
}
}
/* now move the monsters in the movelist. If we have at least one
smart monster, build a proximity ripple and use it for all smart
monster movement.
*/
if (movecnt > 0 )
{
if ( c[SCAREMONST] )
for ( i = 0 ; i < movecnt ; i++ )
move_scared( movelist[i].x, movelist[i].y );
else
{
if ( smart_count > 0 )
{
/* I was going to put in code that prevented the rebuilding
of the proximity ripple if the player had not moved since
the last turn. Unfortunately, this permits the player to
blast down doors to treasure rooms and not have a single
intelligent monster move.
*/
build_proximity_ripple();
for ( i = 0 ; i < movecnt ; i++ )
if ( movelist[i].smart )
move_smart( movelist[i].x, movelist[i].y );
else
move_dumb( movelist[i].x, movelist[i].y );
}
else
for ( i = 0 ; i < movecnt ; i++ )
move_dumb( movelist[i].x, movelist[i].y );
}
}
/* Also check for the last monster hit. This is necessary to prevent
the player from getting free hits on a monster with long range
spells or when stealthed.
*/
if ( c[AGGRAVATE] || !c[STEALTH] )
{
/* If the last monster hit is within the move window, its already
been moved.
*/
if ( ( ( lasthx < tmp3 || lasthx >= tmp4 ) ||
( lasthy < tmp1 || lasthy >= tmp2 ) ) &&
mitem[lasthx][lasthy] )
{
if ( c[SCAREMONST] )
move_scared( lasthx, lasthy );
else
if ( monster[mitem[lasthx][lasthy]].intelligence > min_int )
{
if ( smart_count == 0 )
build_proximity_ripple( );
move_smart( lasthx, lasthy );
}
else
move_dumb( lasthx, lasthy );
lasthx = w1x[0]; /* make sure the monster gets moved again */
lasthy = w1y[0];
}
}
else
{
/* If the last monster hit is within the move window, and not
asleep due to stealth, then it has already been moved.
Otherwise (monster outside window, asleep due to stealth),
move the monster and update the lasthit x,y position.
*/
if ( ( lasthx < tmp3 || lasthx >= tmp4 ) ||
( lasthy < tmp1 || lasthy >= tmp2 ) &&
mitem[lasthx][lasthy] || !stealth[lasthx][lasthy] )
{
if ( c[SCAREMONST] )
move_scared( lasthx, lasthy );
else
if ( monster[mitem[lasthx][lasthy]].intelligence > min_int )
{
if ( smart_count == 0 )
build_proximity_ripple( );
move_smart( lasthx, lasthy );
}
else
move_dumb( lasthx, lasthy );
lasthx = w1x[0]; /* make sure the monster gets moved again */
lasthy = w1y[0];
}
}
}
static signed char screen[MAXX][MAXY]; /* proximity ripple storage */
/* queue for breadth-first 'search' build of proximity ripple.
*/
#define MAX_QUEUE 100
static struct queue_entry
{
char x ;
char y ;
char distance ;
} queue[MAX_QUEUE];
static int queue_head = 0 ;
static int queue_tail = 0 ;
/* put a location on the proximity ripple queue
*/
#define PUTQUEUE( _x, _y, _d ) \
{ \
queue[queue_tail].x = (_x) ; \
queue[queue_tail].y = (_y) ; \
queue[queue_tail].distance = (_d); \
queue_tail++; \
if (queue_tail == MAX_QUEUE) \
queue_tail = 0 ; \
}
/* take a location from the proximity ripple queue
*/
#define GETQUEUE( _x, _y, _d ) \
{ \
(_x) = queue[queue_head].x ; \
(_y) = queue[queue_head].y ; \
(_d) = queue[queue_head].distance ; \
queue_head++; \
if (queue_head == MAX_QUEUE) \
queue_head = 0 ; \
}
/* check for the proximity ripple queue being empty
*/
#define QUEUEEMPTY() (queue_head == queue_tail)
/*
For smart monster movement, build a proximity ripple from the player's
position, out to a 'distance' of 20. For example:
W 5 4 4 W W X Player is at position marked 1
W 5 W 3 3 W W W is a wall. Monsters will attempt
W 6 W 2 W 4 W to move to a location with a smaller
W 7 W 1 W 5 W value than their current position.
W 8 W W W 6 W Note that a monster at location X
W 9 9 8 7 7 7 will not move at all.
W W W 8 W W W
*/
static void build_proximity_ripple(void)
{
int xl, yl, xh, yh ;
int k, m, z, tmpx, tmpy;
int curx, cury, curdist;
xl=tmp3-2; yl=tmp1-2; xh=tmp4+2; yh=tmp2+2;
vxy(&xl,&yl); vxy(&xh,&yh);
for (k=yl; k<=yh; k++)
for (m=xl; m<=xh; m++)
{
switch(item[m][k])
{
case OWALL:
case OPIT:
case OTRAPARROW:
case ODARTRAP:
case OCLOSEDDOOR:
case OTRAPDOOR:
case OTELEPORTER:
screen[m][k]=127;
break;
case OENTRANCE:
if (level==1)
screen[m][k] = 127;
else
screen[m][k] = 0;
break;
default:
screen[m][k] = 0;
break;
};
}
screen[playerx][playery]=1;
/* now perform proximity ripple from playerx,playery to monster */
xl=tmp3-1; yl=tmp1-1; xh=tmp4+1; yh=tmp2+1;
vxy(&xl,&yl); vxy(&xh,&yh);
PUTQUEUE( playerx, playery, 1 );
do
{
GETQUEUE( curx, cury, curdist );
/* test all spots around the current one being looked at.
*/
if ( ( curx >= xl && curx <= xh ) &&
( cury >= yl && cury <= yh ) )
{
for (z=1; z<9; z++)
{
tmpx = curx + diroffx[z] ;
tmpy = cury + diroffy[z] ;
vxy( &tmpx, &tmpy );
if (screen[tmpx][tmpy] == 0 )
{
screen[tmpx][tmpy] = curdist + 1;
PUTQUEUE( tmpx, tmpy, curdist + 1 );
}
}
}
}
while (!QUEUEEMPTY());
}
/*
Move scared monsters randomly away from the player position.
*/
static void move_scared(int i, int j)
{
int xl, yl, tmp;
/* check for a half-speed monster, and check if not to move. Could be
done in the monster list build.
*/
switch(mitem[i][j])
{
case TROGLODYTE: case HOBGOBLIN: case METAMORPH: case XVART:
case INVISIBLESTALKER: case ICELIZARD: if ((gtime & 1) == 1) return;
};
if ((xl = i+rnd(3)-2) < 0)
xl=0;
if (xl >= MAXX)
xl=MAXX-1;
if ((yl = j+rnd(3)-2) < 0)
yl=0;
if (yl >= MAXY)
yl=MAXY-1;
if ((tmp=item[xl][yl]) != OWALL)
if (mitem[xl][yl] == 0)
if ((mitem[i][j] != VAMPIRE) || (tmp != OMIRROR))
if (tmp != OCLOSEDDOOR)
mmove(i,j,xl,yl);
}
/*
Move monsters that are moving intelligently, using the proximity
ripple. Attempt to move to a position in the proximity ripple
that is closer to the player.
Parameters: the X,Y position of the monster to be moved.
*/
static void move_smart(int i, int j)
{
int x, y, z ;
/* check for a half-speed monster, and check if not to move. Could be
done in the monster list build.
*/
switch(mitem[i][j])
{
case TROGLODYTE: case HOBGOBLIN: case METAMORPH: case XVART:
case INVISIBLESTALKER: case ICELIZARD: if ((gtime & 1) == 1) return;
};
/* find an adjoining location in the proximity ripple that is
closer to the player (has a lower value) than the monster's
current position.
*/
if (mitem[i][j] != VAMPIRE)
for (z=1; z<9; z++) /* go around in a circle */
{
x = i + diroffx[z] ;
y = j + diroffy[z] ;
if ( screen[x][y] < screen[i][j] )
if ( !mitem[x][y] )
{
mmove(i,j,w1x[0]=x,w1y[0]=y);
return;
}
}
else
/* prevent vampires from moving onto mirrors
*/
for (z=1; z<9; z++) /* go around in a circle */
{
x = i + diroffx[z] ;
y = j + diroffy[z] ;
if (( screen[x][y] < screen[i][j] ) &&
( item[x][y] != OMIRROR ))
if ( !mitem[x][y] )
{
mmove(i,j,w1x[0]=x,w1y[0]=y);
return;
}
}
}
/*
For monsters that are not moving in an intelligent fashion. Move
in a direct fashion toward the player's current position.
Parameters: the X,Y position of the monster to move.
*/
static void move_dumb(int i, int j)
{
int xl, yl, xh, yh ;
int k, m, tmp, tmpd, tmpx, tmpy ;
/* check for a half-speed monster, and check if not to move. Could be
done in the monster list build.
*/
switch(mitem[i][j])
{
case TROGLODYTE: case HOBGOBLIN: case METAMORPH: case XVART:
case INVISIBLESTALKER: case ICELIZARD: if ((gtime & 1) == 1) return;
};
/* dumb monsters move here */
/* set up range of spots to check. instead of checking all points
around the monster, only check those closest to the player. For
example, if the player is up and right of the monster, check only
the three spots up and right of the monster.
*/
xl=i-1; yl=j-1; xh=i+2; yh=j+2;
if (i<playerx) xl++; else if (i>playerx) --xh;
if (j<playery) yl++; else if (j>playery) --yh;
if (xl < 0) xl = 0;
if (yl < 0) yl = 0;
if (xh > MAXX) xh = MAXX; /* MAXX OK; loop check below is <, not <= */
if (yh > MAXY) yh = MAXY; /* MAXY OK; loop check below is <, not <= */
/* check all spots in the range. find the one that is closest to
the player. if the monster is already next to the player, exit
the check immediately.
*/
tmpd = 10000 ;
tmpx = i ; tmpy = j ;
for ( k = xl ; k < xh ; k++ )
for ( m = yl ; m < yh ; m++ )
if ( k == playerx && m == playery )
{
tmpd = 1 ;
tmpx = k ;
tmpy = m ;
break; /* exitloop */
}
else if ((item[k][m] != OWALL) &&
(item[k][m] != OCLOSEDDOOR) &&
((mitem[k][m] == 0 ) || (( k == i ) && ( m == j ))) &&
((mitem[i][j] != VAMPIRE) || (item[k][m] != OMIRROR)))
{
tmp = (playerx-k)*(playerx-k)+(playery-m)*(playery-m);
if (tmp < tmpd)
{
tmpd = tmp;
tmpx = k;
tmpy = m;
} /* end if */
} /* end if */
/* we have finished checking the spaces around the monster. if
any can be moved on and are closer to the player than the
current location, move the monster.
*/
if ((tmpd < 10000) && ((tmpx != i) || (tmpy != j)))
{
mmove( i, j, tmpx, tmpy );
w1x[0] = tmpx ; /* for last monster hit */
w1y[0] = tmpy ;
}
else
{
w1x[0] = i ; /* for last monster hit */
w1y[0] = j ;
}
} /* end move_dumb() */
/*
* mmove(x,y,xd,yd) Function to actually perform the monster movement
* int x,y,xd,yd;
*
* Enter with the from coordinates in (x,y) and the destination coordinates
* in (xd,yd).
*/
static void mmove(int aa, int bb, int cc, int dd)
{
int tmp,i,flag;
char *who,*p;
flag=0; /* set to 1 if monster hit by arrow trap */
if ((cc==playerx) && (dd==playery))
{
hitplayer(aa,bb);
return;
}
i=item[cc][dd];
if ((i==OPIT) || (i==OTRAPDOOR))
switch(mitem[aa][bb])
{
case BAT: case EYE:
case SPIRITNAGA: case PLATINUMDRAGON: case WRAITH:
case VAMPIRE: case SILVERDRAGON: case POLTERGEIST:
case DEMONLORD: case DEMONLORD+1: case DEMONLORD+2:
case DEMONLORD+3: case DEMONLORD+4: case DEMONLORD+5:
case DEMONLORD+6: case DEMONPRINCE: break;
default: mitem[aa][bb]=0; /* fell in a pit or trapdoor */
};
tmp = mitem[aa][bb];
mitem[cc][dd] = tmp;
if (i==OANNIHILATION)
{
if (tmp>=DEMONLORD+3) /* demons dispel spheres */
{
cursors();
lprintf("\nThe %s dispels the sphere!",monster[tmp].name);
rmsphere(cc,dd); /* delete the sphere */
}
else mitem[cc][dd]=i=tmp=0;
}
stealth[cc][dd]=1;
if ((hitp[cc][dd] = hitp[aa][bb]) < 0) hitp[cc][dd]=1;
mitem[aa][bb] = 0;
if (tmp == LEPRECHAUN)
switch(i)
{
case OGOLDPILE: case OMAXGOLD: case OKGOLD: case ODGOLD:
case ODIAMOND: case ORUBY: case OEMERALD: case OSAPPHIRE:
item[cc][dd] = 0; /* leprechaun takes gold */
};
if (tmp == TROLL) /* if a troll regenerate him */
if ((gtime & 1) == 0)
if (monster[tmp].hitpoints > hitp[cc][dd]) hitp[cc][dd]++;
if (i==OTRAPARROW) /* arrow hits monster */
{ who = "An arrow"; if ((hitp[cc][dd] -= rnd(10)+level) <= 0)
{ mitem[cc][dd]=0; flag=2; } else flag=1; }
if (i==ODARTRAP) /* dart hits monster */
{ who = "A dart"; if ((hitp[cc][dd] -= rnd(6)) <= 0)
{ mitem[cc][dd]=0; flag=2; } else flag=1; }
if (i==OTELEPORTER) /* monster hits teleport trap */
{ flag=3; fillmonst(mitem[cc][dd]); mitem[cc][dd]=0; }
if (c[BLINDCOUNT]) return; /* if blind don't show where monsters are */
if (know[cc][dd] & HAVESEEN)
{
p=0;
if (flag) cursors();
switch(flag)
{
case 1: p="\n%s hits the %s"; break;
case 2: p="\n%s hits and kills the %s"; break;
case 3: p="\nThe %s%s gets teleported"; who=""; break;
};
if (p) { lprintf(p,who,monster[tmp].name); }
}
/* if (yrepcount>1) { know[aa][bb] &= 2; know[cc][dd] &= 2; return; } */
if (know[aa][bb] & HAVESEEN) show1cell(aa,bb);
if (know[cc][dd] & HAVESEEN) show1cell(cc,dd);
}