Freeciv-3.2
Loading...
Searching...
No Matches
savegame2.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14/*
15 This file includes the definition of a new savegame format introduced with
16 2.3.0. It is defined by the mandatory option '+version2'. The main load
17 function checks if this option is present. If not, the old (pre-2.3.0)
18 loading routines are used.
19 The format version is also saved in the settings section of the savefile, as an
20 integer (savefile.version). The integer is used to determine the version
21 of the savefile.
22
23 Structure of this file:
24
25 - The real work is done by savegame2_load().
26 This function call all submodules (settings, players, etc.)
27
28 - The remaining part of this file is split into several sections:
29 * helper functions
30 * load functions for all submodules (and their subsubmodules)
31
32 - If possible, all functions for load submodules should exit in
33 pairs named sg_load_<submodule>. If one is not
34 needed please add a comment why.
35
36 - The submodules can be further divided as:
37 sg_load_<submodule>_<subsubmodule>
38
39 - If needed (due to static variables in the *.c files) these functions
40 can be located in the corresponding source files (as done for the settings
41 and the event_cache).
42
43 Loading a savegame:
44
45 - The status of the process is saved within the static variable
46 'sg_success'. This variable is set to TRUE within savegame2_load().
47 If you encounter an error use sg_failure_*() to set it to FALSE and
48 return an error message. Furthermore, sg_check_* should be used at the
49 start of each (submodule) function to return if previous functions failed.
50
51 - While the loading process dependencies between different modules exits.
52 They can be handled within the struct loaddata *loading which is used as
53 first argument for all sg_load_*() function. Please indicate the
54 dependencies within the definition of this struct.
55
56*/
57
58#ifdef HAVE_CONFIG_H
59#include <fc_config.h>
60#endif
61
62#include <ctype.h>
63#include <stdarg.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67
68/* utility */
69#include "bitvector.h"
70#include "fcintl.h"
71#include "idex.h"
72#include "log.h"
73#include "mem.h"
74#include "rand.h"
75#include "registry.h"
76#include "shared.h"
77#include "support.h" /* bool type */
78#include "timing.h"
79
80/* common */
81#include "achievements.h"
82#include "ai.h"
83#include "bitvector.h"
84#include "capability.h"
85#include "citizens.h"
86#include "city.h"
87#include "game.h"
88#include "government.h"
89#include "map.h"
90#include "mapimg.h"
91#include "movement.h"
92#include "multipliers.h"
93#include "packets.h"
94#include "research.h"
95#include "rgbcolor.h"
96#include "specialist.h"
97#include "unit.h"
98#include "unitlist.h"
99#include "version.h"
100
101/* server */
102#include "barbarian.h"
103#include "citizenshand.h"
104#include "citytools.h"
105#include "cityturn.h"
106#include "diplhand.h"
107#include "maphand.h"
108#include "meta.h"
109#include "notify.h"
110#include "plrhand.h"
111#include "report.h"
112#include "ruleset.h"
113#include "sanitycheck.h"
114#include "savecompat.h"
115#include "score.h"
116#include "settings.h"
117#include "spacerace.h"
118#include "srv_main.h"
119#include "stdinhand.h"
120#include "techtools.h"
121#include "unittools.h"
122
123/* server/advisors */
124#include "advdata.h"
125#include "advbuilding.h"
126#include "infracache.h"
127
128/* server/generator */
129#include "mapgen.h"
130#include "mapgen_utils.h"
131
132/* server/scripting */
133#include "script_server.h"
134
135/* ai */
136#include "aitraits.h"
137#include "difficulty.h"
138
139#include "savegame2.h"
140
141extern bool sg_success;
142
143#define ACTIVITY_OLD_ROAD (ACTIVITY_LAST + 1)
144#define ACTIVITY_OLD_RAILROAD (ACTIVITY_LAST + 2)
145#define ACTIVITY_OLD_POLLUTION_SG2 (ACTIVITY_OLD_RAILROAD + 1)
146#define ACTIVITY_OLD_FALLOUT_SG2 (ACTIVITY_OLD_POLLUTION_SG2 + 1)
147#define ACTIVITY_LAST_SAVEGAME2 (ACTIVITY_OLD_FALLOUT_SG2 + 1)
148
149/*
150 * This loops over the entire map to save data. It collects all the data of
151 * a line using GET_XY_CHAR and then executes the macro SECFILE_INSERT_LINE.
152 *
153 * Parameters:
154 * ptile: current tile within the line (used by GET_XY_CHAR)
155 * GET_XY_CHAR: macro returning the map character for each position
156 * secfile: a secfile struct
157 * secpath, ...: path as used for sprintf() with arguments; the last item
158 * will be the y coordinate
159 * Example:
160 * SAVE_MAP_CHAR(ptile, terrain2char(ptile->terrain), file, "map.t%04d");
161 */
162#define SAVE_MAP_CHAR(ptile, GET_XY_CHAR, secfile, secpath, ...) \
163{ \
164 char _line[wld.map.xsize + 1]; \
165 int _nat_x, _nat_y; \
166 \
167 for (_nat_y = 0; _nat_y < wld.map.ysize; _nat_y++) { \
168 for (_nat_x = 0; _nat_x < wld.map.xsize; _nat_x++) { \
169 struct tile *ptile = native_pos_to_tile(&(wld.map), _nat_x, _nat_y); \
170 fc_assert_action(ptile != NULL, continue); \
171 _line[_nat_x] = (GET_XY_CHAR); \
172 sg_failure_ret(fc_isprint(_line[_nat_x] & 0x7f), \
173 "Trying to write invalid map data at position " \
174 "(%d, %d) for path %s: '%c' (%d)", _nat_x, _nat_y, \
175 secpath, _line[_nat_x], _line[_nat_x]); \
176 } \
177 _line[wld.map.xsize] = '\0'; \
178 secfile_insert_str(secfile, _line, secpath, ## __VA_ARGS__, _nat_y); \
179 } \
180}
181
182/*
183 * This loops over the entire map to load data. It inputs a line of data
184 * using the macro SECFILE_LOOKUP_LINE and then loops using the macro
185 * SET_XY_CHAR to load each char into the map at (map_x, map_y). Internal
186 * variables ch, map_x, map_y, nat_x, and nat_y are allocated within the
187 * macro but definable by the caller.
188 *
189 * Parameters:
190 * ch: a variable to hold a char (data for a single position,
191 * used by SET_XY_CHAR)
192 * ptile: current tile within the line (used by SET_XY_CHAR)
193 * SET_XY_CHAR: macro to load the map character at each (map_x, map_y)
194 * secfile: a secfile struct
195 * secpath, ...: path as used for sprintf() with arguments; the last item
196 * will be the y coordinate
197 * Example:
198 * LOAD_MAP_CHAR(ch, ptile,
199 * map_get_player_tile(ptile, plr)->terrain
200 * = char2terrain(ch), file, "player%d.map_t%04d", plrno);
201 *
202 * Note: some (but not all) of the code this is replacing used to skip over
203 * lines that did not exist. This allowed for backward-compatibility.
204 * We could add another parameter that specified whether it was OK to
205 * skip the data, but there's not really much advantage to exiting
206 * early in this case. Instead, we let any map data type to be empty,
207 * and just print an informative warning message about it.
208 */
209#define LOAD_MAP_CHAR(ch, ptile, SET_XY_CHAR, secfile, secpath, ...) \
210{ \
211 int _nat_x, _nat_y; \
212 bool _printed_warning = FALSE; \
213 for (_nat_y = 0; _nat_y < wld.map.ysize; _nat_y++) { \
214 const char *_line = secfile_lookup_str(secfile, secpath, \
215 ## __VA_ARGS__, _nat_y); \
216 if (NULL == _line) { \
217 char buf[64]; \
218 fc_snprintf(buf, sizeof(buf), secpath, ## __VA_ARGS__, _nat_y); \
219 log_verbose("Line not found='%s'", buf); \
220 _printed_warning = TRUE; \
221 continue; \
222 } else if (strlen(_line) != wld.map.xsize) { \
223 char buf[64]; \
224 fc_snprintf(buf, sizeof(buf), secpath, ## __VA_ARGS__, _nat_y); \
225 log_verbose("Line too short (expected %d got " SIZE_T_PRINTF \
226 ")='%s'", wld.map.xsize, strlen(_line), buf); \
227 _printed_warning = TRUE; \
228 continue; \
229 } \
230 for (_nat_x = 0; _nat_x < wld.map.xsize; _nat_x++) { \
231 const char ch = _line[_nat_x]; \
232 struct tile *ptile = native_pos_to_tile(&(wld.map), _nat_x, _nat_y); \
233 (SET_XY_CHAR); \
234 } \
235 } \
236 if (_printed_warning) { \
237 /* TRANS: Minor error message. */ \
238 log_sg(_("Saved game contains incomplete map data. This can" \
239 " happen with old saved games, or it may indicate an" \
240 " invalid saved game file. Proceed at your own risk.")); \
241 } \
242}
243
244/* Iterate on the extras half-bytes */
245#define halfbyte_iterate_extras(e, num_extras_types) \
246{ \
247 int e; \
248 for (e = 0; 4 * e < (num_extras_types); e++) {
249
250#define halfbyte_iterate_extras_end \
251 } \
252}
253
254/* Iterate on the specials half-bytes */
255#define halfbyte_iterate_special(s, num_specials_types) \
256{ \
257 enum tile_special_type s; \
258 for (s = 0; 4 * s < (num_specials_types); s++) {
259
260#define halfbyte_iterate_special_end \
261 } \
262}
263
264/* Iterate on the bases half-bytes */
265#define halfbyte_iterate_bases(b, num_bases_types) \
266{ \
267 int b; \
268 for (b = 0; 4 * b < (num_bases_types); b++) {
269
270#define halfbyte_iterate_bases_end \
271 } \
272}
273
274/* Iterate on the roads half-bytes */
275#define halfbyte_iterate_roads(r, num_roads_types) \
276{ \
277 int r; \
278 for (r = 0; 4 * r < (num_roads_types); r++) {
279
280#define halfbyte_iterate_roads_end \
281 } \
282}
283
284#define TOKEN_SIZE 10
285
286#define ORDER_OLD_BUILD_CITY (-1)
287#define ORDER_OLD_DISBAND (-2)
288#define ORDER_OLD_BUILD_WONDER (-3)
289#define ORDER_OLD_TRADE_ROUTE (-4)
290#define ORDER_OLD_HOMECITY (-5)
291
292static struct loaddata *loaddata_new(struct section_file *file);
293static void loaddata_destroy(struct loaddata *loading);
294
295static enum unit_orders char2order(char order);
296static enum direction8 char2dir(char dir);
297static char activity2char(int activity);
298static int char2activity(char activity);
299static int unquote_block(const char *const quoted_, void *dest,
300 int dest_length);
301static void worklist_load(struct section_file *file, int wlist_max_length,
302 struct worklist *pwl,
303 const char *path, ...);
304static void unit_ordering_apply(void);
305static void sg_extras_set_dbv(struct dbv *extras, char ch,
306 struct extra_type **idx);
307static void sg_extras_set_bv(bv_extras *extras, char ch,
308 struct extra_type **idx);
309static void sg_special_set_dbv(struct tile *ptile, struct dbv *extras, char ch,
310 const enum tile_special_type *idx,
311 bool rivers_overlay);
312static void sg_special_set_bv(struct tile *ptile, bv_extras *extras, char ch,
313 const enum tile_special_type *idx,
314 bool rivers_overlay);
315static void sg_bases_set_dbv(struct dbv *extras, char ch, struct base_type **idx);
316static void sg_bases_set_bv(bv_extras *extras, char ch, struct base_type **idx);
317static void sg_roads_set_dbv(struct dbv *extras, char ch, struct road_type **idx);
318static void sg_roads_set_bv(bv_extras *extras, char ch, struct road_type **idx);
319static struct extra_type *char2resource(char c);
320static struct terrain *char2terrain(char ch);
321static Tech_type_id technology_load(struct section_file *file,
322 const char *path, int plrno);
323
324static void sg_load_ruleset(struct loaddata *loading);
325static void sg_load_savefile(struct loaddata *loading);
326
327static void sg_load_game(struct loaddata *loading);
328
329static void sg_load_ruledata(struct loaddata *loading);
330
331static void sg_load_random(struct loaddata *loading);
332
333static void sg_load_script(struct loaddata *loading);
334
335static void sg_load_scenario(struct loaddata *loading);
336
337static void sg_load_settings(struct loaddata *loading);
338
339static void sg_load_map(struct loaddata *loading);
340static void sg_load_map_tiles(struct loaddata *loading);
341static void sg_load_map_tiles_extras(struct loaddata *loading);
342static void sg_load_map_tiles_bases(struct loaddata *loading);
343static void sg_load_map_tiles_roads(struct loaddata *loading);
345 bool rivers_overlay);
346static void sg_load_map_tiles_resources(struct loaddata *loading);
347
348static void sg_load_map_startpos(struct loaddata *loading);
349static void sg_load_map_owner(struct loaddata *loading);
350static void sg_load_map_worked(struct loaddata *loading);
351static void sg_load_map_known(struct loaddata *loading);
352
353static void sg_load_players_basic(struct loaddata *loading);
354static void sg_load_players(struct loaddata *loading);
355static void sg_load_player_main(struct loaddata *loading,
356 struct player *plr);
357static void sg_load_player_cities(struct loaddata *loading,
358 struct player *plr);
359static bool sg_load_player_city(struct loaddata *loading, struct player *plr,
360 struct city *pcity, const char *citystr,
361 int wlist_max_length);
363 struct player *plr,
364 struct city *pcity,
365 const char *citystr);
366static void sg_load_player_units(struct loaddata *loading,
367 struct player *plr);
368static bool sg_load_player_unit(struct loaddata *loading,
369 struct player *plr, struct unit *punit,
370 const char *unitstr);
372 struct player *plr);
373static void sg_load_player_attributes(struct loaddata *loading,
374 struct player *plr);
375static void sg_load_player_vision(struct loaddata *loading,
376 struct player *plr);
378 struct player *plr,
379 struct vision_site *pdcity,
380 const char *citystr);
381
382static void sg_load_researches(struct loaddata *loading);
383
384static void sg_load_event_cache(struct loaddata *loading);
385
386static void sg_load_treaties(struct loaddata *loading);
387
388static void sg_load_history(struct loaddata *loading);
389
390static void sg_load_mapimg(struct loaddata *loading);
391
392static void sg_load_sanitycheck(struct loaddata *loading);
393
394
395/* =======================================================================
396 * Basic load / save functions.
397 * ======================================================================= */
398
399/************************************************************************/
402void savegame2_load(struct section_file *file)
403{
404 struct loaddata *loading;
406
407 /* initialise loading */
412
413 /* Load the savegame data. */
414 /* Set up correct ruleset */
416 /* [compat] */
418 /* [savefile] */
420 /* [game] */
422 /* [scenario] */
424 /* [random] */
426 /* [settings] */
428 /* [ruledata] */
430 /* [players] (basic data) */
432 /* [map]; needs width and height loaded by [settings] */
434 /* [research] */
436 /* [player<i>] */
438 /* [event_cache] */
440 /* [treaties] */
442 /* [history] */
444 /* [mapimg] */
446 /* [script] -- must come last as may reference game objects */
448 /* [post_load_compat]; needs the game loaded by [savefile] */
450
451 /* Sanity checks for the loaded game. */
453
454 /* deinitialise loading */
458
459 if (!sg_success) {
460 log_error("Failure loading savegame!");
461 /* Try to get the server back to a vaguely sane state */
465 }
466}
467
468/************************************************************************/
471static struct loaddata *loaddata_new(struct section_file *file)
472{
473 struct loaddata *loading = calloc(1, sizeof(*loading));
474 loading->file = file;
475 loading->secfile_options = NULL;
476
477 loading->improvement.order = NULL;
478 loading->improvement.size = -1;
479 loading->technology.order = NULL;
480 loading->technology.size = -1;
481 loading->activities.order = NULL;
482 loading->activities.size = -1;
483 loading->trait.order = NULL;
484 loading->trait.size = -1;
485 loading->extra.order = NULL;
486 loading->extra.size = -1;
487 loading->multiplier.order = NULL;
488 loading->multiplier.size = -1;
489 loading->special.order = NULL;
490 loading->special.size = -1;
491 loading->base.order = NULL;
492 loading->base.size = -1;
493 loading->road.order = NULL;
494 loading->road.size = -1;
495 loading->specialist.order = NULL;
496 loading->specialist.size = -1;
497 loading->ds_t.order = NULL;
498 loading->ds_t.size = -1;
499 loading->coptions.order = NULL;
500 loading->coptions.size = -1;
501
502 loading->server_state = S_S_INITIAL;
503 loading->rstate = fc_rand_state();
504 loading->worked_tiles = NULL;
505
506 return loading;
507}
508
509/************************************************************************/
513{
514 if (loading->improvement.order != NULL) {
515 free(loading->improvement.order);
516 }
517
518 if (loading->technology.order != NULL) {
519 free(loading->technology.order);
520 }
521
522 if (loading->activities.order != NULL) {
523 free(loading->activities.order);
524 }
525
526 if (loading->trait.order != NULL) {
527 free(loading->trait.order);
528 }
529
530 if (loading->extra.order != NULL) {
531 free(loading->extra.order);
532 }
533
534 if (loading->multiplier.order != NULL) {
535 free(loading->multiplier.order);
536 }
537
538 if (loading->special.order != NULL) {
539 free(loading->special.order);
540 }
541
542 if (loading->base.order != NULL) {
543 free(loading->base.order);
544 }
545
546 if (loading->road.order != NULL) {
547 free(loading->road.order);
548 }
549
550 if (loading->specialist.order != NULL) {
551 free(loading->specialist.order);
552 }
553
554 if (loading->ds_t.order != NULL) {
555 free(loading->ds_t.order);
556 }
557
558 if (loading->coptions.order != NULL) {
559 free(loading->coptions.order);
560 }
561
562 if (loading->worked_tiles != NULL) {
563 free(loading->worked_tiles);
564 }
565
566 free(loading);
567}
568
569
570/************************************************************************/
579
580/************************************************************************/
589
590/* =======================================================================
591 * Helper functions.
592 * ======================================================================= */
593
594/************************************************************************/
597static enum unit_orders char2order(char order)
598{
599 switch (order) {
600 case 'm':
601 case 'M':
602 return ORDER_MOVE;
603 case 'w':
604 case 'W':
605 return ORDER_FULL_MP;
606 case 'b':
607 case 'B':
609 case 'a':
610 case 'A':
611 return ORDER_ACTIVITY;
612 case 'd':
613 case 'D':
614 return ORDER_OLD_DISBAND;
615 case 'u':
616 case 'U':
618 case 't':
619 case 'T':
621 case 'h':
622 case 'H':
623 return ORDER_OLD_HOMECITY;
624 case 'x':
625 case 'X':
626 return ORDER_ACTION_MOVE;
627 }
628
629 /* This can happen if the savegame is invalid. */
630 return ORDER_LAST;
631}
632
633/************************************************************************/
636static enum direction8 char2dir(char dir)
637{
638 /* Numberpad values for the directions. */
639 switch (dir) {
640 case '1':
641 return DIR8_SOUTHWEST;
642 case '2':
643 return DIR8_SOUTH;
644 case '3':
645 return DIR8_SOUTHEAST;
646 case '4':
647 return DIR8_WEST;
648 case '6':
649 return DIR8_EAST;
650 case '7':
651 return DIR8_NORTHWEST;
652 case '8':
653 return DIR8_NORTH;
654 case '9':
655 return DIR8_NORTHEAST;
656 }
657
658 /* This can happen if the savegame is invalid. */
659 return direction8_invalid();
660}
661
662/************************************************************************/
665static char activity2char(int activity)
666{
667 switch (activity) {
668 case ACTIVITY_IDLE:
669 return 'w';
670 case ACTIVITY_CLEAN:
671 return 'C';
673 return 'p';
675 return 'r';
676 case ACTIVITY_MINE:
677 return 'm';
679 return 'i';
681 return 'f';
682 case ACTIVITY_SENTRY:
683 return 's';
685 return 'l';
686 case ACTIVITY_PILLAGE:
687 return 'e';
688 case ACTIVITY_GOTO:
689 return 'g';
690 case ACTIVITY_EXPLORE:
691 return 'x';
693 return 'o';
695 return 'y';
697 return 'u';
698 case ACTIVITY_BASE:
699 return 'b';
701 return 'R';
702 case ACTIVITY_CONVERT:
703 return 'c';
705 case ACTIVITY_PLANT:
706 return '?';
707 case ACTIVITY_LAST:
708 break;
709 }
710
712
713 return '?';
714}
715
716/************************************************************************/
719static int char2activity(char activity)
720{
721 int a;
722
723 for (a = 0; a < ACTIVITY_LAST_SAVEGAME2; a++) {
724 char achar = activity2char(a);
725
726 if (activity == achar) {
727 return a;
728 }
729 }
730
731 /* This can happen if the savegame is invalid. */
732 return ACTIVITY_LAST;
733}
734
735/************************************************************************/
740static int unquote_block(const char *const quoted_, void *dest,
741 int dest_length)
742{
743 int i, length, parsed, tmp;
744 char *endptr;
745 const char *quoted = quoted_;
746
747 parsed = sscanf(quoted, "%d", &length);
748
749 if (parsed != 1) {
750 log_error(_("Syntax error in attribute block."));
751 return 0;
752 }
753
754 if (length > dest_length) {
755 return 0;
756 }
757
758 quoted = strchr(quoted, ':');
759
760 if (quoted == NULL) {
761 log_error(_("Syntax error in attribute block."));
762 return 0;
763 }
764
765 quoted++;
766
767 for (i = 0; i < length; i++) {
768 tmp = strtol(quoted, &endptr, 16);
769
770 if ((endptr - quoted) != 2
771 || *endptr != ' '
772 || (tmp & 0xff) != tmp) {
773 log_error(_("Syntax error in attribute block."));
774 return 0;
775 }
776
777 ((unsigned char *) dest)[i] = tmp;
778 quoted += 3;
779 }
780
781 return length;
782}
783
784/************************************************************************/
789 struct worklist *pwl, const char *path, ...)
790{
791 int i;
792 const char *kind;
793 const char *name;
794 char path_str[1024];
795 va_list ap;
796
797 /* The first part of the registry path is taken from the varargs to the
798 * function. */
799 va_start(ap, path);
800 fc_vsnprintf(path_str, sizeof(path_str), path, ap);
801 va_end(ap);
802
805 "%s.wl_length", path_str);
806
807 for (i = 0; i < pwl->length; i++) {
808 kind = secfile_lookup_str(file, "%s.wl_kind%d", path_str, i);
809
810 /* We lookup the production value by name. An invalid entry isn't a
811 * fatal error; we just truncate the worklist. */
812 name = secfile_lookup_str_default(file, "-", "%s.wl_value%d",
813 path_str, i);
814 pwl->entries[i] = universal_by_rule_name(kind, name);
815 if (pwl->entries[i].kind == universals_n_invalid()) {
816 log_sg("%s.wl_value%d: unknown \"%s\" \"%s\".", path_str, i, kind,
817 name);
818 pwl->length = i;
819 break;
820 }
821 }
822
823 /* Padding entries */
824 for (; i < wlist_max_length; i++) {
825 (void) secfile_entry_lookup(file, "%s.wl_kind%d", path_str, i);
826 (void) secfile_entry_lookup(file, "%s.wl_value%d", path_str, i);
827 }
828}
829
830/************************************************************************/
834static void unit_ordering_apply(void)
835{
836 players_iterate(pplayer) {
837 city_list_iterate(pplayer->cities, pcity) {
838 unit_list_sort_ord_city(pcity->units_supported);
839 }
842
843 whole_map_iterate(&(wld.map), ptile) {
844 unit_list_sort_ord_map(ptile->units);
846}
847
848/************************************************************************/
855static void sg_extras_set_dbv(struct dbv *extras, char ch,
856 struct extra_type **idx)
857{
858 int i, bin;
859 const char *pch = strchr(hex_chars, ch);
860
861 if (!pch || ch == '\0') {
862 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
863 bin = 0;
864 } else {
865 bin = pch - hex_chars;
866 }
867
868 for (i = 0; i < 4; i++) {
869 struct extra_type *pextra = idx[i];
870
871 if (pextra == NULL) {
872 continue;
873 }
874 if ((bin & (1 << i))
875 && (wld.map.server.have_huts || !is_extra_caused_by(pextra, EC_HUT))) {
876 dbv_set(extras, extra_index(pextra));
877 }
878 }
879}
880
881/************************************************************************/
889 struct extra_type **idx)
890{
891 int i, bin;
892 const char *pch = strchr(hex_chars, ch);
893
894 if (!pch || ch == '\0') {
895 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
896 bin = 0;
897 } else {
898 bin = pch - hex_chars;
899 }
900
901 for (i = 0; i < 4; i++) {
902 struct extra_type *pextra = idx[i];
903
904 if (pextra == NULL) {
905 continue;
906 }
907 if ((bin & (1 << i))
908 && (wld.map.server.have_huts || !is_extra_caused_by(pextra, EC_HUT))) {
909 BV_SET(*extras, extra_index(pextra));
910 }
911 }
912}
913
914/************************************************************************/
921static void sg_special_set_dbv(struct tile *ptile, struct dbv *extras, char ch,
922 const enum tile_special_type *idx,
923 bool rivers_overlay)
924{
925 int i, bin;
926 const char *pch = strchr(hex_chars, ch);
927
928 if (!pch || ch == '\0') {
929 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
930 bin = 0;
931 } else {
932 bin = pch - hex_chars;
933 }
934
935 for (i = 0; i < 4; i++) {
936 enum tile_special_type sp = idx[i];
937
938 if (sp == S_LAST) {
939 continue;
940 }
941 if (rivers_overlay && sp != S_OLD_RIVER) {
942 continue;
943 }
944
945 if (sp == S_HUT && !wld.map.server.have_huts) {
946 /* It would be logical to have this in the saving side -
947 * really not saving the huts in the first place, BUT
948 * 1) They have been saved by older versions, so we
949 * have to deal with such savegames.
950 * 2) This makes scenario author less likely to lose
951 * one's work completely after carefully placing huts
952 * and then saving with 'have_huts' disabled. */
953 continue;
954 }
955
956 if (bin & (1 << i)) {
957 if (sp == S_OLD_ROAD) {
958 struct road_type *proad;
959
961 if (proad) {
963 }
964 } else if (sp == S_OLD_RAILROAD) {
965 struct road_type *proad;
966
968 if (proad) {
970 }
971 } else if (sp == S_OLD_RIVER) {
972 struct road_type *proad;
973
975 if (proad) {
977 }
978 } else {
979 struct extra_type *pextra = NULL;
980 enum extra_cause cause = EC_COUNT;
981
982 /* Converting from old hardcoded specials to as sensible extra as we can */
983 switch (sp) {
984 case S_IRRIGATION:
985 case S_FARMLAND:
986 /* If old savegame has both irrigation and farmland, EC_IRRIGATION
987 * gets applied twice, which hopefully has the correct result. */
988 cause = EC_IRRIGATION;
989 break;
990 case S_MINE:
991 cause = EC_MINE;
992 break;
993 case S_POLLUTION:
994 cause = EC_POLLUTION;
995 break;
996 case S_HUT:
997 cause = EC_HUT;
998 break;
999 case S_FALLOUT:
1000 cause = EC_FALLOUT;
1001 break;
1002 default:
1004 break;
1005 }
1006
1007 if (cause != EC_COUNT) {
1008 struct tile *vtile = tile_virtual_new(ptile);
1009 struct terrain *pterr = tile_terrain(vtile);
1010 const struct req_context tile_ctxt = { .tile = vtile };
1011
1012 /* Do not let the extras already set to the real tile mess with setup
1013 * of the player tiles if that's what we're doing. */
1014 dbv_to_bv(vtile->extras.vec, extras);
1015
1016 /* It's ok not to know which player or which unit originally built the extra -
1017 * in the rules used when specials were saved these could not have made any
1018 * difference. */
1019 /* Can't use next_extra_for_tile() as it works for buildable extras only. */
1020
1021 if ((cause != EC_IRRIGATION || pterr->irrigation_time != 0)
1022 && (cause != EC_MINE || pterr->mining_time != 0)
1023 && (cause != EC_BASE || pterr->base_time != 0)
1024 && (cause != EC_ROAD || pterr->road_time != 0)) {
1028 || tile_city(vtile) != NULL
1029 || extra_base_get(candidate)->border_sq <= 0)
1031 &candidate->reqs,
1032 RPT_POSSIBLE)) {
1033 pextra = candidate;
1034 break;
1035 }
1036 }
1038 }
1039
1041 }
1042
1043 if (pextra) {
1044 dbv_set(extras, extra_index(pextra));
1045 }
1046 }
1047 }
1048 }
1049}
1050
1051/************************************************************************/
1058static void sg_special_set_bv(struct tile *ptile, bv_extras *extras, char ch,
1059 const enum tile_special_type *idx,
1060 bool rivers_overlay)
1061{
1062 int i, bin;
1063 const char *pch = strchr(hex_chars, ch);
1064
1065 if (!pch || ch == '\0') {
1066 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1067 bin = 0;
1068 } else {
1069 bin = pch - hex_chars;
1070 }
1071
1072 for (i = 0; i < 4; i++) {
1073 enum tile_special_type sp = idx[i];
1074
1075 if (sp == S_LAST) {
1076 continue;
1077 }
1078 if (rivers_overlay && sp != S_OLD_RIVER) {
1079 continue;
1080 }
1081
1082 if (sp == S_HUT && !wld.map.server.have_huts) {
1083 /* It would be logical to have this in the saving side -
1084 * really not saving the huts in the first place, BUT
1085 * 1) They have been saved by older versions, so we
1086 * have to deal with such savegames.
1087 * 2) This makes scenario author less likely to lose
1088 * one's work completely after carefully placing huts
1089 * and then saving with 'have_huts' disabled. */
1090 continue;
1091 }
1092
1093 if (bin & (1 << i)) {
1094 if (sp == S_OLD_ROAD) {
1095 struct road_type *proad;
1096
1098 if (proad) {
1100 }
1101 } else if (sp == S_OLD_RAILROAD) {
1102 struct road_type *proad;
1103
1105 if (proad) {
1107 }
1108 } else if (sp == S_OLD_RIVER) {
1109 struct road_type *proad;
1110
1112 if (proad) {
1114 }
1115 } else {
1116 struct extra_type *pextra = NULL;
1117 enum extra_cause cause = EC_COUNT;
1118
1119 /* Converting from old hardcoded specials to as sensible extra as we can */
1120 switch (sp) {
1121 case S_IRRIGATION:
1122 case S_FARMLAND:
1123 /* If old savegame has both irrigation and farmland, EC_IRRIGATION
1124 * gets applied twice, which hopefully has the correct result. */
1125 cause = EC_IRRIGATION;
1126 break;
1127 case S_MINE:
1128 cause = EC_MINE;
1129 break;
1130 case S_POLLUTION:
1131 cause = EC_POLLUTION;
1132 break;
1133 case S_HUT:
1134 cause = EC_HUT;
1135 break;
1136 case S_FALLOUT:
1137 cause = EC_FALLOUT;
1138 break;
1139 default:
1141 break;
1142 }
1143
1144 if (cause != EC_COUNT) {
1145 struct tile *vtile = tile_virtual_new(ptile);
1146 struct terrain *pterr = tile_terrain(vtile);
1147 const struct req_context tile_ctxt = { .tile = vtile };
1148
1149 /* Do not let the extras already set to the real tile mess with setup
1150 * of the player tiles if that's what we're doing. */
1151 vtile->extras = *extras;
1152
1153 /* It's ok not to know which player or which unit originally built the extra -
1154 * in the rules used when specials were saved these could not have made any
1155 * difference. */
1156 /* Can't use next_extra_for_tile() as it works for buildable extras only. */
1157
1158 if ((cause != EC_IRRIGATION || pterr->irrigation_time != 0)
1159 && (cause != EC_MINE || pterr->mining_time != 0)
1160 && (cause != EC_BASE || pterr->base_time != 0)
1161 && (cause != EC_ROAD || pterr->road_time != 0)) {
1165 || tile_city(vtile) != NULL
1166 || extra_base_get(candidate)->border_sq <= 0)
1168 &candidate->reqs,
1169 RPT_POSSIBLE)) {
1170 pextra = candidate;
1171 break;
1172 }
1173 }
1175 }
1176
1178 }
1179
1180 if (pextra) {
1181 BV_SET(*extras, extra_index(pextra));
1182 }
1183 }
1184 }
1185 }
1186}
1187
1188/************************************************************************/
1195static void sg_bases_set_dbv(struct dbv *extras, char ch,
1196 struct base_type **idx)
1197{
1198 int i, bin;
1199 const char *pch = strchr(hex_chars, ch);
1200
1201 if (!pch || ch == '\0') {
1202 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1203 bin = 0;
1204 } else {
1205 bin = pch - hex_chars;
1206 }
1207
1208 for (i = 0; i < 4; i++) {
1209 struct base_type *pbase = idx[i];
1210
1211 if (pbase == NULL) {
1212 continue;
1213 }
1214 if (bin & (1 << i)) {
1216 }
1217 }
1218}
1219
1220/************************************************************************/
1227static void sg_bases_set_bv(bv_extras *extras, char ch, struct base_type **idx)
1228{
1229 int i, bin;
1230 const char *pch = strchr(hex_chars, ch);
1231
1232 if (!pch || ch == '\0') {
1233 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1234 bin = 0;
1235 } else {
1236 bin = pch - hex_chars;
1237 }
1238
1239 for (i = 0; i < 4; i++) {
1240 struct base_type *pbase = idx[i];
1241
1242 if (pbase == NULL) {
1243 continue;
1244 }
1245 if (bin & (1 << i)) {
1247 }
1248 }
1249}
1250
1251/************************************************************************/
1258static void sg_roads_set_dbv(struct dbv *extras, char ch, struct road_type **idx)
1259{
1260 int i, bin;
1261 const char *pch = strchr(hex_chars, ch);
1262
1263 if (!pch || ch == '\0') {
1264 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1265 bin = 0;
1266 } else {
1267 bin = pch - hex_chars;
1268 }
1269
1270 for (i = 0; i < 4; i++) {
1271 struct road_type *proad = idx[i];
1272
1273 if (proad == NULL) {
1274 continue;
1275 }
1276 if (bin & (1 << i)) {
1278 }
1279 }
1280}
1281
1282/************************************************************************/
1289static void sg_roads_set_bv(bv_extras *extras, char ch, struct road_type **idx)
1290{
1291 int i, bin;
1292 const char *pch = strchr(hex_chars, ch);
1293
1294 if (!pch || ch == '\0') {
1295 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1296 bin = 0;
1297 } else {
1298 bin = pch - hex_chars;
1299 }
1300
1301 for (i = 0; i < 4; i++) {
1302 struct road_type *proad = idx[i];
1303
1304 if (proad == NULL) {
1305 continue;
1306 }
1307 if (bin & (1 << i)) {
1309 }
1310 }
1311}
1312
1313/************************************************************************/
1316static struct extra_type *char2resource(char c)
1317{
1318 /* speed common values */
1320 || c == RESOURCE_NONE_IDENTIFIER) {
1321 return NULL;
1322 }
1323
1324 return resource_by_identifier(c);
1325}
1326
1327/************************************************************************/
1331static struct terrain *char2terrain(char ch)
1332{
1333 /* terrain_by_identifier plus fatal error */
1335 return T_UNKNOWN;
1336 }
1337 terrain_type_iterate(pterrain) {
1338 if (pterrain->identifier == ch) {
1339 return pterrain;
1340 }
1342
1343 log_fatal("Unknown terrain identifier '%c' in savegame.", ch);
1344
1346
1348}
1349
1350/************************************************************************/
1354 const char *path, int plrno)
1355{
1356 char path_with_name[128];
1357 const char *name;
1358 struct advance *padvance;
1359
1361 "%s_name", path);
1362
1364
1365 if (!name || name[0] == '\0') {
1366 /* Used by researching_saved */
1367 return A_UNKNOWN;
1368 }
1369 if (fc_strcasecmp(name, "A_FUTURE") == 0) {
1370 return A_FUTURE;
1371 }
1372 if (fc_strcasecmp(name, "A_NONE") == 0) {
1373 return A_NONE;
1374 }
1375 if (fc_strcasecmp(name, "A_UNSET") == 0) {
1376 return A_UNSET;
1377 }
1378
1381 "%s: unknown technology \"%s\".", path_with_name, name);
1382
1383 return advance_number(padvance);
1384}
1385
1386/* =======================================================================
1387 * Load savefile data.
1388 * ======================================================================= */
1389
1390/************************************************************************/
1394{
1395 const char *ruleset = secfile_lookup_str_default(loading->file,
1397 "savefile.rulesetdir");
1398
1399 /* Load ruleset. */
1401 if (!strcmp("default", game.server.rulesetdir)) {
1402 int version;
1403
1404 version = secfile_lookup_int_default(loading->file, -1, "savefile.version");
1405 if (version >= 30) {
1406 /* Here 'default' really means current default.
1407 * Saving happens with real ruleset name, so savegames containing this
1408 * are special scenarios. */
1410 } else {
1411 /* 'default' is the old name of the classic ruleset */
1412 sz_strlcpy(game.server.rulesetdir, "classic");
1413 }
1414 log_verbose("Savegame specified ruleset '%s'. Really loading '%s'.",
1416 }
1418 /* Failed to load correct ruleset */
1419 sg_failure_ret(FALSE, _("Failed to load ruleset '%s' needed for savegame."),
1420 ruleset);
1421 }
1422}
1423
1424/************************************************************************/
1428{
1429 int i;
1430 const char *terr_name;
1431 const char *str;
1432
1433 /* Check status and return if not OK (sg_success FALSE). */
1434 sg_check_ret();
1435
1436 /* Load savefile options. */
1437 loading->secfile_options
1438 = secfile_lookup_str(loading->file, "savefile.options");
1439
1440 /* We don't need these entries, but read them anyway to avoid
1441 * warnings about unread secfile entries. */
1442 (void) secfile_entry_by_path(loading->file, "savefile.reason");
1443 (void) secfile_entry_by_path(loading->file, "savefile.revision");
1444
1445 str = secfile_lookup_str(loading->file, "savefile.orig_version");
1447
1448 /* In case of savegame2.c saves, missing entry means savegame older than support
1449 * for saving last_updated by turn. So this must default to TRUE. */
1451 "savefile.last_updated_as_year");
1452
1453 /* Load improvements. */
1454 loading->improvement.size
1456 "savefile.improvement_size");
1457 if (loading->improvement.size) {
1458 loading->improvement.order
1459 = secfile_lookup_str_vec(loading->file, &loading->improvement.size,
1460 "savefile.improvement_vector");
1461 sg_failure_ret(loading->improvement.size != 0,
1462 "Failed to load improvement order: %s",
1463 secfile_error());
1464 }
1465
1466 /* Load technologies. */
1467 loading->technology.size
1469 "savefile.technology_size");
1470 if (loading->technology.size) {
1471 loading->technology.order
1472 = secfile_lookup_str_vec(loading->file, &loading->technology.size,
1473 "savefile.technology_vector");
1474 sg_failure_ret(loading->technology.size != 0,
1475 "Failed to load technology order: %s",
1476 secfile_error());
1477 }
1478
1479 /* Load Activities. */
1480 loading->activities.size
1482 "savefile.activities_size");
1483 if (loading->activities.size) {
1484 loading->activities.order
1485 = secfile_lookup_str_vec(loading->file, &loading->activities.size,
1486 "savefile.activities_vector");
1487 sg_failure_ret(loading->activities.size != 0,
1488 "Failed to load activity order: %s",
1489 secfile_error());
1490 }
1491
1492 /* Load traits. */
1493 loading->trait.size
1495 "savefile.trait_size");
1496 if (loading->trait.size) {
1497 loading->trait.order
1498 = secfile_lookup_str_vec(loading->file, &loading->trait.size,
1499 "savefile.trait_vector");
1500 sg_failure_ret(loading->trait.size != 0,
1501 "Failed to load trait order: %s",
1502 secfile_error());
1503 }
1504
1505 /* Load extras. */
1506 loading->extra.size
1508 "savefile.extras_size");
1509 if (loading->extra.size) {
1510 const char **modname;
1511 size_t nmod;
1512 int j;
1513
1514 modname = secfile_lookup_str_vec(loading->file, &loading->extra.size,
1515 "savefile.extras_vector");
1516 sg_failure_ret(loading->extra.size != 0,
1517 "Failed to load extras order: %s",
1518 secfile_error());
1520 "Number of extras defined by the ruleset (= %d) are "
1521 "lower than the number in the savefile (= %d).",
1522 game.control.num_extra_types, (int)loading->extra.size);
1523 /* make sure that the size of the array is divisible by 4 */
1524 nmod = 4 * ((loading->extra.size + 3) / 4);
1525 loading->extra.order = fc_calloc(nmod, sizeof(*loading->extra.order));
1526 for (j = 0; j < loading->extra.size; j++) {
1527 loading->extra.order[j] = extra_type_by_rule_name(modname[j]);
1528 }
1529 free(modname);
1530 for (; j < nmod; j++) {
1531 loading->extra.order[j] = NULL;
1532 }
1533 }
1534
1535 /* Load multipliers. */
1536 loading->multiplier.size
1538 "savefile.multipliers_size");
1539 if (loading->multiplier.size) {
1540 const char **modname;
1541 int j;
1542
1543 modname = secfile_lookup_str_vec(loading->file, &loading->multiplier.size,
1544 "savefile.multipliers_vector");
1545 sg_failure_ret(loading->multiplier.size != 0,
1546 "Failed to load multipliers order: %s",
1547 secfile_error());
1548 /* It's OK for the set of multipliers in the savefile to differ
1549 * from those in the ruleset. */
1550 loading->multiplier.order = fc_calloc(loading->multiplier.size,
1551 sizeof(*loading->multiplier.order));
1552 for (j = 0; j < loading->multiplier.size; j++) {
1553 loading->multiplier.order[j] = multiplier_by_rule_name(modname[j]);
1554 if (!loading->multiplier.order[j]) {
1555 log_verbose("Multiplier \"%s\" in savegame but not in ruleset, "
1556 "discarding", modname[j]);
1557 }
1558 }
1559 free(modname);
1560 }
1561
1562 /* Load specials. */
1563 loading->special.size
1565 "savefile.specials_size");
1566 if (loading->special.size) {
1567 const char **modname;
1568 size_t nmod;
1569 enum tile_special_type j;
1570
1571 modname = secfile_lookup_str_vec(loading->file, &loading->special.size,
1572 "savefile.specials_vector");
1573 sg_failure_ret(loading->special.size != 0,
1574 "Failed to load specials order: %s",
1575 secfile_error());
1576 /* make sure that the size of the array is divisible by 4 */
1577 /* Allocating extra 4 slots, just a couple of bytes,
1578 * in case of special.size being divisible by 4 already is intentional.
1579 * Added complexity would cost those couple of bytes in code size alone,
1580 * and we actually need at least one slot immediately after last valid
1581 * one. That's where S_LAST is (or was in version that saved the game)
1582 * and in some cases S_LAST gets written to savegame, at least as
1583 * activity target special when activity targets some base or road
1584 * instead. By having current S_LAST in that index allows us to map
1585 * that old S_LAST to current S_LAST, just like any real special within
1586 * special.size gets mapped. */
1587 nmod = loading->special.size + (4 - (loading->special.size % 4));
1588 loading->special.order = fc_calloc(nmod,
1589 sizeof(*loading->special.order));
1590 for (j = 0; j < loading->special.size; j++) {
1591 if (!fc_strcasecmp("Road", modname[j])) {
1592 loading->special.order[j] = S_OLD_ROAD;
1593 } else if (!fc_strcasecmp("Railroad", modname[j])) {
1594 loading->special.order[j] = S_OLD_RAILROAD;
1595 } else if (!fc_strcasecmp("River", modname[j])) {
1596 loading->special.order[j] = S_OLD_RIVER;
1597 } else {
1598 loading->special.order[j] = special_by_rule_name(modname[j]);
1599 }
1600 }
1601 free(modname);
1602 for (; j < nmod; j++) {
1603 loading->special.order[j] = S_LAST;
1604 }
1605 }
1606
1607 /* Load bases. */
1608 loading->base.size
1610 "savefile.bases_size");
1611 if (loading->base.size) {
1612 const char **modname;
1613 size_t nmod;
1614 int j;
1615
1616 modname = secfile_lookup_str_vec(loading->file, &loading->base.size,
1617 "savefile.bases_vector");
1618 sg_failure_ret(loading->base.size != 0,
1619 "Failed to load bases order: %s",
1620 secfile_error());
1621 /* make sure that the size of the array is divisible by 4 */
1622 nmod = 4 * ((loading->base.size + 3) / 4);
1623 loading->base.order = fc_calloc(nmod, sizeof(*loading->base.order));
1624 for (j = 0; j < loading->base.size; j++) {
1625 struct extra_type *pextra = extra_type_by_rule_name(modname[j]);
1626
1627 sg_failure_ret(pextra != NULL
1628 || game.control.num_base_types >= loading->base.size,
1629 "Unknown base type %s in savefile.",
1630 modname[j]);
1631
1632 if (pextra != NULL) {
1633 loading->base.order[j] = extra_base_get(pextra);
1634 } else {
1635 loading->base.order[j] = NULL;
1636 }
1637 }
1638 free(modname);
1639 for (; j < nmod; j++) {
1640 loading->base.order[j] = NULL;
1641 }
1642 }
1643
1644 /* Load roads. */
1645 loading->road.size
1647 "savefile.roads_size");
1648 if (loading->road.size) {
1649 const char **modname;
1650 size_t nmod;
1651 int j;
1652
1653 modname = secfile_lookup_str_vec(loading->file, &loading->road.size,
1654 "savefile.roads_vector");
1655 sg_failure_ret(loading->road.size != 0,
1656 "Failed to load roads order: %s",
1657 secfile_error());
1659 "Number of roads defined by the ruleset (= %d) are "
1660 "lower than the number in the savefile (= %d).",
1661 game.control.num_road_types, (int)loading->road.size);
1662 /* make sure that the size of the array is divisible by 4 */
1663 nmod = 4 * ((loading->road.size + 3) / 4);
1664 loading->road.order = fc_calloc(nmod, sizeof(*loading->road.order));
1665 for (j = 0; j < loading->road.size; j++) {
1666 struct extra_type *pextra = extra_type_by_rule_name(modname[j]);
1667
1668 if (pextra != NULL) {
1669 loading->road.order[j] = extra_road_get(pextra);
1670 } else {
1671 loading->road.order[j] = NULL;
1672 }
1673 }
1674 free(modname);
1675 for (; j < nmod; j++) {
1676 loading->road.order[j] = NULL;
1677 }
1678 }
1679
1680 /* Load specialists. */
1681 loading->specialist.size
1683 "savefile.specialists_size");
1684 if (loading->specialist.size) {
1685 const char **modname;
1686 size_t nmod;
1687 int j;
1688
1689 modname = secfile_lookup_str_vec(loading->file, &loading->specialist.size,
1690 "savefile.specialists_vector");
1691 sg_failure_ret(loading->specialist.size != 0,
1692 "Failed to load specialists order: %s",
1693 secfile_error());
1695 "Number of specialists defined by the ruleset (= %d) are "
1696 "lower than the number in the savefile (= %d).",
1697 game.control.num_specialist_types, (int)loading->specialist.size);
1698 /* make sure that the size of the array is divisible by 4 */
1699 /* That's not really needed with specialists at the moment, but done this way
1700 * for consistency with other types, and to be prepared for the time it needs
1701 * to be this way. */
1702 nmod = 4 * ((loading->specialist.size + 3) / 4);
1703 loading->specialist.order = fc_calloc(nmod, sizeof(*loading->specialist.order));
1704 for (j = 0; j < loading->specialist.size; j++) {
1705 loading->specialist.order[j] = specialist_by_rule_name(modname[j]);
1706 }
1707 free(modname);
1708 for (; j < nmod; j++) {
1709 loading->specialist.order[j] = NULL;
1710 }
1711 }
1712
1713 /* Load diplomatic state type order. */
1714 loading->ds_t.size
1716 "savefile.diplstate_type_size");
1717
1718 sg_failure_ret(loading->ds_t.size > 0,
1719 "Failed to load diplomatic state type order: %s",
1720 secfile_error());
1721
1722 if (loading->ds_t.size) {
1723 const char **modname;
1724 int j;
1725
1726 modname = secfile_lookup_str_vec(loading->file, &loading->ds_t.size,
1727 "savefile.diplstate_type_vector");
1728
1729 loading->ds_t.order = fc_calloc(loading->ds_t.size,
1730 sizeof(*loading->ds_t.order));
1731
1732 for (j = 0; j < loading->ds_t.size; j++) {
1733 loading->ds_t.order[j] = diplstate_type_by_name(modname[j],
1735 }
1736
1737 free(modname);
1738 }
1739
1740 /* Load city options order. */
1741 loading->coptions.size
1743 "savefile.city_options_size");
1744
1745 {
1746 const char *modname_old[] = { "Disband", "Sci_Specialists", "Tax_Specialists" };
1747 const char **modname;
1748 int j;
1749 bool compat;
1750
1751 if (loading->coptions.size > 0) {
1752 modname = secfile_lookup_str_vec(loading->file, &loading->coptions.size,
1753 "savefile.city_options_vector");
1754 compat = FALSE;
1755 } else {
1757 loading->coptions.size = 3;
1758 compat = TRUE;
1759 }
1760
1761 loading->coptions.order = fc_calloc(loading->coptions.size,
1762 sizeof(*loading->coptions.order));
1763
1764 for (j = 0; j < loading->coptions.size; j++) {
1765 loading->coptions.order[j] = city_options_by_name(modname[j],
1767 }
1768
1769 if (!compat) {
1770 free(modname);
1771 }
1772 }
1773
1774 /* Terrain identifiers */
1776 pterr->identifier_load = '\0';
1778
1779 i = 0;
1781 "savefile.terrident%d.name", i)) != NULL) {
1783
1784 if (pterr != NULL) {
1785 const char *iptr = secfile_lookup_str_default(loading->file, NULL,
1786 "savefile.terrident%d.identifier", i);
1787
1788 pterr->identifier_load = *iptr;
1789 } else {
1790 log_error("Identifier for unknown terrain type %s.", terr_name);
1791 }
1792 i++;
1793 }
1794
1797 if (pterr != pterr2 && pterr->identifier_load != '\0') {
1798 sg_failure_ret((pterr->identifier_load != pterr2->identifier_load),
1799 "%s and %s share a saved identifier",
1801 }
1804}
1805
1806/* =======================================================================
1807 * Load game status.
1808 * ======================================================================= */
1809
1810/************************************************************************/
1814{
1815 int i;
1816 const char *name;
1817
1818 /* Check status and return if not OK (sg_success FALSE). */
1819 sg_check_ret();
1820
1821 for (i = 0;
1823 "ruledata.government%d.name", i));
1824 i++) {
1826
1827 if (gov != NULL) {
1829 "ruledata.government%d.changes", i);
1830 }
1831 }
1832}
1833
1834/************************************************************************/
1837static void sg_load_game(struct loaddata *loading)
1838{
1839 int game_version;
1840 const char *str;
1841 int i;
1842
1843 /* Check status and return if not OK (sg_success FALSE). */
1844 sg_check_ret();
1845
1846 /* Load version. */
1848 = secfile_lookup_int_default(loading->file, 0, "game.version");
1849 /* We require at least version 2.2.99 */
1850 sg_failure_ret(20299 <= game_version, "Saved game is too old, at least "
1851 "version 2.2.99 required.");
1852
1853 loading->full_version = game_version;
1854
1855 secfile_entry_ignore(loading->file, "scenario.game_version");
1856
1857 /* Load server state. */
1858 str = secfile_lookup_str_default(loading->file, "S_S_INITIAL",
1859 "game.server_state");
1860 loading->server_state = server_states_by_name(str, strcmp);
1861 if (!server_states_is_valid(loading->server_state)) {
1862 /* Don't take any risk! */
1863 loading->server_state = S_S_INITIAL;
1864 }
1865
1868 "game.meta_patches");
1870
1872 /* Do not overwrite this if the user requested a specific metaserver
1873 * from the command line (option --Metaserver). */
1877 "game.meta_server"));
1878 }
1879
1880 if ('\0' == srvarg.serverid[0]) {
1881 /* Do not overwrite this if the user requested a specific metaserver
1882 * from the command line (option --serverid). */
1885 "game.serverid"));
1886 }
1887 sz_strlcpy(server.game_identifier,
1888 secfile_lookup_str_default(loading->file, "", "game.id"));
1889 /* We are not checking game_identifier legality just yet.
1890 * That's done when we are sure that rand seed has been initialized,
1891 * so that we can generate new game_identifier, if needed.
1892 * See sq_load_sanitycheck(). */
1893
1896 "game.phase_mode");
1899 "game.phase_mode_stored");
1902 "game.phase");
1906 "game.scoreturn");
1907
1910 "game.timeoutint");
1913 "game.timeoutintinc");
1916 "game.timeoutinc");
1919 "game.timeoutincmult");
1922 "game.timeoutcounter");
1923
1924 game.info.turn
1925 = secfile_lookup_int_default(loading->file, 0, "game.turn");
1927 "game.year"), "%s", secfile_error());
1929 = secfile_lookup_bool_default(loading->file, FALSE, "game.year_0_hack");
1930
1932 = secfile_lookup_int_default(loading->file, 0, "game.globalwarming");
1934 = secfile_lookup_int_default(loading->file, 0, "game.heating");
1936 = secfile_lookup_int_default(loading->file, 0, "game.warminglevel");
1937
1939 = secfile_lookup_int_default(loading->file, 0, "game.nuclearwinter");
1941 = secfile_lookup_int_default(loading->file, 0, "game.cooling");
1943 = secfile_lookup_int_default(loading->file, 0, "game.coolinglevel");
1944
1945 /* Savegame may have stored random_seed for documentation purposes only,
1946 * but we want to keep it for resaving. */
1947 game.server.seed = secfile_lookup_int_default(loading->file, 0, "game.random_seed");
1948
1949 /* Global advances. */
1951 "game.global_advances");
1952 if (str != NULL) {
1953 sg_failure_ret(strlen(str) == loading->technology.size,
1954 "Invalid length of 'game.global_advances' ("
1955 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
1956 strlen(str), loading->technology.size);
1957 for (i = 0; i < loading->technology.size; i++) {
1958 sg_failure_ret(str[i] == '1' || str[i] == '0',
1959 "Undefined value '%c' within 'game.global_advances'.",
1960 str[i]);
1961 if (str[i] == '1') {
1962 struct advance *padvance =
1963 advance_by_rule_name(loading->technology.order[i]);
1964
1965 if (padvance != NULL) {
1967 }
1968 }
1969 }
1970 }
1971
1973 = !secfile_lookup_bool_default(loading->file, TRUE, "game.save_players");
1974
1976 = secfile_lookup_int_default(loading->file, 0, "game.last_turn_change_time") / 100.0;
1977}
1978
1979/* =======================================================================
1980 * Load random status.
1981 * ======================================================================= */
1982
1983/************************************************************************/
1986static void sg_load_random(struct loaddata *loading)
1987{
1988 /* Check status and return if not OK (sg_success FALSE). */
1989 sg_check_ret();
1990
1991 if (secfile_lookup_bool_default(loading->file, FALSE, "random.saved")) {
1992 const char *str;
1993 int i;
1994
1995 /* Since random state was previously saved, save it also when resaving.
1996 * This affects only pre-2.6 scenarios where scenario.save_random
1997 * is not defined.
1998 * - If this is 2.6 or later scenario -> it would have saved random.saved = TRUE
1999 * only if scenario.save_random is already TRUE
2000 *
2001 * Do NOT touch this in case of regular savegame. They always have random.saved
2002 * set, but if one starts to make scenario based on a savegame, we want
2003 * default scenario settings in the beginning (default save_random = FALSE).
2004 */
2007 }
2008
2010 "random.index_J"), "%s", secfile_error());
2012 "random.index_K"), "%s", secfile_error());
2014 "random.index_X"), "%s", secfile_error());
2015
2016 for (i = 0; i < 8; i++) {
2017 str = secfile_lookup_str(loading->file, "random.table%d",i);
2018 sg_failure_ret(NULL != str, "%s", secfile_error());
2019 sscanf(str, "%8x %8x %8x %8x %8x %8x %8x", &loading->rstate.v[7*i],
2020 &loading->rstate.v[7*i+1], &loading->rstate.v[7*i+2],
2021 &loading->rstate.v[7*i+3], &loading->rstate.v[7*i+4],
2022 &loading->rstate.v[7*i+5], &loading->rstate.v[7*i+6]);
2023 }
2024 loading->rstate.is_init = TRUE;
2025 fc_rand_set_state(loading->rstate);
2026 } else {
2027 /* No random values - mark the setting. */
2028 (void) secfile_entry_by_path(loading->file, "random.saved");
2029
2030 /* We're loading a game without a seed (which is okay, if it's a scenario).
2031 * We need to generate the game seed now because it will be needed later
2032 * during the load. */
2034 loading->rstate = fc_rand_state();
2035 }
2036}
2037
2038/* =======================================================================
2039 * Load lua script data.
2040 * ======================================================================= */
2041
2042/************************************************************************/
2045static void sg_load_script(struct loaddata *loading)
2046{
2047 /* Check status and return if not OK (sg_success FALSE). */
2048 sg_check_ret();
2049
2051}
2052
2053/* =======================================================================
2054 * Load scenario data.
2055 * ======================================================================= */
2056
2057/************************************************************************/
2061{
2062 const char *buf;
2063 bool lake_flood_default;
2064
2065 /* Check status and return if not OK (sg_success FALSE). */
2066 sg_check_ret();
2067
2068 if (NULL == secfile_section_lookup(loading->file, "scenario")) {
2070
2071 return;
2072 }
2073
2074 /* Default is that when there's scenario section (which we already checked)
2075 * this is a scenario. Only if it explicitly says that it's not, we consider
2076 * this regular savegame */
2077 game.scenario.is_scenario = secfile_lookup_bool_default(loading->file, TRUE, "scenario.is_scenario");
2078
2079 if (!game.scenario.is_scenario) {
2080 return;
2081 }
2082
2083 buf = secfile_lookup_str_default(loading->file, "", "scenario.name");
2084 if (buf[0] != '\0') {
2086 }
2087
2089 "scenario.authors");
2090 if (buf[0] != '\0') {
2092 } else {
2093 game.scenario.authors[0] = '\0';
2094 }
2095
2097 "scenario.description");
2098 if (buf[0] != '\0') {
2100 } else {
2101 game.scenario_desc.description[0] = '\0';
2102 }
2103
2105 = secfile_lookup_bool_default(loading->file, FALSE, "scenario.save_random");
2107 = secfile_lookup_bool_default(loading->file, TRUE, "scenario.players");
2110 "scenario.startpos_nations");
2111
2114 "scenario.prevent_new_cities");
2115 if (loading->version < 20599) {
2116 /* Lake flooding may break some old scenarios where rivers made out of
2117 * lake terrains, so play safe there */
2119 } else {
2120 /* If lake flooding is a problem for a newer scenario, it could explicitly
2121 * disable it. */
2123 }
2126 "scenario.lake_flooding");
2129 "scenario.handmade");
2132 "scenario.allow_ai_type_fallback");
2134
2135 sg_failure_ret(loading->server_state == S_S_INITIAL
2136 || (loading->server_state == S_S_RUNNING
2137 && game.scenario.players),
2138 "Invalid scenario definition (server state '%s' and "
2139 "players are %s).",
2140 server_states_name(loading->server_state),
2141 game.scenario.players ? "saved" : "not saved");
2142
2143 /* Remove all defined players. They are recreated with the skill level
2144 * defined by the scenario. */
2145 (void) aifill(0);
2146}
2147
2148/* =======================================================================
2149 * Load game settings.
2150 * ======================================================================= */
2151
2152/************************************************************************/
2156{
2157 /* Check status and return if not OK (sg_success FALSE). */
2158 sg_check_ret();
2159
2160 settings_game_load(loading->file, "settings");
2161
2162 /* Save current status of fogofwar. */
2164
2165 /* Add all compatibility settings here. */
2166}
2167
2168/* =======================================================================
2169 * Load the main map.
2170 * ======================================================================= */
2171
2172/************************************************************************/
2175static void sg_load_map(struct loaddata *loading)
2176{
2177 /* Check status and return if not OK (sg_success FALSE). */
2178 sg_check_ret();
2179
2180 /* This defaults to TRUE even if map has not been generated. Also,
2181 * old versions have also explicitly saved TRUE even in pre-game.
2182 * We rely on that
2183 * 1) scenario maps have it explicitly right.
2184 * 2) when map is actually generated, it re-initialize this to FALSE. */
2186 = secfile_lookup_bool_default(loading->file, TRUE, "map.have_huts");
2187
2188 /* Savegame may have stored random_seed for documentation purposes only,
2189 * but we want to keep it for resaving. */
2191 = secfile_lookup_int_default(loading->file, 0, "map.random_seed");
2192
2193 if (S_S_INITIAL == loading->server_state
2195 /* Generator MAPGEN_SCENARIO is used;
2196 * this map was done with the map editor. */
2197
2198 /* Load tiles. */
2201
2202 if (loading->version >= 30) {
2203 /* 2.6.0 or newer */
2205 } else {
2207 if (loading->version >= 20) {
2208 /* 2.5.0 or newer */
2210 }
2211 if (has_capability("specials", loading->secfile_options)) {
2212 /* Load specials. */
2214 }
2215 }
2216
2217 /* have_resources TRUE only if set so by sg_load_map_tiles_resources() */
2219 if (has_capability("specials", loading->secfile_options)) {
2220 /* Load resources. */
2222 } else if (has_capability("riversoverlay", loading->secfile_options)) {
2223 /* Load only rivers overlay. */
2225 }
2226
2227 /* Nothing more needed for a scenario. */
2228 secfile_entry_ignore(loading->file, "game.save_known");
2229
2230 return;
2231 }
2232
2233 if (S_S_INITIAL == loading->server_state) {
2234 /* Nothing more to do if it is not a scenario but in initial state. */
2235 return;
2236 }
2237
2240 if (loading->version >= 30) {
2241 /* 2.6.0 or newer */
2243 } else {
2245 if (loading->version >= 20) {
2246 /* 2.5.0 or newer */
2248 }
2250 }
2255}
2256
2257/************************************************************************/
2261{
2262 /* Check status and return if not OK (sg_success FALSE). */
2263 sg_check_ret();
2264
2265 /* Initialize the map for the current topology. 'map.xsize' and
2266 * 'map.ysize' must be set. */
2268
2269 /* Allocate map. */
2271
2272 /* get the terrain type */
2273 LOAD_MAP_CHAR(ch, ptile, ptile->terrain = char2terrain(ch), loading->file,
2274 "map.t%04d");
2276
2277 /* Check for special tile sprites. */
2278 whole_map_iterate(&(wld.map), ptile) {
2279 const char *spec_sprite;
2280 const char *label;
2281 int nat_x, nat_y;
2282
2284 spec_sprite = secfile_lookup_str(loading->file, "map.spec_sprite_%d_%d",
2285 nat_x, nat_y);
2286 label = secfile_lookup_str_default(loading->file, NULL, "map.label_%d_%d",
2287 nat_x, nat_y);
2288 if (NULL != ptile->spec_sprite) {
2289 ptile->spec_sprite = fc_strdup(spec_sprite);
2290 }
2291 if (label != NULL) {
2292 tile_set_label(ptile, label);
2293 }
2295}
2296
2297/************************************************************************/
2301{
2302 /* Check status and return if not OK (sg_success FALSE). */
2303 sg_check_ret();
2304
2305 /* Load extras. */
2306 halfbyte_iterate_extras(j, loading->extra.size) {
2307 LOAD_MAP_CHAR(ch, ptile, sg_extras_set_bv(&ptile->extras,
2308 ch, loading->extra.order + 4 * j),
2309 loading->file, "map.e%02d_%04d", j);
2311}
2312
2313/************************************************************************/
2317{
2318 /* Check status and return if not OK (sg_success FALSE). */
2319 sg_check_ret();
2320
2321 /* Load bases. */
2322 halfbyte_iterate_bases(j, loading->base.size) {
2323 LOAD_MAP_CHAR(ch, ptile, sg_bases_set_bv(&ptile->extras, ch,
2324 loading->base.order + 4 * j),
2325 loading->file, "map.b%02d_%04d", j);
2327}
2328
2329/************************************************************************/
2333{
2334 /* Check status and return if not OK (sg_success FALSE). */
2335 sg_check_ret();
2336
2337 /* Load roads. */
2338 halfbyte_iterate_roads(j, loading->road.size) {
2339 LOAD_MAP_CHAR(ch, ptile, sg_roads_set_bv(&ptile->extras, ch,
2340 loading->road.order + 4 * j),
2341 loading->file, "map.r%02d_%04d", j);
2343}
2344
2345/************************************************************************/
2349 bool rivers_overlay)
2350{
2351 /* Check status and return if not OK (sg_success FALSE). */
2352 sg_check_ret();
2353
2354 /* If 'rivers_overlay' is set to TRUE, load only the rivers overlay map
2355 * from the savegame file.
2356 *
2357 * A scenario may define the terrain of the map but not list the specials
2358 * on it (thus allowing users to control the placement of specials).
2359 * However rivers are a special case and must be included in the map along
2360 * with the scenario. Thus in those cases this function should be called
2361 * to load the river information separate from any other special data.
2362 *
2363 * This does not need to be called from map_load(), because map_load()
2364 * loads the rivers overlay along with the rest of the specials. Call this
2365 * only if you've already called map_load_tiles(), and want to load only
2366 * the rivers overlay but no other specials. Scenarios that encode things
2367 * this way should have the "riversoverlay" capability. */
2368 halfbyte_iterate_special(j, loading->special.size) {
2369 LOAD_MAP_CHAR(ch, ptile, sg_special_set_bv(ptile, &ptile->extras, ch,
2370 loading->special.order + 4 * j,
2372 loading->file, "map.spe%02d_%04d", j);
2374}
2375
2376/************************************************************************/
2380{
2381 /* Check status and return if not OK (sg_success FALSE). */
2382 sg_check_ret();
2383
2385 loading->file, "map.res%04d");
2386
2387 /* After the resources are loaded, indicate those currently valid. */
2388 whole_map_iterate(&(wld.map), ptile) {
2389 if (NULL == ptile->resource) {
2390 continue;
2391 }
2392
2393 if (ptile->terrain == NULL || !terrain_has_resource(ptile->terrain, ptile->resource)) {
2394 BV_CLR(ptile->extras, extra_index(ptile->resource));
2395 }
2397
2400}
2401
2402/************************************************************************/
2407{
2408 struct nation_type *pnation;
2409 struct startpos *psp;
2410 struct tile *ptile;
2411 const char SEPARATOR = '#';
2412 const char *nation_names;
2413 int nat_x, nat_y;
2414 bool exclude;
2415 int i, startpos_count;
2416
2417 /* Check status and return if not OK (sg_success FALSE). */
2418 sg_check_ret();
2419
2421 = secfile_lookup_int_default(loading->file, 0, "map.startpos_count");
2422
2423 if (0 == startpos_count) {
2424 /* Nothing to do. */
2425 return;
2426 }
2427
2428 for (i = 0; i < startpos_count; i++) {
2429 if (!secfile_lookup_int(loading->file, &nat_x, "map.startpos%d.x", i)
2430 || !secfile_lookup_int(loading->file, &nat_y,
2431 "map.startpos%d.y", i)) {
2432 log_sg("Warning: Undefined coordinates for startpos %d", i);
2433 continue;
2434 }
2435
2436 ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
2437 if (NULL == ptile) {
2438 log_error("Start position native coordinates (%d, %d) do not exist "
2439 "in this map. Skipping...", nat_x, nat_y);
2440 continue;
2441 }
2442
2443 exclude = secfile_lookup_bool_default(loading->file, FALSE,
2444 "map.startpos%d.exclude", i);
2445
2446 psp = map_startpos_new(ptile);
2447
2449 "map.startpos%d.nations", i);
2450 if (NULL != nation_names && '\0' != nation_names[0]) {
2451 const size_t size = strlen(nation_names) + 1;
2452 char buf[size], *start, *end;
2453
2455 for (start = buf - 1; NULL != start; start = end) {
2456 start++;
2457 if ((end = strchr(start, SEPARATOR))) {
2458 *end = '\0';
2459 }
2460
2461 pnation = nation_by_rule_name(start);
2462 if (NO_NATION_SELECTED != pnation) {
2463 if (exclude) {
2464 startpos_disallow(psp, pnation);
2465 } else {
2466 startpos_allow(psp, pnation);
2467 }
2468 } else {
2469 log_verbose("Missing nation \"%s\".", start);
2470 }
2471 }
2472 }
2473 }
2474
2475 if (0 < map_startpos_count()
2476 && loading->server_state == S_S_INITIAL
2478 log_verbose("Number of starts (%d) are lower than rules.max_players "
2479 "(%d), lowering rules.max_players.",
2482 }
2483
2484 /* Re-initialize nation availability in light of start positions.
2485 * This has to be after loading [scenario] and [map].startpos and
2486 * before we seek nations for players. */
2488}
2489
2490/************************************************************************/
2494{
2495 int x, y;
2496 struct player *owner = NULL;
2497 struct tile *claimer = NULL;
2498 struct player *eowner = NULL;
2499
2500 /* Check status and return if not OK (sg_success FALSE). */
2501 sg_check_ret();
2502
2503 if (game.info.is_new_game) {
2504 /* No owner/source information for a new game / scenario. */
2505 return;
2506 }
2507
2508 /* Owner and ownership source are stored as plain numbers */
2509 for (y = 0; y < wld.map.ysize; y++) {
2510 const char *buffer1 = secfile_lookup_str(loading->file,
2511 "map.owner%04d", y);
2512 const char *buffer2 = secfile_lookup_str(loading->file,
2513 "map.source%04d", y);
2514 const char *buffer3 = secfile_lookup_str(loading->file,
2515 "map.eowner%04d", y);
2516 const char *ptr1 = buffer1;
2517 const char *ptr2 = buffer2;
2518 const char *ptr3 = buffer3;
2519
2522 if (loading->version >= 30) {
2524 }
2525
2526 for (x = 0; x < wld.map.xsize; x++) {
2527 char token1[TOKEN_SIZE];
2528 char token2[TOKEN_SIZE];
2529 char token3[TOKEN_SIZE];
2530 int number;
2531 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
2532
2533 scanin(&ptr1, ",", token1, sizeof(token1));
2534 sg_failure_ret(token1[0] != '\0',
2535 "Map size not correct (map.owner%d).", y);
2536 if (strcmp(token1, "-") == 0) {
2537 owner = NULL;
2538 } else {
2540 "Got map owner %s in (%d, %d).", token1, x, y);
2541 owner = player_by_number(number);
2542 }
2543
2544 scanin(&ptr2, ",", token2, sizeof(token2));
2545 sg_failure_ret(token2[0] != '\0',
2546 "Map size not correct (map.source%d).", y);
2547 if (strcmp(token2, "-") == 0) {
2548 claimer = NULL;
2549 } else {
2551 "Got map source %s in (%d, %d).", token2, x, y);
2552 claimer = index_to_tile(&(wld.map), number);
2553 }
2554
2555 if (loading->version >= 30) {
2556 scanin(&ptr3, ",", token3, sizeof(token3));
2557 sg_failure_ret(token3[0] != '\0',
2558 "Map size not correct (map.eowner%d).", y);
2559 if (strcmp(token3, "-") == 0) {
2560 eowner = NULL;
2561 } else {
2563 "Got base owner %s in (%d, %d).", token3, x, y);
2564 eowner = player_by_number(number);
2565 }
2566 } else {
2567 eowner = owner;
2568 }
2569
2571 tile_claim_bases(ptile, eowner);
2572 log_debug("extras_owner(%d, %d) = %s", TILE_XY(ptile), player_name(eowner));
2573 }
2574 }
2575}
2576
2577/************************************************************************/
2581{
2582 int x, y;
2583
2584 /* Check status and return if not OK (sg_success FALSE). */
2585 sg_check_ret();
2586
2587 sg_failure_ret(loading->worked_tiles == NULL,
2588 "City worked map not loaded!");
2589
2590 loading->worked_tiles = fc_malloc(MAP_INDEX_SIZE *
2591 sizeof(*loading->worked_tiles));
2592
2593 for (y = 0; y < wld.map.ysize; y++) {
2594 const char *buffer = secfile_lookup_str(loading->file, "map.worked%04d",
2595 y);
2596 const char *ptr = buffer;
2597
2598 sg_failure_ret(NULL != buffer,
2599 "Savegame corrupt - map line %d not found.", y);
2600 for (x = 0; x < wld.map.xsize; x++) {
2601 char token[TOKEN_SIZE];
2602 int number;
2603 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
2604
2605 scanin(&ptr, ",", token, sizeof(token));
2606 sg_failure_ret('\0' != token[0],
2607 "Savegame corrupt - map size not correct.");
2608 if (strcmp(token, "-") == 0) {
2609 number = -1;
2610 } else {
2611 sg_failure_ret(str_to_int(token, &number) && 0 < number,
2612 "Savegame corrupt - got tile worked by city "
2613 "id=%s in (%d, %d).", token, x, y);
2614 }
2615
2616 loading->worked_tiles[ptile->index] = number;
2617 }
2618 }
2619}
2620
2621/************************************************************************/
2625{
2626 /* Check status and return if not OK (sg_success FALSE). */
2627 sg_check_ret();
2628
2629 players_iterate(pplayer) {
2630 /* Allocate player private map here; it is needed in different modules
2631 * besides this one ((i.e. sg_load_player_*()). */
2632 player_map_init(pplayer);
2634
2636 "game.save_known")) {
2637 int lines = player_slot_max_used_number() / 32 + 1;
2638 int j, p, l, i;
2639 unsigned int *known = fc_calloc(lines * MAP_INDEX_SIZE, sizeof(*known));
2640
2641 for (l = 0; l < lines; l++) {
2642 for (j = 0; j < 8; j++) {
2643 for (i = 0; i < 4; i++) {
2644 /* Only bother trying to load the map for this halfbyte if at least
2645 * one of the corresponding player slots is in use. */
2646 if (player_slot_is_used(player_slot_by_number(l*32 + j*4 + i))) {
2647 LOAD_MAP_CHAR(ch, ptile,
2648 known[l * MAP_INDEX_SIZE + tile_index(ptile)]
2649 |= ascii_hex2bin(ch, j),
2650 loading->file, "map.k%02d_%04d", l * 8 + j);
2651 break;
2652 }
2653 }
2654 }
2655 }
2656
2657 players_iterate(pplayer) {
2658 dbv_clr_all(&pplayer->tile_known);
2660
2661 /* HACK: we read the known data from hex into 32-bit integers, and
2662 * now we convert it to the known tile data of each player. */
2663 whole_map_iterate(&(wld.map), ptile) {
2664 players_iterate(pplayer) {
2665 p = player_index(pplayer);
2666 l = player_index(pplayer) / 32;
2667
2668 if (known[l * MAP_INDEX_SIZE + tile_index(ptile)] & (1u << (p % 32))) {
2669 map_set_known(ptile, pplayer);
2670 }
2673
2674 FC_FREE(known);
2675 }
2676}
2677
2678/* =======================================================================
2679 * Load player data.
2680 *
2681 * This is split into two parts as some data can only be loaded if the
2682 * number of players is known and the corresponding player slots are
2683 * defined.
2684 * ======================================================================= */
2685
2686/************************************************************************/
2690{
2691 int i, k, nplayers;
2692 const char *str;
2693 bool shuffle_loaded = TRUE;
2694
2695 /* Check status and return if not OK (sg_success FALSE). */
2696 sg_check_ret();
2697
2698 if (S_S_INITIAL == loading->server_state
2699 || game.info.is_new_game) {
2700 /* Nothing more to do. */
2701 return;
2702 }
2703
2704 /* Load destroyed wonders: */
2706 "players.destroyed_wonders");
2707 sg_failure_ret(str != NULL, "%s", secfile_error());
2708 sg_failure_ret(strlen(str) == loading->improvement.size,
2709 "Invalid length for 'players.destroyed_wonders' ("
2710 SIZE_T_PRINTF" ~= " SIZE_T_PRINTF ")",
2711 strlen(str), loading->improvement.size);
2712 for (k = 0; k < loading->improvement.size; k++) {
2713 sg_failure_ret(str[k] == '1' || str[k] == '0',
2714 "Undefined value '%c' within "
2715 "'players.destroyed_wonders'.", str[k]);
2716
2717 if (str[k] == '1') {
2718 struct impr_type *pimprove =
2719 improvement_by_rule_name(loading->improvement.order[k]);
2720
2721 if (pimprove) {
2724 }
2725 }
2726 }
2727
2728 server.identity_number
2729 = secfile_lookup_int_default(loading->file, server.identity_number,
2730 "players.identity_number_used");
2731
2732 /* First remove all defined players. */
2733 players_iterate(pplayer) {
2734 server_remove_player(pplayer);
2736
2737 /* Now, load the players from the savefile. */
2738 player_slots_iterate(pslot) {
2739 struct player *pplayer;
2740 struct rgbcolor *prgbcolor = NULL;
2741 int pslot_id = player_slot_index(pslot);
2742
2743 if (NULL == secfile_section_lookup(loading->file, "player%d",
2744 pslot_id)) {
2745 continue;
2746 }
2747
2748 /* Get player AI type. */
2749 str = secfile_lookup_str(loading->file, "player%d.ai_type",
2750 player_slot_index(pslot));
2751 sg_failure_ret(str != NULL, "%s", secfile_error());
2752
2753 /* Get player color */
2754 if (!rgbcolor_load(loading->file, &prgbcolor, "player%d.color",
2755 pslot_id)) {
2756 if (loading->version >= 10 && game_was_started()) {
2757 /* 2.4.0 or later savegame. This is not an error in 2.3 savefiles,
2758 * as they predate the introduction of configurable player colors. */
2759 log_sg("Game has started, yet player %d has no color defined.",
2760 pslot_id);
2761 /* This will be fixed up later */
2762 } else {
2763 log_verbose("No color defined for player %d.", pslot_id);
2764 /* Colors will be assigned on game start, or at end of savefile
2765 * loading if game has already started */
2766 }
2767 }
2768
2769 /* Create player. */
2770 pplayer = server_create_player(player_slot_index(pslot), str,
2771 prgbcolor,
2774 sg_failure_ret(pplayer != NULL, "Invalid AI type: '%s'!", str);
2775
2776 server_player_init(pplayer, FALSE, FALSE);
2777
2778 /* Free the color definition. */
2780
2781 /* Multipliers (policies) */
2782
2783 /* First initialise player values with ruleset defaults; this will
2784 * cover any in the ruleset not known when the savefile was created. */
2785 multipliers_iterate(pmul) {
2786 pplayer->multipliers[multiplier_index(pmul)].value
2787 = pplayer->multipliers[multiplier_index(pmul)].target = pmul->def;
2789
2790 /* Now override with any values from the savefile. */
2791 for (k = 0; k < loading->multiplier.size; k++) {
2792 const struct multiplier *pmul = loading->multiplier.order[k];
2793
2794 if (pmul) {
2796 int val =
2798 "player%d.multiplier%d.val",
2799 player_slot_index(pslot), k);
2800 int rval = (((CLIP(pmul->start, val, pmul->stop)
2801 - pmul->start) / pmul->step) * pmul->step) + pmul->start;
2802
2803 if (rval != val) {
2804 log_verbose("Player %d had illegal value for multiplier \"%s\": "
2805 "was %d, clamped to %d", pslot_id,
2806 multiplier_rule_name(pmul), val, rval);
2807 }
2808 pplayer->multipliers[idx].value = rval;
2809
2810 val =
2812 pplayer->multipliers[idx].value,
2813 "player%d.multiplier%d.target",
2814 player_slot_index(pslot), k);
2815 rval = (((CLIP(pmul->start, val, pmul->stop)
2816 - pmul->start) / pmul->step) * pmul->step) + pmul->start;
2817
2818 if (rval != val) {
2819 log_verbose("Player %d had illegal value for multiplier_target "
2820 "\"%s\": was %d, clamped to %d", pslot_id,
2821 multiplier_rule_name(pmul), val, rval);
2822 }
2823 pplayer->multipliers[idx].target = rval;
2824
2825 /* Never present in savegame2 format */
2826 pplayer->multipliers[idx].changed = 0;
2827 } /* else silently discard multiplier not in current ruleset */
2828 }
2829
2830 /* Just in case savecompat starts adding it in the future. */
2831 pplayer->server.border_vision =
2833 "player%d.border_vision",
2834 player_slot_index(pslot));
2836
2837 /* check number of players */
2838 nplayers = secfile_lookup_int_default(loading->file, 0, "players.nplayers");
2839 sg_failure_ret(player_count() == nplayers, "The value of players.nplayers "
2840 "(%d) from the loaded game does not match the number of "
2841 "players present (%d).", nplayers, player_count());
2842
2843 /* Load team information. */
2844 players_iterate(pplayer) {
2845 int team;
2846 struct team_slot *tslot = NULL;
2847
2849 "player%d.team_no",
2850 player_number(pplayer))
2852 "Invalid team definition for player %s (nb %d).",
2853 player_name(pplayer), player_number(pplayer));
2854 /* Should never fail when slot given is not NULL */
2855 team_add_player(pplayer, team_new(tslot));
2857
2858 /* Loading the shuffle list is quite complex. At the time of saving the
2859 * shuffle data is saved as
2860 * shuffled_player_<number> = player_slot_id
2861 * where number is an increasing number and player_slot_id is a number
2862 * between 0 and the maximum number of player slots. Now we have to create
2863 * a list
2864 * shuffler_players[number] = player_slot_id
2865 * where all player slot IDs are used exactly one time. The code below
2866 * handles this ... */
2867 if (secfile_lookup_int_default(loading->file, -1,
2868 "players.shuffled_player_%d", 0) >= 0) {
2869 int slots = player_slot_count();
2870 int plrcount = player_count();
2873
2874 for (i = 0; i < slots; i++) {
2875 /* Array to save used numbers. */
2877 /* List of all player IDs (needed for set_shuffled_players()). It is
2878 * initialised with the value -1 to indicate that no value is set. */
2879 shuffled_players[i] = -1;
2880 }
2881
2882 /* Load shuffled player list. */
2883 for (i = 0; i < plrcount; i++) {
2884 int shuffle
2886 "players.shuffled_player_%d", i);
2887
2888 if (shuffle == -1) {
2889 log_sg("Missing player shuffle information (index %d) "
2890 "- reshuffle player list!", i);
2892 break;
2893 } else if (shuffled_player_set[shuffle]) {
2894 log_sg("Player shuffle %d used two times "
2895 "- reshuffle player list!", shuffle);
2897 break;
2898 }
2899 /* Set this ID as used. */
2901
2902 /* Save the player ID in the shuffle list. */
2904 }
2905
2906 if (shuffle_loaded) {
2907 /* Insert missing numbers. */
2908 int shuffle_index = plrcount;
2909
2910 for (i = 0; i < slots; i++) {
2911 if (!shuffled_player_set[i]) {
2913 }
2914
2915 /* shuffle_index must not grow higher than size of shuffled_players. */
2917 "Invalid player shuffle data!");
2918 }
2919
2920#ifdef FREECIV_DEBUG
2921 log_debug("[load shuffle] player_count() = %d", player_count());
2922 player_slots_iterate(pslot) {
2923 int plrid = player_slot_index(pslot);
2924
2925 log_debug("[load shuffle] id: %3d => slot: %3d | slot %3d: %s",
2927 shuffled_player_set[plrid] ? "is used" : "-");
2929#endif /* FREECIV_DEBUG */
2930
2931 /* Set shuffle list from savegame. */
2933 }
2934 }
2935
2936 if (!shuffle_loaded) {
2937 /* No shuffled players included or error loading them, so shuffle them
2938 * (this may include scenarios). */
2940 }
2941}
2942
2943/************************************************************************/
2947{
2948 /* Check status and return if not OK (sg_success FALSE). */
2949 sg_check_ret();
2950
2951 if (game.info.is_new_game) {
2952 /* Nothing to do. */
2953 return;
2954 }
2955
2956 players_iterate(pplayer) {
2957 sg_load_player_main(loading, pplayer);
2959 sg_load_player_units(loading, pplayer);
2961
2962 /* Check the success of the functions above. */
2963 sg_check_ret();
2964
2965 /* Print out some information */
2966 if (is_ai(pplayer)) {
2967 log_normal(_("%s has been added as %s level AI-controlled player "
2968 "(%s)."), player_name(pplayer),
2969 ai_level_translated_name(pplayer->ai_common.skill_level),
2970 ai_name(pplayer->ai));
2971 } else {
2972 log_normal(_("%s has been added as human player."),
2973 player_name(pplayer));
2974 }
2976
2977 /* Also load the transport status of the units here. It must be a special
2978 * case as all units must be known (unit on an allied transporter). */
2979 players_iterate(pplayer) {
2980 /* Load unit transport status. */
2983
2984 /* Savegame may contain nation assignments that are incompatible with the
2985 * current nationset -- for instance, if it predates the introduction of
2986 * nationsets. Ensure they are compatible, one way or another. */
2988
2989 /* Some players may have invalid nations in the ruleset. Once all players
2990 * are loaded, pick one of the remaining nations for them. */
2991 players_iterate(pplayer) {
2992 if (pplayer->nation == NO_NATION_SELECTED) {
2995 /* TRANS: Minor error message: <Leader> ... <Poles>. */
2996 log_sg(_("%s had invalid nation; changing to %s."),
2997 player_name(pplayer), nation_plural_for_player(pplayer));
2998
2999 ai_traits_init(pplayer);
3000 }
3002
3003 /* Sanity check alliances, prevent allied-with-ally-of-enemy. */
3006 if (pplayers_allied(plr, aplayer)) {
3008 DS_ALLIANCE);
3009
3012 log_sg("Illegal alliance structure detected: "
3013 "%s alliance to %s reduced to peace treaty.",
3018 }
3019 }
3022
3023 /* Update cached city illness. This can depend on trade routes,
3024 * so can't be calculated until all players have been loaded. */
3025 if (game.info.illness_on) {
3026 cities_iterate(pcity) {
3027 pcity->server.illness
3028 = city_illness_calc(pcity, NULL, NULL,
3029 &(pcity->illness_trade), NULL);
3031 }
3032
3033 /* Update all city information. This must come after all cities are
3034 * loaded (in player_load) but before player (dumb) cities are loaded
3035 * in player_load_vision(). */
3036 players_iterate(plr) {
3037 city_list_iterate(plr->cities, pcity) {
3038 city_refresh(pcity);
3039 sanity_check_city(pcity);
3040 CALL_PLR_AI_FUNC(city_got, plr, plr, pcity);
3043
3044 /* Since the cities must be placed on the map to put them on the
3045 player map we do this afterwards */
3046 players_iterate(pplayer) {
3048 /* Check the success of the function above. */
3049 sg_check_ret();
3051
3052 /* Check shared vision. Shared tiles are never given in savegame2 save */
3053 players_iterate(pplayer) {
3054 BV_CLR_ALL(pplayer->gives_shared_vision);
3055 BV_CLR_ALL(pplayer->gives_shared_tiles);
3056 BV_CLR_ALL(pplayer->server.really_gives_vision);
3058
3059 /* Set up shared vision... */
3060 players_iterate(pplayer) {
3061 int plr1 = player_index(pplayer);
3062
3064 int plr2 = player_index(pplayer2);
3065
3067 "player%d.diplstate%d.gives_shared_vision", plr1, plr2)) {
3068 give_shared_vision(pplayer, pplayer2);
3069 }
3072
3073 /* ...and check it */
3076 /* TODO: Is there a good reason player is not marked as
3077 * giving shared vision to themselves -> really_gives_vision()
3078 * returning FALSE when pplayer1 == pplayer2 */
3079 if (pplayer1 != pplayer2
3082 sg_regr(3000900,
3083 _("%s did not give shared vision to team member %s."),
3086 }
3088 sg_regr(3000900,
3089 _("%s did not give shared vision to team member %s."),
3092 }
3093 }
3096
3099
3100 /* All vision is ready; this calls city_thaw_workers_queue(). */
3102
3103 /* Make sure everything is consistent. */
3104 players_iterate(pplayer) {
3105 unit_list_iterate(pplayer->units, punit) {
3107 struct tile *ptile = unit_tile(punit);
3108
3109 log_sg("%s doing illegal activity in savegame!",
3111 log_sg("Activity: %s, Target: %s, Tile: (%d, %d), Terrain: %s",
3115 : "missing",
3116 TILE_XY(ptile), terrain_rule_name(tile_terrain(ptile)));
3118 }
3121
3122 cities_iterate(pcity) {
3123 city_refresh(pcity);
3124 city_thaw_workers(pcity); /* may auto_arrange_workers() */
3126
3127 /* Player colors are always needed once game has started. Pre-2.4 savegames
3128 * lack them. This cannot be in compatibility conversion layer as we need
3129 * all the player data available to be able to assign best colors. */
3130 if (game_was_started()) {
3132 }
3133}
3134
3135/************************************************************************/
3139 struct player *plr)
3140{
3141 const char **slist;
3142 int i, plrno = player_number(plr);
3143 const char *str;
3144 struct government *gov;
3145 const char *level;
3146 const char *barb_str;
3147 size_t nval;
3148
3149 /* Check status and return if not OK (sg_success FALSE). */
3150 sg_check_ret();
3151
3152 /* Basic player data. */
3153 str = secfile_lookup_str(loading->file, "player%d.name", plrno);
3154 sg_failure_ret(str != NULL, "%s", secfile_error());
3156 sz_strlcpy(plr->username,
3158 "player%d.username", plrno));
3160 "player%d.unassigned_user", plrno),
3161 "%s", secfile_error());
3164 "player%d.orig_username",
3165 plrno));
3168 "player%d.ranked_username",
3169 plrno));
3171 "player%d.unassigned_ranked", plrno),
3172 "%s", secfile_error());
3174 "player%d.delegation_username",
3175 plrno);
3176 /* Defaults to no delegation. */
3177 if (strlen(str)) {
3179 }
3180
3181 /* Player flags */
3182 BV_CLR_ALL(plr->flags);
3183 slist = secfile_lookup_str_vec(loading->file, &nval, "player%d.flags", plrno);
3184 for (i = 0; i < nval; i++) {
3185 const char *sval = slist[i];
3187
3188 sg_failure_ret(plr_flag_id_is_valid(fid), "Invalid player flag \"%s\".", sval);
3189
3190 BV_SET(plr->flags, fid);
3191 }
3192 free(slist);
3193
3194 /* Nation */
3195 str = secfile_lookup_str(loading->file, "player%d.nation", plrno);
3197 if (plr->nation != NULL) {
3198 ai_traits_init(plr);
3199 }
3200
3201 /* Government */
3202 str = secfile_lookup_str(loading->file, "player%d.government_name",
3203 plrno);
3205 sg_failure_ret(gov != NULL, "Player%d: unsupported government \"%s\".",
3206 plrno, str);
3207 plr->government = gov;
3208
3209 /* Target government */
3211 "player%d.target_government_name", plrno);
3212 if (str != NULL) {
3214 } else {
3215 plr->target_government = NULL;
3216 }
3219 "player%d.revolution_finishes", plrno);
3220
3221 /* Load diplomatic data (diplstate + embassy + vision).
3222 * Shared vision is loaded in sg_load_players(). */
3224 players_iterate(pplayer) {
3225 char buf[32];
3226 int unconverted;
3227 struct player_diplstate *ds = player_diplstate_get(plr, pplayer);
3228 i = player_index(pplayer);
3229
3230 /* load diplomatic status */
3231 fc_snprintf(buf, sizeof(buf), "player%d.diplstate%d", plrno, i);
3232
3233 unconverted =
3234 secfile_lookup_int_default(loading->file, -1, "%s.type", buf);
3235 if (unconverted >= 0 && unconverted < loading->ds_t.size) {
3236 /* Look up what state the unconverted number represents. */
3237 ds->type = loading->ds_t.order[unconverted];
3238 } else {
3239 log_sg("No valid diplomatic state type between players %d and %d",
3240 plrno, i);
3241
3242 ds->type = DS_WAR;
3243 }
3244
3245 unconverted =
3246 secfile_lookup_int_default(loading->file, -1, "%s.max_state", buf);
3247 if (unconverted >= 0 && unconverted < loading->ds_t.size) {
3248 /* Look up what state the unconverted number represents. */
3249 ds->max_state = loading->ds_t.order[unconverted];
3250 } else {
3251 log_sg("No valid diplomatic max_state between players %d and %d",
3252 plrno, i);
3253
3254 ds->max_state = DS_WAR;
3255 }
3256
3257 /* FIXME: If either party is barbarian, we cannot enforce below check */
3258#if 0
3259 if (ds->type == DS_WAR && ds->first_contact_turn <= 0) {
3260 sg_regr(3020000,
3261 "Player%d: War with player %d who has never been met. "
3262 "Reverted to No Contact state.", plrno, i);
3263 ds->type = DS_NO_CONTACT;
3264 }
3265#endif
3266
3267 if (valid_dst_closest(ds) != ds->max_state) {
3268 sg_regr(3020000,
3269 "Player%d: closest diplstate to player %d less than current. "
3270 "Updated.", plrno, i);
3271 ds->max_state = ds->type;
3272 }
3273
3274 ds->first_contact_turn =
3276 "%s.first_contact_turn", buf);
3277 ds->turns_left =
3278 secfile_lookup_int_default(loading->file, -2, "%s.turns_left", buf);
3279 ds->has_reason_to_cancel =
3281 "%s.has_reason_to_cancel", buf);
3282 ds->contact_turns_left =
3284 "%s.contact_turns_left", buf);
3285
3286 if (secfile_lookup_bool_default(loading->file, FALSE, "%s.embassy",
3287 buf)) {
3288 BV_SET(plr->real_embassy, i);
3289 }
3290 /* 'gives_shared_vision' is loaded in sg_load_players() as all cities
3291 * must be known. */
3293
3294 /* load ai data */
3296 char buf[32];
3297
3298 fc_snprintf(buf, sizeof(buf), "player%d.ai%d", plrno,
3300
3302 secfile_lookup_int_default(loading->file, 1, "%s.love", buf);
3303 CALL_FUNC_EACH_AI(player_load_relations, plr, aplayer, loading->file, plrno);
3305
3306 CALL_FUNC_EACH_AI(player_load, plr, loading->file, plrno);
3307
3308 /* Some sane defaults */
3309 plr->ai_common.fuzzy = 0;
3310 plr->ai_common.expand = 100;
3311 plr->ai_common.science_cost = 100;
3312
3313
3315 "player%d.ai.level", plrno);
3316 if (level != NULL) {
3317 if (!fc_strcasecmp("Handicapped", level)) {
3318 /* Up to freeciv-3.1 Restricted AI level was known as Handicapped */
3320 } else {
3322 }
3323 } else {
3325 }
3326
3331 "player%d.ai.skill_level",
3332 plrno));
3333 }
3334
3336 "player%d.ai.barb_type", plrno);
3338
3340 log_sg("Player%d: Invalid barbarian type \"%s\". "
3341 "Changed to \"None\".", plrno, barb_str);
3343 }
3344
3345 if (is_barbarian(plr)) {
3346 server.nbarbarians++;
3347 }
3348
3349 if (is_ai(plr)) {
3351 CALL_PLR_AI_FUNC(gained_control, plr, plr);
3352 }
3353
3354 /* Load nation style. */
3355 {
3356 struct nation_style *style;
3357
3358 str = secfile_lookup_str(loading->file, "player%d.style_by_name", plrno);
3359
3360 /* Handle pre-2.6 savegames */
3361 if (str == NULL) {
3362 str = secfile_lookup_str(loading->file, "player%d.city_style_by_name",
3363 plrno);
3364 }
3365
3366 sg_failure_ret(str != NULL, "%s", secfile_error());
3367 style = style_by_rule_name(str);
3368 if (style == NULL) {
3369 style = style_by_number(0);
3370 log_sg("Player%d: unsupported city_style_name \"%s\". "
3371 "Changed to \"%s\".", plrno, str, style_rule_name(style));
3372 }
3373 plr->style = style;
3374 }
3375
3377 "player%d.idle_turns", plrno),
3378 "%s", secfile_error());
3380 "player%d.is_male", plrno);
3382 "player%d.is_alive", plrno),
3383 "%s", secfile_error());
3385 "player%d.turns_alive", plrno),
3386 "%s", secfile_error());
3388 "player%d.last_war", plrno),
3389 "%s", secfile_error());
3391 "player%d.phase_done", plrno);
3393 "player%d.gold", plrno),
3394 "%s", secfile_error());
3396 "player%d.rates.tax", plrno),
3397 "%s", secfile_error());
3399 "player%d.rates.science", plrno),
3400 "%s", secfile_error());
3402 "player%d.rates.luxury", plrno),
3403 "%s", secfile_error());
3404 plr->server.bulbs_last_turn =
3406 "player%d.research.bulbs_last_turn", plrno);
3407
3408 /* Traits */
3409 if (plr->nation) {
3410 for (i = 0; i < loading->trait.size; i++) {
3411 enum trait tr = trait_by_name(loading->trait.order[i], fc_strcasecmp);
3412
3413 if (trait_is_valid(tr)) {
3414 int val = secfile_lookup_int_default(loading->file, -1, "player%d.trait%d.val",
3415 plrno, i);
3416
3417 if (val != -1) {
3418 plr->ai_common.traits[tr].val = val;
3419 }
3420
3422 "player%d.trait%d.mod", plrno, i),
3423 "%s", secfile_error());
3424 plr->ai_common.traits[tr].mod = val;
3425 }
3426 }
3427 }
3428
3429 /* Achievements */
3430 {
3431 int count;
3432
3433 count = secfile_lookup_int_default(loading->file, -1,
3434 "player%d.achievement_count", plrno);
3435
3436 if (count > 0) {
3437 for (i = 0; i < count; i++) {
3438 const char *name;
3439 struct achievement *pach;
3440 bool first;
3441
3443 "player%d.achievement%d.name", plrno, i);
3445
3447 "Unknown achievement \"%s\".", name);
3448
3450 "player%d.achievement%d.first",
3451 plrno, i),
3452 "achievement error: %s", secfile_error());
3453
3454 sg_failure_ret(pach->first == NULL || !first,
3455 "Multiple players listed as first to get achievement \"%s\".",
3456 name);
3457
3458 BV_SET(pach->achievers, player_index(plr));
3459
3460 if (first) {
3461 pach->first = plr;
3462 }
3463 }
3464 }
3465 }
3466
3467 /* Player score. */
3468 plr->score.happy =
3470 "score%d.happy", plrno);
3471 plr->score.content =
3473 "score%d.content", plrno);
3474 plr->score.unhappy =
3476 "score%d.unhappy", plrno);
3477 plr->score.angry =
3479 "score%d.angry", plrno);
3480
3481 /* Make sure that the score about specialists in current ruleset that
3482 * were not present at saving time are set to zero. */
3484 plr->score.specialists[sp] = 0;
3486
3487 for (i = 0; i < loading->specialist.size; i++) {
3488 plr->score.specialists[specialist_index(loading->specialist.order[i])]
3490 "score%d.specialists%d", plrno, i);
3491 }
3492
3493 plr->score.wonders =
3495 "score%d.wonders", plrno);
3496 plr->score.techs =
3498 "score%d.techs", plrno);
3499 plr->score.techout =
3501 "score%d.techout", plrno);
3502 plr->score.landarea =
3504 "score%d.landarea", plrno);
3505 plr->score.settledarea =
3507 "score%d.settledarea", plrno);
3508 plr->score.population =
3510 "score%d.population", plrno);
3511 plr->score.cities =
3513 "score%d.cities", plrno);
3514 plr->score.units =
3516 "score%d.units", plrno);
3517 plr->score.pollution =
3519 "score%d.pollution", plrno);
3520 plr->score.literacy =
3522 "score%d.literacy", plrno);
3523 plr->score.bnp =
3525 "score%d.bnp", plrno);
3526 plr->score.mfg =
3528 "score%d.mfg", plrno);
3529 plr->score.spaceship =
3531 "score%d.spaceship", plrno);
3532 plr->score.units_built =
3534 "score%d.units_built", plrno);
3535 plr->score.units_killed =
3537 "score%d.units_killed", plrno);
3538 plr->score.units_lost =
3540 "score%d.units_lost", plrno);
3541 plr->score.units_used = 0; /* Was never saved to savegame2.c saves */
3542 plr->score.culture =
3544 "score%d.culture", plrno);
3545 plr->score.game =
3547 "score%d.total", plrno);
3548
3549 /* Load space ship data. */
3550 {
3551 struct player_spaceship *ship = &plr->spaceship;
3552 char prefix[32];
3553 const char *st;
3554 int ei;
3555
3556 fc_snprintf(prefix, sizeof(prefix), "player%d.spaceship", plrno);
3559 &ei,
3560 "%s.state", prefix),
3561 "%s", secfile_error());
3562 ship->state = ei;
3563
3564 if (ship->state != SSHIP_NONE) {
3565 sg_failure_ret(secfile_lookup_int(loading->file, &ship->structurals,
3566 "%s.structurals", prefix),
3567 "%s", secfile_error());
3568 sg_failure_ret(secfile_lookup_int(loading->file, &ship->components,
3569 "%s.components", prefix),
3570 "%s", secfile_error());
3572 "%s.modules", prefix),
3573 "%s", secfile_error());
3575 "%s.fuel", prefix),
3576 "%s", secfile_error());
3577 sg_failure_ret(secfile_lookup_int(loading->file, &ship->propulsion,
3578 "%s.propulsion", prefix),
3579 "%s", secfile_error());
3580 sg_failure_ret(secfile_lookup_int(loading->file, &ship->habitation,
3581 "%s.habitation", prefix),
3582 "%s", secfile_error());
3583 sg_failure_ret(secfile_lookup_int(loading->file, &ship->life_support,
3584 "%s.life_support", prefix),
3585 "%s", secfile_error());
3586 sg_failure_ret(secfile_lookup_int(loading->file, &ship->solar_panels,
3587 "%s.solar_panels", prefix),
3588 "%s", secfile_error());
3589
3590 st = secfile_lookup_str(loading->file, "%s.structure", prefix);
3591 sg_failure_ret(st != NULL, "%s", secfile_error())
3592 for (i = 0; i < NUM_SS_STRUCTURALS && st[i]; i++) {
3593 sg_failure_ret(st[i] == '1' || st[i] == '0',
3594 "Undefined value '%c' within '%s.structure'.", st[i],
3595 prefix)
3596
3597 if (!(st[i] == '0')) {
3598 BV_SET(ship->structure, i);
3599 }
3600 }
3601 if (ship->state >= SSHIP_LAUNCHED) {
3602 sg_failure_ret(secfile_lookup_int(loading->file, &ship->launch_year,
3603 "%s.launch_year", prefix),
3604 "%s", secfile_error());
3605 }
3607 }
3608 }
3609
3610 /* Load lost wonder data. */
3611 str = secfile_lookup_str(loading->file, "player%d.lost_wonders", plrno);
3612 /* If not present, probably an old savegame; nothing to be done */
3613 if (str != NULL) {
3614 int k;
3615
3616 sg_failure_ret(strlen(str) == loading->improvement.size,
3617 "Invalid length for 'player%d.lost_wonders' ("
3618 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")",
3619 plrno, strlen(str), loading->improvement.size);
3620 for (k = 0; k < loading->improvement.size; k++) {
3621 sg_failure_ret(str[k] == '1' || str[k] == '0',
3622 "Undefined value '%c' within "
3623 "'player%d.lost_wonders'.", plrno, str[k]);
3624
3625 if (str[k] == '1') {
3626 struct impr_type *pimprove =
3627 improvement_by_rule_name(loading->improvement.order[k]);
3628
3629 if (pimprove) {
3630 plr->wonders[improvement_index(pimprove)] = WONDER_LOST;
3631 }
3632 }
3633 }
3634 }
3635
3636 plr->history =
3637 secfile_lookup_int_default(loading->file, 0, "player%d.culture", plrno);
3638 plr->server.huts =
3639 secfile_lookup_int_default(loading->file, 0, "player%d.hut_count", plrno);
3640}
3641
3642/************************************************************************/
3646 struct player *plr)
3647{
3648 int ncities, i, plrno = player_number(plr);
3649 bool tasks_handled;
3650 int wlist_max_length;
3651
3652 /* Check status and return if not OK (sg_success FALSE). */
3653 sg_check_ret();
3654
3656 "player%d.ncities", plrno),
3657 "%s", secfile_error());
3658
3659 if (!plr->is_alive && ncities > 0) {
3660 log_sg("'player%d.ncities' = %d for dead player!", plrno, ncities);
3661 ncities = 0;
3662 }
3663
3664 if (!player_has_flag(plr, PLRF_FIRST_CITY) && ncities > 0) {
3665 /* Probably barbarians in an old savegame; fix up */
3667 }
3668
3670 "player%d.wl_max_length",
3671 plrno);
3672
3673 /* Load all cities of the player. */
3674 for (i = 0; i < ncities; i++) {
3675 char buf[32];
3676 struct city *pcity;
3677
3678 fc_snprintf(buf, sizeof(buf), "player%d.c%d", plrno, i);
3679
3680 /* Create a dummy city. */
3681 pcity = create_city_virtual(plr, NULL, buf);
3682 adv_city_alloc(pcity);
3683 if (!sg_load_player_city(loading, plr, pcity, buf, wlist_max_length)) {
3684 adv_city_free(pcity);
3685 destroy_city_virtual(pcity);
3686 sg_failure_ret(FALSE, "Error loading city %d of player %d.", i, plrno);
3687 }
3688
3690 idex_register_city(&wld, pcity);
3691
3692 /* Load the information about the nationality of citizens. This is done
3693 * here because the city sanity check called by citizens_update() requires
3694 * that the city is registered. */
3696
3697 /* After everything is loaded, but before vision. */
3698 map_claim_ownership(city_tile(pcity), plr, city_tile(pcity), TRUE);
3699
3700 /* adding the city contribution to fog-of-war */
3701 pcity->server.vision = vision_new(plr, city_tile(pcity));
3703 city_refresh_vision(pcity);
3704
3705 city_list_append(plr->cities, pcity);
3706 }
3707
3709 for (i = 0; !tasks_handled; i++) {
3710 int city_id;
3711 struct city *pcity = NULL;
3712
3713 city_id = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.city",
3714 plrno, i);
3715
3716 if (city_id != -1) {
3717 pcity = player_city_by_number(plr, city_id);
3718 }
3719
3720 if (pcity != NULL) {
3721 const char *str;
3722 int nat_x, nat_y;
3723 struct worker_task *ptask = fc_malloc(sizeof(struct worker_task));
3724
3725 nat_x = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.x", plrno, i);
3726 nat_y = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.y", plrno, i);
3727
3728 ptask->ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
3729
3730 str = secfile_lookup_str(loading->file, "player%d.task%d.activity", plrno, i);
3732
3734 "Unknown workertask activity %s", str);
3735
3736 str = secfile_lookup_str(loading->file, "player%d.task%d.target", plrno, i);
3737
3738 if (strcmp("-", str)) {
3740
3741 sg_failure_ret(ptask->tgt != NULL,
3742 "Unknown workertask target %s", str);
3743 } else {
3744 ptask->tgt = NULL;
3745 }
3746
3747 ptask->want = secfile_lookup_int_default(loading->file, 1,
3748 "player%d.task%d.want", plrno, i);
3749
3751 } else {
3753 }
3754 }
3755}
3756
3757/************************************************************************/
3760static bool sg_load_player_city(struct loaddata *loading, struct player *plr,
3761 struct city *pcity, const char *citystr,
3762 int wlist_max_length)
3763{
3764 struct player *past;
3765 const char *kind, *name, *str;
3766 int id, i, repair, sp_count = 0, workers = 0, value;
3767 int nat_x, nat_y;
3768 citizens size;
3769 const char *stylename;
3770 const struct civ_map *nmap = &(wld.map);
3771
3773 FALSE, "%s", secfile_error());
3775 FALSE, "%s", secfile_error());
3776 pcity->tile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
3777 sg_warn_ret_val(NULL != pcity->tile, FALSE,
3778 "%s has invalid center tile (%d, %d)",
3779 citystr, nat_x, nat_y);
3781 "%s duplicates city (%d, %d)", citystr, nat_x, nat_y);
3782
3783 /* Instead of dying, use 'citystr' string for damaged name. */
3785 "%s.name", citystr));
3786
3787 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->id, "%s.id",
3788 citystr), FALSE, "%s", secfile_error());
3789
3791 "%s.original", citystr);
3792 past = player_by_number(id);
3793 if (NULL != past) {
3794 pcity->original = past;
3795 }
3796
3797 /* savegame2 saves never had this information. Guess. */
3798 if (pcity->original != plr) {
3799 pcity->acquire_t = CACQ_CONQUEST;
3800 } else {
3801 pcity->acquire_t = CACQ_FOUNDED;
3802 }
3803
3804 sg_warn_ret_val(secfile_lookup_int(loading->file, &value, "%s.size",
3805 citystr), FALSE, "%s", secfile_error());
3806 size = (citizens)value; /* Set the correct type */
3807 sg_warn_ret_val(value == (int)size, FALSE,
3808 "Invalid city size: %d, set to %d", value, size);
3809 city_size_set(pcity, size);
3810
3811 for (i = 0; i < loading->specialist.size; i++) {
3812 sg_warn_ret_val(secfile_lookup_int(loading->file, &value, "%s.nspe%d",
3813 citystr, i),
3814 FALSE, "%s", secfile_error());
3815 pcity->specialists[specialist_index(loading->specialist.order[i])]
3816 = (citizens)value;
3817 sp_count += value;
3818 }
3819
3820 /* savegame2.c saves were ever saved with MAX_TRADE_ROUTES_OLD routes max */
3821 for (i = 0; i < MAX_TRADE_ROUTES_OLD; i++) {
3822 int partner = secfile_lookup_int_default(loading->file, 0,
3823 "%s.traderoute%d", citystr, i);
3824
3825 if (partner != 0) {
3826 struct trade_route *proute = fc_malloc(sizeof(struct trade_route));
3827
3828 proute->partner = partner;
3830 proute->goods = goods_by_number(0); /* First good */
3831
3833 }
3834 }
3835
3837 "%s.food_stock", citystr),
3838 FALSE, "%s", secfile_error());
3840 "%s.shield_stock", citystr),
3841 FALSE, "%s", secfile_error());
3842 pcity->history =
3843 secfile_lookup_int_default(loading->file, 0, "%s.history", citystr);
3844
3845 pcity->airlift =
3846 secfile_lookup_int_default(loading->file, 0, "%s.airlift", citystr);
3847 pcity->was_happy =
3848 secfile_lookup_bool_default(loading->file, FALSE, "%s.was_happy",
3849 citystr);
3850 pcity->had_famine = FALSE;
3851
3852 pcity->turn_plague =
3853 secfile_lookup_int_default(loading->file, 0, "%s.turn_plague", citystr);
3854
3856 "%s.anarchy", citystr),
3857 FALSE, "%s", secfile_error());
3858 pcity->rapture =
3859 secfile_lookup_int_default(loading->file, 0, "%s.rapture", citystr);
3860 pcity->steal =
3861 secfile_lookup_int_default(loading->file, 0, "%s.steal", citystr);
3862
3863 /* Before did_buy for undocumented hack */
3864 pcity->turn_founded =
3865 secfile_lookup_int_default(loading->file, -2, "%s.turn_founded",
3866 citystr);
3867 sg_warn_ret_val(secfile_lookup_int(loading->file, &i, "%s.did_buy",
3868 citystr), FALSE, "%s", secfile_error());
3869 pcity->did_buy = (i != 0);
3870 if (i == -1 && pcity->turn_founded == -2) {
3871 /* Undocumented hack */
3872 pcity->turn_founded = game.info.turn;
3873 }
3874
3875 pcity->did_sell
3876 = secfile_lookup_bool_default(loading->file, FALSE, "%s.did_sell", citystr);
3877
3879 "%s.turn_last_built", citystr),
3880 FALSE, "%s", secfile_error());
3881
3882 kind = secfile_lookup_str(loading->file, "%s.currently_building_kind",
3883 citystr);
3884 name = secfile_lookup_str(loading->file, "%s.currently_building_name",
3885 citystr);
3886 pcity->production = universal_by_rule_name(kind, name);
3888 "%s.currently_building: unknown \"%s\" \"%s\".",
3889 citystr, kind, name);
3890
3891 kind = secfile_lookup_str(loading->file, "%s.changed_from_kind",
3892 citystr);
3893 name = secfile_lookup_str(loading->file, "%s.changed_from_name",
3894 citystr);
3897 "%s.changed_from: unknown \"%s\" \"%s\".",
3898 citystr, kind, name);
3899
3900 pcity->before_change_shields =
3902 "%s.before_change_shields", citystr);
3903 pcity->caravan_shields =
3905 "%s.caravan_shields", citystr);
3906 pcity->disbanded_shields =
3908 "%s.disbanded_shields", citystr);
3911 "%s.last_turns_shield_surplus",
3912 citystr);
3913
3915 "%s.style", citystr);
3916 if (stylename != NULL) {
3918 } else {
3919 pcity->style = 0;
3920 }
3921 if (pcity->style < 0) {
3922 pcity->style = city_style(pcity);
3923 }
3924
3925 pcity->server.synced = FALSE; /* Must re-sync with clients */
3926
3927 /* Initialise list of city improvements. */
3928 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3929 pcity->built[i].turn = I_NEVER;
3930 }
3931
3932 /* Load city improvements. */
3933 str = secfile_lookup_str(loading->file, "%s.improvements", citystr);
3935 sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE,
3936 "Invalid length of '%s.improvements' ("
3937 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
3938 citystr, strlen(str), loading->improvement.size);
3939 for (i = 0; i < loading->improvement.size; i++) {
3940 sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE,
3941 "Undefined value '%c' within '%s.improvements'.",
3942 str[i], citystr)
3943
3944 if (str[i] == '1') {
3945 struct impr_type *pimprove =
3946 improvement_by_rule_name(loading->improvement.order[i]);
3947
3948 if (pimprove) {
3949 city_add_improvement(pcity, pimprove);
3950 }
3951 }
3952 }
3953
3954 sg_failure_ret_val(loading->worked_tiles != NULL, FALSE,
3955 "No worked tiles map defined.");
3956
3957 city_freeze_workers(pcity);
3958
3959 /* Load new savegame with variable (squared) city radius and worked
3960 * tiles map */
3961
3962 int radius_sq
3963 = secfile_lookup_int_default(loading->file, -1, "%s.city_radius_sq",
3964 citystr);
3965 city_map_radius_sq_set(pcity, radius_sq);
3966
3968 if (loading->worked_tiles[ptile->index] == pcity->id) {
3969 if (sq_map_distance(ptile, pcity->tile) > radius_sq) {
3970 log_sg("[%s] '%s' (%d, %d) has worker outside current radius "
3971 "at (%d, %d); repairing", citystr, city_name_get(pcity),
3972 TILE_XY(pcity->tile), TILE_XY(ptile));
3974 sp_count++;
3975 } else {
3976 tile_set_worked(ptile, pcity);
3977 workers++;
3978 }
3979
3980#ifdef FREECIV_DEBUG
3981 /* Set this tile to unused; a check for not reset tiles is
3982 * included in game_load_internal() */
3983 loading->worked_tiles[ptile->index] = -1;
3984#endif /* FREECIV_DEBUG */
3985 }
3987
3988 if (tile_worked(city_tile(pcity)) != pcity) {
3989 struct city *pwork = tile_worked(city_tile(pcity));
3990
3991 if (NULL != pwork) {
3992 log_sg("[%s] city center of '%s' (%d,%d) [%d] is worked by '%s' "
3993 "(%d,%d) [%d]; repairing", citystr, city_name_get(pcity),
3996
3997 tile_set_worked(city_tile(pcity), NULL); /* remove tile from pwork */
3998 pwork->specialists[DEFAULT_SPECIALIST]++;
4000 } else {
4001 log_sg("[%s] city center of '%s' (%d,%d) [%d] is empty; repairing",
4002 citystr, city_name_get(pcity), TILE_XY(city_tile(pcity)),
4003 city_size_get(pcity));
4004 }
4005
4006 /* repair pcity */
4007 tile_set_worked(city_tile(pcity), pcity);
4008 city_repair_size(pcity, -1);
4009 }
4010
4011 repair = city_size_get(pcity) - sp_count - (workers - FREE_WORKED_TILES);
4012 if (0 != repair) {
4013 log_sg("[%s] size mismatch for '%s' (%d,%d): size [%d] != "
4014 "(workers [%d] - free worked tiles [%d]) + specialists [%d]",
4015 citystr, city_name_get(pcity), TILE_XY(city_tile(pcity)), city_size_get(pcity),
4016 workers, FREE_WORKED_TILES, sp_count);
4017
4018 /* repair pcity */
4019 city_repair_size(pcity, repair);
4020 }
4021
4022 /* worklist_init() done in create_city_virtual() */
4023 worklist_load(loading->file, wlist_max_length, &pcity->worklist, "%s", citystr);
4024
4025 /* Load city options. */
4026 BV_CLR_ALL(pcity->city_options);
4027 for (i = 0; i < loading->coptions.size; i++) {
4028 if (secfile_lookup_bool_default(loading->file, FALSE, "%s.option%d",
4029 citystr, i)) {
4030 BV_SET(pcity->city_options, loading->coptions.order[i]);
4031 }
4032 }
4033 /* Was never stored to savegame2 saves */
4034 pcity->wlcb = WLCB_SMART;
4035
4036 CALL_FUNC_EACH_AI(city_load, loading->file, pcity, citystr);
4037
4038 return TRUE;
4039}
4040
4041/************************************************************************/
4045 struct player *plr,
4046 struct city *pcity,
4047 const char *citystr)
4048{
4050 citizens size;
4051
4052 citizens_init(pcity);
4053 player_slots_iterate(pslot) {
4054 int nationality;
4055
4057 "%s.citizen%d", citystr,
4058 player_slot_index(pslot));
4059 if (nationality > 0 && !player_slot_is_used(pslot)) {
4060 log_sg("Citizens of an invalid nation for %s (player slot %d)!",
4061 city_name_get(pcity), player_slot_index(pslot));
4062 continue;
4063 }
4064
4065 if (nationality != -1 && player_slot_is_used(pslot)) {
4067 "Invalid value for citizens of player %d in %s: %d.",
4069 citizens_nation_set(pcity, pslot, nationality);
4070 }
4072 /* Sanity check. */
4073 size = citizens_count(pcity);
4074 if (size != city_size_get(pcity)) {
4075 if (size != 0) {
4076 /* size == 0 can be result from the fact that ruleset had no
4077 * nationality enabled at saving time, so no citizens at all
4078 * were saved. But something more serious must be going on if
4079 * citizens have been saved partially - if some of them are there. */
4080 log_sg("City size and number of citizens does not match in %s "
4081 "(%d != %d)! Repairing ...", city_name_get(pcity),
4082 city_size_get(pcity), size);
4083 }
4084 citizens_update(pcity, NULL);
4085 }
4086 }
4087}
4088
4089/************************************************************************/
4093 struct player *plr)
4094{
4095 int nunits, i, plrno = player_number(plr);
4096
4097 /* Check status and return if not OK (sg_success FALSE). */
4098 sg_check_ret();
4099
4101 "player%d.nunits", plrno),
4102 "%s", secfile_error());
4103 if (!plr->is_alive && nunits > 0) {
4104 log_sg("'player%d.nunits' = %d for dead player!", plrno, nunits);
4105 nunits = 0; /* Some old savegames may be buggy. */
4106 }
4107
4108 for (i = 0; i < nunits; i++) {
4109 struct unit *punit;
4110 struct city *pcity;
4111 const char *name;
4112 char buf[32];
4113 struct unit_type *type;
4114 struct tile *ptile;
4115
4116 fc_snprintf(buf, sizeof(buf), "player%d.u%d", plrno, i);
4117
4118 name = secfile_lookup_str(loading->file, "%s.type_by_name", buf);
4120 sg_failure_ret(type != NULL, "%s: unknown unit type \"%s\".", buf, name);
4121
4122 /* Create a dummy unit. */
4123 punit = unit_virtual_create(plr, NULL, type, 0);
4124 if (!sg_load_player_unit(loading, plr, punit, buf)) {
4126 sg_failure_ret(FALSE, "Error loading unit %d of player %d.", i, plrno);
4127 }
4128
4131
4132 if ((pcity = game_city_by_number(punit->homecity))) {
4134 } else if (punit->homecity > IDENTITY_NUMBER_ZERO) {
4135 log_sg("%s: bad home city %d.", buf, punit->homecity);
4137 }
4138
4139 ptile = unit_tile(punit);
4140
4141 /* allocate the unit's contribution to fog of war */
4144 /* NOTE: There used to be some map_set_known calls here. These were
4145 * unneeded since unfogging the tile when the unit sees it will
4146 * automatically reveal that tile. */
4147
4150
4151 /* Claim ownership of fortress? */
4152 if ((extra_owner(ptile) == NULL
4153 || pplayers_at_war(extra_owner(ptile), plr))
4155 tile_claim_bases(ptile, plr);
4156 }
4157 }
4158}
4159
4160/************************************************************************/
4170static int sg_order_to_action(int order, struct unit *act_unit,
4171 struct tile *tgt_tile)
4172{
4173 switch (order) {
4175 if (tile_city(tgt_tile)
4177 /* The player's cities are loaded right before their units. It wasn't
4178 * possible for rulesets to allow joining foreign cities before 3.0.
4179 * This means that a converted build city order only can be a Join
4180 * City order if it targets a domestic city. */
4181 return ACTION_JOIN_CITY;
4182 } else {
4183 /* Assume that the intention was to found a new city. */
4184 return ACTION_FOUND_CITY;
4185 }
4187 /* Maps one to one with each other. */
4188 return ACTION_HELP_WONDER;
4190 /* Maps one to one with each other. */
4191 return ACTION_TRADE_ROUTE;
4192 case ORDER_OLD_DISBAND:
4193 /* Added to the order system in the same commit as Help Wonder. Assume
4194 * that anyone that intended to order Help Wonder used Help Wonder. */
4195 /* Could in theory be intended as an order to disband in the field. Why
4196 * would the player give a unit an order to go to a non city location
4197 * and disband there? Assume the intention was to recover production
4198 * until a non recovering disband order is found. */
4200 case ORDER_OLD_HOMECITY:
4201 return ACTION_HOME_CITY;
4202 }
4203
4204 /* The order hasn't been replaced by an action. */
4205 return ACTION_NONE;
4206}
4207
4208/************************************************************************/
4212 struct player *plr, struct unit *punit,
4213 const char *unitstr)
4214{
4215 int activity;
4216 int nat_x, nat_y;
4217 enum tile_special_type target;
4218 struct extra_type *pextra = NULL;
4219 struct base_type *pbase = NULL;
4220 struct road_type *proad = NULL;
4221 struct tile *ptile;
4222 int extra_id;
4223 int base_id;
4224 int road_id;
4225 int ei;
4226 const char *facing_str;
4228 int natnbr;
4229 bool ai_controlled;
4230
4232 unitstr), FALSE, "%s", secfile_error());
4234 FALSE, "%s", secfile_error());
4236 FALSE, "%s", secfile_error());
4237
4238 ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
4239 sg_warn_ret_val(NULL != ptile, FALSE, "%s invalid tile (%d, %d)",
4240 unitstr, nat_x, nat_y);
4241 unit_tile_set(punit, ptile);
4242
4245 "%s.facing", unitstr);
4246 if (facing_str[0] != 'x') {
4247 /* We don't touch punit->facing if savegame does not contain that
4248 * information. Initial orientation set by unit_virtual_create()
4249 * is as good as any. */
4250 enum direction8 facing = char2dir(facing_str[0]);
4251
4252 if (direction8_is_valid(facing)) {
4253 punit->facing = facing;
4254 } else {
4255 log_error("Illegal unit orientation '%s'", facing_str);
4256 }
4257 }
4258
4259 /* If savegame has unit nationality, it doesn't hurt to
4260 * internally set it even if nationality rules are disabled. */
4262 player_number(plr),
4263 "%s.nationality", unitstr);
4264
4266 if (punit->nationality == NULL) {
4267 punit->nationality = plr;
4268 }
4269
4271 "%s.homecity", unitstr), FALSE,
4272 "%s", secfile_error());
4274 "%s.moves", unitstr), FALSE,
4275 "%s", secfile_error());
4277 "%s.fuel", unitstr), FALSE,
4278 "%s", secfile_error());
4280 "%s.activity", unitstr), FALSE,
4281 "%s", secfile_error());
4282 activity = unit_activity_by_name(loading->activities.order[ei],
4284
4287 "%s.born", unitstr);
4289
4291 "%s.activity_tgt", unitstr);
4292
4293 if (extra_id != -2) {
4294 if (extra_id >= 0 && extra_id < loading->extra.size) {
4295 pextra = loading->extra.order[extra_id];
4296 set_unit_activity_targeted(punit, activity, pextra);
4297 } else if (activity == ACTIVITY_IRRIGATE) {
4301 punit);
4302 if (tgt != NULL) {
4304 } else {
4306 }
4307 } else if (activity == ACTIVITY_MINE) {
4309 EC_MINE,
4311 punit);
4312 if (tgt != NULL) {
4314 } else {
4316 }
4317 } else {
4318 set_unit_activity(punit, activity);
4319 }
4320 } else {
4321 /* extra_id == -2 -> activity_tgt not set */
4323 "%s.activity_base", unitstr);
4324 if (base_id >= 0 && base_id < loading->base.size) {
4325 pbase = loading->base.order[base_id];
4326 }
4328 "%s.activity_road", unitstr);
4329 if (road_id >= 0 && road_id < loading->road.size) {
4330 proad = loading->road.order[road_id];
4331 }
4332
4333 {
4335 loading->special.size /* S_LAST */,
4336 "%s.activity_target", unitstr);
4337 if (tgt_no >= 0 && tgt_no < loading->special.size) {
4338 target = loading->special.order[tgt_no];
4339 } else {
4340 target = S_LAST;
4341 }
4342 }
4343
4344 if (target == S_OLD_ROAD) {
4345 target = S_LAST;
4347 } else if (target == S_OLD_RAILROAD) {
4348 target = S_LAST;
4350 }
4351
4352 if (activity == ACTIVITY_OLD_ROAD) {
4353 activity = ACTIVITY_GEN_ROAD;
4355 } else if (activity == ACTIVITY_OLD_RAILROAD) {
4356 activity = ACTIVITY_GEN_ROAD;
4358 }
4359
4360 /* We need changed_from == ACTIVITY_IDLE by now so that
4361 * set_unit_activity() and friends don't spuriously restore activity
4362 * points -- unit should have been created this way */
4364
4365 if (activity == ACTIVITY_BASE) {
4366 if (pbase) {
4368 } else {
4369 log_sg("Cannot find base %d for %s to build",
4372 }
4373 } else if (activity == ACTIVITY_GEN_ROAD) {
4374 if (proad) {
4376 } else {
4377 log_sg("Cannot find road %d for %s to build",
4380 }
4381 } else if (activity == ACTIVITY_PILLAGE) {
4382 struct extra_type *a_target;
4383
4384 if (target != S_LAST) {
4385 a_target = special_extra_get(target);
4386 } else if (pbase != NULL) {
4388 } else if (proad != NULL) {
4390 } else {
4391 a_target = NULL;
4392 }
4393 /* An out-of-range base number is seen with old savegames. We take
4394 * it as indicating undirected pillaging. We will assign pillage
4395 * targets before play starts. */
4397 } else if (activity == ACTIVITY_IRRIGATE) {
4401 punit);
4402 if (tgt != NULL) {
4404 } else {
4406 }
4407 } else if (activity == ACTIVITY_MINE) {
4409 EC_MINE,
4411 punit);
4412 if (tgt != NULL) {
4414 } else {
4416 }
4417 } else if (activity == ACTIVITY_OLD_POLLUTION_SG2
4418 || activity == ACTIVITY_OLD_FALLOUT_SG2) {
4420 ERM_CLEAN,
4422 punit);
4423 if (tgt != NULL) {
4425 } else {
4427 }
4428 } else {
4430 }
4431 } /* activity_tgt == NULL */
4432
4434 "%s.activity_count", unitstr), FALSE,
4435 "%s", secfile_error());
4436
4439 "%s.changed_from", unitstr);
4440
4442 "%s.changed_from_tgt", unitstr);
4443
4444 if (extra_id != -2) {
4445 if (extra_id >= 0 && extra_id < loading->extra.size) {
4446 punit->changed_from_target = loading->extra.order[extra_id];
4447 } else {
4449 }
4450 } else {
4451 /* extra_id == -2 -> changed_from_tgt not set */
4452
4453 cfspe =
4455 "%s.changed_from_target", unitstr);
4456 base_id =
4458 "%s.changed_from_base", unitstr);
4459 road_id =
4461 "%s.changed_from_road", unitstr);
4462
4463 if (road_id == -1) {
4464 if (cfspe == S_OLD_ROAD) {
4466 if (proad) {
4468 }
4469 } else if (cfspe == S_OLD_RAILROAD) {
4471 if (proad) {
4473 }
4474 }
4475 }
4476
4477 if (base_id >= 0 && base_id < loading->base.size) {
4479 } else if (road_id >= 0 && road_id < loading->road.size) {
4481 } else if (cfspe != S_LAST) {
4483 } else {
4485 }
4486
4491 punit);
4492 if (tgt != NULL) {
4494 } else {
4496 }
4497 } else if (punit->changed_from == ACTIVITY_MINE) {
4499 EC_MINE,
4501 punit);
4502 if (tgt != NULL) {
4504 } else {
4506 }
4510 ERM_CLEAN,
4512 punit);
4513 if (tgt != NULL) {
4515 } else {
4517 }
4518 }
4519 }
4520
4523 "%s.changed_from_count", unitstr);
4524
4525 /* Special case: for a long time, we accidentally incremented
4526 * activity_count while a unit was sentried, so it could increase
4527 * without bound (bug #20641) and be saved in old savefiles.
4528 * We zero it to prevent potential trouble overflowing the range
4529 * in network packets, etc. */
4530 if (activity == ACTIVITY_SENTRY) {
4531 punit->activity_count = 0;
4532 }
4535 }
4536
4537 punit->veteran
4538 = secfile_lookup_int_default(loading->file, 0, "%s.veteran", unitstr);
4539 {
4540 /* Protect against change in veteran system in ruleset */
4541 const int levels = utype_veteran_levels(unit_type_get(punit));
4542 if (punit->veteran >= levels) {
4543 fc_assert(levels >= 1);
4544 punit->veteran = levels - 1;
4545 }
4546 }
4549 "%s.done_moving", unitstr);
4552 "%s.battlegroup", unitstr);
4553
4555 "%s.go", unitstr)) {
4556 int gnat_x, gnat_y;
4557
4559 "%s.goto_x", unitstr), FALSE,
4560 "%s", secfile_error());
4562 "%s.goto_y", unitstr), FALSE,
4563 "%s", secfile_error());
4564
4566 } else {
4567 punit->goto_tile = NULL;
4568
4569 /* These variables are not used but needed for saving the unit table.
4570 * Load them to prevent unused variables errors. */
4571 (void) secfile_entry_lookup(loading->file, "%s.goto_x", unitstr);
4572 (void) secfile_entry_lookup(loading->file, "%s.goto_y", unitstr);
4573 }
4574
4575 /* Load AI data of the unit. */
4576 CALL_FUNC_EACH_AI(unit_load, loading->file, punit, unitstr);
4577
4580 "%s.ai", unitstr), FALSE,
4581 "%s", secfile_error());
4582 if (ai_controlled) {
4583 /* Autosettler and Autoexplore are separated by
4584 * compat_post_load_030100() when set to SSA_AUTOSETTLER */
4586 } else {
4588 }
4590 "%s.hp", unitstr), FALSE,
4591 "%s", secfile_error());
4592
4594 = secfile_lookup_int_default(loading->file, 0, "%s.ord_map", unitstr);
4596 = secfile_lookup_int_default(loading->file, 0, "%s.ord_city", unitstr);
4597 punit->moved
4598 = secfile_lookup_bool_default(loading->file, FALSE, "%s.moved", unitstr);
4601 "%s.paradropped", unitstr);
4602
4603 /* The transport status (punit->transported_by) is loaded in
4604 * sg_player_units_transport(). */
4605
4606 /* Initialize upkeep values: these are hopefully initialized
4607 * elsewhere before use (specifically, in city_support(); but
4608 * fixme: check whether always correctly initialized?).
4609 * Below is mainly for units which don't have homecity --
4610 * otherwise these don't get initialized (and AI calculations
4611 * etc may use junk values). */
4615
4619 "%s.action_decision_want", unitstr);
4620
4622 /* Load the tile to act against. */
4623 int adwt_x, adwt_y;
4624
4625 if (secfile_lookup_int(loading->file, &adwt_x,
4626 "%s.action_decision_tile_x", unitstr)
4628 "%s.action_decision_tile_y", unitstr)) {
4630 adwt_x, adwt_y);
4631 } else {
4634 log_sg("Bad action_decision_tile for unit %d", punit->id);
4635 }
4636 } else {
4637 (void) secfile_entry_lookup(loading->file, "%s.action_decision_tile_x", unitstr);
4638 (void) secfile_entry_lookup(loading->file, "%s.action_decision_tile_y", unitstr);
4640 }
4641
4642 /* Load the unit orders */
4643 {
4644 int len = secfile_lookup_int_default(loading->file, 0,
4645 "%s.orders_length", unitstr);
4646
4647 if (len > 0) {
4648 const char *orders_unitstr, *dir_unitstr, *act_unitstr;
4649 const char *tgt_unitstr;
4650 const char *base_unitstr = NULL;
4651 const char *road_unitstr = NULL;
4654 int j;
4655
4656 punit->orders.list = fc_malloc(len * sizeof(*(punit->orders.list)));
4660 "%s.orders_index", unitstr);
4663 "%s.orders_repeat", unitstr);
4666 "%s.orders_vigilant", unitstr);
4667
4670 "%s.orders_list", unitstr);
4673 "%s.dir_list", unitstr);
4676 "%s.activity_list", unitstr);
4678 = secfile_lookup_str_default(loading->file, NULL, "%s.tgt_list", unitstr);
4679
4680 if (tgt_unitstr == NULL) {
4682 = secfile_lookup_str(loading->file, "%s.base_list", unitstr);
4684 = secfile_lookup_str_default(loading->file, NULL, "%s.road_list", unitstr);
4685 }
4686
4688
4689 for (j = 0; j < len; j++) {
4690 struct unit_order *order = &punit->orders.list[j];
4691
4692 if (orders_unitstr[j] == '\0' || dir_unitstr[j] == '\0'
4693 || act_unitstr[j] == '\0') {
4694 log_sg("Invalid unit orders.");
4696 break;
4697 }
4698 order->order = char2order(orders_unitstr[j]);
4699 order->dir = char2dir(dir_unitstr[j]);
4700 order->activity = char2activity(act_unitstr[j]);
4701 /* Target, if needed, is set in compat_post_load_030100() */
4702 order->target = NO_TARGET;
4703 order->sub_target = NO_TARGET;
4704
4705 if (order->order == ORDER_LAST
4706 || (order->order == ORDER_MOVE && !direction8_is_valid(order->dir))
4707 || (order->order == ORDER_ACTION_MOVE
4708 && !direction8_is_valid(order->dir))
4709 || (order->order == ORDER_ACTIVITY
4710 && order->activity == ACTIVITY_LAST)) {
4711 /* An invalid order. Just drop the orders for this unit. */
4713 punit->orders.list = NULL;
4714 punit->orders.length = 0;
4716 punit->goto_tile = NULL;
4717 break;
4718 }
4719
4720 /* The order may have been replaced by the perform action order */
4721 order->action = sg_order_to_action(order->order, punit,
4722 punit->goto_tile);
4723 if (order->action != ACTION_NONE) {
4724 /* The order was converted by order_to_action */
4725 order->order = ORDER_PERFORM_ACTION;
4726 }
4727
4728 if (tgt_unitstr) {
4729 if (tgt_unitstr[j] != '?') {
4731
4732 if (extra_id < 0 || extra_id >= loading->extra.size) {
4733 log_sg("Cannot find extra %d for %s to build",
4735 order->sub_target = EXTRA_NONE;
4736 } else {
4737 order->sub_target = extra_id;
4738 }
4739 } else {
4740 order->sub_target = EXTRA_NONE;
4741 }
4742 } else {
4743 /* In pre-2.6 savegames, base_list and road_list were only saved
4744 * for those activities (and not e.g. pillaging) */
4745 if (base_unitstr && base_unitstr[j] != '?'
4746 && order->activity == ACTIVITY_BASE) {
4748
4749 if (base_id < 0 || base_id >= loading->base.size) {
4750 log_sg("Cannot find base %d for %s to build",
4753 NULL, NULL));
4754 }
4755
4756 order->sub_target
4758 } else if (road_unitstr && road_unitstr[j] != '?'
4759 && order->activity == ACTIVITY_GEN_ROAD) {
4761
4762 if (road_id < 0 || road_id >= loading->road.size) {
4763 log_sg("Cannot find road %d for %s to build",
4765 road_id = 0;
4766 }
4767
4768 order->sub_target
4770 } else {
4771 order->sub_target = EXTRA_NONE;
4772 }
4773
4774 if (order->activity == ACTIVITY_OLD_ROAD) {
4775 order->activity = ACTIVITY_GEN_ROAD;
4776 order->sub_target
4778 } else if (order->activity == ACTIVITY_OLD_RAILROAD) {
4779 order->activity = ACTIVITY_GEN_ROAD;
4780 order->sub_target
4782 }
4783 }
4784 }
4785 } else {
4786 punit->goto_tile = NULL;
4788 punit->orders.list = NULL;
4789 punit->orders.length = 0;
4790
4791 (void) secfile_entry_lookup(loading->file, "%s.orders_index", unitstr);
4792 (void) secfile_entry_lookup(loading->file, "%s.orders_repeat", unitstr);
4793 (void) secfile_entry_lookup(loading->file, "%s.orders_vigilant", unitstr);
4794 (void) secfile_entry_lookup(loading->file, "%s.orders_list", unitstr);
4795 (void) secfile_entry_lookup(loading->file, "%s.dir_list", unitstr);
4796 (void) secfile_entry_lookup(loading->file, "%s.activity_list", unitstr);
4797 (void) secfile_entry_lookup(loading->file, "%s.tgt_list", unitstr);
4798 }
4799 }
4800
4801 return TRUE;
4802}
4803
4804/************************************************************************/
4809 struct player *plr)
4810{
4811 int nunits, i, plrno = player_number(plr);
4812
4813 /* Check status and return if not OK (sg_success FALSE). */
4814 sg_check_ret();
4815
4816 /* Recheck the number of units for the player. This is a copied from
4817 * sg_load_player_units(). */
4819 "player%d.nunits", plrno),
4820 "%s", secfile_error());
4821 if (!plr->is_alive && nunits > 0) {
4822 log_sg("'player%d.nunits' = %d for dead player!", plrno, nunits);
4823 nunits = 0; /* Some old savegames may be buggy. */
4824 }
4825
4826 for (i = 0; i < nunits; i++) {
4827 int id_unit, id_trans;
4828 struct unit *punit, *ptrans;
4829
4831 "player%d.u%d.id",
4832 plrno, i);
4834 fc_assert_action(punit != NULL, continue);
4835
4837 "player%d.u%d.transported_by",
4838 plrno, i);
4839 if (id_trans == -1) {
4840 /* Not transported. */
4841 continue;
4842 }
4843
4845 fc_assert_action(id_trans == -1 || ptrans != NULL, continue);
4846
4847 if (ptrans) {
4848#ifndef FREECIV_NDEBUG
4849 bool load_success =
4850#endif
4852
4853 fc_assert_action(load_success, continue);
4854 }
4855 }
4856}
4857
4858/************************************************************************/
4862 struct player *plr)
4863{
4864 int plrno = player_number(plr);
4865
4866 /* Check status and return if not OK (sg_success FALSE). */
4867 sg_check_ret();
4868
4869 /* Toss any existing attribute_block (should not exist) */
4870 if (plr->attribute_block.data) {
4872 plr->attribute_block.data = NULL;
4873 }
4874
4875 /* This is a big heap of opaque data for the client, check everything! */
4877 loading->file, 0, "player%d.attribute_v2_block_length", plrno);
4878
4879 if (0 > plr->attribute_block.length) {
4880 log_sg("player%d.attribute_v2_block_length=%d too small", plrno,
4881 plr->attribute_block.length);
4882 plr->attribute_block.length = 0;
4883 } else if (MAX_ATTRIBUTE_BLOCK < plr->attribute_block.length) {
4884 log_sg("player%d.attribute_v2_block_length=%d too big (max %d)",
4886 plr->attribute_block.length = 0;
4887 } else if (0 < plr->attribute_block.length) {
4888 int part_nr, parts;
4889 int quoted_length;
4890 char *quoted;
4891#ifndef FREECIV_NDEBUG
4892 size_t actual_length;
4893#endif
4894
4897 "player%d.attribute_v2_block_length_quoted",
4898 plrno), "%s", secfile_error());
4901 "player%d.attribute_v2_block_parts", plrno),
4902 "%s", secfile_error());
4903
4905 quoted[0] = '\0';
4907 for (part_nr = 0; part_nr < parts; part_nr++) {
4908 const char *current =
4910 "player%d.attribute_v2_block_data.part%d",
4911 plrno, part_nr);
4912 if (!current) {
4913 log_sg("attribute_v2_block_parts=%d actual=%d", parts, part_nr);
4914 break;
4915 }
4916 log_debug("attribute_v2_block_length_quoted=%d"
4917 " have=" SIZE_T_PRINTF " part=" SIZE_T_PRINTF,
4918 quoted_length, strlen(quoted), strlen(current));
4919 fc_assert(strlen(quoted) + strlen(current) <= quoted_length);
4920 strcat(quoted, current);
4921 }
4923 "attribute_v2_block_length_quoted=%d"
4924 " actual=" SIZE_T_PRINTF,
4926
4927#ifndef FREECIV_NDEBUG
4929#endif
4931 plr->attribute_block.data,
4932 plr->attribute_block.length);
4934 free(quoted);
4935 }
4936}
4937
4938/************************************************************************/
4942 struct player *plr)
4943{
4944 int plrno = player_number(plr);
4945 int total_ncities =
4947 "player%d.dc_total", plrno);
4948 int i;
4949 bool someone_alive = FALSE;
4950
4951 /* Check status and return if not OK (sg_success FALSE). */
4952 sg_check_ret();
4953
4956 if (pteam_member->is_alive) {
4958 break;
4959 }
4961
4962 if (!someone_alive) {
4963 /* Reveal all for completely dead teams. */
4965 }
4966 }
4967
4968 if (!plr->is_alive
4969 || -1 == total_ncities
4970 || !game.info.fogofwar
4972 "game.save_private_map")) {
4973 /* We have:
4974 * - a dead player;
4975 * - fogged cities are not saved for any reason;
4976 * - a savegame with fog of war turned off;
4977 * - or game.save_private_map is not set to FALSE in the scenario /
4978 * savegame. The players private knowledge is set to be what they could
4979 * see without fog of war. */
4980 whole_map_iterate(&(wld.map), ptile) {
4981 if (map_is_known(ptile, plr)) {
4982 struct city *pcity = tile_city(ptile);
4983
4984 update_player_tile_last_seen(plr, ptile);
4985 update_player_tile_knowledge(plr, ptile);
4986
4987 if (NULL != pcity) {
4988 update_dumb_city(plr, pcity);
4989 }
4990 }
4992
4993 /* Nothing more to do; */
4994 return;
4995 }
4996
4997 /* Load player map (terrain). */
4998 LOAD_MAP_CHAR(ch, ptile,
4999 map_get_player_tile(ptile, plr)->terrain
5000 = char2terrain(ch), loading->file,
5001 "player%d.map_t%04d", plrno);
5002
5003 /* Load player map (resources). */
5004 LOAD_MAP_CHAR(ch, ptile,
5005 map_get_player_tile(ptile, plr)->resource
5006 = char2resource(ch), loading->file,
5007 "player%d.map_res%04d", plrno);
5008
5009 if (loading->version >= 30) {
5010 /* 2.6.0 or newer */
5011
5012 /* Load player map (extras). */
5013 halfbyte_iterate_extras(j, loading->extra.size) {
5014 LOAD_MAP_CHAR(ch, ptile,
5016 ch, loading->extra.order + 4 * j),
5017 loading->file, "player%d.map_e%02d_%04d", plrno, j);
5019 } else {
5020 /* Load player map (specials). */
5021 halfbyte_iterate_special(j, loading->special.size) {
5022 LOAD_MAP_CHAR(ch, ptile,
5023 sg_special_set_dbv(ptile,
5024 &(map_get_player_tile(ptile, plr)->extras),
5025 ch, loading->special.order + 4 * j, FALSE),
5026 loading->file, "player%d.map_spe%02d_%04d", plrno, j);
5028
5029 /* Load player map (bases). */
5030 halfbyte_iterate_bases(j, loading->base.size) {
5031 LOAD_MAP_CHAR(ch, ptile,
5033 ch, loading->base.order + 4 * j),
5034 loading->file, "player%d.map_b%02d_%04d", plrno, j);
5036
5037 /* Load player map (roads). */
5038 if (loading->version >= 20) {
5039 /* 2.5.0 or newer */
5040 halfbyte_iterate_roads(j, loading->road.size) {
5041 LOAD_MAP_CHAR(ch, ptile,
5043 ch, loading->road.order + 4 * j),
5044 loading->file, "player%d.map_r%02d_%04d", plrno, j);
5046 }
5047 }
5048
5050 /* Load player map (border). */
5051 int x, y;
5052
5053 for (y = 0; y < wld.map.ysize; y++) {
5054 const char *buffer
5055 = secfile_lookup_str(loading->file, "player%d.map_owner%04d",
5056 plrno, y);
5057 const char *buffer2
5058 = secfile_lookup_str(loading->file, "player%d.extras_owner%04d",
5059 plrno, y);
5060 const char *ptr = buffer;
5061 const char *ptr2 = buffer2;
5062
5063 sg_failure_ret(NULL != buffer,
5064 "Savegame corrupt - map line %d not found.", y);
5065 for (x = 0; x < wld.map.xsize; x++) {
5066 char token[TOKEN_SIZE];
5067 char token2[TOKEN_SIZE];
5068 int number;
5069 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
5070
5071 scanin(&ptr, ",", token, sizeof(token));
5072 sg_failure_ret('\0' != token[0],
5073 "Savegame corrupt - map size not correct.");
5074 if (strcmp(token, "-") == 0) {
5075 map_get_player_tile(ptile, plr)->owner = NULL;
5076 } else {
5077 sg_failure_ret(str_to_int(token, &number),
5078 "Savegame corrupt - got tile owner=%s in (%d, %d).",
5079 token, x, y);
5080 map_get_player_tile(ptile, plr)->owner = player_by_number(number);
5081 }
5082
5083 if (loading->version >= 30) {
5084 scanin(&ptr2, ",", token2, sizeof(token2));
5085 sg_failure_ret('\0' != token2[0],
5086 "Savegame corrupt - map size not correct.");
5087 if (strcmp(token2, "-") == 0) {
5088 map_get_player_tile(ptile, plr)->extras_owner = NULL;
5089 } else {
5091 "Savegame corrupt - got extras owner=%s in (%d, %d).",
5092 token, x, y);
5093 map_get_player_tile(ptile, plr)->extras_owner = player_by_number(number);
5094 }
5095 } else {
5097 = map_get_player_tile(ptile, plr)->owner;
5098 }
5099 }
5100 }
5101 }
5102
5103 /* Load player map (update time). */
5104 for (i = 0; i < 4; i++) {
5105 /* put 4-bit segments of 16-bit "updated" field */
5106 if (i == 0) {
5107 LOAD_MAP_CHAR(ch, ptile,
5108 map_get_player_tile(ptile, plr)->last_updated
5109 = ascii_hex2bin(ch, i),
5110 loading->file, "player%d.map_u%02d_%04d", plrno, i);
5111 } else {
5112 LOAD_MAP_CHAR(ch, ptile,
5113 map_get_player_tile(ptile, plr)->last_updated
5114 |= ascii_hex2bin(ch, i),
5115 loading->file, "player%d.map_u%02d_%04d", plrno, i);
5116 }
5117 }
5118
5119 /* Load player map known cities. */
5120 for (i = 0; i < total_ncities; i++) {
5121 struct vision_site *pdcity;
5122 char buf[32];
5123 fc_snprintf(buf, sizeof(buf), "player%d.dc%d", plrno, i);
5124
5128 pdcity);
5130 } else {
5131 /* Error loading the data. */
5132 log_sg("Skipping seen city %d for player %d.", i, plrno);
5133 if (pdcity != NULL) {
5135 }
5136 }
5137 }
5138
5139 /* Repair inconsistent player maps. */
5140 whole_map_iterate(&(wld.map), ptile) {
5141 if (map_is_known_and_seen(ptile, plr, V_MAIN)) {
5142 struct city *pcity = tile_city(ptile);
5143
5144 update_player_tile_knowledge(plr, ptile);
5145 reality_check_city(plr, ptile);
5146
5147 if (NULL != pcity) {
5148 update_dumb_city(plr, pcity);
5149 }
5150 } else if (!game.server.foggedborders && map_is_known(ptile, plr)) {
5151 /* Non fogged borders aren't loaded. See hrm Bug #879084 */
5152 struct player_tile *plrtile = map_get_player_tile(ptile, plr);
5153
5154 plrtile->owner = tile_owner(ptile);
5155 }
5157}
5158
5159/************************************************************************/
5163 struct player *plr,
5164 struct vision_site *pdcity,
5165 const char *citystr)
5166{
5167 const char *str;
5168 int i, id, size;
5169 citizens city_size;
5170 int nat_x, nat_y;
5171 const char *stylename;
5172 const char *vname;
5173
5175 citystr),
5176 FALSE, "%s", secfile_error());
5178 citystr),
5179 FALSE, "%s", secfile_error());
5180 pdcity->location = native_pos_to_tile(&(wld.map), nat_x, nat_y);
5181 sg_warn_ret_val(NULL != pdcity->location, FALSE,
5182 "%s invalid tile (%d,%d)", citystr, nat_x, nat_y);
5183
5184 sg_warn_ret_val(secfile_lookup_int(loading->file, &id, "%s.owner",
5185 citystr),
5186 FALSE, "%s", secfile_error());
5187 pdcity->owner = player_by_number(id);
5188 sg_warn_ret_val(NULL != pdcity->owner, FALSE,
5189 "%s has invalid owner (%d); skipping.", citystr, id);
5190
5192 "%s.id", citystr),
5193 FALSE, "%s", secfile_error());
5195 "%s has invalid id (%d); skipping.", citystr, id);
5196
5198 "%s.size", citystr),
5199 FALSE, "%s", secfile_error());
5200 city_size = (citizens)size; /* set the correct type */
5201 sg_warn_ret_val(size == (int)city_size, FALSE,
5202 "Invalid city size: %d; set to %d.", size, city_size);
5203 vision_site_size_set(pdcity, city_size);
5204
5205 /* Initialise list of improvements */
5206 BV_CLR_ALL(pdcity->improvements);
5207 str = secfile_lookup_str(loading->file, "%s.improvements", citystr);
5209 sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE,
5210 "Invalid length of '%s.improvements' ("
5211 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
5212 citystr, strlen(str), loading->improvement.size);
5213 for (i = 0; i < loading->improvement.size; i++) {
5214 sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE,
5215 "Undefined value '%c' within '%s.improvements'.",
5216 str[i], citystr)
5217
5218 if (str[i] == '1') {
5219 struct impr_type *pimprove =
5220 improvement_by_rule_name(loading->improvement.order[i]);
5221
5222 if (pimprove) {
5223 BV_SET(pdcity->improvements, improvement_index(pimprove));
5224 }
5225 }
5226 }
5227
5229 "%s.name", citystr);
5230
5231 if (vname != NULL) {
5232 pdcity->name = fc_strdup(vname);
5233 }
5234
5236 "%s.occupied", citystr);
5238 "%s.walls", citystr);
5240 "%s.happy", citystr);
5242 "%s.unhappy", citystr);
5244 "%s.style", citystr);
5245 if (stylename != NULL) {
5247 } else {
5248 pdcity->style = 0;
5249 }
5250 if (pdcity->style < 0) {
5251 pdcity->style = 0;
5252 }
5253
5254 pdcity->city_image = secfile_lookup_int_default(loading->file, -100,
5255 "%s.city_image", citystr);
5256
5257 pdcity->capital = CAPITAL_NOT;
5258
5259 return TRUE;
5260}
5261
5262/* =======================================================================
5263 * Load the researches.
5264 * ======================================================================= */
5265
5266/************************************************************************/
5270{
5271 struct research *presearch;
5272 int count;
5273 int number;
5274 const char *str;
5275 int i, j;
5276 bool got_tech;
5277
5278 /* Check status and return if not OK (sg_success FALSE). */
5279 sg_check_ret();
5280
5281 /* Initialize all researches. */
5285
5286 /* May be unsaved (e.g. scenario case). */
5287 count = secfile_lookup_int_default(loading->file, 0, "research.count");
5288 for (i = 0; i < count; i++) {
5290 "research.r%d.number", i),
5291 "%s", secfile_error());
5292 presearch = research_by_number(number);
5294 "Invalid research number %d in 'research.r%d.number'",
5295 number, i);
5296
5297 presearch->tech_goal = technology_load(loading->file,
5298 "research.r%d.goal", i);
5300 &presearch->techs_researched,
5301 "research.r%d.techs", i),
5302 "%s", secfile_error());
5304 &presearch->future_tech,
5305 "research.r%d.futuretech", i),
5306 "%s", secfile_error());
5308 &presearch->bulbs_researched,
5309 "research.r%d.bulbs", i),
5310 "%s", secfile_error());
5312 &presearch->bulbs_researching_saved,
5313 "research.r%d.bulbs_before", i),
5314 "%s", secfile_error());
5315 presearch->researching_saved = technology_load(loading->file,
5316 "research.r%d.saved", i);
5317 presearch->researching = technology_load(loading->file,
5318 "research.r%d.now", i);
5320 &got_tech,
5321 "research.r%d.got_tech", i),
5322 "%s", secfile_error());
5323 if (got_tech) {
5324 presearch->free_bulbs = presearch->bulbs_researched;
5325 }
5326
5327 str = secfile_lookup_str(loading->file, "research.r%d.done", i);
5328 sg_failure_ret(str != NULL, "%s", secfile_error());
5329 sg_failure_ret(strlen(str) == loading->technology.size,
5330 "Invalid length of 'research.r%d.done' ("
5331 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
5332 i, strlen(str), loading->technology.size);
5333 for (j = 0; j < loading->technology.size; j++) {
5334 sg_failure_ret(str[j] == '1' || str[j] == '0',
5335 "Undefined value '%c' within 'research.r%d.done'.",
5336 str[j], i);
5337
5338 if (str[j] == '1') {
5339 struct advance *padvance =
5340 advance_by_rule_name(loading->technology.order[j]);
5341
5342 if (padvance) {
5344 TECH_KNOWN);
5345 }
5346 }
5347 }
5348 }
5349
5350 /* In case of tech_leakage, we can update research only after all the
5351 * researches have been loaded */
5355}
5356
5357/* =======================================================================
5358 * Load the event cache. Should be the last thing to do.
5359 * ======================================================================= */
5360
5361/************************************************************************/
5365{
5366 /* Check status and return if not OK (sg_success FALSE). */
5367 sg_check_ret();
5368
5369 event_cache_load(loading->file, "event_cache");
5370}
5371
5372/* =======================================================================
5373 * Load the open treaties
5374 * ======================================================================= */
5375
5376/************************************************************************/
5380{
5381 int tidx;
5382 const char *plr0;
5383
5384 /* Check status and return if not OK (sg_success FALSE). */
5385 sg_check_ret();
5386
5387 for (tidx = 0; (plr0 = secfile_lookup_str_default(loading->file, NULL,
5388 "treaty%d.plr0", tidx)) != NULL ;
5389 tidx++) {
5390 const char *plr1;
5391 const char *ct;
5392 int cidx;
5393 struct player *p0, *p1;
5394
5395 plr1 = secfile_lookup_str(loading->file, "treaty%d.plr1", tidx);
5396
5397 p0 = player_by_name(plr0);
5398 p1 = player_by_name(plr1);
5399
5400 if (p0 == NULL || p1 == NULL) {
5401 log_error("Treaty between unknown players %s and %s", plr0, plr1);
5402 } else {
5403 struct Treaty *ptreaty = fc_malloc(sizeof(*ptreaty));
5404
5407
5408 for (cidx = 0; (ct = secfile_lookup_str_default(loading->file, NULL,
5409 "treaty%d.clause%d.type",
5410 tidx, cidx)) != NULL ;
5411 cidx++ ) {
5413 const char *plrx;
5414
5415 if (!clause_type_is_valid(type)) {
5416 log_error("Invalid clause type \"%s\"", ct);
5417 } else {
5418 struct player *pgiver = NULL;
5419
5420 plrx = secfile_lookup_str(loading->file, "treaty%d.clause%d.from",
5421 tidx, cidx);
5422
5423 if (!fc_strcasecmp(plrx, plr0)) {
5424 pgiver = p0;
5425 } else if (!fc_strcasecmp(plrx, plr1)) {
5426 pgiver = p1;
5427 } else {
5428 log_error("Clause giver %s is not participant of the treaty"
5429 "between %s and %s", plrx, plr0, plr1);
5430 }
5431
5432 if (pgiver != NULL) {
5433 int value;
5434
5435 value = secfile_lookup_int_default(loading->file, 0,
5436 "treaty%d.clause%d.value",
5437 tidx, cidx);
5438
5439 add_clause(ptreaty, pgiver, type, value, NULL);
5440 }
5441 }
5442 }
5443
5444 /* These must be after clauses have been added so that acceptance
5445 * does not get cleared by what seems like changes to the treaty. */
5447 "treaty%d.accept0", tidx);
5449 "treaty%d.accept1", tidx);
5450 }
5451 }
5452}
5453
5454/* =======================================================================
5455 * Load the history report
5456 * ======================================================================= */
5457
5458/************************************************************************/
5462{
5464 int turn;
5465
5466 /* Check status and return if not OK (sg_success FALSE). */
5467 sg_check_ret();
5468
5469 turn = secfile_lookup_int_default(loading->file, -2, "history.turn");
5470
5471 if (turn != -2) {
5472 hist->turn = turn;
5473 }
5474
5475 if (turn + 1 >= game.info.turn) {
5476 const char *str;
5477
5478 str = secfile_lookup_str(loading->file, "history.title");
5479 sg_failure_ret(str != NULL, "%s", secfile_error());
5480 sz_strlcpy(hist->title, str);
5481 str = secfile_lookup_str(loading->file, "history.body");
5482 sg_failure_ret(str != NULL, "%s", secfile_error());
5483 sz_strlcpy(hist->body, str);
5484 }
5485}
5486
5487/* =======================================================================
5488 * Load the mapimg definitions.
5489 * ======================================================================= */
5490
5491/************************************************************************/
5494static void sg_load_mapimg(struct loaddata *loading)
5495{
5496 int mapdef_count, i;
5497
5498 /* Check status and return if not OK (sg_success FALSE). */
5499 sg_check_ret();
5500
5501 /* Clear all defined map images. */
5502 while (mapimg_count() > 0) {
5503 mapimg_delete(0);
5504 }
5505
5507 "mapimg.count");
5508 log_verbose("Saved map image definitions: %d.", mapdef_count);
5509
5510 if (0 >= mapdef_count) {
5511 return;
5512 }
5513
5514 for (i = 0; i < mapdef_count; i++) {
5515 const char *p;
5516
5517 p = secfile_lookup_str(loading->file, "mapimg.mapdef%d", i);
5518 if (NULL == p) {
5519 log_verbose("[Mapimg %4d] Missing definition.", i);
5520 continue;
5521 }
5522
5523 if (!mapimg_define(p, FALSE)) {
5524 log_error("Invalid map image definition %4d: %s.", i, p);
5525 }
5526
5527 log_verbose("Mapimg %4d loaded.", i);
5528 }
5529}
5530
5531/* =======================================================================
5532 * Sanity checks for loading a game.
5533 * ======================================================================= */
5534
5535/************************************************************************/
5539{
5540 int players;
5541
5542 /* Check status and return if not OK (sg_success FALSE). */
5543 sg_check_ret();
5544
5545 if (game.info.is_new_game) {
5546 /* Nothing to do for new games (or not started scenarios). */
5547 return;
5548 }
5549
5550 /* Old savegames may have maxplayers lower than current player count,
5551 * fix. */
5552 players = normal_player_count();
5553 if (game.server.max_players < players) {
5554 log_verbose("Max players lower than current players, fixing");
5555 game.server.max_players = players;
5556 }
5557
5558 /* Fix ferrying sanity */
5559 players_iterate(pplayer) {
5560 unit_list_iterate_safe(pplayer->units, punit) {
5563 log_sg("Removing %s unferried %s in %s at (%d, %d)",
5569 }
5572
5573 /* Fix stacking issues. We don't rely on the savegame preserving
5574 * alliance invariants (old savegames often did not) so if there are any
5575 * unallied units on the same tile we just bounce them. */
5576 players_iterate(pplayer) {
5578 resolve_unit_stacks(pplayer, aplayer, TRUE);
5581
5582 /* Recalculate the potential buildings for each city. Has caused some
5583 * problems with game random state.
5584 * This also changes the game state if you save the game directly after
5585 * loading it and compare the results. */
5586 players_iterate(pplayer) {
5587 /* Building advisor needs data phase open in order to work */
5588 adv_data_phase_init(pplayer, FALSE);
5589 building_advisor(pplayer);
5590 /* Close data phase again so it can be opened again when game starts. */
5591 adv_data_phase_done(pplayer);
5593
5594 /* Prevent a buggy or intentionally crafted save game from crashing
5595 * Freeciv. See hrm Bug #887748 */
5596 players_iterate(pplayer) {
5597 city_list_iterate(pplayer->cities, pcity) {
5598 worker_task_list_iterate(pcity->task_reqs, ptask) {
5599 if (!worker_task_is_sane(ptask)) {
5600 log_error("[city id: %d] Bad worker task %d.",
5601 pcity->id, ptask->act);
5602 worker_task_list_remove(pcity->task_reqs, ptask);
5603 free(ptask);
5604 ptask = NULL;
5605 }
5609
5610 /* Check worked tiles map */
5611#ifdef FREECIV_DEBUG
5612 if (loading->worked_tiles != NULL) {
5613 /* check the entire map for unused worked tiles */
5614 whole_map_iterate(&(wld.map), ptile) {
5615 if (loading->worked_tiles[ptile->index] != -1) {
5616 log_error("[city id: %d] Unused worked tile at (%d, %d).",
5617 loading->worked_tiles[ptile->index], TILE_XY(ptile));
5618 }
5620 }
5621#endif /* FREECIV_DEBUG */
5622
5623 /* Check researching technologies and goals. */
5625 int techs;
5626
5627 if (presearch->researching != A_UNSET
5628 && !is_future_tech(presearch->researching)
5629 && (valid_advance_by_number(presearch->researching) == NULL
5631 != TECH_PREREQS_KNOWN))) {
5632 log_sg(_("%s had invalid researching technology."),
5634 presearch->researching = A_UNSET;
5635 }
5636 if (presearch->tech_goal != A_UNSET
5637 && !is_future_tech(presearch->tech_goal)
5638 && (valid_advance_by_number(presearch->tech_goal) == NULL
5641 == TECH_KNOWN))) {
5642 log_sg(_("%s had invalid technology goal."),
5644 presearch->tech_goal = A_UNSET;
5645 }
5646
5648
5649 if (presearch->techs_researched != techs) {
5650 sg_regr(3000300,
5651 _("%s had finished researches count wrong."),
5653 presearch->techs_researched = techs;
5654 }
5656
5657 players_iterate(pplayer) {
5658 unit_list_iterate_safe(pplayer->units, punit) {
5659 if (punit->has_orders
5661 punit->orders.list)) {
5662 log_sg("Invalid unit orders for unit %d.", punit->id);
5664 }
5667
5668 /* Check max rates (rules may have changed since saving) */
5669 players_iterate(pplayer) {
5672
5673 if (0 == strlen(server.game_identifier)
5674 || !is_base64url(server.game_identifier)) {
5675 /* This uses fc_rand(), so random state has to be initialized before. */
5676 randomize_base64url_string(server.game_identifier,
5677 sizeof(server.game_identifier));
5678 }
5679
5680 /* Check if some player has more than one of some UTYF_UNIQUE unit type */
5681 players_iterate(pplayer) {
5682 int unique_count[U_LAST];
5683
5684 memset(unique_count, 0, sizeof(unique_count));
5685
5686 unit_list_iterate(pplayer->units, punit) {
5689
5692 log_sg(_("%s has multiple units of type %s though it should be possible "
5693 "to have only one."),
5695 }
5698
5699 /* Restore game random state, just in case various initialization code
5700 * inexplicably altered the previously existing state. */
5701 if (!game.info.is_new_game) {
5702 fc_rand_set_state(loading->rstate);
5703
5704 if (loading->version < 30) {
5705 /* For older savegames we have to recalculate the score with current data,
5706 * instead of using beginning-of-turn saved scores. */
5707 players_iterate(pplayer) {
5708 calc_civ_score(pplayer);
5710 }
5711 }
5712
5713 /* At the end do the default sanity checks. */
5714 sanity_check();
5715}
struct achievement * achievement_by_rule_name(const char *name)
#define ACTION_NONE
Definition actions.h:311
void building_advisor(struct player *pplayer)
bool adv_data_phase_init(struct player *pplayer, bool is_new_phase)
Definition advdata.c:263
void adv_data_phase_done(struct player *pplayer)
Definition advdata.c:565
const char * ai_name(const struct ai_type *ai)
Definition ai.c:335
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
void ai_traits_init(struct player *pplayer)
Definition aitraits.c:33
#define str
Definition astring.c:76
Base_type_id base_number(const struct base_type *pbase)
Definition base.c:92
struct extra_type * base_extra_get(const struct base_type *pbase)
Definition base.c:101
struct base_type * get_base_by_gui_type(enum base_gui_type type, const struct unit *punit, const struct tile *ptile)
Definition base.c:139
struct base_type * base_by_number(const Base_type_id id)
Definition base.c:76
void dbv_set(struct dbv *pdbv, int bit)
Definition bitvector.c:144
void dbv_clr_all(struct dbv *pdbv)
Definition bitvector.c:179
void dbv_to_bv(unsigned char *dest, const struct dbv *src)
Definition bitvector.c:235
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
#define BV_CLR(bv, bit)
Definition bitvector.h:86
bool has_capability(const char *cap, const char *capstr)
Definition capability.c:77
void citizens_nation_set(struct city *pcity, const struct player_slot *pslot, citizens count)
Definition citizens.c:145
citizens citizens_count(const struct city *pcity)
Definition citizens.c:162
void citizens_init(struct city *pcity)
Definition citizens.c:32
void citizens_update(struct city *pcity, struct player *plr)
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Definition city.c:148
void city_name_set(struct city *pcity, const char *new_name)
Definition city.c:1145
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3430
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2870
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1180
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3357
void destroy_city_virtual(struct city *pcity)
Definition city.c:3516
int city_style_by_rule_name(const char *s)
Definition city.c:1738
#define cities_iterate_end
Definition city.h:517
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define cities_iterate(pcity)
Definition city.h:512
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:86
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define output_type_iterate(output)
Definition city.h:845
#define city_owner(_pcity_)
Definition city.h:563
#define FREE_WORKED_TILES
Definition city.h:882
#define MAX_CITY_SIZE
Definition city.h:106
#define city_list_iterate_end
Definition city.h:510
#define I_NEVER
Definition city.h:247
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:230
#define city_tile_iterate_end
Definition city.h:238
#define output_type_iterate_end
Definition city.h:851
bool update_dumb_city(struct player *pplayer, struct city *pcity)
Definition citytools.c:2769
bool send_city_suppression(bool now)
Definition citytools.c:2166
static void void city_freeze_workers(struct city *pcity)
Definition citytools.c:137
void city_thaw_workers(struct city *pcity)
Definition citytools.c:147
void reality_check_city(struct player *pplayer, struct tile *ptile)
Definition citytools.c:2840
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3442
void auto_arrange_workers(struct city *pcity)
Definition cityturn.c:367
void city_repair_size(struct city *pcity, int change)
Definition cityturn.c:852
bool city_refresh(struct city *pcity)
Definition cityturn.c:159
char * techs
Definition comments.c:31
char * incite_cost
Definition comments.c:75
static void road(QVariant data1, QVariant data2)
Definition dialogs.cpp:2919
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2940
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
void set_ai_level_directer(struct player *pplayer, enum ai_level level)
Definition difficulty.c:39
enum diplstate_type valid_dst_closest(struct player_diplstate *dst)
Definition diplhand.c:111
struct Treaty * ptreaty
Definition diplodlg_g.h:28
void init_treaty(struct Treaty *ptreaty, struct player *plr0, struct player *plr1)
Definition diptreaty.c:99
bool add_clause(struct Treaty *ptreaty, struct player *pfrom, enum clause_type type, int val, struct player *client_player)
Definition diptreaty.c:145
void treaty_add(struct Treaty *ptreaty)
Definition diptreaty.c:361
int int id
Definition editgui_g.h:28
struct extra_type * next_extra_for_tile(const struct tile *ptile, enum extra_cause cause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:765
struct extra_type * extra_type_by_rule_name(const char *name)
Definition extras.c:212
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1114
int extra_number(const struct extra_type *pextra)
Definition extras.c:161
struct extra_type * prev_extra_in_tile(const struct tile *ptile, enum extra_rmcause rmcause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:790
static struct extra_type extras[MAX_EXTRA_TYPES]
Definition extras.c:31
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
#define is_extra_caused_by(e, c)
Definition extras.h:203
#define extra_index(_e_)
Definition extras.h:183
#define EXTRA_NONE
Definition extras.h:85
#define extra_base_get(_e_)
Definition extras.h:190
#define extra_road_get(_e_)
Definition extras.h:191
#define extra_type_by_cause_iterate_end
Definition extras.h:339
#define extra_type_by_cause_iterate(_cause, _extra)
Definition extras.h:333
static char * ruleset
Definition fc_manual.c:163
#define NO_TARGET
Definition fc_types.h:354
int Road_type_id
Definition fc_types.h:384
@ ROCO_RAILROAD
Definition fc_types.h:1253
@ ROCO_RIVER
Definition fc_types.h:1253
@ ROCO_ROAD
Definition fc_types.h:1253
int Tech_type_id
Definition fc_types.h:377
unsigned char citizens
Definition fc_types.h:388
@ RPT_POSSIBLE
Definition fc_types.h:700
int Base_type_id
Definition fc_types.h:383
int Multiplier_type_id
Definition fc_types.h:386
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:92
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct unit * game_unit_by_number(int id)
Definition game.c:116
void initialize_globals(void)
Definition game.c:683
struct city * game_city_by_number(int id)
Definition game.c:107
#define GAME_DEFAULT_TIMEOUTINTINC
Definition game.h:598
#define GAME_DEFAULT_SCORETURN
Definition game.h:582
#define GAME_DEFAULT_TIMEOUTINT
Definition game.h:597
#define GAME_DEFAULT_TIMEOUTINCMULT
Definition game.h:600
#define GAME_DEFAULT_TIMEOUTINC
Definition game.h:599
#define GAME_DEFAULT_RULESETDIR
Definition game.h:676
#define GAME_DEFAULT_TIMEOUTCOUNTER
Definition game.h:602
#define GAME_DEFAULT_PHASE_MODE
Definition game.h:617
struct government * government_by_rule_name(const char *name)
Definition government.c:55
struct city * owner
Definition citydlg.c:226
GType type
Definition repodlgs.c:1313
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:82
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:67
Impr_type_id improvement_index(const struct impr_type *pimprove)
struct impr_type * improvement_by_rule_name(const char *name)
#define WONDER_DESTROYED
#define WONDER_LOST
void adv_city_free(struct city *pcity)
Definition infracache.c:501
void adv_city_alloc(struct city *pcity)
Definition infracache.c:488
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#define log_fatal(message,...)
Definition log.h:100
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_debug(message,...)
Definition log.h:115
#define log_normal(message,...)
Definition log.h:107
#define log_error(message,...)
Definition log.h:103
bool startpos_disallow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1660
#define nat_x
#define nat_y
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:641
struct startpos * map_startpos_new(struct tile *ptile)
Definition map.c:1866
void map_init_topology(struct civ_map *nmap)
Definition map.c:303
void main_map_allocate(void)
Definition map.c:519
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
int map_startpos_count(void)
Definition map.c:1853
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:443
bool startpos_allow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1643
#define MAP_INDEX_SIZE
Definition map.h:137
#define whole_map_iterate(_map, _tile)
Definition map.h:545
#define index_to_native_pos(pnat_x, pnat_y, mindex)
Definition map.h:157
#define whole_map_iterate_end
Definition map.h:554
@ MAPGEN_SCENARIO
Definition map_types.h:47
void assign_continent_numbers(void)
void player_map_init(struct player *pplayer)
Definition maphand.c:1221
void update_player_tile_last_seen(struct player *pplayer, struct tile *ptile)
Definition maphand.c:1467
void map_claim_ownership(struct tile *ptile, struct player *powner, struct tile *psource, bool claim_bases)
Definition maphand.c:2207
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:894
bool send_tile_suppression(bool now)
Definition maphand.c:475
bool really_gives_vision(struct player *me, struct player *them)
Definition maphand.c:345
void map_know_and_see_all(struct player *pplayer)
Definition maphand.c:1196
bool update_player_tile_knowledge(struct player *pplayer, struct tile *ptile)
Definition maphand.c:1398
void tile_claim_bases(struct tile *ptile, struct player *powner)
Definition maphand.c:2220
void map_set_known(struct tile *ptile, struct player *pplayer)
Definition maphand.c:1178
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:920
void change_playertile_site(struct player_tile *ptile, struct vision_site *new_site)
Definition maphand.c:1159
void map_calculate_borders(void)
Definition maphand.c:2373
void give_shared_vision(struct player *pfrom, struct player *pto)
Definition maphand.c:1632
struct player_tile * map_get_player_tile(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:1382
bool mapimg_define(const char *maparg, bool check)
Definition mapimg.c:769
bool mapimg_delete(int id)
Definition mapimg.c:1204
int mapimg_count(void)
Definition mapimg.c:573
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_strdup(str)
Definition mem.h:43
#define fc_malloc(sz)
Definition mem.h:34
void set_meta_patches_string(const char *string)
Definition meta.c:172
const char * default_meta_patches_string(void)
Definition meta.c:83
#define DEFAULT_META_SERVER_ADDR
Definition meta.h:21
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:351
const char * multiplier_rule_name(const struct multiplier *pmul)
struct multiplier * multiplier_by_rule_name(const char *name)
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
#define multipliers_iterate(_mul_)
Definition multipliers.h:61
#define multipliers_iterate_end
Definition multipliers.h:67
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:121
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
#define NO_NATION_SELECTED
Definition nation.h:30
void event_cache_load(struct section_file *file, const char *section)
Definition notify.c:783
int parts
Definition packhand.c:132
char * lines
Definition packhand.c:131
int len
Definition packhand.c:127
bool player_slot_is_used(const struct player_slot *pslot)
Definition player.c:448
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1229
struct player * player_by_number(const int player_id)
Definition player.c:849
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1476
int player_count(void)
Definition player.c:817
int player_slot_count(void)
Definition player.c:418
struct player_slot * player_slot_by_number(int player_id)
Definition player.c:463
int player_number(const struct player *pplayer)
Definition player.c:837
enum dipl_reason pplayer_can_make_treaty(const struct player *p1, const struct player *p2, enum diplstate_type treaty)
Definition player.c:159
const char * player_name(const struct player *pplayer)
Definition player.c:895
int player_slot_max_used_number(void)
Definition player.c:476
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
int player_slot_index(const struct player_slot *pslot)
Definition player.c:426
struct player * player_by_name(const char *name)
Definition player.c:881
bool player_has_flag(const struct player *pplayer, enum plr_flag_id flag)
Definition player.c:1990
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1203
int player_index(const struct player *pplayer)
Definition player.c:829
bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
Definition player.c:861
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:324
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1405
struct player_slot * slots
Definition player.c:51
#define players_iterate_end
Definition player.h:537
dipl_reason
Definition player.h:190
@ DIPL_ALLIANCE_PROBLEM_THEM
Definition player.h:192
@ DIPL_ALLIANCE_PROBLEM_US
Definition player.h:192
#define players_iterate(_pplayer)
Definition player.h:532
#define MAX_ATTRIBUTE_BLOCK
Definition player.h:221
#define player_list_iterate(playerlist, pplayer)
Definition player.h:555
static bool is_barbarian(const struct player *pplayer)
Definition player.h:489
#define player_slots_iterate(_pslot)
Definition player.h:523
#define is_ai(plr)
Definition player.h:230
#define player_list_iterate_end
Definition player.h:557
#define players_iterate_alive_end
Definition player.h:547
#define player_slots_iterate_end
Definition player.h:527
#define players_iterate_alive(_pplayer)
Definition player.h:542
void server_player_set_name(struct player *pplayer, const char *name)
Definition plrhand.c:2268
struct player * server_create_player(int player_id, const char *ai_tname, struct rgbcolor *prgbcolor, bool allow_ai_type_fallbacking)
Definition plrhand.c:1894
int normal_player_count(void)
Definition plrhand.c:3205
void player_limit_to_max_rates(struct player *pplayer)
Definition plrhand.c:2057
struct nation_type * pick_a_nation(const struct nation_list *choices, bool ignore_conflicts, bool needs_startpos, enum barbarian_type barb_type)
Definition plrhand.c:2453
void set_shuffled_players(int *shuffled_players)
Definition plrhand.c:2403
void player_delegation_set(struct player *pplayer, const char *username)
Definition plrhand.c:3251
void shuffle_players(void)
Definition plrhand.c:2378
void server_remove_player(struct player *pplayer)
Definition plrhand.c:1943
void server_player_init(struct player *pplayer, bool initmap, bool needs_team)
Definition plrhand.c:1619
void assign_player_colors(void)
Definition plrhand.c:1734
void fit_nationset_to_players(void)
Definition plrhand.c:2659
RANDOM_STATE fc_rand_state(void)
Definition rand.c:208
void fc_rand_set_state(RANDOM_STATE state)
Definition rand.c:229
const char * secfile_error(void)
bool secfile_lookup_int(const struct section_file *secfile, int *ival, const char *path,...)
const char ** secfile_lookup_str_vec(const struct section_file *secfile, size_t *dim, const char *path,...)
struct entry * secfile_entry_lookup(const struct section_file *secfile, const char *path,...)
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
struct entry * secfile_entry_by_path(const struct section_file *secfile, const char *path)
struct section * secfile_section_lookup(const struct section_file *secfile, const char *path,...)
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
bool secfile_lookup_bool(const struct section_file *secfile, bool *bval, const char *path,...)
#define secfile_lookup_enum_default(secfile, defval, specenum_type, path,...)
#define secfile_entry_ignore(_sfile_, _fmt_,...)
struct history_report * history_report_get(void)
Definition report.c:1824
bool are_reqs_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
struct universal universal_by_rule_name(const char *kind, const char *value)
bool research_invention_reachable(const struct research *presearch, const Tech_type_id tech)
Definition research.c:668
const char * research_name_translation(const struct research *presearch)
Definition research.c:156
enum tech_state research_invention_set(struct research *presearch, Tech_type_id tech, enum tech_state value)
Definition research.c:637
struct research * research_by_number(int number)
Definition research.c:118
int recalculate_techs_researched(const struct research *presearch)
Definition research.c:1345
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:619
void research_update(struct research *presearch)
Definition research.c:501
#define researches_iterate(_presearch)
Definition research.h:155
#define researches_iterate_end
Definition research.h:158
void rgbcolor_destroy(struct rgbcolor *prgbcolor)
Definition rgbcolor.c:74
bool rgbcolor_load(struct section_file *file, struct rgbcolor **prgbcolor, char *path,...)
Definition rgbcolor.c:90
struct extra_type * road_extra_get(const struct road_type *proad)
Definition road.c:42
Road_type_id road_number(const struct road_type *proad)
Definition road.c:32
struct road_type * road_by_number(Road_type_id id)
Definition road.c:58
struct road_type * road_by_compat_special(enum road_compat compat)
Definition road.c:160
bool load_rulesets(const char *restore, const char *alt, bool compat_mode, rs_conversion_logger logger, bool act, bool buffer_script, bool load_luadata)
Definition ruleset.c:9362
#define sanity_check()
Definition sanitycheck.h:43
#define sanity_check_city(x)
Definition sanitycheck.h:41
struct extra_type * resource_by_identifier(const char identifier)
Definition savecompat.c:306
static struct compatibility compat[]
Definition savecompat.c:105
int char2num(char ch)
Definition savecompat.c:251
void sg_load_compat(struct loaddata *loading, enum sgf_version format_class)
Definition savecompat.c:138
enum ai_level ai_level_convert(int old_level)
int ascii_hex2bin(char ch, int halfbyte)
Definition savecompat.c:227
struct extra_type * special_extra_get(int spe)
Definition savecompat.c:292
enum tile_special_type special_by_rule_name(const char *name)
Definition savecompat.c:266
void sg_load_post_load_compat(struct loaddata *loading, enum sgf_version format_class)
Definition savecompat.c:183
const char * special_rule_name(enum tile_special_type type)
Definition savecompat.c:282
#define sg_check_ret(...)
Definition savecompat.h:150
#define sg_warn(condition, message,...)
Definition savecompat.h:160
tile_special_type
Definition savecompat.h:29
@ S_MINE
Definition savecompat.h:31
@ S_HUT
Definition savecompat.h:33
@ S_FALLOUT
Definition savecompat.h:35
@ S_POLLUTION
Definition savecompat.h:32
@ S_OLD_RIVER
Definition savecompat.h:44
@ S_FARMLAND
Definition savecompat.h:34
@ S_OLD_ROAD
Definition savecompat.h:42
@ S_LAST
Definition savecompat.h:38
@ S_IRRIGATION
Definition savecompat.h:30
@ S_OLD_RAILROAD
Definition savecompat.h:43
#define hex_chars
Definition savecompat.h:205
#define sg_failure_ret_val(condition, _val, message,...)
Definition savecompat.h:184
#define sg_failure_ret(condition, message,...)
Definition savecompat.h:177
#define MAX_TRADE_ROUTES_OLD
Definition savecompat.h:222
#define sg_regr(fixversion, message,...)
Definition savecompat.h:193
@ SAVEGAME_2
Definition savecompat.h:27
#define log_sg
Definition savecompat.h:146
#define sg_warn_ret_val(condition, _val, message,...)
Definition savecompat.h:171
static void unit_ordering_apply(void)
Definition savegame2.c:834
static void sg_load_players_basic(struct loaddata *loading)
Definition savegame2.c:2689
static struct loaddata * loaddata_new(struct section_file *file)
Definition savegame2.c:471
#define ACTIVITY_OLD_POLLUTION_SG2
Definition savegame2.c:145
static void sg_load_map_known(struct loaddata *loading)
Definition savegame2.c:2624
#define halfbyte_iterate_roads_end
Definition savegame2.c:280
static struct extra_type * char2resource(char c)
Definition savegame2.c:1316
static void sg_load_map_owner(struct loaddata *loading)
Definition savegame2.c:2493
bool sg_success
Definition savecompat.c:34
#define halfbyte_iterate_extras_end
Definition savegame2.c:250
static void sg_load_map_tiles_roads(struct loaddata *loading)
Definition savegame2.c:2332
#define halfbyte_iterate_extras(e, num_extras_types)
Definition savegame2.c:245
static void sg_load_map(struct loaddata *loading)
Definition savegame2.c:2175
static enum unit_orders char2order(char order)
Definition savegame2.c:597
static int unquote_block(const char *const quoted_, void *dest, int dest_length)
Definition savegame2.c:740
static void sg_bases_set_bv(bv_extras *extras, char ch, struct base_type **idx)
Definition savegame2.c:1227
#define LOAD_MAP_CHAR(ch, ptile, SET_XY_CHAR, secfile, secpath,...)
Definition savegame2.c:209
#define ACTIVITY_OLD_RAILROAD
Definition savegame2.c:144
static void sg_load_player_city_citizens(struct loaddata *loading, struct player *plr, struct city *pcity, const char *citystr)
Definition savegame2.c:4044
static void sg_load_player_cities(struct loaddata *loading, struct player *plr)
Definition savegame2.c:3645
static void sg_load_map_tiles(struct loaddata *loading)
Definition savegame2.c:2260
static char activity2char(int activity)
Definition savegame2.c:665
static void sg_load_savefile(struct loaddata *loading)
Definition savegame2.c:1427
#define ACTIVITY_OLD_FALLOUT_SG2
Definition savegame2.c:146
static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, struct unit *punit, const char *unitstr)
Definition savegame2.c:4211
static void set_unit_activity_base(struct unit *punit, Base_type_id base)
Definition savegame2.c:573
static bool sg_load_player_city(struct loaddata *loading, struct player *plr, struct city *pcity, const char *citystr, int wlist_max_length)
Definition savegame2.c:3760
static void sg_load_settings(struct loaddata *loading)
Definition savegame2.c:2155
static void sg_bases_set_dbv(struct dbv *extras, char ch, struct base_type **idx)
Definition savegame2.c:1195
static void sg_load_player_units(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4092
#define halfbyte_iterate_special(s, num_specials_types)
Definition savegame2.c:255
void savegame2_load(struct section_file *file)
Definition savegame2.c:402
static void sg_load_researches(struct loaddata *loading)
Definition savegame2.c:5269
#define halfbyte_iterate_bases_end
Definition savegame2.c:270
static void sg_load_map_worked(struct loaddata *loading)
Definition savegame2.c:2580
static void sg_load_random(struct loaddata *loading)
Definition savegame2.c:1986
#define halfbyte_iterate_special_end
Definition savegame2.c:260
#define halfbyte_iterate_bases(b, num_bases_types)
Definition savegame2.c:265
static void sg_load_player_units_transport(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4808
static void sg_load_history(struct loaddata *loading)
Definition savegame2.c:5461
static int char2activity(char activity)
Definition savegame2.c:719
#define ORDER_OLD_BUILD_WONDER
Definition savegame2.c:288
static void sg_load_map_tiles_specials(struct loaddata *loading, bool rivers_overlay)
Definition savegame2.c:2348
#define TOKEN_SIZE
Definition savegame2.c:284
static void sg_load_script(struct loaddata *loading)
Definition savegame2.c:2045
static void sg_load_scenario(struct loaddata *loading)
Definition savegame2.c:2060
static void sg_load_ruleset(struct loaddata *loading)
Definition savegame2.c:1393
static void sg_load_game(struct loaddata *loading)
Definition savegame2.c:1837
static void sg_load_player_main(struct loaddata *loading, struct player *plr)
Definition savegame2.c:3138
#define ACTIVITY_OLD_ROAD
Definition savegame2.c:143
static void sg_load_treaties(struct loaddata *loading)
Definition savegame2.c:5379
static void sg_extras_set_bv(bv_extras *extras, char ch, struct extra_type **idx)
Definition savegame2.c:888
static void set_unit_activity_road(struct unit *punit, Road_type_id road)
Definition savegame2.c:583
static Tech_type_id technology_load(struct section_file *file, const char *path, int plrno)
Definition savegame2.c:1353
static enum direction8 char2dir(char dir)
Definition savegame2.c:636
static void sg_load_map_tiles_resources(struct loaddata *loading)
Definition savegame2.c:2379
#define ORDER_OLD_BUILD_CITY
Definition savegame2.c:286
static struct terrain * char2terrain(char ch)
Definition savegame2.c:1331
#define ORDER_OLD_DISBAND
Definition savegame2.c:287
static void sg_load_mapimg(struct loaddata *loading)
Definition savegame2.c:5494
#define ORDER_OLD_HOMECITY
Definition savegame2.c:290
static void sg_special_set_bv(struct tile *ptile, bv_extras *extras, char ch, const enum tile_special_type *idx, bool rivers_overlay)
Definition savegame2.c:1058
static void sg_load_player_attributes(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4861
static void sg_load_ruledata(struct loaddata *loading)
Definition savegame2.c:1813
static void sg_special_set_dbv(struct tile *ptile, struct dbv *extras, char ch, const enum tile_special_type *idx, bool rivers_overlay)
Definition savegame2.c:921
static void sg_load_player_vision(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4941
static void worklist_load(struct section_file *file, int wlist_max_length, struct worklist *pwl, const char *path,...)
Definition savegame2.c:788
static bool sg_load_player_vision_city(struct loaddata *loading, struct player *plr, struct vision_site *pdcity, const char *citystr)
Definition savegame2.c:5162
static void sg_roads_set_bv(bv_extras *extras, char ch, struct road_type **idx)
Definition savegame2.c:1289
static void sg_roads_set_dbv(struct dbv *extras, char ch, struct road_type **idx)
Definition savegame2.c:1258
static void sg_load_map_startpos(struct loaddata *loading)
Definition savegame2.c:2406
static void loaddata_destroy(struct loaddata *loading)
Definition savegame2.c:512
static void sg_load_players(struct loaddata *loading)
Definition savegame2.c:2946
#define ORDER_OLD_TRADE_ROUTE
Definition savegame2.c:289
static void sg_load_map_tiles_extras(struct loaddata *loading)
Definition savegame2.c:2300
static void sg_load_sanitycheck(struct loaddata *loading)
Definition savegame2.c:5538
static void sg_load_event_cache(struct loaddata *loading)
Definition savegame2.c:5364
static void sg_load_map_tiles_bases(struct loaddata *loading)
Definition savegame2.c:2316
static void sg_extras_set_dbv(struct dbv *extras, char ch, struct extra_type **idx)
Definition savegame2.c:855
#define ACTIVITY_LAST_SAVEGAME2
Definition savegame2.c:147
static int sg_order_to_action(int order, struct unit *act_unit, struct tile *tgt_tile)
Definition savegame2.c:4170
#define halfbyte_iterate_roads(r, num_roads_types)
Definition savegame2.c:275
void calc_civ_score(struct player *pplayer)
Definition score.c:251
void script_server_state_load(struct section_file *file)
void settings_game_load(struct section_file *file, const char *section)
Definition settings.c:4946
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
bool str_to_int(const char *str, int *pint)
Definition shared.c:517
bool is_base64url(const char *s)
Definition shared.c:318
char scanin(const char **buf, char *delimiters, char *dest, int size)
Definition shared.c:1922
void randomize_base64url_string(char *s, size_t n)
Definition shared.c:339
#define CLIP(lower, current, upper)
Definition shared.h:57
#define ARRAY_SIZE(x)
Definition shared.h:85
void spaceship_calc_derived(struct player_spaceship *ship)
Definition spacerace.c:46
void spaceship_init(struct player_spaceship *ship)
Definition spaceship.c:96
#define NUM_SS_STRUCTURALS
Definition spaceship.h:87
@ SSHIP_LAUNCHED
Definition spaceship.h:85
@ SSHIP_NONE
Definition spaceship.h:84
struct specialist * specialist_by_rule_name(const char *name)
Definition specialist.c:112
Specialist_type_id specialist_index(const struct specialist *sp)
Definition specialist.c:82
#define specialist_type_iterate_end
Definition specialist.h:79
#define specialist_type_iterate(sp)
Definition specialist.h:73
#define DEFAULT_SPECIALIST
Definition specialist.h:43
size_t size
Definition specvec.h:72
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
void server_game_init(bool keep_ruleset_value)
Definition srv_main.c:3464
const char * aifill(int amount)
Definition srv_main.c:2466
bool game_was_started(void)
Definition srv_main.c:349
void identity_number_reserve(int id)
Definition srv_main.c:1994
struct server_arguments srvarg
Definition srv_main.c:176
void init_game_seed(void)
Definition srv_main.c:203
void update_nations_with_startpos(void)
Definition srv_main.c:2271
void server_game_free(void)
Definition srv_main.c:3488
bool accept0
Definition diptreaty.h:80
bool accept1
Definition diptreaty.h:80
struct player * first
int val
Definition traits.h:38
int mod
Definition traits.h:39
int turn
Definition city.h:246
Definition city.h:320
struct worker_task_list * task_reqs
Definition city.h:412
int turn_last_built
Definition city.h:387
enum city_wl_cancel_behavior wlcb
Definition city.h:404
int food_stock
Definition city.h:367
struct built_status built[B_LAST]
Definition city.h:394
struct player * original
Definition city.h:324
int history
Definition city.h:410
bool did_sell
Definition city.h:380
int id
Definition city.h:326
int last_turns_shield_surplus
Definition city.h:392
int disbanded_shields
Definition city.h:391
int turn_plague
Definition city.h:374
bv_city_options city_options
Definition city.h:403
bool was_happy
Definition city.h:381
enum city_acquire_type acquire_t
Definition city.h:329
int turn_founded
Definition city.h:386
int airlift
Definition city.h:378
int caravan_shields
Definition city.h:390
bool did_buy
Definition city.h:379
struct trade_route_list * routes
Definition city.h:344
int anarchy
Definition city.h:384
struct worklist worklist
Definition city.h:401
struct universal production
Definition city.h:396
citizens * nationality
Definition city.h:341
int steal
Definition city.h:414
int before_change_shields
Definition city.h:389
int style
Definition city.h:327
bool had_famine
Definition city.h:382
bool synced
Definition city.h:448
citizens specialists[SP_MAX]
Definition city.h:336
struct tile * tile
Definition city.h:322
int shield_stock
Definition city.h:368
struct vision * vision
Definition city.h:455
struct city::@17::@19 server
struct universal changed_from
Definition city.h:399
struct unit_list * units_supported
Definition city.h:406
int rapture
Definition city.h:385
bool last_updated_year
Definition game.h:240
float turn_change_time
Definition game.h:222
bool vision_reveal_tiles
Definition game.h:204
struct packet_scenario_description scenario_desc
Definition game.h:88
struct packet_ruleset_control control
Definition game.h:83
bool fogofwar_old
Definition game.h:238
struct packet_game_info info
Definition game.h:89
int timeoutcounter
Definition game.h:211
char rulesetdir[MAX_LEN_NAME]
Definition game.h:242
int scoreturn
Definition game.h:229
randseed seed
Definition game.h:231
struct packet_scenario_info scenario
Definition game.h:87
int timeoutint
Definition game.h:207
unsigned revealmap
Definition game.h:181
char orig_game_version[MAX_LEN_NAME]
Definition game.h:225
bool foggedborders
Definition game.h:151
struct civ_game::@31::@35 server
int timeoutincmult
Definition game.h:209
int timeoutinc
Definition game.h:208
int phase_mode_stored
Definition game.h:220
int max_players
Definition game.h:160
int timeoutintinc
Definition game.h:210
int xsize
Definition map_types.h:78
randseed seed
Definition map_types.h:92
int ysize
Definition map_types.h:78
bool have_resources
Definition map_types.h:108
struct civ_map::@42::@44 server
bool have_huts
Definition map_types.h:107
enum map_generator generator
Definition map_types.h:98
int changed_to_times
Definition government.h:64
const char ** order
Definition savecompat.h:54
struct section_file * file
Definition savecompat.h:48
int great_wonder_owners[B_LAST]
bool global_advances[A_LAST]
enum ai_level skill_level
enum phase_mode_type phase_mode
char description[MAX_LEN_CONTENT]
char authors[MAX_LEN_PACKET/3]
enum ai_level skill_level
Definition player.h:114
struct ai_trait * traits
Definition player.h:124
enum barbarian_type barbarian_type
Definition player.h:120
int science_cost
Definition player.h:117
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:122
int expand
Definition player.h:116
int fuzzy
Definition player.h:115
enum diplstate_type type
Definition player.h:197
int units_killed
Definition player.h:103
int landarea
Definition player.h:92
int population
Definition player.h:94
int pollution
Definition player.h:97
int wonders
Definition player.h:89
int settledarea
Definition player.h:93
int units_used
Definition player.h:106
int specialists[SP_MAX]
Definition player.h:88
int units_lost
Definition player.h:104
int angry
Definition player.h:87
int techout
Definition player.h:91
int units
Definition player.h:96
int units_built
Definition player.h:102
int content
Definition player.h:85
int happy
Definition player.h:84
int spaceship
Definition player.h:101
int culture
Definition player.h:107
int unhappy
Definition player.h:86
int cities
Definition player.h:95
int literacy
Definition player.h:98
int techs
Definition player.h:90
struct player * extras_owner
Definition maphand.h:35
struct player * owner
Definition maphand.h:34
struct city_list * cities
Definition player.h:279
int bulbs_last_turn
Definition player.h:349
struct player_ai ai_common
Definition player.h:286
bv_plr_flags flags
Definition player.h:290
bool is_male
Definition player.h:255
int wonders[B_LAST]
Definition player.h:303
bool unassigned_ranked
Definition player.h:253
struct government * target_government
Definition player.h:257
char username[MAX_LEN_NAME]
Definition player.h:250
int revolution_finishes
Definition player.h:271
int nturns_idle
Definition player.h:263
struct government * government
Definition player.h:256
struct team * team
Definition player.h:259
int turns_alive
Definition player.h:264
struct unit_list * units
Definition player.h:280
char ranked_username[MAX_LEN_NAME]
Definition player.h:252
int huts
Definition player.h:347
bool is_alive
Definition player.h:266
bv_player real_embassy
Definition player.h:275
struct player_economic economic
Definition player.h:282
struct player_spaceship spaceship
Definition player.h:284
struct attribute_block_s attribute_block
Definition player.h:305
struct player_score score
Definition player.h:281
struct multiplier_value multipliers[MAX_NUM_MULTIPLIERS]
Definition player.h:312
struct nation_type * nation
Definition player.h:258
struct nation_style * style
Definition player.h:277
bool border_vision
Definition player.h:325
bool phase_done
Definition player.h:261
struct player::@70::@72 server
int history
Definition player.h:314
char orig_username[MAX_LEN_NAME]
Definition player.h:345
int last_war_action
Definition player.h:268
bool unassigned_user
Definition player.h:251
const struct tile * tile
char metaserver_addr[256]
Definition srv_main.h:29
char serverid[256]
Definition srv_main.h:49
Definition map.c:41
Definition team.c:40
Definition tile.h:50
int index
Definition tile.h:51
bv_extras extras
Definition tile.h:55
struct unit_list * units
Definition tile.h:58
struct tile * claimer
Definition tile.h:64
enum unit_orders order
Definition unit.h:93
Definition unit.h:138
int length
Definition unit.h:195
int upkeep[O_LAST]
Definition unit.h:148
bool has_orders
Definition unit.h:193
struct unit::@80 orders
enum action_decision action_decision_want
Definition unit.h:202
int battlegroup
Definition unit.h:191
enum unit_activity activity
Definition unit.h:157
int moves_left
Definition unit.h:150
int id
Definition unit.h:145
int ord_city
Definition unit.h:242
bool moved
Definition unit.h:173
int ord_map
Definition unit.h:241
int index
Definition unit.h:195
struct vision * vision
Definition unit.h:244
bool vigilant
Definition unit.h:197
int hp
Definition unit.h:151
int fuel
Definition unit.h:153
struct extra_type * changed_from_target
Definition unit.h:170
int current_form_turn
Definition unit.h:208
enum direction8 facing
Definition unit.h:142
struct unit::@81::@84 server
struct extra_type * activity_target
Definition unit.h:164
int activity_count
Definition unit.h:162
struct unit_order * list
Definition unit.h:198
enum unit_activity changed_from
Definition unit.h:168
struct player * nationality
Definition unit.h:144
bool repeat
Definition unit.h:196
int homecity
Definition unit.h:146
bool paradropped
Definition unit.h:174
bool done_moving
Definition unit.h:181
int birth_turn
Definition unit.h:207
struct tile * goto_tile
Definition unit.h:155
struct tile * action_decision_tile
Definition unit.h:203
int veteran
Definition unit.h:152
int changed_from_count
Definition unit.h:169
enum server_side_agent ssa_controller
Definition unit.h:172
enum universals_n kind
Definition fc_types.h:902
struct civ_map map
int city_style(struct city *pcity)
Definition style.c:241
struct nation_style * style_by_rule_name(const char *name)
Definition style.c:117
struct nation_style * style_by_number(int id)
Definition style.c:88
const char * style_rule_name(const struct nation_style *pstyle)
Definition style.c:108
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:900
#define sz_strlcpy(dest, src)
Definition support.h:195
#define RETURN_VALUE_AFTER_EXIT(_val_)
Definition support.h:146
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct team_slot * team_slot_by_number(int team_id)
Definition team.c:175
bool team_add_player(struct player *pplayer, struct team *pteam)
Definition team.c:467
struct team * team_new(struct team_slot *tslot)
Definition team.c:317
const struct player_list * team_members(const struct team *pteam)
Definition team.c:456
bool is_future_tech(Tech_type_id tech)
Definition tech.c:281
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176
struct advance * advance_by_rule_name(const char *name)
Definition tech.c:200
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
#define A_FUTURE
Definition tech.h:46
#define A_NONE
Definition tech.h:43
#define A_UNSET
Definition tech.h:48
#define A_UNKNOWN
Definition tech.h:49
void init_tech(struct research *research, bool update)
Definition techtools.c:1094
struct terrain * terrain_by_rule_name(const char *name)
Definition terrain.c:186
const char * terrain_rule_name(const struct terrain *pterrain)
Definition terrain.c:247
bool terrain_has_resource(const struct terrain *pterrain, const struct extra_type *presource)
Definition terrain.c:255
#define terrain_type_iterate(_p)
Definition terrain.h:373
#define T_UNKNOWN
Definition terrain.h:57
#define TERRAIN_UNKNOWN_IDENTIFIER
Definition terrain.h:195
#define terrain_type_iterate_end
Definition terrain.h:379
#define RESOURCE_NONE_IDENTIFIER
Definition terrain.h:47
#define RESOURCE_NULL_IDENTIFIER
Definition terrain.h:46
bool tile_has_claimable_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:215
void tile_virtual_destroy(struct tile *vtile)
Definition tile.c:1033
struct tile * tile_virtual_new(const struct tile *ptile)
Definition tile.c:981
bool tile_set_label(struct tile *ptile, const char *label)
Definition tile.c:1095
void tile_set_resource(struct tile *ptile, struct extra_type *presource)
Definition tile.c:349
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
void tile_set_worked(struct tile *ptile, struct city *pcity)
Definition tile.c:106
#define tile_index(_pt_)
Definition tile.h:88
#define tile_worked(_tile)
Definition tile.h:118
#define tile_terrain(_tile)
Definition tile.h:114
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_has_extra(ptile, pextra)
Definition tile.h:151
#define tile_owner(_tile)
Definition tile.h:96
struct goods_type * goods_by_number(Goods_type_id id)
void free_unit_orders(struct unit *punit)
Definition unit.c:1777
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2378
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1096
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2449
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:849
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target)
Definition unit.c:1113
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1632
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2657
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1737
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1263
#define unit_tile(_pu)
Definition unit.h:397
#define BATTLEGROUP_NONE
Definition unit.h:190
unit_orders
Definition unit.h:37
@ ORDER_ACTION_MOVE
Definition unit.h:45
@ ORDER_ACTIVITY
Definition unit.h:41
@ ORDER_FULL_MP
Definition unit.h:43
@ ORDER_MOVE
Definition unit.h:39
@ ORDER_LAST
Definition unit.h:49
@ ORDER_PERFORM_ACTION
Definition unit.h:47
#define unit_owner(_pu)
Definition unit.h:396
void unit_list_sort_ord_map(struct unit_list *punitlist)
Definition unitlist.c:73
void unit_list_sort_ord_city(struct unit_list *punitlist)
Definition unitlist.c:85
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
void resolve_unit_stacks(struct player *pplayer, struct player *aplayer, bool verbose)
Definition unittools.c:1395
void unit_refresh_vision(struct unit *punit)
Definition unittools.c:4841
void bounce_unit(struct unit *punit, bool verbose)
Definition unittools.c:1222
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int utype_upkeep_cost(const struct unit_type *ut, struct player *pplayer, Output_type_id otype)
Definition unittype.c:132
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1767
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2625
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:91
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:617
#define unit_type_iterate(_p)
Definition unittype.h:855
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:862
void vision_site_size_set(struct vision_site *psite, citizens size)
Definition vision.c:180
struct vision * vision_new(struct player *pplayer, struct tile *ptile)
Definition vision.c:33
bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles)
Definition vision.c:62
struct vision_site * vision_site_new(int identity, struct tile *location, struct player *owner)
Definition vision.c:86
void vision_site_destroy(struct vision_site *psite)
Definition vision.c:74
bool worker_task_is_sane(struct worker_task *ptask)
Definition workertask.c:40
#define worker_task_list_iterate(tasklist, ptask)
Definition workertask.h:33
#define worker_task_list_iterate_end
Definition workertask.h:35
void worklist_init(struct worklist *pwl)
Definition worklist.c:38