vault.c (5659B)
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 2 /* vault.c - version 1.0.2 */ 3 4 #include "hack.h" 5 #include "mkroom.h" 6 7 extern struct monst *makemon(); 8 #define FCSIZ (ROWNO+COLNO) 9 struct fakecorridor { 10 xchar fx,fy,ftyp; 11 }; 12 13 struct egd { 14 int fcbeg, fcend; /* fcend: first unused pos */ 15 xchar gdx, gdy; /* goal of guard's walk */ 16 unsigned gddone:1; 17 struct fakecorridor fakecorr[FCSIZ]; 18 }; 19 20 struct permonst pm_guard = 21 { "guard", '@', 12, 12, -1, 4, 10, sizeof(struct egd) }; 22 23 static struct monst *guard; 24 static int gdlevel; 25 #define EGD ((struct egd *)(&(guard->mextra[0]))) 26 27 static 28 restfakecorr() 29 { 30 register fcx,fcy,fcbeg; 31 register struct rm *crm; 32 33 while((fcbeg = EGD->fcbeg) < EGD->fcend) { 34 fcx = EGD->fakecorr[fcbeg].fx; 35 fcy = EGD->fakecorr[fcbeg].fy; 36 if((u.ux == fcx && u.uy == fcy) || cansee(fcx,fcy) || 37 m_at(fcx,fcy)) 38 return; 39 crm = &levl[fcx][fcy]; 40 crm->typ = EGD->fakecorr[fcbeg].ftyp; 41 if(!crm->typ) crm->seen = 0; 42 newsym(fcx,fcy); 43 EGD->fcbeg++; 44 } 45 /* it seems he left the corridor - let the guard disappear */ 46 mondead(guard); 47 guard = 0; 48 } 49 50 static 51 goldincorridor() 52 { 53 register int fci; 54 55 for(fci = EGD->fcbeg; fci < EGD->fcend; fci++) 56 if(g_at(EGD->fakecorr[fci].fx, EGD->fakecorr[fci].fy)) 57 return(1); 58 return(0); 59 } 60 61 setgd(){ 62 register struct monst *mtmp; 63 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) if(mtmp->isgd){ 64 guard = mtmp; 65 gdlevel = dlevel; 66 return; 67 } 68 guard = 0; 69 } 70 71 invault(){ 72 register tmp = inroom(u.ux, u.uy); 73 if(tmp < 0 || rooms[tmp].rtype != VAULT) { 74 u.uinvault = 0; 75 return; 76 } 77 if(++u.uinvault % 50 == 0 && (!guard || gdlevel != dlevel)) { 78 char buf[BUFSZ]; 79 register x,y,dd,gx,gy; 80 81 /* first find the goal for the guard */ 82 for(dd = 1; (dd < ROWNO || dd < COLNO); dd++) { 83 for(y = u.uy-dd; y <= u.uy+dd; y++) { 84 if(y < 0 || y > ROWNO-1) continue; 85 for(x = u.ux-dd; x <= u.ux+dd; x++) { 86 if(y != u.uy-dd && y != u.uy+dd && x != u.ux-dd) 87 x = u.ux+dd; 88 if(x < 0 || x > COLNO-1) continue; 89 if(levl[x][y].typ == CORR) goto fnd; 90 } 91 } 92 } 93 impossible("Not a single corridor on this level??"); 94 tele(); 95 return; 96 fnd: 97 gx = x; gy = y; 98 99 /* next find a good place for a door in the wall */ 100 x = u.ux; y = u.uy; 101 while(levl[x][y].typ == ROOM) { 102 register int dx,dy; 103 104 dx = (gx > x) ? 1 : (gx < x) ? -1 : 0; 105 dy = (gy > y) ? 1 : (gy < y) ? -1 : 0; 106 if(abs(gx-x) >= abs(gy-y)) 107 x += dx; 108 else 109 y += dy; 110 } 111 112 /* make something interesting happen */ 113 if(!(guard = makemon(&pm_guard,x,y))) return; 114 guard->isgd = guard->mpeaceful = 1; 115 EGD->gddone = 0; 116 gdlevel = dlevel; 117 if(!cansee(guard->mx, guard->my)) { 118 mondead(guard); 119 guard = 0; 120 return; 121 } 122 123 pline("Suddenly one of the Vault's guards enters!"); 124 pmon(guard); 125 do { 126 pline("\"Hello stranger, who are you?\" - "); 127 getlin(buf); 128 } while (!letter(buf[0])); 129 130 if(!strcmp(buf, "Croesus") || !strcmp(buf, "Kroisos")) { 131 pline("\"Oh, yes - of course. Sorry to have disturbed you.\""); 132 mondead(guard); 133 guard = 0; 134 return; 135 } 136 clrlin(); 137 pline("\"I don't know you.\""); 138 if(!u.ugold) 139 pline("\"Please follow me.\""); 140 else { 141 pline("\"Most likely all that gold was stolen from this vault.\""); 142 pline("\"Please drop your gold (say d$ ) and follow me.\""); 143 } 144 EGD->gdx = gx; 145 EGD->gdy = gy; 146 EGD->fcbeg = 0; 147 EGD->fakecorr[0].fx = x; 148 EGD->fakecorr[0].fy = y; 149 EGD->fakecorr[0].ftyp = levl[x][y].typ; 150 levl[x][y].typ = DOOR; 151 EGD->fcend = 1; 152 } 153 } 154 155 gd_move(){ 156 register int x,y,dx,dy,gx,gy,nx,ny,typ; 157 register struct fakecorridor *fcp; 158 register struct rm *crm; 159 if(!guard || gdlevel != dlevel){ 160 impossible("Where is the guard?"); 161 return(2); /* died */ 162 } 163 if(u.ugold || goldincorridor()) 164 return(0); /* didnt move */ 165 if(dist(guard->mx,guard->my) > 1 || EGD->gddone) { 166 restfakecorr(); 167 return(0); /* didnt move */ 168 } 169 x = guard->mx; 170 y = guard->my; 171 /* look around (hor & vert only) for accessible places */ 172 for(nx = x-1; nx <= x+1; nx++) for(ny = y-1; ny <= y+1; ny++) { 173 if(nx == x || ny == y) if(nx != x || ny != y) 174 if(isok(nx,ny)) 175 if(!IS_WALL(typ = (crm = &levl[nx][ny])->typ) && typ != POOL) { 176 register int i; 177 for(i = EGD->fcbeg; i < EGD->fcend; i++) 178 if(EGD->fakecorr[i].fx == nx && 179 EGD->fakecorr[i].fy == ny) 180 goto nextnxy; 181 if((i = inroom(nx,ny)) >= 0 && rooms[i].rtype == VAULT) 182 goto nextnxy; 183 /* seems we found a good place to leave him alone */ 184 EGD->gddone = 1; 185 if(ACCESSIBLE(typ)) goto newpos; 186 crm->typ = (typ == SCORR) ? CORR : DOOR; 187 goto proceed; 188 } 189 nextnxy: ; 190 } 191 nx = x; 192 ny = y; 193 gx = EGD->gdx; 194 gy = EGD->gdy; 195 dx = (gx > x) ? 1 : (gx < x) ? -1 : 0; 196 dy = (gy > y) ? 1 : (gy < y) ? -1 : 0; 197 if(abs(gx-x) >= abs(gy-y)) nx += dx; else ny += dy; 198 199 while((typ = (crm = &levl[nx][ny])->typ) != 0) { 200 /* in view of the above we must have IS_WALL(typ) or typ == POOL */ 201 /* must be a wall here */ 202 if(isok(nx+nx-x,ny+ny-y) && typ != POOL && 203 ZAP_POS(levl[nx+nx-x][ny+ny-y].typ)){ 204 crm->typ = DOOR; 205 goto proceed; 206 } 207 if(dy && nx != x) { 208 nx = x; ny = y+dy; 209 continue; 210 } 211 if(dx && ny != y) { 212 ny = y; nx = x+dx; dy = 0; 213 continue; 214 } 215 /* I don't like this, but ... */ 216 crm->typ = DOOR; 217 goto proceed; 218 } 219 crm->typ = CORR; 220 proceed: 221 if(cansee(nx,ny)) { 222 mnewsym(nx,ny); 223 prl(nx,ny); 224 } 225 fcp = &(EGD->fakecorr[EGD->fcend]); 226 if(EGD->fcend++ == FCSIZ) panic("fakecorr overflow"); 227 fcp->fx = nx; 228 fcp->fy = ny; 229 fcp->ftyp = typ; 230 newpos: 231 if(EGD->gddone) nx = ny = 0; 232 guard->mx = nx; 233 guard->my = ny; 234 pmon(guard); 235 restfakecorr(); 236 return(1); 237 } 238 239 gddead(){ 240 guard = 0; 241 } 242 243 replgd(mtmp,mtmp2) 244 register struct monst *mtmp, *mtmp2; 245 { 246 if(mtmp == guard) 247 guard = mtmp2; 248 }