xref: /netbsd-src/games/hack/hack.bones.c (revision fcfbc606bf3db3fc16f197f74ffba093b21eb8ad)
1*fcfbc606Sdholland /*	$NetBSD: hack.bones.c,v 1.9 2011/07/20 07:04:30 dholland Exp $	*/
23ea4a95cSchristos 
302ded532Smycroft /*
41c7f94e5Sjsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
51c7f94e5Sjsm  * Amsterdam
61c7f94e5Sjsm  * All rights reserved.
71c7f94e5Sjsm  *
81c7f94e5Sjsm  * Redistribution and use in source and binary forms, with or without
91c7f94e5Sjsm  * modification, are permitted provided that the following conditions are
101c7f94e5Sjsm  * met:
111c7f94e5Sjsm  *
121c7f94e5Sjsm  * - Redistributions of source code must retain the above copyright notice,
131c7f94e5Sjsm  * this list of conditions and the following disclaimer.
141c7f94e5Sjsm  *
151c7f94e5Sjsm  * - Redistributions in binary form must reproduce the above copyright
161c7f94e5Sjsm  * notice, this list of conditions and the following disclaimer in the
171c7f94e5Sjsm  * documentation and/or other materials provided with the distribution.
181c7f94e5Sjsm  *
191c7f94e5Sjsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
201c7f94e5Sjsm  * Informatica, nor the names of its contributors may be used to endorse or
211c7f94e5Sjsm  * promote products derived from this software without specific prior
221c7f94e5Sjsm  * written permission.
231c7f94e5Sjsm  *
241c7f94e5Sjsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
251c7f94e5Sjsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
261c7f94e5Sjsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
271c7f94e5Sjsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
281c7f94e5Sjsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
291c7f94e5Sjsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
301c7f94e5Sjsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
311c7f94e5Sjsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
321c7f94e5Sjsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
331c7f94e5Sjsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
341c7f94e5Sjsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
351c7f94e5Sjsm  */
361c7f94e5Sjsm 
371c7f94e5Sjsm /*
381c7f94e5Sjsm  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
391c7f94e5Sjsm  * All rights reserved.
401c7f94e5Sjsm  *
411c7f94e5Sjsm  * Redistribution and use in source and binary forms, with or without
421c7f94e5Sjsm  * modification, are permitted provided that the following conditions
431c7f94e5Sjsm  * are met:
441c7f94e5Sjsm  * 1. Redistributions of source code must retain the above copyright
451c7f94e5Sjsm  *    notice, this list of conditions and the following disclaimer.
461c7f94e5Sjsm  * 2. Redistributions in binary form must reproduce the above copyright
471c7f94e5Sjsm  *    notice, this list of conditions and the following disclaimer in the
481c7f94e5Sjsm  *    documentation and/or other materials provided with the distribution.
491c7f94e5Sjsm  * 3. The name of the author may not be used to endorse or promote products
501c7f94e5Sjsm  *    derived from this software without specific prior written permission.
511c7f94e5Sjsm  *
521c7f94e5Sjsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
531c7f94e5Sjsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
541c7f94e5Sjsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
551c7f94e5Sjsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
561c7f94e5Sjsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
571c7f94e5Sjsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
581c7f94e5Sjsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
591c7f94e5Sjsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
601c7f94e5Sjsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
611c7f94e5Sjsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6202ded532Smycroft  */
6302ded532Smycroft 
643ea4a95cSchristos #include <sys/cdefs.h>
6502ded532Smycroft #ifndef lint
66*fcfbc606Sdholland __RCSID("$NetBSD: hack.bones.c,v 1.9 2011/07/20 07:04:30 dholland Exp $");
6702ded532Smycroft #endif				/* not lint */
6861f28255Scgd 
693ea4a95cSchristos #include <fcntl.h>
703ea4a95cSchristos #include <unistd.h>
71*fcfbc606Sdholland #include "hack.h"
72*fcfbc606Sdholland #include "extern.h"
7361f28255Scgd 
749b92b189Sdholland static char bones[] = "bones_xx";
7561f28255Scgd 
7661f28255Scgd /* save bones and possessions of a deceased adventurer */
773ea4a95cSchristos void
savebones(void)781fa8a9a6Sdholland savebones(void)
793ea4a95cSchristos {
803ea4a95cSchristos 	int fd;
813ea4a95cSchristos 	struct obj *otmp;
823ea4a95cSchristos 	struct trap *ttmp;
833ea4a95cSchristos 	struct monst *mtmp;
843ea4a95cSchristos 
853ea4a95cSchristos 	if (dlevel <= 0 || dlevel > MAXLEVEL)
863ea4a95cSchristos 		return;
873ea4a95cSchristos 	if (!rn2(1 + dlevel / 2))
883ea4a95cSchristos 		return;		/* not so many ghosts on low levels */
8961f28255Scgd 	bones[6] = '0' + (dlevel / 10);
9061f28255Scgd 	bones[7] = '0' + (dlevel % 10);
91ab8b6343Sjsm 	if ((fd = open(bones, O_RDONLY)) >= 0) {
9261f28255Scgd 		(void) close(fd);
9361f28255Scgd 		return;
9461f28255Scgd 	}
9561f28255Scgd 	/* drop everything; the corpse's possessions are usually cursed */
9661f28255Scgd 	otmp = invent;
9761f28255Scgd 	while (otmp) {
9861f28255Scgd 		otmp->ox = u.ux;
9961f28255Scgd 		otmp->oy = u.uy;
10061f28255Scgd 		otmp->age = 0;	/* very long ago */
10161f28255Scgd 		otmp->owornmask = 0;
1023ea4a95cSchristos 		if (rn2(5))
1033ea4a95cSchristos 			otmp->cursed = 1;
10461f28255Scgd 		if (!otmp->nobj) {
10561f28255Scgd 			otmp->nobj = fobj;
10661f28255Scgd 			fobj = invent;
10761f28255Scgd 			invent = 0;	/* superfluous */
10861f28255Scgd 			break;
10961f28255Scgd 		}
11061f28255Scgd 		otmp = otmp->nobj;
11161f28255Scgd 	}
1123ea4a95cSchristos 	if (!(mtmp = makemon(PM_GHOST, u.ux, u.uy)))
1133ea4a95cSchristos 		return;
11461f28255Scgd 	mtmp->mx = u.ux;
11561f28255Scgd 	mtmp->my = u.uy;
11661f28255Scgd 	mtmp->msleep = 1;
11761f28255Scgd 	(void) strcpy((char *) mtmp->mextra, plname);
11861f28255Scgd 	mkgold(somegold() + d(dlevel, 30), u.ux, u.uy);
11961f28255Scgd 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
12061f28255Scgd 		mtmp->m_id = 0;
12161f28255Scgd 		if (mtmp->mtame) {
12261f28255Scgd 			mtmp->mtame = 0;
12361f28255Scgd 			mtmp->mpeaceful = 0;
12461f28255Scgd 		}
12561f28255Scgd 		mtmp->mlstmv = 0;
1263ea4a95cSchristos 		if (mtmp->mdispl)
1273ea4a95cSchristos 			unpmon(mtmp);
12861f28255Scgd 	}
12961f28255Scgd 	for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
13061f28255Scgd 		ttmp->tseen = 0;
13161f28255Scgd 	for (otmp = fobj; otmp; otmp = otmp->nobj) {
13261f28255Scgd 		otmp->o_id = 0;
13361f28255Scgd 		/* otmp->o_cnt_id = 0; - superfluous */
13461f28255Scgd 		otmp->onamelth = 0;
13561f28255Scgd 		otmp->known = 0;
13661f28255Scgd 		otmp->invlet = 0;
13761f28255Scgd 		if (otmp->olet == AMULET_SYM && !otmp->spe) {
13861f28255Scgd 			otmp->spe = -1;	/* no longer the actual amulet */
1393ea4a95cSchristos 			otmp->cursed = 1;	/* flag as gotten from a
1403ea4a95cSchristos 						 * ghost */
14161f28255Scgd 		}
14261f28255Scgd 	}
1433ea4a95cSchristos 	if ((fd = creat(bones, FMASK)) < 0)
1443ea4a95cSchristos 		return;
14561f28255Scgd 	savelev(fd, dlevel);
14661f28255Scgd 	(void) close(fd);
14761f28255Scgd }
14861f28255Scgd 
1493ea4a95cSchristos int
getbones(void)1501fa8a9a6Sdholland getbones(void)
1513ea4a95cSchristos {
1523ea4a95cSchristos 	int fd, x, y, ok;
1533ea4a95cSchristos 
1543ea4a95cSchristos 	if (rn2(3))
1553ea4a95cSchristos 		return (0);	/* only once in three times do we find bones */
15661f28255Scgd 	bones[6] = '0' + dlevel / 10;
15761f28255Scgd 	bones[7] = '0' + dlevel % 10;
158ab8b6343Sjsm 	if ((fd = open(bones, O_RDONLY)) < 0)
1593ea4a95cSchristos 		return (0);
16061f28255Scgd 	if ((ok = uptodate(fd)) != 0) {
16161f28255Scgd 		getlev(fd, 0, dlevel);
1623ea4a95cSchristos 		for (x = 0; x < COLNO; x++)
1633ea4a95cSchristos 			for (y = 0; y < ROWNO; y++)
16461f28255Scgd 				levl[x][y].seen = levl[x][y].new = 0;
16561f28255Scgd 	}
16661f28255Scgd 	(void) close(fd);
16761f28255Scgd #ifdef WIZARD
1683ea4a95cSchristos 	if (!wizard)		/* duvel!frans: don't remove bones while
1693ea4a95cSchristos 				 * debugging */
1703ea4a95cSchristos #endif	/* WiZARD */
17161f28255Scgd 		if (unlink(bones) < 0) {
17261f28255Scgd 			pline("Cannot unlink %s .", bones);
17361f28255Scgd 			return (0);
17461f28255Scgd 		}
17561f28255Scgd 	return (ok);
17661f28255Scgd }
177