Sync usage with man page.
[netbsd-mini2440.git] / games / larn / movem.c
blob3e1980e6bc9eeb87a0ab742bfa2d8b60db2b01be
1 /* $NetBSD: movem.c,v 1.6 2008/01/28 05:38:54 dholland Exp $ */
3 /*
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>
14 #ifndef lint
15 __RCSID("$NetBSD: movem.c,v 1.6 2008/01/28 05:38:54 dholland Exp $");
16 #endif /* not lint */
18 #include "header.h"
19 #include "extern.h"
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.
30 * Returns no value.
32 static short w1[9], w1x[9], w1y[9];
33 static int tmp1, tmp2, tmp3, tmp4, distance;
34 void
35 movemonst()
37 int i, j;
38 if (c[TIMESTOP])
39 return; /* no action if time is stopped */
40 if (c[HASTESELF])
41 if ((c[HASTESELF] & 1) == 0)
42 return;
43 if (spheres)
44 movsphere(); /* move the spheres of annihilation if any */
45 if (c[HOLDMONST])
46 return; /* no action if monsters are held */
48 if (c[AGGRAVATE]) { /* determine window of monsters to move */
49 tmp1 = playery - 5;
50 tmp2 = playery + 6;
51 tmp3 = playerx - 10;
52 tmp4 = playerx + 11;
53 distance = 40; /* depth of intelligent monster movement */
54 } else {
55 tmp1 = playery - 3;
56 tmp2 = playery + 4;
57 tmp3 = playerx - 5;
58 tmp4 = playerx + 6;
59 distance = 17; /* depth of intelligent monster movement */
62 if (level == 0) { /* if on outside level monsters can move in
63 * perimeter */
64 if (tmp1 < 0)
65 tmp1 = 0;
66 if (tmp2 > MAXY)
67 tmp2 = MAXY;
68 if (tmp3 < 0)
69 tmp3 = 0;
70 if (tmp4 > MAXX)
71 tmp4 = MAXX;
72 } else { /* if in a dungeon monsters can't be on the
73 * perimeter (wall there) */
74 if (tmp1 < 1)
75 tmp1 = 1;
76 if (tmp2 > MAXY - 1)
77 tmp2 = MAXY - 1;
78 if (tmp3 < 1)
79 tmp3 = 1;
80 if (tmp4 > MAXX - 1)
81 tmp4 = MAXX - 1;
84 for (j = tmp1; j < tmp2; j++) /* now reset monster moved flags */
85 for (i = tmp3; i < tmp4; i++)
86 moved[i][j] = 0;
87 moved[lasthx][lasthy] = 0;
89 if (c[AGGRAVATE] || !c[STEALTH]) { /* who gets moved? split for
90 * efficiency */
91 for (j = tmp1; j < tmp2; j++) /* look thru all locations in
92 * window */
93 for (i = tmp3; i < tmp4; i++)
94 if (mitem[i][j]) /* if there is a monster
95 * to move */
96 if (moved[i][j] == 0) /* if it has not already
97 * been moved */
98 movemt(i, j); /* go and move the
99 * monster */
100 } else { /* not aggravated and not stealth */
101 for (j = tmp1; j < tmp2; j++) /* look thru all locations in
102 * window */
103 for (i = tmp3; i < tmp4; i++)
104 if (mitem[i][j]) /* if there is a monster
105 * to move */
106 if (moved[i][j] == 0) /* if it has not already
107 * been moved */
108 if (stealth[i][j]) /* if it is asleep due
109 * to stealth */
110 movemt(i, j); /* go and move the
111 * monster */
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
117 * been moved */
118 movemt(lasthx, lasthy);
119 lasthx = w1x[0];
120 lasthy = w1y[0];
126 * movemt(x,y) Function to move a monster at (x,y) -- must determine where
127 * int x,y;
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).
131 * Returns no value.
133 static int tmpitem, xl, xh, yl, yh;
134 static void
135 movemt(i, j)
136 int i, j;
138 int k, m, z, tmp, xtmp, ytmp, monst;
139 switch (monst = mitem[i][j]) { /* for half speed monsters */
140 case TROGLODYTE:
141 case HOBGOBLIN:
142 case METAMORPH:
143 case XVART:
144 case INVISIBLESTALKER:
145 case ICELIZARD:
146 if ((gltime & 1) == 1)
147 return;
150 if (c[SCAREMONST]) { /* choose destination randomly if scared */
151 if ((xl = i + rnd(3) - 2) < 0)
152 xl = 0;
153 if (xl >= MAXX)
154 xl = MAXX - 1;
155 if ((yl = j + rnd(3) - 2) < 0)
156 yl = 0;
157 if (yl >= MAXY)
158 yl = MAXY - 1;
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)
163 mmove(i, j, xl, yl);
164 return;
166 if (monster[monst].intelligence > 10 - c[HARDGAME]) { /* if smart monster */
167 /* intelligent movement here -- first setup screen array */
168 xl = tmp3 - 2;
169 yl = tmp1 - 2;
170 xh = tmp4 + 2;
171 yh = tmp2 + 2;
172 vxy(&xl, &yl);
173 vxy(&xh, &yh);
174 for (k = yl; k < yh; k++)
175 for (m = xl; m < xh; m++) {
176 switch (item[m][k]) {
177 case OWALL:
178 case OPIT:
179 case OTRAPARROW:
180 case ODARTRAP:
181 case OCLOSEDDOOR:
182 case OTRAPDOOR:
183 case OTELEPORTER:
184 smm: screen[m][k] = 127;
185 break;
186 case OMIRROR:
187 if (mitem[m][k] == VAMPIRE)
188 goto smm;
189 default:
190 screen[m][k] = 0;
191 break;
194 screen[playerx][playery] = 1;
197 * now perform proximity ripple from playerx,playery to
198 * monster
200 xl = tmp3 - 1;
201 yl = tmp1 - 1;
202 xh = tmp4 + 1;
203 yh = tmp2 + 1;
204 vxy(&xl, &yl);
205 vxy(&xh, &yh);
206 for (tmp = 1; tmp < distance; tmp++) /* only up to 20 squares
207 * away */
208 for (k = yl; k < yh; k++)
209 for (m = xl; m < xh; m++)
210 if (screen[m][k] == tmp) /* if find proximity n
211 * advance it */
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)
216 goto out;
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);
225 return;
228 /* dumb monsters move here */
229 xl = i - 1;
230 yl = j - 1;
231 xh = i + 2;
232 yh = j + 2;
233 if (i < playerx)
234 xl++;
235 else if (i > playerx)
236 --xh;
237 if (j < playery)
238 yl++;
239 else if (j > playery)
240 --yh;
241 for (k = 0; k < 9; k++)
242 w1[k] = 10000;
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);
254 w1x[tmp] = k;
255 w1y[tmp] = m;
259 tmp = 0;
260 for (k = 1; k < 9; k++)
261 if (w1[tmp] > w1[k])
262 tmp = k;
264 if (w1[tmp] < 10000)
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
271 * int x,y,xd,yd;
273 * Enter with the from coordinates in (x,y) and the destination coordinates
274 * in (xd,yd).
276 static void
277 mmove(aa, bb, cc, dd)
278 int aa, bb, cc, dd;
280 int tmp, i, flag;
281 const char *who = NULL, *p;
283 flag = 0; /* set to 1 if monster hit by arrow trap */
284 if ((cc == playerx) && (dd == playery)) {
285 hitplayer(aa, bb);
286 moved[aa][bb] = 1;
287 return;
289 i = item[cc][dd];
290 if ((i == OPIT) || (i == OTRAPDOOR))
291 switch (mitem[aa][bb]) {
292 case SPIRITNAGA:
293 case PLATINUMDRAGON:
294 case WRAITH:
295 case VAMPIRE:
296 case SILVERDRAGON:
297 case POLTERGEIST:
298 case DEMONLORD:
299 case DEMONLORD + 1:
300 case DEMONLORD + 2:
301 case DEMONLORD + 3:
302 case DEMONLORD + 4:
303 case DEMONLORD + 5:
304 case DEMONLORD + 6:
305 case DEMONPRINCE:
306 break;
308 default:
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 */
314 cursors();
315 lprintf("\nThe %s dispels the sphere!", monster[tmp].name);
316 rmsphere(cc, dd); /* delete the sphere */
317 } else
318 i = tmp = mitem[cc][dd] = 0;
320 stealth[cc][dd] = 1;
321 if ((hitp[cc][dd] = hitp[aa][bb]) < 0)
322 hitp[cc][dd] = 1;
323 mitem[aa][bb] = 0;
324 moved[cc][dd] = 1;
325 if (tmp == LEPRECHAUN)
326 switch (i) {
327 case OGOLDPILE:
328 case OMAXGOLD:
329 case OKGOLD:
330 case ODGOLD:
331 case ODIAMOND:
332 case ORUBY:
333 case OEMERALD:
334 case OSAPPHIRE:
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])
341 hitp[cc][dd]++;
343 if (i == OTRAPARROW) { /* arrow hits monster */
344 who = "An arrow";
345 if ((hitp[cc][dd] -= rnd(10) + level) <= 0) {
346 mitem[cc][dd] = 0;
347 flag = 2;
348 } else
349 flag = 1;
351 if (i == ODARTRAP) { /* dart hits monster */
352 who = "A dart";
353 if ((hitp[cc][dd] -= rnd(6)) <= 0) {
354 mitem[cc][dd] = 0;
355 flag = 2;
356 } else
357 flag = 1;
359 if (i == OTELEPORTER) { /* monster hits teleport trap */
360 flag = 3;
361 fillmonst(mitem[cc][dd]);
362 mitem[cc][dd] = 0;
364 if (c[BLINDCOUNT])
365 return; /* if blind don't show where monsters are */
366 if (know[cc][dd] & 1) {
367 p = 0;
368 if (flag)
369 cursors();
370 switch (flag) {
371 case 1:
372 p = "\n%s hits the %s";
373 break;
374 case 2:
375 p = "\n%s hits and kills the %s";
376 break;
377 case 3:
378 p = "\nThe %s%s gets teleported";
379 who = "";
380 break;
382 if (p) {
383 lprintf(p, who, monster[tmp].name);
384 beep();
388 * if (yrepcount>1) { know[aa][bb] &= 2; know[cc][dd] &= 2; return;
391 if (know[aa][bb] & 1)
392 show1cell(aa, bb);
393 if (know[cc][dd] & 1)
394 show1cell(cc, dd);
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
404 * spheres.
405 * No value is returned.
407 #define SPHMAX 20 /* maximum number of spheres movsphere can
408 * handle */
409 static void
410 movsphere()
412 int x, y, dir, len;
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
418 * list */
419 if (sp2->lev == level) { /* only if this level */
420 sph[x] = *sp2;
421 sph[x++].p = 0; /* copy the struct */
422 if (x > 1)
423 sph[x - 2].p = &sph[x - 1]; /* link pointers */
425 if (x)
426 sp = sph; /* if any spheres, point to them */
427 else
428 return; /* no spheres */
430 for (sp = sph; sp; sp = sp->p) { /* look through sphere list */
431 x = sp->x;
432 y = sp->y;
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 */
437 continue;
439 switch (rnd((int) max(7, c[INTELLIGENCE] >> 1))) { /* time to move the
440 * sphere */
441 case 1:
442 case 2: /* change direction to a random one */
443 sp->dir = rnd(8);
444 default: /* move in normal direction */
445 dir = sp->dir;
446 len = sp->lifetime;
447 rmsphere(x, y);
448 newsphere(x + diroffx[dir], y + diroffy[dir], dir, len);