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 $
5 * Here are the functions in this file:
7 * movemonst() Routine to move the monsters toward the player
8 * movemt(x,y) Function to move a monster at (x,y) -- must determine where
9 * mmove(x,y,xd,yd) Function to actually perform the monster movement
10 * movsphere() Function to look for and move spheres of annihilation
14 static void movemt(int, int);
15 static void mmove(int, int, int, int);
16 static void movsphere(void);
19 * movemonst() Routine to move the monsters toward the player
21 * This routine has the responsibility to determine which monsters are to
22 * move, and call movemt() to do the move.
25 static short w1
[9], w1x
[9], w1y
[9];
26 static int tmp1
, tmp2
, tmp3
, tmp4
, distance
;
32 if (c
[TIMESTOP
]) /* no action if time is stopped */
35 if ((c
[HASTESELF
] & 1) == 0)
37 if (spheres
) /* move the spheres of annihilation if any */
39 if (c
[HOLDMONST
]) /* no action if monsters are held */
42 if (c
[AGGRAVATE
]) { /* determine window of monsters to move */
47 distance
= 40; /* depth of intelligent monster movement */
53 distance
= 17; /* depth of intelligent monster movement */
56 /* if on outside level monsters can move in perimeter */
66 } else { /* if in a dungeon monsters can't be on the perimeter (wall there) */
77 for (j
= tmp1
; j
< tmp2
; j
++) /* now reset monster moved flags */
78 for (i
= tmp3
; i
< tmp4
; i
++)
80 moved
[lasthx
][lasthy
] = 0;
82 /* who gets moved? split for efficiency */
83 if (c
[AGGRAVATE
] || !c
[STEALTH
]) {
84 /* look thru all locations in window */
85 for (j
= tmp1
; j
< tmp2
; j
++)
86 for (i
= tmp3
; i
< tmp4
; i
++)
87 /* if there is a monster to move */
89 /* if it has not already been moved */
91 /* go and move the monster */
93 } else { /* not aggravated and not stealth */
94 /* look thru all locations in window */
95 for (j
= tmp1
; j
< tmp2
; j
++)
96 for (i
= tmp3
; i
< tmp4
; i
++)
97 /* if there is a monster to move */
99 /* if it has not already been moved */
100 if (moved
[i
][j
] == 0)
101 /* if it is asleep due to stealth */
103 /* go and move the monster */
107 /* now move monster last hit by player if not already moved */
108 if (mitem
[lasthx
][lasthy
]) {
109 /* if it has not already been moved */
110 if (moved
[lasthx
][lasthy
] == 0) {
111 movemt(lasthx
, lasthy
);
119 * movemt(x,y) Function to move a monster at (x,y) -- must determine where
122 * This routine is responsible for determining where one monster at (x,y) will
123 * move to. Enter with the monsters coordinates in (x,y).
126 static int tmpitem
, xl
, xh
, yl
, yh
;
131 int k
, m
, z
, tmp
, xtmp
, ytmp
, monst
;
132 switch (monst
= mitem
[i
][j
]) { /* for half speed monsters */
137 case INVISIBLESTALKER
:
139 if ((gtime
& 1) == 1)
143 if (c
[SCAREMONST
]) { /* choose destination randomly if scared */
144 if ((xl
= i
+ rnd(3) - 2) < 0)
148 if ((yl
= j
+ rnd(3) - 2) < 0)
152 if ((tmp
= item
[xl
][yl
]) != OWALL
)
153 if (mitem
[xl
][yl
] == 0)
154 if ((mitem
[i
][j
] != VAMPIRE
) || (tmpitem
!= OMIRROR
))
155 if (tmp
!= OCLOSEDDOOR
)
160 if (monster
[monst
].intelligence
> 10 - c
[HARDGAME
]) { /* if smart monster */
161 /* intelligent movement here -- first setup screen array */
168 for (k
= yl
; k
< yh
; k
++)
169 for (m
= xl
; m
< xh
; m
++) {
170 switch (item
[m
][k
]) {
178 smm
: screen
[m
][k
] = 127;
181 if (mitem
[m
][k
] == VAMPIRE
)
188 screen
[playerx
][playery
] = 1;
190 /* now perform proximity ripple from playerx,playery to monster */
197 for (tmp
= 1; tmp
< distance
; tmp
++) /* only up to 20 squares away */
198 for (k
= yl
; k
< yh
; k
++)
199 for (m
= xl
; m
< xh
; m
++)
200 /* if find proximity n advance it */
201 if (screen
[m
][k
] == tmp
)
202 /* go around in a circle */
203 for (z
= 1; z
< 9; z
++) {
204 if (screen
[xtmp
= m
+ diroffx
[z
]][ytmp
= k
+ diroffy
[z
]] == 0)
205 screen
[xtmp
][ytmp
] = tmp
+ 1;
206 if (xtmp
== i
&& ytmp
== j
)
210 out
: if (tmp
< distance
) /* did find connectivity */
211 /* now select lowest value around playerx,playery */
212 for (z
= 1; z
< 9; z
++) /* go around in a circle */
213 if (screen
[xl
= i
+ diroffx
[z
]][yl
= j
+ diroffy
[z
]] == tmp
)
214 if (!mitem
[xl
][yl
]) {
215 mmove(i
, j
, w1x
[0] = xl
, w1y
[0] = yl
);
220 /* dumb monsters move here */
227 else if (i
> playerx
)
231 else if (j
> playery
)
233 for (k
= 0; k
< 9; k
++)
236 for (k
= xl
; k
< xh
; k
++)
237 /* for each square compute distance to player */
238 for (m
= yl
; m
< yh
; m
++) {
239 tmp
= k
- i
+ 4 + 3 * (m
- j
);
240 tmpitem
= item
[k
][m
];
241 if (tmpitem
!= OWALL
|| (k
== playerx
&& m
== playery
))
242 if (mitem
[k
][m
] == 0)
243 if ((mitem
[i
][j
] != VAMPIRE
) || (tmpitem
!= OMIRROR
))
244 if (tmpitem
!= OCLOSEDDOOR
) {
245 w1
[tmp
] = (playerx
- k
) * (playerx
- k
) + (playery
- m
) * (playery
- m
);
252 for (k
= 1; k
< 9; k
++)
257 if ((i
!= w1x
[tmp
]) || (j
!= w1y
[tmp
]))
258 mmove(i
, j
, w1x
[tmp
], w1y
[tmp
]);
262 * mmove(x,y,xd,yd) Function to actually perform the monster movement
265 * Enter with the from coordinates in (x,y) and the destination coordinates
269 mmove(int aa
, int bb
, int cc
, int dd
)
272 const char *who
= NULL
, *p
;
273 flag
= 0; /* set to 1 if monster hit by arrow trap */
274 if ((cc
== playerx
) && (dd
== playery
)) {
280 if ((i
== OPIT
) || (i
== OTRAPDOOR
))
281 switch (mitem
[aa
][bb
]) {
299 mitem
[aa
][bb
] = 0; /* fell in a pit or trapdoor */
301 tmp
= mitem
[cc
][dd
] = mitem
[aa
][bb
];
302 if (i
== OANNIHILATION
) {
303 if (tmp
>= DEMONLORD
+ 3) { /* demons dispel spheres */
305 lprintf("\nThe %s dispels the sphere!", monster
[tmp
].name
);
306 rmsphere(cc
, dd
); /* delete the sphere */
308 i
= tmp
= mitem
[cc
][dd
] = 0;
311 if ((hitp
[cc
][dd
] = hitp
[aa
][bb
]) < 0)
315 if (tmp
== LEPRECHAUN
)
325 item
[cc
][dd
] = 0; /* leprechaun takes gold */
328 if (tmp
== TROLL
) /* if a troll regenerate him */
329 if ((gtime
& 1) == 0)
330 if (monster
[tmp
].hitpoints
> hitp
[cc
][dd
])
333 if (i
== OTRAPARROW
) { /* arrow hits monster */
335 if ((hitp
[cc
][dd
] -= rnd(10) + level
) <= 0) {
341 if (i
== ODARTRAP
) { /* dart hits monster */
343 if ((hitp
[cc
][dd
] -= rnd(6)) <= 0) {
349 if (i
== OTELEPORTER
) { /* monster hits teleport trap */
351 fillmonst(mitem
[cc
][dd
]);
354 if (c
[BLINDCOUNT
]) /* if blind don't show where monsters are */
356 if (know
[cc
][dd
] & 1) {
362 p
= "\n%s hits the %s";
365 p
= "\n%s hits and kills the %s";
368 p
= "\nThe %s%s gets teleported";
373 lprintf(p
, who
, monster
[tmp
].name
);
377 if (know
[aa
][bb
] & 1)
379 if (know
[cc
][dd
] & 1)
384 * movsphere() Function to look for and move spheres of annihilation
386 * This function works on the sphere linked list, first duplicating the list
387 * (the act of moving changes the list), then processing each sphere in order
388 * to move it. They eat anything in their way, including stairs, volcanic
389 * shafts, potions, etc, except for upper level demons, who can dispel
391 * No value is returned.
393 #define SPHMAX 20 /* maximum number of spheres movsphere can handle */
398 struct sphere
*sp
, *sp2
;
399 struct sphere sph
[SPHMAX
];
401 /* first duplicate sphere list */
402 /* look through sphere list */
403 for (sp
= NULL
, x
= 0, sp2
= spheres
; sp2
; sp2
= sp2
->p
)
404 if (sp2
->lev
== level
) { /* only if this level */
406 sph
[x
++].p
= 0; /* copy the struct */
408 sph
[x
- 2].p
= &sph
[x
- 1]; /* link pointers */
410 if (x
) /* if any spheres, point to them */
412 else /* no spheres */
415 for (sp
= sph
; sp
; sp
= sp
->p
) { /* look through sphere list */
418 if (item
[x
][y
] != OANNIHILATION
) /* not really there */
420 if (--(sp
->lifetime
) < 0) { /* has sphere run out of gas? */
421 rmsphere(x
, y
); /* delete sphere */
424 /* time to move the sphere */
425 switch (rnd((int)max(7, c
[INTELLIGENCE
] >> 1))) {
427 case 2: /* change direction to a random one */
429 default: /* move in normal direction */
433 newsphere(x
+ diroffx
[dir
], y
+ diroffy
[dir
], dir
, len
);