Source:NetHack 3.2.0/dungeon.c

From NetHackWiki
Jump to navigation Jump to search

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

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

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)dungeon.c	3.2	96/03/10	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include "hack.h"
6.    #include "dgn_file.h"
7.    #include "dlb.h"
8.    
9.    #ifdef OVL1
10.   
11.   #define DUNGEON_FILE	"dungeon"
12.   
13.   #define X_START		"x-start"
14.   #define X_LOCATE	"x-locate"
15.   #define X_GOAL		"x-goal"
16.   
17.   struct proto_dungeon {
18.   	struct	tmpdungeon tmpdungeon[MAXDUNGEON];
19.   	struct	tmplevel   tmplevel[LEV_LIMIT];
20.   	s_level *final_lev[LEV_LIMIT];	/* corresponding level pointers */
21.   	struct	tmpbranch  tmpbranch[BRANCH_LIMIT];
22.   
23.   	int	start;	/* starting index of current dungeon sp levels */
24.   	int	n_levs;	/* number of tmplevel entries */
25.   	int	n_brs;	/* number of tmpbranch entries */
26.   };
27.   
28.   int n_dgns;				/* number of dungeons (used here,  */
29.   					/*   and mklev.c)		   */
30.   static branch *branches = (branch *) 0;	/* dungeon branch list		   */
31.   
32.   static void FDECL(Fread, (genericptr_t, int, int, dlb *));
33.   static xchar FDECL(dname_to_dnum, (const char *));
34.   static int FDECL(find_branch, (const char *, struct proto_dungeon *));
35.   static xchar FDECL(parent_dnum, (const char *, struct proto_dungeon *));
36.   static int FDECL(level_range, (XCHAR_P,int,int,int,struct proto_dungeon *,int *));
37.   static xchar FDECL(parent_dlevel, (const char *, struct proto_dungeon *));
38.   static int FDECL(correct_branch_type, (struct tmpbranch *));
39.   static branch *FDECL(add_branch, (int, int, struct proto_dungeon *));
40.   static void FDECL(add_level, (s_level *));
41.   static void FDECL(init_level, (int,int,struct proto_dungeon *));
42.   static int FDECL(possible_places, (int, boolean *, struct proto_dungeon *));
43.   static xchar FDECL(pick_level, (boolean *, int));
44.   static boolean FDECL(place_level, (int, struct proto_dungeon *));
45.   #ifdef WIZARD
46.   static const char *FDECL(br_string, (int));
47.   static void FDECL(print_branch, (winid, int, int, int));
48.   #endif
49.   
50.   #ifdef DEBUG
51.   #define DD	dungeons[i]
52.   static void NDECL(dumpit);
53.   
54.   static void
55.   dumpit()
56.   {
57.   	int	i;
58.   	s_level	*x;
59.   	branch *br;
60.   
61.   	for(i = 0; i < n_dgns; i++)  {
62.   	    fprintf(stderr, "\n#%d \"%s\" (%s):\n", i,
63.   				DD.dname, DD.proto);
64.   	    fprintf(stderr, "    num_dunlevs %d, dunlev_ureached %d\n",
65.   				DD.num_dunlevs, DD.dunlev_ureached);
66.   	    fprintf(stderr, "    depth_start %d, ledger_start %d\n",
67.   				DD.depth_start, DD.ledger_start);
68.   	    fprintf(stderr, "    flags:%s%s%s\n",
69.   		    DD.flags.rogue_like ? " rogue_like" : "",
70.   		    DD.flags.maze_like  ? " maze_like"  : "",
71.   		    DD.flags.hellish    ? " hellish"    : "");
72.   	    getchar();
73.   	}
74.   	fprintf(stderr,"\nSpecial levels:\n");
75.   	for(x = sp_levchn; x; x = x->next) {
76.   	    fprintf(stderr, "%s (%d): ", x->proto, x->rndlevs);
77.   	    fprintf(stderr, "on %d, %d; ", x->dlevel.dnum, x->dlevel.dlevel);
78.   	    fprintf(stderr, "flags:%s%s%s%s\n",
79.   		    x->flags.rogue_like	? " rogue_like" : "",
80.   		    x->flags.maze_like  ? " maze_like"  : "",
81.   		    x->flags.hellish    ? " hellish"    : "",
82.   		    x->flags.town       ? " town"       : "");
83.   	    getchar();
84.   	}
85.   	fprintf(stderr,"\nBranches:\n");
86.   	for (br = branches; br; br = br->next) {
87.   	    fprintf(stderr, "%d: %s, end1 %d %d, end2 %d %d, %s\n",
88.   		br->id,
89.   		br->type == BR_STAIR ? "stair" :
90.   		    br->type == BR_NO_END1 ? "no end1" :
91.   		    br->type == BR_NO_END2 ? "no end2" :
92.   		    br->type == BR_PORTAL  ? "portal"  :
93.   					     "unknown",
94.   		br->end1.dnum, br->end1.dlevel,
95.   		br->end2.dnum, br->end2.dlevel,
96.   		br->end1_up ? "end1 up" : "end1 down");
97.   	}
98.   	getchar();
99.   	fprintf(stderr,"\nDone\n");
100.  	getchar();
101.  }
102.  #endif
103.  
104.  /* Save the dungeon structures. */
105.  void
106.  save_dungeon(fd, perform_write, free_data)
107.      int fd;
108.      boolean perform_write, free_data;
109.  {
110.      branch *curr, *next;
111.      int    count;
112.  
113.      if (perform_write) {
114.  	bwrite(fd, (genericptr_t) &n_dgns, sizeof n_dgns);
115.  	bwrite(fd, (genericptr_t) dungeons, sizeof(dungeon) * (unsigned)n_dgns);
116.  	bwrite(fd, (genericptr_t) &dungeon_topology, sizeof dungeon_topology);
117.  	bwrite(fd, (genericptr_t) tune, sizeof tune);
118.  
119.  	for (count = 0, curr = branches; curr; curr = curr->next)
120.  	    count++;
121.  	bwrite(fd, (genericptr_t) &count, sizeof(count));
122.  
123.  	for (curr = branches; curr; curr = curr->next)
124.  	    bwrite(fd, (genericptr_t) curr, sizeof (branch));
125.  
126.  	count = maxledgerno();
127.  	bwrite(fd, (genericptr_t) &count, sizeof count);
128.  	bwrite(fd, (genericptr_t) level_info,
129.  			(unsigned)count * sizeof (struct linfo));
130.  	bwrite(fd, (genericptr_t) &inv_pos, sizeof inv_pos);
131.      }
132.  
133.      if (free_data) {
134.  	for (curr = branches; curr; curr = next) {
135.  	    next = curr->next;
136.  	    free((genericptr_t) curr);
137.  	}
138.  	branches = 0;
139.      }
140.  }
141.  
142.  /* Restore the dungeon structures. */
143.  void
144.  restore_dungeon(fd)
145.      int fd;
146.  {
147.      branch *curr, *last;
148.      int    count, i;
149.  
150.      mread(fd, (genericptr_t) &n_dgns, sizeof(n_dgns));
151.      mread(fd, (genericptr_t) dungeons, sizeof(dungeon) * (unsigned)n_dgns);
152.      mread(fd, (genericptr_t) &dungeon_topology, sizeof dungeon_topology);
153.      mread(fd, (genericptr_t) tune, sizeof tune);
154.  
155.      last = branches = (branch *) 0;
156.  
157.      mread(fd, (genericptr_t) &count, sizeof(count));
158.      for (i = 0; i < count; i++) {
159.  	curr = (branch *) alloc(sizeof(branch));
160.  	mread(fd, (genericptr_t) curr, sizeof(branch));
161.  	curr->next = (branch *) 0;
162.  	if (last)
163.  	    last->next = curr;
164.  	else
165.  	    branches = curr;
166.  	last = curr;
167.      }
168.  
169.      mread(fd, (genericptr_t) &count, sizeof(count));
170.      if (count >= MAXLINFO)
171.  	panic("level information count larger (%d) than allocated size", count);
172.      mread(fd, (genericptr_t) level_info, (unsigned)count*sizeof(struct linfo));
173.      mread(fd, (genericptr_t) &inv_pos, sizeof inv_pos);
174.  }
175.  
176.  static void
177.  Fread(ptr, size, nitems, stream)
178.  	genericptr_t	ptr;
179.  	int	size, nitems;
180.  	dlb	*stream;
181.  {
182.  	int cnt;
183.  
184.  	if((cnt = dlb_fread(ptr, size, nitems, stream)) != nitems) {
185.  	    panic(
186.   "Premature EOF on dungeon description file!\r\nExpected %d bytes - got %d.",
187.  		  (size * nitems), (size * cnt));
188.  	    terminate(EXIT_FAILURE);
189.  	}
190.  }
191.  
192.  static xchar
193.  dname_to_dnum(s)
194.  const char	*s;
195.  {
196.  	xchar	i;
197.  
198.  	for (i = 0; i < n_dgns; i++)
199.  	    if (!strcmp(dungeons[i].dname, s)) return i;
200.  
201.  	panic("Couldn't resolve dungeon number for name \"%s\".", s);
202.  	/*NOT REACHED*/
203.  	return (xchar)0;
204.  }
205.  
206.  s_level *
207.  find_level(s)
208.  	const char *s;
209.  {
210.  	s_level *curr;
211.  	for(curr = sp_levchn; curr; curr = curr->next)
212.  	    if (!strcmpi(s, curr->proto)) break;
213.  	return curr;
214.  }
215.  
216.  /* Find the branch that links the named dungeon. */
217.  static int
218.  find_branch(s, pd)
219.  	const char *s;		/* dungeon name */
220.  	struct proto_dungeon *pd;
221.  {
222.  	int i;
223.  
224.  	if (pd) {
225.  	    for (i = 0; i < pd->n_brs; i++)
226.  		if (!strcmp(pd->tmpbranch[i].name, s)) break;
227.  	    if (i == pd->n_brs) panic("find_branch: can't find %s", s);
228.  	} else {
229.  	    /* support for level tport by name */
230.  	    branch *br;
231.  	    const char *dnam;
232.  
233.  	    for (br = branches; br; br = br->next) {
234.  		dnam = dungeons[br->end2.dnum].dname;
235.  		if (!strcmpi(dnam, s) ||
236.  			(!strncmpi(dnam, "The ", 4) && !strcmpi(dnam + 4, s)))
237.  		    break;
238.  	    }
239.  	    i = br ? ((ledger_no(&br->end1) << 8) | ledger_no(&br->end2)) : -1;
240.  	}
241.  	return i;
242.  }
243.  
244.  
245.  /*
246.   * Find the "parent" by searching the prototype branch list for the branch
247.   * listing, then figuring out to which dungeon it belongs.
248.   */
249.  static xchar
250.  parent_dnum(s, pd)
251.  const char	   *s;	/* dungeon name */
252.  struct proto_dungeon *pd;
253.  {
254.  	int	i;
255.  	xchar	pdnum;
256.  
257.  	i = find_branch(s, pd);
258.  	/*
259.  	 * Got branch, now find parent dungeon.  Stop if we have reached
260.  	 * "this" dungeon (if we haven't found it by now it is an error).
261.  	 */
262.  	for (pdnum = 0; strcmp(pd->tmpdungeon[pdnum].name, s); pdnum++)
263.  	    if ((i -= pd->tmpdungeon[pdnum].branches) < 0)
264.  		return(pdnum);
265.  
266.  	panic("parent_dnum: couldn't resolve branch.");
267.  	/*NOT REACHED*/
268.  	return (xchar)0;
269.  }
270.  
271.  /*
272.   * Return a starting point and number of successive positions a level
273.   * or dungeon entrance can occupy.
274.   *
275.   * Note: This follows the acouple (instead of the rcouple) rules for a
276.   *	 negative random component (rand < 0).  These rules are found
277.   *	 in dgn_comp.y.  The acouple [absolute couple] section says that
278.   *	 a negative random component means from the (adjusted) base to the
279.   *	 end of the dungeon.
280.   */
281.  static int
282.  level_range(dgn, base, rand, chain, pd, adjusted_base)
283.  	xchar	dgn;
284.  	int	base, rand, chain;
285.  	struct proto_dungeon *pd;
286.  	int *adjusted_base;
287.  {
288.  	int lmax = dungeons[dgn].num_dunlevs;
289.  
290.  	if (chain >= 0) {		 /* relative to a special level */
291.  	    s_level *levtmp = pd->final_lev[chain];
292.  	    if (!levtmp) panic("level_range: empty chain level!");
293.  
294.  	    base += levtmp->dlevel.dlevel;
295.  	} else {			/* absolute in the dungeon */
296.  	    /* from end of dungeon */
297.  	    if (base < 0) base = (lmax + base + 1);
298.  	}
299.  
300.  	if (base < 1 || base > lmax)
301.  	    panic("level_range: base value out of range");
302.  
303.  	*adjusted_base = base;
304.  
305.  	if (rand == -1) {	/* from base to end of dungeon */
306.  	    return (lmax - base + 1);
307.  	} else if (rand) {
308.  	    /* make sure we don't run off the end of the dungeon */
309.  	    return (((base + rand - 1) > lmax) ? lmax-base+1 : rand);
310.  	} /* else only one choice */
311.  	return 1;
312.  }
313.  
314.  static xchar
315.  parent_dlevel(s, pd)
316.  	const char	*s;
317.  	struct proto_dungeon *pd;
318.  {
319.  	int i, num, base;
320.  
321.  	i = find_branch(s, pd);
322.  	num = level_range(parent_dnum(s, pd), pd->tmpbranch[i].lev.base,
323.  					      pd->tmpbranch[i].lev.rand,
324.  					      pd->tmpbranch[i].chain,
325.  					      pd, &base);
326.  	return (xchar) rn1(num,base);
327.  }
328.  
329.  /* Convert from the temporary branch type to the dungeon branch type. */
330.  static int
331.  correct_branch_type(tbr)
332.      struct tmpbranch *tbr;
333.  {
334.      switch (tbr->type) {
335.  	case TBR_STAIR:		return BR_STAIR;
336.  	case TBR_NO_UP:		return tbr->up ? BR_NO_END1 : BR_NO_END2;
337.  	case TBR_NO_DOWN:	return tbr->up ? BR_NO_END2 : BR_NO_END1;
338.  	case TBR_PORTAL:	return BR_PORTAL;
339.      }
340.      impossible("correct_branch_type: unknown branch type");
341.      return BR_STAIR;
342.  }
343.  
344.  /*
345.   * Add the given branch to the branch list.  The branch list is ordered
346.   * by end1 dungeon and level followed by end2 dungeon and level.  If
347.   * extract_first is true, then the branch is already part of the list
348.   * but needs to be repositioned.
349.   */
350.  void
351.  insert_branch(new_branch, extract_first)
352.     branch *new_branch;
353.     boolean extract_first;
354.  {
355.      branch *curr, *prev;
356.      long new_val, curr_val, prev_val;
357.  
358.      if (extract_first) {
359.  	for (prev = 0, curr = branches; curr; prev = curr, curr = curr->next)
360.  	    if (curr == new_branch) break;
361.  
362.  	if (!curr) panic("insert_branch: not found");
363.  	if (prev)
364.  	    prev->next = curr->next;
365.  	else
366.  	    branches = curr->next;
367.      }
368.      new_branch->next = (branch *) 0;
369.  
370.  /* Convert the branch into a unique number so we can sort them. */
371.  #define branch_val(bp) ((((long)(bp)->end1.dnum * (MAXLEVEL+1) + (long)(bp)->end1.dlevel) * (MAXDUNGEON+1) * (MAXLEVEL+1)) + ((long)(bp)->end2.dnum * (MAXLEVEL+1) + (long)(bp)->end2.dlevel))
372.  
373.      /*
374.       * Insert the new branch into the correct place in the branch list.
375.       */
376.      prev = (branch *) 0;
377.      prev_val = -1;
378.      new_val = branch_val(new_branch);
379.      for (curr = branches; curr;
380.  		    prev_val = curr_val, prev = curr, curr = curr->next) {
381.  	curr_val = branch_val(curr);
382.  	if (prev_val < new_val && new_val <= curr_val) break;
383.      }
384.      if (prev) {
385.  	new_branch->next = curr;
386.  	prev->next = new_branch;
387.      } else {
388.  	new_branch->next = branches;
389.  	branches = new_branch;
390.      }
391.  }
392.  
393.  /* Add a dungeon branch to the branch list. */
394.  static branch *
395.  add_branch(dgn, child_entry_level, pd)
396.      int dgn;
397.      int child_entry_level;
398.      struct proto_dungeon *pd;
399.  {
400.      static int branch_id = 0;
401.      int branch_num;
402.      branch *new_branch;
403.  
404.      branch_num = find_branch(dungeons[dgn].dname,pd);
405.      new_branch = (branch *) alloc(sizeof(branch));
406.      new_branch->next = (branch *) 0;
407.      new_branch->id = branch_id++;
408.      new_branch->type = correct_branch_type(&pd->tmpbranch[branch_num]);
409.      new_branch->end1.dnum = parent_dnum(dungeons[dgn].dname, pd);
410.      new_branch->end1.dlevel = parent_dlevel(dungeons[dgn].dname, pd);
411.      new_branch->end2.dnum = dgn;
412.      new_branch->end2.dlevel = child_entry_level;
413.      new_branch->end1_up = pd->tmpbranch[branch_num].up ? TRUE : FALSE;
414.  
415.      insert_branch(new_branch, FALSE);
416.      return new_branch;
417.  }
418.  
419.  /*
420.   * Add new level to special level chain.  Insert it in level order with the
421.   * other levels in this dungeon.  This assumes that we are never given a
422.   * level that has a dungeon number less than the dungeon number of the
423.   * last entry.
424.   */
425.  static void
426.  add_level(new_lev)
427.      s_level *new_lev;
428.  {
429.  	s_level *prev, *curr;
430.  
431.  	prev = (s_level *) 0;
432.  	for (curr = sp_levchn; curr; curr = curr->next) {
433.  	    if (curr->dlevel.dnum == new_lev->dlevel.dnum &&
434.  		    curr->dlevel.dlevel > new_lev->dlevel.dlevel)
435.  		break;
436.  	    prev = curr;
437.  	}
438.  	if (!prev) {
439.  	    new_lev->next = sp_levchn;
440.  	    sp_levchn = new_lev;
441.  	} else {
442.  	    new_lev->next = curr;
443.  	    prev->next = new_lev;
444.  	}
445.  }
446.  
447.  static void
448.  init_level(dgn, proto_index, pd)
449.  	int dgn, proto_index;
450.  	struct proto_dungeon *pd;
451.  {
452.  	s_level	*new_level;
453.  	struct tmplevel *tlevel = &pd->tmplevel[proto_index];
454.  
455.  	pd->final_lev[proto_index] = (s_level *) 0; /* no "real" level */
456.  #ifdef WIZARD
457.  	if (!wizard)
458.  #endif
459.  	    if (tlevel->chance <= rn2(100)) return;
460.  
461.  	pd->final_lev[proto_index] = new_level =
462.  					(s_level *) alloc(sizeof(s_level));
463.  	/* load new level with data */
464.  	Strcpy(new_level->proto, tlevel->name);
465.  	new_level->boneid = tlevel->boneschar;
466.  	new_level->dlevel.dnum = dgn;
467.  	new_level->dlevel.dlevel = 0;	/* for now */
468.  
469.  	new_level->flags.town = !!(tlevel->flags & TOWN);
470.  	new_level->flags.hellish = !!(tlevel->flags & HELLISH);
471.  	new_level->flags.maze_like = !!(tlevel->flags & MAZELIKE);
472.  	new_level->flags.rogue_like = !!(tlevel->flags & ROGUELIKE);
473.  	new_level->flags.align = ((tlevel->flags & D_ALIGN_MASK) >> 4);
474.  
475.  	new_level->rndlevs = tlevel->rndlevs;
476.  	new_level->next    = (s_level *) 0;
477.  }
478.  
479.  static int
480.  possible_places(idx, map, pd)
481.      int idx;		/* prototype index */
482.      boolean *map;	/* array MAXLEVEL+1 in length */
483.      struct proto_dungeon *pd;
484.  {
485.      int i, start, count;
486.      s_level *lev = pd->final_lev[idx];
487.  
488.      /* init level possibilities */
489.      for (i = 0; i <= MAXLEVEL; i++) map[i] = FALSE;
490.  
491.      /* get base and range and set those entried to true */
492.      count = level_range(lev->dlevel.dnum, pd->tmplevel[idx].lev.base,
493.  					pd->tmplevel[idx].lev.rand,
494.  					pd->tmplevel[idx].chain,
495.  					pd, &start);
496.      for (i = start; i < start+count; i++)
497.  	map[i] = TRUE;
498.  
499.      /* mark off already placed levels */
500.      for (i = pd->start; i < idx; i++) {
501.  	if (pd->final_lev[i] && map[pd->final_lev[i]->dlevel.dlevel]) {
502.  	    map[pd->final_lev[i]->dlevel.dlevel] = FALSE;
503.  	    --count;
504.  	}
505.      }
506.  
507.      return count;
508.  }
509.  
510.  /* Pick the nth TRUE entry in the given boolean array. */
511.  static xchar
512.  pick_level(map, nth)
513.      boolean *map;	/* an array MAXLEVEL+1 in size */
514.      int nth;
515.  {
516.      int i;
517.      for (i = 1; i <= MAXLEVEL; i++)
518.  	if (map[i] && !nth--) return (xchar) i;
519.      panic("pick_level:  ran out of valid levels");
520.      return 0;
521.  }
522.  
523.  #ifdef DDEBUG
524.  static void FDECL(indent,(int));
525.  
526.  static void
527.  indent(d)
528.  int d;
529.  {
530.      while (d-- > 0) fputs("    ", stderr);
531.  }
532.  #endif
533.  
534.  /*
535.   * Place a level.  First, find the possible places on a dungeon map
536.   * template.  Next pick one.  Then try to place the next level.  If
537.   * sucessful, we're done.  Otherwise, try another (and another) until
538.   * all possible places have been tried.  If all possible places have
539.   * been exausted, return false.
540.   */
541.  static boolean
542.  place_level(proto_index, pd)
543.      int proto_index;
544.      struct proto_dungeon *pd;
545.  {
546.      boolean map[MAXLEVEL+1];	/* valid levels are 1..MAXLEVEL inclusive */
547.      s_level *lev;
548.      int npossible;
549.  #ifdef DDEBUG
550.      int i;
551.  #endif
552.  
553.      if (proto_index == pd->n_levs) return TRUE;	/* at end of proto levels */
554.  
555.      lev = pd->final_lev[proto_index];
556.  
557.      /* No level created for this prototype, goto next. */
558.      if (!lev) return place_level(proto_index+1, pd);
559.  
560.      npossible = possible_places(proto_index, map, pd);
561.  
562.      for (; npossible; --npossible) {
563.  	lev->dlevel.dlevel = pick_level(map, rn2(npossible));
564.  #ifdef DDEBUG
565.  	indent(proto_index-pd->start);
566.  	fprintf(stderr,"%s: trying %d [ ", lev->proto, lev->dlevel.dlevel);
567.  	for (i = 1; i <= MAXLEVEL; i++)
568.  	    if (map[i]) fprintf(stderr,"%d ", i);
569.  	fprintf(stderr,"]\n");
570.  #endif
571.  	if (place_level(proto_index+1, pd)) return TRUE;
572.  	map[lev->dlevel.dlevel] = FALSE;	/* this choice didn't work */
573.      }
574.  #ifdef DDEBUG
575.      indent(proto_index-pd->start);
576.      fprintf(stderr,"%s: failed\n", lev->proto);
577.  #endif
578.      return FALSE;
579.  }
580.  
581.  
582.  struct level_map {
583.  	const char *lev_name;
584.  	d_level *lev_spec;
585.  } level_map[] = {
586.  	{ "air",	&air_level },
587.  	{ "asmodeus",	&asmodeus_level },
588.  	{ "astral",	&astral_level },
589.  	{ "baalz",	&baalzebub_level },
590.  	{ "bigroom",	&bigroom_level },
591.  	{ "castle",	&stronghold_level },
592.  	{ "earth",	&earth_level },
593.  	{ "fakewiz1",	&portal_level },
594.  	{ "fire",	&fire_level },
595.  	{ "juiblex",	&juiblex_level },
596.  	{ "knox",	&knox_level },
597.  	{ "medusa",	&medusa_level },
598.  	{ "oracle",	&oracle_level },
599.  	{ "orcus",	&orcus_level },
600.  #ifdef REINCARNATION
601.  	{ "rogue",	&rogue_level },
602.  #endif
603.  	{ "sanctum",	&sanctum_level },
604.  	{ "valley",	&valley_level },
605.  	{ "water",	&water_level },
606.  	{ "wizard1",	&wiz1_level },
607.  	{ "wizard2",	&wiz2_level },
608.  	{ "wizard3",	&wiz3_level },
609.  	{ X_START,	&qstart_level },
610.  	{ X_LOCATE,	&qlocate_level },
611.  	{ X_GOAL,	&nemesis_level },
612.  	{ "",		(d_level *)0 }
613.  };
614.  
615.  void
616.  init_dungeons()		/* initialize the "dungeon" structs */
617.  {
618.  	dlb	*dgn_file;
619.  	register int i, cl = 0, cb = 0;
620.  	register s_level *x;
621.  	struct proto_dungeon pd;
622.  	struct level_map *lev_map;
623.  	long vers_info[3];
624.  
625.  	pd.n_levs = pd.n_brs = 0;
626.  
627.  	dgn_file = dlb_fopen(DUNGEON_FILE, RDBMODE);
628.  	if (!dgn_file)
629.  	    panic("Cannot open dungeon description file \"%s\"!", DUNGEON_FILE);
630.  
631.  	/* validate the data's version against the program's version */
632.  	Fread((genericptr_t) vers_info, sizeof vers_info, 1, dgn_file);
633.  	if (!check_version(vers_info, DUNGEON_FILE, TRUE))
634.  	    panic("Dungeon description not valid.");
635.  
636.  	/*
637.  	 * Read in each dungeon and transfer the results to the internal
638.  	 * dungeon arrays.
639.  	 */
640.  	sp_levchn = (s_level *) 0;
641.  	Fread((genericptr_t)&n_dgns, sizeof(int), 1, dgn_file);
642.  	if (n_dgns >= MAXDUNGEON)
643.  	    panic("init_dungeons: too many dungeons");
644.  
645.  	for (i = 0; i < n_dgns; i++) {
646.  	    Fread((genericptr_t)&pd.tmpdungeon[i],
647.  				    sizeof(struct tmpdungeon), 1, dgn_file);
648.  #ifdef WIZARD
649.  	    if(!wizard)
650.  #endif
651.  	      if(pd.tmpdungeon[i].chance && (pd.tmpdungeon[i].chance <= rn2(100))) {
652.  		int j;
653.  
654.  		/* skip over any levels or branches */
655.  		for(j = 0; j < pd.tmpdungeon[i].levels; j++)
656.  		    Fread((genericptr_t)&pd.tmplevel[cl], sizeof(struct tmplevel),
657.  							1, dgn_file);
658.  
659.  		for(j = 0; j < pd.tmpdungeon[i].branches; j++)
660.  		    Fread((genericptr_t)&pd.tmpbranch[cb],
661.  					sizeof(struct tmpbranch), 1, dgn_file);
662.  		n_dgns--; i--;
663.  		continue;
664.  	      }
665.  
666.  	    Strcpy(dungeons[i].dname, pd.tmpdungeon[i].name);
667.  	    Strcpy(dungeons[i].proto, pd.tmpdungeon[i].protoname);
668.  	    dungeons[i].boneid = pd.tmpdungeon[i].boneschar;
669.  
670.  	    if(pd.tmpdungeon[i].lev.rand)
671.  		dungeons[i].num_dunlevs = (xchar)rn1(pd.tmpdungeon[i].lev.rand,
672.  						     pd.tmpdungeon[i].lev.base);
673.  	    else dungeons[i].num_dunlevs = (xchar)pd.tmpdungeon[i].lev.base;
674.  
675.  	    if(!i) {
676.  		dungeons[i].ledger_start = 0;
677.  		dungeons[i].depth_start = 1;
678.  		dungeons[i].dunlev_ureached = 1;
679.  	    } else {
680.  		dungeons[i].ledger_start = dungeons[i-1].ledger_start +
681.  					      dungeons[i-1].num_dunlevs;
682.  		dungeons[i].dunlev_ureached = 0;
683.  	    }
684.  
685.  	    dungeons[i].flags.hellish = !!(pd.tmpdungeon[i].flags & HELLISH);
686.  	    dungeons[i].flags.maze_like = !!(pd.tmpdungeon[i].flags & MAZELIKE);
687.  	    dungeons[i].flags.rogue_like = !!(pd.tmpdungeon[i].flags & ROGUELIKE);
688.  	    dungeons[i].flags.align = ((pd.tmpdungeon[i].flags & D_ALIGN_MASK) >> 4);
689.  	    /*
690.  	     * Set the entry level for this dungeon.  The pd.tmpdungeon entry
691.  	     * value means:
692.  	     *		< 0	from bottom (-1 == bottom level)
693.  	     *		  0	default (top)
694.  	     *		> 0	actual level (1 = top)
695.  	     *
696.  	     * Note that the entry_lev field in the dungeon structure is
697.  	     * redundant.  It is used only here and in print_dungeon().
698.  	     */
699.  	    if (pd.tmpdungeon[i].entry_lev < 0) {
700.  		dungeons[i].entry_lev = dungeons[i].num_dunlevs +
701.  						pd.tmpdungeon[i].entry_lev + 1;
702.  		if (dungeons[i].entry_lev <= 0) dungeons[i].entry_lev = 1;
703.  	    } else if (pd.tmpdungeon[i].entry_lev > 0) {
704.  		dungeons[i].entry_lev = pd.tmpdungeon[i].entry_lev;
705.  		if (dungeons[i].entry_lev > dungeons[i].num_dunlevs)
706.  		    dungeons[i].entry_lev = pd.tmpdungeon[i].entry_lev;
707.  	    } else { /* default */
708.  		dungeons[i].entry_lev = 1;	/* defaults to top level */
709.  	    }
710.  
711.  	    if (i) {	/* set depth */
712.  		branch *br;
713.  		schar from_depth;
714.  		boolean from_up;
715.  
716.  		br = add_branch(i, dungeons[i].entry_lev, &pd);
717.  
718.  		/* Get the depth of the connecting end. */
719.  		if (br->end1.dnum == i) {
720.  		    from_depth = depth(&br->end2);
721.  		    from_up = !br->end1_up;
722.  		} else {
723.  		    from_depth = depth(&br->end1);
724.  		    from_up = br->end1_up;
725.  		}
726.  
727.  		/*
728.  		 * Calculate the depth of the top of the dungeon via
729.  		 * its branch.  First, the depth of the entry point:
730.  		 *
731.  		 *	depth of branch from "parent" dungeon
732.  		 *	+ -1 or 1 depending on a up or down stair or
733.  		 *	  0 if portal
734.  		 *
735.  		 * Followed by the depth of the top of the dungeon:
736.  		 *
737.  		 *	- (entry depth - 1)
738.  		 *
739.  		 * We'll say that portals stay on the same depth.
740.  		 */
741.  		dungeons[i].depth_start = from_depth
742.  					+ (br->type == BR_PORTAL ? 0 :
743.  							(from_up ? -1 : 1))
744.  					- (dungeons[i].entry_lev - 1);
745.  	    }
746.  
747.  	    /* this is redundant - it should have been flagged by dgn_comp */
748.  	    if(dungeons[i].num_dunlevs > MAXLEVEL)
749.  		dungeons[i].num_dunlevs = MAXLEVEL;
750.  
751.  	    pd.start = pd.n_levs;	/* save starting point */
752.  	    pd.n_levs += pd.tmpdungeon[i].levels;
753.  	    if (pd.n_levs > LEV_LIMIT)
754.  		panic("init_dungeon: too many special levels");
755.  	    /*
756.  	     * Read in the prototype special levels.  Don't add generated
757.  	     * special levels until they are all placed.
758.  	     */
759.  	    for(; cl < pd.n_levs; cl++) {
760.  		Fread((genericptr_t)&pd.tmplevel[cl],
761.  					sizeof(struct tmplevel), 1, dgn_file);
762.  		init_level(i, cl, &pd);
763.  	    }
764.  	    /*
765.  	     * Recursively place the generated levels for this dungeon.  This
766.  	     * routine will attempt all possible combinations before giving
767.  	     * up.
768.  	     */
769.  	    if (!place_level(pd.start, &pd))
770.  		panic("init_dungeon:  couldn't place levels");
771.  #ifdef DDEBUG
772.  	    fprintf(stderr, "--- end of dungeon %d ---\n", i);
773.  	    fflush(stderr);
774.  	    getchar();
775.  #endif
776.  	    for (; pd.start < pd.n_levs; pd.start++)
777.  		if (pd.final_lev[pd.start]) add_level(pd.final_lev[pd.start]);
778.  
779.  
780.  	    pd.n_brs += pd.tmpdungeon[i].branches;
781.  	    if (pd.n_brs > BRANCH_LIMIT)
782.  		panic("init_dungeon: too many branches");
783.  	    for(; cb < pd.n_brs; cb++)
784.  		Fread((genericptr_t)&pd.tmpbranch[cb],
785.  					sizeof(struct tmpbranch), 1, dgn_file);
786.  	}
787.  	(void) dlb_fclose(dgn_file);
788.  
789.  	for (i = 0; i < 5; i++) tune[i] = 'A' + rn2(7);
790.  	tune[5] = 0;
791.  
792.  	/*
793.  	 * Find most of the special levels and dungeons so we can access their
794.  	 * locations quickly.
795.  	 */
796.  	for (lev_map = level_map; lev_map->lev_name[0]; lev_map++) {
797.  		x = find_level(lev_map->lev_name);
798.  		if (x) {
799.  			assign_level(lev_map->lev_spec, &x->dlevel);
800.  			if (!strncmp(lev_map->lev_name, "x-", 2)) {
801.  				/* This is where the name substitution on the
802.  				 * levels of the quest dungeon occur.
803.  				 */
804.  				x->proto[0] = pl_character[0];
805.  			} else if (lev_map->lev_spec == &knox_level) {
806.  				branch *br;
807.  				/*
808.  				 * Kludge to allow floating Knox entrance.  We
809.  				 * specify a floating entrance by the fact that
810.  				 * its entrance (end1) has a bogus dnum, namely
811.  				 * n_dgns.
812.  				 */
813.  				for (br = branches; br; br = br->next)
814.  				    if (on_level(&br->end2, &knox_level)) break;
815.  
816.  				if (br) br->end1.dnum = n_dgns;
817.  				/* adjust the branch's position on the list */
818.  				insert_branch(br, TRUE);
819.  			}
820.  		}
821.  	}
822.  /*
823.   *	I hate hardwiring these names. :-(
824.   */
825.  	quest_dnum = dname_to_dnum("The Quest");
826.  	mines_dnum = dname_to_dnum("The Gnomish Mines");
827.  	tower_dnum = dname_to_dnum("Vlad's Tower");
828.  
829.  	/* one special fixup for dummy surface level */
830.  	if ((x = find_level("dummy")) != 0) {
831.  	    i = x->dlevel.dnum;
832.  	    /* the code above puts earth one level above dungeon level #1,
833.  	       making the dummy level overlay level 1; but the whole reason
834.  	       for having the dummy level is to make earth have depth -1
835.  	       instead of 0, so adjust the start point to shift endgame up */
836.  	    if (dunlevs_in_dungeon(&x->dlevel) > 1 - dungeons[i].depth_start)
837.  		dungeons[i].depth_start -= 1;
838.  	    /* TO DO: strip "dummy" out all the way here,
839.  	       so that it's hidden from <ctrl/O> feedback. */
840.  	}
841.  
842.  #ifdef DEBUG
843.  	dumpit();
844.  #endif
845.  }
846.  
847.  xchar
848.  dunlev(lev)	/* return the level number for lev in *this* dungeon */
849.  d_level	*lev;
850.  {
851.  	return(lev->dlevel);
852.  }
853.  
854.  xchar
855.  dunlevs_in_dungeon(lev)	/* return the lowest level number for *this* dungeon*/
856.  d_level	*lev;
857.  {
858.  	return(dungeons[lev->dnum].num_dunlevs);
859.  }
860.  
861.  xchar
862.  deepest_lev_reached(noquest) /* return the lowest level explored in the game*/
863.  boolean noquest;
864.  {
865.  	/* this function is used for three purposes: to provide a factor
866.  	 * of difficulty in monster generation; to provide a factor of
867.  	 * difficulty in experience calculations (botl.c and end.c); and
868.  	 * to insert the deepest level reached in the game in the topten
869.  	 * display.  the 'noquest' arg switch is required for the latter.
870.  	 *
871.  	 * from the player's point of view, going into the Quest is _not_
872.  	 * going deeper into the dungeon -- it is going back "home", where
873.  	 * the dungeon starts at level 1.  given the setup in dungeon.def,
874.  	 * the depth of the Quest (thought of as starting at level 1) is
875.  	 * never lower than the level of entry into the Quest, so we exclude
876.  	 * the Quest from the topten "deepest level reached" display
877.  	 * calculation.  _However_ the Quest is a difficult dungeon, so we
878.  	 * include it in the factor of difficulty calculations.
879.  	 */
880.  	register int i;
881.  	d_level tmp;
882.  	register schar ret = 0;
883.  
884.  	for(i = 0; i < n_dgns; i++) {
885.  	    if((tmp.dlevel = dungeons[i].dunlev_ureached) == 0) continue;
886.  	    if(!strcmp(dungeons[i].dname, "The Quest") && noquest) continue;
887.  
888.  	    tmp.dnum = i;
889.  	    if(depth(&tmp) > ret) ret = depth(&tmp);
890.  	}
891.  	return((xchar) ret);
892.  }
893.  
894.  /* return a bookkeeping level number for purpose of comparisons and
895.   * save/restore */
896.  xchar
897.  ledger_no(lev)
898.  d_level	*lev;
899.  {
900.  	return((xchar)(lev->dlevel + dungeons[lev->dnum].ledger_start));
901.  }
902.  
903.  /*
904.   * The last level in the bookkeeping list of level is the bottom of the last
905.   * dungeon in the dungeons[] array.
906.   *
907.   * Maxledgerno() -- which is the max number of levels in the bookkeeping
908.   * list, should not be confused with dunlevs_in_dungeon(lev) -- which
909.   * returns the max number of levels in lev's dungeon, and both should
910.   * not be confused with deepest_lev_reached() -- which returns the lowest
911.   * depth visited by the player.
912.   */
913.  xchar
914.  maxledgerno()
915.  {
916.      return (xchar) (dungeons[n_dgns-1].ledger_start +
917.  				dungeons[n_dgns-1].num_dunlevs);
918.  }
919.  
920.  /* return the dungeon that this ledgerno exists in */
921.  xchar
922.  ledger_to_dnum(ledgerno)
923.  xchar	ledgerno;
924.  {
925.  	register int i;
926.  
927.  	/* find i such that (i->base + 1) <= ledgerno <= (i->base + i->count) */
928.  	for (i = 0; i < n_dgns; i++)
929.  	    if (dungeons[i].ledger_start < ledgerno &&
930.  		ledgerno <= dungeons[i].ledger_start + dungeons[i].num_dunlevs)
931.  		return (xchar)i;
932.  
933.  	panic("level number out of range [ledger_to_dnum(%d)]", (int)ledgerno);
934.  	/*NOT REACHED*/
935.  	return (xchar)0;
936.  }
937.  
938.  /* return the level of the dungeon this ledgerno exists in */
939.  xchar
940.  ledger_to_dlev(ledgerno)
941.  xchar	ledgerno;
942.  {
943.  	return((xchar)(ledgerno - dungeons[ledger_to_dnum(ledgerno)].ledger_start));
944.  }
945.  
946.  #endif /* OVL1 */
947.  #ifdef OVL0
948.  
949.  /* returns the depth of a level, in floors below the surface	*/
950.  /* (note levels in different dungeons can have the same depth).	*/
951.  schar
952.  depth(lev)
953.  d_level	*lev;
954.  {
955.  	return((schar)( dungeons[lev->dnum].depth_start + lev->dlevel - 1));
956.  }
957.  
958.  boolean
959.  on_level(lev1, lev2)	/* are "lev1" and "lev2" actually the same? */
960.  d_level	*lev1, *lev2;
961.  {
962.  	return((boolean)((lev1->dnum == lev2->dnum) && (lev1->dlevel == lev2->dlevel)));
963.  }
964.  
965.  #endif /* OVL0 */
966.  #ifdef OVL1
967.  
968.  /* is this level referenced in the special level chain? */
969.  s_level *
970.  Is_special(lev)
971.  d_level	*lev;
972.  {
973.  	s_level *levtmp;
974.  
975.  	for (levtmp = sp_levchn; levtmp; levtmp = levtmp->next)
976.  	    if (on_level(lev, &levtmp->dlevel)) return(levtmp);
977.  
978.  	return((s_level *)0);
979.  }
980.  
981.  /*
982.   * Is this a multi-dungeon branch level?  If so, return a pointer to the
983.   * branch.  Otherwise, return null.
984.   */
985.  branch *
986.  Is_branchlev(lev)
987.  	d_level	*lev;
988.  {
989.  	branch *curr;
990.  
991.  	for (curr = branches; curr; curr = curr->next) {
992.  	    if (on_level(lev, &curr->end1) || on_level(lev, &curr->end2))
993.  		return curr;
994.  	}
995.  	return (branch *) 0;
996.  }
997.  
998.  /* goto the next level (or appropriate dungeon) */
999.  void
1000. next_level(at_stairs )
1001. boolean	at_stairs;
1002. {
1003. 	if (at_stairs && u.ux == sstairs.sx && u.uy == sstairs.sy) {
1004. 		/* Taking a down dungeon branch. */
1005. 		goto_level(&sstairs.tolev, at_stairs, FALSE, FALSE);
1006. 	} else {
1007. 		/* Going down a stairs or jump in a trap door. */
1008. 		d_level	newlevel;
1009. 
1010. 		newlevel.dnum = u.uz.dnum;
1011. 		newlevel.dlevel = u.uz.dlevel + 1;
1012. 		goto_level(&newlevel, at_stairs, !at_stairs, FALSE);
1013. 	}
1014. }
1015. 
1016. /* goto the previous level (or appropriate dungeon) */
1017. void
1018. prev_level(at_stairs)
1019. boolean	at_stairs;
1020. {
1021. 	if (at_stairs && u.ux == sstairs.sx && u.uy == sstairs.sy) {
1022. 		/* Taking an up dungeon branch. */
1023. 		if(!u.uz.dnum  && !u.uhave.amulet) done(ESCAPED);
1024. 		else goto_level(&sstairs.tolev, at_stairs, FALSE, FALSE);
1025. 	} else {
1026. 		/* Going up a stairs or rising through the ceiling. */
1027. 		d_level	newlevel;
1028. 		newlevel.dnum = u.uz.dnum;
1029. 		newlevel.dlevel = u.uz.dlevel - 1;
1030. 		goto_level(&newlevel, at_stairs, FALSE, FALSE);
1031. 	}
1032. }
1033. 
1034. void
1035. u_on_newpos(x, y)
1036. int x, y;
1037. {
1038. 	u.ux = x;
1039. 	u.uy = y;
1040. #ifdef CLIPPING
1041. 	cliparound(u.ux, u.uy);
1042. #endif
1043. }
1044. 
1045. void
1046. u_on_sstairs() {	/* place you on the special staircase */
1047. 
1048. 	if (sstairs.sx) {
1049. 	    u_on_newpos(sstairs.sx, sstairs.sy);
1050. 	} else {
1051. 	    /* code stolen from goto_level */
1052. 	    int trycnt = 0;
1053. 	    xchar x, y;
1054. #ifdef DEBUG
1055. 	    pline("u_on_sstairs: picking random spot");
1056. #endif
1057. #define badspot(x,y) ((levl[x][y].typ != ROOM && levl[x][y].typ != CORR) || MON_AT(x, y))
1058. 	    do {
1059. 		x = rnd(COLNO-1);
1060. 		y = rn2(ROWNO);
1061. 		if (!badspot(x, y)) {
1062. 		    u_on_newpos(x, y);
1063. 		    return;
1064. 		}
1065. 	    } while (++trycnt <= 500);
1066. 	    panic("u_on_sstairs: could not relocate player!");
1067. #undef badspot
1068. 	}
1069. }
1070. 
1071. void
1072. u_on_upstairs()	/* place you on upstairs (or special equivalent) */
1073. {
1074. 	if (xupstair) {
1075. 		u_on_newpos(xupstair, yupstair);
1076. 	} else
1077. 		u_on_sstairs();
1078. }
1079. 
1080. void
1081. u_on_dnstairs()	/* place you on dnstairs (or special equivalent) */
1082. {
1083. 	if (xdnstair) {
1084. 		u_on_newpos(xdnstair, ydnstair);
1085. 	} else
1086. 		u_on_sstairs();
1087. }
1088. 
1089. boolean
1090. On_stairs(x, y)
1091. xchar x, y;
1092. {
1093. 	return((boolean)((x == xupstair && y == yupstair) ||
1094. 	       (x == xdnstair && y == ydnstair) ||
1095. 	       (x == xdnladder && y == ydnladder) ||
1096. 	       (x == xupladder && y == yupladder) ||
1097. 	       (x == sstairs.sx && y == sstairs.sy)));
1098. }
1099. 
1100. boolean
1101. Is_botlevel(lev)
1102. d_level *lev;
1103. {
1104. 	return((boolean)(lev->dlevel == dungeons[lev->dnum].num_dunlevs));
1105. }
1106. 
1107. boolean
1108. Can_dig_down(lev)
1109. d_level *lev;
1110. {
1111. 	return((boolean)(!level.flags.hardfloor
1112. 	    && !Is_botlevel(lev) && !Invocation_lev(lev)));
1113. }
1114. 
1115. /*
1116.  * Like Can_dig_down (above), but also allows falling through on the
1117.  * stronghold level.  Normally, the bottom level of a dungeon resists
1118.  * both digging and falling.
1119.  */
1120. boolean
1121. Can_fall_thru(lev)
1122. d_level *lev;
1123. {
1124. 	return((boolean)(Can_dig_down(lev) || Is_stronghold(lev)));
1125. }
1126. 
1127. /*
1128.  * True if one can rise up a level (e.g. cursed gain level).
1129.  * This happens on intermediate dungeon levels or on any top dungeon
1130.  * level that has a stairwell style branch to the next higher dungeon.
1131.  * Checks for amulets and such must be done elsewhere.
1132.  */
1133. boolean
1134. Can_rise_up(x, y, lev)
1135. int	x, y;
1136. d_level *lev;
1137. {
1138.     /* can't rise up from inside the top of the Wizard's tower */
1139.     if (In_endgame(lev) || (Is_wiz1_level(lev) && In_W_tower(x, y, lev)))
1140. 	return FALSE;
1141.     return (boolean)(lev->dlevel > 1 ||
1142. 		(dungeons[lev->dnum].entry_lev == 1 && ledger_no(lev) != 1 &&
1143. 		 sstairs.sx && sstairs.up));
1144. }
1145. 
1146. /*
1147.  * It is expected that the second argument of get_level is a depth value,
1148.  * either supplied by the user (teleport control) or randomly generated.
1149.  * But more than one level can be at the same depth.  If the target level
1150.  * is "above" the present depth location, get_level must trace "up" from
1151.  * the player's location (through the ancestors dungeons) the dungeon
1152.  * within which the target level is located.  With only one exception
1153.  * which does not pass through this routine (see level_tele), teleporting
1154.  * "down" is confined to the current dungeon.  At present, level teleport
1155.  * in dungeons that build up is confined within them.
1156.  */
1157. void
1158. get_level(newlevel, levnum)
1159. d_level *newlevel;
1160. int levnum;
1161. {
1162. 	branch *br;
1163. 	xchar dgn = u.uz.dnum;
1164. 
1165. 	if (levnum <= 0) {
1166. 	    /* can only currently happen in endgame */
1167. 	    levnum = u.uz.dlevel;
1168. 	} else if (levnum > dungeons[dgn].depth_start
1169. 			    + dungeons[dgn].num_dunlevs - 1) {
1170. 	    /* beyond end of dungeon, jump to last level */
1171. 	    levnum = dungeons[dgn].num_dunlevs;
1172. 	} else {
1173. 	    /* The desired level is in this dungeon or a "higher" one. */
1174. 
1175. 	    /*
1176. 	     * Branch up the tree until we reach a dungeon that contains the
1177. 	     * levnum.
1178. 	     */
1179. 	    if (levnum < dungeons[dgn].depth_start) {
1180. 
1181. 		do {
1182. 		    /*
1183. 		     * Find the parent dungeon of this dungeon.
1184. 		     *
1185. 		     * This assumes that end2 is always the "child" and it is
1186. 		     * unique.
1187. 		     */
1188. 		    for (br = branches; br; br = br->next)
1189. 			if (br->end2.dnum == dgn) break;
1190. 		    if (!br)
1191. 			panic("get_level: can't find parent dungeon");
1192. 
1193. 		    dgn = br->end1.dnum;
1194. 		} while (levnum < dungeons[dgn].depth_start);
1195. 	    }
1196. 
1197. 	    /* We're within the same dungeon; calculate the level. */
1198. 	    levnum = levnum - dungeons[dgn].depth_start + 1;
1199. 	}
1200. 
1201. 	newlevel->dnum = dgn;
1202. 	newlevel->dlevel = levnum;
1203. }
1204. 
1205. #endif /* OVL1 */
1206. #ifdef OVL0
1207. 
1208. boolean
1209. In_quest(lev)	/* are you in the quest dungeon? */
1210. d_level *lev;
1211. {
1212. 	return((boolean)(lev->dnum == quest_dnum));
1213. }
1214. 
1215. #endif /* OVL0 */
1216. #ifdef OVL1
1217. 
1218. boolean
1219. In_mines(lev)	/* are you in the mines dungeon? */
1220. d_level	*lev;
1221. {
1222. 	return((boolean)(lev->dnum == mines_dnum));
1223. }
1224. 
1225. /*
1226.  * Return the branch for the given dungeon.
1227.  *
1228.  * This function assumes:
1229.  *	+ This is not called with "Dungeons of Doom".
1230.  *	+ There is only _one_ branch to a given dungeon.
1231.  *	+ Field end2 is the "child" dungeon.
1232.  */
1233. branch *
1234. dungeon_branch(s)
1235.     const char *s;
1236. {
1237.     branch *br;
1238.     xchar  dnum;
1239. 
1240.     dnum = dname_to_dnum(s);
1241. 
1242.     /* Find the branch that connects to dungeon i's branch. */
1243.     for (br = branches; br; br = br->next)
1244. 	if (br->end2.dnum == dnum) break;
1245. 
1246.     if (!br) panic("dgn_entrance: can't find entrance to %s", s);
1247. 
1248.     return br;
1249. }
1250. 
1251. /*
1252.  * This returns true if the hero is on the same level as the entrance to
1253.  * the named dungeon.
1254.  *
1255.  * Called from do.c and mklev.c.
1256.  *
1257.  * Assumes that end1 is always the "parent".
1258.  */
1259. boolean
1260. at_dgn_entrance(s)
1261.     const char *s;
1262. {
1263.     branch *br;
1264. 
1265.     br = dungeon_branch(s);
1266.     return((boolean)(on_level(&u.uz, &br->end1) ? TRUE : FALSE));
1267. }
1268. 
1269. boolean
1270. In_V_tower(lev)	/* is `lev' part of Vlad's tower? */
1271. d_level	*lev;
1272. {
1273. 	return((boolean)(lev->dnum == tower_dnum));
1274. }
1275. 
1276. boolean
1277. On_W_tower_level(lev)	/* is `lev' a level containing the Wizard's tower? */
1278. d_level	*lev;
1279. {
1280. 	return (boolean)(Is_wiz1_level(lev) ||
1281. 			 Is_wiz2_level(lev) ||
1282. 			 Is_wiz3_level(lev));
1283. }
1284. 
1285. boolean
1286. In_W_tower(x, y, lev)	/* is <x,y> of `lev' inside the Wizard's tower? */
1287. int	x, y;
1288. d_level	*lev;
1289. {
1290. 	if (!On_W_tower_level(lev)) return FALSE;
1291. 	/*
1292. 	 * Both of the exclusion regions for arriving via level teleport
1293. 	 * (from above or below) define the tower's boundary.
1294. 	 *	assert( updest.nIJ == dndest.nIJ for I={l|h},J={x|y} );
1295. 	 */
1296. 	if (dndest.nlx > 0)
1297. 	    return (boolean)within_bounded_area(x, y, dndest.nlx, dndest.nly,
1298. 						dndest.nhx, dndest.nhy);
1299. 	else
1300. 	    impossible("No boundary for Wizard's Tower?");
1301. 	return FALSE;
1302. }
1303. 
1304. #endif /* OVL1 */
1305. #ifdef OVL0
1306. 
1307. boolean
1308. In_hell(lev)	/* are you in one of the Hell levels? */
1309. d_level	*lev;
1310. {
1311. 	return((boolean)(dungeons[lev->dnum].flags.hellish));
1312. }
1313. 
1314. #endif /* OVL0 */
1315. #ifdef OVL1
1316. 
1317. void
1318. find_hell(lev)	/* sets *lev to be the gateway to Gehennom... */
1319. d_level *lev;
1320. {
1321. 	lev->dnum = valley_level.dnum;
1322. 	lev->dlevel = 1;
1323. }
1324. 
1325. void
1326. goto_hell(at_stairs, falling)	/* go directly to hell... */
1327. boolean	at_stairs, falling;
1328. {
1329. 	d_level lev;
1330. 
1331. 	find_hell(&lev);
1332. 	goto_level(&lev, at_stairs, falling, FALSE);
1333. }
1334. 
1335. void
1336. assign_level(dest, src)		/* equivalent to dest = source */
1337. d_level	*dest, *src;
1338. {
1339. 	dest->dnum = src->dnum;
1340. 	dest->dlevel = src->dlevel;
1341. }
1342. 
1343. void
1344. assign_rnd_level(dest, src, range)	/* dest = src + rn1(range) */
1345. d_level	*dest, *src;
1346. int range;
1347. {
1348. 	dest->dnum = src->dnum;
1349. 	dest->dlevel = src->dlevel + ((range > 0) ? rnd(range) : -rnd(-range)) ;
1350. 
1351. 	if(dest->dlevel > dunlevs_in_dungeon(dest))
1352. 		dest->dlevel = dunlevs_in_dungeon(dest);
1353. 	else if(dest->dlevel < 1)
1354. 		dest->dlevel = 1;
1355. }
1356. 
1357. #endif /* OVL1 */
1358. #ifdef OVL0
1359. 
1360. int
1361. induced_align(pct)
1362. int	pct;
1363. {
1364. 	s_level	*lev = Is_special(&u.uz);
1365. 	aligntyp al;
1366. 
1367. 	if (lev && lev->flags.align)
1368. 		if(rn2(100) < pct) return(lev->flags.align);
1369. 
1370. 	if(dungeons[u.uz.dnum].flags.align)
1371. 		if(rn2(100) < pct) return(dungeons[u.uz.dnum].flags.align);
1372. 
1373. 	al = rn2(3) - 1;
1374. 	return(Align2amask(al));
1375. }
1376. 
1377. #endif /* OVL0 */
1378. #ifdef OVL1
1379. 
1380. boolean
1381. Invocation_lev(lev)
1382. d_level *lev;
1383. {
1384. 	return((boolean)(In_hell(lev) &&
1385. 		lev->dlevel == (dungeons[lev->dnum].num_dunlevs - 1)));
1386. }
1387. 
1388. /* use instead of depth() wherever a degree of difficulty is made
1389.  * dependent on the location in the dungeon (eg. monster creation).
1390.  */
1391. xchar
1392. level_difficulty()
1393. {
1394. 	if (In_endgame(&u.uz))
1395. 		return((xchar)(depth(&sanctum_level) + u.ulevel/2));
1396. 	else
1397. 		if (u.uhave.amulet)
1398. 			return(deepest_lev_reached(FALSE));
1399. 		else
1400. 			return((xchar) depth(&u.uz));
1401. }
1402. 
1403. /* Take one word and try to match it to a level.
1404.  * Recognized levels are as shown by print_dungeon().
1405.  */
1406. schar
1407. lev_by_name(nam)
1408. const char *nam;
1409. {
1410.     schar lev = 0;
1411.     s_level *slev;
1412.     d_level dlev;
1413.     const char *p;
1414.     int idx, idxtoo;
1415.     char buf[BUFSZ];
1416. 
1417.     /* allow strings like "the oracle level" to find "oracle" */
1418.     if (!strncmpi(nam, "the ", 4)) nam += 4;
1419.     if ((p = strstri(nam, " level")) != 0 && p == eos((char*)nam) - 6) {
1420. 	nam = strcpy(buf, nam);
1421. 	*(eos(buf) - 6) = '\0';
1422.     }
1423.     /* hell is the old name, and wouldn't match; gehennom would match its
1424.        branch, yielding the castle level instead of the valley of the dead */
1425.     if (!strcmpi(nam, "gehennom") || !strcmpi(nam, "hell")) {
1426. 	if (In_V_tower(&u.uz)) nam = " to Vlad's tower";  /* branch to... */
1427. 	else nam = "valley";
1428.     }
1429. 
1430.     if ((slev = find_level(nam)) != 0) {
1431. 	dlev = slev->dlevel;
1432. 	idx = ledger_no(&dlev);
1433. 	if ((dlev.dnum == u.uz.dnum ||
1434. 		/* within same branch, or else main dungeon <-> gehennom */
1435. 		(u.uz.dnum == valley_level.dnum &&
1436. 			dlev.dnum == medusa_level.dnum) ||
1437. 		(u.uz.dnum == medusa_level.dnum &&
1438. 			dlev.dnum == valley_level.dnum)) &&
1439. 	    (	/* either wizard mode or else seen and not forgotten */
1440. #ifdef WIZARD
1441. 	     wizard ||
1442. #endif
1443. 		(level_info[idx].flags & (FORGOTTEN|VISITED)) == VISITED)) {
1444. 	    lev = depth(&slev->dlevel);
1445. 	}
1446.     } else {	/* not a specific level; try branch names */
1447. 	idx = find_branch(nam, (struct proto_dungeon *)0);
1448. 	/* "<branch> to Xyzzy" */
1449. 	if (idx < 0 && (p = strstri(nam, " to ")) != 0)
1450. 	    idx = find_branch(p + 4, (struct proto_dungeon *)0);
1451. 
1452. 	if (idx >= 0) {
1453. 	    idxtoo = (idx >> 8) & 0x00FF;
1454. 	    idx &= 0x00FF;
1455. 	    if (  /* either wizard mode, or else _both_ sides of branch seen */
1456. #ifdef WIZARD
1457. 		wizard ||
1458. #endif
1459. 		((level_info[idx].flags & (FORGOTTEN|VISITED)) == VISITED &&
1460. 		 (level_info[idxtoo].flags & (FORGOTTEN|VISITED)) == VISITED)) {
1461. 		if (ledger_to_dnum(idxtoo) == u.uz.dnum) idx = idxtoo;
1462. 		dlev.dnum = ledger_to_dnum(idx);
1463. 		dlev.dlevel = ledger_to_dlev(idx);
1464. 		lev = depth(&dlev);
1465. 	    }
1466. 	}
1467.     }
1468.     return lev;
1469. }
1470. 
1471. 
1472. #ifdef WIZARD
1473. 
1474. /* Convert a branch type to a string usable by print_dungeon(). */
1475. static const char *
1476. br_string(type)
1477.     int type;
1478. {
1479.     switch (type) {
1480. 	case BR_PORTAL:	 return "Portal";
1481. 	case BR_NO_END1: return "Connection";
1482. 	case BR_NO_END2: return "One way stair";
1483. 	case BR_STAIR:	 return "Stair";
1484.     }
1485.     return " (unknown)";
1486. }
1487. 
1488. /* Print all child branches between the lower and upper bounds. */
1489. static void
1490. print_branch(win, dnum, lower_bound, upper_bound)
1491.     winid win;
1492.     int   dnum;
1493.     int   lower_bound;
1494.     int   upper_bound;
1495. {
1496.     branch *br;
1497.     char buf[BUFSZ];
1498. 
1499.     /* This assumes that end1 is the "parent". */
1500.     for (br = branches; br; br = br->next) {
1501. 	if (br->end1.dnum == dnum && lower_bound < br->end1.dlevel &&
1502. 					br->end1.dlevel <= upper_bound) {
1503. 	    Sprintf(buf,"   %s to %s: %d",
1504. 		    br_string(br->type),
1505. 		    dungeons[br->end2.dnum].dname,
1506. 		    depth(&br->end1));
1507. 	    putstr(win, 0, buf);
1508. 	}
1509.     }
1510. }
1511. 
1512. /* Print available dungeon information. */
1513. void
1514. print_dungeon()
1515. {
1516.     int     i, last_level, nlev;
1517.     char    buf[BUFSZ];
1518.     boolean first;
1519.     s_level *slev;
1520.     dungeon *dptr;
1521.     branch  *br;
1522.     winid   win = create_nhwindow(NHW_MENU);
1523. 
1524.     for (i = 0, dptr = dungeons; i < n_dgns; i++, dptr++) {
1525. 	nlev = dptr->num_dunlevs;
1526. 	if (nlev > 1)
1527. 	    Sprintf(buf, "%s: levels %d to %d", dptr->dname, dptr->depth_start,
1528. 						dptr->depth_start + nlev - 1);
1529. 	else
1530. 	    Sprintf(buf, "%s: level %d", dptr->dname, dptr->depth_start);
1531. 
1532. 	/* Most entrances are uninteresting. */
1533. 	if (dptr->entry_lev != 1) {
1534. 	    if (dptr->entry_lev == nlev)
1535. 		Strcat(buf, ", entrance from below");
1536. 	    else
1537. 		Sprintf(eos(buf), ", entrance on %d",
1538. 			dptr->depth_start + dptr->entry_lev - 1);
1539. 	}
1540. 	putstr(win, 0, buf);
1541. 
1542. 	/*
1543. 	 * Circle through the special levels to find levels that are in
1544. 	 * this dungeon.
1545. 	 */
1546. 	for (slev = sp_levchn, last_level = 0; slev; slev = slev->next) {
1547. 	    if (slev->dlevel.dnum != i) continue;
1548. 
1549. 	    /* print any branches before this level */
1550. 	    print_branch(win, i, last_level, slev->dlevel.dlevel);
1551. 
1552. 	    Sprintf(buf, "   %s: %d", slev->proto, depth(&slev->dlevel));
1553. 	    if (Is_stronghold(&slev->dlevel))
1554. 		Sprintf(eos(buf), " (tune %s)", tune);
1555. 	    putstr(win, 0, buf);
1556. 
1557. 	    last_level = slev->dlevel.dlevel;
1558. 	}
1559. 	/* print branches after the last special level */
1560. 	print_branch(win, i, last_level, MAXLEVEL);
1561.     }
1562. 
1563.     /* Print out floating branches (if any). */
1564.     for (first = TRUE, br = branches; br; br = br->next) {
1565. 	if (br->end1.dnum == n_dgns) {
1566. 	    if (first) {
1567. 		putstr(win, 0, "");
1568. 		putstr(win, 0, "Floating branches");
1569. 		first = FALSE;
1570. 	    }
1571. 	    Sprintf(buf, "   %s to %s",
1572. 			br_string(br->type), dungeons[br->end2.dnum].dname);
1573. 	    putstr(win, 0, buf);
1574. 	}
1575.     }
1576. 
1577.     /* I hate searching for the invocation pos while debugging. -dean */
1578.     if (Invocation_lev(&u.uz)) {
1579. 	putstr(win, 0, "");
1580. 	Sprintf(buf, "Invocation position @ (%d,%d), hero @ (%d,%d)",
1581. 		inv_pos.x, inv_pos.y, u.ux, u.uy);
1582. 	putstr(win, 0, buf);
1583.     }
1584.     /*
1585.      * The following is based on the assumption that the inter-level portals
1586.      * created by the level compiler (not the dungeon compiler) only exist
1587.      * one per level (currently true, of course).
1588.      */
1589.     else if (Is_earthlevel(&u.uz) || Is_waterlevel(&u.uz)
1590. 				|| Is_firelevel(&u.uz) || Is_airlevel(&u.uz)) {
1591. 	struct trap *trap;
1592. 	for (trap = ftrap; trap; trap = trap->ntrap)
1593. 	    if (trap->ttyp == MAGIC_PORTAL) break;
1594. 
1595. 	putstr(win, 0, "");
1596. 	if (trap)
1597. 	    Sprintf(buf, "Portal @ (%d,%d), hero @ (%d,%d)",
1598. 		trap->tx, trap->ty, u.ux, u.uy);
1599. 	else
1600. 	    Sprintf(buf, "No portal found.");
1601. 	putstr(win, 0, buf);
1602.     }
1603. 
1604.     display_nhwindow(win, TRUE);
1605.     destroy_nhwindow(win);
1606. }
1607. #endif /* WIZARD */
1608. 
1609. #endif /* OVL1 */
1610. 
1611. /*dungeon.c*/