1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 #include <fc_config.h>
19 #include "bitvector.h"
25 #include "government.h"
30 #include "specialist.h"
36 #include "citytools.h"
37 #include "cityturn.h" /* city_repair_size() */
41 #include "unittools.h"
43 #include "sanitycheck.h"
46 #ifdef SANITY_CHECKING
48 #define SANITY_FAIL(format, ...) \
49 fc_assert_fail(file, function, line, NULL, format, ## __VA_ARGS__)
51 #define SANITY_CHECK(check) \
52 fc_assert_full(file, function, line, check, , NOLOGMSG, NOLOGMSG)
54 #define SANITY_CITY(_city, check) \
55 fc_assert_full(file, function, line, check, , \
56 "(%4d, %4d) in \"%s\"[%d]", TILE_XY((_city)->tile), \
57 city_name_get(_city), city_size_get(_city))
59 #define SANITY_TERRAIN(_tile, check) \
60 fc_assert_full(file, function, line, check, , \
61 "(%4d, %4d) at \"%s\"", TILE_XY(_tile), \
62 terrain_rule_name(tile_terrain(_tile)))
64 #define SANITY_TILE(_tile, check) \
66 struct city *_tile##_city = tile_city(_tile); \
67 if (NULL != _tile##_city) { \
68 SANITY_CITY(_tile##_city, check); \
70 SANITY_TERRAIN(_tile, check); \
74 static void check_city_feelings(const struct city
*pcity
, const char *file
,
75 const char *function
, int line
);
77 /**************************************************************************
78 Sanity checking on map (tile) specials.
79 **************************************************************************/
80 static void check_specials(const char *file
, const char *function
, int line
)
82 whole_map_iterate(ptile
) {
83 const struct terrain
*pterrain
= tile_terrain(ptile
);
85 extra_type_iterate(pextra
) {
86 if (tile_has_extra(ptile
, pextra
)) {
87 extra_deps_iterate(&(pextra
->reqs
), pdep
) {
88 SANITY_TILE(ptile
, tile_has_extra(ptile
, pdep
));
89 } extra_deps_iterate_end
;
91 } extra_type_iterate_end
;
93 extra_type_by_cause_iterate(EC_MINE
, pextra
) {
94 if (tile_has_extra(ptile
, pextra
)) {
95 SANITY_TILE(ptile
, pterrain
->mining_result
== pterrain
);
97 } extra_type_by_cause_iterate_end
;
98 extra_type_by_cause_iterate(EC_IRRIGATION
, pextra
) {
99 if (tile_has_extra(ptile
, pextra
)) {
100 SANITY_TILE(ptile
, pterrain
->irrigation_result
== pterrain
);
102 } extra_type_by_cause_iterate_end
;
104 SANITY_TILE(ptile
, terrain_index(pterrain
) >= T_FIRST
105 && terrain_index(pterrain
) < terrain_count());
106 } whole_map_iterate_end
;
109 /**************************************************************************
110 Sanity checking on fog-of-war (visibility, shared vision, etc.).
111 **************************************************************************/
112 static void check_fow(const char *file
, const char *function
, int line
)
114 if (!game_was_started()) {
115 /* The private map of the players is only allocated at game start. */
119 whole_map_iterate(ptile
) {
120 players_iterate(pplayer
) {
121 struct player_tile
*plr_tile
= map_get_player_tile(ptile
, pplayer
);
123 vision_layer_iterate(v
) {
124 /* underflow of unsigned int */
125 SANITY_TILE(ptile
, plr_tile
->seen_count
[v
] < 30000);
126 SANITY_TILE(ptile
, plr_tile
->own_seen
[v
] < 30000);
127 SANITY_TILE(ptile
, plr_tile
->own_seen
[v
] <= plr_tile
->seen_count
[v
]);
128 } vision_layer_iterate_end
;
130 /* Lots of server bits depend on this. */
131 SANITY_TILE(ptile
, plr_tile
->seen_count
[V_INVIS
]
132 <= plr_tile
->seen_count
[V_MAIN
]);
133 SANITY_TILE(ptile
, plr_tile
->own_seen
[V_INVIS
]
134 <= plr_tile
->own_seen
[V_MAIN
]);
135 } players_iterate_end
;
136 } whole_map_iterate_end
;
138 SANITY_CHECK(game
.government_during_revolution
!= NULL
);
139 SANITY_CHECK(game
.government_during_revolution
140 == government_by_number(game
.info
.government_during_revolution_id
));
143 /**************************************************************************
144 Miscellaneous sanity checks.
145 **************************************************************************/
146 static void check_misc(const char *file
, const char *function
, int line
)
148 int nplayers
= 0, nbarbs
= 0;
150 /* Do not use player_slots_iterate as we want to check the index! */
151 player_slots_iterate(pslot
) {
152 if (player_slot_is_used(pslot
)) {
153 if (is_barbarian(player_slot_get_player(pslot
))) {
158 } player_slots_iterate_end
;
160 SANITY_CHECK(nplayers
== player_count());
161 SANITY_CHECK(nbarbs
== server
.nbarbarians
);
163 SANITY_CHECK(player_count() <= player_slot_count());
164 SANITY_CHECK(team_count() <= MAX_NUM_TEAM_SLOTS
);
165 SANITY_CHECK(normal_player_count() <= game
.server
.max_players
);
168 /**************************************************************************
169 Sanity checks on the map itself. See also check_specials.
170 **************************************************************************/
171 static void check_map(const char *file
, const char *function
, int line
)
173 whole_map_iterate(ptile
) {
174 struct city
*pcity
= tile_city(ptile
);
175 int cont
= tile_continent(ptile
);
177 CHECK_INDEX(tile_index(ptile
));
180 SANITY_TILE(ptile
, same_pos(pcity
->tile
, ptile
));
181 if (BORDERS_DISABLED
!= game
.info
.borders
) {
182 SANITY_TILE(ptile
, tile_owner(ptile
) != NULL
);
186 if (is_ocean_tile(ptile
)) {
187 SANITY_TILE(ptile
, cont
< 0);
188 adjc_iterate(ptile
, tile1
) {
189 if (is_ocean_tile(tile1
)) {
190 SANITY_TILE(ptile
, tile_continent(tile1
) == cont
);
194 SANITY_TILE(ptile
, cont
> 0);
195 adjc_iterate(ptile
, tile1
) {
196 if (!is_ocean_tile(tile1
)) {
197 SANITY_TILE(ptile
, tile_continent(tile1
) == cont
);
202 unit_list_iterate(ptile
->units
, punit
) {
203 SANITY_TILE(ptile
, same_pos(unit_tile(punit
), ptile
));
205 /* Check diplomatic status of stacked units. */
206 unit_list_iterate(ptile
->units
, punit2
) {
207 SANITY_TILE(ptile
, pplayers_allied(unit_owner(punit
),
208 unit_owner(punit2
)));
209 } unit_list_iterate_end
;
211 SANITY_TILE(ptile
, pplayers_allied(unit_owner(punit
),
214 } unit_list_iterate_end
;
215 } whole_map_iterate_end
;
218 /**************************************************************************
219 Verify that the city itself has sane values.
220 **************************************************************************/
221 static bool check_city_good(struct city
*pcity
, const char *file
,
222 const char *function
, int line
)
224 struct player
*pplayer
= city_owner(pcity
);
225 struct tile
*pcenter
= city_tile(pcity
);
227 if (NULL
== pcenter
) {
229 SANITY_FAIL("(----,----) city has no tile (skipping remaining tests), "
230 "at %s \"%s\"[%d]%s",
231 nation_rule_name(nation_of_player(pplayer
)),
232 city_name_get(pcity
), city_size_get(pcity
),
237 SANITY_CITY(pcity
, !terrain_has_flag(tile_terrain(pcenter
), TER_NO_CITIES
));
239 if (BORDERS_DISABLED
!= game
.info
.borders
) {
240 SANITY_CITY(pcity
, NULL
!= tile_owner(pcenter
));
243 if (NULL
!= tile_owner(pcenter
)) {
244 if (tile_owner(pcenter
) != pplayer
) {
245 SANITY_FAIL("(%4d,%4d) tile owned by %s, at %s \"%s\"[%d]%s",
247 nation_rule_name(nation_of_player(tile_owner(pcenter
))),
248 nation_rule_name(nation_of_player(pplayer
)),
249 city_name_get(pcity
), city_size_get(pcity
),
254 unit_list_iterate(pcity
->units_supported
, punit
) {
255 SANITY_CITY(pcity
, punit
->homecity
== pcity
->id
);
256 SANITY_CITY(pcity
, unit_owner(punit
) == pplayer
);
257 } unit_list_iterate_end
;
259 city_built_iterate(pcity
, pimprove
) {
260 if (is_small_wonder(pimprove
)) {
261 SANITY_CITY(pcity
, city_from_small_wonder(pplayer
, pimprove
) == pcity
);
262 } else if (is_great_wonder(pimprove
)) {
263 SANITY_CITY(pcity
, city_from_great_wonder(pimprove
) == pcity
);
265 } city_built_iterate_end
;
270 /**************************************************************************
271 Sanity check city size versus worker and specialist counts.
272 **************************************************************************/
273 static void check_city_size(struct city
*pcity
, const char *file
,
274 const char *function
, int line
)
277 int citizen_count
= 0;
278 struct tile
*pcenter
= city_tile(pcity
);
280 SANITY_CITY(pcity
, city_size_get(pcity
) >= 1);
282 city_tile_iterate_skip_free_worked(city_map_radius_sq_get(pcity
), pcenter
,
283 ptile
, _index
, _x
, _y
) {
284 if (tile_worked(ptile
) == pcity
) {
287 } city_tile_iterate_skip_free_worked_end
;
289 citizen_count
+= city_specialists(pcity
);
290 delta
= city_size_get(pcity
) - citizen_count
;
292 SANITY_FAIL("(%4d,%4d) %d citizens not equal [size], "
293 "repairing \"%s\"[%d]", TILE_XY(pcity
->tile
),
294 citizen_count
, city_name_get(pcity
), city_size_get(pcity
));
296 citylog_map_workers(LOG_DEBUG
, pcity
);
297 log_debug("[%s (%d)] specialists: %d", city_name_get(pcity
), pcity
->id
,
298 city_specialists(pcity
));
300 city_repair_size(pcity
, delta
);
301 city_refresh_from_main_map(pcity
, NULL
);
305 /**************************************************************************
306 Verify that the number of people with feelings + specialists equal
308 **************************************************************************/
309 static void check_city_feelings(const struct city
*pcity
, const char *file
,
310 const char *function
, int line
)
313 int spe
= city_specialists(pcity
);
315 for (feel
= FEELING_BASE
; feel
< FEELING_LAST
; feel
++) {
319 for (ccategory
= CITIZEN_HAPPY
; ccategory
< CITIZEN_LAST
; ccategory
++) {
320 sum
+= pcity
->feel
[ccategory
][feel
];
323 /* While loading savegame, we want to check sanity of values read from the
324 * savegame despite the fact that city workers_frozen level of the city
325 * is above zero -> can't limit sanitycheck callpoints by that. Instead
326 * we check even more relevant needs_arrange. */
327 SANITY_CITY(pcity
, !pcity
->server
.needs_arrange
);
329 SANITY_CITY(pcity
, city_size_get(pcity
) - spe
== sum
);
333 /**************************************************************************
334 Verify that the city has sane values.
335 **************************************************************************/
336 void real_sanity_check_city(struct city
*pcity
, const char *file
,
337 const char *function
, int line
)
339 if (check_city_good(pcity
, file
, function
, line
)) {
340 check_city_size(pcity
, file
, function
, line
);
341 check_city_feelings(pcity
, file
, function
, line
);
345 /**************************************************************************
346 Sanity checks on all cities in the world.
347 **************************************************************************/
348 static void check_cities(const char *file
, const char *function
, int line
)
350 players_iterate(pplayer
) {
351 city_list_iterate(pplayer
->cities
, pcity
) {
352 SANITY_CITY(pcity
, city_owner(pcity
) == pplayer
);
354 real_sanity_check_city(pcity
, file
, function
, line
);
355 } city_list_iterate_end
;
356 } players_iterate_end
;
359 /**************************************************************************
360 Sanity checks on all units in the world.
361 **************************************************************************/
362 static void check_units(const char *file
, const char *function
, int line
)
364 players_iterate(pplayer
) {
365 unit_list_iterate(pplayer
->units
, punit
) {
366 struct tile
*ptile
= unit_tile(punit
);
367 struct terrain
*pterr
= tile_terrain(ptile
);
370 struct unit
*ptrans
= unit_transport_get(punit
);
372 SANITY_CHECK(unit_owner(punit
) == pplayer
);
374 if (IDENTITY_NUMBER_ZERO
!= punit
->homecity
) {
375 SANITY_CHECK(phome
= player_city_by_number(pplayer
,
378 SANITY_CHECK(city_owner(phome
) == pplayer
);
382 /* Unit in the correct player list? */
383 SANITY_CHECK(player_unit_by_number(unit_owner(punit
),
386 if (!can_unit_continue_current_activity(punit
)) {
387 SANITY_FAIL("(%4d,%4d) %s has activity %s, "
388 "but it can't continue at %s",
389 TILE_XY(ptile
), unit_rule_name(punit
),
390 get_activity_text(punit
->activity
),
391 tile_get_info_text(ptile
, TRUE
, 0));
394 if (activity_requires_target(punit
->activity
)
395 && (punit
->activity
!= ACTIVITY_IRRIGATE
|| pterr
->irrigation_result
== pterr
)
396 && (punit
->activity
!= ACTIVITY_MINE
|| pterr
->mining_result
== pterr
)) {
397 SANITY_CHECK(punit
->activity_target
!= NULL
);
400 pcity
= tile_city(ptile
);
402 SANITY_CHECK(pplayers_allied(city_owner(pcity
), pplayer
));
405 SANITY_CHECK(punit
->moves_left
>= 0);
406 SANITY_CHECK(punit
->hp
> 0);
408 /* Check for ground units in the ocean. */
409 SANITY_CHECK(can_unit_exist_at_tile(punit
, ptile
) || ptrans
!= NULL
);
411 /* Check for over-full transports. */
412 SANITY_CHECK(get_transporter_occupancy(punit
)
413 <= get_transporter_capacity(punit
));
415 /* Check transporter. This should be last as the pointer ptrans will
417 if (ptrans
!= NULL
) {
418 struct unit
*plevel
= punit
;
421 /* Make sure the transporter is on the tile. */
422 SANITY_CHECK(same_pos(unit_tile(punit
), unit_tile(ptrans
)));
424 /* Can punit be cargo for its transporter? */
425 SANITY_CHECK(unit_transport_check(punit
, ptrans
));
427 /* Check that the unit is listed as transported. */
428 SANITY_CHECK(unit_list_search(unit_transport_cargo(ptrans
),
431 /* Check the depth of the transportation. */
433 struct unit_list
*pcargos
= unit_transport_cargo(ptrans
);
435 SANITY_CHECK(pcargos
!= NULL
);
436 SANITY_CHECK(level
< GAME_TRANSPORT_MAX_RECURSIVE
);
438 /* Check for next level. */
440 ptrans
= unit_transport_get(plevel
);
444 /* Transporter capacity will be checked when transporter itself
448 /* Check that cargo is marked as transported with this unit */
449 unit_list_iterate(unit_transport_cargo(punit
), pcargo
) {
450 SANITY_CHECK(unit_transport_get(pcargo
) == punit
);
451 } unit_list_iterate_end
;
452 } unit_list_iterate_end
;
453 } players_iterate_end
;
456 /**************************************************************************
457 Sanity checks on all players.
458 **************************************************************************/
459 static void check_players(const char *file
, const char *function
, int line
)
461 players_iterate(pplayer
) {
462 int found_palace
= 0;
464 if (!pplayer
->is_alive
) {
465 /* Dead players' units and cities are disbanded in kill_player(). */
466 SANITY_CHECK(unit_list_size(pplayer
->units
) == 0);
467 SANITY_CHECK(city_list_size(pplayer
->cities
) == 0);
472 SANITY_CHECK(pplayer
->server
.adv
!= NULL
);
473 SANITY_CHECK(!pplayer
->nation
|| pplayer
->nation
->player
== pplayer
);
474 SANITY_CHECK(player_list_search(team_members(pplayer
->team
), pplayer
));
476 SANITY_CHECK(!(city_list_size(pplayer
->cities
) > 0
477 && !pplayer
->server
.got_first_city
));
479 city_list_iterate(pplayer
->cities
, pcity
) {
480 if (is_capital(pcity
)) {
483 SANITY_CITY(pcity
, found_palace
<= 1);
484 } city_list_iterate_end
;
486 players_iterate(pplayer2
) {
487 struct player_diplstate
*state1
, *state2
;
489 if (pplayer2
== pplayer
) {
490 break; /* Do diplomatic sanity check only once per player couple. */
493 state1
= player_diplstate_get(pplayer
, pplayer2
);
494 state2
= player_diplstate_get(pplayer2
, pplayer
);
495 SANITY_CHECK(state1
->type
== state2
->type
);
496 SANITY_CHECK(state1
->max_state
== state2
->max_state
);
497 if (state1
->type
== DS_CEASEFIRE
498 || state1
->type
== DS_ARMISTICE
) {
499 SANITY_CHECK(state1
->turns_left
== state2
->turns_left
);
501 if (state1
->type
== DS_TEAM
) {
502 SANITY_CHECK(players_on_same_team(pplayer
, pplayer2
));
503 SANITY_CHECK(player_has_real_embassy(pplayer
, pplayer2
));
504 SANITY_CHECK(player_has_real_embassy(pplayer2
, pplayer
));
505 SANITY_CHECK(really_gives_vision(pplayer
, pplayer2
));
506 SANITY_CHECK(really_gives_vision(pplayer2
, pplayer
));
508 if (pplayer
->is_alive
509 && pplayer2
->is_alive
510 && pplayers_allied(pplayer
, pplayer2
)) {
511 enum dipl_reason allied_players_can_be_allied
=
512 pplayer_can_make_treaty(pplayer
, pplayer2
, DS_ALLIANCE
);
513 SANITY_CHECK(allied_players_can_be_allied
514 != DIPL_ALLIANCE_PROBLEM_US
);
515 SANITY_CHECK(allied_players_can_be_allied
516 != DIPL_ALLIANCE_PROBLEM_THEM
);
518 } players_iterate_end
;
520 if (pplayer
->revolution_finishes
== -1) {
521 if (government_of_player(pplayer
) == game
.government_during_revolution
) {
522 SANITY_FAIL("%s government is anarchy, but does not finish!",
523 nation_rule_name(nation_of_player(pplayer
)));
525 SANITY_CHECK(government_of_player(pplayer
) != game
.government_during_revolution
);
526 } else if (pplayer
->revolution_finishes
> game
.info
.turn
) {
527 SANITY_CHECK(government_of_player(pplayer
) == game
.government_during_revolution
);
529 /* Things may vary in this case depending on when the sanity_check
530 * call is made. No better check is possible. */
533 /* Dying players shouldn't be left around. But they are. */
534 SANITY_CHECK(!BV_ISSET(pplayer
->server
.status
, PSTATUS_DYING
));
535 } players_iterate_end
;
537 nations_iterate(pnation
) {
538 SANITY_CHECK(!pnation
->player
|| pnation
->player
->nation
== pnation
);
539 } nations_iterate_end
;
541 teams_iterate(pteam
) {
542 player_list_iterate(team_members(pteam
), pplayer
) {
543 SANITY_CHECK(pplayer
->team
== pteam
);
544 } player_list_iterate_end
;
548 /****************************************************************************
549 Sanity checking on teams.
550 ****************************************************************************/
551 static void check_teams(const char *file
, const char *function
, int line
)
553 int count
[MAX_NUM_TEAM_SLOTS
];
555 memset(count
, 0, sizeof(count
));
556 players_iterate(pplayer
) {
557 /* For the moment, all players have teams. */
558 SANITY_CHECK(pplayer
->team
!= NULL
);
560 count
[team_index(pplayer
->team
)]++;
562 } players_iterate_end
;
564 team_slots_iterate(tslot
) {
565 if (team_slot_is_used(tslot
)) {
566 struct team
*pteam
= team_slot_get_team(tslot
);
567 fc_assert_exit(pteam
);
568 SANITY_CHECK(player_list_size(team_members(pteam
))
569 == count
[team_slot_index(tslot
)]);
571 } team_slots_iterate_end
;
574 /****************************************************************************
575 Sanity checks on all players.
576 ****************************************************************************/
578 check_researches(const char *file
, const char *function
, int line
)
580 researches_iterate(presearch
) {
581 SANITY_CHECK(S_S_RUNNING
!= server_state()
582 || A_UNSET
== presearch
->researching
583 || is_future_tech(presearch
->researching
)
584 || (A_NONE
!= presearch
->researching
585 && valid_advance_by_number(presearch
->researching
)));
586 SANITY_CHECK(A_UNSET
== presearch
->tech_goal
587 || (A_NONE
!= presearch
->tech_goal
588 && valid_advance_by_number(presearch
->tech_goal
)));
589 } researches_iterate_end
;
592 /**************************************************************************
593 Sanity checking on connections.
594 **************************************************************************/
595 static void check_connections(const char *file
, const char *function
,
598 /* est_connections is a subset of all_connections */
599 SANITY_CHECK(conn_list_size(game
.all_connections
)
600 >= conn_list_size(game
.est_connections
));
603 /**************************************************************************
604 Do sanity checks on the server state. Call this once per turn or
605 whenever you feel like it.
607 But be careful, calling it too much would make the server slow down. And
608 at some times the server isn't supposed to be in a sane state so you
609 can't call it in the middle of an operation that is supposed to be
611 **************************************************************************/
612 void real_sanity_check(const char *file
, const char *function
, int line
)
614 if (!map_is_empty()) {
615 /* Don't sanity-check the map if it hasn't been created yet (this
616 * happens when loading scenarios). */
617 check_specials(file
, function
, line
);
618 check_map(file
, function
, line
);
619 check_cities(file
, function
, line
);
620 check_units(file
, function
, line
);
621 check_fow(file
, function
, line
);
623 check_misc(file
, function
, line
);
624 check_players(file
, function
, line
);
625 check_teams(file
, function
, line
);
626 check_researches(file
, function
, line
);
627 check_connections(file
, function
, line
);
630 /*****************************************************************************
631 Verify that the tile has sane values. This should be called after the
633 *****************************************************************************/
634 void real_sanity_check_tile(struct tile
*ptile
, const char *file
,
635 const char *function
, int line
)
637 SANITY_CHECK(ptile
!= NULL
);
638 SANITY_CHECK(ptile
->terrain
!= NULL
);
640 unit_list_iterate(ptile
->units
, punit
) {
641 /* Check if the units can survive on the tile (terrain). Here only the
642 * 'easy' test if the unit is transported is done. A complete check is
643 * done by check_units() in real_sanity_check(). */
644 if (!can_unit_exist_at_tile(punit
, ptile
)
645 && !unit_transported(punit
)) {
646 SANITY_FAIL("(%4d,%4d) %s can't survive on %s", TILE_XY(ptile
),
647 unit_rule_name(punit
), tile_get_info_text(ptile
, TRUE
, 0));
649 } unit_list_iterate_end
;
652 #endif /* SANITY_CHECKING */