Source:NetHack 1.3d/mkmaze.c

From NetHackWiki
Revision as of 00:01, 4 March 2008 by Kernigh bot (talk | contribs) (NetHack 1.3d/mkmaze.c moved to Source:NetHack 1.3d/mkmaze.c: Robot: moved page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Below is the full text to mkmaze.c from the source code of NetHack 1.3d. To link to a particular line, write [[NetHack 1.3d/mkmaze.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

Screenshots and source code from Hack are used under the CWI license.

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