(linenum→info "unix/slp.c:2238")

bsd-games/2.17/hack/hack.lev.c

    1: /*      $NetBSD: hack.lev.c,v 1.6 2003/04/02 18:36:37 jsm Exp $      */
    2: 
    3: /*
    4:  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
    5:  * Amsterdam
    6:  * All rights reserved.
    7:  *
    8:  * Redistribution and use in source and binary forms, with or without
    9:  * modification, are permitted provided that the following conditions are
   10:  * met:
   11:  *
   12:  * - Redistributions of source code must retain the above copyright notice,
   13:  * this list of conditions and the following disclaimer.
   14:  *
   15:  * - Redistributions in binary form must reproduce the above copyright
   16:  * notice, this list of conditions and the following disclaimer in the
   17:  * documentation and/or other materials provided with the distribution.
   18:  *
   19:  * - Neither the name of the Stichting Centrum voor Wiskunde en
   20:  * Informatica, nor the names of its contributors may be used to endorse or
   21:  * promote products derived from this software without specific prior
   22:  * written permission.
   23:  *
   24:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   25:  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   26:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   27:  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
   28:  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   29:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   30:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   31:  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   32:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   33:  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   34:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35:  */
   36: 
   37: /*
   38:  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
   39:  * All rights reserved.
   40:  *
   41:  * Redistribution and use in source and binary forms, with or without
   42:  * modification, are permitted provided that the following conditions
   43:  * are met:
   44:  * 1. Redistributions of source code must retain the above copyright
   45:  *    notice, this list of conditions and the following disclaimer.
   46:  * 2. Redistributions in binary form must reproduce the above copyright
   47:  *    notice, this list of conditions and the following disclaimer in the
   48:  *    documentation and/or other materials provided with the distribution.
   49:  * 3. The name of the author may not be used to endorse or promote products
   50:  *    derived from this software without specific prior written permission.
   51:  *
   52:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
   53:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
   54:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   55:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   56:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   57:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   58:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   59:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   60:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   61:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   62:  */
   63: 
   64: #include <sys/cdefs.h>
   65: #ifndef lint
   66: __RCSID("$NetBSD: hack.lev.c,v 1.6 2003/04/02 18:36:37 jsm Exp $");
   67: #endif                          /* not lint */
   68: 
   69: #include <stdlib.h>
   70: #include <unistd.h>
   71: #include "hack.h"
   72: #include "extern.h"
   73: #include "def.mkroom.h"
   74: 
   75: #ifndef NOWORM
   76: #include        "def.wseg.h"
   77: #endif                          /* NOWORM */
   78: 
   79: boolean         level_exists[MAXLEVEL + 1];
   80: 
   81: void
   82: savelev(fd, lev)
   83:         int             fd;
   84:         xchar           lev;
   85: {
   86: #ifndef NOWORM
   87:         struct wseg    *wtmp, *wtmp2;
   88:         int tmp;
   89: #endif  /* NOWORM */
   90: 
   91:         if (fd < 0)
   92:                 panic("Save on bad file!");   /* impossible */
   93:         if (lev >= 0 && lev <= MAXLEVEL)
   94:                 level_exists[lev] = TRUE;
   95: 
   96:         bwrite(fd, (char *) &hackpid, sizeof(hackpid));
   97:         bwrite(fd, (char *) &lev, sizeof(lev));
   98:         bwrite(fd, (char *) levl, sizeof(levl));
   99:         bwrite(fd, (char *) &moves, sizeof(long));
  100:         bwrite(fd, (char *) &xupstair, sizeof(xupstair));
  101:         bwrite(fd, (char *) &yupstair, sizeof(yupstair));
  102:         bwrite(fd, (char *) &xdnstair, sizeof(xdnstair));
  103:         bwrite(fd, (char *) &ydnstair, sizeof(ydnstair));
  104:         savemonchn(fd, fmon);
  105:         savegoldchn(fd, fgold);
  106:         savetrapchn(fd, ftrap);
  107:         saveobjchn(fd, fobj);
  108:         saveobjchn(fd, billobjs);
  109:         billobjs = 0;
  110:         save_engravings(fd);
  111: #ifndef QUEST
  112:         bwrite(fd, (char *) rooms, sizeof(rooms));
  113:         bwrite(fd, (char *) doors, sizeof(doors));
  114: #endif  /* QUEST */
  115:         fgold = 0;
  116:         ftrap = 0;
  117:         fmon = 0;
  118:         fobj = 0;
  119: #ifndef NOWORM
  120:         bwrite(fd, (char *) wsegs, sizeof(wsegs));
  121:         for (tmp = 1; tmp < 32; tmp++) {
  122:                 for (wtmp = wsegs[tmp]; wtmp; wtmp = wtmp2) {
  123:                         wtmp2 = wtmp->nseg;
  124:                         bwrite(fd, (char *) wtmp, sizeof(struct wseg));
  125:                 }
  126:                 wsegs[tmp] = 0;
  127:         }
  128:         bwrite(fd, (char *) wgrowtime, sizeof(wgrowtime));
  129: #endif  /* NOWORM */
  130: }
  131: 
  132: void
  133: bwrite(fd, loc, num)
  134:         int fd;
  135:         const void     *loc;
  136:         unsigned        num;
  137: {
  138:         if (write(fd, loc, num) != (ssize_t)num)
  139:                 panic("cannot write %u bytes to file #%d", num, fd);
  140: }
  141: 
  142: void
  143: saveobjchn(fd, otmp)
  144:         int fd;
  145:         struct obj     *otmp;
  146: {
  147:         struct obj     *otmp2;
  148:         unsigned        xl;
  149:         int             minusone = -1;
  150: 
  151:         while (otmp) {
  152:                 otmp2 = otmp->nobj;
  153:                 xl = otmp->onamelth;
  154:                 bwrite(fd, (char *) &xl, sizeof(int));
  155:                 bwrite(fd, (char *) otmp, xl + sizeof(struct obj));
  156:                 free((char *) otmp);
  157:                 otmp = otmp2;
  158:         }
  159:         bwrite(fd, (char *) &minusone, sizeof(int));
  160: }
  161: 
  162: void
  163: savemonchn(fd, mtmp)
  164:         int fd;
  165:         struct monst   *mtmp;
  166: {
  167:         struct monst   *mtmp2;
  168:         unsigned        xl;
  169:         int             minusone = -1;
  170:         const struct permonst *monbegin = &mons[0];
  171: 
  172:         bwrite(fd, &monbegin, sizeof(monbegin));
  173: 
  174:         while (mtmp) {
  175:                 mtmp2 = mtmp->nmon;
  176:                 xl = mtmp->mxlth + mtmp->mnamelth;
  177:                 bwrite(fd, (char *) &xl, sizeof(int));
  178:                 bwrite(fd, (char *) mtmp, xl + sizeof(struct monst));
  179:                 if (mtmp->minvent)
  180:                         saveobjchn(fd, mtmp->minvent);
  181:                 free((char *) mtmp);
  182:                 mtmp = mtmp2;
  183:         }
  184:         bwrite(fd, (char *) &minusone, sizeof(int));
  185: }
  186: 
  187: void
  188: savegoldchn(fd, gold)
  189:         int fd;
  190:         struct gold    *gold;
  191: {
  192:         struct gold    *gold2;
  193:         while (gold) {
  194:                 gold2 = gold->ngold;
  195:                 bwrite(fd, (char *) gold, sizeof(struct gold));
  196:                 free((char *) gold);
  197:                 gold = gold2;
  198:         }
  199:         bwrite(fd, nul, sizeof(struct gold));
  200: }
  201: 
  202: void
  203: savetrapchn(fd, trap)
  204:         int fd;
  205:         struct trap    *trap;
  206: {
  207:         struct trap    *trap2;
  208:         while (trap) {
  209:                 trap2 = trap->ntrap;
  210:                 bwrite(fd, (char *) trap, sizeof(struct trap));
  211:                 free((char *) trap);
  212:                 trap = trap2;
  213:         }
  214:         bwrite(fd, nul, sizeof(struct trap));
  215: }
  216: 
  217: void
  218: getlev(fd, pid, lev)
  219:         int             fd, pid;
  220:         xchar           lev;
  221: {
  222:         struct gold    *gold;
  223:         struct trap    *trap;
  224: #ifndef NOWORM
  225:         struct wseg    *wtmp;
  226: #endif  /* NOWORM */
  227:         int            tmp;
  228:         long            omoves;
  229:         int             hpid;
  230:         xchar           dlvl;
  231: 
  232:         /* First some sanity checks */
  233:         mread(fd, (char *) &hpid, sizeof(hpid));
  234:         mread(fd, (char *) &dlvl, sizeof(dlvl));
  235:         if ((pid && pid != hpid) || (lev && dlvl != lev)) {
  236:                 pline("Strange, this map is not as I remember it.");
  237:                 pline("Somebody is trying some trickery here ...");
  238:                 pline("This game is void ...");
  239:                 done("tricked");
  240:         }
  241:         fgold = 0;
  242:         ftrap = 0;
  243:         mread(fd, (char *) levl, sizeof(levl));
  244:         mread(fd, (char *) &omoves, sizeof(omoves));
  245:         mread(fd, (char *) &xupstair, sizeof(xupstair));
  246:         mread(fd, (char *) &yupstair, sizeof(yupstair));
  247:         mread(fd, (char *) &xdnstair, sizeof(xdnstair));
  248:         mread(fd, (char *) &ydnstair, sizeof(ydnstair));
  249: 
  250:         fmon = restmonchn(fd);
  251: 
  252:         /* regenerate animals while on another level */
  253:         {
  254:                 long            tmoves = (moves > omoves) ? moves - omoves : 0;
  255:                 struct monst   *mtmp, *mtmp2;
  256: 
  257:                 for (mtmp = fmon; mtmp; mtmp = mtmp2) {
  258:                         long            newhp;       /* tmoves may be very large */
  259: 
  260:                         mtmp2 = mtmp->nmon;
  261:                         if (strchr(genocided, mtmp->data->mlet)) {
  262:                                 mondead(mtmp);
  263:                                 continue;
  264:                         }
  265:                         if (mtmp->mtame && tmoves > 250) {
  266:                                 mtmp->mtame = 0;
  267:                                 mtmp->mpeaceful = 0;
  268:                         }
  269:                         newhp = mtmp->mhp +
  270:                                 (strchr(MREGEN, mtmp->data->mlet) ? tmoves : tmoves / 20);
  271:                         if (newhp > mtmp->mhpmax)
  272:                                 mtmp->mhp = mtmp->mhpmax;
  273:                         else
  274:                                 mtmp->mhp = newhp;
  275:                 }
  276:         }
  277: 
  278:         setgd();
  279:         gold = newgold();
  280:         mread(fd, (char *) gold, sizeof(struct gold));
  281:         while (gold->gx) {
  282:                 gold->ngold = fgold;
  283:                 fgold = gold;
  284:                 gold = newgold();
  285:                 mread(fd, (char *) gold, sizeof(struct gold));
  286:         }
  287:         free((char *) gold);
  288:         trap = newtrap();
  289:         mread(fd, (char *) trap, sizeof(struct trap));
  290:         while (trap->tx) {
  291:                 trap->ntrap = ftrap;
  292:                 ftrap = trap;
  293:                 trap = newtrap();
  294:                 mread(fd, (char *) trap, sizeof(struct trap));
  295:         }
  296:         free((char *) trap);
  297:         fobj = restobjchn(fd);
  298:         billobjs = restobjchn(fd);
  299:         rest_engravings(fd);
  300: #ifndef QUEST
  301:         mread(fd, (char *) rooms, sizeof(rooms));
  302:         mread(fd, (char *) doors, sizeof(doors));
  303: #endif  /* QUEST */
  304: #ifndef NOWORM
  305:         mread(fd, (char *) wsegs, sizeof(wsegs));
  306:         for (tmp = 1; tmp < 32; tmp++)
  307:                 if (wsegs[tmp]) {
  308:                         wheads[tmp] = wsegs[tmp] = wtmp = newseg();
  309:                         while (1) {
  310:                                 mread(fd, (char *) wtmp, sizeof(struct wseg));
  311:                                 if (!wtmp->nseg)
  312:                                         break;
  313:                                 wheads[tmp]->nseg = wtmp = newseg();
  314:                                 wheads[tmp] = wtmp;
  315:                         }
  316:                 }
  317:         mread(fd, (char *) wgrowtime, sizeof(wgrowtime));
  318: #endif  /* NOWORM */
  319: }
  320: 
  321: void
  322: mread(fd, buf, len)
  323:         int fd;
  324:         char           *buf;
  325:         unsigned        len;
  326: {
  327:         int             rlen;
  328: 
  329:         rlen = read(fd, buf, (int) len);
  330:         if (rlen != (int)len) {
  331:                 pline("Read %d instead of %u bytes.\n", rlen, len);
  332:                 if (restoring) {
  333:                         (void) unlink(SAVEF);
  334:                         error("Error restoring old game.");
  335:                 }
  336:                 panic("Error reading level file.");
  337:         }
  338: }
  339: 
  340: void
  341: mklev()
  342: {
  343:         if (getbones())
  344:                 return;
  345: 
  346:         in_mklev = TRUE;
  347:         makelevel();
  348:         in_mklev = FALSE;
  349: }
Syntax (Markdown)