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