1 /* $NetBSD: movem.c,v 1.6 2008/01/28 05:38:54 dholland Exp $ */
4 * movem.c (move monster) Larn is copyrighted 1986 by Noah Morgan.
6 * Here are the functions in this file:
8 * movemonst() Routine to move the monsters toward the player
9 * movemt(x,y) Function to move a monster at (x,y) -- must determine where
10 * mmove(x,y,xd,yd) Function to actually perform the monster movement
11 * movsphere() Function to look for and move spheres of annihilation
13 #include <sys/cdefs.h>
15 __RCSID("$NetBSD: movem.c,v 1.6 2008/01/28 05:38:54 dholland Exp $");
21 static void movemt(int, int);
22 static void mmove(int, int, int, int);
23 static void movsphere(void);
26 * movemonst() Routine to move the monsters toward the player
28 * This routine has the responsibility to determine which monsters are to
29 * move, and call movemt() to do the move.
32 static short w1
[9], w1x
[9], w1y
[9];
33 static int tmp1
, tmp2
, tmp3
, tmp4
, distance
;
39 return; /* no action if time is stopped */
41 if ((c
[HASTESELF
] & 1) == 0)
44 movsphere(); /* move the spheres of annihilation if any */
46 return; /* no action if monsters are held */
48 if (c
[AGGRAVATE
]) { /* determine window of monsters to move */
53 distance
= 40; /* depth of intelligent monster movement */
59 distance
= 17; /* depth of intelligent monster movement */
62 if (level
== 0) { /* if on outside level monsters can move in
72 } else { /* if in a dungeon monsters can't be on the
73 * perimeter (wall there) */
84 for (j
= tmp1
; j
< tmp2
; j
++) /* now reset monster moved flags */
85 for (i
= tmp3
; i
< tmp4
; i
++)
87 moved
[lasthx
][lasthy
] = 0;
89 if (c
[AGGRAVATE
] || !c
[STEALTH
]) { /* who gets moved? split for
91 for (j
= tmp1
; j
< tmp2
; j
++) /* look thru all locations in
93 for (i
= tmp3
; i
< tmp4
; i
++)
94 if (mitem
[i
][j
]) /* if there is a monster
96 if (moved
[i
][j
] == 0) /* if it has not already
98 movemt(i
, j
); /* go and move the
100 } else { /* not aggravated and not stealth */
101 for (j
= tmp1
; j
< tmp2
; j
++) /* look thru all locations in
103 for (i
= tmp3
; i
< tmp4
; i
++)
104 if (mitem
[i
][j
]) /* if there is a monster
106 if (moved
[i
][j
] == 0) /* if it has not already
108 if (stealth
[i
][j
]) /* if it is asleep due
110 movemt(i
, j
); /* go and move the
114 if (mitem
[lasthx
][lasthy
]) { /* now move monster last hit by
115 * player if not already moved */
116 if (moved
[lasthx
][lasthy
] == 0) { /* if it has not already
118 movemt(lasthx
, lasthy
);
126 * movemt(x,y) Function to move a monster at (x,y) -- must determine where
129 * This routine is responsible for determining where one monster at (x,y) will
130 * move to. Enter with the monsters coordinates in (x,y).
133 static int tmpitem
, xl
, xh
, yl
, yh
;
138 int k
, m
, z
, tmp
, xtmp
, ytmp
, monst
;
139 switch (monst
= mitem
[i
][j
]) { /* for half speed monsters */
144 case INVISIBLESTALKER
:
146 if ((gltime
& 1) == 1)
150 if (c
[SCAREMONST
]) { /* choose destination randomly if scared */
151 if ((xl
= i
+ rnd(3) - 2) < 0)
155 if ((yl
= j
+ rnd(3) - 2) < 0)
159 if ((tmp
= item
[xl
][yl
]) != OWALL
)
160 if (mitem
[xl
][yl
] == 0)
161 if ((mitem
[i
][j
] != VAMPIRE
) || (tmpitem
!= OMIRROR
))
162 if (tmp
!= OCLOSEDDOOR
)
166 if (monster
[monst
].intelligence
> 10 - c
[HARDGAME
]) { /* if smart monster */
167 /* intelligent movement here -- first setup screen array */
174 for (k
= yl
; k
< yh
; k
++)
175 for (m
= xl
; m
< xh
; m
++) {
176 switch (item
[m
][k
]) {
184 smm
: screen
[m
][k
] = 127;
187 if (mitem
[m
][k
] == VAMPIRE
)
194 screen
[playerx
][playery
] = 1;
197 * now perform proximity ripple from playerx,playery to
206 for (tmp
= 1; tmp
< distance
; tmp
++) /* only up to 20 squares
208 for (k
= yl
; k
< yh
; k
++)
209 for (m
= xl
; m
< xh
; m
++)
210 if (screen
[m
][k
] == tmp
) /* if find proximity n
212 for (z
= 1; z
< 9; z
++) { /* go around in a circle */
213 if (screen
[xtmp
= m
+ diroffx
[z
]][ytmp
= k
+ diroffy
[z
]] == 0)
214 screen
[xtmp
][ytmp
] = tmp
+ 1;
215 if (xtmp
== i
&& ytmp
== j
)
219 out
: if (tmp
< distance
) /* did find connectivity */
220 /* now select lowest value around playerx,playery */
221 for (z
= 1; z
< 9; z
++) /* go around in a circle */
222 if (screen
[xl
= i
+ diroffx
[z
]][yl
= j
+ diroffy
[z
]] == tmp
)
223 if (!mitem
[xl
][yl
]) {
224 mmove(i
, j
, w1x
[0] = xl
, w1y
[0] = yl
);
228 /* dumb monsters move here */
235 else if (i
> playerx
)
239 else if (j
> playery
)
241 for (k
= 0; k
< 9; k
++)
244 for (k
= xl
; k
< xh
; k
++)
245 for (m
= yl
; m
< yh
; m
++) { /* for each square compute
246 * distance to player */
247 tmp
= k
- i
+ 4 + 3 * (m
- j
);
248 tmpitem
= item
[k
][m
];
249 if (tmpitem
!= OWALL
|| (k
== playerx
&& m
== playery
))
250 if (mitem
[k
][m
] == 0)
251 if ((mitem
[i
][j
] != VAMPIRE
) || (tmpitem
!= OMIRROR
))
252 if (tmpitem
!= OCLOSEDDOOR
) {
253 w1
[tmp
] = (playerx
- k
) * (playerx
- k
) + (playery
- m
) * (playery
- m
);
260 for (k
= 1; k
< 9; k
++)
265 if ((i
!= w1x
[tmp
]) || (j
!= w1y
[tmp
]))
266 mmove(i
, j
, w1x
[tmp
], w1y
[tmp
]);
270 * mmove(x,y,xd,yd) Function to actually perform the monster movement
273 * Enter with the from coordinates in (x,y) and the destination coordinates
277 mmove(aa
, bb
, cc
, dd
)
281 const char *who
= NULL
, *p
;
283 flag
= 0; /* set to 1 if monster hit by arrow trap */
284 if ((cc
== playerx
) && (dd
== playery
)) {
290 if ((i
== OPIT
) || (i
== OTRAPDOOR
))
291 switch (mitem
[aa
][bb
]) {
309 mitem
[aa
][bb
] = 0; /* fell in a pit or trapdoor */
311 tmp
= mitem
[cc
][dd
] = mitem
[aa
][bb
];
312 if (i
== OANNIHILATION
) {
313 if (tmp
>= DEMONLORD
+ 3) { /* demons dispel spheres */
315 lprintf("\nThe %s dispels the sphere!", monster
[tmp
].name
);
316 rmsphere(cc
, dd
); /* delete the sphere */
318 i
= tmp
= mitem
[cc
][dd
] = 0;
321 if ((hitp
[cc
][dd
] = hitp
[aa
][bb
]) < 0)
325 if (tmp
== LEPRECHAUN
)
335 item
[cc
][dd
] = 0; /* leprechaun takes gold */
338 if (tmp
== TROLL
) /* if a troll regenerate him */
339 if ((gltime
& 1) == 0)
340 if (monster
[tmp
].hitpoints
> hitp
[cc
][dd
])
343 if (i
== OTRAPARROW
) { /* arrow hits monster */
345 if ((hitp
[cc
][dd
] -= rnd(10) + level
) <= 0) {
351 if (i
== ODARTRAP
) { /* dart hits monster */
353 if ((hitp
[cc
][dd
] -= rnd(6)) <= 0) {
359 if (i
== OTELEPORTER
) { /* monster hits teleport trap */
361 fillmonst(mitem
[cc
][dd
]);
365 return; /* if blind don't show where monsters are */
366 if (know
[cc
][dd
] & 1) {
372 p
= "\n%s hits the %s";
375 p
= "\n%s hits and kills the %s";
378 p
= "\nThe %s%s gets teleported";
383 lprintf(p
, who
, monster
[tmp
].name
);
388 * if (yrepcount>1) { know[aa][bb] &= 2; know[cc][dd] &= 2; return;
391 if (know
[aa
][bb
] & 1)
393 if (know
[cc
][dd
] & 1)
398 * movsphere() Function to look for and move spheres of annihilation
400 * This function works on the sphere linked list, first duplicating the list
401 * (the act of moving changes the list), then processing each sphere in order
402 * to move it. They eat anything in their way, including stairs, volcanic
403 * shafts, potions, etc, except for upper level demons, who can dispel
405 * No value is returned.
407 #define SPHMAX 20 /* maximum number of spheres movsphere can
413 struct sphere
*sp
, *sp2
;
414 struct sphere sph
[SPHMAX
];
416 /* first duplicate sphere list */
417 for (sp
= 0, x
= 0, sp2
= spheres
; sp2
; sp2
= sp2
->p
) /* look through sphere
419 if (sp2
->lev
== level
) { /* only if this level */
421 sph
[x
++].p
= 0; /* copy the struct */
423 sph
[x
- 2].p
= &sph
[x
- 1]; /* link pointers */
426 sp
= sph
; /* if any spheres, point to them */
428 return; /* no spheres */
430 for (sp
= sph
; sp
; sp
= sp
->p
) { /* look through sphere list */
433 if (item
[x
][y
] != OANNIHILATION
)
434 continue; /* not really there */
435 if (--(sp
->lifetime
) < 0) { /* has sphere run out of gas? */
436 rmsphere(x
, y
); /* delete sphere */
439 switch (rnd((int) max(7, c
[INTELLIGENCE
] >> 1))) { /* time to move the
442 case 2: /* change direction to a random one */
444 default: /* move in normal direction */
448 newsphere(x
+ diroffx
[dir
], y
+ diroffy
[dir
], dir
, len
);