Freeciv21
Develop your civilization from humble roots to a global empire
tile.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file
3  is part of Freeciv21. Freeciv21 is free software:
4 |\_/|,,_____,~~` you can redistribute it and/or modify it under the
5 (.".)~~ )`~}} terms of the GNU General Public License as published
6  \o/\ /---~\\ ~}} by the Free Software Foundation, either version 3 of
7  _// _// ~} the License, or (at your option) any later version.
8  You should have received a copy of the GNU General
9  Public License along with Freeciv21. If not, see
10  https://www.gnu.org/licenses/.
11  */
12 
13 #include <QBitArray>
14 // utility
15 #include "bitvector.h"
16 #include "log.h"
17 #include "support.h"
18 
19 // common
20 #include "fc_interface.h"
21 #include "game.h"
22 #include "map.h"
23 #include "road.h"
24 #include "unit.h"
25 #include "unitlist.h"
26 
27 #include "tile.h"
28 
29 static bv_extras empty_extras;
30 
31 #ifndef tile_index
35 int tile_index(const struct tile *ptile) { return ptile->index; }
36 #endif
37 
38 #ifndef tile_owner
42 struct player *tile_owner(const struct tile *ptile) { return ptile->owner; }
43 #endif
44 
45 #ifndef tile_claimer
49 struct tile *tile_claimer(const struct tile *ptile)
50 {
51  return ptile->claimer;
52 }
53 #endif
54 
58 void tile_set_owner(struct tile *ptile, struct player *pplayer,
59  struct tile *claimer)
60 {
61  if (BORDERS_DISABLED != game.info.borders
62  // City tiles are always owned by the city owner.
63  || (tile_city(ptile) != nullptr || ptile->owner != nullptr)) {
64  ptile->owner = pplayer;
65  ptile->claimer = claimer;
66  }
67 }
68 
72 struct city *tile_city(const struct tile *ptile)
73 {
74  if (!ptile) {
75  return nullptr;
76  }
77 
78  struct city *pcity = ptile->worked;
79 
80  if (nullptr != pcity && is_city_center(pcity, ptile)) {
81  return pcity;
82  }
83  return nullptr;
84 }
85 
86 #ifndef tile_worked
90 struct city *tile_worked(const struct tile *ptile) { return ptile->worked; }
91 #endif
92 
96 void tile_set_worked(struct tile *ptile, struct city *pcity)
97 {
98  ptile->worked = pcity;
99 }
100 
101 #ifndef tile_terrain
105 struct terrain *tile_terrain(const struct tile *ptile)
106 {
107  return ptile->terrain;
108 }
109 #endif
110 
114 void tile_set_terrain(struct tile *ptile, struct terrain *pterrain)
115 {
116  /* The terrain change is valid if one of the following is TRUE:
117  * - pterrain is nullptr (= unknown terrain)
118  * - ptile is a virtual tile
119  * - pterrain does not has the flag TER_NO_CITIES
120  * - there is no city on ptile
121  * - client may have had tile fogged and is receiving terrain change before
122  * city removal
123  * This should be read as: The terrain change is INVALID if a terrain with
124  * the flag TER_NO_CITIES is given for a real tile with a city (i.e. all
125  * check evaluate to TRUE). */
127  nullptr == pterrain || !is_server() || tile_virtual_check(ptile)
128  || !terrain_has_flag(pterrain, TER_NO_CITIES)
129  || nullptr == tile_city(ptile),
130  "At (%d, %d), the terrain \"%s\" (nb %d) doesn't "
131  "support cities, whereas \"%s\" (nb %d) is built there.",
132  TILE_XY(ptile), terrain_rule_name(pterrain), terrain_number(pterrain),
133  city_name_get(tile_city(ptile)), tile_city(ptile)->id);
134 
135  ptile->terrain = pterrain;
136  if (ptile->resource != nullptr) {
137  if (nullptr != pterrain
138  && terrain_has_resource(pterrain, ptile->resource)) {
139  BV_SET(ptile->extras, extra_index(ptile->resource));
140  } else {
141  BV_CLR(ptile->extras, extra_index(ptile->resource));
142  }
143  }
144 }
145 
149 const bv_extras *tile_extras_null()
150 {
151  static bool empty_cleared = false;
152 
153  if (!empty_cleared) {
155  empty_cleared = true;
156  }
157 
158  return &(empty_extras);
159 }
160 
164 bool tile_has_base_flag(const struct tile *ptile, enum base_flag_id flag)
165 {
166  extra_type_by_cause_iterate(EC_BASE, pextra)
167  {
168  struct base_type *pbase = extra_base_get(pextra);
169 
170  if (tile_has_extra(ptile, pextra) && base_has_flag(pbase, flag)) {
171  return true;
172  }
173  }
175 
176  return false;
177 }
178 
182 bool tile_has_base_flag_for_unit(const struct tile *ptile,
183  const struct unit_type *punittype,
184  enum base_flag_id flag)
185 {
186  extra_type_by_cause_iterate(EC_BASE, pextra)
187  {
188  struct base_type *pbase = extra_base_get(pextra);
189 
190  if (tile_has_extra(ptile, pextra)
191  && base_has_flag_for_utype(pbase, flag, punittype)) {
192  return true;
193  }
194  }
196 
197  return false;
198 }
199 
203 bool tile_has_claimable_base(const struct tile *ptile,
204  const struct unit_type *punittype)
205 {
206  extra_type_by_cause_iterate(EC_BASE, pextra)
207  {
208  struct base_type *pbase = extra_base_get(pextra);
209 
210  if (tile_has_extra(ptile, pextra) && territory_claiming_base(pbase)
211  && is_native_extra_to_uclass(pextra, utype_class(punittype))) {
212  return true;
213  }
214  }
216 
217  return false;
218 }
219 
223 int tile_extras_defense_bonus(const struct tile *ptile,
224  const struct unit_type *punittype)
225 {
226  return tile_extras_class_defense_bonus(ptile, utype_class(punittype));
227 }
228 
232 int tile_extras_class_defense_bonus(const struct tile *ptile,
233  const struct unit_class *pclass)
234 {
235  int natural_bonus = 0;
236  int fortification_bonus = 0;
237  int total_bonus;
238 
240  {
241  if (tile_has_extra(ptile, pextra)
242  && is_native_extra_to_uclass(pextra, pclass)) {
243  natural_bonus += pextra->defense_bonus;
244  }
245  }
247 
249  {
250  if (tile_has_extra(ptile, pextra)
251  && is_native_extra_to_uclass(pextra, pclass)) {
252  fortification_bonus += pextra->defense_bonus;
253  }
254  }
256 
257  total_bonus =
258  (100 + natural_bonus) * (100 + fortification_bonus) / 100 - 100;
259 
260  return total_bonus;
261 }
262 
266 int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
267 {
268  int const_incr = 0;
269  int incr = 0;
270 
271  extra_type_by_cause_iterate(EC_ROAD, pextra)
272  {
273  if (tile_has_extra(ptile, pextra)) {
274  struct road_type *proad = extra_road_get(pextra);
275 
276  const_incr += proad->tile_incr_const[o];
277  incr += proad->tile_incr[o];
278  }
279  }
281 
282  return const_incr
283  + incr * tile_terrain(ptile)->road_output_incr_pct[o] / 100;
284 }
285 
289 int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
290 {
291  int bonus = 0;
292 
293  extra_type_by_cause_iterate(EC_ROAD, pextra)
294  {
295  if (tile_has_extra(ptile, pextra)) {
296  struct road_type *proad = extra_road_get(pextra);
297 
298  bonus += proad->tile_bonus[o];
299  }
300  }
302 
303  return bonus;
304 }
305 
309 bool tile_has_refuel_extra(const struct tile *ptile,
310  const struct unit_type *punittype)
311 {
312  extra_type_iterate(pextra)
313  {
314  if (tile_has_extra(ptile, pextra) && extra_has_flag(pextra, EF_REFUEL)
315  && is_native_extra_to_utype(pextra, punittype)) {
316  return true;
317  }
318  }
320 
321  return false;
322 }
323 
327 bool tile_has_native_base(const struct tile *ptile,
328  const struct unit_type *punittype)
329 {
330  extra_type_by_cause_iterate(EC_BASE, pextra)
331  {
332  if (tile_has_extra(ptile, pextra)
333  && is_native_extra_to_utype(pextra, punittype)) {
334  return true;
335  }
336  }
338 
339  return false;
340 }
341 
342 #ifndef tile_resource
346 const struct resource_type *tile_resource(const struct tile *ptile)
347 {
348  return ptile->resource;
349 }
350 #endif
351 
355 void tile_set_resource(struct tile *ptile, struct extra_type *presource)
356 {
357  if (presource == ptile->resource) {
358  return; // No change
359  }
360 
361  if (ptile->resource != nullptr) {
362  tile_remove_extra(ptile, ptile->resource);
363  }
364  if (presource != nullptr) {
365  if (ptile->terrain && terrain_has_resource(ptile->terrain, presource)) {
366  tile_add_extra(ptile, presource);
367  }
368  }
369 
370  ptile->resource = presource;
371 }
372 
373 #ifndef tile_continent
379 Continent_id tile_continent(const struct tile *ptile)
380 {
381  return ptile->continent;
382 }
383 #endif
384 
388 void tile_set_continent(struct tile *ptile, Continent_id val)
389 {
390  ptile->continent = val;
391 }
392 
398 enum known_type tile_get_known(const struct tile *ptile,
399  const struct player *pplayer)
400 {
401  if (tile_virtual_check(ptile)) {
402  return TILE_KNOWN_SEEN;
403  }
404  if (!pplayer->tile_known->at(tile_index(ptile))) {
405  return TILE_UNKNOWN;
406  } else if (!fc_funcs->player_tile_vision_get(ptile, pplayer, V_MAIN)) {
407  return TILE_KNOWN_UNSEEN;
408  } else {
409  return TILE_KNOWN_SEEN;
410  }
411 }
412 
416 bool tile_is_seen(const struct tile *target_tile,
417  const struct player *pow_player)
418 {
419  return tile_get_known(target_tile, pow_player) == TILE_KNOWN_SEEN;
420 }
421 
427 int tile_activity_time(enum unit_activity activity, const struct tile *ptile,
428  const struct extra_type *tgt)
429 {
430  struct terrain *pterrain = tile_terrain(ptile);
431 
432  // Make sure nobody uses old activities
433  fc_assert_ret_val(activity != ACTIVITY_FORTRESS
434  && activity != ACTIVITY_AIRBASE,
435  FC_INFINITY);
436 
437  switch (activity) {
438  case ACTIVITY_POLLUTION:
439  case ACTIVITY_FALLOUT:
440  case ACTIVITY_PILLAGE:
441  return terrain_extra_removal_time(pterrain, activity, tgt)
442  * ACTIVITY_FACTOR;
443  case ACTIVITY_TRANSFORM:
444  return pterrain->transform_time * ACTIVITY_FACTOR;
445  case ACTIVITY_CULTIVATE:
446  return pterrain->cultivate_time * ACTIVITY_FACTOR;
447  case ACTIVITY_PLANT:
448  return pterrain->plant_time * ACTIVITY_FACTOR;
449  case ACTIVITY_IRRIGATE:
450  case ACTIVITY_MINE:
451  case ACTIVITY_BASE:
452  case ACTIVITY_GEN_ROAD:
453  return terrain_extra_build_time(pterrain, activity, tgt)
454  * ACTIVITY_FACTOR;
455  default:
456  return 0;
457  }
458 }
459 
463 static void tile_create_extra(struct tile *ptile, const extra_type *pextra)
464 {
465  if (fc_funcs->create_extra != nullptr) {
466  // Assume callback calls tile_add_extra() itself.
467  fc_funcs->create_extra(ptile, pextra, nullptr);
468  } else {
469  tile_add_extra(ptile, pextra);
470  }
471 }
472 
476 static void tile_destroy_extra(struct tile *ptile, struct extra_type *pextra)
477 {
478  if (fc_funcs->destroy_extra != nullptr) {
479  // Assume callback calls tile_remove_extra() itself.
480  fc_funcs->destroy_extra(ptile, pextra);
481  } else {
482  tile_remove_extra(ptile, pextra);
483  }
484 }
485 
491 void tile_change_terrain(struct tile *ptile, struct terrain *pterrain)
492 {
493  tile_set_terrain(ptile, pterrain);
494 
495  // Remove unsupported extras
496  extra_type_iterate(pextra)
497  {
498  if (tile_has_extra(ptile, pextra)
499  && (!is_native_tile_to_extra(pextra, ptile)
500  || extra_has_flag(pextra, EF_TERR_CHANGE_REMOVES))) {
501  tile_destroy_extra(ptile, pextra);
502  }
503  }
505 }
506 
510 static bool add_recursive_extras(struct tile *ptile,
511  const extra_type *pextra, int rec)
512 {
513  if (rec > MAX_EXTRA_TYPES) {
514  // Infinite recursion
515  return false;
516  }
517 
518  // First place dependency extras
519  extra_deps_iterate(&(pextra->reqs), pdep)
520  {
521  if (!tile_has_extra(ptile, pdep)) {
522  add_recursive_extras(ptile, pdep, rec + 1);
523  }
524  }
526 
527  // Is tile native for extra after that?
528  if (!is_native_tile_to_extra(pextra, ptile)) {
529  return false;
530  }
531 
532  tile_create_extra(ptile, pextra);
533 
534  return true;
535 }
536 
540 static bool rm_recursive_extras(struct tile *ptile,
541  struct extra_type *pextra, int rec)
542 {
543  if (rec > MAX_EXTRA_TYPES) {
544  // Infinite recursion
545  return false;
546  }
547 
548  extra_type_iterate(pdepending)
549  {
550  if (tile_has_extra(ptile, pdepending)) {
551  extra_deps_iterate(&(pdepending->reqs), pdep)
552  {
553  if (pdep == pextra) {
554  // Depends on what we are going to remove
555  if (!rm_recursive_extras(ptile, pdepending, rec + 1)) {
556  return false;
557  }
558  }
559  }
561  }
562  }
564 
565  tile_destroy_extra(ptile, pextra);
566 
567  return true;
568 }
569 
578 bool tile_extra_apply(struct tile *ptile, const extra_type *tgt)
579 {
580  // Add extra with its dependencies
581  if (!add_recursive_extras(ptile, tgt, 0)) {
582  return false;
583  }
584 
585  // Remove conflicting extras
586  extra_type_iterate(pextra)
587  {
588  if (tile_has_extra(ptile, pextra) && !can_extras_coexist(pextra, tgt)) {
589  tile_destroy_extra(ptile, pextra);
590  }
591  }
593 
594  return true;
595 }
596 
605 bool tile_extra_rm_apply(struct tile *ptile, struct extra_type *tgt)
606 {
607  // Remove extra with everything depending on it.
608  return rm_recursive_extras(ptile, tgt, 0);
609 }
610 
615 static void tile_irrigate(struct tile *ptile, struct extra_type *tgt)
616 {
617  struct terrain *pterrain = tile_terrain(ptile);
618 
619  if (pterrain == pterrain->irrigation_result) {
620  /* Ideally activity should already been cancelled before nullptr tgt
621  * gets this far, but it's possible that terrain got changed from
622  * one that gets transformed by irrigation (-> nullptr tgt) to one
623  * that does not (-> nullptr tgt illegal) since legality of the action
624  * was last checked */
625  if (tgt != nullptr) {
626  tile_extra_apply(ptile, tgt);
627  }
628  } else if (pterrain->irrigation_result) {
629  tile_change_terrain(ptile, pterrain->irrigation_result);
630  }
631 }
632 
637 static void tile_mine(struct tile *ptile, struct extra_type *tgt)
638 {
639  struct terrain *pterrain = tile_terrain(ptile);
640 
641  if (pterrain == pterrain->mining_result) {
642  /* Ideally activity should already been cancelled before nullptr tgt
643  * gets this far, but it's possible that terrain got changed from
644  * one that gets transformed by mining (-> nullptr tgt) to one
645  * that does not (-> nullptr tgt illegal) since legality of the action
646  * was last checked */
647  if (tgt != nullptr) {
648  tile_extra_apply(ptile, tgt);
649  }
650  } else if (pterrain->mining_result) {
651  tile_change_terrain(ptile, pterrain->mining_result);
652  }
653 }
654 
659 static void tile_transform(struct tile *ptile)
660 {
661  struct terrain *pterrain = tile_terrain(ptile);
662 
663  if (pterrain->transform_result != T_NONE) {
664  tile_change_terrain(ptile, pterrain->transform_result);
665  }
666 }
667 
672 static void tile_plant(struct tile *ptile)
673 {
674  struct terrain *pterrain = tile_terrain(ptile);
675 
676  if (pterrain->mining_result != T_NONE
677  && pterrain->mining_result != pterrain) {
678  tile_change_terrain(ptile, pterrain->mining_result);
679  }
680 }
681 
686 static void tile_cultivate(struct tile *ptile)
687 {
688  struct terrain *pterrain = tile_terrain(ptile);
689 
690  if (pterrain->irrigation_result != T_NONE
691  && pterrain->irrigation_result != pterrain) {
692  tile_change_terrain(ptile, pterrain->irrigation_result);
693  }
694 }
695 
701 bool tile_apply_activity(struct tile *ptile, Activity_type_id act,
702  struct extra_type *tgt)
703 {
704  /* FIXME: for irrigate, mine, and transform we always return TRUE
705  * even if the activity fails. */
706  switch (act) {
707  case ACTIVITY_MINE:
708  tile_mine(ptile, tgt);
709  return true;
710 
711  case ACTIVITY_IRRIGATE:
712  tile_irrigate(ptile, tgt);
713  return true;
714 
715  case ACTIVITY_TRANSFORM:
716  tile_transform(ptile);
717  return true;
718 
719  case ACTIVITY_CULTIVATE:
720  tile_cultivate(ptile);
721  return true;
722 
723  case ACTIVITY_PLANT:
724  tile_plant(ptile);
725  return true;
726 
727  case ACTIVITY_OLD_ROAD:
728  case ACTIVITY_OLD_RAILROAD:
729  case ACTIVITY_FORTRESS:
730  case ACTIVITY_AIRBASE:
731  fc_assert(false);
732  return false;
733 
734  case ACTIVITY_PILLAGE:
735  case ACTIVITY_BASE:
736  case ACTIVITY_GEN_ROAD:
737  case ACTIVITY_POLLUTION:
738  case ACTIVITY_FALLOUT:
739  // do nothing - not implemented
740  return false;
741 
742  case ACTIVITY_IDLE:
743  case ACTIVITY_FORTIFIED:
744  case ACTIVITY_SENTRY:
745  case ACTIVITY_GOTO:
746  case ACTIVITY_EXPLORE:
747  case ACTIVITY_CONVERT:
748  case ACTIVITY_UNKNOWN:
749  case ACTIVITY_FORTIFYING:
750  case ACTIVITY_PATROL_UNUSED:
751  case ACTIVITY_LAST:
752  /* do nothing - these activities have no effect
753  on terrain type or tile extras */
754  return false;
755  }
756  fc_assert(false);
757  return false;
758 }
759 
764 static bool tile_info_pollution(char *buf, int bufsz,
765  const struct tile *ptile,
766  struct extra_type *pextra, bool prevp,
767  bool linebreak)
768 {
769  if (tile_has_visible_extra(ptile, pextra)) {
770  if (!prevp) {
771  if (linebreak) {
772  fc_strlcat(buf, "\n[", bufsz);
773  } else {
774  fc_strlcat(buf, " [", bufsz);
775  }
776  } else {
777  fc_strlcat(buf, "/", bufsz);
778  }
779 
780  fc_strlcat(buf, extra_name_translation(pextra), bufsz);
781 
782  return true;
783  }
784 
785  return prevp;
786 }
787 
799 const char *tile_get_info_text(const struct tile *ptile,
800  bool include_nuisances, int linebreaks)
801 {
802  static char s[256];
803  bool pollution;
804  bool lb = false;
805  int bufsz = sizeof(s);
806 
808  if (linebreaks & TILE_LB_TERRAIN_RIVER) {
809  // Linebreak needed before next text
810  lb = true;
811  }
812 
813  extra_type_iterate(pextra)
814  {
815  if (pextra->category == ECAT_NATURAL
816  && tile_has_visible_extra(ptile, pextra)) {
817  if (lb) {
818  sz_strlcat(s, "\n");
819  lb = false;
820  } else {
821  sz_strlcat(s, "/");
822  }
824  }
825  }
827  if (linebreaks & TILE_LB_RIVER_RESOURCE) {
828  // New linebreak requested
829  lb = true;
830  }
831 
832  if (tile_resource_is_valid(ptile)) {
833  if (lb) {
834  sz_strlcat(s, "\n");
835  lb = false;
836  } else {
837  sz_strlcat(s, " ");
838  }
839  cat_snprintf(s, sizeof(s), "(%s)",
841  }
842  if (linebreaks & TILE_LB_RESOURCE_POLL) {
843  // New linebreak requested
844  lb = true;
845  }
846 
847  if (include_nuisances) {
848  pollution = false;
849  extra_type_iterate(pextra)
850  {
851  if (pextra->category == ECAT_NUISANCE) {
852  pollution =
853  tile_info_pollution(s, bufsz, ptile, pextra, pollution, lb);
854  }
855  }
857  if (pollution) {
858  sz_strlcat(s, "]");
859  }
860  }
861 
862  return s;
863 }
864 
868 bool tile_has_road(const struct tile *ptile, const struct road_type *proad)
869 {
870  return tile_has_extra(ptile, road_extra_get(proad));
871 }
872 
876 bool tile_has_river(const struct tile *ptile)
877 {
878  // TODO: Have a list of rivers and iterate only that
879  extra_type_by_cause_iterate(EC_ROAD, priver)
880  {
881  if (tile_has_extra(ptile, priver)
882  && road_has_flag(extra_road_get(priver), RF_RIVER)) {
883  return true;
884  }
885  }
887 
888  return false;
889 }
890 
894 bool tile_has_road_flag(const struct tile *ptile, enum road_flag_id flag)
895 {
896  extra_type_by_cause_iterate(EC_ROAD, pextra)
897  {
898  if (tile_has_extra(ptile, pextra)) {
899  struct road_type *proad = extra_road_get(pextra);
900 
901  if (road_has_flag(proad, flag)) {
902  return true;
903  }
904  }
905  }
907 
908  return false;
909 }
910 
914 bool tile_has_extra_flag(const struct tile *ptile, enum extra_flag_id flag)
915 {
916  extra_type_iterate(pextra)
917  {
918  if (tile_has_extra(ptile, pextra) && extra_has_flag(pextra, flag)) {
919  return true;
920  }
921  }
923 
924  return false;
925 }
926 
930 bool tile_has_conflicting_extra(const struct tile *ptile,
931  const struct extra_type *pextra)
932 {
933  extra_type_iterate(pconfl)
934  {
935  if (BV_ISSET(pextra->conflicts, extra_index(pconfl))
936  && tile_has_extra(ptile, pconfl)) {
937  return true;
938  }
939  }
941 
942  return false;
943 }
944 
948 bool tile_has_visible_extra(const struct tile *ptile,
949  const struct extra_type *pextra)
950 {
951  bool hidden = false;
952 
953  if (!BV_ISSET(ptile->extras, extra_index(pextra))) {
954  return false;
955  }
956 
957  extra_type_iterate(top)
958  {
959  int topi = extra_index(top);
960 
961  if (BV_ISSET(pextra->hidden_by, topi) && BV_ISSET(ptile->extras, topi)) {
962  hidden = true;
963  break;
964  }
965  }
967 
968  return !hidden;
969 }
970 
974 void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
975 {
976  if (pextra != nullptr) {
977  BV_SET(ptile->extras, extra_index(pextra));
978  }
979 }
980 
984 void tile_remove_extra(struct tile *ptile, const struct extra_type *pextra)
985 {
986  if (pextra != nullptr) {
987  BV_CLR(ptile->extras, extra_index(pextra));
988  }
989 }
990 
997 struct tile *tile_virtual_new(const struct tile *ptile)
998 {
999  struct tile *vtile;
1000 
1001  vtile = new tile[1]();
1002 
1003  // initialise some values
1004  vtile->index = TILE_INDEX_NONE;
1005  vtile->continent = -1;
1006 
1007  BV_CLR_ALL(vtile->extras);
1008  vtile->resource = nullptr;
1009  vtile->terrain = nullptr;
1010  vtile->units = unit_list_new();
1011  vtile->worked = nullptr;
1012  vtile->owner = nullptr;
1013  vtile->placing = nullptr;
1014  vtile->extras_owner = nullptr;
1015  vtile->claimer = nullptr;
1016  vtile->spec_sprite = nullptr;
1017 
1018  if (ptile) {
1019  /* Used by is_city_center to give virtual tiles the output bonuses
1020  * they deserve. */
1021  vtile->index = tile_index(ptile);
1022 
1023  // Copy all but the unit list.
1024  extra_type_iterate(pextra)
1025  {
1026  if (BV_ISSET(ptile->extras, extra_number(pextra))) {
1027  BV_SET(vtile->extras, extra_number(pextra));
1028  }
1029  }
1031 
1032  vtile->resource = ptile->resource;
1033  vtile->terrain = ptile->terrain;
1034  vtile->worked = ptile->worked;
1035  vtile->owner = ptile->owner;
1036  vtile->extras_owner = ptile->extras_owner;
1037  vtile->claimer = ptile->claimer;
1038  vtile->spec_sprite = nullptr;
1039  }
1040 
1041  return vtile;
1042 }
1043 
1051 void tile_virtual_destroy(struct tile *vtile)
1052 {
1053  struct city *vcity;
1054 
1055  if (!vtile) {
1056  return;
1057  }
1058 
1059  if (vtile->units) {
1060  unit_list_iterate(vtile->units, vunit)
1061  {
1062  if (unit_is_virtual(vunit)) {
1063  unit_virtual_destroy(vunit);
1064  }
1065  }
1067  unit_list_destroy(vtile->units);
1068  vtile->units = nullptr;
1069  }
1070 
1071  vcity = tile_city(vtile);
1072  if (vcity) {
1073  if (city_is_virtual(vcity)) {
1074  destroy_city_virtual(vcity);
1075  }
1076  tile_set_worked(vtile, nullptr);
1077  }
1078 
1079  delete[] vtile;
1080 }
1081 
1085 bool tile_virtual_check(const tile *vtile)
1086 {
1087  int tindex;
1088 
1089  if (!vtile || map_is_empty()) {
1090  return false;
1091  } else if (tile_index(vtile) == TILE_INDEX_NONE) {
1092  return true;
1093  }
1094 
1095  tindex = tile_index(vtile);
1096  fc_assert_ret_val(0 <= tindex && tindex < map_num_tiles(), false);
1097 
1098  return (vtile != wld.map.tiles + tindex);
1099 }
1100 
1104 bool tile_set_label(struct tile *ptile, const char *label)
1105 {
1106  bool changed = false;
1107 
1108  // Handle empty label as nullptr label
1109  if (label != nullptr && label[0] == '\0') {
1110  label = nullptr;
1111  }
1112 
1113  if (ptile->label != nullptr) {
1114  if (label == nullptr) {
1115  changed = true;
1116  } else if (strcmp(ptile->label, label)) {
1117  changed = true;
1118  }
1119  delete[] ptile->label;
1120  ptile->label = nullptr;
1121  } else if (label != nullptr) {
1122  changed = true;
1123  }
1124 
1125  if (label != nullptr) {
1126  if (strlen(label) >= MAX_LEN_MAP_LABEL) {
1127  qCritical("Overlong map label '%s'", label);
1128  }
1129  ptile->label = fc_strdup(label);
1130  }
1131 
1132  return changed;
1133 }
1134 
1138 bool tile_is_placing(const struct tile *ptile)
1139 {
1140  return ptile->placing != nullptr;
1141 }
bool base_has_flag(const struct base_type *pbase, enum base_flag_id flag)
Check if base provides effect.
Definition: base.cpp:24
bool base_has_flag_for_utype(const struct base_type *pbase, enum base_flag_id flag, const struct unit_type *punittype)
Base provides base flag for unit? Checks if base provides flag and if base is native to unit.
Definition: base.cpp:90
bool territory_claiming_base(const struct base_type *pbase)
Does this base type claim territory?
Definition: base.cpp:196
#define BV_CLR_ALL(bv)
Definition: bitvector.h:62
#define BV_SET(bv, bit)
Definition: bitvector.h:44
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
#define BV_CLR(bv, bit)
Definition: bitvector.h:49
bool city_is_virtual(const struct city *pcity)
Return TRUE if the city is a virtual city.
Definition: city.cpp:3478
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
void destroy_city_virtual(struct city *pcity)
Removes the virtual skeleton of a city.
Definition: city.cpp:3421
bool is_city_center(const struct city *pcity, const struct tile *ptile)
Return TRUE if the city is centered at the given tile.
Definition: city.cpp:3497
int extra_number(const struct extra_type *pextra)
Return the extra id.
Definition: extras.cpp:132
bool extra_has_flag(const struct extra_type *pextra, enum extra_flag_id flag)
Check if extra has given flag.
Definition: extras.cpp:779
bool is_native_tile_to_extra(const struct extra_type *pextra, const struct tile *ptile)
Is tile native to extra?
Definition: extras.cpp:559
const char * extra_name_translation(const struct extra_type *pextra)
Return the (translated) name of the extra type.
Definition: extras.cpp:165
bool is_native_extra_to_utype(const struct extra_type *pextra, const struct unit_type *punittype)
Is extra native to unit type?
Definition: extras.cpp:770
bool can_extras_coexist(const struct extra_type *pextra1, const struct extra_type *pextra2)
Can two extras coexist in same tile?
Definition: extras.cpp:902
bool is_native_extra_to_uclass(const struct extra_type *pextra, const struct unit_class *pclass)
Is extra native to unit class?
Definition: extras.cpp:761
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_deps_iterate(_reqs, _dep)
Definition: extras.h:335
#define extra_type_iterate_end
Definition: extras.h:285
#define extra_index(_e_)
Definition: extras.h:163
#define extra_deps_iterate_end
Definition: extras.h:343
#define extra_base_get(_e_)
Definition: extras.h:170
#define extra_road_get(_e_)
Definition: extras.h:171
#define extra_type_by_cause_iterate_end
Definition: extras.h:307
#define extra_type_by_cause_iterate(_cause, _extra)
Definition: extras.h:299
const struct functions * fc_funcs
enum unit_activity Activity_type_id
Definition: fc_types.h:296
#define EC_NATURAL_DEFENSIVE
Definition: fc_types.h:937
#define EC_DEFENSIVE
Definition: fc_types.h:936
#define MAX_EXTRA_TYPES
Definition: fc_types.h:42
#define MAX_LEN_MAP_LABEL
Definition: fc_types.h:63
output_type_id
Definition: fc_types.h:84
signed short Continent_id
Definition: fc_types.h:289
@ BORDERS_DISABLED
Definition: fc_types.h:863
struct civ_game game
Definition: game.cpp:47
bool is_server()
Is program type server?
Definition: game.cpp:57
struct world wld
Definition: game.cpp:48
#define fc_assert_msg(condition, message,...)
Definition: log.h:96
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
bool map_is_empty()
Returns TRUE if we are at a stage of the game where the map has not yet been generated/loaded.
Definition: map.cpp:124
int map_num_tiles()
Returns the total number of (real) positions (or tiles) on the map.
Definition: map.cpp:954
bool road_has_flag(const struct road_type *proad, enum road_flag_id flag)
Check if road provides effect.
Definition: road.cpp:367
struct extra_type * road_extra_get(const struct road_type *proad)
Return extra that road is.
Definition: road.cpp:33
#define FC_INFINITY
Definition: shared.h:32
Definition: base.h:43
Definition: city.h:291
struct packet_game_info info
Definition: game.h:80
struct tile * tiles
Definition: map_types.h:76
bv_extras conflicts
Definition: extras.h:116
struct requirement_vector reqs
Definition: extras.h:90
bv_extras hidden_by
Definition: extras.h:117
void(* create_extra)(struct tile *ptile, const extra_type *pextra, struct player *pplayer)
Definition: fc_interface.h:31
bool(* player_tile_vision_get)(const struct tile *ptile, const struct player *pplayer, enum vision_layer vision)
Definition: fc_interface.h:36
void(* destroy_extra)(struct tile *ptile, struct extra_type *pextra)
Definition: fc_interface.h:33
Definition: player.h:231
QBitArray * tile_known
Definition: player.h:291
Definition: road.h:54
int tile_bonus[O_LAST]
Definition: road.h:61
int tile_incr_const[O_LAST]
Definition: road.h:59
int tile_incr[O_LAST]
Definition: road.h:60
int plant_time
Definition: terrain.h:198
struct terrain * irrigation_result
Definition: terrain.h:200
int cultivate_time
Definition: terrain.h:196
int transform_time
Definition: terrain.h:211
struct terrain * mining_result
Definition: terrain.h:204
struct terrain * transform_result
Definition: terrain.h:210
Definition: tile.h:42
char * spec_sprite
Definition: tile.h:58
char * label
Definition: tile.h:57
int index
Definition: tile.h:43
bv_extras extras
Definition: tile.h:47
struct extra_type * resource
Definition: tile.h:48
struct unit_list * units
Definition: tile.h:50
struct player * extras_owner
Definition: tile.h:55
struct terrain * terrain
Definition: tile.h:49
struct extra_type * placing
Definition: tile.h:53
struct city * worked
Definition: tile.h:51
struct player * owner
Definition: tile.h:52
Continent_id continent
Definition: tile.h:46
struct tile * claimer
Definition: tile.h:56
struct civ_map map
Definition: world_object.h:21
size_t fc_strlcat(char *dest, const char *src, size_t n)
fc_strlcat() provides utf-8 version of (non-standard) function strlcat() It is intended as more user-...
Definition: support.cpp:448
int cat_snprintf(char *str, size_t n, const char *format,...)
cat_snprintf is like a combination of fc_snprintf and fc_strlcat; it does snprintf to the end of an e...
Definition: support.cpp:564
#define sz_strlcpy(dest, src)
Definition: support.h:140
#define fc_strdup(str)
Definition: support.h:111
#define sz_strlcat(dest, src)
Definition: support.h:142
int terrain_extra_removal_time(const struct terrain *pterrain, enum unit_activity activity, const struct extra_type *tgt)
Time to complete the extra removal activity on the given terrain.
Definition: terrain.cpp:615
const char * terrain_rule_name(const struct terrain *pterrain)
Return the (untranslated) rule name of the terrain.
Definition: terrain.cpp:184
bool terrain_has_resource(const struct terrain *pterrain, const struct extra_type *presource)
Check for resource in terrain resources list.
Definition: terrain.cpp:192
const char * terrain_name_translation(const struct terrain *pterrain)
Return the (translated) name of the terrain.
Definition: terrain.cpp:175
int terrain_extra_build_time(const struct terrain *pterrain, enum unit_activity activity, const struct extra_type *tgt)
Time to complete the extra building activity on the given terrain.
Definition: terrain.cpp:579
Terrain_type_id terrain_number(const struct terrain *pterrain)
Return the terrain index.
Definition: terrain.cpp:119
#define T_NONE
Definition: terrain.h:50
#define terrain_has_flag(terr, flag)
Definition: terrain.h:260
int tile_extras_class_defense_bonus(const struct tile *ptile, const struct unit_class *pclass)
Calculate defense bonus given for unit class by extras.
Definition: tile.cpp:232
static bool tile_info_pollution(char *buf, int bufsz, const struct tile *ptile, struct extra_type *pextra, bool prevp, bool linebreak)
Add one entry about pollution situation to buffer.
Definition: tile.cpp:764
static void tile_cultivate(struct tile *ptile)
Cultivate (ACTIVITY_CULTIVATE) the tile.
Definition: tile.cpp:686
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Adds extra to tile.
Definition: tile.cpp:974
int tile_roads_output_bonus(const struct tile *ptile, enum output_type_id o)
Calculate output bonus given by roads.
Definition: tile.cpp:289
bool tile_has_extra_flag(const struct tile *ptile, enum extra_flag_id flag)
Check if tile contains extra providing effect.
Definition: tile.cpp:914
bool tile_has_claimable_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base providing effect for unit.
Definition: tile.cpp:203
void tile_set_terrain(struct tile *ptile, struct terrain *pterrain)
Set the given terrain at the specified tile.
Definition: tile.cpp:114
void tile_virtual_destroy(struct tile *vtile)
Frees all memory used by the virtual tile, including freeing virtual units in the tile's unit list an...
Definition: tile.cpp:1051
static void tile_irrigate(struct tile *ptile, struct extra_type *tgt)
Build irrigation on the tile.
Definition: tile.cpp:615
bool tile_has_river(const struct tile *ptile)
Tile has any river type.
Definition: tile.cpp:876
static void tile_destroy_extra(struct tile *ptile, struct extra_type *pextra)
Destroy extra from tile.
Definition: tile.cpp:476
bool tile_is_placing(const struct tile *ptile)
Is there a placing ongoing?
Definition: tile.cpp:1138
bool tile_has_base_flag(const struct tile *ptile, enum base_flag_id flag)
Check if tile contains base providing effect.
Definition: tile.cpp:164
bool tile_apply_activity(struct tile *ptile, Activity_type_id act, struct extra_type *tgt)
Apply an activity (Activity_type_id, e.g., ACTIVITY_TRANSFORM) to a tile.
Definition: tile.cpp:701
const char * tile_get_info_text(const struct tile *ptile, bool include_nuisances, int linebreaks)
Return a (static) string with tile name describing terrain and extras of some categories.
Definition: tile.cpp:799
const bv_extras * tile_extras_null()
Returns a bit vector of the extras present at nullptr tile.
Definition: tile.cpp:149
static void tile_transform(struct tile *ptile)
Transform (ACTIVITY_TRANSFORM) the tile.
Definition: tile.cpp:659
void tile_remove_extra(struct tile *ptile, const struct extra_type *pextra)
Removes extra from tile if such exist.
Definition: tile.cpp:984
void tile_change_terrain(struct tile *ptile, struct terrain *pterrain)
Change the terrain to the given type.
Definition: tile.cpp:491
bool tile_has_visible_extra(const struct tile *ptile, const struct extra_type *pextra)
Returns TRUE if the given tile has a road of given type on it.
Definition: tile.cpp:948
static bool add_recursive_extras(struct tile *ptile, const extra_type *pextra, int rec)
Recursively add all extra dependencies to add given extra.
Definition: tile.cpp:510
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Calculate defense bonus given for unit type by bases and roads.
Definition: tile.cpp:223
static void tile_mine(struct tile *ptile, struct extra_type *tgt)
Build a mine on the tile.
Definition: tile.cpp:637
bool tile_has_base_flag_for_unit(const struct tile *ptile, const struct unit_type *punittype, enum base_flag_id flag)
Check if tile contains base providing effect for unit.
Definition: tile.cpp:182
bool tile_has_road(const struct tile *ptile, const struct road_type *proad)
Returns TRUE if the given tile has a road of given type on it.
Definition: tile.cpp:868
bool tile_virtual_check(const tile *vtile)
Check if the given tile is a virtual one or not.
Definition: tile.cpp:1085
bool tile_has_conflicting_extra(const struct tile *ptile, const struct extra_type *pextra)
Returns TRUE if the given tile has a extra conflicting with the given one.
Definition: tile.cpp:930
static void tile_create_extra(struct tile *ptile, const extra_type *pextra)
Create extra to tile.
Definition: tile.cpp:463
static void tile_plant(struct tile *ptile)
Plant (ACTIVITY_PLANT) the tile.
Definition: tile.cpp:672
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Set the owner of a tile (may be nullptr).
Definition: tile.cpp:58
bool tile_is_seen(const struct tile *target_tile, const struct player *pow_player)
Returns TRUE iff the target_tile is seen by pow_player.
Definition: tile.cpp:416
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Time to complete the given activity on the given tile.
Definition: tile.cpp:427
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains base native for unit.
Definition: tile.cpp:327
int tile_roads_output_incr(const struct tile *ptile, enum output_type_id o)
Calculate output increment given by roads.
Definition: tile.cpp:266
bool tile_extra_rm_apply(struct tile *ptile, struct extra_type *tgt)
Remove extra and adjust other extras accordingly.
Definition: tile.cpp:605
bool tile_set_label(struct tile *ptile, const char *label)
Sets label for tile.
Definition: tile.cpp:1104
static bool rm_recursive_extras(struct tile *ptile, struct extra_type *pextra, int rec)
Recursively remove all extras depending on given extra.
Definition: tile.cpp:540
void tile_set_resource(struct tile *ptile, struct extra_type *presource)
Set the given resource at the specified tile.
Definition: tile.cpp:355
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Return a known_type enumeration value for the tile.
Definition: tile.cpp:398
bool tile_extra_apply(struct tile *ptile, const extra_type *tgt)
Add extra and adjust other extras accordingly.
Definition: tile.cpp:578
bool tile_has_refuel_extra(const struct tile *ptile, const struct unit_type *punittype)
Check if tile contains refuel extra native for unit.
Definition: tile.cpp:309
bool tile_has_road_flag(const struct tile *ptile, enum road_flag_id flag)
Check if tile contains road providing effect.
Definition: tile.cpp:894
static bv_extras empty_extras
Definition: tile.cpp:29
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
Definition: tile.cpp:96
void tile_set_continent(struct tile *ptile, Continent_id val)
Set the continent ID of the tile.
Definition: tile.cpp:388
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
struct tile * tile_virtual_new(const struct tile *ptile)
Returns a virtual tile.
Definition: tile.cpp:997
#define TILE_LB_RIVER_RESOURCE
Definition: tile.h:161
#define tile_claimer(_tile)
Definition: tile.h:82
#define TILE_LB_RESOURCE_POLL
Definition: tile.h:162
#define tile_index(_pt_)
Definition: tile.h:70
static bool tile_resource_is_valid(const struct tile *ptile)
Definition: tile.h:85
#define tile_worked(_tile)
Definition: tile.h:97
#define tile_resource(_tile)
Definition: tile.h:84
known_type
Definition: tile.h:28
@ TILE_KNOWN_UNSEEN
Definition: tile.h:30
@ TILE_UNKNOWN
Definition: tile.h:29
@ TILE_KNOWN_SEEN
Definition: tile.h:31
#define ACTIVITY_FACTOR
Definition: tile.h:151
#define TILE_LB_TERRAIN_RIVER
Definition: tile.h:160
#define tile_terrain(_tile)
Definition: tile.h:93
#define TILE_XY(ptile)
Definition: tile.h:36
#define tile_continent(_tile)
Definition: tile.h:74
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
#define TILE_INDEX_NONE
Definition: tile.h:40
#define tile_owner(_tile)
Definition: tile.h:78
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
bool unit_is_virtual(const struct unit *punit)
Return TRUE if this is a valid unit pointer but does not correspond to any unit that exists in the ga...
Definition: unit.cpp:2027
#define unit_list_iterate(unitlist, punit)
Definition: unitlist.h:25
#define unit_list_iterate_end
Definition: unitlist.h:27
#define utype_class(_t_)
Definition: unittype.h:691