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)
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 ***********************************************************************/
15 #include <fc_config.h>
25 #include "bitvector.h"
36 #include "bitvector.h"
37 #include "capability.h"
41 #include "government.h"
47 #include "specialist.h"
55 #include "barbarian.h"
56 #include "citizenshand.h"
57 #include "citytools.h"
66 #include "savecompat.h"
69 #include "spacerace.h"
71 #include "stdinhand.h"
72 #include "techtools.h"
73 #include "unittools.h"
77 #include "advbuilding.h"
78 #include "infracache.h"
80 /* server/generator */
81 #include "utilities.h"
83 /* server/scripting */
84 #include "script_server.h"
88 #include "difficulty.h"
94 #define log_worker log_verbose
97 * This loops over the entire map to save data. It collects all the data of
98 * a line using GET_XY_CHAR and then executes the macro SECFILE_INSERT_LINE.
99 * Internal variables map_x, map_y, nat_x, nat_y, and line are allocated
100 * within the macro but definable by the caller of the macro.
103 * line: buffer variable to hold a line of chars
104 * map_x, map_y: variables for internal map coordinates
105 * nat_x, nat_y: variables for output/native coordinates
106 * GET_XY_CHAR: macro returning the map character for each position
107 * SECFILE_INSERT_LINE: macro to output each processed line (line nat_y)
109 * Note: don't use this macro DIRECTLY, use
110 * SAVE_NORMAL_MAP_DATA or SAVE_PLAYER_MAP_DATA instead.
112 #define SAVE_MAP_DATA(ptile, line, \
113 GET_XY_CHAR, SECFILE_INSERT_LINE) \
115 char line[game.map.xsize + 1]; \
116 int _nat_x, _nat_y; \
118 for (_nat_y = 0; _nat_y < game.map.ysize; _nat_y++) { \
119 for (_nat_x = 0; _nat_x < game.map.xsize; _nat_x++) { \
120 struct tile *ptile = native_pos_to_tile(_nat_x, _nat_y); \
121 fc_assert_action(ptile != NULL, continue); \
122 line[_nat_x] = (GET_XY_CHAR); \
123 fc_assert_msg(fc_isprint(line[_nat_x] & 0x7f), \
124 "Trying to write invalid map data: '%c' %d", \
125 line[_nat_x], line[_nat_x]); \
127 line[game.map.xsize] = '\0'; \
128 (SECFILE_INSERT_LINE); \
133 * Wrappers for SAVE_MAP_DATA.
135 * SAVE_NORMAL_MAP_DATA saves a standard line of map data.
137 * SAVE_PLAYER_MAP_DATA saves a line of map data from a playermap.
139 #define SAVE_NORMAL_MAP_DATA(ptile, secfile, secname, GET_XY_CHAR) \
140 SAVE_MAP_DATA(ptile, _line, GET_XY_CHAR, \
141 secfile_insert_str(secfile, _line, secname, _nat_y))
143 #define SAVE_PLAYER_MAP_DATA(ptile, secfile, secname, plrno, \
145 SAVE_MAP_DATA(ptile, _line, GET_XY_CHAR, \
146 secfile_insert_str(secfile, _line, secname, plrno, _nat_y))
149 * This loops over the entire map to load data. It inputs a line of data
150 * using the macro SECFILE_LOOKUP_LINE and then loops using the macro
151 * SET_XY_CHAR to load each char into the map at (map_x, map_y). Internal
152 * variables ch, map_x, map_y, nat_x, and nat_y are allocated within the
153 * macro but definable by the caller.
156 * ch: a variable to hold a char (data for a single position)
157 * map_x, map_y: variables for internal map coordinates
158 * nat_x, nat_y: variables for output/native coordinates
159 * SET_XY_CHAR: macro to load the map character at each (map_x, map_y)
160 * SECFILE_LOOKUP_LINE: macro to input the nat_y line for processing
162 * Note: some (but not all) of the code this is replacing used to
163 * skip over lines that did not exist. This allowed for
164 * backward-compatibility. We could add another parameter that
165 * specified whether it was OK to skip the data, but there's not
166 * really much advantage to exiting early in this case. Instead,
167 * we let any map data type to be empty, and just print an
168 * informative warning message about it.
170 #define LOAD_MAP_DATA(ch, nat_y, ptile, \
171 SECFILE_LOOKUP_LINE, SET_XY_CHAR) \
173 int _nat_x, _nat_y; \
174 bool _printed_warning = FALSE; \
175 for (_nat_y = 0; _nat_y < game.map.ysize; _nat_y++) { \
176 const int nat_y = _nat_y; \
177 const char *_line = (SECFILE_LOOKUP_LINE); \
178 if (NULL == _line) { \
179 log_verbose("Line not found='%s'", #SECFILE_LOOKUP_LINE); \
180 _printed_warning = TRUE; \
182 } else if (strlen(_line) != game.map.xsize) { \
183 log_verbose("Line too short (expected %d got %lu)='%s'", \
184 game.map.xsize, (unsigned long) strlen(_line), \
185 #SECFILE_LOOKUP_LINE); \
186 _printed_warning = TRUE; \
189 for (_nat_x = 0; _nat_x < game.map.xsize; _nat_x++) { \
190 const char ch = _line[_nat_x]; \
191 struct tile *ptile = native_pos_to_tile(_nat_x, _nat_y); \
195 if (_printed_warning) { \
197 /* TRANS: Minor error message. */ \
198 log_error(_("Saved game contains incomplete map data. This can" \
199 " happen with old saved games, or it may indicate an" \
200 " invalid saved game file. Proceed at your own risk.")); \
204 /* Iterate on the specials half-bytes */
205 #define special_halfbyte_iterate(s, num_specials_types) \
207 enum tile_special_type s; \
209 for(s = 0; 4 * s < (num_specials_types); s++) {
211 #define special_halfbyte_iterate_end \
215 /* Iterate on the bases half-bytes */
216 #define bases_halfbyte_iterate(b, num_bases_types) \
220 for(b = 0; 4 * b < (num_bases_types); b++) {
222 #define bases_halfbyte_iterate_end \
226 static const char num_chars
[] =
227 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-+";
229 static bool load_river_overlay
= FALSE
;
231 static void set_savegame_special(struct tile
*ptile
, bv_extras
*extras
,
232 char ch
, const enum tile_special_type
*idx
);
234 static void game_load_internal(struct section_file
*file
);
236 static struct player
*identifier_to_player(char c
);
239 static void worklist_load(struct section_file
*file
, struct worklist
*pwl
,
240 const char *path
, ...)
241 fc__attribute((__format__ (__printf__
, 3, 4)));
243 /****************************************************************************
244 Exit from savegame loading on fatal errors. Currently there's no way to
245 recover, but server exits completely.
246 ****************************************************************************/
247 static void save_exit(void)
252 /****************************************************************************
253 Converts single character into numerical value. This is not hex conversion.
254 ****************************************************************************/
255 static int char2num(char ch
)
259 pch
= strchr(num_chars
, ch
);
261 fc_assert_ret_val_msg(NULL
!= pch
, 0,
262 "Unknown ascii value for num: '%c' %d", ch
, ch
);
264 return pch
- num_chars
;
267 /****************************************************************************
268 Dereferences the terrain character. See terrains[].identifier
269 example: char2terrain('a') => T_ARCTIC
270 ****************************************************************************/
271 static struct terrain
*char2terrain(char ch
)
273 /* terrain_by_identifier plus fatal error */
274 if (ch
== TERRAIN_UNKNOWN_IDENTIFIER
) {
277 terrain_type_iterate(pterrain
) {
278 if (pterrain
->identifier
== ch
) {
281 } terrain_type_iterate_end
;
283 log_fatal("Unknown terrain identifier '%c' in savegame.", ch
);
287 /****************************************************************************
288 Returns an order for a character identifier. See also order2char.
289 ****************************************************************************/
290 static enum unit_orders
char2order(char order
)
298 return ORDER_FULL_MP
;
301 return ORDER_BUILD_CITY
;
304 return ORDER_ACTIVITY
;
307 return ORDER_DISBAND
;
310 return ORDER_BUILD_WONDER
;
313 return ORDER_TRADE_ROUTE
;
316 return ORDER_HOMECITY
;
319 /* This can happen if the savegame is invalid. */
323 /****************************************************************************
324 Returns a direction for a character identifier. See also dir2char.
325 ****************************************************************************/
326 static enum direction8
char2dir(char dir
)
328 /* Numberpad values for the directions. */
331 return DIR8_SOUTHWEST
;
335 return DIR8_SOUTHEAST
;
341 return DIR8_NORTHWEST
;
345 return DIR8_NORTHEAST
;
348 /* This can happen if the savegame is invalid. */
349 return direction8_invalid();
352 /****************************************************************************
353 Returns a character identifier for an activity. See also char2activity.
354 ****************************************************************************/
355 static char activity2char(enum unit_activity activity
)
360 case ACTIVITY_POLLUTION
:
362 case ACTIVITY_OLD_ROAD
:
366 case ACTIVITY_IRRIGATE
:
368 case ACTIVITY_FORTIFIED
:
370 case ACTIVITY_FORTRESS
:
372 case ACTIVITY_SENTRY
:
374 case ACTIVITY_OLD_RAILROAD
:
376 case ACTIVITY_PILLAGE
:
380 case ACTIVITY_EXPLORE
:
382 case ACTIVITY_TRANSFORM
:
384 case ACTIVITY_AIRBASE
:
386 case ACTIVITY_FORTIFYING
:
388 case ACTIVITY_FALLOUT
:
392 case ACTIVITY_GEN_ROAD
:
394 case ACTIVITY_CONVERT
:
396 case ACTIVITY_UNKNOWN
:
397 case ACTIVITY_PATROL_UNUSED
:
407 /****************************************************************************
408 Returns an activity for a character identifier. See also activity2char.
409 ****************************************************************************/
410 static enum unit_activity
char2activity(char activity
)
412 enum unit_activity a
;
414 for (a
= 0; a
< ACTIVITY_LAST
; a
++) {
415 char achar
= activity2char(a
);
417 if (activity
== achar
|| activity
== fc_toupper(achar
)) {
422 /* This can happen if the savegame is invalid. */
423 return ACTIVITY_LAST
;
426 /****************************************************************************
427 Setting compress type compability checks.
428 ****************************************************************************/
429 static inline enum fz_method
int2fz_method(int magic
)
432 case 0: /* Was FZ_PLAIN. */
434 case 1: /* Was FZ_ZLIB. */
435 #ifdef FREECIV_HAVE_LIBZ
438 log_verbose("Not compiled with zlib support, reverting to default.");
440 #endif /* FREECIV_HAVE_LIBZ */
441 case 2: /* Was FZ_BZIP2. */
442 #ifdef FREECIV_HAVE_LIBBZ2
445 log_verbose("Not compiled with bzib2 support, reverting to default.");
447 #endif /* FREECIV_HAVE_LIBZ */
449 #ifdef FREECIV_HAVE_LIBLZMA
452 log_verbose("Not compiled with xz support, reverting to default.");
454 #endif /* FREECIV_HAVE_LIBLZMA */
456 return GAME_DEFAULT_COMPRESS_TYPE
;
459 /****************************************************************************
460 Unquote a string. The unquoted data is written into dest. If the unquoted
461 data will be larger than dest_length the function aborts. It returns the
462 actual length of the unquoted block.
463 ****************************************************************************/
464 static int unquote_block(const char *const quoted_
, void *dest
,
467 int i
, length
, parsed
, tmp
;
469 const char *quoted
= quoted_
;
471 parsed
= sscanf(quoted
, "%d", &length
);
472 fc_assert_ret_val(1 == parsed
, 0);
474 if (length
<= dest_length
) {
477 quoted
= strchr(quoted
, ':');
478 fc_assert_ret_val(quoted
!= NULL
, 0);
481 for (i
= 0; i
< length
; i
++) {
482 tmp
= strtol(quoted
, &endptr
, 16);
483 fc_assert_ret_val((endptr
- quoted
) == 2, 0);
484 fc_assert_ret_val(*endptr
== ' ', 0);
485 fc_assert_ret_val((tmp
& 0xff) == tmp
, 0);
486 ((unsigned char *) dest
)[i
] = tmp
;
493 /****************************************************************************
494 Load the worklist elements specified by path to the worklist pointed to
497 pwl should be a pointer to an existing worklist.
499 path and ... give the prefix to load from, printf-style.
500 ****************************************************************************/
501 static void worklist_load(struct section_file
*file
, struct worklist
*pwl
,
502 const char *path
, ...)
510 /* The first part of the registry path is taken from the varargs to the
513 fc_vsnprintf(path_str
, sizeof(path_str
), path
, ap
);
517 pwl
->length
= secfile_lookup_int_default(file
, 0,
518 "%s.wl_length", path_str
);
520 for (i
= 0; i
< pwl
->length
; i
++) {
521 kind
= secfile_lookup_str(file
, "%s.wl_kind%d", path_str
, i
);
523 /* before 2.2.0 unit production was indicated by flag. */
524 bool is_unit
= secfile_lookup_bool_default(file
, FALSE
,
527 kind
= universals_n_name(is_unit
? VUT_UTYPE
: VUT_IMPROVEMENT
);
530 /* We lookup the production value by name. An invalid entry isn't a
531 * fatal error; we just truncate the worklist. */
532 name
= secfile_lookup_str_default(file
, "-", "%s.wl_value%d",
534 pwl
->entries
[i
] = universal_by_rule_name(kind
, name
);
535 if (pwl
->entries
[i
].kind
== universals_n_invalid()) {
536 log_error("%s.wl_value%d: unknown \"%s\" \"%s\".",
537 path_str
, i
, kind
, name
);
544 /***************************************************************
545 load starting positions for the players from a savegame file
546 Now we don't know how many start positions there are nor how many
547 should be because rulesets are loaded later. So try to load as
548 many as they are; there should be at least enough for every
549 player. This could be changed/improved in future.
550 ***************************************************************/
551 static void map_load_startpos(struct section_file
*file
,
552 enum server_states state
)
554 int savegame_start_positions
;
559 for (savegame_start_positions
= 0;
560 secfile_lookup_int_default(file
, -1, "map.r%dsx",
561 savegame_start_positions
) != -1;
562 savegame_start_positions
++) {
567 for (i
= 0; i
< savegame_start_positions
; i
++) {
568 const char *nation_name
= secfile_lookup_str(file
,
569 "map.r%dsnation", i
);
570 struct nation_type
*pnation
= NO_NATION_SELECTED
;
571 struct startpos
*psp
;
574 if (NULL
!= nation_name
) {
575 pnation
= nation_by_rule_name(nation_name
);
576 if (NO_NATION_SELECTED
== pnation
) {
577 log_error("Warning: Unknown nation %s for starting position %d",
582 if (!secfile_lookup_int(file
, &nat_x
, "map.r%dsx", i
)
583 || !secfile_lookup_int(file
, &nat_y
, "map.r%dsy", i
)) {
584 log_error("%s", secfile_error());
588 ptile
= native_pos_to_tile(nat_x
, nat_y
);
590 log_error("Start position native coordinates (%d, %d) do not exist "
591 "in this map. Skipping...", nat_x
, nat_y
);
595 psp
= map_startpos_new(native_pos_to_tile(nat_x
, nat_y
));
596 if (NO_NATION_SELECTED
!= pnation
) {
597 startpos_allow(psp
, pnation
);
601 if (0 < map_startpos_count()
602 && state
== S_S_INITIAL
603 && map_startpos_count() < game
.server
.max_players
) {
604 log_verbose("Number of starts (%d) are lower than rules.max_players "
605 "(%d), lowering rules.max_players.",
606 map_startpos_count(), game
.server
.max_players
);
607 game
.server
.max_players
= map_startpos_count();
611 /***************************************************************
612 Load the tile map from a savegame file
613 ***************************************************************/
614 static void map_load_tiles(struct section_file
*file
)
616 game
.map
.topology_id
= secfile_lookup_int_default(file
, MAP_ORIGINAL_TOPO
,
619 /* In some cases we read these before, but not always, and
620 * its safe to read them again:
622 if (!secfile_lookup_int(file
, &game
.map
.xsize
, "map.width")
623 || !secfile_lookup_int(file
, &game
.map
.ysize
, "map.height")) {
624 log_error("%s", secfile_error());
628 /* Initialize the map for the current topology. 'map.xsize' and
629 * 'map.ysize' must be set. */
634 /* get the terrain type */
635 LOAD_MAP_DATA(ch
, line
, ptile
,
636 secfile_lookup_str(file
, "map.t%03d", line
),
637 ptile
->terrain
= char2terrain(ch
));
639 assign_continent_numbers();
641 whole_map_iterate(ptile
) {
642 const char *spec_sprite
;
646 index_to_native_pos(&nat_x
, &nat_y
, tile_index(ptile
));
647 spec_sprite
= secfile_lookup_str(file
, "map.spec_sprite_%d_%d", nat_x
,
649 label
= secfile_lookup_str_default(file
, NULL
, "map.label_%d_%d", nat_x
,
652 if (NULL
!= ptile
->spec_sprite
) {
653 ptile
->spec_sprite
= fc_strdup(spec_sprite
);
656 tile_set_label(ptile
, label
);
658 } whole_map_iterate_end
;
661 /* The order of the special bits, so that we can rebuild the
662 * bitvector from old savegames (where the order of the bits is
663 * unspecified). Here S_LAST means an unused bit (but see also resource
665 static const enum tile_special_type default_specials
[] = {
666 S_LAST
, S_OLD_ROAD
, S_IRRIGATION
, S_OLD_RAILROAD
,
667 S_MINE
, S_POLLUTION
, S_HUT
, S_OLD_FORTRESS
,
668 S_LAST
, S_OLD_RIVER
, S_FARMLAND
, S_OLD_AIRBASE
,
669 S_FALLOUT
, S_LAST
, S_LAST
, S_LAST
672 /****************************************************************************
673 Load the rivers overlay map from a savegame file.
675 A scenario may define the terrain of the map but not list the specials on
676 it (thus allowing users to control the placement of specials). However
677 rivers are a special case and must be included in the map along with the
678 scenario. Thus in those cases this function should be called to load
679 the river information separate from any other special data.
681 This does not need to be called from map_load(), because map_load() loads
682 the rivers overlay along with the rest of the specials. Call this only
683 if you've already called map_load_tiles(), and want to load only the
684 rivers overlay but no other specials. Scenarios that encode things this
685 way should have the "riversoverlay" capability.
686 ****************************************************************************/
687 static void map_load_rivers_overlay(struct section_file
*file
,
688 const enum tile_special_type
*special_order
,
689 int num_special_types
)
691 /* used by set_savegame_special */
692 load_river_overlay
= TRUE
;
695 special_halfbyte_iterate(j
, num_special_types
) {
696 char buf
[16]; /* enough for sprintf() below */
697 sprintf (buf
, "map.spe%02d_%%03d", j
);
699 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
700 secfile_lookup_str(file
, buf
, nat_y
),
701 set_savegame_special(ptile
, &ptile
->extras
, ch
, special_order
+ 4 * j
));
702 } special_halfbyte_iterate_end
;
704 /* Get the bits of the special flags which contain the river special
705 and extract the rivers overlay from them. */
706 FC_STATIC_ASSERT(S_LAST
<= 32, S_LAST_too_big
);
707 LOAD_MAP_DATA(ch
, line
, ptile
,
708 secfile_lookup_str(file
, "map.n%03d", line
),
709 set_savegame_special(ptile
, &ptile
->extras
, ch
, default_specials
+ 8));
712 load_river_overlay
= FALSE
;
715 /****************************************************************************
716 Complicated helper function for loading specials from a savegame.
718 'ch' gives the character loaded from the savegame. Specials are packed
719 in four to a character in hex notation. 'index' is a mapping of
720 savegame bit -> special bit. S_LAST is used to mark unused savegame bits.
721 ****************************************************************************/
722 static void set_savegame_special(struct tile
*ptile
, bv_extras
*extras
,
724 const enum tile_special_type
*idx
)
727 const char *pch
= strchr(hex_chars
, ch
);
729 if (!pch
|| ch
== '\0') {
730 log_error("Unknown hex value: '%c' (%d)", ch
, ch
);
733 bin
= pch
- hex_chars
;
736 for (i
= 0; i
< 4; i
++) {
737 enum tile_special_type sp
= idx
[i
];
742 if (load_river_overlay
&& sp
!= S_OLD_RIVER
) {
746 if (bin
& (1 << i
)) {
747 /* Pre 2.2 savegames have fortresses and airbases as part of specials */
748 if (sp
== S_OLD_FORTRESS
) {
749 struct base_type
*pbase
;
751 pbase
= get_base_by_gui_type(BASE_GUI_FORTRESS
, NULL
, NULL
);
753 BV_SET(*extras
, extra_index(base_extra_get(pbase
)));
755 } else if (sp
== S_OLD_AIRBASE
) {
756 struct base_type
*pbase
;
758 pbase
= get_base_by_gui_type(BASE_GUI_AIRBASE
, NULL
, NULL
);
760 BV_SET(*extras
, extra_index(base_extra_get(pbase
)));
762 } else if (sp
== S_OLD_ROAD
) {
763 struct road_type
*proad
;
765 proad
= road_by_compat_special(ROCO_ROAD
);
767 BV_SET(*extras
, extra_index(road_extra_get(proad
)));
769 } else if (sp
== S_OLD_RAILROAD
) {
770 struct road_type
*proad
;
772 proad
= road_by_compat_special(ROCO_RAILROAD
);
774 BV_SET(*extras
, extra_index(road_extra_get(proad
)));
776 } else if (sp
== S_OLD_RIVER
) {
777 struct road_type
*proad
;
779 proad
= road_by_compat_special(ROCO_RIVER
);
781 BV_SET(*extras
, extra_index(road_extra_get(proad
)));
784 struct extra_type
*pextra
= NULL
;
785 enum extra_cause cause
= EC_COUNT
;
787 /* Converting from old hardcoded specials to as sensible extra as we can */
791 /* If old savegame has both irrigation and farmland, EC_IRRIGATION
792 * gets applied twice, which hopefully has the correct result. */
793 cause
= EC_IRRIGATION
;
799 cause
= EC_POLLUTION
;
808 pextra
= extra_type_by_rule_name(special_rule_name(sp
));
812 if (cause
!= EC_COUNT
) {
813 struct tile
*vtile
= tile_virtual_new(ptile
);
815 /* Do not let the extras already set to the real tile mess with setup
816 * of the player tiles if that's what we're doing. */
817 vtile
->extras
= *extras
;
819 /* It's ok not to know which player or which unit originally built the extra -
820 * in the rules used when specials were saved these could not have made any
822 pextra
= next_extra_for_tile(vtile
, cause
, NULL
, NULL
);
824 tile_virtual_destroy(vtile
);
828 BV_SET(*extras
, extra_index(pextra
));
835 /****************************************************************************
836 Helper function for loading bases from a savegame.
838 'ch' gives the character loaded from the savegame. Bases are packed
839 in four to a character in hex notation. 'index' is a mapping of
840 savegame bit -> base bit.
841 ****************************************************************************/
842 static void set_savegame_bases(bv_extras
*extras
,
844 struct base_type
**idx
)
847 const char *pch
= strchr(hex_chars
, ch
);
849 if (!pch
|| ch
== '\0') {
850 log_error("Unknown hex value: '%c' (%d)", ch
, ch
);
853 bin
= pch
- hex_chars
;
856 for (i
= 0; i
< 4; i
++) {
857 struct base_type
*pbase
= idx
[i
];
862 if (bin
& (1 << i
)) {
863 BV_SET(*extras
, extra_index(base_extra_get(pbase
)));
868 /****************************************************************************
869 Complicated helper function for reading resources from a savegame.
870 This reads resources saved in the specials bitvector.
871 ch is the character read from the map, and n is the number of the special
872 (0 for special_1, 1 for special_2).
873 ****************************************************************************/
874 static void set_savegame_old_resource(struct resource
**r
,
875 const struct terrain
*terrain
,
878 fc_assert_ret(n
== 0 || n
== 1);
879 /* If resource is already set to non-NULL or there is no resource found
880 * in this half-byte, then abort */
881 if (*r
|| !(ascii_hex2bin (ch
, 0) & 0x1) || !terrain
->resources
[0]) {
884 /* Note that we must handle the case of special_2 set when there is
885 * only one resource defined for the terrain (example, Shields on
886 * Grassland terrain where both resources are identical) */
887 if (n
== 0 || !terrain
->resources
[1]) {
888 *r
= terrain
->resources
[0];
890 *r
= terrain
->resources
[1];
894 /****************************************************************************
895 Return the resource for the given identifier.
896 ****************************************************************************/
897 static struct resource
*identifier_to_resource(char c
)
899 /* speed common values */
900 if (c
== RESOURCE_NULL_IDENTIFIER
901 || c
== RESOURCE_NONE_IDENTIFIER
) {
904 return resource_by_identifier(c
);
907 /****************************************************************************
908 Return the player for the given identifier.
909 ****************************************************************************/
910 static struct player
*identifier_to_player(char c
)
915 if ('0' <= c
&& c
<= '9') {
916 return player_by_number(c
- '0');
918 return player_by_number(10 + c
- 'A');
921 /***************************************************************
922 load a complete map from a savegame file
923 ***************************************************************/
924 static void map_load(struct section_file
*file
,
925 enum server_states state
,
926 const char *savefile_options
,
927 const enum tile_special_type
*special_order
,
928 int num_special_types
,
929 struct base_type
**base_order
,
933 * This is already called in game_init(), and calling it
934 * here stomps on map.huts etc. --dwp
937 map_load_tiles(file
);
938 if (secfile_lookup_bool_default(file
, TRUE
, "game.save_starts")) {
939 map_load_startpos(file
, state
);
943 /* First load the resources. This is straightforward. */
944 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
945 secfile_lookup_str(file
, "map.res%03d", nat_y
),
946 ptile
->resource
= identifier_to_resource(ch
));
948 special_halfbyte_iterate(j
, num_special_types
) {
949 char buf
[16]; /* enough for sprintf() below */
950 sprintf (buf
, "map.spe%02d_%%03d", j
);
952 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
953 secfile_lookup_str(file
, buf
, nat_y
),
954 set_savegame_special(ptile
, &ptile
->extras
,
955 ch
, special_order
+ 4 * j
));
956 } special_halfbyte_iterate_end
;
958 /* get 4-bit segments of 16-bit "special" field. */
959 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
960 secfile_lookup_str(file
, "map.l%03d", nat_y
),
961 set_savegame_special(ptile
, &ptile
->extras
,
962 ch
, default_specials
+ 0));
963 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
964 secfile_lookup_str(file
, "map.u%03d", nat_y
),
965 set_savegame_special(ptile
, &ptile
->extras
,
966 ch
, default_specials
+ 4));
967 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
968 secfile_lookup_str(file
, "map.n%03d", nat_y
),
969 set_savegame_special(ptile
, &ptile
->extras
,
970 ch
, default_specials
+ 8));
971 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
972 secfile_lookup_str(file
, "map.f%03d", nat_y
),
973 set_savegame_special(ptile
, &ptile
->extras
,
974 ch
, default_specials
+ 12));
975 /* Setup resources (from half-bytes 1 and 3 of old savegames) */
976 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
977 secfile_lookup_str(file
, "map.l%03d", nat_y
),
978 set_savegame_old_resource(&ptile
->resource
, ptile
->terrain
, ch
, 0));
979 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
980 secfile_lookup_str(file
, "map.n%03d", nat_y
),
981 set_savegame_old_resource(&ptile
->resource
, ptile
->terrain
, ch
, 1));
984 /* after the resources are loaded, indicate those currently valid */
985 whole_map_iterate(ptile
) {
986 if (NULL
== ptile
->resource
987 || NULL
== ptile
->terrain
) {
990 if (terrain_has_resource(ptile
->terrain
, ptile
->resource
)) {
991 /* cannot use set_special() for internal values */
992 ptile
->resource_valid
= TRUE
;
994 } whole_map_iterate_end
;
996 if (has_capability("bases", savefile_options
)) {
997 char zeroline
[game
.map
.xsize
+ 1];
1000 /* This is needed when new bases has been added to ruleset, and
1001 * thus game.control.num_base_types is greater than, when game was saved. */
1002 for (i
= 0; i
< game
.map
.xsize
; i
++) {
1007 bases_halfbyte_iterate(j
, num_bases_types
) {
1008 char buf
[16]; /* enough for sprintf() below */
1009 sprintf(buf
, "map.b%02d_%%03d", j
);
1011 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1012 secfile_lookup_str_default(file
, zeroline
, buf
, nat_y
),
1013 set_savegame_bases(&ptile
->extras
, ch
, base_order
+ 4 * j
));
1014 } bases_halfbyte_iterate_end
;
1018 /****************************************************************************
1019 Load data about tiles owners. This must be after players are allocated.
1020 ****************************************************************************/
1021 static void map_load_owner(struct section_file
*file
,
1022 const char *savefile_options
)
1024 /* Owner and ownership source are stored as plain numbers */
1025 if (has_capability("new_owner_map", savefile_options
)) {
1027 struct player
*owner
= NULL
;
1028 struct tile
*claimer
= NULL
;
1030 for (y
= 0; y
< game
.map
.ysize
; y
++) {
1031 const char *buffer1
= secfile_lookup_str(file
, "map.owner%03d", y
);
1032 const char *buffer2
= secfile_lookup_str(file
, "map.source%03d", y
);
1033 const char *ptr1
= buffer1
;
1034 const char *ptr2
= buffer2
;
1036 if (buffer1
== NULL
|| buffer2
== NULL
) {
1037 log_error("Savegame corrupt - map line %d not found.", y
);
1041 for (x
= 0; x
< game
.map
.xsize
; x
++) {
1042 char token1
[TOKEN_SIZE
];
1043 char token2
[TOKEN_SIZE
];
1045 struct tile
*ptile
= native_pos_to_tile(x
, y
);
1047 scanin(&ptr1
, ",", token1
, sizeof(token1
));
1048 scanin(&ptr2
, ",", token2
, sizeof(token2
));
1050 if (token1
[0] == '\0' || token2
[0] == '\0') {
1051 log_error("Savegame corrupt - map size not correct.");
1055 if (strcmp(token1
, "-") == 0) {
1058 if (!str_to_int(token1
, &number
)) {
1059 log_error("Savegame corrupt - got map owner %s "
1060 "in (%d, %d).", token1
, x
, y
);
1063 owner
= player_by_number(number
);
1065 if (strcmp(token2
, "-") == 0) {
1068 if (!str_to_int(token2
, &number
)) {
1069 log_error("Savegame corrupt - got map source %s "
1070 "in (%d, %d).", token2
, x
, y
);
1073 claimer
= index_to_tile(number
);
1076 map_claim_ownership(ptile
, owner
, claimer
, TRUE
);
1082 /****************************************************************************
1083 Load data about known tiles. This must be after players are allocated.
1084 ****************************************************************************/
1085 static void map_load_known(struct section_file
*file
,
1086 const char *savefile_options
)
1088 if (secfile_lookup_bool_default(file
, TRUE
, "game.save_known")) {
1089 int *known
= fc_calloc(MAP_INDEX_SIZE
, sizeof(int));
1091 /* get 4-bit segments of the first half of the 32-bit "known" field */
1092 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1093 secfile_lookup_str(file
, "map.a%03d", nat_y
),
1094 known
[tile_index(ptile
)] = ascii_hex2bin(ch
, 0));
1095 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1096 secfile_lookup_str(file
, "map.b%03d", nat_y
),
1097 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 1));
1098 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1099 secfile_lookup_str(file
, "map.c%03d", nat_y
),
1100 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 2));
1101 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1102 secfile_lookup_str(file
, "map.d%03d", nat_y
),
1103 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 3));
1105 if (has_capability("known32fix", savefile_options
)) {
1106 /* get 4-bit segments of the second half of the 32-bit "known" field */
1107 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1108 secfile_lookup_str(file
, "map.e%03d", nat_y
),
1109 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 4));
1110 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1111 secfile_lookup_str(file
, "map.g%03d", nat_y
),
1112 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 5));
1113 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1114 secfile_lookup_str(file
, "map.h%03d", nat_y
),
1115 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 6));
1116 LOAD_MAP_DATA(ch
, nat_y
, ptile
,
1117 secfile_lookup_str(file
, "map.i%03d", nat_y
),
1118 known
[tile_index(ptile
)] |= ascii_hex2bin(ch
, 7));
1121 players_iterate(pplayer
) {
1122 dbv_clr_all(&pplayer
->tile_known
);
1123 } players_iterate_end
;
1125 /* HACK: we read the known data from hex into a 32-bit integer, and
1126 * now we convert it to bv_player. */
1127 whole_map_iterate(ptile
) {
1128 players_iterate(pplayer
) {
1129 if (known
[tile_index(ptile
)] & (1u << player_index(pplayer
))) {
1130 map_set_known(ptile
, pplayer
);
1132 } players_iterate_end
;
1133 } whole_map_iterate_end
;
1137 game
.map
.server
.have_resources
= TRUE
;
1138 game
.scenario
.have_resources
= TRUE
;
1141 /*****************************************************************************
1142 Load technology from path_name and if doesn't exist (because savegame
1143 is too old) load from path.
1144 *****************************************************************************/
1145 static Tech_type_id
technology_load(struct section_file
*file
,
1146 const char* path
, int plrno
)
1148 char path_with_name
[128];
1150 struct advance
*padvance
;
1152 fc_snprintf(path_with_name
, sizeof(path_with_name
),
1155 name
= secfile_lookup_str(file
, path_with_name
, plrno
);
1157 log_fatal("%s: no tech name", path_with_name
);
1161 if (fc_strcasecmp(name
, "A_FUTURE") == 0) {
1164 if (fc_strcasecmp(name
, "A_NONE") == 0) {
1167 if (fc_strcasecmp(name
, "A_UNSET") == 0) {
1170 if (name
[0] == '\0') {
1171 /* used by researching_saved */
1175 padvance
= advance_by_rule_name(name
);
1176 if (NULL
== padvance
) {
1177 log_fatal("%s: unknown technology \"%s\".", path_with_name
, name
);
1180 return advance_number(padvance
);
1183 /****************************************************************************
1184 Loads the units for the given player.
1185 ****************************************************************************/
1186 static void player_load_units(struct player
*plr
, int plrno
,
1187 struct section_file
*file
,
1188 const char *savefile_options
,
1189 const enum tile_special_type
*special_order
,
1190 int num_special_types
,
1191 struct base_type
**base_order
,
1195 enum unit_activity activity
;
1197 if (!secfile_lookup_int(file
, &nunits
, "player%d.nunits", plrno
)) {
1198 log_error("%s", secfile_error());
1202 if (!plr
->is_alive
&& nunits
> 0) {
1203 nunits
= 0; /* Some old savegames may be buggy. */
1206 for (i
= 0; i
< nunits
; i
++) {
1210 const char* type_name
;
1211 struct unit_type
*type
;
1213 enum tile_special_type target
;
1214 struct base_type
*pbase
= NULL
;
1215 struct road_type
*proad
= NULL
;
1221 type_name
= secfile_lookup_str(file
, "player%d.u%d.type_by_name",
1224 log_fatal("Player %d, unit %d, no type", plrno
, i
);
1228 type
= unit_type_by_rule_name(type_name
);
1230 log_fatal("player%d.u%d: unknown unit type \"%s\".",
1231 plrno
, i
, type_name
);
1236 = secfile_lookup_int_default(file
, 0, "player%d.u%d.veteran", plrno
, i
);
1238 /* Protect against change in veteran system in ruleset */
1239 const int levels
= utype_veteran_levels(type
);
1241 if (veteran
>= levels
) {
1242 fc_assert(levels
>= 1);
1243 veteran
= levels
- 1;
1247 punit
= unit_virtual_create(plr
, NULL
, type
, veteran
);
1248 if (!secfile_lookup_int(file
, &punit
->id
,
1249 "player%d.u%d.id", plrno
, i
)) {
1250 log_error("%s", secfile_error());
1253 identity_number_reserve(punit
->id
);
1254 idex_register_unit(punit
);
1256 if (!secfile_lookup_int(file
, &nat_x
, "player%d.u%d.x",
1258 || !secfile_lookup_int(file
, &nat_y
, "player%d.u%d.y",
1260 log_error("%s", secfile_error());
1264 unit_tile_set(punit
, native_pos_to_tile(nat_x
, nat_y
));
1265 ptile
= unit_tile(punit
);
1266 if (NULL
== ptile
) {
1267 log_fatal("player%d.u%d invalid tile (%d, %d)",
1268 plrno
, i
, nat_x
, nat_y
);
1272 punit
->nationality
= plr
;
1274 /* Avoid warning when loading pre-2.1 saves containing foul status */
1275 (void) secfile_entry_lookup(file
, "player%d.u%d.foul", plrno
, i
);
1277 if (!secfile_lookup_int(file
, &punit
->homecity
, "player%d.u%d.homecity", plrno
, i
)
1278 || !secfile_lookup_int(file
, &punit
->moves_left
, "player%d.u%d.moves", plrno
, i
)
1279 || !secfile_lookup_int(file
, &punit
->fuel
, "player%d.u%d.fuel", plrno
, i
)
1280 || !secfile_lookup_int(file
, &ei
, "player%d.u%d.activity", plrno
, i
)) {
1281 log_error("%s", secfile_error());
1287 if ((pcity
= game_city_by_number(punit
->homecity
))) {
1288 unit_list_prepend(pcity
->units_supported
, punit
);
1289 } else if (punit
->homecity
> IDENTITY_NUMBER_ZERO
) {
1290 log_error("player%d.u%d: bad home city %d.",
1291 plrno
, i
, punit
->homecity
);
1292 punit
->homecity
= IDENTITY_NUMBER_ZERO
;
1295 punit
->server
.birth_turn
= secfile_lookup_int_default(file
, game
.info
.turn
,
1296 "player%d.u%d.born", plrno
, i
);
1297 base
= secfile_lookup_int_default(file
, -1,
1298 "player%d.u%d.activity_base", plrno
, i
);
1299 if (base
>= 0 && base
< num_base_types
) {
1300 pbase
= base_order
[base
];
1303 if (special_order
) {
1305 = secfile_lookup_int_default(file
, num_special_types
/* S_LAST */,
1306 "player%d.u%d.activity_target", plrno
, i
);
1307 if (tgt_no
>= 0 && tgt_no
< num_special_types
) {
1308 target
= special_order
[tgt_no
];
1313 /* (Perhaps targeted pillaging post-dates default_specials, but better
1314 * safe than sorry.) */
1316 = secfile_lookup_int_default(file
, ARRAY_SIZE(default_specials
),
1317 "player%d.u%d.activity_target", plrno
, i
);
1318 if (tgt_no
>= 0 && tgt_no
< ARRAY_SIZE(default_specials
)) {
1319 target
= default_specials
[tgt_no
];
1324 if (target
== S_OLD_FORTRESS
) {
1325 struct extra_type
*pextra
= extra_type_by_rule_name("Fortress");
1327 if (pextra
!= NULL
) {
1328 pbase
= extra_base_get(pextra
);
1330 } else if (target
== S_OLD_AIRBASE
) {
1331 struct extra_type
*pextra
= extra_type_by_rule_name("Airbase");
1333 if (pextra
!= NULL
) {
1334 pbase
= extra_base_get(pextra
);
1338 if (target
== S_OLD_ROAD
) {
1340 proad
= road_by_compat_special(ROCO_ROAD
);
1341 } else if (target
== S_OLD_RAILROAD
) {
1343 proad
= road_by_compat_special(ROCO_RAILROAD
);
1346 if (activity
== ACTIVITY_PATROL_UNUSED
) {
1347 /* Previously ACTIVITY_PATROL and ACTIVITY_GOTO were used for
1348 * client-side goto. Now client-side goto is handled by setting
1349 * a special flag, and units with orders generally have ACTIVITY_IDLE.
1350 * Old orders are lost. Old client-side goto units will still have
1351 * ACTIVITY_GOTO and will goto the correct position via server goto.
1352 * Old client-side patrol units lose their patrol routes and are put
1353 * into idle mode. */
1354 activity
= ACTIVITY_IDLE
;
1357 if (activity
== ACTIVITY_FORTRESS
) {
1358 activity
= ACTIVITY_BASE
;
1359 pbase
= get_base_by_gui_type(BASE_GUI_FORTRESS
, punit
, ptile
);
1360 } else if (activity
== ACTIVITY_AIRBASE
) {
1361 activity
= ACTIVITY_BASE
;
1362 pbase
= get_base_by_gui_type(BASE_GUI_AIRBASE
, punit
, ptile
);
1365 if (activity
== ACTIVITY_OLD_ROAD
) {
1366 activity
= ACTIVITY_GEN_ROAD
;
1367 proad
= road_by_compat_special(ROCO_ROAD
);
1368 } else if (activity
== ACTIVITY_OLD_RAILROAD
) {
1369 activity
= ACTIVITY_GEN_ROAD
;
1370 proad
= road_by_compat_special(ROCO_RAILROAD
);
1373 if (activity
== ACTIVITY_IRRIGATE
) {
1374 struct extra_type
*tgt
= next_extra_for_tile(unit_tile(punit
),
1379 set_unit_activity_targeted(punit
, ACTIVITY_IRRIGATE
, tgt
);
1381 set_unit_activity_targeted(punit
, ACTIVITY_IRRIGATE
, NULL
);
1383 } else if (activity
== ACTIVITY_MINE
) {
1384 struct extra_type
*tgt
= next_extra_for_tile(unit_tile(punit
),
1389 set_unit_activity_targeted(punit
, ACTIVITY_MINE
, tgt
);
1391 set_unit_activity_targeted(punit
, ACTIVITY_MINE
, NULL
);
1393 } else if (activity
== ACTIVITY_POLLUTION
) {
1394 struct extra_type
*tgt
= prev_extra_in_tile(unit_tile(punit
),
1399 set_unit_activity_targeted(punit
, ACTIVITY_POLLUTION
, tgt
);
1401 set_unit_activity_targeted(punit
, ACTIVITY_POLLUTION
, NULL
);
1403 } else if (activity
== ACTIVITY_FALLOUT
) {
1404 struct extra_type
*tgt
= prev_extra_in_tile(unit_tile(punit
),
1409 set_unit_activity_targeted(punit
, ACTIVITY_FALLOUT
, tgt
);
1411 set_unit_activity_targeted(punit
, ACTIVITY_FALLOUT
, NULL
);
1413 } else if (activity
== ACTIVITY_BASE
) {
1415 set_unit_activity_base(punit
, base_number(pbase
));
1417 log_error("Cannot find base %d for %s to build",
1418 base
, unit_rule_name(punit
));
1419 set_unit_activity(punit
, ACTIVITY_IDLE
);
1421 } else if (activity
== ACTIVITY_GEN_ROAD
) {
1423 set_unit_activity_road(punit
, road_number(proad
));
1425 log_error("Cannot find road for %s to build",
1426 unit_rule_name(punit
));
1427 set_unit_activity(punit
, ACTIVITY_IDLE
);
1429 } else if (activity
== ACTIVITY_PILLAGE
) {
1430 struct extra_type
*a_target
;
1432 if (target
!= S_LAST
) {
1433 a_target
= special_extra_get(target
);
1434 } else if (pbase
!= NULL
) {
1435 a_target
= base_extra_get(pbase
);
1436 } else if (proad
!= NULL
) {
1437 a_target
= road_extra_get(proad
);
1441 /* An out-of-range base number is seen with old savegames. We take
1442 * it as indicating undirected pillaging. We will assign pillage
1443 * targets before play starts. */
1444 set_unit_activity_targeted(punit
, activity
, a_target
);
1446 set_unit_activity(punit
, activity
);
1449 /* need to do this to assign/deassign settlers correctly -- Syela
1451 * was punit->activity=secfile_lookup_int(file,
1452 * "player%d.u%d.activity",plrno, i); */
1453 if (!secfile_lookup_int(file
, &punit
->activity_count
,
1454 "player%d.u%d.activity_count", plrno
, i
)) {
1455 log_error("%s", secfile_error());
1459 /* Special case: for a long time, we accidentally incremented
1460 * activity_count while a unit was sentried, so it could increase
1461 * without bound (bug #20641) and be saved in old savefiles.
1462 * We zero it to prevent potential trouble overflowing the range
1463 * in network packets, etc. */
1464 if (activity
== ACTIVITY_SENTRY
) {
1465 punit
->activity_count
= 0;
1468 punit
->done_moving
= secfile_lookup_bool_default(file
,
1469 (punit
->moves_left
== 0), "player%d.u%d.done_moving", plrno
, i
);
1472 = secfile_lookup_int_default(file
, BATTLEGROUP_NONE
,
1473 "player%d.u%d.battlegroup", plrno
, i
);
1475 /* Load the goto information. Older savegames will not have the
1476 * "go" field, so we just load the goto destination by default. */
1477 if (secfile_lookup_bool_default(file
, TRUE
,
1478 "player%d.u%d.go", plrno
, i
)) {
1481 if (!secfile_lookup_int(file
, &gnat_x
,
1482 "player%d.u%d.goto_x", plrno
, i
)
1483 || !secfile_lookup_int(file
, &gnat_y
,
1484 "player%d.u%d.goto_y", plrno
, i
)) {
1485 log_error("%s", secfile_error());
1489 punit
->goto_tile
= native_pos_to_tile(gnat_x
, gnat_y
);
1491 punit
->goto_tile
= NULL
;
1494 /* Load AI data of the unit. */
1495 fc_snprintf(unitstr
, sizeof(unitstr
), "player%d.u%d", plrno
, i
);
1496 CALL_FUNC_EACH_AI(unit_load
, file
, punit
, unitstr
);
1498 if (!secfile_lookup_bool(file
, &punit
->ai_controlled
,
1499 "player%d.u%d.ai", plrno
, i
)
1500 || !secfile_lookup_int(file
, &punit
->hp
,
1501 "player%d.u%d.hp", plrno
, i
)) {
1502 log_error("%s", secfile_error());
1506 punit
->server
.ord_map
1507 = secfile_lookup_int_default(file
, 0,
1508 "player%d.u%d.ord_map", plrno
, i
);
1509 punit
->server
.ord_city
1510 = secfile_lookup_int_default(file
, 0,
1511 "player%d.u%d.ord_city", plrno
, i
);
1513 = secfile_lookup_bool_default(file
, FALSE
,
1514 "player%d.u%d.moved", plrno
, i
);
1516 = secfile_lookup_bool_default(file
, FALSE
,
1517 "player%d.u%d.paradropped", plrno
, i
);
1519 /* 'transported_by' is loaded later. */
1521 /* Initialize upkeep values: these are hopefully initialized
1522 elsewhere before use (specifically, in city_support(); but
1523 fixme: check whether always correctly initialized?).
1524 Below is mainly for units which don't have homecity --
1525 otherwise these don't get initialized (and AI calculations
1526 etc may use junk values).
1528 output_type_iterate(o
) {
1529 punit
->upkeep
[o
] = utype_upkeep_cost(type
, plr
, o
);
1530 } output_type_iterate_end
;
1532 /* load the unit orders */
1533 if (has_capability("orders", savefile_options
)) {
1534 int road_idx
= road_index(road_by_compat_special(ROCO_ROAD
));
1535 int rail_idx
= road_index(road_by_compat_special(ROCO_RAILROAD
));
1537 int len
= secfile_lookup_int_default(file
, 0,
1538 "player%d.u%d.orders_length", plrno
, i
);
1540 const char *orders_buf
, *dir_buf
, *act_buf
, *base_buf
;
1542 punit
->orders
.list
= fc_malloc(len
* sizeof(*(punit
->orders
.list
)));
1543 punit
->orders
.length
= len
;
1544 punit
->orders
.index
= secfile_lookup_int_default(file
, 0,
1545 "player%d.u%d.orders_index", plrno
, i
);
1546 punit
->orders
.repeat
= secfile_lookup_bool_default(file
, FALSE
,
1547 "player%d.u%d.orders_repeat", plrno
, i
);
1548 punit
->orders
.vigilant
= secfile_lookup_bool_default(file
, FALSE
,
1549 "player%d.u%d.orders_vigilant", plrno
, i
);
1551 orders_buf
= secfile_lookup_str_default(file
, "",
1552 "player%d.u%d.orders_list", plrno
, i
);
1553 dir_buf
= secfile_lookup_str_default(file
, "",
1554 "player%d.u%d.dir_list", plrno
, i
);
1555 act_buf
= secfile_lookup_str_default(file
, "",
1556 "player%d.u%d.activity_list", plrno
, i
);
1557 base_buf
= secfile_lookup_str(file
, "player%d.u%d.base_list",
1559 punit
->has_orders
= TRUE
;
1560 for (j
= 0; j
< len
; j
++) {
1561 struct unit_order
*order
= &punit
->orders
.list
[j
];
1562 struct base_type
*order_base
= NULL
;
1564 if (orders_buf
[j
] == '\0' || dir_buf
[j
] == '\0'
1565 || act_buf
[j
] == '\0') {
1566 log_error("Invalid unit orders.");
1567 free_unit_orders(punit
);
1570 order
->order
= char2order(orders_buf
[j
]);
1571 order
->dir
= char2dir(dir_buf
[j
]);
1572 order
->activity
= char2activity(act_buf
[j
]);
1573 if (order
->order
== ORDER_LAST
1574 || (order
->order
== ORDER_MOVE
&& !direction8_is_valid(order
->dir
))
1575 || (order
->order
== ORDER_ACTIVITY
1576 && order
->activity
== ACTIVITY_LAST
)) {
1577 /* An invalid order. Just drop the orders for this unit. */
1578 free(punit
->orders
.list
);
1579 punit
->orders
.list
= NULL
;
1580 punit
->has_orders
= FALSE
;
1584 /* Pre 2.2 savegames had activities ACTIVITY_FORTRESS and ACTIVITY_AIRBASE */
1585 if (order
->activity
== ACTIVITY_FORTRESS
) {
1586 order_base
= get_base_by_gui_type(BASE_GUI_FORTRESS
, NULL
, NULL
);
1587 order
->activity
= ACTIVITY_IDLE
; /* In case no matching gui_type found */
1588 } else if (order
->activity
== ACTIVITY_AIRBASE
) {
1589 order_base
= get_base_by_gui_type(BASE_GUI_AIRBASE
, NULL
, NULL
);
1590 order
->activity
= ACTIVITY_IDLE
; /* In case no matching gui_type found */
1592 if (order_base
!= NULL
) {
1593 /* Either ACTIVITY_FORTRESS or ACTIVITY_AIRBASE */
1594 order
->activity
= ACTIVITY_BASE
;
1595 order
->target
= extra_number(base_extra_get(order_base
));
1596 } else if (base_buf
&& base_buf
[j
] != '?') {
1597 base
= char2num(base_buf
[j
]);
1599 if (base
>= 0 && base
< num_base_types
) {
1600 order_base
= base_order
[base
];
1602 log_error("Cannot find base %d for %s to build",
1603 base
, unit_rule_name(punit
));
1604 base
= base_number(get_base_by_gui_type(BASE_GUI_FORTRESS
, NULL
, NULL
));
1607 order
->target
= extra_number(base_extra_get(base_by_number(base
)));
1609 order
->target
= EXTRA_NONE
;
1612 if (order
->activity
== ACTIVITY_OLD_ROAD
) {
1613 order
->activity
= ACTIVITY_GEN_ROAD
;
1614 order
->target
= extra_number(road_extra_get(road_by_number(road_idx
)));
1615 } else if (order
->activity
== ACTIVITY_OLD_RAILROAD
) {
1616 order
->activity
= ACTIVITY_GEN_ROAD
;
1617 order
->target
= extra_number(road_extra_get(road_by_number(rail_idx
)));
1620 if (punit
->orders
.list
[len
- 1].order
== ORDER_MOVE
) {
1621 /* Last move never safe in old versions. */
1622 punit
->orders
.list
[len
- 1].order
= ORDER_ACTION_MOVE
;
1625 punit
->has_orders
= FALSE
;
1626 punit
->orders
.list
= NULL
;
1629 /* Old-style goto routes get discarded. */
1630 punit
->has_orders
= FALSE
;
1631 punit
->orders
.list
= NULL
;
1634 /* allocate the unit's contribution to fog of war */
1635 punit
->server
.vision
= vision_new(unit_owner(punit
), ptile
);
1636 unit_refresh_vision(punit
);
1637 /* NOTE: There used to be some map_set_known calls here. These were
1638 * unneeded since unfogging the tile when the unit sees it will
1639 * automatically reveal that tile. */
1641 unit_list_append(plr
->units
, punit
);
1643 unit_list_prepend(ptile
->units
, punit
);
1645 /* Claim ownership of fortress? */
1646 if ((extra_owner(ptile
) == NULL
1647 || pplayers_at_war(extra_owner(ptile
), plr
))
1648 && tile_has_claimable_base(ptile
, unit_type_get(punit
))) {
1649 tile_claim_bases(ptile
, plr
);
1654 /****************************************************************************
1655 Load transporter status. This must be after _all_ units are loaded due to
1656 possible allied units on transports.
1657 ****************************************************************************/
1658 static void player_load_units_transporter(struct player
*plr
,
1659 struct section_file
*file
)
1661 int nunits
, i
, plrno
= player_index(plr
);
1663 /* Copied from player_load_unit(). */
1664 if (!secfile_lookup_int(file
, &nunits
,
1665 "player%d.nunits", plrno
)) {
1666 log_error("%s", secfile_error());
1670 if (!plr
->is_alive
&& nunits
> 0) {
1671 nunits
= 0; /* Some old savegames may be buggy. */
1674 for (i
= 0; i
< nunits
; i
++) {
1675 int id_unit
, id_trans
;
1676 struct unit
*punit
, *ptrans
;
1678 id_unit
= secfile_lookup_int_default(file
, -1,
1681 punit
= player_unit_by_number(plr
, id_unit
);
1682 fc_assert_action(punit
!= NULL
, continue);
1684 id_trans
= secfile_lookup_int_default(file
, -1,
1685 "player%d.u%d.transported_by",
1687 if (id_trans
== -1) {
1688 /* Not transported. */
1692 ptrans
= game_unit_by_number(id_trans
);
1693 fc_assert_action(id_trans
== -1 || ptrans
!= NULL
, continue);
1696 bool result
= unit_transport_load(punit
, ptrans
, TRUE
);
1705 /****************************************************************************
1706 Load all information about player "plrno" into the structure pointed to
1708 ****************************************************************************/
1709 static void player_load_main(struct player
*plr
, int plrno
,
1710 struct section_file
*file
,
1711 const char *savefile_options
,
1712 const char **technology_order
,
1713 size_t technology_order_size
)
1718 struct government
*gov
;
1719 struct research
*research
;
1720 struct nation_type
*pnation
;
1721 struct nation_style
*style
;
1724 research
= research_get(plr
);
1726 old_barb_type
= secfile_lookup_int_default(file
, 0,
1727 "player%d.ai.is_barbarian",
1730 plr
->ai_common
.barbarian_type
= barb_type_convert(old_barb_type
);
1732 if (is_barbarian(plr
)) {
1733 server
.nbarbarians
++;
1736 server_player_set_name(plr
, secfile_lookup_str(file
, "player%d.name",
1738 sz_strlcpy(plr
->username
,
1739 secfile_lookup_str_default(file
, "", "player%d.username", plrno
));
1740 plr
->unassigned_user
= (!strcmp(plr
->username
, ANON_USER_NAME
));
1741 sz_strlcpy(plr
->ranked_username
,
1742 secfile_lookup_str_default(file
, "", "player%d.ranked_username",
1744 plr
->unassigned_ranked
= (!strcmp(plr
->ranked_username
, ANON_USER_NAME
));
1746 /* Nations stored by name. */
1747 p
= secfile_lookup_str(file
, "player%d.nation", plrno
);
1749 pnation
= nation_by_rule_name(p
);
1751 pnation
= NO_NATION_SELECTED
;
1754 if (pnation
!= NO_NATION_SELECTED
) {
1756 /* 2.1 and earlier savegames have same nation for both barbarian players.
1757 * Reassign correct nations for such barbarians. */
1758 enum barbarian_type nat_barb_type
= nation_barbarian_type(pnation
);
1760 if ((is_sea_barbarian(plr
) && nat_barb_type
!= SEA_BARBARIAN
)
1761 || (is_land_barbarian(plr
) && nat_barb_type
!= LAND_BARBARIAN
)) {
1762 log_verbose("Reassigning barbarian nation for %s", player_name(plr
));
1763 plr
->nation
= NO_NATION_SELECTED
;
1765 player_set_nation(plr
, pnation
);
1768 plr
->nation
= NO_NATION_SELECTED
;
1770 /* Nation may be unselected at this point; we check for this later and
1771 * reassign nations to players who don't have them. */
1773 init_tech(research
, FALSE
);
1774 /* We used to call give_initial_techs here, but that shouldn't be
1775 * necessary. The savegame should already mark those techs as known.
1776 * give_initial_techs will crash if the nation is unset. */
1778 /* It is important that barbarian_type is loaded before
1779 * calling is_barbarian() and is_land_barbarian() */
1780 if (is_barbarian(plr
) && plr
->nation
== NO_NATION_SELECTED
) {
1781 if (is_land_barbarian(plr
)) {
1782 pnation
= pick_a_nation(NULL
, FALSE
, FALSE
, LAND_BARBARIAN
);
1784 pnation
= pick_a_nation(NULL
, FALSE
, FALSE
, SEA_BARBARIAN
);
1786 player_set_nation(plr
, pnation
);
1790 name
= secfile_lookup_str(file
, "player%d.government_name", plrno
);
1792 log_fatal("Player%d: no government.", plrno
);
1795 gov
= government_by_rule_name(name
);
1797 log_fatal("Player%d: unsupported government \"%s\".", plrno
, name
);
1800 plr
->government
= gov
;
1802 /* Target government */
1803 name
= secfile_lookup_str(file
, "player%d.target_government_name", plrno
);
1805 gov
= government_by_rule_name(name
);
1810 plr
->target_government
= gov
;
1812 /* Old servers didn't have this value. */
1813 plr
->target_government
= government_of_player(plr
);
1816 /* Multipliers: post-date version 1 savefiles, so use ruleset defaults */
1817 multipliers_iterate(pmul
) {
1818 plr
->multipliers
[multiplier_index(pmul
)]
1819 = plr
->multipliers_target
[multiplier_index(pmul
)] = pmul
->def
;
1820 } multipliers_iterate_end
;
1822 p
= secfile_lookup_str(file
, "player%d.city_style_by_name", plrno
);
1824 char* old_order
[4] = {"European", "Classical", "Tropical", "Asian"};
1825 c_s
= secfile_lookup_int_default(file
, 0, "player%d.city_style", plrno
);
1826 if (c_s
< 0 || c_s
> 3) {
1827 log_error("Player%d: unsupported city_style %d. Changed to \"%s\".",
1828 plrno
, c_s
, old_order
[0]);
1833 style
= style_by_rule_name(p
);
1834 if (style
== NULL
) {
1835 style
= style_by_number(0);
1836 log_error("Player%d: unsupported city_style_name \"%s\". "
1837 "Changed to \"%s\".", plrno
, p
, style_rule_name(style
));
1842 plr
->is_male
=secfile_lookup_bool_default(file
, TRUE
, "player%d.is_male", plrno
);
1843 if (!secfile_lookup_bool(file
, &plr
->is_alive
,
1844 "player%d.is_alive", plrno
)
1845 || !secfile_lookup_bool(file
, &plr
->ai_controlled
,
1846 "player%d.ai.control", plrno
)) {
1847 log_error("%s", secfile_error());
1851 plr
->turns_alive
= secfile_lookup_int_default(file
, game
.info
.turn
,
1852 "player%d.turns_alive", plrno
);
1853 plr
->phase_done
= FALSE
;
1855 /* Backwards-compatibility: the tech goal value is still stored in the
1856 * "ai" section even though it was moved into the research struct. */
1857 research
->tech_goal
=
1858 technology_load(file
, "player%d.ai.tech_goal", plrno
);
1860 if (research
->tech_goal
== A_NONE
1861 || NULL
== valid_advance_by_number(research
->tech_goal
)) {
1862 /* Old servers (1.14.1) saved both A_UNSET and A_NONE by 0
1863 * Here 0 means A_UNSET
1865 research
->tech_goal
= A_UNSET
;
1867 /* Some sane defaults */
1868 plr
->ai_common
.fuzzy
= 0; /* set later */
1869 plr
->ai_common
.expand
= 100; /* set later */
1870 plr
->ai_common
.science_cost
= 100; /* set later */
1871 plr
->ai_common
.skill_level
=
1872 ai_level_convert(secfile_lookup_int_default(file
, game
.info
.skill_level
,
1873 "player%d.ai.skill_level", plrno
));
1874 if (plr
->ai_controlled
&& !ai_level_is_valid(plr
->ai_common
.skill_level
)) {
1875 plr
->ai_common
.skill_level
= ai_level_convert(GAME_OLD_DEFAULT_SKILL_LEVEL
);
1877 if (plr
->ai_controlled
) {
1878 /* Set AI parameters */
1879 set_ai_level_directer(plr
, plr
->ai_common
.skill_level
);
1882 if (!secfile_lookup_int(file
, &plr
->economic
.gold
, "player%d.gold", plrno
)
1883 || !secfile_lookup_int(file
, &plr
->economic
.tax
, "player%d.tax", plrno
)
1884 || !secfile_lookup_int(file
, &plr
->economic
.science
, "player%d.science", plrno
)
1885 || !secfile_lookup_int(file
, &plr
->economic
.luxury
, "player%d.luxury", plrno
)) {
1886 log_error("%s", secfile_error());
1890 plr
->server
.bulbs_last_turn
=
1891 secfile_lookup_int_default(file
, 0,
1892 "player%d.bulbs_last_turn", plrno
);
1894 /* The number of techs and future techs the player has
1895 * researched/acquired. */
1896 if (!secfile_lookup_int(file
, &research
->techs_researched
,
1897 "player%d.researchpoints", plrno
)
1898 || !secfile_lookup_int(file
, &research
->future_tech
,
1899 "player%d.futuretech", plrno
)
1900 || !secfile_lookup_int(file
, &research
->bulbs_researched
,
1901 "player%d.researched", plrno
)) {
1902 log_error("%s", secfile_error());
1906 /* We use default values for bulbs_researching_saved, researching_saved,
1907 * and got_tech to preserve backwards-compatibility with save files
1908 * that didn't store this information. But the local variables have
1909 * changed to reflect such minor differences. */
1910 research
->bulbs_researching_saved
=
1911 secfile_lookup_int_default(file
, 0,
1912 "player%d.researched_before", plrno
);
1913 research
->researching_saved
=
1914 technology_load(file
, "player%d.research_changed_from", plrno
);
1916 research
->researching
=
1917 technology_load(file
, "player%d.researching", plrno
);
1919 if (research
->researching
== A_NONE
1920 || NULL
== valid_advance_by_number(research
->researching
)) {
1921 /* Old servers (1.14.1) used to save A_FUTURE by 0
1922 * This has to be interpreted from context because A_NONE was also
1925 research
->researching
= A_FUTURE
;
1928 research
->got_tech
=
1929 secfile_lookup_bool_default(file
, FALSE
,
1930 "player%d.research_got_tech", plrno
);
1932 /* For new savegames using the technology_order[] list, any unknown
1933 * inventions are ignored. Older games are more strictly enforced,
1934 * as an invalid index is probably indication of corruption.
1936 p
= secfile_lookup_str(file
, "player%d.invs_new", plrno
);
1938 log_fatal("Player %d: no inventions info", plrno
);
1941 for (k
= 0; k
< technology_order_size
&& p
[k
]; k
++) {
1943 struct advance
*padvance
= advance_by_rule_name(technology_order
[k
]);
1945 research_invention_set(research
, advance_number(padvance
),
1952 if (!secfile_lookup_bool(file
, &plr
->server
.got_first_city
,
1953 "player%d.capital", plrno
)) {
1954 log_error("%s", secfile_error());
1959 /* The old-style "revolution" value indicates the number of turns until
1960 * the revolution is complete, or 0 if there is no revolution. The
1961 * new-style "revolution_finishes" value indicates the turn in which
1962 * the revolution will complete (which may be less than the current
1963 * turn) or -1 if there is no revolution. */
1964 int revolution
= secfile_lookup_int_default(file
, 0, "player%d.revolution",
1967 if (revolution
== 0) {
1968 if (government_of_player(plr
) != game
.government_during_revolution
) {
1971 /* some old savegames may be buggy */
1972 revolution
= game
.info
.turn
+ 1;
1975 revolution
= game
.info
.turn
+ revolution
;
1977 plr
->revolution_finishes
1978 = secfile_lookup_int_default(file
, revolution
,
1979 "player%d.revolution_finishes", plrno
);
1982 /* Unit statistics. */
1983 plr
->score
.units_built
=
1984 secfile_lookup_int_default(file
, 0, "player%d.units_built", plrno
);
1985 plr
->score
.units_killed
=
1986 secfile_lookup_int_default(file
, 0, "player%d.units_killed", plrno
);
1987 plr
->score
.units_lost
=
1988 secfile_lookup_int_default(file
, 0, "player%d.units_lost", plrno
);
1991 struct player_spaceship
*ship
= &plr
->spaceship
;
1996 fc_snprintf(prefix
, sizeof(prefix
), "player%d.spaceship", plrno
);
1997 spaceship_init(ship
);
1999 if (!secfile_lookup_int(file
, &ei
,
2000 "%s.state", prefix
)) {
2001 log_error("%s", secfile_error());
2007 if (ship
->state
!= SSHIP_NONE
) {
2008 if (!secfile_lookup_int(file
, &ship
->structurals
, "%s.structurals", prefix
)
2009 || !secfile_lookup_int(file
, &ship
->components
, "%s.components", prefix
)
2010 || !secfile_lookup_int(file
, &ship
->modules
, "%s.modules", prefix
)
2011 || !secfile_lookup_int(file
, &ship
->fuel
, "%s.fuel", prefix
)
2012 || !secfile_lookup_int(file
, &ship
->propulsion
, "%s.propulsion", prefix
)
2013 || !secfile_lookup_int(file
, &ship
->habitation
, "%s.habitation", prefix
)
2014 || !secfile_lookup_int(file
, &ship
->life_support
, "%s.life_support", prefix
)
2015 || !secfile_lookup_int(file
, &ship
->solar_panels
, "%s.solar_panels", prefix
)) {
2016 log_error("%s", secfile_error());
2020 st
= secfile_lookup_str(file
, "%s.structure", prefix
);
2021 for (i
= 0; i
< NUM_SS_STRUCTURALS
; i
++) {
2023 /* Already not set */
2024 } else if (st
[i
] == '1') {
2025 BV_SET(ship
->structure
, i
);
2027 log_error("invalid spaceship structure '%c' (%d)", st
[i
], st
[i
]);
2030 if (ship
->state
>= SSHIP_LAUNCHED
) {
2031 if (!secfile_lookup_int(file
, &ship
->launch_year
,
2032 "%s.launch_year", prefix
)) {
2033 log_error("%s", secfile_error());
2037 spaceship_calc_derived(ship
);
2041 BV_CLR_ALL(plr
->real_embassy
);
2042 if (has_capability("embassies2", savefile_options
)) {
2043 /* done while loading diplstates data */
2044 } else if (has_capability("embassies", savefile_options
)) {
2045 players_iterate(pother
) {
2046 if (secfile_lookup_bool_default(file
, FALSE
, "player%d.embassy%d",
2047 plrno
, player_index(pother
))) {
2048 BV_SET(plr
->real_embassy
, player_index(pother
));
2050 } players_iterate_end
;
2052 /* Required for 2.0 and earlier savegames. Remove eventually and make
2053 * the cap check mandatory. */
2054 int embassy
= secfile_lookup_int_default(file
, 0,
2055 "player%d.embassy", plrno
);
2057 players_iterate(pother
) {
2058 if (embassy
& (1 << player_index(pother
))) {
2059 BV_SET(plr
->real_embassy
, player_index(pother
));
2061 } players_iterate_end
;
2064 /* "Old" observer players will still be loaded but are considered dead. */
2065 players_iterate(aplayer
) {
2066 struct player_diplstate
*ds
= player_diplstate_get(plr
, aplayer
);
2067 i
= player_index(aplayer
);
2070 plr
->ai_common
.love
[i
]
2071 = secfile_lookup_int_default(file
, 1, "player%d.ai%d.love", plrno
, i
);
2073 /* diplomatic state */
2076 secfile_lookup_int_default(file
, DS_WAR
,
2077 "player%d.diplstate%d.type", plrno
, i
);
2079 secfile_lookup_int_default(file
, DS_WAR
,
2080 "player%d.diplstate%d.max_state", plrno
, i
);
2081 ds
->first_contact_turn
=
2082 secfile_lookup_int_default(file
, 0,
2083 "player%d.diplstate%d.first_contact_turn", plrno
, i
);
2085 secfile_lookup_int_default(file
, -2,
2086 "player%d.diplstate%d.turns_left", plrno
, i
);
2087 ds
->has_reason_to_cancel
=
2088 secfile_lookup_int_default(file
, 0,
2089 "player%d.diplstate%d.has_reason_to_cancel",
2091 ds
->contact_turns_left
=
2092 secfile_lookup_int_default(file
, 0,
2093 "player%d.diplstate%d.contact_turns_left", plrno
, i
);
2095 if (has_capability("embassies2", savefile_options
)
2096 && secfile_lookup_bool_default(file
, FALSE
,
2097 "player%d.diplstate%d.embassy",
2099 BV_SET(plr
->real_embassy
, player_index(aplayer
));
2101 } players_iterate_end
;
2103 CALL_FUNC_EACH_AI(player_load
, plr
, file
, plrno
);
2106 /****************************************************************************
2107 Load one tile of the city map. Returns FALSE if the end of the string is
2110 TODO: Compatibility function for savegame of version 2.2.x. Remove if
2111 backwards compatibility is removed.
2112 ****************************************************************************/
2113 /* enum city_tile_type string constants for savegame */
2114 #define S_TILE_EMPTY '0'
2115 #define S_TILE_WORKER '1'
2116 #define S_TILE_UNAVAILABLE '2'
2117 #define S_TILE_UNKNOWN '?'
2118 static bool player_load_city_tile_S22(int plrno
, int i
, struct city
*pcity
,
2119 int x
, int y
, char tile_status
,
2122 int radius_sq
= city_map_radius_sq_get(pcity
);
2123 struct city
*pwork
= NULL
;
2124 struct tile
*ptile
= is_valid_city_coords(radius_sq
, x
, y
)
2125 ? city_map_to_tile(city_tile(pcity
), radius_sq
, x
, y
)
2128 switch (tile_status
) {
2130 /* too short, will yield very odd results! */
2135 if (NULL
== ptile
) {
2136 log_worker("player%d.c%d.workers {%d, %d} '%c' not valid for "
2137 "(%d, %d) \"%s\"[%d], ignoring",
2138 plrno
, i
, x
, y
, tile_status
,
2139 TILE_XY(city_tile(pcity
)), city_name_get(pcity
),
2140 city_size_get(pcity
));
2141 } else if (NULL
!= (pwork
= tile_worked(ptile
))) {
2142 log_worker("player%d.c%d.workers {%d, %d} '%c' conflict at "
2143 "(%d, %d) for (%d ,%d) \"%s\"[%d] with (%d, %d) "
2144 "\"%s\"[%d], converting to unavailable",
2145 plrno
, i
, x
, y
, tile_status
, TILE_XY(ptile
),
2146 TILE_XY(city_tile(pcity
)), city_name_get(pcity
),
2147 city_size_get(pcity
),
2148 TILE_XY(city_tile(pwork
)), city_name_get(pwork
),
2149 city_size_get(pwork
));
2154 if (NULL
== ptile
) {
2155 log_worker("player%d.c%d.workers {%d, %d} '%c' not valid for "
2156 "(%d, %d) \"%s\"[%d], ignoring",
2157 plrno
, i
, x
, y
, tile_status
,
2158 TILE_XY(city_tile(pcity
)), city_name_get(pcity
),
2159 city_size_get(pcity
));
2160 } else if (NULL
!= (pwork
= tile_worked(ptile
))) {
2161 log_worker("player%d.c%d.workers {%d, %d} '%c' conflict at "
2162 "(%d, %d) for (%d, %d) \"%s\"[%d] with (%d, %d) "
2163 "\"%s\"[%d], converting to default specialist",
2164 plrno
, i
, x
, y
, tile_status
, TILE_XY(ptile
),
2165 TILE_XY(city_tile(pcity
)), city_name_get(pcity
),
2166 city_size_get(pcity
),
2167 TILE_XY(city_tile(pwork
)), city_name_get(pwork
),
2168 city_size_get(pwork
));
2170 pcity
->specialists
[DEFAULT_SPECIALIST
]++;
2171 auto_arrange_workers(pcity
);
2174 tile_set_worked(ptile
, pcity
);
2179 case S_TILE_UNAVAILABLE
:
2183 case S_TILE_UNKNOWN
:
2184 if (NULL
== ptile
) {
2185 /* already set C_TILE_UNUSABLE */
2187 log_worker("player%d.c%d.workers {%d, %d} '%c' not valid at "
2188 "(%d, %d) for (%d, %d) \"%s\"[%d], converting to "
2189 "unavailable", plrno
, i
, x
, y
, tile_status
, TILE_XY(ptile
),
2190 TILE_XY(city_tile(pcity
)), city_name_get(pcity
), city_size_get(pcity
));
2195 log_worker("player%d.c%d.workers {%d, %d} '%c' not valid for "
2196 "(%d, %d) \"%s\"[%d], ignoring", plrno
, i
, x
, y
, tile_status
,
2197 TILE_XY(city_tile(pcity
)), city_name_get(pcity
), city_size_get(pcity
));
2204 #undef S_TILE_WORKER
2205 #undef S_TILE_UNAVAILABLE
2206 #undef S_TILE_UNKNOWN
2208 /****************************************************************************
2209 Load map of tiles worked by cities. The return pointer has to be freed by
2211 ****************************************************************************/
2212 static int *player_load_cities_worked_map(struct section_file
*file
,
2213 const char *savefile_options
)
2215 int *worked_tiles
= NULL
;
2218 if (!has_capability("tile_worked", savefile_options
)) {
2222 worked_tiles
= fc_malloc(MAP_INDEX_SIZE
* sizeof(*worked_tiles
));
2224 for (y
= 0; y
< game
.map
.ysize
; y
++) {
2225 const char *buffer
= secfile_lookup_str(file
, "map.worked%03d", y
);
2226 const char *ptr
= buffer
;
2228 if (buffer
== NULL
) {
2229 log_error("Savegame corrupt - map line %d not found.", y
);
2234 for (x
= 0; x
< game
.map
.xsize
; x
++) {
2235 char token
[TOKEN_SIZE
];
2237 struct tile
*ptile
= native_pos_to_tile(x
, y
);
2239 scanin(&ptr
, ",", token
, sizeof(token
));
2240 if (token
[0] == '\0') {
2241 log_error("Savegame corrupt - map size not correct.");
2245 if (strcmp(token
, "-") == 0) {
2248 if (!str_to_int(token
, &number
) || number
<= 0) {
2249 log_error("Savegame corrupt - got tile worked by city "
2250 "id = %s in (%d, %d).", token
, x
, y
);
2256 worked_tiles
[ptile
->index
] = number
;
2260 return worked_tiles
;
2263 /****************************************************************************
2264 Load the cities of a player.
2265 ****************************************************************************/
2266 static void player_load_cities(struct player
*plr
, int plrno
,
2267 struct section_file
*file
,
2268 const char *savefile_options
,
2269 const char **improvement_order
,
2270 size_t improvement_order_size
,
2273 char named
[MAX_LEN_NAME
], citystr
[32];
2274 struct player
*past
;
2282 if (!secfile_lookup_int(file
, &ncities
,
2283 "player%d.ncities", plrno
)) {
2284 log_error("%s", secfile_error());
2288 if (!plr
->is_alive
&& ncities
> 0) {
2289 log_error("player%d.ncities=%d for dead player!", plrno
, ncities
);
2290 ncities
= 0; /* Some old savegames may be buggy. */
2293 if (!plr
->server
.got_first_city
&& ncities
> 0) {
2294 /* Probably barbarians in an old savegame; fix up */
2295 plr
->server
.got_first_city
= TRUE
;
2298 for (i
= 0; i
< ncities
; i
++) { /* read the cities */
2299 int sp_count
= 0, workers
= 0;
2301 struct tile
*pcenter
;
2303 if (!secfile_lookup_int(file
, &nat_x
, "player%d.c%d.x", plrno
, i
)
2304 || !secfile_lookup_int(file
, &nat_y
, "player%d.c%d.y", plrno
, i
)) {
2305 log_error("%s", secfile_error());
2309 pcenter
= native_pos_to_tile(nat_x
, nat_y
);
2310 if (NULL
== pcenter
) {
2311 log_fatal("player%d.c%d invalid tile (%d, %d)",
2312 plrno
, i
, nat_x
, nat_y
);
2316 if (NULL
!= tile_city(pcenter
)) {
2317 log_fatal("player%d.c%d duplicate city (%d, %d)",
2318 plrno
, i
, nat_x
, nat_y
);
2322 /* lookup name out of order */
2323 fc_snprintf(named
, sizeof(named
), "player%d.c%d.name", plrno
, i
);
2324 /* instead of dying, use name string for damaged name */
2325 name
= secfile_lookup_str_default(file
, named
, "%s", named
);
2326 /* copied into city->name */
2327 pcity
= create_city_virtual(plr
, pcenter
, name
);
2328 adv_city_alloc(pcity
);
2329 citizens_init(pcity
);
2331 if (!secfile_lookup_int(file
, &pcity
->id
, "player%d.c%d.id", plrno
, i
)) {
2332 log_error("%s", secfile_error());
2336 identity_number_reserve(pcity
->id
);
2337 idex_register_city(pcity
);
2339 id
= secfile_lookup_int_default(file
, plrno
,
2340 "player%d.c%d.original", plrno
, i
);
2341 past
= player_by_number(id
);
2343 pcity
->original
= past
;
2346 /* no city_choose_build_default(), values loaded below! */
2352 if (!secfile_lookup_int(file
, &value
,
2353 "player%d.c%d.size", plrno
, i
)) {
2354 log_error("%s", secfile_error());
2358 size
= (citizens
)value
; /* set the correct type */
2359 if (value
!= (int)size
) {
2360 log_error("Invalid city size: %d; set to %d.", value
, size
);
2362 city_size_set(pcity
, size
);
2365 specialist_type_iterate(sp
) {
2368 if (!secfile_lookup_int(file
, &value
,
2369 "player%d.c%d.n%s", plrno
, i
,
2370 specialist_rule_name
2371 (specialist_by_number(sp
)))) {
2372 log_error("%s", secfile_error());
2376 pcity
->specialists
[sp
] = (citizens
)value
; /* set the correct type */
2377 if (value
!= (int)pcity
->specialists
[sp
]) {
2378 log_error("Invalid number of specialists: %d; set to %d.", value
,
2379 pcity
->specialists
[sp
]);
2381 sp_count
+= pcity
->specialists
[sp
];
2382 } specialist_type_iterate_end
;
2384 for (j
= 0; j
< MAX_TRADE_ROUTES
; j
++) {
2385 pcity
->trade
[j
] = secfile_lookup_int_default(file
, 0,
2386 "player%d.c%d.traderoute%d",
2390 if (!secfile_lookup_int(file
, &pcity
->food_stock
,
2391 "player%d.c%d.food_stock",
2393 || !secfile_lookup_int(file
, &pcity
->shield_stock
,
2394 "player%d.c%d.shield_stock",
2396 log_error("%s", secfile_error());
2401 secfile_lookup_int_default(file
, 0, "player%d.c%d.airlift", plrno
,i
);
2403 secfile_lookup_bool_default(file
, FALSE
, "player%d.c%d.was_happy",
2405 pcity
->turn_plague
=
2406 secfile_lookup_int_default(file
, 0, "player%d.c%d.turn_plague",
2409 if (!secfile_lookup_int(file
, &pcity
->anarchy
,
2410 "player%d.c%d.anarchy", plrno
, i
)) {
2411 log_error("%s", secfile_error());
2416 secfile_lookup_int_default(file
, 0, "player%d.c%d.rapture",
2418 pcity
->server
.steal
=
2419 secfile_lookup_int_default(file
, 0, "player%d.c%d.steal",
2422 /* before did_buy for undocumented hack */
2423 pcity
->turn_founded
=
2424 secfile_lookup_int_default(file
, -2, "player%d.c%d.turn_founded",
2426 if (!secfile_lookup_int(file
, &j
, "player%d.c%d.did_buy",
2428 log_error("%s", secfile_error());
2432 pcity
->did_buy
= (j
!= 0);
2433 if (j
== -1 && pcity
->turn_founded
== -2) {
2434 /* undocumented hack */
2435 pcity
->turn_founded
= game
.info
.turn
;
2438 secfile_lookup_bool_default(file
, FALSE
, "player%d.c%d.did_sell", plrno
,i
);
2440 if (has_capability("turn_last_built", savefile_options
)) {
2441 if (!secfile_lookup_int(file
, &pcity
->turn_last_built
,
2442 "player%d.c%d.turn_last_built",
2444 log_error("%s", secfile_error());
2447 /* Before, turn_last_built was stored as a year. There is no easy
2448 * way to convert this into a turn value. */
2449 pcity
->turn_last_built
= 0;
2452 kind
= secfile_lookup_str(file
, "player%d.c%d.currently_building_kind",
2455 /* before 2.2.0 unit production was indicated by flag. */
2456 bool is_unit
= secfile_lookup_bool_default(file
, FALSE
,
2457 "player%d.c%d.is_building_unit",
2459 kind
= universals_n_name(is_unit
? VUT_UTYPE
: VUT_IMPROVEMENT
);
2462 name
= secfile_lookup_str(file
, "player%d.c%d.currently_building_name",
2465 log_fatal("Player %d, city %d: No production name", plrno
, i
);
2468 pcity
->production
= universal_by_rule_name(kind
, name
);
2469 if (pcity
->production
.kind
== universals_n_invalid()) {
2470 log_fatal("player%d.c%d.currently_building: unknown \"%s\" \"%s\".",
2471 plrno
, i
, kind
, name
);
2475 kind
= secfile_lookup_str(file
, "player%d.c%d.changed_from_kind",
2478 /* before 2.2.0 unit production was indicated by flag. */
2479 bool is_unit
= secfile_lookup_bool_default(file
, FALSE
,
2480 "player%d.c%d.changed_from_is_unit",
2482 kind
= universals_n_name(is_unit
? VUT_UTYPE
: VUT_IMPROVEMENT
);
2485 name
= secfile_lookup_str(file
, "player%d.c%d.changed_from_name",
2488 log_fatal("Player %d, city %d: No old production name",
2492 pcity
->changed_from
= universal_by_rule_name(kind
, name
);
2493 if (pcity
->changed_from
.kind
== universals_n_invalid()) {
2494 log_fatal("player%d.c%d.changed_from: unknown \"%s\" \"%s\".",
2495 plrno
, i
, kind
, name
);
2499 pcity
->before_change_shields
=
2500 secfile_lookup_int_default(file
, pcity
->shield_stock
,
2501 "player%d.c%d.before_change_shields", plrno
, i
);
2502 pcity
->caravan_shields
=
2503 secfile_lookup_int_default(file
, 0,
2504 "player%d.c%d.caravan_shields", plrno
, i
);
2505 pcity
->disbanded_shields
=
2506 secfile_lookup_int_default(file
, 0,
2507 "player%d.c%d.disbanded_shields", plrno
, i
);
2508 pcity
->last_turns_shield_surplus
=
2509 secfile_lookup_int_default(file
, 0,
2510 "player%d.c%d.last_turns_shield_surplus",
2513 pcity
->style
= city_style(pcity
);
2515 pcity
->server
.synced
= FALSE
; /* must re-sync with clients */
2517 /* Fix for old buggy savegames. */
2518 if (!has_capability("known32fix", savefile_options
)
2520 city_tile_iterate(city_map_radius_sq_get(pcity
), pcenter
, tile1
) {
2521 map_set_known(tile1
, plr
);
2522 } city_tile_iterate_end
;
2525 city_freeze_workers(pcity
);
2527 if (worked_tiles
== NULL
) {
2528 /* 2.2.x savegame */
2529 #define CITY_MAP_OLD_RADIUS 2
2530 #define CITY_MAP_OLD_SIZE (CITY_MAP_OLD_RADIUS * 2 + 1)
2531 city_map_radius_sq_set(pcity
, CITY_MAP_OLD_RADIUS
2532 * CITY_MAP_OLD_RADIUS
+ 1);
2533 p
= secfile_lookup_str_default(file
, "", "player%d.c%d.workers",
2536 if (strlen(p
) != CITY_MAP_OLD_SIZE
* CITY_MAP_OLD_SIZE
) {
2537 /* FIXME: somehow need to rearrange */
2538 log_worker("player%d.c%d.workers length %lu not equal city"
2539 " map size %lu, needs rearranging!", plrno
, i
,
2540 (unsigned long)strlen(p
),
2541 (unsigned long)(CITY_MAP_OLD_SIZE
* CITY_MAP_OLD_SIZE
));
2545 /* start the loop at offset xy_min compared to the new max
2546 * city radius and end at xy_max */
2547 int xy_min
= CITY_MAP_MAX_RADIUS
- CITY_MAP_OLD_RADIUS
;
2548 int xy_max
= CITY_MAP_MAX_RADIUS
+ CITY_MAP_OLD_RADIUS
+ 1;
2550 for(y
= xy_min
; y
< xy_max
; y
++) {
2555 for(x
= xy_min
; x
< xy_max
; x
++) {
2557 ok
= player_load_city_tile_S22(plrno
, i
, pcity
, x
, y
, *p
,
2563 #undef CITY_MAP_OLD_SIZE
2564 #undef CITY_MAP_OLD_RADIUS
2566 /* load new savegame with variable (squared) city radius and
2567 * worked tiles map */
2569 /* FIXME: Dummy to mark this value as used. It is needed to prevent
2570 * old servers from crashing if a new savegame is loaded. */
2571 (void) secfile_entry_lookup(file
, "player%d.c%d.workers", plrno
, i
);
2574 = secfile_lookup_int_default(file
, -1, "player%d.c%d.city_radius_sq",
2576 city_map_radius_sq_set(pcity
, radius_sq
);
2578 city_tile_iterate(radius_sq
, city_tile(pcity
), ptile
) {
2579 if (worked_tiles
[ptile
->index
] == pcity
->id
) {
2580 tile_set_worked(ptile
, pcity
);
2584 /* set this tile to unused; a check for not resetted tiles is
2585 * included in game_load_internal() */
2586 worked_tiles
[ptile
->index
] = -1;
2589 } city_tile_iterate_end
;
2592 if (tile_worked(pcenter
) != pcity
) {
2593 struct city
*pwork
= tile_worked(pcenter
);
2595 if (NULL
!= pwork
) {
2596 log_error("[player%d.c%d] city center of '%s' (%d,%d) [%d] is "
2597 "worked by '%s' (%d,%d) [%d]; repairing ", plrno
, i
,
2598 city_name_get(pcity
), TILE_XY(pcenter
), city_size_get(pcity
),
2599 city_name_get(pwork
), TILE_XY(city_tile(pwork
)),
2600 city_size_get(pwork
));
2602 tile_set_worked(pcenter
, NULL
); /* remove tile from pwork */
2603 pwork
->specialists
[DEFAULT_SPECIALIST
]++;
2604 auto_arrange_workers(pwork
);
2606 log_error("[player%d.c%d] city center of '%s' (%d,%d) [%d] is "
2607 "empty; repairing ", plrno
, i
, city_name_get(pcity
),
2608 TILE_XY(pcenter
), city_size_get(pcity
));
2612 tile_set_worked(pcenter
, pcity
);
2613 city_repair_size(pcity
, -1);
2616 k
= city_size_get(pcity
) - sp_count
- (workers
- FREE_WORKED_TILES
);
2618 log_error("[player%d.c%d] size mismatch for '%s' (%d,%d): "
2619 "size [%d] != (workers [%d] - free worked tiles [%d]) + "
2621 plrno
, i
, city_name_get(pcity
), TILE_XY(pcenter
),
2622 city_size_get(pcity
),
2623 workers
, FREE_WORKED_TILES
, sp_count
);
2626 city_repair_size(pcity
, k
);
2629 /* Initialise list of improvements with City- and Building-wide
2631 for (k
= 0; k
< ARRAY_SIZE(pcity
->built
); k
++) {
2632 pcity
->built
[k
].turn
= I_NEVER
;
2635 /* For new savegames using the improvement_order[] list, any unknown
2636 * improvements are ignored. Older games are more strictly enforced,
2637 * as an invalid index is probably indication of corruption.
2639 p
= secfile_lookup_str(file
, "player%d.c%d.improvements_new",
2642 log_fatal("Player %d, city %d: No buildings information",
2646 for (k
= 0; k
< improvement_order_size
&& p
[k
]; k
++) {
2648 struct impr_type
*pimprove
=
2649 improvement_by_rule_name(improvement_order
[k
]);
2651 city_add_improvement(pcity
, pimprove
);
2657 /* worklist_init() done in create_city_virtual() */
2658 worklist_load(file
, &pcity
->worklist
, "player%d.c%d", plrno
, i
);
2660 /* after 2.1.0 new options format. Old options are lost on upgrade. */
2661 BV_CLR_ALL(pcity
->city_options
);
2662 for (j
= 0; j
< CITYO_LAST
; j
++) {
2663 if (secfile_lookup_bool_default(file
, FALSE
,
2664 "player%d.c%d.option%d",
2666 BV_SET(pcity
->city_options
, j
);
2670 /* Create new citizens info, since these old savegames do not contain
2672 citizens_update(pcity
, NULL
);
2674 fc_snprintf(citystr
, sizeof(citystr
), "player%d.c%d", plrno
, i
);
2675 CALL_FUNC_EACH_AI(city_load
, file
, pcity
, citystr
);
2677 /* After everything is loaded, but before vision. */
2678 map_claim_ownership(pcenter
, plr
, pcenter
, TRUE
);
2680 /* adding the city contribution to fog-of-war */
2681 pcity
->server
.vision
= vision_new(plr
, pcenter
);
2682 vision_reveal_tiles(pcity
->server
.vision
, game
.server
.vision_reveal_tiles
);
2683 city_refresh_vision(pcity
);
2685 city_list_append(plr
->cities
, pcity
);
2689 /****************************************************************************
2690 Load player custom (client specific) attribute data
2691 ****************************************************************************/
2692 static void player_load_attributes(struct player
*plr
, int plrno
,
2693 struct section_file
*file
)
2695 /* Toss any existing attribute_block (should not exist) */
2696 if (plr
->attribute_block
.data
) {
2697 free(plr
->attribute_block
.data
);
2698 plr
->attribute_block
.data
= NULL
;
2701 /* This is a big heap of opaque data for the client, check everything! */
2702 plr
->attribute_block
.length
= secfile_lookup_int_default(
2703 file
, 0, "player%d.attribute_v2_block_length", plrno
);
2705 if (0 > plr
->attribute_block
.length
) {
2706 log_error("player%d.attribute_v2_block_length=%d too small", plrno
,
2707 plr
->attribute_block
.length
);
2708 plr
->attribute_block
.length
= 0;
2709 } else if (MAX_ATTRIBUTE_BLOCK
< plr
->attribute_block
.length
) {
2710 log_error("player%d.attribute_v2_block_length=%d too big (max %d)",
2711 plrno
, plr
->attribute_block
.length
, MAX_ATTRIBUTE_BLOCK
);
2712 plr
->attribute_block
.length
= 0;
2713 } else if (0 < plr
->attribute_block
.length
) {
2715 size_t actual_length
;
2719 plr
->attribute_block
.data
= fc_malloc(plr
->attribute_block
.length
);
2721 if (!secfile_lookup_int(file
, "ed_length
,
2722 "player%d.attribute_v2_block_length_quoted", plrno
)) {
2723 log_error("%s", secfile_error());
2727 quoted
= fc_malloc(quoted_length
+ 1);
2730 if (!secfile_lookup_int(file
, &parts
,
2731 "player%d.attribute_v2_block_parts", plrno
)) {
2732 log_error("%s", secfile_error());
2736 for (part_nr
= 0; part_nr
< parts
; part_nr
++) {
2737 const char *current
=
2738 secfile_lookup_str(file
, "player%d.attribute_v2_block_data.part%d",
2741 log_error("attribute_v2_block_parts=%d actual=%d", parts
, part_nr
);
2744 log_debug("attribute_v2_block_length_quoted=%lu have=%lu part=%lu",
2745 (unsigned long) quoted_length
,
2746 (unsigned long) strlen(quoted
),
2747 (unsigned long) strlen(current
));
2748 fc_assert(strlen(quoted
) + strlen(current
) <= quoted_length
);
2749 strcat(quoted
, current
);
2751 fc_assert_msg(quoted_length
== strlen(quoted
),
2752 "attribute_v2_block_length_quoted=%lu actual=%lu",
2753 (unsigned long) quoted_length
,
2754 (unsigned long) strlen(quoted
));
2757 unquote_block(quoted
,
2758 plr
->attribute_block
.data
,
2759 plr
->attribute_block
.length
);
2760 fc_assert(actual_length
== plr
->attribute_block
.length
);
2765 /****************************************************************************
2766 Load the private vision map for fog of war
2767 ****************************************************************************/
2768 static void player_load_vision(struct player
*plr
, int plrno
,
2769 struct section_file
*file
,
2770 const char *savefile_options
,
2771 const enum tile_special_type
*special_order
,
2772 size_t num_special_types
,
2773 const char **improvement_order
,
2774 size_t improvement_order_size
,
2775 struct base_type
**base_order
,
2776 size_t num_base_types
)
2782 secfile_lookup_int_default(file
, -1, "player%d.total_ncities", plrno
);
2785 map_know_and_see_all(plr
);
2788 * - it from a fog of war build;
2789 * - fog of war was on (otherwise the private map wasn't saved);
2790 * - is not from a "unit only" fog of war save file;
2791 * - game.save_private_map is not set to FALSE in the scenario / savegame
2793 if (NULL
!= secfile_entry_by_path(file
, "game.fogofwar")
2794 && game
.info
.fogofwar
2795 && -1 != total_ncities
2796 && secfile_lookup_bool_default(file
, TRUE
, "game.save_private_map")) {
2797 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2798 secfile_lookup_str(file
, "player%d.map_t%03d",
2800 map_get_player_tile(ptile
, plr
)->terrain
=
2803 if (special_order
) {
2804 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2805 secfile_lookup_str(file
, "player%d.map_res%03d", plrno
, vnat_y
),
2806 map_get_player_tile(ptile
, plr
)->resource
2807 = identifier_to_resource(ch
));
2809 special_halfbyte_iterate(j
, num_special_types
) {
2810 char buf
[32]; /* enough for sprintf() below */
2811 sprintf (buf
, "player%d.map_spe%02d_%%03d", plrno
, j
);
2812 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2813 secfile_lookup_str(file
, buf
, vnat_y
),
2814 set_savegame_special(ptile
, &map_get_player_tile(ptile
, plr
)->extras
,
2815 ch
, special_order
+ 4 * j
));
2816 } special_halfbyte_iterate_end
;
2818 /* get 4-bit segments of 12-bit "special" field. */
2819 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2820 secfile_lookup_str(file
, "player%d.map_l%03d", plrno
, vnat_y
),
2821 set_savegame_special(ptile
, &map_get_player_tile(ptile
, plr
)->extras
,
2822 ch
, default_specials
+ 0));
2823 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2824 secfile_lookup_str(file
, "player%d.map_u%03d", plrno
, vnat_y
),
2825 set_savegame_special(ptile
, &map_get_player_tile(ptile
, plr
)->extras
,
2826 ch
, default_specials
+ 4));
2827 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2828 secfile_lookup_str_default (file
, NULL
, "player%d.map_n%03d",
2830 set_savegame_special(ptile
, &map_get_player_tile(ptile
, plr
)->extras
,
2831 ch
, default_specials
+ 8));
2832 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2833 secfile_lookup_str(file
, "map.l%03d", vnat_y
),
2834 set_savegame_old_resource(&map_get_player_tile(ptile
, plr
)->resource
,
2835 ptile
->terrain
, ch
, 0));
2836 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2837 secfile_lookup_str(file
, "map.n%03d", vnat_y
),
2838 set_savegame_old_resource(&map_get_player_tile(ptile
, plr
)->resource
,
2839 ptile
->terrain
, ch
, 1));
2842 if (has_capability("bases", savefile_options
)) {
2843 char zeroline
[game
.map
.xsize
+ 1];
2846 /* This is needed when new bases has been added to ruleset, and
2847 * thus game.control.num_base_types is greater than, when game was saved. */
2848 for(xi
= 0; xi
< game
.map
.xsize
; xi
++) {
2853 bases_halfbyte_iterate(j
, num_base_types
) {
2854 char buf
[32]; /* should be enough for snprintf() below */
2856 fc_snprintf(buf
, sizeof(buf
), "player%d.map_b%02d_%%03d", plrno
, j
);
2858 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2859 secfile_lookup_str_default(file
, zeroline
, buf
, vnat_y
),
2860 set_savegame_bases(&map_get_player_tile(ptile
, plr
)->extras
,
2861 ch
, base_order
+ 4 * j
));
2862 } bases_halfbyte_iterate_end
;
2864 /* Already loaded fortresses and airbases as part of specials */
2867 if (game
.server
.foggedborders
) {
2868 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2869 secfile_lookup_str(file
, "player%d.map_owner%03d", plrno
, vnat_y
),
2870 map_get_player_tile(ptile
, plr
)->owner
= identifier_to_player(ch
));
2873 /* get 4-bit segments of 16-bit "updated" field */
2874 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2876 (file
, "player%d.map_ua%03d", plrno
, vnat_y
),
2877 map_get_player_tile(ptile
, plr
)->last_updated
=
2878 ascii_hex2bin(ch
, 0));
2879 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2881 (file
, "player%d.map_ub%03d", plrno
, vnat_y
),
2882 map_get_player_tile(ptile
, plr
)->last_updated
|=
2883 ascii_hex2bin(ch
, 1));
2884 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2886 (file
, "player%d.map_uc%03d", plrno
, vnat_y
),
2887 map_get_player_tile(ptile
, plr
)->last_updated
|=
2888 ascii_hex2bin(ch
, 2));
2889 LOAD_MAP_DATA(ch
, vnat_y
, ptile
,
2891 (file
, "player%d.map_ud%03d", plrno
, vnat_y
),
2892 map_get_player_tile(ptile
, plr
)->last_updated
|=
2893 ascii_hex2bin(ch
, 3));
2895 for (i
= 0; i
< total_ncities
; i
++) {
2896 /* similar to vision_site_new() */
2897 struct vision_site
*pdcity
= vision_site_new(0, NULL
, NULL
);
2899 if (!secfile_lookup_int(file
, &nat_x
, "player%d.dc%d.x", plrno
, i
)
2900 || !secfile_lookup_int(file
, &nat_y
, "player%d.dc%d.y", plrno
, i
)) {
2901 log_error("%s", secfile_error());
2905 pdcity
->location
= native_pos_to_tile(nat_x
, nat_y
);
2906 if (NULL
== pdcity
->location
) {
2907 log_error("player%d.dc%d invalid tile (%d,%d)",
2908 plrno
, i
, nat_x
, nat_y
);
2913 if (!secfile_lookup_int(file
, &pdcity
->identity
,
2914 "player%d.dc%d.id", plrno
, i
)) {
2915 log_error("%s", secfile_error());
2919 if (IDENTITY_NUMBER_ZERO
>= pdcity
->identity
) {
2920 log_error("player%d.dc%d has invalid id (%d); skipping.",
2921 plrno
, i
, pdcity
->identity
);
2926 if (!secfile_lookup_int(file
, &id
, "player%d.dc%d.owner",
2928 log_error("%s", secfile_error());
2933 /* Earlier versions redundantly saved the dummy for their own cities.
2934 * Since 2.2.0, map_claim_ownership() rebuilds them at city load time.
2936 log_verbose("player%d.dc%d has same owner (%d); skipping.",
2942 pdcity
->owner
= player_by_number(id
);
2943 if (NULL
== vision_site_owner(pdcity
)) {
2944 log_error("player%d.dc%d has invalid owner (%d); skipping.",
2954 if (!secfile_lookup_int(file
, &size
,
2955 "player%d.dc%d.size", plrno
, i
)) {
2956 log_error("%s", secfile_error());
2960 city_size
= (citizens
)size
; /* set the correct type */
2961 if (size
!= (int)city_size
) {
2962 log_error("Invalid city size: %d; set to %d.", size
, city_size
);
2964 vision_site_size_set(pdcity
, size
);
2967 pdcity
->occupied
= secfile_lookup_bool_default(file
, FALSE
,
2968 "player%d.dc%d.occupied", plrno
, i
);
2969 pdcity
->walls
= secfile_lookup_bool_default(file
, FALSE
,
2970 "player%d.dc%d.walls", plrno
, i
);
2971 pdcity
->happy
= secfile_lookup_bool_default(file
, FALSE
,
2972 "player%d.dc%d.happy", plrno
, i
);
2973 pdcity
->unhappy
= secfile_lookup_bool_default(file
, FALSE
,
2974 "player%d.dc%d.unhappy", plrno
, i
);
2978 /* Old savegames never have real city image information, so set
2979 * it to special value "server doesn't know" */
2980 pdcity
->city_image
= -100;
2982 /* Initialise list of improvements */
2983 BV_CLR_ALL(pdcity
->improvements
);
2985 p
= secfile_lookup_str(file
, "player%d.dc%d.improvements",
2990 for (k
= 0; k
< improvement_order_size
&& p
[k
]; k
++) {
2992 struct impr_type
*pimprove
=
2993 improvement_by_rule_name(improvement_order
[k
]);
2995 BV_SET(pdcity
->improvements
, improvement_index(pimprove
));
3000 sz_strlcpy(pdcity
->name
, secfile_lookup_str(file
, "player%d.dc%d.name", plrno
, i
));
3002 change_playertile_site(map_get_player_tile(pdcity
->location
, plr
),
3004 identity_number_reserve(pdcity
->identity
);
3007 /* Repair inconsistent player maps. There was a bug in some pre-1.11
3008 savegames, and possibly other versions, and border support changed
3009 from time to time. Anyway, it shouldn't hurt. */
3010 whole_map_iterate(ptile
) {
3011 if (map_is_known_and_seen(ptile
, plr
, V_MAIN
)) {
3012 struct city
*pcity
= tile_city(ptile
);
3014 update_player_tile_knowledge(plr
, ptile
);
3015 reality_check_city(plr
, ptile
);
3017 if (NULL
!= pcity
) {
3018 update_dumb_city(plr
, pcity
);
3021 } whole_map_iterate_end
;
3023 /* We have an old savegame or fog of war was turned off; the
3024 players private knowledge is set to be what he could see
3025 without fog of war */
3026 whole_map_iterate(ptile
) {
3027 if (map_is_known(ptile
, plr
)) {
3028 struct city
*pcity
= tile_city(ptile
);
3030 update_player_tile_last_seen(plr
, ptile
);
3031 update_player_tile_knowledge(plr
, ptile
);
3033 if (NULL
!= pcity
) {
3034 update_dumb_city(plr
, pcity
);
3037 } whole_map_iterate_end
;
3041 /***************************************************************
3042 For each city and tile, sort unit lists according to
3043 ord_city and ord_map values.
3044 ***************************************************************/
3045 static void apply_unit_ordering(void)
3047 players_iterate(pplayer
) {
3048 city_list_iterate(pplayer
->cities
, pcity
) {
3049 unit_list_sort_ord_city(pcity
->units_supported
);
3051 city_list_iterate_end
;
3052 } players_iterate_end
;
3054 whole_map_iterate(ptile
) {
3055 unit_list_sort_ord_map(ptile
->units
);
3056 } whole_map_iterate_end
;
3059 /***************************************************************
3060 Called only in server/stdinhand.c load_command()
3061 Entire ruleset is always sent afterward.
3062 ***************************************************************/
3063 void legacy_game_load(struct section_file
*file
)
3065 bool was_send_city_suppressed
= send_city_suppression(TRUE
);
3066 bool was_send_tile_suppressed
= send_tile_suppression(TRUE
);
3068 game_load_internal(file
);
3069 /* load script state last so we have access to all game data */
3070 script_server_state_load(file
);
3072 send_tile_suppression(was_send_tile_suppressed
);
3073 send_city_suppression(was_send_city_suppressed
);
3076 /***************************************************************
3077 Real game_load function.
3078 ***************************************************************/
3079 static void game_load_internal(struct section_file
*file
)
3083 enum server_states tmp_server_state
;
3084 RANDOM_STATE rstate
;
3086 size_t improvement_order_size
= 0;
3087 size_t technology_order_size
= 0;
3089 const char *scen_text
;
3090 const char **improvement_order
= NULL
;
3091 const char **technology_order
= NULL
;
3092 enum tile_special_type
*special_order
= NULL
;
3093 size_t num_special_types
= 0;
3094 struct base_type
**base_order
= NULL
;
3095 size_t num_base_types
= 0;
3096 const char *savefile_options
= secfile_lookup_str(file
, "savefile.options");
3098 const struct entry
*pentry
;
3102 /* We don't need savefile.reason, but read it anyway to avoid
3103 * warnings about unread secfile entries. */
3104 (void) secfile_entry_by_path(file
, "savefile.reason");
3106 if (has_capability("improvement_order", savefile_options
)) {
3107 improvement_order
= secfile_lookup_str_vec(file
, &improvement_order_size
,
3108 "savefile.improvement_order");
3110 if (has_capability("technology_order", savefile_options
)) {
3111 technology_order
= secfile_lookup_str_vec(file
, &technology_order_size
,
3112 "savefile.technology_order");
3116 scen_text
= secfile_lookup_str_default(file
, "", "scenario.name");
3117 if (scen_text
[0] != '\0') {
3118 game
.scenario
.is_scenario
= TRUE
;
3119 sz_strlcpy(game
.scenario
.name
, scen_text
);
3120 game
.scenario
.authors
[0] = '\0';
3121 scen_text
= secfile_lookup_str_default(file
, "",
3122 "scenario.description");
3123 if (scen_text
[0] != '\0') {
3124 sz_strlcpy(game
.scenario_desc
.description
, scen_text
);
3126 game
.scenario_desc
.description
[0] = '\0';
3128 game
.scenario
.players
3129 = secfile_lookup_bool_default(file
, TRUE
, "scenario.save_players");
3131 /* Old scenarios couldn't prevent cities */
3132 game
.scenario
.prevent_new_cities
= FALSE
;
3133 game
.scenario
.lake_flooding
= FALSE
;
3135 /* Assume any scenario from pre-editor time handmade with special settings. */
3136 game
.scenario
.handmade
= TRUE
;
3138 game
.scenario
.is_scenario
= FALSE
;
3142 if ((string
= secfile_lookup_str(file
, "game.rulesetdir"))) {
3143 /* A ruleset was explicitly required, let's ignore the "rulesetdir"
3144 * capability then. */
3145 if (!strcmp("default", string
)) {
3146 sz_strlcpy(game
.server
.rulesetdir
, "classic");
3148 sz_strlcpy(game
.server
.rulesetdir
, string
);
3151 civstyle
= secfile_lookup_int_default(file
, 2, "game.civstyle");
3152 string
= (civstyle
== 1) ? "civ1" : "classic";
3154 if (!has_capability("rulesetdir", savefile_options
)) {
3155 const char *str2
, *str
=
3156 secfile_lookup_str_default(file
, "classic", "game.info.t.techs");
3158 /* Savegame is missing "rulesetdir" capability, so this is ancient
3159 * terrain ruleset known as "classic", not to be confused with current
3160 * "classic" ruleset. */
3161 if (strcmp("classic",
3162 secfile_lookup_str_default(file
, "foobar",
3163 "game.info.t.terrain")) == 0) {
3164 /* TRANS: Fatal error message. */
3165 log_fatal(_("Saved game uses the ancient format terrain"
3166 " ruleset, and is no longer supported."));
3171 str2 = secfile_lookup_str_default(file, "classic", x); \
3172 if (strcmp(str, str2) != 0) { \
3173 log_normal(_("Warning: Different rulesetdirs ('%s' and '%s') are " \
3174 "no longer supported. Using '%s'."), str, str2, str); \
3177 T("game.info.t.units");
3178 T("game.info.t.buildings");
3179 T("game.info.t.terrain");
3180 T("game.info.t.governments");
3181 T("game.info.t.nations");
3182 T("game.info.t.cities");
3183 T("game.info.t.game");
3186 if (!strcmp("default", str
)) {
3187 sz_strlcpy(game
.server
.rulesetdir
, "classic");
3189 sz_strlcpy(game
.server
.rulesetdir
, str
);
3192 sz_strlcpy(game
.server
.rulesetdir
, string
);
3196 /* Determine if the game is a new one (ex. scenario) or a savegame. */
3197 if (secfile_lookup_bool(file
, &game
.info
.is_new_game
,
3198 "game.save_players")) {
3199 game
.info
.is_new_game
= !game
.info
.is_new_game
;
3201 /* "game.save_players" is missing, try to get the number of players. */
3202 game
.info
.is_new_game
= (!secfile_lookup_int(file
, &i
, "game.nplayers")
3207 if (!load_rulesets(NULL
, TRUE
, FALSE
)) {
3208 /* Failed to load correct ruleset */
3212 /* [settings]; must be *after* loading the ruleset */
3213 settings_game_load(file
, "settings");
3216 game_version
= secfile_lookup_int_default(file
, 0, "game.version");
3218 /* we require at least version 2.0.0 */
3219 if (20000 > game_version
) {
3220 /* TRANS: Fatal error message. */
3221 log_fatal(_("Saved game is too old, at least version 2.0.0 required."));
3225 if (has_capability("resources", savefile_options
)) {
3226 const char **modname
;
3228 enum tile_special_type j
;
3230 modname
= secfile_lookup_str_vec(file
, &num_special_types
,
3231 "savefile.specials");
3232 /* make sure that the size of the array is divisible by 4 */
3233 /* Allocating extra 4 slots, just a couple of bytes, in case of
3234 * num_special_types being divisible by 4 already is intentional.
3235 * Added complexity would cost those couple of bytes in code size alone,
3236 * and we actually need at least one slot immediately after last valid
3237 * one. That's where S_LAST is (or was in version that saved the game)
3238 * and in some cases S_LAST gets written to savegame, at least as
3239 * activity target special when activity targets some base instead.
3240 * By having current S_LAST in that index allows us to map that old
3241 * S_LAST to current S_LAST, just like any real special within range
3243 nmod
= num_special_types
+ (4 - (num_special_types
% 4));
3244 special_order
= fc_calloc(nmod
, sizeof(*special_order
));
3245 for (j
= 0; j
< num_special_types
; j
++) {
3246 if (!strcasecmp("Road", modname
[j
])) {
3247 special_order
[j
] = S_OLD_ROAD
;
3248 } else if (!strcasecmp("Railroad", modname
[j
])) {
3249 special_order
[j
] = S_OLD_RAILROAD
;
3250 } else if (!strcasecmp("River", modname
[j
])) {
3251 special_order
[j
] = S_OLD_RIVER
;
3252 } else if (!strcasecmp("Fortress", modname
[j
])) {
3253 special_order
[j
] = S_OLD_FORTRESS
;
3254 } else if (!strcasecmp("Airbase", modname
[j
])) {
3255 special_order
[j
] = S_OLD_AIRBASE
;
3257 special_order
[j
] = special_by_rule_name(modname
[j
]);
3261 for (; j
< nmod
; j
++) {
3262 special_order
[j
] = S_LAST
;
3266 tmp_server_state
= server_states_invalid();
3267 if ((pentry
= secfile_entry_lookup(file
, "game.server_state"))) {
3268 switch (entry_type(pentry
)) {
3270 /* New in 2.2: server_state as string; see srv_main.h */
3272 const char *state_str
;
3274 if (entry_str_get(pentry
, &state_str
)) {
3275 tmp_server_state
= server_states_by_name(state_str
, strcmp
);
3280 /* In former savegames, server states are saved as magic numbers. */
3282 enum old_server_states
{
3283 OLD_S_S_INITIAL
= 0,
3284 OLD_S_S_GENERATING_WAITING
= 1,
3285 OLD_S_S_RUNNING
= 2,
3291 if (entry_int_get(pentry
, &ei
)) {
3293 switch (saved_state
) {
3294 case OLD_S_S_INITIAL
:
3295 case OLD_S_S_GENERATING_WAITING
:
3296 tmp_server_state
= S_S_INITIAL
;
3298 case OLD_S_S_RUNNING
:
3299 tmp_server_state
= S_S_RUNNING
;
3302 tmp_server_state
= S_S_OVER
;
3310 case ENTRY_FILEREFERENCE
:
3314 if (!server_states_is_valid(tmp_server_state
)) {
3315 /* Don't take any risk! */
3316 tmp_server_state
= S_S_INITIAL
;
3319 if (game
.scenario
.is_scenario
== TRUE
3320 && (tmp_server_state
!= S_S_INITIAL
3321 || (tmp_server_state
== S_S_RUNNING
3322 && game
.scenario
.players
== TRUE
))) {
3323 log_fatal("Invalid scenario definition (server state '%d' and "
3324 "players are %s).", tmp_server_state
,
3325 game
.scenario
.players
? "saved" : "not saved");
3329 if (game
.scenario
.is_scenario
) {
3330 /* Remove all defined players. They are recreated with the skill level
3331 * defined by the scenario. */
3339 set_meta_patches_string(secfile_lookup_str_default(file
,
3340 default_meta_patches_string(),
3341 "game.metapatches"));
3342 game
.server
.meta_info
.user_message_set
=
3343 secfile_lookup_bool_default(file
, FALSE
, "game.user_metamessage");
3344 if (game
.server
.meta_info
.user_message_set
) {
3345 set_user_meta_message_string(secfile_lookup_str_default(file
,
3346 default_meta_message_string(),
3347 "game.metamessage"));
3349 /* To avoid warnings when loading pre-2.1 savegames */
3350 (void) secfile_entry_by_path(file
, "game.metamessage");
3353 if (0 == strcmp(DEFAULT_META_SERVER_ADDR
, srvarg
.metaserver_addr
)) {
3354 /* Do not overwrite this if the user requested a specific metaserver
3355 * from the command line (option --Metaserver). */
3356 sz_strlcpy(srvarg
.metaserver_addr
,
3357 secfile_lookup_str_default(file
, DEFAULT_META_SERVER_ADDR
,
3358 "game.metaserver"));
3360 if ('\0' == srvarg
.serverid
[0]) {
3361 /* Do not overwrite this if the user requested a specific metaserver
3362 * from the command line (option --serverid). */
3363 sz_strlcpy(srvarg
.serverid
,
3364 secfile_lookup_str_default(file
, "", "game.serverid"));
3367 if (!secfile_lookup_int(file
, &game
.info
.gold
, "game.gold")
3368 || !secfile_lookup_int(file
, &game
.info
.tech
, "game.tech")
3369 || !secfile_lookup_int(file
, &level
, "game.skill_level")
3370 || !secfile_lookup_int(file
, &game
.info
.timeout
, "game.timeout")) {
3371 log_error("%s", secfile_error());
3375 game
.info
.skill_level
= ai_level_convert(level
);
3377 if (!ai_level_is_valid(game
.info
.skill_level
)) {
3378 game
.info
.skill_level
= GAME_OLD_DEFAULT_SKILL_LEVEL
;
3381 game
.server
.timeoutint
=
3382 secfile_lookup_int_default(file
, GAME_DEFAULT_TIMEOUTINT
,
3384 game
.server
.timeoutintinc
=
3385 secfile_lookup_int_default(file
, GAME_DEFAULT_TIMEOUTINTINC
,
3386 "game.timeoutintinc");
3387 game
.server
.timeoutinc
=
3388 secfile_lookup_int_default(file
, GAME_DEFAULT_TIMEOUTINC
,
3390 game
.server
.timeoutincmult
=
3391 secfile_lookup_int_default(file
, GAME_DEFAULT_TIMEOUTINCMULT
,
3392 "game.timeoutincmult");
3393 game
.server
.timeoutcounter
=
3394 secfile_lookup_int_default(file
, 1, "game.timeoutcounter");
3396 game
.server
.timeoutaddenemymove
3397 = secfile_lookup_int_default(file
, game
.server
.timeoutaddenemymove
,
3398 "game.timeoutaddenemymove");
3400 game
.server
.unitwaittime
3401 = secfile_lookup_int_default(file
, GAME_DEFAULT_UNITWAITTIME
,
3402 "game.unitwaittime");
3404 game
.server
.end_turn
= secfile_lookup_int_default(file
, 5000,
3407 = secfile_lookup_int_default(file
, GAME_DEFAULT_SHIELDBOX
,
3409 game
.info
.sciencebox
3410 = secfile_lookup_int_default(file
, 0, "game.box_science");
3411 if (game
.info
.sciencebox
== 0) {
3412 /* Researchcost was used for 2.0 and earlier servers. */
3413 game
.info
.sciencebox
3414 = 5 * secfile_lookup_int_default(file
, 0, "game.researchcost");
3415 if (game
.info
.sciencebox
== 0) {
3416 /* With even earlier servers (?) techlevel was used for this info. */
3417 game
.info
.sciencebox
= 5 * secfile_lookup_int_default(file
, 0, "game.techlevel");
3421 if (!secfile_lookup_int(file
, &game
.info
.year
, "game.year")) {
3422 log_error("%s", secfile_error());
3426 game
.info
.year_0_hack
= secfile_lookup_bool_default(file
, FALSE
,
3427 "game.year_0_hack");
3429 if (has_capability("turn", savefile_options
)) {
3430 game
.info
.turn
= secfile_lookup_int_default(file
, 0, "game.turn");
3431 if (0 > game
.info
.turn
) {
3432 /* Some old scenarios claims the game starts at turn -2 for unknown
3433 * reasons. However, since the values -1 (I_NEVER) and -2
3434 * (I_DESTROYED) are used at turn number for the city improvements,
3435 * we cannot use that strange hack anymore. */
3442 if (secfile_lookup_bool(file
, &bval
, "game.simultaneous_phases_now")) {
3443 game
.info
.phase_mode
= (bval
? PMT_CONCURRENT
3444 : PMT_PLAYERS_ALTERNATE
);
3447 game
.info
.phase_mode
= GAME_DEFAULT_PHASE_MODE
;
3450 if (secfile_lookup_bool(file
, &bval
,
3451 "game.simultaneous_phases_stored")) {
3452 game
.server
.phase_mode_stored
= (bval
? PMT_CONCURRENT
3453 : PMT_PLAYERS_ALTERNATE
);
3455 game
.server
.phase_mode_stored
= game
.info
.phase_mode
;
3458 game
.info
.phase_mode
3459 = secfile_lookup_int_default(file
, game
.info
.phase_mode
,
3461 game
.server
.phase_mode_stored
3462 = secfile_lookup_int_default(file
, game
.server
.phase_mode_stored
,
3463 "game.phase_mode_stored");
3465 if (!secfile_lookup_int(file
, &game
.server
.min_players
, "game.min_players")
3466 || !secfile_lookup_int(file
, &game
.server
.max_players
, "game.max_players")
3467 || !secfile_lookup_int(file
, &game
.info
.globalwarming
, "game.globalwarming")
3468 || !secfile_lookup_int(file
, &game
.info
.warminglevel
, "game.warminglevel")) {
3469 log_error("%s", secfile_error());
3473 game
.info
.heating
= secfile_lookup_int_default(file
, 0, "game.heating");
3474 game
.info
.nuclearwinter
= secfile_lookup_int_default(file
, 0, "game.nuclearwinter");
3475 game
.info
.cooling
= secfile_lookup_int_default(file
, 0, "game.cooling");
3476 game
.info
.coolinglevel
= secfile_lookup_int_default(file
, 8, "game.coolinglevel");
3477 game
.info
.notradesize
=
3478 secfile_lookup_int_default(file
, GAME_DEFAULT_NOTRADESIZE
, "game.notradesize");
3479 game
.info
.fulltradesize
=
3480 secfile_lookup_int_default(file
, GAME_DEFAULT_FULLTRADESIZE
, "game.fulltradesize");
3482 game
.info
.trading_city
=
3483 secfile_lookup_bool_default(file
, GAME_DEFAULT_TRADING_CITY
,
3484 "game.trading_city");
3485 game
.info
.trading_tech
=
3486 secfile_lookup_bool_default(file
, GAME_DEFAULT_TRADING_TECH
,
3487 "game.trading_tech");
3488 game
.info
.trading_gold
=
3489 secfile_lookup_bool_default(file
, GAME_DEFAULT_TRADING_GOLD
,
3490 "game.trading_gold");
3492 game
.info
.trademindist
=
3493 secfile_lookup_int_default(file
, GAME_DEFAULT_TRADEMINDIST
, "game.trademindist");
3494 game
.info
.angrycitizen
=
3495 secfile_lookup_bool_default(file
, GAME_DEFAULT_ANGRYCITIZEN
, "game.angrycitizen");
3497 /* Backwards compatibility. Prior to 2.4, citymindist==0 meant to take
3498 * it from ruleset min_dist_bw_cities, but that no longer exists. */
3500 secfile_lookup_int_default(file
, GAME_DEFAULT_CITYMINDIST
,
3501 "game.citymindist");
3502 if (citymindist
!= 0) {
3503 game
.info
.citymindist
= citymindist
;
3504 } /* else leave it at ruleset/server default */
3506 game
.info
.rapturedelay
=
3507 secfile_lookup_int_default(file
, GAME_DEFAULT_RAPTUREDELAY
, "game.rapturedelay");
3508 game
.server
.diplbulbcost
=
3509 secfile_lookup_int_default(file
, GAME_DEFAULT_DIPLBULBCOST
, "game.diplcost");
3510 game
.server
.diplgoldcost
= game
.server
.diplbulbcost
; /* They used to be one value */
3511 game
.server
.freecost
=
3512 secfile_lookup_int_default(file
, GAME_DEFAULT_FREECOST
, "game.freecost");
3513 game
.server
.conquercost
=
3514 secfile_lookup_int_default(file
, GAME_DEFAULT_CONQUERCOST
, "game.conquercost");
3516 game
.info
.foodbox
= secfile_lookup_int_default(file
, 0, "game.box_food");
3517 if (game
.info
.foodbox
== 0) {
3518 /* foodbox was used for 2.0 and earlier servers. */
3519 game
.info
.foodbox
= 10 * secfile_lookup_int_default(file
, 100, "game.foodbox");
3521 game
.server
.techpenalty
=
3522 secfile_lookup_int_default(file
, GAME_DEFAULT_TECHPENALTY
, "game.techpenalty");
3523 game
.server
.razechance
=
3524 secfile_lookup_int_default(file
, GAME_DEFAULT_RAZECHANCE
, "game.razechance");
3526 game
.server
.save_nturns
=
3527 secfile_lookup_int_default(file
, GAME_DEFAULT_SAVETURNS
, "game.save_nturns");
3529 /* suppress warnings about unused entries in old savegames: */
3530 (void) secfile_entry_lookup(file
, "game.rail_food");
3531 (void) secfile_entry_lookup(file
, "game.rail_prod");
3532 (void) secfile_entry_lookup(file
, "game.rail_trade");
3533 (void) secfile_entry_lookup(file
, "game.farmfood");
3535 /* National borders setting. */
3536 game
.info
.borders
= secfile_lookup_int_default(file
, BORDERS_DISABLED
,
3538 if (BORDERS_EXPAND
< game
.info
.borders
) {
3539 game
.info
.borders
= BORDERS_ENABLED
;
3541 if (secfile_lookup_bool_default(file
, FALSE
, "game.happyborders")) {
3542 game
.info
.happyborders
= HB_NATIONAL
;
3544 game
.info
.happyborders
= HB_DISABLED
;
3548 game
.info
.diplomacy
= secfile_lookup_int_default(file
, GAME_DEFAULT_DIPLOMACY
,
3551 sz_strlcpy(game
.server
.save_name
,
3552 secfile_lookup_str_default(file
, GAME_DEFAULT_SAVE_NAME
,
3554 game
.server
.save_compress_level
3555 = secfile_lookup_int_default(file
, GAME_DEFAULT_COMPRESS_LEVEL
,
3556 "game.save_compress_level");
3557 game
.server
.save_compress_type
=
3558 int2fz_method(secfile_lookup_int_default(file
, -1,
3559 "game.save_compress_type"));
3561 game
.info
.aifill
= secfile_lookup_int_default(file
, 0, "game.aifill");
3563 game
.server
.scorelog
= secfile_lookup_bool_default(file
, FALSE
,
3565 game
.server
.scoreturn
=
3566 secfile_lookup_int_default(file
,
3567 game
.info
.turn
+ GAME_DEFAULT_SCORETURN
,
3569 sz_strlcpy(server
.game_identifier
,
3570 secfile_lookup_str_default(file
, "", "game.id"));
3571 /* We are not checking game_identifier legality just yet.
3572 * That's done when we are sure that rand seed has been initialized,
3573 * so that we can generate new game_identifier, if needed. */
3575 game
.info
.fogofwar
= secfile_lookup_bool_default(file
, FALSE
, "game.fogofwar");
3576 game
.server
.fogofwar_old
= game
.info
.fogofwar
;
3578 game
.server
.foggedborders
3579 = secfile_lookup_bool_default(file
, GAME_DEFAULT_FOGGEDBORDERS
,
3580 "game.foggedborders");
3582 game
.info
.global_warming
3583 = secfile_lookup_bool_default(file
, GAME_DEFAULT_GLOBAL_WARMING
,
3584 "game.global_warming");
3585 game
.info
.nuclear_winter
3586 = secfile_lookup_bool_default(file
, GAME_DEFAULT_NUCLEAR_WINTER
,
3587 "game.nuclear_winter");
3589 game
.server
.civilwarsize
=
3590 secfile_lookup_int_default(file
, GAME_DEFAULT_CIVILWARSIZE
,
3591 "game.civilwarsize");
3592 game
.info
.restrictinfra
=
3593 secfile_lookup_bool_default(file
, GAME_DEFAULT_RESTRICTINFRA
,
3594 "game.restrictinfra");
3595 game
.server
.contactturns
=
3596 secfile_lookup_int_default(file
, GAME_DEFAULT_CONTACTTURNS
,
3597 "game.contactturns");
3599 if(has_capability("diplchance_percent", savefile_options
)) {
3600 game
.server
.diplchance
= secfile_lookup_int_default(file
, game
.server
.diplchance
,
3603 game
.server
.diplchance
= secfile_lookup_int_default(file
, 3, /* old default */
3605 if (game
.server
.diplchance
< 2) {
3606 game
.server
.diplchance
= GAME_MAX_DIPLCHANCE
;
3607 } else if (game
.server
.diplchance
> 10) {
3608 game
.server
.diplchance
= GAME_MIN_DIPLCHANCE
;
3610 game
.server
.diplchance
= 100 - (10 * (game
.server
.diplchance
- 1));
3614 game
.server
.aqueductloss
=
3615 secfile_lookup_int_default(file
, game
.server
.aqueductloss
,
3616 "game.aqueductloss");
3618 /* Lowest bit of old killcitizen value indicates if
3619 * land units should kill citizens. We take that as
3620 * new boolean killcitizen value. */
3621 game
.info
.killcitizen
=
3622 (secfile_lookup_int_default(file
, 1, "game.killcitizen")
3624 game
.server
.killunhomed
=
3625 secfile_lookup_int_default(file
, game
.server
.killunhomed
,
3626 "game.killunhomed");
3627 game
.server
.savepalace
=
3628 secfile_lookup_bool_default(file
, game
.server
.savepalace
,
3630 game
.server
.homecaughtunits
=
3631 secfile_lookup_bool_default(file
, game
.server
.homecaughtunits
,
3632 "game.homecaughtunits");
3633 game
.server
.turnblock
=
3634 secfile_lookup_bool_default(file
, game
.server
.turnblock
,
3636 game
.server
.fixedlength
=
3637 secfile_lookup_bool_default(file
, game
.server
.fixedlength
,
3638 "game.fixedlength");
3639 game
.server
.barbarianrate
=
3640 secfile_lookup_int_default(file
, game
.server
.barbarianrate
,
3642 game
.server
.onsetbarbarian
=
3643 secfile_lookup_int_default(file
, game
.server
.onsetbarbarian
,
3645 /* old savegames use the year while newer savegames use the turn. */
3646 game
.server
.onsetbarbarian
= MAX(0, game
.server
.onsetbarbarian
);
3648 game
.server
.revolution_length
=
3649 secfile_lookup_int_default(file
, game
.server
.revolution_length
,
3651 if (game
.server
.revolution_length
== 0) {
3652 /* Value 0 meant RANDOM 1-5 */
3653 game
.server
.revolution_length
= 5;
3654 game
.info
.revolentype
= REVOLEN_RANDOM
;
3656 game
.info
.revolentype
= REVOLEN_FIXED
;
3659 game
.server
.occupychance
=
3660 secfile_lookup_int_default(file
, game
.server
.occupychance
,
3661 "game.occupychance");
3662 game
.server
.autoattack
=
3663 secfile_lookup_bool_default(file
, GAME_DEFAULT_AUTOATTACK
,
3665 game
.server
.seed
= game
.server
.seed_setting
=
3666 secfile_lookup_int_default(file
, game
.server
.seed_setting
,
3668 game
.server
.allowed_city_names
=
3669 secfile_lookup_int_default(file
, game
.server
.allowed_city_names
,
3670 "game.allowed_city_names");
3671 game
.server
.migration
=
3672 secfile_lookup_bool_default(file
, game
.server
.migration
,
3674 game
.server
.mgr_turninterval
=
3675 secfile_lookup_int_default(file
, game
.server
.mgr_turninterval
,
3676 "game.mgr_turninterval");
3677 game
.server
.mgr_foodneeded
=
3678 secfile_lookup_bool_default(file
, game
.server
.mgr_foodneeded
,
3679 "game.mgr_foodneeded");
3680 game
.server
.mgr_distance
=
3681 secfile_lookup_int_default(file
, game
.server
.mgr_distance
,
3682 "game.mgr_distance");
3683 game
.server
.mgr_nationchance
=
3684 secfile_lookup_int_default(file
, game
.server
.mgr_nationchance
,
3685 "game.mgr_nationchance");
3686 game
.server
.mgr_worldchance
=
3687 secfile_lookup_int_default(file
, game
.server
.mgr_worldchance
,
3688 "game.mgr_worldchance");
3690 game
.server
.techlost_donor
=
3691 secfile_lookup_int_default(file
, game
.server
.techlost_donor
,
3692 "game.techlost_donor");
3693 game
.server
.techlost_recv
=
3694 secfile_lookup_int_default(file
, game
.server
.techlost_recv
,
3695 "game.techlost_recv");
3696 game
.info
.team_pooled_research
=
3697 secfile_lookup_int_default(file
, game
.info
.team_pooled_research
,
3698 "game.team_pooled_research");
3700 sz_strlcpy(game
.server
.demography
,
3701 secfile_lookup_str_default(file
, GAME_DEFAULT_DEMOGRAPHY
,
3702 "game.demography"));
3703 sz_strlcpy(game
.server
.allow_take
,
3704 secfile_lookup_str_default(file
, GAME_DEFAULT_ALLOW_TAKE
,
3705 "game.allow_take"));
3707 spacerace
= secfile_lookup_bool_default(file
, victory_enabled(VC_SPACERACE
),
3710 game
.info
.victory_conditions
|= (1 << VC_SPACERACE
);
3712 game
.info
.victory_conditions
&= ~(1 << VC_SPACERACE
);
3715 game
.server
.endspaceship
=
3716 secfile_lookup_bool_default(file
, game
.server
.endspaceship
,
3717 "game.endspaceship");
3719 game
.server
.auto_ai_toggle
=
3720 secfile_lookup_bool_default(file
, game
.server
.auto_ai_toggle
,
3721 "game.auto_ai_toggle");
3723 game
.server
.event_cache
.turns
=
3724 secfile_lookup_int_default(file
, game
.server
.event_cache
.turns
,
3725 "game.event_cache.turns");
3726 game
.server
.event_cache
.max_size
=
3727 secfile_lookup_int_default(file
, game
.server
.event_cache
.max_size
,
3728 "game.event_cache.max_size");
3729 game
.server
.event_cache
.chat
=
3730 secfile_lookup_bool_default(file
, game
.server
.event_cache
.chat
,
3731 "game.event_cache.chat");
3732 game
.server
.event_cache
.info
=
3733 secfile_lookup_bool_default(file
, game
.server
.event_cache
.info
,
3734 "game.event_cache.info");
3737 if (has_capability("bases", savefile_options
)) {
3738 const char **modname
= NULL
;
3741 num_base_types
= secfile_lookup_int_default(file
, 0,
3742 "savefile.num_bases");
3744 if (num_base_types
> 0) {
3745 modname
= secfile_lookup_str_vec(file
, &num_base_types
,
3749 /* make sure that the size of the array is divisible by 4 */
3750 base_order
= fc_calloc(4 * ((num_base_types
+ 3) / 4),
3751 sizeof(*base_order
));
3752 for (j
= 0; j
< num_base_types
; j
++) {
3753 struct extra_type
*pextra
= extra_type_by_rule_name(modname
[j
]);
3755 if (pextra
!= NULL
) {
3756 base_order
[j
] = extra_base_get(pextra
);
3758 base_order
[j
] = NULL
;
3769 if (!has_capability("startunits", savefile_options
)) {
3770 int settlers
= secfile_lookup_int_default(file
, 0, "game.settlers");
3771 int explorer
= secfile_lookup_int_default(file
, 0, "game.explorer");
3774 for (su
= 0; settlers
> 0 && su
< (MAX_LEN_STARTUNIT
- 1) ; su
++, settlers
--) {
3775 game
.server
.start_units
[su
] = 'c';
3777 for (; explorer
> 0 && su
< (MAX_LEN_STARTUNIT
- 1) ; su
++, explorer
--) {
3778 game
.server
.start_units
[su
] = 'x';
3780 game
.server
.start_units
[su
] = '\0';
3782 sz_strlcpy(game
.server
.start_units
,
3783 secfile_lookup_str_default(file
,
3784 GAME_DEFAULT_START_UNITS
,
3785 "game.start_units"));
3787 game
.server
.dispersion
=
3788 secfile_lookup_int_default(file
, GAME_DEFAULT_DISPERSION
,
3792 game
.map
.topology_id
= secfile_lookup_int_default(file
, MAP_ORIGINAL_TOPO
,
3794 game
.map
.server
.mapsize
3795 = secfile_lookup_int_default(file
, MAP_DEFAULT_MAPSIZE
,
3797 game
.map
.server
.size
= secfile_lookup_int_default(file
, MAP_DEFAULT_SIZE
,
3799 game
.map
.server
.tilesperplayer
3800 = secfile_lookup_int_default(file
, MAP_DEFAULT_TILESPERPLAYER
,
3801 "map.tilesperplayer");
3802 game
.map
.server
.startpos
= secfile_lookup_int_default(file
,
3803 MAP_DEFAULT_STARTPOS
,
3805 if (!secfile_lookup_int(file
, &game
.map
.server
.riches
, "map.riches")
3806 || !secfile_lookup_int(file
, &game
.map
.server
.huts
, "map.huts")
3807 || !secfile_lookup_int(file
, &ei
, "map.generator")
3808 || !secfile_lookup_int(file
, &game
.map
.server
.seed_setting
,
3810 || !secfile_lookup_int(file
, &game
.map
.server
.landpercent
,
3811 "map.landpercent")) {
3812 log_error("%s", secfile_error());
3815 game
.map
.server
.generator
= ei
;
3816 game
.map
.server
.seed
= game
.map
.server
.seed_setting
;
3818 game
.map
.server
.wetness
=
3819 secfile_lookup_int_default(file
, MAP_DEFAULT_WETNESS
, "map.wetness");
3820 game
.map
.server
.steepness
=
3821 secfile_lookup_int_default(file
, MAP_DEFAULT_STEEPNESS
,
3823 game
.map
.server
.have_huts
= secfile_lookup_bool_default(file
, TRUE
,
3825 game
.map
.server
.temperature
=
3826 secfile_lookup_int_default(file
, MAP_DEFAULT_TEMPERATURE
,
3828 game
.map
.server
.alltemperate
3829 = secfile_lookup_bool_default(file
, MAP_DEFAULT_ALLTEMPERATE
,
3830 "map.alltemperate");
3831 game
.map
.server
.tinyisles
3832 = secfile_lookup_bool_default(file
, MAP_DEFAULT_TINYISLES
,
3834 game
.map
.server
.separatepoles
3835 = secfile_lookup_bool_default(file
, MAP_DEFAULT_SEPARATE_POLES
,
3836 "map.separatepoles");
3838 if (has_capability("startoptions", savefile_options
)) {
3839 if (!secfile_lookup_int(file
, &game
.map
.xsize
, "map.width")
3840 || !secfile_lookup_int(file
, &game
.map
.ysize
, "map.height")) {
3841 log_error("%s", secfile_error());
3845 /* old versions saved with these names in S_S_INITIAL: */
3846 if (!secfile_lookup_int(file
, &game
.map
.xsize
, "map.xsize")
3847 || !secfile_lookup_int(file
, &game
.map
.ysize
, "map.ysize")) {
3848 log_error("%s", secfile_error());
3853 if (S_S_INITIAL
== tmp_server_state
3854 && MAPGEN_SCENARIO
== game
.map
.server
.generator
) {
3855 /* generator 0 = map done with a map editor aka a "scenario" */
3856 if (has_capability("specials",savefile_options
)) {
3857 map_load(file
, tmp_server_state
, savefile_options
,
3858 special_order
, num_special_types
,
3859 base_order
, num_base_types
);
3862 map_load_tiles(file
);
3863 if (has_capability("riversoverlay",savefile_options
)) {
3864 map_load_rivers_overlay(file
, special_order
, num_special_types
);
3866 if (has_capability("startpos", savefile_options
)) {
3867 map_load_startpos(file
, tmp_server_state
);
3873 if (S_S_INITIAL
== tmp_server_state
) {
3879 1) if the block exists at all.
3880 2) if it is saved. */
3881 if (NULL
!= secfile_entry_lookup(file
, "random.index_J")
3882 && secfile_lookup_bool_default(file
, TRUE
, "game.save_random")) {
3883 if (!secfile_lookup_int(file
, &rstate
.j
, "random.index_J")
3884 || !secfile_lookup_int(file
, &rstate
.k
, "random.index_K")
3885 || !secfile_lookup_int(file
, &rstate
.x
, "random.index_X")) {
3886 log_error("%s", secfile_error());
3890 /* Since random state was previously saved, save it also when resaving.
3892 * Do NOT touch this in case of regular savegame. They always have random
3893 * saved, but if one starts to make scenario based on a savegame, we want
3894 * default scenario settings in the beginning (default save_random = FALSE).
3896 if (game
.scenario
.is_scenario
) {
3897 game
.scenario
.save_random
= TRUE
;
3900 for (i
= 0; i
< 8; i
++) {
3901 string
= secfile_lookup_str(file
, "random.table%d",i
);
3902 fc_assert(NULL
!= string
);
3903 sscanf(string
, "%8x %8x %8x %8x %8x %8x %8x", &rstate
.v
[7*i
],
3904 &rstate
.v
[7*i
+1], &rstate
.v
[7*i
+2], &rstate
.v
[7*i
+3],
3905 &rstate
.v
[7*i
+4], &rstate
.v
[7*i
+5], &rstate
.v
[7*i
+6]);
3907 rstate
.is_init
= TRUE
;
3908 fc_rand_set_state(rstate
);
3911 (void) secfile_entry_by_path(file
, "game.save_random");
3913 /* We're loading a running game without a seed (which is okay, if it's
3914 * a scenario). We need to generate the game seed now because it will
3915 * be needed later during the load. */
3916 if (S_S_RUNNING
<= tmp_server_state
) {
3918 rstate
= fc_rand_state();
3922 if (0 == strlen(server
.game_identifier
)
3923 || !is_base64url(server
.game_identifier
)) {
3924 /* This uses fc_rand(), so random state has to be initialized
3926 randomize_base64url_string(server
.game_identifier
,
3927 sizeof(server
.game_identifier
));
3930 settings_consider_all_changed();
3932 map_load(file
, tmp_server_state
, savefile_options
,
3933 special_order
, num_special_types
,
3934 base_order
, num_base_types
);
3936 if (!game
.info
.is_new_game
) {
3937 int *worked_tiles
= NULL
; /* temporary map for worked tiles */
3938 int loaded_players
= 0;
3941 /* Update time has been saved as year in legacysave.c */
3942 game
.server
.last_updated_year
= TRUE
;
3944 /* destroyed wonders: */
3945 string
= secfile_lookup_str(file
, "game.destroyed_wonders_new");
3947 log_fatal("No destroyed wonders information");
3950 for (k
= 0; k
< improvement_order_size
&& string
[k
]; k
++) {
3951 if (string
[k
] == '1') {
3952 struct impr_type
*pimprove
=
3953 improvement_by_rule_name(improvement_order
[k
]);
3955 game
.info
.great_wonder_owners
[improvement_index(pimprove
)] =
3962 server
.identity_number
=
3963 secfile_lookup_int_default(file
, server
.identity_number
,
3964 "game.identity_number_used");
3966 /* Initialize nations we loaded from rulesets. This has to be after
3967 * map loading and before we seek nations for players */
3968 update_nations_with_startpos();
3970 /* Load worked tiles map */
3971 worked_tiles
= player_load_cities_worked_map(file
, savefile_options
);
3973 /* Now, load the players from the savefile. */
3974 player_slots_iterate(pslot
) {
3975 struct player
*pplayer
;
3977 if (NULL
== secfile_section_lookup(file
, "player%d",
3978 player_slot_index(pslot
))) {
3983 pplayer
= server_create_player(player_slot_index(pslot
),
3984 default_ai_type_name(), NULL
,
3986 server_player_init(pplayer
, FALSE
, FALSE
);
3988 } player_slots_iterate_end
;
3990 /* check number of players */
3992 int nplayers
= secfile_lookup_int_default(file
, 0, "game.nplayers");
3994 fc_assert_ret(player_count() == nplayers
);
3997 /* Load team informations. All players should now have teams. This is
3998 * not the case with old savegames. Assign first the requested teams,
3999 * then find empty teams for other players. */
4000 players_iterate(pplayer
) {
4002 struct team_slot
*tslot
;
4004 if (secfile_lookup_int(file
, &team
, "player%d.team_no",
4005 player_number(pplayer
))
4006 && (tslot
= team_slot_by_number(team
))) {
4007 team_add_player(pplayer
, team_new(tslot
));
4009 } players_iterate_end
;
4010 players_iterate(pplayer
) {
4011 if (NULL
== pplayer
->team
) {
4012 team_add_player(pplayer
, team_new(NULL
));
4014 } players_iterate_end
;
4016 /* Load map player data. */
4017 players_iterate(pplayer
) {
4018 player_map_init(pplayer
);
4019 } players_iterate_end
;
4020 map_load_known(file
, savefile_options
);
4021 map_load_owner(file
, savefile_options
);
4023 /* Now, load each players data about other players. Thus must be after
4024 * the player slots are activated. */
4025 players_iterate(pplayer
) {
4026 int plrno
= player_number(pplayer
);
4028 player_load_main(pplayer
, plrno
, file
, savefile_options
,
4029 technology_order
, technology_order_size
);
4030 player_load_cities(pplayer
, plrno
, file
, savefile_options
,
4031 improvement_order
, improvement_order_size
,
4033 player_load_units(pplayer
, plrno
, file
, savefile_options
,
4034 special_order
, num_special_types
,
4035 base_order
, num_base_types
);
4036 player_load_attributes(pplayer
, plrno
, file
);
4038 /* print out some informations */
4039 if (pplayer
->ai_controlled
) {
4040 CALL_PLR_AI_FUNC(gained_control
, pplayer
, pplayer
);
4041 log_normal(_("%s has been added as %s level AI-controlled player "
4042 "(%s)."), player_name(pplayer
),
4043 ai_level_translated_name(pplayer
->ai_common
.skill_level
),
4044 ai_name(pplayer
->ai
));
4046 log_normal(_("%s has been added as human player."),
4047 player_name(pplayer
));
4049 } players_iterate_end
;
4051 players_iterate(pplayer
) {
4052 /* Backward compatibility: if we had any open-ended orders (pillage)
4053 * in the savegame, assign specific targets now */
4054 unit_list_iterate(pplayer
->units
, punit
) {
4055 unit_assign_specific_activity_target(punit
,
4057 &punit
->activity_target
);
4058 } unit_list_iterate_end
;
4059 /* Load transporter status. */
4060 player_load_units_transporter(pplayer
, file
);
4061 /* Update cached city illness. This can depend on trade routes,
4062 * so can't be calculated until all players have been loaded. */
4063 if (game
.info
.illness_on
) {
4064 city_list_iterate(pplayer
->cities
, pcity
) {
4065 pcity
->server
.illness
4066 = city_illness_calc(pcity
, NULL
, NULL
,
4067 &(pcity
->illness_trade
), NULL
);
4068 } city_list_iterate_end
;
4071 } players_iterate_end
;
4073 /* Free worked tiles map */
4074 if (worked_tiles
!= NULL
) {
4076 /* check the entire map for unused worked tiles */
4077 whole_map_iterate(ptile
) {
4078 if (worked_tiles
[ptile
->index
] != -1) {
4079 log_error("[city id: %d] Unused worked tile at (%d, %d).",
4080 worked_tiles
[ptile
->index
], TILE_XY(ptile
));
4082 } whole_map_iterate_end
;
4084 FC_FREE(worked_tiles
);
4087 /* In case of tech_leakage, we can update research only after all
4088 * the players have been loaded */
4089 researches_iterate(presearch
) {
4090 /* Mark the reachable techs */
4091 research_update(presearch
);
4093 /* Check researching technology and goal. */
4094 if (presearch
->researching
!= A_UNSET
4095 && !is_future_tech(presearch
->researching
)
4096 && (valid_advance_by_number(presearch
->researching
) == NULL
4097 || (research_invention_state(presearch
, presearch
->researching
)
4098 != TECH_PREREQS_KNOWN
))) {
4099 log_error(_("%s had invalid researching technology."),
4100 research_name_translation(presearch
));
4101 presearch
->researching
= A_UNSET
;
4103 if (presearch
->tech_goal
!= A_UNSET
4104 && !is_future_tech(presearch
->tech_goal
)
4105 && (valid_advance_by_number(presearch
->researching
) == NULL
4106 || !research_invention_reachable(presearch
,
4107 presearch
->tech_goal
)
4108 || (research_invention_state(presearch
, presearch
->tech_goal
)
4110 log_error(_("%s had invalid technology goal."),
4111 research_name_translation(presearch
));
4112 presearch
->tech_goal
= A_UNSET
;
4114 } researches_iterate_end
;
4116 /* The savegame may contain valid nations that are not included in
4117 * the current default nationset for the ruleset, since it predates
4118 * nationsets. Try to reconcile these. */
4119 fit_nationset_to_players();
4121 /* Some players may have invalid nations in the ruleset. Once all
4122 * players are loaded, pick one of the remaining nations for them. */
4123 players_iterate(pplayer
) {
4124 if (pplayer
->nation
== NO_NATION_SELECTED
) {
4125 player_set_nation(pplayer
, pick_a_nation(NULL
, FALSE
, TRUE
,
4127 /* TRANS: Minor error message: <Leader> ... <Poles>. */
4128 log_error(_("%s had invalid nation; changing to %s."),
4129 player_name(pplayer
), nation_plural_for_player(pplayer
));
4132 ai_traits_init(pplayer
);
4133 } players_iterate_end
;
4135 /* Sanity check alliances, prevent allied-with-ally-of-enemy */
4136 players_iterate_alive(plr
) {
4137 players_iterate_alive(aplayer
) {
4138 if (pplayers_allied(plr
, aplayer
)) {
4139 enum dipl_reason can_ally
= pplayer_can_make_treaty(plr
, aplayer
,
4141 if (can_ally
== DIPL_ALLIANCE_PROBLEM_US
4142 || can_ally
== DIPL_ALLIANCE_PROBLEM_THEM
) {
4143 log_error("Illegal alliance structure detected: "
4144 "%s alliance to %s reduced to peace treaty.",
4145 nation_rule_name(nation_of_player(plr
)),
4146 nation_rule_name(nation_of_player(aplayer
)));
4147 player_diplstate_get(plr
, aplayer
)->type
= DS_PEACE
;
4148 player_diplstate_get(aplayer
, plr
)->type
= DS_PEACE
;
4151 } players_iterate_alive_end
;
4152 } players_iterate_alive_end
;
4154 /* Update all city information. This must come after all cities are
4155 * loaded (in player_load) but before player (dumb) cities are loaded
4156 * in player_load_vision(). */
4157 players_iterate(plr
) {
4158 city_list_iterate(plr
->cities
, pcity
) {
4159 city_refresh(pcity
);
4160 CALL_PLR_AI_FUNC(city_got
, plr
, plr
, pcity
);
4161 } city_list_iterate_end
;
4162 } players_iterate_end
;
4164 /* Since the cities must be placed on the map to put them on the
4165 player map we do this afterwards */
4166 players_iterate(pplayer
) {
4167 int n
= player_index(pplayer
);
4168 player_load_vision(pplayer
, n
, file
, savefile_options
,
4169 special_order
, num_special_types
,
4170 improvement_order
, improvement_order_size
,
4171 base_order
, num_base_types
);
4172 } players_iterate_end
;
4174 /* We do this here since if the did it in player_load, player 1
4175 would try to unfog (unloaded) player 2's map when player 1's units
4177 players_iterate(pplayer
) {
4178 BV_CLR_ALL(pplayer
->gives_shared_vision
);
4179 BV_CLR_ALL(pplayer
->server
.really_gives_vision
);
4180 } players_iterate_end
;
4182 players_iterate(pplayer
) {
4183 int n
= player_index(pplayer
);
4185 if (has_capability("vision", savefile_options
)) {
4186 players_iterate(pplayer2
) {
4187 if (secfile_lookup_bool_default(file
, FALSE
,
4188 "player%d.diplstate%d.gives_shared_vision",
4189 n
, player_index(pplayer2
))) {
4190 give_shared_vision(pplayer
, pplayer2
);
4192 } players_iterate_end
;
4194 const char *vision
= secfile_lookup_str(file
,
4195 "player%d.gives_shared_vision", n
);
4198 players_iterate(pplayer2
) {
4199 if (vision
[player_index(pplayer2
)] == '1') {
4200 give_shared_vision(pplayer
, pplayer2
);
4202 } players_iterate_end
;
4205 } players_iterate_end
;
4207 initialize_globals();
4208 apply_unit_ordering();
4210 /* all vision is ready */
4211 map_calculate_borders(); /* does city_thaw_workers_queue() */
4212 /* city_refresh() below */
4214 /* Make sure everything is consistent. */
4216 /* Old savegames may have maxplayers lower than current player count,
4218 players
= normal_player_count();
4219 if (game
.server
.max_players
< players
) {
4220 log_verbose("Max players lower than current players, fixing");
4221 game
.server
.max_players
= players
;
4224 players_iterate(pplayer
) {
4225 unit_list_iterate(pplayer
->units
, punit
) {
4226 if (!can_unit_continue_current_activity(punit
)) {
4227 log_error("Unit doing illegal activity in savegame!");
4228 punit
->activity
= ACTIVITY_IDLE
;
4230 } unit_list_iterate_end
;
4231 } players_iterate_end
;
4233 cities_iterate(pcity
) {
4234 city_refresh(pcity
); /* again */
4235 city_thaw_workers(pcity
); /* may auto_arrange_workers() */
4236 } cities_iterate_end
;
4239 if (secfile_lookup_int_default(file
, -1,
4240 "game.shuffled_player_%d", 0) >= 0) {
4241 int shuffled_players
[player_slot_count()];
4243 /* players_iterate() not used here */
4244 for (i
= 0; i
< player_slot_count(); i
++) {
4245 shuffled_players
[i
] = secfile_lookup_int_default(file
,
4246 i
, "game.shuffled_player_%d", i
);
4248 set_shuffled_players(shuffled_players
);
4250 /* No shuffled players included, so shuffle them (this may include
4255 /* Fix ferrying sanity */
4256 players_iterate(pplayer
) {
4257 unit_list_iterate_safe(pplayer
->units
, punit
) {
4258 if (!unit_transport_get(punit
)
4259 && !can_unit_exist_at_tile(punit
, unit_tile(punit
))) {
4260 log_error("Removing %s unferried %s in %s at (%d, %d)",
4261 nation_rule_name(nation_of_player(pplayer
)),
4262 unit_rule_name(punit
),
4263 terrain_rule_name(unit_tile(punit
)->terrain
),
4264 TILE_XY(unit_tile(punit
)));
4265 bounce_unit(punit
, TRUE
);
4267 } unit_list_iterate_safe_end
;
4268 } players_iterate_end
;
4270 /* Fix stacking issues. We don't rely on the savegame preserving
4271 * alliance invariants (old savegames often did not) so if there are any
4272 * unallied units on the same tile we just bounce them. */
4273 players_iterate(pplayer
) {
4274 players_iterate(aplayer
) {
4275 resolve_unit_stacks(pplayer
, aplayer
, TRUE
);
4276 } players_iterate_end
;
4277 } players_iterate_end
;
4279 players_iterate(pplayer
) {
4280 calc_civ_score(pplayer
);
4281 } players_iterate_end
;
4283 /* Recalculate the potential buildings for each city.
4284 * Has caused some problems with game random state. */
4285 players_iterate(pplayer
) {
4286 bool saved_ai_control
= pplayer
->ai_controlled
;
4288 /* Recalculate for all players. */
4289 pplayer
->ai_controlled
= FALSE
;
4291 /* Building advisor needs data phase open in order to work */
4292 adv_data_phase_init(pplayer
, FALSE
);
4293 building_advisor(pplayer
);
4294 /* Close data phase again so it can be opened again when game starts. */
4295 adv_data_phase_done(pplayer
);
4297 pplayer
->ai_controlled
= saved_ai_control
;
4298 } players_iterate_end
;
4300 /* Player colors are always needed once game has started. Pre-2.4 savegames
4302 if (game_was_started()) {
4303 assign_player_colors();
4306 /* Restore game random state, just in case various initialization code
4307 * inexplicably altered the previously existing state. */
4308 if (!game
.info
.is_new_game
) {
4309 fc_rand_set_state(rstate
);
4312 /* load event cache */
4313 event_cache_load(file
, "event_cache");
4315 /* Free load data. */
4316 if (NULL
!= base_order
) {
4319 if (NULL
!= special_order
) {
4320 free(special_order
);
4322 if (NULL
!= improvement_order
) {
4323 free(improvement_order
);
4325 if (NULL
!= technology_order
) {
4326 free(technology_order
);