Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpTerritoryManager.cpp
Show First 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | case MT_WaterChanged: | ||||
MakeDirty(); | MakeDirty(); | ||||
break; | break; | ||||
} | } | ||||
case MT_Update: | case MT_Update: | ||||
{ | { | ||||
if (m_TriggerEvent) | if (m_TriggerEvent) | ||||
{ | { | ||||
m_TriggerEvent = false; | m_TriggerEvent = false; | ||||
CMessageTerritoriesChanged msg; | GetSimContext().GetComponentManager().BroadcastMessage(CMessageTerritoriesChanged()); | ||||
GetSimContext().GetComponentManager().BroadcastMessage(msg); | |||||
} | } | ||||
break; | break; | ||||
} | } | ||||
case MT_Interpolate: | case MT_Interpolate: | ||||
{ | { | ||||
const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg); | const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg); | ||||
Interpolate(msgData.deltaSimTime, msgData.offset); | Interpolate(msgData.deltaSimTime, msgData.offset); | ||||
break; | break; | ||||
▲ Show 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | |||||
// Tile data type, for easier accessing of coordinates | // Tile data type, for easier accessing of coordinates | ||||
struct Tile | struct Tile | ||||
{ | { | ||||
Tile(u16 i, u16 j) : x(i), z(j) { } | Tile(u16 i, u16 j) : x(i), z(j) { } | ||||
u16 x, z; | u16 x, z; | ||||
}; | }; | ||||
// Floodfill templates that expand neighbours from a certain source onwards | // Floodfill templates that expand neighbours from a certain source onwards | ||||
// (x, z) are the coordinates of the currently expanded tile | // (posX, posZ) are the coordinates of the currently expanded tile | ||||
// (nx, nz) are the coordinates of the current neighbour handled | // (nx, nz) are the coordinates of the current neighbour handled | ||||
// The user of this floodfill should use "continue" on every neighbour that | // The user of this floodfill should use "continue" on every neighbour that | ||||
// shouldn't be expanded on its own. (without continue, an infinite loop will happen) | // shouldn't be expanded on its own. (without continue, an infinite loop will happen) | ||||
# define FLOODFILL(i, j, code)\ | # define FLOODFILL(i, j, code)\ | ||||
do {\ | do {\ | ||||
const int NUM_NEIGHBOURS = 8;\ | const int NUM_NEIGHBOURS = 8;\ | ||||
const int NEIGHBOURS_X[NUM_NEIGHBOURS] = {1,-1, 0, 0, 1,-1, 1,-1};\ | const int NEIGHBOURS_X[NUM_NEIGHBOURS] = {1,-1, 0, 0, 1,-1, 1,-1};\ | ||||
const int NEIGHBOURS_Z[NUM_NEIGHBOURS] = {0, 0, 1,-1, 1,-1,-1, 1};\ | const int NEIGHBOURS_Z[NUM_NEIGHBOURS] = {0, 0, 1,-1, 1,-1,-1, 1};\ | ||||
std::queue<Tile> openTiles;\ | std::queue<Tile> openTiles;\ | ||||
openTiles.emplace(i, j);\ | openTiles.emplace(i, j);\ | ||||
while (!openTiles.empty())\ | while (!openTiles.empty())\ | ||||
{\ | {\ | ||||
u16 x = openTiles.front().x;\ | u16 posX = openTiles.front().x;\ | ||||
u16 z = openTiles.front().z;\ | u16 posZ = openTiles.front().z;\ | ||||
openTiles.pop();\ | openTiles.pop();\ | ||||
for (int n = 0; n < NUM_NEIGHBOURS; ++n)\ | for (int n = 0; n < NUM_NEIGHBOURS; ++n)\ | ||||
{\ | {\ | ||||
u16 nx = x + NEIGHBOURS_X[n];\ | u16 nx = posX + NEIGHBOURS_X[n];\ | ||||
u16 nz = z + NEIGHBOURS_Z[n];\ | u16 nz = posZ + NEIGHBOURS_Z[n];\ | ||||
/* Check the bounds, underflow will cause the values to be big again */\ | /* Check the bounds, underflow will cause the values to be big again */\ | ||||
if (nx >= tilesW || nz >= tilesH)\ | if (nx >= tilesW || nz >= tilesH)\ | ||||
continue;\ | continue;\ | ||||
code\ | code\ | ||||
openTiles.emplace(nx, nz);\ | openTiles.emplace(nx, nz);\ | ||||
}\ | }\ | ||||
}\ | }\ | ||||
}\ | }\ | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | for (entity_id_t ent : ents) | ||||
m_Territories->set(i, j, owner); | m_Territories->set(i, j, owner); | ||||
} | } | ||||
// Expand influences outwards | // Expand influences outwards | ||||
FLOODFILL(i, j, | FLOODFILL(i, j, | ||||
u32 dg = falloff * m_CostGrid->get(nx, nz); | u32 dg = falloff * m_CostGrid->get(nx, nz); | ||||
// diagonal neighbour -> multiply with approx sqrt(2) | // diagonal neighbour -> multiply with approx sqrt(2) | ||||
if (nx != x && nz != z) | if (nx != posX && nz != posZ) | ||||
dg = (dg * 362) / 256; | dg = (dg * 362) / 256; | ||||
// Don't expand if new cost is not better than previous value for that tile | // Don't expand if new cost is not better than previous value for that tile | ||||
// (arranged to avoid underflow if entityGrid.get(x, z) < dg) | // (arranged to avoid underflow if entityGrid.get(x, z) < dg) | ||||
if (entityGrid.get(x, z) <= entityGrid.get(nx, nz) + dg) | if (entityGrid.get(posX, posZ) <= entityGrid.get(nx, nz) + dg) | ||||
continue; | continue; | ||||
// weight of this tile = weight of predecessor - falloff from predecessor | // weight of this tile = weight of predecessor - falloff from predecessor | ||||
u32 newWeight = entityGrid.get(x, z) - dg; | u32 newWeight = entityGrid.get(posX, posZ) - dg; | ||||
u32 totalWeight = playerGrid.get(nx, nz) - entityGrid.get(nx, nz) + newWeight; | u32 totalWeight = playerGrid.get(nx, nz) - entityGrid.get(nx, nz) + newWeight; | ||||
playerGrid.set(nx, nz, totalWeight); | playerGrid.set(nx, nz, totalWeight); | ||||
entityGrid.set(nx, nz, newWeight); | entityGrid.set(nx, nz, newWeight); | ||||
// if this weight is better than the best thus far, set the owner | // if this weight is better than the best thus far, set the owner | ||||
if (totalWeight > bestWeightGrid.get(nx, nz)) | if (totalWeight > bestWeightGrid.get(nx, nz)) | ||||
{ | { | ||||
bestWeightGrid.set(nx, nz, totalWeight); | bestWeightGrid.set(nx, nz, totalWeight); | ||||
m_Territories->set(nx, nz, owner); | m_Territories->set(nx, nz, owner); | ||||
▲ Show 20 Lines • Show All 334 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator