hack.save.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* $NetBSD: hack.save.c,v 1.8 2003/04/02 18:36:40 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  4. * Amsterdam
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * - Neither the name of the Stichting Centrum voor Wiskunde en
  19. * Informatica, nor the names of its contributors may be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. The name of the author may not be used to endorse or promote products
  48. * derived from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  51. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  52. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  53. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  56. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  57. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  58. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  59. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. */
  61. #include <sys/cdefs.h>
  62. #ifndef lint
  63. __RCSID("$NetBSD: hack.save.c,v 1.8 2003/04/02 18:36:40 jsm Exp $");
  64. #endif /* not lint */
  65. #include <signal.h>
  66. #include <stdlib.h>
  67. #include <unistd.h>
  68. #include <fcntl.h>
  69. #include "hack.h"
  70. #include "extern.h"
  71. int
  72. dosave()
  73. {
  74. if (dosave0(0)) {
  75. settty("Be seeing you ...\n");
  76. exit(0);
  77. }
  78. return (0);
  79. }
  80. #ifndef NOSAVEONHANGUP
  81. void
  82. hangup(n)
  83. int n __attribute__((__unused__));
  84. {
  85. (void) dosave0(1);
  86. exit(1);
  87. }
  88. #endif /* NOSAVEONHANGUP */
  89. /* returns 1 if save successful */
  90. int
  91. dosave0(hu)
  92. int hu;
  93. {
  94. int fd, ofd;
  95. int tmp; /* not ! */
  96. (void) signal(SIGHUP, SIG_IGN);
  97. (void) signal(SIGINT, SIG_IGN);
  98. if ((fd = creat(SAVEF, FMASK)) < 0) {
  99. if (!hu)
  100. pline("Cannot open save file. (Continue or Quit)");
  101. (void) unlink(SAVEF); /* ab@unido */
  102. return (0);
  103. }
  104. if (flags.moonphase == FULL_MOON) /* ut-sally!fletcher */
  105. u.uluck--; /* and unido!ab */
  106. savelev(fd, dlevel);
  107. saveobjchn(fd, invent);
  108. saveobjchn(fd, fcobj);
  109. savemonchn(fd, fallen_down);
  110. tmp = getuid();
  111. bwrite(fd, (char *) &tmp, sizeof tmp);
  112. bwrite(fd, (char *) &flags, sizeof(struct flag));
  113. bwrite(fd, (char *) &dlevel, sizeof dlevel);
  114. bwrite(fd, (char *) &maxdlevel, sizeof maxdlevel);
  115. bwrite(fd, (char *) &moves, sizeof moves);
  116. bwrite(fd, (char *) &u, sizeof(struct you));
  117. if (u.ustuck)
  118. bwrite(fd, (char *) &(u.ustuck->m_id), sizeof u.ustuck->m_id);
  119. bwrite(fd, (char *) pl_character, sizeof pl_character);
  120. bwrite(fd, (char *) genocided, sizeof genocided);
  121. bwrite(fd, (char *) fut_geno, sizeof fut_geno);
  122. savenames(fd);
  123. for (tmp = 1; tmp <= maxdlevel; tmp++) {
  124. if (tmp == dlevel || !level_exists[tmp])
  125. continue;
  126. glo(tmp);
  127. if ((ofd = open(lock, O_RDONLY)) < 0) {
  128. if (!hu)
  129. pline("Error while saving: cannot read %s.", lock);
  130. (void) close(fd);
  131. (void) unlink(SAVEF);
  132. if (!hu)
  133. done("tricked");
  134. return (0);
  135. }
  136. getlev(ofd, hackpid, tmp);
  137. (void) close(ofd);
  138. bwrite(fd, (char *) &tmp, sizeof tmp); /* level number */
  139. savelev(fd, tmp); /* actual level */
  140. (void) unlink(lock);
  141. }
  142. (void) close(fd);
  143. glo(dlevel);
  144. (void) unlink(lock); /* get rid of current level --jgm */
  145. glo(0);
  146. (void) unlink(lock);
  147. return (1);
  148. }
  149. int
  150. dorecover(fd)
  151. int fd;
  152. {
  153. int nfd;
  154. int tmp; /* not a ! */
  155. unsigned mid; /* idem */
  156. struct obj *otmp;
  157. restoring = TRUE;
  158. getlev(fd, 0, 0);
  159. invent = restobjchn(fd);
  160. for (otmp = invent; otmp; otmp = otmp->nobj)
  161. if (otmp->owornmask)
  162. setworn(otmp, otmp->owornmask);
  163. fcobj = restobjchn(fd);
  164. fallen_down = restmonchn(fd);
  165. mread(fd, (char *) &tmp, sizeof tmp);
  166. if (tmp != (int)getuid()) { /* strange ... */
  167. (void) close(fd);
  168. (void) unlink(SAVEF);
  169. puts("Saved game was not yours.");
  170. restoring = FALSE;
  171. return (0);
  172. }
  173. mread(fd, (char *) &flags, sizeof(struct flag));
  174. mread(fd, (char *) &dlevel, sizeof dlevel);
  175. mread(fd, (char *) &maxdlevel, sizeof maxdlevel);
  176. mread(fd, (char *) &moves, sizeof moves);
  177. mread(fd, (char *) &u, sizeof(struct you));
  178. if (u.ustuck)
  179. mread(fd, (char *) &mid, sizeof mid);
  180. mread(fd, (char *) pl_character, sizeof pl_character);
  181. mread(fd, (char *) genocided, sizeof genocided);
  182. mread(fd, (char *) fut_geno, sizeof fut_geno);
  183. restnames(fd);
  184. while (1) {
  185. if (read(fd, (char *) &tmp, sizeof tmp) != sizeof tmp)
  186. break;
  187. getlev(fd, 0, tmp);
  188. glo(tmp);
  189. if ((nfd = creat(lock, FMASK)) < 0)
  190. panic("Cannot open temp file %s!\n", lock);
  191. savelev(nfd, tmp);
  192. (void) close(nfd);
  193. }
  194. (void) lseek(fd, (off_t) 0, SEEK_SET);
  195. getlev(fd, 0, 0);
  196. (void) close(fd);
  197. (void) unlink(SAVEF);
  198. if (Punished) {
  199. for (otmp = fobj; otmp; otmp = otmp->nobj)
  200. if (otmp->olet == CHAIN_SYM)
  201. goto chainfnd;
  202. panic("Cannot find the iron chain?");
  203. chainfnd:
  204. uchain = otmp;
  205. if (!uball) {
  206. for (otmp = fobj; otmp; otmp = otmp->nobj)
  207. if (otmp->olet == BALL_SYM && otmp->spe)
  208. goto ballfnd;
  209. panic("Cannot find the iron ball?");
  210. ballfnd:
  211. uball = otmp;
  212. }
  213. }
  214. if (u.ustuck) {
  215. struct monst *mtmp;
  216. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  217. if (mtmp->m_id == mid)
  218. goto monfnd;
  219. panic("Cannot find the monster ustuck.");
  220. monfnd:
  221. u.ustuck = mtmp;
  222. }
  223. #ifndef QUEST
  224. setsee(); /* only to recompute seelx etc. - these
  225. * weren't saved */
  226. #endif /* QUEST */
  227. docrt();
  228. restoring = FALSE;
  229. return (1);
  230. }
  231. struct obj *
  232. restobjchn(fd)
  233. int fd;
  234. {
  235. struct obj *otmp, *otmp2 = NULL;
  236. struct obj *first = 0;
  237. int xl;
  238. while (1) {
  239. mread(fd, (char *) &xl, sizeof(xl));
  240. if (xl == -1)
  241. break;
  242. otmp = newobj(xl);
  243. if (!first)
  244. first = otmp;
  245. else
  246. otmp2->nobj = otmp;
  247. mread(fd, (char *) otmp, (unsigned) xl + sizeof(struct obj));
  248. if (!otmp->o_id)
  249. otmp->o_id = flags.ident++;
  250. otmp2 = otmp;
  251. }
  252. if (first && otmp2->nobj) {
  253. impossible("Restobjchn: error reading objchn.");
  254. otmp2->nobj = 0;
  255. }
  256. return (first);
  257. }
  258. struct monst *
  259. restmonchn(fd)
  260. int fd;
  261. {
  262. struct monst *mtmp, *mtmp2 = NULL;
  263. struct monst *first = 0;
  264. int xl;
  265. struct permonst *monbegin;
  266. long differ;
  267. mread(fd, (char *) &monbegin, sizeof(monbegin));
  268. differ = (const char *) (&mons[0]) - (const char *) (monbegin);
  269. #ifdef lint
  270. /* suppress "used before set" warning from lint */
  271. mtmp2 = 0;
  272. #endif /* lint */
  273. while (1) {
  274. mread(fd, (char *) &xl, sizeof(xl));
  275. if (xl == -1)
  276. break;
  277. mtmp = newmonst(xl);
  278. if (!first)
  279. first = mtmp;
  280. else
  281. mtmp2->nmon = mtmp;
  282. mread(fd, (char *) mtmp, (unsigned) xl + sizeof(struct monst));
  283. if (!mtmp->m_id)
  284. mtmp->m_id = flags.ident++;
  285. mtmp->data = (const struct permonst *)
  286. ((const char *) mtmp->data + differ);
  287. if (mtmp->minvent)
  288. mtmp->minvent = restobjchn(fd);
  289. mtmp2 = mtmp;
  290. }
  291. if (first && mtmp2->nmon) {
  292. impossible("Restmonchn: error reading monchn.");
  293. mtmp2->nmon = 0;
  294. }
  295. return (first);
  296. }