add back and splitted database
This commit is contained in:
1 parent
a7e65be715
commit
55ccee5b57
16 files changed
+3215
-2534
No files matched your search
@@ -0,0 +1,481 @@
|
||||
// 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.
|
||||
|
||||
// --- Diplomacy --- (see Cevo/Database.h)
|
||||
|
||||
#include "Cevo/Database.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
namespace Cevo {
|
||||
namespace Database {
|
||||
|
||||
void introduceEnemy(int p1, int p2)
|
||||
{
|
||||
rw[p1].Treaty[p2] = trNone;
|
||||
rw[p2].Treaty[p1] = trNone;
|
||||
}
|
||||
|
||||
int techCost(int p)
|
||||
{
|
||||
TPlayerContext &player = rw[p];
|
||||
int result = techBaseCost(nTech[p], difficulty[p]);
|
||||
if (player.ResearchTech >= 0) {
|
||||
if (player.ResearchTech == adMilitary || player.Tech[player.ResearchTech] == tsSeen)
|
||||
result >>= 1;
|
||||
else if (IsFutureTech(player.ResearchTech))
|
||||
result *= (player.Government == gFuture) ? 2 : 4;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void giveCivilReport(int p, int pAbout)
|
||||
{
|
||||
TEnemyReport &report = *rw[p].EnemyReport[pAbout];
|
||||
report.TurnOfCivilReport = lastValidStat[pAbout];
|
||||
std::memcpy(report.Treaty, rw[pAbout].Treaty, sizeof(report.Treaty));
|
||||
report.Government = rw[pAbout].Government;
|
||||
report.Money = rw[pAbout].Money;
|
||||
report.ResearchTech = rw[pAbout].ResearchTech;
|
||||
report.ResearchDone = rw[pAbout].Research * 100 / techCost(pAbout);
|
||||
if (report.ResearchDone > 100)
|
||||
report.ResearchDone = 100;
|
||||
std::memcpy(report.Tech, rw[pAbout].Tech, nAdv);
|
||||
}
|
||||
|
||||
void giveMilReport(int p, int pAbout)
|
||||
{
|
||||
TEnemyReport &report = *rw[p].EnemyReport[pAbout];
|
||||
report.TurnOfMilReport = lastValidStat[pAbout];
|
||||
report.nModelCounted = rw[pAbout].nModel;
|
||||
for (int mix = 0; mix < rw[pAbout].nModel; ++mix) {
|
||||
tellAboutModel(p, pAbout, mix);
|
||||
report.UnCount[mix] = 0;
|
||||
}
|
||||
const TUn *un = *rw[pAbout].Un;
|
||||
for (int uix = 0; uix < rw[pAbout].nUn; ++uix)
|
||||
if (un[uix].Loc >= 0)
|
||||
++report.UnCount[un[uix].mix];
|
||||
}
|
||||
|
||||
bool copyCivilReport(int pSender, int pTarget, int pAbout)
|
||||
{
|
||||
bool result = false;
|
||||
if (rw[pTarget].Treaty[pAbout] == trNoContact)
|
||||
introduceEnemy(pTarget, pAbout);
|
||||
TEnemyReport &rSender = *rw[pSender].EnemyReport[pAbout];
|
||||
TEnemyReport &rTarget = *rw[pTarget].EnemyReport[pAbout];
|
||||
if (rSender.TurnOfCivilReport > rTarget.TurnOfCivilReport) { // only if newer than current information
|
||||
rTarget.TurnOfCivilReport = rSender.TurnOfCivilReport;
|
||||
std::memcpy(rTarget.Treaty, rSender.Treaty, sizeof(rTarget.Treaty));
|
||||
rTarget.Government = rSender.Government;
|
||||
rTarget.Money = rSender.Money;
|
||||
rTarget.ResearchTech = rSender.ResearchTech;
|
||||
rTarget.ResearchDone = rSender.ResearchDone;
|
||||
result = true;
|
||||
}
|
||||
for (int i = 0; i < nAdv; ++i)
|
||||
if (rTarget.Tech[i] < rSender.Tech[i]) {
|
||||
rTarget.Tech[i] = rSender.Tech[i];
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool copyMilReport(int pSender, int pTarget, int pAbout)
|
||||
{
|
||||
bool result = false;
|
||||
if (rw[pTarget].Treaty[pAbout] == trNoContact)
|
||||
introduceEnemy(pTarget, pAbout);
|
||||
TEnemyReport &rSender = *rw[pSender].EnemyReport[pAbout];
|
||||
TEnemyReport &rTarget = *rw[pTarget].EnemyReport[pAbout];
|
||||
if (rSender.TurnOfMilReport > rTarget.TurnOfMilReport) { // only if newer than current information
|
||||
rTarget.TurnOfMilReport = rSender.TurnOfMilReport;
|
||||
rTarget.nModelCounted = rSender.nModelCounted;
|
||||
std::memcpy(rTarget.UnCount, rSender.UnCount,
|
||||
sizeof(std::uint16_t) * static_cast<std::size_t>(rSender.nModelCounted));
|
||||
for (int mix = 0; mix < rTarget.nModelCounted; ++mix)
|
||||
tellAboutModel(pTarget, pAbout, mix);
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void copyModel(int pSender, int pTarget, int mix)
|
||||
{
|
||||
// only if target doesn't already have a model like this
|
||||
bool ok = rw[pTarget].nModel < nMmax;
|
||||
TModelInfo miSender{};
|
||||
MakeModelInfo(pSender, mix, (*rw[pSender].Model)[mix], miSender);
|
||||
for (int i = 0; i < rw[pTarget].nModel && ok; ++i) {
|
||||
TModelInfo miTarget{};
|
||||
MakeModelInfo(pTarget, i, (*rw[pTarget].Model)[i], miTarget);
|
||||
if (IsSameModel(miSender, miTarget))
|
||||
ok = false;
|
||||
}
|
||||
if (ok) {
|
||||
TModel &newModel = (*rw[pTarget].Model)[rw[pTarget].nModel];
|
||||
newModel = (*rw[pSender].Model)[mix];
|
||||
newModel.IntroTurn = static_cast<std::uint16_t>(gTurn);
|
||||
if (newModel.Kind == mkSelfDeveloped)
|
||||
newModel.Kind = mkEnemyDeveloped;
|
||||
newModel.Status = 0;
|
||||
newModel.SavedStatus = 0;
|
||||
newModel.Built = 0;
|
||||
newModel.Lost = 0;
|
||||
++rw[pTarget].nModel;
|
||||
++researched[pTarget];
|
||||
tellAboutModel(pSender, pTarget, rw[pTarget].nModel - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void copyMap(int pSender, int pTarget)
|
||||
{
|
||||
TPlayerContext &sender = rw[pSender];
|
||||
TPlayerContext &target = rw[pTarget];
|
||||
const std::int16_t *senderObserved = *sender.MapObservedLast;
|
||||
std::int16_t *targetObserved = *target.MapObservedLast;
|
||||
const std::uint32_t *senderMap = *sender.Map;
|
||||
std::uint32_t *targetMap = *target.Map;
|
||||
const std::int8_t *senderTerritory = *sender.Territory;
|
||||
std::int8_t *targetTerritory = *target.Territory;
|
||||
TCityInfo *senderEnemyCity = *sender.EnemyCity;
|
||||
TCityInfo *targetEnemyCity = *target.EnemyCity;
|
||||
const TCity *senderCity = *sender.City;
|
||||
|
||||
for (int loc = 0; loc < mapSize; ++loc) {
|
||||
if (senderObserved[loc] <= targetObserved[loc])
|
||||
continue;
|
||||
std::uint32_t tile = senderMap[loc];
|
||||
if (tile & fCity) {
|
||||
int i = 0;
|
||||
while (i < target.nEnemyCity && targetEnemyCity[i].Loc != loc)
|
||||
++i;
|
||||
if (i == target.nEnemyCity) {
|
||||
++target.nEnemyCity;
|
||||
assert(target.nEnemyCity < necmax);
|
||||
targetEnemyCity[i].Status = 0;
|
||||
targetEnemyCity[i].SavedStatus = 0;
|
||||
}
|
||||
if (tile & fOwned) { // city owned by sender -- create new info
|
||||
int cix = sender.nCity - 1;
|
||||
while (cix >= 0 && senderCity[cix].Loc != loc)
|
||||
--cix;
|
||||
makeCityInfo(pSender, cix, targetEnemyCity[i]);
|
||||
} else { // city not owned by sender -- copy old info
|
||||
int cix = sender.nEnemyCity - 1;
|
||||
while (cix >= 0 && senderEnemyCity[cix].Loc != loc)
|
||||
--cix;
|
||||
targetEnemyCity[i] = senderEnemyCity[cix];
|
||||
}
|
||||
} else if (targetMap[loc] & fCity) { // remove enemycity
|
||||
for (int cix = 0; cix < target.nEnemyCity; ++cix)
|
||||
if (targetEnemyCity[cix].Loc == loc)
|
||||
targetEnemyCity[cix].Loc = -1;
|
||||
}
|
||||
|
||||
tile &= (~static_cast<std::uint32_t>(fSpecial | fModern) | resourceMask[pTarget]);
|
||||
tile |= (targetMap[loc] & fModern);
|
||||
if ((tile & fTerrain) == (targetMap[loc] & fTerrain))
|
||||
tile |= (targetMap[loc] & fSpecial);
|
||||
|
||||
if ((targetMap[loc] & fTerrain) == static_cast<std::uint32_t>(fUNKNOWN))
|
||||
++discovered[pTarget];
|
||||
targetMap[loc] = (targetMap[loc] & fInEnemyZoC) // always preserve this flag!
|
||||
| (tile & ~static_cast<std::uint32_t>(fUnit | fHiddenUnit | fStealthUnit | fObserved |
|
||||
fSpiedOut | fOwned | fInEnemyZoC | fOwnZoCUnit |
|
||||
fPeace | fGrWall));
|
||||
// (the original re-assigns Territory unconditionally right after an
|
||||
// equivalent if-guarded assignment - the guard was a no-op once its
|
||||
// only other effect, a BorderHelper update, had been commented out)
|
||||
targetTerritory[loc] = senderTerritory[loc];
|
||||
targetObserved[loc] = senderObserved[loc];
|
||||
}
|
||||
}
|
||||
|
||||
std::uint32_t doSpyMission(int p, int pCity, int cix, int mission)
|
||||
{
|
||||
std::uint32_t result = 0;
|
||||
switch (mission) {
|
||||
case smSabotageProd:
|
||||
(*rw[pCity].City)[cix].Flags |= chProductionSabotaged;
|
||||
break;
|
||||
case smStealMap:
|
||||
copyMap(pCity, p);
|
||||
recalcPeaceMap(p);
|
||||
break;
|
||||
case smStealCivilReport:
|
||||
if (rw[p].Treaty[pCity] == trNoContact)
|
||||
introduceEnemy(p, pCity);
|
||||
giveCivilReport(p, pCity);
|
||||
break;
|
||||
case smStealMilReport:
|
||||
if (rw[p].Treaty[pCity] == trNoContact)
|
||||
introduceEnemy(p, pCity);
|
||||
giveMilReport(p, pCity);
|
||||
break;
|
||||
case smStealForeignReports:
|
||||
for (int p1 = 0; p1 < nPl; ++p1) {
|
||||
if (p1 != p && p1 != pCity && rw[pCity].EnemyReport[p1]) {
|
||||
if (rw[pCity].EnemyReport[p1]->TurnOfCivilReport >= 0 && copyCivilReport(pCity, p, p1))
|
||||
result |= (1u << (2 * p1));
|
||||
if (rw[pCity].EnemyReport[p1]->TurnOfMilReport >= 0 && copyMilReport(pCity, p, p1))
|
||||
result |= (2u << (2 * p1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void showPrice(int pSender, int pTarget, std::uint32_t price)
|
||||
{
|
||||
switch (price & opMask) {
|
||||
case opTech: // + advance
|
||||
if (rw[pTarget].EnemyReport[pSender]->Tech[price - opTech] < tsApplicable)
|
||||
rw[pTarget].EnemyReport[pSender]->Tech[price - opTech] = tsApplicable;
|
||||
break;
|
||||
case opModel: // + model index
|
||||
tellAboutModel(pTarget, pSender, static_cast<int>(price - opModel));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool payPrice(int pSender, int pTarget, std::uint32_t price, bool execute)
|
||||
{
|
||||
bool result = true;
|
||||
switch (price & opMask) {
|
||||
case opCivilReport: { // + turn + concerned player shl 16
|
||||
const int pSubject = static_cast<int>((price >> 16) & 0xF);
|
||||
if (pTarget == pSubject)
|
||||
result = false;
|
||||
else if (pSender == pSubject) {
|
||||
if (execute)
|
||||
giveCivilReport(pTarget, pSender);
|
||||
} else if (rw[pSender].EnemyReport[pSubject]->TurnOfCivilReport < 0)
|
||||
result = false;
|
||||
else if (execute)
|
||||
copyCivilReport(pSender, pTarget, pSubject);
|
||||
break;
|
||||
}
|
||||
case opMilReport: { // + turn + concerned player shl 16
|
||||
const int pSubject = static_cast<int>((price >> 16) & 0xF);
|
||||
if (pTarget == pSubject)
|
||||
result = false;
|
||||
else if (pSender == pSubject) {
|
||||
if (execute)
|
||||
giveMilReport(pTarget, pSender);
|
||||
} else if (rw[pSender].EnemyReport[pSubject]->TurnOfMilReport < 0)
|
||||
result = false;
|
||||
else if (execute)
|
||||
copyMilReport(pSender, pTarget, pSubject);
|
||||
break;
|
||||
}
|
||||
case opMap:
|
||||
if (execute) {
|
||||
copyMap(pSender, pTarget);
|
||||
recalcPeaceMap(pTarget);
|
||||
}
|
||||
break;
|
||||
case opTreaty: { // + nation treaty (opMask only keeps the top byte, so
|
||||
// this single case covers the original's whole
|
||||
// opTreaty..opTreaty+trAlliance range)
|
||||
if (static_cast<int>(price - opTreaty) == rw[pSender].Treaty[pTarget] - 1) { // agreed treaty end
|
||||
if (execute)
|
||||
cancelTreaty(pSender, pTarget, false);
|
||||
} else {
|
||||
int newTreaty = -1;
|
||||
if (static_cast<int>(price - opTreaty) == rw[pSender].Treaty[pTarget] + 1)
|
||||
newTreaty = static_cast<int>(price - opTreaty);
|
||||
else if (rw[pSender].Treaty[pTarget] == trNone && static_cast<int>(price - opTreaty) == trPeace)
|
||||
newTreaty = trPeace;
|
||||
if (newTreaty < 0)
|
||||
result = false;
|
||||
else if (execute) {
|
||||
assert(newTreaty > rw[pSender].Treaty[pTarget]);
|
||||
rw[pSender].Treaty[pTarget] = newTreaty;
|
||||
rw[pTarget].Treaty[pSender] = newTreaty;
|
||||
if (newTreaty >= trFriendlyContact) {
|
||||
giveCivilReport(pTarget, pSender);
|
||||
giveCivilReport(pSender, pTarget);
|
||||
}
|
||||
if (newTreaty == trAlliance) {
|
||||
giveMilReport(pTarget, pSender);
|
||||
giveMilReport(pSender, pTarget);
|
||||
copyMap(pSender, pTarget);
|
||||
copyMap(pTarget, pSender);
|
||||
recalcMapZoC(pSender);
|
||||
recalcMapZoC(pTarget);
|
||||
}
|
||||
if (newTreaty != trPeace && newTreaty != trFriendlyContact) {
|
||||
rw[pSender].EvaStart[pTarget] = -PeaceEvaTurns - 1;
|
||||
rw[pTarget].EvaStart[pSender] = -PeaceEvaTurns - 1;
|
||||
}
|
||||
recalcPeaceMap(pSender);
|
||||
recalcPeaceMap(pTarget);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case opShipParts: { // + number + part type shl 16
|
||||
const int n = static_cast<int>(price & 0xFFFF); // number
|
||||
const int i = static_cast<int>((price >> 16) & 0xF); // type
|
||||
if (i < nShipPart && gShip[pSender].Parts[i] >= n) {
|
||||
if (execute) {
|
||||
gShip[pSender].Parts[i] -= n;
|
||||
rw[pSender].Ship[pSender].Parts[i] = gShip[pSender].Parts[i];
|
||||
rw[pTarget].Ship[pSender].Parts[i] = gShip[pSender].Parts[i];
|
||||
if (rw[pTarget].NatBuilt[imSpacePort] > 0) { // space ship control requires space port
|
||||
gShip[pTarget].Parts[i] += n;
|
||||
rw[pSender].Ship[pTarget].Parts[i] = gShip[pTarget].Parts[i];
|
||||
rw[pTarget].Ship[pTarget].Parts[i] = gShip[pTarget].Parts[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case opMoney: // + value
|
||||
if (price - opMoney <= MaxMoneyPrice && rw[pSender].Money >= static_cast<int>(price - opMoney)) {
|
||||
if (execute) {
|
||||
rw[pSender].Money -= static_cast<int>(price - opMoney);
|
||||
rw[pTarget].Money += static_cast<int>(price - opMoney);
|
||||
}
|
||||
} else {
|
||||
result = false;
|
||||
}
|
||||
break;
|
||||
case opTribute: // + value
|
||||
// tribute no longer in use - execute branch intentionally empty
|
||||
break;
|
||||
case opTech: // + advance
|
||||
if (rw[pSender].Tech[price - opTech] >= tsApplicable) {
|
||||
if (execute && rw[pTarget].Tech[price - opTech] == tsNA) {
|
||||
seeTech(pTarget, static_cast<int>(price - opTech));
|
||||
rw[pSender].EnemyReport[pTarget]->Tech[price - opTech] = tsSeen;
|
||||
}
|
||||
} else {
|
||||
result = false;
|
||||
}
|
||||
break;
|
||||
case opAllTech:
|
||||
if (execute) {
|
||||
for (int i = 0; i < nAdv; ++i) {
|
||||
if (rw[pSender].Tech[i] >= tsApplicable && rw[pTarget].Tech[i] == tsNA) {
|
||||
seeTech(pTarget, i);
|
||||
rw[pSender].EnemyReport[pTarget]->Tech[i] = tsSeen;
|
||||
rw[pTarget].EnemyReport[pSender]->Tech[i] = tsApplicable;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case opModel: // + model index
|
||||
if (static_cast<int>(price - opModel) < rw[pSender].nModel) {
|
||||
if (execute)
|
||||
copyModel(pSender, pTarget, static_cast<int>(price - opModel));
|
||||
} else {
|
||||
result = false;
|
||||
}
|
||||
break;
|
||||
case opAllModel:
|
||||
if (execute)
|
||||
for (int i = 0; i < rw[pSender].nModel; ++i) {
|
||||
tellAboutModel(pTarget, pSender, i);
|
||||
copyModel(pSender, pTarget, i);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void cancelTreaty(int p, int pWith, bool decreaseCredibility)
|
||||
{
|
||||
TPlayerContext &player = rw[p];
|
||||
TPlayerContext &withPlayer = rw[pWith];
|
||||
const int oldTreaty = player.Treaty[pWith];
|
||||
peaceEnded = 0;
|
||||
if (oldTreaty >= trPeace)
|
||||
player.LastCancelTreaty[pWith] = gTurn;
|
||||
if (decreaseCredibility) {
|
||||
if (oldTreaty == trPeace) {
|
||||
player.Credibility >>= 1;
|
||||
if (player.MaxCredibility > 0)
|
||||
player.MaxCredibility -= 10;
|
||||
if (player.Credibility > player.MaxCredibility)
|
||||
player.Credibility = player.MaxCredibility;
|
||||
} else if (oldTreaty == trAlliance) {
|
||||
player.Credibility = player.Credibility * 3 / 4;
|
||||
}
|
||||
withPlayer.EnemyReport[p]->Credibility = player.Credibility;
|
||||
}
|
||||
|
||||
if (oldTreaty == trPeace) {
|
||||
for (int p1 = 0; p1 < nPl; ++p1) {
|
||||
if (p1 == pWith || (decreaseCredibility && p1 != p && withPlayer.Treaty[p1] == trAlliance &&
|
||||
player.Treaty[p1] >= trPeace)) {
|
||||
player.Treaty[p1] = trNone;
|
||||
rw[p1].Treaty[p] = trNone;
|
||||
player.EvaStart[p1] = -PeaceEvaTurns - 1;
|
||||
rw[p1].EvaStart[p] = -PeaceEvaTurns - 1;
|
||||
peaceEnded += (1 << p1);
|
||||
}
|
||||
}
|
||||
checkBorders(-1);
|
||||
if (mode > moLoading_Fast && peaceEnded > 0)
|
||||
recalcMapZoC(p);
|
||||
} else {
|
||||
player.Treaty[pWith] = oldTreaty - 1;
|
||||
withPlayer.Treaty[p] = oldTreaty - 1;
|
||||
if (oldTreaty == trFriendlyContact) { // necessary for loading
|
||||
giveCivilReport(p, pWith);
|
||||
giveCivilReport(pWith, p);
|
||||
} else if (oldTreaty == trAlliance) { // necessary for loading
|
||||
giveMilReport(p, pWith);
|
||||
giveMilReport(pWith, p);
|
||||
}
|
||||
if (mode > moLoading_Fast && oldTreaty == trAlliance) {
|
||||
recalcMapZoC(p);
|
||||
recalcMapZoC(pWith);
|
||||
}
|
||||
}
|
||||
if (oldTreaty == trPeace || oldTreaty == trAlliance) {
|
||||
recalcPeaceMap(p);
|
||||
recalcPeaceMap(pWith);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Database
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user