pc-hack

PC HACK 3.61 source code (archival)
git clone http://frotz.net/git/pc-hack.git
Log | Files | Refs

mkmaze.c (4040B)


      1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
      2 /* mkmaze.c - version 1.0.2 */
      3 
      4 #include "hack.h"
      5 #include "mkroom.h"		/* not really used */
      6 extern struct monst *makemon();
      7 extern struct permonst pm_wizard;
      8 extern struct obj *mkobj_at();
      9 extern coord mazexy();
     10 struct permonst hell_hound =
     11 	{ "hell hound", 'd', 12, 14, 2, 3, 6, 0 };
     12 
     13 makemaz()
     14 {
     15 	int x,y;
     16 	register zx,zy;
     17 	coord mm;
     18 	boolean al = (dlevel >= 30 && !flags.made_amulet);
     19 
     20 	for(x = 2; x < COLNO-1; x++)
     21 		for(y = 2; y < ROWNO-1; y++)
     22 			levl[x][y].typ = (x%2 && y%2) ? 0 : HWALL;
     23 	if(al) {
     24 	    register struct monst *mtmp;
     25 
     26 	    zx = 2*(COLNO/4) - 1;
     27 	    zy = 2*(ROWNO/4) - 1;
     28 	    for(x = zx-2; x < zx+4; x++) for(y = zy-2; y <= zy+2; y++) {
     29 		levl[x][y].typ =
     30 		    (y == zy-2 || y == zy+2 || x == zx-2 || x == zx+3) ? POOL :
     31 		    (y == zy-1 || y == zy+1 || x == zx-1 || x == zx+2) ? HWALL:
     32 		    ROOM;
     33 	    }
     34 	    (void) mkobj_at(AMULET_SYM, zx, zy);
     35 	    flags.made_amulet = 1;
     36 	    walkfrom(zx+4, zy);
     37 	    if(mtmp = makemon(&hell_hound, zx, zy))
     38 		mtmp->msleep = 1;
     39 	    if(mtmp = makemon(PM_WIZARD, zx+1, zy)) {
     40 		mtmp->msleep = 1;
     41 		flags.no_of_wizards = 1;
     42 	    }
     43 	} else {
     44 	    mm = mazexy();
     45 	    zx = mm.x;
     46 	    zy = mm.y;
     47 	    walkfrom(zx,zy);
     48 	    (void) mksobj_at(WAN_WISHING, zx, zy);
     49 	    (void) mkobj_at(ROCK_SYM, zx, zy);	/* put a rock on top of it */
     50 	}
     51 
     52 	for(x = 2; x < COLNO-1; x++)
     53 		for(y = 2; y < ROWNO-1; y++) {
     54 			switch(levl[x][y].typ) {
     55 #ifdef DGK
     56 			case HWALL:
     57 				levl[x][y].scrsym = symbol.hwall;
     58 				break;
     59 			case ROOM:
     60 				levl[x][y].scrsym = symbol.room;
     61 				break;
     62 #else
     63 			case HWALL:
     64 				levl[x][y].scrsym = '-';
     65 				break;
     66 			case ROOM:
     67 				levl[x][y].scrsym = '.';
     68 				break;
     69 #endif /* DGK /**/
     70 			}
     71 		}
     72 	for(x = rn1(8,11); x; x--) {
     73 		mm = mazexy();
     74 		(void) mkobj_at(rn2(2) ? GEM_SYM : 0, mm.x, mm.y);
     75 	}
     76 	for(x = rn1(10,2); x; x--) {
     77 		mm = mazexy();
     78 		(void) mkobj_at(ROCK_SYM, mm.x, mm.y);
     79 	}
     80 	mm = mazexy();
     81 	(void) makemon(PM_MINOTAUR, mm.x, mm.y);
     82 	for(x = rn1(5,7); x; x--) {
     83 		mm = mazexy();
     84 		(void) makemon((struct permonst *) 0, mm.x, mm.y);
     85 	}
     86 	for(x = rn1(6,7); x; x--) {
     87 		mm = mazexy();
     88 		mkgold(0L,mm.x,mm.y);
     89 	}
     90 	for(x = rn1(6,7); x; x--)
     91 		mktrap(0,1,(struct mkroom *) 0);
     92 	mm = mazexy();
     93 	levl[(xupstair = mm.x)][(yupstair = mm.y)].scrsym = '<';
     94 	levl[xupstair][yupstair].typ = STAIRS;
     95 	xdnstair = ydnstair = 0;
     96 }
     97 
     98 #ifdef DGK
     99 /* Make the mazewalk iterative by faking a stack.  This is needed to
    100  * ensure the mazewalk is successful in the limited stack space of
    101  * the program.  This iterative version uses the mimumum amount of stack
    102  * that is totally safe.
    103  */
    104 walkfrom(x,y)
    105 int x,y;
    106 {
    107 #define CELLS (ROWNO * COLNO) / 4		/* a maze cell is 4 squares */
    108 	char mazex[CELLS + 1], mazey[CELLS + 1];	/* char's are OK */
    109 	int q, a, dir, pos;
    110 	int dirs[4];
    111 
    112 	pos = 1;
    113 	mazex[pos] = (char) x;
    114 	mazey[pos] = (char) y;
    115 	while (pos) {
    116 		x = (int) mazex[pos];
    117 		y = (int) mazey[pos];
    118 		levl[x][y].typ = ROOM;
    119 		q = 0;
    120 		for (a = 0; a < 4; a++)
    121 			if(okay(x, y, a)) dirs[q++]= a;
    122 		if (!q)
    123 			pos--;
    124 		else {
    125 			dir = dirs[rn2(q)];
    126 			move(&x, &y, dir);
    127 			levl[x][y].typ = ROOM;
    128 			move(&x, &y, dir);
    129 			pos++;
    130 			if (pos > CELLS)
    131 				panic("Overflow in walkfrom");
    132 			mazex[pos] = (char) x;
    133 			mazey[pos] = (char) y;
    134 		}
    135 	}
    136 }
    137 #else
    138 
    139 walkfrom(x,y) int x,y; {
    140 register int q,a,dir;
    141 int dirs[4];
    142 	levl[x][y].typ = ROOM;
    143 	while(1) {
    144 		q = 0;
    145 		for(a = 0; a < 4; a++)
    146 			if(okay(x,y,a)) dirs[q++]= a;
    147 		if(!q) return;
    148 		dir = dirs[rn2(q)];
    149 		move(&x,&y,dir);
    150 		levl[x][y].typ = ROOM;
    151 		move(&x,&y,dir);
    152 		walkfrom(x,y);
    153 	}
    154 }
    155 #endif /* DGK */
    156 
    157 move(x,y,dir)
    158 register int *x, *y;
    159 register int dir;
    160 {
    161 	switch(dir){
    162 		case 0: --(*y); break;
    163 		case 1: (*x)++; break;
    164 		case 2: (*y)++; break;
    165 		case 3: --(*x); break;
    166 	}
    167 }
    168 
    169 okay(x,y,dir)
    170 int x,y;
    171 register int dir;
    172 {
    173 	move(&x,&y,dir);
    174 	move(&x,&y,dir);
    175 	if(x<3 || y<3 || x>COLNO-3 || y>ROWNO-3 || levl[x][y].typ != 0)
    176 		return(0);
    177 	else
    178 		return(1);
    179 }
    180 
    181 coord
    182 mazexy(){
    183 	coord mm;
    184 	mm.x = 3 + 2*rn2(COLNO/2 - 2);
    185 	mm.y = 3 + 2*rn2(ROWNO/2 - 2);
    186 	return mm;
    187 }