2 * movem.c (move monster) Larn is copyrighted 1986 by Noah Morgan.
3 * $FreeBSD: src/games/larn/movem.c,v 1.4 1999/11/16 02:57:23 billf Exp $
4 * $DragonFly: src/games/larn/movem.c,v 1.3 2006/08/26 17:05:05 pavalos Exp $
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
15 static void movemt(int, int);
16 static void mmove(int, int, int, int);
17 static void movsphere(void);
20 * movemonst() Routine to move the monsters toward the player
22 * This routine has the responsibility to determine which monsters are to
23 * move, and call movemt() to do the move.
26 static short w1
[9], w1x
[9], w1y
[9];
27 static int tmp1
, tmp2
, tmp3
, tmp4
, distance
;
33 if (c
[TIMESTOP
]) /* no action if time is stopped */
36 if ((c
[HASTESELF
] & 1) == 0)
38 if (spheres
) /* move the spheres of annihilation if any */
40 if (c
[HOLDMONST
]) /* no action if monsters are held */
43 if (c
[AGGRAVATE
]) { /* determine window of monsters to move */
48 distance
= 40; /* depth of intelligent monster movement */
54 distance
= 17; /* depth of intelligent monster movement */
57 /* if on outside level monsters can move in perimeter */
67 } else { /* if in a dungeon monsters can't be on the perimeter (wall there) */
78 for (j
= tmp1
; j
< tmp2
; j
++) /* now reset monster moved flags */
79 for (i
= tmp3
; i
< tmp4
; i
++)
81 moved
[lasthx
][lasthy
] = 0;
83 /* who gets moved? split for efficiency */
84 if (c
[AGGRAVATE
] || !c
[STEALTH
]) {
85 /* look thru all locations in window */
86 for (j
= tmp1
; j
< tmp2
; j
++)
87 for (i
= tmp3
; i
< tmp4
; i
++)
88 /* if there is a monster to move */
90 /* if it has not already been moved */
92 /* go and move the monster */
94 } else { /* not aggravated and not stealth */
95 /* look thru all locations in window */
96 for (j
= tmp1
; j
< tmp2
; j
++)
97 for (i
= tmp3
; i
< tmp4
; i
++)
98 /* if there is a monster to move */
100 /* if it has not already been moved */
101 if (moved
[i
][j
] == 0)
102 /* if it is asleep due to stealth */
104 /* go and move the monster */
108 /* now move monster last hit by player if not already moved */
109 if (mitem
[lasthx
][lasthy
]) {
110 /* if it has not already been moved */
111 if (moved
[lasthx
][lasthy
] == 0) {
112 movemt(lasthx
, lasthy
);
120 * movemt(x,y) Function to move a monster at (x,y) -- must determine where
123 * This routine is responsible for determining where one monster at (x,y) will
124 * move to. Enter with the monsters coordinates in (x,y).
127 static int tmpitem
, xl
, xh
, yl
, yh
;
132 int k
, m
, z
, tmp
, xtmp
, ytmp
, monst
;
133 switch (monst
= mitem
[i
][j
]) { /* for half speed monsters */
138 case INVISIBLESTALKER
:
140 if ((gtime
& 1) == 1)
144 if (c
[SCAREMONST
]) { /* choose destination randomly if scared */
145 if ((xl
= i
+ rnd(3) - 2) < 0)
149 if ((yl
= j
+ rnd(3) - 2) < 0)
153 if ((tmp
= item
[xl
][yl
]) != OWALL
)
154 if (mitem
[xl
][yl
] == 0)
155 if ((mitem
[i
][j
] != VAMPIRE
) || (tmpitem
!= OMIRROR
))
156 if (tmp
!= OCLOSEDDOOR
)
161 if (monster
[monst
].intelligence
> 10 - c
[HARDGAME
]) { /* if smart monster */
162 /* intelligent movement here -- first setup screen array */
169 for (k
= yl
; k
< yh
; k
++)
170 for (m
= xl
; m
< xh
; m
++) {
171 switch (item
[m
][k
]) {
179 smm
: screen
[m
][k
] = 127;
182 if (mitem
[m
][k
] == VAMPIRE
)
189 screen
[playerx
][playery
] = 1;
191 /* now perform proximity ripple from playerx,playery to monster */
198 for (tmp
= 1; tmp
< distance
; tmp
++) /* only up to 20 squares away */
199 for (k
= yl
; k
< yh
; k
++)
200 for (m
= xl
; m
< xh
; m
++)
201 /* if find proximity n advance it */
202 if (screen
[m
][k
] == tmp
)
203 /* go around in a circle */
204 for (z
= 1; z
< 9; z
++) {
205 if (screen
[xtmp
= m
+ diroffx
[z
]][ytmp
= k
+ diroffy
[z
]] == 0)
206 screen
[xtmp
][ytmp
] = tmp
+ 1;
207 if (xtmp
== i
&& ytmp
== j
)
211 out
: if (tmp
< distance
) /* did find connectivity */
212 /* now select lowest value around playerx,playery */
213 for (z
= 1; z
< 9; z
++) /* go around in a circle */
214 if (screen
[xl
= i
+ diroffx
[z
]][yl
= j
+ diroffy
[z
]] == tmp
)
215 if (!mitem
[xl
][yl
]) {
216 mmove(i
, j
, w1x
[0] = xl
, w1y
[0] = yl
);
221 /* dumb monsters move here */
228 else if (i
> playerx
)
232 else if (j
> playery
)
234 for (k
= 0; k
< 9; k
++)
237 for (k
= xl
; k
< xh
; k
++)
238 /* for each square compute distance to player */
239 for (m
= yl
; m
< yh
; m
++) {
240 tmp
= k
- i
+ 4 + 3 * (m
- j
);
241 tmpitem
= item
[k
][m
];
242 if (tmpitem
!= OWALL
|| (k
== playerx
&& m
== playery
))
243 if (mitem
[k
][m
] == 0)
244 if ((mitem
[i
][j
] != VAMPIRE
) || (tmpitem
!= OMIRROR
))
245 if (tmpitem
!= OCLOSEDDOOR
) {
246 w1
[tmp
] = (playerx
- k
) * (playerx
- k
) + (playery
- m
) * (playery
- m
);
253 for (k
= 1; k
< 9; k
++)
258 if ((i
!= w1x
[tmp
]) || (j
!= w1y
[tmp
]))
259 mmove(i
, j
, w1x
[tmp
], w1y
[tmp
]);
263 * mmove(x,y,xd,yd) Function to actually perform the monster movement
266 * Enter with the from coordinates in (x,y) and the destination coordinates
270 mmove(int aa
, int bb
, int cc
, int dd
)
273 const char *who
= NULL
, *p
;
274 flag
= 0; /* set to 1 if monster hit by arrow trap */
275 if ((cc
== playerx
) && (dd
== playery
)) {
281 if ((i
== OPIT
) || (i
== OTRAPDOOR
))
282 switch (mitem
[aa
][bb
]) {
300 mitem
[aa
][bb
] = 0; /* fell in a pit or trapdoor */
302 tmp
= mitem
[cc
][dd
] = mitem
[aa
][bb
];
303 if (i
== OANNIHILATION
) {
304 if (tmp
>= DEMONLORD
+ 3) { /* demons dispel spheres */
306 lprintf("\nThe %s dispels the sphere!", monster
[tmp
].name
);
307 rmsphere(cc
, dd
); /* delete the sphere */
309 i
= tmp
= mitem
[cc
][dd
] = 0;
312 if ((hitp
[cc
][dd
] = hitp
[aa
][bb
]) < 0)
316 if (tmp
== LEPRECHAUN
)
326 item
[cc
][dd
] = 0; /* leprechaun takes gold */
329 if (tmp
== TROLL
) /* if a troll regenerate him */
330 if ((gtime
& 1) == 0)
331 if (monster
[tmp
].hitpoints
> hitp
[cc
][dd
])
334 if (i
== OTRAPARROW
) { /* arrow hits monster */
336 if ((hitp
[cc
][dd
] -= rnd(10) + level
) <= 0) {
342 if (i
== ODARTRAP
) { /* dart hits monster */
344 if ((hitp
[cc
][dd
] -= rnd(6)) <= 0) {
350 if (i
== OTELEPORTER
) { /* monster hits teleport trap */
352 fillmonst(mitem
[cc
][dd
]);
355 if (c
[BLINDCOUNT
]) /* if blind don't show where monsters are */
357 if (know
[cc
][dd
] & 1) {
363 p
= "\n%s hits the %s";
366 p
= "\n%s hits and kills the %s";
369 p
= "\nThe %s%s gets teleported";
374 lprintf(p
, who
, monster
[tmp
].name
);
378 if (know
[aa
][bb
] & 1)
380 if (know
[cc
][dd
] & 1)
385 * movsphere() Function to look for and move spheres of annihilation
387 * This function works on the sphere linked list, first duplicating the list
388 * (the act of moving changes the list), then processing each sphere in order
389 * to move it. They eat anything in their way, including stairs, volcanic
390 * shafts, potions, etc, except for upper level demons, who can dispel
392 * No value is returned.
394 #define SPHMAX 20 /* maximum number of spheres movsphere can handle */
399 struct sphere
*sp
, *sp2
;
400 struct sphere sph
[SPHMAX
];
402 /* first duplicate sphere list */
403 /* look through sphere list */
404 for (sp
= 0, x
= 0, sp2
= spheres
; sp2
; sp2
= sp2
->p
)
405 if (sp2
->lev
== level
) { /* only if this level */
407 sph
[x
++].p
= 0; /* copy the struct */
409 sph
[x
- 2].p
= &sph
[x
- 1]; /* link pointers */
411 if (x
) /* if any spheres, point to them */
413 else /* no spheres */
416 for (sp
= sph
; sp
; sp
= sp
->p
) { /* look through sphere list */
419 if (item
[x
][y
] != OANNIHILATION
) /* not really there */
421 if (--(sp
->lifetime
) < 0) { /* has sphere run out of gas? */
422 rmsphere(x
, y
); /* delete sphere */
425 /* time to move the sphere */
426 switch (rnd((int)max(7, c
[INTELLIGENCE
] >> 1))) {
428 case 2: /* change direction to a random one */
430 default: /* move in normal direction */
434 newsphere(x
+ diroffx
[dir
], y
+ diroffy
[dir
], dir
, len
);