Freeciv-3.4
Loading...
Searching...
No Matches
savegame3.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 3.0. It is defined by the mandatory option '+version3'. The main load
17 function checks if this option is present. If not, the old (pre-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 main function for saving is savegame3_save().
26
27 - The real work is done by savegame3_load() and savegame3_save_real().
28 This function call all submodules (settings, players, etc.)
29
30 - The remaining part of this file is split into several sections:
31 * helper functions
32 * save / load functions for all submodules (and their subsubmodules)
33
34 - If possible, all functions for load / save submodules should exit in
35 pairs named sg_load_<submodule> and sg_save_<submodule>. If one is not
36 needed please add a comment why.
37
38 - The submodules can be further divided as:
39 sg_load_<submodule>_<subsubmodule>
40
41 - If needed (due to static variables in the *.c files) these functions
42 can be located in the corresponding source files (as done for the settings
43 and the event_cache).
44
45 Creating a savegame:
46
47 (nothing at the moment)
48
49 Loading a savegame:
50
51 - The status of the process is saved within the static variable
52 'sg_success'. This variable is set to TRUE within savegame3_load().
53 If you encounter an error use sg_failure_*() to set it to FALSE and
54 return an error message. Furthermore, sg_check_* should be used at the
55 start of each (submodule) function to return if previous functions failed.
56
57 - While the loading process dependencies between different modules exits.
58 They can be handled within the struct loaddata *loading which is used as
59 first argument for all sg_load_*() function. Please indicate the
60 dependencies within the definition of this struct.
61
62*/
63
64#ifdef HAVE_CONFIG_H
65#include <fc_config.h>
66#endif
67
68#include <ctype.h>
69#include <stdarg.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73
74/* utility */
75#include "bitvector.h"
76#include "fcintl.h"
77#include "idex.h"
78#include "log.h"
79#include "mem.h"
80#include "rand.h"
81#include "registry.h"
82#include "shared.h"
83#include "support.h" /* bool type */
84#include "timing.h"
85
86/* common */
87#include "achievements.h"
88#include "ai.h"
89#include "bitvector.h"
90#include "capability.h"
91#include "citizens.h"
92#include "city.h"
93#include "counters.h"
94#include "game.h"
95#include "government.h"
96#include "map.h"
97#include "mapimg.h"
98#include "movement.h"
99#include "multipliers.h"
100#include "packets.h"
101#include "research.h"
102#include "rgbcolor.h"
103#include "sex.h"
104#include "specialist.h"
105#include "unit.h"
106#include "unitlist.h"
107#include "version.h"
108
109/* server */
110#include "barbarian.h"
111#include "citizenshand.h"
112#include "citytools.h"
113#include "cityturn.h"
114#include "diplhand.h"
115#include "maphand.h"
116#include "meta.h"
117#include "notify.h"
118#include "plrhand.h"
119#include "report.h"
120#include "ruleload.h"
121#include "sanitycheck.h"
122#include "savecompat.h"
123#include "score.h"
124#include "settings.h"
125#include "spacerace.h"
126#include "srv_main.h"
127#include "stdinhand.h"
128#include "techtools.h"
129#include "unittools.h"
130
131/* server/advisors */
132#include "advdata.h"
133#include "advbuilding.h"
134#include "infracache.h"
135
136/* server/generator */
137#include "mapgen.h"
138#include "mapgen_utils.h"
139
140/* server/scripting */
141#include "script_server.h"
142
143/* ai */
144#include "aitraits.h"
145#include "difficulty.h"
146
147#include "savegame3.h"
148
149extern bool sg_success;
150
151#define ACTIVITY_OLD_POLLUTION_SG3 (ACTIVITY_LAST + 1)
152#define ACTIVITY_OLD_FALLOUT_SG3 (ACTIVITY_OLD_POLLUTION_SG3 + 1)
153#define ACTIVITY_LAST_SAVEGAME3 (ACTIVITY_OLD_FALLOUT_SG3 + 1)
154
155#ifdef FREECIV_TESTMATIC
156#define SAVE_DUMMY_TURN_CHANGE_TIME 1
157#endif
158
159/*
160 * This loops over the entire map to save data. It collects all the data of
161 * a line using GET_XY_CHAR and then executes the macro SECFILE_INSERT_LINE.
162 *
163 * Parameters:
164 * ptile: current tile within the line (used by GET_XY_CHAR)
165 * GET_XY_CHAR: macro returning the map character for each position
166 * secfile: a secfile struct
167 * secpath, ...: path as used for sprintf() with arguments; the last item
168 * will be the y coordinate
169 * Example:
170 * SAVE_MAP_CHAR(ptile, terrain2char(ptile->terrain), file, "map.t%04d");
171 */
172#define SAVE_MAP_CHAR(ptile, GET_XY_CHAR, secfile, secpath, ...) \
173{ \
174 char _line[MAP_NATIVE_WIDTH + 1]; \
175 int _nat_x, _nat_y; \
176 \
177 for (_nat_y = 0; _nat_y < MAP_NATIVE_HEIGHT; _nat_y++) { \
178 for (_nat_x = 0; _nat_x < MAP_NATIVE_WIDTH; _nat_x++) { \
179 struct tile *ptile = native_pos_to_tile(&(wld.map), _nat_x, _nat_y); \
180 fc_assert_action(ptile != NULL, continue); \
181 _line[_nat_x] = (GET_XY_CHAR); \
182 sg_failure_ret(fc_isprint(_line[_nat_x] & 0x7f), \
183 "Trying to write invalid map data at position " \
184 "(%d, %d) for path %s: '%c' (%d)", _nat_x, _nat_y, \
185 secpath, _line[_nat_x], _line[_nat_x]); \
186 } \
187 _line[MAP_NATIVE_WIDTH] = '\0'; \
188 secfile_insert_str(secfile, _line, secpath, ## __VA_ARGS__, _nat_y); \
189 } \
190}
191
192/*
193 * This loops over the entire map to load data. It inputs a line of data
194 * using the macro SECFILE_LOOKUP_LINE and then loops using the macro
195 * SET_XY_CHAR to load each char into the map at (map_x, map_y). Internal
196 * variables ch, map_x, map_y, nat_x, and nat_y are allocated within the
197 * macro but definable by the caller.
198 *
199 * Parameters:
200 * ch: a variable to hold a char (data for a single position,
201 * used by SET_XY_CHAR)
202 * ptile: current tile within the line (used by SET_XY_CHAR)
203 * SET_XY_CHAR: macro to load the map character at each (map_x, map_y)
204 * secfile: a secfile struct
205 * secpath, ...: path as used for sprintf() with arguments; the last item
206 * will be the y coordinate
207 * Example:
208 * LOAD_MAP_CHAR(ch, ptile,
209 * map_get_player_tile(ptile, plr)->terrain
210 * = char2terrain(ch), file, "player%d.map_t%04d", plrno);
211 *
212 * Note: some (but not all) of the code this is replacing used to skip over
213 * lines that did not exist. This allowed for backward-compatibility.
214 * We could add another parameter that specified whether it was OK to
215 * skip the data, but there's not really much advantage to exiting
216 * early in this case. Instead, we let any map data type to be empty,
217 * and just print an informative warning message about it.
218 */
219#define LOAD_MAP_CHAR(ch, ptile, SET_XY_CHAR, secfile, secpath, ...) \
220{ \
221 int _nat_x, _nat_y; \
222 bool _printed_warning = FALSE; \
223 for (_nat_y = 0; _nat_y < MAP_NATIVE_HEIGHT; _nat_y++) { \
224 const char *_line = secfile_lookup_str(secfile, secpath, \
225 ## __VA_ARGS__, _nat_y); \
226 if (NULL == _line) { \
227 char buf[64]; \
228 fc_snprintf(buf, sizeof(buf), secpath, ## __VA_ARGS__, _nat_y); \
229 log_verbose("Line not found='%s'", buf); \
230 _printed_warning = TRUE; \
231 continue; \
232 } else if (strlen(_line) != MAP_NATIVE_WIDTH) { \
233 char buf[64]; \
234 fc_snprintf(buf, sizeof(buf), secpath, ## __VA_ARGS__, _nat_y); \
235 log_verbose("Line too short (expected %d got " SIZE_T_PRINTF \
236 ")='%s'", MAP_NATIVE_WIDTH, strlen(_line), buf); \
237 _printed_warning = TRUE; \
238 continue; \
239 } \
240 for (_nat_x = 0; _nat_x < MAP_NATIVE_WIDTH; _nat_x++) { \
241 const char ch = _line[_nat_x]; \
242 struct tile *ptile = native_pos_to_tile(&(wld.map), _nat_x, _nat_y); \
243 (SET_XY_CHAR); \
244 } \
245 } \
246 if (_printed_warning) { \
247 /* TRANS: Minor error message. */ \
248 log_sg(_("Saved game contains incomplete map data. This can" \
249 " happen with old saved games, or it may indicate an" \
250 " invalid saved game file. Proceed at your own risk.")); \
251 } \
252}
253
254/* Iterate on the extras half-bytes */
255#define halfbyte_iterate_extras(e, num_extras_types) \
256{ \
257 int e; \
258 for (e = 0; 4 * e < (num_extras_types); e++) {
259
260#define halfbyte_iterate_extras_end \
261 } \
262}
263
264struct savedata {
267
268 /* set by the caller */
269 const char *save_reason;
271
272 /* Set in sg_save_game(); needed in sg_save_map_*(); ... */
274};
275
276#define TOKEN_SIZE 10
277
278static const char savefile_options_default[] =
279 " +version3";
280/* The following savefile option are added if needed:
281 * - nothing at current version
282 * See also calls to sg_save_savefile_options(). */
283
284static void savegame3_save_real(struct section_file *file,
285 const char *save_reason,
286 bool scenario);
287static struct loaddata *loaddata_new(struct section_file *file);
288static void loaddata_destroy(struct loaddata *loading);
289
290static struct savedata *savedata_new(struct section_file *file,
291 const char *save_reason,
292 bool scenario);
293static void savedata_destroy(struct savedata *saving);
294
295static enum unit_orders char2order(char order);
296static char order2char(enum unit_orders order);
297static enum direction8 char2dir(char dir);
298static char dir2char(enum direction8 dir);
299static char activity2char(int activity);
300static enum unit_activity char2activity(char activity);
301static char *quote_block(const void *const data, int length);
302static int unquote_block(const char *const quoted_, void *dest,
303 int dest_length);
304static void worklist_load(struct section_file *file, int wlist_max_length,
305 struct worklist *pwl, const char *path, ...);
306static void worklist_save(struct section_file *file,
307 const struct worklist *pwl,
308 int max_length, const char *path, ...);
309static void unit_ordering_calc(void);
310static void unit_ordering_apply(void);
311static void sg_extras_set_dbv(struct dbv *extras, char ch, struct extra_type **idx);
312static void sg_extras_set_bv(bv_extras *extras, char ch, struct extra_type **idx);
313static char sg_extras_get_dbv(struct dbv *extras, struct extra_type *presource,
314 const int *idx);
315static char sg_extras_get_bv(bv_extras extras, struct extra_type *presource,
316 const int *idx);
317static struct terrain *char2terrain(char ch);
318static char terrain2char(const struct terrain *pterrain);
319static Tech_type_id technology_load(struct section_file *file,
320 const char *path, int plrno);
321static void technology_save(struct section_file *file,
322 const char *path, int plrno, Tech_type_id tech);
323
324static void sg_load_savefile(struct loaddata *loading);
325static void sg_save_savefile(struct savedata *saving);
326static void sg_save_savefile_options(struct savedata *saving,
327 const char *option);
328
329static void sg_load_game(struct loaddata *loading);
330static void sg_save_game(struct savedata *saving);
331
332static void sg_load_ruledata(struct loaddata *loading);
333static void sg_save_ruledata(struct savedata *saving);
334
335static void sg_load_random(struct loaddata *loading);
336static void sg_save_random(struct savedata *saving);
337
338static void sg_load_script(struct loaddata *loading);
339static void sg_save_script(struct savedata *saving);
340
341static void sg_load_scenario(struct loaddata *loading);
342static void sg_save_scenario(struct savedata *saving);
343
344static void sg_load_settings(struct loaddata *loading);
345static void sg_save_settings(struct savedata *saving);
346
347static void sg_load_counters (struct loaddata * loading);
348static void sg_save_counters (struct savedata * saving);
349
350static void sg_load_map(struct loaddata *loading);
351static void sg_save_map(struct savedata *saving);
352static void sg_load_map_tiles(struct loaddata *loading);
353static void sg_load_map_altitude(struct loaddata *loading);
354static void sg_save_map_tiles(struct savedata *saving);
355static void sg_save_map_altitude(struct savedata *saving);
356static void sg_load_map_tiles_extras(struct loaddata *loading);
357static void sg_save_map_tiles_extras(struct savedata *saving);
358
359static void sg_load_map_startpos(struct loaddata *loading);
360static void sg_save_map_startpos(struct savedata *saving);
361static void sg_load_map_owner(struct loaddata *loading);
362static void sg_save_map_owner(struct savedata *saving);
363static void sg_load_map_worked(struct loaddata *loading);
364static void sg_save_map_worked(struct savedata *saving);
365static void sg_load_map_known(struct loaddata *loading);
366static void sg_save_map_known(struct savedata *saving);
367
368static void sg_load_players_basic(struct loaddata *loading);
369static void sg_load_players(struct loaddata *loading);
370static void sg_load_player_main(struct loaddata *loading,
371 struct player *plr);
372static void sg_load_player_cities(struct loaddata *loading,
373 struct player *plr);
374static bool sg_load_player_city(struct loaddata *loading, struct player *plr,
375 struct city *pcity, const char *citystr,
378 struct player *plr,
379 struct city *pcity,
380 const char *citystr);
381static void sg_load_player_units(struct loaddata *loading,
382 struct player *plr);
383static bool sg_load_player_unit(struct loaddata *loading,
384 struct player *plr, struct unit *punit,
386 const char *unitstr);
388 struct player *plr);
389static void sg_load_player_attributes(struct loaddata *loading,
390 struct player *plr);
391static void sg_load_player_vision(struct loaddata *loading,
392 struct player *plr);
394 struct player *plr,
395 struct vision_site *pdcity,
396 const char *citystr);
397static void sg_save_players(struct savedata *saving);
398static void sg_save_player_main(struct savedata *saving,
399 struct player *plr);
400static void sg_save_player_cities(struct savedata *saving,
401 struct player *plr);
402static void sg_save_player_units(struct savedata *saving,
403 struct player *plr);
404static void sg_save_player_attributes(struct savedata *saving,
405 struct player *plr);
406static void sg_save_player_vision(struct savedata *saving,
407 struct player *plr);
408
409static void sg_load_researches(struct loaddata *loading);
410static void sg_save_researches(struct savedata *saving);
411
412static void sg_load_event_cache(struct loaddata *loading);
413static void sg_save_event_cache(struct savedata *saving);
414
415static void sg_load_treaties(struct loaddata *loading);
416static void sg_save_treaties(struct savedata *saving);
417
418static void sg_load_history(struct loaddata *loading);
419static void sg_save_history(struct savedata *saving);
420
421static void sg_load_mapimg(struct loaddata *loading);
422static void sg_save_mapimg(struct savedata *saving);
423
424static void sg_load_sanitycheck(struct loaddata *loading);
425static void sg_save_sanitycheck(struct savedata *saving);
426
427
428/************************************************************************/
431void savegame3_save(struct section_file *sfile, const char *save_reason,
432 bool scenario)
433{
434 fc_assert_ret(sfile != NULL);
435
436#ifdef DEBUG_TIMERS
437 struct timer *savetimer = timer_new(TIMER_CPU, TIMER_DEBUG, "save");
439#endif
440
441 log_verbose("saving game in new format ...");
442 savegame3_save_real(sfile, save_reason, scenario);
443
444#ifdef DEBUG_TIMERS
446 log_debug("Creating secfile in %.3f seconds.", timer_read_seconds(savetimer));
448#endif /* DEBUG_TIMERS */
449}
450
451/* =======================================================================
452 * Basic load / save functions.
453 * ======================================================================= */
454
455/************************************************************************/
458void savegame3_load(struct section_file *file)
459{
460 struct loaddata *loading;
462
463 /* initialise loading */
468
469 /* Load the savegame data. */
470 /* [compat] */
472 /* [scenario] */
474 /* [savefile] */
476 /* [game] */
478 /* [random] */
480 /* [settings] */
482 /* [ruledata] */
484 /* [players] (basic data) */
486 /* [map]; needs width and height loaded by [settings] */
488 /* [research] */
490 /* [player<i>] */
492 /* [counters] */
494 /* [event_cache] */
496 /* [treaties] */
498 /* [history] */
500 /* [mapimg] */
502 /* [script] -- must come last as may reference game objects */
504 /* [post_load_compat]; needs the game loaded by [savefile] */
506
507 /* Sanity checks for the loaded game. */
509
510 /* deinitialise loading */
514
515 if (!sg_success) {
516 log_error("Failure loading savegame!");
517 /* Try to get the server back to a vaguely sane state */
521 }
522}
523
524/************************************************************************/
528 const char *save_reason,
529 bool scenario)
530{
531 struct savedata *saving;
532
533 /* initialise loading */
536
537 /* [scenario] */
538 /* This should be first section so scanning through all scenarios just for
539 * names and descriptions would go faster. */
541 /* [savefile] */
543 /* [counters] */
545 /* [game] */
547 /* [random] */
549 /* [script] */
551 /* [settings] */
553 /* [ruledata] */
555 /* [map] */
557 /* [player<i>] */
559 /* [research] */
561 /* [event_cache] */
563 /* [treaty<i>] */
565 /* [history] */
567 /* [mapimg] */
569
570 /* Sanity checks for the saved game. */
572
573 /* deinitialise saving */
575
576 if (!sg_success) {
577 log_error("Failure saving savegame!");
578 }
579}
580
581/************************************************************************/
584static struct loaddata *loaddata_new(struct section_file *file)
585{
586 struct loaddata *loading = calloc(1, sizeof(*loading));
587 loading->file = file;
588 loading->secfile_options = NULL;
589
590 loading->improvement.order = NULL;
591 loading->improvement.size = -1;
592 loading->technology.order = NULL;
593 loading->technology.size = -1;
594 loading->activities.order = NULL;
595 loading->activities.size = -1;
596 loading->trait.order = NULL;
597 loading->trait.size = -1;
598 loading->extra.order = NULL;
599 loading->extra.size = -1;
600 loading->multiplier.order = NULL;
601 loading->multiplier.size = -1;
602 loading->specialist.order = NULL;
603 loading->specialist.size = -1;
604 loading->action.order = NULL;
605 loading->action.size = -1;
606 loading->act_dec.order = NULL;
607 loading->act_dec.size = -1;
608 loading->ssa.order = NULL;
609 loading->ssa.size = -1;
610 loading->coptions.order = NULL;
611 loading->coptions.size = -1;
612
613 loading->server_state = S_S_INITIAL;
614 loading->rstate = fc_rand_state();
615 loading->worked_tiles = NULL;
616
617 return loading;
618}
619
620/************************************************************************/
624{
625 if (loading->improvement.order != NULL) {
626 free(loading->improvement.order);
627 }
628
629 if (loading->technology.order != NULL) {
630 free(loading->technology.order);
631 }
632
633 if (loading->activities.order != NULL) {
634 free(loading->activities.order);
635 }
636
637 if (loading->trait.order != NULL) {
638 free(loading->trait.order);
639 }
640
641 if (loading->extra.order != NULL) {
642 free(loading->extra.order);
643 }
644
645 if (loading->multiplier.order != NULL) {
646 free(loading->multiplier.order);
647 }
648
649 if (loading->specialist.order != NULL) {
650 free(loading->specialist.order);
651 }
652
653 if (loading->action.order != NULL) {
654 free(loading->action.order);
655 }
656
657 if (loading->act_dec.order != NULL) {
658 free(loading->act_dec.order);
659 }
660
661 if (loading->ssa.order != NULL) {
662 free(loading->ssa.order);
663 }
664
665 if (loading->coptions.order != NULL) {
666 free(loading->coptions.order);
667 }
668
669 if (loading->worked_tiles != NULL) {
670 free(loading->worked_tiles);
671 }
672
673 free(loading);
674}
675
676/************************************************************************/
679static struct savedata *savedata_new(struct section_file *file,
680 const char *save_reason,
681 bool scenario)
682{
683 struct savedata *saving = calloc(1, sizeof(*saving));
684 saving->file = file;
685 saving->secfile_options[0] = '\0';
686
687 saving->save_reason = save_reason;
688 saving->scenario = scenario;
689
690 saving->save_players = FALSE;
691
692 return saving;
693}
694
695/************************************************************************/
698static void savedata_destroy(struct savedata *saving)
699{
700 free(saving);
701}
702
703/* =======================================================================
704 * Helper functions.
705 * ======================================================================= */
706
707/************************************************************************/
710static enum unit_orders char2order(char order)
711{
712 switch (order) {
713 case 'm':
714 case 'M':
715 return ORDER_MOVE;
716 case 'w':
717 case 'W':
718 return ORDER_FULL_MP;
719 case 'a':
720 case 'A':
721 return ORDER_ACTIVITY;
722 case 'x':
723 case 'X':
724 return ORDER_ACTION_MOVE;
725 case 'p':
726 case 'P':
728 }
729
730 /* This can happen if the savegame is invalid. */
731 return ORDER_LAST;
732}
733
734/************************************************************************/
737static char order2char(enum unit_orders order)
738{
739 switch (order) {
740 case ORDER_MOVE:
741 return 'm';
742 case ORDER_FULL_MP:
743 return 'w';
744 case ORDER_ACTIVITY:
745 return 'a';
747 return 'x';
749 return 'p';
750 case ORDER_LAST:
751 break;
752 }
753
755 return '?';
756}
757
758/************************************************************************/
761static enum direction8 char2dir(char dir)
762{
763 /* Numberpad values for the directions. */
764 switch (dir) {
765 case '1':
766 return DIR8_SOUTHWEST;
767 case '2':
768 return DIR8_SOUTH;
769 case '3':
770 return DIR8_SOUTHEAST;
771 case '4':
772 return DIR8_WEST;
773 case '6':
774 return DIR8_EAST;
775 case '7':
776 return DIR8_NORTHWEST;
777 case '8':
778 return DIR8_NORTH;
779 case '9':
780 return DIR8_NORTHEAST;
781 }
782
783 /* This can happen if the savegame is invalid. */
784 return direction8_invalid();
785}
786
787/************************************************************************/
790static char dir2char(enum direction8 dir)
791{
792 /* Numberpad values for the directions. */
793 switch (dir) {
794 case DIR8_NORTH:
795 return '8';
796 case DIR8_SOUTH:
797 return '2';
798 case DIR8_EAST:
799 return '6';
800 case DIR8_WEST:
801 return '4';
802 case DIR8_NORTHEAST:
803 return '9';
804 case DIR8_NORTHWEST:
805 return '7';
806 case DIR8_SOUTHEAST:
807 return '3';
808 case DIR8_SOUTHWEST:
809 return '1';
810 }
811
813
814 return '?';
815}
816
817/************************************************************************/
823static char activity2char(int activity)
824{
825 switch (activity) {
826 case ACTIVITY_IDLE:
827 return 'w';
828 case ACTIVITY_CLEAN:
829 return 'C';
831 return 'p';
832 case ACTIVITY_MINE:
833 return 'm';
834 case ACTIVITY_PLANT:
835 return 'M';
837 return 'i';
839 return 'I';
841 return 'f';
842 case ACTIVITY_SENTRY:
843 return 's';
844 case ACTIVITY_PILLAGE:
845 return 'e';
846 case ACTIVITY_GOTO:
847 return 'g';
848 case ACTIVITY_EXPLORE:
849 return 'x';
851 return 'o';
853 return 'y';
855 return 'u';
856 case ACTIVITY_BASE:
857 return 'b';
859 return 'R';
860 case ACTIVITY_CONVERT:
861 return 'c';
862 case ACTIVITY_LAST:
863 break;
864 }
865
867
868 return '?';
869}
870
871/************************************************************************/
874static enum unit_activity char2activity(char activity)
875{
876 enum unit_activity a;
877
878 for (a = 0; a < ACTIVITY_LAST_SAVEGAME3; a++) {
879 /* Skip ACTIVITY_LAST. The SAVEGAME3 specific values are after it. */
880 if (a != ACTIVITY_LAST) {
881 char achar = activity2char(a);
882
883 if (activity == achar) {
884 return a;
885 }
886 }
887 }
888
889 /* This can happen if the savegame is invalid. */
890 return ACTIVITY_LAST;
891}
892
893/************************************************************************/
897static char *quote_block(const void *const data, int length)
898{
899 char *buffer = fc_malloc(length * 3 + 10);
900 size_t offset;
901 int i;
902
903 sprintf(buffer, "%d:", length);
904 offset = strlen(buffer);
905
906 for (i = 0; i < length; i++) {
907 sprintf(buffer + offset, "%02x ", ((unsigned char *) data)[i]);
908 offset += 3;
909 }
910 return buffer;
911}
912
913/************************************************************************/
918static int unquote_block(const char *const quoted_, void *dest,
919 int dest_length)
920{
921 int i, length, parsed, tmp;
922 char *endptr;
923 const char *quoted = quoted_;
924
925 parsed = sscanf(quoted, "%d", &length);
926
927 if (parsed != 1) {
928 log_error(_("Syntax error in attribute block."));
929 return 0;
930 }
931
932 if (length > dest_length) {
933 return 0;
934 }
935
936 quoted = strchr(quoted, ':');
937
938 if (quoted == NULL) {
939 log_error(_("Syntax error in attribute block."));
940 return 0;
941 }
942
943 quoted++;
944
945 for (i = 0; i < length; i++) {
946 tmp = strtol(quoted, &endptr, 16);
947
948 if ((endptr - quoted) != 2
949 || *endptr != ' '
950 || (tmp & 0xff) != tmp) {
951 log_error(_("Syntax error in attribute block."));
952 return 0;
953 }
954
955 ((unsigned char *) dest)[i] = tmp;
956 quoted += 3;
957 }
958
959 return length;
960}
961
962/************************************************************************/
967 struct worklist *pwl, const char *path, ...)
968{
969 int i;
970 const char *kind;
971 const char *name;
972 char path_str[1024];
973 va_list ap;
974
975 /* The first part of the registry path is taken from the varargs to the
976 * function. */
977 va_start(ap, path);
978 fc_vsnprintf(path_str, sizeof(path_str), path, ap);
979 va_end(ap);
980
983 "%s.wl_length", path_str);
984
985 for (i = 0; i < pwl->length; i++) {
986 kind = secfile_lookup_str(file, "%s.wl_kind%d", path_str, i);
987
988 /* We lookup the production value by name. An invalid entry isn't a
989 * fatal error; we just truncate the worklist. */
990 name = secfile_lookup_str_default(file, "-", "%s.wl_value%d",
991 path_str, i);
992 pwl->entries[i] = universal_by_rule_name(kind, name);
993 if (pwl->entries[i].kind == universals_n_invalid()) {
994 log_sg("%s.wl_value%d: unknown \"%s\" \"%s\".", path_str, i, kind,
995 name);
996 pwl->length = i;
997 break;
998 }
999 }
1000
1001 /* Padding entries */
1002 for (; i < wlist_max_length; i++) {
1003 (void) secfile_entry_lookup(file, "%s.wl_kind%d", path_str, i);
1004 (void) secfile_entry_lookup(file, "%s.wl_value%d", path_str, i);
1005 }
1006}
1007
1008/************************************************************************/
1012static void worklist_save(struct section_file *file,
1013 const struct worklist *pwl,
1014 int max_length, const char *path, ...)
1015{
1016 char path_str[1024];
1017 int i;
1018 va_list ap;
1019
1020 /* The first part of the registry path is taken from the varargs to the
1021 * function. */
1022 va_start(ap, path);
1023 fc_vsnprintf(path_str, sizeof(path_str), path, ap);
1024 va_end(ap);
1025
1026 secfile_insert_int(file, pwl->length, "%s.wl_length", path_str);
1027
1028 for (i = 0; i < pwl->length; i++) {
1029 const struct universal *entry = pwl->entries + i;
1031 "%s.wl_kind%d", path_str, i);
1033 "%s.wl_value%d", path_str, i);
1034 }
1035
1037
1038 /* We want to keep savegame in tabular format, so each line has to be
1039 * of equal length. Fill table up to maximum worklist size. */
1040 for (i = pwl->length ; i < max_length; i++) {
1041 secfile_insert_str(file, "", "%s.wl_kind%d", path_str, i);
1042 secfile_insert_str(file, "", "%s.wl_value%d", path_str, i);
1043 }
1044}
1045
1046/************************************************************************/
1050static void unit_ordering_calc(void)
1051{
1052 int j;
1053
1054 players_iterate(pplayer) {
1055 /* to avoid junk values for unsupported units: */
1056 unit_list_iterate(pplayer->units, punit) {
1057 punit->server.ord_city = 0;
1059 city_list_iterate(pplayer->cities, pcity) {
1060 j = 0;
1061 unit_list_iterate(pcity->units_supported, punit) {
1062 punit->server.ord_city = j++;
1066
1067 whole_map_iterate(&(wld.map), ptile) {
1068 j = 0;
1069 unit_list_iterate(ptile->units, punit) {
1070 punit->server.ord_map = j++;
1073}
1074
1075/************************************************************************/
1079static void unit_ordering_apply(void)
1080{
1081 players_iterate(pplayer) {
1082 city_list_iterate(pplayer->cities, pcity) {
1083 unit_list_sort_ord_city(pcity->units_supported);
1084 }
1087
1088 whole_map_iterate(&(wld.map), ptile) {
1089 unit_list_sort_ord_map(ptile->units);
1091}
1092
1093/************************************************************************/
1100static void sg_extras_set_dbv(struct dbv *extras, char ch,
1101 struct extra_type **idx)
1102{
1103 int i, bin;
1104 const char *pch = strchr(hex_chars, ch);
1105
1106 if (!pch || ch == '\0') {
1107 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1108 bin = 0;
1109 } else {
1110 bin = pch - hex_chars;
1111 }
1112
1113 for (i = 0; i < 4; i++) {
1114 struct extra_type *pextra = idx[i];
1115
1116 if (pextra == NULL) {
1117 continue;
1118 }
1119 if ((bin & (1 << i))
1120 && (wld.map.server.have_huts || !is_extra_caused_by(pextra, EC_HUT))) {
1121 dbv_set(extras, extra_index(pextra));
1122 }
1123 }
1124}
1125
1126/************************************************************************/
1134 struct extra_type **idx)
1135{
1136 int i, bin;
1137 const char *pch = strchr(hex_chars, ch);
1138
1139 if (!pch || ch == '\0') {
1140 log_sg("Unknown hex value: '%c' (%d)", ch, ch);
1141 bin = 0;
1142 } else {
1143 bin = pch - hex_chars;
1144 }
1145
1146 for (i = 0; i < 4; i++) {
1147 struct extra_type *pextra = idx[i];
1148
1149 if (pextra == NULL) {
1150 continue;
1151 }
1152 if ((bin & (1 << i))
1153 && (wld.map.server.have_huts || !is_extra_caused_by(pextra, EC_HUT))) {
1154 BV_SET(*extras, extra_index(pextra));
1155 }
1156 }
1157}
1158
1159/************************************************************************/
1165static char sg_extras_get_dbv(struct dbv *extras, struct extra_type *presource,
1166 const int *idx)
1167{
1168 int i, bin = 0;
1169 int max = dbv_bits(extras);
1170
1171 for (i = 0; i < 4; i++) {
1172 int extra = idx[i];
1173
1174 if (extra < 0) {
1175 break;
1176 }
1177
1178 if (extra < max
1179 && (dbv_isset(extras, extra)
1180 /* An invalid resource, a resource that can't exist at the tile's
1181 * current terrain, isn't in the bit extra vector. Save it so it
1182 * can return if the tile's terrain changes to something it can
1183 * exist on. */
1184 || extra_by_number(extra) == presource)) {
1185 bin |= (1 << i);
1186 }
1187 }
1188
1189 return hex_chars[bin];
1190}
1191
1192/************************************************************************/
1198static char sg_extras_get_bv(bv_extras extras, struct extra_type *presource,
1199 const int *idx)
1200{
1201 int i, bin = 0;
1202
1203 for (i = 0; i < 4; i++) {
1204 int extra = idx[i];
1205
1206 if (extra < 0) {
1207 break;
1208 }
1209
1210 if (BV_ISSET(extras, extra)
1211 /* An invalid resource, a resource that can't exist at the tile's
1212 * current terrain, isn't in the bit extra vector. Save it so it
1213 * can return if the tile's terrain changes to something it can
1214 * exist on. */
1215 || extra_by_number(extra) == presource) {
1216 bin |= (1 << i);
1217 }
1218 }
1219
1220 return hex_chars[bin];
1221}
1222
1223/************************************************************************/
1227static struct terrain *char2terrain(char ch)
1228{
1229 /* terrain_by_identifier plus fatal error */
1231 return T_UNKNOWN;
1232 }
1233 terrain_type_iterate(pterrain) {
1234 if (pterrain->identifier_load == ch) {
1235 return pterrain;
1236 }
1238
1239 log_fatal("Unknown terrain identifier '%c' in savegame.", ch);
1240
1242
1244}
1245
1246/************************************************************************/
1250static char terrain2char(const struct terrain *pterrain)
1251{
1252 if (pterrain == T_UNKNOWN) {
1254 } else {
1255 return pterrain->identifier;
1256 }
1257}
1258
1259/************************************************************************/
1263 const char *path, int plrno)
1264{
1265 char path_with_name[128];
1266 const char *name;
1267 struct advance *padvance;
1268
1270 "%s_name", path);
1271
1273
1274 if (!name || name[0] == '\0') {
1275 /* Used by researching_saved */
1276 return A_UNKNOWN;
1277 }
1278 if (fc_strcasecmp(name, "A_FUTURE") == 0) {
1279 return A_FUTURE;
1280 }
1281 if (fc_strcasecmp(name, "A_NONE") == 0) {
1282 return A_NONE;
1283 }
1284 if (fc_strcasecmp(name, "A_UNSET") == 0) {
1285 return A_UNSET;
1286 }
1287
1290 "%s: unknown technology \"%s\".", path_with_name, name);
1291
1292 return advance_number(padvance);
1293}
1294
1295/************************************************************************/
1298static void technology_save(struct section_file *file,
1299 const char *path, int plrno, Tech_type_id tech)
1300{
1301 char path_with_name[128];
1302 const char *name;
1303
1305 "%s_name", path);
1306
1307 switch (tech) {
1308 case A_UNKNOWN: /* Used by researching_saved */
1309 name = "";
1310 break;
1311 case A_NONE:
1312 name = "A_NONE";
1313 break;
1314 case A_UNSET:
1315 name = "A_UNSET";
1316 break;
1317 case A_FUTURE:
1318 name = "A_FUTURE";
1319 break;
1320 default:
1322 break;
1323 }
1324
1326}
1327
1328/* =======================================================================
1329 * Load / save savefile data.
1330 * ======================================================================= */
1331
1332/************************************************************************/
1336{
1337 int i;
1338 const char *terr_name;
1339 bool ruleset_datafile;
1341 const char *str;
1342
1343 /* Check status and return if not OK (sg_success FALSE). */
1344 sg_check_ret();
1345
1346 /* Load savefile options. */
1347 loading->secfile_options
1348 = secfile_lookup_str(loading->file, "savefile.options");
1349
1350 /* We don't need these entries, but read them anyway to avoid
1351 * warnings about unread secfile entries. */
1352 (void) secfile_entry_by_path(loading->file, "savefile.reason");
1353 (void) secfile_entry_by_path(loading->file, "savefile.revision");
1354
1355 str = secfile_lookup_str(loading->file, "savefile.orig_version");
1357
1358 if (game.scenario.datafile[0] != '\0') {
1360 } else {
1362 }
1363
1366 const char *req_caps;
1367
1370 /* Failed to load correct ruleset */
1371 sg_failure_ret(FALSE, _("Failed to load ruleset '%s'."),
1373 }
1374
1375 req_caps = secfile_lookup_str_default(loading->file, "",
1376 "scenario.ruleset_caps");
1377 strncpy(game.scenario.req_caps, req_caps,
1378 sizeof(game.scenario.req_caps) - 1);
1379 game.scenario.req_caps[sizeof(game.scenario.req_caps) - 1] = '\0';
1380
1381 if (!has_capabilities(req_caps, game.ruleset_capabilities)) {
1382 /* Current ruleset lacks required capabilities. */
1383 log_normal(_("Scenario requires ruleset capabilities: %s"), req_caps);
1384 log_normal(_("Ruleset has capabilities: %s"),
1386 /* TRANS: ... ruleset dir ... scenario name ... */
1387 log_error(_("Current ruleset %s not compatible with the scenario %s."
1388 " Trying to switch to the ruleset specified by the"
1389 " scenario."),
1391
1393 }
1394 }
1395
1398 const char *ruleset, *alt_dir;
1399
1402 "savefile.rulesetdir");
1403
1404 /* Load ruleset. */
1406 if (!strcmp("default", game.server.rulesetdir)) {
1407 /* Here 'default' really means current default.
1408 * Saving happens with real ruleset name, so savegames containing this
1409 * are special scenarios. */
1411 log_verbose("Savegame specified ruleset '%s'. Really loading '%s'.",
1413 }
1414
1415 alt_dir = secfile_lookup_str_default(loading->file, NULL,
1416 "savefile.ruleset_alt_dir");
1417
1418 if (!load_rulesets(NULL, alt_dir, FALSE, NULL, TRUE, FALSE, ruleset_datafile)) {
1419 if (alt_dir) {
1421 _("Failed to load either of rulesets '%s' or '%s' "
1422 "needed for savegame."),
1423 ruleset, alt_dir);
1424 } else {
1426 _("Failed to load ruleset '%s' needed for savegame."),
1427 ruleset);
1428 }
1429 }
1430
1432 /* TRANS: ruleset dir */
1433 log_normal(_("Successfully loaded the scenario's ruleset %s."), ruleset);
1434 }
1435 }
1436
1437 /* Remove all aifill players. Correct number of them get created later
1438 * with correct skill level etc. */
1439 (void) aifill(0);
1440
1441 /* Time to load scenario specific luadata */
1442 if (game.scenario.datafile[0] != '\0') {
1443 if (!fc_strcasecmp("none", game.scenario.datafile)) {
1445 } else {
1446 const struct strvec *paths[] = { get_scenario_dirs(), NULL };
1447 const struct strvec **path;
1448 const char *found = NULL;
1449 char testfile[MAX_LEN_PATH];
1450 struct section_file *secfile;
1451
1452 for (path = paths; found == NULL && *path != NULL; path++) {
1453 fc_snprintf(testfile, sizeof(testfile), "%s.luadata", game.scenario.datafile);
1454
1455 found = fileinfoname(*path, testfile);
1456 }
1457
1458 if (found == NULL) {
1459 log_error(_("Can't find scenario luadata file %s.luadata."), game.scenario.datafile);
1460 sg_success = FALSE;
1461 return;
1462 }
1463
1464 secfile = secfile_load(found, FALSE);
1465 if (secfile == NULL) {
1466 log_error(_("Failed to load scenario luadata file %s.luadata"),
1468 sg_success = FALSE;
1469 return;
1470 }
1471
1472 game.server.luadata = secfile;
1473 }
1474 }
1475
1477 "savefile.dbid");
1478
1479 /* This is in the savegame only if the game has been started before savegame3.c time,
1480 * and in that case it's TRUE. If it's missing, it's to be considered FALSE. */
1482 "savefile.last_updated_as_year");
1483
1484 /* Load improvements. */
1485 loading->improvement.size
1487 "savefile.improvement_size");
1488 if (loading->improvement.size) {
1489 loading->improvement.order
1490 = secfile_lookup_str_vec(loading->file, &loading->improvement.size,
1491 "savefile.improvement_vector");
1492 sg_failure_ret(loading->improvement.size != 0,
1493 "Failed to load improvement order: %s",
1494 secfile_error());
1495 }
1496
1497 /* Load technologies. */
1498 loading->technology.size
1500 "savefile.technology_size");
1501 if (loading->technology.size) {
1502 loading->technology.order
1503 = secfile_lookup_str_vec(loading->file, &loading->technology.size,
1504 "savefile.technology_vector");
1505 sg_failure_ret(loading->technology.size != 0,
1506 "Failed to load technology order: %s",
1507 secfile_error());
1508 }
1509
1510 /* Load Activities. */
1511 loading->activities.size
1513 "savefile.activities_size");
1514 if (loading->activities.size) {
1515 loading->activities.order
1516 = secfile_lookup_str_vec(loading->file, &loading->activities.size,
1517 "savefile.activities_vector");
1518 sg_failure_ret(loading->activities.size != 0,
1519 "Failed to load activity order: %s",
1520 secfile_error());
1521 }
1522
1523 /* Load traits. */
1524 loading->trait.size
1526 "savefile.trait_size");
1527 if (loading->trait.size) {
1528 loading->trait.order
1529 = secfile_lookup_str_vec(loading->file, &loading->trait.size,
1530 "savefile.trait_vector");
1531 sg_failure_ret(loading->trait.size != 0,
1532 "Failed to load trait order: %s",
1533 secfile_error());
1534 }
1535
1536 /* Load extras. */
1537 loading->extra.size
1539 "savefile.extras_size");
1540 if (loading->extra.size) {
1541 const char **modname;
1542 size_t nmod;
1543 int j;
1544
1545 modname = secfile_lookup_str_vec(loading->file, &loading->extra.size,
1546 "savefile.extras_vector");
1547 sg_failure_ret(loading->extra.size != 0,
1548 "Failed to load extras order: %s",
1549 secfile_error());
1551 "Number of extras defined by the ruleset (= %d) are "
1552 "lower than the number in the savefile (= %d).",
1553 game.control.num_extra_types, (int)loading->extra.size);
1554 /* make sure that the size of the array is divisible by 4 */
1555 nmod = 4 * ((loading->extra.size + 3) / 4);
1556 loading->extra.order = fc_calloc(nmod, sizeof(*loading->extra.order));
1557 for (j = 0; j < loading->extra.size; j++) {
1558 loading->extra.order[j] = extra_type_by_rule_name(modname[j]);
1559 }
1560 free(modname);
1561 for (; j < nmod; j++) {
1562 loading->extra.order[j] = NULL;
1563 }
1564 }
1565
1566 /* Load multipliers. */
1567 loading->multiplier.size
1569 "savefile.multipliers_size");
1570 if (loading->multiplier.size) {
1571 const char **modname;
1572 int j;
1573
1574 modname = secfile_lookup_str_vec(loading->file, &loading->multiplier.size,
1575 "savefile.multipliers_vector");
1576 sg_failure_ret(loading->multiplier.size != 0,
1577 "Failed to load multipliers order: %s",
1578 secfile_error());
1579 /* It's OK for the set of multipliers in the savefile to differ
1580 * from those in the ruleset. */
1581 loading->multiplier.order = fc_calloc(loading->multiplier.size,
1582 sizeof(*loading->multiplier.order));
1583 for (j = 0; j < loading->multiplier.size; j++) {
1584 loading->multiplier.order[j] = multiplier_by_rule_name(modname[j]);
1585 if (!loading->multiplier.order[j]) {
1586 log_verbose("Multiplier \"%s\" in savegame but not in ruleset, "
1587 "discarding", modname[j]);
1588 }
1589 }
1590 free(modname);
1591 }
1592
1593 /* Load specialists. */
1594 loading->specialist.size
1596 "savefile.specialists_size");
1597 if (loading->specialist.size) {
1598 const char **modname;
1599 size_t nmod;
1600 int j;
1601
1602 modname = secfile_lookup_str_vec(loading->file, &loading->specialist.size,
1603 "savefile.specialists_vector");
1604 sg_failure_ret(loading->specialist.size != 0,
1605 "Failed to load specialists order: %s",
1606 secfile_error());
1608 "Number of specialists defined by the ruleset (= %d) are "
1609 "lower than the number in the savefile (= %d).",
1610 game.control.num_specialist_types, (int)loading->specialist.size);
1611 /* make sure that the size of the array is divisible by 4 */
1612 /* That's not really needed with specialists at the moment, but done this way
1613 * for consistency with other types, and to be prepared for the time it needs
1614 * to be this way. */
1615 nmod = 4 * ((loading->specialist.size + 3) / 4);
1616 loading->specialist.order = fc_calloc(nmod, sizeof(*loading->specialist.order));
1617 for (j = 0; j < loading->specialist.size; j++) {
1618 loading->specialist.order[j] = specialist_by_rule_name(modname[j]);
1619 }
1620 free(modname);
1621 for (; j < nmod; j++) {
1622 loading->specialist.order[j] = NULL;
1623 }
1624 }
1625
1626 /* Load action order. */
1627 loading->action.size = secfile_lookup_int_default(loading->file, 0,
1628 "savefile.action_size");
1629
1630 sg_failure_ret(loading->action.size > 0,
1631 "Failed to load action order: %s",
1632 secfile_error());
1633
1634 if (loading->action.size) {
1635 const char **modname;
1636 int j;
1637
1638 modname = secfile_lookup_str_vec(loading->file, &loading->action.size,
1639 "savefile.action_vector");
1640
1641 loading->action.order = fc_calloc(loading->action.size,
1642 sizeof(*loading->action.order));
1643
1644 for (j = 0; j < loading->action.size; j++) {
1646
1647 if (real_action) {
1648 loading->action.order[j] = real_action->id;
1649 } else {
1650 log_sg("Unknown action \'%s\'", modname[j]);
1651 loading->action.order[j] = ACTION_NONE;
1652 }
1653 }
1654
1655 free(modname);
1656 }
1657
1658 /* Load action decision order. */
1659 loading->act_dec.size
1661 "savefile.action_decision_size");
1662
1663 sg_failure_ret(loading->act_dec.size > 0,
1664 "Failed to load action decision order: %s",
1665 secfile_error());
1666
1667 if (loading->act_dec.size) {
1668 const char **modname;
1669 int j;
1670
1671 modname = secfile_lookup_str_vec(loading->file, &loading->act_dec.size,
1672 "savefile.action_decision_vector");
1673
1674 loading->act_dec.order = fc_calloc(loading->act_dec.size,
1675 sizeof(*loading->act_dec.order));
1676
1677 for (j = 0; j < loading->act_dec.size; j++) {
1678 loading->act_dec.order[j] = action_decision_by_name(modname[j],
1680 }
1681
1682 free(modname);
1683 }
1684
1685 /* Load server side agent order. */
1686 loading->ssa.size
1688 "savefile.server_side_agent_size");
1689
1690 sg_failure_ret(loading->ssa.size > 0,
1691 "Failed to load server side agent order: %s",
1692 secfile_error());
1693
1694 if (loading->ssa.size) {
1695 const char **modname;
1696 int j;
1697
1698 modname = secfile_lookup_str_vec(loading->file, &loading->ssa.size,
1699 "savefile.server_side_agent_list");
1700
1701 loading->ssa.order = fc_calloc(loading->ssa.size,
1702 sizeof(*loading->ssa.order));
1703
1704 for (j = 0; j < loading->ssa.size; j++) {
1705 loading->ssa.order[j] = server_side_agent_by_name(modname[j],
1707 }
1708
1709 free(modname);
1710 }
1711
1712 /* Load city options order. */
1713 loading->coptions.size
1715 "savefile.city_options_size");
1716
1717 sg_failure_ret(loading->coptions.size > 0,
1718 "Failed to load city options order: %s",
1719 secfile_error());
1720
1721 if (loading->coptions.size) {
1722 const char **modname;
1723 int j;
1724
1725 modname = secfile_lookup_str_vec(loading->file, &loading->coptions.size,
1726 "savefile.city_options_vector");
1727
1728 loading->coptions.order = fc_calloc(loading->coptions.size,
1729 sizeof(*loading->coptions.order));
1730
1731 for (j = 0; j < loading->coptions.size; j++) {
1732 loading->coptions.order[j] = city_options_by_name(modname[j],
1734 }
1735
1736 free(modname);
1737 }
1738
1739 /* Terrain identifiers */
1741 pterr->identifier_load = '\0';
1743
1744 i = 0;
1746 "savefile.terrident%d.name", i)) != NULL) {
1748
1749 if (pterr != NULL) {
1750 const char *iptr = secfile_lookup_str_default(loading->file, NULL,
1751 "savefile.terrident%d.identifier", i);
1752
1753 pterr->identifier_load = *iptr;
1754 } else {
1755 log_error("Identifier for unknown terrain type %s.", terr_name);
1756 }
1757 i++;
1758 }
1759
1762 if (pterr != pterr2 && pterr->identifier_load != '\0') {
1763 sg_failure_ret((pterr->identifier_load != pterr2->identifier_load),
1764 "%s and %s share a saved identifier",
1766 }
1769}
1770
1771/************************************************************************/
1775{
1776 int i;
1777
1778 /* Check status and return if not OK (sg_success FALSE). */
1779 sg_check_ret();
1780
1781 /* Save savefile options. */
1783
1784 secfile_insert_int(saving->file, current_compat_ver(), "savefile.version");
1785
1786 /* Save reason of the savefile generation. */
1787 secfile_insert_str(saving->file, saving->save_reason, "savefile.reason");
1788
1789 /* Save as accurate freeciv revision information as possible */
1790 secfile_insert_str(saving->file, freeciv_datafile_version(), "savefile.revision");
1791
1792 /* Freeciv version used in the very first launch of this game -
1793 * or even saving in pregame. */
1795 "savefile.orig_version");
1796
1797 /* Save rulesetdir at this point as this ruleset is required by this
1798 * savefile. */
1799 secfile_insert_str(saving->file, game.server.rulesetdir, "savefile.rulesetdir");
1800
1801 if (game.control.version[0] != '\0') {
1802 /* Current ruleset has version information, save it.
1803 * This is never loaded, but exist in savegame file only for debugging purposes. */
1804 secfile_insert_str(saving->file, game.control.version, "savefile.rulesetversion");
1805 }
1806
1807 if (game.control.alt_dir[0] != '\0') {
1808 secfile_insert_str(saving->file, game.control.alt_dir, "savefile.ruleset_alt_dir");
1809 }
1810
1812 secfile_insert_bool(saving->file, TRUE, "savefile.last_updated_as_year");
1813 }
1814
1815 secfile_insert_int(saving->file, game.server.dbid, "savefile.dbid");
1816
1817 /* Save improvement order in savegame, so we are not dependent on ruleset
1818 * order. If the game isn't started improvements aren't loaded so we can
1819 * not save the order. */
1821 "savefile.improvement_size");
1822 if (improvement_count() > 0) {
1823 const char *buf[improvement_count()];
1824
1825 improvement_iterate(pimprove) {
1826 buf[improvement_index(pimprove)] = improvement_rule_name(pimprove);
1828
1830 "savefile.improvement_vector");
1831 }
1832
1833 /* Save technology order in savegame, so we are not dependent on ruleset
1834 * order. If the game isn't started advances aren't loaded so we can not
1835 * save the order. */
1837 "savefile.technology_size");
1838 if (game.control.num_tech_types > 0) {
1839 const char *buf[game.control.num_tech_types];
1840
1841 buf[A_NONE] = "A_NONE";
1842 advance_iterate(a) {
1846 "savefile.technology_vector");
1847 }
1848
1849 /* Save activities order in the savegame. */
1851 "savefile.activities_size");
1852 if (ACTIVITY_LAST > 0) {
1853 const char **modname;
1854 int j;
1855
1856 i = 0;
1857
1859
1860 for (j = 0; j < ACTIVITY_LAST; j++) {
1862 }
1863
1866 "savefile.activities_vector");
1867 free(modname);
1868 }
1869
1870 /* Save specialists order in the savegame. */
1872 "savefile.specialists_size");
1873 {
1874 const char **modname;
1875
1876 i = 0;
1878
1882
1884 "savefile.specialists_vector");
1885
1886 free(modname);
1887 }
1888
1889 /* Save trait order in savegame. */
1891 "savefile.trait_size");
1892 {
1893 const char **modname;
1894 enum trait tr;
1895 int j;
1896
1897 modname = fc_calloc(TRAIT_COUNT, sizeof(*modname));
1898
1899 for (tr = trait_begin(), j = 0; tr != trait_end(); tr = trait_next(tr), j++) {
1900 modname[j] = trait_name(tr);
1901 }
1902
1904 "savefile.trait_vector");
1905 free(modname);
1906 }
1907
1908 /* Save extras order in the savegame. */
1910 "savefile.extras_size");
1911 if (game.control.num_extra_types > 0) {
1912 const char **modname;
1913
1914 i = 0;
1916
1917 extra_type_iterate(pextra) {
1918 modname[i++] = extra_rule_name(pextra);
1920
1923 "savefile.extras_vector");
1924 free(modname);
1925 }
1926
1927 /* Save multipliers order in the savegame. */
1929 "savefile.multipliers_size");
1930 if (multiplier_count() > 0) {
1931 const char **modname;
1932
1934
1935 multipliers_iterate(pmul) {
1938
1941 "savefile.multipliers_vector");
1942 free(modname);
1943 }
1944
1945 /* Save city_option order in the savegame. */
1947 "savefile.city_options_size");
1948 if (CITYO_LAST > 0) {
1949 const char **modname;
1950 int j;
1951
1952 i = 0;
1953 modname = fc_calloc(CITYO_LAST, sizeof(*modname));
1954
1955 for (j = 0; j < CITYO_LAST; j++) {
1956 modname[i++] = city_options_name(j);
1957 }
1958
1960 CITYO_LAST,
1961 "savefile.city_options_vector");
1962 free(modname);
1963 }
1964
1965 /* Save action order in the savegame. */
1967 "savefile.action_size");
1968 if (NUM_ACTIONS > 0) {
1969 const char **modname;
1970 int j;
1971
1972 i = 0;
1973 modname = fc_calloc(NUM_ACTIONS, sizeof(*modname));
1974
1975 for (j = 0; j < NUM_ACTIONS; j++) {
1977 }
1978
1981 "savefile.action_vector");
1982 free(modname);
1983 }
1984
1985 /* Save action decision order in the savegame. */
1987 "savefile.action_decision_size");
1988 if (ACT_DEC_COUNT > 0) {
1989 const char **modname;
1990 int j;
1991
1992 i = 0;
1994
1995 for (j = 0; j < ACT_DEC_COUNT; j++) {
1997 }
1998
2001 "savefile.action_decision_vector");
2002 free(modname);
2003 }
2004
2005 /* Save server side agent order in the savegame. */
2007 "savefile.server_side_agent_size");
2008 if (SSA_COUNT > 0) {
2009 const char **modname;
2010 int j;
2011
2012 i = 0;
2013 modname = fc_calloc(SSA_COUNT, sizeof(*modname));
2014
2015 for (j = 0; j < SSA_COUNT; j++) {
2017 }
2018
2020 SSA_COUNT,
2021 "savefile.server_side_agent_list");
2022 free(modname);
2023 }
2024
2025 /* Save terrain character mapping in the savegame. */
2026 i = 0;
2028 char buf[2];
2029
2030 secfile_insert_str(saving->file, terrain_rule_name(pterr), "savefile.terrident%d.name", i);
2032 buf[1] = '\0';
2033 secfile_insert_str(saving->file, buf, "savefile.terrident%d.identifier", i++);
2035}
2036
2037/************************************************************************/
2041 const char *option)
2042{
2043 /* Check status and return if not OK (sg_success FALSE). */
2044 sg_check_ret();
2045
2046 if (option == NULL) {
2047 /* no additional option */
2048 return;
2049 }
2050
2051 sz_strlcat(saving->secfile_options, option);
2052 secfile_replace_str(saving->file, saving->secfile_options,
2053 "savefile.options");
2054}
2055
2056/* =======================================================================
2057 * Load / save game status.
2058 * ======================================================================= */
2059
2060/************************************************************************/
2064{
2065 int i;
2066 const char *name;
2067
2068 /* Check status and return if not OK (sg_success FALSE). */
2069 sg_check_ret();
2070
2071 for (i = 0;
2073 "ruledata.government%d.name", i));
2074 i++) {
2076
2077 if (gov != NULL) {
2079 "ruledata.government%d.changes", i);
2080 }
2081 }
2082}
2083
2084/************************************************************************/
2087static void sg_load_game(struct loaddata *loading)
2088{
2089 const char *str;
2090 int i;
2091
2092 /* Check status and return if not OK (sg_success FALSE). */
2093 sg_check_ret();
2094
2095 /* Load server state. */
2096 str = secfile_lookup_str_default(loading->file, "S_S_INITIAL",
2097 "game.server_state");
2098 loading->server_state = server_states_by_name(str, strcmp);
2099 if (!server_states_is_valid(loading->server_state)) {
2100 /* Don't take any risk! */
2101 loading->server_state = S_S_INITIAL;
2102 }
2103
2105 = secfile_lookup_int_default(loading->file, 0, "game.phase_seconds");
2106
2109 "game.meta_patches");
2111
2113 /* Do not overwrite this if the user requested a specific metaserver
2114 * from the command line (option --Metaserver). */
2118 "game.meta_server"));
2119 }
2120
2121 if ('\0' == srvarg.serverid[0]) {
2122 /* Do not overwrite this if the user requested a specific metaserver
2123 * from the command line (option --serverid). */
2126 "game.serverid"));
2127 }
2128 sz_strlcpy(server.game_identifier,
2129 secfile_lookup_str_default(loading->file, "", "game.id"));
2130 /* We are not checking game_identifier legality just yet.
2131 * That's done when we are sure that rand seed has been initialized,
2132 * so that we can generate new game_identifier, if needed.
2133 * See sq_load_sanitycheck(). */
2134
2136 "game.phase_mode");
2137 if (str != NULL) {
2140 log_error("Illegal phase mode \"%s\"", str);
2142 }
2143 } else {
2144 log_error("Phase mode missing");
2145 }
2146
2148 "game.phase_mode_stored");
2149 if (str != NULL) {
2152 log_error("Illegal stored phase mode \"%s\"", str);
2154 }
2155 } else {
2156 log_error("Stored phase mode missing");
2157 }
2160 "game.phase");
2164 "game.scoreturn");
2165
2168 "game.timeoutint");
2171 "game.timeoutintinc");
2174 "game.timeoutinc");
2177 "game.timeoutincmult");
2180 "game.timeoutcounter");
2181
2182 game.info.turn
2183 = secfile_lookup_int_default(loading->file, 0, "game.turn");
2185 "game.year"), "%s", secfile_error());
2187 "game.world_peace_start"), "%s", secfile_error());
2189 = secfile_lookup_bool_default(loading->file, FALSE, "game.year_0_hack");
2190
2192 = secfile_lookup_int_default(loading->file, 0, "game.globalwarming");
2194 = secfile_lookup_int_default(loading->file, 0, "game.heating");
2196 = secfile_lookup_int_default(loading->file, 0, "game.warminglevel");
2197
2199 = secfile_lookup_int_default(loading->file, 0, "game.nuclearwinter");
2201 = secfile_lookup_int_default(loading->file, 0, "game.cooling");
2203 = secfile_lookup_int_default(loading->file, 0, "game.coolinglevel");
2204
2205 /* Savegame may have stored random_seed for documentation purposes only,
2206 * but we want to keep it for resaving. */
2207 game.server.seed = secfile_lookup_int_default(loading->file, 0, "game.random_seed");
2208
2209 /* Global advances. */
2211 "game.global_advances");
2212 if (str != NULL) {
2213 sg_failure_ret(strlen(str) == loading->technology.size,
2214 "Invalid length of 'game.global_advances' ("
2215 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
2216 strlen(str), loading->technology.size);
2217 for (i = 0; i < loading->technology.size; i++) {
2218 sg_failure_ret(str[i] == '1' || str[i] == '0',
2219 "Undefined value '%c' within 'game.global_advances'.",
2220 str[i]);
2221 if (str[i] == '1') {
2222 struct advance *padvance =
2223 advance_by_rule_name(loading->technology.order[i]);
2224
2225 if (padvance != NULL) {
2227 }
2228 }
2229 }
2230 }
2231
2233 = !secfile_lookup_bool_default(loading->file, TRUE, "game.save_players");
2234
2236 = secfile_lookup_float_default(loading->file, 0, "game.last_turn_change_time");
2237}
2238
2239/************************************************************************/
2243{
2244 int set_count = 0;
2245
2247 char path[256];
2248
2249 fc_snprintf(path, sizeof(path),
2250 "ruledata.government%d", set_count++);
2251
2253 "%s.name", path);
2254 secfile_insert_int(saving->file, pgov->changed_to_times,
2255 "%s.changes", path);
2257}
2258
2259/************************************************************************/
2262static void sg_save_game(struct savedata *saving)
2263{
2265 char global_advances[game.control.num_tech_types + 1];
2266 int i;
2267
2268 /* Check status and return if not OK (sg_success FALSE). */
2269 sg_check_ret();
2270
2271 /* Game state: once the game is no longer a new game (ie, has been
2272 * started the first time), it should always be considered a running
2273 * game for savegame purposes. */
2274 if (saving->scenario && !game.scenario.players) {
2276 } else {
2278 }
2280 "game.server_state");
2281
2282 if (game.server.phase_timer != NULL) {
2286 "game.phase_seconds");
2287 }
2288
2290 "game.meta_patches");
2291 secfile_insert_str(saving->file, meta_addr_port(), "game.meta_server");
2292
2293 secfile_insert_str(saving->file, server.game_identifier, "game.id");
2294 secfile_insert_str(saving->file, srvarg.serverid, "game.serverid");
2295
2298 "game.phase_mode");
2301 "game.phase_mode_stored");
2303 "game.phase");
2305 "game.scoreturn");
2306
2308 "game.timeoutint");
2310 "game.timeoutintinc");
2312 "game.timeoutinc");
2314 "game.timeoutincmult");
2316 "game.timeoutcounter");
2317
2318 secfile_insert_int(saving->file, game.info.turn, "game.turn");
2319 secfile_insert_int(saving->file, game.info.year, "game.year");
2320 secfile_insert_int(saving->file, game.server.world_peace_start, "game.world_peace_start");
2322 "game.year_0_hack");
2323
2325 "game.globalwarming");
2327 "game.heating");
2329 "game.warminglevel");
2330
2332 "game.nuclearwinter");
2334 "game.cooling");
2336 "game.coolinglevel");
2337 /* For debugging purposes only.
2338 * Do not save it if it's 0 (not known);
2339 * this confuses people reading this 'document' less than
2340 * saving 0. */
2341 if (game.server.seed != 0) {
2343 "game.random_seed");
2344 }
2345
2346 /* Global advances. */
2347 for (i = 0; i < game.control.num_tech_types; i++) {
2348 global_advances[i] = game.info.global_advances[i] ? '1' : '0';
2349 }
2350 global_advances[i] = '\0';
2351 secfile_insert_str(saving->file, global_advances, "game.global_advances");
2352
2353 if (!game_was_started()) {
2354 saving->save_players = FALSE;
2355 } else {
2356 if (saving->scenario) {
2357 saving->save_players = game.scenario.players;
2358 } else {
2359 saving->save_players = TRUE;
2360 }
2361#ifndef SAVE_DUMMY_TURN_CHANGE_TIME
2363 "game.last_turn_change_time");
2364#else /* SAVE_DUMMY_TURN_CHANGE_TIME */
2366 "game.last_turn_change_time");
2367#endif /* SAVE_DUMMY_TURN_CHANGE_TIME */
2368 }
2369 secfile_insert_bool(saving->file, saving->save_players,
2370 "game.save_players");
2371
2372 if (srv_state != S_S_INITIAL) {
2373 const char *ainames[ai_type_get_count()];
2374
2375 i = 0;
2376 ai_type_iterate(ait) {
2377 ainames[i] = ait->name;
2378 i++;
2380
2382 "game.ai_types");
2383 }
2384}
2385
2386/* =======================================================================
2387 * Load / save random status.
2388 * ======================================================================= */
2389
2390/************************************************************************/
2393static void sg_load_random(struct loaddata *loading)
2394{
2395 /* Check status and return if not OK (sg_success FALSE). */
2396 sg_check_ret();
2397
2398 if (secfile_lookup_bool_default(loading->file, FALSE, "random.saved")) {
2399 const char *str;
2400 int i;
2401
2403 "random.index_J"), "%s", secfile_error());
2405 "random.index_K"), "%s", secfile_error());
2407 "random.index_X"), "%s", secfile_error());
2408
2409 for (i = 0; i < 8; i++) {
2410 str = secfile_lookup_str(loading->file, "random.table%d",i);
2411 sg_failure_ret(NULL != str, "%s", secfile_error());
2412 sscanf(str, "%8x %8x %8x %8x %8x %8x %8x", &loading->rstate.v[7*i],
2413 &loading->rstate.v[7*i+1], &loading->rstate.v[7*i+2],
2414 &loading->rstate.v[7*i+3], &loading->rstate.v[7*i+4],
2415 &loading->rstate.v[7*i+5], &loading->rstate.v[7*i+6]);
2416 }
2417 loading->rstate.is_init = TRUE;
2418 fc_rand_set_state(loading->rstate);
2419 } else {
2420 /* No random values - mark the setting. */
2421 (void) secfile_entry_by_path(loading->file, "random.saved");
2422
2423 /* We're loading a game without a seed (which is okay, if it's a scenario).
2424 * We need to generate the game seed now because it will be needed later
2425 * during the load. */
2427 loading->rstate = fc_rand_state();
2428 }
2429}
2430
2431/************************************************************************/
2434static void sg_save_random(struct savedata *saving)
2435{
2436 /* Check status and return if not OK (sg_success FALSE). */
2437 sg_check_ret();
2438
2439 if (fc_rand_is_init() && (!saving->scenario || game.scenario.save_random)) {
2440 int i;
2441 RANDOM_STATE rstate = fc_rand_state();
2442
2443 secfile_insert_bool(saving->file, TRUE, "random.saved");
2444 fc_assert(rstate.is_init);
2445
2446 secfile_insert_int(saving->file, rstate.j, "random.index_J");
2447 secfile_insert_int(saving->file, rstate.k, "random.index_K");
2448 secfile_insert_int(saving->file, rstate.x, "random.index_X");
2449
2450 for (i = 0; i < 8; i++) {
2451 char vec[100];
2452
2453 fc_snprintf(vec, sizeof(vec),
2454 "%8x %8x %8x %8x %8x %8x %8x", rstate.v[7 * i],
2455 rstate.v[7 * i + 1], rstate.v[7 * i + 2],
2456 rstate.v[7 * i + 3], rstate.v[7 * i + 4],
2457 rstate.v[7 * i + 5], rstate.v[7 * i + 6]);
2458 secfile_insert_str(saving->file, vec, "random.table%d", i);
2459 }
2460 } else {
2461 secfile_insert_bool(saving->file, FALSE, "random.saved");
2462 }
2463}
2464
2465/* =======================================================================
2466 * Load / save lua script data.
2467 * ======================================================================= */
2468
2469/************************************************************************/
2472static void sg_load_script(struct loaddata *loading)
2473{
2474 /* Check status and return if not OK (sg_success FALSE). */
2475 sg_check_ret();
2476
2478}
2479
2480/************************************************************************/
2483static void sg_save_script(struct savedata *saving)
2484{
2485 /* Check status and return if not OK (sg_success FALSE). */
2486 sg_check_ret();
2487
2489}
2490
2491/* =======================================================================
2492 * Load / save scenario data.
2493 * ======================================================================= */
2494
2495/************************************************************************/
2499{
2500 const char *buf;
2501 int game_version;
2502
2503 /* Check status and return if not OK (sg_success FALSE). */
2504 sg_check_ret();
2505
2506 /* Load version. */
2508 = secfile_lookup_int_default(loading->file, 0, "scenario.game_version");
2509 /* We require at least version 2.90.99 - and at that time we saved version
2510 * numbers as 10000*MAJOR+100*MINOR+PATCH */
2511 sg_failure_ret(29099 <= game_version, "Saved game is too old, at least "
2512 "version 2.90.99 required.");
2513
2514 loading->full_version = game_version;
2515
2516 game.scenario.datafile[0] = '\0';
2517
2519 "scenario.is_scenario"), "%s", secfile_error());
2520 if (!game.scenario.is_scenario) {
2521 return;
2522 }
2523
2524 buf = secfile_lookup_str_default(loading->file, "", "scenario.name");
2525 if (buf[0] != '\0') {
2527 }
2528
2530 "scenario.authors");
2531 if (buf[0] != '\0') {
2533 } else {
2534 game.scenario.authors[0] = '\0';
2535 }
2536
2538 "scenario.description");
2539 if (buf[0] != '\0') {
2541 } else {
2542 game.scenario_desc.description[0] = '\0';
2543 }
2545 = secfile_lookup_bool_default(loading->file, FALSE, "scenario.save_random");
2547 = secfile_lookup_bool_default(loading->file, TRUE, "scenario.players");
2550 "scenario.startpos_nations");
2553 "scenario.prevent_new_cities");
2556 "scenario.lake_flooding");
2559 "scenario.handmade");
2562 "scenario.allow_ai_type_fallback");
2563
2566 "scenario.ruleset_locked");
2567
2569 "scenario.datafile");
2570 if (buf[0] != '\0') {
2572 }
2573
2574 sg_failure_ret(loading->server_state == S_S_INITIAL
2575 || (loading->server_state == S_S_RUNNING
2576 && game.scenario.players),
2577 "Invalid scenario definition (server state '%s' and "
2578 "players are %s).",
2579 server_states_name(loading->server_state),
2580 game.scenario.players ? "saved" : "not saved");
2581}
2582
2583/************************************************************************/
2587{
2588 struct entry *mod_entry;
2589 int game_version;
2590
2591 /* Check status and return if not OK (sg_success FALSE). */
2592 sg_check_ret();
2593
2594 game_version = MAJOR_VERSION * 1000000 + MINOR_VERSION * 10000 + PATCH_VERSION * 100;
2595#ifdef EMERGENCY_VERSION
2597#endif /* EMERGENCY_VERSION */
2598 secfile_insert_int(saving->file, game_version, "scenario.game_version");
2599
2600 if (!saving->scenario || !game.scenario.is_scenario) {
2601 secfile_insert_bool(saving->file, FALSE, "scenario.is_scenario");
2602 return;
2603 }
2604
2605 secfile_insert_bool(saving->file, TRUE, "scenario.is_scenario");
2606
2607 /* Name is mandatory to the level that is saved even if empty. */
2608 mod_entry = secfile_insert_str(saving->file, game.scenario.name, "scenario.name");
2610
2611 /* Authors list is saved only if it exist */
2612 if (game.scenario.authors[0] != '\0') {
2614 "scenario.authors");
2616 }
2617
2618 /* Description is saved only if it exist */
2619 if (game.scenario_desc.description[0] != '\0') {
2621 "scenario.description");
2623 }
2624
2625 secfile_insert_bool(saving->file, game.scenario.save_random, "scenario.save_random");
2626 secfile_insert_bool(saving->file, game.scenario.players, "scenario.players");
2628 "scenario.startpos_nations");
2631 "scenario.prevent_new_cities");
2632 }
2634 "scenario.lake_flooding");
2635 if (game.scenario.handmade) {
2637 "scenario.handmade");
2638 }
2641 "scenario.allow_ai_type_fallback");
2642 }
2643
2644 if (game.scenario.datafile[0] != '\0') {
2646 "scenario.datafile");
2647 }
2649 "scenario.ruleset_locked");
2650 if (!game.scenario.ruleset_locked && game.scenario.req_caps[0] != '\0') {
2652 "scenario.ruleset_caps");
2653 }
2654}
2655
2656/* =======================================================================
2657 * Load / save game settings.
2658 * ======================================================================= */
2659
2660/************************************************************************/
2664{
2665 /* Check status and return if not OK (sg_success FALSE). */
2666 sg_check_ret();
2667
2668 settings_game_load(loading->file, "settings");
2669
2670 /* Save current status of fogofwar. */
2672
2673 /* Add all compatibility settings here. */
2674}
2675
2676/************************************************************************/
2680{
2682
2683 /* Check status and return if not OK (sg_success FALSE). */
2684 sg_check_ret();
2685
2686 if (saving->scenario) {
2687 wld.map.server.generator = MAPGEN_SCENARIO; /* We want a scenario. */
2688 }
2689
2690 settings_game_save(saving->file, "settings");
2691 /* Restore real map generator. */
2693
2694 /* Add all compatibility settings here. */
2695}
2696
2697
2698/************************************************************************/
2702{
2703 struct city *pcity;
2704 int i, j;
2705 size_t length, length2;
2706 int *city_count;
2708 "savefile.city_counters_order_size");
2709
2710 if (0==length) {
2711
2712 return;
2713 }
2714
2715 loading->counter.order = secfile_lookup_str_vec(loading->file, &loading->counter.size, "savefile.city_counters_order_vector");
2716
2717 sg_failure_ret(loading->counter.order != 0,
2718 "Failed to load counter's ruleset order: %s",
2719 secfile_error());
2720 sg_failure_ret(loading->counter.size = length,
2721 "Counter vector in savegame have bad size: %s",
2722 secfile_error());
2723
2724 int corder[length];
2725
2726 for (i = 0; i < length; i++) {
2727
2728 struct counter *ctg = counter_by_rule_name(loading->counter.order[i]);
2730 }
2731
2732 i = 0;
2733 while (NULL != (city_count =
2735 "counters.c%d", i))) {
2736
2737 sg_failure_ret((length2 -1 == (size_t) length), ( "Bad city counters vector size. Should be " SIZE_T_PRINTF ". Is " SIZE_T_PRINTF "." ), length, length2 - 1);
2738
2740
2741 sg_failure_ret(NULL != pcity, "City with id %d not found. Is savegame malformed? Abort loading.", city_count[0]);
2742
2743 for (j = 0; j < length; j++) {
2744
2745 pcity->counter_values[corder[j]] = city_count[j+1];
2746 }
2747 i++;
2748 }
2749}
2750
2751/************************************************************************/
2755{
2756 /* Check status and return if not OK (sg_success FALSE). */
2757 sg_check_ret();
2758
2759 const char **countnames;
2760 int *countvalues;
2761 int i, j, count;
2762
2764
2765 secfile_insert_int(saving->file, count,
2766 "savefile.city_counters_order_size");
2767
2768 if (0 == count) {
2769
2770 return;
2771 }
2772
2773 countnames = fc_calloc(count, sizeof(*countnames));
2774 for (j = 0; j < count; j++) {
2776 }
2777
2779 "savefile.city_counters_order_vector");
2780
2782
2783 // Saving city's counters
2784
2785 j = 0;
2786 countvalues = fc_calloc(count+1, sizeof(*countvalues));
2787
2789
2790 city_list_iterate(_pplayer->cities, pcity) {
2791
2792 countvalues[0] = pcity->id;
2793 for (i = 0; i < count; ++i) {
2794
2795 countvalues[i+1] = pcity->counter_values[i];
2796 }
2797
2798 secfile_insert_int_vec(saving->file, countvalues, count + 1, "counters.c%d", j);
2799 ++j;
2802
2804}
2805
2806/* =======================================================================
2807 * Load / save the main map.
2808 * ======================================================================= */
2809
2810/************************************************************************/
2813static void sg_load_map(struct loaddata *loading)
2814{
2815 /* Check status and return if not OK (sg_success FALSE). */
2816 sg_check_ret();
2817
2818 /* This defaults to TRUE even if map has not been generated.
2819 * We rely on that
2820 * 1) scenario maps have it explicitly right.
2821 * 2) when map is actually generated, it re-initialize this to FALSE. */
2823 = secfile_lookup_bool_default(loading->file, TRUE, "map.have_huts");
2824
2826 "map.altitude"),
2827 "%s", secfile_error());
2828
2830 = secfile_lookup_bool_default(loading->file, TRUE, "map.have_resources");
2831
2833
2834 /* Savegame may have stored random_seed for documentation purposes only,
2835 * but we want to keep it for resaving. */
2837 = secfile_lookup_int_default(loading->file, 0, "map.random_seed");
2838
2839 if (S_S_INITIAL == loading->server_state
2841 /* Generator MAPGEN_SCENARIO is used;
2842 * this map was done with the map editor. */
2843
2844 /* Load tiles. */
2848
2849 /* Nothing more needed for a scenario. */
2850 secfile_entry_ignore(loading->file, "game.save_known");
2851
2852 return;
2853 }
2854
2855 if (S_S_INITIAL == loading->server_state) {
2856 /* Nothing more to do if it is not a scenario but in initial state. */
2857 return;
2858 }
2859
2861 if (wld.map.altitude_info) {
2863 }
2869}
2870
2871/************************************************************************/
2874static void sg_save_map(struct savedata *saving)
2875{
2876 /* Check status and return if not OK (sg_success FALSE). */
2877 sg_check_ret();
2878
2879 if (map_is_empty()) {
2880 /* No map. */
2881 return;
2882 }
2883
2884 if (saving->scenario) {
2886 "map.have_huts");
2888 "map.have_resources");
2889 } else {
2890 secfile_insert_bool(saving->file, TRUE, "map.have_huts");
2891 secfile_insert_bool(saving->file, TRUE, "map.have_resources");
2892 }
2893
2894 secfile_insert_bool(saving->file, wld.map.altitude_info, "map.altitude");
2895
2896 /* For debugging purposes only.
2897 * Do not save it if it's 0 (not known);
2898 * this confuses people reading this 'document' less than
2899 * saving 0. */
2900 if (wld.map.server.seed != 0) {
2902 "map.random_seed");
2903 }
2904
2906 if (wld.map.altitude_info) {
2908 }
2914}
2915
2916/************************************************************************/
2920{
2921 /* Check status and return if not OK (sg_success FALSE). */
2922 sg_check_ret();
2923
2924 /* Initialize the map for the current topology. 'map.xsize' and
2925 * 'map.ysize' must be set. */
2927
2928 /* Allocate map. */
2930
2931 /* get the terrain type */
2932 LOAD_MAP_CHAR(ch, ptile, ptile->terrain = char2terrain(ch), loading->file,
2933 "map.t%04d");
2935
2936 /* Check for special tile sprites. */
2937 whole_map_iterate(&(wld.map), ptile) {
2938 const char *spec_sprite;
2939 const char *label;
2940 int nat_x, nat_y;
2941
2943 spec_sprite = secfile_lookup_str(loading->file, "map.spec_sprite_%d_%d",
2944 nat_x, nat_y);
2945 label = secfile_lookup_str_default(loading->file, NULL, "map.label_%d_%d",
2946 nat_x, nat_y);
2947 if (NULL != ptile->spec_sprite) {
2948 ptile->spec_sprite = fc_strdup(spec_sprite);
2949 }
2950 if (label != NULL) {
2951 tile_set_label(ptile, label);
2952 }
2954}
2955
2956/************************************************************************/
2960{
2961 /* Check status and return if not OK (sg_success FALSE). */
2962 sg_check_ret();
2963
2964 /* Save the terrain type. */
2965 SAVE_MAP_CHAR(ptile, terrain2char(ptile->terrain), saving->file,
2966 "map.t%04d");
2967
2968 /* Save special tile sprites. */
2969 whole_map_iterate(&(wld.map), ptile) {
2970 int nat_x, nat_y;
2971
2973 if (ptile->spec_sprite) {
2974 secfile_insert_str(saving->file, ptile->spec_sprite,
2975 "map.spec_sprite_%d_%d", nat_x, nat_y);
2976 }
2977 if (ptile->label != NULL) {
2978 secfile_insert_str(saving->file, ptile->label,
2979 "map.label_%d_%d", nat_x, nat_y);
2980 }
2982}
2983
2984/************************************************************************/
2988{
2989 int y;
2990
2991 /* Check status and return if not OK (sg_success FALSE). */
2992 sg_check_ret();
2993
2994 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
2995 const char *buffer = secfile_lookup_str(loading->file,
2996 "map.alt%04d", y);
2997 const char *ptr = buffer;
2998 int x;
2999
3000 sg_failure_ret(buffer != nullptr, "%s", secfile_error());
3001
3002 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3003 char token[TOKEN_SIZE];
3004 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3005 int number;
3006
3007 scanin(&ptr, ",", token, sizeof(token));
3008 sg_failure_ret(token[0] != '\0',
3009 "Map size not correct (map.alt%d).", y);
3010 sg_failure_ret(str_to_int(token, &number),
3011 "Got map alt %s in (%d, %d).", token, x, y);
3012 ptile->altitude = number;
3013 }
3014 }
3015}
3016
3017/************************************************************************/
3021{
3022 int y;
3023
3024 /* Check status and return if not OK (sg_success FALSE). */
3025 sg_check_ret();
3026
3027 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3029 int x;
3030
3031 line[0] = '\0';
3032 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3033 char token[TOKEN_SIZE];
3034 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3035
3036 fc_snprintf(token, sizeof(token), "%d", ptile->altitude);
3037
3038 strcat(line, token);
3039 if (x + 1 < MAP_NATIVE_WIDTH) {
3040 strcat(line, ",");
3041 }
3042 }
3043
3044 secfile_insert_str(saving->file, line, "map.alt%04d", y);
3045 }
3046}
3047
3048/************************************************************************/
3052{
3053 /* Check status and return if not OK (sg_success FALSE). */
3054 sg_check_ret();
3055
3056 /* Load extras. */
3057 halfbyte_iterate_extras(j, loading->extra.size) {
3058 LOAD_MAP_CHAR(ch, ptile, sg_extras_set_bv(&ptile->extras, ch,
3059 loading->extra.order + 4 * j),
3060 loading->file, "map.e%02d_%04d", j);
3062
3063 if (S_S_INITIAL != loading->server_state
3066 whole_map_iterate(&(wld.map), ptile) {
3068 if (tile_has_extra(ptile, pres)) {
3069 tile_set_resource(ptile, pres);
3070
3071 if (!terrain_has_resource(ptile->terrain, ptile->resource)) {
3072 BV_CLR(ptile->extras, extra_index(pres));
3073 }
3074 }
3077 }
3078}
3079
3080/************************************************************************/
3084{
3085 /* Check status and return if not OK (sg_success FALSE). */
3086 sg_check_ret();
3087
3088 /* Save extras. */
3090 int mod[4];
3091 int l;
3092
3093 for (l = 0; l < 4; l++) {
3094 if (4 * j + 1 > game.control.num_extra_types) {
3095 mod[l] = -1;
3096 } else {
3097 mod[l] = 4 * j + l;
3098 }
3099 }
3100 SAVE_MAP_CHAR(ptile, sg_extras_get_bv(ptile->extras, ptile->resource, mod),
3101 saving->file, "map.e%02d_%04d", j);
3103}
3104
3105/************************************************************************/
3110{
3111 struct nation_type *pnation;
3112 struct startpos *psp;
3113 struct tile *ptile;
3114 const char SEPARATOR = '#';
3115 const char *nation_names;
3116 int nat_x, nat_y;
3117 bool exclude;
3118 int i, startpos_count;
3119
3120 /* Check status and return if not OK (sg_success FALSE). */
3121 sg_check_ret();
3122
3124 = secfile_lookup_int_default(loading->file, 0, "map.startpos_count");
3125
3126 if (0 == startpos_count) {
3127 /* Nothing to do. */
3128 return;
3129 }
3130
3131 for (i = 0; i < startpos_count; i++) {
3132 if (!secfile_lookup_int(loading->file, &nat_x, "map.startpos%d.x", i)
3133 || !secfile_lookup_int(loading->file, &nat_y,
3134 "map.startpos%d.y", i)) {
3135 log_sg("Warning: Undefined coordinates for startpos %d", i);
3136 continue;
3137 }
3138
3139 ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
3140 if (NULL == ptile) {
3141 log_error("Start position native coordinates (%d, %d) do not exist "
3142 "in this map. Skipping...", nat_x, nat_y);
3143 continue;
3144 }
3145
3146 exclude = secfile_lookup_bool_default(loading->file, FALSE,
3147 "map.startpos%d.exclude", i);
3148
3149 psp = map_startpos_new(ptile);
3150
3152 "map.startpos%d.nations", i);
3153 if (NULL != nation_names && '\0' != nation_names[0]) {
3154 const size_t size = strlen(nation_names) + 1;
3155 char buf[size], *start, *end;
3156
3158 for (start = buf - 1; NULL != start; start = end) {
3159 start++;
3160 if ((end = strchr(start, SEPARATOR))) {
3161 *end = '\0';
3162 }
3163
3164 pnation = nation_by_rule_name(start);
3165 if (NO_NATION_SELECTED != pnation) {
3166 if (exclude) {
3167 startpos_disallow(psp, pnation);
3168 } else {
3169 startpos_allow(psp, pnation);
3170 }
3171 } else {
3172 log_verbose("Missing nation \"%s\".", start);
3173 }
3174 }
3175 }
3176 }
3177
3178 if (0 < map_startpos_count()
3179 && loading->server_state == S_S_INITIAL
3181 log_verbose("Number of starts (%d) are lower than rules.max_players "
3182 "(%d), lowering rules.max_players.",
3185 }
3186
3187 /* Re-initialize nation availability in light of start positions.
3188 * This has to be after loading [scenario] and [map].startpos and
3189 * before we seek nations for players. */
3191}
3192
3193/************************************************************************/
3197{
3198 struct tile *ptile;
3199 const char SEPARATOR = '#';
3200 int i = 0;
3201
3202 /* Check status and return if not OK (sg_success FALSE). */
3203 sg_check_ret();
3204
3206 return;
3207 }
3208
3210 "map.startpos_count");
3211
3213 int nat_x, nat_y;
3214
3215 ptile = startpos_tile(psp);
3216
3218 secfile_insert_int(saving->file, nat_x, "map.startpos%d.x", i);
3219 secfile_insert_int(saving->file, nat_y, "map.startpos%d.y", i);
3220
3222 "map.startpos%d.exclude", i);
3223 if (startpos_allows_all(psp)) {
3224 secfile_insert_str(saving->file, "", "map.startpos%d.nations", i);
3225 } else {
3226 const struct nation_hash *nations = startpos_raw_nations(psp);
3228
3229 nation_names[0] = '\0';
3230 nation_hash_iterate(nations, pnation) {
3231 if ('\0' == nation_names[0]) {
3233 sizeof(nation_names));
3234 } else {
3236 "%c%s", SEPARATOR, nation_rule_name(pnation));
3237 }
3240 "map.startpos%d.nations", i);
3241 }
3242 i++;
3244
3246}
3247
3248/************************************************************************/
3252{
3253 int y;
3254 struct tile *claimer = NULL;
3255 struct extra_type *placing = NULL;
3256
3257 /* Check status and return if not OK (sg_success FALSE). */
3258 sg_check_ret();
3259
3260 if (game.info.is_new_game) {
3261 /* No owner/source information for a new game / scenario. */
3262 return;
3263 }
3264
3265 /* Owner, ownership source, and infra turns are stored as plain numbers */
3266 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3267 const char *buffer1 = secfile_lookup_str(loading->file,
3268 "map.owner%04d", y);
3269 const char *buffer2 = secfile_lookup_str(loading->file,
3270 "map.source%04d", y);
3271 const char *buffer3 = secfile_lookup_str(loading->file,
3272 "map.eowner%04d", y);
3274 NULL,
3275 "map.placing%04d", y);
3277 NULL,
3278 "map.infra_turns%04d", y);
3279 const char *ptr1 = buffer1;
3280 const char *ptr2 = buffer2;
3281 const char *ptr3 = buffer3;
3282 const char *ptr_placing = buffer_placing;
3283 const char *ptr_turns = buffer_turns;
3284 int x;
3285
3289
3290 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3291 char token1[TOKEN_SIZE];
3292 char token2[TOKEN_SIZE];
3293 char token3[TOKEN_SIZE];
3295 char token_turns[TOKEN_SIZE];
3296 struct player *owner = NULL;
3297 struct player *eowner = NULL;
3298 int turns;
3299 int number;
3300 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3301
3302 scanin(&ptr1, ",", token1, sizeof(token1));
3303 sg_failure_ret(token1[0] != '\0',
3304 "Map size not correct (map.owner%d).", y);
3305 if (strcmp(token1, "-") == 0) {
3306 owner = NULL;
3307 } else {
3309 "Got map owner %s in (%d, %d).", token1, x, y);
3310 owner = player_by_number(number);
3311 }
3312
3313 scanin(&ptr2, ",", token2, sizeof(token2));
3314 sg_failure_ret(token2[0] != '\0',
3315 "Map size not correct (map.source%d).", y);
3316 if (strcmp(token2, "-") == 0) {
3317 claimer = NULL;
3318 } else {
3320 "Got map source %s in (%d, %d).", token2, x, y);
3321 claimer = index_to_tile(&(wld.map), number);
3322 }
3323
3324 scanin(&ptr3, ",", token3, sizeof(token3));
3325 sg_failure_ret(token3[0] != '\0',
3326 "Map size not correct (map.eowner%d).", y);
3327 if (strcmp(token3, "-") == 0) {
3328 eowner = NULL;
3329 } else {
3331 "Got base owner %s in (%d, %d).", token3, x, y);
3332 eowner = player_by_number(number);
3333 }
3334
3335 if (ptr_placing != NULL) {
3337 sg_failure_ret(token_placing[0] != '\0',
3338 "Map size not correct (map.placing%d).", y);
3339 if (strcmp(token_placing, "-") == 0) {
3340 placing = NULL;
3341 } else {
3343 "Got placing extra %s in (%d, %d).", token_placing, x, y);
3344 placing = extra_by_number(number);
3345 }
3346 } else {
3347 placing = NULL;
3348 }
3349
3350 if (ptr_turns != NULL) {
3351 scanin(&ptr_turns, ",", token_turns, sizeof(token_turns));
3352 sg_failure_ret(token_turns[0] != '\0',
3353 "Map size not correct (map.infra_turns%d).", y);
3355 "Got infra_turns %s in (%d, %d).", token_turns, x, y);
3356 turns = number;
3357 } else {
3358 turns = 1;
3359 }
3360
3362 tile_claim_bases(ptile, eowner);
3363 ptile->placing = placing;
3364 ptile->infra_turns = turns;
3365 log_debug("extras_owner(%d, %d) = %s", TILE_XY(ptile), player_name(eowner));
3366 }
3367 }
3368}
3369
3370/************************************************************************/
3374{
3375 int y;
3376
3377 /* Check status and return if not OK (sg_success FALSE). */
3378 sg_check_ret();
3379
3380 if (saving->scenario && !saving->save_players) {
3381 /* Nothing to do for a scenario without saved players. */
3382 return;
3383 }
3384
3385 /* Store owner and ownership source as plain numbers. */
3386 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3388 int x;
3389
3390 line[0] = '\0';
3391 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3392 char token[TOKEN_SIZE];
3393 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3394
3395 if (!saving->save_players || tile_owner(ptile) == NULL) {
3396 strcpy(token, "-");
3397 } else {
3398 fc_snprintf(token, sizeof(token), "%d",
3399 player_number(tile_owner(ptile)));
3400 }
3401 strcat(line, token);
3402 if (x + 1 < MAP_NATIVE_WIDTH) {
3403 strcat(line, ",");
3404 }
3405 }
3406
3407 secfile_insert_str(saving->file, line, "map.owner%04d", y);
3408 }
3409
3410 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3412 int x;
3413
3414 line[0] = '\0';
3415 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3416 char token[TOKEN_SIZE];
3417 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3418
3419 if (ptile->claimer == NULL) {
3420 strcpy(token, "-");
3421 } else {
3422 fc_snprintf(token, sizeof(token), "%d", tile_index(ptile->claimer));
3423 }
3424 strcat(line, token);
3425 if (x + 1 < MAP_NATIVE_WIDTH) {
3426 strcat(line, ",");
3427 }
3428 }
3429
3430 secfile_insert_str(saving->file, line, "map.source%04d", y);
3431 }
3432
3433 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3435 int x;
3436
3437 line[0] = '\0';
3438 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3439 char token[TOKEN_SIZE];
3440 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3441
3442 if (!saving->save_players || extra_owner(ptile) == NULL) {
3443 strcpy(token, "-");
3444 } else {
3445 fc_snprintf(token, sizeof(token), "%d",
3446 player_number(extra_owner(ptile)));
3447 }
3448 strcat(line, token);
3449 if (x + 1 < MAP_NATIVE_WIDTH) {
3450 strcat(line, ",");
3451 }
3452 }
3453
3454 secfile_insert_str(saving->file, line, "map.eowner%04d", y);
3455 }
3456
3457 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3459 int x;
3460
3461 line[0] = '\0';
3462 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3463 char token[TOKEN_SIZE];
3464 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3465
3466 if (ptile->placing == NULL) {
3467 strcpy(token, "-");
3468 } else {
3469 fc_snprintf(token, sizeof(token), "%d",
3470 extra_number(ptile->placing));
3471 }
3472 strcat(line, token);
3473 if (x + 1 < MAP_NATIVE_WIDTH) {
3474 strcat(line, ",");
3475 }
3476 }
3477
3478 secfile_insert_str(saving->file, line, "map.placing%04d", y);
3479 }
3480
3481 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3483 int x;
3484
3485 line[0] = '\0';
3486 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3487 char token[TOKEN_SIZE];
3488 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3489
3490 if (ptile->placing != NULL) {
3491 fc_snprintf(token, sizeof(token), "%d",
3492 ptile->infra_turns);
3493 } else {
3494 fc_snprintf(token, sizeof(token), "0");
3495 }
3496 strcat(line, token);
3497 if (x + 1 < MAP_NATIVE_WIDTH) {
3498 strcat(line, ",");
3499 }
3500 }
3501
3502 secfile_insert_str(saving->file, line, "map.infra_turns%04d", y);
3503 }
3504}
3505
3506/************************************************************************/
3510{
3511 int x, y;
3512
3513 /* Check status and return if not OK (sg_success FALSE). */
3514 sg_check_ret();
3515
3516 sg_failure_ret(loading->worked_tiles == NULL,
3517 "City worked map not loaded!");
3518
3519 loading->worked_tiles = fc_malloc(MAP_INDEX_SIZE *
3520 sizeof(*loading->worked_tiles));
3521
3522 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3523 const char *buffer = secfile_lookup_str(loading->file, "map.worked%04d",
3524 y);
3525 const char *ptr = buffer;
3526
3527 sg_failure_ret(NULL != buffer,
3528 "Savegame corrupt - map line %d not found.", y);
3529 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3530 char token[TOKEN_SIZE];
3531 int number;
3532 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3533
3534 scanin(&ptr, ",", token, sizeof(token));
3535 sg_failure_ret('\0' != token[0],
3536 "Savegame corrupt - map size not correct.");
3537 if (strcmp(token, "-") == 0) {
3538 number = -1;
3539 } else {
3540 sg_failure_ret(str_to_int(token, &number) && 0 < number,
3541 "Savegame corrupt - got tile worked by city "
3542 "id=%s in (%d, %d).", token, x, y);
3543 }
3544
3545 loading->worked_tiles[ptile->index] = number;
3546 }
3547 }
3548}
3549
3550/************************************************************************/
3554{
3555 int x, y;
3556
3557 /* Check status and return if not OK (sg_success FALSE). */
3558 sg_check_ret();
3559
3560 if (saving->scenario && !saving->save_players) {
3561 /* Nothing to do for a scenario without saved players. */
3562 return;
3563 }
3564
3565 /* Additionally save the tiles worked by the cities */
3566 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
3568
3569 line[0] = '\0';
3570 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
3571 char token[TOKEN_SIZE];
3572 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
3573 struct city *pcity = tile_worked(ptile);
3574
3575 if (pcity == NULL) {
3576 strcpy(token, "-");
3577 } else {
3578 fc_snprintf(token, sizeof(token), "%d", pcity->id);
3579 }
3580 strcat(line, token);
3581 if (x < MAP_NATIVE_WIDTH) {
3582 strcat(line, ",");
3583 }
3584 }
3585 secfile_insert_str(saving->file, line, "map.worked%04d", y);
3586 }
3587}
3588
3589/************************************************************************/
3593{
3594 /* Check status and return if not OK (sg_success FALSE). */
3595 sg_check_ret();
3596
3597 players_iterate(pplayer) {
3598 /* Allocate player private map here; it is needed in different modules
3599 * besides this one ((i.e. sg_load_player_*()). */
3600 player_map_init(pplayer);
3602
3604 "game.save_known")) {
3605 int lines = player_slot_max_used_number() / 32 + 1;
3606 int j, p, l, i;
3607 unsigned int *known = fc_calloc(lines * MAP_INDEX_SIZE, sizeof(*known));
3608
3609 for (l = 0; l < lines; l++) {
3610 for (j = 0; j < 8; j++) {
3611 for (i = 0; i < 4; i++) {
3612 /* Only bother trying to load the map for this halfbyte if at least
3613 * one of the corresponding player slots is in use. */
3614 if (player_slot_is_used(player_slot_by_number(l*32 + j*4 + i))) {
3615 LOAD_MAP_CHAR(ch, ptile,
3616 known[l * MAP_INDEX_SIZE + tile_index(ptile)]
3617 |= ascii_hex2bin(ch, j),
3618 loading->file, "map.k%02d_%04d", l * 8 + j);
3619 break;
3620 }
3621 }
3622 }
3623 }
3624
3625 players_iterate(pplayer) {
3626 dbv_clr_all(&pplayer->tile_known);
3628
3629 /* HACK: we read the known data from hex into 32-bit integers, and
3630 * now we convert it to the known tile data of each player. */
3631 whole_map_iterate(&(wld.map), ptile) {
3632 players_iterate(pplayer) {
3633 p = player_index(pplayer);
3634 l = player_index(pplayer) / 32;
3635
3636 if (known[l * MAP_INDEX_SIZE + tile_index(ptile)] & (1u << (p % 32))) {
3637 map_set_known(ptile, pplayer);
3638 }
3641
3642 FC_FREE(known);
3643 }
3644}
3645
3646/************************************************************************/
3650{
3651 /* Check status and return if not OK (sg_success FALSE). */
3652 sg_check_ret();
3653
3654 if (!saving->save_players) {
3655 secfile_insert_bool(saving->file, FALSE, "game.save_known");
3656 return;
3657 } else {
3658 int lines = player_slot_max_used_number() / 32 + 1;
3659
3661 "game.save_known");
3663 int j, p, l, i;
3664 unsigned int *known = fc_calloc(lines * MAP_INDEX_SIZE, sizeof(*known));
3665
3666 /* HACK: we convert the data into a 32-bit integer, and then save it as
3667 * hex. */
3668
3669 whole_map_iterate(&(wld.map), ptile) {
3670 players_iterate(pplayer) {
3671 if (map_is_known(ptile, pplayer)) {
3672 p = player_index(pplayer);
3673 l = p / 32;
3674 known[l * MAP_INDEX_SIZE + tile_index(ptile)]
3675 |= (1u << (p % 32)); /* "p % 32" = "p - l * 32" */
3676 }
3679
3680 for (l = 0; l < lines; l++) {
3681 for (j = 0; j < 8; j++) {
3682 for (i = 0; i < 4; i++) {
3683 /* Only bother saving the map for this halfbyte if at least one
3684 * of the corresponding player slots is in use */
3685 if (player_slot_is_used(player_slot_by_number(l*32 + j*4 + i))) {
3686 /* put 4-bit segments of the 32-bit "known" field */
3688 + tile_index(ptile)], j),
3689 saving->file, "map.k%02d_%04d", l * 8 + j);
3690 break;
3691 }
3692 }
3693 }
3694 }
3695
3696 FC_FREE(known);
3697 }
3698 }
3699}
3700
3701/* =======================================================================
3702 * Load / save player data.
3703 *
3704 * This is split into two parts as some data can only be loaded if the
3705 * number of players is known and the corresponding player slots are
3706 * defined.
3707 * ======================================================================= */
3708
3709/************************************************************************/
3713{
3714 int i, k, nplayers;
3715 const char *str;
3716 bool shuffle_loaded = TRUE;
3717
3718 /* Check status and return if not OK (sg_success FALSE). */
3719 sg_check_ret();
3720
3721 if (S_S_INITIAL == loading->server_state
3722 || game.info.is_new_game) {
3723 /* Nothing more to do. */
3724 return;
3725 }
3726
3727 /* Load destroyed wonders: */
3729 "players.destroyed_wonders");
3730 sg_failure_ret(str != NULL, "%s", secfile_error());
3731 sg_failure_ret(strlen(str) == loading->improvement.size,
3732 "Invalid length for 'players.destroyed_wonders' ("
3733 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")",
3734 strlen(str), loading->improvement.size);
3735 for (k = 0; k < loading->improvement.size; k++) {
3736 sg_failure_ret(str[k] == '1' || str[k] == '0',
3737 "Undefined value '%c' within "
3738 "'players.destroyed_wonders'.", str[k]);
3739
3740 if (str[k] == '1') {
3741 struct impr_type *pimprove =
3742 improvement_by_rule_name(loading->improvement.order[k]);
3743 if (pimprove) {
3746 }
3747 }
3748 }
3749
3750 server.identity_number
3751 = secfile_lookup_int_default(loading->file, server.identity_number,
3752 "players.identity_number_used");
3753
3754 /* First remove all defined players. */
3755 players_iterate(pplayer) {
3756 server_remove_player(pplayer);
3758
3759 /* Now, load the players from the savefile. */
3760 player_slots_iterate(pslot) {
3761 struct player *pplayer;
3762 struct rgbcolor *prgbcolor = NULL;
3763 int pslot_id = player_slot_index(pslot);
3764
3765 if (NULL == secfile_section_lookup(loading->file, "player%d",
3766 pslot_id)) {
3767 continue;
3768 }
3769
3770 /* Get player AI type. */
3771 str = secfile_lookup_str(loading->file, "player%d.ai_type",
3772 player_slot_index(pslot));
3773 sg_failure_ret(str != NULL, "%s", secfile_error());
3774
3775 /* Get player color */
3776 if (!rgbcolor_load(loading->file, &prgbcolor, "player%d.color",
3777 pslot_id)) {
3778 if (game_was_started()) {
3779 log_sg("Game has started, yet player %d has no color defined.",
3780 pslot_id);
3781 /* This will be fixed up later */
3782 } else {
3783 log_verbose("No color defined for player %d.", pslot_id);
3784 /* Colors will be assigned on game start, or at end of savefile
3785 * loading if game has already started */
3786 }
3787 }
3788
3789 /* Create player. */
3790 pplayer = server_create_player(player_slot_index(pslot), str,
3791 prgbcolor,
3794 sg_failure_ret(pplayer != NULL, "Invalid AI type: '%s'!", str);
3795
3796 server_player_init(pplayer, FALSE, FALSE);
3797
3798 /* Free the color definition. */
3800
3801 /* Multipliers (policies) */
3802
3803 /* First initialise player values with ruleset defaults; this will
3804 * cover any in the ruleset not known when the savefile was created. */
3805 multipliers_iterate(pmul) {
3806 pplayer->multipliers[multiplier_index(pmul)].value
3807 = pplayer->multipliers[multiplier_index(pmul)].target = pmul->def;
3809
3810 /* Now override with any values from the savefile. */
3811 for (k = 0; k < loading->multiplier.size; k++) {
3812 const struct multiplier *pmul = loading->multiplier.order[k];
3813
3814 if (pmul) {
3816 int val =
3818 "player%d.multiplier%d.val",
3819 player_slot_index(pslot), k);
3820 int rval = (((CLIP(pmul->start, val, pmul->stop)
3821 - pmul->start) / pmul->step) * pmul->step) + pmul->start;
3822
3823 if (rval != val) {
3824 log_verbose("Player %d had illegal value for multiplier \"%s\": "
3825 "was %d, clamped to %d", pslot_id,
3826 multiplier_rule_name(pmul), val, rval);
3827 }
3828 pplayer->multipliers[idx].value = rval;
3829
3830 val =
3832 pplayer->multipliers[idx].value,
3833 "player%d.multiplier%d.target",
3834 player_slot_index(pslot), k);
3835 rval = (((CLIP(pmul->start, val, pmul->stop)
3836 - pmul->start) / pmul->step) * pmul->step) + pmul->start;
3837
3838 if (rval != val) {
3839 log_verbose("Player %d had illegal value for multiplier_target "
3840 " \"%s\": was %d, clamped to %d", pslot_id,
3841 multiplier_rule_name(pmul), val, rval);
3842 }
3843 pplayer->multipliers[idx].target = rval;
3844
3845 pplayer->multipliers[idx].changed
3847 "player%d.multiplier%d.changed",
3848 player_slot_index(pslot), k);
3849 } /* else silently discard multiplier not in current ruleset */
3850 }
3851
3852 /* Must be loaded before tile owner is set. */
3853 pplayer->server.border_vision =
3855 "player%d.border_vision",
3856 player_slot_index(pslot));
3857
3859 "player%d.autoselect_weight",
3860 pslot_id);
3862
3863 /* check number of players */
3864 nplayers = secfile_lookup_int_default(loading->file, 0, "players.nplayers");
3865 sg_failure_ret(player_count() == nplayers, "The value of players.nplayers "
3866 "(%d) from the loaded game does not match the number of "
3867 "players present (%d).", nplayers, player_count());
3868
3869 /* Load team information. */
3870 players_iterate(pplayer) {
3871 int team;
3872 struct team_slot *tslot = NULL;
3873
3875 "player%d.team_no",
3876 player_number(pplayer))
3878 "Invalid team definition for player %s (nb %d).",
3879 player_name(pplayer), player_number(pplayer));
3880 /* Should never fail when slot given is not nullptr */
3881 team_add_player(pplayer, team_new(tslot));
3883
3884 /* Loading the shuffle list is quite complex. At the time of saving the
3885 * shuffle data is saved as
3886 * shuffled_player_<number> = player_slot_id
3887 * where number is an increasing number and player_slot_id is a number
3888 * between 0 and the maximum number of player slots. Now we have to create
3889 * a list
3890 * shuffler_players[number] = player_slot_id
3891 * where all player slot IDs are used exactly one time. The code below
3892 * handles this ... */
3893 if (secfile_lookup_int_default(loading->file, -1,
3894 "players.shuffled_player_%d", 0) >= 0) {
3895 int slots = player_slot_count();
3896 int plrcount = player_count();
3899
3900 for (i = 0; i < slots; i++) {
3901 /* Array to save used numbers. */
3903 /* List of all player IDs (needed for set_shuffled_players()). It is
3904 * initialised with the value -1 to indicate that no value is set. */
3905 shuffled_players[i] = -1;
3906 }
3907
3908 /* Load shuffled player list. */
3909 for (i = 0; i < plrcount; i++) {
3910 int shuffle
3912 "players.shuffled_player_%d", i);
3913
3914 if (shuffle == -1) {
3915 log_sg("Missing player shuffle information (index %d) "
3916 "- reshuffle player list!", i);
3918 break;
3919 } else if (shuffled_player_set[shuffle]) {
3920 log_sg("Player shuffle %d used two times "
3921 "- reshuffle player list!", shuffle);
3923 break;
3924 }
3925 /* Set this ID as used. */
3927
3928 /* Save the player ID in the shuffle list. */
3930 }
3931
3932 if (shuffle_loaded) {
3933 /* Insert missing numbers. */
3934 int shuffle_index = plrcount;
3935
3936 for (i = 0; i < slots; i++) {
3937 if (!shuffled_player_set[i]) {
3939 }
3940
3941 /* shuffle_index must not grow higher than size of shuffled_players. */
3943 "Invalid player shuffle data!");
3944 }
3945
3946#ifdef FREECIV_DEBUG
3947 log_debug("[load shuffle] player_count() = %d", player_count());
3948 player_slots_iterate(pslot) {
3949 int plrid = player_slot_index(pslot);
3950
3951 log_debug("[load shuffle] id: %3d => slot: %3d | slot %3d: %s",
3953 shuffled_player_set[plrid] ? "is used" : "-");
3955#endif /* FREECIV_DEBUG */
3956
3957 /* Set shuffle list from savegame. */
3959 }
3960 }
3961
3962 if (!shuffle_loaded) {
3963 /* No shuffled players included or error loading them, so shuffle them
3964 * (this may include scenarios). */
3966 }
3967}
3968
3969/************************************************************************/
3973{
3974 /* Check status and return if not OK (sg_success FALSE). */
3975 sg_check_ret();
3976
3977 if (game.info.is_new_game) {
3978 /* Nothing to do. */
3979 return;
3980 }
3981
3982 players_iterate(pplayer) {
3983 sg_load_player_main(loading, pplayer);
3985 sg_load_player_units(loading, pplayer);
3987
3988 /* Check the success of the functions above. */
3989 sg_check_ret();
3990
3991 /* Print out some information */
3992 if (is_ai(pplayer)) {
3993 log_normal(_("%s has been added as %s level AI-controlled player "
3994 "(%s)."), player_name(pplayer),
3995 ai_level_translated_name(pplayer->ai_common.skill_level),
3996 ai_name(pplayer->ai));
3997 } else {
3998 log_normal(_("%s has been added as human player."),
3999 player_name(pplayer));
4000 }
4002
4003 /* Also load the transport status of the units here. It must be a special
4004 * case as all units must be known (unit on an allied transporter). */
4005 players_iterate(pplayer) {
4006 /* Load unit transport status. */
4009
4010 /* Savegame may contain nation assignments that are incompatible with the
4011 * current nationset. Ensure they are compatible, one way or another. */
4013
4014 /* Some players may have invalid nations in the ruleset. Once all players
4015 * are loaded, pick one of the remaining nations for them. */
4016 players_iterate(pplayer) {
4017 if (pplayer->nation == NO_NATION_SELECTED) {
4020 /* TRANS: Minor error message: <Leader> ... <Poles>. */
4021 log_sg(_("%s had invalid nation; changing to %s."),
4022 player_name(pplayer), nation_plural_for_player(pplayer));
4023
4024 ai_traits_init(pplayer);
4025 }
4027
4028 /* Sanity check alliances, prevent allied-with-ally-of-enemy. */
4031 if (pplayers_allied(plr, aplayer)) {
4033 DS_ALLIANCE);
4034
4037 log_sg("Illegal alliance structure detected: "
4038 "%s alliance to %s reduced to peace treaty.",
4043 }
4044 }
4047
4048 /* Update cached city illness. This can depend on trade routes,
4049 * so can't be calculated until all players have been loaded. */
4050 if (game.info.illness_on) {
4052 pcity->server.illness
4054 &(pcity->illness_trade), NULL);
4056 }
4057
4058 /* Update all city information. This must come after all cities are
4059 * loaded (in player_load) but before player (dumb) cities are loaded
4060 * in player_load_vision(). */
4061 players_iterate(plr) {
4062 city_list_iterate(plr->cities, pcity) {
4065 CALL_PLR_AI_FUNC(city_got, plr, plr, pcity);
4068
4069 /* Since the cities must be placed on the map to put them on the
4070 player map we do this afterwards */
4071 players_iterate(pplayer) {
4073 /* Check the success of the function above. */
4074 sg_check_ret();
4076
4077 /* Check shared vision and tiles. */
4078 players_iterate(pplayer) {
4079 BV_CLR_ALL(pplayer->gives_shared_vision);
4080 BV_CLR_ALL(pplayer->gives_shared_tiles);
4081 BV_CLR_ALL(pplayer->server.really_gives_vision);
4083
4084 /* Set up shared vision... */
4085 players_iterate(pplayer) {
4086 int plr1 = player_index(pplayer);
4087
4089 int plr2 = player_index(pplayer2);
4090
4092 "player%d.diplstate%d.gives_shared_vision", plr1, plr2)) {
4093 give_shared_vision(pplayer, pplayer2);
4094 }
4096 "player%d.diplstate%d.gives_shared_tiles", plr1, plr2)) {
4097 BV_SET(pplayer->gives_shared_tiles, player_index(pplayer2));
4098 }
4101
4102 /* ...and check it */
4105 /* TODO: Is there a good reason player is not marked as
4106 * giving shared vision to themselves -> really_gives_vision()
4107 * returning FALSE when pplayer1 == pplayer2 */
4108 if (pplayer1 != pplayer2
4111 sg_regr(3000900,
4112 _("%s did not give shared vision to team member %s."),
4115 }
4117 sg_regr(3000900,
4118 _("%s did not give shared vision to team member %s."),
4121 }
4122 }
4125
4128
4129 /* All vision is ready; this calls city_thaw_workers_queue(). */
4131
4132 /* Make sure everything is consistent. */
4133 players_iterate(pplayer) {
4134 unit_list_iterate(pplayer->units, punit) {
4136 struct tile *ptile = unit_tile(punit);
4137
4138 log_sg("%s doing illegal activity in savegame!",
4140 log_sg("Activity: %s, Target: %s, Tile: (%d, %d), Terrain: %s",
4144 : "missing",
4145 TILE_XY(ptile), terrain_rule_name(tile_terrain(ptile)));
4147 }
4150
4153 city_thaw_workers(pcity); /* may auto_arrange_workers() */
4155
4156 /* Player colors are always needed once game has started. Pre-2.4 savegames
4157 * lack them. This cannot be in compatibility conversion layer as we need
4158 * all the player data available to be able to assign best colors. */
4159 if (game_was_started()) {
4161 }
4162}
4163
4164/************************************************************************/
4167static void sg_save_players(struct savedata *saving)
4168{
4169 /* Check status and return if not OK (sg_success FALSE). */
4170 sg_check_ret();
4171
4172 if ((saving->scenario && !saving->save_players)
4173 || !game_was_started()) {
4174 /* Nothing to do for a scenario without saved players or a game in
4175 * INITIAL state. */
4176 return;
4177 }
4178
4179 secfile_insert_int(saving->file, player_count(), "players.nplayers");
4180
4181 /* Save destroyed wonders as bitvector. Note that improvement order
4182 * is saved in 'savefile.improvement.order'. */
4183 {
4184 char destroyed[B_LAST+1];
4185
4186 improvement_iterate(pimprove) {
4187 if (is_great_wonder(pimprove)
4188 && great_wonder_is_destroyed(pimprove)) {
4189 destroyed[improvement_index(pimprove)] = '1';
4190 } else {
4191 destroyed[improvement_index(pimprove)] = '0';
4192 }
4194 destroyed[improvement_count()] = '\0';
4196 "players.destroyed_wonders");
4197 }
4198
4199 secfile_insert_int(saving->file, server.identity_number,
4200 "players.identity_number_used");
4201
4202 /* Save player order. */
4203 {
4204 int i = 0;
4205 shuffled_players_iterate(pplayer) {
4206 secfile_insert_int(saving->file, player_number(pplayer),
4207 "players.shuffled_player_%d", i);
4208 i++;
4210 }
4211
4212 /* Sort units. */
4214
4215 /* Save players. */
4216 players_iterate(pplayer) {
4217 sg_save_player_main(saving, pplayer);
4218 sg_save_player_cities(saving, pplayer);
4219 sg_save_player_units(saving, pplayer);
4221 sg_save_player_vision(saving, pplayer);
4223}
4224
4225/************************************************************************/
4229 struct player *plr)
4230{
4231 const char **slist;
4232 int i, plrno = player_number(plr);
4233 const char *str;
4234 struct government *gov;
4235 const char *level;
4236 const char *barb_str;
4237 size_t nval;
4238 const char *kind;
4239
4240 /* Check status and return if not OK (sg_success FALSE). */
4241 sg_check_ret();
4242
4243 /* Basic player data. */
4244 str = secfile_lookup_str(loading->file, "player%d.name", plrno);
4245 sg_failure_ret(str != NULL, "%s", secfile_error());
4247 sz_strlcpy(plr->username,
4249 "player%d.username", plrno));
4251 "player%d.unassigned_user", plrno),
4252 "%s", secfile_error());
4255 "player%d.orig_username", plrno));
4258 "player%d.ranked_username",
4259 plrno));
4261 "player%d.unassigned_ranked", plrno),
4262 "%s", secfile_error());
4264 "player%d.delegation_username",
4265 plrno);
4266 /* Defaults to no delegation. */
4267 if (strlen(str)) {
4269 }
4270
4271 /* Player flags */
4272 BV_CLR_ALL(plr->flags);
4273 slist = secfile_lookup_str_vec(loading->file, &nval, "player%d.flags", plrno);
4274 for (i = 0; i < nval; i++) {
4275 const char *sval = slist[i];
4277
4278 sg_failure_ret(plr_flag_id_is_valid(fid), "Invalid player flag \"%s\".", sval);
4279
4280 BV_SET(plr->flags, fid);
4281 }
4282 free(slist);
4283
4284 /* Nation */
4285 str = secfile_lookup_str(loading->file, "player%d.nation", plrno);
4287 if (plr->nation != NULL) {
4288 ai_traits_init(plr);
4289 }
4290
4291 /* Government */
4292 str = secfile_lookup_str(loading->file, "player%d.government_name",
4293 plrno);
4295 sg_failure_ret(gov != NULL, "Player%d: unsupported government \"%s\".",
4296 plrno, str);
4297 plr->government = gov;
4298
4299 /* Target government */
4301 "player%d.target_government_name", plrno);
4302 if (str != NULL) {
4304 } else {
4305 plr->target_government = NULL;
4306 }
4309 "player%d.revolution_finishes", plrno);
4310
4311 /* Load diplomatic data (diplstate + embassy + vision).
4312 * Shared vision is loaded in sg_load_players(). */
4314 players_iterate(pplayer) {
4315 char buf[32];
4316 struct player_diplstate *ds = player_diplstate_get(plr, pplayer);
4317 i = player_index(pplayer);
4318
4319 /* Load diplomatic status */
4320 fc_snprintf(buf, sizeof(buf), "player%d.diplstate%d", plrno, i);
4321
4322 ds->type =
4324 diplstate_type, "%s.current", buf);
4325 ds->max_state =
4327 diplstate_type, "%s.closest", buf);
4328
4329 /* FIXME: If either party is barbarian, we cannot enforce below check */
4330#if 0
4331 if (ds->type == DS_WAR && ds->first_contact_turn <= 0) {
4332 sg_regr(3020000,
4333 "Player%d: War with player %d who has never been met. "
4334 "Reverted to No Contact state.", plrno, i);
4335 ds->type = DS_NO_CONTACT;
4336 }
4337#endif
4338
4339 if (valid_dst_closest(ds) != ds->max_state) {
4340 sg_regr(3020000,
4341 "Player%d: closest diplstate to player %d less than current. "
4342 "Updated.", plrno, i);
4343 ds->max_state = ds->type;
4344 }
4345
4346 ds->first_contact_turn =
4348 "%s.first_contact_turn", buf);
4349 ds->turns_left =
4350 secfile_lookup_int_default(loading->file, -2, "%s.turns_left", buf);
4351 ds->has_reason_to_cancel =
4353 "%s.has_reason_to_cancel", buf);
4354 ds->contact_turns_left =
4356 "%s.contact_turns_left", buf);
4357
4358 if (secfile_lookup_bool_default(loading->file, FALSE, "%s.embassy",
4359 buf)) {
4360 BV_SET(plr->real_embassy, i);
4361 }
4362 /* 'gives_shared_vision' is loaded in sg_load_players() as all cities
4363 * must be known. */
4365
4366 /* load ai data */
4368 char buf[32];
4369
4370 fc_snprintf(buf, sizeof(buf), "player%d.ai%d", plrno,
4372
4374 secfile_lookup_int_default(loading->file, 1, "%s.love", buf);
4375 CALL_FUNC_EACH_AI(player_load_relations, plr, aplayer, loading->file, plrno);
4377
4379 "player%d.adv.wonder_city",
4380 plrno);
4381
4382 CALL_FUNC_EACH_AI(player_load, plr, loading->file, plrno);
4383
4384 /* Some sane defaults */
4385 plr->ai_common.fuzzy = 0;
4386 plr->ai_common.expand = 100;
4387 plr->ai_common.science_cost = 100;
4388
4389
4391 "player%d.ai.level", plrno);
4392 if (level != NULL && !fc_strcasecmp("Handicapped", level)) {
4393 /* Up to freeciv-3.1 Restricted AI level was known as Handicapped */
4395 } else {
4397 }
4398
4400 log_sg("Player%d: Invalid AI level \"%s\". "
4401 "Changed to \"%s\".", plrno, level,
4404 }
4405
4407 "player%d.ai.barb_type", plrno);
4409
4411 log_sg("Player%d: Invalid barbarian type \"%s\". "
4412 "Changed to \"None\".", plrno, barb_str);
4414 }
4415
4416 if (is_barbarian(plr)) {
4417 server.nbarbarians++;
4418 }
4419
4420 if (is_ai(plr)) {
4422 CALL_PLR_AI_FUNC(gained_control, plr, plr);
4423 }
4424
4425 /* Load nation style. */
4426 {
4427 struct nation_style *style;
4428
4429 str = secfile_lookup_str(loading->file, "player%d.style_by_name", plrno);
4430
4431 sg_failure_ret(str != NULL, "%s", secfile_error());
4432 style = style_by_rule_name(str);
4433 if (style == NULL) {
4434 style = style_by_number(0);
4435 log_sg("Player%d: unsupported city_style_name \"%s\". "
4436 "Changed to \"%s\".", plrno, str, style_rule_name(style));
4437 }
4438 plr->style = style;
4439 }
4440
4442 "player%d.idle_turns", plrno),
4443 "%s", secfile_error());
4444 kind = secfile_lookup_str(loading->file, "player%d.kind", plrno);
4445 if (sex_by_name(kind) == SEX_MALE) {
4446 plr->is_male = TRUE;
4447 } else {
4448 plr->is_male = FALSE;
4449 }
4451 "player%d.is_alive", plrno),
4452 "%s", secfile_error());
4454 "player%d.turns_alive", plrno),
4455 "%s", secfile_error());
4457 "player%d.last_war", plrno),
4458 "%s", secfile_error());
4460 "player%d.phase_done", plrno);
4462 "player%d.gold", plrno),
4463 "%s", secfile_error());
4465 "player%d.rates.tax", plrno),
4466 "%s", secfile_error());
4468 "player%d.rates.science", plrno),
4469 "%s", secfile_error());
4471 "player%d.rates.luxury", plrno),
4472 "%s", secfile_error());
4474 "player%d.infrapts",
4475 plrno);
4476 plr->server.bulbs_last_turn =
4478 "player%d.research.bulbs_last_turn", plrno);
4479
4480 /* Traits */
4481 if (plr->nation) {
4482 for (i = 0; i < loading->trait.size; i++) {
4483 enum trait tr = trait_by_name(loading->trait.order[i], fc_strcasecmp);
4484
4485 if (trait_is_valid(tr)) {
4486 int val;
4487
4488 sg_failure_ret(secfile_lookup_int(loading->file, &val, "player%d.trait%d.val",
4489 plrno, i),
4490 "%s", secfile_error());
4491 plr->ai_common.traits[tr].val = val;
4492
4494 "player%d.trait%d.mod", plrno, i),
4495 "%s", secfile_error());
4496 plr->ai_common.traits[tr].mod = val;
4497 }
4498 }
4499 }
4500
4501 /* Achievements */
4502 {
4503 int count;
4504
4505 count = secfile_lookup_int_default(loading->file, -1,
4506 "player%d.achievement_count", plrno);
4507
4508 if (count > 0) {
4509 for (i = 0; i < count; i++) {
4510 const char *name;
4511 struct achievement *pach;
4512 bool first;
4513
4515 "player%d.achievement%d.name", plrno, i);
4517
4519 "Unknown achievement \"%s\".", name);
4520
4522 "player%d.achievement%d.first",
4523 plrno, i),
4524 "achievement error: %s", secfile_error());
4525
4526 sg_failure_ret(pach->first == NULL || !first,
4527 "Multiple players listed as first to get achievement \"%s\".",
4528 name);
4529
4530 BV_SET(pach->achievers, player_index(plr));
4531
4532 if (first) {
4533 pach->first = plr;
4534 }
4535 }
4536 }
4537 }
4538
4539 /* Player score. */
4540 plr->score.happy =
4542 "score%d.happy", plrno);
4543 plr->score.content =
4545 "score%d.content", plrno);
4546 plr->score.unhappy =
4548 "score%d.unhappy", plrno);
4549 plr->score.angry =
4551 "score%d.angry", plrno);
4552
4553 /* Make sure that the score about specialists in current ruleset that
4554 * were not present at saving time are set to zero. */
4556 plr->score.specialists[sp] = 0;
4558
4559 for (i = 0; i < loading->specialist.size; i++) {
4560 plr->score.specialists[specialist_index(loading->specialist.order[i])]
4562 "score%d.specialists%d", plrno, i);
4563 }
4564
4565 plr->score.wonders =
4567 "score%d.wonders", plrno);
4568 plr->score.techs =
4570 "score%d.techs", plrno);
4571 plr->score.techout =
4573 "score%d.techout", plrno);
4574 plr->score.landarea =
4576 "score%d.landarea", plrno);
4577 plr->score.settledarea =
4579 "score%d.settledarea", plrno);
4580 plr->score.population =
4582 "score%d.population", plrno);
4583 plr->score.cities =
4585 "score%d.cities", plrno);
4586 plr->score.units =
4588 "score%d.units", plrno);
4589 plr->score.pollution =
4591 "score%d.pollution", plrno);
4592 plr->score.literacy =
4594 "score%d.literacy", plrno);
4595 plr->score.bnp =
4597 "score%d.bnp", plrno);
4598 plr->score.mfg =
4600 "score%d.mfg", plrno);
4601 plr->score.spaceship =
4603 "score%d.spaceship", plrno);
4604 plr->score.units_built =
4606 "score%d.units_built", plrno);
4607 plr->score.units_killed =
4609 "score%d.units_killed", plrno);
4610 plr->score.units_lost =
4612 "score%d.units_lost", plrno);
4613 plr->score.units_used =
4615 "score%d.units_used", plrno);
4616 plr->score.culture =
4618 "score%d.culture", plrno);
4619 plr->score.game =
4621 "score%d.total", plrno);
4622
4623 /* Load space ship data. */
4624 {
4625 struct player_spaceship *ship = &plr->spaceship;
4626 char prefix[32];
4627 const char *st;
4628 int ei;
4629
4630 fc_snprintf(prefix, sizeof(prefix), "player%d.spaceship", plrno);
4633 &ei,
4634 "%s.state", prefix),
4635 "%s", secfile_error());
4636 ship->state = ei;
4637
4638 if (ship->state != SSHIP_NONE) {
4639 sg_failure_ret(secfile_lookup_int(loading->file, &ship->structurals,
4640 "%s.structurals", prefix),
4641 "%s", secfile_error());
4642 sg_failure_ret(secfile_lookup_int(loading->file, &ship->components,
4643 "%s.components", prefix),
4644 "%s", secfile_error());
4646 "%s.modules", prefix),
4647 "%s", secfile_error());
4649 "%s.fuel", prefix),
4650 "%s", secfile_error());
4651 sg_failure_ret(secfile_lookup_int(loading->file, &ship->propulsion,
4652 "%s.propulsion", prefix),
4653 "%s", secfile_error());
4654 sg_failure_ret(secfile_lookup_int(loading->file, &ship->habitation,
4655 "%s.habitation", prefix),
4656 "%s", secfile_error());
4657 sg_failure_ret(secfile_lookup_int(loading->file, &ship->life_support,
4658 "%s.life_support", prefix),
4659 "%s", secfile_error());
4660 sg_failure_ret(secfile_lookup_int(loading->file, &ship->solar_panels,
4661 "%s.solar_panels", prefix),
4662 "%s", secfile_error());
4663
4664 st = secfile_lookup_str(loading->file, "%s.structure", prefix);
4665 sg_failure_ret(st != NULL, "%s", secfile_error())
4666 for (i = 0; i < NUM_SS_STRUCTURALS && st[i]; i++) {
4667 sg_failure_ret(st[i] == '1' || st[i] == '0',
4668 "Undefined value '%c' within '%s.structure'.", st[i],
4669 prefix)
4670
4671 if (!(st[i] == '0')) {
4672 BV_SET(ship->structure, i);
4673 }
4674 }
4675 if (ship->state >= SSHIP_LAUNCHED) {
4676 sg_failure_ret(secfile_lookup_int(loading->file, &ship->launch_year,
4677 "%s.launch_year", prefix),
4678 "%s", secfile_error());
4679 }
4681 }
4682 }
4683
4684 /* Load lost wonder data. */
4685 str = secfile_lookup_str(loading->file, "player%d.lost_wonders", plrno);
4686 /* If not present, probably an old savegame; nothing to be done */
4687 if (str != NULL) {
4688 int k;
4689
4690 sg_failure_ret(strlen(str) == loading->improvement.size,
4691 "Invalid length for 'player%d.lost_wonders' ("
4692 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ")",
4693 plrno, strlen(str), loading->improvement.size);
4694 for (k = 0; k < loading->improvement.size; k++) {
4695 sg_failure_ret(str[k] == '1' || str[k] == '0',
4696 "Undefined value '%c' within "
4697 "'player%d.lost_wonders'.", plrno, str[k]);
4698
4699 if (str[k] == '1') {
4700 struct impr_type *pimprove =
4701 improvement_by_rule_name(loading->improvement.order[k]);
4702 if (pimprove) {
4703 plr->wonders[improvement_index(pimprove)] = WONDER_LOST;
4704 }
4705 }
4706 }
4707 }
4708
4709 plr->history =
4710 secfile_lookup_int_default(loading->file, 0, "player%d.history", plrno);
4711 plr->server.huts =
4712 secfile_lookup_int_default(loading->file, 0, "player%d.hut_count", plrno);
4713}
4714
4715/************************************************************************/
4719 struct player *plr)
4720{
4721 int i, k, plrno = player_number(plr);
4722 struct player_spaceship *ship = &plr->spaceship;
4723 const char *flag_names[PLRF_COUNT];
4724 int set_count;
4725
4726 /* Check status and return if not OK (sg_success FALSE). */
4727 sg_check_ret();
4728
4729 set_count = 0;
4730 for (i = 0; i < PLRF_COUNT; i++) {
4731 if (player_has_flag(plr, i)) {
4733 }
4734 }
4735
4737 "player%d.flags", plrno);
4738
4739 secfile_insert_str(saving->file, ai_name(plr->ai),
4740 "player%d.ai_type", plrno);
4742 "player%d.name", plrno);
4744 "player%d.username", plrno);
4746 "player%d.unassigned_user", plrno);
4747 if (plr->rgb != NULL) {
4748 rgbcolor_save(saving->file, plr->rgb, "player%d.color", plrno);
4749 } else {
4750 /* Colorless players are ok in pregame */
4751 if (game_was_started()) {
4752 log_sg("Game has started, yet player %d has no color defined.", plrno);
4753 }
4754 }
4756 "player%d.ranked_username", plrno);
4758 "player%d.unassigned_ranked", plrno);
4760 "player%d.orig_username", plrno);
4763 : "",
4764 "player%d.delegation_username", plrno);
4766 "player%d.nation", plrno);
4767 secfile_insert_int(saving->file, plr->team ? team_index(plr->team) : -1,
4768 "player%d.team_no", plrno);
4769
4772 "player%d.government_name", plrno);
4773
4774 if (plr->target_government) {
4777 "player%d.target_government_name", plrno);
4778 }
4779
4781 "player%d.style_by_name", plrno);
4782
4784 "player%d.idle_turns", plrno);
4785 if (plr->is_male) {
4787 "player%d.kind", plrno);
4788 } else {
4790 "player%d.kind", plrno);
4791 }
4793 "player%d.is_alive", plrno);
4795 "player%d.turns_alive", plrno);
4797 "player%d.last_war", plrno);
4799 "player%d.phase_done", plrno);
4800
4801 players_iterate(pplayer) {
4802 char buf[32];
4803 struct player_diplstate *ds = player_diplstate_get(plr, pplayer);
4804
4805 i = player_index(pplayer);
4806
4807 /* save diplomatic state */
4808 fc_snprintf(buf, sizeof(buf), "player%d.diplstate%d", plrno, i);
4809
4810 secfile_insert_enum(saving->file, ds->type,
4811 diplstate_type, "%s.current", buf);
4812 secfile_insert_enum(saving->file, ds->max_state,
4813 diplstate_type, "%s.closest", buf);
4814 secfile_insert_int(saving->file, ds->first_contact_turn,
4815 "%s.first_contact_turn", buf);
4816 secfile_insert_int(saving->file, ds->turns_left,
4817 "%s.turns_left", buf);
4818 secfile_insert_int(saving->file, ds->has_reason_to_cancel,
4819 "%s.has_reason_to_cancel", buf);
4820 secfile_insert_int(saving->file, ds->contact_turns_left,
4821 "%s.contact_turns_left", buf);
4823 "%s.embassy", buf);
4824 secfile_insert_bool(saving->file, gives_shared_vision(plr, pplayer),
4825 "%s.gives_shared_vision", buf);
4826 secfile_insert_bool(saving->file, gives_shared_tiles(plr, pplayer),
4827 "%s.gives_shared_tiles", buf);
4829
4832 /* save ai data */
4834 "player%d.ai%d.love", plrno, i);
4835 CALL_FUNC_EACH_AI(player_save_relations, plr, aplayer, saving->file, plrno);
4837
4839 "player%d.adv.wonder_city", plrno);
4840
4841 CALL_FUNC_EACH_AI(player_save, plr, saving->file, plrno);
4842
4843 /* Multipliers (policies) */
4844 i = multiplier_count();
4845
4846 for (k = 0; k < i; k++) {
4848 "player%d.multiplier%d.val", plrno, k);
4850 "player%d.multiplier%d.target", plrno, k);
4852 "player%d.multiplier%d.changed", plrno, k);
4853 }
4854
4856 "player%d.ai.level", plrno);
4858 "player%d.ai.barb_type", plrno);
4860 "player%d.gold", plrno);
4862 "player%d.rates.tax", plrno);
4864 "player%d.rates.science", plrno);
4866 "player%d.rates.luxury", plrno);
4868 "player%d.infrapts", plrno);
4870 "player%d.research.bulbs_last_turn", plrno);
4871
4872 /* Save traits */
4873 {
4874 enum trait tr;
4875 int j;
4876
4877 for (tr = trait_begin(), j = 0; tr != trait_end(); tr = trait_next(tr), j++) {
4879 "player%d.trait%d.val", plrno, j);
4881 "player%d.trait%d.mod", plrno, j);
4882 }
4883 }
4884
4885 /* Save achievements */
4886 {
4887 int j = 0;
4888
4890 if (achievement_player_has(pach, plr)) {
4892 "player%d.achievement%d.name", plrno, j);
4893 if (pach->first == plr) {
4895 "player%d.achievement%d.first", plrno, j);
4896 } else {
4898 "player%d.achievement%d.first", plrno, j);
4899 }
4900
4901 j++;
4902 }
4904
4905 secfile_insert_int(saving->file, j,
4906 "player%d.achievement_count", plrno);
4907 }
4908
4910 "player%d.revolution_finishes", plrno);
4911
4912 /* Player score */
4914 "score%d.happy", plrno);
4916 "score%d.content", plrno);
4918 "score%d.unhappy", plrno);
4920 "score%d.angry", plrno);
4923 "score%d.specialists%d", plrno, sp);
4926 "score%d.wonders", plrno);
4928 "score%d.techs", plrno);
4930 "score%d.techout", plrno);
4932 "score%d.landarea", plrno);
4934 "score%d.settledarea", plrno);
4936 "score%d.population", plrno);
4938 "score%d.cities", plrno);
4940 "score%d.units", plrno);
4942 "score%d.pollution", plrno);
4944 "score%d.literacy", plrno);
4945 secfile_insert_int(saving->file, plr->score.bnp,
4946 "score%d.bnp", plrno);
4947 secfile_insert_int(saving->file, plr->score.mfg,
4948 "score%d.mfg", plrno);
4950 "score%d.spaceship", plrno);
4952 "score%d.units_built", plrno);
4954 "score%d.units_killed", plrno);
4956 "score%d.units_lost", plrno);
4958 "score%d.units_used", plrno);
4960 "score%d.culture", plrno);
4961 secfile_insert_int(saving->file, plr->score.game,
4962 "score%d.total", plrno);
4963
4964 /* Save space ship status. */
4965 secfile_insert_int(saving->file, ship->state, "player%d.spaceship.state",
4966 plrno);
4967 if (ship->state != SSHIP_NONE) {
4968 char buf[32];
4969 char st[NUM_SS_STRUCTURALS+1];
4970 int ssi;
4971
4972 fc_snprintf(buf, sizeof(buf), "player%d.spaceship", plrno);
4973
4974 secfile_insert_int(saving->file, ship->structurals,
4975 "%s.structurals", buf);
4976 secfile_insert_int(saving->file, ship->components,
4977 "%s.components", buf);
4978 secfile_insert_int(saving->file, ship->modules,
4979 "%s.modules", buf);
4980 secfile_insert_int(saving->file, ship->fuel, "%s.fuel", buf);
4981 secfile_insert_int(saving->file, ship->propulsion, "%s.propulsion", buf);
4982 secfile_insert_int(saving->file, ship->habitation, "%s.habitation", buf);
4983 secfile_insert_int(saving->file, ship->life_support,
4984 "%s.life_support", buf);
4985 secfile_insert_int(saving->file, ship->solar_panels,
4986 "%s.solar_panels", buf);
4987
4988 for (ssi = 0; ssi < NUM_SS_STRUCTURALS; ssi++) {
4989 st[ssi] = BV_ISSET(ship->structure, ssi) ? '1' : '0';
4990 }
4991 st[ssi] = '\0';
4992 secfile_insert_str(saving->file, st, "%s.structure", buf);
4993 if (ship->state >= SSHIP_LAUNCHED) {
4994 secfile_insert_int(saving->file, ship->launch_year,
4995 "%s.launch_year", buf);
4996 }
4997 }
4998
4999 /* Save lost wonders info. */
5000 {
5001 char lost[B_LAST+1];
5002
5003 improvement_iterate(pimprove) {
5004 if (is_wonder(pimprove) && wonder_is_lost(plr, pimprove)) {
5005 lost[improvement_index(pimprove)] = '1';
5006 } else {
5007 lost[improvement_index(pimprove)] = '0';
5008 }
5010 lost[improvement_count()] = '\0';
5012 "player%d.lost_wonders", plrno);
5013 }
5014
5015 secfile_insert_int(saving->file, plr->history,
5016 "player%d.history", plrno);
5018 "player%d.hut_count", plrno);
5019
5021 "player%d.border_vision", plrno);
5022
5023 if (saving->scenario) {
5024 if (plr->autoselect_weight < 0) { /* Apply default behavior */
5025 int def = 1; /* We want users to get a player in a scenario */
5026
5028 /* This isn't usable player */
5029 def = 0;
5030 }
5031
5032 secfile_insert_int(saving->file, def,
5033 "player%d.autoselect_weight", plrno);
5034 } else {
5036 "player%d.autoselect_weight", plrno);
5037 }
5038 }
5039}
5040
5041/************************************************************************/
5045 struct player *plr)
5046{
5047 int ncities, i, plrno = player_number(plr);
5048 bool tasks_handled;
5050
5051 /* Check status and return if not OK (sg_success FALSE). */
5052 sg_check_ret();
5053
5055 "player%d.ncities", plrno),
5056 "%s", secfile_error());
5057
5058 if (!plr->is_alive && ncities > 0) {
5059 log_sg("'player%d.ncities' = %d for dead player!", plrno, ncities);
5060 ncities = 0;
5061 }
5062
5064 "player%d.wl_max_length",
5065 plrno);
5067 "player%d.routes_max_length", plrno);
5068
5069 /* Load all cities of the player. */
5070 for (i = 0; i < ncities; i++) {
5071 char buf[32];
5072 struct city *pcity;
5073
5074 fc_snprintf(buf, sizeof(buf), "player%d.c%d", plrno, i);
5075
5076 /* Create a dummy city. */
5083 sg_failure_ret(FALSE, "Error loading city %d of player %d.", i, plrno);
5084 }
5085
5088
5089 /* Load the information about the nationality of citizens. This is done
5090 * here because the city sanity check called by citizens_update() requires
5091 * that the city is registered. */
5093
5094 /* After everything is loaded, but before vision. */
5096
5097 /* adding the city contribution to fog-of-war */
5101
5103 }
5104
5106 for (i = 0; !tasks_handled; i++) {
5107 int city_id;
5108 struct city *pcity = NULL;
5109
5110 city_id = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.city",
5111 plrno, i);
5112
5113 if (city_id != -1) {
5114 pcity = player_city_by_number(plr, city_id);
5115 }
5116
5117 if (pcity != NULL) {
5118 const char *str;
5119 int nat_x, nat_y;
5120 struct worker_task *ptask = fc_malloc(sizeof(struct worker_task));
5121
5122 nat_x = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.x", plrno, i);
5123 nat_y = secfile_lookup_int_default(loading->file, -1, "player%d.task%d.y", plrno, i);
5124
5125 ptask->ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
5126
5127 str = secfile_lookup_str(loading->file, "player%d.task%d.activity", plrno, i);
5129
5131 "Unknown workertask activity %s", str);
5132
5133 str = secfile_lookup_str(loading->file, "player%d.task%d.target", plrno, i);
5134
5135 if (strcmp("-", str)) {
5137
5138 sg_failure_ret(ptask->tgt != NULL,
5139 "Unknown workertask target %s", str);
5140 } else {
5141 ptask->tgt = NULL;
5142
5143 if (ptask->act == ACTIVITY_IRRIGATE) {
5145 } else if (ptask->act == ACTIVITY_MINE) {
5146 ptask->act = ACTIVITY_MINE;
5147 }
5148 }
5149
5150 ptask->want = secfile_lookup_int_default(loading->file, 1,
5151 "player%d.task%d.want", plrno, i);
5152
5153 worker_task_list_append(pcity->task_reqs, ptask);
5154 } else {
5156 }
5157 }
5158}
5159
5160/************************************************************************/
5163static bool sg_load_player_city(struct loaddata *loading, struct player *plr,
5164 struct city *pcity, const char *citystr,
5166{
5167 struct player *past;
5168 const char *kind, *name, *str;
5169 int id, i, repair, sp_count = 0, workers = 0, value;
5170 int nat_x, nat_y;
5171 citizens size;
5172 const char *stylename;
5173 int partner;
5174 int want;
5175 int tmp_int;
5176 const struct civ_map *nmap = &(wld.map);
5177 enum capital_type cap;
5178
5180 FALSE, "%s", secfile_error());
5182 FALSE, "%s", secfile_error());
5185 "%s has invalid center tile (%d, %d)",
5186 citystr, nat_x, nat_y);
5188 "%s duplicates city (%d, %d)", citystr, nat_x, nat_y);
5189
5190 /* Instead of dying, use 'citystr' string for damaged name. */
5192 "%s.name", citystr));
5193
5195 citystr), FALSE, "%s", secfile_error());
5196
5198 "%s.original", citystr);
5199 past = player_by_number(id);
5200 if (NULL != past) {
5201 pcity->original = past;
5202 }
5203
5204 sg_warn_ret_val(secfile_lookup_int(loading->file, &value, "%s.size",
5205 citystr), FALSE, "%s", secfile_error());
5206 size = (citizens)value; /* Set the correct type */
5207 sg_warn_ret_val(value == (int)size, FALSE,
5208 "Invalid city size: %d, set to %d", value, size);
5210
5212 "%s.capital", citystr),
5214
5216 pcity->capital = cap;
5217 if (cap == CAPITAL_PRIMARY) {
5218 plr->primary_capital_id = pcity->id;
5219 }
5220 } else {
5221 pcity->capital = CAPITAL_NOT;
5222 }
5223
5224 for (i = 0; i < loading->specialist.size; i++) {
5225 Specialist_type_id si = specialist_index(loading->specialist.order[i]);
5226
5227 sg_warn_ret_val(secfile_lookup_int(loading->file, &value, "%s.nspe%d",
5228 citystr, i),
5229 FALSE, "%s", secfile_error());
5230 pcity->specialists[si] = (citizens)value;
5232 sp_count += value;
5233 }
5234 }
5235
5236 partner = secfile_lookup_int_default(loading->file, 0, "%s.traderoute0", citystr);
5237 for (i = 0; partner != 0; i++) {
5238 struct trade_route *proute = fc_malloc(sizeof(struct trade_route));
5239 const char *dir;
5240 const char *good_str;
5241
5242 /* Append to routes list immediately, so the pointer can be found for freeing
5243 * even if we abort */
5245
5246 proute->partner = partner;
5247 dir = secfile_lookup_str(loading->file, "%s.route_direction%d", citystr, i);
5249 "No traderoute direction found for %s", citystr);
5252 "Illegal route direction %s", dir);
5253 good_str = secfile_lookup_str(loading->file, "%s.route_good%d", citystr, i);
5255 "No good found for %s", citystr);
5257 sg_warn_ret_val(proute->goods != NULL, FALSE,
5258 "Illegal good %s", good_str);
5259
5260 /* Next one */
5262 "%s.traderoute%d", citystr, i + 1);
5263 }
5264
5265 for (; i < routes_max; i++) {
5266 (void) secfile_entry_lookup(loading->file, "%s.traderoute%d", citystr, i);
5267 (void) secfile_entry_lookup(loading->file, "%s.route_direction%d", citystr, i);
5268 (void) secfile_entry_lookup(loading->file, "%s.route_good%d", citystr, i);
5269 }
5270
5271 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->food_stock,
5272 "%s.food_stock", citystr),
5273 FALSE, "%s", secfile_error());
5274 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->shield_stock,
5275 "%s.shield_stock", citystr),
5276 FALSE, "%s", secfile_error());
5277 pcity->history =
5278 secfile_lookup_int_default(loading->file, 0, "%s.history", citystr);
5279
5280 pcity->airlift =
5281 secfile_lookup_int_default(loading->file, 0, "%s.airlift", citystr);
5282 pcity->was_happy =
5283 secfile_lookup_bool_default(loading->file, FALSE, "%s.was_happy",
5284 citystr);
5285 pcity->had_famine =
5286 secfile_lookup_bool_default(loading->file, FALSE, "%s.had_famine",
5287 citystr);
5288
5289 pcity->turn_plague =
5290 secfile_lookup_int_default(loading->file, 0, "%s.turn_plague", citystr);
5291
5293 "%s.anarchy", citystr),
5294 FALSE, "%s", secfile_error());
5295 pcity->rapture =
5296 secfile_lookup_int_default(loading->file, 0, "%s.rapture", citystr);
5297 pcity->steal =
5298 secfile_lookup_int_default(loading->file, 0, "%s.steal", citystr);
5299
5300 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->turn_founded,
5301 "%s.turn_founded", citystr),
5302 FALSE, "%s", secfile_error());
5304 "%s.acquire_t", citystr),
5305 FALSE, "%s", secfile_error());
5306 pcity->acquire_t = tmp_int;
5307 sg_warn_ret_val(secfile_lookup_bool(loading->file, &pcity->did_buy, "%s.did_buy",
5308 citystr), FALSE, "%s", secfile_error());
5309 sg_warn_ret_val(secfile_lookup_bool(loading->file, &pcity->did_sell, "%s.did_sell",
5310 citystr), FALSE, "%s", secfile_error());
5311
5312 sg_warn_ret_val(secfile_lookup_int(loading->file, &pcity->turn_last_built,
5313 "%s.turn_last_built", citystr),
5314 FALSE, "%s", secfile_error());
5315
5316 kind = secfile_lookup_str(loading->file, "%s.currently_building_kind",
5317 citystr);
5318 name = secfile_lookup_str(loading->file, "%s.currently_building_name",
5319 citystr);
5320 pcity->production = universal_by_rule_name(kind, name);
5321 sg_warn_ret_val(pcity->production.kind != universals_n_invalid(), FALSE,
5322 "%s.currently_building: unknown \"%s\" \"%s\".",
5323 citystr, kind, name);
5324
5325 want = secfile_lookup_int_default(loading->file, 0,
5326 "%s.current_want", citystr);
5327 if (pcity->production.kind == VUT_IMPROVEMENT) {
5328 pcity->server.adv->
5329 building_want[improvement_index(pcity->production.value.building)]
5330 = want;
5331 }
5332
5333 kind = secfile_lookup_str(loading->file, "%s.changed_from_kind",
5334 citystr);
5335 name = secfile_lookup_str(loading->file, "%s.changed_from_name",
5336 citystr);
5339 "%s.changed_from: unknown \"%s\" \"%s\".",
5340 citystr, kind, name);
5341
5342 pcity->before_change_shields =
5343 secfile_lookup_int_default(loading->file, pcity->shield_stock,
5344 "%s.before_change_shields", citystr);
5345 pcity->caravan_shields =
5347 "%s.caravan_shields", citystr);
5348 pcity->disbanded_shields =
5350 "%s.disbanded_shields", citystr);
5351 pcity->last_turns_shield_surplus =
5353 "%s.last_turns_shield_surplus",
5354 citystr);
5355
5357 "%s.style", citystr);
5358 if (stylename != NULL) {
5360 } else {
5361 pcity->style = 0;
5362 }
5363 if (pcity->style < 0) {
5364 pcity->style = city_style(pcity);
5365 }
5366
5367 pcity->server.synced = FALSE; /* Must re-sync with clients */
5368
5369 /* Initialise list of city improvements. */
5370 for (i = 0; i < ARRAY_SIZE(pcity->built); i++) {
5371 pcity->built[i].turn = I_NEVER;
5372 }
5373
5374 /* Load city improvements. */
5375 str = secfile_lookup_str(loading->file, "%s.improvements", citystr);
5377 sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE,
5378 "Invalid length of '%s.improvements' ("
5379 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
5380 citystr, strlen(str), loading->improvement.size);
5381 for (i = 0; i < loading->improvement.size; i++) {
5382 sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE,
5383 "Undefined value '%c' within '%s.improvements'.",
5384 str[i], citystr)
5385
5386 if (str[i] == '1') {
5387 struct impr_type *pimprove
5388 = improvement_by_rule_name(loading->improvement.order[i]);
5389
5390 if (pimprove) {
5391 city_add_improvement(pcity, pimprove);
5392 }
5393 }
5394 }
5395
5396 sg_failure_ret_val(loading->worked_tiles != NULL, FALSE,
5397 "No worked tiles map defined.");
5398
5400
5401 /* Load new savegame with variable (squared) city radius and worked
5402 * tiles map */
5403
5404 int radius_sq
5405 = secfile_lookup_int_default(loading->file, -1, "%s.city_radius_sq",
5406 citystr);
5407 city_map_radius_sq_set(pcity, radius_sq);
5408
5410 if (loading->worked_tiles[ptile->index] == pcity->id) {
5411 if (sq_map_distance(ptile, pcity->tile) > radius_sq) {
5412 log_sg("[%s] '%s' (%d, %d) has worker outside current radius "
5413 "at (%d, %d); repairing", citystr, city_name_get(pcity),
5414 TILE_XY(pcity->tile), TILE_XY(ptile));
5415 pcity->specialists[DEFAULT_SPECIALIST]++;
5416 sp_count++;
5417 } else {
5418 tile_set_worked(ptile, pcity);
5419 workers++;
5420 }
5421
5422#ifdef FREECIV_DEBUG
5423 /* Set this tile to unused; a check for not reset tiles is
5424 * included in game_load_internal() */
5425 loading->worked_tiles[ptile->index] = -1;
5426#endif /* FREECIV_DEBUG */
5427 }
5429
5430 if (tile_worked(city_tile(pcity)) != pcity) {
5431 struct city *pwork = tile_worked(city_tile(pcity));
5432
5433 if (NULL != pwork) {
5434 log_sg("[%s] city center of '%s' (%d,%d) [%d] is worked by '%s' "
5435 "(%d,%d) [%d]; repairing", citystr, city_name_get(pcity),
5438
5439 tile_set_worked(city_tile(pcity), NULL); /* Remove tile from pwork */
5440 pwork->specialists[DEFAULT_SPECIALIST]++;
5442 } else {
5443 log_sg("[%s] city center of '%s' (%d,%d) [%d] is empty; repairing",
5446 }
5447
5448 /* Repair pcity */
5451 }
5452
5454 if (0 != repair) {
5455 log_sg("[%s] size mismatch for '%s' (%d,%d): size [%d] != "
5456 "(workers [%d] - free worked tiles [%d]) + specialists [%d]",
5458 workers, FREE_WORKED_TILES, sp_count);
5459
5460 /* Repair pcity */
5462 }
5463
5464 /* worklist_init() done in create_city_virtual() */
5465 worklist_load(loading->file, wlist_max_length, &pcity->worklist, "%s", citystr);
5466
5467 /* Load city options. */
5468 BV_CLR_ALL(pcity->city_options);
5469 for (i = 0; i < loading->coptions.size; i++) {
5470 if (secfile_lookup_bool_default(loading->file, FALSE, "%s.option%d",
5471 citystr, i)) {
5472 BV_SET(pcity->city_options, loading->coptions.order[i]);
5473 }
5474 }
5476 "%s.wlcb", citystr),
5477 FALSE, "%s", secfile_error());
5478 pcity->wlcb = tmp_int;
5479
5480 /* Load the city rally point. */
5481 {
5482 int len = secfile_lookup_int_default(loading->file, 0,
5483 "%s.rally_point_length", citystr);
5484 int unconverted;
5485
5486 pcity->rally_point.length = len;
5487 if (len > 0) {
5489
5490 pcity->rally_point.orders
5491 = fc_malloc(len * sizeof(*(pcity->rally_point.orders)));
5492 pcity->rally_point.persistent
5494 "%s.rally_point_persistent", citystr);
5495 pcity->rally_point.vigilant
5497 "%s.rally_point_vigilant", citystr);
5498
5501 "%s.rally_point_orders", citystr);
5504 "%s.rally_point_dirs", citystr);
5507 "%s.rally_point_activities", citystr);
5508
5509 for (i = 0; i < len; i++) {
5510 struct unit_order *order = &pcity->rally_point.orders[i];
5511
5512 if (rally_orders[i] == '\0' || rally_dirs[i] == '\0'
5513 || rally_activities[i] == '\0') {
5514 log_sg("Invalid rally point.");
5515 free(pcity->rally_point.orders);
5516 pcity->rally_point.orders = NULL;
5517 pcity->rally_point.length = 0;
5518 break;
5519 }
5520 order->order = char2order(rally_orders[i]);
5521 order->dir = char2dir(rally_dirs[i]);
5522 order->activity = char2activity(rally_activities[i]);
5523
5525 "%s.rally_point_action_vec,%d",
5526 citystr, i);
5527
5528 if (unconverted == -1) {
5529 order->action = ACTION_NONE;
5530 } else if (unconverted >= 0 && unconverted < loading->action.size) {
5531 /* Look up what action id the unconverted number represents. */
5532 order->action = loading->action.order[unconverted];
5533 } else {
5534 if (order->order == ORDER_PERFORM_ACTION) {
5535 sg_regr(3020000, "Invalid action id in order for city rally point %d",
5536 pcity->id);
5537 }
5538
5539 order->action = ACTION_NONE;
5540 }
5541
5542 order->target
5544 "%s.rally_point_tgt_vec,%d",
5545 citystr, i);
5546 order->sub_target
5548 "%s.rally_point_sub_tgt_vec,%d",
5549 citystr, i);
5550 }
5551 } else {
5552 pcity->rally_point.orders = NULL;
5553
5554 (void) secfile_entry_lookup(loading->file, "%s.rally_point_persistent",
5555 citystr);
5556 (void) secfile_entry_lookup(loading->file, "%s.rally_point_vigilant",
5557 citystr);
5558 (void) secfile_entry_lookup(loading->file, "%s.rally_point_orders",
5559 citystr);
5560 (void) secfile_entry_lookup(loading->file, "%s.rally_point_dirs",
5561 citystr);
5562 (void) secfile_entry_lookup(loading->file, "%s.rally_point_activities",
5563 citystr);
5564 (void) secfile_entry_lookup(loading->file, "%s.rally_point_action_vec",
5565 citystr);
5567 "%s.rally_point_tgt_vec", citystr);
5569 "%s.rally_point_sub_tgt_vec", citystr);
5570 }
5571 }
5572
5573 /* Load the city manager parameters. */
5574 {
5575 bool enabled = secfile_lookup_bool_default(loading->file, FALSE,
5576 "%s.cma_enabled", citystr);
5577 if (enabled) {
5578 struct cm_parameter *param = fc_calloc(1, sizeof(struct cm_parameter));
5579
5580 for (i = 0; i < O_LAST; i++) {
5582 loading->file, 0, "%s.cma_minimal_surplus,%d", citystr, i);
5584 loading->file, 0, "%s.cma_factor,%d", citystr, i);
5585 }
5586
5588 loading->file, FALSE, "%s.max_growth", citystr);
5590 loading->file, FALSE, "%s.require_happy", citystr);
5592 loading->file, FALSE, "%s.allow_disorder", citystr);
5594 loading->file, FALSE, "%s.allow_specialists", citystr);
5596 loading->file, 0, "%s.happy_factor", citystr);
5597 pcity->cm_parameter = param;
5598 } else {
5599 pcity->cm_parameter = NULL;
5600
5601 for (i = 0; i < O_LAST; i++) {
5603 "%s.cma_minimal_surplus,%d", citystr, i);
5605 "%s.cma_factor,%d", citystr, i);
5606 }
5607
5608 (void) secfile_entry_lookup(loading->file, "%s.max_growth",
5609 citystr);
5610 (void) secfile_entry_lookup(loading->file, "%s.require_happy",
5611 citystr);
5612 (void) secfile_entry_lookup(loading->file, "%s.allow_disorder",
5613 citystr);
5614 (void) secfile_entry_lookup(loading->file, "%s.allow_specialists",
5615 citystr);
5616 (void) secfile_entry_lookup(loading->file, "%s.happy_factor",
5617 citystr);
5618 }
5619 }
5620
5621 CALL_FUNC_EACH_AI(city_load, loading->file, pcity, citystr);
5622
5623 return TRUE;
5624}
5625
5626/************************************************************************/
5630 struct player *plr,
5631 struct city *pcity,
5632 const char *citystr)
5633{
5635 citizens size;
5636
5638 player_slots_iterate(pslot) {
5639 int nationality;
5640
5641 nationality = secfile_lookup_int_default(loading->file, -1,
5642 "%s.citizen%d", citystr,
5643 player_slot_index(pslot));
5644 if (nationality > 0 && !player_slot_is_used(pslot)) {
5645 log_sg("Citizens of an invalid nation for %s (player slot %d)!",
5647 continue;
5648 }
5649
5650 if (nationality != -1 && player_slot_is_used(pslot)) {
5651 sg_warn(nationality >= 0 && nationality <= MAX_CITY_SIZE,
5652 "Invalid value for citizens of player %d in %s: %d.",
5653 player_slot_index(pslot), city_name_get(pcity), nationality);
5654 citizens_nation_set(pcity, pslot, nationality);
5655 }
5657 /* Sanity check. */
5659 if (size != city_size_get(pcity)) {
5660 if (size != 0) {
5661 /* size == 0 can be result from the fact that ruleset had no
5662 * nationality enabled at saving time, so no citizens at all
5663 * were saved. But something more serious must be going on if
5664 * citizens have been saved partially - if some of them are there. */
5665 log_sg("City size and number of citizens does not match in %s "
5666 "(%d != %d)! Repairing ...", city_name_get(pcity),
5668 }
5670 }
5671 }
5672}
5673
5674/************************************************************************/
5678 struct player *plr)
5679{
5681 int i = 0;
5682 int plrno = player_number(plr);
5684
5685 /* Check status and return if not OK (sg_success FALSE). */
5686 sg_check_ret();
5687
5689 "player%d.ncities", plrno);
5690
5692 /* Initialise the nation list for the citizens information. */
5693 player_slots_iterate(pslot) {
5696 }
5697
5698 /* First determine length of longest worklist, rally point order, and the
5699 * nationalities we have. */
5701 int routes;
5702
5703 /* Check the sanity of the city. */
5706
5707 if (pcity->worklist.length > wlist_max_length) {
5708 wlist_max_length = pcity->worklist.length;
5709 }
5710
5711 if (pcity->rally_point.length > rally_point_max_length) {
5712 rally_point_max_length = pcity->rally_point.length;
5713 }
5714
5715 routes = city_num_trade_routes(pcity);
5716 if (routes > routes_max) {
5717 routes_max = routes;
5718 }
5719
5721 /* Find all nations of the citizens,*/
5722 players_iterate(pplayer) {
5723 if (!nations[player_index(pplayer)]
5724 && citizens_nation_get(pcity, pplayer->slot) != 0) {
5725 nations[player_index(pplayer)] = TRUE;
5726 }
5728 }
5730
5732 "player%d.wl_max_length", plrno);
5734 "player%d.routes_max_length", plrno);
5735
5737 struct tile *pcenter = city_tile(pcity);
5738 char impr_buf[B_LAST + 1];
5739 char buf[32];
5740 int j, nat_x, nat_y;
5741
5742 fc_snprintf(buf, sizeof(buf), "player%d.c%d", plrno, i);
5743
5744
5746 secfile_insert_int(saving->file, nat_y, "%s.y", buf);
5747 secfile_insert_int(saving->file, nat_x, "%s.x", buf);
5748
5749 secfile_insert_int(saving->file, pcity->id, "%s.id", buf);
5750
5751 if (pcity->original != NULL) {
5752 secfile_insert_int(saving->file, player_number(pcity->original),
5753 "%s.original", buf);
5754 } else {
5755 secfile_insert_int(saving->file, -1, "%s.original", buf);
5756 }
5757 secfile_insert_int(saving->file, city_size_get(pcity), "%s.size", buf);
5758
5760 "%s.capital", buf);
5761
5762 j = 0;
5764 secfile_insert_int(saving->file, pcity->specialists[sp], "%s.nspe%d",
5765 buf, j++);
5767
5768 j = 0;
5770 secfile_insert_int(saving->file, proute->partner, "%s.traderoute%d",
5771 buf, j);
5773 "%s.route_direction%d", buf, j);
5775 "%s.route_good%d", buf, j);
5776 j++;
5778
5779 /* Save dummy values to keep tabular format happy */
5780 for (; j < routes_max; j++) {
5781 secfile_insert_int(saving->file, 0, "%s.traderoute%d", buf, j);
5783 "%s.route_direction%d", buf, j);
5785 "%s.route_good%d", buf, j);
5786 }
5787
5788 secfile_insert_int(saving->file, pcity->food_stock, "%s.food_stock",
5789 buf);
5790 secfile_insert_int(saving->file, pcity->shield_stock, "%s.shield_stock",
5791 buf);
5792 secfile_insert_int(saving->file, pcity->history, "%s.history",
5793 buf);
5794
5795 secfile_insert_int(saving->file, pcity->airlift, "%s.airlift",
5796 buf);
5797 secfile_insert_bool(saving->file, pcity->was_happy, "%s.was_happy",
5798 buf);
5799 secfile_insert_bool(saving->file, pcity->had_famine, "%s.had_famine",
5800 buf);
5801 secfile_insert_int(saving->file, pcity->turn_plague, "%s.turn_plague",
5802 buf);
5803
5804 secfile_insert_int(saving->file, pcity->anarchy, "%s.anarchy", buf);
5805 secfile_insert_int(saving->file, pcity->rapture, "%s.rapture", buf);
5806 secfile_insert_int(saving->file, pcity->steal, "%s.steal", buf);
5807 secfile_insert_int(saving->file, pcity->turn_founded, "%s.turn_founded",
5808 buf);
5809 secfile_insert_int(saving->file, pcity->acquire_t, "%s.acquire_t", buf);
5810 secfile_insert_bool(saving->file, pcity->did_buy, "%s.did_buy", buf);
5811 secfile_insert_bool(saving->file, pcity->did_sell, "%s.did_sell", buf);
5812 secfile_insert_int(saving->file, pcity->turn_last_built,
5813 "%s.turn_last_built", buf);
5814
5815 /* For visual debugging, variable length strings together here */
5816 secfile_insert_str(saving->file, city_name_get(pcity), "%s.name", buf);
5817
5819 "%s.currently_building_kind", buf);
5821 "%s.currently_building_name", buf);
5822
5823 if (pcity->production.kind == VUT_IMPROVEMENT) {
5825 pcity->server.adv->
5826 building_want[improvement_index(pcity->production.value.building)],
5827 "%s.current_want", buf);
5828 } else {
5829 secfile_insert_int(saving->file, 0,
5830 "%s.current_want", buf);
5831 }
5832
5834 "%s.changed_from_kind", buf);
5836 "%s.changed_from_name", buf);
5837
5838 secfile_insert_int(saving->file, pcity->before_change_shields,
5839 "%s.before_change_shields", buf);
5840 secfile_insert_int(saving->file, pcity->caravan_shields,
5841 "%s.caravan_shields", buf);
5842 secfile_insert_int(saving->file, pcity->disbanded_shields,
5843 "%s.disbanded_shields", buf);
5844 secfile_insert_int(saving->file, pcity->last_turns_shield_surplus,
5845 "%s.last_turns_shield_surplus", buf);
5846
5848 "%s.style", buf);
5849
5850 /* Save the squared city radius and all tiles within the corresponding
5851 * city map. */
5852 secfile_insert_int(saving->file, pcity->city_radius_sq,
5853 "player%d.c%d.city_radius_sq", plrno, i);
5854 /* The tiles worked by the city are saved using the main map.
5855 * See also sg_save_map_worked(). */
5856
5857 /* Save improvement list as bytevector. Note that improvement order
5858 * is saved in savefile.improvement_order. */
5859 improvement_iterate(pimprove) {
5860 impr_buf[improvement_index(pimprove)]
5861 = (pcity->built[improvement_index(pimprove)].turn <= I_NEVER) ? '0'
5862 : '1';
5864 impr_buf[improvement_count()] = '\0';
5865
5867 "Invalid size of the improvement vector (%s.improvements: "
5868 SIZE_T_PRINTF " < " SIZE_T_PRINTF ").", buf,
5869 strlen(impr_buf), sizeof(impr_buf));
5870 secfile_insert_str(saving->file, impr_buf, "%s.improvements", buf);
5871
5872 worklist_save(saving->file, &pcity->worklist, wlist_max_length, "%s",
5873 buf);
5874
5875 for (j = 0; j < CITYO_LAST; j++) {
5876 secfile_insert_bool(saving->file, BV_ISSET(pcity->city_options, j),
5877 "%s.option%d", buf, j);
5878 }
5879 secfile_insert_int(saving->file, pcity->wlcb,
5880 "%s.wlcb", buf);
5881
5882 CALL_FUNC_EACH_AI(city_save, saving->file, pcity, buf);
5883
5885 /* Save nationality of the citizens,*/
5886 players_iterate(pplayer) {
5887 if (nations[player_index(pplayer)]) {
5889 citizens_nation_get(pcity, pplayer->slot),
5890 "%s.citizen%d", buf, player_index(pplayer));
5891 }
5893 }
5894
5895 secfile_insert_int(saving->file, pcity->rally_point.length,
5896 "%s.rally_point_length", buf);
5897 if (pcity->rally_point.length) {
5898 int len = pcity->rally_point.length;
5899 char orders[len + 1], dirs[len + 1], activities[len + 1];
5900 int actions[len];
5901 int targets[len];
5902 int sub_targets[len];
5903
5904 secfile_insert_bool(saving->file, pcity->rally_point.persistent,
5905 "%s.rally_point_persistent", buf);
5906 secfile_insert_bool(saving->file, pcity->rally_point.vigilant,
5907 "%s.rally_point_vigilant", buf);
5908
5909 for (j = 0; j < len; j++) {
5910 orders[j] = order2char(pcity->rally_point.orders[j].order);
5911 dirs[j] = '?';
5912 activities[j] = '?';
5913 targets[j] = NO_TARGET;
5914 sub_targets[j] = NO_TARGET;
5915 actions[j] = -1;
5916 switch (pcity->rally_point.orders[j].order) {
5917 case ORDER_MOVE:
5918 case ORDER_ACTION_MOVE:
5919 dirs[j] = dir2char(pcity->rally_point.orders[j].dir);
5920 break;
5921 case ORDER_ACTIVITY:
5922 sub_targets[j] = pcity->rally_point.orders[j].sub_target;
5923 activities[j]
5924 = activity2char(pcity->rally_point.orders[j].activity);
5925 actions[j]
5926 = pcity->rally_point.orders[j].action;
5927 break;
5929 actions[j] = pcity->rally_point.orders[j].action;
5930 targets[j] = pcity->rally_point.orders[j].target;
5931 sub_targets[j] = pcity->rally_point.orders[j].sub_target;
5932 break;
5933 case ORDER_FULL_MP:
5934 case ORDER_LAST:
5935 break;
5936 }
5937
5938 if (actions[j] == ACTION_NONE) {
5939 actions[j] = -1;
5940 }
5941 }
5942 orders[len] = dirs[len] = activities[len] = '\0';
5943
5944 secfile_insert_str(saving->file, orders, "%s.rally_point_orders", buf);
5945 secfile_insert_str(saving->file, dirs, "%s.rally_point_dirs", buf);
5946 secfile_insert_str(saving->file, activities,
5947 "%s.rally_point_activities", buf);
5948
5950 "%s.rally_point_action_vec", buf);
5951 /* Fill in dummy values for order targets so the registry will save
5952 * the unit table in a tabular format. */
5953 for (j = len; j < rally_point_max_length; j++) {
5954 secfile_insert_int(saving->file, -1, "%s.rally_point_action_vec,%d",
5955 buf, j);
5956 }
5957
5958 secfile_insert_int_vec(saving->file, targets, len,
5959 "%s.rally_point_tgt_vec", buf);
5960 /* Fill in dummy values for order targets so the registry will save
5961 * the unit table in a tabular format. */
5962 for (j = len; j < rally_point_max_length; j++) {
5964 "%s.rally_point_tgt_vec,%d", buf, j);
5965 }
5966
5967 secfile_insert_int_vec(saving->file, sub_targets, len,
5968 "%s.rally_point_sub_tgt_vec", buf);
5969 /* Fill in dummy values for order targets so the registry will save
5970 * the unit table in a tabular format. */
5971 for (j = len; j < rally_point_max_length; j++) {
5972 secfile_insert_int(saving->file, -1, "%s.rally_point_sub_tgt_vec,%d",
5973 buf, j);
5974 }
5975 } else {
5976 /* Put all the same fields into the savegame - otherwise the
5977 * registry code can't correctly use a tabular format and the
5978 * savegame will be bigger. */
5979 secfile_insert_bool(saving->file, FALSE, "%s.rally_point_persistent",
5980 buf);
5981 secfile_insert_bool(saving->file, FALSE, "%s.rally_point_vigilant",
5982 buf);
5983 secfile_insert_str(saving->file, "-", "%s.rally_point_orders", buf);
5984 secfile_insert_str(saving->file, "-", "%s.rally_point_dirs", buf);
5985 secfile_insert_str(saving->file, "-", "%s.rally_point_activities",
5986 buf);
5987
5988 /* Fill in dummy values for order targets so the registry will save
5989 * the unit table in a tabular format. */
5990
5991 /* The start of a vector has no number. */
5992 secfile_insert_int(saving->file, -1, "%s.rally_point_action_vec",
5993 buf);
5994 for (j = 1; j < rally_point_max_length; j++) {
5995 secfile_insert_int(saving->file, -1, "%s.rally_point_action_vec,%d",
5996 buf, j);
5997 }
5998
5999 /* The start of a vector has no number. */
6000 secfile_insert_int(saving->file, NO_TARGET, "%s.rally_point_tgt_vec",
6001 buf);
6002 for (j = 1; j < rally_point_max_length; j++) {
6004 "%s.rally_point_tgt_vec,%d", buf, j);
6005 }
6006
6007 /* The start of a vector has no number. */
6008 secfile_insert_int(saving->file, -1, "%s.rally_point_sub_tgt_vec",
6009 buf);
6010 for (j = 1; j < rally_point_max_length; j++) {
6011 secfile_insert_int(saving->file, -1, "%s.rally_point_sub_tgt_vec,%d",
6012 buf, j);
6013 }
6014 }
6015
6016 secfile_insert_bool(saving->file, pcity->cm_parameter != NULL,
6017 "%s.cma_enabled", buf);
6018 if (pcity->cm_parameter) {
6020 pcity->cm_parameter->minimal_surplus, O_LAST,
6021 "%s.cma_minimal_surplus", buf);
6023 pcity->cm_parameter->factor, O_LAST,
6024 "%s.cma_factor", buf);
6025 secfile_insert_bool(saving->file, pcity->cm_parameter->max_growth,
6026 "%s.max_growth", buf);
6027 secfile_insert_bool(saving->file, pcity->cm_parameter->require_happy,
6028 "%s.require_happy", buf);
6029 secfile_insert_bool(saving->file, pcity->cm_parameter->allow_disorder,
6030 "%s.allow_disorder", buf);
6032 pcity->cm_parameter->allow_specialists,
6033 "%s.allow_specialists", buf);
6034 secfile_insert_int(saving->file, pcity->cm_parameter->happy_factor,
6035 "%s.happy_factor", buf);
6036 } else {
6037 int zeros[O_LAST];
6038
6039 memset(zeros, 0, sizeof(zeros));
6041 "%s.cma_minimal_surplus", buf);
6043 "%s.cma_factor", buf);
6044 secfile_insert_bool(saving->file, FALSE, "%s.max_growth", buf);
6045 secfile_insert_bool(saving->file, FALSE, "%s.require_happy", buf);
6046 secfile_insert_bool(saving->file, FALSE, "%s.allow_disorder", buf);
6047 secfile_insert_bool(saving->file, FALSE, "%s.allow_specialists", buf);
6048 secfile_insert_int(saving->file, 0, "%s.happy_factor", buf);
6049 }
6050
6051 i++;
6053
6054 i = 0;
6056 worker_task_list_iterate(pcity->task_reqs, ptask) {
6057 int nat_x, nat_y;
6058
6060 secfile_insert_int(saving->file, pcity->id, "player%d.task%d.city",
6061 plrno, i);
6062 secfile_insert_int(saving->file, nat_y, "player%d.task%d.y", plrno, i);
6063 secfile_insert_int(saving->file, nat_x, "player%d.task%d.x", plrno, i);
6065 "player%d.task%d.activity",
6066 plrno, i);
6067 if (ptask->tgt != NULL) {
6069 "player%d.task%d.target",
6070 plrno, i);
6071 } else {
6072 secfile_insert_str(saving->file, "-",
6073 "player%d.task%d.target",
6074 plrno, i);
6075 }
6076 secfile_insert_int(saving->file, ptask->want, "player%d.task%d.want", plrno, i);
6077
6078 i++;
6081}
6082
6083/************************************************************************/
6087 struct player *plr)
6088{
6089 int nunits, i, plrno = player_number(plr);
6090 size_t orders_max_length;
6091
6092 /* Check status and return if not OK (sg_success FALSE). */
6093 sg_check_ret();
6094
6096 "player%d.nunits", plrno),
6097 "%s", secfile_error());
6098 if (!plr->is_alive && nunits > 0) {
6099 log_sg("'player%d.nunits' = %d for dead player!", plrno, nunits);
6100 nunits = 0; /* Some old savegames may be buggy. */
6101 }
6102
6104 "player%d.orders_max_length",
6105 plrno);
6106
6107 for (i = 0; i < nunits; i++) {
6108 struct unit *punit;
6109 struct city *pcity;
6110 const char *name;
6111 char buf[32];
6112 struct unit_type *type;
6113 struct tile *ptile;
6114
6115 fc_snprintf(buf, sizeof(buf), "player%d.u%d", plrno, i);
6116
6117 name = secfile_lookup_str(loading->file, "%s.type_by_name", buf);
6119 sg_failure_ret(type != NULL, "%s: unknown unit type \"%s\".", buf, name);
6120
6121 /* Create a dummy unit. */
6122 punit = unit_virtual_create(plr, NULL, type, 0);
6125 sg_failure_ret(FALSE, "Error loading unit %d of player %d.", i, plrno);
6126 }
6127
6130
6132 unit_list_prepend(pcity->units_supported, punit);
6133 } else if (punit->homecity > IDENTITY_NUMBER_ZERO) {
6134 log_sg("%s: bad home city %d.", buf, punit->homecity);
6136 }
6137
6138 ptile = unit_tile(punit);
6139
6140 /* allocate the unit's contribution to fog of war */
6143 /* NOTE: There used to be some map_set_known calls here. These were
6144 * unneeded since unfogging the tile when the unit sees it will
6145 * automatically reveal that tile. */
6146
6149 }
6150}
6151
6152/************************************************************************/
6156 struct player *plr, struct unit *punit,
6158 const char *unitstr)
6159{
6160 enum unit_activity activity;
6161 int nat_x, nat_y;
6162 struct extra_type *pextra = NULL;
6163 struct tile *ptile;
6164 int extra_id;
6165 int ei;
6166 const char *facing_str;
6167 int natnbr;
6168 int unconverted;
6169 const char *str;
6170 enum gen_action action;
6171
6173 unitstr), FALSE, "%s", secfile_error());
6175 FALSE, "%s", secfile_error());
6177 FALSE, "%s", secfile_error());
6178
6179 ptile = native_pos_to_tile(&(wld.map), nat_x, nat_y);
6180 sg_warn_ret_val(NULL != ptile, FALSE, "%s invalid tile (%d, %d)",
6181 unitstr, nat_x, nat_y);
6182 unit_tile_set(punit, ptile);
6183
6186 "%s.facing", unitstr);
6187 if (facing_str[0] != 'x') {
6188 /* We don't touch punit->facing if savegame does not contain that
6189 * information. Initial orientation set by unit_virtual_create()
6190 * is as good as any. */
6191 enum direction8 facing = char2dir(facing_str[0]);
6192
6193 if (direction8_is_valid(facing)) {
6194 punit->facing = facing;
6195 } else {
6196 log_error("Illegal unit orientation '%s'", facing_str);
6197 }
6198 }
6199
6200 /* If savegame has unit nationality, it doesn't hurt to
6201 * internally set it even if nationality rules are disabled. */
6203 player_number(plr),
6204 "%s.nationality", unitstr);
6205
6207 if (punit->nationality == NULL) {
6208 punit->nationality = plr;
6209 }
6210
6212 "%s.homecity", unitstr), FALSE,
6213 "%s", secfile_error());
6215 "%s.moves", unitstr), FALSE,
6216 "%s", secfile_error());
6218 "%s.fuel", unitstr), FALSE,
6219 "%s", secfile_error());
6221 "%s.activity", unitstr), FALSE,
6222 "%s", secfile_error());
6223 activity = unit_activity_by_name(loading->activities.order[ei],
6226 "%s.action", unitstr), FALSE,
6227 "%s", secfile_error());
6228 if (ei == -1) {
6230 } else if (ei >= 0 && ei < loading->action.size) {
6231 action = loading->action.order[ei];
6232 } else {
6233 log_sg("Invalid action id for unit %d", punit->id);
6235 }
6236
6239 "%s.born", unitstr);
6242 "%s.current_form_turn", unitstr);
6243
6245 "%s.activity_tgt", unitstr);
6246
6247 if (extra_id != -2) {
6248 if (extra_id >= 0 && extra_id < loading->extra.size) {
6249 pextra = loading->extra.order[extra_id];
6250 set_unit_activity_targeted(punit, activity, pextra, action);
6251 } else if (activity == ACTIVITY_IRRIGATE) {
6255 punit);
6256 if (tgt != NULL) {
6258 } else {
6260 }
6261 } else if (activity == ACTIVITY_MINE) {
6263 EC_MINE,
6265 punit);
6266 if (tgt != NULL) {
6268 } else {
6270 }
6271 } else {
6272 set_unit_activity(punit, activity, action);
6273 }
6274 } else {
6276 } /* activity_tgt == NULL */
6277
6279 "%s.activity_count", unitstr), FALSE,
6280 "%s", secfile_error());
6281
6284 "%s.changed_from", unitstr);
6285
6287 "%s.changed_from_tgt", unitstr), FALSE,
6288 "%s", secfile_error());
6289
6290 if (extra_id >= 0 && extra_id < loading->extra.size) {
6291 punit->changed_from_target = loading->extra.order[extra_id];
6292 } else {
6294 }
6295
6298 "%s.changed_from_count", unitstr);
6299
6300 /* Special case: for a long time, we accidentally incremented
6301 * activity_count while a unit was sentried, so it could increase
6302 * without bound (bug #20641) and be saved in old savefiles.
6303 * We zero it to prevent potential trouble overflowing the range
6304 * in network packets, etc. */
6305 if (activity == ACTIVITY_SENTRY) {
6306 punit->activity_count = 0;
6307 }
6310 }
6311
6312 punit->veteran
6313 = secfile_lookup_int_default(loading->file, 0, "%s.veteran", unitstr);
6314 {
6315 /* Protect against change in veteran system in ruleset */
6316 const int levels = utype_veteran_levels(unit_type_get(punit));
6317
6318 if (punit->veteran >= levels) {
6319 fc_assert(levels >= 1);
6320 punit->veteran = levels - 1;
6321 }
6322 }
6325 "%s.done_moving", unitstr);
6328 "%s.battlegroup", unitstr);
6329
6331 "%s.go", unitstr)) {
6332 int gnat_x, gnat_y;
6333
6335 "%s.goto_x", unitstr), FALSE,
6336 "%s", secfile_error());
6338 "%s.goto_y", unitstr), FALSE,
6339 "%s", secfile_error());
6340
6342 } else {
6343 punit->goto_tile = NULL;
6344
6345 /* These variables are not used but needed for saving the unit table.
6346 * Load them to prevent unused variables errors. */
6347 (void) secfile_entry_lookup(loading->file, "%s.goto_x", unitstr);
6348 (void) secfile_entry_lookup(loading->file, "%s.goto_y", unitstr);
6349 }
6350
6351 /* Load AI data of the unit. */
6352 CALL_FUNC_EACH_AI(unit_load, loading->file, punit, unitstr);
6353
6356 "%s.server_side_agent",
6357 unitstr);
6358 if (unconverted >= 0 && unconverted < loading->ssa.size) {
6359 /* Look up what server side agent the unconverted number represents. */
6360 punit->ssa_controller = loading->ssa.order[unconverted];
6361 } else {
6362 log_sg("Invalid server side agent %d for unit %d",
6363 unconverted, punit->id);
6364
6366 }
6367
6369 "%s.hp", unitstr), FALSE,
6370 "%s", secfile_error());
6371
6373 = secfile_lookup_int_default(loading->file, 0, "%s.ord_map", unitstr);
6375 = secfile_lookup_int_default(loading->file, 0, "%s.ord_city", unitstr);
6376 punit->moved
6377 = secfile_lookup_bool_default(loading->file, FALSE, "%s.moved", unitstr);
6380 "%s.paradropped", unitstr);
6381 str = secfile_lookup_str_default(loading->file, "", "%s.carrying", unitstr);
6382 if (str[0] != '\0') {
6384 }
6385
6386 /* The transport status (punit->transported_by) is loaded in
6387 * sg_player_units_transport(). */
6388
6389 /* Initialize upkeep values: these are hopefully initialized
6390 * elsewhere before use (specifically, in city_support(); but
6391 * fixme: check whether always correctly initialized?).
6392 * Below is mainly for units which don't have homecity --
6393 * otherwise these don't get initialized (and AI calculations
6394 * etc may use junk values). */
6398
6400 "%s.action_decision", unitstr),
6401 FALSE, "%s", secfile_error());
6402
6403 if (unconverted >= 0 && unconverted < loading->act_dec.size) {
6404 /* Look up what action decision want the unconverted number
6405 * represents. */
6406 punit->action_decision_want = loading->act_dec.order[unconverted];
6407 } else {
6408 log_sg("Invalid action decision want for unit %d", punit->id);
6409
6411 }
6412
6414 /* Load the tile to act against. */
6415 int adwt_x, adwt_y;
6416
6417 if (secfile_lookup_int(loading->file, &adwt_x,
6418 "%s.action_decision_tile_x", unitstr)
6420 "%s.action_decision_tile_y", unitstr)) {
6422 adwt_x, adwt_y);
6423 } else {
6426 log_sg("Bad action_decision_tile for unit %d", punit->id);
6427 }
6428 } else {
6429 (void) secfile_entry_lookup(loading->file, "%s.action_decision_tile_x", unitstr);
6430 (void) secfile_entry_lookup(loading->file, "%s.action_decision_tile_y", unitstr);
6432 }
6433
6435
6436 /* Load the unit orders */
6437 {
6438 int len = secfile_lookup_int_default(loading->file, 0,
6439 "%s.orders_length", unitstr);
6440
6441 if (len > 0) {
6442 const char *orders_unitstr, *dir_unitstr, *act_unitstr;
6443 int j;
6444
6445 punit->orders.list = fc_malloc(len * sizeof(*(punit->orders.list)));
6449 "%s.orders_index", unitstr);
6452 "%s.orders_repeat", unitstr);
6455 "%s.orders_vigilant", unitstr);
6456
6459 "%s.orders_list", unitstr);
6462 "%s.dir_list", unitstr);
6465 "%s.activity_list", unitstr);
6466
6468
6469 for (j = 0; j < len; j++) {
6470 struct unit_order *order = &punit->orders.list[j];
6472 int order_sub_tgt;
6473
6474 if (orders_unitstr[j] == '\0' || dir_unitstr[j] == '\0'
6475 || act_unitstr[j] == '\0') {
6476 log_sg("Invalid unit orders.");
6478 break;
6479 }
6480 order->order = char2order(orders_unitstr[j]);
6481 order->dir = char2dir(dir_unitstr[j]);
6482 order->activity = char2activity(act_unitstr[j]);
6483
6485 "%s.action_vec,%d",
6486 unitstr, j);
6487
6488 if (unconverted == -1) {
6489 order->action = ACTION_NONE;
6490 } else if (unconverted >= 0 && unconverted < loading->action.size) {
6491 /* Look up what action id the unconverted number represents. */
6492 order->action = loading->action.order[unconverted];
6493 } else {
6494 if (order->order == ORDER_PERFORM_ACTION) {
6495 sg_regr(3020000, "Invalid action id in order for unit %d", punit->id);
6496 }
6497
6498 order->action = ACTION_NONE;
6499 }
6500
6501 if (order->order == ORDER_LAST
6502 || (order->order == ORDER_MOVE && !direction8_is_valid(order->dir))
6503 || (order->order == ORDER_ACTION_MOVE
6504 && !direction8_is_valid(order->dir))
6505 || (order->order == ORDER_PERFORM_ACTION
6506 && !action_id_exists(order->action))
6507 || (order->order == ORDER_ACTIVITY
6508 && (order->activity == ACTIVITY_LAST
6509 || !action_id_exists(order->action)))) {
6510 /* An invalid order. Just drop the orders for this unit. */
6512 punit->orders.list = NULL;
6513 punit->orders.length = 0;
6515 punit->goto_tile = NULL;
6516 break;
6517 }
6518
6520 "%s.tgt_vec,%d",
6521 unitstr, j);
6523 "%s.sub_tgt_vec,%d",
6524 unitstr, j);
6525
6526 if (order->order == ORDER_PERFORM_ACTION) {
6527 /* Validate sub target */
6528 switch (action_id_get_sub_target_kind(order->action)) {
6529 case ASTK_BUILDING:
6530 /* Sub target is a building. */
6532 /* Sub target is invalid. */
6533 log_sg("Cannot find building %d for %s to %s",
6536 order->sub_target = B_LAST;
6537 } else {
6538 order->sub_target = order_sub_tgt;
6539 }
6540 break;
6541 case ASTK_TECH:
6542 /* Sub target is a technology. */
6543 if (order_sub_tgt == A_NONE
6545 && order_sub_tgt != A_FUTURE)) {
6546 /* Target tech is invalid. */
6547 log_sg("Cannot find tech %d for %s to steal",
6549 order->sub_target = A_NONE;
6550 } else {
6551 order->sub_target = order_sub_tgt;
6552 }
6553 break;
6554 case ASTK_EXTRA:
6556 /* These take an extra. */
6558 break;
6559 case ASTK_SPECIALIST:
6560 /* A valid specialist must be supplied. */
6561 if (order_sub_tgt < 0
6562 || order_sub_tgt >= loading->specialist.size
6563 || !loading->specialist.order[order_sub_tgt]) {
6564 log_sg("Cannot find specialist %d for %s to become",
6566 order->sub_target = NO_TARGET;
6567 } else {
6568 order->sub_target = specialist_index(loading->specialist.order[order_sub_tgt]);
6569 }
6570 break;
6571 case ASTK_NONE:
6572 /* None of these can take a sub target. */
6574 "Specified sub target for action %d unsupported.",
6575 order->action);
6576 order->sub_target = NO_TARGET;
6577 break;
6578 case ASTK_COUNT:
6580 "Bad action action %d.",
6581 order->action);
6582 order->sub_target = NO_TARGET;
6583 break;
6584 }
6585 }
6586
6587 if (order->order == ORDER_ACTIVITY || action_wants_extra) {
6588 enum unit_activity act;
6589
6591 if (order_sub_tgt != EXTRA_NONE) {
6592 log_sg("Cannot find extra %d for %s to build",
6594 }
6595
6596 order->sub_target = EXTRA_NONE;
6597 } else {
6598 order->sub_target = order_sub_tgt;
6599 }
6600
6601 /* An action or an activity may require an extra target. */
6602 if (action_wants_extra) {
6603 act = action_id_get_activity(order->action);
6604 } else {
6605 act = order->activity;
6606 }
6607
6608 if (unit_activity_is_valid(act)
6610 && order->sub_target == EXTRA_NONE) {
6611 /* Missing required action extra target. */
6613 punit->orders.list = NULL;
6614 punit->orders.length = 0;
6616 punit->goto_tile = NULL;
6617 break;
6618 }
6619 } else if (order->order != ORDER_PERFORM_ACTION) {
6620 if (order_sub_tgt != -1) {
6621 log_sg("Unexpected sub_target %d (expected %d) for order type %d",
6622 order_sub_tgt, -1, order->order);
6623 }
6624 order->sub_target = NO_TARGET;
6625 }
6626 }
6627
6628 for (; j < orders_max_length; j++) {
6630 "%s.action_vec,%d", unitstr, j);
6632 "%s.tgt_vec,%d", unitstr, j);
6634 "%s.sub_tgt_vec,%d", unitstr, j);
6635 }
6636 } else {
6637 int j;
6638
6640 punit->goto_tile = NULL;
6641 punit->orders.list = NULL;
6642 punit->orders.length = 0;
6643
6644 (void) secfile_entry_lookup(loading->file, "%s.orders_index", unitstr);
6645 (void) secfile_entry_lookup(loading->file, "%s.orders_repeat", unitstr);
6646 (void) secfile_entry_lookup(loading->file, "%s.orders_vigilant", unitstr);
6647 (void) secfile_entry_lookup(loading->file, "%s.orders_list", unitstr);
6648 (void) secfile_entry_lookup(loading->file, "%s.dir_list", unitstr);
6649 (void) secfile_entry_lookup(loading->file, "%s.activity_list", unitstr);
6650 (void) secfile_entry_lookup(loading->file, "%s.action_vec", unitstr);
6651 (void) secfile_entry_lookup(loading->file, "%s.tgt_vec", unitstr);
6652 (void) secfile_entry_lookup(loading->file, "%s.sub_tgt_vec", unitstr);
6653
6654 for (j = 1; j < orders_max_length; j++) {
6656 "%s.action_vec,%d", unitstr, j);
6658 "%s.tgt_vec,%d", unitstr, j);
6660 "%s.sub_tgt_vec,%d", unitstr, j);
6661 }
6662 }
6663 }
6664
6665 return TRUE;
6666}
6667
6668/************************************************************************/
6673 struct player *plr)
6674{
6675 int nunits, i, plrno = player_number(plr);
6676
6677 /* Check status and return if not OK (sg_success FALSE). */
6678 sg_check_ret();
6679
6680 /* Recheck the number of units for the player. This is a copied from
6681 * sg_load_player_units(). */
6683 "player%d.nunits", plrno),
6684 "%s", secfile_error());
6685 if (!plr->is_alive && nunits > 0) {
6686 log_sg("'player%d.nunits' = %d for dead player!", plrno, nunits);
6687 nunits = 0; /* Some old savegames may be buggy. */
6688 }
6689
6690 for (i = 0; i < nunits; i++) {
6691 int id_unit, id_trans;
6692 struct unit *punit, *ptrans;
6693
6695 "player%d.u%d.id",
6696 plrno, i);
6698 fc_assert_action(punit != NULL, continue);
6699
6701 "player%d.u%d.transported_by",
6702 plrno, i);
6703 if (id_trans == -1) {
6704 /* Not transported. */
6705 continue;
6706 }
6707
6709 fc_assert_action(id_trans == -1 || ptrans != NULL, continue);
6710
6711 if (ptrans) {
6712#ifndef FREECIV_NDEBUG
6713 bool load_success =
6714#endif
6716
6717 fc_assert_action(load_success, continue);
6718 }
6719 }
6720}
6721
6722/************************************************************************/
6726 struct player *plr)
6727{
6728 int i = 0;
6729 int longest_order = 0;
6730 int plrno = player_number(plr);
6731
6732 /* Check status and return if not OK (sg_success FALSE). */
6733 sg_check_ret();
6734
6736 "player%d.nunits", plrno);
6737
6738 /* Find the longest unit order so different order length won't break
6739 * storing units in the tabular format. */
6741 if (punit->has_orders) {
6742 if (longest_order < punit->orders.length) {
6744 }
6745 }
6747
6749 "player%d.orders_max_length", plrno);
6750
6752 char buf[32];
6753 char dirbuf[2] = " ";
6754 int nat_x, nat_y;
6755 int last_order, j;
6756
6757 fc_snprintf(buf, sizeof(buf), "player%d.u%d", plrno, i);
6758 dirbuf[0] = dir2char(punit->facing);
6759 secfile_insert_int(saving->file, punit->id, "%s.id", buf);
6760
6762 secfile_insert_int(saving->file, nat_x, "%s.x", buf);
6763 secfile_insert_int(saving->file, nat_y, "%s.y", buf);
6764
6765 secfile_insert_str(saving->file, dirbuf, "%s.facing", buf);
6768 "%s.nationality", buf);
6769 }
6770 secfile_insert_int(saving->file, punit->veteran, "%s.veteran", buf);
6771 secfile_insert_int(saving->file, punit->hp, "%s.hp", buf);
6772 secfile_insert_int(saving->file, punit->homecity, "%s.homecity", buf);
6774 "%s.type_by_name", buf);
6775
6776 secfile_insert_int(saving->file, punit->activity, "%s.activity", buf);
6778 "%s.activity_count", buf);
6779 if (punit->action == ACTION_NONE) {
6780 secfile_insert_int(saving->file, -1, "%s.action", buf);
6781 } else {
6782 secfile_insert_int(saving->file, punit->action, "%s.action", buf);
6783 }
6784 if (punit->activity_target == NULL) {
6785 secfile_insert_int(saving->file, -1, "%s.activity_tgt", buf);
6786 } else {
6788 "%s.activity_tgt", buf);
6789 }
6790
6792 "%s.changed_from", buf);
6794 "%s.changed_from_count", buf);
6795 if (punit->changed_from_target == NULL) {
6796 secfile_insert_int(saving->file, -1, "%s.changed_from_tgt", buf);
6797 } else {
6799 "%s.changed_from_tgt", buf);
6800 }
6801
6803 "%s.done_moving", buf);
6804 secfile_insert_int(saving->file, punit->moves_left, "%s.moves", buf);
6805 secfile_insert_int(saving->file, punit->fuel, "%s.fuel", buf);
6807 "%s.born", buf);
6809 "%s.current_form_turn", buf);
6811 "%s.battlegroup", buf);
6812
6813 if (punit->goto_tile) {
6815 secfile_insert_bool(saving->file, TRUE, "%s.go", buf);
6816 secfile_insert_int(saving->file, nat_x, "%s.goto_x", buf);
6817 secfile_insert_int(saving->file, nat_y, "%s.goto_y", buf);
6818 } else {
6819 secfile_insert_bool(saving->file, FALSE, "%s.go", buf);
6820 /* Set this values to allow saving it as table. */
6821 secfile_insert_int(saving->file, 0, "%s.goto_x", buf);
6822 secfile_insert_int(saving->file, 0, "%s.goto_y", buf);
6823 }
6824
6826 "%s.server_side_agent", buf);
6827
6828 /* Save AI data of the unit. */
6829 CALL_FUNC_EACH_AI(unit_save, saving->file, punit, buf);
6830
6832 "%s.ord_map", buf);
6834 "%s.ord_city", buf);
6835 secfile_insert_bool(saving->file, punit->moved, "%s.moved", buf);
6837 "%s.paradropped", buf);
6839 ? unit_transport_get(punit)->id : -1,
6840 "%s.transported_by", buf);
6841 if (punit->carrying != NULL) {
6843 "%s.carrying", buf);
6844 } else {
6845 secfile_insert_str(saving->file, "", "%s.carrying", buf);
6846 }
6847
6849 "%s.action_decision", buf);
6850
6851 /* Stored as tile rather than direction to make sure the target tile is
6852 * sane. */
6857 "%s.action_decision_tile_x", buf);
6859 "%s.action_decision_tile_y", buf);
6860 } else {
6861 /* Dummy values to get tabular format. */
6862 secfile_insert_int(saving->file, -1,
6863 "%s.action_decision_tile_x", buf);
6864 secfile_insert_int(saving->file, -1,
6865 "%s.action_decision_tile_y", buf);
6866 }
6867
6869 "%s.stay", buf);
6870
6871 if (punit->has_orders) {
6872 int len = punit->orders.length;
6873 char orders_buf[len + 1], dir_buf[len + 1];
6874 char act_buf[len + 1];
6875 int action_buf[len];
6876 int tgt_vec[len];
6877 int sub_tgt_vec[len];
6878
6879 last_order = len;
6880
6881 secfile_insert_int(saving->file, len, "%s.orders_length", buf);
6883 "%s.orders_index", buf);
6885 "%s.orders_repeat", buf);
6887 "%s.orders_vigilant", buf);
6888
6889 for (j = 0; j < len; j++) {
6891 dir_buf[j] = '?';
6892 act_buf[j] = '?';
6893 tgt_vec[j] = NO_TARGET;
6894 sub_tgt_vec[j] = -1;
6895 action_buf[j] = -1;
6896 switch (punit->orders.list[j].order) {
6897 case ORDER_MOVE:
6898 case ORDER_ACTION_MOVE:
6899 dir_buf[j] = dir2char(punit->orders.list[j].dir);
6900 break;
6901 case ORDER_ACTIVITY:
6905 break;
6908 tgt_vec[j] = punit->orders.list[j].target;
6910 break;
6911 case ORDER_FULL_MP:
6912 case ORDER_LAST:
6913 break;
6914 }
6915
6916 if (action_buf[j] == ACTION_NONE) {
6917 action_buf[j] = -1;
6918 }
6919 }
6920 orders_buf[len] = dir_buf[len] = act_buf[len] = '\0';
6921
6922 secfile_insert_str(saving->file, orders_buf, "%s.orders_list", buf);
6923 secfile_insert_str(saving->file, dir_buf, "%s.dir_list", buf);
6924 secfile_insert_str(saving->file, act_buf, "%s.activity_list", buf);
6925
6927 "%s.action_vec", buf);
6928 /* Fill in dummy values for order targets so the registry will save
6929 * the unit table in a tabular format. */
6930 for (j = last_order; j < longest_order; j++) {
6931 secfile_insert_int(saving->file, -1, "%s.action_vec,%d", buf, j);
6932 }
6933
6935 "%s.tgt_vec", buf);
6936 /* Fill in dummy values for order targets so the registry will save
6937 * the unit table in a tabular format. */
6938 for (j = last_order; j < longest_order; j++) {
6939 secfile_insert_int(saving->file, NO_TARGET, "%s.tgt_vec,%d", buf, j);
6940 }
6941
6943 "%s.sub_tgt_vec", buf);
6944 /* Fill in dummy values for order targets so the registry will save
6945 * the unit table in a tabular format. */
6946 for (j = last_order; j < longest_order; j++) {
6947 secfile_insert_int(saving->file, -1, "%s.sub_tgt_vec,%d", buf, j);
6948 }
6949 } else {
6950
6951 /* Put all the same fields into the savegame - otherwise the
6952 * registry code can't correctly use a tabular format and the
6953 * savegame will be bigger. */
6954 secfile_insert_int(saving->file, 0, "%s.orders_length", buf);
6955 secfile_insert_int(saving->file, 0, "%s.orders_index", buf);
6956 secfile_insert_bool(saving->file, FALSE, "%s.orders_repeat", buf);
6957 secfile_insert_bool(saving->file, FALSE, "%s.orders_vigilant", buf);
6958 secfile_insert_str(saving->file, "-", "%s.orders_list", buf);
6959 secfile_insert_str(saving->file, "-", "%s.dir_list", buf);
6960 secfile_insert_str(saving->file, "-", "%s.activity_list", buf);
6961
6962 /* Fill in dummy values for order targets so the registry will save
6963 * the unit table in a tabular format. */
6964
6965 /* The start of a vector has no number. */
6966 secfile_insert_int(saving->file, -1, "%s.action_vec", buf);
6967 for (j = 1; j < longest_order; j++) {
6968 secfile_insert_int(saving->file, -1, "%s.action_vec,%d", buf, j);
6969 }
6970
6971 /* The start of a vector has no number. */
6972 secfile_insert_int(saving->file, NO_TARGET, "%s.tgt_vec", buf);
6973 for (j = 1; j < longest_order; j++) {
6974 secfile_insert_int(saving->file, NO_TARGET, "%s.tgt_vec,%d", buf, j);
6975 }
6976
6977 /* The start of a vector has no number. */
6978 secfile_insert_int(saving->file, -1, "%s.sub_tgt_vec", buf);
6979 for (j = 1; j < longest_order; j++) {
6980 secfile_insert_int(saving->file, -1, "%s.sub_tgt_vec,%d", buf, j);
6981 }
6982 }
6983
6984 i++;
6986}
6987
6988/************************************************************************/
6992 struct player *plr)
6993{
6994 int plrno = player_number(plr);
6995
6996 /* Check status and return if not OK (sg_success FALSE). */
6997 sg_check_ret();
6998
6999 /* Toss any existing attribute_block (should not exist) */
7000 if (plr->attribute_block.data) {
7002 plr->attribute_block.data = NULL;
7003 }
7004
7005 /* This is a big heap of opaque data for the client, check everything! */
7007 loading->file, 0, "player%d.attribute_v2_block_length", plrno);
7008
7009 if (0 > plr->attribute_block.length) {
7010 log_sg("player%d.attribute_v2_block_length=%d too small", plrno,
7011 plr->attribute_block.length);
7012 plr->attribute_block.length = 0;
7013 } else if (MAX_ATTRIBUTE_BLOCK < plr->attribute_block.length) {
7014 log_sg("player%d.attribute_v2_block_length=%d too big (max %d)",
7016 plr->attribute_block.length = 0;
7017 } else if (0 < plr->attribute_block.length) {
7018 int part_nr, parts;
7019 int quoted_length;
7020 char *quoted;
7021#ifndef FREECIV_NDEBUG
7022 size_t actual_length;
7023#endif
7024
7027 "player%d.attribute_v2_block_length_quoted",
7028 plrno), "%s", secfile_error());
7031 "player%d.attribute_v2_block_parts", plrno),
7032 "%s", secfile_error());
7033
7035 quoted[0] = '\0';
7037 for (part_nr = 0; part_nr < parts; part_nr++) {
7038 const char *current =
7040 "player%d.attribute_v2_block_data.part%d",
7041 plrno, part_nr);
7042 if (!current) {
7043 log_sg("attribute_v2_block_parts=%d actual=%d", parts, part_nr);
7044 break;
7045 }
7046 log_debug("attribute_v2_block_length_quoted=%d"
7047 " have=" SIZE_T_PRINTF " part=" SIZE_T_PRINTF,
7048 quoted_length, strlen(quoted), strlen(current));
7049 fc_assert(strlen(quoted) + strlen(current) <= quoted_length);
7050 strcat(quoted, current);
7051 }
7053 "attribute_v2_block_length_quoted=%d"
7054 " actual=" SIZE_T_PRINTF,
7056
7057#ifndef FREECIV_NDEBUG
7059#endif
7061 plr->attribute_block.data,
7062 plr->attribute_block.length);
7064 free(quoted);
7065 }
7066}
7067
7068/************************************************************************/
7072 struct player *plr)
7073{
7074 int plrno = player_number(plr);
7075
7076 /* Check status and return if not OK (sg_success FALSE). */
7077 sg_check_ret();
7078
7079 /* This is a big heap of opaque data from the client. Although the binary
7080 * format is not user editable, keep the lines short enough for debugging,
7081 * and hope that data compression will keep the file a reasonable size.
7082 * Note that the "quoted" format is a multiple of 3.
7083 */
7084#define PART_SIZE (3*256)
7085#define PART_ADJUST (3)
7086 if (plr->attribute_block.data) {
7087 char part[PART_SIZE + PART_ADJUST];
7088 int parts;
7089 int current_part_nr;
7091 plr->attribute_block.length);
7092 char *quoted_at = strchr(quoted, ':');
7093 size_t bytes_left = strlen(quoted);
7094 size_t bytes_at_colon = 1 + (quoted_at - quoted);
7096
7098 "player%d.attribute_v2_block_length", plrno);
7100 "player%d.attribute_v2_block_length_quoted", plrno);
7101
7102 /* Try to wring some compression efficiencies out of the "quoted" format.
7103 * The first line has a variable length decimal, mis-aligning triples.
7104 */
7105 if ((bytes_left - bytes_adjust) > PART_SIZE) {
7106 /* first line can be longer */
7107 parts = 1 + (bytes_left - bytes_adjust - 1) / PART_SIZE;
7108 } else {
7109 parts = 1;
7110 }
7111
7113 "player%d.attribute_v2_block_parts", plrno);
7114
7115 if (parts > 1) {
7117
7118 /* first line can be longer */
7120 part[size_of_current_part] = '\0';
7122 "player%d.attribute_v2_block_data.part%d",
7123 plrno, 0);
7126 current_part_nr = 1;
7127 } else {
7128 quoted_at = quoted;
7129 current_part_nr = 0;
7130 }
7131
7134
7136 part[size_of_current_part] = '\0';
7138 "player%d.attribute_v2_block_data.part%d",
7139 plrno,
7143 }
7144 fc_assert(bytes_left == 0);
7145 free(quoted);
7146 }
7147#undef PART_ADJUST
7148#undef PART_SIZE
7149}
7150
7151/************************************************************************/
7155 struct player *plr)
7156{
7157 int plrno = player_number(plr);
7158 int total_ncities
7160 "player%d.dc_total", plrno);
7161 int i;
7162 bool someone_alive = FALSE;
7163
7164 /* Check status and return if not OK (sg_success FALSE). */
7165 sg_check_ret();
7166
7169 if (pteam_member->is_alive) {
7171 break;
7172 }
7174
7175 if (!someone_alive) {
7176 /* Reveal all for completely dead teams. */
7178 }
7179 }
7180
7181 if (-1 == total_ncities
7182 || !game.info.fogofwar
7184 "game.save_private_map")) {
7185 /* We have:
7186 * - a dead player;
7187 * - fogged cities are not saved for any reason;
7188 * - a savegame with fog of war turned off;
7189 * - or game.save_private_map is not set to FALSE in the scenario /
7190 * savegame. The players private knowledge is set to be what they could
7191 * see without fog of war. */
7192 whole_map_iterate(&(wld.map), ptile) {
7193 if (map_is_known(ptile, plr)) {
7194 struct city *pcity = tile_city(ptile);
7195
7196 update_player_tile_last_seen(plr, ptile);
7197 update_player_tile_knowledge(plr, ptile);
7198
7199 if (NULL != pcity) {
7200 update_dumb_city(plr, pcity);
7201 }
7202 }
7204
7205 /* Nothing more to do; */
7206 return;
7207 }
7208
7209 /* Load player map (terrain). */
7210 LOAD_MAP_CHAR(ch, ptile,
7211 map_get_player_tile(ptile, plr)->terrain
7212 = char2terrain(ch), loading->file,
7213 "player%d.map_t%04d", plrno);
7214
7215 /* Load player map (extras). */
7216 halfbyte_iterate_extras(j, loading->extra.size) {
7217 LOAD_MAP_CHAR(ch, ptile,
7219 ch, loading->extra.order + 4 * j),
7220 loading->file, "player%d.map_e%02d_%04d", plrno, j);
7222
7223 whole_map_iterate(&(wld.map), ptile) {
7224 struct player_tile *plrtile = map_get_player_tile(ptile, plr);
7225 bool regr_warn = FALSE;
7226
7228 int pres_id = extra_number(pres);
7229
7230 if (BV_ISSET(plrtile->extras, pres_id)) {
7231 if (plrtile->terrain == nullptr) {
7232 if (!regr_warn) {
7233 sg_regr(3030000, "FoW tile (%d, %d) has extras, though it's on unknown.",
7234 TILE_XY(ptile));
7235 regr_warn = TRUE;
7236 }
7237 BV_CLR(plrtile->extras, pres_id);
7238 } else {
7239 plrtile->resource = pres;
7240 if (!terrain_has_resource(plrtile->terrain, pres)) {
7241 BV_CLR(plrtile->extras, pres_id);
7242 }
7243 }
7244 }
7247
7249 /* Load player map (border). */
7250 int x, y;
7251
7252 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
7253 const char *buffer
7254 = secfile_lookup_str(loading->file, "player%d.map_owner%04d",
7255 plrno, y);
7256 const char *buffer2
7257 = secfile_lookup_str(loading->file, "player%d.extras_owner%04d",
7258 plrno, y);
7259 const char *ptr = buffer;
7260 const char *ptr2 = buffer2;
7261
7262 sg_failure_ret(NULL != buffer,
7263 "Savegame corrupt - map line %d not found.", y);
7264 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
7265 char token[TOKEN_SIZE];
7266 char token2[TOKEN_SIZE];
7267 int number;
7268 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
7269
7270 scanin(&ptr, ",", token, sizeof(token));
7271 sg_failure_ret('\0' != token[0],
7272 "Savegame corrupt - map size not correct.");
7273 if (strcmp(token, "-") == 0) {
7274 map_get_player_tile(ptile, plr)->owner = NULL;
7275 } else {
7276 sg_failure_ret(str_to_int(token, &number),
7277 "Savegame corrupt - got tile owner=%s in (%d, %d).",
7278 token, x, y);
7279 map_get_player_tile(ptile, plr)->owner = player_by_number(number);
7280 }
7281
7282 scanin(&ptr2, ",", token2, sizeof(token2));
7283 sg_failure_ret('\0' != token2[0],
7284 "Savegame corrupt - map size not correct.");
7285 if (strcmp(token2, "-") == 0) {
7286 map_get_player_tile(ptile, plr)->extras_owner = NULL;
7287 } else {
7289 "Savegame corrupt - got extras owner=%s in (%d, %d).",
7290 token, x, y);
7291 map_get_player_tile(ptile, plr)->extras_owner = player_by_number(number);
7292 }
7293 }
7294 }
7295 }
7296
7297 /* Load player map (update time). */
7298 for (i = 0; i < 4; i++) {
7299 /* put 4-bit segments of 16-bit "updated" field */
7300 if (i == 0) {
7301 LOAD_MAP_CHAR(ch, ptile,
7302 map_get_player_tile(ptile, plr)->last_updated
7303 = ascii_hex2bin(ch, i),
7304 loading->file, "player%d.map_u%02d_%04d", plrno, i);
7305 } else {
7306 LOAD_MAP_CHAR(ch, ptile,
7307 map_get_player_tile(ptile, plr)->last_updated
7308 |= ascii_hex2bin(ch, i),
7309 loading->file, "player%d.map_u%02d_%04d", plrno, i);
7310 }
7311 }
7312
7313 /* Load player map known cities. */
7314 for (i = 0; i < total_ncities; i++) {
7315 struct vision_site *pdcity;
7316 char buf[32];
7317 fc_snprintf(buf, sizeof(buf), "player%d.dc%d", plrno, i);
7318
7322 pdcity);
7324 } else {
7325 /* Error loading the data. */
7326 log_sg("Skipping seen city %d for player %d.", i, plrno);
7327 if (pdcity != NULL) {
7329 }
7330 }
7331 }
7332
7333 /* Repair inconsistent player maps. */
7334 whole_map_iterate(&(wld.map), ptile) {
7335 if (map_is_known_and_seen(ptile, plr, V_MAIN)) {
7336 struct city *pcity = tile_city(ptile);
7337
7338 update_player_tile_knowledge(plr, ptile);
7339 reality_check_city(plr, ptile);
7340
7341 if (NULL != pcity) {
7342 update_dumb_city(plr, pcity);
7343 }
7344 } else if (!game.server.foggedborders && map_is_known(ptile, plr)) {
7345 /* Non fogged borders aren't loaded. See hrm Bug #879084 */
7346 struct player_tile *plrtile = map_get_player_tile(ptile, plr);
7347
7348 plrtile->owner = tile_owner(ptile);
7349 }
7351}
7352
7353/************************************************************************/
7357 struct player *plr,
7358 struct vision_site *pdcity,
7359 const char *citystr)
7360{
7361 const char *str;
7362 int i, id, size;
7363 citizens city_size;
7364 int nat_x, nat_y;
7365 const char *stylename;
7366 enum capital_type cap;
7367 const char *vname;
7368
7370 citystr),
7371 FALSE, "%s", secfile_error());
7373 citystr),
7374 FALSE, "%s", secfile_error());
7375 pdcity->location = native_pos_to_tile(&(wld.map), nat_x, nat_y);
7376 sg_warn_ret_val(NULL != pdcity->location, FALSE,
7377 "%s invalid tile (%d,%d)", citystr, nat_x, nat_y);
7378
7379 sg_warn_ret_val(secfile_lookup_int(loading->file, &id, "%s.owner",
7380 citystr),
7381 FALSE, "%s", secfile_error());
7382 pdcity->owner = player_by_number(id);
7383 sg_warn_ret_val(NULL != pdcity->owner, FALSE,
7384 "%s has invalid owner (%d); skipping.", citystr, id);
7385
7386 sg_warn_ret_val(secfile_lookup_int(loading->file, &id, "%s.original",
7387 citystr),
7388 FALSE, "%s", secfile_error());
7389 if (id >= 0) {
7390 pdcity->original = player_by_number(id);
7391 sg_warn_ret_val(NULL != pdcity->original, FALSE,
7392 "%s has invalid original owner (%d); skipping.", citystr, id);
7393 } else {
7394 pdcity->original = nullptr;
7395 }
7396
7398 "%s.id", citystr),
7399 FALSE, "%s", secfile_error());
7401 "%s has invalid id (%d); skipping.", citystr, id);
7402
7404 "%s.size", citystr),
7405 FALSE, "%s", secfile_error());
7406 city_size = (citizens)size; /* set the correct type */
7407 sg_warn_ret_val(size == (int)city_size, FALSE,
7408 "Invalid city size: %d; set to %d.", size, city_size);
7409 vision_site_size_set(pdcity, city_size);
7410
7411 /* Initialise list of improvements */
7412 BV_CLR_ALL(pdcity->improvements);
7413 str = secfile_lookup_str(loading->file, "%s.improvements", citystr);
7415 sg_warn_ret_val(strlen(str) == loading->improvement.size, FALSE,
7416 "Invalid length of '%s.improvements' ("
7417 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
7418 citystr, strlen(str), loading->improvement.size);
7419 for (i = 0; i < loading->improvement.size; i++) {
7420 sg_warn_ret_val(str[i] == '1' || str[i] == '0', FALSE,
7421 "Undefined value '%c' within '%s.improvements'.",
7422 str[i], citystr)
7423
7424 if (str[i] == '1') {
7425 struct impr_type *pimprove =
7426 improvement_by_rule_name(loading->improvement.order[i]);
7427 if (pimprove) {
7428 BV_SET(pdcity->improvements, improvement_index(pimprove));
7429 }
7430 }
7431 }
7432
7434 "%s.name", citystr);
7435
7436 if (vname != NULL) {
7437 pdcity->name = fc_strdup(vname);
7438 }
7439
7441 "%s.occupied", citystr);
7443 "%s.walls", citystr);
7445 "%s.happy", citystr);
7447 "%s.unhappy", citystr);
7449 "%s.style", citystr);
7450 if (stylename != NULL) {
7452 } else {
7453 pdcity->style = 0;
7454 }
7455 if (pdcity->style < 0) {
7456 pdcity->style = 0;
7457 }
7458
7459 pdcity->city_image = secfile_lookup_int_default(loading->file, -100,
7460 "%s.city_image", citystr);
7461
7463 "%s.capital", citystr),
7465
7467 pdcity->capital = cap;
7468 } else {
7469 pdcity->capital = CAPITAL_NOT;
7470 }
7471
7472 return TRUE;
7473}
7474
7475/************************************************************************/
7479 struct player *plr)
7480{
7481 int i, plrno = player_number(plr);
7482
7483 /* Check status and return if not OK (sg_success FALSE). */
7484 sg_check_ret();
7485
7487 /* The player can see all, there's no reason to save the private map. */
7488 return;
7489 }
7490
7491 /* Save the map (terrain). */
7492 SAVE_MAP_CHAR(ptile,
7494 saving->file, "player%d.map_t%04d", plrno);
7495
7497 /* Save the map (borders). */
7498 int x, y;
7499
7500 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
7502
7503 line[0] = '\0';
7504 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
7505 char token[TOKEN_SIZE];
7506 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
7507 struct player_tile *plrtile = map_get_player_tile(ptile, plr);
7508
7509 if (plrtile == NULL || plrtile->owner == NULL) {
7510 strcpy(token, "-");
7511 } else {
7512 fc_snprintf(token, sizeof(token), "%d",
7513 player_number(plrtile->owner));
7514 }
7515 strcat(line, token);
7516 if (x < MAP_NATIVE_WIDTH) {
7517 strcat(line, ",");
7518 }
7519 }
7520 secfile_insert_str(saving->file, line, "player%d.map_owner%04d",
7521 plrno, y);
7522 }
7523
7524 for (y = 0; y < MAP_NATIVE_HEIGHT; y++) {
7526
7527 line[0] = '\0';
7528 for (x = 0; x < MAP_NATIVE_WIDTH; x++) {
7529 char token[TOKEN_SIZE];
7530 struct tile *ptile = native_pos_to_tile(&(wld.map), x, y);
7531 struct player_tile *plrtile = map_get_player_tile(ptile, plr);
7532
7533 if (plrtile == NULL || plrtile->extras_owner == NULL) {
7534 strcpy(token, "-");
7535 } else {
7536 fc_snprintf(token, sizeof(token), "%d",
7537 player_number(plrtile->extras_owner));
7538 }
7539 strcat(line, token);
7540 if (x < MAP_NATIVE_WIDTH) {
7541 strcat(line, ",");
7542 }
7543 }
7544 secfile_insert_str(saving->file, line, "player%d.extras_owner%04d",
7545 plrno, y);
7546 }
7547 }
7548
7549 /* Save the map (extras). */
7551 int mod[4];
7552 int l;
7553
7554 for (l = 0; l < 4; l++) {
7555 if (4 * j + 1 > game.control.num_extra_types) {
7556 mod[l] = -1;
7557 } else {
7558 mod[l] = 4 * j + l;
7559 }
7560 }
7561
7562 SAVE_MAP_CHAR(ptile,
7564 map_get_player_tile(ptile, plr)->resource,
7565 mod),
7566 saving->file, "player%d.map_e%02d_%04d", plrno, j);
7568
7569 /* Save the map (update time). */
7570 for (i = 0; i < 4; i++) {
7571 /* put 4-bit segments of 16-bit "updated" field */
7572 SAVE_MAP_CHAR(ptile,
7574 map_get_player_tile(ptile, plr)->last_updated, i),
7575 saving->file, "player%d.map_u%02d_%04d", plrno, i);
7576 }
7577
7578 /* Save known cities. */
7579 i = 0;
7580 whole_map_iterate(&(wld.map), ptile) {
7581 struct vision_site *pdcity = map_get_player_city(ptile, plr);
7582 char impr_buf[B_LAST + 1];
7583 char buf[32];
7584
7585 fc_snprintf(buf, sizeof(buf), "player%d.dc%d", plrno, i);
7586
7587 if (NULL != pdcity && plr != vision_site_owner(pdcity)) {
7588 int nat_x, nat_y;
7589
7591 secfile_insert_int(saving->file, nat_y, "%s.y", buf);
7592 secfile_insert_int(saving->file, nat_x, "%s.x", buf);
7593
7594 secfile_insert_int(saving->file, pdcity->identity, "%s.id", buf);
7596 "%s.owner", buf);
7597 if (pdcity->original != nullptr) {
7599 "%s.original", buf);
7600 } else {
7601 secfile_insert_int(saving->file, -1, "%s.original", buf);
7602 }
7603
7605 "%s.size", buf);
7606 secfile_insert_bool(saving->file, pdcity->occupied,
7607 "%s.occupied", buf);
7608 secfile_insert_bool(saving->file, pdcity->walls, "%s.walls", buf);
7609 secfile_insert_bool(saving->file, pdcity->happy, "%s.happy", buf);
7610 secfile_insert_bool(saving->file, pdcity->unhappy, "%s.unhappy", buf);
7612 "%s.style", buf);
7613 secfile_insert_int(saving->file, pdcity->city_image, "%s.city_image", buf);
7615 "%s.capital", buf);
7616
7617 /* Save improvement list as bitvector. Note that improvement order
7618 * is saved in savefile.improvement.order. */
7619 improvement_iterate(pimprove) {
7620 impr_buf[improvement_index(pimprove)]
7621 = BV_ISSET(pdcity->improvements, improvement_index(pimprove))
7622 ? '1' : '0';
7624 impr_buf[improvement_count()] = '\0';
7626 "Invalid size of the improvement vector (%s.improvements: "
7627 SIZE_T_PRINTF " < " SIZE_T_PRINTF" ).",
7628 buf, strlen(impr_buf), sizeof(impr_buf));
7629 secfile_insert_str(saving->file, impr_buf, "%s.improvements", buf);
7630 if (pdcity->name != NULL) {
7631 secfile_insert_str(saving->file, pdcity->name, "%s.name", buf);
7632 }
7633
7634 i++;
7635 }
7637
7638 secfile_insert_int(saving->file, i, "player%d.dc_total", plrno);
7639}
7640
7641/* =======================================================================
7642 * Load / save the researches.
7643 * ======================================================================= */
7644
7645/************************************************************************/
7649{
7650 struct research *presearch;
7651 int count;
7652 int number;
7653 const char *str;
7654 int i, j;
7655 int *vlist_research;
7656
7658 /* Check status and return if not OK (sg_success FALSE). */
7659 sg_check_ret();
7660
7661 /* Initialize all researches. */
7665
7666 /* May be unsaved (e.g. scenario case). */
7667 count = secfile_lookup_int_default(loading->file, 0, "research.count");
7668 for (i = 0; i < count; i++) {
7670 "research.r%d.number", i),
7671 "%s", secfile_error());
7672 presearch = research_by_number(number);
7674 "Invalid research number %d in 'research.r%d.number'",
7675 number, i);
7676
7677 presearch->tech_goal = technology_load(loading->file,
7678 "research.r%d.goal", i);
7680 &presearch->future_tech,
7681 "research.r%d.futuretech", i),
7682 "%s", secfile_error());
7684 &presearch->bulbs_researched,
7685 "research.r%d.bulbs", i),
7686 "%s", secfile_error());
7688 &presearch->bulbs_researching_saved,
7689 "research.r%d.bulbs_before", i),
7690 "%s", secfile_error());
7691 presearch->researching_saved = technology_load(loading->file,
7692 "research.r%d.saved", i);
7693 presearch->researching = technology_load(loading->file,
7694 "research.r%d.now", i);
7696 &presearch->free_bulbs,
7697 "research.r%d.free_bulbs", i),
7698 "%s", secfile_error());
7699
7700 str = secfile_lookup_str(loading->file, "research.r%d.done", i);
7701 sg_failure_ret(str != NULL, "%s", secfile_error());
7702 sg_failure_ret(strlen(str) == loading->technology.size,
7703 "Invalid length of 'research.r%d.done' ("
7704 SIZE_T_PRINTF " ~= " SIZE_T_PRINTF ").",
7705 i, strlen(str), loading->technology.size);
7706 for (j = 0; j < loading->technology.size; j++) {
7707 sg_failure_ret(str[j] == '1' || str[j] == '0',
7708 "Undefined value '%c' within 'research.r%d.done'.",
7709 str[j], i);
7710
7711 if (str[j] == '1') {
7712 struct advance *padvance =
7713 advance_by_rule_name(loading->technology.order[j]);
7714
7715 if (padvance) {
7717 TECH_KNOWN);
7718 }
7719 }
7720 }
7721
7723 size_t count_res;
7724 int tn;
7725
7727 "research.r%d.vbs", i);
7728
7729 for (tn = 0; tn < count_res; tn++) {
7730 struct advance *padvance = advance_by_rule_name(loading->technology.order[tn]);
7731
7732 if (padvance != NULL) {
7733 presearch->inventions[advance_index(padvance)].bulbs_researched_saved
7734 = vlist_research[tn];
7735 }
7736 }
7737 }
7738 }
7739
7740 /* In case of tech_leakage, we can update research only after all the
7741 * researches have been loaded */
7745}
7746
7747/************************************************************************/
7751{
7752 char invs[A_LAST];
7753 int i = 0;
7754 int *vlist_research;
7755
7757 /* Check status and return if not OK (sg_success FALSE). */
7758 sg_check_ret();
7759
7760 if (saving->save_players) {
7763 "research.r%d.number", i);
7764 technology_save(saving->file, "research.r%d.goal",
7765 i, presearch->tech_goal);
7766 secfile_insert_int(saving->file, presearch->future_tech,
7767 "research.r%d.futuretech", i);
7768 secfile_insert_int(saving->file, presearch->bulbs_researching_saved,
7769 "research.r%d.bulbs_before", i);
7773 vlist_research[j] = presearch->inventions[j].bulbs_researched_saved;
7777 "research.r%d.vbs", i);
7778 if (vlist_research) {
7780 }
7781 }
7782 technology_save(saving->file, "research.r%d.saved",
7783 i, presearch->researching_saved);
7784 secfile_insert_int(saving->file, presearch->bulbs_researched,
7785 "research.r%d.bulbs", i);
7786 technology_save(saving->file, "research.r%d.now",
7787 i, presearch->researching);
7788 secfile_insert_int(saving->file, presearch->free_bulbs,
7789 "research.r%d.free_bulbs", i);
7790 /* Save technology lists as bytevector. Note that technology order is
7791 * saved in savefile.technology.order */
7792 advance_index_iterate(A_NONE, tech_id) {
7793 invs[tech_id] = (valid_advance_by_number(tech_id) != NULL
7795 == TECH_KNOWN ? '1' : '0');
7798 secfile_insert_str(saving->file, invs, "research.r%d.done", i);
7799 i++;
7801 secfile_insert_int(saving->file, i, "research.count");
7802 }
7803}
7804
7805/* =======================================================================
7806 * Load / save the event cache. Should be the last thing to do.
7807 * ======================================================================= */
7808
7809/************************************************************************/
7813{
7814 /* Check status and return if not OK (sg_success FALSE). */
7815 sg_check_ret();
7816
7817 event_cache_load(loading->file, "event_cache");
7818}
7819
7820/************************************************************************/
7824{
7825 /* Check status and return if not OK (sg_success FALSE). */
7826 sg_check_ret();
7827
7828 if (saving->scenario) {
7829 /* Do _not_ save events in a scenario. */
7830 return;
7831 }
7832
7833 event_cache_save(saving->file, "event_cache");
7834}
7835
7836/* =======================================================================
7837 * Load / save the open treaties
7838 * ======================================================================= */
7839
7840/************************************************************************/
7844{
7845 int tidx;
7846 const char *plr0;
7847
7848 /* Check status and return if not OK (sg_success FALSE). */
7849 sg_check_ret();
7850
7851 for (tidx = 0; (plr0 = secfile_lookup_str_default(loading->file, NULL,
7852 "treaty%d.plr0", tidx)) != NULL ;
7853 tidx++) {
7854 const char *plr1;
7855 const char *ct;
7856 int cidx;
7857 struct player *p0, *p1;
7858
7859 plr1 = secfile_lookup_str(loading->file, "treaty%d.plr1", tidx);
7860
7861 p0 = player_by_name(plr0);
7862 p1 = player_by_name(plr1);
7863
7864 if (p0 == NULL || p1 == NULL) {
7865 log_error("Treaty between unknown players %s and %s", plr0, plr1);
7866 } else {
7867 struct treaty *ptreaty = fc_malloc(sizeof(*ptreaty));
7868
7871
7872 for (cidx = 0; (ct = secfile_lookup_str_default(loading->file, NULL,
7873 "treaty%d.clause%d.type",
7874 tidx, cidx)) != NULL ;
7875 cidx++ ) {
7877 const char *plrx;
7878
7879 if (!clause_type_is_valid(type)) {
7880 log_error("Invalid clause type \"%s\"", ct);
7881 } else {
7882 struct player *pgiver = NULL;
7883
7884 plrx = secfile_lookup_str(loading->file, "treaty%d.clause%d.from",
7885 tidx, cidx);
7886
7887 if (!fc_strcasecmp(plrx, plr0)) {
7888 pgiver = p0;
7889 } else if (!fc_strcasecmp(plrx, plr1)) {
7890 pgiver = p1;
7891 } else {
7892 log_error("Clause giver %s is not participant of the treaty"
7893 "between %s and %s", plrx, plr0, plr1);
7894 }
7895
7896 if (pgiver != NULL) {
7897 int value;
7898
7899 value = secfile_lookup_int_default(loading->file, 0,
7900 "treaty%d.clause%d.value",
7901 tidx, cidx);
7902
7903 add_clause(ptreaty, pgiver, type, value, NULL);
7904 }
7905 }
7906 }
7907
7908 /* These must be after clauses have been added so that acceptance
7909 * does not get cleared by what seems like changes to the treaty. */
7911 "treaty%d.accept0", tidx);
7913 "treaty%d.accept1", tidx);
7914 }
7915 }
7916}
7917
7918typedef struct {
7919 int tidx;
7922
7923/************************************************************************/
7926static void treaty_save(struct treaty *ptr, void *data_in)
7927{
7928 char tpath[512];
7929 int cidx = 0;
7931
7932 fc_snprintf(tpath, sizeof(tpath), "treaty%d", data->tidx++);
7933
7934 secfile_insert_str(data->file, player_name(ptr->plr0), "%s.plr0", tpath);
7935 secfile_insert_str(data->file, player_name(ptr->plr1), "%s.plr1", tpath);
7936 secfile_insert_bool(data->file, ptr->accept0, "%s.accept0", tpath);
7937 secfile_insert_bool(data->file, ptr->accept1, "%s.accept1", tpath);
7938
7940 char cpath[512];
7941
7942 fc_snprintf(cpath, sizeof(cpath), "%s.clause%d", tpath, cidx++);
7943
7944 secfile_insert_str(data->file, clause_type_name(pclaus->type), "%s.type", cpath);
7945 secfile_insert_str(data->file, player_name(pclaus->from), "%s.from", cpath);
7946 secfile_insert_int(data->file, pclaus->value, "%s.value", cpath);
7948}
7949
7950/************************************************************************/
7954{
7955 treaty_cb_data data = { .tidx = 0, .file = saving->file };
7956
7958}
7959
7960/* =======================================================================
7961 * Load / save the history report
7962 * ======================================================================= */
7963
7964/************************************************************************/
7968{
7970 int turn;
7971
7972 /* Check status and return if not OK (sg_success FALSE). */
7973 sg_check_ret();
7974
7975 turn = secfile_lookup_int_default(loading->file, -2, "history.turn");
7976
7977 if (turn != -2) {
7978 hist->turn = turn;
7979 }
7980
7981 if (turn + 1 >= game.info.turn) {
7982 const char *str;
7983
7984 str = secfile_lookup_str(loading->file, "history.title");
7985 sg_failure_ret(str != NULL, "%s", secfile_error());
7986 sz_strlcpy(hist->title, str);
7987 str = secfile_lookup_str(loading->file, "history.body");
7988 sg_failure_ret(str != NULL, "%s", secfile_error());
7989 sz_strlcpy(hist->body, str);
7990 }
7991}
7992
7993/************************************************************************/
7996static void sg_save_history(struct savedata *saving)
7997{
7999
8000 secfile_insert_int(saving->file, hist->turn, "history.turn");
8001
8002 if (hist->turn + 1 >= game.info.turn) {
8003 secfile_insert_str(saving->file, hist->title, "history.title");
8004 secfile_insert_str(saving->file, hist->body, "history.body");
8005 }
8006}
8007
8008/* =======================================================================
8009 * Load / save the mapimg definitions.
8010 * ======================================================================= */
8011
8012/************************************************************************/
8015static void sg_load_mapimg(struct loaddata *loading)
8016{
8017 int mapdef_count, i;
8018
8019 /* Check status and return if not OK (sg_success FALSE). */
8020 sg_check_ret();
8021
8022 /* Clear all defined map images. */
8023 while (mapimg_count() > 0) {
8024 mapimg_delete(0);
8025 }
8026
8028 "mapimg.count");
8029 log_verbose("Saved map image definitions: %d.", mapdef_count);
8030
8031 if (0 >= mapdef_count) {
8032 return;
8033 }
8034
8035 for (i = 0; i < mapdef_count; i++) {
8036 const char *p;
8037
8038 p = secfile_lookup_str(loading->file, "mapimg.mapdef%d", i);
8039 if (NULL == p) {
8040 log_verbose("[Mapimg %4d] Missing definition.", i);
8041 continue;
8042 }
8043
8044 if (!mapimg_define(p, FALSE)) {
8045 log_error("Invalid map image definition %4d: %s.", i, p);
8046 }
8047
8048 log_verbose("Mapimg %4d loaded.", i);
8049 }
8050}
8051
8052/************************************************************************/
8055static void sg_save_mapimg(struct savedata *saving)
8056{
8057 /* Check status and return if not OK (sg_success FALSE). */
8058 sg_check_ret();
8059
8060 secfile_insert_int(saving->file, mapimg_count(), "mapimg.count");
8061 if (mapimg_count() > 0) {
8062 int i;
8063
8064 for (i = 0; i < mapimg_count(); i++) {
8065 char buf[MAX_LEN_MAPDEF];
8066
8067 mapimg_id2str(i, buf, sizeof(buf));
8068 secfile_insert_str(saving->file, buf, "mapimg.mapdef%d", i);
8069 }
8070 }
8071}
8072
8073/* =======================================================================
8074 * Sanity checks for loading / saving a game.
8075 * ======================================================================= */
8076
8077/************************************************************************/
8081{
8082 int players;
8083
8084 /* Check status and return if not OK (sg_success FALSE). */
8085 sg_check_ret();
8086
8087 if (game.info.is_new_game) {
8088 /* Nothing to do for new games (or not started scenarios). */
8089 return;
8090 }
8091
8092 /* Old savegames may have maxplayers lower than current player count,
8093 * fix. */
8094 players = normal_player_count();
8095 if (game.server.max_players < players) {
8096 log_verbose("Max players lower than current players, fixing");
8097 game.server.max_players = players;
8098 }
8099
8100 /* Fix ferrying sanity */
8101 players_iterate(pplayer) {
8102 unit_list_iterate_safe(pplayer->units, punit) {
8105 log_sg("Removing %s unferried %s in %s at (%d, %d)",
8111 }
8114
8115 /* Fix stacking issues. We don't rely on the savegame preserving
8116 * alliance invariants (old savegames often did not) so if there are any
8117 * unallied units on the same tile we just bounce them. */
8118 players_iterate(pplayer) {
8120 resolve_unit_stacks(pplayer, aplayer, TRUE);
8123
8124 /* Recalculate the potential buildings for each city. Has caused some
8125 * problems with game random state.
8126 * This also changes the game state if you save the game directly after
8127 * loading it and compare the results. */
8128 players_iterate(pplayer) {
8129 /* Building advisor needs data phase open in order to work */
8130 adv_data_phase_init(pplayer, FALSE);
8131 building_advisor(pplayer);
8132 /* Close data phase again so it can be opened again when game starts. */
8133 adv_data_phase_done(pplayer);
8135
8136 /* Prevent a buggy or intentionally crafted save game from crashing
8137 * Freeciv. See hrm Bug #887748 */
8138 players_iterate(pplayer) {
8139 city_list_iterate(pplayer->cities, pcity) {
8140 worker_task_list_iterate(pcity->task_reqs, ptask) {
8141 if (!worker_task_is_sane(ptask)) {
8142 log_error("[city id: %d] Bad worker task %d.",
8143 pcity->id, ptask->act);
8144 worker_task_list_remove(pcity->task_reqs, ptask);
8145 free(ptask);
8146 ptask = NULL;
8147 }
8151
8152 /* Check worked tiles map */
8153#ifdef FREECIV_DEBUG
8154 if (loading->worked_tiles != NULL) {
8155 /* check the entire map for unused worked tiles */
8156 whole_map_iterate(&(wld.map), ptile) {
8157 if (loading->worked_tiles[ptile->index] != -1) {
8158 log_error("[city id: %d] Unused worked tile at (%d, %d).",
8159 loading->worked_tiles[ptile->index], TILE_XY(ptile));
8160 }
8162 }
8163#endif /* FREECIV_DEBUG */
8164
8165 /* Check researching technologies and goals. */
8167 if (presearch->researching != A_UNSET
8168 && !is_future_tech(presearch->researching)
8169 && (valid_advance_by_number(presearch->researching) == NULL
8171 != TECH_PREREQS_KNOWN))) {
8172 log_sg(_("%s had invalid researching technology."),
8174 presearch->researching = A_UNSET;
8175 }
8176 if (presearch->tech_goal != A_UNSET
8177 && !is_future_tech(presearch->tech_goal)
8178 && (valid_advance_by_number(presearch->tech_goal) == NULL
8181 == TECH_KNOWN))) {
8182 log_sg(_("%s had invalid technology goal."),
8184 presearch->tech_goal = A_UNSET;
8185 }
8186
8189
8190 /* Check if some player has more than one of some UTYF_UNIQUE unit type */
8191 players_iterate(pplayer) {
8192 int unique_count[U_LAST];
8193
8194 memset(unique_count, 0, sizeof(unique_count));
8195
8196 unit_list_iterate(pplayer->units, punit) {
8199
8202 log_sg(_("%s has multiple units of type %s though it should be possible "
8203 "to have only one."),
8205 }
8208
8209 players_iterate(pplayer) {
8210 unit_list_iterate_safe(pplayer->units, punit) {
8211 if (punit->has_orders
8213 punit->orders.list)) {
8214 log_sg("Invalid unit orders for unit %d.", punit->id);
8216 }
8219
8220 /* Check max rates (rules may have changed since saving) */
8221 players_iterate(pplayer) {
8224
8225 if (0 == strlen(server.game_identifier)
8226 || !is_base64url(server.game_identifier)) {
8227 /* This uses fc_rand(), so random state has to be initialized before. */
8228 randomize_base64url_string(server.game_identifier,
8229 sizeof(server.game_identifier));
8230 }
8231
8232 /* Restore game random state, just in case various initialization code
8233 * inexplicably altered the previously existing state. */
8234 if (!game.info.is_new_game) {
8235 fc_rand_set_state(loading->rstate);
8236 }
8237
8238 /* At the end do the default sanity checks. */
8239 sanity_check();
8240}
8241
8242/************************************************************************/
8246{
8247 /* Check status and return if not OK (sg_success FALSE). */
8248 sg_check_ret();
8249}
bool achievement_player_has(const struct achievement *pach, const struct player *pplayer)
const char * achievement_rule_name(struct achievement *pach)
struct achievement * achievement_by_rule_name(const char *name)
#define achievements_iterate_end
#define achievements_iterate(_ach_)
static struct action * actions[MAX_NUM_ACTIONS]
Definition actions.c:57
const char * action_id_name_translation(action_id act_id)
Definition actions.c:1271
struct action * action_by_rule_name(const char *name)
Definition actions.c:1100
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1260
bool action_id_exists(const action_id act_id)
Definition actions.c:1089
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:421
#define NUM_ACTIONS
Definition actions.h:63
#define action_id_get_activity(act_id)
Definition actions.h:466
#define ACTION_NONE
Definition actions.h:59
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
int ai_type_get_count(void)
Definition ai.c:327
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
#define ai_type_iterate_end
Definition ai.h:372
#define ai_type_iterate(NAME_ai)
Definition ai.h:365
void ai_traits_init(struct player *pplayer)
Definition aitraits.c:33
#define str
Definition astring.c:76
int dbv_bits(struct dbv *pdbv)
Definition bitvector.c:108
void dbv_set(struct dbv *pdbv, int bit)
Definition bitvector.c:142
bool dbv_isset(const struct dbv *pdbv, int bit)
Definition bitvector.c:120
void dbv_clr_all(struct dbv *pdbv)
Definition bitvector.c:174
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
#define BV_CLR(bv, bit)
Definition bitvector.h:94
bool has_capabilities(const char *us, const char *them)
Definition capability.c:88
citizens citizens_nation_get(const struct city *pcity, const struct player_slot *pslot)
Definition citizens.c:74
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
const char * city_style_rule_name(const int style)
Definition city.c:1765
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3470
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:3397
void destroy_city_virtual(struct city *pcity)
Definition city.c:3556
int city_style_by_rule_name(const char *s)
Definition city.c:1738
#define cities_iterate_end
Definition city.h:517
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define cities_iterate(pcity)
Definition city.h:512
#define CITY_MAP_MAX_RADIUS_SQ
Definition city.h:84
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
#define output_type_iterate(output)
Definition city.h:846
#define FREE_WORKED_TILES
Definition city.h:883
#define MAX_CITY_SIZE
Definition city.h:104
#define city_list_iterate_end
Definition city.h:510
#define I_NEVER
Definition city.h:245
#define city_tile_iterate(_nmap, _radius_sq, _city_tile, _tile)
Definition city.h:228
#define city_tile_iterate_end
Definition city.h:236
#define output_type_iterate_end
Definition city.h:852
bool update_dumb_city(struct player *pplayer, struct city *pcity)
Definition citytools.c:2781
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:2852
void city_refresh_vision(struct city *pcity)
Definition citytools.c:3454
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
struct counter * counter_by_index(int index, enum counter_target target)
Definition counters.c:183
int counter_index(const struct counter *pcount)
Definition counters.c:174
struct counter * counter_by_rule_name(const char *name)
Definition counters.c:115
const char * counter_rule_name(struct counter *pcount)
Definition counters.c:165
int counters_get_city_counters_count(void)
Definition counters.c: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 * 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
void treaties_iterate(treaty_cb cb, void *data)
Definition diptreaty.c:396
#define clause_list_iterate_end
Definition diptreaty.h:73
#define clause_list_iterate(clauselist, pclause)
Definition diptreaty.h:71
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 * extra_by_number(int id)
Definition extras.c:183
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 extra_type_iterate(_p)
Definition extras.h:315
#define extra_type_iterate_end
Definition extras.h:321
#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_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:160
#define NO_TARGET
Definition fc_types.h:214
int Tech_type_id
Definition fc_types.h:237
unsigned char citizens
Definition fc_types.h:248
int Specialist_type_id
Definition fc_types.h:235
#define MAX_NUM_PLAYER_SLOTS
Definition fc_types.h:32
@ CTGT_CITY
Definition fc_types.h:127
#define MAX_LEN_NAME
Definition fc_types.h:67
@ O_LAST
Definition fc_types.h:102
int Multiplier_type_id
Definition fc_types.h:246
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:93
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct unit * game_unit_by_number(int id)
Definition game.c:116
void initialize_globals(void)
Definition game.c:691
struct city * game_city_by_number(int id)
Definition game.c:107
#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_of_player(const struct player *pplayer)
Definition government.c:116
const char * government_rule_name(const struct government *pgovern)
Definition government.c:135
struct government * government_by_rule_name(const char *name)
Definition government.c:57
#define governments_iterate(NAME_pgov)
Definition government.h:127
#define governments_iterate_end
Definition government.h:130
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
struct impr_type * improvement_by_number(const Impr_type_id id)
bool great_wonder_is_destroyed(const struct impr_type *pimprove)
bool wonder_is_lost(const struct player *pplayer, const struct impr_type *pimprove)
const char * improvement_rule_name(const struct impr_type *pimprove)
Impr_type_id improvement_index(const struct impr_type *pimprove)
bool is_wonder(const struct impr_type *pimprove)
bool is_great_wonder(const struct impr_type *pimprove)
struct impr_type * improvement_by_rule_name(const char *name)
Impr_type_id improvement_count(void)
#define improvement_iterate_end
#define WONDER_DESTROYED
#define improvement_iterate(_p)
#define WONDER_LOST
#define B_LAST
Definition improvement.h:42
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 fc_assert_ret(condition)
Definition log.h:192
#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
struct tile * startpos_tile(const struct startpos *psp)
Definition map.c:1820
bool startpos_allows_all(const struct startpos *psp)
Definition map.c:1843
const struct nation_hash * startpos_raw_nations(const struct startpos *psp)
Definition map.c:1916
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_is_excluding(const struct startpos *psp)
Definition map.c:1903
bool map_is_empty(void)
Definition map.c:148
bool startpos_allow(struct startpos *psp, struct nation_type *pnation)
Definition map.c:1787
#define map_startpos_iterate(NAME_psp)
Definition map.h:136
#define map_startpos_iterate_end
Definition map.h:139
#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
map_generator
Definition map_types.h:46
@ 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
struct vision_site * map_get_player_city(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:1372
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_id2str(int id, char *str, size_t str_len)
Definition mapimg.c:1312
bool mapimg_define(const char *maparg, bool check)
Definition mapimg.c:769
bool mapimg_delete(int id)
Definition mapimg.c:1205
int mapimg_count(void)
Definition mapimg.c:573
#define MAX_LEN_MAPDEF
Definition mapimg.h:65
#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
char * meta_addr_port(void)
Definition meta.c:203
const char * default_meta_patches_string(void)
Definition meta.c:83
const char * get_meta_patches_string(void)
Definition meta.c:107
#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:350
const char * multiplier_rule_name(const struct multiplier *pmul)
Multiplier_type_id multiplier_count(void)
Definition multipliers.c:88
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:458
struct nation_type * nation_by_rule_name(const char *name)
Definition nation.c:121
static struct nation_type * nations
Definition nation.c:46
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
#define nation_hash_iterate(nationhash, pnation)
Definition nation.h:93
#define nation_hash_iterate_end
Definition nation.h:95
#define NO_NATION_SELECTED
Definition nation.h:30
void event_cache_load(struct section_file *file, const char *section)
Definition notify.c:783
void event_cache_save(struct section_file *file, const char *section)
Definition notify.c:903
int parts
Definition packhand.c:133
char * lines
Definition packhand.c:132
int len
Definition packhand.c:128
bool player_slot_is_used(const struct player_slot *pslot)
Definition player.c:450
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1237
struct player * player_by_number(const int player_id)
Definition player.c:852
bool players_on_same_team(const struct player *pplayer1, const struct player *pplayer2)
Definition player.c:1488
int player_count(void)
Definition player.c:819
int player_slot_count(void)
Definition player.c:420
struct player_slot * player_slot_by_number(int player_id)
Definition player.c:465
int player_number(const struct player *pplayer)
Definition player.c:839
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:900
int player_slot_max_used_number(void)
Definition player.c:478
int player_slot_index(const struct player_slot *pslot)
Definition player.c:428
struct player * player_by_name(const char *name)
Definition player.c:886
bool player_has_flag(const struct player *pplayer, enum plr_flag_id flag)
Definition player.c:2004
bool player_has_real_embassy(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:240
struct city * player_city_by_number(const struct player *pplayer, int city_id)
Definition player.c:1211
int player_index(const struct player *pplayer)
Definition player.c:831
bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
Definition player.c:864
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:1417
struct player_slot * slots
Definition player.c:51
bool gives_shared_tiles(const struct player *me, const struct player *them)
Definition player.c:1505
bool gives_shared_vision(const struct player *me, const struct player *them)
Definition player.c:1497
#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
const char * player_delegation_get(const struct player *pplayer)
Definition plrhand.c:3242
void fit_nationset_to_players(void)
Definition plrhand.c:2664
#define shuffled_players_iterate_end
Definition plrhand.h:108
#define shuffled_players_iterate(NAME_pplayer)
Definition plrhand.h:98
bool fc_rand_is_init(void)
Definition rand.c:200
RANDOM_STATE fc_rand_state(void)
Definition rand.c:208
void fc_rand_set_state(RANDOM_STATE state)
Definition rand.c:229
struct section_file * secfile_load(const char *filename, bool allow_duplicates)
Definition registry.c:51
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,...)
bool entry_str_set_gt_marking(struct entry *pentry, bool gt_marking)
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
int * secfile_lookup_int_vec(const struct section_file *secfile, size_t *dim, 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_insert_int(secfile, value, path,...)
#define secfile_insert_enum(secfile, enumerator, specenum_type, path,...)
#define secfile_insert_int_vec(secfile, values, dim, path,...)
#define secfile_lookup_enum_default(secfile, defval, specenum_type, path,...)
#define secfile_insert_str_vec(secfile, strings, dim, path,...)
#define secfile_entry_ignore(_sfile_, _fmt_,...)
#define secfile_insert_str(secfile, string, path,...)
#define secfile_insert_bool(secfile, value, path,...)
#define secfile_replace_str(secfile, string, path,...)
#define secfile_insert_float(secfile, value, path,...)
struct history_report * history_report_get(void)
Definition report.c:1859
const char * universal_rule_name(const struct universal *psource)
const char * universal_type_rule_name(const struct universal *psource)
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:671
const char * research_name_translation(const struct research *presearch)
Definition research.c:158
enum tech_state research_invention_set(struct research *presearch, Tech_type_id tech, enum tech_state value)
Definition research.c:640
struct research * research_by_number(int number)
Definition research.c:119
int research_number(const struct research *presearch)
Definition research.c:109
int recalculate_techs_researched(const struct research *presearch)
Definition research.c:1357
enum tech_state research_invention_state(const struct research *presearch, Tech_type_id tech)
Definition research.c:622
void research_update(struct research *presearch)
Definition research.c:504
#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
void rgbcolor_save(struct section_file *file, const struct rgbcolor *prgbcolor, char *path,...)
Definition rgbcolor.c:121
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:9413
#define sanity_check()
Definition sanitycheck.h:43
#define sanity_check_city(x)
Definition sanitycheck.h:41
int current_compat_ver(void)
Definition savecompat.c:233
char bin2ascii_hex(int value, int halfbyte_wanted)
Definition savecompat.c:243
void sg_load_compat(struct loaddata *loading, enum sgf_version format_class)
Definition savecompat.c:154
int ascii_hex2bin(char ch, int halfbyte)
Definition savecompat.c:255
void sg_load_post_load_compat(struct loaddata *loading, enum sgf_version format_class)
Definition savecompat.c:205
#define sg_check_ret(...)
Definition savecompat.h:150
#define sg_warn(condition, message,...)
Definition savecompat.h:160
#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 sg_regr(fixversion, message,...)
Definition savecompat.h:193
@ SAVEGAME_3
Definition savecompat.h:27
#define log_sg
Definition savecompat.h:146
#define sg_warn_ret_val(condition, _val, message,...)
Definition savecompat.h:171
void savegame3_save(struct section_file *sfile, const char *save_reason, bool scenario)
Definition savegame3.c:431
static void sg_save_history(struct savedata *saving)
Definition savegame3.c:7996
static char sg_extras_get_bv(bv_extras extras, struct extra_type *presource, const int *idx)
Definition savegame3.c:1198
static void sg_save_counters(struct savedata *saving)
Definition savegame3.c:2754
static void sg_save_map_altitude(struct savedata *saving)
Definition savegame3.c:3020
static void unit_ordering_apply(void)
Definition savegame3.c:1079
static void sg_load_players_basic(struct loaddata *loading)
Definition savegame3.c:3712
static struct loaddata * loaddata_new(struct section_file *file)
Definition savegame3.c:584
static void worklist_save(struct section_file *file, const struct worklist *pwl, int max_length, const char *path,...)
Definition savegame3.c:1012
static void sg_load_map_known(struct loaddata *loading)
Definition savegame3.c:3592
static void sg_load_map_owner(struct loaddata *loading)
Definition savegame3.c:3251
bool sg_success
Definition savecompat.c:35
#define ACTIVITY_OLD_FALLOUT_SG3
Definition savegame3.c:152
static char * quote_block(const void *const data, int length)
Definition savegame3.c:897
#define halfbyte_iterate_extras_end
Definition savegame3.c:260
#define halfbyte_iterate_extras(e, num_extras_types)
Definition savegame3.c:255
static void sg_load_map(struct loaddata *loading)
Definition savegame3.c:2813
static enum unit_orders char2order(char order)
Definition savegame3.c:710
static int unquote_block(const char *const quoted_, void *dest, int dest_length)
Definition savegame3.c:918
static void sg_save_map_tiles_extras(struct savedata *saving)
Definition savegame3.c:3083
static void sg_save_map_worked(struct savedata *saving)
Definition savegame3.c:3553
#define ACTIVITY_OLD_POLLUTION_SG3
Definition savegame3.c:151
static void sg_save_players(struct savedata *saving)
Definition savegame3.c:4167
#define LOAD_MAP_CHAR(ch, ptile, SET_XY_CHAR, secfile, secpath,...)
Definition savegame3.c:219
static void treaty_save(struct treaty *ptr, void *data_in)
Definition savegame3.c:7926
static void sg_save_player_cities(struct savedata *saving, struct player *plr)
Definition savegame3.c:5677
static void sg_load_player_city_citizens(struct loaddata *loading, struct player *plr, struct city *pcity, const char *citystr)
Definition savegame3.c:5629
static void sg_load_player_cities(struct loaddata *loading, struct player *plr)
Definition savegame3.c:5044
static void sg_load_map_tiles(struct loaddata *loading)
Definition savegame3.c:2919
static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, struct unit *punit, int orders_max_length, const char *unitstr)
Definition savegame3.c:6155
static char activity2char(int activity)
Definition savegame3.c:823
static struct savedata * savedata_new(struct section_file *file, const char *save_reason, bool scenario)
Definition savegame3.c:679
static void sg_load_savefile(struct loaddata *loading)
Definition savegame3.c:1335
static enum unit_activity char2activity(char activity)
Definition savegame3.c:874
static void unit_ordering_calc(void)
Definition savegame3.c:1050
static void sg_load_settings(struct loaddata *loading)
Definition savegame3.c:2663
static void sg_load_player_units(struct loaddata *loading, struct player *plr)
Definition savegame3.c:6086
static char terrain2char(const struct terrain *pterrain)
Definition savegame3.c:1250
static void sg_save_random(struct savedata *saving)
Definition savegame3.c:2434
static void sg_save_map_startpos(struct savedata *saving)
Definition savegame3.c:3196
static void sg_load_researches(struct loaddata *loading)
Definition savegame3.c:7648
static void sg_load_map_worked(struct loaddata *loading)
Definition savegame3.c:3509
#define SAVE_MAP_CHAR(ptile, GET_XY_CHAR, secfile, secpath,...)
Definition savegame3.c:172
static void sg_save_savefile_options(struct savedata *saving, const char *option)
Definition savegame3.c:2040
static void sg_load_random(struct loaddata *loading)
Definition savegame3.c:2393
static void sg_save_savefile(struct savedata *saving)
Definition savegame3.c:1774
static void sg_load_player_units_transport(struct loaddata *loading, struct player *plr)
Definition savegame3.c:6672
static void sg_load_history(struct loaddata *loading)
Definition savegame3.c:7967
static void sg_save_treaties(struct savedata *saving)
Definition savegame3.c:7953
static void sg_save_map_owner(struct savedata *saving)
Definition savegame3.c:3373
static void sg_save_researches(struct savedata *saving)
Definition savegame3.c:7750
static void savegame3_save_real(struct section_file *file, const char *save_reason, bool scenario)
Definition savegame3.c:527
#define TOKEN_SIZE
Definition savegame3.c:276
static void sg_load_script(struct loaddata *loading)
Definition savegame3.c:2472
static void sg_load_scenario(struct loaddata *loading)
Definition savegame3.c:2498
static void sg_load_game(struct loaddata *loading)
Definition savegame3.c:2087
static void savedata_destroy(struct savedata *saving)
Definition savegame3.c:698
static void sg_load_player_main(struct loaddata *loading, struct player *plr)
Definition savegame3.c:4228
static char order2char(enum unit_orders order)
Definition savegame3.c:737
static void sg_load_treaties(struct loaddata *loading)
Definition savegame3.c:7843
static void sg_save_scenario(struct savedata *saving)
Definition savegame3.c:2586
static void sg_extras_set_bv(bv_extras *extras, char ch, struct extra_type **idx)
Definition savegame3.c:1133
#define PART_SIZE
static void sg_save_player_units(struct savedata *saving, struct player *plr)
Definition savegame3.c:6725
static void sg_save_ruledata(struct savedata *saving)
Definition savegame3.c:2242
static char sg_extras_get_dbv(struct dbv *extras, struct extra_type *presource, const int *idx)
Definition savegame3.c:1165
static Tech_type_id technology_load(struct section_file *file, const char *path, int plrno)
Definition savegame3.c:1262
static void sg_save_player_vision(struct savedata *saving, struct player *plr)
Definition savegame3.c:7478
static enum direction8 char2dir(char dir)
Definition savegame3.c:761
#define ACTIVITY_LAST_SAVEGAME3
Definition savegame3.c:153
static bool sg_load_player_city(struct loaddata *loading, struct player *plr, struct city *pcity, const char *citystr, int wlist_max_length, int routes_max)
Definition savegame3.c:5163
static struct terrain * char2terrain(char ch)
Definition savegame3.c:1227
static void sg_save_sanitycheck(struct savedata *saving)
Definition savegame3.c:8245
static void sg_load_mapimg(struct loaddata *loading)
Definition savegame3.c:8015
static void sg_load_player_attributes(struct loaddata *loading, struct player *plr)
Definition savegame3.c:6991
static void sg_save_player_attributes(struct savedata *saving, struct player *plr)
Definition savegame3.c:7071
static void sg_load_ruledata(struct loaddata *loading)
Definition savegame3.c:2063
static void sg_save_map(struct savedata *saving)
Definition savegame3.c:2874
static void sg_load_player_vision(struct loaddata *loading, struct player *plr)
Definition savegame3.c:7154
static void sg_save_map_tiles(struct savedata *saving)
Definition savegame3.c:2959
static void worklist_load(struct section_file *file, int wlist_max_length, struct worklist *pwl, const char *path,...)
Definition savegame3.c:966
static void sg_save_mapimg(struct savedata *saving)
Definition savegame3.c:8055
static const char savefile_options_default[]
Definition savegame3.c:278
static char dir2char(enum direction8 dir)
Definition savegame3.c:790
static bool sg_load_player_vision_city(struct loaddata *loading, struct player *plr, struct vision_site *pdcity, const char *citystr)
Definition savegame3.c:7356
static void sg_load_counters(struct loaddata *loading)
Definition savegame3.c:2701
static void sg_load_map_startpos(struct loaddata *loading)
Definition savegame3.c:3109
static void loaddata_destroy(struct loaddata *loading)
Definition savegame3.c:623
static void sg_load_players(struct loaddata *loading)
Definition savegame3.c:3972
#define PART_ADJUST
static void sg_load_map_altitude(struct loaddata *loading)
Definition savegame3.c:2987
static void technology_save(struct section_file *file, const char *path, int plrno, Tech_type_id tech)
Definition savegame3.c:1298
static void sg_save_event_cache(struct savedata *saving)
Definition savegame3.c:7823
static void sg_load_map_tiles_extras(struct loaddata *loading)
Definition savegame3.c:3051
static void sg_load_sanitycheck(struct loaddata *loading)
Definition savegame3.c:8080
static void sg_load_event_cache(struct loaddata *loading)
Definition savegame3.c:7812
void savegame3_load(struct section_file *file)
Definition savegame3.c:458
static void sg_extras_set_dbv(struct dbv *extras, char ch, struct extra_type **idx)
Definition savegame3.c:1100
static void sg_save_game(struct savedata *saving)
Definition savegame3.c:2262
static void sg_save_player_main(struct savedata *saving, struct player *plr)
Definition savegame3.c:4718
static void sg_save_script(struct savedata *saving)
Definition savegame3.c:2483
static void sg_save_settings(struct savedata *saving)
Definition savegame3.c:2679
static void sg_save_map_known(struct savedata *saving)
Definition savegame3.c:3649
void script_server_state_save(struct section_file *file)
void script_server_state_load(struct section_file *file)
void settings_game_load(struct section_file *file, const char *section)
Definition settings.c:4958
void settings_game_save(struct section_file *file, const char *section)
Definition settings.c:4870
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
sex_t sex_by_name(const char *name)
Definition sex.c:27
const char * sex_rule_name(sex_t kind)
Definition sex.c:43
@ SEX_FEMALE
Definition sex.h:22
@ SEX_MALE
Definition sex.h:23
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1094
bool str_to_int(const char *str, int *pint)
Definition shared.c:515
const struct strvec * get_scenario_dirs(void)
Definition shared.c:971
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
#define MIN(x, y)
Definition shared.h:55
#define MAX_LEN_PATH
Definition shared.h:32
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:123
struct specialist * specialist_by_number(const Specialist_type_id id)
Definition specialist.c:110
Specialist_type_id specialist_index(const struct specialist *sp)
Definition specialist.c:90
const char * specialist_rule_name(const struct specialist *sp)
Definition specialist.c:157
Specialist_type_id specialist_count(void)
Definition specialist.c:71
bool is_normal_specialist_id(Specialist_type_id sp)
Definition specialist.c:196
#define specialist_type_iterate_end
Definition specialist.h:85
#define specialist_type_iterate(sp)
Definition specialist.h:79
#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:3503
const char * aifill(int amount)
Definition srv_main.c:2511
bool game_was_started(void)
Definition srv_main.c:355
void identity_number_reserve(int id)
Definition srv_main.c:2038
struct server_arguments srvarg
Definition srv_main.c:182
void init_game_seed(void)
Definition srv_main.c:209
void update_nations_with_startpos(void)
Definition srv_main.c:2316
enum server_states server_state(void)
Definition srv_main.c:339
void server_game_free(void)
Definition srv_main.c:3527
int x
Definition rand.h:52
RANDOM_TYPE v[56]
Definition rand.h:51
int k
Definition rand.h:52
bool is_init
Definition rand.h:53
int j
Definition rand.h:52
struct player * first
int wonder_city
Definition advdata.h:55
int val
Definition traits.h:38
int mod
Definition traits.h:39
Definition city.h:318
size_t length
Definition city.h:415
bool multiresearch
Definition game.h:170
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
bool save_private_map
Definition game.h:269
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 additional_phase_seconds
Definition game.h:219
struct section_file * luadata
Definition game.h:254
int scoreturn
Definition game.h:232
randseed seed
Definition game.h:234
int dbid
Definition game.h:255
struct packet_scenario_info scenario
Definition game.h:87
int timeoutint
Definition game.h:210
struct timer * phase_timer
Definition game.h:218
unsigned revealmap
Definition game.h:184
char orig_game_version[MAX_LEN_NAME]
Definition game.h:228
bool save_known
Definition game.h:266
bool foggedborders
Definition game.h:154
struct civ_game::@32::@36::@39 save_options
char * ruleset_capabilities
Definition game.h:86
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
bool save_starts
Definition game.h:268
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
bool allow_disorder
Definition cm.h:44
int factor[O_LAST]
Definition cm.h:47
bool max_growth
Definition cm.h:42
bool allow_specialists
Definition cm.h:45
bool require_happy
Definition cm.h:43
int minimal_surplus[O_LAST]
Definition cm.h:41
int happy_factor
Definition cm.h:48
int changed_to_times
Definition government.h:66
struct section_file * file
Definition savecompat.h:48
bool global_advances[A_LAST]
int great_wonder_owners[B_LAST]
enum ai_level skill_level
enum phase_mode_type phase_mode
char alt_dir[MAX_LEN_NAME]
char version[MAX_LEN_NAME]
char description[MAX_LEN_CONTENT]
char authors[MAX_LEN_PACKET/3]
char datafile[MAX_LEN_NAME]
Definition goto.c:52
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 infra_points
Definition player.h:67
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
short last_updated
Definition maphand.h:44
struct player * owner
Definition maphand.h:34
struct extra_type * resource
Definition maphand.h:32
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
int primary_capital_id
Definition player.h:275
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
int autoselect_weight
Definition player.h:299
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
const struct ai_type * ai
Definition player.h:289
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
struct adv_data * adv
Definition player.h:334
int history
Definition player.h:316
char orig_username[MAX_LEN_NAME]
Definition player.h:347
int last_war_action
Definition player.h:270
struct rgbcolor * rgb
Definition player.h:312
bool unassigned_user
Definition player.h:253
const char * save_reason
Definition savegame3.c:269
struct section_file * file
Definition savegame3.c:265
bool scenario
Definition savegame3.c:270
bool save_players
Definition savegame3.c:273
char secfile_options[512]
Definition savegame3.c:266
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
char identifier
Definition terrain.h:84
Definition tile.h:50
int altitude
Definition tile.h:65
int index
Definition tile.h:51
struct unit_list * units
Definition tile.h:58
int infra_turns
Definition tile.h:62
struct extra_type * placing
Definition tile.h:61
struct tile * claimer
Definition tile.h:64
Definition timing.c:81
enum route_direction dir
Definition traderoutes.h:86
struct section_file * file
Definition savegame3.c:7920
struct player * plr1
Definition diptreaty.h:82
struct clause_list * clauses
Definition diptreaty.h:84
bool accept1
Definition diptreaty.h:83
struct player * plr0
Definition diptreaty.h:82
bool accept0
Definition diptreaty.h:83
enum unit_activity activity
Definition unit.h:95
enum unit_orders order
Definition unit.h:94
int action
Definition unit.h:102
enum direction8 dir
Definition unit.h:104
int target
Definition unit.h:98
int sub_target
Definition unit.h:99
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
enum gen_action action
Definition unit.h:160
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
bool stay
Definition unit.h:208
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 unit_adv * adv
Definition unit.h:239
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 goods_type * carrying
Definition unit.h:189
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
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
int cat_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:986
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
#define sz_strlcat(dest, src)
Definition support.h:196
int team_index(const struct team *pteam)
Definition team.c:383
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
struct advance * advance_by_number(const Tech_type_id atype)
Definition tech.c:107
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
const char * advance_rule_name(const struct advance *padvance)
Definition tech.c:309
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
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 advance_index_iterate_end
Definition tech.h:246
#define A_FIRST
Definition tech.h:44
#define A_NONE
Definition tech.h:43
#define advance_iterate(_p)
Definition tech.h:273
#define A_UNSET
Definition tech.h:48
#define advance_iterate_end
Definition tech.h:274
#define A_UNKNOWN
Definition tech.h:49
#define A_LAST
Definition tech.h:45
#define advance_index_iterate(_start, _index)
Definition tech.h:242
void init_tech(struct research *research, bool update)
Definition techtools.c:1094
struct terrain * terrain_by_rule_name(const char *name)
Definition terrain.c:189
char terrain_identifier(const struct terrain *pterrain)
Definition terrain.c:128
const char * terrain_rule_name(const struct terrain *pterrain)
Definition terrain.c:250
bool terrain_has_resource(const struct terrain *pterrain, const struct extra_type *presource)
Definition terrain.c:258
#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
bool tile_set_label(struct tile *ptile, const char *label)
Definition tile.c:1098
void tile_set_resource(struct tile *ptile, struct extra_type *presource)
Definition tile.c:350
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:107
#define tile_index(_pt_)
Definition tile.h:89
#define tile_worked(_tile)
Definition tile.h:119
#define tile_terrain(_tile)
Definition tile.h:115
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_has_extra(ptile, pextra)
Definition tile.h:152
#define tile_owner(_tile)
Definition tile.h:97
void timer_destroy(struct timer *t)
Definition timing.c:208
void timer_start(struct timer *t)
Definition timing.c:263
void timer_stop(struct timer *t)
Definition timing.c:305
struct timer * timer_new(enum timer_timetype type, enum timer_use use, const char *name)
Definition timing.c:160
double timer_read_seconds(struct timer *t)
Definition timing.c:379
#define TIMER_DEBUG
Definition timing.h:61
@ TIMER_CPU
Definition timing.h:41
int city_num_trade_routes(const struct city *pcity)
struct goods_type * goods_by_rule_name(const char *name)
const char * goods_rule_name(struct goods_type *pgood)
struct goods_type * goods_by_number(Goods_type_id id)
#define trade_routes_iterate_end
#define trade_routes_iterate(c, proute)
void free_unit_orders(struct unit *punit)
Definition unit.c:1833
int unit_upkeep_cost(const struct unit *punit, Output_type_id otype)
Definition unit.c:2994
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2474
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1301
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2545
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:884
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1688
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2753
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:1159
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1793
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1311
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1141
#define unit_tile(_pu)
Definition unit.h:407
#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:406
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:1406
void unit_refresh_vision(struct unit *punit)
Definition unittools.c:5016
void bounce_unit(struct unit *punit, bool verbose)
Definition unittools.c:1230
bool unit_activity_needs_target_from_client(enum unit_activity activity)
Definition unittools.c:1063
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
struct unit_type * unit_type_by_rule_name(const char *name)
Definition unittype.c:1793
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1613
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2657
Unit_type_id utype_index(const struct unit_type *punittype)
Definition unittype.c:93
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1586
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624
#define unit_type_iterate(_p)
Definition unittype.h:863
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:870
const char * freeciv_datafile_version(void)
Definition version.c:186
#define MINOR_VERSION
Definition version_gen.h:7
#define EMERGENCY_VERSION
Definition version_gen.h:9
#define MAJOR_VERSION
Definition version_gen.h:6
#define PATCH_VERSION
Definition version_gen.h:8
void vision_site_size_set(struct vision_site *psite, citizens size)
Definition vision.c:180
citizens vision_site_size_get(const struct vision_site *psite)
Definition vision.c:170
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
#define vision_site_owner(v)
Definition vision.h:129
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 MAX_LEN_WORKLIST
Definition worklist.h:24
#define MAP_NATIVE_WIDTH
#define MAP_INDEX_SIZE
#define MAP_NATIVE_HEIGHT