NetBSD/games/hack/hack.lev.c

294 lines
6.4 KiB
C
Raw Normal View History

/* $NetBSD: hack.lev.c,v 1.5 2001/03/25 20:44:01 jsm Exp $ */
1997-10-19 20:56:41 +04:00
1993-08-02 21:16:36 +04:00
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
1993-08-02 21:16:36 +04:00
#ifndef lint
__RCSID("$NetBSD: hack.lev.c,v 1.5 2001/03/25 20:44:01 jsm 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
1997-10-19 20:56:41 +04:00
void
savelev(fd, lev)
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(fd, loc, num)
int fd;
const void *loc;
1997-10-19 20:56:41 +04:00
unsigned 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 */
if (write(fd, loc, (int) num) != num)
1993-03-21 12:45:37 +03:00
panic("cannot write %u bytes to file #%d", num, fd);
}
1997-10-19 20:56:41 +04:00
void
saveobjchn(fd, otmp)
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(fd, mtmp)
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));
}
1997-10-19 20:56:41 +04:00
void
savegoldchn(fd, gold)
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));
}
1997-10-19 20:56:41 +04:00
void
savetrapchn(fd, trap)
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(fd, pid, lev)
int fd, 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 */
mread(fd, (char *) &hpid, sizeof(hpid));
mread(fd, (char *) &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;
mread(fd, (char *) levl, sizeof(levl));
1997-10-19 20:56:41 +04:00
mread(fd, (char *) &omoves, sizeof(omoves));
mread(fd, (char *) &xupstair, sizeof(xupstair));
mread(fd, (char *) &yupstair, sizeof(yupstair));
mread(fd, (char *) &xdnstair, sizeof(xdnstair));
mread(fd, (char *) &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();
1997-10-19 20:56:41 +04:00
mread(fd, (char *) gold, sizeof(struct gold));
while (gold->gx) {
1993-03-21 12:45:37 +03:00
gold->ngold = fgold;
fgold = gold;
gold = newgold();
1997-10-19 20:56:41 +04:00
mread(fd, (char *) gold, sizeof(struct gold));
1993-03-21 12:45:37 +03:00
}
free((char *) gold);
trap = newtrap();
1997-10-19 20:56:41 +04:00
mread(fd, (char *) trap, sizeof(struct trap));
while (trap->tx) {
1993-03-21 12:45:37 +03:00
trap->ntrap = ftrap;
ftrap = trap;
trap = newtrap();
1997-10-19 20:56:41 +04:00
mread(fd, (char *) 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
1997-10-19 20:56:41 +04:00
mread(fd, (char *) rooms, sizeof(rooms));
mread(fd, (char *) doors, sizeof(doors));
#endif /* QUEST */
1993-03-21 12:45:37 +03:00
#ifndef NOWORM
1997-10-19 20:56:41 +04:00
mread(fd, (char *) wsegs, sizeof(wsegs));
for (tmp = 1; tmp < 32; tmp++)
if (wsegs[tmp]) {
wheads[tmp] = wsegs[tmp] = wtmp = newseg();
while (1) {
mread(fd, (char *) wtmp, sizeof(struct wseg));
if (!wtmp->nseg)
break;
wheads[tmp]->nseg = wtmp = newseg();
wheads[tmp] = wtmp;
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
mread(fd, (char *) wgrowtime, sizeof(wgrowtime));
#endif /* NOWORM */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
mread(fd, buf, len)
1997-10-19 20:56:41 +04:00
int fd;
char *buf;
unsigned len;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
int rlen;
1993-03-21 12:45:37 +03:00
rlen = read(fd, buf, (int) len);
1997-10-19 20:56:41 +04:00
if (rlen != len) {
1993-03-21 12:45:37 +03:00
pline("Read %d instead of %u 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
1993-03-21 12:45:37 +03:00
mklev()
{
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;
}