1 /* $Id$ */
2 /* File: load2.c */
3 
4 /* Purpose: support for loading savefiles -BEN- */
5 
6 #define SERVER
7 
8 #include "angband.h"
9 
10 static void new_rd_wild();
11 static void new_rd_floors();
12 void rd_towns();
13 void rd_byte(byte *ip);
14 void rd_u16b(u16b *ip);
15 void rd_s16b(s16b *ip);
16 void rd_u32b(u32b *ip);
17 void rd_s32b(s32b *ip);
18 void rd_string(char *str, int max);
19 
20 /*
21  * This file is responsible for loading all "2.7.X" savefiles
22  *
23  * Note that 2.7.0 - 2.7.2 savefiles are obsolete and will not work.
24  *
25  * We attempt to prevent corrupt savefiles from inducing memory errors.
26  *
27  * Note that Angband 2.7.9 encodes "terrain features" in the savefile
28  * using the old 2.7.8 method.  Angband 2.8.0 will use the same method
29  * to read pre-2.8.0 savefiles, but will use a new method to save them,
30  * which will only affect "save.c".
31  *
32  * Note that Angband 2.8.0 will use a VERY different savefile method,
33  * which will use "blocks" of information which can be ignored or parsed,
34  * and which will not use a silly "protection" scheme on the savefiles,
35  * but which may still use some form of "checksums" to prevent the use
36  * of "corrupt" savefiles, which might cause nasty weirdness.
37  *
38  * Note that this file should not use the random number generator, the
39  * object flavors, the visual attr/char mappings, or anything else which
40  * is initialized *after* or *during* the "load character" function.
41  *
42  * We should also make the "cheating" options official flags, and
43  * move the "byte" options to a different part of the code, perhaps
44  * with a few more (for variety).
45  *
46  * Implement simple "savefile extenders" using some form of "sized"
47  * chunks of bytes, with a {size,type,data} format, so everyone can
48  * know the size, interested people can know the type, and the actual
49  * data is available to the parsing routines that acknowledge the type.
50  *
51  * Consider changing the "globe of invulnerability" code so that it
52  * takes some form of "maximum damage to protect from" in addition to
53  * the existing "number of turns to protect for", and where each hit
54  * by a monster will reduce the shield by that amount.
55  *
56  * XXX XXX XXX
57  */
58 
59 
60 
61 
62 
63 /*
64  * Local "savefile" pointer
65  */
66 static FILE	*fff;
67 
68 /*
69  * Local savefile buffer
70  */
71 static char	*fff_buf;
72 static int	fff_buf_pos = 0;
73 #define MAX_BUF_SIZE	4096
74 
75 /*
76  * Hack -- old "encryption" byte
77  */
78 static byte	xor_byte;
79 
80 /*
81  * Hack -- simple "checksum" on the actual values
82  */
83 static u32b	v_check = 0L;
84 
85 /*
86  * Hack -- simple "checksum" on the encoded bytes
87  */
88 static u32b	x_check = 0L;
89 
90 
91 
92 /*
93  * This function determines if the version of the savefile
94  * currently being read is older than version "x.y.z".
95  */
older_than(byte x,byte y,byte z)96 static bool older_than(byte x, byte y, byte z)
97 {
98 	/* Much older, or much more recent */
99 	if (sf_major < x) return (TRUE);
100 	if (sf_major > x) return (FALSE);
101 
102 	/* Distinctly older, or distinctly more recent */
103 	if (sf_minor < y) return (TRUE);
104 	if (sf_minor > y) return (FALSE);
105 
106 	/* Barely older, or barely more recent */
107 	if (sf_patch < z) return (TRUE);
108 	if (sf_patch > z) return (FALSE);
109 
110 	/* Identical versions */
111 	return (FALSE);
112 }
113 
114 /*
115  * This function determines if the version of the server savefile
116  * currently being read is older than version "x.y.z".
117  */
s_older_than(byte x,byte y,byte z)118 static bool s_older_than(byte x, byte y, byte z) {
119 	/* Much older, or much more recent */
120 	if (ssf_major < x) return (TRUE);
121 	if (ssf_major > x) return (FALSE);
122 
123 	/* Distinctly older, or distinctly more recent */
124 	if (ssf_minor < y) return (TRUE);
125 	if (ssf_minor > y) return (FALSE);
126 
127 	/* Barely older, or barely more recent */
128 	if (ssf_patch < z) return (TRUE);
129 	if (ssf_patch > z) return (FALSE);
130 
131 	/* Identical versions */
132 	return (FALSE);
133 }
134 
135 /*
136  * This function determines if the version of the quests savefile
137  * currently being read is older than version "x.y.z".
138  */
q_older_than(byte x,byte y,byte z)139 static bool q_older_than(byte x, byte y, byte z) {
140 	/* Much older, or much more recent */
141 	if (qsf_major < x) return (TRUE);
142 	if (qsf_major > x) return (FALSE);
143 
144 	/* Distinctly older, or distinctly more recent */
145 	if (qsf_minor < y) return (TRUE);
146 	if (qsf_minor > y) return (FALSE);
147 
148 	/* Barely older, or barely more recent */
149 	if (qsf_patch < z) return (TRUE);
150 	if (qsf_patch > z) return (FALSE);
151 
152 	/* Identical versions */
153 	return FALSE;
154 }
155 
156 /*
157  * Hack -- determine if an item is "wearable" (or a missile)
158  */
wearable_p(object_type * o_ptr)159 bool wearable_p(object_type *o_ptr)
160 {
161 	/* Valid "tval" codes */
162 	switch (o_ptr->tval)
163 	{
164 		case TV_SHOT:
165 		case TV_ARROW:
166 		case TV_BOLT:
167 		case TV_BOW:
168 		case TV_BOOMERANG:
169 		case TV_DIGGING:
170 		case TV_BLUNT:
171 		case TV_POLEARM:
172 		case TV_SWORD:
173 		case TV_BOOTS:
174 		case TV_GLOVES:
175 		case TV_HELM:
176 		case TV_CROWN:
177 		case TV_SHIELD:
178 		case TV_CLOAK:
179 		case TV_SOFT_ARMOR:
180 		case TV_HARD_ARMOR:
181 		case TV_DRAG_ARMOR:
182 		case TV_LITE:
183 		case TV_AMULET:
184 		case TV_RING:
185 		case TV_AXE:
186 		case TV_MSTAFF:
187 			{
188 				return (TRUE);
189 			}
190 	}
191 
192 	/* Nope */
193 	return (FALSE);
194 }
195 
196 
197 /*
198  * The following functions are used to load the basic building blocks
199  * of savefiles.  They also maintain the "checksum" info for 2.7.0+
200  */
201 
sf_get(void)202 static byte sf_get(void)
203 {
204 	byte c, v;
205 
206 #if 0
207 	/* Get a character, decode the value */
208 	c = getc(fff) & 0xFF;
209 #else
210 	/* Buffered reading */
211 	if (fff_buf_pos >= MAX_BUF_SIZE) {
212 		if (fread(fff_buf, 1, MAX_BUF_SIZE, fff) < MAX_BUF_SIZE) {
213 			if (!feof(fff)) {
214 				s_printf("Failed to read from savefile: %s\n", strerror(ferror(fff)));
215 			}
216 		}
217 		fff_buf_pos = 0;
218 	}
219 
220 	c = fff_buf[fff_buf_pos++];
221 #endif
222 	v = c ^ xor_byte;
223 	xor_byte = c;
224 
225 	/* Maintain the checksum info */
226 	v_check += v;
227 	x_check += xor_byte;
228 
229 	/* Return the value */
230 	return (v);
231 }
232 
rd_byte(byte * ip)233 void rd_byte(byte *ip)
234 {
235 	*ip = sf_get();
236 }
237 
rd_u16b(u16b * ip)238 void rd_u16b(u16b *ip)
239 {
240 	(*ip) = sf_get();
241 	(*ip) |= ((u16b)(sf_get()) << 8);
242 }
243 
rd_s16b(s16b * ip)244 void rd_s16b(s16b *ip)
245 {
246 	rd_u16b((u16b*)ip);
247 }
248 
rd_u32b(u32b * ip)249 void rd_u32b(u32b *ip)
250 {
251 	(*ip) = sf_get();
252 	(*ip) |= ((u32b)(sf_get()) << 8);
253 	(*ip) |= ((u32b)(sf_get()) << 16);
254 	(*ip) |= ((u32b)(sf_get()) << 24);
255 }
256 
rd_s32b(s32b * ip)257 void rd_s32b(s32b *ip)
258 {
259 	rd_u32b((u32b*)ip);
260 }
261 
262 
263 /*
264  * Hack -- read a string
265  */
rd_string(char * str,int max)266 void rd_string(char *str, int max)
267 {
268 	int i;
269 
270 	/* Read the string */
271 	for (i = 0; TRUE; i++)
272 	{
273 		byte tmp8u;
274 
275 		/* Read a byte */
276 		rd_byte(&tmp8u);
277 
278 		/* Collect string while legal */
279 		if (i < max) str[i] = tmp8u;
280 
281 		/* End of string */
282 		if (!tmp8u) break;
283 	}
284 
285 	/* Terminate */
286 	str[max-1] = '\0';
287 }
288 
289 
290 /*
291  * Hack -- strip some bytes
292  */
strip_bytes(int n)293 static void strip_bytes(int n)
294 {
295 	int i;
296 
297 	/* Strip the bytes */
298 	for (i = 0; i < n; i++) (void)sf_get();
299 }
300 
301 
302 
303 /*
304  * Old inventory slot values (pre-2.7.3)
305  */
306 #define OLD_INVEN_WIELD     22
307 #define OLD_INVEN_HEAD      23
308 #define OLD_INVEN_NECK      24
309 #define OLD_INVEN_BODY      25
310 #define OLD_INVEN_ARM       26
311 #define OLD_INVEN_HANDS     27
312 #define OLD_INVEN_RIGHT     28
313 #define OLD_INVEN_LEFT      29
314 #define OLD_INVEN_FEET      30
315 #define OLD_INVEN_OUTER     31
316 #define OLD_INVEN_LITE      32
317 #define OLD_INVEN_AUX       33
318 
319 
320 /*
321  * Read an item (2.7.0 or later)
322  *
323  * Note that savefiles from 2.7.0 and 2.7.1 are obsolete.
324  *
325  * Note that pre-2.7.9 savefiles (from Angband 2.5.1 onward anyway)
326  * can be read using the code above.
327  *
328  * This function attempts to "repair" old savefiles, and to extract
329  * the most up to date values for various object fields.
330  *
331  * Note that Angband 2.7.9 introduced a new method for object "flags"
332  * in which the "flags" on an object are actually extracted when they
333  * are needed from the object kind, artifact index, ego-item index,
334  * and two special "xtra" fields which are used to encode any "extra"
335  * power of certain ego-items.  This had the side effect that items
336  * imported from pre-2.7.9 savefiles will lose any "extra" powers they
337  * may have had, and also, all "uncursed" items will become "cursed"
338  * again, including Calris, even if it is being worn at the time.  As
339  * a complete hack, items which are inscribed with "uncursed" will be
340  * "uncursed" when imported from pre-2.7.9 savefiles.
341  */
342 /* For wilderness levels, dun_depth has been changed from 1 to 4 bytes. */
rd_item(object_type * o_ptr)343 static void rd_item(object_type *o_ptr) {
344 	byte old_dd;
345 	byte old_ds;
346 	s16b old_ac;
347 
348 	u32b f1, f2, f3, f4, f5, f6, esp;
349 	object_kind *k_ptr;
350 	char note[128];
351 
352 	byte tmpbyte;
353 	s32b tmp32s;
354 
355 
356 	/* Hack -- wipe */
357 	WIPE(o_ptr, object_type);
358 
359 	rd_s32b(&o_ptr->owner);
360 	rd_s16b(&o_ptr->level);
361 	if (!older_than(4, 1, 7)) {
362 		if (older_than(4, 4, 14)) {
363 			rd_s32b(&tmp32s);
364 			o_ptr->mode = tmp32s;
365 		}
366 		else rd_byte(&o_ptr->mode);
367 	}
368 
369 	/* Kind (discarded though - Jir -) */
370 	rd_s16b(&o_ptr->k_idx);
371 
372 	/* Location */
373 	rd_byte(&o_ptr->iy);
374 	rd_byte(&o_ptr->ix);
375 
376 	rd_s16b(&o_ptr->wpos.wx);
377 	rd_s16b(&o_ptr->wpos.wy);
378 	rd_s16b(&o_ptr->wpos.wz);
379 
380 	/* Type/Subtype */
381 	rd_byte(&o_ptr->tval);
382 	rd_byte(&o_ptr->sval);
383 	if (!older_than(4, 2, 6)) {
384 		rd_byte(&o_ptr->tval2);
385 		rd_byte(&o_ptr->sval2);
386 	}
387 
388 /* HACKHACKHACK - C. Blue - Moved Khopesh to polearms */
389 //if (o_ptr->tval == 23 && o_ptr->sval == 14) {o_ptr->tval = 22; o_ptr->sval = 9;}
390 
391 #ifdef EXPAND_TV_POTION
392 	/* Convert outdated potions that still use TV_POTION2 to new TV_POTION values */
393 	if (o_ptr->tval == TV_POTION2) {
394 		o_ptr->tval = TV_POTION;
395 		switch (o_ptr->sval) {
396 		case SV_POTION2_CHAUVE_SOURIS: o_ptr->sval = SV_POTION_CHAUVE_SOURIS; break;
397 		case SV_POTION2_LEARNING: o_ptr->sval = SV_POTION_LEARNING; break;
398 		case SV_POTION2_CURE_SANITY: o_ptr->sval = SV_POTION_CURE_SANITY; break;
399 		case SV_POTION2_CURE_LIGHT_SANITY: o_ptr->sval = SV_POTION_CURE_LIGHT_SANITY; break;
400 		case SV_POTION2_CURE_SERIOUS_SANITY: o_ptr->sval = SV_POTION_CURE_SERIOUS_SANITY; break;
401 		case SV_POTION2_CURE_CRITICAL_SANITY: o_ptr->sval = SV_POTION_CURE_CRITICAL_SANITY; break;
402 		default:
403 			/* failure? revert change! */
404 			o_ptr->tval = TV_POTION2;
405 		}
406 	}
407 #else
408 	/* Convert already converted potions back if we reverted EXPAND_TV_POTION, which shouldn't happen :-p */
409 	if (o_ptr->tval == TV_POTION) {
410 		o_ptr->tval = TV_POTION2;
411 		switch (o_ptr->sval) {
412 		case SV_POTION_CHAUVE_SOURIS: o_ptr->sval = SV_POTION2_CHAUVE_SOURIS; break;
413 		case SV_POTION_LEARNING: o_ptr->sval = SV_POTION2_LEARNING; break;
414 		case SV_POTION_CURE_SANITY: o_ptr->sval = SV_POTION2_CURE_SANITY; break;
415 		case SV_POTION_CURE_LIGHT_SANITY: o_ptr->sval = SV_POTION2_CURE_LIGHT_SANITY; break;
416 		case SV_POTION_CURE_SERIOUS_SANITY: o_ptr->sval = SV_POTION2_CURE_SERIOUS_SANITY; break;
417 		case SV_POTION_CURE_CRITICAL_SANITY: o_ptr->sval = SV_POTION2_CURE_CRITICAL_SANITY; break;
418 		default:
419 			/* failure? revert tval */
420 			o_ptr->tval = TV_POTION;
421 		}
422 	}
423 #endif
424 
425 	/* (4.5.8.2 testing) Convert potion svals, their order has been rearranged to optimise inventory sorting order */
426 	if (o_ptr->tval == TV_POTION && older_than(4, 5, 32)) {
427 		//10<->4,14->65,65->64,54<->62; 12->40,13->12,15-23->13-21,64->23,40->22
428 		switch (o_ptr->sval) {
429 		case 4: o_ptr->sval = 10; break;
430 		case 10: o_ptr->sval = 4; break;
431 		case 12: o_ptr->sval = 40; break;
432 		case 13: o_ptr->sval = 12; break;
433 		case 14: o_ptr->sval = 65; break;
434 		case 15: o_ptr->sval = 13; break;
435 		case 16: o_ptr->sval = 14; break;
436 		case 17: o_ptr->sval = 15; break;
437 		case 18: o_ptr->sval = 16; break;
438 		case 19: o_ptr->sval = 17; break;
439 		case 20: o_ptr->sval = 18; break;
440 		case 21: o_ptr->sval = 19; break;
441 		case 22: o_ptr->sval = 20; break;
442 		case 23: o_ptr->sval = 21; break;
443 		case 40: o_ptr->sval = 22; break;
444 		case 54: o_ptr->sval = 62; break;
445 		case 62: o_ptr->sval = 54; break;
446 		case 64: o_ptr->sval = 23; break;
447 		case 65: o_ptr->sval = 64; break;
448 		}
449 	}
450 
451 	/* Base pval */
452 	rd_s32b(&o_ptr->bpval);
453 	rd_s32b(&o_ptr->pval);
454         if (!older_than(4, 2, 3)) {
455 		rd_s32b(&o_ptr->pval2);
456 		rd_s32b(&o_ptr->pval3);
457 	}
458         if (!older_than(4, 2, 6)) {
459 		rd_s32b(&o_ptr->sigil);
460 		rd_s32b(&o_ptr->sseed);
461 	}
462 
463 #if 0 /* too bad hack ;) */
464 	/* Update money pile colour in case the code determining those has been changed */
465 	if (o_ptr->tval == TV_GOLD) {
466 		/* Bad hack: Assume it's a pile dropped by a player in a house.
467 		   This will turn all money piles in towns/wilderness to 'wrong' colour in turn though,
468 		   since they'll all be 'compact=TRUE'. */
469 		if (!o_ptr->wpos.wz) o_ptr->k_idx = gold_colour(o_ptr->pval, FALSE, TRUE);
470 		else o_ptr->k_idx = gold_colour(o_ptr->pval);
471 		o_ptr->sval = k_info[o_ptr->k_idx].sval;
472 	}
473 #endif
474 
475 	rd_byte(&o_ptr->discount);
476 	rd_byte(&o_ptr->number);
477 	rd_s16b(&o_ptr->weight);
478 
479 	if (!older_than(4, 3, 1)) {
480 		rd_u16b(&o_ptr->name1);
481 		rd_u16b(&o_ptr->name2);
482 
483 	} else {
484 		/* Increase portability with pointers to correct type - mikaelh */
485 		byte old_name1, old_name2;
486 		rd_byte(&old_name1);
487 		rd_byte(&old_name2);
488 		o_ptr->name1 = old_name1;
489 		if (o_ptr->name1 == 255) o_ptr->name1 = ART_RANDART;
490 		o_ptr->name2 = old_name2;
491 	}
492 	rd_s32b(&o_ptr->name3);
493         if (!older_than(4, 2, 1))
494 		rd_s32b(&o_ptr->timeout);
495 	else {
496 		/* Increase portability with pointers to correct type - mikaelh */
497 		s16b old_timeout;
498 		rd_s16b(&old_timeout);
499 		o_ptr->timeout = old_timeout;
500 	}
501 
502 	rd_s16b(&o_ptr->to_h);
503 	rd_s16b(&o_ptr->to_d);
504 	rd_s16b(&o_ptr->to_a);
505 
506 
507 	/* -------------------- Fix erroneus item parameters -------------------- */
508 
509 #if 0 /* DEBUGGING PURPOSES - the_sandman */
510 	if (o_ptr->tval == 46)
511 	 {
512 	  s_printf("TRAP_DEBUG: Trap with s_val:%d,to_h:%d,to_d:%d,to_a:%d loaded\n",
513 				o_ptr->sval, o_ptr->to_h, o_ptr->to_d, o_ptr->to_a);
514 	 }
515 #endif
516 
517 #if 0 /* should all be fixed now hopefully - C. Blue */
518 	/* Cap all old non-trueart bows - mikaelh */
519 	if (o_ptr->tval == TV_BOW && (o_ptr->name1 == 0 || o_ptr->name1 == ART_RANDART))
520 	{/* CAP_ITEM_BONI */
521 		if (o_ptr->to_h > 30) o_ptr->to_h = 30;
522 		if (o_ptr->to_d > 30) o_ptr->to_d = 30;
523 	}
524 #endif
525 
526 #ifdef USE_NEW_SHIELDS
527 	/* Cap all old shields' +ac - C. Blue */
528 	if (o_ptr->tval == TV_SHIELD)
529 	{/* CAP_ITEM_BONI */
530  #ifndef NEW_SHIELDS_NO_AC
531 		if (o_ptr->to_a > 15) o_ptr->to_a = 15;
532  #else
533 		o_ptr->to_a = 0;
534  #endif
535 	}
536 #endif
537 
538 #if 0 /* should all be fixed by now */
539 	/* Fix shields base AC or percentage, in case USE_NEW_SHIELDS has been toggled. */
540 	if (o_ptr->tval == TV_SHIELD) {
541 		o_ptr->ac = k_info[o_ptr->k_idx].ac;
542 		/* Fix to_h being set on shields (see above) - mikaelh (removed above bad code - C. Blue) */
543 		o_ptr->to_h = 0;
544 	}
545 #endif
546 
547 #ifdef TO_AC_CAP_30
548 	/* CAP_ITEM_BONI */
549 	if (o_ptr->to_a > 30 && !true_artifact_p(o_ptr)) o_ptr->to_a = 30;
550 #endif
551 
552 #if 0 /* should all be fixed by now */
553 	/* Fix rods that got 'double-timeout' by erroneous discharge function*/
554 	if (o_ptr->tval == TV_ROD) o_ptr->timeout = 0;
555 #endif
556 
557 #if 0 /* should all be fixed by now */
558 	if (o_ptr->tval == TV_LITE && o_ptr->sval == SV_LITE_FEANORIAN) {
559 		if (o_ptr->level < 30) {
560 			if (o_ptr->name2 || o_ptr->name2b)
561 				o_ptr->level = 40;
562 			else
563 				o_ptr->level = 31;
564 		}
565 	}
566 #endif
567 
568 	/* ---------------------------------------------------------------------- */
569 
570 
571 	rd_s16b(&old_ac);
572 
573 	rd_byte(&old_dd);
574 	rd_byte(&old_ds);
575 
576 	if (!older_than(4, 4, 0)) rd_u16b(&o_ptr->ident);
577 	else {
578 		rd_byte(&tmpbyte);
579 		o_ptr->ident = tmpbyte;
580 	}
581 
582 	if (!older_than(4, 3, 1)) {
583 		rd_u16b(&o_ptr->name2b);
584 	} else {
585 		/* Increase portability with pointers to correct type - mikaelh */
586 		byte old_name2b;
587 		rd_byte(&old_name2b);
588 		o_ptr->name2b = old_name2b;
589 	}
590 
591 	if (older_than(4, 3, 20)) {
592 		/* Old flags + Unused */
593 		strip_bytes(14);
594 	}
595 
596 	/* Special powers */
597 	rd_byte(&o_ptr->xtra1);
598 	rd_byte(&o_ptr->xtra2);
599 	/* more special powers (for self-made spellbook feature) */
600 	if (!older_than(4, 3, 16)) {
601 		rd_byte(&o_ptr->xtra3);
602 		rd_byte(&o_ptr->xtra4);
603 		rd_byte(&o_ptr->xtra5);
604 		rd_byte(&o_ptr->xtra6);
605 		rd_byte(&o_ptr->xtra7);
606 		rd_byte(&o_ptr->xtra8);
607 		rd_byte(&o_ptr->xtra9);
608 	}
609 
610 	if (!older_than(4, 3, 20)) {
611 		if (!older_than(4, 3, 21)) {
612 			rd_s32b(&tmp32s);
613 			o_ptr->marked = tmp32s;
614 		} else {
615 			rd_byte(&tmpbyte);
616 			o_ptr->marked = tmpbyte;
617 		}
618 		rd_byte(&o_ptr->marked2);
619 	}
620 
621 	if (!older_than(4, 5, 19)) {
622 		rd_byte((byte *)&o_ptr->questor);
623 		rd_s16b(&o_ptr->questor_idx);
624 		rd_s16b(&o_ptr->quest);
625 		if (!older_than(4, 5, 24)) rd_s16b(&o_ptr->quest_stage);
626 		rd_byte(&o_ptr->questor_invincible);
627 	} else {
628 		o_ptr->questor = FALSE;
629 		o_ptr->quest = 0;
630 	}
631 
632 	/* Inscription */
633 	rd_string(note, 128);
634 	/* Save the inscription */
635 	if (note[0]) o_ptr->note = quark_add(note);
636 	/* hack: 'empty' inscription (startup items) cannot
637 	   be saved this way, so we try to restore it now.. - collides with stolen goods! */
638 	else if (o_ptr->discount == 100 && o_ptr->level == 0
639 	    && object_value_real(0, o_ptr) <= 10000) /* avoid stolen-goods-level-0-hack collision */
640 		o_ptr->note = quark_add("");
641 	if (!older_than(4, 4, 10)) {
642 		rd_byte(&tmpbyte);
643 		o_ptr->note_utag = tmpbyte;
644 	} else o_ptr->note_utag = 0;
645 
646 	rd_u16b(&o_ptr->next_o_idx);
647 	if (!older_than(4, 4, 22)) {
648 		rd_byte(&tmpbyte);
649 		o_ptr->stack_pos = tmpbyte;
650 	}
651 	rd_u16b(&o_ptr->held_m_idx);
652 
653 	/* hack: remove (due to bug) created explodingness from magic ammo */
654 	if (is_ammo(o_ptr->tval) && o_ptr->sval == SV_AMMO_MAGIC && !o_ptr->name1) o_ptr->pval = 0;
655 
656 	/* Obtain k_idx from tval/sval instead :) */
657 	if (o_ptr->k_idx)	/* zero is cipher :) */
658 		o_ptr->k_idx = lookup_kind(o_ptr->tval, o_ptr->sval);
659 
660 #ifdef SEAL_INVALID_OBJECTS
661 	if (!seal_or_unseal_object(o_ptr)) return;
662 #else
663 	/* Object does no longer exist? Delete it! */
664 	if (!o_ptr->k_idx) {
665 		o_ptr->tval = o_ptr->sval = 0;
666 		return;
667 	}
668 #endif
669 
670 #if 0 /* commented out again till it's of use once more (hopefully not) */
671 	/*HACK just to get rid of invalid seals in Bree.. */
672 	if (o_ptr->tval == TV_SPECIAL && o_ptr->sval == SV_SEAL) {
673 		invwipe(o_ptr);
674 		return;
675 	}
676 #endif
677 
678 	/* Obtain the "kind" template */
679 	k_ptr = &k_info[o_ptr->k_idx];
680 
681 	/* Hack -- notice "broken" items */
682 	if (k_ptr->cost <= 0) o_ptr->ident |= ID_BROKEN;
683 
684 
685 	/* Repair non "wearable" items */
686 	if ((o_ptr->tval != TV_TRAPKIT) && !wearable_p(o_ptr)) {
687 		/* Acquire correct fields */
688 		o_ptr->to_h = k_ptr->to_h;
689 		o_ptr->to_d = k_ptr->to_d;
690 		o_ptr->to_a = k_ptr->to_a;
691 
692 		/* Acquire correct fields */
693 		o_ptr->ac = k_ptr->ac;
694 		o_ptr->dd = k_ptr->dd;
695 		o_ptr->ds = k_ptr->ds;
696 
697 		/* All done */
698 		return;
699 	}
700 
701 	/* Extract the flags */
702 	object_flags(o_ptr, &f1, &f2, &f3, &f4, &f5, &f6, &esp);
703 
704 	/* Paranoia */
705 	if (o_ptr->name2) {
706 		ego_item_type *e_ptr;
707 
708 		/* Obtain the ego-item info */
709 		e_ptr = &e_info[o_ptr->name2];
710 
711 		/* Verify that ego-item */
712 		if (!e_ptr->name) o_ptr->name2 = 0;
713 	}
714 
715 
716 	/* Acquire standard fields */
717 	o_ptr->ac = k_ptr->ac;
718 	o_ptr->dd = k_ptr->dd;
719 	o_ptr->ds = k_ptr->ds;
720 
721 	/* Acquire standard weight */
722 	o_ptr->weight = k_ptr->weight;
723 
724 #if 0 /* what's the purpose of this? seems just wrong? maybe deprecated, ancient hack? */
725 	/* Hack -- extract the "broken" flag */
726 	if (o_ptr->pval < 0) o_ptr->ident |= ID_BROKEN;
727 #endif
728 
729 
730 	/* Artifacts */
731 	if (o_ptr->name1) {
732 		artifact_type *a_ptr;
733 
734 		/* Obtain the artifact info */
735 		/* Hack -- Randarts! */
736 		if (o_ptr->name1 == ART_RANDART)
737 			a_ptr = randart_make(o_ptr);
738 		else
739 			a_ptr = &a_info[o_ptr->name1];
740 
741 		/* Hack: Fix old, meanwhile illegal, randarts */
742 		if (!a_ptr) {
743 			o_ptr->name1 = 0;
744 			o_ptr->name3 = 0L;
745 			//o_ptr->pval = 0;
746 		} else {
747 			/* Acquire new artifact "pval" */
748 			o_ptr->pval = a_ptr->pval;
749 
750 			/* Acquire new artifact fields */
751 			o_ptr->ac = a_ptr->ac;
752 			o_ptr->dd = a_ptr->dd;
753 			o_ptr->ds = a_ptr->ds;
754 
755 			/* Acquire new artifact weight */
756 			o_ptr->weight = a_ptr->weight;
757 
758 			/* Hack -- extract the "broken" flag */
759 			if (!a_ptr->cost) o_ptr->ident |= ID_BROKEN;
760 		}
761 	}
762 
763 	/* Ego items */
764 	if (o_ptr->name2) {
765 		ego_item_type *e_ptr;
766 		artifact_type *a_ptr;
767 
768 		/* Obtain the ego-item info */
769 		e_ptr = &e_info[o_ptr->name2];
770 
771 #if 0
772 		/* UnHack. pffft! */
773 		if ((o_ptr->ac < old_ac)) o_ptr->ac = old_ac;
774 		if ((o_ptr->dd < old_dd)) o_ptr->dd = old_dd;
775 		if ((o_ptr->ds < old_ds)) o_ptr->ds = old_ds;
776 #else
777 		a_ptr = ego_make(o_ptr);
778 		o_ptr->ac += a_ptr->ac;
779 		o_ptr->dd += a_ptr->dd;
780 		o_ptr->ds += a_ptr->ds;
781 #endif
782 
783 		/* Hack -- extract the "broken" flag */
784 		if (!e_ptr->cost) o_ptr->ident |= ID_BROKEN;
785 	}
786 
787 	/* hack- fix trap kits with wrong enchantments */
788 	if (o_ptr->tval == TV_TRAPKIT && !is_firearm_trapkit(o_ptr->sval)) o_ptr->to_h = o_ptr->to_d = 0;
789 }
790 
791 
792 /*
793  * Read a "monster" record
794  */
rd_monster_race(monster_race * r_ptr)795 static void rd_monster_race(monster_race *r_ptr) {
796 	byte tmpbyte;
797 	int i;
798 
799 	rd_u16b(&r_ptr->name);
800 	rd_u16b(&r_ptr->text);
801 	rd_byte(&tmpbyte);
802 	r_ptr->hdice = tmpbyte;
803 	rd_byte(&tmpbyte);
804 	r_ptr->hside = tmpbyte;
805 	rd_s16b(&r_ptr->ac);
806 	rd_s16b(&r_ptr->sleep);
807 	rd_byte(&r_ptr->aaf);
808 	rd_byte(&r_ptr->speed);
809 	rd_s32b(&r_ptr->mexp);
810 	rd_s16b(&r_ptr->extra);
811 	rd_byte(&r_ptr->freq_innate);
812 	rd_byte(&r_ptr->freq_spell);
813 	rd_u32b(&r_ptr->flags1);
814 	rd_u32b(&r_ptr->flags2);
815 	rd_u32b(&r_ptr->flags3);
816 	rd_u32b(&r_ptr->flags4);
817 	rd_u32b(&r_ptr->flags5);
818 	rd_u32b(&r_ptr->flags6);
819 	if (!s_older_than(4, 5, 19)) {
820 		rd_u32b(&r_ptr->flags7);
821 		rd_u32b(&r_ptr->flags8);
822 		rd_u32b(&r_ptr->flags9);
823 		rd_u32b(&r_ptr->flags0);
824 	}
825 	rd_s16b(&r_ptr->level);
826 	rd_byte(&r_ptr->rarity);
827 	rd_byte(&r_ptr->d_attr);
828 	rd_byte((byte *)&r_ptr->d_char);
829 	rd_byte(&r_ptr->x_attr);
830 	rd_byte((byte *)&r_ptr->x_char);
831 	for (i = 0; i < 4; i++) {
832 		rd_byte(&r_ptr->blow[i].method);
833 		rd_byte(&r_ptr->blow[i].effect);
834 		rd_byte(&r_ptr->blow[i].d_dice);
835 		rd_byte(&r_ptr->blow[i].d_side);
836 	}
837 }
838 
839 
840 /*
841  * Read a monster
842  */
843 
rd_monster(monster_type * m_ptr)844 static void rd_monster(monster_type *m_ptr) {
845 	byte i;
846 
847 	/* Hack -- wipe */
848 	WIPE(m_ptr, monster_type);
849 	if (s_older_than(4, 2, 7)) {
850 		m_ptr->pet = 0;
851 	} else {
852 		rd_byte(&m_ptr->pet);
853 	}
854 	rd_byte((byte *)&m_ptr->special);
855 	if (!s_older_than(4, 5, 19)) {
856 		rd_byte((byte *)&m_ptr->questor);
857 		rd_s16b(&m_ptr->questor_idx);
858 		rd_s16b(&m_ptr->quest);
859 		rd_byte(&m_ptr->questor_invincible);
860 		rd_byte(&m_ptr->questor_hostile);
861 		if (!s_older_than(4, 5, 27)) rd_byte(&m_ptr->questor_target);
862 	} else m_ptr->questor = FALSE;
863 
864 	/* Owner */
865 	rd_s32b(&m_ptr->owner);
866 
867 	/* Read the monster race */
868 	rd_s16b(&m_ptr->r_idx);
869 
870 	/* Read the other information */
871 	rd_byte(&m_ptr->fy);
872 	rd_byte(&m_ptr->fx);
873 
874 	rd_s16b(&m_ptr->wpos.wx);
875 	rd_s16b(&m_ptr->wpos.wy);
876 	rd_s16b(&m_ptr->wpos.wz);
877 
878 	rd_s16b(&m_ptr->ac);
879 	rd_byte(&m_ptr->speed);
880 	rd_s32b(&m_ptr->exp);
881 	rd_s16b(&m_ptr->level);
882 	for (i = 0; i < 4; i++) {
883 		rd_byte(&(m_ptr->blow[i].method));
884 		rd_byte(&(m_ptr->blow[i].effect));
885 		rd_byte(&(m_ptr->blow[i].d_dice));
886 		rd_byte(&(m_ptr->blow[i].d_side));
887 	}
888 	rd_s32b(&m_ptr->hp);
889 	rd_s32b(&m_ptr->maxhp);
890 	rd_s16b(&m_ptr->csleep);
891 	rd_byte(&m_ptr->mspeed);
892 	rd_s16b(&m_ptr->energy);
893 	rd_byte(&m_ptr->stunned);
894 	rd_byte(&m_ptr->confused);
895 	rd_byte(&m_ptr->monfear);
896 	if (!s_older_than(4, 3, 15)) {
897 		rd_byte(&m_ptr->paralyzed);
898 		rd_byte(&m_ptr->bleeding);
899 		rd_byte(&m_ptr->poisoned);
900 		rd_byte(&m_ptr->blinded);
901 		rd_byte(&m_ptr->silenced);
902 	}
903 
904 	rd_u16b(&m_ptr->hold_o_idx);
905 	rd_u16b(&m_ptr->clone);
906 
907 	rd_s16b(&m_ptr->mind);
908 	if (m_ptr->special || m_ptr->questor) {
909 		MAKE(m_ptr->r_ptr, monster_race);
910 		rd_monster_race(m_ptr->r_ptr);
911 	}
912 
913 	rd_u16b(&m_ptr->ego);
914 	rd_s32b(&m_ptr->name3);
915 
916 	if (!s_older_than(4, 5, 27)) {
917 		rd_s16b(&m_ptr->status);
918 		rd_s16b(&m_ptr->target);
919 		rd_s16b(&m_ptr->possessor);
920 		rd_s16b(&m_ptr->destx);
921 		rd_s16b(&m_ptr->desty);
922 		rd_s16b(&m_ptr->determination);
923 		rd_s16b(&m_ptr->limit_hp);
924 	}
925 }
926 
927 
928 /*
929  * Read the global server-wide monster lore
930  */
rd_global_lore(int r_idx)931 static void rd_global_lore(int r_idx)
932 {
933 	monster_race *r_ptr = &r_info[r_idx];
934 
935 	/* Count sights/deaths/kills */
936 	if (s_older_than(4, 4, 7)) {
937 		s16b tmp16b;
938 
939 		rd_s16b(&tmp16b);
940 		r_ptr->r_sights = tmp16b;
941 
942 		rd_s16b(&tmp16b);
943 		r_ptr->r_deaths = tmp16b;
944 
945 		/* was r_ptr->r_pkills */
946 		strip_bytes(2);
947 
948 		rd_s16b(&tmp16b);
949 		r_ptr->r_tkills = tmp16b;
950 	} else {
951 		rd_s32b(&r_ptr->r_sights);
952 		rd_s32b(&r_ptr->r_deaths);
953 		rd_s32b(&r_ptr->r_tkills);
954 	}
955 
956 	/* Hack -- if killed, it's been seen */
957 	if (r_ptr->r_tkills > r_ptr->r_sights) r_ptr->r_sights = r_ptr->r_tkills;
958 
959 	if (s_older_than(4, 4, 7)) {
960 		/* Old monster lore information - mikaelh */
961 		strip_bytes(38);
962 	}
963 
964 	/* Read the global monster limit */
965 	if (s_older_than(4, 4, 8)) {
966 		byte tmpbyte;
967 		rd_byte(&tmpbyte);
968 		r_ptr->max_num = tmpbyte;
969 	} else {
970 		rd_s32b(&r_ptr->max_num);
971 	}
972 
973 	if (!s_older_than(4, 3, 24)) {
974 		if (s_older_than(4, 4, 8)) {
975 			byte tmpbyte;
976 			rd_byte(&tmpbyte);
977 			r_ptr->cur_num = tmpbyte;
978 		} else {
979 			rd_s32b(&r_ptr->cur_num);
980 		}
981 	}
982 
983 	if (s_older_than(4, 4, 7)) {
984 		/* Old reserved bytes - mikaelh */
985 		strip_bytes(3);
986 	}
987 }
988 
989 
990 
991 
992 /*
993  * Read a store
994  */
rd_store(store_type * st_ptr)995 static errr rd_store(store_type *st_ptr) {
996 	int j;
997 
998 	byte num;
999 	u16b own;
1000 
1001 	/* Read the basic info */
1002 	rd_s32b(&st_ptr->store_open);
1003 	rd_s16b(&st_ptr->insult_cur);
1004 	rd_u16b(&own);
1005 	rd_byte(&num);
1006 	rd_s16b(&st_ptr->good_buy);
1007 	rd_s16b(&st_ptr->bad_buy);
1008 
1009 	/* Last visit */
1010 	rd_s32b(&st_ptr->last_visit);
1011 
1012 	/* Extract the owner (see above) */
1013 	st_ptr->owner = own;
1014 
1015 	/* Replace no longer eligible store owners */
1016 	verify_store_owner(st_ptr);
1017 
1018 	/* Read the items */
1019 	for (j = 0; j < num; j++) {
1020 		object_type forge;
1021 
1022 		/* Read the item */
1023 		rd_item(&forge);
1024 
1025 		/* Hack -- verify item */
1026 		if (!forge.k_idx) s_printf("Warning! Non-existing item detected(erased).\n");
1027 
1028 		/* Acquire valid items */
1029 		else if (st_ptr->stock_num < STORE_INVEN_MAX &&
1030 		    st_ptr->stock_num < st_ptr->stock_size) {
1031 			/* Acquire the item */
1032 			st_ptr->stock[st_ptr->stock_num++] = forge;
1033 		}
1034 	}
1035 
1036 	/* Success */
1037 	return (0);
1038 }
1039 
rd_bbs()1040 static void rd_bbs() {
1041         int i, j;
1042 	s16b saved_lines, parties, guilds;
1043 	char dummy[MAX_CHARS_WIDE];
1044 
1045         rd_s16b(&saved_lines);
1046 
1047 #if 0
1048         for (i = 0; ((i < BBS_LINES) && (i < saved_lines)); i++)
1049                 rd_string(bbs_line[i], MAX_CHARS_WIDE);
1050 #else
1051         for (i = 0; i < saved_lines; i++)
1052 		if (i >= BBS_LINES) rd_string(dummy, MAX_CHARS_WIDE);
1053 		else rd_string(bbs_line[i], MAX_CHARS_WIDE);
1054 #endif
1055 
1056 	/* load pbbs & gbbs - C. Blue */
1057 	if (!s_older_than(4, 4, 9)) {
1058 		/* read old parties */
1059 		rd_s16b(&parties);
1060 		for (j = 0; j < parties; j++)
1061 			if (j < MAX_PARTIES) {
1062 				for (i = 0; i < saved_lines; i++)
1063 					if (i >= BBS_LINES) rd_string(dummy, MAX_CHARS_WIDE);
1064 					else rd_string(pbbs_line[j][i], MAX_CHARS_WIDE);
1065 			} else {
1066 				for (i = 0; i < saved_lines; i++)
1067 					rd_string(dummy, MAX_CHARS_WIDE);
1068 			}
1069 		/* read old guilds */
1070 		rd_s16b(&guilds);
1071 		for (j = 0; j < guilds; j++)
1072 			if (j < MAX_GUILDS) {
1073 				for (i = 0; i < saved_lines; i++)
1074 					if (i >= BBS_LINES) rd_string(dummy, MAX_CHARS_WIDE);
1075 					else rd_string(gbbs_line[j][i], MAX_CHARS_WIDE);
1076 			} else {
1077 				for (i = 0; i < saved_lines; i++)
1078 					rd_string(dummy, MAX_CHARS_WIDE);
1079 			}
1080 	}
1081 }
1082 
rd_notes()1083 static void rd_notes() {
1084         int i;
1085         s16b j;
1086         char dummy[MAX_CHARS_WIDE];
1087 
1088         rd_s16b(&j);
1089         for (i = 0; i < j; i++) {
1090 	        if (i >= MAX_NOTES) {
1091 			rd_string(dummy, MAX_CHARS_WIDE);
1092 			rd_string(dummy, NAME_LEN);
1093 			rd_string(dummy, NAME_LEN);
1094 			continue;
1095 	        }
1096                 rd_string(priv_note[i], MAX_CHARS_WIDE);
1097                 rd_string(priv_note_sender[i], NAME_LEN);
1098                 rd_string(priv_note_target[i], NAME_LEN);
1099         }
1100 
1101         rd_s16b(&j);
1102         for (i = 0; i < j; i++) {
1103 	        if (i >= MAX_PARTYNOTES) {
1104 			rd_string(dummy, MAX_CHARS_WIDE);
1105 			rd_string(dummy, NAME_LEN);
1106 			continue;
1107 	        }
1108                 rd_string(party_note[i], MAX_CHARS_WIDE);
1109                 rd_string(party_note_target[i], NAME_LEN);
1110         }
1111 
1112         rd_s16b(&j);
1113         for (i = 0; i < j; i++) {
1114 	        if (i >= MAX_GUILDNOTES) {
1115 			rd_string(dummy, MAX_CHARS_WIDE);
1116 			rd_string(dummy, NAME_LEN);
1117 			continue;
1118 	        }
1119                 rd_string(guild_note[i], MAX_CHARS_WIDE);
1120                 rd_string(guild_note_target[i], NAME_LEN);
1121         }
1122         //omitted (use custom.lua instead): admin_note[MAX_ADMINNOTES]
1123 }
1124 
rd_xorders()1125 static void rd_xorders() {
1126 	int i;
1127 	rd_s16b(&questid);
1128 	for (i = 0; i < MAX_XORDERS; i++) {
1129 		rd_u16b(&xorders[i].active);
1130 		rd_u16b(&xorders[i].id);
1131 		rd_s16b(&xorders[i].type);
1132 		rd_u16b(&xorders[i].flags);
1133 		rd_s32b(&xorders[i].creator);
1134 		rd_s32b(&xorders[i].turn);
1135 	}
1136 }
1137 
rd_quests()1138 static void rd_quests() {
1139 	int i, j, k;
1140 	s16b max, questors;
1141 	byte flags = QI_FLAGS, tmpbyte;
1142 	int dummysize1 = sizeof(byte) * 7 + sizeof(s16b) * 3 + sizeof(s32b);
1143 	int dummysize2 = sizeof(byte) * 5 + sizeof(s16b);
1144 
1145 	rd_s16b(&max);
1146 	rd_s16b(&questors);
1147 	if (max > max_q_idx) s_printf("Warning: Read more quest info than available quests.\n");
1148 	if (questors > QI_QUESTORS) s_printf("Warning: Read more questors than allowed.\n");
1149 	if (!s_older_than(4, 5, 20)) {
1150 		rd_byte(&flags);
1151 		if (flags > QI_FLAGS) s_printf("Warning: Read more flags than available.\n");
1152 	}
1153 
1154 	for (i = 0; i < max; i++) {
1155 		if (max > max_q_idx) {
1156 			strip_bytes(dummysize1);
1157 			continue;
1158 		}
1159 
1160 		/* read 'active' and 'disabled' state */
1161 		rd_byte((byte *) &tmpbyte);
1162 		/* in case it was already disabled in q_info.txt */
1163 		if (!q_info[i].disabled) {
1164 			q_info[i].active = (tmpbyte != 0);
1165 			rd_byte((byte *) &q_info[i].disabled);
1166 		} else {
1167 			rd_byte((byte *) &tmpbyte); /* strip as dummy info */
1168 			q_info[i].disabled_on_load = TRUE;
1169 		}
1170 
1171 		rd_s16b(&q_info[i].cur_cooldown);
1172 		rd_s16b(&q_info[i].cur_stage);
1173 		rd_s32b(&q_info[i].turn_activated);
1174 		//restructure if (!older_than(4, 5, 26)) rd_s32b(&q_info[i].turn_acquired);
1175 
1176 		for (j = 0; j < questors; j++) {
1177 			if (j >= QI_QUESTORS) {
1178 				strip_bytes(dummysize2);
1179 				continue;
1180 			}
1181 #if 0//restructure
1182 			rd_byte((byte *) &q_info[i].current_wpos[j].wx);
1183 			rd_byte((byte *) &q_info[i].current_wpos[j].wy);
1184 			rd_byte((byte *) &q_info[i].current_wpos[j].wz);
1185 			rd_byte((byte *) &q_info[i].current_x[j]);
1186 			rd_byte((byte *) &q_info[i].current_y[j]);
1187 
1188 			rd_s16b(&q_info[i].questor_m_idx[j]);
1189 #else
1190 			strip_bytes(7);
1191 #endif
1192 		}
1193 
1194 		if (!older_than(4, 5, 25))
1195 			rd_u16b(&q_info[i].flags);
1196 		else if (!s_older_than(4, 5, 20)) {
1197 			for (j = 0; j < flags; j++) {
1198 				if (j >= QI_FLAGS) {
1199 					strip_bytes(1);
1200 					continue;
1201 				}
1202 				rd_byte(&tmpbyte);
1203 				if (tmpbyte) q_info[i].flags |= (0x1 << j);
1204 			}
1205 		}
1206 
1207 		if (!older_than(4, 5, 24)) {
1208 			for (k = 0; k < QI_STAGES; k++) {
1209 				for (j = 0; j < QI_GOALS; j++) {
1210 #if 0//restructure
1211 					rd_byte((byte *) &q_info[i].goals[k][j]);
1212 					rd_s16b(&q_info[i].kill_number_left[k][j]);
1213 #else
1214 					strip_bytes(3);
1215 #endif
1216 				}
1217 				for (j = 0; j < 5; j++) {
1218 #if 0//restructure
1219 					rd_byte((byte *) &q_info[i].goalsopt[k][j]);
1220 					rd_s16b(&q_info[i].killopt_number_left[k][j]);
1221 #else
1222 					strip_bytes(3);
1223 #endif
1224 				}
1225 			}
1226 		}
1227 	}
1228 	s_printf("Read %d/%d saved quests states (discarded %d).\n", max, max_q_idx, max > max_q_idx ? max - max_q_idx : 0);
1229 	fix_questors_on_startup();
1230 }
1231 
rd_guilds()1232 static void rd_guilds() {
1233 	int i;
1234 	u16b tmp16u;
1235 	rd_u16b(&tmp16u);
1236 	if(tmp16u > MAX_GUILDS){
1237 		s_printf("Too many guilds (%d)\n", tmp16u);
1238 		return;
1239 	}
1240 	for (i = 0; i < tmp16u; i++) {
1241 		if (!s_older_than(4, 5, 8)) rd_u32b(&guilds[i].dna);
1242 		else {
1243 			guilds[i].dna = rand_int(0xFFFF) << 16;
1244 			guilds[i].dna += rand_int(0xFFFF);
1245 		}
1246 		rd_string(guilds[i].name, 80);
1247 		rd_s32b(&guilds[i].master);
1248 		rd_s32b(&guilds[i].members);
1249 		if (!s_older_than(4, 5, 2)) rd_byte(&guilds[i].cmode);
1250 		else {
1251 			cptr name = NULL;
1252 			/* first entry is dummy anyway */
1253 			if (i == 0) guilds[0].cmode = 0;
1254 			else if (guilds[i].master && (name = lookup_player_name(guilds[i].master)) != NULL) {
1255 				guilds[i].cmode = lookup_player_mode(guilds[i].master);
1256 	                        s_printf("Guild '%s' (%d): Mode has been fixed to master's ('%s',%d) mode %d.\n",
1257                                     guilds[i].name, i, name, guilds[i].master, guilds[i].cmode);
1258                         } else { /* leaderless guild, ow */
1259                                 s_printf("Guild '%s' (%d): Fixing lost guild, master (%d) is '%s'.\n",
1260                                     guilds[i].name, i, guilds[i].master, name ? name : "(null)");
1261                                 fix_lost_guild_mode(i);
1262                         }
1263 		}
1264 		rd_u32b(&guilds[i].flags);
1265 		rd_s16b(&guilds[i].minlev);
1266 		if (!s_older_than(4, 4, 20)) {
1267 			int j;
1268 			for (j = 0; j < 5; j++)
1269 				rd_string(guilds[i].adder[j], NAME_LEN);
1270 		}
1271 		if (!s_older_than(4, 5, 0)) rd_s16b(&guilds[i].h_idx);
1272 		else guilds[i].h_idx = 0;
1273 	}
1274 }
1275 
1276 /*
1277  * Read some party info
1278 
1279  FOUND THE BUIG!!!!!! the disapearing party bug. no more.
1280  I hope......
1281  -APD-
1282  */
rd_party(int n)1283 static void rd_party(int n) {
1284 	party_type *party_ptr = &parties[n];
1285 
1286 	/* Party name */
1287 	rd_string(party_ptr->name, 80);
1288 
1289 	/* Party owner's name */
1290 	rd_string(party_ptr->owner, 20);
1291 
1292 	/* Number of people and creation time */
1293 	rd_s32b(&party_ptr->members);
1294 	rd_s32b(&party_ptr->created);
1295 
1296 	/* Party type and members */
1297 	if (!s_older_than(4, 1, 7))
1298 		rd_byte(&party_ptr->mode);
1299 	else
1300 		party_ptr->mode = 0;
1301 
1302 	if (!s_older_than(4, 5, 2)) rd_byte(&party_ptr->cmode);
1303 	else {
1304 		/* first entry is dummy anyway; party in use at all? if not then we're done */
1305 		if (n == 0 || !party_ptr->members) party_ptr->cmode = 0;
1306 		else {
1307 			u32b p_id = lookup_player_id(party_ptr->owner);
1308                         if (p_id) {
1309                                 parties[n].cmode = lookup_player_mode(p_id);
1310                                 s_printf("Party '%s' (%d): Mode has been fixed to %d ('%s',%d).\n",
1311                                     parties[n].name, n, parties[n].cmode, parties[n].owner, p_id);
1312                         }
1313                         /* paranoia - a party without owner shouldn't exist */
1314                         else s_printf("Party '%s' (%d): Mode couldn't be fixed ('%s',%d).\n",
1315                             parties[n].name, n, parties[n].owner, p_id);
1316 		}
1317 	}
1318 
1319 	if (!s_older_than(4, 4, 19)) rd_u32b(&party_ptr->flags);
1320 
1321 	/* Hack -- repair dead parties
1322 	   I THINK this line was causing some problems....
1323 	   lets find out
1324 	   */
1325 	if (!lookup_player_id(party_ptr->owner))
1326 	{
1327 		/*
1328 		   Set to no people in party
1329 		   party_ptr->members = 0;
1330 		   */
1331 	}
1332 }
1333 
1334 /*
1335  * Read some house info
1336  */
rd_house(int n)1337 static void rd_house(int n)
1338 {
1339 	int i;
1340 	house_type *house_ptr = &houses[n];
1341 	cave_type **zcave;
1342 	object_type dump;
1343 
1344 	rd_byte(&house_ptr->x);
1345 	rd_byte(&house_ptr->y);
1346 	rd_byte(&house_ptr->dx);
1347 	rd_byte(&house_ptr->dy);
1348 	MAKE(house_ptr->dna, struct dna_type);
1349 	rd_u32b(&house_ptr->dna->creator);
1350         if (!s_older_than(4, 4, 14)) rd_byte(&house_ptr->dna->mode);
1351 	rd_s32b(&house_ptr->dna->owner);
1352 	rd_byte(&house_ptr->dna->owner_type);
1353 #if 0 /* player hash is read _AFTER_ houses! So this is done via /fix-house-modes instead- C. Blue */
1354 	if (s_older_than(4, 4, 14)) {
1355 		/* if house doesn't have its mode set yet, search
1356 		   hash for player who owns it and set mode to his. */
1357 		if (house_ptr->dna->owner &&
1358 		    (house_ptr->dna->owner_type == OT_PLAYER)) {
1359 			house_ptr->dna->mode = lookup_player_mode(house_ptr->dna->owner);
1360 		}
1361 	}
1362 #endif
1363 	rd_byte(&house_ptr->dna->a_flags);
1364 	rd_u16b(&house_ptr->dna->min_level);
1365 	rd_u32b(&house_ptr->dna->price);
1366 	rd_u16b(&house_ptr->flags);
1367 
1368 	rd_s16b(&house_ptr->wpos.wx);
1369 	rd_s16b(&house_ptr->wpos.wy);
1370 	rd_s16b(&house_ptr->wpos.wz);
1371 
1372 	if ((zcave = getcave(&house_ptr->wpos))) {
1373 		struct c_special *cs_ptr;
1374 		if (house_ptr->flags & HF_STOCK) {
1375 			/* add dna to static levels even though town-generated */
1376 			if ((cs_ptr = GetCS(&zcave[house_ptr->dy][house_ptr->dx], CS_DNADOOR)))
1377 				cs_ptr->sc.ptr = house_ptr->dna;
1378 			else if ((cs_ptr = AddCS(&zcave[house_ptr->dy][house_ptr->dx], CS_DNADOOR)))
1379 				cs_ptr->sc.ptr = house_ptr->dna;
1380 		}
1381 		else
1382 		{
1383 			/* add dna to static levels */
1384 			if ((cs_ptr = GetCS(&zcave[house_ptr->dy][house_ptr->dx], CS_DNADOOR)))
1385 				cs_ptr->sc.ptr = house_ptr->dna;
1386 			else if ((cs_ptr = AddCS(&zcave[house_ptr->y+house_ptr->dy][house_ptr->x+house_ptr->dx], CS_DNADOOR)))
1387 				cs_ptr->sc.ptr = house_ptr->dna;
1388 		}
1389 	}
1390 	if (house_ptr->flags&HF_RECT){
1391 		rd_byte(&house_ptr->coords.rect.width);
1392 		rd_byte(&house_ptr->coords.rect.height);
1393 	}
1394 	else{
1395 		i = -2;
1396 		C_MAKE(house_ptr->coords.poly, MAXCOORD, char);
1397 		do {
1398 			i += 2;
1399 			rd_byte((byte*)&house_ptr->coords.poly[i]);
1400 			rd_byte((byte*)&house_ptr->coords.poly[i + 1]);
1401 		} while(house_ptr->coords.poly[i] || house_ptr->coords.poly[i + 1]);
1402 		GROW(house_ptr->coords.poly, MAXCOORD, i + 2, byte);
1403 	}
1404 
1405 	if (!s_older_than(4, 4, 13)) {
1406 		rd_byte(&house_ptr->colour);
1407 		rd_byte(&house_ptr->xtra);
1408 	}
1409 
1410 #ifndef USE_MANG_HOUSE_ONLY
1411 	rd_s16b(&house_ptr->stock_num);
1412 	rd_s16b(&house_ptr->stock_size);
1413 	if (house_ptr->stock_size > STORE_INVEN_MAX) house_ptr->stock_size = STORE_INVEN_MAX;
1414 	C_MAKE(house_ptr->stock, house_ptr->stock_size, object_type);
1415 	for (i = 0; i < house_ptr->stock_num; i++) {
1416 		if (i < STORE_INVEN_MAX) rd_item(&house_ptr->stock[i]);
1417 		else rd_item(&dump);
1418 	}
1419 	if (house_ptr->stock_num > STORE_INVEN_MAX) house_ptr->stock_num = STORE_INVEN_MAX;
1420 #endif	/* USE_MANG_HOUSE_ONLY */
1421 }
1422 
rd_wild(wilderness_type * w_ptr)1423 static void rd_wild(wilderness_type *w_ptr)
1424 {
1425 	/* future use */
1426 	strip_bytes(4);
1427 	/* terrain type */
1428 	rd_u16b(&w_ptr->type);
1429 
1430 	/* this needs to be loaded or it'll be lost until this piece of
1431 	   wilderness is unstaticed again. - C. Blue */
1432 	if (!s_older_than(4, 5, 12)) rd_u16b(&w_ptr->bled);
1433 	else w_ptr->bled = WILD_GRASSLAND; /* init */
1434 
1435 	/* the flags */
1436 	rd_u32b(&w_ptr->flags);
1437 
1438 	/* the player(KING) owning the wild */
1439 	rd_s32b(&w_ptr->own);
1440 }
1441 
1442 /*
1443  * Read/Write the "extra" information
1444  */
1445 
rd_extra(int Ind)1446 static bool rd_extra(int Ind)
1447 {
1448 	player_type *p_ptr = Players[Ind];
1449 
1450 	int i, j;
1451 	monster_race *r_ptr;
1452 	char login_char_name[80];
1453 
1454 	byte tmp8u;
1455 	u16b tmp16u, panic;
1456 	s16b tmp16s;
1457 	u32b tmp32u;
1458 	s32b tmp32s;
1459 
1460 	/* 'Savegame filename character conversion' exploit fix - C. Blue */
1461 	strcpy(login_char_name, p_ptr->name);
1462 	rd_string(p_ptr->name, 32);
1463 	if (strcmp(p_ptr->name, login_char_name)) {
1464 		s_printf("$INTRUSION$ - %s tried to use savegame %s\n", p_ptr->name, login_char_name);
1465 		return (1);
1466 	}
1467 
1468 	rd_string(p_ptr->died_from, 80);
1469 
1470 	/* if new enough, load the died_from_list information */
1471 	rd_string(p_ptr->died_from_list, 80);
1472 	rd_s16b(&p_ptr->died_from_depth);
1473 
1474 	for (i = 0; i < 4; i++)
1475 		rd_string(p_ptr->history[i], 60);
1476 
1477 	if (older_than(4, 2, 7)) {
1478 		p_ptr->has_pet = 0; //assume no pet
1479 	} else {
1480 		rd_byte(&p_ptr->has_pet);
1481 	}
1482 
1483 	/* Divinity has been absorbed by traits (ptrait) now */
1484 	if (older_than(4, 4, 12)) {
1485 		/* Divinity support in savefile now on all servers - mikaelh */
1486 		if (older_than(4, 3, 2)) {
1487 //			p_ptr->divinity = DIVINE_UNDEF; /* which is simply 0 */
1488 			p_ptr->ptrait = TRAIT_NONE;
1489 		} else {
1490 			rd_byte(&tmp8u);
1491 			/* backwards compatible */
1492 			if (tmp8u == 0x1) p_ptr->ptrait = TRAIT_ENLIGHTENED;
1493 			if (tmp8u == 0x10) p_ptr->ptrait = TRAIT_CORRUPTED;
1494 		}
1495 	}
1496 
1497 	/* Class/Race/Gender/Party */
1498 	rd_byte(&p_ptr->prace);
1499 #ifdef ENABLE_KOBOLD /* Kobold was inserted in the middle, instead of added to the end of the races array */
1500         if (older_than(4, 4, 28) && p_ptr->prace >= RACE_KOBOLD) p_ptr->prace++;
1501 #endif
1502         rd_byte(&p_ptr->pclass);
1503         if (!older_than(4, 4, 11)) rd_byte(&p_ptr->ptrait);
1504 	if (older_than(4, 3, 5)) { /* class order changed: warrior now first class, so newbies won't choose adventurer */
1505 		if (p_ptr->pclass == CLASS_WARRIOR) p_ptr->pclass = CLASS_ADVENTURER;
1506 		else if (p_ptr->pclass < CLASS_DRUID) p_ptr->pclass--;
1507 	}
1508 	rd_byte(&p_ptr->male);
1509 	if (older_than(4, 2, 4)) {
1510 		rd_byte(&tmp8u);
1511 		p_ptr->party = tmp8u; /* convert the old byte to u16b - mikaelh */
1512 	}
1513 	else rd_u16b(&p_ptr->party);
1514 	rd_byte(&p_ptr->mode);
1515 
1516 	/* Special Race/Class info */
1517 	rd_byte(&p_ptr->hitdie);
1518 	rd_s16b(&p_ptr->expfact);
1519 
1520 	/* Age/Height/Weight */
1521 	rd_s16b(&p_ptr->age);
1522 	rd_s16b(&p_ptr->ht);
1523 	rd_s16b(&p_ptr->wt);
1524 #ifndef SAVEDATA_TRANSFER_KLUDGE
1525 	rd_u16b(&p_ptr->align_good);	/* alignment */
1526 	rd_u16b(&p_ptr->align_law);
1527 #endif	/* SAVEDATA_TRANSFER_KLUDGE */
1528 
1529 	/* Read the stat info */
1530 	for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_max[i]);
1531 	for (i = 0; i < 6; i++) rd_s16b(&p_ptr->stat_cur[i]);
1532 
1533 	/* Dump the stats (maximum and current) */
1534 	for (i = 0; i < 6; ++i) rd_s16b(&p_ptr->stat_cnt[i]);
1535 	for (i = 0; i < 6; ++i) rd_s16b(&p_ptr->stat_los[i]);
1536 
1537         /* Read the skills */
1538 	{
1539 		rd_u16b(&tmp16u);
1540 		if (tmp16u > MAX_SKILLS)
1541 		{
1542 			quit("Too many skills!");
1543 		}
1544 		for (i = 0; i < tmp16u; ++i)
1545 		{
1546 			rd_s32b(&p_ptr->s_info[i].value);
1547 			rd_u16b(&p_ptr->s_info[i].mod);
1548 			rd_byte(&tmp8u);
1549 			p_ptr->s_info[i].dev = tmp8u;
1550 			rd_byte(&tmp8u);
1551 			if (!older_than(4, 4, 3)) {
1552 				p_ptr->s_info[i].flags1 = tmp8u;
1553 				rd_s32b(&p_ptr->s_info[i].base_value);
1554 			} else {
1555 #if 0 //SMOOTHSKILLS
1556 				p_ptr->s_info[i].hidden = tmp8u;
1557 #else
1558 				if (tmp8u) p_ptr->s_info[i].flags1 |= SKF1_HIDDEN;
1559 #endif
1560 				if (!older_than(4, 3, 17)) {
1561 					rd_byte(&tmp8u);
1562 #if 0 //SMOOTHSKILLS
1563 					p_ptr->s_info[i].dummy = (tmp8u) ? TRUE : FALSE;
1564 #else
1565 					if (tmp8u) p_ptr->s_info[i].flags1 |= SKF1_DUMMY;
1566 #endif
1567 				} else {
1568 #if 0 //SMOOTHSKILLS
1569 					p_ptr->s_info[i].dummy = FALSE;
1570 #else
1571 					p_ptr->s_info[i].flags1 &= ~SKF1_DUMMY;
1572 #endif
1573 				}
1574 			}
1575 			p_ptr->s_info[i].touched = TRUE;
1576 		}
1577 		rd_s16b(&p_ptr->skill_points);
1578 
1579 		/* /undoskills - mikaelh */
1580 		if (!older_than(4, 4, 4)) {
1581 			for (i = 0; i < tmp16u; ++i)
1582 			{
1583 				rd_s32b(&p_ptr->s_info_old[i].value);
1584 				rd_u16b(&p_ptr->s_info_old[i].mod);
1585 				rd_byte(&tmp8u);
1586 				p_ptr->s_info_old[i].dev = tmp8u;
1587 				rd_byte(&tmp8u);
1588 				p_ptr->s_info_old[i].flags1 = tmp8u;
1589 				rd_s32b(&p_ptr->s_info_old[i].base_value);
1590 				p_ptr->s_info_old[i].touched = TRUE;
1591 			}
1592 			rd_s16b(&p_ptr->skill_points_old);
1593 			rd_byte((byte *) &p_ptr->reskill_possible);
1594 		}
1595 	}
1596 
1597 	/* Make a copy of the skills - mikaelh */
1598 //	memcpy(p_ptr->s_info_old, p_ptr->s_info, MAX_SKILLS * sizeof(skill_player));
1599 //	p_ptr->skill_points_old = p_ptr->skill_points;
1600 
1601 	rd_s32b(&p_ptr->id);
1602 
1603 	rd_u32b(&p_ptr->dna);
1604 	rd_s32b(&p_ptr->turn);
1605 
1606 	if (!older_than(4, 4, 5)) {
1607 		rd_s32b(&p_ptr->turns_online);
1608 		rd_s32b(&p_ptr->turns_afk);
1609 		rd_s32b(&p_ptr->turns_idle);
1610 	}
1611 	if (!older_than(4, 4, 6)) rd_s32b(&p_ptr->turns_active);
1612 
1613 	/* If he was created in the pre-ID days, give him one */
1614 	if (!p_ptr->id)
1615 		p_ptr->id = newid();
1616 
1617 	strip_bytes(20);	/* transient stats - these get ignored by both save and load atm. */
1618 
1619 	rd_s32b(&p_ptr->au);
1620 
1621 	rd_s32b(&p_ptr->max_exp);
1622         /* Build maximum level (the one displayed if life levels were restored right now) */
1623 	p_ptr->max_lev = 1;
1624 #ifndef ALT_EXPRATIO
1625 	while ((p_ptr->max_lev < (is_admin(p_ptr) ? PY_MAX_LEVEL : PY_MAX_PLAYER_LEVEL)) &&
1626 	    (p_ptr->max_exp >= ((s64b)(((s64b)player_exp[p_ptr->max_lev-1] * (s64b)p_ptr->expfact) / 100L))))
1627 #else
1628 	while ((p_ptr->max_lev < (is_admin(p_ptr) ? PY_MAX_LEVEL : PY_MAX_PLAYER_LEVEL)) &&
1629 	    (p_ptr->max_exp >= (s64b)player_exp[p_ptr->max_lev-1]))
1630 #endif
1631 	{
1632 		/* Gain a level */
1633 		p_ptr->max_lev++;
1634 	}
1635 	rd_s32b(&p_ptr->exp);
1636 	rd_u16b(&p_ptr->exp_frac);
1637 
1638 	rd_s16b(&p_ptr->lev);
1639 
1640 	rd_s16b(&p_ptr->mhp);
1641 	rd_s16b(&p_ptr->chp);
1642 	rd_u16b(&p_ptr->chp_frac);
1643 
1644 	if (!older_than(4, 3, 8)) {
1645 		rd_s16b(&p_ptr->mst);
1646 		rd_s16b(&p_ptr->cst);
1647 		rd_s16b(&p_ptr->cst_frac);
1648 	}
1649 /* hack for old chars */
1650 if (p_ptr->mst != 10) p_ptr->mst = 10;
1651 
1652 	rd_s16b(&p_ptr->msp);
1653 	rd_s16b(&p_ptr->csp);
1654 	rd_u16b(&p_ptr->csp_frac);
1655 
1656 	rd_s16b(&p_ptr->max_plv);
1657 	rd_s16b(&p_ptr->max_dlv);
1658         if (!older_than(4, 4, 23)) {
1659 		for (i = 0; i < MAX_D_IDX * 2; i++) {
1660 			rd_byte(&tmp8u);
1661 			p_ptr->max_depth[i] = tmp8u;
1662 			rd_byte(&tmp8u);
1663 			p_ptr->max_depth_wx[i] = tmp8u;
1664 			rd_byte(&tmp8u);
1665 			p_ptr->max_depth_wy[i] = tmp8u;
1666 			rd_byte(&tmp8u);
1667 			p_ptr->max_depth_tower[i] = (tmp8u != 0);
1668 			/* hack: fix for chars that logged in before this was completed properly */
1669 			if (p_ptr->max_depth[i] > 200) p_ptr->dummy_option_8 = TRUE;
1670 		}
1671 	}
1672 	/* else branch is in rd_savefile_new_aux() since we need wild_map[] array */
1673 
1674 	rd_s16b(&p_ptr->py);
1675 	rd_s16b(&p_ptr->px);
1676 
1677 	rd_s16b(&p_ptr->wpos.wx);
1678 	rd_s16b(&p_ptr->wpos.wy);
1679 	rd_s16b(&p_ptr->wpos.wz);
1680 
1681 	rd_u16b(&p_ptr->town_x);
1682 	rd_u16b(&p_ptr->town_y);
1683 
1684 	/* More info */
1685 
1686 	rd_s16b(&p_ptr->ghost);
1687 	rd_s16b(&p_ptr->sc);
1688 	rd_s16b(&p_ptr->fruit_bat);
1689 
1690 	/* Read the flags */
1691 	rd_byte(&p_ptr->lives);
1692 	/* hack, if save file was from an older version we need to convert it: */
1693  	if (cfg.lifes && !p_ptr->lives) p_ptr->lives = cfg.lifes+1;
1694 	/* If the server's life amount was reduced, apply it to players */
1695 	if (cfg.lifes && (p_ptr->lives > cfg.lifes+1)) p_ptr->lives = cfg.lifes+1;
1696 
1697         if (!older_than(4, 2, 0)) {
1698 		rd_byte(&p_ptr->houses_owned);
1699 	} else {
1700 		for (i = 0; i < num_houses; i++) {
1701 			if ((houses[i].dna->owner_type == OT_PLAYER) &&
1702 			    (houses[i].dna->owner == p_ptr->id))
1703 				p_ptr->houses_owned++;
1704 		}
1705 	}
1706 
1707 	/* hack */
1708 	rd_byte(&tmp8u);
1709 	rd_s16b(&p_ptr->blind);
1710 	rd_s16b(&p_ptr->paralyzed);
1711 	rd_s16b(&p_ptr->confused);
1712 	rd_s16b(&p_ptr->food);
1713 	strip_bytes(4);	/* Old "food_digested" / "protection" */
1714 	rd_s16b(&p_ptr->energy);
1715         rd_s16b(&p_ptr->fast);
1716         if (!older_than(4, 0, 3))
1717                 rd_s16b(&p_ptr->fast_mod);
1718 	rd_s16b(&p_ptr->slow);
1719 	rd_s16b(&p_ptr->afraid);
1720 	rd_s16b(&p_ptr->cut);
1721 	rd_s16b(&p_ptr->stun);
1722 	rd_s16b(&p_ptr->poisoned);
1723 	rd_s16b(&p_ptr->image);
1724 	rd_s16b(&p_ptr->protevil);
1725 	rd_s16b(&p_ptr->invuln);
1726 	rd_s16b(&p_ptr->hero);
1727 	rd_s16b(&p_ptr->shero);
1728 	if (!older_than(4, 3, 7)) rd_s16b(&p_ptr->berserk);
1729 	rd_s16b(&p_ptr->shield);
1730         if (!older_than(4, 0, 4)) {
1731                 rd_s16b(&p_ptr->shield_power);
1732                 rd_s16b(&p_ptr->shield_opt);
1733                 rd_s16b(&p_ptr->shield_power_opt);
1734                 rd_s16b(&p_ptr->shield_power_opt2);
1735                 rd_s16b(&p_ptr->tim_thunder);
1736                 rd_s16b(&p_ptr->tim_thunder_p1);
1737                 rd_s16b(&p_ptr->tim_thunder_p2);
1738                 rd_s16b(&p_ptr->tim_lev);
1739                 rd_s16b(&p_ptr->tim_ffall);
1740                 rd_s16b(&p_ptr->tim_regen);
1741                 rd_s16b(&p_ptr->tim_regen_pow);
1742         }
1743 	rd_s16b(&p_ptr->blessed);
1744 	rd_s16b(&p_ptr->tim_invis);
1745 	rd_s16b(&p_ptr->word_recall);
1746 	rd_s16b(&p_ptr->see_infra);
1747 	rd_s16b(&p_ptr->tim_infra);
1748 	rd_s16b(&p_ptr->oppose_fire);
1749 	rd_s16b(&p_ptr->oppose_cold);
1750 	rd_s16b(&p_ptr->oppose_acid);
1751 	rd_s16b(&p_ptr->oppose_elec);
1752 	rd_s16b(&p_ptr->oppose_pois);
1753 	rd_s16b(&p_ptr->prob_travel);
1754 	rd_s16b(&p_ptr->st_anchor);
1755 	rd_s16b(&p_ptr->tim_esp);
1756 	rd_s16b(&p_ptr->adrenaline);
1757 	rd_s16b(&p_ptr->biofeedback);
1758 
1759 	rd_byte(&p_ptr->confusing);
1760 		rd_u16b(&p_ptr->tim_jail);
1761 		rd_u16b(&p_ptr->tim_susp);
1762 		rd_u16b(&p_ptr->pkill);
1763 		rd_u16b(&p_ptr->tim_pkill);
1764 	rd_s16b(&p_ptr->tim_wraith);
1765 	rd_byte((byte *)&p_ptr->wraith_in_wall);
1766 	rd_byte(&p_ptr->searching);
1767 	rd_byte(&p_ptr->maximize);
1768 	rd_byte(&p_ptr->preserve);
1769 	rd_byte(&p_ptr->stunning);
1770 
1771 	rd_s16b(&p_ptr->body_monster);
1772 	/* Fix limits */
1773 	if (p_ptr->body_monster > MAX_R_IDX || p_ptr->body_monster < 0) p_ptr->body_monster = 0;
1774 	rd_s16b(&p_ptr->auto_tunnel);
1775 
1776 	rd_s16b(&p_ptr->tim_meditation);
1777 
1778 	rd_s16b(&p_ptr->tim_invisibility);
1779 	rd_s16b(&p_ptr->tim_invis_power);
1780 	if (!older_than(4, 5, 9)) rd_s16b(&p_ptr->tim_invis_power2);
1781 
1782 	rd_s16b(&p_ptr->fury);
1783 
1784 	rd_s16b(&p_ptr->tim_manashield);
1785 
1786 	rd_s16b(&p_ptr->tim_traps);
1787 
1788 	rd_s16b(&p_ptr->tim_mimic);
1789 	rd_s16b(&p_ptr->tim_mimic_what);
1790 
1791 	/* Monster Memory */
1792 	rd_u16b(&tmp16u);
1793 
1794 	/* Incompatible save files */
1795 	if (tmp16u > MAX_R_IDX) {
1796 		s_printf("Too many (%u) monster races!\n", tmp16u);
1797 		return (22);
1798 	}
1799 	for (i = 0; i < tmp16u; i++) {
1800 		rd_s16b(&p_ptr->r_killed[i]);
1801 
1802 		/* Hack -- try to fix the unique list */
1803 		r_ptr = &r_info[i];
1804 
1805 		if (p_ptr->r_killed[i] && !r_ptr->r_tkills &&
1806 		    !is_admin(p_ptr) &&
1807 		    (r_ptr->flags1 & RF1_UNIQUE)) {
1808 			r_ptr->r_tkills = r_ptr->r_sights = 1;
1809 			s_printf("TKILL FIX: Player %s assisted for/killed unique %d\n", p_ptr->name, i);
1810 		}
1811 	}
1812 
1813         if (!older_than(4, 4, 24)) rd_u32b(&p_ptr->gold_picked_up);
1814         else strip_bytes(4);
1815 
1816         if (!older_than(4, 4, 24)) {
1817 		rd_byte(&tmp8u);
1818 		p_ptr->insta_res = tmp8u;
1819     	}
1820         else strip_bytes(1);
1821         if (!older_than(4, 4, 25)) rd_byte(&p_ptr->castles_owned);
1822         else strip_bytes(1);
1823 
1824 	if (!older_than(4, 5, 6)) rd_s16b(&p_ptr->flash_self);
1825 	else {
1826 		strip_bytes(2);
1827 		p_ptr->flash_self = -1; /* disabled by default */
1828 	}
1829 
1830 	if (!older_than(4, 5, 7)) {
1831 		rd_byte(&tmp8u);
1832 		p_ptr->fluent_artifact_reset = tmp8u;
1833 	} else {
1834 		strip_bytes(1);
1835 		p_ptr->fluent_artifact_reset = FALSE;
1836 	}
1837 
1838 	if (!older_than(4, 5, 14)) rd_byte(&p_ptr->sanity_bar);
1839 	else {
1840 		int skill = get_skill(p_ptr, SKILL_HEALTH);
1841 		strip_bytes(1);
1842 		if (skill >= 40) p_ptr->sanity_bar = 3;
1843 		else if (skill >= 20) p_ptr->sanity_bar = 2;
1844 		else if (skill >= 10) p_ptr->sanity_bar = 1;
1845 		else p_ptr->sanity_bar = 0;
1846 	}
1847 
1848 	if (!older_than(4, 5, 17)) {
1849 		rd_byte(&tmp8u);
1850 		p_ptr->IDDC_found_rndtown = tmp8u;
1851 	} else {
1852 		strip_bytes(1);
1853 		p_ptr->IDDC_found_rndtown = FALSE;
1854 	}
1855 
1856 	if (!older_than(4, 5, 18)) { //superfluous check! (as are the others above too, mostly..) We can just read the silyl byte since it's 0 anyway otherwise. --todo: cleanup
1857 		rd_byte(&tmp8u);
1858 		p_ptr->IDDC_logscum = (tmp8u != 0);
1859 	} else {
1860 		strip_bytes(1);
1861 		p_ptr->IDDC_logscum = FALSE;
1862 	}
1863 
1864 	/* hack: no save file version increment for this one */
1865 	rd_byte(&p_ptr->IDDC_flags);
1866 
1867 	if (!older_than(4, 5, 33)) {
1868 		rd_s16b(&p_ptr->word_recall);
1869 		rd_s16b(&p_ptr->recall_pos.wx);
1870 		rd_s16b(&p_ptr->recall_pos.wy);
1871 		rd_s16b(&p_ptr->recall_pos.wz);
1872 	} else {
1873 		strip_bytes(8);
1874 		p_ptr->recall_pos.wx = p_ptr->wpos.wx;
1875 		p_ptr->recall_pos.wy = p_ptr->wpos.wy;
1876 		p_ptr->recall_pos.wz = p_ptr->max_dlv;
1877 	}
1878 
1879 	/* Future use */
1880 	strip_bytes(22);
1881 
1882 	/* Toggle for possible automatic save-game updates
1883 	   (done via script login-hook, eg custom.lua) - C. Blue */
1884 	rd_byte(&p_ptr->updated_savegame);
1885 
1886 #if 0 /* ALT_EXPRATIO conversion: */
1887 if (p_ptr->updated_savegame == 0) {
1888     s64b i, i100, ief;
1889     i = (s64b)p_ptr->max_exp;
1890     i100 = (s64b)100;
1891     ief = (s64b)p_ptr->expfact;
1892     i = (i * i100) / ief;
1893     p_ptr->max_exp = (s32b)i;
1894     p_ptr->max_lev = 1;
1895     while ((p_ptr->max_lev < (is_admin(p_ptr) ? PY_MAX_LEVEL : PY_MAX_PLAYER_LEVEL)) &&
1896         (p_ptr->max_exp >= (s64b)player_exp[p_ptr->max_lev-1]))
1897     {
1898 	/* Gain a level */
1899 	p_ptr->max_lev++;
1900     }
1901     p_ptr->exp = p_ptr->max_exp;
1902     if (p_ptr->lev > p_ptr->max_lev) p_ptr->lev = p_ptr->max_lev;
1903     p_ptr->updated_savegame = 3;
1904 }
1905 #endif
1906 
1907 	/* for automatic artifact resets (similar to updated_savegame) */
1908 	if (!older_than(4, 3, 22)) rd_byte(&p_ptr->artifact_reset);
1909 
1910 	/* Skip the flags -- these are currently not written either */
1911 	strip_bytes(12);
1912 
1913 
1914 	/* Hack -- the two "special seeds" */
1915 	/*rd_u32b(&seed_flavor);
1916 	  rd_u32b(&seed_town);*/
1917         if (!older_than(4, 0, 5)) rd_s32b(&p_ptr->mimic_seed);
1918         if (!older_than(4, 4, 27)) {
1919         	rd_byte(&tmp8u);
1920 		p_ptr->mimic_immunity = tmp8u;
1921     	}
1922 
1923 	if (!older_than(4, 5, 4)) {
1924 		rd_u16b(&tmp16u);
1925 		p_ptr->autoret = tmp16u;
1926 	}
1927 	else p_ptr->autoret = 0;
1928 
1929 	if (!older_than(4, 0, 6)) rd_s16b(&p_ptr->martyr_timeout);
1930 	else p_ptr->martyr_timeout = 0;
1931 
1932 	/* Special stuff */
1933 	rd_u16b(&panic);
1934         if (panic) {
1935                 Players[Ind]->panic = TRUE;
1936                 s_printf("loaded a panic-saved player %s\n", Players[Ind]->name);
1937         }
1938 
1939 	rd_u16b(&p_ptr->total_winner);
1940 	if (!older_than(4, 3, 0)) rd_u16b(&p_ptr->once_winner);
1941 	if (!older_than(4, 4, 29)) {
1942 		rd_byte(&tmp8u);
1943 		if (tmp8u) p_ptr->iron_winner = TRUE;
1944 		rd_byte(&tmp8u);
1945 		if (tmp8u) p_ptr->iron_winner_ded = TRUE;
1946 	}
1947 
1948 	rd_s16b(&p_ptr->own1.wx);
1949 	rd_s16b(&p_ptr->own1.wy);
1950 	rd_s16b(&p_ptr->own1.wz);
1951 	rd_s16b(&p_ptr->own2.wx);
1952 	rd_s16b(&p_ptr->own2.wy);
1953 	rd_s16b(&p_ptr->own2.wz);
1954 
1955 	rd_u16b(&p_ptr->retire_timer);
1956 	rd_u16b(&p_ptr->noscore);
1957 
1958 	/* Read "death" */
1959 	rd_byte(&tmp8u);
1960 	p_ptr->death = tmp8u;
1961 
1962 	rd_byte((byte*)&p_ptr->black_breath);
1963 
1964 	rd_s16b(&p_ptr->msane);
1965 	rd_s16b(&p_ptr->csane);
1966 	rd_u16b(&p_ptr->csane_frac);
1967 
1968 	rd_s32b(&p_ptr->balance);
1969 	rd_s32b(&p_ptr->tim_blacklist);
1970 
1971 	if (!older_than(4, 2, 5)) rd_s32b(&p_ptr->tim_watchlist);
1972 	else p_ptr->tim_watchlist = 0;
1973 
1974 	if (!older_than(4, 2, 9)) rd_s32b(&p_ptr->pstealing);
1975 	else p_ptr->pstealing = 0;
1976 
1977         if (!older_than(4, 2, 8)) {
1978 		for (i = 0; i <	MAX_GLOBAL_EVENTS; i++) {
1979 			rd_s16b(&tmp16s);
1980 			p_ptr->global_event_type[i] = tmp16s;
1981 			if (!older_than(4, 3, 9)) {
1982 				rd_s32b(&tmp32s);
1983 				p_ptr->global_event_signup[i] = tmp32s;
1984 				rd_s32b(&tmp32s);
1985 				p_ptr->global_event_started[i] = tmp32s;
1986 			} else {
1987 				rd_u32b(&tmp32u);
1988 				p_ptr->global_event_signup[i] = tmp32u;
1989 				rd_u32b(&tmp32u);
1990 				p_ptr->global_event_started[i] = tmp32u;
1991 			}
1992 			for (j = 0; j < 4; j++) rd_u32b(&p_ptr->global_event_progress[i][j]);
1993 		}
1994 	}
1995 
1996 	if (!older_than(4, 5, 10))
1997 		for (i = 0; i < MAX_GLOBAL_EVENT_TYPES; i++) {
1998 			rd_s16b(&tmp16s);
1999 			p_ptr->global_event_participated[i] = tmp16s;
2000 		}
2001 	else
2002 		for (i = 0; i < MAX_GLOBAL_EVENT_TYPES; i++)
2003 			p_ptr->global_event_participated[i] = 0;
2004 
2005 	if (!older_than(4, 3, 3)) {
2006 		rd_s16b(&tmp16s);
2007 		p_ptr->combat_stance = tmp16s;
2008 		rd_s16b(&tmp16s);
2009 		p_ptr->combat_stance_power = tmp16s;
2010 	}
2011 	if (!older_than(4, 3, 4)) rd_byte(&p_ptr->cloaked);
2012 	if (!older_than(4, 3, 9)) rd_byte((byte *) &p_ptr->shadow_running);
2013 	if (!older_than(4, 3, 10)) rd_byte((byte *) &p_ptr->shoot_till_kill);
2014 	if (!older_than(4, 3, 23)) rd_byte((byte *) &p_ptr->dual_mode);
2015 
2016 	if (!older_than(4, 3, 11)) {
2017 		rd_s16b(&tmp16s);
2018 		p_ptr->kills = tmp16s;
2019 		if (!older_than(4, 5, 11)) {
2020 			rd_s16b(&tmp16s);
2021 			p_ptr->kills_own = tmp16s;
2022 		}
2023 		rd_s16b(&tmp16s);
2024 		p_ptr->kills_lower = tmp16s;
2025 		rd_s16b(&tmp16s);
2026 		p_ptr->kills_higher = tmp16s;
2027 		rd_s16b(&tmp16s);
2028 		p_ptr->kills_equal = tmp16s;
2029 	}
2030 	if (!older_than(4, 3, 12)) {
2031 		rd_s16b(&tmp16s);
2032 		p_ptr->free_mimic = tmp16s;
2033 	}
2034 
2035 	/* read obsolete values */
2036 	if (older_than(4, 4, 30) &&
2037 	    /* Runecraft */
2038 	    !older_than(4, 3, 26)) {
2039     		rd_s16b(&tmp16s);
2040 	    	rd_s16b(&tmp16s);
2041     		rd_s16b(&tmp16s);
2042     		rd_s16b(&tmp16s);
2043         	rd_s16b(&tmp16s);
2044 
2045 	    	rd_u16b(&tmp16u);
2046     		rd_u16b(&tmp16u);
2047     		rd_u16b(&tmp16u);
2048 	}
2049 
2050 	if (!older_than(4, 4, 1)) {
2051 		rd_byte(&tmp8u); /* aura states (on/off) */
2052 		if ((tmp8u & 0x1) && get_skill(p_ptr, SKILL_AURA_FEAR)) p_ptr->aura[0] = TRUE;
2053 		if ((tmp8u & 0x2) && get_skill(p_ptr, SKILL_AURA_SHIVER)) p_ptr->aura[1] = TRUE;
2054 		if ((tmp8u & 0x4) && get_skill(p_ptr, SKILL_AURA_DEATH)) p_ptr->aura[2] = TRUE;
2055 
2056 		rd_u16b(&p_ptr->deaths);
2057 		rd_u16b(&p_ptr->soft_deaths);
2058 
2059 		/* array of 'warnings' and hints aimed at newbies */
2060 		rd_u16b(&tmp16u);
2061 		if (tmp16u | 0x01) p_ptr->warning_technique_melee = 1;
2062 		if (tmp16u | 0x02) p_ptr->warning_technique_ranged = 1;
2063 	} else {
2064 		/* auto-enable for now (MAX_AURAS) */
2065 		if (get_skill(p_ptr, SKILL_AURA_FEAR)) p_ptr->aura[0] = TRUE;
2066 		if (get_skill(p_ptr, SKILL_AURA_SHIVER)) p_ptr->aura[1] = TRUE;
2067 		if (get_skill(p_ptr, SKILL_AURA_DEATH)) p_ptr->aura[2] = TRUE;
2068 	}
2069 
2070 	if (!older_than(4, 4, 2)) rd_string(p_ptr->info_msg, 80);
2071 	/* for now 'forget' info_msg on logout: */
2072 	strcpy(p_ptr->info_msg, "");
2073 
2074 	/* for ENABLE_GO_GAME */
2075 	if (!older_than(4, 4, 17)) {
2076 		rd_byte(&tmp8u);
2077 		p_ptr->go_level = tmp8u;
2078 		rd_byte(&tmp8u);
2079 		p_ptr->go_sublevel = tmp8u;
2080 	}
2081 
2082 	if (!older_than(4, 4, 19)) {
2083 		/* For things like 'Officer' status to add others etc */
2084 		rd_u32b(&p_ptr->party_flags);
2085 		rd_u32b(&p_ptr->guild_flags);
2086 	}
2087 
2088 	/* read obsolete values */
2089 	if (older_than(4, 4, 30) &&
2090 	    /* Read obselete Runecraft variables into dummy variables - Kurzel */
2091 	    !older_than(4, 4, 25)) {
2092 	        rd_s16b(&tmp16s);
2093 
2094 	        rd_byte(&tmp8u);
2095 	        rd_s16b(&tmp16s);
2096 	        rd_s16b(&tmp16s);
2097 	        rd_u16b(&tmp16u);
2098 
2099 	        rd_u16b(&tmp16u);
2100 	        rd_byte(&tmp8u);
2101 	        rd_byte(&tmp8u);
2102 	        rd_u32b(&tmp32u);
2103 
2104 	        rd_byte(&tmp8u);
2105 	        rd_u16b(&tmp16u);
2106 	        rd_u16b(&tmp16u);
2107 	        rd_byte(&tmp8u);
2108 
2109 	        rd_u16b(&tmp16u);
2110 	        rd_u16b(&tmp16u);
2111 	        rd_u16b(&tmp16u);
2112 	        rd_u16b(&tmp16u);
2113 	        rd_u16b(&tmp16u);
2114 	        rd_u16b(&tmp16u);
2115 	        rd_u16b(&tmp16u);
2116 
2117 #if 0
2118 	        rd_u16b(&tmp16u);
2119 #endif
2120 	        rd_u16b(&tmp16u);
2121 	        rd_u16b(&tmp16u);
2122 	        rd_u16b(&tmp16u);
2123 
2124 	        rd_byte(&tmp8u);
2125 	        rd_byte(&tmp8u);
2126 
2127 	        rd_u16b(&tmp16u);
2128 	        rd_u16b(&tmp16u);
2129 	        //rd_u16b(&tmp16u);
2130 	        //rd_u16b(&tmp16u);
2131 	        rd_u16b(&tmp16u);
2132 	        rd_u16b(&tmp16u);
2133 
2134 #if 1
2135 	        if (!older_than(4, 4, 26)) {
2136 	                rd_u16b(&tmp16u);
2137 	                rd_byte(&tmp8u);
2138 	                rd_u32b(&tmp32u);
2139 	                rd_u16b(&tmp16u);
2140 	                rd_byte(&tmp8u);
2141 	                rd_u32b(&tmp32u);
2142 	        }
2143 #endif
2144 
2145 	        rd_u16b(&tmp16u);
2146 	        rd_u16b(&tmp16u);
2147 
2148 #if 0
2149 	        rd_u16b(&tmp16u);
2150 	        rd_u16b(&tmp16u);
2151 	        rd_u16b(&tmp16u);
2152 	        rd_u16b(&tmp16u);
2153 	        rd_u16b(&tmp16u);
2154 #endif
2155 
2156 	        rd_u16b(&tmp16u);
2157 	        rd_byte(&tmp8u);
2158 	}
2159 
2160 	if (!older_than(4, 5, 3)) rd_u16b(&p_ptr->tim_deflect);
2161 	else p_ptr->tim_deflect = 0;
2162 
2163 	if (!older_than(4, 5, 28)) {
2164 		rd_u16b(&p_ptr->cards_diamonds);
2165 		rd_u16b(&p_ptr->cards_hearts);
2166 		rd_u16b(&p_ptr->cards_spades);
2167 		rd_u16b(&p_ptr->cards_clubs);
2168 	}
2169 
2170 	/* Success */
2171 	return FALSE;
2172 }
2173 
2174 
2175 
2176 
2177 /*
2178  * Read the player inventory
2179  *
2180  * Note that the inventory changed in Angband 2.7.4.  Two extra
2181  * pack slots were added and the equipment was rearranged.  Note
2182  * that these two features combine when parsing old save-files, in
2183  * which items from the old "aux" slot are "carried", perhaps into
2184  * one of the two new "inventory" slots.
2185  *
2186  * Note that the inventory is "re-sorted" later by "dungeon()".
2187  */
rd_inventory(int Ind)2188 static errr rd_inventory(int Ind)
2189 {
2190 	player_type *p_ptr = Players[Ind];
2191 
2192 	int slot = 0;
2193 
2194 	object_type forge;
2195 
2196 	/* No weight */
2197 	p_ptr->total_weight = 0;
2198 
2199 	/* No items */
2200 	p_ptr->inven_cnt = 0;
2201 	p_ptr->equip_cnt = 0;
2202 
2203 	/* Read until done */
2204 	while (TRUE) {
2205 		u16b n;
2206 
2207 		/* Get the next item index */
2208 		rd_u16b(&n);
2209 
2210 		/* Nope, we reached the end */
2211 		if (n == 0xFFFF) break;
2212 
2213 		/* Read the item */
2214 		rd_item(&forge);
2215 
2216 		/* Hack -- verify item */
2217 		if (!forge.k_idx) {
2218 			s_printf("Warning! Non-existing item detected (erased).\n");
2219 			continue;
2220 		}
2221 
2222 #ifdef FLUENT_ARTIFACT_RESETS
2223 		/* hack: If an artifact wasn't successfully erased when it should have been
2224 		   (happens if the save file was temporarily removed), fix it now. */
2225 		if (forge.name1 && forge.name1 != ART_RANDART && !multiple_artifact_p(&forge) &&
2226 		    (!a_info[forge.name1].cur_num || a_info[forge.name1].carrier != p_ptr->id)) {
2227 			s_printf("Warning! Already redistributed artifact %d detected (erased).\n", forge.name1);
2228 			continue;
2229 		}
2230 #endif
2231 
2232 #if 0
2233 		/* Mega-Hack -- Handle artifacts that aren't yet "created" */
2234 if (p_ptr->updated_savegame == 3) { // <- another megahack, see lua_arts_fix()
2235 		if (artifact_p(&forge))
2236 		{
2237 			/* If this artifact isn't created, mark it as created */
2238 			/* Only if this isn't a "death" restore */
2239 			if (!a_info[forge.name1].cur_num && !p_ptr->death)
2240 				handle_art_inum(forge.name1);
2241 			if (!a_info[forge.name1].known && (forge.ident & ID_KNOWN))
2242 				a_info[forge.name1].known = TRUE;
2243 		}
2244 }
2245 #endif
2246 
2247 		/* Wield equipment */
2248 		if (n >= INVEN_WIELD)
2249 		{
2250 			/* Structure copy */
2251 			p_ptr->inventory[n] = forge;
2252 
2253 			/* Add the weight */
2254 			p_ptr->total_weight += (forge.number * forge.weight);
2255 
2256 			/* One more item */
2257 			p_ptr->equip_cnt++;
2258 		}
2259 
2260 		/* Warning -- backpack is full */
2261 		else if (p_ptr->inven_cnt == INVEN_PACK)
2262 		{
2263 			s_printf("Too many items in the inventory!\n");
2264 
2265 			/* Fail */
2266 			return (54);
2267 		}
2268 
2269 		/* Carry inventory */
2270 		else
2271 		{
2272 			/* Get a slot */
2273 			n = slot++;
2274 
2275 			/* Structure copy */
2276 			p_ptr->inventory[n] = forge;
2277 
2278 			/* Add the weight */
2279 			p_ptr->total_weight += (forge.number * forge.weight);
2280 
2281 			/* One more item */
2282 			p_ptr->inven_cnt++;
2283 		}
2284 	}
2285 
2286 	/* Success */
2287 	return (0);
2288 }
2289 
2290 /*
2291  * Read hostility information
2292  *
2293  * Note that this function is responsible for deleting stale entries
2294  */
rd_hostilities(int Ind)2295 static errr rd_hostilities(int Ind)
2296 {
2297 	player_type *p_ptr = Players[Ind];
2298 	hostile_type *h_ptr;
2299 	int i;
2300 	s32b id;
2301 	u16b tmp16u;
2302 
2303 	/* Read number */
2304 	rd_u16b(&tmp16u);
2305 
2306 	/* Read available ID's */
2307 	for (i = 0; i < tmp16u; i++)
2308 	{
2309 		/* Read next ID */
2310 		rd_s32b(&id);
2311 
2312 		/* Check for stale player */
2313 		if (id > 0 && !lookup_player_name(id)) continue;
2314 
2315 		/* Check for stale party */
2316 		if (id < 0 && !parties[0 - id].members) continue;
2317 
2318 		/* Create node */
2319 		MAKE(h_ptr, hostile_type);
2320 		h_ptr->id = id;
2321 
2322 		/* Add to chain */
2323 		h_ptr->next = p_ptr->hostile;
2324 		p_ptr->hostile = h_ptr;
2325 	}
2326 
2327 	/* Success */
2328 	return (0);
2329 }
2330 
2331 
2332 
2333 /*
2334  * Read the run-length encoded dungeon
2335  *
2336  * Note that this only reads the dungeon features and flags,
2337  * the objects and monsters will be read later.
2338  *
2339  */
2340 
rd_floor(void)2341 static errr rd_floor(void)
2342 {
2343 	struct worldpos wpos;
2344 	s16b tmp16b;
2345 	byte tmp;
2346 	cave_type **zcave;
2347 	u16b max_y, max_x;
2348 
2349 	int i;
2350 	byte k, y, x;
2351 	cave_type *c_ptr;
2352 	dun_level *l_ptr;
2353 
2354 	unsigned char runlength, feature;
2355 	u16b flags;
2356 
2357 
2358 	/*** Depth info ***/
2359 
2360 	/* Level info */
2361 	rd_s16b(&wpos.wx);
2362 	rd_s16b(&wpos.wy);
2363 	rd_s16b(&wpos.wz);
2364 	if (wpos.wx == 0x7fff && wpos.wy == 0x7fff && wpos.wz == 0x7fff)
2365 		return(1);
2366 	rd_u16b(&max_y);
2367 	rd_u16b(&max_x);
2368 
2369 	/* Alloc before doing NPOD() */
2370 	alloc_dungeon_level(&wpos);
2371 	zcave = getcave(&wpos);
2372 	l_ptr = getfloor(&wpos);
2373 
2374 	/* players on this depth */
2375 	rd_s16b(&tmp16b);
2376 	new_players_on_depth(&wpos, tmp16b, FALSE);
2377 #if DEBUG_LEVEL > 1
2378 	s_printf("%d players on %s.\n", players_on_depth(&wpos), wpos_format(0, &wpos));
2379 #endif
2380 
2381 	rd_byte(&tmp);
2382 	new_level_up_y(&wpos, tmp);
2383 	rd_byte(&tmp);
2384 	new_level_up_x(&wpos, tmp);
2385 	rd_byte(&tmp);
2386 	new_level_down_y(&wpos, tmp);
2387 	rd_byte(&tmp);
2388 	new_level_down_x(&wpos, tmp);
2389 	rd_byte(&tmp);
2390 	new_level_rand_y(&wpos, tmp);
2391 	rd_byte(&tmp);
2392 	new_level_rand_x(&wpos, tmp);
2393 
2394 	if (l_ptr) {
2395 		time_t now;
2396 		now = time(&now);
2397 		l_ptr->lastused = now;
2398 
2399 		if (!s_older_than(4, 5, 18)) rd_u32b(&l_ptr->id);
2400 
2401 		rd_u32b(&l_ptr->flags1);
2402 		if (!s_older_than(4, 5, 18)) rd_u32b(&l_ptr->flags2);
2403 
2404 		rd_byte(&l_ptr->hgt);
2405 		rd_byte(&l_ptr->wid);
2406 	}
2407 
2408 	/*** Run length decoding ***/
2409 
2410 	/* where do all these features go, long time ago... ? */
2411 
2412 	/* Load the dungeon data */
2413 	for (x = y = 0; y < max_y; ) {
2414 		/* Grab RLE info */
2415 		rd_byte(&runlength);
2416 		rd_byte(&feature);
2417 		rd_u16b(&flags);
2418 
2419 		/* Apply the RLE info */
2420 		for (i = 0; i < runlength; i++) {
2421 			/* Access the cave */
2422 			c_ptr = &zcave[y][x];
2423 
2424 			/* set the feature */
2425 			c_ptr->feat = feature;
2426 
2427 #ifdef HOUSE_PAINTING
2428 			/* if we read a house door, restore house paint */
2429 			if (c_ptr->feat == FEAT_HOME || c_ptr->feat == FEAT_HOME_OPEN) {
2430 				int h_idx;
2431 				house_type *h_ptr;
2432 
2433 				h_idx = pick_house(&wpos, y, x);
2434 				if (h_idx != -1) {
2435 					h_ptr = &houses[h_idx];
2436 					/* if house is 'painted', colour adjacent walls accordingly */
2437 					if (h_ptr->colour) {
2438  #if 0 /* only effective for already allocated sectors; use /fix-house-modes instead */
2439 						/* hack: fix old houses before this colour hack was added for HOUSE_PAINTING_HIDE_BAD_MODE */
2440 						if ((h_ptr->dna->mode & MODE_EVERLASTING) && h_ptr->colour < 100)
2441 							h_ptr->colour += 100;
2442  #endif
2443 
2444 						cave_type *hwc_ptr;
2445 						int hwx, hwy;
2446 						for (hwx = x - 1; hwx <= x + 1; hwx++)
2447 						for (hwy = y - 1; hwy <= y + 1; hwy++) {
2448 							if (!in_bounds(hwy, hwx)) continue;
2449 
2450 							hwc_ptr = &zcave[hwy][hwx];
2451 							/* Only colour house wall grids */
2452 							if (hwc_ptr->feat == FEAT_WALL_HOUSE ||
2453 							    hwc_ptr->feat == FEAT_HOME ||
2454 							    hwc_ptr->feat == FEAT_HOME_OPEN)
2455 								hwc_ptr->colour = h_ptr->colour;
2456 						}
2457 					}
2458 				}
2459 			}
2460 #endif
2461 
2462 			/* set flags */
2463 			c_ptr->info = flags;
2464 
2465 			/* increment our position */
2466 			x++;
2467 			if (x >= max_x) {
2468 				/* Wrap onto a new row */
2469 				x = 0;
2470 				y++;
2471 			}
2472 		}
2473 	}
2474 
2475 	/*** another run for c_special ***/
2476 	{
2477 		struct c_special *cs_ptr;
2478 		byte n;
2479 		while (TRUE)
2480 		{
2481 			rd_byte(&x);
2482 			rd_byte(&y);
2483 			rd_byte(&n);	/* Number of c_special to add */
2484 
2485 			/* terminated? */
2486 			if (x == 255 && y == 255 && n == 255) break;
2487 
2488 			c_ptr = &zcave[y][x];
2489 			while(n--){
2490 				rd_byte(&k);
2491 				cs_ptr = ReplaceCS(c_ptr, k);
2492 				csfunc[k].load(cs_ptr);
2493 			}
2494 		}
2495 	}
2496 
2497 	/* fix possibly no longer correct lighting regarding time of the day */
2498 	if (wpos.wz == 0) {
2499 		/* hack this particular sector */
2500 		if (IS_DAY) world_surface_day(&wpos);
2501 		else world_surface_night(&wpos);
2502 	}
2503 
2504 #if 0 /* the glitch resulted from an ancient char being parked at that depth */
2505 	/* Maybe paranoia, but there was a glitch resulting in IDDC_logscum flag being set in -1k IDDC town */
2506 	if (in_irondeepdive(&wpos)) {
2507 		//int dun_lev = getlevel(wpos); -- not sure if dungeon positions have actually already been loaded at this point, so just read wz directly.
2508 		if (is_fixed_irondeepdive_town(&wpos, ABS(wpos.wz)) ||
2509 		    is_extra_fixed_irondeepdive_town(&wpos, ABS(wpos.wz))) {
2510 			struct dun_level *l_ptr = getfloor(&wpos);
2511 			l_ptr->flags1 |= LF1_DUNGEON_TOWN;
2512 		}
2513 	}
2514 #endif
2515 
2516 	/* Success */
2517 	return (0);
2518 }
2519 
2520 /* Reads in a players memory of the level he is currently on, in run-length encoded
2521  * format.  Simmilar to the above function.
2522  */
2523 
rd_cave_memory(int Ind)2524 static errr rd_cave_memory(int Ind)
2525 {
2526 	player_type *p_ptr = Players[Ind];
2527 	u16b max_y, max_x;
2528 	int i, y, x;
2529 
2530 	unsigned char runlength, cur_flag;
2531 
2532 	/* Remember unique ID of the old floor */
2533 	if (!older_than(4, 4, 16)) rd_u32b(&p_ptr->dlev_id);
2534 	else p_ptr->dlev_id = 0;
2535 
2536 	/* Memory dimensions */
2537 	rd_u16b(&max_y);
2538 	rd_u16b(&max_x);
2539 
2540 	/*** Run length decoding ***/
2541 
2542 	/* Load the memory data */
2543 	for (x = y = 0; y < max_y; ) {
2544 		/* Grab RLE info */
2545 		rd_byte(&runlength);
2546 		rd_byte(&cur_flag);
2547 
2548 		/* Apply the RLE info */
2549 		for (i = 0; i < runlength; i++) {
2550 			p_ptr->cave_flag[y][x] = cur_flag;
2551 
2552 			/* incrament our position */
2553 			x++;
2554 			if (x >= max_x) {
2555 				/* Wrap onto a new row */
2556 				x = 0;
2557 				y++;
2558 
2559 				/* paranoia */
2560 				/* if (y >= max_y) break; */
2561 			}
2562 		}
2563 	}
2564 
2565 	/* Success */
2566 	return (0);
2567 }
2568 
2569 /* Reads auction data. */
2570 
rd_auctions()2571 static void rd_auctions()
2572 {
2573 	int i, j;
2574 	u32b old_auction_alloc;
2575 	auction_type *auc_ptr;
2576 	bid_type *bid_ptr;
2577 	s32b tmp32s;
2578 
2579 	old_auction_alloc = auction_alloc;
2580 	if (!s_older_than(4, 4, 10)) rd_u32b(&auction_alloc);
2581 	else {
2582 		rd_s32b(&tmp32s);
2583 		auction_alloc = tmp32s;
2584 	}
2585 
2586 	GROW(auctions, old_auction_alloc, auction_alloc, auction_type);
2587 
2588 	for (i = 0; i < auction_alloc; i++)
2589 	{
2590 		auc_ptr = &auctions[i];
2591 		rd_byte(&auc_ptr->status);
2592 		rd_byte(&auc_ptr->flags);
2593 		rd_byte(&auc_ptr->mode);
2594 		rd_s32b(&auc_ptr->owner);
2595 		rd_item(&auc_ptr->item);
2596 		C_MAKE(auc_ptr->desc, 160, char);
2597 		rd_string(auc_ptr->desc, 160);
2598 		if (auc_ptr->desc[0] == '\0')
2599 		{
2600 			/* Free an empty string */
2601 			C_FREE(auc_ptr->desc, 160, char);
2602 		}
2603 		rd_s32b(&auc_ptr->starting_price);
2604 		rd_s32b(&auc_ptr->buyout_price);
2605 		rd_s32b(&auc_ptr->bids_cnt);
2606 		if (auc_ptr->bids_cnt)
2607 		{
2608 			C_MAKE(auc_ptr->bids, auc_ptr->bids_cnt, bid_type);
2609 		}
2610 		for (j = 0; j < auc_ptr->bids_cnt; j++)
2611 		{
2612 			bid_ptr = &auc_ptr->bids[j];
2613 			rd_s32b(&bid_ptr->bid);
2614 			rd_s32b(&bid_ptr->bidder);
2615 		}
2616 		rd_s32b(&auc_ptr->winning_bid);
2617 
2618 		/* The time_t's are saved as s32b's */
2619 		rd_s32b(&tmp32s);
2620 		auc_ptr->start = (time_t) tmp32s;
2621 		rd_s32b(&tmp32s);
2622 		auc_ptr->duration = (time_t) tmp32s;
2623 	}
2624 }
2625 
2626 /*
2627  * Actually read the savefile
2628  *
2629  * Angband 2.8.0 will completely replace this code, see "save.c",
2630  * though this code will be kept to read pre-2.8.0 savefiles.
2631  */
rd_savefile_new_aux(int Ind)2632 static errr rd_savefile_new_aux(int Ind) {
2633 	player_type *p_ptr = Players[Ind];
2634 	int i, j, err_code = 0;
2635 
2636 //	byte panic;
2637 	u16b tmp16u;
2638 	u32b tmp32u;
2639 	byte tmpbyte;
2640 
2641 
2642 #ifdef VERIFY_CHECKSUMS
2643 	u32b n_x_check, n_v_check;
2644 	u32b o_x_check, o_v_check;
2645 #endif
2646 
2647 	/* Strip the version bytes */
2648 	strip_bytes(4);
2649 
2650 	/* Hack -- decrypt */
2651 	xor_byte = sf_extra;
2652 
2653 
2654 	/* Clear the checksums */
2655 	v_check = 0L;
2656 	x_check = 0L;
2657 
2658 
2659 	/* Operating system info */
2660 	rd_u32b(&sf_xtra);
2661 
2662 	/* Time of savefile creation */
2663 	rd_u32b(&sf_when);
2664 
2665 	/* Number of resurrections */
2666 	rd_u16b(&sf_lives);
2667 
2668 	/* Number of times played */
2669 	rd_u16b(&sf_saves);
2670 
2671 
2672 	/* Later use (always zero) */
2673 	rd_u32b(&tmp32u);
2674 
2675 	/* Later use (always zero) */
2676 	rd_u32b(&tmp32u);
2677 
2678 	/* Object Memory */
2679 	rd_u16b(&tmp16u);
2680 
2681 	/* Incompatible save files */
2682 	if (tmp16u > MAX_K_IDX)
2683 	{
2684 		s_printf(format("Too many (%u) object kinds!\n", tmp16u));
2685 		return (22);
2686 	}
2687 
2688 	/* Read the object memory */
2689 	for (i = 0; i < tmp16u; i++)
2690 	{
2691 		byte tmp8u;
2692 
2693 		rd_byte(&tmp8u);
2694 
2695 		Players[Ind]->obj_aware[i] = (tmp8u & 0x01) ? TRUE : FALSE;
2696 		Players[Ind]->obj_tried[i] = (tmp8u & 0x02) ? TRUE : FALSE;
2697 		Players[Ind]->obj_felt[i] = (tmp8u & 0x04) ? TRUE : FALSE;
2698 		Players[Ind]->obj_felt_heavy[i] = (tmp8u & 0x08) ? TRUE : FALSE;
2699 	}
2700 
2701 	/* Trap Memory */
2702 	rd_u16b(&tmp16u);
2703 
2704 	/* Incompatible save files */
2705 	if (tmp16u > MAX_T_IDX)
2706 	{
2707 		s_printf(format("Too many (%u) trap kinds!\n", tmp16u));
2708 		return (22);
2709 	}
2710 
2711 	/* Read the object memory */
2712 	for (i = 0; i < tmp16u; i++)
2713 	{
2714 		byte tmp8u;
2715 
2716 		rd_byte(&tmp8u);
2717 
2718 		Players[Ind]->trap_ident[i] = (tmp8u & 0x01) ? TRUE : FALSE;
2719 	}
2720 
2721 	/* Read the extra stuff */
2722 	err_code = rd_extra(Ind);
2723 	if (err_code == 1)
2724 		return 1;
2725 	else if (err_code)
2726 		return 35;
2727 
2728 	/* Read the player_hp array */
2729 	rd_u16b(&tmp16u);
2730 
2731 	/* Read the player_hp array */
2732 	for (i = 0; i < tmp16u; i++)
2733 	{
2734 		rd_s16b(&p_ptr->player_hp[i]);
2735 	}
2736 
2737 
2738 	/* Important -- Initialize the race/class */
2739 	p_ptr->rp_ptr = &race_info[p_ptr->prace];
2740 	p_ptr->cp_ptr = &class_info[p_ptr->pclass];
2741 
2742 	/* Read the inventory */
2743 	if (rd_inventory(Ind))
2744 	{
2745 		s_printf("Unable to read inventory\n");
2746 		return (21);
2747 	}
2748 
2749 	/* Read hostility information if new enough */
2750 	if (rd_hostilities(Ind))
2751 	{
2752 		return (22);
2753 	}
2754 
2755 	/* read the dungeon memory if new enough */
2756 	rd_cave_memory(Ind);
2757 
2758 	/* read the wilderness map if new enough */
2759 	{
2760 		/* get the map size */
2761 		rd_u32b(&tmp32u);
2762 
2763 		/* if too many map entries */
2764 		if (tmp32u > MAX_WILD_X*MAX_WILD_Y)
2765 		{
2766 			return 23;
2767 		}
2768 
2769 		/* read in the map */
2770 		for (i = 0; i < (int) tmp32u; i++)
2771 		{
2772 			rd_byte(&p_ptr->wild_map[i]);
2773 		}
2774 	}
2775 
2776 	/* continued 'else' branch from rd_extra(), now that we have wild_map[] array */
2777         if (older_than(4, 4, 23) || p_ptr->dummy_option_8) {
2778 		/* in case we're here by a fix-hack */
2779 		if (p_ptr->dummy_option_8) {
2780 			s_printf("fixing max_depth[] for '%s'\n", p_ptr->name);
2781 			p_ptr->dummy_option_8 = FALSE;
2782 		}
2783 
2784 		/* hack - Sauron vs Shadow of Dol Guldur - just for consistency */
2785 		if (p_ptr->r_killed[RI_SAURON] == 1) p_ptr->r_killed[RI_DOL_GULDUR] = 1;
2786 
2787 		/* init his max_depth[] array */
2788 		fix_max_depth(p_ptr);
2789 	}
2790 
2791 	/* Keep Clean: Delete all max depth entries of dungeons that were removed meanwhile. */
2792 	excise_obsolete_max_depth(p_ptr);
2793 
2794 	/* read player guild membership */
2795 	rd_byte(&p_ptr->guild);
2796 	if (!older_than(4, 5, 8)) rd_u32b(&p_ptr->guild_dna);
2797 	else if (p_ptr->guild) p_ptr->guild_dna = guilds[p_ptr->guild].dna;
2798 	rd_u16b(&p_ptr->xorder_id);
2799         rd_s16b(&p_ptr->xorder_num);
2800 
2801 
2802 	/* read personal quest data */
2803 	if (!older_than(4, 5, 26)) {
2804 		rd_byte((byte *) &p_ptr->quest_any_k);
2805 		rd_byte((byte *) &p_ptr->quest_any_k_target);
2806 		rd_byte((byte *) &p_ptr->quest_any_k_within_target);
2807 		rd_byte((byte *) &p_ptr->quest_any_r);
2808 		rd_byte((byte *) &p_ptr->quest_any_r_target);
2809 		rd_byte((byte *) &p_ptr->quest_any_r_within_target);
2810 		rd_byte((byte *) &p_ptr->quest_any_deliver_xy);
2811 		rd_byte((byte *) &p_ptr->quest_any_deliver_xy_within_target);
2812 	}
2813 
2814 	if (!older_than(4, 5, 19)) {
2815 		for (i = 0; i < MAX_CONCURRENT_QUESTS; i++) {
2816 			rd_s16b(&p_ptr->quest_idx[i]);
2817 			rd_string(p_ptr->quest_codename[i], 10);
2818 			if (older_than(4, 5, 21)) {
2819 				byte tmpbyte;
2820 				rd_byte(&tmpbyte);
2821 				p_ptr->quest_stage[i] = tmpbyte;
2822 			} else rd_s16b(&p_ptr->quest_stage[i]);
2823 			if (!older_than(4, 5, 27)) rd_s16b(&p_ptr->quest_stage_timer[i]);
2824 
2825 			if (!older_than(4, 5, 23)) {
2826 				for (j = 0; j < QI_GOALS; j++) {
2827 					rd_byte((byte *) &p_ptr->quest_goals[i][j]);
2828 					rd_s16b(&p_ptr->quest_kill_number[i][j]);
2829 					rd_s16b(&p_ptr->quest_retrieve_number[i][j]);
2830 					/* hack: added in 4, 5, 26 actually: */
2831 					rd_byte((byte *) &p_ptr->quest_goals_nisi[i][j]);
2832 				}
2833 				/* the 'missing' byte to strip is instead used for 'nisi' above */
2834 		// FREE SPACE:
2835 				strip_bytes(5 * (2 + 2));//5 = QI_OPTIONAL
2836 			} else {
2837 				for (j = 0; j < QI_GOALS; j++)
2838 					rd_byte((byte *) &p_ptr->quest_goals[i][j]);
2839 				strip_bytes(5);
2840 			}
2841 
2842 			if (!older_than(4, 5, 26)) {
2843 				rd_byte((byte *) &p_ptr->quest_kill[i]);
2844 				rd_byte((byte *) &p_ptr->quest_retrieve[i]);
2845 
2846 				rd_byte((byte *) &p_ptr->quest_deliver_pos[i]);
2847 				rd_byte((byte *) &p_ptr->quest_deliver_xy[i]);
2848 
2849 				rd_s32b(&p_ptr->quest_acquired[i]);
2850 				rd_s32b(&p_ptr->quest_timed_stage_change[i]);
2851 			} else {
2852 				strip_bytes(3);
2853 				rd_byte((byte *) &p_ptr->quest_deliver_pos[i]);
2854 				strip_bytes(1);
2855 				rd_byte((byte *) &p_ptr->quest_deliver_xy[i]);
2856 				strip_bytes(6);
2857 			}
2858 
2859 			if (!older_than(4, 5, 25))
2860 				rd_u16b(&p_ptr->quest_flags[i]);
2861 			else if (!older_than(4, 5, 21)) {
2862 				for (j = 0; j < QI_FLAGS; j++) {
2863 					rd_byte(&tmpbyte);
2864 					if (tmpbyte) p_ptr->quest_flags[i] |= (0x1 << j);
2865 				}
2866 			}
2867 		}
2868 
2869 		/* remember quests we completed */
2870 		for (i = 0; i < (older_than(4, 5, 29) ? 50 : MAX_Q_IDX); i++) {
2871 			rd_s16b(&p_ptr->quest_done[i]);
2872 			if (!older_than(4, 5, 22)) rd_s16b(&p_ptr->quest_cooldown[i]);
2873 		}
2874 	} else
2875 		for (i = 0; i < MAX_CONCURRENT_QUESTS; i++) {
2876 			p_ptr->quest_idx[i] = -1;
2877 			p_ptr->quest_codename[i][0] = 0;
2878 		}
2879 
2880 
2881         if (!older_than(4, 0, 1)) {
2882                 rd_byte(&p_ptr->spell_project);
2883         } else {
2884                 p_ptr->spell_project = 0;
2885         }
2886 
2887         /* Special powers */
2888         if (!older_than(4, 0, 2)) {
2889                 rd_s16b(&p_ptr->power_num);
2890 
2891                 for (i = 0; i < MAX_POWERS; i++) {
2892                         rd_s16b(&p_ptr->powers[i]);
2893                 }
2894         } else {
2895                 p_ptr->power_num = 0;
2896         }
2897 
2898 #ifdef VERIFY_CHECKSUMS
2899 
2900 	/* Save the checksum */
2901 	n_v_check = v_check;
2902 
2903 	/* Read the old checksum */
2904 	rd_u32b(&o_v_check);
2905 
2906 	/* Verify */
2907 	if (o_v_check != n_v_check)
2908 	{
2909 		s_printf("Invalid checksum\n");
2910 		return (11);
2911 	}
2912 
2913 
2914 	/* Save the encoded checksum */
2915 	n_x_check = x_check;
2916 
2917 	/* Read the checksum */
2918 	rd_u32b(&o_x_check);
2919 
2920 
2921 	/* Verify */
2922 	if (o_x_check != n_x_check)
2923 	{
2924 		s_printf("Invalid encoded checksum\n");
2925 		return (11);
2926 	}
2927 
2928 #endif
2929 
2930 
2931 	/* Hack -- no ghosts */
2932 	r_info[MAX_R_IDX-1].max_num = 0;
2933 
2934 	/* Initialize a little more */
2935 	p_ptr->ignore = NULL;
2936 	p_ptr->w_ignore = NULL;
2937 	p_ptr->afk = FALSE;
2938 
2939 	/* Success */
2940 	return (0);
2941 }
2942 
2943 
2944 /*
2945  * Actually read the savefile
2946  *
2947  * Angband 2.8.0 will completely replace this code, see "save.c",
2948  * though this code will be kept to read pre-2.8.0 savefiles.
2949  */
rd_savefile_new(int Ind)2950 errr rd_savefile_new(int Ind) {
2951 	player_type *p_ptr = Players[Ind];
2952 
2953 	errr err;
2954 
2955 	/* The savefile is a binary file */
2956 	fff = my_fopen(p_ptr->savefile, "rb");
2957 
2958 	/* Paranoia */
2959 	if (!fff) return (-1);
2960 
2961 	/* Allocate a new buffer */
2962 	fff_buf = C_NEW(MAX_BUF_SIZE, char);
2963 	fff_buf_pos = MAX_BUF_SIZE;
2964 
2965 	/* Call the sub-function */
2966 	err = rd_savefile_new_aux(Ind);
2967 
2968 	/* Free the buffer */
2969 	C_FREE(fff_buf, MAX_BUF_SIZE, char);
2970 
2971 	/* Check for errors */
2972 	if (ferror(fff)) err = -1;
2973 
2974 	/* Close the file */
2975 	my_fclose(fff);
2976 
2977 	/* Result */
2978 	return (err);
2979 }
2980 
2981 /* Added this to load2.c too, for load_quests() - C. Blue */
file_exist(char * buf)2982 static bool file_exist(char *buf) {
2983 	int fd;
2984 
2985 	fd = fd_open(buf, O_RDONLY);
2986 	if (fd >= 0) {
2987 		fd_close(fd);
2988 		return (TRUE);
2989 	}
2990 	else return (FALSE);
2991 }
2992 
rd_server_savefile()2993 errr rd_server_savefile() {
2994 	unsigned int i;
2995 
2996 	errr err = 0;
2997 
2998 	char savefile[MAX_PATH_LENGTH];
2999 
3000 	byte tmp8u, panic = 0;
3001 	u16b tmp16u;
3002 	s16b tmp16s;
3003 	u32b tmp32u;
3004 	s32b tmp32s;
3005 
3006 	/* Savefile name */
3007 	path_build(savefile, MAX_PATH_LENGTH, ANGBAND_DIR_SAVE, "server");
3008 
3009 	/* The server savefile is a binary file */
3010 	fff = my_fopen(savefile, "rb");
3011 
3012 	/* Paranoia */
3013 	if (!fff) return (-1);
3014 
3015 	/* Allocate a new buffer */
3016 	fff_buf = C_NEW(MAX_BUF_SIZE, char);
3017 	fff_buf_pos = MAX_BUF_SIZE;
3018 
3019 	/* Read the version */
3020 	xor_byte = 0;
3021 	rd_byte(&ssf_major);
3022 	xor_byte = 0;
3023 	rd_byte(&ssf_minor);
3024 	xor_byte = 0;
3025 	rd_byte(&ssf_patch);
3026 	xor_byte = 0;
3027 	rd_byte(&ssf_extra);
3028 
3029 	/* Hack -- decrypt */
3030 	xor_byte = sf_extra;
3031 
3032 
3033 	/* Clear the checksums */
3034 	v_check = 0L;
3035 	x_check = 0L;
3036 
3037 
3038 	/* Operating system info */
3039 	rd_u32b(&sf_xtra);
3040 
3041 	/* Time of savefile creation */
3042 	rd_u32b(&sf_when);
3043 
3044 	/* Number of lives */
3045 	rd_u16b(&sf_lives);
3046 
3047 	/* Number of times played */
3048 	rd_u16b(&sf_saves);
3049 
3050 	/* Was the last shutdown a panic save? - C. Blue */
3051 	if (!s_older_than(4, 2, 2)) rd_byte(&panic); /* no further use yet */
3052 	if (panic) s_printf("Last server shutdown was a panic save.\n");
3053 
3054 	/* read server state regarding any updates (for lua) - C. Blue */
3055 	if (!s_older_than(4, 3, 19)) {
3056 		rd_s16b(&tmp16s);
3057 		updated_server = tmp16s;
3058 	}
3059 	/* read server state regarding artifact updates */
3060 	if (!s_older_than(4, 3, 22)) {
3061 		rd_s16b(&tmp16s);
3062 		artifact_reset = tmp16s;
3063 	}
3064 
3065 	/* Later use (always zero) */
3066 	rd_u32b(&tmp32u);
3067 
3068 	/* Later use (always zero) */
3069 	rd_u32b(&tmp32u);
3070 
3071 	/* Monster Memory */
3072 	rd_u16b(&tmp16u);
3073 
3074 	/* Incompatible save files */
3075 	if (tmp16u > MAX_R_IDX) {
3076 		s_printf(format("Too many (%u) monster races!\n", tmp16u));
3077 		return (21);
3078 	}
3079 
3080 	/* Read the available records */
3081 	for (i = 0; i < tmp16u; i++) {
3082 		/* Read the monster race information */
3083 		rd_global_lore(i);
3084 	}
3085 
3086 	/* Load the Artifacts */
3087 	rd_u16b(&tmp16u);
3088 
3089 	/* Incompatible save files */
3090 	if (tmp16u > MAX_A_IDX) {
3091 		s_printf(format("Too many (%u) artifacts!\n", tmp16u));
3092 		return (24);
3093 	}
3094 	/* Read the artifact flags */
3095 	for (i = 0; i < tmp16u; i++) {
3096 		rd_byte(&tmp8u);
3097 		a_info[i].cur_num = tmp8u;
3098 		rd_byte(&tmp8u);
3099 		a_info[i].known = tmp8u;
3100 		if (s_older_than(4, 5, 5)) {
3101 			rd_byte(&tmp8u);
3102 			rd_byte(&tmp8u);
3103 			a_info[i].carrier = 0;
3104 		} else rd_s32b(&a_info[i].carrier);
3105 		if (s_older_than(4, 5, 6)) {
3106 			determine_artifact_timeout(i, NULL);
3107 			a_info[i].iddc = FALSE;
3108 			a_info[i].winner = FALSE;
3109 		} else {
3110 			rd_s32b(&a_info[i].timeout);
3111 #ifdef FLUENT_ARTIFACT_RESETS
3112 			/* fluent-artifact-timeout was temporarily disabled? reset timer now then */
3113 			if (a_info[i].timeout == -2) determine_artifact_timeout(i, NULL);
3114 #else
3115 			a_info[i].timeout = -2;
3116 #endif
3117 			if (!s_older_than(4, 5, 31)) {
3118 				rd_byte(&tmp8u);
3119 				a_info[i].iddc = (tmp8u & 0x1);
3120 				a_info[i].winner = (tmp8u & 0x2);
3121 			} else a_info[i].iddc = a_info[i].winner = FALSE;
3122 		}
3123 	}
3124 
3125 	rd_u16b(&tmp16u);
3126 
3127 	/* Incompatible save files */
3128 	if (tmp16u > MAX_PARTIES) {
3129 		s_printf(format("Too many (%u) parties!\n", tmp16u));
3130 		return (25);
3131 	}
3132 
3133 	/* Read the available records */
3134 	for (i = 0; i < tmp16u; i++) {
3135 		rd_party(i);
3136 	}
3137 	if (s_older_than(4, 2, 4)) {
3138 		for (i = tmp16u; i < MAX_PARTIES; i++) {
3139 			/* HACK Initialize new parties just to make sure they'll work - mikaelh */
3140 			parties[i].members = 0;
3141 			parties[i].created = 0;
3142 		}
3143 	}
3144 
3145 	/* XXX If new enough, read in the saved levels and monsters. */
3146 
3147 	/* read the number of levels to be loaded */
3148 	rd_towns();
3149 	new_rd_wild();
3150 	new_rd_floors();
3151 
3152 	/* get the number of monsters to be loaded */
3153 	rd_u32b(&tmp32u);
3154 	if (tmp32u > MAX_M_IDX) {
3155 		s_printf(format("Too many (%u) monsters!\n", tmp16u));
3156 		return (29);
3157 	}
3158 	/* load the monsters */
3159 	for (i = 0; i < tmp32u; i++) rd_monster(&m_list[m_pop()]);
3160 
3161 	/* Read object info if new enough */
3162 	rd_u16b(&tmp16u);
3163 
3164 	/* Incompatible save files */
3165 	if (tmp16u > MAX_O_IDX) {
3166 		s_printf(format("Too many (%u) objects!\n", tmp16u));
3167 		return (26);
3168 	}
3169 
3170 	/* Read the available records */
3171 	for (i = 0; i < tmp16u; i++) rd_item(&o_list[i]);
3172 
3173 	/* Set the maximum object number */
3174 	o_max = tmp16u;
3175 
3176 	/* Read house info if new enough */
3177 	rd_s32b(&num_houses);
3178 
3179 	while (house_alloc <= num_houses) {
3180 		GROW(houses, house_alloc, house_alloc + 512, house_type);
3181 		house_alloc += 512;
3182 	}
3183 
3184 	/* Incompatible save files */
3185 	if (num_houses > MAX_HOUSES) {
3186 		s_printf(format("Too many (%u) houses!\n", num_houses));
3187 		return (27);
3188 	}
3189 
3190 	/* Read the available records */
3191 	for (i = 0; i < (unsigned int) num_houses; i++) {
3192 		rd_house(i);
3193 		if(!(houses[i].flags&HF_STOCK))
3194 			wild_add_uhouse(&houses[i]);
3195 	}
3196 
3197 	/* Read the player name database if new enough */
3198 	{
3199 		char name[80];
3200 		byte level, old_party, guild, race, class, mode;
3201 		u32b acct, guild_flags;
3202 		u16b party;
3203 		u16b xorder;
3204 		byte admin;
3205 		struct worldpos wpos;
3206 
3207 		rd_u32b(&tmp32u);
3208 
3209 		/* Read the available records */
3210 		for (i = 0; i < tmp32u; i++) {
3211 			s32b laston;
3212 
3213 			/* Read the ID */
3214 			rd_s32b(&tmp32s);
3215 			rd_u32b(&acct);
3216 			rd_s32b(&laston);
3217 			rd_byte(&race);
3218 #ifdef ENABLE_KOBOLD /* Kobold was inserted in the middle, instead of added to the end of the races array */
3219 			if (s_older_than(4, 4, 28) && race >= RACE_KOBOLD) race++;
3220 #endif
3221 			rd_byte(&class);
3222 			if (!s_older_than(4, 2, 2)) rd_byte(&mode); else mode = 0;
3223 			rd_byte(&level);
3224 			if (s_older_than(4, 2, 4)) {
3225 				rd_byte(&old_party);
3226 				party = old_party; /* convert old byte to u16b - mikaelh */
3227 			}
3228 			else rd_u16b(&party);
3229 			rd_byte(&guild);
3230 			if (!s_older_than(4, 5, 1)) rd_u32b(&guild_flags); else guild_flags = 0;
3231 			rd_u16b(&xorder);
3232 			if (!s_older_than(4, 5, 13)) rd_byte(&admin); else admin = 0;
3233 			if (!s_older_than(4, 5, 30)) {
3234 				rd_s16b(&wpos.wx);
3235 				rd_s16b(&wpos.wy);
3236 				rd_s16b(&wpos.wz);
3237 			} else {
3238 				wpos.wx = 0;
3239 				wpos.wy = 0;
3240 				wpos.wz = 0;
3241 			}
3242 
3243 			/* Read the player name */
3244 			rd_string(name, 80);
3245 
3246 			/* Store the player name */
3247 			add_player_name(name, tmp32s, acct, race, class, mode, level, party, guild, guild_flags, xorder, laston, admin, wpos);
3248 		}
3249 		s_printf("Read %d player name records.\n", tmp32u);
3250 	}
3251 
3252 	rd_u32b(&seed_flavor);
3253 	rd_u32b(&seed_town);
3254 
3255 	rd_guilds();
3256 
3257 	rd_xorders();
3258 	if (!s_older_than(4, 5, 19)) {
3259 		if (s_older_than(4, 5, 26)) rd_quests(); /* moved quest data to a separate save file! */
3260 
3261 		/* still, always fix questors of course!
3262 		   (could be done at a completely different place in the server code though) */
3263 		else fix_questors_on_startup();
3264 	}
3265 
3266 	rd_u32b(&account_id);
3267 	rd_s32b(&player_id);
3268 
3269 	rd_s32b(&turn);
3270 
3271 	if (!s_older_than(4, 3, 18)) rd_notes();
3272 	if (!s_older_than(4, 3, 6)) rd_bbs();
3273 
3274 	if (!s_older_than(4, 3, 13)) rd_byte(&season);
3275 	if (!s_older_than(4, 3, 14)) rd_byte(&weather_frequency);
3276 
3277 	if (!s_older_than(4, 3, 25)) rd_auctions();
3278 
3279 	/* read Ironman Deep Dive Challenge records */
3280 	if (!s_older_than(4, 5, 16)) {
3281 		char dummy[MAX_CHARS_WIDE];
3282 
3283 		rd_byte(&tmp8u);
3284 		for (i = 0; i < tmp8u; i++) {
3285 			rd_s16b(&tmp16s);
3286 			if (i >= IDDC_HIGHSCORE_SIZE) {
3287 				rd_string(dummy, MAX_CHARS_WIDE);
3288 				rd_string(dummy, MAX_CHARS_WIDE);
3289 				rd_string(dummy, MAX_CHARS_WIDE);
3290 				rd_s16b(&tmp16s);
3291 				continue;
3292 			}
3293 			deep_dive_level[i] = tmp16s;
3294 			rd_string(deep_dive_name[i], MAX_CHARS);
3295 			rd_string(deep_dive_char[i], MAX_CHARS);
3296 			rd_string(deep_dive_account[i], MAX_CHARS);
3297 			rd_s16b(&tmp16s);
3298 			deep_dive_class[i] = tmp16s;
3299 		}
3300 		for (i = tmp8u; i < IDDC_HIGHSCORE_SIZE; i++) {
3301 			deep_dive_level[i] = 0;
3302 			deep_dive_name[i][0] = 0;
3303 			deep_dive_char[i][0] = 0;
3304 			deep_dive_account[i][0] = 0;
3305 			deep_dive_class[i] = 0;
3306 		}
3307 	} else if (!s_older_than(4, 5, 15)) {
3308 		char dummy[MAX_CHARS_WIDE];
3309 
3310 		rd_byte(&tmp8u);
3311 		for (i = 0; i < tmp8u; i++) {
3312 			rd_s16b(&tmp16s);
3313 			if (i >= IDDC_HIGHSCORE_SIZE) {
3314 				rd_string(dummy, MAX_CHARS_WIDE);
3315 				continue;
3316 			}
3317 			deep_dive_level[i] = tmp16s;
3318 			rd_string(deep_dive_name[i], MAX_CHARS);
3319 		}
3320 		for (i = tmp8u; i < IDDC_HIGHSCORE_SIZE; i++) {
3321 			deep_dive_level[i] = 0;
3322 			deep_dive_name[i][0] = 0;
3323 		}
3324 	} else if (!s_older_than(4, 4, 18)) {
3325 	        for (i = 0; i < 20; i++) {
3326 			rd_s16b(&tmp16s);
3327 			deep_dive_level[i] = tmp16s;
3328 			rd_string(deep_dive_name[i], MAX_CHARS);
3329 		}
3330 	}
3331 
3332 	/* Hack -- no ghosts */
3333 	r_info[MAX_R_IDX-1].max_num = 0;
3334 
3335 	/* Free the buffer */
3336 	C_FREE(fff_buf, MAX_BUF_SIZE, char);
3337 
3338 	/* Check for errors */
3339 	if (ferror(fff)) err = -1;
3340 
3341 	/* Close the file */
3342 	my_fclose(fff);
3343 
3344 	/* Result */
3345 	return (err);
3346 }
3347 
new_rd_wild()3348 void new_rd_wild()
3349 {
3350 	int x, y, i;
3351 #ifdef DUNGEON_VISIT_BONUS
3352 	int dungeons = 0;
3353 #endif
3354 	wilderness_type *wptr;
3355 	struct dungeon_type *d_ptr;
3356 	u32b tmp;
3357 	rd_u32b(&tmp);	/* MAX_WILD_Y */
3358 	rd_u32b(&tmp);	/* MAX_WILD_X */
3359 	for (y = 0; y < MAX_WILD_Y; y++) {
3360 		for (x = 0; x < MAX_WILD_X; x++) {
3361 			wptr = &wild_info[y][x];
3362 			rd_wild(wptr);
3363 			if (wptr->flags & WILD_F_DOWN) {
3364 				MAKE(d_ptr, struct dungeon_type);
3365 				rd_byte(&wptr->up_x);
3366 				rd_byte(&wptr->up_y);
3367 				rd_u16b(&d_ptr->id);
3368 				rd_u16b(&d_ptr->type);
3369 				rd_u16b(&d_ptr->baselevel);
3370 				rd_u32b(&d_ptr->flags1);
3371 				rd_u32b(&d_ptr->flags2);
3372 				if (!s_older_than(4, 4, 24)) rd_u32b(&d_ptr->flags3);
3373 				rd_byte(&d_ptr->maxdepth);
3374 
3375 #ifdef DUNGEON_VISIT_BONUS
3376 				dungeons++;
3377 				if (d_ptr->id > dungeon_id_max) dungeon_id_max = d_ptr->id;
3378 				if (d_ptr->id != 0) {
3379 					dungeon_x[d_ptr->id] = x;
3380 					dungeon_y[d_ptr->id] = y;
3381 					dungeon_tower[d_ptr->id] = FALSE;
3382 				}
3383 #endif
3384 				if (d_ptr->type == DI_NETHER_REALM) {
3385 					netherrealm_wpos_x = x;
3386 					netherrealm_wpos_y = y;
3387 					netherrealm_wpos_z = -1;
3388 					netherrealm_start = d_ptr->baselevel;
3389 					netherrealm_end = d_ptr->baselevel + d_ptr->maxdepth - 1;
3390 					netherrealm_end_wz = -d_ptr->maxdepth;
3391 				}
3392 				else if (d_ptr->type == DI_VALINOR) {
3393 					valinor_wpos_x = x;
3394 					valinor_wpos_y = y;
3395 					valinor_wpos_z = -1;
3396 				}
3397 				else if (d_ptr->type == DI_HALLS_OF_MANDOS) {
3398 					hallsofmandos_wpos_x = x;
3399 					hallsofmandos_wpos_y = y;
3400 					hallsofmandos_wpos_z = -1;
3401 				}
3402 
3403 #if 0
3404 				for (i = 0; i < 10; i++) {
3405 					rd_byte((byte*)&d_ptr->r_char[i]);
3406 					rd_byte((byte*)&d_ptr->nr_char[i]);
3407 				}
3408 #else
3409 				strip_bytes(20);
3410 #endif
3411 
3412 				if (!s_older_than(4, 4, 21)) {
3413 #ifdef DUNGEON_VISIT_BONUS
3414 					rd_u16b(&dungeon_visit_frequency[d_ptr->id]);
3415 					set_dungeon_bonus(d_ptr->id, FALSE);
3416 #else
3417 					strip_bytes(2);
3418 #endif
3419 				}
3420 
3421 				if (!s_older_than(4, 5, 27)) {
3422 					rd_byte(&d_ptr->theme);
3423 					rd_s16b(&d_ptr->quest);
3424 					rd_s16b(&d_ptr->quest_stage);
3425 				}
3426 
3427 				C_MAKE(d_ptr->level, d_ptr->maxdepth, struct dun_level);
3428 				for (i = 0; i < d_ptr->maxdepth; i++) {
3429 					C_MAKE(d_ptr->level[i].uniques_killed, MAX_R_IDX, char);
3430 				}
3431 				wptr->dungeon = d_ptr;
3432 			}
3433 			if (wptr->flags & WILD_F_UP) {
3434 				MAKE(d_ptr, struct dungeon_type);
3435 				rd_byte(&wptr->dn_x);
3436 				rd_byte(&wptr->dn_y);
3437 				rd_u16b(&d_ptr->id);
3438 				rd_u16b(&d_ptr->type);
3439 				rd_u16b(&d_ptr->baselevel);
3440 				rd_u32b(&d_ptr->flags1);
3441 				rd_u32b(&d_ptr->flags2);
3442 				if (!s_older_than(4, 4, 24)) rd_u32b(&d_ptr->flags3);
3443 				rd_byte(&d_ptr->maxdepth);
3444 
3445 #ifdef DUNGEON_VISIT_BONUS
3446 				dungeons++;
3447 				if (d_ptr->id > dungeon_id_max) dungeon_id_max = d_ptr->id;
3448 				if (d_ptr->id != 0) {
3449 					dungeon_x[d_ptr->id] = x;
3450 					dungeon_y[d_ptr->id] = y;
3451 					dungeon_tower[d_ptr->id] = TRUE;
3452 				}
3453 #endif
3454 				if (d_ptr->type == DI_NETHER_REALM) {
3455 					netherrealm_wpos_x = x;
3456 					netherrealm_wpos_y = y;
3457 					netherrealm_wpos_z = 1;
3458 					netherrealm_start = d_ptr->baselevel;
3459 					netherrealm_end = d_ptr->baselevel + d_ptr->maxdepth - 1;
3460 					netherrealm_end_wz = d_ptr->maxdepth;
3461 				}
3462 				else if (d_ptr->type == DI_VALINOR) {
3463 					valinor_wpos_x = x;
3464 					valinor_wpos_y = y;
3465 					valinor_wpos_z = 1;
3466 				}
3467 				else if (d_ptr->type == DI_HALLS_OF_MANDOS) {
3468 					hallsofmandos_wpos_x = x;
3469 					hallsofmandos_wpos_y = y;
3470 					hallsofmandos_wpos_z = 1;
3471 				}
3472 
3473 #if 0
3474 				for (i = 0; i < 10; i++) {
3475 					rd_byte((byte*)&d_ptr->r_char[i]);
3476 					rd_byte((byte*)&d_ptr->nr_char[i]);
3477 				}
3478 #else
3479 				strip_bytes(20);
3480 #endif
3481 
3482 				if (!s_older_than(4, 4, 21)) {
3483 #ifdef DUNGEON_VISIT_BONUS
3484 					rd_u16b(&dungeon_visit_frequency[d_ptr->id]);
3485 					set_dungeon_bonus(d_ptr->id, FALSE);
3486 #else
3487 					strip_bytes(2);
3488 #endif
3489 				}
3490 
3491 				if (!s_older_than(4, 5, 27)) {
3492 					rd_byte(&d_ptr->theme);
3493 					rd_s16b(&d_ptr->quest);
3494 					rd_s16b(&d_ptr->quest_stage);
3495 				}
3496 
3497 				C_MAKE(d_ptr->level, d_ptr->maxdepth, struct dun_level);
3498 				for (i = 0; i < d_ptr->maxdepth; i++) {
3499 					C_MAKE(d_ptr->level[i].uniques_killed, MAX_R_IDX, char);
3500 				}
3501 				wptr->tower = d_ptr;
3502 			}
3503 		}
3504 	}
3505 #ifdef DUNGEON_VISIT_BONUS
3506 	if (dungeons != dungeon_id_max) {
3507 		s_printf("Mismatch: Found %d dungeons and %d dungeon ids -> reindexing:\n", dungeons, dungeon_id_max);
3508 		reindex_dungeons();
3509 	}
3510 #endif
3511 }
3512 
new_rd_floors()3513 void new_rd_floors()
3514 {
3515 	while(!rd_floor());
3516 }
3517 
rd_towns()3518 void rd_towns() {
3519 	int i, j;
3520 
3521 	/* Dealloc stores first - mikaelh */
3522 	for (i = 0; i < numtowns; i++) dealloc_stores(i);
3523 	C_KILL(town, numtowns, struct town_type); /* first is alloced */
3524 
3525 	rd_u16b(&numtowns);
3526 	C_MAKE(town, numtowns, struct town_type);
3527 	for (i = 0; i < numtowns; i++) {
3528 		rd_u16b(&town[i].x);
3529 		rd_u16b(&town[i].y);
3530 		rd_u16b(&town[i].baselevel);
3531 		rd_u16b(&town[i].flags);
3532 		rd_u16b(&town[i].num_stores);
3533 		rd_u16b(&town[i].type);
3534 		wild_info[town[i].y][town[i].x].type = WILD_TOWN;
3535 		wild_info[town[i].y][town[i].x].radius = town[i].baselevel;
3536 		wild_info[town[i].y][town[i].x].town_idx = i;
3537 		alloc_stores(i);
3538 		for (j = 0; j < town[i].num_stores; j++)
3539 			rd_store(&town[i].townstore[j]);
3540 	}
3541 }
3542 
load_guildhalls(struct worldpos * wpos)3543 void load_guildhalls(struct worldpos *wpos){
3544 	int i;
3545 	FILE *gfp;
3546 	struct guildsave data;
3547 	char buf[1024];
3548 	char fname[30];
3549 	data.mode = 0;
3550 	for (i = 0; i < num_houses; i++) {
3551 		if ((houses[i].dna->owner_type == OT_GUILD) && (inarea(wpos, &houses[i].wpos))) {
3552 			if (!houses[i].dna->owner) continue;
3553 #if DEBUG_LEVEL > 2
3554 			s_printf("load guildhall %d\n", i);
3555 #endif
3556 			sprintf(fname, "guild%.4d.data", i);
3557 //			path_build(buf, 1024, ANGBAND_DIR_DATA, fname);
3558 			path_build(buf, 1024, ANGBAND_DIR_SAVE, fname);/* moved this 'file spam' over to save... C. Blue */
3559 			gfp = fopen(buf, "rb");
3560 			if (gfp == (FILE*)NULL) continue;
3561 			data.fp = gfp;
3562 			fill_house(&houses[i], FILL_GUILD, (void*)&data);
3563 			fclose(gfp);
3564 		}
3565 	}
3566 }
3567 
save_guildhalls(struct worldpos * wpos)3568 void save_guildhalls(struct worldpos *wpos){
3569 	int i;
3570 	FILE *gfp;
3571 	struct guildsave data;
3572 	char buf[1024];
3573 	char fname[30];
3574 	data.mode = 1;
3575 	for (i = 0; i < num_houses; i++) {
3576 		if ((houses[i].dna->owner_type == OT_GUILD) && (inarea(wpos, &houses[i].wpos))) {
3577 			if (!houses[i].dna->owner) continue;
3578 #if DEBUG_LEVEL > 2
3579 			s_printf("save guildhall %d\n", i);
3580 #endif
3581 			sprintf(fname, "guild%.4d.data", i);
3582 //			path_build(buf, 1024, ANGBAND_DIR_DATA, fname);
3583 			path_build(buf, 1024, ANGBAND_DIR_SAVE, fname); /* moved this 'file spam' over to save... C. Blue */
3584 			gfp = fopen(buf, "rb+");
3585 			if (gfp == (FILE*)NULL) {
3586 				gfp = fopen(buf, "wb");
3587 				if (gfp == (FILE*)NULL)
3588 					continue;
3589 			}
3590 			data.fp = gfp;
3591 			fill_house(&houses[i], FILL_GUILD, (void*)&data);
3592 			fclose(gfp);
3593 		}
3594 	}
3595 }
3596 
3597 /* fix max_depth[] array for a character, which is usually done
3598    when converting an outdated savegame. - C. Blue */
fix_max_depth(player_type * p_ptr)3599 void fix_max_depth(player_type *p_ptr) {
3600 	struct worldpos wpos;
3601 	int i, j, x, y;
3602 	dungeon_type *d_ptr;
3603 
3604 	/* wipe (paranoia, should already be zeroed) */
3605 	for (i = 0; i < MAX_D_IDX * 2; i++) {
3606 		p_ptr->max_depth_wx[i] = 0;
3607 		p_ptr->max_depth_wy[i] = 0;
3608 		p_ptr->max_depth[i] = 0;
3609 		p_ptr->max_depth_tower[i] = FALSE;
3610 	}
3611 	/* attempt a fair translation */
3612 	for (x = 0; x < 64; x++) for (y = 0; y < 64; y++) {
3613 		/* exempt 0,0 */
3614 		if (!x && !y) continue;
3615 
3616                 wpos.wx = x; wpos.wy = y;
3617 
3618 		/* skip if player has never visited this dungeon's worldmap sector even! */
3619 		if (!is_admin(p_ptr) &&
3620 		    !(p_ptr->wild_map[wild_idx(&wpos) / 8] & (1 << (wild_idx(&wpos) % 8)))) continue;
3621 
3622 		/* translate */
3623 		if ((d_ptr = wild_info[y][x].tower)) {
3624 			wpos.wz = 1;
3625 			j = recall_depth_idx(&wpos, p_ptr);
3626 			if (p_ptr->max_dlv < d_ptr->baselevel) i = 0;
3627 			else i = p_ptr->max_dlv - d_ptr->baselevel + 1;
3628 			if (i > d_ptr->maxdepth) i = d_ptr->maxdepth;
3629 			if (x == WPOS_IRONDEEPDIVE_X && y == WPOS_IRONDEEPDIVE_Y && 1 == WPOS_IRONDEEPDIVE_Z) i = 0;
3630 			p_ptr->max_depth[j] = i;
3631 		}
3632 		if ((d_ptr = wild_info[y][x].dungeon)) {
3633 			wpos.wz = -1;
3634 			j = recall_depth_idx(&wpos, p_ptr);
3635 			if (p_ptr->max_dlv < d_ptr->baselevel) i = 0;
3636 			else i = p_ptr->max_dlv - d_ptr->baselevel + 1;
3637 			if (i > d_ptr->maxdepth) i = d_ptr->maxdepth;
3638 			if (x == WPOS_IRONDEEPDIVE_X && y == WPOS_IRONDEEPDIVE_Y && -1 == WPOS_IRONDEEPDIVE_Z) i = 0;
3639 			p_ptr->max_depth[j] = i;
3640 		}
3641 	}
3642 
3643 	s_printf("max_depth[] has been fixed/reset for '%s'.\n", p_ptr->name);
3644 }
3645 
3646 /* Note: there was still a 0,0 tower in my DM's list, but whatever.. */
fix_max_depth_bug(player_type * p_ptr)3647 void fix_max_depth_bug(player_type *p_ptr) {
3648 	int i, j;
3649 
3650 	for (i = 0; i < MAX_D_IDX * 2; i++) {
3651 #if 0 /* faster */
3652 		if (p_ptr->max_depth[i]) continue; /* (all bugged entries have depth 0) */
3653 #else /* cleaner */
3654 		if (!p_ptr->max_depth_wx[i] && !p_ptr->max_depth_wy[i]) continue; /* entry doesn't exist? */
3655 		if (p_ptr->max_depth_tower[i] || /* it's not a dungeon or it is and that dungeon actually exists? */
3656 		    wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].dungeon)
3657 			/* (all bugged entries are 'dungeons') */
3658 			continue;
3659 #endif
3660 		/* wipe: all "dungeons" that do not exist */
3661 		p_ptr->max_depth_wx[i] = 0;
3662 		p_ptr->max_depth_wy[i] = 0;
3663 		//p_ptr->max_depth_tower[i] = FALSE; (redundant)
3664 
3665 		/* important: move the following dungeons in our personal list up one,
3666 		   so as to not leave any holes (breaks the get_recall_depth algo) */
3667 		for (j = i; j < MAX_D_IDX * 2 - 1; j++) {
3668 			p_ptr->max_depth[j] = p_ptr->max_depth[j + 1];
3669 			p_ptr->max_depth_wx[j] = p_ptr->max_depth_wx[j + 1];
3670 			p_ptr->max_depth_wy[j] = p_ptr->max_depth_wy[j + 1];
3671 			p_ptr->max_depth_tower[j] = p_ptr->max_depth_tower[j + 1];
3672 		}
3673 		/* wipe the last entry accordingly, since it has been moved up by one */
3674 		p_ptr->max_depth[j] = 0;
3675 		p_ptr->max_depth_wx[j] = 0;
3676 		p_ptr->max_depth_wy[j] = 0;
3677 		p_ptr->max_depth_tower[j] = FALSE;
3678 	}
3679 
3680 	s_printf("max_depth[] has been bug-fixed for '%s'.\n", p_ptr->name);
3681 }
3682 /* Update max depths when dungeon/tower flag of a dungeon/tower was changed.
3683    Note: This assumes that apart from towns no dungeon and tower are on the same worldmap sector,
3684    or it might not swap the depths of the two correctly. - C. Blue */
fix_max_depth_towerdungeon(int Ind)3685 void fix_max_depth_towerdungeon(int Ind) {
3686 	player_type *p_ptr = Players[Ind];
3687 	int i;
3688 
3689 	s_printf("max_depth[] tower/dungeon changes have been fixed for '%s'.\n", p_ptr->name);
3690 
3691 	for (i = 0; i < MAX_D_IDX * 2; i++) {
3692 		if (!p_ptr->max_depth_wx[i] && !p_ptr->max_depth_wy[i]) continue; /* entry doesn't exist? */
3693 
3694 		/* actually fix bugged player data too here: */
3695 		if (!wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].tower &&
3696 		    !wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].dungeon) {
3697 			/* wipe the wrong entry */
3698 			s_printf(" erased (%d,%d) max_depth[]\n", p_ptr->max_depth_wx[i], p_ptr->max_depth_wy[i]);
3699 			p_ptr->max_depth[i] = 0;
3700 			p_ptr->max_depth_wx[i] = 0;
3701 			p_ptr->max_depth_wy[i] = 0;
3702 			p_ptr->max_depth_tower[i] = FALSE;
3703 
3704 			i--;
3705 			condense_max_depth(p_ptr);
3706 			continue;
3707 		}
3708 
3709 		/* both tower+dungeon here? too bad, we cannot decide then :/ -> skip them */
3710 		if (wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].tower &&
3711 		    wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].dungeon)
3712 			continue;
3713 
3714 		/* convert our tower depth to dungeon depth? */
3715 		if (p_ptr->max_depth_tower[i]) {
3716 			if (wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].tower) continue;
3717 			p_ptr->max_depth_tower[i] = FALSE;
3718 			s_printf("  flipped %d,%d to dungeon.\n", p_ptr->max_depth_wx[i], p_ptr->max_depth_wy[i]);
3719 			continue;
3720 		}
3721 
3722 		/* convert our dungeon depth to tower depth? */
3723 		if (!p_ptr->max_depth_tower[i]) {
3724 			if (wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].dungeon) continue;
3725 			p_ptr->max_depth_tower[i] = TRUE;
3726 			s_printf("  flipped %d,%d to tower.\n", p_ptr->max_depth_wx[i], p_ptr->max_depth_wy[i]);
3727 			continue;
3728 		}
3729 	}
3730 }
condense_max_depth(player_type * p_ptr)3731 void condense_max_depth(player_type *p_ptr) {
3732 	int i, j, k, d;
3733 	/* moar fixing old bugginess: remove all 0,0,0 entries between valid entries
3734 	   (empty entries aka 0,0,0 should only occur tailing the other entries) */
3735 	for (i = 0; i < MAX_D_IDX * 2; i++) {
3736 		/* hack: fix IDDC to zero */
3737 		if (p_ptr->max_depth_wx[i] == WPOS_IRONDEEPDIVE_X && p_ptr->max_depth_wy[i] == WPOS_IRONDEEPDIVE_Y
3738 		    && (p_ptr->max_depth_tower[i] ? 1 : -1) == WPOS_IRONDEEPDIVE_Z) {
3739 			p_ptr->max_depth[i] = 0;
3740 			continue;
3741 		}
3742 
3743 		if (p_ptr->max_depth_wx[i] || p_ptr->max_depth_wy[i]) continue; /* entry is not empty? */
3744 		/* ..entry i is empty.. */
3745 		for (j = i + 1; j < MAX_D_IDX * 2; j++) {
3746 			if (!p_ptr->max_depth_wx[j] && !p_ptr->max_depth_wy[j]) continue; /* entry is correctly empty? */
3747 
3748 			/* move it down by one */
3749 			d = j - i;
3750 			for (k = i; k < MAX_D_IDX * 2 - d; k++) {
3751 				p_ptr->max_depth[k] = p_ptr->max_depth[k + d];
3752 				p_ptr->max_depth_wx[k] = p_ptr->max_depth_wx[k + d];
3753 				p_ptr->max_depth_wy[k] = p_ptr->max_depth_wy[k + d];
3754 				p_ptr->max_depth_tower[k] = p_ptr->max_depth_tower[k + d];
3755 			}
3756 			/* wipe the last d entries accordingly, since they have been moved up by d */
3757 			for (k = MAX_D_IDX * 2 - d; k < MAX_D_IDX * 2; k++) {
3758 				p_ptr->max_depth[k] = 0;
3759 				p_ptr->max_depth_wx[k] = 0;
3760 				p_ptr->max_depth_wy[k] = 0;
3761 				p_ptr->max_depth_tower[k] = FALSE;
3762 			}
3763 			break;
3764 		}
3765 	}
3766 	s_printf("max_depth[] has been condensed for '%s'.\n", p_ptr->name);
3767 }
3768 /* Remove max_depth entries of dungeons/towers that were removed meanwhile. */
excise_obsolete_max_depth(player_type * p_ptr)3769 void excise_obsolete_max_depth(player_type *p_ptr) {
3770 	int i, j;
3771 
3772 	for (i = 0; i < MAX_D_IDX * 2; i++) {
3773 		if (!p_ptr->max_depth_wx[i] && !p_ptr->max_depth_wy[i]) continue; /* entry doesn't exist? */
3774 
3775 		if (p_ptr->max_depth_tower[i] && wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].tower) continue;
3776 		if (!p_ptr->max_depth_tower[i] && wild_info[p_ptr->max_depth_wy[i]][p_ptr->max_depth_wx[i]].dungeon) continue;
3777 
3778 		s_printf("Excised max_depth[] at (%d,%d,%d) for '%s'.\n",
3779 		    p_ptr->max_depth_wx[i], p_ptr->max_depth_wy[i], p_ptr->max_depth_tower[i] ? 1 : -1, p_ptr->name);
3780 
3781 		/* wipe: all "dungeons" that do not exist */
3782 		p_ptr->max_depth_wx[i] = 0;
3783 		p_ptr->max_depth_wy[i] = 0;
3784 		p_ptr->max_depth_tower[i] = FALSE;
3785 
3786 		/* important: move the following dungeons in our personal list up one
3787 		   so as to not leave any holes (breaks the get_recall_depth algo) */
3788 		for (j = i; j < MAX_D_IDX * 2 - 1; j++) {
3789 			p_ptr->max_depth[j] = p_ptr->max_depth[j + 1];
3790 			p_ptr->max_depth_wx[j] = p_ptr->max_depth_wx[j + 1];
3791 			p_ptr->max_depth_wy[j] = p_ptr->max_depth_wy[j + 1];
3792 			p_ptr->max_depth_tower[j] = p_ptr->max_depth_tower[j + 1];
3793 		}
3794 		/* wipe the last entry accordingly, since it has been moved up by one */
3795 		p_ptr->max_depth[j] = 0;
3796 		p_ptr->max_depth_wx[j] = 0;
3797 		p_ptr->max_depth_wy[j] = 0;
3798 		p_ptr->max_depth_tower[j] = FALSE;
3799 	}
3800 }
3801 
3802 #ifdef SEAL_INVALID_OBJECTS
seal_object(object_type * o_ptr)3803 static void seal_object(object_type *o_ptr) {
3804  #if 1 /* convert DDSM to EDSM */
3805 	if (o_ptr->tval == TV_DRAG_ARMOR && o_ptr->sval == SV_DRAGON_DEATH) {
3806 		s_printf("CONVERTING: %d, %d\n", o_ptr->tval, o_ptr->sval);
3807 		o_ptr->tval2 = o_ptr->tval;//remember, just in case
3808 		o_ptr->sval2 = o_ptr->sval;
3809 		o_ptr->sval = SV_DRAGON_SHINING;
3810 		o_ptr->k_idx = lookup_kind(o_ptr->tval, o_ptr->sval);
3811 		return;
3812 	}
3813  #endif
3814 
3815 	s_printf("SEALING: %d, %d\n", o_ptr->tval, o_ptr->sval);
3816 	o_ptr->tval2 = o_ptr->tval;
3817 	o_ptr->sval2 = o_ptr->sval;
3818 	o_ptr->note = quark_add(format("%d-%d", o_ptr->tval2, o_ptr->sval2));
3819 	o_ptr->tval = TV_SPECIAL;
3820 	o_ptr->sval = SV_SEAL;
3821 	o_ptr->k_idx = lookup_kind(o_ptr->tval, o_ptr->sval);
3822 }
unseal_object(object_type * o_ptr)3823 static void unseal_object(object_type *o_ptr) {
3824 	o_ptr->tval = o_ptr->tval2;
3825 	o_ptr->sval = o_ptr->sval2;
3826 	o_ptr->k_idx = lookup_kind(o_ptr->tval, o_ptr->sval);
3827 	o_ptr->note = 0;
3828 	o_ptr->note_utag = 0;
3829 	s_printf("UNSEALING: %d, %d\n", o_ptr->tval, o_ptr->sval);
3830 }
3831 /* Seal or unseal an object as required,
3832    or return FALSE if object no longer exists - C. Blue */
seal_or_unseal_object(object_type * o_ptr)3833 bool seal_or_unseal_object(object_type *o_ptr) {
3834 	/* Object does no longer exist? (for example now commented out, in k_info)
3835 	   - turn it into a 'seal' instead of deleting it! */
3836 	if (!o_ptr->k_idx) {
3837 		/* Object does no longer exist? Delete it! */
3838 		if (!o_ptr->tval && !o_ptr->sval) return FALSE;
3839 
3840 		seal_object(o_ptr);
3841 
3842 		/* In case someone is silly and removes seals while leaving the definition enabled: */
3843 		if (!o_ptr->k_idx) return FALSE;
3844 //		s_printf("sealed to %d, %d\n", o_ptr->tval, o_ptr->sval);
3845 	} else if (o_ptr->tval == TV_SPECIAL && o_ptr->sval == SV_SEAL) {
3846  #if 1 /* convert DDSM to EDSM -- fix for already sealed ones */
3847 		if (o_ptr->tval2 == TV_DRAG_ARMOR && o_ptr->sval2 == SV_DRAGON_SHINING) {
3848 			s_printf("RECONVERTING: %d, %d\n", o_ptr->tval2, o_ptr->sval2);
3849 			o_ptr->tval = o_ptr->tval2;
3850 			o_ptr->sval = o_ptr->sval2;
3851 			o_ptr->sval2 = SV_DRAGON_DEATH;
3852 			o_ptr->k_idx = lookup_kind(o_ptr->tval, o_ptr->sval);
3853 			o_ptr->note = 0;
3854 			o_ptr->note_utag = 0;
3855 			return TRUE;
3856 		}
3857  #endif
3858 
3859 		/* Object didn't exist to begin with? Delete it! */
3860 		if (!o_ptr->tval2 && !o_ptr->sval2) {
3861 			o_ptr->tval = o_ptr->sval = o_ptr->k_idx = 0;
3862 			return FALSE;
3863 		}
3864 
3865 		/* Try to restore the original item from the seal */
3866 		if (lookup_kind(o_ptr->tval2, o_ptr->sval2)) unseal_object(o_ptr);
3867 	}
3868  #if 0 /* glitchfix */
3869 	else if (o_ptr->tval == TV_DRAG_ARMOR && o_ptr->sval == SV_DRAGON_SHINING &&
3870 	    (o_ptr->ident & ID_BROKEN) &&
3871 	    o_ptr->name2 != EGO_BLASTED && o_ptr->name2b != EGO_BLASTED) {
3872 		o_ptr->ident &= ~ID_BROKEN;
3873 		s_printf("unbroken EDSM\n");
3874 	}
3875  #endif
3876  #if 1 /* glitchfix */
3877 	/* fix those which got AC nulled -_- */
3878 	else if (o_ptr->tval == TV_DRAG_ARMOR && o_ptr->sval == SV_DRAGON_SHINING && o_ptr->to_a == 0) {
3879 		o_ptr->to_a = 15 + rand_int(12);
3880 		s_printf("unnulled EDSM\n");
3881 	}
3882  #endif
3883 
3884 	/* success, aka object still exists */
3885 	return TRUE;
3886 }
3887 #endif
3888 
load_banlist(void)3889 void load_banlist(void) {
3890 	char buf[1024];
3891 	FILE *fp;
3892 	struct combo_ban *ptr;
3893 
3894 	path_build(buf, 1024, ANGBAND_DIR_CONFIG, "banlist.txt");
3895 
3896 	fp = fopen(buf, "r");
3897 	if (!fp) return;
3898 
3899 	do {
3900 		ptr = NEW(struct combo_ban);
3901 
3902 		if (fscanf(fp, "%[^|]|%[^|]|%[^|]|%d|%[^\n]\n", ptr->acc, ptr->ip, ptr->hostname, &ptr->time, ptr->reason) == EOF) {
3903 			s_printf("Reading banlist.txt: %s\n", strerror(ferror(fp)));
3904 			KILL(ptr, struct combo_ban);
3905 			break;
3906 		}
3907 
3908 		/* ffff... scanf */
3909 		if (ptr->acc[0] == ' ' && ptr->acc[1] == 0) ptr->acc[0] = 0;
3910 		if (ptr->ip[0] == ' ' && ptr->ip[1] == 0) ptr->ip[0] = 0;
3911 		if (ptr->hostname[0] == ' ' && ptr->hostname[1] == 0) ptr->hostname[0] = 0;
3912 		if (ptr->reason[0] == ' ' && ptr->reason[1] == 0) ptr->reason[0] = 0;
3913 
3914 		ptr->next = banlist;
3915 		banlist = ptr;
3916 	} while (!feof(fp));
3917 	fclose(fp);
3918 }
3919 
3920 /* ---------- Load dynamic quest data.  ---------- */
3921 /* This cannot be done in the server savefile, because it gets read before all
3922    ?_info.txt files are read from lib/data. So for example the remaining number
3923    of kills in a kill-quest cannot be stored anywhere, since the stage and
3924    stage goals are not yet initialised. Oops.
3925    However, saving this quest data of random/varying lenght is a mess anyway,
3926    so it's good that we keep it far away from the server savefile. */
load_quests_file()3927 static errr load_quests_file() {
3928 	int i, j, k;
3929 	errr err = 0;
3930 	char savefile[MAX_PATH_LENGTH];
3931 
3932 	s16b tmp_s16b;
3933 
3934 	quest_info *q_ptr;
3935 
3936 	qi_questor *q_questor;
3937 	qi_goal *q_goal;
3938 	qi_stage *q_stage;
3939 
3940 
3941 	s16b load_q_idx;
3942 	byte load_questors;
3943 	byte load_stages;
3944 	byte load_goals;
3945 
3946 	char load_codename[QI_CODENAME_LEN + 1];
3947 	char load_creator[MAX_CHARS];
3948 	u16b load_name;
3949 
3950 
3951 	path_build(savefile, MAX_PATH_LENGTH, ANGBAND_DIR_SAVE, "quests");
3952 	fff = my_fopen(savefile, "rb");
3953 	if (!fff) return (-1);
3954 
3955 	/* Allocate a new buffer */
3956 	fff_buf = C_NEW(MAX_BUF_SIZE, char);
3957 	fff_buf_pos = MAX_BUF_SIZE;
3958 
3959 	/* Read the version */
3960 	xor_byte = 0;
3961 	rd_byte(&qsf_major);
3962 	xor_byte = 0;
3963 	rd_byte(&qsf_minor);
3964 	xor_byte = 0;
3965 	rd_byte(&qsf_patch);
3966 	xor_byte = 0;
3967 	rd_byte(&qsf_extra);
3968 	/* Hack -- decrypt */
3969 	xor_byte = sf_extra;
3970 	/* Clear the checksums */
3971 	v_check = 0L;
3972 	x_check = 0L;
3973 
3974 	/* Operating system info */
3975 	rd_u32b(&sf_xtra);
3976 	/* Time of savefile creation */
3977 	rd_u32b(&sf_when);
3978 
3979 	/* begin reading the actual quest data */
3980 
3981 	rd_s16b(&load_q_idx);
3982 	if (load_q_idx < max_q_idx)
3983 		s_printf("Warning! Quests in save file (%d) fall short of quests in q_info.txt (%d).\n", load_q_idx, max_q_idx);
3984 	if (load_q_idx > max_q_idx)
3985 		s_printf("Warning! Quests in save file (%d) exceed quests in q_info.txt (%d).\n", load_q_idx, max_q_idx);
3986 	for (i = 0; i < load_q_idx; i++) {
3987 		if (i >= max_q_idx) break; /* Discard quests that exceed our info */
3988 
3989 		q_ptr = &q_info[i];
3990 
3991 		//to recognize the quest (ID)
3992 		rd_string(load_codename, QI_CODENAME_LEN + 1);
3993 		rd_string(load_creator, MAX_CHARS);
3994 		rd_u16b(&load_name);
3995 
3996 		/* Verify quest ID */
3997 		if (strcmp(q_ptr->codename, load_codename)) {
3998 			s_printf("Warning! Quest %d codename mismatch '%s'. Discarding its data.\n", i, load_codename);
3999 
4000 			/* Discard the complete data of this quest */
4001 			strip_bytes(22);
4002 			rd_byte(&load_questors);
4003 			strip_bytes(load_questors * 15);
4004 			rd_byte(&load_stages);
4005 			for (j = 0; j < load_stages; j++) {
4006 				strip_bytes(load_questors * 2);
4007 				strip_bytes(5);
4008 				rd_byte(&load_goals);
4009 				strip_bytes(load_goals * 4);
4010 			}
4011 			continue;
4012 		}
4013 
4014 		//main quest data
4015 		rd_byte((byte *) &q_ptr->active);
4016 #if 0 /* 0'ed for now, use 'x' disable feature from q_info.txt exclusively. */
4017 		rd_byte((byte *) &q_ptr->disabled);
4018 #else
4019 		strip_bytes(1);
4020 		/* Quest was disabled in q_info.txt? Then set 'disabled_on_load' marker. */
4021 		if (q_ptr->disabled) q_ptr->disabled_on_load = TRUE;
4022 #endif
4023 
4024 		rd_s16b(&q_ptr->cur_cooldown);
4025 		rd_s32b(&q_ptr->turn_activated);
4026 		rd_s32b(&q_ptr->turn_acquired);
4027 
4028 		rd_s16b(&q_ptr->cur_stage);
4029 		rd_u16b(&q_ptr->flags);
4030 		rd_byte((byte *) &q_ptr->tainted);
4031 		rd_s16b(&q_ptr->objects_registered);
4032 
4033 		rd_s16b(&q_ptr->timed_countdown);
4034 		rd_s16b(&q_ptr->timed_countdown_stage);
4035 		rd_byte((byte *) &q_ptr->timed_countdown_quiet);
4036 
4037 		//dynamically generated random passwords
4038 		if (!q_older_than(1, 0, 1)) {
4039 			for (j = 0; j < QI_PASSWORDS; j++)
4040 				rd_string(q_ptr->password[j], QI_PASSWORD_LEN + 1);
4041 		} else {
4042 			/* substitute now */
4043 			quest_init_passwords(i);
4044 		}
4045 
4046 		//questors:
4047 		rd_byte(&load_questors);
4048 		if (load_questors < q_ptr->questors)
4049 			s_printf("Warning! Questors in save file (%d) fall short of questors in q_info.txt (%d).\n", load_questors, q_ptr->questors);
4050 		if (load_questors > q_ptr->questors)
4051 			s_printf("Warning! Questors in save file (%d) exceed questors in q_info.txt (%d).\n", load_questors, q_ptr->questors);
4052 		for (j = 0; j < load_questors; j++) {
4053 			if (j >= q_ptr->questors) {
4054 				/* Discard questors that exceed our info */
4055 				strip_bytes(15);
4056 				continue;
4057 			}
4058 
4059 			q_questor = &q_ptr->questor[j];
4060 
4061 			rd_s16b(&q_questor->current_wpos.wx);
4062 			rd_s16b(&q_questor->current_wpos.wy);
4063 			rd_s16b(&q_questor->current_wpos.wz);
4064 
4065 			rd_s16b(&q_questor->current_x);
4066 			rd_s16b(&q_questor->current_y);
4067 
4068 			rd_s16b(&q_questor->mo_idx);
4069 			rd_s16b(&q_questor->talk_focus);//not needed
4070 
4071 			rd_byte((byte *) &q_questor->tainted);
4072 		}
4073 
4074 		//stages:
4075 		rd_byte(&load_stages);
4076 		if (load_stages < q_ptr->stages)
4077 			s_printf("Warning! Stages in save file (%d) fall short of stages in q_info.txt (%d).\n", load_stages, q_ptr->stages);
4078 		if (load_stages > q_ptr->stages)
4079 			s_printf("Warning! Stages in save file (%d) exceed stages in q_info.txt (%d).\n", load_stages, q_ptr->stages);
4080 		for (j = 0; j < load_stages; j++) {
4081 			if (j >= q_ptr->stages) {
4082 				/* Discard stages that exceed our info */
4083 				//questor hostility timers:
4084 				strip_bytes(load_questors * 2);
4085 				//goals:
4086 				strip_bytes(5);
4087 				rd_byte(&load_goals);
4088 				strip_bytes(load_goals * 4);
4089 				continue;
4090 			}
4091 
4092 			q_stage = &q_ptr->stage[j];
4093 
4094 			//questor hostility timers:
4095 			for (k = 0; k < load_questors; k++) {
4096 				if (k >= q_ptr->questors || !q_stage->questor_hostility[k]) {
4097 					/* Discard questors that exceed our info */
4098 					strip_bytes(2);
4099 					continue;
4100 				}
4101 				rd_s16b(&tmp_s16b);
4102 				if (tmp_s16b != 9999) q_stage->questor_hostility[k]->hostile_revert_timed_countdown = tmp_s16b;
4103 				else q_stage->questor_hostility[k]->hostile_revert_timed_countdown = 0;
4104 			}
4105 
4106 			//goals:
4107 			rd_byte(&load_goals);
4108 			if (load_goals < q_ptr->stage[j].goals)
4109 				s_printf("Warning! Goals in save file (%d) fall short of goals in q_info.txt (%d).\n", load_goals, q_ptr->stage[j].goals);
4110 			if (load_goals > q_ptr->stage[j].goals)
4111 				s_printf("Warning! Goals in save file (%d) exceed goals in q_info.txt (%d).\n", load_goals, q_ptr->stage[j].goals);
4112 			for (k = 0; k < load_goals; k++) {
4113 	    			if (j >= q_ptr->stage[j].goals) {
4114 					/* Discard goals that exceed our info */
4115 					strip_bytes(4);
4116 					continue;
4117 				}
4118 
4119 				q_goal = &q_stage->goal[k];
4120 
4121 				rd_byte((byte *) &q_goal->cleared);
4122 				rd_byte((byte *) &q_goal->nisi);
4123 
4124 				//kill goals:
4125 				rd_s16b(&tmp_s16b);
4126 				/* verify if it's still/not anymore a kill quest goal */
4127 				if (tmp_s16b != -1) {
4128 					if (!q_goal->kill)
4129 						s_printf("Warning! Quest %d stage %d goal %d was previously 'kill'.\n", i, j, k);
4130 					else
4131 						q_goal->kill->number_left = tmp_s16b;
4132 				} else {
4133 					if (q_goal->kill) {
4134 						s_printf("Warning! Quest %d stage %d goal %d was previously not 'kill'.\n", i, j, k);
4135 						q_goal->kill->number_left = 0;
4136 					}
4137 				}
4138 			}
4139 		}
4140 	}
4141 
4142 	s_printf("Read %d saved quests states.\n", load_q_idx);
4143 
4144 	/* finish up */
4145 
4146 	C_FREE(fff_buf, MAX_BUF_SIZE, char);
4147 	if (ferror(fff)) err = -1;
4148 	my_fclose(fff);
4149 	return (err);
4150 }
4151 /* Must be called after 'init_some_arrays', so that we already know the quest info! */
load_quests(void)4152 void load_quests(void) {
4153 	int fd = -1;
4154 	byte vvv[4];
4155 	errr err = 0;
4156 	cptr what = "generic";
4157 	char buf[1024];
4158 
4159 	path_build(buf, 1024, ANGBAND_DIR_SAVE, "quests");
4160 
4161 	if (!file_exist(buf)) {
4162 		s_printf("Quests savefile does not exist\n");
4163 		return; //FALSE;
4164 	}
4165 	if (!err) {
4166 		fd = fd_open(buf, O_RDONLY);
4167 		if (fd < 0) err = -1;
4168 		if (err) what = "Cannot open quests savefile";
4169 	}
4170 	if (!err) {
4171 		/* Read the first four bytes */
4172 		if (fd_read(fd, (char*)(vvv), 4)) err = -1;
4173 		if (err) what = "Cannot read quests savefile";
4174 		(void)fd_close(fd);
4175 	}
4176 	if (!err) {
4177 		qsf_major = vvv[0];
4178 		qsf_minor = vvv[1];
4179 		qsf_patch = vvv[2];
4180 		qsf_extra = vvv[3];
4181 
4182 		err = load_quests_file();
4183 		if (err) what ="Cannot parse quests savefile error %d";
4184 	}
4185 	if (!err) {
4186 		if ((QUEST_SF_VERSION_MAJOR != qsf_major) ||
4187 		    (QUEST_SF_VERSION_MINOR != qsf_minor) ||
4188 		    (QUEST_SF_VERSION_PATCH != qsf_patch)) {
4189 			printf("Converted a %d.%d.%d quests savefile.\n",
4190 			    qsf_major, qsf_minor, qsf_patch);
4191 		}
4192 		return; //TRUE;
4193 	}
4194 	s_printf("Error (%s) reading a %d.%d.%d quests savefile.\n", what, qsf_major, qsf_minor, qsf_patch);
4195 	return; //FALSE;
4196 }
4197