1 /**********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
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"
24 #include "fc_interface.h"
34 /****************************************************************************
35 Return the tile index.
36 ****************************************************************************/
37 int tile_index(const struct tile
*ptile
)
44 /****************************************************************************
45 Return the player who owns this tile (or NULL if none).
46 ****************************************************************************/
47 struct player
*tile_owner(const struct tile
*ptile
)
54 /****************************************************************************
55 Return the player who owns this tile (or NULL if none).
56 ****************************************************************************/
57 struct tile
*tile_claimer(const struct tile
*ptile
)
59 return ptile
->claimer
;
63 /****************************************************************************
64 Set the owner of a tile (may be NULL).
65 ****************************************************************************/
66 void tile_set_owner(struct tile
*ptile
, struct player
*pplayer
,
69 if (BORDERS_DISABLED
!= game
.info
.borders
) {
70 ptile
->owner
= pplayer
;
71 ptile
->claimer
= claimer
;
75 /****************************************************************************
76 Return the city on this tile (or NULL), checking for city center.
77 ****************************************************************************/
78 struct city
*tile_city(const struct tile
*ptile
)
80 struct city
*pcity
= ptile
->worked
;
82 if (NULL
!= pcity
&& is_city_center(pcity
, ptile
)) {
89 /****************************************************************************
90 Return any city working the specified tile (or NULL).
91 ****************************************************************************/
92 struct city
*tile_worked(const struct tile
*ptile
)
98 /****************************************************************************
99 Set the city/worker on the tile (may be NULL).
100 ****************************************************************************/
101 void tile_set_worked(struct tile
*ptile
, struct city
*pcity
)
103 ptile
->worked
= pcity
;
107 /****************************************************************************
108 Return the terrain at the specified tile.
109 ****************************************************************************/
110 struct terrain
*tile_terrain(const struct tile
*ptile
)
112 return ptile
->terrain
;
116 /****************************************************************************
117 Set the given terrain at the specified tile.
118 ****************************************************************************/
119 void tile_set_terrain(struct tile
*ptile
, struct terrain
*pterrain
)
121 /* The terrain change is valid if one of the following is TRUE:
122 * - pterrain is NULL (= unknown terrain)
123 * - ptile is a virtual tile
124 * - pterrain does not has the flag TER_NO_CITIES
125 * - there is no city on ptile.
126 * This should be read as: The terrain change is INVALID if a terrain with
127 * the flag TER_NO_CITIES is given for a real tile with a city (i.e. all
128 * check evaluate to TRUE). */
129 fc_assert_msg(NULL
== pterrain
130 || tile_virtual_check(ptile
)
131 || !terrain_has_flag(pterrain
, TER_NO_CITIES
)
132 || NULL
== tile_city(ptile
),
133 "At (%d, %d), the terrain \"%s\" (nb %d) doesn't "
134 "support cities, whereas \"%s\" (nb %d) is built there.",
135 TILE_XY(ptile
), terrain_rule_name(pterrain
),
136 terrain_number(pterrain
), city_name(tile_city(ptile
)),
137 tile_city(ptile
)->id
);
139 ptile
->terrain
= pterrain
;
141 && NULL
!= ptile
->resource
142 && terrain_has_resource(pterrain
, ptile
->resource
)) {
143 /* cannot use set_special() for internal values */
144 BV_SET(ptile
->special
, S_RESOURCE_VALID
);
146 BV_CLR(ptile
->special
, S_RESOURCE_VALID
);
150 /****************************************************************************
151 Return the specials of the tile. See terrain.h.
153 Note that this returns a mask of _all_ the specials on the tile. To
154 check a specific special use tile_has_special.
155 ****************************************************************************/
156 bv_special
tile_specials(const struct tile
*ptile
)
158 return ptile
->special
;
161 /****************************************************************************
162 Sets the tile's specials to those present in the given bit vector.
163 ****************************************************************************/
164 void tile_set_specials(struct tile
*ptile
, bv_special specials
)
169 ptile
->special
= specials
;
172 /****************************************************************************
173 Returns TRUE iff the given tile has the given special.
174 ****************************************************************************/
175 bool tile_has_special(const struct tile
*ptile
,
176 enum tile_special_type special
)
178 return contains_special(ptile
->special
, special
);
181 /****************************************************************************
182 Returns TRUE iff the given tile has any specials.
183 ****************************************************************************/
184 bool tile_has_any_specials(const struct tile
*ptile
)
186 return contains_any_specials(ptile
->special
);
189 /****************************************************************************
190 Returns a bit vector of the bases present at the tile.
191 ****************************************************************************/
192 bv_bases
tile_bases(const struct tile
*ptile
)
202 /****************************************************************************
203 Returns a bit vector of the roads present at the tile.
204 ****************************************************************************/
205 bv_roads
tile_roads(const struct tile
*ptile
)
216 /****************************************************************************
217 Set the bases on the tile to those present in the given bit vector.
218 ****************************************************************************/
219 void tile_set_bases(struct tile
*ptile
, bv_bases bases
)
224 ptile
->bases
= bases
;
227 /****************************************************************************
229 FIXME: Should remove conflicting old base and return bool indicating that.
230 ****************************************************************************/
231 void tile_add_base(struct tile
*ptile
, const struct base_type
*pbase
)
233 BV_SET(ptile
->bases
, base_index(pbase
));
236 /****************************************************************************
237 Removes base from tile if such exist
238 ****************************************************************************/
239 void tile_remove_base(struct tile
*ptile
, const struct base_type
*pbase
)
241 BV_CLR(ptile
->bases
, base_index(pbase
));
244 /****************************************************************************
245 Check if tile contains base providing effect
246 ****************************************************************************/
247 bool tile_has_base_flag(const struct tile
*ptile
, enum base_flag_id flag
)
249 base_type_iterate(pbase
) {
250 if (tile_has_base(ptile
, pbase
) && base_has_flag(pbase
, flag
)) {
253 } base_type_iterate_end
;
258 /****************************************************************************
259 Check if tile contains base providing effect for unit
260 ****************************************************************************/
261 bool tile_has_base_flag_for_unit(const struct tile
*ptile
,
262 const struct unit_type
*punittype
,
263 enum base_flag_id flag
)
265 base_type_iterate(pbase
) {
266 if (tile_has_base(ptile
, pbase
)
267 && base_has_flag_for_utype(pbase
, flag
, punittype
)) {
270 } base_type_iterate_end
;
275 /****************************************************************************
276 Check if tile contains base providing effect for unit
277 ****************************************************************************/
278 bool tile_has_claimable_base(const struct tile
*ptile
,
279 const struct unit_type
*punittype
)
281 base_type_iterate(pbase
) {
282 if (tile_has_base(ptile
, pbase
)
283 && territory_claiming_base(pbase
)
284 && is_native_base_to_uclass(pbase
, utype_class(punittype
))) {
287 } base_type_iterate_end
;
292 /****************************************************************************
293 Calculate defense bonus given for unit type by bases and roads
294 ****************************************************************************/
295 int tile_extras_defense_bonus(const struct tile
*ptile
,
296 const struct unit_type
*punittype
)
298 return tile_extras_class_defense_bonus(ptile
, utype_class(punittype
));
301 /****************************************************************************
302 Calculate defense bonus given for unit class by bases and roads
303 ****************************************************************************/
304 int tile_extras_class_defense_bonus(const struct tile
*ptile
,
305 const struct unit_class
*pclass
)
309 base_type_iterate(pbase
) {
310 if (tile_has_base(ptile
, pbase
)
311 && is_native_base_to_uclass(pbase
, pclass
)) {
312 bonus
+= pbase
->defense_bonus
;
314 } base_type_iterate_end
;
316 road_type_iterate(proad
) {
317 if (tile_has_road(ptile
, proad
)
318 && is_native_road_to_uclass(proad
, pclass
)) {
319 bonus
+= proad
->defense_bonus
;
321 } road_type_iterate_end
;
326 /****************************************************************************
327 Calculate output increment given by roads
328 ****************************************************************************/
329 int tile_roads_output_incr(const struct tile
*ptile
, enum output_type_id o
)
334 road_type_iterate(proad
) {
335 if (tile_has_road(ptile
, proad
)) {
336 const_incr
+= proad
->tile_incr_const
[o
];
337 incr
+= proad
->tile_incr
[o
];
339 } road_type_iterate_end
;
341 return const_incr
+ incr
* tile_terrain(ptile
)->road_output_incr_pct
[o
] / 100;
344 /****************************************************************************
345 Calculate output bonus given by roads
346 ****************************************************************************/
347 int tile_roads_output_bonus(const struct tile
*ptile
, enum output_type_id o
)
351 road_type_iterate(proad
) {
352 if (tile_has_road(ptile
, proad
)) {
353 bonus
+= proad
->tile_bonus
[o
];
355 } road_type_iterate_end
;
360 /****************************************************************************
361 Check if tile contains base native for unit
362 ****************************************************************************/
363 bool tile_has_native_base(const struct tile
*ptile
,
364 const struct unit_type
*punittype
)
366 base_type_iterate(pbase
) {
367 if (tile_has_base(ptile
, pbase
)
368 && is_native_base_to_utype(pbase
, punittype
)) {
371 } base_type_iterate_end
;
376 /****************************************************************************
377 Add the given special or specials to the tile.
379 Note that this does not erase any existing specials already on the tile
380 (see tile_clear_special or tile_clear_all_specials for that). Also note
381 the special to be set may be a mask, so you can set more than one
382 special at a time (but this is not recommended).
383 ****************************************************************************/
384 void tile_set_special(struct tile
*ptile
, enum tile_special_type spe
)
386 set_special(&ptile
->special
, spe
);
389 #ifndef tile_resource
390 /****************************************************************************
391 Return the resource at the specified tile.
392 ****************************************************************************/
393 const struct resource
*tile_resource(const struct tile
*ptile
)
395 return ptile
->resource
;
399 /****************************************************************************
400 Set the given resource at the specified tile.
401 ****************************************************************************/
402 void tile_set_resource(struct tile
*ptile
, struct resource
*presource
)
404 ptile
->resource
= presource
;
405 if (NULL
!= ptile
->terrain
407 && terrain_has_resource(ptile
->terrain
, presource
)) {
408 /* cannot use set_special() for internal values */
409 BV_SET(ptile
->special
, S_RESOURCE_VALID
);
411 BV_CLR(ptile
->special
, S_RESOURCE_VALID
);
415 /****************************************************************************
416 Clear the given special or specials from the tile.
418 This function clears all the specials set in the 'spe' mask from the
419 tile's set of specials. All other specials are unaffected.
420 ****************************************************************************/
421 void tile_clear_special(struct tile
*ptile
, enum tile_special_type spe
)
423 clear_special(&ptile
->special
, spe
);
426 /****************************************************************************
427 Remove any and all specials from this tile.
428 ****************************************************************************/
429 void tile_clear_all_specials(struct tile
*ptile
)
431 clear_all_specials(&ptile
->special
);
434 #ifndef tile_continent
435 /****************************************************************************
436 Return the continent ID of the tile. Typically land has a positive
437 continent number and ocean has a negative number; no tile should have
438 a 0 continent number.
439 ****************************************************************************/
440 Continent_id
tile_continent(const struct tile
*ptile
)
442 return ptile
->continent
;
446 /****************************************************************************
447 Set the continent ID of the tile. See tile_continent.
448 ****************************************************************************/
449 void tile_set_continent(struct tile
*ptile
, Continent_id val
)
451 ptile
->continent
= val
;
454 /****************************************************************************
455 Return a known_type enumeration value for the tile.
457 Note that the client only has known data about its own player.
458 ****************************************************************************/
459 enum known_type
tile_get_known(const struct tile
*ptile
,
460 const struct player
*pplayer
)
462 if (!dbv_isset(&pplayer
->tile_known
, tile_index(ptile
))) {
464 } else if (!fc_funcs
->player_tile_vision_get(ptile
, pplayer
, V_MAIN
)) {
465 return TILE_KNOWN_UNSEEN
;
467 return TILE_KNOWN_SEEN
;
471 /****************************************************************************
472 Time to complete the given activity on the given tile.
473 ****************************************************************************/
474 int tile_activity_time(enum unit_activity activity
, const struct tile
*ptile
)
476 struct terrain
*pterrain
= tile_terrain(ptile
);
478 /* Make sure nobody uses old activities */
479 fc_assert_ret_val(activity
!= ACTIVITY_FORTRESS
480 && activity
!= ACTIVITY_AIRBASE
, FC_INFINITY
);
482 /* ACTIVITY_BASE not handled here */
483 fc_assert_ret_val(activity
!= ACTIVITY_BASE
, FC_INFINITY
);
484 fc_assert_ret_val(activity
!= ACTIVITY_GEN_ROAD
, FC_INFINITY
);
487 case ACTIVITY_POLLUTION
:
488 return pterrain
->clean_pollution_time
* ACTIVITY_FACTOR
;
490 return pterrain
->mining_time
* ACTIVITY_FACTOR
;
491 case ACTIVITY_IRRIGATE
:
492 return pterrain
->irrigation_time
* ACTIVITY_FACTOR
;
493 case ACTIVITY_TRANSFORM
:
494 return pterrain
->transform_time
* ACTIVITY_FACTOR
;
495 case ACTIVITY_FALLOUT
:
496 return pterrain
->clean_fallout_time
* ACTIVITY_FACTOR
;
502 /****************************************************************************
503 Time to complete the base building activity on the given tile.
504 ****************************************************************************/
505 int tile_activity_base_time(const struct tile
*ptile
,
508 struct terrain
*pterrain
= tile_terrain(ptile
);
510 return terrain_base_time(pterrain
, base
) * ACTIVITY_FACTOR
;
513 /****************************************************************************
514 Time to complete the road building activity on the given tile.
515 ****************************************************************************/
516 int tile_activity_road_time(const struct tile
*ptile
,
519 struct terrain
*pterrain
= tile_terrain(ptile
);
521 return terrain_road_time(pterrain
, road
) * ACTIVITY_FACTOR
;
524 /****************************************************************************
525 Clear all "dirtiness" (pollution and fallout) from the tile.
526 ****************************************************************************/
527 static void tile_clear_dirtiness(struct tile
*ptile
)
529 tile_clear_special(ptile
, S_POLLUTION
);
530 tile_clear_special(ptile
, S_FALLOUT
);
533 /****************************************************************************
534 Change the terrain to the given type. This does secondary tile updates to
535 the tile (as will happen when mining/irrigation/transforming changes the
537 ****************************************************************************/
538 void tile_change_terrain(struct tile
*ptile
, struct terrain
*pterrain
)
540 tile_set_terrain(ptile
, pterrain
);
542 if (is_ocean(pterrain
)) {
543 /* The code can't handle these specials in ocean. */
544 tile_clear_special(ptile
, S_HUT
);
547 if (terrain_has_flag(pterrain
, TER_NO_POLLUTION
)) {
548 tile_clear_dirtiness(ptile
);
551 /* Clear mining/irrigation if resulting terrain type cannot support
553 if (pterrain
->mining_result
!= pterrain
) {
554 tile_clear_special(ptile
, S_MINE
);
557 if (pterrain
->irrigation_result
!= pterrain
) {
558 tile_clear_special(ptile
, S_IRRIGATION
);
559 tile_clear_special(ptile
, S_FARMLAND
);
562 /* Clear unsupported bases. */
563 base_type_iterate(pbase
) {
564 if (tile_has_base(ptile
, pbase
)
565 && !is_native_tile_to_base(pbase
, ptile
)) {
566 if (fc_funcs
->destroy_base
!= NULL
) {
567 /* Assume callback calls tile_remove_base() itself. */
568 fc_funcs
->destroy_base(ptile
, pbase
);
570 tile_remove_base(ptile
, pbase
);
573 } base_type_iterate_end
;
575 road_type_iterate(proad
) {
576 if (tile_has_road(ptile
, proad
)
577 && !is_native_tile_to_road(proad
, ptile
)) {
578 tile_remove_road(ptile
, proad
);
580 } road_type_iterate_end
;
583 /****************************************************************************
584 Add the special to the tile. This does secondary tile updates to
586 ****************************************************************************/
587 void tile_add_special(struct tile
*ptile
, enum tile_special_type special
)
589 fc_assert_ret(special
!= S_OLD_FORTRESS
&& special
!= S_OLD_AIRBASE
);
590 fc_assert_ret(special
!= S_OLD_ROAD
&& special
!= S_OLD_RAILROAD
&& special
!= S_OLD_RIVER
);
592 tile_set_special(ptile
, special
);
596 tile_add_special(ptile
, S_IRRIGATION
);
597 /* Fall through to irrigation */
599 tile_clear_special(ptile
, S_MINE
);
602 tile_clear_special(ptile
, S_IRRIGATION
);
603 tile_clear_special(ptile
, S_FARMLAND
);
614 /****************************************************************************
615 Remove the special from the tile. This does secondary tile updates to
617 ****************************************************************************/
618 void tile_remove_special(struct tile
*ptile
, enum tile_special_type special
)
620 fc_assert_ret(special
!= S_OLD_FORTRESS
&& special
!= S_OLD_AIRBASE
);
621 fc_assert_ret(special
!= S_OLD_ROAD
&& special
!= S_OLD_RAILROAD
&& special
!= S_OLD_RIVER
);
623 tile_clear_special(ptile
, special
);
627 tile_clear_special(ptile
, S_FARMLAND
);
640 /****************************************************************************
641 Build irrigation on the tile. This may change the specials of the tile
642 or change the terrain type itself.
643 ****************************************************************************/
644 static void tile_irrigate(struct tile
*ptile
)
646 struct terrain
*pterrain
= tile_terrain(ptile
);
648 if (pterrain
== pterrain
->irrigation_result
) {
649 if (tile_has_special(ptile
, S_IRRIGATION
)) {
650 tile_add_special(ptile
, S_FARMLAND
);
652 tile_add_special(ptile
, S_IRRIGATION
);
654 } else if (pterrain
->irrigation_result
) {
655 tile_change_terrain(ptile
, pterrain
->irrigation_result
);
659 /****************************************************************************
660 Build a mine on the tile. This may change the specials of the tile
661 or change the terrain type itself.
662 ****************************************************************************/
663 static void tile_mine(struct tile
*ptile
)
665 struct terrain
*pterrain
= tile_terrain(ptile
);
667 if (pterrain
== pterrain
->mining_result
) {
668 tile_set_special(ptile
, S_MINE
);
669 tile_clear_special(ptile
, S_FARMLAND
);
670 tile_clear_special(ptile
, S_IRRIGATION
);
671 } else if (pterrain
->mining_result
) {
672 tile_change_terrain(ptile
, pterrain
->mining_result
);
676 /****************************************************************************
677 Transform (ACTIVITY_TRANSFORM) the tile. This usually changes the tile's
679 ****************************************************************************/
680 static void tile_transform(struct tile
*ptile
)
682 struct terrain
*pterrain
= tile_terrain(ptile
);
684 if (pterrain
->transform_result
!= T_NONE
) {
685 tile_change_terrain(ptile
, pterrain
->transform_result
);
689 /****************************************************************************
690 Apply an activity (Activity_type_id, e.g., ACTIVITY_TRANSFORM) to a tile.
691 Return false if there was a error or if the activity is not implemented
693 ****************************************************************************/
694 bool tile_apply_activity(struct tile
*ptile
, Activity_type_id act
)
696 /* FIXME: for irrigate, mine, and transform we always return TRUE
697 * even if the activity fails. */
699 case ACTIVITY_POLLUTION
:
700 case ACTIVITY_FALLOUT
:
701 tile_clear_dirtiness(ptile
);
708 case ACTIVITY_IRRIGATE
:
709 tile_irrigate(ptile
);
712 case ACTIVITY_TRANSFORM
:
713 tile_transform(ptile
);
716 case ACTIVITY_OLD_ROAD
:
717 case ACTIVITY_OLD_RAILROAD
:
718 case ACTIVITY_FORTRESS
:
719 case ACTIVITY_AIRBASE
:
723 case ACTIVITY_PILLAGE
:
725 case ACTIVITY_GEN_ROAD
:
726 /* do nothing - not implemented */
730 case ACTIVITY_FORTIFIED
:
731 case ACTIVITY_SENTRY
:
733 case ACTIVITY_EXPLORE
:
734 case ACTIVITY_CONVERT
:
735 case ACTIVITY_UNKNOWN
:
736 case ACTIVITY_FORTIFYING
:
737 case ACTIVITY_PATROL_UNUSED
:
739 /* do nothing - these activities have no effect
740 on terrain type or tile specials */
747 /****************************************************************************
748 Add one entry about pollution situation to buffer.
749 Return if there has been any pollution (even prior calling this)
750 ****************************************************************************/
751 static bool tile_info_pollution(char *buf
, int bufsz
,
752 const struct tile
*ptile
,
753 enum tile_special_type special
,
754 bool prevp
, bool linebreak
)
756 if (tile_has_special(ptile
, special
)) {
759 fc_strlcat(buf
, "\n[", bufsz
);
761 fc_strlcat(buf
, " [", bufsz
);
764 fc_strlcat(buf
, "/", bufsz
);
767 fc_strlcat(buf
, special_name_translation(special
), bufsz
);
775 /****************************************************************************
776 Return a (static) string with tile name describing terrain and specials.
777 If include_nuisances is set, pollution and nuclear fallout will be
783 "Hills (Coals) [Pollution]"
784 ****************************************************************************/
785 const char *tile_get_info_text(const struct tile
*ptile
,
786 bool include_nuisances
, int linebreaks
)
791 int bufsz
= sizeof(s
);
793 sz_strlcpy(s
, terrain_name_translation(tile_terrain(ptile
)));
794 if (linebreaks
& TILE_LB_TERRAIN_RIVER
) {
795 /* Linebreak needed before next text */
799 road_type_iterate(proad
) {
800 if (tile_has_road(ptile
, proad
)
801 && road_has_flag(proad
, RF_NATURAL
)) {
808 sz_strlcat(s
, road_name_translation(proad
));
810 } road_type_iterate_end
;
811 if (linebreaks
& TILE_LB_RIVER_RESOURCE
) {
812 /* New linebreak requested */
816 if (tile_resource_is_valid(ptile
)) {
823 cat_snprintf(s
, sizeof(s
), "(%s)",
824 resource_name_translation(ptile
->resource
));
826 if (linebreaks
& TILE_LB_RESOURCE_POLL
) {
827 /* New linebreak requested */
831 if (include_nuisances
) {
833 pollution
= tile_info_pollution(s
, bufsz
, ptile
, S_POLLUTION
, pollution
,
835 pollution
= tile_info_pollution(s
, bufsz
, ptile
, S_FALLOUT
, pollution
,
845 /****************************************************************************
846 Returns TRUE if the given tile has a base of given type on it.
847 ****************************************************************************/
848 bool tile_has_base(const struct tile
*ptile
, const struct base_type
*pbase
)
850 return BV_ISSET(ptile
->bases
, base_index(pbase
));
853 /****************************************************************************
854 Returns TRUE if the given tile has a base conflicting with the given one.
855 ****************************************************************************/
856 bool tile_has_conflicting_base(const struct tile
*ptile
, const struct base_type
*pbase
)
858 base_type_iterate(pconfl
) {
859 if (BV_ISSET(pbase
->conflicts
, base_index(pconfl
))
860 && tile_has_base(ptile
, pconfl
)) {
863 } base_type_iterate_end
;
868 /****************************************************************************
869 Returns TRUE if the given tile has any bases on it.
870 ****************************************************************************/
871 bool tile_has_any_bases(const struct tile
*ptile
)
876 return BV_ISSET_ANY(ptile
->bases
);
879 /****************************************************************************
880 Returns TRUE if the given tile has a road of given type on it.
881 ****************************************************************************/
882 bool tile_has_road(const struct tile
*ptile
, const struct road_type
*proad
)
884 return BV_ISSET(ptile
->roads
, road_index(proad
));
887 /****************************************************************************
888 Tile has any river type
889 ****************************************************************************/
890 bool tile_has_river(const struct tile
*ptile
)
892 road_type_iterate(priver
) {
893 if (tile_has_road(ptile
, priver
)
894 && road_has_flag(priver
, RF_RIVER
)) {
897 } road_type_iterate_end
;
902 /****************************************************************************
904 ****************************************************************************/
905 void tile_add_road(struct tile
*ptile
, const struct road_type
*proad
)
908 BV_SET(ptile
->roads
, road_index(proad
));
912 /****************************************************************************
913 Removes road from tile if such exist
914 ****************************************************************************/
915 void tile_remove_road(struct tile
*ptile
, const struct road_type
*proad
)
918 BV_CLR(ptile
->roads
, road_index(proad
));
922 /****************************************************************************
923 Returns a virtual tile. If ptile is given, the properties of this tile are
924 copied, else it is completely blank (except for the unit list
925 vtile->units, which is created for you). Be sure to call tile_virtual_free
926 on it when it is no longer needed.
927 ****************************************************************************/
928 struct tile
*tile_virtual_new(const struct tile
*ptile
)
932 vtile
= fc_calloc(1, sizeof(*vtile
));
934 /* initialise some values */
936 vtile
->continent
= -1;
938 BV_CLR_ALL(vtile
->special
);
939 BV_CLR_ALL(vtile
->bases
);
940 BV_CLR_ALL(vtile
->roads
);
941 vtile
->resource
= NULL
;
942 vtile
->terrain
= NULL
;
943 vtile
->units
= unit_list_new();
944 vtile
->worked
= NULL
;
946 vtile
->claimer
= NULL
;
947 vtile
->spec_sprite
= NULL
;
950 /* Used by is_city_center to give virtual tiles the output bonuses
952 vtile
->index
= tile_index(ptile
);
954 /* Copy all but the unit list. */
955 tile_special_type_iterate(spe
) {
956 if (BV_ISSET(ptile
->special
, spe
)) {
957 BV_SET(vtile
->special
, spe
);
959 } tile_special_type_iterate_end
;
961 if (BV_ISSET(ptile
->special
, S_RESOURCE_VALID
)) {
962 BV_SET(vtile
->special
, S_RESOURCE_VALID
);
965 base_type_iterate(pbase
) {
966 if (BV_ISSET(ptile
->bases
, base_number(pbase
))) {
967 BV_SET(vtile
->bases
, base_number(pbase
));
969 } base_type_iterate_end
;
971 road_type_iterate(proad
) {
972 if (tile_has_road(ptile
, proad
)) {
973 tile_add_road(vtile
, proad
);
975 } road_type_iterate_end
;
977 vtile
->resource
= ptile
->resource
;
978 vtile
->terrain
= ptile
->terrain
;
979 vtile
->worked
= ptile
->worked
;
980 vtile
->owner
= ptile
->owner
;
981 vtile
->claimer
= ptile
->claimer
;
982 vtile
->spec_sprite
= NULL
;
988 /****************************************************************************
989 Frees all memory used by the virtual tile, including freeing virtual
990 units in the tile's unit list and the virtual city on this tile if one
993 NB: Do not call this on real tiles!
994 ****************************************************************************/
995 void tile_virtual_destroy(struct tile
*vtile
)
1004 unit_list_iterate(vtile
->units
, vunit
) {
1005 if (unit_is_virtual(vunit
)) {
1006 unit_virtual_destroy(vunit
);
1008 } unit_list_iterate_end
;
1009 unit_list_destroy(vtile
->units
);
1010 vtile
->units
= NULL
;
1013 vcity
= tile_city(vtile
);
1015 if (city_is_virtual(vcity
)) {
1016 destroy_city_virtual(vcity
);
1018 tile_set_worked(vtile
, NULL
);
1024 /****************************************************************************
1025 Check if the given tile is a virtual one or not.
1026 ****************************************************************************/
1027 bool tile_virtual_check(struct tile
*vtile
)
1031 if (!vtile
|| map_is_empty()) {
1035 tindex
= tile_index(vtile
);
1036 fc_assert_ret_val(0 <= tindex
&& tindex
< map_num_tiles(), FALSE
);
1038 return (vtile
!= map
.tiles
+ tindex
);
1041 /****************************************************************************
1042 Returns key that should be used when storing tile to hash or when
1043 retrieving it from there.
1044 ****************************************************************************/
1045 void *tile_hash_key(const struct tile
*ptile
)
1047 void *key
= 0; /* Initialize whole sizeof(void *) */
1049 key
= FC_INT_TO_PTR(ptile
->index
);
1054 /****************************************************************************
1055 Sets label for tile. Returns whether label changed.
1056 ****************************************************************************/
1057 bool tile_set_label(struct tile
*ptile
, const char *label
)
1059 bool changed
= FALSE
;
1061 /* Handle empty label as NULL label */
1062 if (label
!= NULL
&& label
[0] == '\0') {
1066 if (ptile
->label
!= NULL
) {
1067 if (label
== NULL
) {
1069 } else if (strcmp(ptile
->label
, label
)) {
1072 FC_FREE(ptile
->label
);
1073 ptile
->label
= NULL
;
1074 } else if (label
!= NULL
) {
1078 if (label
!= NULL
) {
1079 ptile
->label
= fc_strdup(label
);