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