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