movem.c revision 1.7 1 1.7 dholland /* $NetBSD: movem.c,v 1.7 2009/08/12 08:04:05 dholland Exp $ */
2 1.2 mycroft
3 1.1 cgd /*
4 1.5 christos * movem.c (move monster) Larn is copyrighted 1986 by Noah Morgan.
5 1.1 cgd *
6 1.5 christos * Here are the functions in this file:
7 1.1 cgd *
8 1.5 christos * movemonst() Routine to move the monsters toward the player
9 1.5 christos * movemt(x,y) Function to move a monster at (x,y) -- must determine where
10 1.5 christos * mmove(x,y,xd,yd) Function to actually perform the monster movement
11 1.5 christos * movsphere() Function to look for and move spheres of annihilation
12 1.1 cgd */
13 1.5 christos #include <sys/cdefs.h>
14 1.5 christos #ifndef lint
15 1.7 dholland __RCSID("$NetBSD: movem.c,v 1.7 2009/08/12 08:04:05 dholland Exp $");
16 1.5 christos #endif /* not lint */
17 1.5 christos
18 1.1 cgd #include "header.h"
19 1.5 christos #include "extern.h"
20 1.1 cgd
21 1.7 dholland static void movemt(int, int);
22 1.7 dholland static void mmove(int, int, int, int);
23 1.7 dholland static void movsphere(void);
24 1.7 dholland
25 1.1 cgd /*
26 1.5 christos * movemonst() Routine to move the monsters toward the player
27 1.1 cgd *
28 1.5 christos * This routine has the responsibility to determine which monsters are to
29 1.5 christos * move, and call movemt() to do the move.
30 1.5 christos * Returns no value.
31 1.1 cgd */
32 1.5 christos static short w1[9], w1x[9], w1y[9];
33 1.5 christos static int tmp1, tmp2, tmp3, tmp4, distance;
34 1.5 christos void
35 1.1 cgd movemonst()
36 1.5 christos {
37 1.5 christos int i, j;
38 1.5 christos if (c[TIMESTOP])
39 1.5 christos return; /* no action if time is stopped */
40 1.5 christos if (c[HASTESELF])
41 1.5 christos if ((c[HASTESELF] & 1) == 0)
42 1.5 christos return;
43 1.5 christos if (spheres)
44 1.5 christos movsphere(); /* move the spheres of annihilation if any */
45 1.5 christos if (c[HOLDMONST])
46 1.5 christos return; /* no action if monsters are held */
47 1.5 christos
48 1.5 christos if (c[AGGRAVATE]) { /* determine window of monsters to move */
49 1.5 christos tmp1 = playery - 5;
50 1.5 christos tmp2 = playery + 6;
51 1.5 christos tmp3 = playerx - 10;
52 1.5 christos tmp4 = playerx + 11;
53 1.5 christos distance = 40; /* depth of intelligent monster movement */
54 1.5 christos } else {
55 1.5 christos tmp1 = playery - 3;
56 1.5 christos tmp2 = playery + 4;
57 1.5 christos tmp3 = playerx - 5;
58 1.5 christos tmp4 = playerx + 6;
59 1.5 christos distance = 17; /* depth of intelligent monster movement */
60 1.5 christos }
61 1.5 christos
62 1.5 christos if (level == 0) { /* if on outside level monsters can move in
63 1.5 christos * perimeter */
64 1.5 christos if (tmp1 < 0)
65 1.5 christos tmp1 = 0;
66 1.5 christos if (tmp2 > MAXY)
67 1.5 christos tmp2 = MAXY;
68 1.5 christos if (tmp3 < 0)
69 1.5 christos tmp3 = 0;
70 1.5 christos if (tmp4 > MAXX)
71 1.5 christos tmp4 = MAXX;
72 1.5 christos } else { /* if in a dungeon monsters can't be on the
73 1.5 christos * perimeter (wall there) */
74 1.5 christos if (tmp1 < 1)
75 1.5 christos tmp1 = 1;
76 1.5 christos if (tmp2 > MAXY - 1)
77 1.5 christos tmp2 = MAXY - 1;
78 1.5 christos if (tmp3 < 1)
79 1.5 christos tmp3 = 1;
80 1.5 christos if (tmp4 > MAXX - 1)
81 1.5 christos tmp4 = MAXX - 1;
82 1.5 christos }
83 1.1 cgd
84 1.5 christos for (j = tmp1; j < tmp2; j++) /* now reset monster moved flags */
85 1.5 christos for (i = tmp3; i < tmp4; i++)
86 1.1 cgd moved[i][j] = 0;
87 1.5 christos moved[lasthx][lasthy] = 0;
88 1.5 christos
89 1.5 christos if (c[AGGRAVATE] || !c[STEALTH]) { /* who gets moved? split for
90 1.5 christos * efficiency */
91 1.5 christos for (j = tmp1; j < tmp2; j++) /* look thru all locations in
92 1.5 christos * window */
93 1.5 christos for (i = tmp3; i < tmp4; i++)
94 1.5 christos if (mitem[i][j]) /* if there is a monster
95 1.5 christos * to move */
96 1.5 christos if (moved[i][j] == 0) /* if it has not already
97 1.5 christos * been moved */
98 1.5 christos movemt(i, j); /* go and move the
99 1.5 christos * monster */
100 1.5 christos } else { /* not aggravated and not stealth */
101 1.5 christos for (j = tmp1; j < tmp2; j++) /* look thru all locations in
102 1.5 christos * window */
103 1.5 christos for (i = tmp3; i < tmp4; i++)
104 1.5 christos if (mitem[i][j]) /* if there is a monster
105 1.5 christos * to move */
106 1.5 christos if (moved[i][j] == 0) /* if it has not already
107 1.5 christos * been moved */
108 1.5 christos if (stealth[i][j]) /* if it is asleep due
109 1.5 christos * to stealth */
110 1.5 christos movemt(i, j); /* go and move the
111 1.5 christos * monster */
112 1.5 christos }
113 1.1 cgd
114 1.5 christos if (mitem[lasthx][lasthy]) { /* now move monster last hit by
115 1.5 christos * player if not already moved */
116 1.5 christos if (moved[lasthx][lasthy] == 0) { /* if it has not already
117 1.5 christos * been moved */
118 1.5 christos movemt(lasthx, lasthy);
119 1.5 christos lasthx = w1x[0];
120 1.5 christos lasthy = w1y[0];
121 1.1 cgd }
122 1.1 cgd }
123 1.5 christos }
124 1.1 cgd
125 1.1 cgd /*
126 1.5 christos * movemt(x,y) Function to move a monster at (x,y) -- must determine where
127 1.5 christos * int x,y;
128 1.1 cgd *
129 1.5 christos * This routine is responsible for determining where one monster at (x,y) will
130 1.5 christos * move to. Enter with the monsters coordinates in (x,y).
131 1.5 christos * Returns no value.
132 1.1 cgd */
133 1.5 christos static int tmpitem, xl, xh, yl, yh;
134 1.7 dholland static void
135 1.5 christos movemt(i, j)
136 1.5 christos int i, j;
137 1.5 christos {
138 1.5 christos int k, m, z, tmp, xtmp, ytmp, monst;
139 1.5 christos switch (monst = mitem[i][j]) { /* for half speed monsters */
140 1.5 christos case TROGLODYTE:
141 1.5 christos case HOBGOBLIN:
142 1.5 christos case METAMORPH:
143 1.5 christos case XVART:
144 1.5 christos case INVISIBLESTALKER:
145 1.5 christos case ICELIZARD:
146 1.5 christos if ((gltime & 1) == 1)
147 1.5 christos return;
148 1.5 christos };
149 1.5 christos
150 1.5 christos if (c[SCAREMONST]) { /* choose destination randomly if scared */
151 1.5 christos if ((xl = i + rnd(3) - 2) < 0)
152 1.5 christos xl = 0;
153 1.5 christos if (xl >= MAXX)
154 1.5 christos xl = MAXX - 1;
155 1.5 christos if ((yl = j + rnd(3) - 2) < 0)
156 1.5 christos yl = 0;
157 1.5 christos if (yl >= MAXY)
158 1.5 christos yl = MAXY - 1;
159 1.5 christos if ((tmp = item[xl][yl]) != OWALL)
160 1.5 christos if (mitem[xl][yl] == 0)
161 1.5 christos if ((mitem[i][j] != VAMPIRE) || (tmpitem != OMIRROR))
162 1.5 christos if (tmp != OCLOSEDDOOR)
163 1.5 christos mmove(i, j, xl, yl);
164 1.1 cgd return;
165 1.5 christos }
166 1.5 christos if (monster[monst].intelligence > 10 - c[HARDGAME]) { /* if smart monster */
167 1.5 christos /* intelligent movement here -- first setup screen array */
168 1.5 christos xl = tmp3 - 2;
169 1.5 christos yl = tmp1 - 2;
170 1.5 christos xh = tmp4 + 2;
171 1.5 christos yh = tmp2 + 2;
172 1.5 christos vxy(&xl, &yl);
173 1.5 christos vxy(&xh, &yh);
174 1.5 christos for (k = yl; k < yh; k++)
175 1.5 christos for (m = xl; m < xh; m++) {
176 1.5 christos switch (item[m][k]) {
177 1.5 christos case OWALL:
178 1.5 christos case OPIT:
179 1.5 christos case OTRAPARROW:
180 1.5 christos case ODARTRAP:
181 1.5 christos case OCLOSEDDOOR:
182 1.5 christos case OTRAPDOOR:
183 1.5 christos case OTELEPORTER:
184 1.5 christos smm: screen[m][k] = 127;
185 1.5 christos break;
186 1.5 christos case OMIRROR:
187 1.5 christos if (mitem[m][k] == VAMPIRE)
188 1.5 christos goto smm;
189 1.5 christos default:
190 1.5 christos screen[m][k] = 0;
191 1.5 christos break;
192 1.5 christos };
193 1.5 christos }
194 1.5 christos screen[playerx][playery] = 1;
195 1.1 cgd
196 1.5 christos /*
197 1.5 christos * now perform proximity ripple from playerx,playery to
198 1.5 christos * monster
199 1.5 christos */
200 1.5 christos xl = tmp3 - 1;
201 1.5 christos yl = tmp1 - 1;
202 1.5 christos xh = tmp4 + 1;
203 1.5 christos yh = tmp2 + 1;
204 1.5 christos vxy(&xl, &yl);
205 1.5 christos vxy(&xh, &yh);
206 1.5 christos for (tmp = 1; tmp < distance; tmp++) /* only up to 20 squares
207 1.5 christos * away */
208 1.5 christos for (k = yl; k < yh; k++)
209 1.5 christos for (m = xl; m < xh; m++)
210 1.5 christos if (screen[m][k] == tmp) /* if find proximity n
211 1.5 christos * advance it */
212 1.5 christos for (z = 1; z < 9; z++) { /* go around in a circle */
213 1.5 christos if (screen[xtmp = m + diroffx[z]][ytmp = k + diroffy[z]] == 0)
214 1.5 christos screen[xtmp][ytmp] = tmp + 1;
215 1.5 christos if (xtmp == i && ytmp == j)
216 1.5 christos goto out;
217 1.5 christos }
218 1.5 christos
219 1.5 christos out: if (tmp < distance) /* did find connectivity */
220 1.5 christos /* now select lowest value around playerx,playery */
221 1.5 christos for (z = 1; z < 9; z++) /* go around in a circle */
222 1.5 christos if (screen[xl = i + diroffx[z]][yl = j + diroffy[z]] == tmp)
223 1.5 christos if (!mitem[xl][yl]) {
224 1.5 christos mmove(i, j, w1x[0] = xl, w1y[0] = yl);
225 1.5 christos return;
226 1.5 christos }
227 1.5 christos }
228 1.1 cgd /* dumb monsters move here */
229 1.5 christos xl = i - 1;
230 1.5 christos yl = j - 1;
231 1.5 christos xh = i + 2;
232 1.5 christos yh = j + 2;
233 1.5 christos if (i < playerx)
234 1.5 christos xl++;
235 1.5 christos else if (i > playerx)
236 1.5 christos --xh;
237 1.5 christos if (j < playery)
238 1.5 christos yl++;
239 1.5 christos else if (j > playery)
240 1.5 christos --yh;
241 1.5 christos for (k = 0; k < 9; k++)
242 1.5 christos w1[k] = 10000;
243 1.5 christos
244 1.5 christos for (k = xl; k < xh; k++)
245 1.5 christos for (m = yl; m < yh; m++) { /* for each square compute
246 1.5 christos * distance to player */
247 1.5 christos tmp = k - i + 4 + 3 * (m - j);
248 1.1 cgd tmpitem = item[k][m];
249 1.5 christos if (tmpitem != OWALL || (k == playerx && m == playery))
250 1.5 christos if (mitem[k][m] == 0)
251 1.5 christos if ((mitem[i][j] != VAMPIRE) || (tmpitem != OMIRROR))
252 1.5 christos if (tmpitem != OCLOSEDDOOR) {
253 1.5 christos w1[tmp] = (playerx - k) * (playerx - k) + (playery - m) * (playery - m);
254 1.5 christos w1x[tmp] = k;
255 1.5 christos w1y[tmp] = m;
256 1.5 christos }
257 1.5 christos }
258 1.1 cgd
259 1.1 cgd tmp = 0;
260 1.5 christos for (k = 1; k < 9; k++)
261 1.5 christos if (w1[tmp] > w1[k])
262 1.5 christos tmp = k;
263 1.1 cgd
264 1.1 cgd if (w1[tmp] < 10000)
265 1.5 christos if ((i != w1x[tmp]) || (j != w1y[tmp]))
266 1.5 christos mmove(i, j, w1x[tmp], w1y[tmp]);
267 1.5 christos }
268 1.1 cgd
269 1.1 cgd /*
270 1.5 christos * mmove(x,y,xd,yd) Function to actually perform the monster movement
271 1.5 christos * int x,y,xd,yd;
272 1.1 cgd *
273 1.5 christos * Enter with the from coordinates in (x,y) and the destination coordinates
274 1.5 christos * in (xd,yd).
275 1.1 cgd */
276 1.7 dholland static void
277 1.5 christos mmove(aa, bb, cc, dd)
278 1.5 christos int aa, bb, cc, dd;
279 1.5 christos {
280 1.5 christos int tmp, i, flag;
281 1.6 dholland const char *who = NULL, *p;
282 1.6 dholland
283 1.5 christos flag = 0; /* set to 1 if monster hit by arrow trap */
284 1.5 christos if ((cc == playerx) && (dd == playery)) {
285 1.5 christos hitplayer(aa, bb);
286 1.5 christos moved[aa][bb] = 1;
287 1.5 christos return;
288 1.5 christos }
289 1.5 christos i = item[cc][dd];
290 1.5 christos if ((i == OPIT) || (i == OTRAPDOOR))
291 1.5 christos switch (mitem[aa][bb]) {
292 1.5 christos case SPIRITNAGA:
293 1.5 christos case PLATINUMDRAGON:
294 1.5 christos case WRAITH:
295 1.5 christos case VAMPIRE:
296 1.5 christos case SILVERDRAGON:
297 1.5 christos case POLTERGEIST:
298 1.5 christos case DEMONLORD:
299 1.5 christos case DEMONLORD + 1:
300 1.5 christos case DEMONLORD + 2:
301 1.5 christos case DEMONLORD + 3:
302 1.5 christos case DEMONLORD + 4:
303 1.5 christos case DEMONLORD + 5:
304 1.5 christos case DEMONLORD + 6:
305 1.5 christos case DEMONPRINCE:
306 1.5 christos break;
307 1.1 cgd
308 1.5 christos default:
309 1.5 christos mitem[aa][bb] = 0; /* fell in a pit or trapdoor */
310 1.1 cgd };
311 1.1 cgd tmp = mitem[cc][dd] = mitem[aa][bb];
312 1.5 christos if (i == OANNIHILATION) {
313 1.5 christos if (tmp >= DEMONLORD + 3) { /* demons dispel spheres */
314 1.1 cgd cursors();
315 1.5 christos lprintf("\nThe %s dispels the sphere!", monster[tmp].name);
316 1.5 christos rmsphere(cc, dd); /* delete the sphere */
317 1.5 christos } else
318 1.5 christos i = tmp = mitem[cc][dd] = 0;
319 1.5 christos }
320 1.5 christos stealth[cc][dd] = 1;
321 1.5 christos if ((hitp[cc][dd] = hitp[aa][bb]) < 0)
322 1.5 christos hitp[cc][dd] = 1;
323 1.5 christos mitem[aa][bb] = 0;
324 1.5 christos moved[cc][dd] = 1;
325 1.1 cgd if (tmp == LEPRECHAUN)
326 1.5 christos switch (i) {
327 1.5 christos case OGOLDPILE:
328 1.5 christos case OMAXGOLD:
329 1.5 christos case OKGOLD:
330 1.5 christos case ODGOLD:
331 1.5 christos case ODIAMOND:
332 1.5 christos case ORUBY:
333 1.5 christos case OEMERALD:
334 1.5 christos case OSAPPHIRE:
335 1.5 christos item[cc][dd] = 0; /* leprechaun takes gold */
336 1.5 christos };
337 1.1 cgd
338 1.5 christos if (tmp == TROLL) /* if a troll regenerate him */
339 1.4 christos if ((gltime & 1) == 0)
340 1.5 christos if (monster[tmp].hitpoints > hitp[cc][dd])
341 1.5 christos hitp[cc][dd]++;
342 1.1 cgd
343 1.5 christos if (i == OTRAPARROW) { /* arrow hits monster */
344 1.5 christos who = "An arrow";
345 1.5 christos if ((hitp[cc][dd] -= rnd(10) + level) <= 0) {
346 1.5 christos mitem[cc][dd] = 0;
347 1.5 christos flag = 2;
348 1.5 christos } else
349 1.5 christos flag = 1;
350 1.5 christos }
351 1.5 christos if (i == ODARTRAP) { /* dart hits monster */
352 1.5 christos who = "A dart";
353 1.5 christos if ((hitp[cc][dd] -= rnd(6)) <= 0) {
354 1.5 christos mitem[cc][dd] = 0;
355 1.5 christos flag = 2;
356 1.5 christos } else
357 1.5 christos flag = 1;
358 1.5 christos }
359 1.5 christos if (i == OTELEPORTER) { /* monster hits teleport trap */
360 1.5 christos flag = 3;
361 1.5 christos fillmonst(mitem[cc][dd]);
362 1.5 christos mitem[cc][dd] = 0;
363 1.5 christos }
364 1.5 christos if (c[BLINDCOUNT])
365 1.5 christos return; /* if blind don't show where monsters are */
366 1.5 christos if (know[cc][dd] & 1) {
367 1.5 christos p = 0;
368 1.5 christos if (flag)
369 1.5 christos cursors();
370 1.5 christos switch (flag) {
371 1.5 christos case 1:
372 1.5 christos p = "\n%s hits the %s";
373 1.5 christos break;
374 1.5 christos case 2:
375 1.5 christos p = "\n%s hits and kills the %s";
376 1.5 christos break;
377 1.5 christos case 3:
378 1.5 christos p = "\nThe %s%s gets teleported";
379 1.5 christos who = "";
380 1.5 christos break;
381 1.5 christos };
382 1.5 christos if (p) {
383 1.5 christos lprintf(p, who, monster[tmp].name);
384 1.5 christos beep();
385 1.1 cgd }
386 1.1 cgd }
387 1.5 christos /*
388 1.5 christos * if (yrepcount>1) { know[aa][bb] &= 2; know[cc][dd] &= 2; return;
389 1.5 christos * }
390 1.5 christos */
391 1.5 christos if (know[aa][bb] & 1)
392 1.5 christos show1cell(aa, bb);
393 1.5 christos if (know[cc][dd] & 1)
394 1.5 christos show1cell(cc, dd);
395 1.5 christos }
396 1.1 cgd
397 1.1 cgd /*
398 1.5 christos * movsphere() Function to look for and move spheres of annihilation
399 1.1 cgd *
400 1.5 christos * This function works on the sphere linked list, first duplicating the list
401 1.5 christos * (the act of moving changes the list), then processing each sphere in order
402 1.5 christos * to move it. They eat anything in their way, including stairs, volcanic
403 1.5 christos * shafts, potions, etc, except for upper level demons, who can dispel
404 1.5 christos * spheres.
405 1.5 christos * No value is returned.
406 1.1 cgd */
407 1.5 christos #define SPHMAX 20 /* maximum number of spheres movsphere can
408 1.5 christos * handle */
409 1.7 dholland static void
410 1.1 cgd movsphere()
411 1.5 christos {
412 1.5 christos int x, y, dir, len;
413 1.5 christos struct sphere *sp, *sp2;
414 1.5 christos struct sphere sph[SPHMAX];
415 1.1 cgd
416 1.1 cgd /* first duplicate sphere list */
417 1.5 christos for (sp = 0, x = 0, sp2 = spheres; sp2; sp2 = sp2->p) /* look through sphere
418 1.5 christos * list */
419 1.5 christos if (sp2->lev == level) { /* only if this level */
420 1.5 christos sph[x] = *sp2;
421 1.5 christos sph[x++].p = 0; /* copy the struct */
422 1.5 christos if (x > 1)
423 1.5 christos sph[x - 2].p = &sph[x - 1]; /* link pointers */
424 1.1 cgd }
425 1.5 christos if (x)
426 1.5 christos sp = sph; /* if any spheres, point to them */
427 1.5 christos else
428 1.5 christos return; /* no spheres */
429 1.1 cgd
430 1.5 christos for (sp = sph; sp; sp = sp->p) { /* look through sphere list */
431 1.5 christos x = sp->x;
432 1.5 christos y = sp->y;
433 1.5 christos if (item[x][y] != OANNIHILATION)
434 1.5 christos continue; /* not really there */
435 1.5 christos if (--(sp->lifetime) < 0) { /* has sphere run out of gas? */
436 1.5 christos rmsphere(x, y); /* delete sphere */
437 1.1 cgd continue;
438 1.1 cgd }
439 1.5 christos switch (rnd((int) max(7, c[INTELLIGENCE] >> 1))) { /* time to move the
440 1.5 christos * sphere */
441 1.5 christos case 1:
442 1.5 christos case 2: /* change direction to a random one */
443 1.5 christos sp->dir = rnd(8);
444 1.5 christos default: /* move in normal direction */
445 1.5 christos dir = sp->dir;
446 1.5 christos len = sp->lifetime;
447 1.5 christos rmsphere(x, y);
448 1.5 christos newsphere(x + diroffx[dir], y + diroffy[dir], dir, len);
449 1.5 christos };
450 1.1 cgd }
451 1.5 christos }
452