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