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