Source:Hack 1.0/savelev.h

From NetHackWiki
Jump to: navigation, search

Below is the full text to savelev.h from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/savelev.h#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.    #ifdef MKLEV
4.    
5.    savelev(){
6.    	int fd;
7.    
8.    #else
9.    extern struct obj *billobjs;
10.   
11.   savelev(fd){
12.   #ifndef NOWORM
13.   	register struct wseg *wtmp, *wtmp2;
14.   	register tmp;
15.   #endif NOWORM
16.   
17.   #endif MKLEV
18.   
19.   #ifdef MKLEV
20.   	if((fd = creat(tfile,FMASK)) < 0) panic("Cannot create %s\n", tfile);
21.   #else
22.   	if(fd < 0) panic("Save on bad file!");
23.   #endif MKLEV
24.   	bwrite(fd,(char *) levl,sizeof(levl));
25.   #ifdef MKLEV
26.   	bwrite(fd,(char *) nul,sizeof(long));
27.   #else
28.   	bwrite(fd,(char *) &moves,sizeof(long));
29.   #endif MKLEV
30.   	bwrite(fd,(char *) &xupstair,sizeof(xupstair));
31.   	bwrite(fd,(char *) &yupstair,sizeof(yupstair));
32.   	bwrite(fd,(char *) &xdnstair,sizeof(xdnstair));
33.   	bwrite(fd,(char *) &ydnstair,sizeof(ydnstair));
34.   	savemonchn(fd, fmon);
35.   	savegenchn(fd, fgold);
36.   	savegenchn(fd, ftrap);
37.   	saveobjchn(fd, fobj);
38.   #ifdef MKLEV
39.   	saveobjchn(fd, (struct obj *) 0);
40.   	bwrite(fd,(char *) nul, sizeof(unsigned));
41.   #else
42.   	saveobjchn(fd, billobjs);
43.   	billobjs = 0;
44.   	save_engravings(fd);
45.   #endif MKLEV
46.   #ifndef QUEST
47.   	bwrite(fd,(char *) rooms,sizeof(rooms));
48.   	bwrite(fd,(char *) doors,sizeof(doors));
49.   #endif QUEST
50.   	fgold = ftrap = 0;
51.   	fmon = 0;
52.   	fobj = 0;
53.   #ifndef MKLEV
54.   /*--------------------------------------------------------------------*/
55.   #ifndef NOWORM
56.   	bwrite(fd,(char *) wsegs,sizeof(wsegs));
57.   	for(tmp=1; tmp<32; tmp++){
58.   		for(wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2){
59.   			wtmp2 = wtmp->nseg;
60.   			bwrite(fd,(char *) wtmp,sizeof(struct wseg));
61.   		}
62.   		wsegs[tmp] = 0;
63.   	}
64.   	bwrite(fd,(char *) wgrowtime,sizeof(wgrowtime));
65.   #endif NOWORM
66.   /*--------------------------------------------------------------------*/
67.   #endif MKLEV
68.   }
69.   
70.   bwrite(fd,loc,num)
71.   register fd;
72.   register char *loc;
73.   register unsigned num;
74.   {
75.   /* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
76.   	if(write(fd, loc, (int) num) != num)
77.   		panic("cannot write %d bytes to file #%d",num,fd);
78.   }
79.   
80.   saveobjchn(fd,otmp)
81.   register fd;
82.   register struct obj *otmp;
83.   {
84.   	register struct obj *otmp2;
85.   	unsigned xl;
86.   	int minusone = -1;
87.   
88.   	while(otmp) {
89.   		otmp2 = otmp->nobj;
90.   		xl = otmp->onamelth;
91.   		bwrite(fd, (char *) &xl, sizeof(int));
92.   		bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
93.   		free((char *) otmp);
94.   		otmp = otmp2;
95.   	}
96.   	bwrite(fd, (char *) &minusone, sizeof(int));
97.   }
98.   
99.   savemonchn(fd,mtmp)
100.  register fd;
101.  register struct monst *mtmp;
102.  {
103.  	register struct monst *mtmp2;
104.  	unsigned xl;
105.  	int minusone = -1;
106.  	struct permonst *monbegin = &mons[0];
107.  
108.  	bwrite(fd, (char *) &monbegin, sizeof(monbegin));
109.  
110.  	while(mtmp) {
111.  		mtmp2 = mtmp->nmon;
112.  		xl = mtmp->mxlth + mtmp->mnamelth;
113.  		bwrite(fd, (char *) &xl, sizeof(int));
114.  		bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
115.  		if(mtmp->minvent) saveobjchn(fd,mtmp->minvent);
116.  		free((char *) mtmp);
117.  		mtmp = mtmp2;
118.  	}
119.  	bwrite(fd, (char *) &minusone, sizeof(int));
120.  }
121.  
122.  savegenchn(fd,gtmp)
123.  register fd;
124.  register struct gen *gtmp;
125.  {
126.  	register struct gen *gtmp2;
127.  	while(gtmp) {
128.  		gtmp2 = gtmp->ngen;
129.  		bwrite(fd, (char *) gtmp, sizeof(struct gen));
130.  		free((char *) gtmp);
131.  		gtmp = gtmp2;
132.  	}
133.  	bwrite(fd, nul, sizeof(struct gen));
134.  }