// 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. #include "GameServer.h" #include "GameServerInternal.h" #include "Global.h" #include "Cevo/CityProcessing.h" #include "Cevo/CmdList.h" #include "Cevo/Database.h" #include "Cevo/Map.h" #include "Cevo/Platform.h" #include "Cevo/UnitProcessing.h" #include #include #include #include #include #include #include #include #include #include #include namespace Cevo { namespace GameServer { int executeMove(int p, int uix, int toLoc, UnitProcessing::TMoveInfo &moveInfo, TShowMove showMove) { int result = 0; TPlayerContext &player = Database::rw[p]; TUn &un = (*player.Un)[uix]; const TModel &model = (*player.Model)[un.mix]; int fromLoc = un.Loc; if (un.Master < 0) Database::freeUnit(p, uix); if ((moveInfo.MoveType == UnitProcessing::mtMove || moveInfo.MoveType == UnitProcessing::mtCapture) && moveInfo.MountainDelay) un.Flags |= unMountainDelay; un.Loc = -2; if (un.TroopLoad + un.AirLoad > 0) { TUn *allUn = *player.Un; for (int i = 0; i < player.nUn; ++i) if (allUn[i].Loc >= 0 && allUn[i].Master == uix) allUn[i].Loc = -2; } Database::updateUnitMap(fromLoc); if (Database::mode >= Database::moMovie) { // show move in interface modules showMove.EndHealth = moveInfo.EndHealth; showMove.EndHealthDef = -1; if (un.Master >= 0) { if ((*player.Model)[(*player.Un)[un.Master].mix].Domain == dAir) showMove.Flags |= umPlaneUnloading; else showMove.Flags |= umShipUnloading; } if (moveInfo.ToMaster >= 0) { if ((*player.Model)[(*player.Un)[moveInfo.ToMaster].mix].Domain == dAir) showMove.Flags |= umPlaneLoading; else showMove.Flags |= umShipLoading; } for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & Database::gWatching) && (p1 != p || playerBrain[p1]->kind == BrainType::Term)) { std::uint32_t minLevel; if (model.Cap[mcStealth] > 0) minLevel = Database::lObserveSuper; else if (model.Cap[mcSub] > 0) minLevel = Database::lObserveAll; else minLevel = Database::lObserveUnhidden; const bool seeFrom = p1 == p || ((Database::observeLevel[fromLoc] >> (2 * p1)) & 3) >= minLevel; const bool seeTo = p1 == p || ((Database::observeLevel[toLoc] >> (2 * p1)) & 3) >= minLevel; if (seeFrom && seeTo) { Database::tellAboutModel(p1, p, un.mix); showMove.emix = (p1 == p) ? -1 : Database::emixSafe(p1, p, un.mix); callPlayer(moveInfo.MoveType == UnitProcessing::mtCapture ? cShowCapturing : cShowMoving, p1, &showMove); } else if (seeFrom) { callPlayer(cShowUnitChanged, p1, &fromLoc); } } } } if (moveInfo.MoveType != UnitProcessing::mtSpyMission) un.Loc = toLoc; if (un.TroopLoad + un.AirLoad > 0) { TUn *allUn = *player.Un; for (int i = 0; i < player.nUn; ++i) if (allUn[i].Loc == -2) allUn[i].Loc = toLoc; } bool extDiscover = false; int nUpdateLoc = 0; std::array updateLoc{}; if (moveInfo.MoveType == UnitProcessing::mtCapture) { assert(Database::occupant[toLoc] < 0); TPlayerContext &defender = Database::rw[moveInfo.Defender]; TUn *defUn = *defender.Un; for (int uix1 = 0; uix1 < defender.nUn; ++uix1) { if (defUn[uix1].Loc >= 0 && defUn[uix1].Home == moveInfo.Dcix) { updateLoc[static_cast(nUpdateLoc)] = defUn[uix1].Loc; ++nUpdateLoc; } } // unit will be removed -- remember position and update for all players if ((*defender.City)[moveInfo.Dcix].Size > 2 && player.nCity < nCmax) { // city captured changeCityOwnerTellPlayers(moveInfo.Defender, moveInfo.Dcix, p); TCity &newCity = (*player.City)[player.nCity - 1]; newCity.Flags = static_cast(CaptureTurns << 16); CityProcessing::cityShrink(p, player.nCity - 1); if (Database::mode == Database::moPlaying) savedTiles[static_cast(player.nCity - 1)] = newCity.Tiles; extDiscover = true; // Temple of Zeus effect if (Database::gWonder[woZeus].EffectiveOwner == p) { Database::giveCivilReport(p, moveInfo.Defender); for (int i = 0; i < nAdv; ++i) { if (!IsFutureTech(i) && player.Tech[i] < tsSeen && defender.Tech[i] >= tsApplicable) { player.Happened |= phStealTech; gStealFrom = moveInfo.Defender; break; } } } if (Database::mode == Database::moPlaying) Database::logCheckBorders(p, player.nCity - 1, moveInfo.Defender); // territory should not be considered for the rest of the command // execution, because during loading a game it's incorrect before // subsequent sIntExpandTerritory is processed } else { // city destroyed destroyCityTellPlayers(moveInfo.Defender, moveInfo.Dcix, false); Database::checkBorders(toLoc, moveInfo.Defender); } Database::recalcPeaceMap(p); if (Database::mode >= Database::moMovie) std::memcpy(player.Wonder, Database::gWonder.data(), sizeof(player.Wonder)); } // if MoveInfo.MoveType = mtCapture if (moveInfo.MoveType == UnitProcessing::mtSpyMission) { std::uint32_t missionResult = Database::doSpyMission(p, moveInfo.Defender, moveInfo.Dcix, spyMission); if (Database::mode == Database::moPlaying && spyMission == smStealForeignReports) callPlayer(cShowMissionResult, p, &missionResult); } un.Health = static_cast(moveInfo.EndHealth); un.Movement = static_cast(un.Movement - moveInfo.Cost); // transport unload if (un.Master >= 0) { if (model.Domain == dAir) --(*player.Un)[un.Master].AirLoad; else { --(*player.Un)[un.Master].TroopLoad; assert(un.Movement <= 0); } un.Master = -1; } if (un.Health <= 0 || moveInfo.MoveType == UnitProcessing::mtSpyMission) { Database::removeUnit(p, uix); // spy mission or victim of HostileDamage } else { // transport load un.Master = moveInfo.ToMaster; if (moveInfo.ToMaster >= 0) { if (model.Domain == dAir) ++(*player.Un)[moveInfo.ToMaster].AirLoad; else ++(*player.Un)[moveInfo.ToMaster].TroopLoad; } else { Database::placeUnit(p, uix); } } if (moveInfo.MoveType == UnitProcessing::mtCapture && nUpdateLoc > 0) Database::recalcMapZoC(p); Database::updateUnitMap(toLoc, moveInfo.MoveType == UnitProcessing::mtCapture); for (int i = 0; i < nUpdateLoc; ++i) Database::updateUnitMap(updateLoc[static_cast(i)]); // tell about lost units of defender if (moveInfo.MoveType != UnitProcessing::mtSpyMission && un.Master < 0) { if (model.Kind == mkDiplomat || model.Domain == dAir || model.Cap[mcRadar] + model.Cap[mcCarrier] + model.Cap[mcAcademy] > 0 || (Database::realMap[toLoc] & fTerrain) == static_cast(fMountains) || (Database::realMap[toLoc] & fTerImp) == tiFort || (Database::realMap[toLoc] & fTerImp) == tiBase) extDiscover = true; std::uint32_t level; if (model.Kind == mkDiplomat || model.Cap[mcSpy] > 0) level = Database::lObserveSuper; else if (model.Domain == dAir || model.Cap[mcRadar] + model.Cap[mcCarrier] > 0) level = Database::lObserveAll; else level = Database::lObserveUnhidden; if (extDiscover) { if (Database::discover21(toLoc, p, static_cast(level), true, model.Domain == dGround)) result |= rEnemySpotted; } else { if (Database::discover9(toLoc, p, static_cast(level), true, model.Domain == dGround)) result |= rEnemySpotted; } } if (Database::mode >= Database::moMovie) { // show after-move in interface modules for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & Database::gWatching) && (p1 != p || playerBrain[p1]->kind == BrainType::Term)) { std::uint32_t minLevel; if (model.Cap[mcStealth] > 0) minLevel = Database::lObserveSuper; else if (model.Cap[mcSub] > 0) minLevel = Database::lObserveAll; else minLevel = Database::lObserveUnhidden; const bool seeFrom = p1 == p || ((Database::observeLevel[fromLoc] >> (2 * p1)) & 3) >= minLevel; const bool seeTo = p1 == p || ((Database::observeLevel[toLoc] >> (2 * p1)) & 3) >= minLevel; if (seeTo && moveInfo.MoveType == UnitProcessing::mtCapture) callPlayer(cShowCityChanged, p1, &toLoc); if (seeFrom && seeTo) callPlayer(cShowAfterMove, p1, &toLoc); else if (moveInfo.MoveType != UnitProcessing::mtSpyMission && seeTo) callPlayer(cShowUnitChanged, p1, &toLoc); for (int i = 0; i < nUpdateLoc; ++i) if (((Database::observeLevel[updateLoc[static_cast(i)]] >> (2 * p1)) & 3) >= Database::lObserveUnhidden) callPlayer(cShowUnitChanged, p1, &updateLoc[static_cast(i)]); } } } return result; } void ensureBattleHistoryCapacity(TPlayerContext &player) { if (player.nBattleHistory == 0) { player.BattleHistory = reinterpret_cast(new TBattle[16]()); } else if (player.nBattleHistory >= 16 && (player.nBattleHistory & (player.nBattleHistory - 1)) == 0) { TBattle *oldArr = reinterpret_cast(player.BattleHistory); auto *newArr = new TBattle[static_cast(player.nBattleHistory) * 2](); std::copy(oldArr, oldArr + player.nBattleHistory, newArr); delete[] oldArr; player.BattleHistory = reinterpret_cast(newArr); } } void writeBattleHistory(int toLoc, int fromLoc, int attacker, int defender, int mixAttacker, int mixDefender, bool attackerLost, bool defenderLost) { TPlayerContext &attackerPlayer = Database::rw[attacker]; ensureBattleHistoryCapacity(attackerPlayer); TBattle &attackerBattle = (*attackerPlayer.BattleHistory)[attackerPlayer.nBattleHistory]; ++attackerPlayer.nBattleHistory; TPlayerContext &defenderPlayer = Database::rw[defender]; ensureBattleHistoryCapacity(defenderPlayer); TBattle &defenderBattle = (*defenderPlayer.BattleHistory)[defenderPlayer.nBattleHistory]; ++defenderPlayer.nBattleHistory; attackerBattle.Enemy = static_cast(defender); attackerBattle.Flags = 0; attackerBattle.Turn = static_cast(Database::gTurn); attackerBattle.mix = static_cast(mixAttacker); attackerBattle.mixEnemy = static_cast(mixDefender); attackerBattle.ToLoc = toLoc; attackerBattle.FromLoc = fromLoc; defenderBattle.Enemy = static_cast(attacker); defenderBattle.Flags = bhEnemyAttack; defenderBattle.Turn = static_cast(Database::gTurn); defenderBattle.mix = static_cast(mixDefender); defenderBattle.mixEnemy = static_cast(mixAttacker); defenderBattle.ToLoc = toLoc; defenderBattle.FromLoc = fromLoc; if (attackerLost) { attackerBattle.Flags |= bhMyUnitLost; defenderBattle.Flags |= bhEnemyUnitLost; } if (defenderLost) { attackerBattle.Flags |= bhEnemyUnitLost; defenderBattle.Flags |= bhMyUnitLost; } } int executeAttack(int p, int uix, int toLoc, UnitProcessing::TMoveInfo &moveInfo, TShowMove showMove) { int result = 0; TPlayerContext &player = Database::rw[p]; TUn &un = (*player.Un)[uix]; const TModel &model = (*player.Model)[un.mix]; int fromLoc = un.Loc; showMove.EndHealth = moveInfo.EndHealth; showMove.EndHealthDef = moveInfo.EndHealthDef; if (moveInfo.MoveType == UnitProcessing::mtAttack) writeBattleHistory(toLoc, fromLoc, p, moveInfo.Defender, un.mix, (*Database::rw[moveInfo.Defender].Un)[moveInfo.Duix].mix, moveInfo.EndHealth <= 0, moveInfo.EndHealthDef <= 0); if (Database::mode >= Database::moMovie) { // show attack in interface modules for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & Database::gWatching) && (p1 != p || playerBrain[p1]->kind == BrainType::Term)) { const bool seeFrom = ((Database::observeLevel[fromLoc] >> (2 * p1)) & 3) >= Database::lObserveUnhidden; const bool seeTo = ((Database::observeLevel[toLoc] >> (2 * p1)) & 3) >= Database::lObserveUnhidden; if (seeFrom && seeTo) { Database::tellAboutModel(p1, p, un.mix); showMove.emix = (p1 == p) ? -1 : Database::emixSafe(p1, p, un.mix); callPlayer(cShowAttacking, p1, &showMove); } } } } bool loseCityPop = false; TPlayerContext *defenderPlayer = (moveInfo.Defender >= 0) ? &Database::rw[moveInfo.Defender] : nullptr; TCity *defenderCity = defenderPlayer ? &(*defenderPlayer->City)[moveInfo.Dcix] : nullptr; if ((Database::realMap[toLoc] & fCity) && ((moveInfo.MoveType == UnitProcessing::mtAttack && moveInfo.EndHealthDef <= 0) || (moveInfo.MoveType == UnitProcessing::mtBombard && (BombardmentDestroysCity || defenderCity->Size > 2)))) { switch (model.Domain) { case dGround: loseCityPop = model.Cap[mcArtillery] > 0 || (defenderCity->Built[imWalls] == 0 && Database::continent[toLoc] != Database::grWallContinent[moveInfo.Defender]); break; case dSea: loseCityPop = defenderCity->Built[imCoastalFort] == 0; break; case dAir: loseCityPop = defenderCity->Built[imMissileBat] == 0; break; default: break; } } const bool cityDestroyed = loseCityPop && defenderCity && defenderCity->Size <= 2; if (moveInfo.MoveType == UnitProcessing::mtBombard) { assert(un.Movement >= 100); if (model.Attack == 0) un.Flags &= ~static_cast(unBombsLoaded); un.Movement -= 100; } else if (moveInfo.MoveType == UnitProcessing::mtExpel) { assert(un.Movement >= 100); un.Job = jNone; un.Flags &= ~static_cast(unFortified); un.Movement -= 100; } else { assert(moveInfo.MoveType == UnitProcessing::mtAttack); if (moveInfo.EndHealth == 0) { Database::removeUnit(p, uix, moveInfo.Defender); // destroy attacker } else { // update attacker const int expGain = (un.Health - moveInfo.EndHealth + 1) >> 1; if (un.Exp + expGain > (nExp - 1) * ExpCost) un.Exp = static_cast((nExp - 1) * ExpCost); else un.Exp = static_cast(un.Exp + expGain); un.Health = static_cast(moveInfo.EndHealth); un.Job = jNone; if ((*defenderPlayer->Model)[(*defenderPlayer->Un)[moveInfo.Duix].mix].Domain < dAir) un.Flags &= ~static_cast(unBombsLoaded); un.Flags &= ~static_cast(unFortified); if (un.Movement > 100) un.Movement -= 100; else un.Movement = 0; } } bool zocDefenderDestroyed = false; int nUpdateLoc = 0; std::array updateLoc{}; int expelToLoc = -1; if (moveInfo.MoveType == UnitProcessing::mtExpel) { // expel friendly unit TUn &defUn = (*defenderPlayer->Un)[moveInfo.Duix]; TCity *defCity = *defenderPlayer->City; if (defUn.Home >= 0) { expelToLoc = defCity[defUn.Home].Loc; } else { expelToLoc = defCity[0].Loc; // search destination for homeless units for (int cix1 = 1; cix1 < defenderPlayer->nCity; ++cix1) if (defCity[cix1].Loc >= 0 && (expelToLoc < 0 || defCity[cix1].Built[imPalace] > 0)) expelToLoc = defCity[cix1].Loc; } if (expelToLoc >= 0) { Database::freeUnit(moveInfo.Defender, moveInfo.Duix); defUn.Loc = expelToLoc; Database::placeUnit(moveInfo.Defender, moveInfo.Duix); updateLoc[static_cast(nUpdateLoc)] = defUn.Loc; ++nUpdateLoc; defUn.Flags |= unWithdrawn; } } else if (moveInfo.MoveType == UnitProcessing::mtAttack && moveInfo.EndHealthDef > 0) { // update defender TUn &defUn = (*defenderPlayer->Un)[moveInfo.Duix]; const int expGain = (defUn.Health - moveInfo.EndHealthDef + 1) >> 1; if (defUn.Exp + expGain > (nExp - 1) * ExpCost) defUn.Exp = static_cast((nExp - 1) * ExpCost); else defUn.Exp = static_cast(defUn.Exp + expGain); defUn.Health = static_cast(moveInfo.EndHealthDef); } else { // destroy defenders if (moveInfo.MoveType != UnitProcessing::mtBombard) { zocDefenderDestroyed = ((*defenderPlayer->Model)[(*defenderPlayer->Un)[moveInfo.Duix].mix].Flags & mdZOC) != 0; if ((((Database::realMap[toLoc] & fCity) == 0) && ((Database::realMap[toLoc] & fTerImp) != tiBase) && ((Database::realMap[toLoc] & fTerImp) != tiFort)) || (loseCityPop && defenderCity->Size == 2)) // no city, base or fortress Database::removeAllUnits(moveInfo.Defender, toLoc, p); else Database::removeUnit(moveInfo.Defender, moveInfo.Duix, p); } if (loseCityPop) { // city defender defeated -- shrink city if (!cityDestroyed) { CityProcessing::cityShrink(moveInfo.Defender, moveInfo.Dcix); } else { TUn *defUnits = *defenderPlayer->Un; for (int uix1 = 0; uix1 < defenderPlayer->nUn; ++uix1) { if (defUnits[uix1].Loc >= 0 && defUnits[uix1].Home == moveInfo.Dcix) { updateLoc[static_cast(nUpdateLoc)] = defUnits[uix1].Loc; ++nUpdateLoc; } } // unit will be removed -- remember position and update for all players destroyCityTellPlayers(moveInfo.Defender, moveInfo.Dcix, false); Database::checkBorders(toLoc, moveInfo.Defender); Database::recalcPeaceMap(p); } } } if (cityDestroyed && nUpdateLoc > 0) Database::recalcMapZoC(p); else if (zocDefenderDestroyed) Database::recalcV8ZoC(p, toLoc); Database::updateUnitMap(fromLoc); Database::updateUnitMap(toLoc, loseCityPop); for (int i = 0; i < nUpdateLoc; ++i) Database::updateUnitMap(updateLoc[static_cast(i)]); // tell about lost units of defender if (Database::mode >= Database::moMovie) { TModelInfo *enemyModel = *player.EnemyModel; for (int i = 0; i < player.nEnemyModel; ++i) enemyModel[i].Lost = Database::destroyed[p][enemyModel[i].Owner][enemyModel[i].mix]; for (int p1 = 0; p1 < nPl; ++p1) { // show after-attack in interface modules if (((1 << p1) & Database::gWatching) && (p1 != p || playerBrain[p1]->kind == BrainType::Term)) { const bool seeFrom = ((Database::observeLevel[fromLoc] >> (2 * p1)) & 3) >= Database::lObserveUnhidden; const bool seeTo = ((Database::observeLevel[toLoc] >> (2 * p1)) & 3) >= Database::lObserveUnhidden; if (seeTo && cityDestroyed) callPlayer(cShowCityChanged, p1, &toLoc); // city was destroyed if (seeFrom && seeTo) { callPlayer(cShowAfterAttack, p1, &toLoc); callPlayer(cShowAfterAttack, p1, &fromLoc); } else { if (seeTo) callPlayer(cShowUnitChanged, p1, &toLoc); if (seeFrom) callPlayer(cShowUnitChanged, p1, &fromLoc); } if (seeTo && moveInfo.MoveType == UnitProcessing::mtExpel && expelToLoc >= 0) callPlayer(cShowUnitChanged, p1, &expelToLoc); } } } return result; } int moveUnit(int p, int uix, int dx, int dy, bool testOnly) { assert(((1 << p) & Database::invalidTreatyMap) == 0); TPlayerContext &player = Database::rw[p]; TUn &un = (*player.Un)[uix]; const int toLoc = Database::dLoc(un.Loc, dx, dy); if (toLoc < 0 || toLoc >= Database::mapSize) return eInvalid; UnitProcessing::TMoveInfo moveInfo; int result = UnitProcessing::calculateMove(p, uix, toLoc, 3 - (dy & 1), testOnly, moveInfo); if (result == eZOC_EnemySpotted) zocTile = toLoc; if (result >= rExecuted && !testOnly) { TShowMove showMove{}; showMove.dx = dx; showMove.dy = dy; showMove.FromLoc = un.Loc; showMove.mix = un.mix; showMove.Health = un.Health; showMove.Fuel = un.Fuel; showMove.Exp = un.Exp; showMove.Load = un.TroopLoad + un.AirLoad; showMove.Owner = p; showMove.Flags = (un.TroopLoad > 0 || un.AirLoad > 0) ? unMulti : 0; switch (moveInfo.MoveType) { case UnitProcessing::mtCapture: showMove.Flags |= umCapturing; break; case UnitProcessing::mtSpyMission: showMove.Flags |= umSpyMission; break; case UnitProcessing::mtBombard: showMove.Flags |= umBombarding; break; case UnitProcessing::mtExpel: showMove.Flags |= umExpelling; break; default: break; } switch (moveInfo.MoveType) { case UnitProcessing::mtMove: case UnitProcessing::mtCapture: case UnitProcessing::mtSpyMission: result = executeMove(p, uix, toLoc, moveInfo, showMove) | result; break; case UnitProcessing::mtAttack: case UnitProcessing::mtBombard: case UnitProcessing::mtExpel: result = executeAttack(p, uix, toLoc, moveInfo, showMove) | result; break; default: break; } } return result; } int countPrice(const TOffer &offer, std::uint32_t priceType) { int result = 0; for (int i = 0; i < offer.nDeliver + offer.nCost; ++i) if ((static_cast(offer.Price[i]) & 0xFFFF0000u) == priceType) ++result; return result; } } // namespace GameServer } // namespace Cevo