Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / game.c
blobe2d11f9594b204ccdad7ed9b8f42340466715097
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "fcintl.h"
20 #include "ioz.h"
21 #include "log.h"
22 #include "mem.h"
23 #include "shared.h"
24 #include "support.h"
26 /* aicore */
27 #include "cm.h"
29 /* common */
30 #include "ai.h"
31 #include "achievements.h"
32 #include "actions.h"
33 #include "city.h"
34 #include "connection.h"
35 #include "disaster.h"
36 #include "extras.h"
37 #include "government.h"
38 #include "idex.h"
39 #include "map.h"
40 #include "multipliers.h"
41 #include "nation.h"
42 #include "packets.h"
43 #include "player.h"
44 #include "research.h"
45 #include "spaceship.h"
46 #include "specialist.h"
47 #include "style.h"
48 #include "tech.h"
49 #include "terrain.h"
50 #include "traderoutes.h"
51 #include "unit.h"
52 #include "unitlist.h"
53 #include "victory.h"
55 #include "game.h"
57 struct civ_game game;
58 struct world wld;
60 bool am_i_server = FALSE;
62 static void game_defaults(void);
64 /**************************************************************************
65 Is program type server?
66 **************************************************************************/
67 bool is_server(void)
69 return am_i_server;
72 /**************************************************************************
73 Set program type to server.
74 **************************************************************************/
75 void i_am_server(void)
77 am_i_server = TRUE;
80 /**************************************************************************
81 Set program type to client.
82 **************************************************************************/
83 void i_am_client(void)
85 am_i_server = FALSE;
88 /**************************************************************************
89 Count the # of thousand citizen in a civilisation.
90 **************************************************************************/
91 int civ_population(const struct player *pplayer)
93 int ppl=0;
94 city_list_iterate(pplayer->cities, pcity)
95 ppl+=city_population(pcity);
96 city_list_iterate_end;
97 return ppl;
101 /**************************************************************************
102 Find city with given name from any player.
103 **************************************************************************/
104 struct city *game_city_by_name(const char *name)
106 players_iterate(pplayer) {
107 struct city *pcity = city_list_find_name(pplayer->cities, name);
109 if (pcity) {
110 return pcity;
112 } players_iterate_end;
114 return NULL;
118 /**************************************************************************
119 Often used function to get a city pointer from a city ID.
120 City may be any city in the game. This now always uses fast idex
121 method, instead of looking through all cities of all players.
122 **************************************************************************/
123 struct city *game_city_by_number(int id)
125 return idex_lookup_city(id);
129 /**************************************************************************
130 Find unit out of all units in game: now uses fast idex method,
131 instead of looking through all units of all players.
132 **************************************************************************/
133 struct unit *game_unit_by_number(int id)
135 return idex_lookup_unit(id);
138 /**************************************************************************
139 In the server call wipe_unit(), and never this function directly.
140 **************************************************************************/
141 void game_remove_unit(struct unit *punit)
143 struct city *pcity;
145 /* It's possible that during city transfer homecity/unit owner
146 * information is inconsistent, and client then tries to remove
147 * now unseen unit so that homecity is not in the list of cities
148 * of the player (seemingly) owning the unit.
149 * Thus cannot use player_city_by_number() here, but have to
150 * consider cities of all players. */
151 pcity = game_city_by_number(punit->homecity);
152 if (pcity) {
153 unit_list_remove(pcity->units_supported, punit);
155 log_debug("game_remove_unit()"
156 " at (%d,%d) unit %d, %s %s home (%d,%d) city %d, %s %s",
157 TILE_XY(unit_tile(punit)),
158 punit->id,
159 nation_rule_name(nation_of_unit(punit)),
160 unit_rule_name(punit),
161 TILE_XY(pcity->tile),
162 punit->homecity,
163 nation_rule_name(nation_of_city(pcity)),
164 city_name_get(pcity));
165 } else if (IDENTITY_NUMBER_ZERO == punit->homecity) {
166 log_debug("game_remove_unit() at (%d,%d) unit %d, %s %s home %d",
167 TILE_XY(unit_tile(punit)),
168 punit->id,
169 nation_rule_name(nation_of_unit(punit)),
170 unit_rule_name(punit),
171 punit->homecity);
172 } else {
173 log_error("game_remove_unit() at (%d,%d) unit %d, %s %s home %d invalid",
174 TILE_XY(unit_tile(punit)),
175 punit->id,
176 nation_rule_name(nation_of_unit(punit)),
177 unit_rule_name(punit),
178 punit->homecity);
181 unit_list_remove(unit_tile(punit)->units, punit);
182 unit_list_remove(unit_owner(punit)->units, punit);
184 idex_unregister_unit(punit);
186 if (game.callbacks.unit_deallocate) {
187 (game.callbacks.unit_deallocate)(punit->id);
189 unit_virtual_destroy(punit);
192 /**************************************************************************
193 Remove city from game.
194 **************************************************************************/
195 void game_remove_city(struct city *pcity)
197 struct tile *pcenter = city_tile(pcity);
198 struct player *powner = city_owner(pcity);
200 if (NULL != powner) {
201 /* always unlink before clearing data */
202 city_list_remove(powner->cities, pcity);
205 if (NULL == pcenter) {
206 log_debug("game_remove_city() virtual city %d, %s",
207 pcity->id,
208 city_name_get(pcity));
209 } else {
210 log_debug("game_remove_city() at (%d,%d) city %d, %s %s",
211 TILE_XY(pcenter),
212 pcity->id,
213 nation_rule_name(nation_of_player(powner)),
214 city_name_get(pcity));
216 city_tile_iterate(city_map_radius_sq_get(pcity), pcenter, ptile) {
217 if (tile_worked(ptile) == pcity) {
218 tile_set_worked(ptile, NULL);
220 } city_tile_iterate_end;
223 idex_unregister_city(pcity);
224 destroy_city_virtual(pcity);
227 /****************************************************************************
228 Set default game values.
229 ****************************************************************************/
230 static void game_defaults(void)
232 int i;
234 /* The control packet. */
235 game.control.government_count = 0;
236 game.control.nation_count = 0;
237 game.control.num_base_types = 0;
238 game.control.num_road_types = 0;
239 game.control.num_resource_types = 0;
240 game.control.num_impr_types = 0;
241 game.control.num_specialist_types = 0;
242 game.control.num_tech_types = 0;
243 game.control.num_unit_classes = 0;
244 game.control.num_unit_types = 0;
245 game.control.num_disaster_types = 0;
246 game.control.num_achievement_types = 0;
247 game.control.num_styles = 0;
248 game.control.num_music_styles = 0;
249 game.control.preferred_tileset[0] = '\0';
250 game.control.preferred_soundset[0] = '\0';
251 game.control.preferred_musicset[0] = '\0';
252 game.control.styles_count = 0;
253 game.control.terrain_count = 0;
255 game.ruleset_summary = NULL;
256 game.ruleset_description = NULL;
258 /* The info packet. */
259 game.info.aifill = GAME_DEFAULT_AIFILL;
260 game.info.airlifting_style = GAME_DEFAULT_AIRLIFTINGSTYLE;
261 game.info.angrycitizen = GAME_DEFAULT_ANGRYCITIZEN;
262 game.info.borders = GAME_DEFAULT_BORDERS;
263 game.calendar.calendar_skip_0 = FALSE;
264 game.info.celebratesize = GAME_DEFAULT_CELEBRATESIZE;
265 game.info.citymindist = GAME_DEFAULT_CITYMINDIST;
266 game.info.cooling = 0;
267 game.info.coolinglevel = 0; /* set later */
268 game.info.diplomacy = GAME_DEFAULT_DIPLOMACY;
269 game.info.fogofwar = GAME_DEFAULT_FOGOFWAR;
270 game.info.foodbox = GAME_DEFAULT_FOODBOX;
271 game.info.fulltradesize = GAME_DEFAULT_FULLTRADESIZE;
272 game.info.global_advance_count = 0;
273 for (i = 0; i < A_LAST; i++) {
274 /* game.num_tech_types = 0 here */
275 game.info.global_advances[i] = FALSE;
277 for (i = 0; i < B_LAST; i++) {
278 /* game.num_impr_types = 0 here */
279 game.info.great_wonder_owners[i] = WONDER_NOT_OWNED;
281 game.info.globalwarming = 0;
282 game.info.global_warming = GAME_DEFAULT_GLOBAL_WARMING;
283 game.info.gold = GAME_DEFAULT_GOLD;
284 game.info.revolentype = GAME_DEFAULT_REVOLENTYPE;
285 game.info.default_government_id = G_LAST;
286 game.info.government_during_revolution_id = G_LAST;
287 game.info.happyborders = GAME_DEFAULT_HAPPYBORDERS;
288 game.info.heating = 0;
289 game.info.is_edit_mode = FALSE;
290 game.info.is_new_game = TRUE;
291 game.info.killstack = GAME_DEFAULT_KILLSTACK;
292 game.info.killcitizen = GAME_DEFAULT_KILLCITIZEN;
293 game.calendar.negative_year_label[0] = '\0';
294 game.info.notradesize = GAME_DEFAULT_NOTRADESIZE;
295 game.info.nuclearwinter = 0;
296 game.info.nuclear_winter = GAME_DEFAULT_NUCLEAR_WINTER;
297 game.calendar.positive_year_label[0] = '\0';
298 game.info.rapturedelay = GAME_DEFAULT_RAPTUREDELAY;
299 game.info.disasters = GAME_DEFAULT_DISASTERS;
300 game.info.restrictinfra = GAME_DEFAULT_RESTRICTINFRA;
301 game.info.sciencebox = GAME_DEFAULT_SCIENCEBOX;
302 game.info.shieldbox = GAME_DEFAULT_SHIELDBOX;
303 game.info.skill_level = GAME_DEFAULT_SKILL_LEVEL;
304 game.info.slow_invasions = RS_DEFAULT_SLOW_INVASIONS;
305 game.info.victory_conditions = GAME_DEFAULT_VICTORY_CONDITIONS;
306 game.info.team_pooled_research = GAME_DEFAULT_TEAM_POOLED_RESEARCH;
307 game.info.tech = GAME_DEFAULT_TECHLEVEL;
308 game.info.timeout = GAME_DEFAULT_TIMEOUT;
309 game.info.trademindist = GAME_DEFAULT_TRADEMINDIST;
310 game.info.trading_city = GAME_DEFAULT_TRADING_CITY;
311 game.info.trading_gold = GAME_DEFAULT_TRADING_GOLD;
312 game.info.trading_tech = GAME_DEFAULT_TRADING_TECH;
313 game.info.turn = 0;
314 game.info.warminglevel = 0; /* set later */
315 game.info.year_0_hack = FALSE;
316 game.info.year = GAME_START_YEAR;
318 /* The scenario packets. */
319 game.scenario.is_scenario = FALSE;
320 game.scenario.name[0] = '\0';
321 game.scenario.authors[0] = '\0';
322 game.scenario.players = TRUE;
323 game.scenario.startpos_nations = FALSE;
324 game.scenario.handmade = FALSE;
325 game.scenario.prevent_new_cities = FALSE;
326 game.scenario.lake_flooding = TRUE;
327 game.scenario.have_resources = TRUE;
328 game.scenario.ruleset_locked = TRUE;
329 game.scenario.save_random = FALSE;
330 game.scenario.allow_ai_type_fallback = FALSE;
332 game.scenario_desc.description[0] = '\0';
334 /* Veteran system. */
335 game.veteran = NULL;
337 /* player colors */
338 game.plr_bg_color = NULL;
340 if (is_server()) {
341 /* All settings only used by the server (./server/ and ./ai/ */
342 sz_strlcpy(game.server.allow_take, GAME_DEFAULT_ALLOW_TAKE);
343 game.server.allowed_city_names = GAME_DEFAULT_ALLOWED_CITY_NAMES;
344 game.server.aqueductloss = GAME_DEFAULT_AQUEDUCTLOSS;
345 game.server.auto_ai_toggle = GAME_DEFAULT_AUTO_AI_TOGGLE;
346 game.server.autoattack = GAME_DEFAULT_AUTOATTACK;
347 game.server.barbarianrate = GAME_DEFAULT_BARBARIANRATE;
348 game.server.civilwarsize = GAME_DEFAULT_CIVILWARSIZE;
349 game.server.connectmsg[0] = '\0';
350 game.server.conquercost = GAME_DEFAULT_CONQUERCOST;
351 game.server.contactturns = GAME_DEFAULT_CONTACTTURNS;
352 for (i = 0; i < DEBUG_LAST; i++) {
353 game.server.debug[i] = FALSE;
355 sz_strlcpy(game.server.demography, GAME_DEFAULT_DEMOGRAPHY);
356 game.server.diplchance = GAME_DEFAULT_DIPLCHANCE;
357 game.server.diplbulbcost = GAME_DEFAULT_DIPLBULBCOST;
358 game.server.diplgoldcost = GAME_DEFAULT_DIPLGOLDCOST;
359 game.server.dispersion = GAME_DEFAULT_DISPERSION;
360 game.server.endspaceship = GAME_DEFAULT_END_SPACESHIP;
361 game.server.end_turn = GAME_DEFAULT_END_TURN;
362 game.server.event_cache.chat = GAME_DEFAULT_EVENT_CACHE_CHAT;
363 game.server.event_cache.info = GAME_DEFAULT_EVENT_CACHE_INFO;
364 game.server.event_cache.max_size = GAME_DEFAULT_EVENT_CACHE_MAX_SIZE;
365 game.server.event_cache.turns = GAME_DEFAULT_EVENT_CACHE_TURNS;
366 game.server.foggedborders = GAME_DEFAULT_FOGGEDBORDERS;
367 game.server.fogofwar_old = game.info.fogofwar;
368 game.server.last_updated_year = FALSE;
369 game.server.freecost = GAME_DEFAULT_FREECOST;
370 game.server.homecaughtunits = GAME_DEFAULT_HOMECAUGHTUNITS;
371 game.server.kick_time = GAME_DEFAULT_KICK_TIME;
372 game.server.killunhomed = GAME_DEFAULT_KILLUNHOMED;
373 game.server.maxconnectionsperhost = GAME_DEFAULT_MAXCONNECTIONSPERHOST;
374 game.server.last_ping = 0;
375 game.server.max_players = GAME_DEFAULT_MAX_PLAYERS;
376 game.server.meta_info.user_message[0] = '\0';
377 /* Do not clear meta_info.type here as it's already set to correct value */
378 game.server.mgr_distance = GAME_DEFAULT_MGR_DISTANCE;
379 game.server.mgr_foodneeded = GAME_DEFAULT_MGR_FOODNEEDED;
380 game.server.mgr_nationchance = GAME_DEFAULT_MGR_NATIONCHANCE;
381 game.server.mgr_turninterval = GAME_DEFAULT_MGR_TURNINTERVAL;
382 game.server.mgr_worldchance = GAME_DEFAULT_MGR_WORLDCHANCE;
383 game.server.migration = GAME_DEFAULT_MIGRATION;
384 game.server.trait_dist = GAME_DEFAULT_TRAIT_DIST_MODE;
385 game.server.min_players = GAME_DEFAULT_MIN_PLAYERS;
386 game.server.natural_city_names = GAME_DEFAULT_NATURALCITYNAMES;
387 game.server.plrcolormode = GAME_DEFAULT_PLRCOLORMODE;
388 game.server.netwait = GAME_DEFAULT_NETWAIT;
389 game.server.occupychance = GAME_DEFAULT_OCCUPYCHANCE;
390 game.server.onsetbarbarian = GAME_DEFAULT_ONSETBARBARIAN;
391 game.server.phase_mode_stored = GAME_DEFAULT_PHASE_MODE;
392 game.server.pingtime = GAME_DEFAULT_PINGTIME;
393 game.server.pingtimeout = GAME_DEFAULT_PINGTIMEOUT;
394 game.server.razechance = GAME_DEFAULT_RAZECHANCE;
395 game.server.revealmap = GAME_DEFAULT_REVEALMAP;
396 game.server.revolution_length = GAME_DEFAULT_REVOLUTION_LENGTH;
397 sz_strlcpy(game.server.rulesetdir, GAME_DEFAULT_RULESETDIR);
398 game.server.save_compress_level = GAME_DEFAULT_COMPRESS_LEVEL;
399 game.server.save_compress_type = GAME_DEFAULT_COMPRESS_TYPE;
400 sz_strlcpy(game.server.save_name, GAME_DEFAULT_SAVE_NAME);
401 game.server.save_nturns = GAME_DEFAULT_SAVETURNS;
402 game.server.save_options.save_known = TRUE;
403 game.server.save_options.save_private_map = TRUE;
404 game.server.save_options.save_starts = TRUE;
405 game.server.savepalace = GAME_DEFAULT_SAVEPALACE;
406 game.server.scorelog = GAME_DEFAULT_SCORELOG;
407 game.server.scoreloglevel = GAME_DEFAULT_SCORELOGLEVEL;
408 game.server.scoreturn = GAME_DEFAULT_SCORETURN - 1;
409 game.server.seed = GAME_DEFAULT_SEED;
410 sz_strlcpy(game.server.start_units, GAME_DEFAULT_START_UNITS);
411 game.server.start_year = GAME_START_YEAR;
412 game.server.tcptimeout = GAME_DEFAULT_TCPTIMEOUT;
413 game.server.techlost_donor = GAME_DEFAULT_TECHLOST_DONOR;
414 game.server.techlost_recv = GAME_DEFAULT_TECHLOST_RECV;
415 game.server.techpenalty = GAME_DEFAULT_TECHPENALTY;
416 game.server.timeoutaddenemymove = GAME_DEFAULT_TIMEOUTADDEMOVE;
417 game.server.timeoutcounter = GAME_DEFAULT_TIMEOUTCOUNTER;
418 game.server.timeoutinc = GAME_DEFAULT_TIMEOUTINC;
419 game.server.timeoutincmult = GAME_DEFAULT_TIMEOUTINCMULT;
420 game.server.timeoutint = GAME_DEFAULT_TIMEOUTINT;
421 game.server.timeoutintinc = GAME_DEFAULT_TIMEOUTINTINC;
422 game.server.turnblock = GAME_DEFAULT_TURNBLOCK;
423 game.server.unitwaittime = GAME_DEFAULT_UNITWAITTIME;
424 game.server.plr_colors = NULL;
425 } else {
426 /* Client side takes care of itself in client_main() */
430 /****************************************************************************
431 Initialise all game settings.
433 The variables are listed in alphabetical order.
434 ****************************************************************************/
435 void game_init(void)
437 game_defaults();
438 player_slots_init();
439 map_init(&wld.map, is_server());
440 team_slots_init();
441 game_ruleset_init();
442 idex_init();
443 cm_init();
444 researches_init();
445 universal_found_functions_init();
448 /****************************************************************************
449 Initialize map-specific parts of the game structure. Maybe these should
450 be moved into the map structure?
451 ****************************************************************************/
452 void game_map_init(void)
454 /* FIXME: it's not clear where these values should be initialized. It
455 * can't be done in game_init because the map isn't created yet. Maybe it
456 * should be done in the mapgen code or in the maphand code. It should
457 * surely be called when the map is generated. */
458 game.info.warminglevel = (map_num_tiles() + 499) / 500;
459 game.info.coolinglevel = (map_num_tiles() + 499) / 500;
462 /***************************************************************
463 Frees all memory of the game.
464 ***************************************************************/
465 void game_free(void)
467 player_slots_free();
468 map_free(&(wld.map));
469 free_city_map_index();
470 idex_free();
471 team_slots_free();
472 game_ruleset_free();
473 researches_free();
474 cm_free();
477 /***************************************************************
478 Do all changes to change view, and not full
479 game_free()/game_init().
480 ***************************************************************/
481 void game_reset(void)
483 if (is_server()) {
484 game_free();
485 game_init();
486 } else {
487 /* Reset the players infos. */
488 players_iterate(pplayer) {
489 player_clear(pplayer, FALSE);
490 } players_iterate_end;
492 map_free(&(wld.map));
493 free_city_map_index();
494 idex_free();
496 map_init(&wld.map, FALSE);
497 idex_init();
498 researches_init();
502 /***************************************************************
503 Initialize the objects which will read from a ruleset.
504 ***************************************************************/
505 void game_ruleset_init(void)
507 nation_sets_groups_init();
508 ruleset_cache_init();
509 disaster_types_init();
510 achievements_init();
511 actions_init();
512 trade_route_types_init();
513 terrains_init();
514 extras_init();
515 goods_init();
516 improvements_init();
517 techs_init();
518 unit_classes_init();
519 unit_types_init();
520 specialists_init();
521 user_unit_class_flags_init();
522 user_unit_type_flags_init();
523 user_terrain_flags_init();
524 user_extra_flags_init();
525 tech_classes_init();
526 user_tech_flags_init();
527 multipliers_init();
529 if (is_server()) {
530 game.server.ruledit.nationlist = NULL;
531 game.server.ruledit.embedded_nations = NULL;
532 game.server.ruledit.embedded_nations_count = 0;
533 game.server.ruledit.allowed_govs = NULL;
534 game.server.ruledit.allowed_terrains = NULL;
535 game.server.ruledit.allowed_styles = NULL;
536 game.server.ruledit.nc_agovs = NULL;
537 game.server.ruledit.nc_aterrs = NULL;
538 game.server.ruledit.nc_astyles = NULL;
539 game.server.ruledit.ag_count = 0;
540 game.server.ruledit.at_count = 0;
541 game.server.ruledit.as_count = 0;
545 /***************************************************************
546 Frees all memory which in objects which are read from a ruleset.
547 ***************************************************************/
548 void game_ruleset_free(void)
550 int i;
552 CALL_FUNC_EACH_AI(units_ruleset_close);
554 /* Clear main structures which can points to the ruleset dependent
555 * structures. */
556 players_iterate(pplayer) {
557 player_ruleset_close(pplayer);
558 } players_iterate_end;
559 game.government_during_revolution = NULL;
561 specialists_free();
562 unit_classes_free();
563 techs_free();
564 governments_free();
565 nations_free();
566 unit_types_free();
567 unit_type_flags_free();
568 unit_class_flags_free();
569 role_unit_precalcs_free();
570 improvements_free();
571 goods_free();
572 extras_free();
573 music_styles_free();
574 city_styles_free();
575 styles_free();
576 actions_free();
577 achievements_free();
578 disaster_types_free();
579 terrains_free();
580 user_tech_flags_free();
581 extra_flags_free();
582 user_terrain_flags_free();
583 ruleset_cache_free();
584 nation_sets_groups_free();
585 multipliers_free();
587 /* Destroy the default veteran system. */
588 veteran_system_destroy(game.veteran);
589 game.veteran = NULL;
591 /* Player colors. */
592 if (game.plr_bg_color != NULL) {
593 rgbcolor_destroy(game.plr_bg_color);
594 game.plr_bg_color = NULL;
597 if (is_server()) {
598 if (game.server.ruledit.description_file != NULL) {
599 free(game.server.ruledit.description_file);
600 game.server.ruledit.description_file = NULL;
602 if (game.server.ruledit.nationlist != NULL) {
603 free(game.server.ruledit.nationlist);
604 game.server.ruledit.nationlist = NULL;
606 if (game.server.ruledit.embedded_nations != NULL) {
607 for (i = 0; i < game.server.ruledit.embedded_nations_count; i++) {
608 free(game.server.ruledit.embedded_nations[i]);
610 free(game.server.ruledit.embedded_nations);
611 game.server.ruledit.embedded_nations = NULL;
612 game.server.ruledit.embedded_nations_count = 0;
613 if (game.server.ruledit.allowed_govs != NULL) {
614 for (i = 0; i < game.server.ruledit.ag_count; i++) {
615 free(game.server.ruledit.nc_agovs[i]);
617 free(game.server.ruledit.allowed_govs);
618 game.server.ruledit.allowed_govs = NULL;
619 game.server.ruledit.nc_agovs = NULL;
621 if (game.server.ruledit.allowed_terrains != NULL) {
622 for (i = 0; i < game.server.ruledit.at_count; i++) {
623 free(game.server.ruledit.nc_aterrs[i]);
625 free(game.server.ruledit.allowed_terrains);
626 game.server.ruledit.allowed_terrains = NULL;
627 game.server.ruledit.nc_aterrs = NULL;
629 if (game.server.ruledit.allowed_styles != NULL) {
630 for (i = 0; i < game.server.ruledit.as_count; i++) {
631 free(game.server.ruledit.nc_astyles[i]);
633 free(game.server.ruledit.allowed_styles);
634 game.server.ruledit.allowed_styles = NULL;
635 game.server.ruledit.nc_astyles = NULL;
640 for (i = 0; i < MAX_CALENDAR_FRAGMENTS; i++) {
641 game.calendar.calendar_fragment_name[i][0] = '\0';
644 if (game.ruleset_summary != NULL) {
645 free(game.ruleset_summary);
646 game.ruleset_summary = NULL;
649 if (game.ruleset_description != NULL) {
650 free(game.ruleset_description);
651 game.ruleset_description = NULL;
655 /***************************************************************
656 Initialize wonder information.
657 ***************************************************************/
658 void initialize_globals(void)
660 players_iterate(pplayer) {
661 city_list_iterate(pplayer->cities, pcity) {
662 city_built_iterate(pcity, pimprove) {
663 if (is_wonder(pimprove)) {
664 if (is_great_wonder(pimprove)) {
665 game.info.great_wonder_owners[improvement_index(pimprove)] =
666 player_number(pplayer);
668 pplayer->wonders[improvement_index(pimprove)] = pcity->id;
670 } city_built_iterate_end;
671 } city_list_iterate_end;
672 } players_iterate_end;
675 /**************************************************************************
676 Return TRUE if it is this player's phase.
677 NB: The meaning of the 'phase' argument must match its use in the
678 function begin_turn() in server/srv_main.c.
679 NB: The phase mode PMT_TEAMS_ALTERNATE assumes that every player is
680 on a team, i.e. that pplayer->team is never NULL.
681 **************************************************************************/
682 bool is_player_phase(const struct player *pplayer, int phase)
684 switch (game.info.phase_mode) {
685 case PMT_CONCURRENT:
686 return TRUE;
687 break;
688 case PMT_PLAYERS_ALTERNATE:
689 return player_number(pplayer) == phase;
690 break;
691 case PMT_TEAMS_ALTERNATE:
692 fc_assert_ret_val(NULL != pplayer->team, FALSE);
693 return team_number(pplayer->team) == phase;
694 break;
695 default:
696 break;
699 fc_assert_msg(FALSE, "Unrecognized phase mode %d in is_player_phase().",
700 phase);
701 return TRUE;
704 /****************************************************************************
705 Return a prettily formatted string containing the population text. The
706 population is passed in as the number of citizens, in unit
707 (tens/hundreds/thousands...) defined in cities.ruleset.
708 ****************************************************************************/
709 const char *population_to_text(int thousand_citizen)
711 /* big_int_to_text can't handle negative values, and in any case we'd
712 * better not have a negative population. */
713 fc_assert_ret_val(thousand_citizen >= 0, NULL);
714 return big_int_to_text(thousand_citizen, game.info.pop_report_zeroes - 1);
717 /**************************************************************************
718 Return a string containing the save year.
719 **************************************************************************/
720 static char *year_suffix(void)
722 static char buf[MAX_LEN_NAME];
723 const char *suffix;
724 char safe_year_suffix[MAX_LEN_NAME];
725 const char *max = safe_year_suffix + MAX_LEN_NAME - 1;
726 char *c = safe_year_suffix;
728 if (game.info.year < 0) {
729 suffix = game.calendar.negative_year_label;
730 } else {
731 suffix = game.calendar.positive_year_label;
734 /* Remove all non alphanumeric characters from the year suffix. */
735 for (; '\0' != *suffix && c < max; suffix++) {
736 if (fc_isalnum(*suffix)) {
737 *c++ = *suffix;
740 *c = '\0';
742 fc_snprintf(buf, sizeof(buf), "%s", safe_year_suffix);
744 return buf;
747 /**************************************************************************
748 Generate a default save file name and place it in the provided buffer.
749 Within the name the following custom formats are allowed:
751 %R = <reason>
752 %S = <suffix>
753 %T = <game.info.turn>
754 %Y = <game.info.year>
756 Examples:
757 'freeciv-T%04T-Y%+04Y-%R' => 'freeciv-T0099-Y-0050-manual'
758 => 'freeciv-T0100-Y00001-auto'
760 Returns the number of characters written, or the number of characters
761 that would have been written if truncation occurs.
763 NB: If you change the format definition, be sure to update the above
764 function comment and the help text for the 'savename' setting.
765 **************************************************************************/
766 int generate_save_name(const char *format, char *buf, int buflen,
767 const char *reason)
769 struct cf_sequence sequences[] = {
770 cf_str_seq('R', (reason == NULL) ? "auto" : reason),
771 cf_str_seq('S', year_suffix()),
772 { 0 }, { 0 }, /* Works for both gcc and tcc */
773 cf_end()
776 cf_int_seq('T', game.info.turn, &sequences[2]);
777 cf_int_seq('Y', game.info.year, &sequences[3]);
779 fc_vsnprintcf(buf, buflen, format, sequences, -1);
781 if (0 == strcmp(format, buf)) {
782 /* Use the default savename if 'format' does not contain
783 * printf information. */
784 char savename[512];
786 fc_snprintf(savename, sizeof(savename), "%s-T%%04T-Y%%05Y-%%R",
787 format);
788 fc_vsnprintcf(buf, buflen, savename, sequences, -1);
791 log_debug("save name generated from '%s': %s", format, buf);
793 return strlen(buf);
796 /**************************************************************************
797 Initialize user flag.
798 **************************************************************************/
799 void user_flag_init(struct user_flag *flag)
801 flag->name = NULL;
802 flag->helptxt = NULL;
805 /**************************************************************************
806 Free user flag.
807 **************************************************************************/
808 void user_flag_free(struct user_flag *flag)
810 if (flag->name != NULL) {
811 FC_FREE(flag->name);
812 flag->name = NULL;
814 if (flag->helptxt != NULL) {
815 FC_FREE(flag->helptxt);
816 flag->helptxt = NULL;
820 /****************************************************************************
821 Return timeout value for the current turn.
822 ****************************************************************************/
823 int current_turn_timeout(void)
825 if (game.info.turn == 1 && game.info.first_timeout != -1) {
826 return game.info.first_timeout;
827 } else {
828 return game.info.timeout;