Source:NetHack 3.4.3/src/bones.c

From NetHackWiki
(Redirected from Source:Ref/savebones)
Jump to: navigation, search

Below is the full text to src/bones.c from NetHack 3.4.3. To link to a particular line, write [[bones.c#line123]], for example.

Top of file

  1. /*	SCCS Id: @(#)bones.c	3.4	2003/09/06	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985,1993. */
  3. /* NetHack may be freely redistributed.  See license for details. */

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. #include "hack.h"
  2. #include "lev.h"
  3.  
  4. extern char bones[];	/* from files.c */
  5. #ifdef MFLOPPY
  6. extern long bytes_counted;
  7. #endif
  8.  
  9. STATIC_DCL boolean FDECL(no_bones_level, (d_level *));
  10. STATIC_DCL void FDECL(goodfruit, (int));
  11. STATIC_DCL void FDECL(resetobjs,(struct obj *,BOOLEAN_P));
  12. STATIC_DCL void FDECL(drop_upon_death, (struct monst *, struct obj *));

no_bones_level

  1. STATIC_OVL boolean
  2. no_bones_level(lev)
  3. d_level *lev;
  4. {
  5. 	extern d_level save_dlevel;		/* in do.c */
  6. 	s_level *sptr;
  7.  
  8. 	if (ledger_no(&save_dlevel)) assign_level(lev, &save_dlevel);
  9.  
  10. 	return (boolean)(((sptr = Is_special(lev)) != 0 && !sptr->boneid)
  11. 		|| !dungeons[lev->dnum].boneid
  12. 		   /* no bones on the last or multiway branch levels */
  13. 		   /* in any dungeon (level 1 isn't multiway).       */
  14. 		|| Is_botlevel(lev) || (Is_branchlev(lev) && lev->dlevel > 1)
  15. 		   /* no bones in the invocation level               */
  16. 		|| (In_hell(lev) && lev->dlevel == dunlevs_in_dungeon(lev) - 1)
  17. 		);
  18. }

goodfruit

  1. /* Call this function for each fruit object saved in the bones level: it marks
  2. * that particular type of fruit as existing (the marker is that that type's
  3. * ID is positive instead of negative).  This way, when we later save the
  4. * chain of fruit types, we know to only save the types that exist.
  5. */
  6. STATIC_OVL void
  7. goodfruit(id)
  8. int id;
  9. {
  10. 	register struct fruit *f;
  11.  
  12. 	for(f=ffruit; f; f=f->nextf) {
  13. 		if(f->fid == -id) {
  14. 			f->fid = id;
  15. 			return;
  16. 		}
  17. 	}
  18. }

resetobjs

  1. STATIC_OVL void
  2. resetobjs(ochain,restore)
  3. struct obj *ochain;
  4. boolean restore;
  5. {
  6. 	struct obj *otmp;
  7.  
  8. 	for (otmp = ochain; otmp; otmp = otmp->nobj) {
  9. 		if (otmp->cobj)
  10. 		    resetobjs(otmp->cobj,restore);
  11.  
  12. 		if (((otmp->otyp != CORPSE || otmp->corpsenm < SPECIAL_PM)
  13. 			&& otmp->otyp != STATUE)
  14. 			&& (!otmp->oartifact ||
  15. 			   (restore && (exist_artifact(otmp->otyp, ONAME(otmp))
  16. 					|| is_quest_artifact(otmp))))) {
  17. 			otmp->oartifact = 0;
  18. 			otmp->onamelth = 0;
  19. 			*ONAME(otmp) = '\0';
  20. 		} else if (otmp->oartifact && restore)
  21. 			artifact_exists(otmp,ONAME(otmp),TRUE);
  22. 		if (!restore) {
  23. 			/* do not zero out o_ids for ghost levels anymore */
  24.  
  25. 			if(objects[otmp->otyp].oc_uses_known) otmp->known = 0;
  26. 			otmp->dknown = otmp->bknown = 0;
  27. 			otmp->rknown = 0;
  28. 			otmp->invlet = 0;
  29. 			otmp->no_charge = 0;
  30.  
  31. 			if (otmp->otyp == SLIME_MOLD) goodfruit(otmp->spe);
  32. #ifdef MAIL
  33. 			else if (otmp->otyp == SCR_MAIL) otmp->spe = 1;
  34. #endif
  35. 			else if (otmp->otyp == EGG) otmp->spe = 0;
  36. 			else if (otmp->otyp == TIN) {
  37. 			    /* make tins of unique monster's meat be empty */
  38. 			    if (otmp->corpsenm >= LOW_PM &&
  39. 				    (mons[otmp->corpsenm].geno & G_UNIQ))
  40. 				otmp->corpsenm = NON_PM;
  41. 			} else if (otmp->otyp == AMULET_OF_YENDOR) {
  42. 			    /* no longer the real Amulet */
  43. 			    otmp->otyp = FAKE_AMULET_OF_YENDOR;
  44. 			    curse(otmp);
  45. 			} else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
  46. 			    if (otmp->lamplit)
  47. 				end_burn(otmp, TRUE);
  48. 			    otmp->otyp = WAX_CANDLE;
  49. 			    otmp->age = 50L;  /* assume used */
  50. 			    if (otmp->spe > 0)
  51. 				otmp->quan = (long)otmp->spe;
  52. 			    otmp->spe = 0;
  53. 			    otmp->owt = weight(otmp);
  54. 			    curse(otmp);
  55. 			} else if (otmp->otyp == BELL_OF_OPENING) {
  56. 			    otmp->otyp = BELL;
  57. 			    curse(otmp);
  58. 			} else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
  59. 			    otmp->otyp = SPE_BLANK_PAPER;
  60. 			    curse(otmp);
  61. 			}
  62. 		}
  63. 	}
  64. }

drop_upon_death

  1. STATIC_OVL void
  2. drop_upon_death(mtmp, cont)
  3. struct monst *mtmp;
  4. struct obj *cont;
  5. {
  6. 	struct obj *otmp;
  7.  
  8. 	uswapwep = 0; /* ensure curse() won't cause swapwep to drop twice */
  9. 	while ((otmp = invent) != 0) {
  10. 		obj_extract_self(otmp);
  11. 		obj_no_longer_held(otmp);
  12.  
  13. 		otmp->owornmask = 0;
  14. 		/* lamps don't go out when dropped */
  15. 		if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
  16. 		    end_burn(otmp, TRUE);	/* smother in statue */
  17.  
  18. 		if(otmp->otyp == SLIME_MOLD) goodfruit(otmp->spe);
  19.  
  20. 		if(rn2(5)) curse(otmp);
  21. 		if (mtmp)
  22. 			(void) add_to_minv(mtmp, otmp);
  23. 		else if (cont)
  24. 			(void) add_to_container(cont, otmp);
  25. 		else
  26. 			place_object(otmp, u.ux, u.uy);
  27. 	}
  28. #ifndef GOLDOBJ
  29. 	if(u.ugold) {
  30. 		long ugold = u.ugold;
  31. 		if (mtmp) mtmp->mgold = ugold;
  32. 		else if (cont) (void) add_to_container(cont, mkgoldobj(ugold));
  33. 		else (void)mkgold(ugold, u.ux, u.uy);
  34. 		u.ugold = ugold;	/* undo mkgoldobj()'s removal */
  35. 	}
  36. #endif
  37. 	if (cont) cont->owt = weight(cont);
  38. }

can_make_bones

  1. /* check whether bones are feasible */
  2. boolean
  3. can_make_bones()
  4. {
  5. 	register struct trap *ttmp;
  6.  
  7. 	if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
  8. 	    return FALSE;
  9. 	if (no_bones_level(&u.uz))
  10. 	    return FALSE;		/* no bones for specific levels */
  11. 	if (u.uswallow) {
  12. 	    return FALSE;		/* no bones when swallowed */
  13. 	}
  14. 	if (!Is_branchlev(&u.uz)) {
  15. 	    /* no bones on non-branches with portals */
  16. 	    for(ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
  17. 		if (ttmp->ttyp == MAGIC_PORTAL) return FALSE;
  18. 	}
  19.  
  20. 	if(depth(&u.uz) <= 0 ||		/* bulletproofing for endgame */
  21. 	   (!rn2(1 + (depth(&u.uz)>>2))	/* fewer ghosts on low levels */
  22. #ifdef WIZARD
  23. 		&& !wizard
  24. #endif
  25. 		)) return FALSE;
  26. 	/* don't let multiple restarts generate multiple copies of objects
  27. 	 * in bones files */
  28. 	if (discover) return FALSE;
  29. 	return TRUE;
  30. }

savebones

  1. /* save bones and possessions of a deceased adventurer */
  2. void
  3. savebones(corpse)
  4. struct obj *corpse;
  5. {
  6. 	int fd, x, y;
  7. 	struct trap *ttmp;
  8. 	struct monst *mtmp;
  9. 	struct permonst *mptr;
  10. 	struct fruit *f;
  11. 	char c, *bonesid;
  12. 	char whynot[BUFSZ];
  13.  
  14. 	/* caller has already checked `can_make_bones()' */
  15.  
  16. 	clear_bypasses();
  17. 	fd = open_bonesfile(&u.uz, &bonesid);
  18. 	if (fd >= 0) {
  19. 		(void) close(fd);
  20. 		compress_bonesfile();
  21. #ifdef WIZARD
  22. 		if (wizard) {
  23. 		    if (yn("Bones file already exists.  Replace it?") == 'y') {
  24. 			if (delete_bonesfile(&u.uz)) goto make_bones;
  25. 			else pline("Cannot unlink old bones.");
  26. 		    }
  27. 		}
  28. #endif
  29. 		return;
  30. 	}
  31.  
  32. #ifdef WIZARD
  33. make_bones:
  34. #endif
  35. 	unleash_all();
  36. 	/* in case these characters are not in their home bases */
  37. 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  38. 	    if (DEADMONSTER(mtmp)) continue;
  39. 	    mptr = mtmp->data;
  40. 	    if (mtmp->iswiz || mptr == &mons[PM_MEDUSA] ||
  41. 		    mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER ||
  42. 		    mptr == &mons[PM_VLAD_THE_IMPALER])
  43. 		mongone(mtmp);
  44. 	}
  45. #ifdef STEED
  46. 	if (u.usteed) dismount_steed(DISMOUNT_BONES);
  47. #endif
  48. 	dmonsfree();		/* discard dead or gone monsters */
  49.  
  50. 	/* mark all fruits as nonexistent; when we come to them we'll mark
  51. 	 * them as existing (using goodfruit())
  52. 	 */
  53. 	for(f=ffruit; f; f=f->nextf) f->fid = -f->fid;
  54.  
  55. 	/* check iron balls separately--maybe they're not carrying it */
  56. 	if (uball) uball->owornmask = uchain->owornmask = 0;
  57.  
  58. 	/* dispose of your possessions, usually cursed */
  59. 	if (u.ugrave_arise == (NON_PM - 1)) {
  60. 		struct obj *otmp;
  61.  
  62. 		/* embed your possessions in your statue */
  63. 		otmp = mk_named_object(STATUE, &mons[u.umonnum],
  64. 				       u.ux, u.uy, plname);
  65.  
  66. 		drop_upon_death((struct monst *)0, otmp);
  67. 		if (!otmp) return;	/* couldn't make statue */
  68. 		mtmp = (struct monst *)0;
  69. 	} else if (u.ugrave_arise < LOW_PM) {
  70. 		/* drop everything */
  71. 		drop_upon_death((struct monst *)0, (struct obj *)0);
  72. 		/* trick makemon() into allowing monster creation
  73. 		 * on your location
  74. 		 */
  75. 		in_mklev = TRUE;
  76. 		mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
  77. 		in_mklev = FALSE;
  78. 		if (!mtmp) return;
  79. 		mtmp = christen_monst(mtmp, plname);
  80. 		if (corpse)
  81. 			(void) obj_attach_mid(corpse, mtmp->m_id); 
  82. 	} else {
  83. 		/* give your possessions to the monster you become */
  84. 		in_mklev = TRUE;
  85. 		mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MM_FLAGS);
  86. 		in_mklev = FALSE;
  87. 		if (!mtmp) {
  88. 			drop_upon_death((struct monst *)0, (struct obj *)0);
  89. 			return;
  90. 		}
  91. 		mtmp = christen_monst(mtmp, plname);
  92. 		newsym(u.ux, u.uy);
  93. 		Your("body rises from the dead as %s...",
  94. 			an(mons[u.ugrave_arise].mname));
  95. 		display_nhwindow(WIN_MESSAGE, FALSE);
  96. 		drop_upon_death(mtmp, (struct obj *)0);
  97. 		m_dowear(mtmp, TRUE);
  98. 	}
  99. 	if (mtmp) {
  100. 		mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
  101. 		mtmp->mhp = mtmp->mhpmax = u.uhpmax;
  102. 		mtmp->female = flags.female;
  103. 		mtmp->msleeping = 1;
  104. 	}
  105. 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  106. 		resetobjs(mtmp->minvent,FALSE);
  107. 		/* do not zero out m_ids for bones levels any more */
  108. 		mtmp->mlstmv = 0L;
  109. 		if(mtmp->mtame) mtmp->mtame = mtmp->mpeaceful = 0;
  110. 	}
  111. 	for(ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
  112. 		ttmp->madeby_u = 0;
  113. 		ttmp->tseen = (ttmp->ttyp == HOLE);
  114. 	}
  115. 	resetobjs(fobj,FALSE);
  116. 	resetobjs(level.buriedobjlist, FALSE);
  117.  
  118. 	/* Hero is no longer on the map. */
  119. 	u.ux = u.uy = 0;
  120.  
  121. 	/* Clear all memory from the level. */
  122. 	for(x=0; x<COLNO; x++) for(y=0; y<ROWNO; y++) {
  123. 	    levl[x][y].seenv = 0;
  124. 	    levl[x][y].waslit = 0;
  125. 	    levl[x][y].glyph = cmap_to_glyph(S_stone);
  126. 	}
  127.  
  128. 	fd = create_bonesfile(&u.uz, &bonesid, whynot);
  129. 	if(fd < 0) {
  130. #ifdef WIZARD
  131. 		if(wizard)
  132. 			pline("%s", whynot);
  133. #endif
  134. 		/* bones file creation problems are silent to the player.
  135. 		 * Keep it that way, but place a clue into the paniclog.
  136. 		 */
  137. 		paniclog("savebones", whynot);
  138. 		return;
  139. 	}
  140. 	c = (char) (strlen(bonesid) + 1);
  141.  
  142. #ifdef MFLOPPY  /* check whether there is room */
  143. 	if (iflags.checkspace) {
  144. 	    savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
  145. 	    /* savelev() initializes bytes_counted to 0, so it must come
  146. 	     * first here even though it does not in the real save.  the
  147. 	     * resulting extra bflush() at the end of savelev() may increase
  148. 	     * bytes_counted by a couple over what the real usage will be.
  149. 	     *
  150. 	     * note it is safe to call store_version() here only because
  151. 	     * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
  152. 	     * this code would have to know the size of the version
  153. 	     * information itself.
  154. 	     */
  155. 	    store_version(fd);
  156. 	    bwrite(fd, (genericptr_t) &c, sizeof c);
  157. 	    bwrite(fd, (genericptr_t) bonesid, (unsigned) c);	/* DD.nnn */
  158. 	    savefruitchn(fd, COUNT_SAVE);
  159. 	    bflush(fd);
  160. 	    if (bytes_counted > freediskspace(bones)) { /* not enough room */
  161. # ifdef WIZARD
  162. 		if (wizard)
  163. 			pline("Insufficient space to create bones file.");
  164. # endif
  165. 		(void) close(fd);
  166. 		cancel_bonesfile();
  167. 		return;
  168. 	    }
  169. 	    co_false();	/* make sure stuff before savelev() gets written */
  170. 	}
  171. #endif /* MFLOPPY */
  172.  
  173. 	store_version(fd);
  174. 	bwrite(fd, (genericptr_t) &c, sizeof c);
  175. 	bwrite(fd, (genericptr_t) bonesid, (unsigned) c);	/* DD.nnn */
  176. 	savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
  177. 	update_mlstmv();	/* update monsters for eventual restoration */
  178. 	savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
  179. 	bclose(fd);
  180. 	commit_bonesfile(&u.uz);
  181. 	compress_bonesfile();
  182. }

getbones

  1. int
  2. getbones()
  3. {
  4. 	register int fd;
  5. 	register int ok;
  6. 	char c, *bonesid, oldbonesid[10];
  7.  
  8. 	if(discover)		/* save bones files for real games */
  9. 		return(0);
  10.  
  11. 	/* wizard check added by GAN 02/05/87 */
  12. 	if(rn2(3)	/* only once in three times do we find bones */
  13. #ifdef WIZARD
  14. 		&& !wizard
  15. #endif
  16. 		) return(0);
  17. 	if(no_bones_level(&u.uz)) return(0);
  18. 	fd = open_bonesfile(&u.uz, &bonesid);
  19. 	if (fd < 0) return(0);
  20.  
  21. 	if ((ok = uptodate(fd, bones)) == 0) {
  22. #ifdef WIZARD
  23. 	    if (!wizard)
  24. #endif
  25. 		pline("Discarding unuseable bones; no need to panic...");
  26. 	} else {
  27. #ifdef WIZARD
  28. 		if(wizard)  {
  29. 			if(yn("Get bones?") == 'n') {
  30. 				(void) close(fd);
  31. 				compress_bonesfile();
  32. 				return(0);
  33. 			}
  34. 		}
  35. #endif
  36. 		mread(fd, (genericptr_t) &c, sizeof c);	/* length incl. '\0' */
  37. 		mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
  38. 		if (strcmp(bonesid, oldbonesid) != 0) {
  39. 			char errbuf[BUFSZ];
  40.  
  41. 			Sprintf(errbuf, "This is bones level '%s', not '%s'!",
  42. 				oldbonesid, bonesid);
  43. #ifdef WIZARD
  44. 			if (wizard) {
  45. 				pline("%s", errbuf);
  46. 				ok = FALSE;	/* won't die of trickery */
  47. 			}
  48. #endif
  49. 			trickery(errbuf);
  50. 		} else {
  51. 			register struct monst *mtmp;
  52.  
  53. 			getlev(fd, 0, 0, TRUE);
  54.  
  55. 			/* Note that getlev() now keeps tabs on unique
  56. 			 * monsters such as demon lords, and tracks the
  57. 			 * birth counts of all species just as makemon()
  58. 			 * does.  If a bones monster is extinct or has been
  59. 			 * subject to genocide, their mhpmax will be
  60. 			 * set to the magic DEFUNCT_MONSTER cookie value.
  61. 			 */
  62. 			for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  63. 			    if (mtmp->mhpmax == DEFUNCT_MONSTER) {
  64. #if defined(DEBUG) && defined(WIZARD)
  65. 				if (wizard)
  66. 				    pline("Removing defunct monster %s from bones.",
  67. 					mtmp->data->mname);
  68. #endif
  69. 				mongone(mtmp);
  70. 			    } else
  71. 				/* to correctly reset named artifacts on the level */
  72. 				resetobjs(mtmp->minvent,TRUE);
  73. 			}
  74. 			resetobjs(fobj,TRUE);
  75. 			resetobjs(level.buriedobjlist,TRUE);
  76. 		}
  77. 	}
  78. 	(void) close(fd);
  79.  
  80. #ifdef WIZARD
  81. 	if(wizard) {
  82. 		if(yn("Unlink bones?") == 'n') {
  83. 			compress_bonesfile();
  84. 			return(ok);
  85. 		}
  86. 	}
  87. #endif
  88. 	if (!delete_bonesfile(&u.uz)) {
  89. 		/* When N games try to simultaneously restore the same
  90. 		 * bones file, N-1 of them will fail to delete it
  91. 		 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
  92. 		 * So no point in a mysterious message for a normal event
  93. 		 * -- just generate a new level for those N-1 games.
  94. 		 */
  95. 		/* pline("Cannot unlink bones."); */
  96. 		return(0);
  97. 	}
  98. 	return(ok);
  99. }
  100.  
  101. /*bones.c*/