// 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. #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 { void beforeTurn0() { TPlayerContext &player = Database::rw[pTurn]; TUn *un = *player.Un; const TModel *model = *player.Model; for (int uix = 0; uix < player.nUn; ++uix) // init movement points for first turn un[uix].Movement = model[un[uix].mix].Speed; if (Database::difficulty[pTurn] > 0) { Database::discoverViewAreas(pTurn); } else { // supervisor Database::discoverAll(pTurn, Database::lObserveSuper); for (int p1 = 1; p1 < nPl; ++p1) { if ((1 << p1) & Database::gAlive) { Database::giveCivilReport(pTurn, p1); Database::giveMilReport(pTurn, p1); } } } } void destroySpacePortTellPlayers(int p, int pCapturer) { // stop ship part production TCity *city = *Database::rw[p].City; for (int cix = 0; cix < Database::rw[p].nCity; ++cix) { TCity &c = city[cix]; if (c.Loc >= 0 && (c.Project & cpImp) && ((c.Project & cpIndex) == woMir || Imp[c.Project & cpIndex].Kind == ikShipPart)) { Database::rw[p].Money += c.Prod0; c.Prod = 0; c.Prod0 = 0; c.Project = cpImp + imTrGoods; c.Project0 = cpImp + imTrGoods; } } // destroy ship TShipInfo &ship = Database::gShip[p]; if (ship.Parts[0] + ship.Parts[1] + ship.Parts[2] > 0) { TShowShipChange showShipChange{}; for (int i = 0; i < nShipPart; ++i) { showShipChange.Ship1Change[i] = -ship.Parts[i]; if (pCapturer >= 0) { showShipChange.Ship2Change[i] = ship.Parts[i]; Database::gShip[pCapturer].Parts[i] += ship.Parts[i]; } ship.Parts[i] = 0; } if (Database::mode >= Database::moMovie) { showShipChange.Reason = pCapturer >= 0 ? scrCapture : scrDestruction; showShipChange.Ship1Owner = p; showShipChange.Ship2Owner = pCapturer; for (int p1 = 0; p1 < nPl; ++p1) { if ((1 << p1) & (Database::gAlive | Database::gWatching)) { std::memcpy(Database::rw[p1].Ship, Database::gShip.data(), sizeof(Database::rw[p1].Ship)); if ((1 << p1) & Database::gWatching) callPlayer(cShowShipChange, p1, &showShipChange); } } } } } void destroyCityTellPlayers(int p, int cix, bool saveUnits) { if ((*Database::rw[p].City)[cix].Built[imSpacePort] > 0) destroySpacePortTellPlayers(p, -1); Database::destroyCity(p, cix, saveUnits); } void changeCityOwnerTellPlayers(int pOld, int cixOld, int pNew) { if ((*Database::rw[pOld].City)[cixOld].Built[imSpacePort] > 0) { if (Database::rw[pNew].NatBuilt[imSpacePort] > 0) destroySpacePortTellPlayers(pOld, pNew); else destroySpacePortTellPlayers(pOld, -1); } Database::changeCityOwner(pOld, cixOld, pNew); } void checkWin(int p) { bool shipComplete = true; for (int i = 0; i < nShipPart; ++i) if (Database::gShip[p].Parts[i] < ShipNeed[i]) { shipComplete = false; break; } if (shipComplete) gWinner |= (1 << p); // game won! } void beforeTurn() { assert((1 << pTurn) & (Database::gAlive | Database::gWatching)); if (((1 << pTurn) & Database::gAlive) == 0 && Database::difficulty[pTurn] > 0) return; TPlayerContext &player = Database::rw[pTurn]; if (Database::gWonder[woGrLibrary].EffectiveOwner == pTurn && gWinner == 0) { // check great library effect int nAlive = 0; for (int p1 = 0; p1 < nPl; ++p1) if ((1 << p1) & Database::gAlive) ++nAlive; for (int ad = 0; ad <= nAdv - 5; ++ad) { if (player.Tech[ad] < tsSeen) { int nAppliers = 0; for (int p1 = 0; p1 < nPl; ++p1) if (p1 != pTurn && ((1 << p1) & Database::gAlive) && Database::rw[p1].Tech[ad] >= tsApplicable) ++nAppliers; if (nAppliers * 2 > nAlive) { Database::seeTech(pTurn, ad); ++Database::nTech[pTurn]; if (Database::mode >= Database::moMovie) callPlayer(cShowGreatLibTech, pTurn, &ad); // do not call CallPlayer(pTurn) while map is invalid } } } } Database::maskD(Database::observeLevel.data(), Database::mapSize, ~static_cast(3 << (2 * pTurn))); if (Database::mode > Database::moLoading_Fast) Database::maskD(*player.Map, Database::mapSize, ~static_cast(fUnit | fHiddenUnit | fStealthUnit | fObserved | fSpiedOut | fOwned | fOwnZoCUnit | fInEnemyZoC)); player.nEnemyUn = 0; bool mirBuilt = false; if (Database::difficulty[pTurn] > 0 && gWinner == 0) { if (player.nCity > 0) { TCity *city0 = *player.City; TUn *un0 = *player.Un; const TModel *model0 = *player.Model; for (int p1 = 0; p1 < nPl; ++p1) { if (Database::gTurn != player.EvaStart[p1] + PeaceEvaTurns) continue; // peace contract -- remove all units from p1's territory int loc1 = city0[0].Loc; // search destination for homeless units for (int cix = 1; cix < player.nCity; ++cix) if (city0[cix].Loc >= 0 && (loc1 < 0 || city0[cix].Built[imPalace] > 0)) loc1 = city0[cix].Loc; for (int uix = 0; uix < player.nUn; ++uix) { TUn &un = un0[uix]; if (un.Loc >= 0 && model0[un.mix].Kind != mkDiplomat && (un.Home >= 0 || loc1 >= 0) && (Database::realMap[un.Loc] >> 27) == static_cast(p1)) { const int oldLoc = un.Loc; if (un.Master >= 0) { // transport unload if (model0[un.mix].Domain == dAir) --un0[un.Master].AirLoad; else --un0[un.Master].TroopLoad; un.Master = -1; } else { Database::freeUnit(pTurn, uix); } un.Loc = un.Home >= 0 ? city0[un.Home].Loc : loc1; Database::placeUnit(pTurn, uix); Database::updateUnitMap(oldLoc); Database::updateUnitMap(un.Loc); un.Flags |= unWithdrawn; player.Happened |= phPeaceEvacuation; } } } } TShowShipChange showShipChange{}; bool tribeExtinct = true; int nUpdateLoc = 0; std::array updateLoc{}; TCity *city = *player.City; for (int cix = 0; cix < player.nCity; ++cix) { // city loop 1 TCity &c = city[cix]; if (c.Loc < 0) continue; // check for siege int siegedTiles = 0; Database::Vicinity21Loc radius; Database::v21ToLoc(c.Loc, radius); for (int v21 = 1; v21 <= 26; ++v21) { if ((c.Tiles & (1u << v21) & ~(1u << Database::CityOwnTile)) == 0) continue; const int loc1 = radius[static_cast(v21)]; assert(loc1 >= 0 && loc1 < Database::mapSize && Database::usedByCity[loc1] == c.Loc); const auto p1 = static_cast(Database::realMap[loc1] >> 27); if ((Database::realMap[loc1] & fCity) || (p1 < nPl && p1 != pTurn && player.Treaty[p1] >= trPeace) || (Database::zocMap[loc1] > 0 && Database::occupant[loc1] != pTurn && player.Treaty[Database::occupant[loc1]] < trPeace)) { c.Tiles &= ~(1u << v21); Database::usedByCity[loc1] = -1; c.Flags |= chSiege; ++siegedTiles; } } while (siegedTiles > 0) { // replace sieged tiles if (!CityProcessing::addBestCityTile(pTurn, cix)) break; --siegedTiles; } if ((c.Flags & chFounded) == 0) { if (CityProcessing::cityTurn(pTurn, cix)) { tribeExtinct = false; } else { // city is erased Database::removeDomainUnits(dSea, pTurn, c.Loc); Database::removeDomainUnits(dAir, pTurn, c.Loc); (*player.Map)[c.Loc] &= ~static_cast(fCity); // !!! do this in inner core updateLoc[static_cast(nUpdateLoc)] = c.Loc; ++nUpdateLoc; destroyCityTellPlayers(pTurn, cix, true); } if ((c.Flags & chProduction) && (c.Project0 & cpImp)) { if ((c.Project0 & cpIndex) == woMir) mirBuilt = true; else if ((c.Project0 & cpIndex) == woManhattan) gColdWarStart = Database::gTurn; else if (Imp[c.Project0 & cpIndex].Kind == ikShipPart) // ship parts produced ++showShipChange.Ship1Change[(c.Project0 & cpIndex) - imShipComp]; } } } // city loop 1 if (nUpdateLoc > 0) { Database::checkBorders(-1, pTurn); for (int i = 0; i < nUpdateLoc; ++i) Database::updateUnitMap(updateLoc[static_cast(i)], true); if (Database::mode >= Database::moMovie) for (int p1 = 0; p1 < nPl; ++p1) if (((1 << p1) & Database::gWatching) && p1 != pTurn) for (int i = 0; i < nUpdateLoc; ++i) if (((Database::observeLevel[updateLoc[static_cast(i)]] >> (2 * p1)) & 3) >= Database::lObserveUnhidden) callPlayer(cShowCityChanged, p1, &updateLoc[static_cast(i)]); } { TUn *un = *player.Un; const TModel *model = *player.Model; for (int uix = 0; uix < player.nUn; ++uix) { // unit loop 2 TUn &u = un[uix]; if (u.Loc < 0) continue; if (u.Health < 100) UnitProcessing::recover(pTurn, uix); if (u.Flags & unMountainDelay) { u.Movement = 0; u.Flags &= ~static_cast(unMountainDelay); } else { u.Movement = Database::unitSpeed(pTurn, u.mix, u.Health); // refresh movement } assert(u.Loc >= 0); if (model[u.mix].Kind != mkDiplomat) { // check treaty violation const auto p1 = static_cast(Database::realMap[u.Loc] >> 27); if (p1 < nPl && p1 != pTurn && player.Treaty[p1] >= trPeace) { if ((u.Job == jCity || u.Job == jPillage || u.Job == jClear || u.Job == jAfforest || u.Job == jTrans) || ((u.Job == jIrr || u.Job == jMine || u.Job == jFort || u.Job == jBase) && (Database::realMap[u.Loc] & fTerImp))) u.Job = jNone; if (Database::gTurn > player.EvaStart[p1] + PeaceEvaTurns && player.Treaty[p1] != trAlliance) { player.EvaStart[p1] = Database::gTurn; player.Happened |= phPeaceViolation; if (Database::mode >= Database::moMovie) callPlayer(cShowPeaceViolation, p1, &pTurn); } } } if (Database::serverVersion[pTurn] >= 0x000EF0) { if (u.Health <= 0 || tribeExtinct) Database::removeUnitUpdateMap(pTurn, uix); } } } if (Database::serverVersion[pTurn] < 0x000EF0) { TUn *un = *player.Un; bool jobDone = false; // intentionally not reset per-iteration, matches the original's stale read for (int uix = 0; uix < player.nUn; ++uix) { // unit loop 3 TUn &u = un[uix]; if (u.Loc < 0) continue; int loc1 = u.Loc; const int job0 = u.Job; if (u.Job != jNone) jobDone = UnitProcessing::work(pTurn, uix); // settlers do terrain improvement jobs if (u.Health <= 0 || tribeExtinct) Database::removeUnitUpdateMap(pTurn, uix); if (job0 == jCity && jobDone) { // new city CityProcessing::addBestCityTile(pTurn, player.nCity - 1); Database::updateUnitMap(loc1, true); if (Database::mode >= Database::moMovie) // tell enemies for (int p1 = 0; p1 < nPl; ++p1) if (((1 << p1) & Database::gWatching) && p1 != pTurn && (Database::observeLevel[loc1] & (3 << (2 * p1)))) callPlayer(cShowCityChanged, p1, &loc1); } } } { // pollution - city loop 3 TCity *cityList = *player.City; for (int cix = 0; cix < player.nCity; ++cix) if (cityList[cix].Loc >= 0 && cityList[cix].Pollution >= MaxPollution) CityProcessing::pollute(pTurn, cix); } Database::compactLists(pTurn); if (player.nUn == 0 && player.nCity == 0) { // nation made extinct player.Happened |= phExtinct; Database::gAlive &= ~(1 << pTurn); stat[stPop][pTurn][static_cast(Database::gTurn)] = 0; stat[stMil][pTurn][static_cast(Database::gTurn)] = 0; stat[stScience][pTurn][static_cast(Database::gTurn)] = 0; stat[stExplore][pTurn][static_cast(Database::gTurn)] = 0; stat[stTerritory][pTurn][static_cast(Database::gTurn)] = 0; stat[stWork][pTurn][static_cast(Database::gTurn)] = 0; for (int p1 = 0; p1 < nPl; ++p1) { if (((1 << p1) & (Database::gAlive | Database::gWatching)) == 0) continue; if (p1 != pTurn) { Database::giveCivilReport(p1, pTurn); if ((Database::gTestFlags & tfUncover) || Database::difficulty[p1] == 0 || Database::rw[p1].Treaty[pTurn] == trAlliance) Database::giveMilReport(p1, pTurn); } TPlayerContext &other = Database::rw[p1]; other.Alive = Database::gAlive; for (int loc1 = 0; loc1 < Database::mapSize; ++loc1) // remove territory of extinct nation from player maps if ((*other.Territory)[loc1] == pTurn) { (*other.Territory)[loc1] = -1; (*other.Map)[loc1] &= ~static_cast(fPeace); } } return; } // check research const int cost = Database::techCost(pTurn); if (Database::gTestFlags & tfImmAdvance) player.Research = cost; if ((player.Happened & phTech) == 0 && player.Research >= cost) { if (player.ResearchTech == adMilitary) Database::enableDevModel(pTurn); // new Unit class initiated else if (player.ResearchTech >= 0) Database::discoverTech(pTurn, player.ResearchTech); player.Research -= cost; player.Happened |= phTech; player.ResearchTech = -1; } else if (player.ResearchTech == -2 && player.nCity > 0) { player.Happened |= phTech; player.ResearchTech = -1; } if (player.Credibility < player.MaxCredibility) { for (int p1 = 0; p1 < nPl; ++p1) { if (p1 != pTurn && ((1 << p1) & Database::gAlive) && player.Treaty[p1] >= trPeace) { ++player.Credibility; break; } } } if (gWinner == 0) checkWin(pTurn); if (Database::mode >= Database::moMovie && gWinner == 0 && (showShipChange.Ship1Change[0] > 0 || showShipChange.Ship1Change[1] > 0 || showShipChange.Ship1Change[2] > 0)) { showShipChange.Reason = scrProduction; showShipChange.Ship1Owner = pTurn; showShipChange.Ship2Owner = -1; for (int p1 = 0; p1 < nPl; ++p1) { if (p1 != pTurn && ((1 << p1) & (Database::gAlive | Database::gWatching))) { std::memcpy(Database::rw[p1].Ship, Database::gShip.data(), sizeof(Database::rw[p1].Ship)); if ((1 << p1) & Database::gWatching) callPlayer(cShowShipChange, p1, &showShipChange); } } } if (winOnAlone && (Database::gAlive & ~((1 << pTurn) | 1)) == 0) gWinner = 1 << pTurn; // break if only one nation left if (Database::gTurn == player.AnarchyStart + AnarchyTurns) { player.AnarchyStart = -AnarchyTurns - 1; player.Government = gDespotism; for (int p1 = 0; p1 < nPl; ++p1) if (p1 != pTurn && ((Database::gAlive | Database::gWatching) & (1 << p1))) Database::rw[p1].EnemyReport[pTurn]->Government = gDespotism; player.Happened += phChangeGov; } } // if Difficulty[pTurn] > 0 if (pTurn == 0 && gWinner > 0) { // game over, give world map and all reports to player 0 Database::discoverAll(pTurn, Database::lObserveSuper); for (int p1 = 1; p1 < nPl; ++p1) { if ((1 << p1) & Database::gAlive) { if (player.Treaty[p1] < trNone) { player.Treaty[p1] = trNone; Database::rw[p1].Treaty[pTurn] = trNone; } Database::giveCivilReport(pTurn, p1); Database::giveMilReport(pTurn, p1); } } } else { // show observed areas if ((Database::gTestFlags & tfUncover) || Database::difficulty[pTurn] == 0) { // supervisor - all tiles visible if (playerBrain[pTurn]->kind != BrainType::NoTerm && (Database::difficulty[pTurn] > 0 || Database::mode > Database::moLoading_Fast)) Database::discoverAll(pTurn, Database::lObserveSuper); } else { Database::discoverViewAreas(pTurn); if (mirBuilt) Database::discoverAll(pTurn, Database::lObserveUnhidden); } } } void afterTurn() { TPlayerContext &player = Database::rw[pTurn]; TCity *city = *player.City; for (int cix = 0; cix < player.nCity; ++cix) { if (city[cix].Loc >= 0) { city[cix].Flags &= (chCaptured | chDisorder); CityProcessing::collectCityResources(pTurn, cix); // new style } } player.Money += player.OracleIncome; player.OracleIncome = 0; if (Database::gWonder[woOracle].EffectiveOwner == pTurn) { for (int p1 = 0; p1 < nPl; ++p1) if (((1 << p1) & Database::gAlive) && (p1 == pTurn || player.Treaty[p1] > trNoContact)) { const TCity *otherCity = *Database::rw[p1].City; for (int cix = 0; cix < Database::rw[p1].nCity; ++cix) if (otherCity[cix].Loc >= 0 && otherCity[cix].Built[imTemple] > 0) ++player.OracleIncome; } } if ((Database::gTestFlags & tfImmImprove) == 0 && player.Government != gAnarchy) for (int cix = 0; cix < player.nCity; ++cix) if (city[cix].Loc >= 0 && (city[cix].Flags & chCaptured) == 0) CityProcessing::payCityMaintenance(pTurn, cix); if (Database::serverVersion[pTurn] >= 0x000EF0) { // let settlers work for (int cix = 0; cix < player.nCity; ++cix) city[cix].Flags &= ~static_cast(chFounded); TUn *un = *player.Un; bool jobDone = false; // intentionally not reset per-iteration, matches the original's stale read for (int uix = 0; uix < player.nUn; ++uix) { TUn &u = un[uix]; if (u.Loc < 0) continue; int loc1 = u.Loc; const int job0 = u.Job; if (u.Job != jNone) jobDone = UnitProcessing::work(pTurn, uix); // settlers do terrain improvement jobs if (u.Health <= 0) Database::removeUnitUpdateMap(pTurn, uix); if (job0 == jCity && jobDone) { // new city CityProcessing::addBestCityTile(pTurn, player.nCity - 1); Database::updateUnitMap(loc1, true); if (Database::mode >= Database::moMovie) // tell enemies for (int p1 = 0; p1 < nPl; ++p1) if (((1 << p1) & Database::gWatching) && p1 != pTurn && (Database::observeLevel[loc1] & (3 << (2 * p1)))) callPlayer(cShowCityChanged, p1, &loc1); } } } { // next turn for all units TUn *un = *player.Un; const TModel *model = *player.Model; for (int uix = 0; uix < player.nUn; ++uix) { TUn &u = un[uix]; if (u.Loc < 0) continue; if (model[u.mix].Domain == dAir) { if (u.Master >= 0 || (Database::realMap[u.Loc] & fCity) || (Database::realMap[u.Loc] & fTerImp) == tiBase) { u.Fuel = static_cast(model[u.mix].Cap[mcFuel]); u.Flags |= unBombsLoaded; } else if (model[u.mix].Kind == mkSpecial_Glider) { // glider if ((Database::realMap[u.Loc] & fTerrain) < static_cast(fGrass)) { Database::removeUnitUpdateMap(pTurn, uix); // unit lost player.Happened |= phGliderLost; } } else { --u.Fuel; if (u.Fuel < 0) { Database::removeUnitUpdateMap(pTurn, uix); // unit lost player.Happened |= phPlaneLost; } } } else if (u.Master < 0 && u.Movement > 0) { // check HostileDamage u.Health = static_cast( u.Health - UnitProcessing::hostileDamage(pTurn, u.mix, u.Loc, u.Movement)); if (u.Health < 0) Database::removeUnitUpdateMap(pTurn, uix); } } // unit loop 1 for (int uix = 0; uix < player.nUn; ++uix) { TUn &u = un[uix]; u.Flags &= ~static_cast(unWithdrawn); if (u.Loc >= 0 && model[u.mix].Domain == dGround && u.Master < 0 && (static_cast(u.Movement) == model[u.mix].Speed || (model[u.mix].Cap[mcAcademy] > 0 && u.Movement * 2 >= model[u.mix].Speed))) u.Flags |= unFortified; // fortify unmoved units } } if ((Database::gTestFlags & tfUncover) == 0 && Database::difficulty[pTurn] > 0) { // restrict view area to current positions Database::maskD(Database::observeLevel.data(), Database::mapSize, ~static_cast(3 << (2 * pTurn))); if (Database::mode > Database::moLoading_Fast) Database::maskD(*player.Map, Database::mapSize, ~static_cast(fUnit | fHiddenUnit | fStealthUnit | fObserved | fSpiedOut | fOwned | fOwnZoCUnit | fInEnemyZoC)); player.nEnemyUn = 0; Database::discoverViewAreas(pTurn); } if (gWinner == 0) for (int p1 = 0; p1 < nPl; ++p1) if ((1 << p1) & Database::gAlive) checkWin(p1); } void nextPlayerImpl() { if (Database::gTurn == 0) beforeTurn0(); else beforeTurn(); noLogCityTileChanges(); generateStat(pTurn); inform(pTurn); changeClient(); } void nextPlayer() { nextPlayerImpl(); } } // namespace GameServer } // namespace Cevo