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