unit processing
This commit is contained in:
1 parent
2226f0bce4
commit
90bff517a1
4 files changed
+1717
-5
No files matched your search
@@ -71,6 +71,10 @@ constexpr int nemmax = 1024;
|
||||
constexpr int necmax = 1024;
|
||||
constexpr int neumax = 4096;
|
||||
|
||||
// Database.pas's `TerrType_Canalable` set, as a bitmask indexed by fTerrain.
|
||||
constexpr std::uint32_t terrTypeCanalableMask = (1u << fGrass) | (1u << fDesert) | (1u << fPrairie) |
|
||||
(1u << fTundra) | (1u << fSwamp) | (1u << fForest) | (1u << fHills);
|
||||
|
||||
// Equivalent of Database.pas's TGameMode - ordinal comparisons (</>) match
|
||||
// the original's enum declaration order.
|
||||
enum GameMode { moLoading_Fast, moLoading, moMovie, moPlaying };
|
||||
@@ -100,6 +104,10 @@ extern int gWatching;
|
||||
extern int gAI;
|
||||
extern int gTurn; // current turn
|
||||
extern int gTestFlags;
|
||||
extern std::array<int, nPl> founded; // number of cities founded per player
|
||||
// Debug-only cross-check (Database.pas's `{$IFOPT O-}InvalidTreatyMap`):
|
||||
// bitset of players whose territory must not be considered mid-command.
|
||||
extern int invalidTreatyMap;
|
||||
extern std::array<TWonderInfo, nWonder> gWonder;
|
||||
extern std::array<int, nPl> grWallContinent; // continent id the Great Wall was built on, -1 if none
|
||||
extern std::array<TShipInfo, nPl> gShip;
|
||||
@@ -199,6 +207,29 @@ void freeUnit(int p, int uix);
|
||||
// to that player (use only from inside sMoveUnit if attack).
|
||||
void removeUnit(int p, int uix, int enemy = -1);
|
||||
void removeUnitUpdateMap(int p, int uix);
|
||||
void removeDomainUnits(int d, int p, int loc);
|
||||
void foundCity(int p, int foundLoc);
|
||||
|
||||
// Database.pas's own formula function (originally in Protocol.pas), used
|
||||
// only by actualSpecialTile() to recompute hidden special-resource bits
|
||||
// after a terrain change (ClearTerrain/AfforestTerrain/TransTerrain jobs).
|
||||
int specialTile(int loc, int terrType, int lx);
|
||||
std::uint32_t actualSpecialTile(int loc);
|
||||
|
||||
// Applies a completed terrain improvement job's effect to the map.
|
||||
void completeJob(int p, int loc, int job);
|
||||
|
||||
// --- Territory Calculation ---
|
||||
|
||||
// OriginLoc: only changes in CountryRadius around this location possible,
|
||||
// -1 for complete map, -2 for double-check (debug only). PlayerLosingCity:
|
||||
// do nothing but remove tiles no longer in reach from this player's
|
||||
// territory, -1 for full border recalculation.
|
||||
void checkBorders(int originLoc, int playerLosingCity = -1);
|
||||
void logCheckBorders(int p, int cix, int playerLosingCity = -1);
|
||||
|
||||
// recalculate fPeace flags for the whole map
|
||||
void recalcPeaceMap(int p);
|
||||
|
||||
// --- Diplomacy ---
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// This file is part of cevo-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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Cevo/Protocol.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace Cevo {
|
||||
namespace UnitProcessing {
|
||||
|
||||
// Equivalent of UnitProcessing.pas (~1469 lines): unit movement/combat,
|
||||
// terrain improvement jobs, and their Start/End Game hooks. Private
|
||||
// implementation-section helpers (Controlled/GetMoveCost/CalculateJobWork)
|
||||
// live in an anonymous namespace in UnitProcessing.cpp, matching the
|
||||
// original's own interface/implementation visibility split.
|
||||
|
||||
enum TMoveType { mtInvalid, mtMove, mtCapture, mtSpyMission, mtAttack, mtBombard, mtExpel };
|
||||
|
||||
struct TMoveInfo {
|
||||
TMoveType MoveType = mtInvalid;
|
||||
int Cost = 0;
|
||||
int ToMaster = -1;
|
||||
int EndHealth = 0;
|
||||
int Defender = -1;
|
||||
int Dcix = -1;
|
||||
int Duix = -1;
|
||||
int EndHealthDef = -1;
|
||||
bool MountainDelay = false;
|
||||
};
|
||||
|
||||
extern int uixSelectedTransport;
|
||||
extern std::array<int, nPl> worked; // settler work statistics
|
||||
|
||||
// --- Moving/Combat ---
|
||||
|
||||
int hostileDamage(int p, int mix, int loc, int mp);
|
||||
int calculateMove(int p, int uix, int toLoc, int moveLength, bool testOnly, TMoveInfo &moveInfo);
|
||||
int getBattleForecast(int loc, TBattleForecast &battleForecast, int &duix, int &dcix, int &aStr, int &dStr,
|
||||
int &aBaseDamage, int &dBaseDamage);
|
||||
int loadUnit(int p, int uix, bool testOnly);
|
||||
int unloadUnit(int p, int uix, bool testOnly);
|
||||
void recover(int p, int uix);
|
||||
int getMoveAdvice(int p, int uix, TMoveAdviceData &a);
|
||||
bool canPlaneReturn(int p, int uix, const TPlaneReturnData &planeReturnData);
|
||||
|
||||
// --- Terrain Improvement ---
|
||||
|
||||
int startJob(int p, int uix, int newJob, bool testOnly);
|
||||
bool work(int p, int uix);
|
||||
int getJobProgress(int p, int loc, TJobProgressData &jobProgressData);
|
||||
|
||||
// --- Start/End Game ---
|
||||
|
||||
void initGame();
|
||||
void releaseGame();
|
||||
|
||||
} // namespace UnitProcessing
|
||||
} // namespace Cevo
|
||||
+399
-5
@@ -30,6 +30,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -55,6 +56,8 @@ int gWatching = 0;
|
||||
int gAI = 0;
|
||||
int gTurn = 0;
|
||||
int gTestFlags = 0;
|
||||
std::array<int, nPl> founded{};
|
||||
int invalidTreatyMap = 0;
|
||||
std::array<TWonderInfo, nWonder> gWonder{};
|
||||
std::array<int, nPl> grWallContinent{};
|
||||
std::array<TShipInfo, nPl> gShip{};
|
||||
@@ -78,6 +81,14 @@ int primitive = 0;
|
||||
// (number of units built, used to assign unique per-player unit IDs).
|
||||
std::array<int, nPl> unBuilt{};
|
||||
|
||||
// Equivalent of Database.pas's private `TerritoryCount: array[0..nPl]`.
|
||||
std::array<int, nPl + 1> territoryCount{};
|
||||
|
||||
// Equivalent of Database.pas's private `BorderChanges` (sIntExpandTerritory
|
||||
// and $F == 8 packed Cardinal words, 4 bits per tile in the CountryRadius
|
||||
// area around a city).
|
||||
std::array<std::uint32_t, 8> borderChanges{};
|
||||
|
||||
// Equivalent of Database.pas's private FreeSlaves().
|
||||
void freeSlaves()
|
||||
{
|
||||
@@ -99,6 +110,11 @@ void countLost(int p, int mix, int enemy)
|
||||
tellAboutModel(enemy, p, mix);
|
||||
++destroyed[enemy][p][mix];
|
||||
}
|
||||
|
||||
// Forward declarations: defined further below (Territory Calculation
|
||||
// section), needed earlier by intServer()'s sIntExpandTerritory case.
|
||||
void changeTerritory(int loc, int p);
|
||||
void expandTerritory(int originLoc);
|
||||
} // namespace
|
||||
|
||||
void maskD(std::uint32_t *x, int count, std::uint32_t mask)
|
||||
@@ -639,6 +655,175 @@ void removeUnitUpdateMap(int p, int uix)
|
||||
updateUnitMap(loc0);
|
||||
}
|
||||
|
||||
void removeDomainUnits(int d, int p, int loc)
|
||||
{
|
||||
TPlayerContext &player = rw[p];
|
||||
TUn *un = *player.Un;
|
||||
const TModel *model = *player.Model;
|
||||
for (int uix = 0; uix < player.nUn; ++uix)
|
||||
if (model[un[uix].mix].Domain == d && un[uix].Loc == loc)
|
||||
removeUnit(p, uix);
|
||||
}
|
||||
|
||||
int specialTile(int loc, int terrType, int lx)
|
||||
{
|
||||
if (terrType == fOcean)
|
||||
return 0;
|
||||
const int y = loc / lx;
|
||||
const int x = loc - y * lx;
|
||||
if (terrType == fGrass) { // formula for productive grassland
|
||||
const int v1 = lymax + x - (y >> 1);
|
||||
return (((v1 >> 1) + x + ((y + 1) >> 1)) & 1) ? 1 : 0;
|
||||
}
|
||||
// formula for special resources
|
||||
const int a = 4 * x - y + 9980;
|
||||
const int qx = a / 10;
|
||||
if (qx * 10 != a || (qx & 3) == 0)
|
||||
return 0;
|
||||
const int qy = (y + x) / 5;
|
||||
if ((qy & 3) == ((qx >> 2) & 1) * 2)
|
||||
return 0;
|
||||
if (terrType == fArctic || terrType == fSwamp)
|
||||
return 1;
|
||||
if (terrType == fShore) {
|
||||
if (((qx + qy) & 1) == 0)
|
||||
return (qx & 3) == 2 ? 2 : 1;
|
||||
return 0;
|
||||
}
|
||||
return ((qx + qy) & 1) + 1;
|
||||
}
|
||||
|
||||
std::uint32_t actualSpecialTile(int loc)
|
||||
{
|
||||
return static_cast<std::uint32_t>(specialTile(loc, static_cast<int>(realMap[loc] & fTerrain), lx));
|
||||
}
|
||||
|
||||
void foundCity(int p, int foundLoc)
|
||||
{
|
||||
TPlayerContext &player = rw[p];
|
||||
if (player.nCity == nCmax)
|
||||
return;
|
||||
++player.nCity;
|
||||
TCity &city = (*player.City)[player.nCity - 1];
|
||||
city.Size = 2;
|
||||
city.Status = 0;
|
||||
city.SavedStatus = 0;
|
||||
std::fill(std::begin(city.Built), std::end(city.Built), 0);
|
||||
city.Food = 0;
|
||||
city.Project = cpImp + imTrGoods;
|
||||
city.Prod = 0;
|
||||
city.Project0 = city.Project;
|
||||
city.Prod0 = 0;
|
||||
city.Pollution = 0;
|
||||
city.N1 = 0;
|
||||
city.Loc = foundLoc;
|
||||
if (usedByCity[foundLoc] >= 0) { // central tile is exploited - toggle in exploiting city
|
||||
int p1 = p;
|
||||
int cix1;
|
||||
searchCity(usedByCity[foundLoc], p1, cix1);
|
||||
int dx, dy;
|
||||
dxdy(usedByCity[foundLoc], foundLoc, dx, dy);
|
||||
const int v21 = ((dy + 3) << 2) + ((dx + 3) >> 1);
|
||||
(*rw[p1].City)[cix1].Tiles &= ~(1u << v21);
|
||||
}
|
||||
city.Tiles = 1u << 13; // exploit central tile
|
||||
usedByCity[foundLoc] = foundLoc;
|
||||
realMap[foundLoc] =
|
||||
(realMap[foundLoc] & (fTerrain | fSpecial | fRiver | (static_cast<std::uint32_t>(nPl) << 27))) | fCity;
|
||||
|
||||
changeTerritory(city.Loc, p);
|
||||
}
|
||||
|
||||
void completeJob(int p, int loc, int job)
|
||||
{
|
||||
assert(job != jCity);
|
||||
int changedTerrain = -1;
|
||||
switch (job) {
|
||||
case jRoad:
|
||||
realMap[loc] |= fRoad;
|
||||
break;
|
||||
case jRR:
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fRoad)) | fRR;
|
||||
break;
|
||||
case jClear:
|
||||
changedTerrain = Terrain[realMap[loc] & fTerrain].ClearTerrain;
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(changedTerrain);
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
|
||||
break;
|
||||
case jIrr:
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiIrrigation;
|
||||
break;
|
||||
case jFarm:
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiFarm;
|
||||
break;
|
||||
case jAfforest:
|
||||
changedTerrain = Terrain[realMap[loc] & fTerrain].AfforestTerrain;
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(changedTerrain);
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
|
||||
break;
|
||||
case jMine:
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiMine;
|
||||
break;
|
||||
case jFort:
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiFort;
|
||||
break;
|
||||
case jCanal:
|
||||
realMap[loc] |= fCanal;
|
||||
break;
|
||||
case jTrans:
|
||||
changedTerrain = Terrain[realMap[loc] & fTerrain].TransTerrain;
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerrain)) | static_cast<std::uint32_t>(changedTerrain);
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fSpecial)) | (actualSpecialTile(loc) << 5);
|
||||
if (((terrTypeCanalableMask >> (realMap[loc] & fTerrain)) & 1) == 0) {
|
||||
removeDomainUnits(dSea, p, loc);
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fCanal);
|
||||
}
|
||||
break;
|
||||
case jPoll:
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fPoll);
|
||||
break;
|
||||
case jBase:
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fTerImp)) | tiBase;
|
||||
break;
|
||||
case jPillage:
|
||||
if (realMap[loc] & fTerImp) {
|
||||
if ((realMap[loc] & fTerImp) == tiBase)
|
||||
removeDomainUnits(dAir, p, loc);
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fTerImp);
|
||||
} else if (realMap[loc] & fCanal) {
|
||||
removeDomainUnits(dSea, p, loc);
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fCanal);
|
||||
} else if (realMap[loc] & fRR) {
|
||||
realMap[loc] = (realMap[loc] & ~static_cast<std::uint32_t>(fRR)) | fRoad;
|
||||
} else if (realMap[loc] & fRoad) {
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fRoad);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (changedTerrain >= 0) { // remove terrain improvements if not possible on new terrain
|
||||
if (((realMap[loc] & fTerImp) == tiIrrigation || (realMap[loc] & fTerImp) == tiFarm) &&
|
||||
(Terrain[changedTerrain].IrrClearWork == 0 || Terrain[changedTerrain].ClearTerrain >= 0))
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fTerImp);
|
||||
if ((realMap[loc] & fTerImp) == tiMine &&
|
||||
(Terrain[changedTerrain].MineAfforestWork == 0 || Terrain[changedTerrain].AfforestTerrain >= 0))
|
||||
realMap[loc] &= ~static_cast<std::uint32_t>(fTerImp);
|
||||
}
|
||||
|
||||
// update map of all observing players
|
||||
if (mode > moLoading_Fast) {
|
||||
constexpr std::uint32_t mask = fTerrain | fSpecial | fTerImp | fRoad | fRR | fCanal | fPoll;
|
||||
for (int p1 = 0; p1 < nPl; ++p1) {
|
||||
if (((1 << p1) & (gAlive | gWatching)) && (((observeLevel[loc] >> (2 * p1)) & 3) > lNoObserve)) {
|
||||
std::uint32_t &tile = (*rw[p1].Map)[loc];
|
||||
tile = (tile & ~mask) | (realMap[loc] & mask);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void introduceEnemy(int p1, int p2)
|
||||
{
|
||||
rw[p1].Treaty[p2] = trNone;
|
||||
@@ -703,13 +888,18 @@ void intServer(int command, int player, int subject, const void *data)
|
||||
rwemix[p1][player][subject] = static_cast<std::int16_t>(toldPlayer.nEnemyModel);
|
||||
++toldPlayer.nEnemyModel;
|
||||
assert(toldPlayer.nEnemyModel < nemmax);
|
||||
} else if (command == sIntExpandTerritory) {
|
||||
if (mode < moPlaying) {
|
||||
std::memcpy(borderChanges.data(), data, sizeof(borderChanges));
|
||||
expandTerritory((*rw[player].City)[subject].Loc);
|
||||
}
|
||||
}
|
||||
// Other sIntXxx cases (sIntHaveContact/sIntCancelTreaty/sIntDiscoverZOC/
|
||||
// sIntExpandTerritory/sIntBuyMaterial/sIntPayPrices/sIntSetDevModel/
|
||||
// sIntSetXxxStatus) are NOT implemented yet - each needs its own
|
||||
// not-yet-ported Database.pas function (CancelTreaty, Discover9,
|
||||
// ExpandTerritory, PayPrice, DiscoverViewAreas, ...). Add them here once
|
||||
// those exist - see repo memory for the current dependency-chain status.
|
||||
// sIntBuyMaterial/sIntPayPrices/sIntSetDevModel/sIntSetXxxStatus) are NOT
|
||||
// implemented yet - each needs its own not-yet-ported Database.pas
|
||||
// function (CancelTreaty, Discover9, PayPrice, DiscoverViewAreas, ...).
|
||||
// Add them here once those exist - see repo memory for the current
|
||||
// dependency-chain status.
|
||||
}
|
||||
|
||||
void tellAboutModel(int p, int taOwner, int tamix)
|
||||
@@ -1102,5 +1292,209 @@ void updateUnitMap(int loc, bool cityChange)
|
||||
}
|
||||
}
|
||||
|
||||
void recalcPeaceMap(int p)
|
||||
{
|
||||
if (mode != moPlaying)
|
||||
return;
|
||||
TPlayerContext &player = rw[p];
|
||||
maskD(*player.Map, mapSize, ~static_cast<std::uint32_t>(fPeace));
|
||||
std::array<bool, nPl + 1> 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;
|
||||
}
|
||||
|
||||
namespace {
|
||||
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<std::uint32_t>(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<std::int8_t>(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<std::uint32_t>(gAlive)) &&
|
||||
(player.Treaty[p] == trPeace || player.Treaty[p] == trFriendlyContact))
|
||||
(*player.Map)[loc] |= fPeace;
|
||||
else
|
||||
(*player.Map)[loc] &= ~static_cast<std::uint32_t>(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<int>(newOwner));
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void checkBorders(int originLoc, int playerLosingCity)
|
||||
{
|
||||
std::array<std::int8_t, lxmax * lymax> country{};
|
||||
std::array<std::int8_t, lxmax * lymax> formerCountry{};
|
||||
std::array<std::int8_t, lxmax * lymax> dist{};
|
||||
std::array<std::int8_t, lxmax * lymax> formerDist{};
|
||||
std::array<std::int8_t, lxmax * lymax> stolenDist{};
|
||||
std::array<std::array<bool, nPl + 1>, 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<std::int8_t>(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<std::int8_t>(newDist);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
country.fill(-1);
|
||||
for (int loc = 0; loc < mapSize; ++loc)
|
||||
dist[loc] = static_cast<std::int8_t>(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<std::int8_t>(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<std::int8_t>(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<int>(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<std::uint32_t>(newOwner) << ((i & 7) * 4);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int loc = 0; loc < mapSize; ++loc) {
|
||||
const int oldOwner = static_cast<int>(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());
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
} // namespace Cevo
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user