1 /* File: load2.c */
2 
3 /* Purpose: support for loading savefiles -BEN- */
4 
5 #include "mangband.h"
6 #include "../common/md5.h"
7 
8 
9 /*
10  * This file is responsible for loading all "2.7.X" savefiles
11  *
12  * Note that 2.7.0 - 2.7.2 savefiles are obsolete and will not work.
13  *
14  * We attempt to prevent corrupt savefiles from inducing memory errors.
15  *
16  * Note that Angband 2.7.9 encodes "terrain features" in the savefile
17  * using the old 2.7.8 method.  Angband 2.8.0 will use the same method
18  * to read pre-2.8.0 savefiles, but will use a new method to save them,
19  * which will only affect "save.c".
20  *
21  * Note that Angband 2.8.0 will use a VERY different savefile method,
22  * which will use "blocks" of information which can be ignored or parsed,
23  * and which will not use a silly "protection" scheme on the savefiles,
24  * but which may still use some form of "checksums" to prevent the use
25  * of "corrupt" savefiles, which might cause nasty weirdness.
26  *
27  * Note that this file should not use the random number generator, the
28  * object flavors, the visual attr/char mappings, or anything else which
29  * is initialized *after* or *during* the "load character" function.
30  *
31  * We should also make the "cheating" options official flags, and
32  * move the "byte" options to a different part of the code, perhaps
33  * with a few more (for variety).
34  *
35  * Implement simple "savefile extenders" using some form of "sized"
36  * chunks of bytes, with a {size,type,data} format, so everyone can
37  * know the size, interested people can know the type, and the actual
38  * data is available to the parsing routines that acknowledge the type.
39  *
40  * Consider changing the "globe of invulnerability" code so that it
41  * takes some form of "maximum damage to protect from" in addition to
42  * the existing "number of turns to protect for", and where each hit
43  * by a monster will reduce the shield by that amount.
44  *
45  * XXX XXX XXX
46  */
47 
48 
49 
50 
51 
52 /*
53  * Local "savefile" pointer
54  */
55 static ang_file* file_handle;
56 /* Line counter */
57 static int line_counter;
58 
59 
60 /*
61  * Hack -- simple "checksum" on the actual values
62  */
63 static u32b	v_check = 0L;
64 
65 /*
66  * Hack -- simple "checksum" on the encoded bytes
67  */
68 static u32b	x_check = 0L;
69 
70 /*
71  * Hack -- buffer for reading text save files
72  */
73 static char file_buf[1024];
74 
75 /*
76  * Functions to read data from the textual format save file
77  */
78 
79 /* Start a section */
start_section_read(char * name)80 bool start_section_read(char* name)
81 {
82 	char seek_section[80];
83 	char got_section[80];
84 	bool matched = FALSE;
85 
86 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
87 	{
88 		line_counter++;
89 		sprintf(seek_section,"<%s>",name);
90 		if(sscanf(file_buf,"%s",got_section) == 1)
91 		{
92 			matched = !strcmp(got_section,seek_section);
93 		}
94 	}
95 	if(!matched)
96 	{
97 		plog(format("Missing section.  Expected '%s', found '%s' at line %i",seek_section,got_section,line_counter));
98 		return (FALSE);
99 	}
100 	return (TRUE);
101 }
102 
103 /* End a section */
end_section_read(char * name)104 bool end_section_read(char* name)
105 {
106 	char seek_section[80];
107 	char got_section[80];
108 	bool matched = FALSE;
109 
110 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
111 	{
112 		line_counter++;
113 		sprintf(seek_section,"</%s>",name);
114 		if(sscanf(file_buf,"%s",got_section) == 1)
115 		{
116 			matched = !strcmp(got_section,seek_section);
117 		}
118 	}
119 	if(!matched)
120 	{
121 		plog(format("Missing end section.  Expected '%s', found '%s' at line %i",seek_section,got_section,line_counter));
122 		return (FALSE);
123 	}
124 	return (TRUE);
125 }
126 
127 /* Read a puny byte */
read_byte(char * name,byte * dst)128 bool read_byte(char* name, byte *dst)
129 {
130 	char seek_name[80];
131 	bool matched = FALSE;
132 	byte value;
133 #ifndef SCNu8
134 	u16b larger_value;
135 #endif
136 
137 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
138 	{
139 		line_counter++;
140 #ifndef SCNu8
141 		if(sscanf(file_buf,"%s = %" SCNu16, seek_name,&larger_value) == 2)
142 #else
143 		if(sscanf(file_buf,"%s = %" SCNu8, seek_name,&value) == 2)
144 #endif
145 		{
146 			matched = !strcmp(seek_name,name);
147 		}
148 	}
149 	if(!matched)
150 	{
151 		plog(format("Missing integer.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
152 		return (FALSE);
153 	}
154 #ifndef SCNu8
155 	if (larger_value > 255)
156 	{
157 		plog(format("Integer overflow.  Expected '%s' <= 255, found '%u' at line %i",name,larger_value,line_counter));
158 		return (FALSE);
159 	}
160 	*dst = (byte)larger_value;
161 #else
162 	*dst = value;
163 #endif
164 	return (TRUE);
165 }
166 
167 
168 /* Read a short integer */
read_short(char * name,s16b * dst)169 bool read_short(char* name, s16b *dst)
170 {
171 	char seek_name[80];
172 	bool matched = FALSE;
173 	s16b value;
174 
175 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
176 	{
177 		line_counter++;
178 		if(sscanf(file_buf,"%s = %" SCNu16, seek_name,&value) == 2)
179 		{
180 			matched = !strcmp(seek_name,name);
181 		}
182 	}
183 	if(!matched)
184 	{
185 		plog(format("Missing integer.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
186 		return (FALSE);
187 	}
188 	*dst = value;
189 	return (TRUE);
190 }
191 
192 /* Read an integer */
read_int(char * name,int * dst)193 bool read_int(char* name, int *dst)
194 {
195 	char seek_name[80];
196 	bool matched = FALSE;
197 	int value;
198 
199 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
200 	{
201 		line_counter++;
202 		if(sscanf(file_buf,"%s = %i",seek_name,&value) == 2)
203 		{
204 			matched = !strcmp(seek_name,name);
205 		}
206 	}
207 	if(!matched)
208 	{
209 		plog(format("Missing integer.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
210 		return (FALSE);
211 	}
212 	*dst = value;
213 	return (TRUE);
214 }
215 
216 /* Read an unsigned integer */
read_uint(const char * name,uint * dst)217 bool read_uint(const char* name, uint *dst)
218 {
219 	char seek_name[80];
220 	bool matched = FALSE;
221 	uint value;
222 
223 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
224 	{
225 		line_counter++;
226 		if(sscanf(file_buf,"%s = %u",seek_name,&value) == 2)
227 		{
228 			matched = !strcmp(seek_name,name);
229 		}
230 	}
231 	if(!matched)
232 	{
233 		plog(format("Missing unsigned integer.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
234 		return (FALSE);
235 	}
236 	*dst = value;
237 	return (TRUE);
238 }
239 
240 /* Read a 'huge' */
read_huge(char * name,huge * dst)241 bool read_huge(char* name, huge *dst)
242 {
243 	char seek_name[80];
244 	bool matched = FALSE;
245 	huge value;
246 
247 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
248 	{
249 		line_counter++;
250 		if(sscanf(file_buf,"%s = %" SCNu64 ,seek_name,&value) == 2)
251 		{
252 			matched = !strcmp(seek_name,name);
253 		}
254 	}
255 	if(!matched)
256 	{
257 		plog(format("Missing signed long.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
258 		return (FALSE);
259 	}
260 	*dst = value;
261 	return (TRUE);
262 }
263 
264 /* Read an hturn */
read_hturn(char * name,hturn * value)265 bool read_hturn(char* name, hturn *value)
266 {
267 	char seek_name[80];
268 	bool matched = FALSE;
269 	s64b era, turn;
270 
271 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
272 	{
273 		line_counter++;
274 		if (sscanf(file_buf, "%s = %" SCNu64 " %" SCNu64, seek_name, &era, &turn) == 3)
275 		{
276 			matched = !strcmp(seek_name,name);
277 		}
278 	}
279 	if(!matched)
280 	{
281 		plog(format("Missing hturn.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
282 		return (FALSE);
283 	}
284 
285 	value->era = era;
286 	value->turn = turn;
287 	return (TRUE);
288 }
289 
290 /* Read a string */
291 /* Returns TRUE if the string could be read */
read_str(char * name,char * value)292 bool read_str(char* name, char* value)
293 {
294 	char seek_name[80];
295 	bool matched = FALSE;
296 	char *c;
297 
298 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
299 	{
300 		line_counter++;
301 		sscanf(file_buf,"%s = ",seek_name);
302 		if (!strcmp(seek_name,name))
303 		{
304 			matched = TRUE;
305 		}
306 	}
307 	if (!matched)
308 	{
309 		plog(format("Missing string data.  Expected '%s' got '%s' at line %i",name,seek_name,line_counter));
310 		return FALSE;
311 	}
312 
313 	c = file_buf;
314 	while(*c != '=') c++;
315 	c+=2;
316 
317 	while( *c >= 31 )
318 	{
319 		*value = *c; c++; value++;
320 	}
321 	*value = '\0';
322 	return (TRUE);
323 }
324 
325 /* Read a quark */
326 /* Tiny wrapper around read_str */
read_quark(char * name,u16b * value)327 bool read_quark(char* name, u16b *value)
328 {
329 	char note[80];
330 	/* Read string into temp buffer. */
331 	if (!read_str(name, note))
332 	{
333 		return (FALSE);
334 	}
335 	if (STRZERO(note))
336 	{
337 		*value = 0;
338 		return (TRUE);
339 	}
340 	/* And create a quark from it */
341 	*value = quark_add(note);
342 	return (TRUE);
343 }
344 
345 /* Read a float */
346 /* Returns TRUE if the float could be read */
read_float(char * name,float * dst)347 bool read_float(char* name, float *dst)
348 {
349 	char seek_name[80];
350 	bool matched = FALSE;
351 	float value;
352 
353 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
354 	{
355 		line_counter++;
356 		if(sscanf(file_buf,"%s = %f",seek_name,&value) == 2)
357 		{
358 			matched = !strcmp(seek_name,name);
359 		}
360 	}
361 	if(!matched)
362 	{
363 		plog(format("Missing float.  Expected '%s', found '%s' at line %i",name,file_buf,line_counter));
364 		return (FALSE);
365 	}
366 	*dst = value;
367 	return (TRUE);
368 }
369 
370 /* Read some binary data */
read_binary(char * name,char * value,int max_len)371 bool read_binary(char* name, char* value, int max_len)
372 {
373 	char seek_name[80];
374 	bool matched = FALSE;
375 	char hex[3];
376 	char *c;
377 	char *bin;
378 	unsigned int abyte;
379 	hex[2] = '\0';
380 
381 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
382 	{
383 		line_counter++;
384 		sscanf(file_buf,"%s = ",seek_name);
385 		if (!strcmp(seek_name,name))
386 		{
387 			matched = TRUE;
388 		}
389 	}
390 	if (!matched)
391 	{
392 		plog(format("Missing binary data.  Expected '%s' got '%s' at line %i",name,seek_name,line_counter));
393 		return (FALSE);
394 	}
395 
396 	c = file_buf;
397 	while(*c != '=') c++;
398 	c+=2;
399 
400 	bin = value;
401 	while( *c >= 31 )
402 	{
403 		hex[0] = *c; c++;
404 		hex[1] = *c; c++;
405 		sscanf(hex,"%2x",&abyte);
406 		*bin = (byte)abyte;
407 		bin++;
408 	}
409 	return (TRUE);
410 }
411 
412 /* Skip a named value */
skip_value(char * name)413 void skip_value(char* name)
414 {
415 	char seek_name[80];
416 	long fpos;
417 
418 	/* Remember where we are incase there is nothing to skip */
419 	fpos = file_tell(file_handle);
420 	sprintf(seek_name,"%s = ",name);
421 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
422 	{
423 		line_counter++;
424 		if(strstr(file_buf,seek_name) == NULL)
425 		{
426 			/* Move back on seek failures */
427 			file_seek(file_handle, fpos);
428 			line_counter--;
429 			/* plog(format("Missing value.  Expected to skip '%s', found '%s' at line %i\n",name,seek_name,line_counter)); */
430 			/* exit(1); */
431 		}
432 	}
433 }
434 
435 /* Check if the given named value is next */
value_exists(const char * name)436 bool value_exists(const char* name)
437 {
438 	char seek_name[80];
439 	bool matched = FALSE;
440 	long fpos;
441 
442 	/* Remember where we are */
443 	fpos = file_tell(file_handle);
444 	sprintf(seek_name,"%s = ",name);
445 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
446 	{
447 		matched = TRUE;
448 		if(strstr(file_buf,seek_name) == NULL)
449 		{
450 			matched = FALSE;
451 		}
452 	}
453 	/* Move back */
454 	file_seek(file_handle, fpos);
455 	return(matched);
456 }
457 
458 /* Check if the given named section is next */
section_exists(char * name)459 bool section_exists(char* name)
460 {
461 	char seek_section[80];
462 	char got_section[80];
463 	bool matched = FALSE;
464 	long fpos;
465 
466 	/* Remember where we are */
467 	fpos = file_tell(file_handle);
468 	if (file_getl(file_handle, file_buf, sizeof(file_buf)-1))
469 	{
470 		sprintf(seek_section,"<%s>",name);
471 		if(sscanf(file_buf,"%s",got_section) == 1)
472 		{
473 			matched = !strcmp(got_section,seek_section);
474 		}
475 	}
476 	/* Move back */
477 	file_seek(file_handle, fpos);
478 	return(matched);
479 }
480 
481 /*
482  * Show information on the screen, one line at a time.
483  * Start at line 2, and wrap, if needed, back to line 2.
484  */
note(cptr msg)485 static void note(cptr msg)
486 {
487 	plog(msg);
488 #if 0
489 	static int y = 2;
490 
491 	/* Draw the message */
492 	prt(msg, y, 0);
493 
494 	/* Advance one line (wrap if needed) */
495 	if (++y >= 24) y = 2;
496 
497 	/* Flush it */
498 	Term_fresh();
499 #endif
500 }
501 
502 
503 /*
504  * Hack -- determine if an item is "wearable" (or a missile)
505  */
wearable_p(object_type * o_ptr)506 static bool wearable_p(object_type *o_ptr)
507 {
508 	/* Valid "tval" codes */
509 	switch (o_ptr->tval)
510 	{
511 		case TV_SHOT:
512 		case TV_ARROW:
513 		case TV_BOLT:
514 		case TV_BOW:
515 		case TV_DIGGING:
516 		case TV_HAFTED:
517 		case TV_POLEARM:
518 		case TV_SWORD:
519 		case TV_BOOTS:
520 		case TV_GLOVES:
521 		case TV_HELM:
522 		case TV_CROWN:
523 		case TV_SHIELD:
524 		case TV_CLOAK:
525 		case TV_SOFT_ARMOR:
526 		case TV_HARD_ARMOR:
527 		case TV_DRAG_ARMOR:
528 		case TV_LITE:
529 		case TV_AMULET:
530 		case TV_RING:
531 		{
532 			return (TRUE);
533 		}
534 	}
535 
536 	/* Nope */
537 	return (FALSE);
538 }
539 
540 
541 /*
542  * Read an item (2.7.0 or later)
543  *
544  * Note that savefiles from 2.7.0 and 2.7.1 are obsolete.
545  *
546  * Note that pre-2.7.9 savefiles (from Angband 2.5.1 onward anyway)
547  * can be read using the code above.
548  *
549  * This function attempts to "repair" old savefiles, and to extract
550  * the most up to date values for various object fields.
551  *
552  * Note that Angband 2.7.9 introduced a new method for object "flags"
553  * in which the "flags" on an object are actually extracted when they
554  * are needed from the object kind, artifact index, ego-item index,
555  * and two special "xtra" fields which are used to encode any "extra"
556  * power of certain ego-items.  This had the side effect that items
557  * imported from pre-2.7.9 savefiles will lose any "extra" powers they
558  * may have had, and also, all "uncursed" items will become "cursed"
559  * again, including Calris, even if it is being worn at the time.  As
560  * a complete hack, items which are inscribed with "uncursed" will be
561  * "uncursed" when imported from pre-2.7.9 savefiles.
562  */
563  /* For wilderness levels, dun_depth has been changed from 1 to 4 bytes. */
rd_item(object_type * o_ptr)564 static bool rd_item(object_type *o_ptr)
565 {
566 #undef __try
567 #define __try(X) if (!(X)) { return (FALSE); }
568 	byte old_dd;
569 	byte old_ds;
570 
571 	u32b f1, f2, f3;
572 
573 	object_kind *k_ptr;
574 
575 	char note[128];
576 
577 	__try( start_section_read("item") );
578 
579 	/* Hack -- wipe */
580 	WIPE(o_ptr, object_type);
581 
582 	/* Skip name */
583 	skip_value("name");
584 
585 	/* Kind */
586 	__try( read_short("k_idx", &o_ptr->k_idx) );
587 
588 	/* Location */
589 	__try( read_byte("iy", &o_ptr->iy) );
590 	__try( read_byte("ix", &o_ptr->ix) );
591 
592 	__try( read_short("dun_depth", &o_ptr->dun_depth) );
593 
594 	/* Type/Subtype */
595 	__try( read_byte("tval", &o_ptr->tval) );
596 	__try( read_byte("sval", &o_ptr->sval) );
597 
598 	/* Base pval */
599 	__try( read_int("bpval", &o_ptr->bpval) );
600 
601 	/* Special pval */
602 	__try( read_int("pval", &o_ptr->pval) );
603 
604 
605 	__try( read_byte("discount", &o_ptr->discount) );
606 	__try( read_byte("number", &o_ptr->number) );
607 	__try( read_short("weight", &o_ptr->weight) );
608 
609 	__try( read_byte("name1", &o_ptr->name1) );
610 	__try( read_byte("name2", &o_ptr->name2) );
611 	__try( read_int("name3", &o_ptr->name3) );
612 	__try( read_short("timeout", &o_ptr->timeout) );
613 
614 	__try( read_short("to_h", &o_ptr->to_h) );
615 	__try( read_short("to_d", &o_ptr->to_d) );
616 	__try( read_short("to_a", &o_ptr->to_a) );
617 
618 	__try( read_short("ac", &o_ptr->ac) );
619 
620 	__try( read_byte("dd", &old_dd) );
621 	__try( read_byte("ds", &old_ds) );
622 
623 	__try( read_byte("ident", &o_ptr->ident) );
624 
625 	/* Special powers */
626 	__try( read_byte("xtra1", &o_ptr->xtra1) );
627 	__try( read_byte("xtra2", &o_ptr->xtra2) );
628 
629 	/* Inscription */
630 	__try( read_quark("inscription", &o_ptr->note) );
631 
632 	/* Owner information */
633 	if (value_exists("owner_name"))
634 	{
635 		__try( read_quark("owner_name", &o_ptr->owner_name) );
636 		__try( read_int("owner_id", &o_ptr->owner_id) );
637 	}
638 
639 	/* Monster holding object */
640 	__try( read_short("held_m_idx", &o_ptr->held_m_idx) );
641 
642 	/* Origin */
643 	if (value_exists("origin"))
644 	{
645 		__try( read_byte("origin", &o_ptr->origin) );
646 		__try( read_byte("origin_depth", &o_ptr->origin_depth) );
647 		__try( read_short("origin_xtra", &o_ptr->origin_xtra) );
648 		__try( read_quark("origin_player", &o_ptr->origin_player) );
649 		if (o_ptr->origin_xtra >= z_info->r_max) { o_ptr->origin_xtra = 0; o_ptr->origin = ORIGIN_NONE; }
650 		if (o_ptr->origin >= ORIGIN_MAX) o_ptr->origin = ORIGIN_NONE;
651 	}
652 
653 	__try( end_section_read("item") );
654 
655 	/* Mega-Hack -- handle "dungeon objects" later */
656 	if ((o_ptr->k_idx >= 445) && (o_ptr->k_idx <= 479)) return (TRUE);
657 
658 
659 	/* Obtain the "kind" template */
660 	k_ptr = &k_info[o_ptr->k_idx];
661 
662 	/* Obtain tval/sval from k_info */
663 	o_ptr->tval = k_ptr->tval;
664 	o_ptr->sval = k_ptr->sval;
665 
666 
667 	/* Hack -- notice "broken" items */
668 	if (k_ptr->cost <= 0) o_ptr->ident |= ID_BROKEN;
669 
670 
671 	/* Repair non "wearable" items */
672 	if (!wearable_p(o_ptr))
673 	{
674 		/* Acquire correct fields */
675 		o_ptr->to_h = k_ptr->to_h;
676 		o_ptr->to_d = k_ptr->to_d;
677 		o_ptr->to_a = k_ptr->to_a;
678 
679 		/* Acquire correct fields */
680 		o_ptr->ac = k_ptr->ac;
681 		o_ptr->dd = k_ptr->dd;
682 		o_ptr->ds = k_ptr->ds;
683 
684 		/* Acquire correct weight */
685 		o_ptr->weight = k_ptr->weight;
686 
687 		/* Paranoia */
688 		o_ptr->name1 = o_ptr->name2 = 0;
689 
690 		/* All done */
691 		return (TRUE);
692 	}
693 
694 
695 	/* Extract the flags */
696 	object_flags(o_ptr, &f1, &f2, &f3);
697 
698 
699 	/* Paranoia */
700 	if (true_artifact_p(o_ptr))
701 	{
702 		artifact_type *a_ptr;
703 
704 		/* Obtain the artifact info */
705 		a_ptr = &a_info[o_ptr->name1];
706 
707 		/* Verify that artifact */
708 		if (!a_ptr->name) o_ptr->name1 = 0;
709 	}
710 
711 	/* Paranoia */
712 	if (o_ptr->name2)
713 	{
714 		ego_item_type *e_ptr;
715 
716 		/* Obtain the ego-item info */
717 		e_ptr = &e_info[o_ptr->name2];
718 
719 		/* Verify that ego-item */
720 		if (!e_ptr->name) o_ptr->name2 = 0;
721 	}
722 
723 
724 	/* Acquire standard fields */
725 	o_ptr->ac = k_ptr->ac;
726 	o_ptr->dd = k_ptr->dd;
727 	o_ptr->ds = k_ptr->ds;
728 
729 	/* Acquire standard weight */
730 	o_ptr->weight = k_ptr->weight;
731 
732 	/* Hack -- extract the "broken" flag */
733 	if (o_ptr->pval < 0) o_ptr->ident |= ID_BROKEN;
734 
735 
736 	/* Artifacts */
737 	if (artifact_p(o_ptr))
738 	{
739 		artifact_type *a_ptr = artifact_ptr(o_ptr);
740 
741 		/* Acquire new artifact "pval" */
742 		o_ptr->pval = a_ptr->pval;
743 
744 		/* Acquire new artifact fields */
745 		o_ptr->ac = a_ptr->ac;
746 		o_ptr->dd = a_ptr->dd;
747 		o_ptr->ds = a_ptr->ds;
748 
749 		/* Acquire new artifact weight */
750 		o_ptr->weight = a_ptr->weight;
751 
752 		/* Hack -- extract the "broken" flag */
753 		if (!a_ptr->cost) o_ptr->ident |= ID_BROKEN;
754 	}
755 
756 	/* Ego items */
757 	if (o_ptr->name2)
758 	{
759 		ego_item_type *e_ptr;
760 
761 		/* Obtain the ego-item info */
762 		e_ptr = &e_info[o_ptr->name2];
763 
764 		/* Hack -- keep some old fields */
765 		if ((o_ptr->dd < old_dd) && (o_ptr->ds == old_ds))
766 		{
767 			/* Keep old boosted damage dice */
768 			o_ptr->dd = old_dd;
769 		}
770 
771 		/* Hack -- extract the "broken" flag */
772 		if (!e_ptr->cost) o_ptr->ident |= ID_BROKEN;
773 
774 		/* Mega-Hack - Enforce the special broken items */
775 		if ((o_ptr->name2 == EGO_BLASTED) ||
776 			(o_ptr->name2 == EGO_SHATTERED))
777 		{
778 			/* These were set to k_info values by preceding code */
779 			o_ptr->ac = 0;
780 			o_ptr->dd = 0;
781 			o_ptr->ds = 0;
782 		}
783 	}
784 	return (TRUE);
785 }
786 
787 
788 /*
789  * Read a monster
790  */
791 
rd_monster(monster_type * m_ptr)792 static bool rd_monster(monster_type *m_ptr)
793 {
794 #undef __try
795 #define __try(X) if (!(X)) { return (FALSE); }
796 
797 	__try( start_section_read("monster") );
798 
799 	/* Hack -- wipe */
800 	WIPE(m_ptr, monster_type);
801 
802 	skip_value("name");
803 
804 	/* Read the monster race */
805 	__try( read_short("r_idx", &m_ptr->r_idx) );
806 
807 	/* Read the other information */
808 	__try( read_byte("fy", &m_ptr->fy) );
809 	__try( read_byte("fx", &m_ptr->fx) );
810 	__try( read_short("dun_depth", &m_ptr->dun_depth) );
811 	__try( read_short("hp", &m_ptr->hp) );
812 	__try( read_short("maxhp", &m_ptr->maxhp) );
813 	__try( read_short("csleep", &m_ptr->csleep) );
814 	__try( read_byte("mspeed", &m_ptr->mspeed) );
815 	__try( read_huge("energy", &m_ptr->energy) );
816 	__try( read_byte("stunned", &m_ptr->stunned) );
817 	__try( read_byte("confused", &m_ptr->confused) );
818 	__try( read_byte("afraid", &m_ptr->monfear) );
819 
820 	__try( end_section_read("monster") );
821 
822 	return (TRUE);
823 }
824 
825 
826 
827 
828 
829 /*
830  * Read the monster lore
831  */
rd_lore(player_type * p_ptr,int r_idx)832 static bool rd_lore(player_type *p_ptr, int r_idx)
833 {
834 #undef __try
835 #define __try(X) if (!(X)) { return (FALSE); }
836 
837 	int i;
838 
839 	monster_lore *l_ptr = p_ptr->l_list + r_idx;
840 
841 	__try( start_section_read("lore") );
842 
843 	/* Count sights/deaths/kills */
844 	__try( read_short("sights", &l_ptr->sights) );
845 	__try( read_short("deaths", &l_ptr->deaths) );
846 	__try( read_short("pkills", &l_ptr->pkills) );
847 	__try( read_short("tkills", &l_ptr->tkills) );
848 
849 	/* Count wakes and ignores */
850 	__try( read_byte("wake", &l_ptr->wake) );
851 	__try( read_byte("ignore", &l_ptr->ignore) );
852 
853 	/* Count drops */
854 	__try( read_byte("drop_gold", &l_ptr->drop_gold) );
855 	__try( read_byte("drop_item", &l_ptr->drop_item) );
856 
857 	/* Count spells */
858 	__try( read_byte("cast_innate", &l_ptr->cast_innate) );
859 	__try( read_byte("cast_spell", &l_ptr->cast_spell) );
860 
861 	/* Count blows of each type */
862 	__try( start_section_read("blows") );
863 	for (i = 0; i < MONSTER_BLOW_MAX; i++)
864 	{
865 		__try( read_byte("blow", &l_ptr->blows[i]) );
866 	}
867 	__try( end_section_read("blows") );
868 
869 
870 	/* Memorize flags */
871 	__try( start_section_read("flags") );
872 	__try( read_int("flag", &l_ptr->flags1) );
873 	__try( read_int("flag", &l_ptr->flags2) );
874 	__try( read_int("flag", &l_ptr->flags3) );
875 	__try( read_int("flag", &l_ptr->flags4) );
876 	__try( read_int("flag", &l_ptr->flags6) );
877 	__try( read_int("flag", &l_ptr->flags6) );
878 	__try( end_section_read("flags") );
879 
880 	/* Repair the lore flags */
881 	/* No need to repair AFAIU
882 	l_ptr->flags1 &= r_ptr->flags1;
883 	l_ptr->flags2 &= r_ptr->flags2;
884 	l_ptr->flags3 &= r_ptr->flags3;
885 	l_ptr->flags4 &= r_ptr->flags4;
886 	l_ptr->flags5 &= r_ptr->flags5;
887 	l_ptr->flags6 &= r_ptr->flags6;
888 	*/
889 	__try( end_section_read("lore") );
890 
891 	return (TRUE);
892 }
893 
894 /*
895  * Read the uniques lore
896  */
rd_u_lore(int r_idx)897 static bool rd_u_lore(int r_idx)
898 {
899 #undef __try
900 #define __try(X) if (!(X)) { return (FALSE); }
901 
902 	monster_race *r_ptr = &r_info[r_idx];
903 
904 	__try( start_section_read("lore") );
905 
906 	skip_value("name");
907 
908 	/* Count sights/deaths/kills */
909 	__try( read_short("sights", &r_ptr->r_sights) );
910 	if (value_exists("deaths")) skip_value("deaths");
911 	if (value_exists("pkills")) skip_value("pkills");
912 	__try( read_short("tkills", &r_ptr->r_tkills) );
913 
914 	if (value_exists("wake")) skip_value("wake");
915 	if (value_exists("ignore")) skip_value("ignore");
916 	if (value_exists("respawn_timer")) skip_value("respawn_timer");
917 	if (value_exists("drop_gold")) skip_value("drop_gold");
918 	if (value_exists("drop_item")) skip_value("drop_item");
919 	if (value_exists("cast_innate")) skip_value("cast_innate");
920 	if (value_exists("cast_spell")) skip_value("cast_spell");
921 
922 	if (section_exists("blows"))
923 	{
924 		__try( start_section_read("blows") );
925 		while(value_exists("blow")) skip_value("blow");
926 		__try( end_section_read("blows") );
927 	}
928 	if (section_exists("flags"))
929 	{
930 		__try( start_section_read("flags") );
931 		while(value_exists("flag")) skip_value("flag");
932 		__try( end_section_read("flags") );
933 	}
934 
935 	/* Read the "Racial" monster limit per level */
936 	__try( read_byte("max_num", &r_ptr->max_num) );
937 
938 	if (value_exists("killer")) skip_value("killer");
939 
940 	__try( end_section_read("lore") );
941 
942 	return (TRUE);
943 }
944 
945 
946 
947 
948 /*
949  * Read a store
950  */
rd_store(int n)951 static bool rd_store(int n)
952 {
953 #undef __try
954 #define __try(X) if (!(X)) { return (FALSE); }
955 
956 	store_type *st_ptr = &store[n];
957 
958 	int j;
959 
960 	byte own, num;
961 
962 	__try( start_section_read("store") );
963 
964 	/* Read the basic info */
965 	__try( read_hturn("store_open", &st_ptr->store_open) );
966 	__try( read_short("insult_cur", &st_ptr->insult_cur) );
967 	__try( read_byte("owner", &own) );
968 	__try( read_byte("stock_num", &num) );
969 	__try( read_short("good_buy", &st_ptr->good_buy) );
970 	__try( read_short("bad_buy", &st_ptr->bad_buy) );
971 
972 	/* Extract the owner (see above) */
973 	st_ptr->owner = own;
974 
975 	__try( start_section_read("stock") );
976 	/* Read the items */
977 	for (j = 0; j < num; j++)
978 	{
979 		object_type forge;
980 
981 		/* Read the item */
982 		__try( rd_item(&forge) );
983 
984 		/* Acquire valid items */
985 		if (st_ptr->stock_num < STORE_INVEN_MAX)
986 		{
987 			/* Acquire the item */
988 			st_ptr->stock[st_ptr->stock_num++] = forge;
989 		}
990 	}
991 	__try( end_section_read("stock") );
992 	__try( end_section_read("store") );
993 
994 	/* Success */
995 	return (TRUE);
996 }
997 
998 
999 /*
1000  * Read some party info
1001 
1002  FOUND THE BUIG!!!!!! the disapearing party bug. no more.
1003  I hope......
1004 -APD-
1005  */
rd_party(int n)1006 static bool rd_party(int n)
1007 {
1008 #undef __try
1009 #define __try(X) if (!(X)) { return (FALSE); }
1010 
1011 	party_type *party_ptr = &parties[n];
1012 
1013 	__try( start_section_read("party") );
1014 
1015 	/* Party name */
1016 	__try( read_str("name",party_ptr->name) );
1017 
1018 	/* Party owner's name */
1019 	__try( read_str("owner",party_ptr->owner) );
1020 
1021 	/* Number of people and creation time */
1022 	__try( read_int("num", &party_ptr->num) );
1023 	__try( read_hturn("created", &party_ptr->created) );
1024 
1025 	__try( end_section_read("party") );
1026 
1027 	return (TRUE);
1028 }
1029 
1030 /*
1031  * Read some house info
1032  */
rd_house(int n)1033 static bool rd_house(int n)
1034 {
1035 #undef __try
1036 #define __try(X) if (!(X)) { return (FALSE); }
1037 
1038 	house_type *house_ptr = &houses[n];
1039 
1040 	__try( start_section_read("house") );
1041 
1042 	/* coordinates of corners of house */
1043 	__try( read_byte("x1", &house_ptr->x_1) );
1044 	__try( read_byte("y1", &house_ptr->y_1) );
1045 	__try( read_byte("x2", &house_ptr->x_2) );
1046 	__try( read_byte("y2", &house_ptr->y_2) );
1047 
1048 	/* coordinates of the house door */
1049 	__try( read_byte("door_y", &house_ptr->door_y) );
1050 	__try( read_byte("door_x", &house_ptr->door_x) );
1051 
1052 	/* Door Strength */
1053 	__try( read_byte("strength", &house_ptr->strength) );
1054 
1055 	/* Owned or not */
1056 	__try( read_str("owned", house_ptr->owned) );
1057 
1058 	__try( read_int("depth", &house_ptr->depth) );
1059 	__try( read_int("price", &house_ptr->price) );
1060 
1061 	__try( end_section_read("house") );
1062 
1063 	return (TRUE);
1064 }
1065 
1066 /*
1067  * Read some arena info
1068  */
rd_arena(int n)1069 static bool rd_arena(int n)
1070 {
1071 #undef __try
1072 #define __try(X) if (!(X)) { return (FALSE); }
1073 
1074 	arena_type *arena_ptr = &arenas[n];
1075 
1076 	__try( start_section_read("arena") );
1077 
1078 	/* coordinates of corners of house */
1079 	__try( read_byte("x1", &arena_ptr->x_1) );
1080 	__try( read_byte("y1", &arena_ptr->y_1) );
1081 	__try( read_byte("x2", &arena_ptr->x_2) );
1082 	__try( read_byte("y2", &arena_ptr->y_2) );
1083 
1084 	__try( read_int("depth", &arena_ptr->depth) );
1085 
1086 	__try( end_section_read("arena") );
1087 
1088 	return (TRUE);
1089 }
1090 
rd_wild(int n)1091 static bool rd_wild(int n)
1092 {
1093 #undef __try
1094 #define __try(X) if (!(X)) { return (FALSE); }
1095 
1096 	wilderness_type *w_ptr = &wild_info[-n];
1097 
1098 	/* the flags */
1099 	__try( read_short("flags", &w_ptr->flags) );
1100 
1101 	return (TRUE);
1102 }
1103 
1104 
1105 /*
1106  * Read/Write the "extra" information
1107  */
1108 
rd_extra(player_type * p_ptr,bool had_header)1109 static bool rd_extra(player_type *p_ptr, bool had_header)
1110 {
1111 #undef __try
1112 #define __try(X) if (!(X)) { return (FALSE); }
1113 
1114 	int i = 0;
1115 
1116 	__try( start_section_read("player") );
1117 
1118 	if (!had_header)
1119 	{
1120 		__try( read_str("playername",p_ptr->name) ); /* 32 */
1121 		skip_value("pass");
1122 	}
1123 
1124 	__try( read_str("died_from",p_ptr->died_from) ); /* 80 */
1125 
1126 	__try( read_str("died_from_list",p_ptr->died_from_list) ); /* 80 */
1127 	__try( read_short("died_from_depth", &p_ptr->died_from_depth) );
1128 
1129 	__try( start_section_read("history") );
1130 	for (i = 0; i < 4; i++)
1131 	{
1132 		__try( read_str("history",p_ptr->history[i]) ); /* 60 */
1133 	}
1134 	if (value_exists("descrip"))
1135 	{
1136 		__try( read_str("descrip",p_ptr->descrip) ); /* 240?! */
1137 	}
1138 	__try( end_section_read("history") );
1139 
1140 	/* Class/Race/Gender/Party */
1141 	if (!had_header)
1142 	{
1143 		__try( read_byte("prace", &p_ptr->prace) );
1144 		__try( read_byte("pclass", &p_ptr->pclass) );
1145 		__try( read_byte("male", &p_ptr->male) );
1146 	}
1147 	__try( read_byte("party", &p_ptr->party) );
1148 
1149 	/* Special Race/Class info */
1150 	__try( read_byte("hitdie", &p_ptr->hitdie) );
1151 	__try( read_short("expfact", &p_ptr->expfact) );
1152 
1153 	/* Age/Height/Weight */
1154 	__try( read_short("age", &p_ptr->age) );
1155 	__try( read_short("ht", &p_ptr->ht) );
1156 	__try( read_short("wt", &p_ptr->wt) );
1157 
1158 	/* Read the stat info */
1159 	__try( start_section_read("stats") );
1160 	for (i = 0; i < A_MAX; i++)
1161 	{
1162 		__try( read_short("stat_max", &p_ptr->stat_max[i]) );
1163 	}
1164 	for (i = 0; i < A_MAX; i++)
1165 	{
1166 		__try( read_short("stat_cur", &p_ptr->stat_cur[i]) );
1167 	}
1168 	__try( end_section_read("stats") );
1169 
1170 	__try( read_int("id", &p_ptr->id) );
1171 
1172 	/* If he was created in the pre-ID days, give him one */
1173 	if (!p_ptr->id)
1174 		p_ptr->id = player_id++;
1175 
1176 	__try( read_int("au", &p_ptr->au) );
1177 
1178 	__try( read_int("max_exp", &p_ptr->max_exp) );
1179 	__try( read_int("exp", &p_ptr->exp) );
1180 	__try( read_short("exp_frac", &p_ptr->exp_frac) );
1181 
1182 	__try( read_short("lev", &p_ptr->lev) );
1183 
1184 	__try( read_short("mhp", &p_ptr->mhp) );
1185 	__try( read_short("chp", &p_ptr->chp) );
1186 	__try( read_short("chp_frac", &p_ptr->chp_frac) );
1187 
1188 	__try( read_short("msp", &p_ptr->msp) );
1189 	__try( read_short("csp", &p_ptr->csp) );
1190 	__try( read_short("csp_frac", &p_ptr->csp_frac) );
1191 
1192 	if(value_exists("no_ghost"))
1193 	{
1194 		u32b tmp32u;
1195 		__try( read_int("no_ghost", &tmp32u) );
1196 	}
1197 
1198 	__try( read_short("max_plv", &p_ptr->max_plv) );
1199 	__try( read_short("max_dlv", &p_ptr->max_dlv) );
1200 
1201 	p_ptr->recall_depth = p_ptr->max_dlv;
1202 
1203 	__try( read_short("py", &p_ptr->py) );
1204 	__try( read_short("px", &p_ptr->px) );
1205 	__try( read_short("dun_depth", &p_ptr->dun_depth) );
1206 
1207 	__try( read_short("world_x", &p_ptr->world_x) );
1208 	__try( read_short("world_y", &p_ptr->world_y) );
1209 
1210 	/* More info */
1211 	__try( read_short("ghost", &p_ptr->ghost) );
1212 	__try( read_short("sc", &p_ptr->sc) );
1213 	__try( read_short("fruit_bat", &p_ptr->fruit_bat) );
1214 
1215 	/* Read the flags */
1216 	__try( read_byte("lives", &p_ptr->lives) );
1217 
1218 	/* hack */
1219 	__try( read_short("blind", &p_ptr->blind) );
1220 	__try( read_short("paralyzed", &p_ptr->paralyzed) );
1221 	__try( read_short("confused", &p_ptr->confused) );
1222 	__try( read_short("food", &p_ptr->food) );
1223 	__try( read_uint("energy", &p_ptr->energy) );
1224 	__try( read_short("fast", &p_ptr->fast) );
1225 	__try( read_short("slow", &p_ptr->slow) );
1226 	__try( read_short("afraid", &p_ptr->afraid) );
1227 	__try( read_short("cut", &p_ptr->cut) );
1228 	__try( read_short("stun", &p_ptr->stun) );
1229 	__try( read_short("poisoned", &p_ptr->poisoned) );
1230 	__try( read_short("image", &p_ptr->image) );
1231 	__try( read_short("protevil", &p_ptr->protevil) );
1232 	__try( read_short("invuln", &p_ptr->invuln) );
1233 	__try( read_short("hero", &p_ptr->hero) );
1234 	__try( read_short("shero", &p_ptr->shero) );
1235 	__try( read_short("shield", &p_ptr->shield) );
1236 	__try( read_short("blessed", &p_ptr->blessed) );
1237 	__try( read_short("tim_invis", &p_ptr->tim_invis) );
1238 	__try( read_short("word_recall", &p_ptr->word_recall) );
1239 	__try( read_short("see_infra", &p_ptr->see_infra) );
1240 	__try( read_short("tim_infra", &p_ptr->tim_infra) );
1241 
1242 	__try( read_short("oppose_fire", &p_ptr->oppose_fire) );
1243 	__try( read_short("oppose_cold", &p_ptr->oppose_cold) );
1244 	__try( read_short("oppose_acid", &p_ptr->oppose_acid) );
1245 	__try( read_short("oppose_elec", &p_ptr->oppose_elec) );
1246 	__try( read_short("oppose_pois", &p_ptr->oppose_pois) );
1247 
1248 	__try( read_byte("confusing", &p_ptr->confusing) );
1249 	__try( read_byte("searching", &p_ptr->searching) );
1250 	__try( read_byte("maximize", &p_ptr->maximize) );
1251 	__try( read_byte("preserve", &p_ptr->preserve) );
1252 
1253 	/* Read the unique list info */
1254 	__try( start_section_read("uniques") );
1255 	for (i = 0; i < z_info->r_max; i++)
1256 	{
1257 		__try( read_short("unique", &p_ptr->r_killed[i]) );
1258 	}
1259 	__try( end_section_read("uniques") );
1260 
1261 	/* Special stuff */
1262 	__try( read_short("panic_save", &panic_save) );
1263 	__try( read_short("total_winner", &p_ptr->total_winner) );
1264 	__try( read_short("retire_timer", &p_ptr->retire_timer) );
1265 	__try( read_short("noscore", &p_ptr->noscore) );
1266 
1267 	/* Read "death" */
1268 	__try( read_byte("death", &p_ptr->death) );
1269 
1270 	__try( end_section_read("player") );
1271 
1272 	/* Success */
1273 	return (TRUE);
1274 }
1275 
1276 
1277 
1278 
1279 /*
1280  * Read the player inventory
1281  *
1282  * Note that the inventory changed in Angband 2.7.4.  Two extra
1283  * pack slots were added and the equipment was rearranged.  Note
1284  * that these two features combine when parsing old save-files, in
1285  * which items from the old "aux" slot are "carried", perhaps into
1286  * one of the two new "inventory" slots.
1287  *
1288  * Note that the inventory is "re-sorted" later by "dungeon()".
1289  */
rd_inventory(player_type * p_ptr)1290 static errr rd_inventory(player_type *p_ptr)
1291 {
1292 #undef __try
1293 #define __try(X) if (!(X)) { return (-1); }
1294 
1295 	int slot = 0;
1296 
1297 	object_type forge;
1298 
1299 	__try( start_section_read("inventory") );
1300 
1301 	/* No weight */
1302 	p_ptr->total_weight = 0;
1303 
1304 	/* No items */
1305 	p_ptr->inven_cnt = 0;
1306 	p_ptr->equip_cnt = 0;
1307 
1308 	/* Read until done */
1309 	while (1)
1310 	{
1311 		u16b n;
1312 
1313 		/* Get the next item index */
1314 		__try( read_short("inv_entry", &n) );
1315 
1316 		/* Nope, we reached the end */
1317 		if (n == 0xFFFF) break;
1318 
1319 		/* Read the item */
1320 		__try( rd_item(&forge) );
1321 
1322 		/* Hack -- verify item */
1323 		if (!forge.k_idx) return (53);
1324 
1325 		/* Mega-Hack -- Handle artifacts that aren't yet "created" */
1326 		if (true_artifact_p(&forge))
1327 		{
1328 			/* If this artifact isn't created, mark it as created */
1329 			/* Only if this isn't a "death" restore */
1330 			if (!a_info[forge.name1].cur_num && !p_ptr->death)
1331 				a_info[forge.name1].cur_num = 1;
1332 		}
1333 
1334 		/* Wield equipment */
1335 		if (n >= INVEN_WIELD)
1336 		{
1337 			/* Structure copy */
1338 			p_ptr->inventory[n] = forge;
1339 
1340 			/* Add the weight */
1341 			p_ptr->total_weight += (forge.number * forge.weight);
1342 
1343 			/* One more item */
1344 			p_ptr->equip_cnt++;
1345 		}
1346 
1347 		/* Warning -- backpack is full */
1348 		else if (p_ptr->inven_cnt == INVEN_PACK)
1349 		{
1350 			/* Oops */
1351 			/*note("Too many items in the inventory!");*/
1352 
1353 			/* Fail */
1354 			return (54);
1355 		}
1356 
1357 		/* Carry inventory */
1358 		else
1359 		{
1360 			/* Get a slot */
1361 			n = slot++;
1362 
1363 			/* Structure copy */
1364 			p_ptr->inventory[n] = forge;
1365 
1366 			/* Add the weight */
1367 			p_ptr->total_weight += (forge.number * forge.weight);
1368 
1369 			/* One more item */
1370 			p_ptr->inven_cnt++;
1371 		}
1372 	}
1373 
1374 	__try( end_section_read("inventory") );
1375 
1376 	/* Success */
1377 	return (0);
1378 }
1379 
1380 /*
1381  * Read the birth options
1382  */
rd_birthoptions(player_type * p_ptr)1383 static errr rd_birthoptions(player_type *p_ptr)
1384 {
1385 #undef __try
1386 #define __try(X) if (!(X)) { return (-1); }
1387 
1388 	s32b i, id;
1389 	u16b tmp16u, ind;
1390 
1391 	if (!section_exists("options"))
1392 	{
1393 		/* Fine, no options */
1394 		return (0);
1395 	}
1396 
1397 	/* Begin */
1398 	__try( start_section_read("options") );
1399 
1400 	/* Read number */
1401 	__try( read_short("num", &tmp16u) );
1402 
1403 	/* HACK -- compat for new birth options */
1404 	if (tmp16u == 2) tmp16u = 4;
1405 
1406 	/* Read each record */
1407 	id = 0;
1408 	for (i = 0; i < OPT_MAX; i++)
1409 	{
1410 		const option_type *opt_ptr = &option_info[i];
1411 
1412 		/* Real index is in the o_uid! */
1413 		ind = option_info[i].o_uid;
1414 
1415 		if (opt_ptr->o_page != 1) continue;
1416 
1417 		/* Next entry is what we expect */
1418 		if (value_exists(opt_ptr->o_text))
1419 		{
1420 			/* Read it */
1421 			u32b val;
1422 			__try( read_uint(opt_ptr->o_text, &val) );
1423 
1424 			/* Set it */
1425 			p_ptr->options[ind] = val ? TRUE : FALSE;
1426 		}
1427 		/* Hack -- compat for new birth options *//* REMOVE ME */
1428 		else if (ind == OPT_ENERGY_BUILDUP || ind == OPT_MONSTER_RECOIL)
1429 		{
1430 			p_ptr->options[ind] = FALSE;
1431 		}
1432 		else
1433 		{
1434 			__try( end_section_read("options") );
1435 			/* Unexpected option */
1436 			return (29);
1437 		}
1438 
1439 		id++;
1440 		/* Don't read anymore */
1441 		if (id >= tmp16u) break;
1442 	}
1443 
1444 	/* Done */
1445 	__try( end_section_read("options") );
1446 
1447 	/* Success */
1448 	return (0);
1449 }
1450 
1451 
1452 /*
1453  * Read hostility information
1454  *
1455  * Note that this function is responsible for deleting stale entries
1456  */
rd_hostilities(player_type * p_ptr)1457 static errr rd_hostilities(player_type *p_ptr)
1458 {
1459 #undef __try
1460 #define __try(X) if (!(X)) { return (-1); }
1461 
1462 	hostile_type *h_ptr;
1463 	int i;
1464 	s32b id;
1465 	u16b tmp16u;
1466 
1467 	__try( start_section_read("hostilities") );
1468 
1469 	/* Read number */
1470 	__try( read_short("num", &tmp16u) );
1471 
1472 	/* Read available ID's */
1473 	for (i = 0; i < tmp16u; i++)
1474 	{
1475 		/* Read next ID */
1476 		__try( read_int("id", &id) );
1477 
1478 		/* Check for stale player */
1479 		if (id > 0 && !lookup_player_name(id)) continue;
1480 
1481 		/* Check for stale party */
1482 		if (id < 0 && !parties[0 - id].num) continue;
1483 
1484 		/* Create node */
1485 		MAKE(h_ptr, hostile_type);
1486 		h_ptr->id = id;
1487 
1488 		/* Add to chain */
1489 		h_ptr->next = p_ptr->hostile;
1490 		p_ptr->hostile = h_ptr;
1491 	}
1492 
1493 	__try( end_section_read("hostilities") );
1494 
1495 	/* Success */
1496 	return (0);
1497 }
1498 
1499 
1500 /*
1501  * Read the run-length encoded dungeon
1502  *
1503  * Note that this only reads the dungeon features and flags,
1504  * the objects and monsters will be read later.
1505  *
1506  */
1507 
rd_dungeon(bool ext,int Depth)1508 static bool rd_dungeon(bool ext, int Depth)
1509 {
1510 #undef __try
1511 #define __try(X) if (!(X)) { return (FALSE); }
1512 
1513 	s32b depth;
1514 	u16b max_y, max_x;
1515 
1516 	int y, x;
1517 	cave_type *c_ptr;
1518 	char cave_row[MAX_WID+1];
1519 
1520 	__try( start_section_read("dungeon_level") );
1521 
1522 	/*** Depth info ***/
1523 
1524 	/* Level info */
1525 	__try( read_int("depth", &depth) );
1526 	__try( read_short("max_height", &max_y) );
1527 	__try( read_short("max_width", &max_x) );
1528 	if (ext) depth = Depth;
1529 
1530 	/* Turn this level was generated */
1531 	if (value_exists("gen_turn"))
1532 	{
1533 		__try( read_hturn("gen_turn", &turn_cavegen[depth]) );
1534 	}
1535 
1536 	/* players on this depth */
1537 	__try( read_short("players_on_depth", &players_on_depth[depth]) );
1538 
1539 	/* Hack -- only read in staircase information for non-wilderness
1540 	 * levels
1541 	 */
1542 
1543 	if (depth >= 0)
1544 	{
1545 		__try( read_byte("level_up_y", &level_up_y[depth]) );
1546 		__try( read_byte("level_up_x", &level_up_x[depth]) );
1547 		__try( read_byte("level_down_y", &level_down_y[depth]) );
1548 		__try( read_byte("level_down_x", &level_down_x[depth]) );
1549 		__try( read_byte("level_rand_y", &level_rand_y[depth]) );
1550 		__try( read_byte("level_rand_x", &level_rand_x[depth]) );
1551 	}
1552 	/* HACK */
1553 	else if (value_exists("level_up_y"))
1554 	{
1555 		skip_value("level_up_y");
1556 		skip_value("level_up_x");
1557 		skip_value("level_down_y");
1558 		skip_value("level_down_x");
1559 		skip_value("level_rand_y");
1560 		skip_value("level_rand_x");
1561 	}
1562 
1563 	/* allocate the memory for the dungoen if it has not already */
1564 	/* been allocated - which it might have been if we are loading */
1565 	/* a special static level file */
1566 	if(!cave[depth])
1567 		alloc_dungeon_level(depth);
1568 
1569 	/* Load features */
1570 	__try( start_section_read("features") );
1571 
1572 		for (y = 0; y < max_y; y++)
1573 		{
1574 			__try( read_binary("row",cave_row,MAX_WID) );
1575 			for(x = 0; x < max_x; x++)
1576 			{
1577 				/* Access the cave */
1578 				c_ptr = &cave[depth][y][x];
1579 
1580 				/* set the feature */
1581 				c_ptr->feat = cave_row[x];
1582 			}
1583 		}
1584 
1585 	__try( end_section_read("features") );
1586 
1587 	/* Load info */
1588 	start_section_read("info");
1589 
1590 		for (y = 0; y < max_y; y++)
1591 		{
1592 			__try( read_binary("row",cave_row,MAX_WID) );
1593 			for(x = 0; x < max_x; x++)
1594 			{
1595 				/* Access the cave */
1596 				c_ptr = &cave[depth][y][x];
1597 
1598 				/* set the feature */
1599 				c_ptr->info = cave_row[x];
1600 			}
1601 		}
1602 
1603 	__try( end_section_read("info") );
1604 
1605 	__try( end_section_read("dungeon_level") );
1606 
1607 	/* Success */
1608 	return (TRUE);
1609 }
1610 
1611 /*
1612  * Read special static pre-designed dungeon levels
1613  *
1614  * Special pre-designed levels are stored in separate
1615  * files with the filename "server-level-<num>" where num is
1616  * the level number.  Level files are searched for at runtime
1617  * and loaded if present.
1618  */
rd_dungeon_special()1619 static errr rd_dungeon_special()
1620 {
1621 #undef __try
1622 #define __try(X) if (!(X)) { return (-1); }
1623 
1624 	bool ok = FALSE;
1625 
1626 	char filename[1024];
1627 	char levelname[32];
1628 	ang_file* fhandle;
1629 	ang_file* server_handle;
1630 	int i,num_levels,j=0,k=0;
1631 
1632 	/* Clear all the special levels */
1633 	for(i=0;i<MAX_SPECIAL_LEVELS;i++)
1634 	{
1635 		special_levels[i] = -999;
1636 	}
1637 
1638 	/* Vanilla Mangand doesn't have special static pre-designed levels */
1639 	if ((!cfg_ironman) && (!cfg_more_towns)) return 0;
1640 
1641 	num_levels = 0;
1642 	/* k = E/W, J = N/S for wilderness towns */
1643 /*	for(k=0;k<MAX_DEPTH;k++)
1644 	{
1645 	for(j=0;j<MAX_DEPTH;j++)
1646 	{*/
1647 	for(i=0;i<MAX_DEPTH;i++)
1648 	{
1649 		/* build a file name */
1650 		sprintf(levelname,"server.level.%i.%i.%i",k,j,i);
1651 		path_build(filename, 1024, ANGBAND_DIR_SAVE, levelname);
1652 		/* open the file if it exists */
1653 		fhandle = file_open(filename, MODE_READ, -1);
1654 		if(fhandle)
1655 		{
1656 			/* swap out the main file pointer for our level file */
1657 			server_handle = file_handle;
1658 			file_handle = fhandle;
1659 			/* load the level */
1660 			ok = rd_dungeon(FALSE, 0);
1661 			/* swap the file pointers back */
1662 			file_handle = server_handle;
1663 			/* close the level file */
1664 			file_close(fhandle);
1665 			/* we have an arbitrary max number of levels */
1666 			if(num_levels + 1 > MAX_SPECIAL_LEVELS)
1667 			{
1668 				note("Too many special pre-designed level files!");
1669 				break;
1670 			}
1671 			/* add this depth to the special level list */
1672 			special_levels[num_levels++] = i;
1673 		}
1674 	}
1675 /*	}
1676 	}*/
1677 	return ok ? 0 : -1;
1678 }
1679 
1680 /* HACK -- Read from file */
rd_dungeon_special_ext(int Depth,cptr levelname)1681 bool rd_dungeon_special_ext(int Depth, cptr levelname)
1682 {
1683 	bool ok = FALSE;
1684 	char filename[1024];
1685 	ang_file* fhandle;
1686 	ang_file* server_handle;
1687 
1688 	path_build(filename, 1024, ANGBAND_DIR_SAVE, levelname);
1689 
1690 	fhandle = file_open(filename, MODE_READ, -1);
1691 
1692 	if (fhandle)
1693 	{
1694 			/* swap out the main file pointer for our level file */
1695 			server_handle = file_handle;
1696 			file_handle = fhandle;
1697 
1698 			/* load the level */
1699 			ok = rd_dungeon(TRUE, Depth);
1700 
1701 			/* swap the file pointers back */
1702 			file_handle = server_handle;
1703 
1704 			/* close the level file */
1705 			file_close(fhandle);
1706 	}
1707 	return ok;
1708 }
1709 
1710 
1711 /* Reads in a players memory of the level he is currently on, in run-length encoded
1712  * format.  Simmilar to the above function.
1713  */
1714 
rd_cave_memory(player_type * p_ptr)1715 static bool rd_cave_memory(player_type *p_ptr)
1716 {
1717 #undef __try
1718 #define __try(X) if (!(X)) { return (FALSE); }
1719 
1720 	u16b max_y, max_x;
1721 	int y, x;
1722 	char cave_row[MAX_WID+1];
1723 
1724 	__try( start_section_read("cave_memory") );
1725 
1726 	/* Memory dimensions */
1727 	__try( read_short("max_height", &max_y) );
1728 	__try( read_short("max_width", &max_x) );
1729 
1730 	for (y = 0; y < max_y; y++)
1731 	{
1732 		__try( read_binary("row", cave_row, MAX_WID) );
1733 		for(x = 0; x < max_x; x++)
1734 		{
1735 			p_ptr->cave_flag[y][x] = cave_row[x];
1736 		}
1737 	}
1738 
1739 	__try( end_section_read("cave_memory") );
1740 
1741 	/* Success */
1742 	return (TRUE);
1743 }
1744 
1745 /* XXX XXX XXX
1746  * This function parses savefile as if it was a text file, searching for
1747  * "pass =" string. It ignores the 'xml' format for sake
1748  * of maintance simplicity (i.e. it doesn't care about savefile format
1749  * changes). It attempts to read out the stored password, and compares it
1750  * to the password provided in "pass_word". If it matches, the hashed
1751  * password stored back onto the "pass_word" buff, which is assumed to be
1752  * of MAX_CHARS length.
1753  *
1754  * Returns 0 on match, -1 on parsing error and -2 if password do not
1755  * match.
1756  *
1757  * See "scoop_player" in "save.c" for more info.
1758  */
rd_savefile_new_scoop_aux(char * sfile,char * pass_word)1759 errr rd_savefile_new_scoop_aux(char *sfile, char *pass_word)
1760 {
1761 	errr err;
1762 
1763 	char pass[80];
1764 	char temp[80];
1765 	char temp2[80];
1766 
1767 	char *read;
1768 
1769 	bool read_pass = FALSE;
1770 
1771 	char buf[1024];
1772 
1773 	/* The savefile is a text file */
1774 	file_handle = file_open(sfile, MODE_READ, -1);
1775 
1776 	/* Paranoia */
1777 	if (!file_handle) return (-1);
1778 
1779 	/* Try to fetch the data */
1780 	while (file_getl(file_handle, buf, 1024))
1781 	{
1782 		read = strtok(buf, " \t=");
1783 		if (!strcmp(read, "pass"))
1784 		{
1785 			read = strtok(NULL, " \t\n=");
1786 			my_strcpy(pass, read, 80);
1787 			read_pass = TRUE;
1788 			continue;
1789 		}
1790 		if (read_pass) break;
1791 	}
1792 
1793 	/* Paranoia */
1794 	temp[0] = '\0';
1795 
1796 	/* Here's where we do our password encryption handling */
1797 	my_strcpy(temp, (const char *)pass, 80);
1798 	MD5Password(temp); /* The hashed version of our stored password */
1799 	my_strcpy(temp2, (const char *)pass_word, 80);
1800 	MD5Password(temp2); /* The hashed version of password from client */
1801 
1802 	err = 0;
1803 
1804 	if (strstr(pass, "$1$"))
1805 	{ /* Most likely an MD5 hashed password saved */
1806 		if (strcmp(pass, pass_word))
1807 		{ /* No match, might be clear text from client */
1808 			if (strcmp(pass, temp2))
1809 			{
1810 				/* No, it's not correct */
1811 				err = -2;
1812 			}
1813 			/* Old style client, but OK otherwise */
1814 		}
1815 	}
1816 	else
1817 	{ /* Most likely clear text password saved */
1818 		if (strstr(pass_word, "$1$"))
1819 		{ /* Most likely hashed password from new client */
1820 			if (strcmp(temp, pass_word))
1821 			{
1822 				/* No, it doesn't match hatched */
1823 				err = -2;
1824 			}
1825 		}
1826 		else
1827 		{ /* Most likely clear text from client as well */
1828 			if (strcmp(pass, pass_word))
1829 			{
1830 				/* No, it's not correct */
1831 				err = -2;
1832 			}
1833 		}
1834 		/* Good match with clear text, save the hashed */
1835 		my_strcpy(pass_word, (const char *)temp, MAX_CHARS);
1836 	}
1837 
1838 	/* Check for errors */
1839 	if (file_error(file_handle)) err = -1;
1840 
1841 	/* Close the file */
1842 	file_close(file_handle);
1843 
1844 	/* Result */
1845 	return (err);
1846 }
1847 
1848 /*
1849  * Actually read the savefile
1850  *
1851  */
rd_savefile_new_aux(player_type * p_ptr)1852 static errr rd_savefile_new_aux(player_type *p_ptr)
1853 {
1854 #undef __try
1855 #define __try(X) if (!(X)) { return (-1); }
1856 	int i;
1857 
1858 	u16b tmp16u;
1859 	u32b tmp32u;
1860 	bool clear = FALSE;
1861 	bool had_header = FALSE;
1862 	char stat_order_hack[6];
1863 
1864 	__try( start_section_read("mangband_player_save") );
1865 	__try( start_section_read("version") );
1866 	__try( read_int("major", &tmp32u) );
1867 	__try( read_int("minor", &tmp32u) );
1868 	__try( read_int("patch", &tmp32u) );
1869 	__try( end_section_read("version") );
1870 
1871 	if (section_exists("header"))
1872 	{
1873 		__try( start_section_read("header") );
1874 		had_header = TRUE;
1875 
1876 		__try( read_str("playername",p_ptr->name) ); /* 32 */
1877 
1878 		skip_value("pass");
1879 
1880 		__try( read_byte("prace", &p_ptr->prace) );
1881 		__try( read_byte("pclass", &p_ptr->pclass) );
1882 		__try( read_byte("male", &p_ptr->male) );
1883 
1884 		__try( read_binary("stat_order", stat_order_hack, A_MAX) );
1885 		for (i = 0; i < A_MAX; i++)
1886 			p_ptr->stat_order[i] = stat_order_hack[i];
1887 
1888 		__try( end_section_read("header") );
1889 	}
1890 
1891 	/* Operating system info */
1892 	__try( read_uint("sf_xtra", &sf_xtra) );
1893 
1894 	/* Time of savefile creation */
1895 	__try( read_uint("sf_when", &sf_when) );
1896 
1897 	/* Number of resurrections */
1898 	__try( read_short("sf_lives", &sf_lives) );
1899 
1900 	/* Number of times played */
1901 	__try( read_short("sf_saves", &sf_saves) );
1902 
1903 	/* Skip the turn info - if present */
1904 	__try( read_hturn("turn", &p_ptr->last_turn) );
1905 
1906 	/* Turn this character was born on */
1907 	if(value_exists("birth_turn"))
1908 	{
1909 		__try( read_hturn("birth_turn", &p_ptr->birth_turn) );
1910 	}
1911 	else
1912 		/* Disable character event logging if no birth turn */
1913 		ht_clr(&p_ptr->birth_turn);
1914 
1915 	/* Player turns (actually time spent playing) */
1916 	if(value_exists("player_turn"))
1917 	{
1918 		__try( read_hturn("player_turn", &p_ptr->turn) );
1919 	}
1920 	else
1921 		ht_clr(&p_ptr->turn);
1922 
1923 	/* Read birth options */
1924 	if (rd_birthoptions(p_ptr))
1925 	{
1926 		return (28);
1927 	}
1928 
1929 	/* Monster Memory */
1930 	if (section_exists("monster_lore")) {
1931 	__try( start_section_read("monster_lore") );
1932 	__try( read_short("max_r_idx", &tmp16u) );
1933 
1934 	/* Incompatible save files */
1935 	if (tmp16u > z_info->r_max)
1936 	{
1937 		note(format("Too many (%u) monster races!", tmp16u));
1938 		return (21);
1939 	}
1940 
1941 	/* Read the available records */
1942 	for (i = 0; i < tmp16u; i++)
1943 	{
1944 		/* Read the lore */
1945 		__try( rd_lore(p_ptr, i) );
1946 	}
1947 	__try( end_section_read("monster_lore") );
1948 	}
1949 
1950 	/* Object Memory */
1951 	__try( start_section_read("object_memory") );
1952 	__try( read_short("max_k_idx", &tmp16u) );
1953 
1954 	/* Incompatible save files */
1955 	if (tmp16u > z_info->k_max)
1956 	{
1957 		note(format("Too many (%u) object kinds!", tmp16u));
1958 		return (22);
1959 	}
1960 
1961 	/* Read the object memory */
1962 	for (i = 0; i < tmp16u; i++)
1963 	{
1964 		byte tmp8u;
1965 
1966 		__try( read_byte("flags", &tmp8u) );
1967 
1968 		p_ptr->kind_aware[i] = (tmp8u & 0x01) ? TRUE : FALSE;
1969 		p_ptr->kind_tried[i] = (tmp8u & 0x02) ? TRUE : FALSE;
1970 	}
1971 	__try( end_section_read("object_memory") );
1972 
1973 	/*if (arg_fiddle) note("Loaded Object Memory");*/
1974 
1975 	/* Read the extra stuff */
1976 	__try( rd_extra(p_ptr, had_header) );
1977 
1978 	/*if (arg_fiddle) note("Loaded extra information");*/
1979 
1980 
1981 	/* Read the player_hp array */
1982 	__try( start_section_read("hp") );
1983 	__try( read_short("py_max_level", &tmp16u) );
1984 
1985 	/* Read the player_hp array */
1986 	for (i = 0; i < tmp16u; i++)
1987 	{
1988 		__try( read_short("hp", &p_ptr->player_hp[i]) );
1989 	}
1990 	__try( end_section_read("hp") );
1991 
1992 
1993 	/* Important -- Initialize the race/class */
1994 	p_ptr->rp_ptr = &p_info[p_ptr->prace];
1995 	p_ptr->cp_ptr = &c_info[p_ptr->pclass];
1996 
1997 
1998 	/* Important -- Choose the magic info */
1999 	p_ptr->mp_ptr = &c_info[p_ptr->pclass].spells;
2000 
2001 
2002 	/* Read spell info */
2003 	if (section_exists("spell_flags"))
2004 	{
2005 		__try( start_section_read("spell_flags") );
2006 		for (i = 0; i < PY_MAX_SPELLS; i++)
2007 		{
2008 			__try( read_byte("flag", &p_ptr->spell_flags[i]) );
2009 		}
2010 		__try( end_section_read("spell_flags") );
2011 	}
2012 	else
2013 	{
2014 		/* Port spell flags from old format */
2015 		u32b spell_learned1, spell_learned2;
2016 		u32b spell_worked1, spell_worked2;
2017 		u32b spell_forgotten1, spell_forgotten2;
2018 		__try( read_uint("spell_learned1", &spell_learned1) );
2019 		__try( read_uint("spell_learned2", &spell_learned2) );
2020 		__try( read_uint("spell_worked1", &spell_worked1) );
2021 		__try( read_uint("spell_worked2", &spell_worked2) );
2022 		__try( read_uint("spell_forgotten1", &spell_forgotten1) );
2023 		__try( read_uint("spell_forgotten2", &spell_forgotten2) );
2024 		for (i = 0; i < PY_MAX_SPELLS; i++)
2025 		{
2026 			if ((i < 32) ?
2027 				(spell_forgotten1 & (1L << i)) :
2028 				(spell_forgotten2 & (1L << (i - 32))))
2029 			{
2030 				p_ptr->spell_flags[i] |= PY_SPELL_FORGOTTEN;
2031 			}
2032 			if ((i < 32) ?
2033 				(spell_learned1 & (1L << i)) :
2034 				(spell_learned2 & (1L << (i - 32))))
2035 			{
2036 				p_ptr->spell_flags[i] |= PY_SPELL_LEARNED;
2037 			}
2038 			if ((i < 32) ?
2039 				(spell_worked1 & (1L << i)) :
2040 				(spell_worked2 & (1L << (i - 32))))
2041 			{
2042 				p_ptr->spell_flags[i] |= PY_SPELL_WORKED;
2043 			}
2044 		}
2045 	}
2046 
2047 	__try( start_section_read("spell_order") );
2048 	for (i = 0; i < PY_MAX_SPELLS; i++)
2049 	{
2050 		__try( read_byte("order", &p_ptr->spell_order[i]) );
2051 	}
2052 	__try( end_section_read("spell_order") );
2053 
2054 	/* Read the inventory */
2055 	if (rd_inventory(p_ptr))
2056 	{
2057 		note("Unable to read inventory");
2058 		return (21);
2059 	}
2060 
2061 	/* Read hostility information if new enough */
2062 	if (rd_hostilities(p_ptr))
2063 	{
2064 		return (22);
2065 	}
2066 
2067 	__try( rd_cave_memory(p_ptr) );
2068 
2069 	/* read the wilderness map */
2070 	__try( start_section_read("wilderness") );
2071 	/* get the map size */
2072 	__try( read_int("max_wild", &tmp32u) );
2073 
2074 	/* if too many map entries */
2075 	if (tmp32u > MAX_WILD)
2076 	{
2077 		return 23;
2078 	}
2079 
2080 	/* read in the map */
2081 	for (i = 0; i < tmp32u; i++)
2082 	{
2083 		__try( read_byte("wild_map", &p_ptr->wild_map[i]) );
2084 	}
2085 	__try( end_section_read("wilderness") );
2086 
2087 	/* Read the character event history */
2088 	if(section_exists("event_history"))
2089 	{
2090 		char buf[160];
2091 		cptr msg;
2092 		history_event evt;
2093 		history_event *last = NULL;
2094 		__try( start_section_read("event_history") );
2095 		while(value_exists("hist"))
2096 		{
2097 			int depth, level;
2098 			history_event *n_evt = NULL;
2099 			__try( read_str("hist", buf) );
2100 			if (sscanf(buf, "%02i:%02i:%02i   %4ift   %2i   ", &evt.days, &evt.hours, &evt.mins,
2101 				&depth, &level) == 5)
2102 			{
2103 				msg = &buf[25];/* skip 25 characters ^ */
2104 				evt.depth = depth / 50;
2105 				evt.message = quark_add(msg);
2106 			}
2107 			/* Allocate */
2108 			MAKE(n_evt, history_event);
2109 			n_evt->days = evt.days; n_evt->hours = evt.hours; n_evt->mins = evt.mins;
2110 			n_evt->depth = evt.depth; n_evt->level = level;
2111 			n_evt->message = evt.message;
2112 			/* Add to chain */
2113 			if (!last)
2114 			{
2115 				p_ptr->charhist = n_evt;
2116 				last = n_evt;
2117 			}
2118 			else
2119 			{
2120 				last->next = n_evt;
2121 				last = n_evt;
2122 			}
2123 		}
2124 		__try( end_section_read("event_history") );
2125 	}
2126 
2127 	/* Read the characters quest list */
2128 	if(section_exists("quests"))
2129 	{
2130 		__try( start_section_read("quests") );
2131 		__try( read_short("max_q_idx", &tmp16u) );
2132 		for(i = 0; i < MAX_Q_IDX; i++)
2133 		{
2134 			__try( read_short("level", &tmp16u) );
2135 			p_ptr->q_list[i].level = tmp16u;
2136 		}
2137 		__try( end_section_read("quests") );
2138 	}
2139 
2140 	/* Read the characters sold artifact list */
2141 	if(section_exists("found_artifacts"))
2142 	{
2143 		__try( start_section_read("found_artifacts") );
2144 		__try( read_short("max_a_idx", &tmp16u) );
2145 		tmp32u = tmp16u;
2146 		/* If we have an unexpected number of arts, just reset our list
2147 		 * of sold artifacts. It's not so important we want to break
2148 		 * save file compatability for it. */
2149 		if( tmp16u != z_info->a_max )
2150 		{
2151 			clear = TRUE;
2152 			tmp16u = 0;
2153 		}
2154 		for(i = 0; i < z_info->a_max; i++)
2155 		{
2156 			if(i < tmp32u)
2157 			{
2158 				if(!clear)
2159 				{
2160 					__try( read_short("a_info", &tmp16u) );
2161 				}
2162 			}
2163 			p_ptr->a_info[i] = tmp16u;
2164 		}
2165 		__try( end_section_read("found_artifacts") );
2166 	}
2167 
2168 	/* Hack -- no ghosts */
2169 	/* r_info[z_info->r_max - 1].max_num = 0; */
2170 
2171 	__try( end_section_read("mangband_player_save") );
2172 
2173 	/* Success */
2174 	return (0);
2175 }
2176 
2177 
2178 /*
2179  * Actually read the savefile
2180  *
2181  * Angband 2.8.0 will completely replace this code, see "save.c",
2182  * though this code will be kept to read pre-2.8.0 savefiles.
2183  */
rd_savefile_new(player_type * p_ptr)2184 errr rd_savefile_new(player_type *p_ptr)
2185 {
2186 	errr err;
2187 
2188 	/* The savefile is a text file */
2189 	file_handle = file_open(p_ptr->savefile, MODE_READ, -1);
2190 
2191 	/* Paranoia */
2192 	if (!file_handle) return (-1);
2193 
2194 	/* Call the sub-function */
2195 	err = rd_savefile_new_aux(p_ptr);
2196 
2197 	/* Check for errors */
2198 	if (file_error(file_handle)) err = -1;
2199 
2200 	/* Close the file */
2201 	file_close(file_handle);
2202 
2203 	/* Result */
2204 	return (err);
2205 }
2206 
rd_server_savefile()2207 errr rd_server_savefile()
2208 {
2209 #undef __try
2210 #define __try(X) if (!(X)) { exit(1); }
2211 #define __tryN(X) if ((X)) { exit(1); }
2212 #define __tryR(X, RET) if (!(X)) { return (RET); }
2213 
2214         int i;
2215 
2216 	errr err = 0;
2217 
2218 	char savefile[1024];
2219 
2220 	byte tmp8u;
2221         u16b tmp16u;
2222         u32b tmp32u;
2223 	s32b tmp32s;
2224 	int major;
2225 	char name[80];
2226 
2227 	/* Savefile name */
2228 	path_build(savefile, 1024, ANGBAND_DIR_SAVE, "server");
2229 
2230 	/* The server savefile is a binary file */
2231 	file_handle = file_open(savefile, MODE_READ, -1);
2232 	line_counter = 0;
2233 
2234 
2235 	__try( start_section_read("mangband_server_save") );
2236 	__try( start_section_read("version") );
2237 	__try( read_int("major", &major) );
2238 	__try( read_int("minor", &major) );
2239 	__try( read_int("patch", &major) );
2240 	__try( end_section_read("version") );
2241 
2242 	/* Paranoia */
2243 	if (!file_handle) return (-1);
2244 
2245         /* Clear the checksums */
2246         v_check = 0L;
2247         x_check = 0L;
2248 
2249         /* Operating system info */
2250 	__try( read_uint("xtra", &sf_xtra) );
2251 
2252         /* Time of savefile creation */
2253 	__try( read_int("timestamp", &sf_when) );
2254 
2255         /* Number of lives */
2256 	__try( read_short("sf_lives", &sf_lives) );
2257 
2258         /* Number of times played */
2259 	__try( read_short("sf_saves", &sf_saves) );
2260 
2261         /* Monster Memory */
2262 	__try( start_section_read("monster_lore") );
2263 	__try( read_short("max_r_idx", &tmp16u) );
2264 
2265         /* Incompatible save files */
2266         if (tmp16u > z_info->r_max)
2267         {
2268                 note(format("Too many (%u) monster races!", tmp16u));
2269                 return (21);
2270         }
2271 
2272         /* Read the available records */
2273         for (i = 0; i < tmp16u; i++)
2274         {
2275 		monster_race *r_ptr;
2276 
2277                 /* Read the lore */
2278 		 __try( rd_u_lore(i) );
2279 
2280 		/* Access the monster race */
2281 		r_ptr = &r_info[i];
2282 
2283         }
2284 
2285 	__try( end_section_read("monster_lore") );
2286 
2287         /* Load the Artifacts */
2288 	__try( start_section_read("artifacts") );
2289 	__try( read_short("max_a_idx", &tmp16u) );
2290 
2291         /* Incompatible save files */
2292         if (tmp16u > z_info->a_max)
2293         {
2294                 note(format("Too many (%u) artifacts!", tmp16u));
2295                 return (24);
2296         }
2297 
2298         /* Read the artifact flags */
2299         for (i = 0; i < tmp16u; i++)
2300         {
2301 		__try( read_byte("artifact", &tmp8u) );
2302                 a_info[i].cur_num = tmp8u;
2303 		/* Owner information */
2304 		if (value_exists("owner_name"))
2305 		{
2306 			__try( read_quark("owner_name", &a_info[i].owner_name) );
2307 			__try( read_int("owner_id", &a_info[i].owner_id) );
2308 		}
2309         }
2310 	__try( end_section_read("artifacts") );
2311 
2312 	/* Read the stores */
2313 	__try( start_section_read("stores") );
2314 	__try( read_short("max_stores", &tmp16u) );
2315 	for (i = 0; i < tmp16u; i++)
2316 	{
2317 		__tryR( rd_store(i), (22) );
2318 	}
2319 	__try( end_section_read("stores") );
2320 
2321 	/* Read party info if savefile is new enough */
2322 	__try( start_section_read("parties") );
2323 	__try( read_short("max_parties", &tmp16u) );
2324 
2325 		/* Incompatible save files */
2326 		if (tmp16u > MAX_PARTIES)
2327 		{
2328 			note(format("Too many (%u) parties!", tmp16u));
2329 			return (25);
2330 		}
2331 
2332 		/* Read the available records */
2333 		for (i = 0; i < tmp16u; i++)
2334 		{
2335 			__try( rd_party(i) );
2336 		}
2337 	__try( end_section_read("parties") );
2338 
2339 	/* XXX If new enough, read in the saved levels and monsters. */
2340 	__try( start_section_read("dungeon_levels") );
2341 		/* read the number of levels to be loaded */
2342 		__try( read_uint("num_levels", &tmp32u) );
2343 		/* load the levels */
2344 		for (i = 0; i < tmp32u; i++)
2345 		{
2346 			__try( rd_dungeon(FALSE, 0) );
2347 		}
2348 		/* load any special static levels */
2349 		__tryN( rd_dungeon_special() );
2350 	__try( end_section_read("dungeon_levels") );
2351 
2352 	__try( start_section_read("monsters") );
2353 		/* get the number of monsters to be loaded */
2354 		__try( read_int("max_monsters", &tmp32u) );
2355 		if (tmp32u > MAX_M_IDX)
2356 		{
2357 			note(format("Too many (%u) monsters!", tmp16u));
2358 			return (29);
2359 		}
2360 		/* load the monsters */
2361 		for (i = 1; i < tmp32u; i++)
2362 		{
2363 			__try( rd_monster(&m_list[m_pop()]) );
2364 		}
2365 	__try( end_section_read("monsters") );
2366 
2367 	/* Read object info */
2368 	__try( start_section_read("objects") );
2369 	__try( read_short("max_objects", &tmp16u) );
2370 
2371 		/* Incompatible save files */
2372 		if (tmp16u > MAX_O_IDX)
2373 		{
2374 			note(format("Too many (%u) objects!", tmp16u));
2375 			return (26);
2376 		}
2377 
2378 		/* Read the available records */
2379 		for (i = 1; i < tmp16u; i++)
2380 		{
2381 			__try( rd_item(&o_list[i]) );
2382 		}
2383 
2384 		/* Set the maximum object number */
2385 		o_max = tmp16u;
2386 	__try( end_section_read("objects") );
2387 
2388 		/* Read holding info */
2389 		/* Reacquire objects */
2390 		for (i = 1; i < o_max; ++i)
2391 		{
2392 			object_type *o_ptr;
2393 			monster_type *m_ptr;
2394 
2395 			/* Get the object */
2396 			o_ptr = &o_list[i];
2397 
2398 			/* Ignore dungeon objects */
2399 			if (!o_ptr->held_m_idx) continue;
2400 
2401 			/* Verify monster index */
2402 			if (o_ptr->held_m_idx > z_info->m_max)
2403 			{
2404 				note("Invalid monster index");
2405 				return (-1);
2406 			}
2407 
2408 			/* Get the monster */
2409 			m_ptr = &m_list[o_ptr->held_m_idx];
2410 
2411 			/* Link the object to the pile */
2412 			o_ptr->next_o_idx = m_ptr->hold_o_idx;
2413 
2414 			/* Link the monster to the object */
2415 			m_ptr->hold_o_idx = i;
2416 		}
2417 
2418 		/* Read house info */
2419 	__try( start_section_read("houses") );
2420 	__try( read_short("num_houses", &tmp16u) );
2421 
2422 		/* Incompatible save files */
2423 		if (tmp16u > MAX_HOUSES)
2424 		{
2425 			note(format("Too many (%u) houses!", tmp16u));
2426 			return (27);
2427 		}
2428 
2429 		/* Read the available records */
2430 		for (i = 0; i < tmp16u; i++)
2431 		{
2432 			__try( rd_house(i) );
2433 		}
2434 		num_houses = tmp16u;
2435 		__try( end_section_read("houses") );
2436 
2437 		/* Read arenas info */
2438 		if (section_exists("arenas"))
2439 		{
2440 			__try( start_section_read("arenas") );
2441 			__try( read_short("num_arenas", &tmp16u) );
2442 
2443 			/* Incompatible save files */
2444 			if (tmp16u > MAX_ARENAS)
2445 			{
2446 				note(format("Too many (%u) arenas!", tmp16u));
2447 				return (27);
2448 			}
2449 
2450 			/* Read the available records */
2451 			for (i = 0; i < tmp16u; i++)
2452 			{
2453 				__try( rd_arena(i) );
2454 			}
2455 			num_arenas = tmp16u;
2456 			__try( end_section_read("arenas") );
2457 		}
2458 
2459 	/* Read wilderness info */
2460 	__try( start_section_read("wilderness") );
2461 	/* read how many wilderness levels */
2462 	__try( read_int("max_wild", &tmp32u) );
2463 
2464 		if (tmp32u > MAX_WILD)
2465 		{
2466 			note("Too many wilderness levels");
2467 			return 28;
2468 		}
2469 
2470 		for (i = 1; i < tmp32u; i++)
2471 		{
2472 			__try( rd_wild(i) );
2473 		}
2474 		__try( end_section_read("wilderness") );
2475 
2476 	/* Read the player name database  */
2477 	__try( start_section_read("player_names") );
2478 	__try( read_int("num_players", &tmp32u) );
2479 
2480 		/* Read the available records */
2481 		for (i = 0; i < tmp32u; i++)
2482 		{
2483 			__try( start_section_read("player") );
2484 
2485 			/* Read the ID */
2486 			__try( read_int("id", &tmp32s) );
2487 
2488 			/* Read the player name */
2489 			__try( read_str("name", name) );
2490 
2491 			/* Store the player name */
2492 			add_player_name(name, tmp32s);
2493 
2494 			__try( end_section_read("player") );
2495 		}
2496 	__try( end_section_read("player_names") );
2497 
2498 
2499 	__try( read_uint("seed_flavor", &seed_flavor) );
2500 	__try( read_uint("seed_town", &seed_town) );
2501 
2502 	__try( read_int("player_id", &player_id) );
2503 
2504 	__try( read_hturn("turn", &turn) );
2505 
2506         /* Hack -- no ghosts */
2507         /*r_info[z_info->r_max - 1].max_num = 0;*/
2508 
2509 	__try( end_section_read("mangband_server_save") );
2510 
2511 	/* Check for errors */
2512 	if (file_error(file_handle)) err = -1;
2513 
2514 	/* Close the file */
2515 	file_close(file_handle);
2516 
2517 	/* Result */
2518 	return (err);
2519 }
2520 
2521