movem.c revision 1.9 1 1.9 dholland /* $NetBSD: movem.c,v 1.9 2012/06/19 05:30:43 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.9 dholland __RCSID("$NetBSD: movem.c,v 1.9 2012/06/19 05:30:43 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.9 dholland movemonst(void)
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.9 dholland movemt(int i, int j)
136 1.5 christos {
137 1.5 christos int k, m, z, tmp, xtmp, ytmp, monst;
138 1.5 christos switch (monst = mitem[i][j]) { /* for half speed monsters */
139 1.5 christos case TROGLODYTE:
140 1.5 christos case HOBGOBLIN:
141 1.5 christos case METAMORPH:
142 1.5 christos case XVART:
143 1.5 christos case INVISIBLESTALKER:
144 1.5 christos case ICELIZARD:
145 1.5 christos if ((gltime & 1) == 1)
146 1.5 christos return;
147 1.5 christos };
148 1.5 christos
149 1.5 christos if (c[SCAREMONST]) { /* choose destination randomly if scared */
150 1.5 christos if ((xl = i + rnd(3) - 2) < 0)
151 1.5 christos xl = 0;
152 1.5 christos if (xl >= MAXX)
153 1.5 christos xl = MAXX - 1;
154 1.5 christos if ((yl = j + rnd(3) - 2) < 0)
155 1.5 christos yl = 0;
156 1.5 christos if (yl >= MAXY)
157 1.5 christos yl = MAXY - 1;
158 1.5 christos if ((tmp = item[xl][yl]) != OWALL)
159 1.5 christos if (mitem[xl][yl] == 0)
160 1.5 christos if ((mitem[i][j] != VAMPIRE) || (tmpitem != OMIRROR))
161 1.5 christos if (tmp != OCLOSEDDOOR)
162 1.5 christos mmove(i, j, xl, yl);
163 1.1 cgd return;
164 1.5 christos }
165 1.5 christos if (monster[monst].intelligence > 10 - c[HARDGAME]) { /* if smart monster */
166 1.5 christos /* intelligent movement here -- first setup screen array */
167 1.5 christos xl = tmp3 - 2;
168 1.5 christos yl = tmp1 - 2;
169 1.5 christos xh = tmp4 + 2;
170 1.5 christos yh = tmp2 + 2;
171 1.5 christos vxy(&xl, &yl);
172 1.5 christos vxy(&xh, &yh);
173 1.5 christos for (k = yl; k < yh; k++)
174 1.5 christos for (m = xl; m < xh; m++) {
175 1.5 christos switch (item[m][k]) {
176 1.5 christos case OWALL:
177 1.5 christos case OPIT:
178 1.5 christos case OTRAPARROW:
179 1.5 christos case ODARTRAP:
180 1.5 christos case OCLOSEDDOOR:
181 1.5 christos case OTRAPDOOR:
182 1.5 christos case OTELEPORTER:
183 1.5 christos smm: screen[m][k] = 127;
184 1.5 christos break;
185 1.5 christos case OMIRROR:
186 1.5 christos if (mitem[m][k] == VAMPIRE)
187 1.5 christos goto smm;
188 1.5 christos default:
189 1.5 christos screen[m][k] = 0;
190 1.5 christos break;
191 1.5 christos };
192 1.5 christos }
193 1.5 christos screen[playerx][playery] = 1;
194 1.1 cgd
195 1.5 christos /*
196 1.5 christos * now perform proximity ripple from playerx,playery to
197 1.5 christos * monster
198 1.5 christos */
199 1.5 christos xl = tmp3 - 1;
200 1.5 christos yl = tmp1 - 1;
201 1.5 christos xh = tmp4 + 1;
202 1.5 christos yh = tmp2 + 1;
203 1.5 christos vxy(&xl, &yl);
204 1.5 christos vxy(&xh, &yh);
205 1.5 christos for (tmp = 1; tmp < distance; tmp++) /* only up to 20 squares
206 1.5 christos * away */
207 1.5 christos for (k = yl; k < yh; k++)
208 1.5 christos for (m = xl; m < xh; m++)
209 1.5 christos if (screen[m][k] == tmp) /* if find proximity n
210 1.5 christos * advance it */
211 1.5 christos for (z = 1; z < 9; z++) { /* go around in a circle */
212 1.5 christos if (screen[xtmp = m + diroffx[z]][ytmp = k + diroffy[z]] == 0)
213 1.5 christos screen[xtmp][ytmp] = tmp + 1;
214 1.5 christos if (xtmp == i && ytmp == j)
215 1.5 christos goto out;
216 1.5 christos }
217 1.5 christos
218 1.5 christos out: if (tmp < distance) /* did find connectivity */
219 1.5 christos /* now select lowest value around playerx,playery */
220 1.5 christos for (z = 1; z < 9; z++) /* go around in a circle */
221 1.5 christos if (screen[xl = i + diroffx[z]][yl = j + diroffy[z]] == tmp)
222 1.5 christos if (!mitem[xl][yl]) {
223 1.5 christos mmove(i, j, w1x[0] = xl, w1y[0] = yl);
224 1.5 christos return;
225 1.5 christos }
226 1.5 christos }
227 1.1 cgd /* dumb monsters move here */
228 1.5 christos xl = i - 1;
229 1.5 christos yl = j - 1;
230 1.5 christos xh = i + 2;
231 1.5 christos yh = j + 2;
232 1.5 christos if (i < playerx)
233 1.5 christos xl++;
234 1.5 christos else if (i > playerx)
235 1.5 christos --xh;
236 1.5 christos if (j < playery)
237 1.5 christos yl++;
238 1.5 christos else if (j > playery)
239 1.5 christos --yh;
240 1.5 christos for (k = 0; k < 9; k++)
241 1.5 christos w1[k] = 10000;
242 1.5 christos
243 1.5 christos for (k = xl; k < xh; k++)
244 1.5 christos for (m = yl; m < yh; m++) { /* for each square compute
245 1.5 christos * distance to player */
246 1.5 christos tmp = k - i + 4 + 3 * (m - j);
247 1.1 cgd tmpitem = item[k][m];
248 1.5 christos if (tmpitem != OWALL || (k == playerx && m == playery))
249 1.5 christos if (mitem[k][m] == 0)
250 1.5 christos if ((mitem[i][j] != VAMPIRE) || (tmpitem != OMIRROR))
251 1.5 christos if (tmpitem != OCLOSEDDOOR) {
252 1.5 christos w1[tmp] = (playerx - k) * (playerx - k) + (playery - m) * (playery - m);
253 1.5 christos w1x[tmp] = k;
254 1.5 christos w1y[tmp] = m;
255 1.5 christos }
256 1.5 christos }
257 1.1 cgd
258 1.1 cgd tmp = 0;
259 1.5 christos for (k = 1; k < 9; k++)
260 1.5 christos if (w1[tmp] > w1[k])
261 1.5 christos tmp = k;
262 1.1 cgd
263 1.1 cgd if (w1[tmp] < 10000)
264 1.5 christos if ((i != w1x[tmp]) || (j != w1y[tmp]))
265 1.5 christos mmove(i, j, w1x[tmp], w1y[tmp]);
266 1.5 christos }
267 1.1 cgd
268 1.1 cgd /*
269 1.5 christos * mmove(x,y,xd,yd) Function to actually perform the monster movement
270 1.5 christos * int x,y,xd,yd;
271 1.1 cgd *
272 1.5 christos * Enter with the from coordinates in (x,y) and the destination coordinates
273 1.5 christos * in (xd,yd).
274 1.1 cgd */
275 1.7 dholland static void
276 1.9 dholland mmove(int aa, int bb, int cc, int dd)
277 1.5 christos {
278 1.5 christos int tmp, i, flag;
279 1.8 christos const char *who = NULL;
280 1.6 dholland
281 1.5 christos flag = 0; /* set to 1 if monster hit by arrow trap */
282 1.5 christos if ((cc == playerx) && (dd == playery)) {
283 1.5 christos hitplayer(aa, bb);
284 1.5 christos moved[aa][bb] = 1;
285 1.5 christos return;
286 1.5 christos }
287 1.5 christos i = item[cc][dd];
288 1.5 christos if ((i == OPIT) || (i == OTRAPDOOR))
289 1.5 christos switch (mitem[aa][bb]) {
290 1.5 christos case SPIRITNAGA:
291 1.5 christos case PLATINUMDRAGON:
292 1.5 christos case WRAITH:
293 1.5 christos case VAMPIRE:
294 1.5 christos case SILVERDRAGON:
295 1.5 christos case POLTERGEIST:
296 1.5 christos case DEMONLORD:
297 1.5 christos case DEMONLORD + 1:
298 1.5 christos case DEMONLORD + 2:
299 1.5 christos case DEMONLORD + 3:
300 1.5 christos case DEMONLORD + 4:
301 1.5 christos case DEMONLORD + 5:
302 1.5 christos case DEMONLORD + 6:
303 1.5 christos case DEMONPRINCE:
304 1.5 christos break;
305 1.1 cgd
306 1.5 christos default:
307 1.5 christos mitem[aa][bb] = 0; /* fell in a pit or trapdoor */
308 1.1 cgd };
309 1.1 cgd tmp = mitem[cc][dd] = mitem[aa][bb];
310 1.5 christos if (i == OANNIHILATION) {
311 1.5 christos if (tmp >= DEMONLORD + 3) { /* demons dispel spheres */
312 1.1 cgd cursors();
313 1.5 christos lprintf("\nThe %s dispels the sphere!", monster[tmp].name);
314 1.5 christos rmsphere(cc, dd); /* delete the sphere */
315 1.5 christos } else
316 1.5 christos i = tmp = mitem[cc][dd] = 0;
317 1.5 christos }
318 1.5 christos stealth[cc][dd] = 1;
319 1.5 christos if ((hitp[cc][dd] = hitp[aa][bb]) < 0)
320 1.5 christos hitp[cc][dd] = 1;
321 1.5 christos mitem[aa][bb] = 0;
322 1.5 christos moved[cc][dd] = 1;
323 1.1 cgd if (tmp == LEPRECHAUN)
324 1.5 christos switch (i) {
325 1.5 christos case OGOLDPILE:
326 1.5 christos case OMAXGOLD:
327 1.5 christos case OKGOLD:
328 1.5 christos case ODGOLD:
329 1.5 christos case ODIAMOND:
330 1.5 christos case ORUBY:
331 1.5 christos case OEMERALD:
332 1.5 christos case OSAPPHIRE:
333 1.5 christos item[cc][dd] = 0; /* leprechaun takes gold */
334 1.5 christos };
335 1.1 cgd
336 1.5 christos if (tmp == TROLL) /* if a troll regenerate him */
337 1.4 christos if ((gltime & 1) == 0)
338 1.5 christos if (monster[tmp].hitpoints > hitp[cc][dd])
339 1.5 christos hitp[cc][dd]++;
340 1.1 cgd
341 1.5 christos if (i == OTRAPARROW) { /* arrow hits monster */
342 1.5 christos who = "An arrow";
343 1.5 christos if ((hitp[cc][dd] -= rnd(10) + level) <= 0) {
344 1.5 christos mitem[cc][dd] = 0;
345 1.5 christos flag = 2;
346 1.5 christos } else
347 1.5 christos flag = 1;
348 1.5 christos }
349 1.5 christos if (i == ODARTRAP) { /* dart hits monster */
350 1.5 christos who = "A dart";
351 1.5 christos if ((hitp[cc][dd] -= rnd(6)) <= 0) {
352 1.5 christos mitem[cc][dd] = 0;
353 1.5 christos flag = 2;
354 1.5 christos } else
355 1.5 christos flag = 1;
356 1.5 christos }
357 1.5 christos if (i == OTELEPORTER) { /* monster hits teleport trap */
358 1.5 christos flag = 3;
359 1.5 christos fillmonst(mitem[cc][dd]);
360 1.5 christos mitem[cc][dd] = 0;
361 1.5 christos }
362 1.5 christos if (c[BLINDCOUNT])
363 1.5 christos return; /* if blind don't show where monsters are */
364 1.5 christos if (know[cc][dd] & 1) {
365 1.5 christos if (flag)
366 1.5 christos cursors();
367 1.5 christos switch (flag) {
368 1.5 christos case 1:
369 1.8 christos lprintf("\n%s hits the %s", who, monster[tmp].name);
370 1.8 christos beep();
371 1.5 christos break;
372 1.5 christos case 2:
373 1.8 christos lprintf("\n%s hits and kills the %s",
374 1.8 christos who, monster[tmp].name);
375 1.8 christos beep();
376 1.5 christos break;
377 1.5 christos case 3:
378 1.8 christos lprintf("\nThe %s gets teleported", monster[tmp].name);
379 1.8 christos beep();
380 1.5 christos break;
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.7 dholland static void
406 1.9 dholland movsphere(void)
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