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