Freeciv-3.3
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 "ruleload.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[MAP_NATIVE_WIDTH + 1]; \
165 int _nat_x, _nat_y; \
166 \
167 for (_nat_y = 0; _nat_y < MAP_NATIVE_HEIGHT; _nat_y++) { \
168 for (_nat_x = 0; _nat_x < MAP_NATIVE_WIDTH; _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[MAP_NATIVE_WIDTH] = '\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 < MAP_NATIVE_HEIGHT; _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) != MAP_NATIVE_WIDTH) { \
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'", MAP_NATIVE_WIDTH, strlen(_line), buf); \
227 _printed_warning = TRUE; \
228 continue; \
229 } \
230 for (_nat_x = 0; _nat_x < MAP_NATIVE_WIDTH; _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/************************************************************************/
580
581/************************************************************************/
591
592/* =======================================================================
593 * Helper functions.
594 * ======================================================================= */
595
596/************************************************************************/
599static enum unit_orders char2order(char order)
600{
601 switch (order) {
602 case 'm':
603 case 'M':
604 return ORDER_MOVE;
605 case 'w':
606 case 'W':
607 return ORDER_FULL_MP;
608 case 'b':
609 case 'B':
611 case 'a':
612 case 'A':
613 return ORDER_ACTIVITY;
614 case 'd':
615 case 'D':
616 return ORDER_OLD_DISBAND;
617 case 'u':
618 case 'U':
620 case 't':
621 case 'T':
623 case 'h':
624 case 'H':
625 return ORDER_OLD_HOMECITY;
626 case 'x':
627 case 'X':
628 return ORDER_ACTION_MOVE;
629 }
630
631 /* This can happen if the savegame is invalid. */
632 return ORDER_LAST;
633}
634
635/************************************************************************/
638static enum direction8 char2dir(char dir)
639{
640 /* Numberpad values for the directions. */
641 switch (dir) {
642 case '1':
643 return DIR8_SOUTHWEST;
644 case '2':
645 return DIR8_SOUTH;
646 case '3':
647 return DIR8_SOUTHEAST;
648 case '4':
649 return DIR8_WEST;
650 case '6':
651 return DIR8_EAST;
652 case '7':
653 return DIR8_NORTHWEST;
654 case '8':
655 return DIR8_NORTH;
656 case '9':
657 return DIR8_NORTHEAST;
658 }
659
660 /* This can happen if the savegame is invalid. */
661 return direction8_invalid();
662}
663
664/************************************************************************/
667static char activity2char(int activity)
668{
669 switch (activity) {
670 case ACTIVITY_IDLE:
671 return 'w';
672 case ACTIVITY_CLEAN:
673 return 'C';
675 return 'p';
677 return 'r';
678 case ACTIVITY_MINE:
679 return 'm';
681 return 'i';
683 return 'f';
684 case ACTIVITY_SENTRY:
685 return 's';
687 return 'l';
688 case ACTIVITY_PILLAGE:
689 return 'e';
690 case ACTIVITY_GOTO:
691 return 'g';
692 case ACTIVITY_EXPLORE:
693 return 'x';
695 return 'o';
697 return 'y';
699 return 'u';
700 case ACTIVITY_BASE:
701 return 'b';
703 return 'R';
704 case ACTIVITY_CONVERT:
705 return 'c';
707 case ACTIVITY_PLANT:
708 return '?';
709 case ACTIVITY_LAST:
710 break;
711 }
712
714
715 return '?';
716}
717
718/************************************************************************/
721static int char2activity(char activity)
722{
723 int a;
724
725 for (a = 0; a < ACTIVITY_LAST_SAVEGAME2; a++) {
726 char achar = activity2char(a);
727
728 if (activity == achar) {
729 return a;
730 }
731 }
732
733 /* This can happen if the savegame is invalid. */
734 return ACTIVITY_LAST;
735}
736
737/************************************************************************/
742static int unquote_block(const char *const quoted_, void *dest,
743 int dest_length)
744{
745 int i, length, parsed, tmp;
746 char *endptr;
747 const char *quoted = quoted_;
748
749 parsed = sscanf(quoted, "%d", &length);
750
751 if (parsed != 1) {
752 log_error(_("Syntax error in attribute block."));
753 return 0;
754 }
755
756 if (length > dest_length) {
757 return 0;
758 }
759
760 quoted = strchr(quoted, ':');
761
762 if (quoted == NULL) {
763 log_error(_("Syntax error in attribute block."));
764 return 0;
765 }
766
767 quoted++;
768
769 for (i = 0; i < length; i++) {
770 tmp = strtol(quoted, &endptr, 16);
771
772 if ((endptr - quoted) != 2
773 || *endptr != ' '
774 || (tmp & 0xff) != tmp) {
775 log_error(_("Syntax error in attribute block."));
776 return 0;
777 }
778
779 ((unsigned char *) dest)[i] = tmp;
780 quoted += 3;
781 }
782
783 return length;
784}
785
786/************************************************************************/
791 struct worklist *pwl, const char *path, ...)
792{
793 int i;
794 const char *kind;
795 const char *name;
796 char path_str[1024];
797 va_list ap;
798
799 /* The first part of the registry path is taken from the varargs to the
800 * function. */
801 va_start(ap, path);
802 fc_vsnprintf(path_str, sizeof(path_str), path, ap);
803 va_end(ap);
804
807 "%s.wl_length", path_str);
808
809 for (i = 0; i < pwl->length; i++) {
810 kind = secfile_lookup_str(file, "%s.wl_kind%d", path_str, i);
811
812 /* We lookup the production value by name. An invalid entry isn't a
813 * fatal error; we just truncate the worklist. */
814 name = secfile_lookup_str_default(file, "-", "%s.wl_value%d",
815 path_str, i);
816 pwl->entries[i] = universal_by_rule_name(kind, name);
817 if (pwl->entries[i].kind == universals_n_invalid()) {
818 log_sg("%s.wl_value%d: unknown \"%s\" \"%s\".", path_str, i, kind,
819 name);
820 pwl->length = i;
821 break;
822 }
823 }
824
825 /* Padding entries */
826 for (; i < wlist_max_length; i++) {
827 (void) secfile_entry_lookup(file, "%s.wl_kind%d", path_str, i);
828 (void) secfile_entry_lookup(file, "%s.wl_value%d", path_str, i);
829 }
830}
831
832/************************************************************************/
836static void unit_ordering_apply(void)
837{
838 players_iterate(pplayer) {
839 city_list_iterate(pplayer->cities, pcity) {
840 unit_list_sort_ord_city(pcity->units_supported);
841 }
844
845 whole_map_iterate(&(wld.map), ptile) {
846 unit_list_sort_ord_map(ptile->units);
848}
849
850/************************************************************************/
857static void sg_extras_set_dbv(struct dbv *extras, char ch,
858 struct extra_type **idx)
859{
860 int i, bin;
861 const char *pch = strchr(hex_chars, ch);
862
863 if (!pch || ch == '\0') {
864 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
865 bin = 0;
866 } else {
867 bin = pch - hex_chars;
868 }
869
870 for (i = 0; i < 4; i++) {
871 struct extra_type *pextra = idx[i];
872
873 if (pextra == NULL) {
874 continue;
875 }
876 if ((bin & (1 << i))
877 && (wld.map.server.have_huts || !is_extra_caused_by(pextra, EC_HUT))) {
878 dbv_set(extras, extra_index(pextra));
879 }
880 }
881}
882
883/************************************************************************/
891 struct extra_type **idx)
892{
893 int i, bin;
894 const char *pch = strchr(hex_chars, ch);
895
896 if (!pch || ch == '\0') {
897 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
898 bin = 0;
899 } else {
900 bin = pch - hex_chars;
901 }
902
903 for (i = 0; i < 4; i++) {
904 struct extra_type *pextra = idx[i];
905
906 if (pextra == NULL) {
907 continue;
908 }
909 if ((bin & (1 << i))
910 && (wld.map.server.have_huts || !is_extra_caused_by(pextra, EC_HUT))) {
911 BV_SET(*extras, extra_index(pextra));
912 }
913 }
914}
915
916/************************************************************************/
923static void sg_special_set_dbv(struct tile *ptile, struct dbv *extras, char ch,
924 const enum tile_special_type *idx,
925 bool rivers_overlay)
926{
927 int i, bin;
928 const char *pch = strchr(hex_chars, ch);
929
930 if (!pch || ch == '\0') {
931 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
932 bin = 0;
933 } else {
934 bin = pch - hex_chars;
935 }
936
937 for (i = 0; i < 4; i++) {
938 enum tile_special_type sp = idx[i];
939
940 if (sp == S_LAST) {
941 continue;
942 }
943 if (rivers_overlay && sp != S_OLD_RIVER) {
944 continue;
945 }
946
947 if (sp == S_HUT && !wld.map.server.have_huts) {
948 /* It would be logical to have this in the saving side -
949 * really not saving the huts in the first place, BUT
950 * 1) They have been saved by older versions, so we
951 * have to deal with such savegames.
952 * 2) This makes scenario author less likely to lose
953 * one's work completely after carefully placing huts
954 * and then saving with 'have_huts' disabled. */
955 continue;
956 }
957
958 if (bin & (1 << i)) {
959 if (sp == S_OLD_ROAD) {
960 struct road_type *proad;
961
963 if (proad) {
965 }
966 } else if (sp == S_OLD_RAILROAD) {
967 struct road_type *proad;
968
970 if (proad) {
972 }
973 } else if (sp == S_OLD_RIVER) {
974 struct road_type *proad;
975
977 if (proad) {
979 }
980 } else {
981 struct extra_type *pextra = NULL;
982 enum extra_cause cause = EC_COUNT;
983
984 /* Converting from old hardcoded specials to as sensible extra as we can */
985 switch (sp) {
986 case S_IRRIGATION:
987 case S_FARMLAND:
988 /* If old savegame has both irrigation and farmland, EC_IRRIGATION
989 * gets applied twice, which hopefully has the correct result. */
990 cause = EC_IRRIGATION;
991 break;
992 case S_MINE:
993 cause = EC_MINE;
994 break;
995 case S_POLLUTION:
996 cause = EC_POLLUTION;
997 break;
998 case S_HUT:
999 cause = EC_HUT;
1000 break;
1001 case S_FALLOUT:
1002 cause = EC_FALLOUT;
1003 break;
1004 default:
1006 break;
1007 }
1008
1009 if (cause != EC_COUNT) {
1010 struct tile *vtile = tile_virtual_new(ptile);
1011 struct terrain *pterr = tile_terrain(vtile);
1012 const struct req_context tile_ctxt = { .tile = vtile };
1013
1014 /* Do not let the extras already set to the real tile mess with setup
1015 * of the player tiles if that's what we're doing. */
1016 dbv_to_bv(vtile->extras.vec, extras);
1017
1018 /* It's ok not to know which player or which unit originally built the extra -
1019 * in the rules used when specials were saved these could not have made any
1020 * difference. */
1021 /* Can't use next_extra_for_tile() as it works for buildable extras only. */
1022
1023 if ((cause != EC_IRRIGATION || pterr->irrigation_time != 0)
1024 && (cause != EC_MINE || pterr->mining_time != 0)
1025 && (cause != EC_BASE || pterr->base_time != 0)
1026 && (cause != EC_ROAD || pterr->road_time != 0)) {
1030 || tile_city(vtile) != NULL
1031 || extra_base_get(candidate)->border_sq <= 0)
1033 &(const struct req_context) {
1034 .player = tile_owner(vtile),
1035 },
1036 &candidate->reqs,
1037 RPT_POSSIBLE)) {
1038 pextra = candidate;
1039 break;
1040 }
1041 }
1043 }
1044
1046 }
1047
1048 if (pextra) {
1049 dbv_set(extras, extra_index(pextra));
1050 }
1051 }
1052 }
1053 }
1054}
1055
1056/************************************************************************/
1063static void sg_special_set_bv(struct tile *ptile, bv_extras *extras, char ch,
1064 const enum tile_special_type *idx,
1065 bool rivers_overlay)
1066{
1067 int i, bin;
1068 const char *pch = strchr(hex_chars, ch);
1069
1070 if (!pch || ch == '\0') {
1071 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1072 bin = 0;
1073 } else {
1074 bin = pch - hex_chars;
1075 }
1076
1077 for (i = 0; i < 4; i++) {
1078 enum tile_special_type sp = idx[i];
1079
1080 if (sp == S_LAST) {
1081 continue;
1082 }
1083 if (rivers_overlay && sp != S_OLD_RIVER) {
1084 continue;
1085 }
1086
1087 if (sp == S_HUT && !wld.map.server.have_huts) {
1088 /* It would be logical to have this in the saving side -
1089 * really not saving the huts in the first place, BUT
1090 * 1) They have been saved by older versions, so we
1091 * have to deal with such savegames.
1092 * 2) This makes scenario author less likely to lose
1093 * one's work completely after carefully placing huts
1094 * and then saving with 'have_huts' disabled. */
1095 continue;
1096 }
1097
1098 if (bin & (1 << i)) {
1099 if (sp == S_OLD_ROAD) {
1100 struct road_type *proad;
1101
1103 if (proad) {
1105 }
1106 } else if (sp == S_OLD_RAILROAD) {
1107 struct road_type *proad;
1108
1110 if (proad) {
1112 }
1113 } else if (sp == S_OLD_RIVER) {
1114 struct road_type *proad;
1115
1117 if (proad) {
1119 }
1120 } else {
1121 struct extra_type *pextra = NULL;
1122 enum extra_cause cause = EC_COUNT;
1123
1124 /* Converting from old hardcoded specials to as sensible extra as we can */
1125 switch (sp) {
1126 case S_IRRIGATION:
1127 case S_FARMLAND:
1128 /* If old savegame has both irrigation and farmland, EC_IRRIGATION
1129 * gets applied twice, which hopefully has the correct result. */
1130 cause = EC_IRRIGATION;
1131 break;
1132 case S_MINE:
1133 cause = EC_MINE;
1134 break;
1135 case S_POLLUTION:
1136 cause = EC_POLLUTION;
1137 break;
1138 case S_HUT:
1139 cause = EC_HUT;
1140 break;
1141 case S_FALLOUT:
1142 cause = EC_FALLOUT;
1143 break;
1144 default:
1146 break;
1147 }
1148
1149 if (cause != EC_COUNT) {
1150 struct tile *vtile = tile_virtual_new(ptile);
1151 struct terrain *pterr = tile_terrain(vtile);
1152 const struct req_context tile_ctxt = { .tile = vtile };
1153
1154 /* Do not let the extras already set to the real tile mess with setup
1155 * of the player tiles if that's what we're doing. */
1156 vtile->extras = *extras;
1157
1158 /* It's ok not to know which player or which unit originally built the extra -
1159 * in the rules used when specials were saved these could not have made any
1160 * difference. */
1161 /* Can't use next_extra_for_tile() as it works for buildable extras only. */
1162
1163 if ((cause != EC_IRRIGATION || pterr->irrigation_time != 0)
1164 && (cause != EC_MINE || pterr->mining_time != 0)
1165 && (cause != EC_BASE || pterr->base_time != 0)
1166 && (cause != EC_ROAD || pterr->road_time != 0)) {
1170 || tile_city(vtile) != NULL
1171 || extra_base_get(candidate)->border_sq <= 0)
1173 &(const struct req_context) {
1174 .player = tile_owner(vtile),
1175 },
1176 &candidate->reqs,
1177 RPT_POSSIBLE)) {
1178 pextra = candidate;
1179 break;
1180 }
1181 }
1183 }
1184
1186 }
1187
1188 if (pextra) {
1189 BV_SET(*extras, extra_index(pextra));
1190 }
1191 }
1192 }
1193 }
1194}
1195
1196/************************************************************************/
1203static void sg_bases_set_dbv(struct dbv *extras, char ch,
1204 struct base_type **idx)
1205{
1206 int i, bin;
1207 const char *pch = strchr(hex_chars, ch);
1208
1209 if (!pch || ch == '\0') {
1210 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1211 bin = 0;
1212 } else {
1213 bin = pch - hex_chars;
1214 }
1215
1216 for (i = 0; i < 4; i++) {
1217 struct base_type *pbase = idx[i];
1218
1219 if (pbase == NULL) {
1220 continue;
1221 }
1222 if (bin & (1 << i)) {
1224 }
1225 }
1226}
1227
1228/************************************************************************/
1235static void sg_bases_set_bv(bv_extras *extras, char ch, struct base_type **idx)
1236{
1237 int i, bin;
1238 const char *pch = strchr(hex_chars, ch);
1239
1240 if (!pch || ch == '\0') {
1241 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1242 bin = 0;
1243 } else {
1244 bin = pch - hex_chars;
1245 }
1246
1247 for (i = 0; i < 4; i++) {
1248 struct base_type *pbase = idx[i];
1249
1250 if (pbase == NULL) {
1251 continue;
1252 }
1253 if (bin & (1 << i)) {
1255 }
1256 }
1257}
1258
1259/************************************************************************/
1266static void sg_roads_set_dbv(struct dbv *extras, char ch, struct road_type **idx)
1267{
1268 int i, bin;
1269 const char *pch = strchr(hex_chars, ch);
1270
1271 if (!pch || ch == '\0') {
1272 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1273 bin = 0;
1274 } else {
1275 bin = pch - hex_chars;
1276 }
1277
1278 for (i = 0; i < 4; i++) {
1279 struct road_type *proad = idx[i];
1280
1281 if (proad == NULL) {
1282 continue;
1283 }
1284 if (bin & (1 << i)) {
1286 }
1287 }
1288}
1289
1290/************************************************************************/
1297static void sg_roads_set_bv(bv_extras *extras, char ch, struct road_type **idx)
1298{
1299 int i, bin;
1300 const char *pch = strchr(hex_chars, ch);
1301
1302 if (!pch || ch == '\0') {
1303 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1304 bin = 0;
1305 } else {
1306 bin = pch - hex_chars;
1307 }
1308
1309 for (i = 0; i < 4; i++) {
1310 struct road_type *proad = idx[i];
1311
1312 if (proad == NULL) {
1313 continue;
1314 }
1315 if (bin & (1 << i)) {
1317 }
1318 }
1319}
1320
1321/************************************************************************/
1324static struct extra_type *char2resource(char c)
1325{
1326 /* speed common values */
1328 || c == RESOURCE_NONE_IDENTIFIER) {
1329 return NULL;
1330 }
1331
1332 return resource_by_identifier(c);
1333}
1334
1335/************************************************************************/
1339static struct terrain *char2terrain(char ch)
1340{
1341 /* terrain_by_identifier plus fatal error */
1343 return T_UNKNOWN;
1344 }
1345 terrain_type_iterate(pterrain) {
1346 if (pterrain->identifier == ch) {
1347 return pterrain;
1348 }
1350
1351 log_fatal("Unknown terrain identifier '%c' in savegame.", ch);
1352
1354
1356}
1357
1358/************************************************************************/
1363 const char *path, int plrno)
1364{
1365 char path_with_name[128];
1366 const char *name;
1367 struct advance *padvance;
1368
1370 "%s_name", path);
1371
1373
1374 if (!name || name[0] == '\0') {
1375 /* used by researching_saved */
1376 return A_UNKNOWN;
1377 }
1378 if (fc_strcasecmp(name, "A_FUTURE") == 0) {
1379 return A_FUTURE;
1380 }
1381 if (fc_strcasecmp(name, "A_NONE") == 0) {
1382 return A_NONE;
1383 }
1384 if (fc_strcasecmp(name, "A_UNSET") == 0) {
1385 return A_UNSET;
1386 }
1387
1390 "%s: unknown technology \"%s\".", path_with_name, name);
1391
1392 return advance_number(padvance);
1393}
1394
1395/* =======================================================================
1396 * Load savefile data.
1397 * ======================================================================= */
1398
1399/************************************************************************/
1403{
1404 const char *ruleset = secfile_lookup_str_default(loading->file,
1406 "savefile.rulesetdir");
1407
1408 /* Load ruleset. */
1410 if (!strcmp("default", game.server.rulesetdir)) {
1411 int version;
1412
1413 version = secfile_lookup_int_default(loading->file, -1, "savefile.version");
1414 if (version >= 30) {
1415 /* Here 'default' really means current default.
1416 * Saving happens with real ruleset name, so savegames containing this
1417 * are special scenarios. */
1419 } else {
1420 /* 'default' is the old name of the classic ruleset */
1421 sz_strlcpy(game.server.rulesetdir, "classic");
1422 }
1423 log_verbose("Savegame specified ruleset '%s'. Really loading '%s'.",
1425 }
1427 /* Failed to load correct ruleset */
1428 sg_failure_ret(FALSE, _("Failed to load ruleset '%s' needed for savegame."),
1429 ruleset);
1430 }
1431}
1432
1433/************************************************************************/
1437{
1438 int i;
1439 const char *terr_name;
1440 const char *str;
1441
1442 /* Check status and return if not OK (sg_success FALSE). */
1443 sg_check_ret();
1444
1445 /* Load savefile options. */
1446 loading->secfile_options
1447 = secfile_lookup_str(loading->file, "savefile.options");
1448
1449 /* We don't need these entries, but read them anyway to avoid
1450 * warnings about unread secfile entries. */
1451 (void) secfile_entry_by_path(loading->file, "savefile.reason");
1452 (void) secfile_entry_by_path(loading->file, "savefile.revision");
1453
1454 str = secfile_lookup_str(loading->file, "savefile.orig_version");
1456
1457 /* In case of savegame2.c saves, missing entry means savegame older than support
1458 * for saving last_updated by turn. So this must default to TRUE. */
1460 "savefile.last_updated_as_year");
1461
1462 /* Load improvements. */
1463 loading->improvement.size
1465 "savefile.improvement_size");
1466 if (loading->improvement.size) {
1467 loading->improvement.order
1468 = secfile_lookup_str_vec(loading->file, &loading->improvement.size,
1469 "savefile.improvement_vector");
1470 sg_failure_ret(loading->improvement.size != 0,
1471 "Failed to load improvement order: %s",
1472 secfile_error());
1473 }
1474
1475 /* Load technologies. */
1476 loading->technology.size
1478 "savefile.technology_size");
1479 if (loading->technology.size) {
1480 loading->technology.order
1481 = secfile_lookup_str_vec(loading->file, &loading->technology.size,
1482 "savefile.technology_vector");
1483 sg_failure_ret(loading->technology.size != 0,
1484 "Failed to load technology order: %s",
1485 secfile_error());
1486 }
1487
1488 /* Load Activities. */
1489 loading->activities.size
1491 "savefile.activities_size");
1492 if (loading->activities.size) {
1493 loading->activities.order
1494 = secfile_lookup_str_vec(loading->file, &loading->activities.size,
1495 "savefile.activities_vector");
1496 sg_failure_ret(loading->activities.size != 0,
1497 "Failed to load activity order: %s",
1498 secfile_error());
1499 }
1500
1501 /* Load traits. */
1502 loading->trait.size
1504 "savefile.trait_size");
1505 if (loading->trait.size) {
1506 loading->trait.order
1507 = secfile_lookup_str_vec(loading->file, &loading->trait.size,
1508 "savefile.trait_vector");
1509 sg_failure_ret(loading->trait.size != 0,
1510 "Failed to load trait order: %s",
1511 secfile_error());
1512 }
1513
1514 /* Load extras. */
1515 loading->extra.size
1517 "savefile.extras_size");
1518 if (loading->extra.size) {
1519 const char **modname;
1520 size_t nmod;
1521 int j;
1522
1523 modname = secfile_lookup_str_vec(loading->file, &loading->extra.size,
1524 "savefile.extras_vector");
1525 sg_failure_ret(loading->extra.size != 0,
1526 "Failed to load extras order: %s",
1527 secfile_error());
1529 "Number of extras defined by the ruleset (= %d) are "
1530 "lower than the number in the savefile (= %d).",
1531 game.control.num_extra_types, (int)loading->extra.size);
1532 /* make sure that the size of the array is divisible by 4 */
1533 nmod = 4 * ((loading->extra.size + 3) / 4);
1534 loading->extra.order = fc_calloc(nmod, sizeof(*loading->extra.order));
1535 for (j = 0; j < loading->extra.size; j++) {
1536 loading->extra.order[j] = extra_type_by_rule_name(modname[j]);
1537 }
1538 free(modname);
1539 for (; j < nmod; j++) {
1540 loading->extra.order[j] = NULL;
1541 }
1542 }
1543
1544 /* Load multipliers. */
1545 loading->multiplier.size
1547 "savefile.multipliers_size");
1548 if (loading->multiplier.size) {
1549 const char **modname;
1550 int j;
1551
1552 modname = secfile_lookup_str_vec(loading->file, &loading->multiplier.size,
1553 "savefile.multipliers_vector");
1554 sg_failure_ret(loading->multiplier.size != 0,
1555 "Failed to load multipliers order: %s",
1556 secfile_error());
1557 /* It's OK for the set of multipliers in the savefile to differ
1558 * from those in the ruleset. */
1559 loading->multiplier.order = fc_calloc(loading->multiplier.size,
1560 sizeof(*loading->multiplier.order));
1561 for (j = 0; j < loading->multiplier.size; j++) {
1562 loading->multiplier.order[j] = multiplier_by_rule_name(modname[j]);
1563 if (!loading->multiplier.order[j]) {
1564 log_verbose("Multiplier \"%s\" in savegame but not in ruleset, "
1565 "discarding", modname[j]);
1566 }
1567 }
1568 free(modname);
1569 }
1570
1571 /* Load specials. */
1572 loading->special.size
1574 "savefile.specials_size");
1575 if (loading->special.size) {
1576 const char **modname;
1577 size_t nmod;
1578 enum tile_special_type j;
1579
1580 modname = secfile_lookup_str_vec(loading->file, &loading->special.size,
1581 "savefile.specials_vector");
1582 sg_failure_ret(loading->special.size != 0,
1583 "Failed to load specials order: %s",
1584 secfile_error());
1585 /* make sure that the size of the array is divisible by 4 */
1586 /* Allocating extra 4 slots, just a couple of bytes,
1587 * in case of special.size being divisible by 4 already is intentional.
1588 * Added complexity would cost those couple of bytes in code size alone,
1589 * and we actually need at least one slot immediately after last valid
1590 * one. That's where S_LAST is (or was in version that saved the game)
1591 * and in some cases S_LAST gets written to savegame, at least as
1592 * activity target special when activity targets some base or road
1593 * instead. By having current S_LAST in that index allows us to map
1594 * that old S_LAST to current S_LAST, just like any real special within
1595 * special.size gets mapped. */
1596 nmod = loading->special.size + (4 - (loading->special.size % 4));
1597 loading->special.order = fc_calloc(nmod,
1598 sizeof(*loading->special.order));
1599 for (j = 0; j < loading->special.size; j++) {
1600 if (!fc_strcasecmp("Road", modname[j])) {
1601 loading->special.order[j] = S_OLD_ROAD;
1602 } else if (!fc_strcasecmp("Railroad", modname[j])) {
1603 loading->special.order[j] = S_OLD_RAILROAD;
1604 } else if (!fc_strcasecmp("River", modname[j])) {
1605 loading->special.order[j] = S_OLD_RIVER;
1606 } else {
1607 loading->special.order[j] = special_by_rule_name(modname[j]);
1608 }
1609 }
1610 free(modname);
1611 for (; j < nmod; j++) {
1612 loading->special.order[j] = S_LAST;
1613 }
1614 }
1615
1616 /* Load bases. */
1617 loading->base.size
1619 "savefile.bases_size");
1620 if (loading->base.size) {
1621 const char **modname;
1622 size_t nmod;
1623 int j;
1624
1625 modname = secfile_lookup_str_vec(loading->file, &loading->base.size,
1626 "savefile.bases_vector");
1627 sg_failure_ret(loading->base.size != 0,
1628 "Failed to load bases order: %s",
1629 secfile_error());
1630 /* make sure that the size of the array is divisible by 4 */
1631 nmod = 4 * ((loading->base.size + 3) / 4);
1632 loading->base.order = fc_calloc(nmod, sizeof(*loading->base.order));
1633 for (j = 0; j < loading->base.size; j++) {
1634 struct extra_type *pextra = extra_type_by_rule_name(modname[j]);
1635
1636 sg_failure_ret(pextra != NULL
1637 || game.control.num_base_types >= loading->base.size,
1638 "Unknown base type %s in savefile.",
1639 modname[j]);
1640
1641 if (pextra != NULL) {
1642 loading->base.order[j] = extra_base_get(pextra);
1643 } else {
1644 loading->base.order[j] = NULL;
1645 }
1646 }
1647 free(modname);
1648 for (; j < nmod; j++) {
1649 loading->base.order[j] = NULL;
1650 }
1651 }
1652
1653 /* Load roads. */
1654 loading->road.size
1656 "savefile.roads_size");
1657 if (loading->road.size) {
1658 const char **modname;
1659 size_t nmod;
1660 int j;
1661
1662 modname = secfile_lookup_str_vec(loading->file, &loading->road.size,
1663 "savefile.roads_vector");
1664 sg_failure_ret(loading->road.size != 0,
1665 "Failed to load roads order: %s",
1666 secfile_error());
1668 "Number of roads defined by the ruleset (= %d) are "
1669 "lower than the number in the savefile (= %d).",
1670 game.control.num_road_types, (int)loading->road.size);
1671 /* make sure that the size of the array is divisible by 4 */
1672 nmod = 4 * ((loading->road.size + 3) / 4);
1673 loading->road.order = fc_calloc(nmod, sizeof(*loading->road.order));
1674 for (j = 0; j < loading->road.size; j++) {
1675 struct extra_type *pextra = extra_type_by_rule_name(modname[j]);
1676
1677 if (pextra != NULL) {
1678 loading->road.order[j] = extra_road_get(pextra);
1679 } else {
1680 loading->road.order[j] = NULL;
1681 }
1682 }
1683 free(modname);
1684 for (; j < nmod; j++) {
1685 loading->road.order[j] = NULL;
1686 }
1687 }
1688
1689 /* Load specialists. */
1690 loading->specialist.size
1692 "savefile.specialists_size");
1693 if (loading->specialist.size) {
1694 const char **modname;
1695 size_t nmod;
1696 int j;
1697
1698 modname = secfile_lookup_str_vec(loading->file, &loading->specialist.size,
1699 "savefile.specialists_vector");
1700 sg_failure_ret(loading->specialist.size != 0,
1701 "Failed to load specialists order: %s",
1702 secfile_error());
1704 "Number of specialists defined by the ruleset (= %d) are "
1705 "lower than the number in the savefile (= %d).",
1706 game.control.num_specialist_types, (int)loading->specialist.size);
1707 /* make sure that the size of the array is divisible by 4 */
1708 /* That's not really needed with specialists at the moment, but done this way
1709 * for consistency with other types, and to be prepared for the time it needs
1710 * to be this way. */
1711 nmod = 4 * ((loading->specialist.size + 3) / 4);
1712 loading->specialist.order = fc_calloc(nmod, sizeof(*loading->specialist.order));
1713 for (j = 0; j < loading->specialist.size; j++) {
1714 loading->specialist.order[j] = specialist_by_rule_name(modname[j]);
1715 }
1716 free(modname);
1717 for (; j < nmod; j++) {
1718 loading->specialist.order[j] = NULL;
1719 }
1720 }
1721
1722 /* Load diplomatic state type order. */
1723 loading->ds_t.size
1725 "savefile.diplstate_type_size");
1726
1727 sg_failure_ret(loading->ds_t.size > 0,
1728 "Failed to load diplomatic state type order: %s",
1729 secfile_error());
1730
1731 if (loading->ds_t.size) {
1732 const char **modname;
1733 int j;
1734
1735 modname = secfile_lookup_str_vec(loading->file, &loading->ds_t.size,
1736 "savefile.diplstate_type_vector");
1737
1738 loading->ds_t.order = fc_calloc(loading->ds_t.size,
1739 sizeof(*loading->ds_t.order));
1740
1741 for (j = 0; j < loading->ds_t.size; j++) {
1742 loading->ds_t.order[j] = diplstate_type_by_name(modname[j],
1744 }
1745
1746 free(modname);
1747 }
1748
1749 /* Load city options order. */
1750 loading->coptions.size
1752 "savefile.city_options_size");
1753
1754 {
1755 const char *modname_old[] = { "Disband", "Sci_Specialists", "Tax_Specialists" };
1756 const char **modname;
1757 int j;
1758 bool compat;
1759
1760 if (loading->coptions.size > 0) {
1761 modname = secfile_lookup_str_vec(loading->file, &loading->coptions.size,
1762 "savefile.city_options_vector");
1763 compat = FALSE;
1764 } else {
1766 loading->coptions.size = 3;
1767 compat = TRUE;
1768 }
1769
1770 loading->coptions.order = fc_calloc(loading->coptions.size,
1771 sizeof(*loading->coptions.order));
1772
1773 for (j = 0; j < loading->coptions.size; j++) {
1774 loading->coptions.order[j] = city_options_by_name(modname[j],
1776 }
1777
1778 if (!compat) {
1779 free(modname);
1780 }
1781 }
1782
1783 /* Terrain identifiers */
1785 pterr->identifier_load = '\0';
1787
1788 i = 0;
1790 "savefile.terrident%d.name", i)) != NULL) {
1792
1793 if (pterr != NULL) {
1794 const char *iptr = secfile_lookup_str_default(loading->file, NULL,
1795 "savefile.terrident%d.identifier", i);
1796
1797 pterr->identifier_load = *iptr;
1798 } else {
1799 log_error("Identifier for unknown terrain type %s.", terr_name);
1800 }
1801 i++;
1802 }
1803
1806 if (pterr != pterr2 && pterr->identifier_load != '\0') {
1807 sg_failure_ret((pterr->identifier_load != pterr2->identifier_load),
1808 "%s and %s share a saved identifier",
1810 }
1813}
1814
1815/* =======================================================================
1816 * Load game status.
1817 * ======================================================================= */
1818
1819/************************************************************************/
1823{
1824 int i;
1825 const char *name;
1826
1827 /* Check status and return if not OK (sg_success FALSE). */
1828 sg_check_ret();
1829
1830 for (i = 0;
1832 "ruledata.government%d.name", i));
1833 i++) {
1835
1836 if (gov != NULL) {
1838 "ruledata.government%d.changes", i);
1839 }
1840 }
1841}
1842
1843/************************************************************************/
1846static void sg_load_game(struct loaddata *loading)
1847{
1848 int game_version;
1849 const char *str;
1850 int i;
1851
1852 /* Check status and return if not OK (sg_success FALSE). */
1853 sg_check_ret();
1854
1855 /* Load version. */
1857 = secfile_lookup_int_default(loading->file, 0, "game.version");
1858 /* We require at least version 2.2.99 */
1859 sg_failure_ret(20299 <= game_version, "Saved game is too old, at least "
1860 "version 2.2.99 required.");
1861
1862 loading->full_version = game_version;
1863
1864 secfile_entry_ignore(loading->file, "scenario.game_version");
1865
1866 /* Load server state. */
1867 str = secfile_lookup_str_default(loading->file, "S_S_INITIAL",
1868 "game.server_state");
1869 loading->server_state = server_states_by_name(str, strcmp);
1870 if (!server_states_is_valid(loading->server_state)) {
1871 /* Don't take any risk! */
1872 loading->server_state = S_S_INITIAL;
1873 }
1874
1877 "game.meta_patches");
1879
1881 /* Do not overwrite this if the user requested a specific metaserver
1882 * from the command line (option --Metaserver). */
1886 "game.meta_server"));
1887 }
1888
1889 if ('\0' == srvarg.serverid[0]) {
1890 /* Do not overwrite this if the user requested a specific metaserver
1891 * from the command line (option --serverid). */
1894 "game.serverid"));
1895 }
1896 sz_strlcpy(server.game_identifier,
1897 secfile_lookup_str_default(loading->file, "", "game.id"));
1898 /* We are not checking game_identifier legality just yet.
1899 * That's done when we are sure that rand seed has been initialized,
1900 * so that we can generate new game_identifier, if needed.
1901 * See sq_load_sanitycheck(). */
1902
1905 "game.phase_mode");
1908 "game.phase_mode_stored");
1911 "game.phase");
1915 "game.scoreturn");
1916
1919 "game.timeoutint");
1922 "game.timeoutintinc");
1925 "game.timeoutinc");
1928 "game.timeoutincmult");
1931 "game.timeoutcounter");
1932
1933 game.info.turn
1934 = secfile_lookup_int_default(loading->file, 0, "game.turn");
1936 "game.year"), "%s", secfile_error());
1939 = secfile_lookup_bool_default(loading->file, FALSE, "game.year_0_hack");
1940
1942 = secfile_lookup_int_default(loading->file, 0, "game.globalwarming");
1944 = secfile_lookup_int_default(loading->file, 0, "game.heating");
1946 = secfile_lookup_int_default(loading->file, 0, "game.warminglevel");
1947
1949 = secfile_lookup_int_default(loading->file, 0, "game.nuclearwinter");
1951 = secfile_lookup_int_default(loading->file, 0, "game.cooling");
1953 = secfile_lookup_int_default(loading->file, 0, "game.coolinglevel");
1954
1955 /* Savegame may have stored random_seed for documentation purposes only,
1956 * but we want to keep it for resaving. */
1957 game.server.seed = secfile_lookup_int_default(loading->file, 0, "game.random_seed");
1958
1959 /* Global advances. */
1961 "game.global_advances");
1962 if (str != NULL) {
1963 sg_failure_ret(strlen(str) == loading->technology.size,
1964 "Invalid length of 'game.global_advances' ("
1965 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
1966 strlen(str), loading->technology.size);
1967 for (i = 0; i < loading->technology.size; i++) {
1968 sg_failure_ret(str[i] == '1' || str[i] == '0',
1969 "Undefined value '%c' within 'game.global_advances'.",
1970 str[i]);
1971 if (str[i] == '1') {
1972 struct advance *padvance =
1973 advance_by_rule_name(loading->technology.order[i]);
1974
1975 if (padvance != NULL) {
1977 }
1978 }
1979 }
1980 }
1981
1983 = !secfile_lookup_bool_default(loading->file, TRUE, "game.save_players");
1984
1986 = secfile_lookup_float_default(loading->file, 0, "game.last_turn_change_time");
1987}
1988
1989/* =======================================================================
1990 * Load random status.
1991 * ======================================================================= */
1992
1993/************************************************************************/
1996static void sg_load_random(struct loaddata *loading)
1997{
1998 /* Check status and return if not OK (sg_success FALSE). */
1999 sg_check_ret();
2000
2001 if (secfile_lookup_bool_default(loading->file, FALSE, "random.saved")) {
2002 const char *str;
2003 int i;
2004
2005 /* Since random state was previously saved, save it also when resaving.
2006 * This affects only pre-2.6 scenarios where scenario.save_random
2007 * is not defined.
2008 * - If this is 2.6 or later scenario -> it would have saved random.saved = TRUE
2009 * only if scenario.save_random is already TRUE
2010 *
2011 * Do NOT touch this in case of regular savegame. They always have random.saved
2012 * set, but if one starts to make scenario based on a savegame, we want
2013 * default scenario settings in the beginning (default save_random = FALSE).
2014 */
2017 }
2018
2020 "random.index_J"), "%s", secfile_error());
2022 "random.index_K"), "%s", secfile_error());
2024 "random.index_X"), "%s", secfile_error());
2025
2026 for (i = 0; i < 8; i++) {
2027 str = secfile_lookup_str(loading->file, "random.table%d",i);
2028 sg_failure_ret(NULL != str, "%s", secfile_error());
2029 sscanf(str, "%8x %8x %8x %8x %8x %8x %8x", &loading->rstate.v[7*i],
2030 &loading->rstate.v[7*i+1], &loading->rstate.v[7*i+2],
2031 &loading->rstate.v[7*i+3], &loading->rstate.v[7*i+4],
2032 &loading->rstate.v[7*i+5], &loading->rstate.v[7*i+6]);
2033 }
2034 loading->rstate.is_init = TRUE;
2035 fc_rand_set_state(loading->rstate);
2036 } else {
2037 /* No random values - mark the setting. */
2038 (void) secfile_entry_by_path(loading->file, "random.saved");
2039
2040 /* We're loading a game without a seed (which is okay, if it's a scenario).
2041 * We need to generate the game seed now because it will be needed later
2042 * during the load. */
2044 loading->rstate = fc_rand_state();
2045 }
2046}
2047
2048/* =======================================================================
2049 * Load lua script data.
2050 * ======================================================================= */
2051
2052/************************************************************************/
2055static void sg_load_script(struct loaddata *loading)
2056{
2057 /* Check status and return if not OK (sg_success FALSE). */
2058 sg_check_ret();
2059
2061}
2062
2063/* =======================================================================
2064 * Load scenario data.
2065 * ======================================================================= */
2066
2067/************************************************************************/
2071{
2072 const char *buf;
2073 bool lake_flood_default;
2074
2075 /* Check status and return if not OK (sg_success FALSE). */
2076 sg_check_ret();
2077
2078 if (NULL == secfile_section_lookup(loading->file, "scenario")) {
2080
2081 return;
2082 }
2083
2084 /* Default is that when there's scenario section (which we already checked)
2085 * this is a scenario. Only if it explicitly says that it's not, we consider
2086 * this regular savegame */
2087 game.scenario.is_scenario = secfile_lookup_bool_default(loading->file, TRUE, "scenario.is_scenario");
2088
2089 if (!game.scenario.is_scenario) {
2090 return;
2091 }
2092
2093 buf = secfile_lookup_str_default(loading->file, "", "scenario.name");
2094 if (buf[0] != '\0') {
2096 }
2097
2099 "scenario.authors");
2100 if (buf[0] != '\0') {
2102 } else {
2103 game.scenario.authors[0] = '\0';
2104 }
2105
2107 "scenario.description");
2108 if (buf[0] != '\0') {
2110 } else {
2111 game.scenario_desc.description[0] = '\0';
2112 }
2113
2115 = secfile_lookup_bool_default(loading->file, FALSE, "scenario.save_random");
2117 = secfile_lookup_bool_default(loading->file, TRUE, "scenario.players");
2120 "scenario.startpos_nations");
2121
2124 "scenario.prevent_new_cities");
2125 if (loading->version < 20599) {
2126 /* Lake flooding may break some old scenarios where rivers made out of
2127 * lake terrains, so play safe there */
2129 } else {
2130 /* If lake flooding is a problem for a newer scenario, it could explicitly
2131 * disable it. */
2133 }
2136 "scenario.lake_flooding");
2139 "scenario.handmade");
2142 "scenario.allow_ai_type_fallback");
2144
2145 sg_failure_ret(loading->server_state == S_S_INITIAL
2146 || (loading->server_state == S_S_RUNNING
2147 && game.scenario.players),
2148 "Invalid scenario definition (server state '%s' and "
2149 "players are %s).",
2150 server_states_name(loading->server_state),
2151 game.scenario.players ? "saved" : "not saved");
2152
2153 /* Remove all defined players. They are recreated with the skill level
2154 * defined by the scenario. */
2155 (void) aifill(0);
2156}
2157
2158/* =======================================================================
2159 * Load game settings.
2160 * ======================================================================= */
2161
2162/************************************************************************/
2166{
2167 /* Check status and return if not OK (sg_success FALSE). */
2168 sg_check_ret();
2169
2170 settings_game_load(loading->file, "settings");
2171
2172 /* Save current status of fogofwar. */
2174
2175 /* Add all compatibility settings here. */
2176}
2177
2178/* =======================================================================
2179 * Load the main map.
2180 * ======================================================================= */
2181
2182/************************************************************************/
2185static void sg_load_map(struct loaddata *loading)
2186{
2187 /* Check status and return if not OK (sg_success FALSE). */
2188 sg_check_ret();
2189
2190 /* This defaults to TRUE even if map has not been generated. Also,
2191 * old versions have also explicitly saved TRUE even in pre-game.
2192 * We rely on that
2193 * 1) scenario maps have it explicitly right.
2194 * 2) when map is actually generated, it re-initialize this to FALSE. */
2196 = secfile_lookup_bool_default(loading->file, TRUE, "map.have_huts");
2197
2199
2200 /* Savegame may have stored random_seed for documentation purposes only,
2201 * but we want to keep it for resaving. */
2203 = secfile_lookup_int_default(loading->file, 0, "map.random_seed");
2204
2205 if (S_S_INITIAL == loading->server_state
2207 /* Generator MAPGEN_SCENARIO is used;
2208 * this map was done with the map editor. */
2209
2210 /* Load tiles. */
2213
2214 if (loading->version >= 30) {
2215 /* 2.6.0 or newer */
2217 } else {
2219 if (loading->version >= 20) {
2220 /* 2.5.0 or newer */
2222 }
2223 if (has_capability("specials", loading->secfile_options)) {
2224 /* Load specials. */
2226 }
2227 }
2228
2229 /* have_resources TRUE only if set so by sg_load_map_tiles_resources() */
2231 if (has_capability("specials", loading->secfile_options)) {
2232 /* Load resources. */
2234 } else if (has_capability("riversoverlay", loading->secfile_options)) {
2235 /* Load only rivers overlay. */
2237 }
2238
2239 /* Nothing more needed for a scenario. */
2240 return;
2241 }
2242
2243 if (S_S_INITIAL == loading->server_state) {
2244 /* Nothing more to do if it is not a scenario but in initial state. */
2245 return;
2246 }
2247
2250 if (loading->version >= 30) {
2251 /* 2.6.0 or newer */
2253 } else {
2255 if (loading->version >= 20) {
2256 /* 2.5.0 or newer */
2258 }
2260 }
2265}
2266
2267/************************************************************************/
2271{
2272 /* Check status and return if not OK (sg_success FALSE). */
2273 sg_check_ret();
2274
2275 /* Initialize the map for the current topology. 'map.xsize' and
2276 * 'map.ysize' must be set. */
2278
2279 /* Allocate map. */
2281
2282 /* get the terrain type */
2283 LOAD_MAP_CHAR(ch, ptile, ptile->terrain = char2terrain(ch), loading->file,
2284 "map.t%04d");
2286
2287 /* Check for special tile sprites. */
2288 whole_map_iterate(&(wld.map), ptile) {
2289 const char *spec_sprite;
2290 const char *label;
2291 int nat_x, nat_y;
2292
2294 spec_sprite = secfile_lookup_str(loading->file, "map.spec_sprite_%d_%d",
2295 nat_x, nat_y);
2296 label = secfile_lookup_str_default(loading->file, NULL, "map.label_%d_%d",
2297 nat_x, nat_y);
2298 if (NULL != ptile->spec_sprite) {
2299 ptile->spec_sprite = fc_strdup(spec_sprite);
2300 }
2301 if (label != NULL) {
2302 tile_set_label(ptile, label);
2303 }
2305}
2306
2307/************************************************************************/
2311{
2312 /* Check status and return if not OK (sg_success FALSE). */
2313 sg_check_ret();
2314
2315 /* Load extras. */
2316 halfbyte_iterate_extras(j, loading->extra.size) {
2317 LOAD_MAP_CHAR(ch, ptile, sg_extras_set_bv(&ptile->extras,
2318 ch, loading->extra.order + 4 * j),
2319 loading->file, "map.e%02d_%04d", j);
2321}
2322
2323/************************************************************************/
2327{
2328 /* Check status and return if not OK (sg_success FALSE). */
2329 sg_check_ret();
2330
2331 /* Load bases. */
2332 halfbyte_iterate_bases(j, loading->base.size) {
2333 LOAD_MAP_CHAR(ch, ptile, sg_bases_set_bv(&ptile->extras, ch,
2334 loading->base.order + 4 * j),
2335 loading->file, "map.b%02d_%04d", j);
2337}
2338
2339/************************************************************************/
2343{
2344 /* Check status and return if not OK (sg_success FALSE). */
2345 sg_check_ret();
2346
2347 /* Load roads. */
2348 halfbyte_iterate_roads(j, loading->road.size) {
2349 LOAD_MAP_CHAR(ch, ptile, sg_roads_set_bv(&ptile->extras, ch,
2350 loading->road.order + 4 * j),
2351 loading->file, "map.r%02d_%04d", j);
2353}
2354
2355/************************************************************************/
2359 bool rivers_overlay)
2360{
2361 /* Check status and return if not OK (sg_success FALSE). */
2362 sg_check_ret();
2363
2364 /* If 'rivers_overlay' is set to TRUE, load only the rivers overlay map
2365 * from the savegame file.
2366 *
2367 * A scenario may define the terrain of the map but not list the specials
2368 * on it (thus allowing users to control the placement of specials).
2369 * However rivers are a special case and must be included in the map along
2370 * with the scenario. Thus in those cases this function should be called
2371 * to load the river information separate from any other special data.
2372 *
2373 * This does not need to be called from map_load(), because map_load()
2374 * loads the rivers overlay along with the rest of the specials. Call this
2375 * only if you've already called map_load_tiles(), and want to load only
2376 * the rivers overlay but no other specials. Scenarios that encode things
2377 * this way should have the "riversoverlay" capability. */
2378 halfbyte_iterate_special(j, loading->special.size) {
2379 LOAD_MAP_CHAR(ch, ptile, sg_special_set_bv(ptile, &ptile->extras, ch,
2380 loading->special.order + 4 * j,
2382 loading->file, "map.spe%02d_%04d", j);
2384}
2385
2386/************************************************************************/
2390{
2391 /* Check status and return if not OK (sg_success FALSE). */
2392 sg_check_ret();
2393
2395 loading->file, "map.res%04d");
2396
2397 /* After the resources are loaded, indicate those currently valid. */
2398 whole_map_iterate(&(wld.map), ptile) {
2399 if (NULL == ptile->resource) {
2400 continue;
2401 }
2402
2403 if (ptile->terrain == NULL || !terrain_has_resource(ptile->terrain, ptile->resource)) {
2404 BV_CLR(ptile->extras, extra_index(ptile->resource));
2405 }
2407
2410}
2411
2412/************************************************************************/
2417{
2418 struct nation_type *pnation;
2419 struct startpos *psp;
2420 struct tile *ptile;
2421 const char SEPARATOR = '#';
2422 const char *nation_names;
2423 int nat_x, nat_y;
2424 bool exclude;
2425 int i, startpos_count;
2426
2427 /* Check status and return if not OK (sg_success FALSE). */
2428 sg_check_ret();
2429
2431 = secfile_lookup_int_default(loading->file, 0, "map.startpos_count");
2432
2433 if (0 == startpos_count) {
2434 /* Nothing to do. */
2435 return;
2436 }
2437
2438 for (i = 0; i < startpos_count; i++) {
2439 if (!secfile_lookup_int(loading->file, &nat_x, "map.startpos%d.x", i)
2440 || !secfile_lookup_int(loading->file, &nat_y,
2441 "map.startpos%d.y", i)) {
2442 log_sg("Warning: Undefined coordinates for startpos %d", i);
2443 continue;
2444 }
2445
2446 ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
2447 if (NULL == ptile) {
2448 log_error("Start position native coordinates (%d, %d) do not exist "
2449 "in this map. Skipping...", nat_x, nat_y);
2450 continue;
2451 }
2452
2453 exclude = secfile_lookup_bool_default(loading->file, FALSE,
2454 "map.startpos%d.exclude", i);
2455
2456 psp = map_startpos_new(ptile);
2457
2459 "map.startpos%d.nations", i);
2460 if (NULL != nation_names && '\0' != nation_names[0]) {
2461 const size_t size = strlen(nation_names) + 1;
2462 char buf[size], *start, *end;
2463
2465 for (start = buf - 1; NULL != start; start = end) {
2466 start++;
2467 if ((end = strchr(start, SEPARATOR))) {
2468 *end = '\0';
2469 }
2470
2471 pnation = nation_by_rule_name(start);
2472 if (NO_NATION_SELECTED != pnation) {
2473 if (exclude) {
2474 startpos_disallow(psp, pnation);
2475 } else {
2476 startpos_allow(psp, pnation);
2477 }
2478 } else {
2479 log_verbose("Missing nation \"%s\".", start);
2480 }
2481 }
2482 }
2483 }
2484
2485 if (0 < map_startpos_count()
2486 && loading->server_state == S_S_INITIAL
2488 log_verbose("Number of starts (%d) are lower than rules.max_players "
2489 "(%d), lowering rules.max_players.",
2492 }
2493
2494 /* Re-initialize nation availability in light of start positions.
2495 * This has to be after loading [scenario] and [map].startpos and
2496 * before we seek nations for players. */
2498}
2499
2500/************************************************************************/
2504{
2505 int x, y;
2506 struct player *owner = NULL;
2507 struct tile *claimer = NULL;
2508 struct player *eowner = NULL;
2509
2510 /* Check status and return if not OK (sg_success FALSE). */
2511 sg_check_ret();
2512
2513 if (game.info.is_new_game) {
2514 /* No owner/source information for a new game / scenario. */
2515 return;
2516 }
2517
2518 /* Owner and ownership source are stored as plain numbers */
2519 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
2520 const char *buffer1 = secfile_lookup_str(loading->file,
2521 "map.owner%04d", y);
2522 const char *buffer2 = secfile_lookup_str(loading->file,
2523 "map.source%04d", y);
2524 const char *buffer3 = secfile_lookup_str(loading->file,
2525 "map.eowner%04d", y);
2526 const char *ptr1 = buffer1;
2527 const char *ptr2 = buffer2;
2528 const char *ptr3 = buffer3;
2529
2532 if (loading->version >= 30) {
2534 }
2535
2536 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
2537 char token1[TOKEN_SIZE];
2538 char token2[TOKEN_SIZE];
2539 char token3[TOKEN_SIZE];
2540 int number;
2541 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
2542
2543 scanin(&ptr1, ",", token1, sizeof(token1));
2544 sg_failure_ret(token1[0] != '\0',
2545 "Map size not correct (map.owner%d).", y);
2546 if (strcmp(token1, "-") == 0) {
2547 owner = NULL;
2548 } else {
2550 "Got map owner %s in (%d, %d).", token1, x, y);
2551 owner = player_by_number(number);
2552 }
2553
2554 scanin(&ptr2, ",", token2, sizeof(token2));
2555 sg_failure_ret(token2[0] != '\0',
2556 "Map size not correct (map.source%d).", y);
2557 if (strcmp(token2, "-") == 0) {
2558 claimer = NULL;
2559 } else {
2561 "Got map source %s in (%d, %d).", token2, x, y);
2562 claimer = index_to_tile(&(wld.map), number);
2563 }
2564
2565 if (loading->version >= 30) {
2566 scanin(&ptr3, ",", token3, sizeof(token3));
2567 sg_failure_ret(token3[0] != '\0',
2568 "Map size not correct (map.eowner%d).", y);
2569 if (strcmp(token3, "-") == 0) {
2570 eowner = NULL;
2571 } else {
2573 "Got base owner %s in (%d, %d).", token3, x, y);
2574 eowner = player_by_number(number);
2575 }
2576 } else {
2577 eowner = owner;
2578 }
2579
2581 tile_claim_bases(ptile, eowner);
2582 log_debug("extras_owner(%d, %d) = %s", TILE_XY(ptile), player_name(eowner));
2583 }
2584 }
2585}
2586
2587/************************************************************************/
2591{
2592 int x, y;
2593
2594 /* Check status and return if not OK (sg_success FALSE). */
2595 sg_check_ret();
2596
2597 sg_failure_ret(loading->worked_tiles == NULL,
2598 "City worked map not loaded!");
2599
2600 loading->worked_tiles = fc_malloc(MAP_INDEX_SIZE *
2601 sizeof(*loading->worked_tiles));
2602
2603 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
2604 const char *buffer = secfile_lookup_str(loading->file, "map.worked%04d",
2605 y);
2606 const char *ptr = buffer;
2607
2608 sg_failure_ret(NULL != buffer,
2609 "Savegame corrupt - map line %d not found.", y);
2610 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
2611 char token[TOKEN_SIZE];
2612 int number;
2613 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
2614
2615 scanin(&ptr, ",", token, sizeof(token));
2616 sg_failure_ret('\0' != token[0],
2617 "Savegame corrupt - map size not correct.");
2618 if (strcmp(token, "-") == 0) {
2619 number = -1;
2620 } else {
2621 sg_failure_ret(str_to_int(token, &number) && 0 < number,
2622 "Savegame corrupt - got tile worked by city "
2623 "id=%s in (%d, %d).", token, x, y);
2624 }
2625
2626 loading->worked_tiles[ptile->index] = number;
2627 }
2628 }
2629}
2630
2631/************************************************************************/
2635{
2636 /* Check status and return if not OK (sg_success FALSE). */
2637 sg_check_ret();
2638
2639 players_iterate(pplayer) {
2640 /* Allocate player private map here; it is needed in different modules
2641 * besides this one ((i.e. sg_load_player_*()). */
2642 player_map_init(pplayer);
2644
2646 "game.save_known")) {
2647 int lines = player_slot_max_used_number()/32 + 1, j, p, l, i;
2648 unsigned int *known = fc_calloc(lines * MAP_INDEX_SIZE, sizeof(*known));
2649
2650 for (l = 0; l < lines; l++) {
2651 for (j = 0; j < 8; j++) {
2652 for (i = 0; i < 4; i++) {
2653 /* Only bother trying to load the map for this halfbyte if at least
2654 * one of the corresponding player slots is in use. */
2655 if (player_slot_is_used(player_slot_by_number(l*32 + j*4 + i))) {
2656 LOAD_MAP_CHAR(ch, ptile,
2657 known[l * MAP_INDEX_SIZE + tile_index(ptile)]
2658 |= ascii_hex2bin(ch, j),
2659 loading->file, "map.k%02d_%04d", l * 8 + j);
2660 break;
2661 }
2662 }
2663 }
2664 }
2665
2666 players_iterate(pplayer) {
2667 dbv_clr_all(&pplayer->tile_known);
2669
2670 /* HACK: we read the known data from hex into 32-bit integers, and
2671 * now we convert it to the known tile data of each player. */
2672 whole_map_iterate(&(wld.map), ptile) {
2673 players_iterate(pplayer) {
2674 p = player_index(pplayer);
2675 l = player_index(pplayer) / 32;
2676
2677 if (known[l * MAP_INDEX_SIZE + tile_index(ptile)] & (1u << (p % 32))) {
2678 map_set_known(ptile, pplayer);
2679 }
2682
2683 FC_FREE(known);
2684 }
2685}
2686
2687/* =======================================================================
2688 * Load player data.
2689 *
2690 * This is split into two parts as some data can only be loaded if the
2691 * number of players is known and the corresponding player slots are
2692 * defined.
2693 * ======================================================================= */
2694
2695/************************************************************************/
2699{
2700 int i, k, nplayers;
2701 const char *str;
2702 bool shuffle_loaded = TRUE;
2703
2704 /* Check status and return if not OK (sg_success FALSE). */
2705 sg_check_ret();
2706
2707 if (S_S_INITIAL == loading->server_state
2708 || game.info.is_new_game) {
2709 /* Nothing more to do. */
2710 return;
2711 }
2712
2713 /* Load destroyed wonders: */
2715 "players.destroyed_wonders");
2716 sg_failure_ret(str != NULL, "%s", secfile_error());
2717 sg_failure_ret(strlen(str) == loading->improvement.size,
2718 "Invalid length for 'players.destroyed_wonders' ("
2719 SIZE_T_PRINTF" ~= " SIZE_T_PRINTF ")",
2720 strlen(str), loading->improvement.size);
2721 for (k = 0; k < loading->improvement.size; k++) {
2722 sg_failure_ret(str[k] == '1' || str[k] == '0',
2723 "Undefined value '%c' within "
2724 "'players.destroyed_wonders'.", str[k]);
2725
2726 if (str[k] == '1') {
2727 struct impr_type *pimprove =
2728 improvement_by_rule_name(loading->improvement.order[k]);
2729
2730 if (pimprove) {
2733 }
2734 }
2735 }
2736
2737 server.identity_number
2738 = secfile_lookup_int_default(loading->file, server.identity_number,
2739 "players.identity_number_used");
2740
2741 /* First remove all defined players. */
2742 players_iterate(pplayer) {
2743 server_remove_player(pplayer);
2745
2746 /* Now, load the players from the savefile. */
2747 player_slots_iterate(pslot) {
2748 struct player *pplayer;
2749 struct rgbcolor *prgbcolor = NULL;
2750 int pslot_id = player_slot_index(pslot);
2751
2752 if (NULL == secfile_section_lookup(loading->file, "player%d",
2753 pslot_id)) {
2754 continue;
2755 }
2756
2757 /* Get player AI type. */
2758 str = secfile_lookup_str(loading->file, "player%d.ai_type",
2759 player_slot_index(pslot));
2760 sg_failure_ret(str != NULL, "%s", secfile_error());
2761
2762 /* Get player color */
2763 if (!rgbcolor_load(loading->file, &prgbcolor, "player%d.color",
2764 pslot_id)) {
2765 if (loading->version >= 10 && game_was_started()) {
2766 /* 2.4.0 or later savegame. This is not an error in 2.3 savefiles,
2767 * as they predate the introduction of configurable player colors. */
2768 log_sg("Game has started, yet player %d has no color defined.",
2769 pslot_id);
2770 /* This will be fixed up later */
2771 } else {
2772 log_verbose("No color defined for player %d.", pslot_id);
2773 /* Colors will be assigned on game start, or at end of savefile
2774 * loading if game has already started */
2775 }
2776 }
2777
2778 /* Create player. */
2779 pplayer = server_create_player(player_slot_index(pslot), str,
2780 prgbcolor,
2783 sg_failure_ret(pplayer != NULL, "Invalid AI type: '%s'!", str);
2784
2785 server_player_init(pplayer, FALSE, FALSE);
2786
2787 /* Free the color definition. */
2789
2790 /* Multipliers (policies) */
2791
2792 /* First initialise player values with ruleset defaults; this will
2793 * cover any in the ruleset not known when the savefile was created. */
2794 multipliers_iterate(pmul) {
2795 pplayer->multipliers[multiplier_index(pmul)].value
2796 = pplayer->multipliers[multiplier_index(pmul)].target = pmul->def;
2798
2799 /* Now override with any values from the savefile. */
2800 for (k = 0; k < loading->multiplier.size; k++) {
2801 const struct multiplier *pmul = loading->multiplier.order[k];
2802
2803 if (pmul) {
2805 int val =
2807 "player%d.multiplier%d.val",
2808 player_slot_index(pslot), k);
2809 int rval = (((CLIP(pmul->start, val, pmul->stop)
2810 - pmul->start) / pmul->step) * pmul->step) + pmul->start;
2811
2812 if (rval != val) {
2813 log_verbose("Player %d had illegal value for multiplier \"%s\": "
2814 "was %d, clamped to %d", pslot_id,
2815 multiplier_rule_name(pmul), val, rval);
2816 }
2817 pplayer->multipliers[idx].value = rval;
2818
2819 val =
2821 pplayer->multipliers[idx].value,
2822 "player%d.multiplier%d.target",
2823 player_slot_index(pslot), k);
2824 rval = (((CLIP(pmul->start, val, pmul->stop)
2825 - pmul->start) / pmul->step) * pmul->step) + pmul->start;
2826
2827 if (rval != val) {
2828 log_verbose("Player %d had illegal value for multiplier_target "
2829 "\"%s\": was %d, clamped to %d", pslot_id,
2830 multiplier_rule_name(pmul), val, rval);
2831 }
2832 pplayer->multipliers[idx].target = rval;
2833
2834 /* Never present in savegame2 format */
2835 pplayer->multipliers[idx].changed = 0;
2836 } /* else silently discard multiplier not in current ruleset */
2837 }
2838
2839 /* Just in case savecompat starts adding it in the future. */
2840 pplayer->server.border_vision =
2842 "player%d.border_vision",
2843 player_slot_index(pslot));
2845
2846 /* check number of players */
2847 nplayers = secfile_lookup_int_default(loading->file, 0, "players.nplayers");
2848 sg_failure_ret(player_count() == nplayers, "The value of players.nplayers "
2849 "(%d) from the loaded game does not match the number of "
2850 "players present (%d).", nplayers, player_count());
2851
2852 /* Load team information. */
2853 players_iterate(pplayer) {
2854 int team;
2855 struct team_slot *tslot = NULL;
2856
2858 "player%d.team_no",
2859 player_number(pplayer))
2861 "Invalid team definition for player %s (nb %d).",
2862 player_name(pplayer), player_number(pplayer));
2863 /* Should never fail when slot given is not nullptr */
2864 team_add_player(pplayer, team_new(tslot));
2866
2867 /* Loading the shuffle list is quite complex. At the time of saving the
2868 * shuffle data is saved as
2869 * shuffled_player_<number> = player_slot_id
2870 * where number is an increasing number and player_slot_id is a number
2871 * between 0 and the maximum number of player slots. Now we have to create
2872 * a list
2873 * shuffler_players[number] = player_slot_id
2874 * where all player slot IDs are used exactly one time. The code below
2875 * handles this ... */
2876 if (secfile_lookup_int_default(loading->file, -1,
2877 "players.shuffled_player_%d", 0) >= 0) {
2878 int slots = player_slot_count();
2879 int plrcount = player_count();
2882
2883 for (i = 0; i < slots; i++) {
2884 /* Array to save used numbers. */
2886 /* List of all player IDs (needed for set_shuffled_players()). It is
2887 * initialised with the value -1 to indicate that no value is set. */
2888 shuffled_players[i] = -1;
2889 }
2890
2891 /* Load shuffled player list. */
2892 for (i = 0; i < plrcount; i++) {
2893 int shuffle
2895 "players.shuffled_player_%d", i);
2896
2897 if (shuffle == -1) {
2898 log_sg("Missing player shuffle information (index %d) "
2899 "- reshuffle player list!", i);
2901 break;
2902 } else if (shuffled_player_set[shuffle]) {
2903 log_sg("Player shuffle %d used two times "
2904 "- reshuffle player list!", shuffle);
2906 break;
2907 }
2908 /* Set this ID as used. */
2910
2911 /* Save the player ID in the shuffle list. */
2913 }
2914
2915 if (shuffle_loaded) {
2916 /* Insert missing numbers. */
2917 int shuffle_index = plrcount;
2918
2919 for (i = 0; i < slots; i++) {
2920 if (!shuffled_player_set[i]) {
2922 }
2923
2924 /* shuffle_index must not grow higher than size of shuffled_players. */
2926 "Invalid player shuffle data!");
2927 }
2928
2929#ifdef FREECIV_DEBUG
2930 log_debug("[load shuffle] player_count() = %d", player_count());
2931 player_slots_iterate(pslot) {
2932 int plrid = player_slot_index(pslot);
2933
2934 log_debug("[load shuffle] id: %3d => slot: %3d | slot %3d: %s",
2936 shuffled_player_set[plrid] ? "is used" : "-");
2938#endif /* FREECIV_DEBUG */
2939
2940 /* Set shuffle list from savegame. */
2942 }
2943 }
2944
2945 if (!shuffle_loaded) {
2946 /* No shuffled players included or error loading them, so shuffle them
2947 * (this may include scenarios). */
2949 }
2950}
2951
2952/************************************************************************/
2956{
2957 /* Check status and return if not OK (sg_success FALSE). */
2958 sg_check_ret();
2959
2960 if (game.info.is_new_game) {
2961 /* Nothing to do. */
2962 return;
2963 }
2964
2965 players_iterate(pplayer) {
2966 sg_load_player_main(loading, pplayer);
2968 sg_load_player_units(loading, pplayer);
2970
2971 /* Check the success of the functions above. */
2972 sg_check_ret();
2973
2974 /* Print out some information */
2975 if (is_ai(pplayer)) {
2976 log_normal(_("%s has been added as %s level AI-controlled player "
2977 "(%s)."), player_name(pplayer),
2978 ai_level_translated_name(pplayer->ai_common.skill_level),
2979 ai_name(pplayer->ai));
2980 } else {
2981 log_normal(_("%s has been added as human player."),
2982 player_name(pplayer));
2983 }
2985
2986 /* Also load the transport status of the units here. It must be a special
2987 * case as all units must be known (unit on an allied transporter). */
2988 players_iterate(pplayer) {
2989 /* Load unit transport status. */
2992
2993 /* Savegame may contain nation assignments that are incompatible with the
2994 * current nationset -- for instance, if it predates the introduction of
2995 * nationsets. Ensure they are compatible, one way or another. */
2997
2998 /* Some players may have invalid nations in the ruleset. Once all players
2999 * are loaded, pick one of the remaining nations for them. */
3000 players_iterate(pplayer) {
3001 if (pplayer->nation == NO_NATION_SELECTED) {
3004 /* TRANS: Minor error message: <Leader> ... <Poles>. */
3005 log_sg(_("%s had invalid nation; changing to %s."),
3006 player_name(pplayer), nation_plural_for_player(pplayer));
3007
3008 ai_traits_init(pplayer);
3009 }
3011
3012 /* Sanity check alliances, prevent allied-with-ally-of-enemy. */
3015 if (pplayers_allied(plr, aplayer)) {
3017 DS_ALLIANCE);
3018
3021 log_sg("Illegal alliance structure detected: "
3022 "%s alliance to %s reduced to peace treaty.",
3027 }
3028 }
3031
3032 /* Update cached city illness. This can depend on trade routes,
3033 * so can't be calculated until all players have been loaded. */
3034 if (game.info.illness_on) {
3036 pcity->server.illness
3038 &(pcity->illness_trade), NULL);
3040 }
3041
3042 /* Update all city information. This must come after all cities are
3043 * loaded (in player_load) but before player (dumb) cities are loaded
3044 * in player_load_vision(). */
3045 players_iterate(plr) {
3046 city_list_iterate(plr->cities, pcity) {
3049 CALL_PLR_AI_FUNC(city_got, plr, plr, pcity);
3052
3053 /* Since the cities must be placed on the map to put them on the
3054 player map we do this afterwards */
3055 players_iterate(pplayer) {
3057 /* Check the success of the function above. */
3058 sg_check_ret();
3060
3061 /* Check shared vision. Shared tiles are never given in savegame2 save */
3062 players_iterate(pplayer) {
3063 BV_CLR_ALL(pplayer->gives_shared_vision);
3064 BV_CLR_ALL(pplayer->gives_shared_tiles);
3065 BV_CLR_ALL(pplayer->server.really_gives_vision);
3067
3068 /* Set up shared vision... */
3069 players_iterate(pplayer) {
3070 int plr1 = player_index(pplayer);
3071
3073 int plr2 = player_index(pplayer2);
3074
3076 "player%d.diplstate%d.gives_shared_vision", plr1, plr2)) {
3077 give_shared_vision(pplayer, pplayer2);
3078 }
3081
3082 /* ...and check it */
3085 /* TODO: Is there a good reason player is not marked as
3086 * giving shared vision to themselves -> really_gives_vision()
3087 * returning FALSE when pplayer1 == pplayer2 */
3088 if (pplayer1 != pplayer2
3091 sg_regr(3000900,
3092 _("%s did not give shared vision to team member %s."),
3095 }
3097 sg_regr(3000900,
3098 _("%s did not give shared vision to team member %s."),
3101 }
3102 }
3105
3108
3109 /* All vision is ready; this calls city_thaw_workers_queue(). */
3111
3112 /* Make sure everything is consistent. */
3113 players_iterate(pplayer) {
3114 unit_list_iterate(pplayer->units, punit) {
3116 struct tile *ptile = unit_tile(punit);
3117
3118 log_sg("%s doing illegal activity in savegame!",
3120 log_sg("Activity: %s, Target: %s, Tile: (%d, %d), Terrain: %s",
3124 : "missing",
3125 TILE_XY(ptile), terrain_rule_name(tile_terrain(ptile)));
3127 }
3130
3133 city_thaw_workers(pcity); /* may auto_arrange_workers() */
3135
3136 /* Player colors are always needed once game has started. Pre-2.4 savegames
3137 * lack them. This cannot be in compatibility conversion layer as we need
3138 * all the player data available to be able to assign best colors. */
3139 if (game_was_started()) {
3141 }
3142}
3143
3144/************************************************************************/
3148 struct player *plr)
3149{
3150 const char **slist;
3151 int i, plrno = player_number(plr);
3152 const char *str;
3153 struct government *gov;
3154 const char *level;
3155 const char *barb_str;
3156 size_t nval;
3157
3158 /* Check status and return if not OK (sg_success FALSE). */
3159 sg_check_ret();
3160
3161 /* Basic player data. */
3162 str = secfile_lookup_str(loading->file, "player%d.name", plrno);
3163 sg_failure_ret(str != NULL, "%s", secfile_error());
3165 sz_strlcpy(plr->username,
3167 "player%d.username", plrno));
3169 "player%d.unassigned_user", plrno),
3170 "%s", secfile_error());
3173 "player%d.orig_username",
3174 plrno));
3177 "player%d.ranked_username",
3178 plrno));
3180 "player%d.unassigned_ranked", plrno),
3181 "%s", secfile_error());
3183 "player%d.delegation_username",
3184 plrno);
3185 /* Defaults to no delegation. */
3186 if (strlen(str)) {
3188 }
3189
3190 /* Player flags */
3191 BV_CLR_ALL(plr->flags);
3192 slist = secfile_lookup_str_vec(loading->file, &nval, "player%d.flags", plrno);
3193 for (i = 0; i < nval; i++) {
3194 const char *sval = slist[i];
3196
3197 sg_failure_ret(plr_flag_id_is_valid(fid), "Invalid player flag \"%s\".", sval);
3198
3199 BV_SET(plr->flags, fid);
3200 }
3201 free(slist);
3202
3203 /* Nation */
3204 str = secfile_lookup_str(loading->file, "player%d.nation", plrno);
3206 if (plr->nation != NULL) {
3207 ai_traits_init(plr);
3208 }
3209
3210 /* Government */
3211 str = secfile_lookup_str(loading->file, "player%d.government_name",
3212 plrno);
3214 sg_failure_ret(gov != NULL, "Player%d: unsupported government \"%s\".",
3215 plrno, str);
3216 plr->government = gov;
3217
3218 /* Target government */
3220 "player%d.target_government_name", plrno);
3221 if (str != NULL) {
3223 } else {
3224 plr->target_government = NULL;
3225 }
3228 "player%d.revolution_finishes", plrno);
3229
3230 /* Load diplomatic data (diplstate + embassy + vision).
3231 * Shared vision is loaded in sg_load_players(). */
3233 players_iterate(pplayer) {
3234 char buf[32];
3235 int unconverted;
3236 struct player_diplstate *ds = player_diplstate_get(plr, pplayer);
3237 i = player_index(pplayer);
3238
3239 /* load diplomatic status */
3240 fc_snprintf(buf, sizeof(buf), "player%d.diplstate%d", plrno, i);
3241
3242 unconverted =
3243 secfile_lookup_int_default(loading->file, -1, "%s.type", buf);
3244 if (unconverted >= 0 && unconverted < loading->ds_t.size) {
3245 /* Look up what state the unconverted number represents. */
3246 ds->type = loading->ds_t.order[unconverted];
3247 } else {
3248 log_sg("No valid diplomatic state type between players %d and %d",
3249 plrno, i);
3250
3251 ds->type = DS_WAR;
3252 }
3253
3254 unconverted =
3255 secfile_lookup_int_default(loading->file, -1, "%s.max_state", buf);
3256 if (unconverted >= 0 && unconverted < loading->ds_t.size) {
3257 /* Look up what state the unconverted number represents. */
3258 ds->max_state = loading->ds_t.order[unconverted];
3259 } else {
3260 log_sg("No valid diplomatic max_state between players %d and %d",
3261 plrno, i);
3262
3263 ds->max_state = DS_WAR;
3264 }
3265
3266 /* FIXME: If either party is barbarian, we cannot enforce below check */
3267#if 0
3268 if (ds->type == DS_WAR && ds->first_contact_turn <= 0) {
3269 sg_regr(3020000,
3270 "Player%d: War with player %d who has never been met. "
3271 "Reverted to No Contact state.", plrno, i);
3272 ds->type = DS_NO_CONTACT;
3273 }
3274#endif
3275
3276 if (valid_dst_closest(ds) != ds->max_state) {
3277 sg_regr(3020000,
3278 "Player%d: closest diplstate to player %d less than current. "
3279 "Updated.", plrno, i);
3280 ds->max_state = ds->type;
3281 }
3282
3283 ds->first_contact_turn =
3285 "%s.first_contact_turn", buf);
3286 ds->turns_left =
3287 secfile_lookup_int_default(loading->file, -2, "%s.turns_left", buf);
3288 ds->has_reason_to_cancel =
3290 "%s.has_reason_to_cancel", buf);
3291 ds->contact_turns_left =
3293 "%s.contact_turns_left", buf);
3294
3295 if (secfile_lookup_bool_default(loading->file, FALSE, "%s.embassy",
3296 buf)) {
3297 BV_SET(plr->real_embassy, i);
3298 }
3299 /* 'gives_shared_vision' is loaded in sg_load_players() as all cities
3300 * must be known. */
3302
3303 /* load ai data */
3305 char buf[32];
3306
3307 fc_snprintf(buf, sizeof(buf), "player%d.ai%d", plrno,
3309
3311 secfile_lookup_int_default(loading->file, 1, "%s.love", buf);
3312 CALL_FUNC_EACH_AI(player_load_relations, plr, aplayer, loading->file, plrno);
3314
3315 CALL_FUNC_EACH_AI(player_load, plr, loading->file, plrno);
3316
3317 /* Some sane defaults */
3318 plr->ai_common.fuzzy = 0;
3319 plr->ai_common.expand = 100;
3320 plr->ai_common.science_cost = 100;
3321
3322
3324 "player%d.ai.level", plrno);
3325 if (level != NULL) {
3326 if (!fc_strcasecmp("Handicapped", level)) {
3327 /* Up to freeciv-3.1 Restricted AI level was known as Handicapped */
3329 } else {
3331 }
3332 } else {
3334 }
3335
3340 "player%d.ai.skill_level",
3341 plrno));
3342 }
3343
3345 "player%d.ai.barb_type", plrno);
3347
3349 log_sg("Player%d: Invalid barbarian type \"%s\". "
3350 "Changed to \"None\".", plrno, barb_str);
3352 }
3353
3354 if (is_barbarian(plr)) {
3355 server.nbarbarians++;
3356 }
3357
3358 if (is_ai(plr)) {
3360 CALL_PLR_AI_FUNC(gained_control, plr, plr);
3361 }
3362
3363 /* Load nation style. */
3364 {
3365 struct nation_style *style;
3366
3367 str = secfile_lookup_str(loading->file, "player%d.style_by_name", plrno);
3368
3369 /* Handle pre-2.6 savegames */
3370 if (str == NULL) {
3371 str = secfile_lookup_str(loading->file, "player%d.city_style_by_name",
3372 plrno);
3373 }
3374
3375 sg_failure_ret(str != NULL, "%s", secfile_error());
3376 style = style_by_rule_name(str);
3377 if (style == NULL) {
3378 style = style_by_number(0);
3379 log_sg("Player%d: unsupported city_style_name \"%s\". "
3380 "Changed to \"%s\".", plrno, str, style_rule_name(style));
3381 }
3382 plr->style = style;
3383 }
3384
3386 "player%d.idle_turns", plrno),
3387 "%s", secfile_error());
3389 "player%d.is_male", plrno);
3391 "player%d.is_alive", plrno),
3392 "%s", secfile_error());
3394 "player%d.turns_alive", plrno),
3395 "%s", secfile_error());
3397 "player%d.last_war", plrno),
3398 "%s", secfile_error());
3400 "player%d.phase_done", plrno);
3402 "player%d.gold", plrno),
3403 "%s", secfile_error());
3405 "player%d.rates.tax", plrno),
3406 "%s", secfile_error());
3408 "player%d.rates.science", plrno),
3409 "%s", secfile_error());
3411 "player%d.rates.luxury", plrno),
3412 "%s", secfile_error());
3413 plr->server.bulbs_last_turn =
3415 "player%d.research.bulbs_last_turn", plrno);
3416
3417 /* Traits */
3418 if (plr->nation) {
3419 for (i = 0; i < loading->trait.size; i++) {
3420 enum trait tr = trait_by_name(loading->trait.order[i], fc_strcasecmp);
3421
3422 if (trait_is_valid(tr)) {
3423 int val = secfile_lookup_int_default(loading->file, -1, "player%d.trait%d.val",
3424 plrno, i);
3425
3426 if (val != -1) {
3427 plr->ai_common.traits[tr].val = val;
3428 }
3429
3431 "player%d.trait%d.mod", plrno, i),
3432 "%s", secfile_error());
3433 plr->ai_common.traits[tr].mod = val;
3434 }
3435 }
3436 }
3437
3438 /* Achievements */
3439 {
3440 int count;
3441
3442 count = secfile_lookup_int_default(loading->file, -1,
3443 "player%d.achievement_count", plrno);
3444
3445 if (count > 0) {
3446 for (i = 0; i < count; i++) {
3447 const char *name;
3448 struct achievement *pach;
3449 bool first;
3450
3452 "player%d.achievement%d.name", plrno, i);
3454
3456 "Unknown achievement \"%s\".", name);
3457
3459 "player%d.achievement%d.first",
3460 plrno, i),
3461 "achievement error: %s", secfile_error());
3462
3463 sg_failure_ret(pach->first == NULL || !first,
3464 "Multiple players listed as first to get achievement \"%s\".",
3465 name);
3466
3467 BV_SET(pach->achievers, player_index(plr));
3468
3469 if (first) {
3470 pach->first = plr;
3471 }
3472 }
3473 }
3474 }
3475
3476 /* Player score. */
3477 plr->score.happy =
3479 "score%d.happy", plrno);
3480 plr->score.content =
3482 "score%d.content", plrno);
3483 plr->score.unhappy =
3485 "score%d.unhappy", plrno);
3486 plr->score.angry =
3488 "score%d.angry", plrno);
3489
3490 /* Make sure that the score about specialists in current ruleset that
3491 * were not present at saving time are set to zero. */
3493 plr->score.specialists[sp] = 0;
3495
3496 for (i = 0; i < loading->specialist.size; i++) {
3497 plr->score.specialists[specialist_index(loading->specialist.order[i])]
3499 "score%d.specialists%d", plrno, i);
3500 }
3501
3502 plr->score.wonders =
3504 "score%d.wonders", plrno);
3505 plr->score.techs =
3507 "score%d.techs", plrno);
3508 plr->score.techout =
3510 "score%d.techout", plrno);
3511 plr->score.landarea =
3513 "score%d.landarea", plrno);
3514 plr->score.settledarea =
3516 "score%d.settledarea", plrno);
3517 plr->score.population =
3519 "score%d.population", plrno);
3520 plr->score.cities =
3522 "score%d.cities", plrno);
3523 plr->score.units =
3525 "score%d.units", plrno);
3526 plr->score.pollution =
3528 "score%d.pollution", plrno);
3529 plr->score.literacy =
3531 "score%d.literacy", plrno);
3532 plr->score.bnp =
3534 "score%d.bnp", plrno);
3535 plr->score.mfg =
3537 "score%d.mfg", plrno);
3538 plr->score.spaceship =
3540 "score%d.spaceship", plrno);
3541 plr->score.units_built =
3543 "score%d.units_built", plrno);
3544 plr->score.units_killed =
3546 "score%d.units_killed", plrno);
3547 plr->score.units_lost =
3549 "score%d.units_lost", plrno);
3550 plr->score.units_used = 0; /* Was never saved to savegame2.c saves */
3551 plr->score.culture =
3553 "score%d.culture", plrno);
3554 plr->score.game =
3556 "score%d.total", plrno);
3557
3558 /* Load space ship data. */
3559 {
3560 struct player_spaceship *ship = &plr->spaceship;
3561 char prefix[32];
3562 const char *st;
3563 int ei;
3564
3565 fc_snprintf(prefix, sizeof(prefix), "player%d.spaceship", plrno);
3568 &ei,
3569 "%s.state", prefix),
3570 "%s", secfile_error());
3571 ship->state = ei;
3572
3573 if (ship->state != SSHIP_NONE) {
3574 sg_failure_ret(secfile_lookup_int(loading->file, &ship->structurals,
3575 "%s.structurals", prefix),
3576 "%s", secfile_error());
3577 sg_failure_ret(secfile_lookup_int(loading->file, &ship->components,
3578 "%s.components", prefix),
3579 "%s", secfile_error());
3581 "%s.modules", prefix),
3582 "%s", secfile_error());
3584 "%s.fuel", prefix),
3585 "%s", secfile_error());
3586 sg_failure_ret(secfile_lookup_int(loading->file, &ship->propulsion,
3587 "%s.propulsion", prefix),
3588 "%s", secfile_error());
3589 sg_failure_ret(secfile_lookup_int(loading->file, &ship->habitation,
3590 "%s.habitation", prefix),
3591 "%s", secfile_error());
3592 sg_failure_ret(secfile_lookup_int(loading->file, &ship->life_support,
3593 "%s.life_support", prefix),
3594 "%s", secfile_error());
3595 sg_failure_ret(secfile_lookup_int(loading->file, &ship->solar_panels,
3596 "%s.solar_panels", prefix),
3597 "%s", secfile_error());
3598
3599 st = secfile_lookup_str(loading->file, "%s.structure", prefix);
3600 sg_failure_ret(st != NULL, "%s", secfile_error())
3601 for (i = 0; i < NUM_SS_STRUCTURALS && st[i]; i++) {
3602 sg_failure_ret(st[i] == '1' || st[i] == '0',
3603 "Undefined value '%c' within '%s.structure'.", st[i],
3604 prefix)
3605
3606 if (!(st[i] == '0')) {
3607 BV_SET(ship->structure, i);
3608 }
3609 }
3610 if (ship->state >= SSHIP_LAUNCHED) {
3611 sg_failure_ret(secfile_lookup_int(loading->file, &ship->launch_year,
3612 "%s.launch_year", prefix),
3613 "%s", secfile_error());
3614 }
3616 }
3617 }
3618
3619 /* Load lost wonder data. */
3620 str = secfile_lookup_str(loading->file, "player%d.lost_wonders", plrno);
3621 /* If not present, probably an old savegame; nothing to be done */
3622 if (str != NULL) {
3623 int k;
3624
3625 sg_failure_ret(strlen(str) == loading->improvement.size,
3626 "Invalid length for 'player%d.lost_wonders' ("
3627 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")",
3628 plrno, strlen(str), loading->improvement.size);
3629 for (k = 0; k < loading->improvement.size; k++) {
3630 sg_failure_ret(str[k] == '1' || str[k] == '0',
3631 "Undefined value '%c' within "
3632 "'player%d.lost_wonders'.", plrno, str[k]);
3633
3634 if (str[k] == '1') {
3635 struct impr_type *pimprove =
3636 improvement_by_rule_name(loading->improvement.order[k]);
3637
3638 if (pimprove) {
3639 plr->wonders[improvement_index(pimprove)] = WONDER_LOST;
3640 }
3641 }
3642 }
3643 }
3644
3645 plr->history =
3646 secfile_lookup_int_default(loading->file, 0, "player%d.culture", plrno);
3647 plr->server.huts =
3648 secfile_lookup_int_default(loading->file, 0, "player%d.hut_count", plrno);
3649}
3650
3651/************************************************************************/
3655 struct player *plr)
3656{
3657 int ncities, i, plrno = player_number(plr);
3658 bool tasks_handled;
3659 int wlist_max_length;
3660
3661 /* Check status and return if not OK (sg_success FALSE). */
3662 sg_check_ret();
3663
3665 "player%d.ncities", plrno),
3666 "%s", secfile_error());
3667
3668 if (!plr->is_alive && ncities > 0) {
3669 log_sg("'player%d.ncities' = %d for dead player!", plrno, ncities);
3670 ncities = 0;
3671 }
3672
3673 if (!player_has_flag(plr, PLRF_FIRST_CITY) && ncities > 0) {
3674 /* Probably barbarians in an old savegame; fix up */
3676 }
3677
3679 "player%d.wl_max_length",
3680 plrno);
3681
3682 /* Load all cities of the player. */
3683 for (i = 0; i < ncities; i++) {
3684 char buf[32];
3685 struct city *pcity;
3686
3687 fc_snprintf(buf, sizeof(buf), "player%d.c%d", plrno, i);
3688
3689 /* Create a dummy city. */
3695 sg_failure_ret(FALSE, "Error loading city %d of player %d.", i, plrno);
3696 }
3697
3700
3701 /* Load the information about the nationality of citizens. This is done
3702 * here because the city sanity check called by citizens_update() requires
3703 * that the city is registered. */
3705
3706 /* After everything is loaded, but before vision. */
3708
3709 /* adding the city contribution to fog-of-war */
3713
3715 }
3716
3718 for (i = 0; !tasks_handled; i++) {
3719 int city_id;
3720 struct city *pcity = NULL;
3721
3722 city_id = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.city",
3723 plrno, i);
3724
3725 if (city_id != -1) {
3726 pcity = player_city_by_number(plr, city_id);
3727 }
3728
3729 if (pcity != NULL) {
3730 const char *str;
3731 int nat_x, nat_y;
3732 struct worker_task *ptask = fc_malloc(sizeof(struct worker_task));
3733
3734 nat_x = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.x", plrno, i);
3735 nat_y = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.y", plrno, i);
3736
3737 ptask->ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
3738
3739 str = secfile_lookup_str(loading->file, "player%d.task%d.activity", plrno, i);
3741
3743 "Unknown workertask activity %s", str);
3744
3745 str = secfile_lookup_str(loading->file, "player%d.task%d.target", plrno, i);
3746
3747 if (strcmp("-", str)) {
3749
3750 sg_failure_ret(ptask->tgt != NULL,
3751 "Unknown workertask target %s", str);
3752 } else {
3753 ptask->tgt = NULL;
3754 }
3755
3756 ptask->want = secfile_lookup_int_default(loading->file, 1,
3757 "player%d.task%d.want", plrno, i);
3758
3759 worker_task_list_append(pcity->task_reqs, ptask);
3760 } else {
3762 }
3763 }
3764}
3765
3766/************************************************************************/
3769static bool sg_load_player_city(struct loaddata *loading, struct player *plr,
3770 struct city *pcity, const char *citystr,
3771 int wlist_max_length)
3772{
3773 struct player *past;
3774 const char *kind, *name, *str;
3775 int id, i, repair, sp_count = 0, workers = 0, value;
3776 int nat_x, nat_y;
3777 citizens size;
3778 const char *stylename;
3779 const struct civ_map *nmap = &(wld.map);
3780
3782 FALSE, "%s", secfile_error());
3784 FALSE, "%s", secfile_error());
3787 "%s has invalid center tile (%d, %d)",
3788 citystr, nat_x, nat_y);
3790 "%s duplicates city (%d, %d)", citystr, nat_x, nat_y);
3791
3792 /* Instead of dying, use 'citystr' string for damaged name. */
3794 "%s.name", citystr));
3795
3797 citystr), FALSE, "%s", secfile_error());
3798
3800 "%s.original", citystr);
3801 past = player_by_number(id);
3802 if (NULL != past) {
3803 pcity->original = past;
3804 }
3805
3806 /* savegame2 saves never had this information. Guess. */
3807 if (pcity->original != plr) {
3808 pcity->acquire_t = CACQ_CONQUEST;
3809 } else {
3810 pcity->acquire_t = CACQ_FOUNDED;
3811 }
3812
3813 sg_warn_ret_val(secfile_lookup_int(loading->file, &value, "%s.size",
3814 citystr), FALSE, "%s", secfile_error());
3815 size = (citizens)value; /* Set the correct type */
3816 sg_warn_ret_val(value == (int)size, FALSE,
3817 "Invalid city size: %d, set to %d", value, size);
3819
3820 for (i = 0; i < loading->specialist.size; i++) {
3821 sg_warn_ret_val(secfile_lookup_int(loading->file, &value, "%s.nspe%d",
3822 citystr, i),
3823 FALSE, "%s", secfile_error());
3824 pcity->specialists[specialist_index(loading->specialist.order[i])]
3825 = (citizens)value;
3826 sp_count += value;
3827 }
3828
3829 /* savegame2.c saves were ever saved with MAX_TRADE_ROUTES_OLD routes max */
3830 for (i = 0; i < MAX_TRADE_ROUTES_OLD; i++) {
3831 int partner = secfile_lookup_int_default(loading->file, 0,
3832 "%s.traderoute%d", citystr, i);
3833
3834 if (partner != 0) {
3835 struct trade_route *proute = fc_malloc(sizeof(struct trade_route));
3836
3837 proute->partner = partner;
3839 proute->goods = goods_by_number(0); /* First good */
3840
3842 }
3843 }
3844
3845 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->food_stock,
3846 "%s.food_stock", citystr),
3847 FALSE, "%s", secfile_error());
3848 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->shield_stock,
3849 "%s.shield_stock", citystr),
3850 FALSE, "%s", secfile_error());
3851 pcity->history =
3852 secfile_lookup_int_default(loading->file, 0, "%s.history", citystr);
3853
3854 pcity->airlift =
3855 secfile_lookup_int_default(loading->file, 0, "%s.airlift", citystr);
3856 pcity->was_happy =
3857 secfile_lookup_bool_default(loading->file, FALSE, "%s.was_happy",
3858 citystr);
3859 pcity->had_famine = FALSE;
3860
3861 pcity->turn_plague =
3862 secfile_lookup_int_default(loading->file, 0, "%s.turn_plague", citystr);
3863
3865 "%s.anarchy", citystr),
3866 FALSE, "%s", secfile_error());
3867 pcity->rapture =
3868 secfile_lookup_int_default(loading->file, 0, "%s.rapture", citystr);
3869 pcity->steal =
3870 secfile_lookup_int_default(loading->file, 0, "%s.steal", citystr);
3871
3872 /* Before did_buy for undocumented hack */
3873 pcity->turn_founded =
3874 secfile_lookup_int_default(loading->file, -2, "%s.turn_founded",
3875 citystr);
3876 sg_warn_ret_val(secfile_lookup_int(loading->file, &i, "%s.did_buy",
3877 citystr), FALSE, "%s", secfile_error());
3878 pcity->did_buy = (i != 0);
3879 if (i == -1 && pcity->turn_founded == -2) {
3880 /* Undocumented hack */
3881 pcity->turn_founded = game.info.turn;
3882 }
3883
3884 pcity->did_sell
3885 = secfile_lookup_bool_default(loading->file, FALSE, "%s.did_sell", citystr);
3886
3887 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->turn_last_built,
3888 "%s.turn_last_built", citystr),
3889 FALSE, "%s", secfile_error());
3890
3891 kind = secfile_lookup_str(loading->file, "%s.currently_building_kind",
3892 citystr);
3893 name = secfile_lookup_str(loading->file, "%s.currently_building_name",
3894 citystr);
3895 pcity->production = universal_by_rule_name(kind, name);
3896 sg_warn_ret_val(pcity->production.kind != universals_n_invalid(), FALSE,
3897 "%s.currently_building: unknown \"%s\" \"%s\".",
3898 citystr, kind, name);
3899
3900 kind = secfile_lookup_str(loading->file, "%s.changed_from_kind",
3901 citystr);
3902 name = secfile_lookup_str(loading->file, "%s.changed_from_name",
3903 citystr);
3906 "%s.changed_from: unknown \"%s\" \"%s\".",
3907 citystr, kind, name);
3908
3909 pcity->before_change_shields =
3910 secfile_lookup_int_default(loading->file, pcity->shield_stock,
3911 "%s.before_change_shields", citystr);
3912 pcity->caravan_shields =
3914 "%s.caravan_shields", citystr);
3915 pcity->disbanded_shields =
3917 "%s.disbanded_shields", citystr);
3918 pcity->last_turns_shield_surplus =
3920 "%s.last_turns_shield_surplus",
3921 citystr);
3922
3924 "%s.style", citystr);
3925 if (stylename != NULL) {
3927 } else {
3928 pcity->style = 0;
3929 }
3930 if (pcity->style < 0) {
3931 pcity->style = city_style(pcity);
3932 }
3933
3934 pcity->server.synced = FALSE; /* Must re-sync with clients */
3935
3936 /* Initialise list of city improvements. */
3937 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
3938 pcity->built[i].turn = I_NEVER;
3939 }
3940
3941 /* Load city improvements. */
3942 str = secfile_lookup_str(loading->file, "%s.improvements", citystr);
3944 sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE,
3945 "Invalid length of '%s.improvements' ("
3946 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
3947 citystr, strlen(str), loading->improvement.size);
3948 for (i = 0; i < loading->improvement.size; i++) {
3949 sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE,
3950 "Undefined value '%c' within '%s.improvements'.",
3951 str[i], citystr)
3952
3953 if (str[i] == '1') {
3954 struct impr_type *pimprove =
3955 improvement_by_rule_name(loading->improvement.order[i]);
3956
3957 if (pimprove) {
3958 city_add_improvement(pcity, pimprove);
3959 }
3960 }
3961 }
3962
3963 sg_failure_ret_val(loading->worked_tiles != NULL, FALSE,
3964 "No worked tiles map defined.");
3965
3967
3968 /* Load new savegame with variable (squared) city radius and worked
3969 * tiles map */
3970
3971 int radius_sq
3972 = secfile_lookup_int_default(loading->file, -1, "%s.city_radius_sq",
3973 citystr);
3974 city_map_radius_sq_set(pcity, radius_sq);
3975
3977 if (loading->worked_tiles[ptile->index] == pcity->id) {
3978 if (sq_map_distance(ptile, pcity->tile) > radius_sq) {
3979 log_sg("[%s] '%s' (%d, %d) has worker outside current radius "
3980 "at (%d, %d); repairing", citystr, city_name_get(pcity),
3981 TILE_XY(pcity->tile), TILE_XY(ptile));
3982 pcity->specialists[DEFAULT_SPECIALIST]++;
3983 sp_count++;
3984 } else {
3985 tile_set_worked(ptile, pcity);
3986 workers++;
3987 }
3988
3989#ifdef FREECIV_DEBUG
3990 /* Set this tile to unused; a check for not reset tiles is
3991 * included in game_load_internal() */
3992 loading->worked_tiles[ptile->index] = -1;
3993#endif /* FREECIV_DEBUG */
3994 }
3996
3997 if (tile_worked(city_tile(pcity)) != pcity) {
3998 struct city *pwork = tile_worked(city_tile(pcity));
3999
4000 if (NULL != pwork) {
4001 log_sg("[%s] city center of '%s' (%d,%d) [%d] is worked by '%s' "
4002 "(%d,%d) [%d]; repairing", citystr, city_name_get(pcity),
4005
4006 tile_set_worked(city_tile(pcity), NULL); /* remove tile from pwork */
4007 pwork->specialists[DEFAULT_SPECIALIST]++;
4009 } else {
4010 log_sg("[%s] city center of '%s' (%d,%d) [%d] is empty; repairing",
4013 }
4014
4015 /* repair pcity */
4018 }
4019
4021 if (0 != repair) {
4022 log_sg("[%s] size mismatch for '%s' (%d,%d): size [%d] != "
4023 "(workers [%d] - free worked tiles [%d]) + specialists [%d]",
4025 workers, FREE_WORKED_TILES, sp_count);
4026
4027 /* repair pcity */
4029 }
4030
4031 /* worklist_init() done in create_city_virtual() */
4032 worklist_load(loading->file, wlist_max_length, &pcity->worklist, "%s", citystr);
4033
4034 /* Load city options. */
4035 BV_CLR_ALL(pcity->city_options);
4036 for (i = 0; i < loading->coptions.size; i++) {
4037 if (secfile_lookup_bool_default(loading->file, FALSE, "%s.option%d",
4038 citystr, i)) {
4039 BV_SET(pcity->city_options, loading->coptions.order[i]);
4040 }
4041 }
4042 /* Was never stored to savegame2 saves */
4043 pcity->wlcb = WLCB_SMART;
4044
4045 CALL_FUNC_EACH_AI(city_load, loading->file, pcity, citystr);
4046
4047 return TRUE;
4048}
4049
4050/************************************************************************/
4054 struct player *plr,
4055 struct city *pcity,
4056 const char *citystr)
4057{
4059 citizens size;
4060
4062 player_slots_iterate(pslot) {
4063 int nationality;
4064
4066 "%s.citizen%d", citystr,
4067 player_slot_index(pslot));
4068 if (nationality > 0 && !player_slot_is_used(pslot)) {
4069 log_sg("Citizens of an invalid nation for %s (player slot %d)!",
4071 continue;
4072 }
4073
4074 if (nationality != -1 && player_slot_is_used(pslot)) {
4076 "Invalid value for citizens of player %d in %s: %d.",
4079 }
4081 /* Sanity check. */
4083 if (size != city_size_get(pcity)) {
4084 if (size != 0) {
4085 /* size == 0 can be result from the fact that ruleset had no
4086 * nationality enabled at saving time, so no citizens at all
4087 * were saved. But something more serious must be going on if
4088 * citizens have been saved partially - if some of them are there. */
4089 log_sg("City size and number of citizens does not match in %s "
4090 "(%d != %d)! Repairing ...", city_name_get(pcity),
4092 }
4094 }
4095 }
4096}
4097
4098/************************************************************************/
4102 struct player *plr)
4103{
4104 int nunits, i, plrno = player_number(plr);
4105
4106 /* Check status and return if not OK (sg_success FALSE). */
4107 sg_check_ret();
4108
4110 "player%d.nunits", plrno),
4111 "%s", secfile_error());
4112 if (!plr->is_alive && nunits > 0) {
4113 log_sg("'player%d.nunits' = %d for dead player!", plrno, nunits);
4114 nunits = 0; /* Some old savegames may be buggy. */
4115 }
4116
4117 for (i = 0; i < nunits; i++) {
4118 struct unit *punit;
4119 struct city *pcity;
4120 const char *name;
4121 char buf[32];
4122 struct unit_type *type;
4123 struct tile *ptile;
4124
4125 fc_snprintf(buf, sizeof(buf), "player%d.u%d", plrno, i);
4126
4127 name = secfile_lookup_str(loading->file, "%s.type_by_name", buf);
4129 sg_failure_ret(type != NULL, "%s: unknown unit type \"%s\".", buf, name);
4130
4131 /* Create a dummy unit. */
4132 punit = unit_virtual_create(plr, NULL, type, 0);
4133 if (!sg_load_player_unit(loading, plr, punit, buf)) {
4135 sg_failure_ret(FALSE, "Error loading unit %d of player %d.", i, plrno);
4136 }
4137
4140
4142 unit_list_prepend(pcity->units_supported, punit);
4143 } else if (punit->homecity > IDENTITY_NUMBER_ZERO) {
4144 log_sg("%s: bad home city %d.", buf, punit->homecity);
4146 }
4147
4148 ptile = unit_tile(punit);
4149
4150 /* allocate the unit's contribution to fog of war */
4153 /* NOTE: There used to be some map_set_known calls here. These were
4154 * unneeded since unfogging the tile when the unit sees it will
4155 * automatically reveal that tile. */
4156
4159
4160 /* Claim ownership of fortress? */
4161 if ((extra_owner(ptile) == NULL
4162 || pplayers_at_war(extra_owner(ptile), plr))
4164 tile_claim_bases(ptile, plr);
4165 }
4166 }
4167}
4168
4169/************************************************************************/
4179static int sg_order_to_action(int order, struct unit *act_unit,
4180 struct tile *tgt_tile)
4181{
4182 switch (order) {
4184 if (tile_city(tgt_tile)
4186 /* The player's cities are loaded right before their units. It wasn't
4187 * possible for rulesets to allow joining foreign cities before 3.0.
4188 * This means that a converted build city order only can be a Join
4189 * City order if it targets a domestic city. */
4190 return ACTION_JOIN_CITY;
4191 } else {
4192 /* Assume that the intention was to found a new city. */
4193 return ACTION_FOUND_CITY;
4194 }
4196 /* Maps one to one with each other. */
4197 return ACTION_HELP_WONDER;
4199 /* Maps one to one with each other. */
4200 return ACTION_TRADE_ROUTE;
4201 case ORDER_OLD_DISBAND:
4202 /* Added to the order system in the same commit as Help Wonder. Assume
4203 * that anyone that intended to order Help Wonder used Help Wonder. */
4204 /* Could in theory be intended as an order to disband in the field. Why
4205 * would the player give a unit an order to go to a non city location
4206 * and disband there? Assume the intention was to recover production
4207 * until a non recovering disband order is found. */
4209 case ORDER_OLD_HOMECITY:
4210 return ACTION_HOME_CITY;
4211 }
4212
4213 /* The order hasn't been replaced by an action. */
4214 return ACTION_NONE;
4215}
4216
4217/************************************************************************/
4221 struct player *plr, struct unit *punit,
4222 const char *unitstr)
4223{
4224 int activity;
4225 int nat_x, nat_y;
4226 enum tile_special_type target;
4227 struct extra_type *pextra = NULL;
4228 struct base_type *pbase = NULL;
4229 struct road_type *proad = NULL;
4230 struct tile *ptile;
4231 int extra_id;
4232 int base_id;
4233 int road_id;
4234 int ei;
4235 const char *facing_str;
4237 int natnbr;
4238 bool ai_controlled;
4239
4241 unitstr), FALSE, "%s", secfile_error());
4243 FALSE, "%s", secfile_error());
4245 FALSE, "%s", secfile_error());
4246
4247 ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
4248 sg_warn_ret_val(NULL != ptile, FALSE, "%s invalid tile (%d, %d)",
4249 unitstr, nat_x, nat_y);
4250 unit_tile_set(punit, ptile);
4251
4254 "%s.facing", unitstr);
4255 if (facing_str[0] != 'x') {
4256 /* We don't touch punit->facing if savegame does not contain that
4257 * information. Initial orientation set by unit_virtual_create()
4258 * is as good as any. */
4259 enum direction8 facing = char2dir(facing_str[0]);
4260
4261 if (direction8_is_valid(facing)) {
4262 punit->facing = facing;
4263 } else {
4264 log_error("Illegal unit orientation '%s'", facing_str);
4265 }
4266 }
4267
4268 /* If savegame has unit nationality, it doesn't hurt to
4269 * internally set it even if nationality rules are disabled. */
4271 player_number(plr),
4272 "%s.nationality", unitstr);
4273
4275 if (punit->nationality == NULL) {
4276 punit->nationality = plr;
4277 }
4278
4280 "%s.homecity", unitstr), FALSE,
4281 "%s", secfile_error());
4283 "%s.moves", unitstr), FALSE,
4284 "%s", secfile_error());
4286 "%s.fuel", unitstr), FALSE,
4287 "%s", secfile_error());
4289 "%s.activity", unitstr), FALSE,
4290 "%s", secfile_error());
4291 activity = unit_activity_by_name(loading->activities.order[ei],
4293
4296 "%s.born", unitstr);
4298
4300 "%s.activity_tgt", unitstr);
4301
4302 if (extra_id != -2) {
4303 if (extra_id >= 0 && extra_id < loading->extra.size) {
4304 pextra = loading->extra.order[extra_id];
4305 set_unit_activity_targeted(punit, activity, pextra,
4306 activity_default_action(activity));
4307 } else if (activity == ACTIVITY_IRRIGATE) {
4311 punit);
4312 if (tgt != NULL) {
4315 } else {
4318 }
4319 } else if (activity == ACTIVITY_MINE) {
4321 EC_MINE,
4323 punit);
4324 if (tgt != NULL) {
4327 } else {
4330 }
4331 } else {
4332 set_unit_activity(punit, activity,
4333 activity_default_action(activity));
4334 }
4335 } else {
4336 /* extra_id == -2 -> activity_tgt not set */
4338 "%s.activity_base", unitstr);
4339 if (base_id >= 0 && base_id < loading->base.size) {
4340 pbase = loading->base.order[base_id];
4341 }
4343 "%s.activity_road", unitstr);
4344 if (road_id >= 0 && road_id < loading->road.size) {
4345 proad = loading->road.order[road_id];
4346 }
4347
4348 {
4350 loading->special.size /* S_LAST */,
4351 "%s.activity_target", unitstr);
4352 if (tgt_no >= 0 && tgt_no < loading->special.size) {
4353 target = loading->special.order[tgt_no];
4354 } else {
4355 target = S_LAST;
4356 }
4357 }
4358
4359 if (target == S_OLD_ROAD) {
4360 target = S_LAST;
4362 } else if (target == S_OLD_RAILROAD) {
4363 target = S_LAST;
4365 }
4366
4367 if (activity == ACTIVITY_OLD_ROAD) {
4368 activity = ACTIVITY_GEN_ROAD;
4370 } else if (activity == ACTIVITY_OLD_RAILROAD) {
4371 activity = ACTIVITY_GEN_ROAD;
4373 }
4374
4375 /* We need changed_from == ACTIVITY_IDLE by now so that
4376 * set_unit_activity() and friends don't spuriously restore activity
4377 * points -- unit should have been created this way */
4379
4380 if (activity == ACTIVITY_BASE) {
4381 if (pbase) {
4383 } else {
4384 log_sg("Cannot find base %d for %s to build",
4388 }
4389 } else if (activity == ACTIVITY_GEN_ROAD) {
4390 if (proad) {
4392 } else {
4393 log_sg("Cannot find road %d for %s to build",
4397 }
4398 } else if (activity == ACTIVITY_PILLAGE) {
4399 struct extra_type *a_target;
4400
4401 if (target != S_LAST) {
4402 a_target = special_extra_get(target);
4403 } else if (pbase != NULL) {
4405 } else if (proad != NULL) {
4407 } else {
4408 a_target = NULL;
4409 }
4410 /* An out-of-range base number is seen with old savegames. We take
4411 * it as indicating undirected pillaging. We will assign pillage
4412 * targets before play starts. */
4414 activity_default_action(activity));
4415 } else if (activity == ACTIVITY_IRRIGATE) {
4419 punit);
4420 if (tgt != NULL) {
4423 } else {
4426 }
4427 } else if (activity == ACTIVITY_MINE) {
4429 EC_MINE,
4431 punit);
4432 if (tgt != NULL) {
4435 } else {
4438 }
4439 } else if (activity == ACTIVITY_OLD_POLLUTION_SG2
4440 || activity == ACTIVITY_OLD_FALLOUT_SG2) {
4442 ERM_CLEAN,
4444 punit);
4445 if (tgt != NULL) {
4448 } else {
4451 }
4452 } else {
4454 activity_default_action(activity));
4455 }
4456 } /* activity_tgt == NULL */
4457
4459 "%s.activity_count", unitstr), FALSE,
4460 "%s", secfile_error());
4461
4464 "%s.changed_from", unitstr);
4465
4467 "%s.changed_from_tgt", unitstr);
4468
4469 if (extra_id != -2) {
4470 if (extra_id >= 0 && extra_id < loading->extra.size) {
4471 punit->changed_from_target = loading->extra.order[extra_id];
4472 } else {
4474 }
4475 } else {
4476 /* extra_id == -2 -> changed_from_tgt not set */
4477
4478 cfspe =
4480 "%s.changed_from_target", unitstr);
4481 base_id =
4483 "%s.changed_from_base", unitstr);
4484 road_id =
4486 "%s.changed_from_road", unitstr);
4487
4488 if (road_id == -1) {
4489 if (cfspe == S_OLD_ROAD) {
4491 if (proad) {
4493 }
4494 } else if (cfspe == S_OLD_RAILROAD) {
4496 if (proad) {
4498 }
4499 }
4500 }
4501
4502 if (base_id >= 0 && base_id < loading->base.size) {
4504 } else if (road_id >= 0 && road_id < loading->road.size) {
4506 } else if (cfspe != S_LAST) {
4508 } else {
4510 }
4511
4516 punit);
4517 if (tgt != NULL) {
4519 } else {
4521 }
4522 } else if (punit->changed_from == ACTIVITY_MINE) {
4524 EC_MINE,
4526 punit);
4527 if (tgt != NULL) {
4529 } else {
4531 }
4535 ERM_CLEAN,
4537 punit);
4538 if (tgt != NULL) {
4540 } else {
4542 }
4543 }
4544 }
4545
4548 "%s.changed_from_count", unitstr);
4549
4550 /* Special case: for a long time, we accidentally incremented
4551 * activity_count while a unit was sentried, so it could increase
4552 * without bound (bug #20641) and be saved in old savefiles.
4553 * We zero it to prevent potential trouble overflowing the range
4554 * in network packets, etc. */
4555 if (activity == ACTIVITY_SENTRY) {
4556 punit->activity_count = 0;
4557 }
4560 }
4561
4562 punit->veteran
4563 = secfile_lookup_int_default(loading->file, 0, "%s.veteran", unitstr);
4564 {
4565 /* Protect against change in veteran system in ruleset */
4566 const int levels = utype_veteran_levels(unit_type_get(punit));
4567 if (punit->veteran >= levels) {
4568 fc_assert(levels >= 1);
4569 punit->veteran = levels - 1;
4570 }
4571 }
4574 "%s.done_moving", unitstr);
4577 "%s.battlegroup", unitstr);
4578
4580 "%s.go", unitstr)) {
4581 int gnat_x, gnat_y;
4582
4584 "%s.goto_x", unitstr), FALSE,
4585 "%s", secfile_error());
4587 "%s.goto_y", unitstr), FALSE,
4588 "%s", secfile_error());
4589
4591 } else {
4592 punit->goto_tile = NULL;
4593
4594 /* These variables are not used but needed for saving the unit table.
4595 * Load them to prevent unused variables errors. */
4596 (void) secfile_entry_lookup(loading->file, "%s.goto_x", unitstr);
4597 (void) secfile_entry_lookup(loading->file, "%s.goto_y", unitstr);
4598 }
4599
4600 /* Load AI data of the unit. */
4601 CALL_FUNC_EACH_AI(unit_load, loading->file, punit, unitstr);
4602
4605 "%s.ai", unitstr), FALSE,
4606 "%s", secfile_error());
4607 if (ai_controlled) {
4608 /* Autoworker and Autoexplore are separated by
4609 * compat_post_load_030100() when set to SSA_AUTOWORKER */
4611 } else {
4613 }
4615 "%s.hp", unitstr), FALSE,
4616 "%s", secfile_error());
4617
4619 = secfile_lookup_int_default(loading->file, 0, "%s.ord_map", unitstr);
4621 = secfile_lookup_int_default(loading->file, 0, "%s.ord_city", unitstr);
4622 punit->moved
4623 = secfile_lookup_bool_default(loading->file, FALSE, "%s.moved", unitstr);
4626 "%s.paradropped", unitstr);
4627
4628 /* The transport status (punit->transported_by) is loaded in
4629 * sg_player_units_transport(). */
4630
4631 /* Initialize upkeep values: these are hopefully initialized
4632 * elsewhere before use (specifically, in city_support(); but
4633 * fixme: check whether always correctly initialized?).
4634 * Below is mainly for units which don't have homecity --
4635 * otherwise these don't get initialized (and AI calculations
4636 * etc may use junk values). */
4640
4644 "%s.action_decision_want", unitstr);
4645
4647 /* Load the tile to act against. */
4648 int adwt_x, adwt_y;
4649
4650 if (secfile_lookup_int(loading->file, &adwt_x,
4651 "%s.action_decision_tile_x", unitstr)
4653 "%s.action_decision_tile_y", unitstr)) {
4655 adwt_x, adwt_y);
4656 } else {
4659 log_sg("Bad action_decision_tile for unit %d", punit->id);
4660 }
4661 } else {
4662 (void) secfile_entry_lookup(loading->file, "%s.action_decision_tile_x", unitstr);
4663 (void) secfile_entry_lookup(loading->file, "%s.action_decision_tile_y", unitstr);
4665 }
4666
4667 /* Load the unit orders */
4668 {
4669 int len = secfile_lookup_int_default(loading->file, 0,
4670 "%s.orders_length", unitstr);
4671
4672 if (len > 0) {
4673 const char *orders_unitstr, *dir_unitstr, *act_unitstr;
4674 const char *tgt_unitstr;
4675 const char *base_unitstr = NULL;
4676 const char *road_unitstr = NULL;
4679 int j;
4680
4681 punit->orders.list = fc_malloc(len * sizeof(*(punit->orders.list)));
4685 "%s.orders_index", unitstr);
4688 "%s.orders_repeat", unitstr);
4691 "%s.orders_vigilant", unitstr);
4692
4695 "%s.orders_list", unitstr);
4698 "%s.dir_list", unitstr);
4701 "%s.activity_list", unitstr);
4703 = secfile_lookup_str_default(loading->file, NULL, "%s.tgt_list", unitstr);
4704
4705 if (tgt_unitstr == NULL) {
4707 = secfile_lookup_str(loading->file, "%s.base_list", unitstr);
4709 = secfile_lookup_str_default(loading->file, NULL, "%s.road_list", unitstr);
4710 }
4711
4713
4714 for (j = 0; j < len; j++) {
4715 struct unit_order *order = &punit->orders.list[j];
4716
4717 if (orders_unitstr[j] == '\0' || dir_unitstr[j] == '\0'
4718 || act_unitstr[j] == '\0') {
4719 log_sg("Invalid unit orders.");
4721 break;
4722 }
4723 order->order = char2order(orders_unitstr[j]);
4724 order->dir = char2dir(dir_unitstr[j]);
4725 order->activity = char2activity(act_unitstr[j]);
4726 /* Target, if needed, is set in compat_post_load_030100() */
4727 order->target = NO_TARGET;
4728 order->sub_target = NO_TARGET;
4729
4730 if (order->order == ORDER_LAST
4731 || (order->order == ORDER_MOVE && !direction8_is_valid(order->dir))
4732 || (order->order == ORDER_ACTION_MOVE
4733 && !direction8_is_valid(order->dir))
4734 || (order->order == ORDER_ACTIVITY
4735 && order->activity == ACTIVITY_LAST)) {
4736 /* An invalid order. Just drop the orders for this unit. */
4738 punit->orders.list = NULL;
4740 break;
4741 }
4742
4743 /* The order may have been replaced by the perform action order */
4744 order->action = sg_order_to_action(order->order, punit,
4745 punit->goto_tile);
4746 if (order->action != ACTION_NONE) {
4747 /* The order was converted by sg_order_to_action() */
4748 order->order = ORDER_PERFORM_ACTION;
4749 }
4750
4751 if (tgt_unitstr) {
4752 if (tgt_unitstr[j] != '?') {
4754
4755 if (extra_id < 0 || extra_id >= loading->extra.size) {
4756 log_sg("Cannot find extra %d for %s to build",
4758 order->sub_target = EXTRA_NONE;
4759 } else {
4760 order->sub_target = extra_id;
4761 }
4762 } else {
4763 order->sub_target = EXTRA_NONE;
4764 }
4765 } else {
4766 /* In pre-2.6 savegames, base_list and road_list were only saved
4767 * for those activities (and not e.g. pillaging) */
4768 if (base_unitstr && base_unitstr[j] != '?'
4769 && order->activity == ACTIVITY_BASE) {
4771
4772 if (base_id < 0 || base_id >= loading->base.size) {
4773 log_sg("Cannot find base %d for %s to build",
4776 NULL, NULL));
4777 }
4778
4779 order->sub_target
4781 } else if (road_unitstr && road_unitstr[j] != '?'
4782 && order->activity == ACTIVITY_GEN_ROAD) {
4784
4785 if (road_id < 0 || road_id >= loading->road.size) {
4786 log_sg("Cannot find road %d for %s to build",
4788 road_id = 0;
4789 }
4790
4791 order->sub_target
4793 } else {
4794 order->sub_target = EXTRA_NONE;
4795 }
4796
4797 if (order->activity == ACTIVITY_OLD_ROAD) {
4798 order->activity = ACTIVITY_GEN_ROAD;
4799 order->sub_target
4801 } else if (order->activity == ACTIVITY_OLD_RAILROAD) {
4802 order->activity = ACTIVITY_GEN_ROAD;
4803 order->sub_target
4805 }
4806 }
4807 }
4808 } else {
4810 punit->orders.list = NULL;
4811
4812 (void) secfile_entry_lookup(loading->file, "%s.orders_index", unitstr);
4813 (void) secfile_entry_lookup(loading->file, "%s.orders_repeat", unitstr);
4814 (void) secfile_entry_lookup(loading->file, "%s.orders_vigilant", unitstr);
4815 (void) secfile_entry_lookup(loading->file, "%s.orders_list", unitstr);
4816 (void) secfile_entry_lookup(loading->file, "%s.dir_list", unitstr);
4817 (void) secfile_entry_lookup(loading->file, "%s.activity_list", unitstr);
4818 (void) secfile_entry_lookup(loading->file, "%s.tgt_list", unitstr);
4819 }
4820 }
4821
4822 return TRUE;
4823}
4824
4825/************************************************************************/
4830 struct player *plr)
4831{
4832 int nunits, i, plrno = player_number(plr);
4833
4834 /* Check status and return if not OK (sg_success FALSE). */
4835 sg_check_ret();
4836
4837 /* Recheck the number of units for the player. This is a copied from
4838 * sg_load_player_units(). */
4840 "player%d.nunits", plrno),
4841 "%s", secfile_error());
4842 if (!plr->is_alive && nunits > 0) {
4843 log_sg("'player%d.nunits' = %d for dead player!", plrno, nunits);
4844 nunits = 0; /* Some old savegames may be buggy. */
4845 }
4846
4847 for (i = 0; i < nunits; i++) {
4848 int id_unit, id_trans;
4849 struct unit *punit, *ptrans;
4850
4852 "player%d.u%d.id",
4853 plrno, i);
4855 fc_assert_action(punit != NULL, continue);
4856
4858 "player%d.u%d.transported_by",
4859 plrno, i);
4860 if (id_trans == -1) {
4861 /* Not transported. */
4862 continue;
4863 }
4864
4866 fc_assert_action(id_trans == -1 || ptrans != NULL, continue);
4867
4868 if (ptrans) {
4869#ifndef FREECIV_NDEBUG
4870 bool load_success =
4871#endif
4873
4874 fc_assert_action(load_success, continue);
4875 }
4876 }
4877}
4878
4879/************************************************************************/
4883 struct player *plr)
4884{
4885 int plrno = player_number(plr);
4886
4887 /* Check status and return if not OK (sg_success FALSE). */
4888 sg_check_ret();
4889
4890 /* Toss any existing attribute_block (should not exist) */
4891 if (plr->attribute_block.data) {
4893 plr->attribute_block.data = NULL;
4894 }
4895
4896 /* This is a big heap of opaque data for the client, check everything! */
4898 loading->file, 0, "player%d.attribute_v2_block_length", plrno);
4899
4900 if (0 > plr->attribute_block.length) {
4901 log_sg("player%d.attribute_v2_block_length=%d too small", plrno,
4902 plr->attribute_block.length);
4903 plr->attribute_block.length = 0;
4904 } else if (MAX_ATTRIBUTE_BLOCK < plr->attribute_block.length) {
4905 log_sg("player%d.attribute_v2_block_length=%d too big (max %d)",
4907 plr->attribute_block.length = 0;
4908 } else if (0 < plr->attribute_block.length) {
4909 int part_nr, parts;
4910 int quoted_length;
4911 char *quoted;
4912#ifndef FREECIV_NDEBUG
4913 size_t actual_length;
4914#endif
4915
4918 "player%d.attribute_v2_block_length_quoted",
4919 plrno), "%s", secfile_error());
4922 "player%d.attribute_v2_block_parts", plrno),
4923 "%s", secfile_error());
4924
4926 quoted[0] = '\0';
4928 for (part_nr = 0; part_nr < parts; part_nr++) {
4929 const char *current =
4931 "player%d.attribute_v2_block_data.part%d",
4932 plrno, part_nr);
4933 if (!current) {
4934 log_sg("attribute_v2_block_parts=%d actual=%d", parts, part_nr);
4935 break;
4936 }
4937 log_debug("attribute_v2_block_length_quoted=%d"
4938 " have=" SIZE_T_PRINTF " part=" SIZE_T_PRINTF,
4939 quoted_length, strlen(quoted), strlen(current));
4940 fc_assert(strlen(quoted) + strlen(current) <= quoted_length);
4941 strcat(quoted, current);
4942 }
4944 "attribute_v2_block_length_quoted=%d"
4945 " actual=" SIZE_T_PRINTF,
4947
4948#ifndef FREECIV_NDEBUG
4950#endif
4952 plr->attribute_block.data,
4953 plr->attribute_block.length);
4955 free(quoted);
4956 }
4957}
4958
4959/************************************************************************/
4963 struct player *plr)
4964{
4965 int plrno = player_number(plr);
4966 int total_ncities =
4968 "player%d.dc_total", plrno);
4969 int i;
4970 bool someone_alive = FALSE;
4971
4972 /* Check status and return if not OK (sg_success FALSE). */
4973 sg_check_ret();
4974
4977 if (pteam_member->is_alive) {
4979 break;
4980 }
4982
4983 if (!someone_alive) {
4984 /* Reveal all for completely dead teams. */
4986 }
4987 }
4988
4989 if (!plr->is_alive
4990 || -1 == total_ncities
4991 || !game.info.fogofwar
4993 "game.save_private_map")) {
4994 /* We have:
4995 * - a dead player;
4996 * - fogged cities are not saved for any reason;
4997 * - a savegame with fog of war turned off;
4998 * - or game.save_private_map is not set to FALSE in the scenario /
4999 * savegame. The players private knowledge is set to be what they could
5000 * see without fog of war. */
5001 whole_map_iterate(&(wld.map), ptile) {
5002 if (map_is_known(ptile, plr)) {
5003 struct city *pcity = tile_city(ptile);
5004
5005 update_player_tile_last_seen(plr, ptile);
5006 update_player_tile_knowledge(plr, ptile);
5007
5008 if (NULL != pcity) {
5009 update_dumb_city(plr, pcity);
5010 }
5011 }
5013
5014 /* Nothing more to do; */
5015 return;
5016 }
5017
5018 /* Load player map (terrain). */
5019 LOAD_MAP_CHAR(ch, ptile,
5020 map_get_player_tile(ptile, plr)->terrain
5021 = char2terrain(ch), loading->file,
5022 "player%d.map_t%04d", plrno);
5023
5024 /* Load player map (resources). */
5025 LOAD_MAP_CHAR(ch, ptile,
5026 map_get_player_tile(ptile, plr)->resource
5027 = char2resource(ch), loading->file,
5028 "player%d.map_res%04d", plrno);
5029
5030 if (loading->version >= 30) {
5031 /* 2.6.0 or newer */
5032
5033 /* Load player map (extras). */
5034 halfbyte_iterate_extras(j, loading->extra.size) {
5035 LOAD_MAP_CHAR(ch, ptile,
5037 ch, loading->extra.order + 4 * j),
5038 loading->file, "player%d.map_e%02d_%04d", plrno, j);
5040 } else {
5041 /* Load player map (specials). */
5042 halfbyte_iterate_special(j, loading->special.size) {
5043 LOAD_MAP_CHAR(ch, ptile,
5044 sg_special_set_dbv(ptile,
5045 &(map_get_player_tile(ptile, plr)->extras),
5046 ch, loading->special.order + 4 * j, FALSE),
5047 loading->file, "player%d.map_spe%02d_%04d", plrno, j);
5049
5050 /* Load player map (bases). */
5051 halfbyte_iterate_bases(j, loading->base.size) {
5052 LOAD_MAP_CHAR(ch, ptile,
5054 ch, loading->base.order + 4 * j),
5055 loading->file, "player%d.map_b%02d_%04d", plrno, j);
5057
5058 /* Load player map (roads). */
5059 if (loading->version >= 20) {
5060 /* 2.5.0 or newer */
5061 halfbyte_iterate_roads(j, loading->road.size) {
5062 LOAD_MAP_CHAR(ch, ptile,
5064 ch, loading->road.order + 4 * j),
5065 loading->file, "player%d.map_r%02d_%04d", plrno, j);
5067 }
5068 }
5069
5071 /* Load player map (border). */
5072 int x, y;
5073
5074 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
5075 const char *buffer
5076 = secfile_lookup_str(loading->file, "player%d.map_owner%04d",
5077 plrno, y);
5078 const char *buffer2
5079 = secfile_lookup_str(loading->file, "player%d.extras_owner%04d",
5080 plrno, y);
5081 const char *ptr = buffer;
5082 const char *ptr2 = buffer2;
5083
5084 sg_failure_ret(NULL != buffer,
5085 "Savegame corrupt - map line %d not found.", y);
5086 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
5087 char token[TOKEN_SIZE];
5088 char token2[TOKEN_SIZE];
5089 int number;
5090 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
5091
5092 scanin(&ptr, ",", token, sizeof(token));
5093 sg_failure_ret('\0' != token[0],
5094 "Savegame corrupt - map size not correct.");
5095 if (strcmp(token, "-") == 0) {
5096 map_get_player_tile(ptile, plr)->owner = NULL;
5097 } else {
5098 sg_failure_ret(str_to_int(token, &number),
5099 "Savegame corrupt - got tile owner=%s in (%d, %d).",
5100 token, x, y);
5101 map_get_player_tile(ptile, plr)->owner = player_by_number(number);
5102 }
5103
5104 if (loading->version >= 30) {
5105 scanin(&ptr2, ",", token2, sizeof(token2));
5106 sg_failure_ret('\0' != token2[0],
5107 "Savegame corrupt - map size not correct.");
5108 if (strcmp(token2, "-") == 0) {
5109 map_get_player_tile(ptile, plr)->extras_owner = NULL;
5110 } else {
5112 "Savegame corrupt - got extras owner=%s in (%d, %d).",
5113 token, x, y);
5114 map_get_player_tile(ptile, plr)->extras_owner = player_by_number(number);
5115 }
5116 } else {
5118 = map_get_player_tile(ptile, plr)->owner;
5119 }
5120 }
5121 }
5122 }
5123
5124 /* Load player map (update time). */
5125 for (i = 0; i < 4; i++) {
5126 /* put 4-bit segments of 16-bit "updated" field */
5127 if (i == 0) {
5128 LOAD_MAP_CHAR(ch, ptile,
5129 map_get_player_tile(ptile, plr)->last_updated
5130 = ascii_hex2bin(ch, i),
5131 loading->file, "player%d.map_u%02d_%04d", plrno, i);
5132 } else {
5133 LOAD_MAP_CHAR(ch, ptile,
5134 map_get_player_tile(ptile, plr)->last_updated
5135 |= ascii_hex2bin(ch, i),
5136 loading->file, "player%d.map_u%02d_%04d", plrno, i);
5137 }
5138 }
5139
5140 /* Load player map known cities. */
5141 for (i = 0; i < total_ncities; i++) {
5142 struct vision_site *pdcity;
5143 char buf[32];
5144 fc_snprintf(buf, sizeof(buf), "player%d.dc%d", plrno, i);
5145
5149 pdcity);
5151 } else {
5152 /* Error loading the data. */
5153 log_sg("Skipping seen city %d for player %d.", i, plrno);
5154 if (pdcity != NULL) {
5156 }
5157 }
5158 }
5159
5160 /* Repair inconsistent player maps. */
5161 whole_map_iterate(&(wld.map), ptile) {
5162 if (map_is_known_and_seen(ptile, plr, V_MAIN)) {
5163 struct city *pcity = tile_city(ptile);
5164
5165 update_player_tile_knowledge(plr, ptile);
5166 reality_check_city(plr, ptile);
5167
5168 if (NULL != pcity) {
5169 update_dumb_city(plr, pcity);
5170 }
5171 } else if (!game.server.foggedborders && map_is_known(ptile, plr)) {
5172 /* Non fogged borders aren't loaded. See hrm Bug #879084 */
5173 struct player_tile *plrtile = map_get_player_tile(ptile, plr);
5174
5175 plrtile->owner = tile_owner(ptile);
5176 }
5178}
5179
5180/************************************************************************/
5184 struct player *plr,
5185 struct vision_site *pdcity,
5186 const char *citystr)
5187{
5188 const char *str;
5189 int i, id, size;
5190 citizens city_size;
5191 int nat_x, nat_y;
5192 const char *stylename;
5193 const char *vname;
5194
5196 citystr),
5197 FALSE, "%s", secfile_error());
5199 citystr),
5200 FALSE, "%s", secfile_error());
5201 pdcity->location = native_pos_to_tile(&(wld.map), nat_x, nat_y);
5202 sg_warn_ret_val(NULL != pdcity->location, FALSE,
5203 "%s invalid tile (%d,%d)", citystr, nat_x, nat_y);
5204
5205 sg_warn_ret_val(secfile_lookup_int(loading->file, &id, "%s.owner",
5206 citystr),
5207 FALSE, "%s", secfile_error());
5208 pdcity->owner = player_by_number(id);
5209 sg_warn_ret_val(NULL != pdcity->owner, FALSE,
5210 "%s has invalid owner (%d); skipping.", citystr, id);
5211
5213 "%s.id", citystr),
5214 FALSE, "%s", secfile_error());
5216 "%s has invalid id (%d); skipping.", citystr, id);
5217
5219 "%s.size", citystr),
5220 FALSE, "%s", secfile_error());
5221 city_size = (citizens)size; /* set the correct type */
5222 sg_warn_ret_val(size == (int)city_size, FALSE,
5223 "Invalid city size: %d; set to %d.", size, city_size);
5224 vision_site_size_set(pdcity, city_size);
5225
5226 /* Initialise list of improvements */
5227 BV_CLR_ALL(pdcity->improvements);
5228 str = secfile_lookup_str(loading->file, "%s.improvements", citystr);
5230 sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE,
5231 "Invalid length of '%s.improvements' ("
5232 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
5233 citystr, strlen(str), loading->improvement.size);
5234 for (i = 0; i < loading->improvement.size; i++) {
5235 sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE,
5236 "Undefined value '%c' within '%s.improvements'.",
5237 str[i], citystr)
5238
5239 if (str[i] == '1') {
5240 struct impr_type *pimprove =
5241 improvement_by_rule_name(loading->improvement.order[i]);
5242
5243 if (pimprove) {
5244 BV_SET(pdcity->improvements, improvement_index(pimprove));
5245 }
5246 }
5247 }
5248
5250 "%s.name", citystr);
5251
5252 if (vname != NULL) {
5253 pdcity->name = fc_strdup(vname);
5254 }
5255
5257 "%s.occupied", citystr);
5259 "%s.walls", citystr);
5261 "%s.happy", citystr);
5263 "%s.unhappy", citystr);
5265 "%s.style", citystr);
5266 if (stylename != NULL) {
5268 } else {
5269 pdcity->style = 0;
5270 }
5271 if (pdcity->style < 0) {
5272 pdcity->style = 0;
5273 }
5274
5275 pdcity->city_image = secfile_lookup_int_default(loading->file, -100,
5276 "%s.city_image", citystr);
5277
5278 pdcity->capital = CAPITAL_NOT;
5279
5280 return TRUE;
5281}
5282
5283/* =======================================================================
5284 * Load the researches.
5285 * ======================================================================= */
5286
5287/************************************************************************/
5291{
5292 struct research *presearch;
5293 int count;
5294 int number;
5295 const char *str;
5296 int i, j;
5297 bool got_tech;
5298
5299 /* Check status and return if not OK (sg_success FALSE). */
5300 sg_check_ret();
5301
5302 /* Initialize all researches. */
5306
5307 /* May be unsaved (e.g. scenario case). */
5308 count = secfile_lookup_int_default(loading->file, 0, "research.count");
5309 for (i = 0; i < count; i++) {
5311 "research.r%d.number", i),
5312 "%s", secfile_error());
5313 presearch = research_by_number(number);
5315 "Invalid research number %d in 'research.r%d.number'",
5316 number, i);
5317
5318 presearch->tech_goal = technology_load(loading->file,
5319 "research.r%d.goal", i);
5321 &presearch->future_tech,
5322 "research.r%d.futuretech", i),
5323 "%s", secfile_error());
5325 &presearch->bulbs_researched,
5326 "research.r%d.bulbs", i),
5327 "%s", secfile_error());
5329 &presearch->bulbs_researching_saved,
5330 "research.r%d.bulbs_before", i),
5331 "%s", secfile_error());
5332 presearch->researching_saved = technology_load(loading->file,
5333 "research.r%d.saved", i);
5334 presearch->researching = technology_load(loading->file,
5335 "research.r%d.now", i);
5337 &got_tech,
5338 "research.r%d.got_tech", i),
5339 "%s", secfile_error());
5340 if (got_tech) {
5341 presearch->free_bulbs = presearch->bulbs_researched;
5342 }
5343
5344 str = secfile_lookup_str(loading->file, "research.r%d.done", i);
5345 sg_failure_ret(str != NULL, "%s", secfile_error());
5346 sg_failure_ret(strlen(str) == loading->technology.size,
5347 "Invalid length of 'research.r%d.done' ("
5348 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
5349 i, strlen(str), loading->technology.size);
5350 for (j = 0; j < loading->technology.size; j++) {
5351 sg_failure_ret(str[j] == '1' || str[j] == '0',
5352 "Undefined value '%c' within 'research.r%d.done'.",
5353 str[j], i);
5354
5355 if (str[j] == '1') {
5356 struct advance *padvance =
5357 advance_by_rule_name(loading->technology.order[j]);
5358
5359 if (padvance) {
5361 TECH_KNOWN);
5362 }
5363 }
5364 }
5365 }
5366
5367 /* In case of tech_leakage, we can update research only after all the
5368 * researches have been loaded */
5372}
5373
5374/* =======================================================================
5375 * Load the event cache. Should be the last thing to do.
5376 * ======================================================================= */
5377
5378/************************************************************************/
5382{
5383 /* Check status and return if not OK (sg_success FALSE). */
5384 sg_check_ret();
5385
5386 event_cache_load(loading->file, "event_cache");
5387}
5388
5389/* =======================================================================
5390 * Load the open treaties
5391 * ======================================================================= */
5392
5393/************************************************************************/
5397{
5398 int tidx;
5399 const char *plr0;
5400
5401 /* Check status and return if not OK (sg_success FALSE). */
5402 sg_check_ret();
5403
5404 for (tidx = 0; (plr0 = secfile_lookup_str_default(loading->file, NULL,
5405 "treaty%d.plr0", tidx)) != NULL ;
5406 tidx++) {
5407 const char *plr1;
5408 const char *ct;
5409 int cidx;
5410 struct player *p0, *p1;
5411
5412 plr1 = secfile_lookup_str(loading->file, "treaty%d.plr1", tidx);
5413
5414 p0 = player_by_name(plr0);
5415 p1 = player_by_name(plr1);
5416
5417 if (p0 == NULL || p1 == NULL) {
5418 log_error("Treaty between unknown players %s and %s", plr0, plr1);
5419 } else {
5420 struct treaty *ptreaty = fc_malloc(sizeof(*ptreaty));
5421
5424
5425 for (cidx = 0; (ct = secfile_lookup_str_default(loading->file, NULL,
5426 "treaty%d.clause%d.type",
5427 tidx, cidx)) != NULL ;
5428 cidx++ ) {
5430 const char *plrx;
5431
5432 if (!clause_type_is_valid(type)) {
5433 log_error("Invalid clause type \"%s\"", ct);
5434 } else {
5435 struct player *pgiver = NULL;
5436
5437 plrx = secfile_lookup_str(loading->file, "treaty%d.clause%d.from",
5438 tidx, cidx);
5439
5440 if (!fc_strcasecmp(plrx, plr0)) {
5441 pgiver = p0;
5442 } else if (!fc_strcasecmp(plrx, plr1)) {
5443 pgiver = p1;
5444 } else {
5445 log_error("Clause giver %s is not participant of the treaty"
5446 "between %s and %s", plrx, plr0, plr1);
5447 }
5448
5449 if (pgiver != NULL) {
5450 int value;
5451
5452 value = secfile_lookup_int_default(loading->file, 0,
5453 "treaty%d.clause%d.value",
5454 tidx, cidx);
5455
5456 add_clause(ptreaty, pgiver, type, value, NULL);
5457 }
5458 }
5459 }
5460
5461 /* These must be after clauses have been added so that acceptance
5462 * does not get cleared by what seems like changes to the treaty. */
5464 "treaty%d.accept0", tidx);
5466 "treaty%d.accept1", tidx);
5467 }
5468 }
5469}
5470
5471/* =======================================================================
5472 * Load the history report
5473 * ======================================================================= */
5474
5475/************************************************************************/
5479{
5481 int turn;
5482
5483 /* Check status and return if not OK (sg_success FALSE). */
5484 sg_check_ret();
5485
5486 turn = secfile_lookup_int_default(loading->file, -2, "history.turn");
5487
5488 if (turn != -2) {
5489 hist->turn = turn;
5490 }
5491
5492 if (turn + 1 >= game.info.turn) {
5493 const char *str;
5494
5495 str = secfile_lookup_str(loading->file, "history.title");
5496 sg_failure_ret(str != NULL, "%s", secfile_error());
5497 sz_strlcpy(hist->title, str);
5498 str = secfile_lookup_str(loading->file, "history.body");
5499 sg_failure_ret(str != NULL, "%s", secfile_error());
5500 sz_strlcpy(hist->body, str);
5501 }
5502}
5503
5504/* =======================================================================
5505 * Load the mapimg definitions.
5506 * ======================================================================= */
5507
5508/************************************************************************/
5511static void sg_load_mapimg(struct loaddata *loading)
5512{
5513 int mapdef_count, i;
5514
5515 /* Check status and return if not OK (sg_success FALSE). */
5516 sg_check_ret();
5517
5518 /* Clear all defined map images. */
5519 while (mapimg_count() > 0) {
5520 mapimg_delete(0);
5521 }
5522
5524 "mapimg.count");
5525 log_verbose("Saved map image definitions: %d.", mapdef_count);
5526
5527 if (0 >= mapdef_count) {
5528 return;
5529 }
5530
5531 for (i = 0; i < mapdef_count; i++) {
5532 const char *p;
5533
5534 p = secfile_lookup_str(loading->file, "mapimg.mapdef%d", i);
5535 if (NULL == p) {
5536 log_verbose("[Mapimg %4d] Missing definition.", i);
5537 continue;
5538 }
5539
5540 if (!mapimg_define(p, FALSE)) {
5541 log_error("Invalid map image definition %4d: %s.", i, p);
5542 }
5543
5544 log_verbose("Mapimg %4d loaded.", i);
5545 }
5546}
5547
5548/* =======================================================================
5549 * Sanity checks for loading a game.
5550 * ======================================================================= */
5551
5552/************************************************************************/
5556{
5557 int players;
5558
5559 /* Check status and return if not OK (sg_success FALSE). */
5560 sg_check_ret();
5561
5562 if (game.info.is_new_game) {
5563 /* Nothing to do for new games (or not started scenarios). */
5564 return;
5565 }
5566
5567 /* Old savegames may have maxplayers lower than current player count,
5568 * fix. */
5569 players = normal_player_count();
5570 if (game.server.max_players < players) {
5571 log_verbose("Max players lower than current players, fixing");
5572 game.server.max_players = players;
5573 }
5574
5575 /* Fix ferrying sanity */
5576 players_iterate(pplayer) {
5577 unit_list_iterate_safe(pplayer->units, punit) {
5580 log_sg("Removing %s unferried %s in %s at (%d, %d)",
5586 }
5589
5590 /* Fix stacking issues. We don't rely on the savegame preserving
5591 * alliance invariants (old savegames often did not) so if there are any
5592 * unallied units on the same tile we just bounce them. */
5593 players_iterate(pplayer) {
5595 resolve_unit_stacks(pplayer, aplayer, TRUE);
5598
5599 /* Recalculate the potential buildings for each city. Has caused some
5600 * problems with game random state.
5601 * This also changes the game state if you save the game directly after
5602 * loading it and compare the results. */
5603 players_iterate(pplayer) {
5604 /* Building advisor needs data phase open in order to work */
5605 adv_data_phase_init(pplayer, FALSE);
5606 building_advisor(pplayer);
5607 /* Close data phase again so it can be opened again when game starts. */
5608 adv_data_phase_done(pplayer);
5610
5611 /* Prevent a buggy or intentionally crafted save game from crashing
5612 * Freeciv. See hrm Bug #887748 */
5613 players_iterate(pplayer) {
5614 city_list_iterate(pplayer->cities, pcity) {
5615 worker_task_list_iterate(pcity->task_reqs, ptask) {
5616 if (!worker_task_is_sane(ptask)) {
5617 log_error("[city id: %d] Bad worker task %d.",
5618 pcity->id, ptask->act);
5619 worker_task_list_remove(pcity->task_reqs, ptask);
5620 free(ptask);
5621 ptask = NULL;
5622 }
5626
5627 /* Check worked tiles map */
5628#ifdef FREECIV_DEBUG
5629 if (loading->worked_tiles != NULL) {
5630 /* check the entire map for unused worked tiles */
5631 whole_map_iterate(&(wld.map), ptile) {
5632 if (loading->worked_tiles[ptile->index] != -1) {
5633 log_error("[city id: %d] Unused worked tile at (%d, %d).",
5634 loading->worked_tiles[ptile->index], TILE_XY(ptile));
5635 }
5637 }
5638#endif /* FREECIV_DEBUG */
5639
5640 /* Check researching technologies and goals. */
5642 if (presearch->researching != A_UNSET
5643 && !is_future_tech(presearch->researching)
5644 && (valid_advance_by_number(presearch->researching) == NULL
5646 != TECH_PREREQS_KNOWN))) {
5647 log_sg(_("%s had invalid researching technology."),
5649 presearch->researching = A_UNSET;
5650 }
5651 if (presearch->tech_goal != A_UNSET
5652 && !is_future_tech(presearch->tech_goal)
5653 && (valid_advance_by_number(presearch->tech_goal) == NULL
5656 == TECH_KNOWN))) {
5657 log_sg(_("%s had invalid technology goal."),
5659 presearch->tech_goal = A_UNSET;
5660 }
5661
5664
5665 players_iterate(pplayer) {
5666 unit_list_iterate_safe(pplayer->units, punit) {
5668 punit->orders.list)) {
5669 log_sg("Invalid unit orders for unit %d.", punit->id);
5671 }
5674
5675 /* Check max rates (rules may have changed since saving) */
5676 players_iterate(pplayer) {
5679
5680 if (0 == strlen(server.game_identifier)
5681 || !is_base64url(server.game_identifier)) {
5682 /* This uses fc_rand(), so random state has to be initialized before. */
5683 randomize_base64url_string(server.game_identifier,
5684 sizeof(server.game_identifier));
5685 }
5686
5687 /* Check if some player has more than one of some UTYF_UNIQUE unit type */
5688 players_iterate(pplayer) {
5689 int unique_count[U_LAST];
5690
5691 memset(unique_count, 0, sizeof(unique_count));
5692
5693 unit_list_iterate(pplayer->units, punit) {
5696
5699 log_sg(_("%s has multiple units of type %s though it should be possible "
5700 "to have only one."),
5702 }
5705
5706 /* Restore game random state, just in case various initialization code
5707 * inexplicably altered the previously existing state. */
5708 if (!game.info.is_new_game) {
5709 fc_rand_set_state(loading->rstate);
5710
5711 if (loading->version < 30) {
5712 /* For older savegames we have to recalculate the score with current data,
5713 * instead of using beginning-of-turn saved scores. */
5714 players_iterate(pplayer) {
5715 calc_civ_score(pplayer);
5717 }
5718 }
5719
5720 /* At the end do the default sanity checks. */
5721 sanity_check();
5722}
struct achievement * achievement_by_rule_name(const char *name)
#define ACTION_NONE
Definition actions.h:55
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:566
const char * ai_name(const struct ai_type *ai)
Definition ai.c:335
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
void ai_traits_init(struct player *pplayer)
Definition aitraits.c:33
#define str
Definition astring.c:76
Base_type_id base_number(const struct base_type *pbase)
Definition base.c:96
struct extra_type * base_extra_get(const struct base_type *pbase)
Definition base.c:105
struct base_type * get_base_by_gui_type(enum base_gui_type type, const struct unit *punit, const struct tile *ptile)
Definition base.c:143
struct base_type * base_by_number(const Base_type_id id)
Definition base.c:80
void dbv_set(struct dbv *pdbv, int bit)
Definition bitvector.c:142
void dbv_clr_all(struct dbv *pdbv)
Definition bitvector.c:174
void dbv_to_bv(unsigned char *dest, const struct dbv *src)
Definition bitvector.c:227
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
#define BV_CLR(bv, bit)
Definition bitvector.h:94
bool has_capability(const char *cap, const char *capstr)
Definition capability.c:79
void citizens_nation_set(struct city *pcity, const struct player_slot *pslot, citizens count)
Definition citizens.c:145
citizens citizens_count(const struct city *pcity)
Definition citizens.c:162
void citizens_init(struct city *pcity)
Definition citizens.c:32
void citizens_update(struct city *pcity, struct player *plr)
void city_map_radius_sq_set(struct city *pcity, int radius_sq)
Definition city.c:148
void city_name_set(struct city *pcity, const char *new_name)
Definition city.c:1145
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3455
int city_illness_calc(const struct city *pcity, int *ill_base, int *ill_size, int *ill_trade, int *ill_pollution)
Definition city.c:2870
void city_size_set(struct city *pcity, citizens size)
Definition city.c:1180
void city_add_improvement(struct city *pcity, const struct impr_type *pimprove)
Definition city.c:3382
void destroy_city_virtual(struct city *pcity)
Definition city.c:3541
int city_style_by_rule_name(const char *s)
Definition city.c:1738
#define cities_iterate_end
Definition city.h:514
#define city_list_iterate(citylist, pcity)
Definition city.h:505
#define city_tile(_pcity_)
Definition city.h:561
#define cities_iterate(pcity)
Definition city.h:509
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:83
static citizens city_size_get(const struct city *pcity)
Definition city.h:566
#define output_type_iterate(output)
Definition city.h:842
#define city_owner(_pcity_)
Definition city.h:560
#define FREE_WORKED_TILES
Definition city.h:879
#define MAX_CITY_SIZE
Definition city.h:103
#define city_list_iterate_end
Definition city.h:507
#define I_NEVER
Definition city.h:244
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:227
#define city_tile_iterate_end
Definition city.h:235
#define output_type_iterate_end
Definition city.h:848
bool update_dumb_city(struct player *pplayer, struct city *pcity)
Definition citytools.c:2776
bool send_city_suppression(bool now)
Definition citytools.c:2170
static void void city_freeze_workers(struct city *pcity)
Definition citytools.c:136
void city_thaw_workers(struct city *pcity)
Definition citytools.c:146
void reality_check_city(struct player *pplayer, struct tile *ptile)
Definition citytools.c:2847
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3451
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:158
char * incite_cost
Definition comments.c:76
static void road(QVariant data1, QVariant data2)
Definition dialogs.cpp:2935
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2956
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
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 int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
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:108
struct treaty * ptreaty
Definition diplodlg_g.h:28
void treaty_add(struct treaty *ptreaty)
Definition diptreaty.c:377
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
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:779
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:1128
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:804
static struct extra_type extras[MAX_EXTRA_TYPES]
Definition extras.c:31
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
#define is_extra_caused_by(e, c)
Definition extras.h:203
#define extra_index(_e_)
Definition extras.h:183
#define EXTRA_NONE
Definition extras.h:85
#define extra_base_get(_e_)
Definition extras.h:190
#define extra_road_get(_e_)
Definition extras.h:191
#define extra_type_by_cause_iterate_end
Definition extras.h:339
#define extra_type_by_cause_iterate(_cause, _extra)
Definition extras.h:333
static char * ruleset
Definition fc_manual.c:163
#define NO_TARGET
Definition fc_types.h:213
int Road_type_id
Definition fc_types.h:243
@ ROCO_RAILROAD
Definition fc_types.h:961
@ ROCO_RIVER
Definition fc_types.h:961
@ ROCO_ROAD
Definition fc_types.h:961
int Tech_type_id
Definition fc_types.h:236
unsigned char citizens
Definition fc_types.h:247
@ RPT_POSSIBLE
Definition fc_types.h:532
int Base_type_id
Definition fc_types.h:242
int Multiplier_type_id
Definition fc_types.h:245
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:92
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:61
struct world wld
Definition game.c:62
struct unit * game_unit_by_number(int id)
Definition game.c:115
void initialize_globals(void)
Definition game.c:688
struct city * game_city_by_number(int id)
Definition game.c:106
#define GAME_DEFAULT_TIMEOUTINTINC
Definition game.h:603
#define GAME_DEFAULT_SCORETURN
Definition game.h:587
#define GAME_DEFAULT_TIMEOUTINT
Definition game.h:602
#define GAME_DEFAULT_TIMEOUTINCMULT
Definition game.h:605
#define GAME_DEFAULT_TIMEOUTINC
Definition game.h:604
#define GAME_DEFAULT_RULESETDIR
Definition game.h:681
#define GAME_DEFAULT_TIMEOUTCOUNTER
Definition game.h:607
#define GAME_DEFAULT_PHASE_MODE
Definition game.h:622
struct government * government_by_rule_name(const char *name)
Definition government.c:55
struct city * owner
Definition citydlg.c:226
GType type
Definition repodlgs.c:1313
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:82
void idex_register_city(struct world *iworld, struct city *pcity)
Definition idex.c:67
Impr_type_id improvement_index(const struct impr_type *pimprove)
struct impr_type * improvement_by_rule_name(const char *name)
#define WONDER_DESTROYED
#define WONDER_LOST
void adv_city_free(struct city *pcity)
Definition infracache.c:502
void adv_city_alloc(struct city *pcity)
Definition infracache.c:489
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#define log_verbose(message,...)
Definition log.h:110
#define fc_assert(condition)
Definition log.h:177
#define log_fatal(message,...)
Definition log.h:101
#define fc_assert_action(condition, action)
Definition log.h:188
#define log_debug(message,...)
Definition log.h:116
#define log_normal(message,...)
Definition log.h:108
#define log_error(message,...)
Definition log.h:104
bool startpos_disallow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1804
#define nat_x
#define nat_y
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:686
struct startpos * map_startpos_new(struct tile *ptile)
Definition map.c:2021
void map_init_topology(struct civ_map *nmap)
Definition map.c:315
void main_map_allocate(void)
Definition map.c:534
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:471
int map_startpos_count(void)
Definition map.c:2008
struct tile * native_pos_to_tile(const struct civ_map *nmap, int nat_x, int nat_y)
Definition map.c:458
bool startpos_allow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1787
#define whole_map_iterate(_map, _tile)
Definition map.h:573
#define index_to_native_pos(pnat_x, pnat_y, mindex)
Definition map.h:161
#define whole_map_iterate_end
Definition map.h:582
@ MAPGEN_SCENARIO
Definition map_types.h:47
void assign_continent_numbers(void)
void player_map_init(struct player *pplayer)
Definition maphand.c:1226
void update_player_tile_last_seen(struct player *pplayer, struct tile *ptile)
Definition maphand.c:1472
void map_claim_ownership(struct tile *ptile, struct player *powner, struct tile *psource, bool claim_bases)
Definition maphand.c:2171
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:899
bool send_tile_suppression(bool now)
Definition maphand.c:473
bool really_gives_vision(struct player *me, struct player *them)
Definition maphand.c:343
void map_know_and_see_all(struct player *pplayer)
Definition maphand.c:1201
bool update_player_tile_knowledge(struct player *pplayer, struct tile *ptile)
Definition maphand.c:1403
void tile_claim_bases(struct tile *ptile, struct player *powner)
Definition maphand.c:2184
void map_set_known(struct tile *ptile, struct player *pplayer)
Definition maphand.c:1183
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:925
void change_playertile_site(struct player_tile *ptile, struct vision_site *new_site)
Definition maphand.c:1164
void map_calculate_borders(void)
Definition maphand.c:2329
void give_shared_vision(struct player *pfrom, struct player *pto)
Definition maphand.c:1637
struct player_tile * map_get_player_tile(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:1387
bool mapimg_define(const char *maparg, bool check)
Definition mapimg.c:769
bool mapimg_delete(int id)
Definition mapimg.c:1207
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:318
const char * multiplier_rule_name(const struct multiplier *pmul)
struct multiplier * multiplier_by_rule_name(const char *name)
Multiplier_type_id multiplier_index(const struct multiplier *pmul)
Definition multipliers.c:80
#define multipliers_iterate(_mul_)
Definition multipliers.h:61
#define multipliers_iterate_end
Definition multipliers.h:67
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:121
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
#define NO_NATION_SELECTED
Definition nation.h:30
void event_cache_load(struct section_file *file, const char *section)
Definition notify.c:783
int parts
Definition packhand.c:132
char * lines
Definition packhand.c:131
int len
Definition packhand.c:127
bool player_slot_is_used(const struct player_slot *pslot)
Definition player.c:448
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1229
struct player * player_by_number(const int player_id)
Definition player.c:849
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1480
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:1996
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:1409
struct player_slot * slots
Definition player.c:51
#define players_iterate_end
Definition player.h:542
dipl_reason
Definition player.h:192
@ DIPL_ALLIANCE_PROBLEM_THEM
Definition player.h:194
@ DIPL_ALLIANCE_PROBLEM_US
Definition player.h:194
#define players_iterate(_pplayer)
Definition player.h:537
#define MAX_ATTRIBUTE_BLOCK
Definition player.h:223
#define player_list_iterate(playerlist, pplayer)
Definition player.h:560
static bool is_barbarian(const struct player *pplayer)
Definition player.h:491
#define player_slots_iterate(_pslot)
Definition player.h:528
#define is_ai(plr)
Definition player.h:232
#define player_list_iterate_end
Definition player.h:562
#define players_iterate_alive_end
Definition player.h:552
#define player_slots_iterate_end
Definition player.h:532
#define players_iterate_alive(_pplayer)
Definition player.h:547
void server_player_set_name(struct player *pplayer, const char *name)
Definition plrhand.c:2270
struct player * server_create_player(int player_id, const char *ai_tname, struct rgbcolor *prgbcolor, bool allow_ai_type_fallbacking)
Definition plrhand.c:1896
int normal_player_count(void)
Definition plrhand.c:3209
void player_limit_to_max_rates(struct player *pplayer)
Definition plrhand.c:2059
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:2458
void set_shuffled_players(int *shuffled_players)
Definition plrhand.c:2408
void player_delegation_set(struct player *pplayer, const char *username)
Definition plrhand.c:3255
void shuffle_players(void)
Definition plrhand.c:2383
void server_remove_player(struct player *pplayer)
Definition plrhand.c:1945
void server_player_init(struct player *pplayer, bool initmap, bool needs_team)
Definition plrhand.c:1620
void assign_player_colors(void)
Definition plrhand.c:1736
void fit_nationset_to_players(void)
Definition plrhand.c:2664
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,...)
float secfile_lookup_float_default(const struct section_file *secfile, float def, const char *path,...)
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
struct entry * secfile_entry_by_path(const struct section_file *secfile, const char *path)
struct section * secfile_section_lookup(const struct section_file *secfile, const char *path,...)
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
bool secfile_lookup_bool(const struct section_file *secfile, bool *bval, const char *path,...)
#define secfile_lookup_enum_default(secfile, defval, specenum_type, path,...)
#define secfile_entry_ignore(_sfile_, _fmt_,...)
struct history_report * history_report_get(void)
Definition report.c:1859
bool are_reqs_active(const struct req_context *context, const struct req_context *other_context, 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:1353
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 ruleload.c:9188
#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:327
static struct compatibility compat[]
Definition savecompat.c:112
int char2num(char ch)
Definition savecompat.c:272
void sg_load_compat(struct loaddata *loading, enum sgf_version format_class)
Definition savecompat.c:147
enum ai_level ai_level_convert(int old_level)
int ascii_hex2bin(char ch, int halfbyte)
Definition savecompat.c:248
struct extra_type * special_extra_get(int spe)
Definition savecompat.c:313
enum tile_special_type special_by_rule_name(const char *name)
Definition savecompat.c:287
void sg_load_post_load_compat(struct loaddata *loading, enum sgf_version format_class)
Definition savecompat.c:198
const char * special_rule_name(enum tile_special_type type)
Definition savecompat.c:303
#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:836
static void sg_load_players_basic(struct loaddata *loading)
Definition savegame2.c:2698
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:2634
#define halfbyte_iterate_roads_end
Definition savegame2.c:280
static struct extra_type * char2resource(char c)
Definition savegame2.c:1324
static void sg_load_map_owner(struct loaddata *loading)
Definition savegame2.c:2503
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:2342
#define halfbyte_iterate_extras(e, num_extras_types)
Definition savegame2.c:245
static void sg_load_map(struct loaddata *loading)
Definition savegame2.c:2185
static enum unit_orders char2order(char order)
Definition savegame2.c:599
static int unquote_block(const char *const quoted_, void *dest, int dest_length)
Definition savegame2.c:742
static void sg_bases_set_bv(bv_extras *extras, char ch, struct base_type **idx)
Definition savegame2.c:1235
#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:4053
static void sg_load_player_cities(struct loaddata *loading, struct player *plr)
Definition savegame2.c:3654
static void sg_load_map_tiles(struct loaddata *loading)
Definition savegame2.c:2270
static char activity2char(int activity)
Definition savegame2.c:667
static void sg_load_savefile(struct loaddata *loading)
Definition savegame2.c:1436
#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:4220
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:3769
static void sg_load_settings(struct loaddata *loading)
Definition savegame2.c:2165
static void sg_bases_set_dbv(struct dbv *extras, char ch, struct base_type **idx)
Definition savegame2.c:1203
static void sg_load_player_units(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4101
#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:5290
#define halfbyte_iterate_bases_end
Definition savegame2.c:270
static void sg_load_map_worked(struct loaddata *loading)
Definition savegame2.c:2590
static void sg_load_random(struct loaddata *loading)
Definition savegame2.c:1996
#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:4829
static void sg_load_history(struct loaddata *loading)
Definition savegame2.c:5478
static int char2activity(char activity)
Definition savegame2.c:721
#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:2358
#define TOKEN_SIZE
Definition savegame2.c:284
static void sg_load_script(struct loaddata *loading)
Definition savegame2.c:2055
static void sg_load_scenario(struct loaddata *loading)
Definition savegame2.c:2070
static void sg_load_ruleset(struct loaddata *loading)
Definition savegame2.c:1402
static void sg_load_game(struct loaddata *loading)
Definition savegame2.c:1846
static void sg_load_player_main(struct loaddata *loading, struct player *plr)
Definition savegame2.c:3147
#define ACTIVITY_OLD_ROAD
Definition savegame2.c:143
static void sg_load_treaties(struct loaddata *loading)
Definition savegame2.c:5396
static void sg_extras_set_bv(bv_extras *extras, char ch, struct extra_type **idx)
Definition savegame2.c:890
static void set_unit_activity_road(struct unit *punit, Road_type_id road)
Definition savegame2.c:584
static Tech_type_id technology_load(struct section_file *file, const char *path, int plrno)
Definition savegame2.c:1362
static enum direction8 char2dir(char dir)
Definition savegame2.c:638
static void sg_load_map_tiles_resources(struct loaddata *loading)
Definition savegame2.c:2389
#define ORDER_OLD_BUILD_CITY
Definition savegame2.c:286
static struct terrain * char2terrain(char ch)
Definition savegame2.c:1339
#define ORDER_OLD_DISBAND
Definition savegame2.c:287
static void sg_load_mapimg(struct loaddata *loading)
Definition savegame2.c:5511
#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:1063
static void sg_load_player_attributes(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4882
static void sg_load_ruledata(struct loaddata *loading)
Definition savegame2.c:1822
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:923
static void sg_load_player_vision(struct loaddata *loading, struct player *plr)
Definition savegame2.c:4962
static void worklist_load(struct section_file *file, int wlist_max_length, struct worklist *pwl, const char *path,...)
Definition savegame2.c:790
static bool sg_load_player_vision_city(struct loaddata *loading, struct player *plr, struct vision_site *pdcity, const char *citystr)
Definition savegame2.c:5183
static void sg_roads_set_bv(bv_extras *extras, char ch, struct road_type **idx)
Definition savegame2.c:1297
static void sg_roads_set_dbv(struct dbv *extras, char ch, struct road_type **idx)
Definition savegame2.c:1266
static void sg_load_map_startpos(struct loaddata *loading)
Definition savegame2.c:2416
static void loaddata_destroy(struct loaddata *loading)
Definition savegame2.c:512
static void sg_load_players(struct loaddata *loading)
Definition savegame2.c:2955
#define ORDER_OLD_TRADE_ROUTE
Definition savegame2.c:289
static void sg_load_map_tiles_extras(struct loaddata *loading)
Definition savegame2.c:2310
static void sg_load_sanitycheck(struct loaddata *loading)
Definition savegame2.c:5555
static void sg_load_event_cache(struct loaddata *loading)
Definition savegame2.c:5381
static void sg_load_map_tiles_bases(struct loaddata *loading)
Definition savegame2.c:2326
static void sg_extras_set_dbv(struct dbv *extras, char ch, struct extra_type **idx)
Definition savegame2.c:857
#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:4179
#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:4913
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
bool str_to_int(const char *str, int *pint)
Definition shared.c:515
bool is_base64url(const char *s)
Definition shared.c:321
char scanin(const char **buf, char *delimiters, char *dest, int size)
Definition shared.c:1923
void randomize_base64url_string(char *s, size_t n)
Definition shared.c:343
#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:3497
const char * aifill(int amount)
Definition srv_main.c:2506
bool game_was_started(void)
Definition srv_main.c:354
void identity_number_reserve(int id)
Definition srv_main.c:2035
struct server_arguments srvarg
Definition srv_main.c:181
void init_game_seed(void)
Definition srv_main.c:208
void update_nations_with_startpos(void)
Definition srv_main.c:2311
void server_game_free(void)
Definition srv_main.c:3521
struct player * first
int val
Definition traits.h:38
int mod
Definition traits.h:39
Definition city.h:317
citizens * nationality
Definition city.h:338
bool last_updated_year
Definition game.h:243
int world_peace_start
Definition game.h:245
float turn_change_time
Definition game.h:225
bool vision_reveal_tiles
Definition game.h:207
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:241
struct packet_game_info info
Definition game.h:89
int timeoutcounter
Definition game.h:214
char rulesetdir[MAX_LEN_NAME]
Definition game.h:246
int scoreturn
Definition game.h:232
randseed seed
Definition game.h:234
struct packet_scenario_info scenario
Definition game.h:87
int timeoutint
Definition game.h:210
unsigned revealmap
Definition game.h:184
char orig_game_version[MAX_LEN_NAME]
Definition game.h:228
bool foggedborders
Definition game.h:154
int timeoutincmult
Definition game.h:212
struct civ_game::@32::@36 server
int timeoutinc
Definition game.h:211
int phase_mode_stored
Definition game.h:223
int max_players
Definition game.h:163
int timeoutintinc
Definition game.h:213
randseed seed
Definition map_types.h:105
bool have_resources
Definition map_types.h:121
bool altitude_info
Definition map_types.h:74
struct civ_map::@44::@46 server
bool have_huts
Definition map_types.h:120
enum map_generator generator
Definition map_types.h:111
int changed_to_times
Definition government.h:64
const char ** order
Definition savecompat.h:54
struct section_file * file
Definition savecompat.h:48
int great_wonder_owners[B_LAST]
bool global_advances[A_LAST]
enum ai_level skill_level
enum phase_mode_type phase_mode
char description[MAX_LEN_CONTENT]
char authors[MAX_LEN_PACKET/3]
enum ai_level skill_level
Definition player.h:116
struct ai_trait * traits
Definition player.h:126
enum barbarian_type barbarian_type
Definition player.h:122
int science_cost
Definition player.h:119
int love[MAX_NUM_PLAYER_SLOTS]
Definition player.h:124
int expand
Definition player.h:118
int fuzzy
Definition player.h:117
enum diplstate_type type
Definition player.h:199
int units_killed
Definition player.h:105
int landarea
Definition player.h:94
int population
Definition player.h:96
int pollution
Definition player.h:99
int wonders
Definition player.h:91
int settledarea
Definition player.h:95
int units_used
Definition player.h:108
int specialists[SP_MAX]
Definition player.h:90
int units_lost
Definition player.h:106
int angry
Definition player.h:89
int techout
Definition player.h:93
int units
Definition player.h:98
int units_built
Definition player.h:104
int content
Definition player.h:87
int happy
Definition player.h:86
int spaceship
Definition player.h:103
int culture
Definition player.h:109
int unhappy
Definition player.h:88
int cities
Definition player.h:97
int literacy
Definition player.h:100
int techs
Definition player.h:92
struct player * extras_owner
Definition maphand.h:35
struct player * owner
Definition maphand.h:34
struct city_list * cities
Definition player.h:281
int bulbs_last_turn
Definition player.h:351
struct player_ai ai_common
Definition player.h:288
bv_plr_flags flags
Definition player.h:292
bool is_male
Definition player.h:257
int wonders[B_LAST]
Definition player.h:305
bool unassigned_ranked
Definition player.h:255
struct government * target_government
Definition player.h:259
char username[MAX_LEN_NAME]
Definition player.h:252
int revolution_finishes
Definition player.h:273
int nturns_idle
Definition player.h:265
struct government * government
Definition player.h:258
struct team * team
Definition player.h:261
int turns_alive
Definition player.h:266
struct unit_list * units
Definition player.h:282
char ranked_username[MAX_LEN_NAME]
Definition player.h:254
int huts
Definition player.h:349
bool is_alive
Definition player.h:268
bv_player real_embassy
Definition player.h:277
struct player::@73::@75 server
struct player_economic economic
Definition player.h:284
struct player_spaceship spaceship
Definition player.h:286
struct attribute_block_s attribute_block
Definition player.h:307
struct player_score score
Definition player.h:283
struct multiplier_value multipliers[MAX_NUM_MULTIPLIERS]
Definition player.h:314
struct nation_type * nation
Definition player.h:260
struct nation_style * style
Definition player.h:279
bool border_vision
Definition player.h:327
bool phase_done
Definition player.h:263
int history
Definition player.h:316
char orig_username[MAX_LEN_NAME]
Definition player.h:347
int last_war_action
Definition player.h:270
bool unassigned_user
Definition player.h:253
const struct tile * tile
char metaserver_addr[256]
Definition srv_main.h:29
char serverid[256]
Definition srv_main.h:49
Definition map.c:40
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
bool accept1
Definition diptreaty.h:83
bool accept0
Definition diptreaty.h:83
enum unit_orders order
Definition unit.h:94
Definition unit.h:140
int length
Definition unit.h:198
int upkeep[O_LAST]
Definition unit.h:150
bool has_orders
Definition unit.h:196
enum action_decision action_decision_want
Definition unit.h:205
int battlegroup
Definition unit.h:194
enum unit_activity activity
Definition unit.h:159
int moves_left
Definition unit.h:152
int id
Definition unit.h:147
int ord_city
Definition unit.h:245
struct unit::@83 orders
bool moved
Definition unit.h:176
int ord_map
Definition unit.h:244
int index
Definition unit.h:198
struct vision * vision
Definition unit.h:247
bool vigilant
Definition unit.h:200
int hp
Definition unit.h:153
int fuel
Definition unit.h:155
struct extra_type * changed_from_target
Definition unit.h:173
int current_form_turn
Definition unit.h:211
enum direction8 facing
Definition unit.h:144
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:142
struct extra_type * activity_target
Definition unit.h:167
int activity_count
Definition unit.h:165
struct unit_order * list
Definition unit.h:201
enum unit_activity changed_from
Definition unit.h:171
struct player * nationality
Definition unit.h:146
bool repeat
Definition unit.h:199
int homecity
Definition unit.h:148
bool paradropped
Definition unit.h:177
bool done_moving
Definition unit.h:184
int birth_turn
Definition unit.h:210
struct tile * goto_tile
Definition unit.h:157
struct tile * action_decision_tile
Definition unit.h:206
int veteran
Definition unit.h:154
int changed_from_count
Definition unit.h:172
enum server_side_agent ssa_controller
Definition unit.h:175
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:960
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition support.c:886
#define sz_strlcpy(dest, src)
Definition support.h:195
#define RETURN_VALUE_AFTER_EXIT(_val_)
Definition support.h:146
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct team_slot * team_slot_by_number(int team_id)
Definition team.c:175
bool team_add_player(struct player *pplayer, struct team *pteam)
Definition team.c:467
struct team * team_new(struct team_slot *tslot)
Definition team.c:317
const struct player_list * team_members(const struct team *pteam)
Definition team.c:456
bool is_future_tech(Tech_type_id tech)
Definition tech.c:281
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176
struct advance * advance_by_rule_name(const char *name)
Definition tech.c:200
Tech_type_id advance_number(const struct advance *padvance)
Definition tech.c:98
#define A_FUTURE
Definition tech.h:46
#define A_NONE
Definition tech.h:43
#define A_UNSET
Definition tech.h:48
#define A_UNKNOWN
Definition tech.h:49
void init_tech(struct research *research, bool update)
Definition techtools.c:1094
struct terrain * terrain_by_rule_name(const char *name)
Definition terrain.c:186
const char * terrain_rule_name(const struct terrain *pterrain)
Definition terrain.c:247
bool terrain_has_resource(const struct terrain *pterrain, const struct extra_type *presource)
Definition terrain.c:255
#define terrain_type_iterate(_p)
Definition terrain.h:266
#define T_UNKNOWN
Definition terrain.h:62
#define TERRAIN_UNKNOWN_IDENTIFIER
Definition terrain.h:88
#define terrain_type_iterate_end
Definition terrain.h:272
#define RESOURCE_NONE_IDENTIFIER
Definition terrain.h:52
#define RESOURCE_NULL_IDENTIFIER
Definition terrain.h:51
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:1035
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:1097
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:89
#define tile_worked(_tile)
Definition tile.h:115
#define tile_terrain(_tile)
Definition tile.h:111
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_has_extra(ptile, pextra)
Definition tile.h:148
#define tile_owner(_tile)
Definition tile.h:97
struct goods_type * goods_by_number(Goods_type_id id)
void free_unit_orders(struct unit *punit)
Definition unit.c:1806
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2447
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2518
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:865
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2930
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1661
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2726
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target, enum gen_action trigger_action)
Definition unit.c:1132
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1766
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1284
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1114
#define unit_tile(_pu)
Definition unit.h:404
#define BATTLEGROUP_NONE
Definition unit.h:193
unit_orders
Definition unit.h:38
@ ORDER_ACTION_MOVE
Definition unit.h:46
@ ORDER_ACTIVITY
Definition unit.h:42
@ ORDER_FULL_MP
Definition unit.h:44
@ ORDER_MOVE
Definition unit.h:40
@ ORDER_LAST
Definition unit.h:50
@ ORDER_PERFORM_ACTION
Definition unit.h:48
#define unit_owner(_pu)
Definition unit.h:403
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:1401
void unit_refresh_vision(struct unit *punit)
Definition unittools.c:5004
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:1773
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1593
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2631
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:1566
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624
#define unit_type_iterate(_p)
Definition unittype.h:862
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:869
void vision_site_size_set(struct vision_site *psite, citizens size)
Definition vision.c:180
struct vision * vision_new(struct player *pplayer, struct tile *ptile)
Definition vision.c:33
bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles)
Definition vision.c:62
struct vision_site * vision_site_new(int identity, struct tile *location, struct player *owner)
Definition vision.c:86
void vision_site_destroy(struct vision_site *psite)
Definition vision.c:74
bool worker_task_is_sane(struct worker_task *ptask)
Definition workertask.c:40
#define worker_task_list_iterate(tasklist, ptask)
Definition workertask.h:33
#define worker_task_list_iterate_end
Definition workertask.h:35
void worklist_init(struct worklist *pwl)
Definition worklist.c:38
#define MAP_NATIVE_WIDTH
#define MAP_INDEX_SIZE
#define MAP_NATIVE_HEIGHT