Freeciv21
Develop your civilization from humble roots to a global empire
vision.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 // common
16 #include "fc_types.h"
17 
18 #include "improvement.h" // bv_imprs
19 
20 /****************************************************************************
21  Vision for cities and units:
22 
23  A vision source has a fixed owner and tile; it changes only in range.
24  Vision range is given in radius squared; most such values will come from
25  the ruleset. All vision is circular.
26 
27  A vision source is created using vision_new; this creates the source
28  without any sight points. Call vision_change_sight to change the sight
29  points of a vision source (generally called from city_refresh_vision
30  and unit_refresh vision; this can be called liberally to do updates after
31  an effect may have changed the source's vision range). Clear the sight
32  using vision_clear_sight before freeing it with vision_free.
33 
34  vision_get_sight returns the sight points of the source. This should
35  only rarely be necessary since all fogging and unfogging operations
36  are taken care of internally.
37 
38  vision_reveal_tiles() controls whether the vision source can discover
39  new (unknown) tiles or simply maintain vision on already-known tiles.
40  By default, cities should pass FALSE for this since they cannot
41  discover new tiles.
42 
43  ***** IMPORTANT *****
44  To change any of the parameters given to vision_new - that is, to change
45  the vision source's position (tile) or owner - you must create a new
46  vision and then clear and free the old vision. Order is very important
47  here since you do not want to fog tiles intermediately. You must store
48  a copy of the old vision source, then create and attach and fill out the
49  sight for a new vision source, and only then may you clear and free the
50  old vision source. In most operations you'll want to stick some other
51  code in between so that for the bulk of the operation all tiles are
52  visible. For instance to move a unit:
53 
54  old_vision = punit->server.vision;
55  punit->server.vision = vision_new(unit_owner(punit), dest_tile);
56  vision_change_sight(punit->server.vision,
57  get_unit_vision_at(punit, dest_tile));
58 
59  ...then do all the work of moving the unit...
60 
61  vision_clear_sight(old_vision);
62  vision_free(old_vision);
63 
64  note that for all the code in the middle both the new and the old
65  vision sources are active. The same process applies when transferring
66  a unit or city between players, etc.
67 ****************************************************************************/
68 
69 /* Invariants: V_MAIN vision ranges must always be more than V_INVIS
70  * ranges. */
71 
72 #define vision_layer_iterate(v) \
73  { \
74  int iv; \
75  for (iv = 0; iv < V_COUNT; iv++) { \
76  enum vision_layer v = (enum vision_layer) iv;
77 #define vision_layer_iterate_end \
78  } \
79  }
80 
81 typedef short int v_radius_t[V_COUNT];
82 
83 struct vision {
84  // These values cannot be changed after initialization.
85  struct player *player;
86  struct tile *tile;
88 
89  // The radius of the vision source.
91 };
92 
93 // Initialize a vision radius array.
94 #define V_RADIUS(main_sq, invis_sq, subs_sq) \
95  { \
96  (short) (main_sq), (short) (invis_sq), (short) (subs_sq) \
97  }
98 
99 #define ASSERT_VISION(v) \
100  do { \
101  fc_assert((v)->radius_sq[V_MAIN] >= (v)->radius_sq[V_INVIS]); \
102  fc_assert((v)->radius_sq[V_MAIN] >= (v)->radius_sq[V_SUBSURFACE]); \
103  } while (false);
104 
105 struct vision *vision_new(struct player *pplayer, struct tile *ptile);
106 void vision_free(struct vision *vision);
107 
108 bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles);
109 
110 /* This is copied in maphand.c really_give_tile_info_from_player_to_player(),
111  * so be careful with pointers!
112  */
113 struct vision_site {
115  struct tile *location; // Cannot be nullptr
116  struct player *owner; // May be nullptr, always check!
117 
118  int identity; // city > IDENTITY_NUMBER_ZERO
119  citizens size; // city size (0 <= size <= MAX_CITY_SIZE)
120 
121  bool occupied;
122  bool walls;
123  bool happy;
124  bool unhappy;
125  int style;
127  enum capital_type capital;
128 
129  bv_imprs improvements;
130 };
131 
132 #define vision_site_owner(v) ((v)->owner)
133 struct vision_site *vision_site_new(int identity, struct tile *location,
134  struct player *owner);
135 struct vision_site *vision_site_new_from_city(const struct city *pcity);
136 void vision_site_update_from_city(struct vision_site *psite,
137  const struct city *pcity);
138 
139 citizens vision_site_size_get(const struct vision_site *psite);
140 void vision_site_size_set(struct vision_site *psite, citizens size);
unsigned char citizens
Definition: fc_types.h:305
#define MAX_LEN_NAME
Definition: fc_types.h:61
size_t size
Definition: specvec.h:64
Definition: city.h:291
Definition: player.h:231
Definition: tile.h:42
char name[MAX_LEN_NAME]
Definition: vision.h:114
bool happy
Definition: vision.h:123
bv_imprs improvements
Definition: vision.h:129
citizens size
Definition: vision.h:119
struct tile * location
Definition: vision.h:115
int identity
Definition: vision.h:118
bool walls
Definition: vision.h:122
int style
Definition: vision.h:125
bool unhappy
Definition: vision.h:124
struct player * owner
Definition: vision.h:116
int city_image
Definition: vision.h:126
bool occupied
Definition: vision.h:121
enum capital_type capital
Definition: vision.h:127
Definition: vision.h:83
struct tile * tile
Definition: vision.h:86
bool can_reveal_tiles
Definition: vision.h:87
v_radius_t radius_sq
Definition: vision.h:90
struct player * player
Definition: vision.h:85
void vision_site_update_from_city(struct vision_site *psite, const struct city *pcity)
Returns the basic structure filled with current elements.
Definition: vision.cpp:96
struct vision * vision_new(struct player *pplayer, struct tile *ptile)
Create a new vision source.
Definition: vision.cpp:27
void vision_site_size_set(struct vision_site *psite, citizens size)
Set the city size.
Definition: vision.cpp:122
citizens vision_site_size_get(const struct vision_site *psite)
Get the city size.
Definition: vision.cpp:112
bool vision_reveal_tiles(struct vision *vision, bool reveal_tiles)
Sets the can_reveal_tiles flag.
Definition: vision.cpp:56
struct vision_site * vision_site_new_from_city(const struct city *pcity)
Returns the basic structure filled with initial elements.
Definition: vision.cpp:82
void vision_free(struct vision *vision)
Free the vision source.
Definition: vision.cpp:44
struct vision_site * vision_site_new(int identity, struct tile *location, struct player *owner)
Returns the basic structure.
Definition: vision.cpp:67
short int v_radius_t[V_COUNT]
Definition: vision.h:81