Source:Hack 1.0/hack.lev.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to hack.lev.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.lev.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.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2.    
3.    #include "hack.h"
4.    #include <signal.h>
5.    #include <stdio.h>
6.    extern struct monst *restmonchn();
7.    extern struct obj *restobjchn();
8.    extern struct obj *billobjs;
9.    extern char *itoa();
10.   
11.   extern char nul[];
12.   #ifndef NOWORM
13.   #include	"def.wseg.h"
14.   
15.   extern struct wseg *wsegs[32], *wheads[32];
16.   extern long wgrowtime[32];
17.   #endif NOWORM
18.   
19.   #include "savelev.h"
20.   
21.   getlev(fd)
22.   {
23.   	register struct gen *gtmp;
24.   #ifndef NOWORM
25.   	register struct wseg *wtmp;
26.   #endif NOWORM
27.   	register tmp;
28.   	long omoves;
29.   
30.   	if(fd<0 || read(fd, (char *) levl, sizeof(levl)) != sizeof(levl))
31.   		return(1);
32.   	fgold = 0;
33.   	ftrap = 0;
34.   	mread(fd, (char *)&omoves, sizeof(omoves));	/* 0 from MKLEV */
35.   	mread(fd, (char *)&xupstair, sizeof(xupstair));
36.   	mread(fd, (char *)&yupstair, sizeof(yupstair));
37.   	mread(fd, (char *)&xdnstair, sizeof(xdnstair));
38.   	mread(fd, (char *)&ydnstair, sizeof(ydnstair));
39.   
40.   	fmon = restmonchn(fd);
41.   	if(omoves) {
42.   	    /* regenerate animals while on another level */
43.   	    long tmoves = (moves > omoves) ? moves-omoves : 0;
44.   	    register struct monst *mtmp, *mtmp2;
45.   	    extern char genocided[];
46.   
47.   	    for(mtmp = fmon; mtmp; mtmp = mtmp2) {
48.   		mtmp2 = mtmp->nmon;
49.   		if(index(genocided, mtmp->data->mlet)) {
50.   			mondead(mtmp);
51.   			continue;
52.   		}
53.   		if(index("ViT", mtmp->data->mlet))
54.   			mtmp->mhp += tmoves;
55.   		else
56.   			mtmp->mhp += tmoves/20;
57.   		if(mtmp->mhp > mtmp->orig_hp)
58.   			mtmp->mhp = mtmp->orig_hp;
59.   	    }
60.   	}
61.   
62.   	setshk();
63.   	setgd();
64.   	gtmp = newgen();
65.   	mread(fd, (char *)gtmp, sizeof(struct gen));
66.   	while(gtmp->gx) {
67.   		gtmp->ngen = fgold;
68.   		fgold = gtmp;
69.   		gtmp = newgen();
70.   		mread(fd, (char *)gtmp, sizeof(struct gen));
71.   	}
72.   	mread(fd, (char *)gtmp, sizeof(struct gen));
73.   	while(gtmp->gx) {
74.   		gtmp->ngen = ftrap;
75.   		ftrap = gtmp;
76.   		gtmp = newgen();
77.   		mread(fd, (char *)gtmp, sizeof(struct gen));
78.   	}
79.   	free((char *) gtmp);
80.   	fobj = restobjchn(fd);
81.   	billobjs = restobjchn(fd);
82.   	rest_engravings(fd);
83.   #ifndef QUEST
84.   	mread(fd, (char *)rooms, sizeof(rooms));
85.   	mread(fd, (char *)doors, sizeof(doors));
86.   #endif QUEST
87.   	if(!omoves) return(0);	/* from MKLEV */
88.   #ifndef NOWORM
89.   	mread(fd, (char *)wsegs, sizeof(wsegs));
90.   	for(tmp = 1; tmp < 32; tmp++) if(wsegs[tmp]){
91.   		wheads[tmp] = wsegs[tmp] = wtmp = newseg();
92.   		while(1) {
93.   			mread(fd, (char *)wtmp, sizeof(struct wseg));
94.   			if(!wtmp->nseg) break;
95.   			wheads[tmp]->nseg = wtmp = newseg();
96.   			wheads[tmp] = wtmp;
97.   		}
98.   	}
99.   	mread(fd, (char *)wgrowtime, sizeof(wgrowtime));
100.  #endif NOWORM
101.  	return(0);
102.  }
103.  
104.  mread(fd, buf, len)
105.  register fd;
106.  register char *buf;
107.  register unsigned len;
108.  {
109.  register int rlen;
110.  	rlen = read(fd, buf, (int) len);
111.  	if(rlen != len){
112.  		pline("Read %d instead of %d bytes\n", rlen, len);
113.  		panic("Cannot read %d bytes from file #%d\n", len, fd);
114.  	}
115.  }
116.  
117.  #ifdef BSD
118.  #include	<sys/wait.h>
119.  #else
120.  #include	<wait.h>
121.  #endif BSD
122.  
123.  mklev()
124.  {
125.  	register int fd;
126.  	char type[2];
127.  	union wait status;
128.  	extern char fut_geno[];
129.  
130.  	if(getbones()) return;
131.  	if(dlevel < rn1(3, 26)) type[0] = 'a';	/* normal level */
132.  	else type[0] = 'b';			/* maze */
133.  	type[1] = 0;
134.  	switch(fork()){
135.  	case 0:
136.  		(void) signal(SIGINT, SIG_IGN);
137.  		(void) signal(SIGQUIT, SIG_IGN);
138.  		execl("./mklev", "mklev", lock, type, itoa(dlevel), fut_geno, 
139.  #ifdef WIZARD
140.  			wizard ? "w" :
141.  #endif WIZARD
142.  					"", (char *) 0);
143.  		exit(2);
144.  	case -1:
145.  		settty("Cannot fork!\n");
146.  		exit(1);
147.  	default:
148.  		(void) fflush(stdout);	/* You fell into a trap ... */
149.  		(void) wait(&status);
150.  	}
151.  	if(status.w_status) {
152.  		if(status.w_coredump) {
153.  			settty("Mklev dumped core. Exiting...\n");
154.  			exit(1);
155.  		}
156.  		if(status.w_termsig) {
157.  			settty("Mklev killed by a signal. Exiting...\n");
158.  			exit(1);
159.  		}
160.  		if(status.w_retcode) {
161.  			if(status.w_retcode == 2) {
162.  				settty("Cannot execl mklev.\n");
163.  				exit(1);
164.  			}
165.  			pline("Mklev failed. Let's try again.");
166.  			mklev();
167.  			return;
168.  		}
169.  	}
170.  	if((fd = open(lock, 0)) < 0) {
171.  		pline("Can't open %s!", lock);
172.  		mklev();
173.  		return;
174.  	}
175.  	(void) getlev(fd);
176.  	(void) close(fd);
177.  }