// This file is part of c-evo-evo. // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // "http://www.cecill.info". // // As a counterpart to the access to the source code and rights to copy, // modify and redistribute granted by the license, users are provided only // with a limited warranty and the software's author, the holder of the // economic rights, and the successive licensors have only limited // liability. // // In this respect, the user's attention is drawn to the risks associated // with loading, using, modifying and/or developing or reproducing the // software by the user in light of its specific status of free software, // that may mean that it is complicated to manipulate, and that also // therefore means that it is reserved for developers and experienced // professionals having in-depth computer knowledge. Users are therefore // encouraged to load and test the software's suitability as regards their // requirements in conditions enabling the security of their systems and/or // data to be ensured and, more generally, to use and operate it in the // same conditions as regards security. // // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. // --- Territory Calculation --- (see Cevo/Database.h) #include "Cevo/Database.h" #include "DatabaseInternal.h" #include #include #include namespace Cevo { namespace Database { void changeTerritory(int loc, int p) { assert(p >= 0); // no player's territory indicated by p == nPl --territoryCount[realMap[loc] >> 27]; ++territoryCount[p]; realMap[loc] = (realMap[loc] & ~(0xFu << 27)) | (static_cast(p) << 27); if (p == 0xF) p = -1; for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & (gAlive | gWatching)) == 0) continue; TPlayerContext &player = rw[p1]; if (((*player.Map)[loc] & fTerrain) == fUNKNOWN) continue; (*player.Territory)[loc] = static_cast(p); // Faithful port of the original relying on x86 SHL masking the shift // count to 5 bits when p==-1 (so `1 shl p` becomes `1 shl 31`, never // matching gAlive, rather than C++'s real UB for negative shifts) - // see repo memory's policy on preserving known upstream quirks. if (p < nPl && p != p1 && ((1u << (p & 31)) & static_cast(gAlive)) && (player.Treaty[p] == trPeace || player.Treaty[p] == trFriendlyContact)) (*player.Map)[loc] |= fPeace; else (*player.Map)[loc] &= ~static_cast(fPeace); } } void expandTerritory(int originLoc) { if (originLoc == -1) throw std::runtime_error("Location error"); int i = 0; int dyMax = 0; while ((dyMax + 1) + ((dyMax + 1) >> 1) <= CountryRadius) ++dyMax; for (int dy = -dyMax; dy <= dyMax; ++dy) { int dxMax = dy & 1; while (std::abs(dy) + (dxMax + 2) + (std::abs(std::abs(dy) - (dxMax + 2)) >> 1) <= CountryRadius) dxMax += 2; for (int dx = -dxMax; dx <= dxMax; ++dx) { if (((dy + dx) & 1) == 0) { const std::uint32_t newOwner = (borderChanges[i / 8] >> ((i % 8) * 4)) & 0xF; const int loc = dLoc(originLoc, dx, dy); if (loc >= 0 && newOwner != (realMap[loc] >> 27)) changeTerritory(loc, static_cast(newOwner)); ++i; } } } } void checkBorders(int originLoc, int playerLosingCity) { std::array country{}; std::array formerCountry{}; std::array dist{}; std::array formerDist{}; std::array stolenDist{}; std::array, nPl + 1> atPeace{}; Vicinity8Loc adjacent; constexpr std::uint32_t noZocTerrainMask = (1u << fShore) | (1u << fMountains) | (1u << fArctic); if (playerLosingCity >= 0) { for (int loc = 0; loc < mapSize; ++loc) stolenDist[loc] = static_cast(CountryRadius + 1); for (int cix = 0; cix < rw[playerLosingCity].nCity; ++cix) { const int loc = (*rw[playerLosingCity].City)[cix].Loc; if (loc >= 0) stolenDist[loc] = 0; } for (int r = 1; r <= (CountryRadius >> 1); ++r) { formerDist = stolenDist; for (int loc = 0; loc < mapSize; ++loc) { if (formerDist[loc] <= CountryRadius - 2 && ((1u << (realMap[loc] & fTerrain)) & noZocTerrainMask) == 0) { v8ToLoc(loc, adjacent); for (int v8 = 0; v8 < 8; ++v8) { const int loc1 = adjacent[v8]; const int newDist = formerDist[loc] + 2 + (v8 & 1); if (loc1 >= 0 && loc1 < mapSize && newDist < stolenDist[loc1]) stolenDist[loc1] = static_cast(newDist); } } } } } country.fill(-1); for (int loc = 0; loc < mapSize; ++loc) dist[loc] = static_cast(CountryRadius + 1); for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & gAlive) == 0) continue; for (int cix = 0; cix < rw[p1].nCity; ++cix) { const int loc = (*rw[p1].City)[cix].Loc; if (loc >= 0) { country[loc] = static_cast(p1); dist[loc] = 0; } } } for (int r = 1; r <= (CountryRadius >> 1); ++r) { formerCountry = country; formerDist = dist; for (int loc = 0; loc < mapSize; ++loc) { if (formerDist[loc] <= CountryRadius - 2 && ((1u << (realMap[loc] & fTerrain)) & noZocTerrainMask) == 0) { assert(formerCountry[loc] >= 0); v8ToLoc(loc, adjacent); for (int v8 = 0; v8 < 8; ++v8) { const int loc1 = adjacent[v8]; const int newDist = formerDist[loc] + 2 + (v8 & 1); if (loc1 >= 0 && loc1 < mapSize && newDist < dist[loc1]) { country[loc1] = formerCountry[loc]; dist[loc1] = static_cast(newDist); } } } } } for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & gAlive) == 0) continue; for (int p2 = 0; p2 < nPl; ++p2) if (p2 != p1 && ((1 << p2) & gAlive) && rw[p1].Treaty[p2] >= trPeace) atPeace[p1][p2] = true; } if (originLoc >= 0) { int i = 0; borderChanges.fill(0); int dyMax = 0; while ((dyMax + 1) + ((dyMax + 1) >> 1) <= CountryRadius) ++dyMax; for (int dy = -dyMax; dy <= dyMax; ++dy) { int dxMax = dy & 1; while (std::abs(dy) + (dxMax + 2) + (std::abs(std::abs(dy) - (dxMax + 2)) >> 1) <= CountryRadius) dxMax += 2; for (int dx = -dxMax; dx <= dxMax; ++dx) { if (((dy + dx) & 1) == 0) { const int loc = dLoc(originLoc, dx, dy); if (loc >= 0) { const int oldOwner = static_cast(realMap[loc] >> 27); int newOwner = country[loc] & 0xF; if (newOwner != oldOwner) { if (atPeace[newOwner][oldOwner] && !(oldOwner == playerLosingCity && stolenDist[loc] > CountryRadius)) newOwner = oldOwner; // peace fixes borders else changeTerritory(loc, newOwner); } borderChanges[i >> 3] |= static_cast(newOwner) << ((i & 7) * 4); } ++i; } } } } else { for (int loc = 0; loc < mapSize; ++loc) { const int oldOwner = static_cast(realMap[loc] >> 27); const int newOwner = country[loc] & 0xF; if (newOwner != oldOwner && (!atPeace[newOwner][oldOwner] || (oldOwner == playerLosingCity && stolenDist[loc] > CountryRadius))) { assert(originLoc != -2); // test if border saving works changeTerritory(loc, newOwner); } } } #ifndef NDEBUG if (originLoc != -2) checkBorders(-2); // check: single pass should do! #endif } void logCheckBorders(int p, int cix, int playerLosingCity) { checkBorders((*rw[p].City)[cix].Loc, playerLosingCity); intServer(sIntExpandTerritory, p, cix, borderChanges.data()); } void recalcPeaceMap(int p) { if (mode != moPlaying) return; TPlayerContext &player = rw[p]; maskD(*player.Map, mapSize, ~static_cast(fPeace)); std::array peacePlayer{}; // index p1+1, covers p1 in -1..nPl-1 for (int p1 = 0; p1 < nPl; ++p1) peacePlayer[p1 + 1] = p1 != p && ((1 << p1) & gAlive) && (player.Treaty[p1] == trPeace || player.Treaty[p1] == trFriendlyContact); std::uint32_t *map = *player.Map; const TOwnerList &territory = *player.Territory; for (int loc = 0; loc < mapSize; ++loc) if (peacePlayer[territory[loc] + 1]) map[loc] |= fPeace; } void recalcMapZoC(int p) { if (mode == moLoading_Fast) return; maskD(*rw[p].Map, mapSize, ~static_cast(fInEnemyZoC)); const int observeMask = 3 << (2 * p); std::uint32_t *map = *rw[p].Map; for (int loc = 0; loc < mapSize; ++loc) { if (zocMap[loc] > 0 && (observeLevel[loc] & observeMask)) { const int p1 = occupant[loc]; assert(p1 != nPl); if (p1 != p && rw[p].Treaty[p1] < trAlliance) { // this non-allied enemy ZoC unit is known to this player -- set flags! Vicinity8Loc adjacent; v8ToLoc(loc, adjacent); for (int v8 = 0; v8 < 8; ++v8) { const int loc1 = adjacent[static_cast(v8)]; if (loc1 >= 0 && loc1 < mapSize) map[loc1] |= fInEnemyZoC; } } } } } void recalcV8ZoC(int p, int loc) { if (mode == moLoading_Fast) return; const int observeMask = 3 << (2 * p); Vicinity8Loc adjacent; v8ToLoc(loc, adjacent); std::uint32_t *map = *rw[p].Map; for (int v8 = 0; v8 < 8; ++v8) { const int loc1 = adjacent[static_cast(v8)]; if (loc1 < 0 || loc1 >= mapSize) continue; std::uint32_t &tile1 = map[loc1]; tile1 &= ~static_cast(fInEnemyZoC); Vicinity8Loc adjacentAdjacent; v8ToLoc(loc1, adjacentAdjacent); for (int v8v8 = 0; v8v8 < 8; ++v8v8) { const int loc2 = adjacentAdjacent[static_cast(v8v8)]; if (loc2 >= 0 && loc2 < mapSize && zocMap[loc2] > 0 && (observeLevel[loc2] & observeMask)) { const int p1 = occupant[loc2]; assert(p1 != nPl); if (p1 != p && rw[p].Treaty[p1] < trAlliance) { tile1 |= fInEnemyZoC; break; } } } } } } // namespace Database } // namespace Cevo