Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "TerritoryBoundary.h"
21 :
22 : #include "lib/debug.h"
23 : #include "maths/Fixed.h"
24 : #include "simulation2/components/ICmpTerritoryManager.h"
25 : #include "simulation2/helpers/Grid.h"
26 : #include "simulation2/helpers/Pathfinding.h"
27 :
28 : #include <cmath>
29 :
30 3 : std::vector<STerritoryBoundary> CTerritoryBoundaryCalculator::ComputeBoundaries(
31 : const Grid<std::uint8_t>* territory)
32 : {
33 3 : std::vector<STerritoryBoundary> boundaries;
34 :
35 : // Copy the territories grid so we can mess with it
36 3 : Grid<std::uint8_t> grid(*territory);
37 :
38 : // Some constants for the border walk
39 3 : CVector2D edgeOffsets[] = {
40 : CVector2D(0.5f, 0.0f),
41 : CVector2D(1.0f, 0.5f),
42 : CVector2D(0.5f, 1.0f),
43 : CVector2D(0.0f, 0.5f)
44 3 : };
45 :
46 : // syntactic sugar
47 3 : const std::uint8_t TILE_BOTTOM = 0;
48 3 : const std::uint8_t TILE_RIGHT = 1;
49 3 : const std::uint8_t TILE_TOP = 2;
50 3 : const std::uint8_t TILE_LEFT = 3;
51 :
52 3 : const int CURVE_CW = -1;
53 3 : const int CURVE_CCW = 1;
54 :
55 : // === Find territory boundaries ===
56 : //
57 : // The territory boundaries delineate areas of tiles that belong to the same player, and that all have the same
58 : // connected-to-a-root-influence-entity status (see also STerritoryBoundary for a more wordy definition). Note that the grid
59 : // values contain bit-packed information (i.e. not just the owning player ID), so we must be careful to only compare grid
60 : // values using the player ID and connected flag bits. The joint mask to select these is referred to as the discriminator mask.
61 : //
62 : // The idea is to scan the (i,j)-grid going up row by row and look for tiles that have a different territory assignment from
63 : // the one right underneath it (or, if it's a tile on the first row, they need only have a territory assignment). These tiles
64 : // are necessarily edge tiles of a territory, and hence a territory boundary must pass through their bottom edge. Therefore,
65 : // we start tracing the outline of the territory starting from said bottom edge, and go CCW around the territory boundary.
66 : // Tracing continues until the starting point is reached, at which point the boundary is complete.
67 : //
68 : // While tracing a boundary, every tile in which the boundary passes through the bottom edge are marked as 'processed', so that
69 : // we know not to start a new run from these tiles when scanning continues (when the boundary is complete). This information
70 : // is maintained in the grid values themselves by means of the 'processed' bit mask (stressing the importance of using the
71 : // discriminator mask to compare only player ID and connected flag).
72 : //
73 : // Thus, we can identify the following conditions for starting a trace from a tile (i,j). Let g(i,j) indicate the
74 : // discriminator grid value at position (i,j); then the conditions are:
75 : // - g(i,j) != 0; the tile must not be neutral
76 : // - j=0 or g(i,j) != g(i,j-1); the tile directly underneath it must have a different owner and/or connected flag
77 : // - the tile must not already be marked as 'processed'
78 : //
79 : // Additionally, there is one more point to be made; the algorithm initially assumes it's tracing CCW around the territory.
80 : // If it's tracing an inner edge, however, this will actually cause it to trace in the CW direction (because inner edges curve
81 : // 'backwards' compared to the outer edges when starting the trace in the same direction). This turns out to actually be
82 : // exactly what the renderer needs to render two territory boundaries on the same edge back-to-back (instead of overlapping
83 : // each other).
84 : //
85 : // In either case, we keep track of the way the outline curves while we're tracing to determine whether we're going CW or CCW.
86 : // If at some point we ever need to revert the winding order or external code needs to know about it explicitly, then we can
87 : // do this by looking at a curvature value which we define to start at 0, and which is incremented by 1 for every CCW turn and
88 : // decremented by 1 for every CW turn. Hence, a negative multiple of 4 means a CW winding order, and a positive one means CCW.
89 :
90 3 : const int TERRITORY_DISCR_MASK = (ICmpTerritoryManager::TERRITORY_BLINKING_MASK | ICmpTerritoryManager::TERRITORY_PLAYER_MASK);
91 :
92 : // Try to find an assigned tile
93 19 : for (std::uint16_t j = 0; j < grid.m_H; ++j)
94 : {
95 138 : for (std::uint16_t i = 0; i < grid.m_W; ++i)
96 : {
97 : // saved tile state; from MSB to LSB:
98 : // processed bit, blinking bit, player ID
99 122 : std::uint8_t tileState = grid.get(i, j);
100 122 : std::uint8_t tileDiscr = (tileState & TERRITORY_DISCR_MASK);
101 :
102 : // ignore neutral tiles (note that tiles without an owner should never have the blinking bit set)
103 122 : if (!tileDiscr)
104 59 : continue;
105 :
106 63 : bool tileProcessed = ((tileState & ICmpTerritoryManager::TERRITORY_PROCESSED_MASK) != 0);
107 63 : bool tileEligible = (j == 0 || tileDiscr != (grid.get(i, j-1) & TERRITORY_DISCR_MASK));
108 :
109 63 : if (tileProcessed || !tileEligible)
110 53 : continue;
111 :
112 : // Found the first tile (which must be the lowest j value of any non-zero tile);
113 : // start at the bottom edge of it and chase anticlockwise around the border until
114 : // we reach the starting point again
115 :
116 10 : int curvature = 0; // +1 for every CCW 90 degree turn, -1 for every CW 90 degree turn; must be multiple of 4 at the end
117 :
118 10 : boundaries.push_back(STerritoryBoundary());
119 10 : boundaries.back().owner = (tileState & ICmpTerritoryManager::TERRITORY_PLAYER_MASK);
120 10 : boundaries.back().blinking = (tileState & ICmpTerritoryManager::TERRITORY_BLINKING_MASK) != 0;
121 10 : std::vector<CVector2D>& points = boundaries.back().points;
122 :
123 10 : std::uint8_t dir = TILE_BOTTOM;
124 :
125 10 : std::uint8_t cdir = dir;
126 10 : std::uint16_t ci = i, cj = j;
127 :
128 10 : std::uint16_t maxi = static_cast<std::uint16_t>(grid.m_W - 1);
129 10 : std::uint16_t maxj = static_cast<std::uint16_t>(grid.m_H - 1);
130 :
131 : // Size of a territory tile in metres
132 10 : float territoryTileSize = (Pathfinding::NAVCELL_SIZE * ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE).ToFloat();
133 :
134 112 : while (true)
135 : {
136 112 : points.push_back((CVector2D(ci, cj) + edgeOffsets[cdir]) * territoryTileSize);
137 :
138 : // Given that we're on an edge on a continuous boundary and aiming anticlockwise,
139 : // we can either carry on straight or turn left or turn right, so examine each
140 : // of the three possible cases (depending on initial direction):
141 112 : switch (cdir)
142 : {
143 30 : case TILE_BOTTOM:
144 :
145 : // mark tile as processed so we don't start a new run from it after this one is complete
146 30 : ENSURE(!(grid.get(ci, cj) & ICmpTerritoryManager::TERRITORY_PROCESSED_MASK));
147 30 : grid.set(ci, cj, grid.get(ci, cj) | ICmpTerritoryManager::TERRITORY_PROCESSED_MASK);
148 :
149 30 : if (ci < maxi && cj > 0 && (grid.get(ci+1, cj-1) & TERRITORY_DISCR_MASK) == tileDiscr)
150 : {
151 3 : ++ci;
152 3 : --cj;
153 3 : cdir = TILE_LEFT;
154 3 : curvature += CURVE_CW;
155 : }
156 27 : else if (ci < maxi && (grid.get(ci+1, cj) & TERRITORY_DISCR_MASK) == tileDiscr)
157 18 : ++ci;
158 : else
159 : {
160 9 : cdir = TILE_RIGHT;
161 9 : curvature += CURVE_CCW;
162 : }
163 : break;
164 :
165 26 : case TILE_RIGHT:
166 26 : if (ci < maxi && cj < maxj && (grid.get(ci+1, cj+1) & TERRITORY_DISCR_MASK) == tileDiscr)
167 : {
168 5 : ++ci;
169 5 : ++cj;
170 5 : cdir = TILE_BOTTOM;
171 5 : curvature += CURVE_CW;
172 : }
173 21 : else if (cj < maxj && (grid.get(ci, cj+1) & TERRITORY_DISCR_MASK) == tileDiscr)
174 13 : ++cj;
175 : else
176 : {
177 8 : cdir = TILE_TOP;
178 8 : curvature += CURVE_CCW;
179 : }
180 : break;
181 :
182 30 : case TILE_TOP:
183 30 : if (ci > 0 && cj < maxj && (grid.get(ci-1, cj+1) & TERRITORY_DISCR_MASK) == tileDiscr)
184 : {
185 4 : --ci;
186 4 : ++cj;
187 4 : cdir = TILE_RIGHT;
188 4 : curvature += CURVE_CW;
189 : }
190 26 : else if (ci > 0 && (grid.get(ci-1, cj) & TERRITORY_DISCR_MASK) == tileDiscr)
191 17 : --ci;
192 : else
193 : {
194 9 : cdir = TILE_LEFT;
195 9 : curvature += CURVE_CCW;
196 : }
197 : break;
198 :
199 26 : case TILE_LEFT:
200 26 : if (ci > 0 && cj > 0 && (grid.get(ci-1, cj-1) & TERRITORY_DISCR_MASK) == tileDiscr)
201 : {
202 5 : --ci;
203 5 : --cj;
204 5 : cdir = TILE_TOP;
205 5 : curvature += CURVE_CW;
206 : }
207 21 : else if (cj > 0 && (grid.get(ci, cj-1) & TERRITORY_DISCR_MASK) == tileDiscr)
208 14 : --cj;
209 : else
210 : {
211 7 : cdir = TILE_BOTTOM;
212 7 : curvature += CURVE_CCW;
213 : }
214 : break;
215 : }
216 :
217 : // Stop when we've reached the starting point again
218 112 : if (ci == i && cj == j && cdir == dir)
219 : break;
220 : }
221 :
222 122 : ENSURE(curvature != 0 && abs(curvature) % 4 == 0);
223 : }
224 : }
225 :
226 3 : return boundaries;
227 3 : }
|