NetBSD/games/hack/hack.lev.c

336 lines
9.1 KiB
C
Raw Normal View History

2011-08-07 00:00:33 +04:00
/* $NetBSD: hack.lev.c,v 1.12 2011/08/06 20:00:33 dholland Exp $ */
1997-10-19 20:56:41 +04:00
1993-08-02 21:16:36 +04:00
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
* Amsterdam
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Stichting Centrum voor Wiskunde en
* Informatica, nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1993-08-02 21:16:36 +04:00
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
1993-08-02 21:16:36 +04:00
#ifndef lint
2011-08-07 00:00:33 +04:00
__RCSID("$NetBSD: hack.lev.c,v 1.12 2011/08/06 20:00:33 dholland Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
#include <stdlib.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include "hack.h"
1997-10-19 20:56:41 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
#include "def.mkroom.h"
#ifndef NOWORM
#include "def.wseg.h"
1997-10-19 20:56:41 +04:00
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
boolean level_exists[MAXLEVEL + 1];
1993-03-21 12:45:37 +03:00
2009-08-12 11:28:40 +04:00
static void savegoldchn(int, struct gold *);
static void savetrapchn(int, struct trap *);
1997-10-19 20:56:41 +04:00
void
savelev(int fd, xchar lev)
1993-03-21 12:45:37 +03:00
{
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
struct wseg *wtmp, *wtmp2;
int tmp;
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (fd < 0)
panic("Save on bad file!"); /* impossible */
if (lev >= 0 && lev <= MAXLEVEL)
1993-03-21 12:45:37 +03:00
level_exists[lev] = TRUE;
1997-10-19 20:56:41 +04:00
bwrite(fd, (char *) &hackpid, sizeof(hackpid));
bwrite(fd, (char *) &lev, sizeof(lev));
bwrite(fd, (char *) levl, sizeof(levl));
bwrite(fd, (char *) &moves, sizeof(long));
bwrite(fd, (char *) &xupstair, sizeof(xupstair));
bwrite(fd, (char *) &yupstair, sizeof(yupstair));
bwrite(fd, (char *) &xdnstair, sizeof(xdnstair));
bwrite(fd, (char *) &ydnstair, sizeof(ydnstair));
1993-03-21 12:45:37 +03:00
savemonchn(fd, fmon);
savegoldchn(fd, fgold);
savetrapchn(fd, ftrap);
saveobjchn(fd, fobj);
saveobjchn(fd, billobjs);
billobjs = 0;
save_engravings(fd);
#ifndef QUEST
1997-10-19 20:56:41 +04:00
bwrite(fd, (char *) rooms, sizeof(rooms));
bwrite(fd, (char *) doors, sizeof(doors));
#endif /* QUEST */
1993-03-21 12:45:37 +03:00
fgold = 0;
ftrap = 0;
fmon = 0;
fobj = 0;
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
bwrite(fd, (char *) wsegs, sizeof(wsegs));
for (tmp = 1; tmp < 32; tmp++) {
for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
1993-03-21 12:45:37 +03:00
wtmp2 = wtmp->nseg;
1997-10-19 20:56:41 +04:00
bwrite(fd, (char *) wtmp, sizeof(struct wseg));
1993-03-21 12:45:37 +03:00
}
wsegs[tmp] = 0;
}
1997-10-19 20:56:41 +04:00
bwrite(fd, (char *) wgrowtime, sizeof(wgrowtime));
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
bwrite(int fd, const void *loc, size_t num)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
/* lint wants the 3rd arg of write to be an int; lint -p an unsigned */
2009-01-18 03:24:29 +03:00
if ((size_t)write(fd, loc, num) != num)
2009-01-18 16:30:33 +03:00
panic("cannot write %zu bytes to file #%d", num, fd);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
saveobjchn(int fd, struct obj *otmp)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct obj *otmp2;
unsigned xl;
int minusone = -1;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
while (otmp) {
1993-03-21 12:45:37 +03:00
otmp2 = otmp->nobj;
xl = otmp->onamelth;
bwrite(fd, (char *) &xl, sizeof(int));
bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
free((char *) otmp);
otmp = otmp2;
}
bwrite(fd, (char *) &minusone, sizeof(int));
}
1997-10-19 20:56:41 +04:00
void
savemonchn(int fd, struct monst *mtmp)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct monst *mtmp2;
unsigned xl;
int minusone = -1;
const struct permonst *monbegin = &mons[0];
1993-03-21 12:45:37 +03:00
bwrite(fd, &monbegin, sizeof(monbegin));
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
while (mtmp) {
1993-03-21 12:45:37 +03:00
mtmp2 = mtmp->nmon;
xl = mtmp->mxlth + mtmp->mnamelth;
bwrite(fd, (char *) &xl, sizeof(int));
bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
1997-10-19 20:56:41 +04:00
if (mtmp->minvent)
saveobjchn(fd, mtmp->minvent);
1993-03-21 12:45:37 +03:00
free((char *) mtmp);
mtmp = mtmp2;
}
bwrite(fd, (char *) &minusone, sizeof(int));
}
2009-08-12 11:28:40 +04:00
static void
savegoldchn(int fd, struct gold *gold)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct gold *gold2;
while (gold) {
1993-03-21 12:45:37 +03:00
gold2 = gold->ngold;
bwrite(fd, (char *) gold, sizeof(struct gold));
free((char *) gold);
gold = gold2;
}
bwrite(fd, nul, sizeof(struct gold));
}
2009-08-12 11:28:40 +04:00
static void
savetrapchn(int fd, struct trap *trap)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct trap *trap2;
while (trap) {
1993-03-21 12:45:37 +03:00
trap2 = trap->ntrap;
bwrite(fd, (char *) trap, sizeof(struct trap));
free((char *) trap);
trap = trap2;
}
bwrite(fd, nul, sizeof(struct trap));
}
1997-10-19 20:56:41 +04:00
void
getlev(int fd, int pid, xchar lev)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
struct gold *gold;
struct trap *trap;
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
struct wseg *wtmp;
#endif /* NOWORM */
int tmp;
long omoves;
int hpid;
xchar dlvl;
1993-03-21 12:45:37 +03:00
/* First some sanity checks */
2011-08-07 00:00:33 +04:00
mread(fd, &hpid, sizeof(hpid));
mread(fd, &dlvl, sizeof(dlvl));
1997-10-19 20:56:41 +04:00
if ((pid && pid != hpid) || (lev && dlvl != lev)) {
1993-03-21 12:45:37 +03:00
pline("Strange, this map is not as I remember it.");
pline("Somebody is trying some trickery here ...");
pline("This game is void ...");
done("tricked");
}
fgold = 0;
ftrap = 0;
2011-08-07 00:00:33 +04:00
mread(fd, levl, sizeof(levl));
mread(fd, &omoves, sizeof(omoves));
mread(fd, &xupstair, sizeof(xupstair));
mread(fd, &yupstair, sizeof(yupstair));
mread(fd, &xdnstair, sizeof(xdnstair));
mread(fd, &ydnstair, sizeof(ydnstair));
1993-03-21 12:45:37 +03:00
fmon = restmonchn(fd);
/* regenerate animals while on another level */
1997-10-19 20:56:41 +04:00
{
long tmoves = (moves > omoves) ? moves - omoves : 0;
struct monst *mtmp, *mtmp2;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
for (mtmp = fmon; mtmp; mtmp = mtmp2) {
long newhp; /* tmoves may be very large */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
mtmp2 = mtmp->nmon;
if (strchr(genocided, mtmp->data->mlet)) {
mondead(mtmp);
continue;
}
if (mtmp->mtame && tmoves > 250) {
mtmp->mtame = 0;
mtmp->mpeaceful = 0;
}
newhp = mtmp->mhp +
(strchr(MREGEN, mtmp->data->mlet) ? tmoves : tmoves / 20);
if (newhp > mtmp->mhpmax)
mtmp->mhp = mtmp->mhpmax;
else
mtmp->mhp = newhp;
1993-03-21 12:45:37 +03:00
}
}
setgd();
gold = newgold();
2011-08-07 00:00:33 +04:00
mread(fd, gold, sizeof(struct gold));
1997-10-19 20:56:41 +04:00
while (gold->gx) {
1993-03-21 12:45:37 +03:00
gold->ngold = fgold;
fgold = gold;
gold = newgold();
2011-08-07 00:00:33 +04:00
mread(fd, gold, sizeof(struct gold));
1993-03-21 12:45:37 +03:00
}
free((char *) gold);
trap = newtrap();
2011-08-07 00:00:33 +04:00
mread(fd, trap, sizeof(struct trap));
1997-10-19 20:56:41 +04:00
while (trap->tx) {
1993-03-21 12:45:37 +03:00
trap->ntrap = ftrap;
ftrap = trap;
trap = newtrap();
2011-08-07 00:00:33 +04:00
mread(fd, trap, sizeof(struct trap));
1993-03-21 12:45:37 +03:00
}
free((char *) trap);
fobj = restobjchn(fd);
billobjs = restobjchn(fd);
rest_engravings(fd);
#ifndef QUEST
2011-08-07 00:00:33 +04:00
mread(fd, rooms, sizeof(rooms));
mread(fd, doors, sizeof(doors));
1997-10-19 20:56:41 +04:00
#endif /* QUEST */
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
2011-08-07 00:00:33 +04:00
mread(fd, wsegs, sizeof(wsegs));
1997-10-19 20:56:41 +04:00
for (tmp = 1; tmp < 32; tmp++)
if (wsegs[tmp]) {
wheads[tmp] = wsegs[tmp] = wtmp = newseg();
while (1) {
2011-08-07 00:00:33 +04:00
mread(fd, wtmp, sizeof(struct wseg));
1997-10-19 20:56:41 +04:00
if (!wtmp->nseg)
break;
wheads[tmp]->nseg = wtmp = newseg();
wheads[tmp] = wtmp;
}
1993-03-21 12:45:37 +03:00
}
2011-08-07 00:00:33 +04:00
mread(fd, wgrowtime, sizeof(wgrowtime));
1997-10-19 20:56:41 +04:00
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
2011-08-07 00:00:33 +04:00
mread(int fd, void *buf, size_t len)
1993-03-21 12:45:37 +03:00
{
2011-08-07 00:00:33 +04:00
ssize_t rlen;
1993-03-21 12:45:37 +03:00
2008-01-28 09:55:41 +03:00
rlen = read(fd, buf, len);
if (rlen < 0 || (size_t)rlen != len) {
2011-08-07 00:00:33 +04:00
pline("Read %zd instead of %zu bytes.\n", rlen, len);
1997-10-19 20:56:41 +04:00
if (restoring) {
1993-03-21 12:45:37 +03:00
(void) unlink(SAVEF);
error("Error restoring old game.");
}
panic("Error reading level file.");
}
}
1997-10-19 20:56:41 +04:00
void
mklev(void)
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (getbones())
return;
1993-03-21 12:45:37 +03:00
in_mklev = TRUE;
makelevel();
in_mklev = FALSE;
}