Home | History | Annotate | Line # | Download | only in hack
hack.mkshop.c revision 1.3
      1 /*
      2  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      3  */
      4 
      5 #ifndef lint
      6 static char rcsid[] = "$Id: hack.mkshop.c,v 1.3 1993/08/02 17:17:26 mycroft Exp $";
      7 #endif /* not lint */
      8 
      9 #include <stdlib.h>
     10 #ifndef QUEST
     11 #include "hack.h"
     12 #include "def.mkroom.h"
     13 #include "def.eshk.h"
     14 #define	ESHK	((struct eshk *)(&(shk->mextra[0])))
     15 extern struct monst *makemon();
     16 extern struct obj *mkobj_at();
     17 extern int nroom;
     18 extern char shtypes[];	/* = "=/)%?!["; 8 types: 7 specialized, 1 mixed */
     19 schar shprobs[] = { 3,3,5,5,10,10,14,50 };	/* their probabilities */
     20 
     21 mkshop(){
     22 register struct mkroom *sroom;
     23 register int sh,sx,sy,i = -1;
     24 register char let;
     25 int roomno;
     26 register struct monst *shk;
     27 #ifdef WIZARD
     28 	/* first determine shoptype */
     29 	if(wizard){
     30 		register char *ep = getenv("SHOPTYPE");
     31 		if(ep){
     32 			if(*ep == 'z' || *ep == 'Z'){
     33 				mkzoo(ZOO);
     34 				return;
     35 			}
     36 			if(*ep == 'm' || *ep == 'M'){
     37 				mkzoo(MORGUE);
     38 				return;
     39 			}
     40 			if(*ep == 'b' || *ep == 'B'){
     41 				mkzoo(BEEHIVE);
     42 				return;
     43 			}
     44 			if(*ep == 's' || *ep == 'S'){
     45 				mkswamp();
     46 				return;
     47 			}
     48 			for(i=0; shtypes[i]; i++)
     49 				if(*ep == shtypes[i]) break;
     50 			goto gottype;
     51 		}
     52 	}
     53 gottype:
     54 #endif WIZARD
     55 	for(sroom = &rooms[0], roomno = 0; ; sroom++, roomno++){
     56 		if(sroom->hx < 0) return;
     57 		if(sroom - rooms >= nroom) {
     58 			pline("rooms not closed by -1?");
     59 			return;
     60 		}
     61 		if(sroom->rtype) continue;
     62 		if(!sroom->rlit || has_dnstairs(sroom) || has_upstairs(sroom))
     63 			continue;
     64 		if(
     65 #ifdef WIZARD
     66 		   (wizard && getenv("SHOPTYPE") && sroom->doorct != 0) ||
     67 #endif WIZARD
     68 			sroom->doorct <= 2 && sroom->doorct > 0) break;
     69 	}
     70 
     71 	if(i < 0) {			/* shoptype not yet determined */
     72 	    register int j;
     73 
     74 	    for(j = rn2(100), i = 0; (j -= shprobs[i])>= 0; i++)
     75 		if(!shtypes[i]) break;			/* superfluous */
     76 	    if(isbig(sroom) && i + SHOPBASE == WANDSHOP)
     77 		i = GENERAL-SHOPBASE;
     78 	}
     79 	sroom->rtype = i + SHOPBASE;
     80 	let = shtypes[i];
     81 	sh = sroom->fdoor;
     82 	sx = doors[sh].x;
     83 	sy = doors[sh].y;
     84 	if(sx == sroom->lx-1) sx++; else
     85 	if(sx == sroom->hx+1) sx--; else
     86 	if(sy == sroom->ly-1) sy++; else
     87 	if(sy == sroom->hy+1) sy--; else {
     88 #ifdef WIZARD
     89 	    /* This is said to happen sometimes, but I've never seen it. */
     90 	    if(wizard) {
     91 		register int j = sroom->doorct;
     92 		extern int doorindex;
     93 
     94 		pline("Where is shopdoor?");
     95 		pline("Room at (%d,%d),(%d,%d).", sroom->lx, sroom->ly,
     96 			sroom->hx, sroom->hy);
     97 		pline("doormax=%d doorct=%d fdoor=%d",
     98 			doorindex, sroom->doorct, sh);
     99 		while(j--) {
    100 			pline("door [%d,%d]", doors[sh].x, doors[sh].y);
    101 			sh++;
    102 		}
    103 		more();
    104 	    }
    105 #endif WIZARD
    106 	    return;
    107 	}
    108 	if(!(shk = makemon(PM_SHK,sx,sy))) return;
    109 	shk->isshk = shk->mpeaceful = 1;
    110 	shk->msleep = 0;
    111 	shk->mtrapseen = ~0;	/* we know all the traps already */
    112 	ESHK->shoproom = roomno;
    113 	ESHK->shoplevel = dlevel;
    114 	ESHK->shd = doors[sh];
    115 	ESHK->shk.x = sx;
    116 	ESHK->shk.y = sy;
    117 	ESHK->robbed = 0;
    118 	ESHK->visitct = 0;
    119 	ESHK->following = 0;
    120 	shk->mgold = 1000 + 30*rnd(100);	/* initial capital */
    121 	ESHK->billct = 0;
    122 	findname(ESHK->shknam, let);
    123 	for(sx = sroom->lx; sx <= sroom->hx; sx++)
    124 	for(sy = sroom->ly; sy <= sroom->hy; sy++){
    125 		register struct monst *mtmp;
    126 		if((sx == sroom->lx && doors[sh].x == sx-1) ||
    127 		   (sx == sroom->hx && doors[sh].x == sx+1) ||
    128 		   (sy == sroom->ly && doors[sh].y == sy-1) ||
    129 		   (sy == sroom->hy && doors[sh].y == sy+1)) continue;
    130 		if(rn2(100) < dlevel && !m_at(sx,sy) &&
    131 		   (mtmp = makemon(PM_MIMIC, sx, sy))){
    132 			mtmp->mimic = 1;
    133 			mtmp->mappearance =
    134 			    (let && rn2(10) < dlevel) ? let : ']';
    135 			continue;
    136 		}
    137 		(void) mkobj_at(let, sx, sy);
    138 	}
    139 }
    140 
    141 mkzoo(type)
    142 int type;
    143 {
    144 	register struct mkroom *sroom;
    145 	register struct monst *mon;
    146 	register int sh,sx,sy,i;
    147 	int goldlim = 500 * dlevel;
    148 	int moct = 0;
    149 	struct permonst *morguemon();
    150 
    151 	i = nroom;
    152 	for(sroom = &rooms[rn2(nroom)]; ; sroom++) {
    153 		if(sroom == &rooms[nroom])
    154 			sroom = &rooms[0];
    155 		if(!i-- || sroom->hx < 0)
    156 			return;
    157 		if(sroom->rtype)
    158 			continue;
    159 		if(type == MORGUE && sroom->rlit)
    160 			continue;
    161 		if(has_upstairs(sroom) || (has_dnstairs(sroom) && rn2(3)))
    162 			continue;
    163 		if(sroom->doorct == 1 || !rn2(5))
    164 			break;
    165 	}
    166 	sroom->rtype = type;
    167 	sh = sroom->fdoor;
    168 	for(sx = sroom->lx; sx <= sroom->hx; sx++)
    169 	for(sy = sroom->ly; sy <= sroom->hy; sy++){
    170 		if((sx == sroom->lx && doors[sh].x == sx-1) ||
    171 		   (sx == sroom->hx && doors[sh].x == sx+1) ||
    172 		   (sy == sroom->ly && doors[sh].y == sy-1) ||
    173 		   (sy == sroom->hy && doors[sh].y == sy+1)) continue;
    174 		mon = makemon(
    175 		   (type == MORGUE) ? morguemon() :
    176 		   (type == BEEHIVE) ? PM_KILLER_BEE : (struct permonst *) 0,
    177 		   sx, sy);
    178 		if(mon) mon->msleep = 1;
    179 		switch(type) {
    180 		case ZOO:
    181 		   i = sq(dist2(sx,sy,doors[sh].x,doors[sh].y));
    182 		   if(i >= goldlim) i = 5*dlevel;
    183 		   goldlim -= i;
    184 		   mkgold((long)(10 + rn2(i)), sx, sy);
    185 		   break;
    186 		case MORGUE:
    187 		   /* Usually there is one dead body in the morgue */
    188 		   if(!moct && rn2(3)) {
    189 			mksobj_at(CORPSE, sx, sy);
    190 			moct++;
    191 		   }
    192 		   break;
    193 		case BEEHIVE:
    194 		   if(!rn2(3)) mksobj_at(LUMP_OF_ROYAL_JELLY, sx, sy);
    195 		   break;
    196 		}
    197 	}
    198 }
    199 
    200 struct permonst *
    201 morguemon()
    202 {
    203 	extern struct permonst pm_ghost;
    204 	register int i = rn2(100), hd = rn2(dlevel);
    205 
    206 	if(hd > 10 && i < 10) return(PM_DEMON);
    207 	if(hd > 8 && i > 85) return(PM_VAMPIRE);
    208 	return((i < 40) ? PM_GHOST : (i < 60) ? PM_WRAITH : PM_ZOMBIE);
    209 }
    210 
    211 mkswamp()	/* Michiel Huisjes & Fred de Wilde */
    212 {
    213 	register struct mkroom *sroom;
    214 	register int sx,sy,i,eelct = 0;
    215 	extern struct permonst pm_eel;
    216 
    217 	for(i=0; i<5; i++) {		/* 5 tries */
    218 		sroom = &rooms[rn2(nroom)];
    219 		if(sroom->hx < 0 || sroom->rtype ||
    220 		   has_upstairs(sroom) || has_dnstairs(sroom))
    221 			continue;
    222 
    223 		/* satisfied; make a swamp */
    224 		sroom->rtype = SWAMP;
    225 		for(sx = sroom->lx; sx <= sroom->hx; sx++)
    226 		for(sy = sroom->ly; sy <= sroom->hy; sy++)
    227 		if((sx+sy)%2 && !o_at(sx,sy) && !t_at(sx,sy)
    228 			     && !m_at(sx,sy) && !nexttodoor(sx,sy)){
    229 			levl[sx][sy].typ = POOL;
    230 			levl[sx][sy].scrsym = POOL_SYM;
    231 			if(!eelct || !rn2(4)) {
    232 				(void) makemon(PM_EEL, sx, sy);
    233 				eelct++;
    234 			}
    235 		}
    236 	}
    237 }
    238 
    239 nexttodoor(sx,sy)
    240 register sx,sy;
    241 {
    242 	register dx,dy;
    243 	register struct rm *lev;
    244 	for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
    245 		if((lev = &levl[sx+dx][sy+dy])->typ == DOOR ||
    246 		    lev->typ == SDOOR || lev->typ == LDOOR)
    247 			return(1);
    248 	return(0);
    249 }
    250 
    251 has_dnstairs(sroom)
    252 register struct mkroom *sroom;
    253 {
    254 	return(sroom->lx <= xdnstair && xdnstair <= sroom->hx &&
    255 		   sroom->ly <= ydnstair && ydnstair <= sroom->hy);
    256 }
    257 
    258 has_upstairs(sroom)
    259 register struct mkroom *sroom;
    260 {
    261 	return(sroom->lx <= xupstair && xupstair <= sroom->hx &&
    262 		   sroom->ly <= yupstair && yupstair <= sroom->hy);
    263 }
    264 
    265 isbig(sroom)
    266 register struct mkroom *sroom;
    267 {
    268 	register int area = (sroom->hx - sroom->lx) * (sroom->hy - sroom->ly);
    269 	return( area > 20 );
    270 }
    271 
    272 dist2(x0,y0,x1,y1){
    273 	return((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1));
    274 }
    275 
    276 sq(a) int a; {
    277 	return(a*a);
    278 }
    279 #endif QUEST
    280