Files
c-evo-evo/game/localplayer/Diplomacy.cpp
T

241 lines
11 KiB
C++

// 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 "Diplomacy.h"
#include "Global.h"
#include "Tribes.h"
#include <QHash>
namespace Cevo {
namespace Diplomacy {
namespace {
// Phrases/Phrases2 (StringTables.pas) aren't ported; disclosed
// plain-English placeholders for the (non tribe-specific) keys this file
// looks up directly via Phrases[...].
QString phrase(const QString &key)
{
static const QHash<QString, QString> table = {
{QStringLiteral("FRCANCELTREATY_PEACE"), QStringLiteral("I hereby cancel our peace treaty.")},
{QStringLiteral("FRCANCELTREATY_FRIENDLY"), QStringLiteral("I hereby end our friendly relations.")},
{QStringLiteral("FRCANCELTREATY_ALLIANCE"), QStringLiteral("I hereby cancel our alliance.")},
{QStringLiteral("FRNOTICE"), QStringLiteral("I have a notice for you.")},
{QStringLiteral("FRDONE"), QStringLiteral("That is all.")},
{QStringLiteral("FRDELCHOICE"), QStringLiteral("in exchange for %1, you may choose what we deliver")},
{QStringLiteral("FRCOSTCHOICE"), QStringLiteral("we offer %1, you may choose the price")},
{QStringLiteral("FROFFER"), QStringLiteral("we offer %1 in exchange for %2")},
{QStringLiteral("PRICE_CHOOSE"), QStringLiteral("something of our choosing")},
{QStringLiteral("PRICE_SHIPCOMP"), QStringLiteral("%1 spaceship component(s)")},
{QStringLiteral("PRICE_SHIPPOW"), QStringLiteral("%1 spaceship power module(s)")},
{QStringLiteral("PRICE_SHIPHAB"), QStringLiteral("%1 spaceship habitation module(s)")},
{QStringLiteral("PRICE_TRIBUTE"), QStringLiteral("a tribute of %1")},
{QStringLiteral("PRICE_CONCAT"), QStringLiteral("%1, %2")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? it.value() : key;
}
QString phraseTreaty(int treatyType)
{
switch (treatyType) {
case trCeaseFire: return QStringLiteral("a cease-fire");
case trPeace: return QStringLiteral("a peace treaty");
case trFriendlyContact: return QStringLiteral("friendly relations");
case trAlliance: return QStringLiteral("an alliance");
default: return QStringLiteral("a treaty");
}
}
QString phraseAdvance(int advance)
{
return phrases.lookup(QStringLiteral("ADVANCES"), advance);
}
QString priceToString(int p, std::uint32_t price)
{
Tribes::Tribe &tribe = *Tribes::tribe[static_cast<std::size_t>(p)];
switch (price & static_cast<std::uint32_t>(opMask)) {
case opChoose:
return phrase(QStringLiteral("PRICE_CHOOSE"));
case opCivilReport:
return tribe.tPhrase(QStringLiteral("PRICE_CIVIL"));
case opMilReport:
return tribe.tPhrase(QStringLiteral("PRICE_MIL"));
case opMap:
return tribe.tPhrase(QStringLiteral("PRICE_MAP"));
case opTreaty:
return phraseTreaty(static_cast<int>(price - opTreaty));
case opShipParts:
switch ((price >> 16) & 0xFu) {
case 0: return phrase(QStringLiteral("PRICE_SHIPCOMP")).arg(price & 0xFFFFu);
case 1: return phrase(QStringLiteral("PRICE_SHIPPOW")).arg(price & 0xFFFFu);
case 2: return phrase(QStringLiteral("PRICE_SHIPHAB")).arg(price & 0xFFFFu);
default: return QString();
}
case opMoney:
return QStringLiteral("%1%c").arg(price - opMoney);
case opTribute:
return phrase(QStringLiteral("PRICE_TRIBUTE")).arg(price - opTribute);
case opTech:
return phraseAdvance(static_cast<int>(price - opTech));
case opAllTech:
return tribe.tPhrase(QStringLiteral("PRICE_ALLTECH"));
case opModel:
return tribe.modelNameAt(static_cast<int>(price - opModel));
case opAllModel:
return tribe.tPhrase(QStringLiteral("PRICE_ALLMODEL"));
default:
return QString();
}
}
} // namespace
QString dipCommandToString(int pSender, int pTarget, int treaty, int oppCommand, int command, const TOffer &oppOffer,
const TOffer &offer)
{
Tribes::Tribe &sender = *Tribes::tribe[static_cast<std::size_t>(pSender)];
Tribes::Tribe &target = *Tribes::tribe[static_cast<std::size_t>(pTarget)];
bool doIntro = oppCommand == scDipStart;
QString result;
switch (command) {
case scDipCancelTreaty:
switch (treaty) {
case trPeace: result = phrase(QStringLiteral("FRCANCELTREATY_PEACE")); break;
case trFriendlyContact: result = phrase(QStringLiteral("FRCANCELTREATY_FRIENDLY")); break;
case trAlliance: result = phrase(QStringLiteral("FRCANCELTREATY_ALLIANCE")); break;
default: break;
}
doIntro = false;
break;
case scDipNotice:
result = phrase(QStringLiteral("FRNOTICE"));
break;
case scDipAccept:
if (oppOffer.nDeliver + oppOffer.nCost == 1 && (oppOffer.Price[0] & static_cast<std::uint32_t>(opMask)) == opTreaty &&
static_cast<int>(oppOffer.Price[0] - opTreaty) > treaty)
result = target.tPhrase(QStringLiteral("FRACCEPTTREATY"));
else if (oppOffer.nDeliver == 0)
result = sender.tPhrase(QStringLiteral("FRACCEPTDEMAND_STRONG"));
else if (oppOffer.nCost == 0)
result = sender.tPhrase(QStringLiteral("FRACCEPTPRESENT"));
else
result = sender.tPhrase(QStringLiteral("FRACCEPTOFFER"));
break;
case scDipBreak:
result = target.tPhrase(QStringLiteral("FRBREAK"));
doIntro = false;
break;
case scDipOffer: {
if (oppCommand == scDipOffer && (oppOffer.nDeliver > 0 || oppOffer.nCost > 0) &&
offer.nCost + offer.nDeliver <= 2) {
// Respond to the offer the other side just made before making our own.
if (oppOffer.nDeliver + oppOffer.nCost == 1 &&
(oppOffer.Price[0] & static_cast<std::uint32_t>(opMask)) == opTreaty &&
static_cast<int>(oppOffer.Price[0] - opTreaty) > treaty)
result = sender.tPhrase(QStringLiteral("FRNOTACCEPTTREATY")) + QLatin1Char('\\');
else if (oppOffer.nDeliver == 0)
result = sender.tPhrase(QStringLiteral("FRNOTACCEPTDEMAND_STRONG")) + QLatin1Char('\\');
else if (oppOffer.nCost == 0)
result = sender.tPhrase(QStringLiteral("FRNOTACCEPTPRESENT")) + QLatin1Char('\\');
}
QString sDeliver;
for (int i = 0; i < offer.nDeliver; ++i) {
const QString sAdd = priceToString(pSender, offer.Price[i]);
sDeliver = i == 0 ? sAdd : phrase(QStringLiteral("PRICE_CONCAT")).arg(sDeliver, sAdd);
}
QString sCost;
for (int i = 0; i < offer.nCost; ++i) {
const QString sAdd = priceToString(pTarget, offer.Price[offer.nDeliver + i]);
sCost = i == 0 ? sAdd : phrase(QStringLiteral("PRICE_CONCAT")).arg(sCost, sAdd);
}
if (offer.nDeliver == 0 && offer.nCost == 0) {
if (oppCommand == scDipOffer && oppOffer.nDeliver == 0 && oppOffer.nCost == 0) {
result = target.tPhrase(QStringLiteral("FRBYE"));
} else {
if (result.isEmpty() && oppCommand == scDipOffer && (oppOffer.nDeliver > 0 || oppOffer.nCost > 0)) {
// The original also has two Phrases2-based "soft" variants
// here, unreachable without a StringTables.pas port.
result = sender.tPhrase(QStringLiteral("FRNOTACCEPTOFFER")) + QLatin1Char(' ');
}
result += phrase(QStringLiteral("FRDONE"));
doIntro = false;
}
} else if (offer.nDeliver + offer.nCost == 1 &&
(offer.Price[0] & static_cast<std::uint32_t>(opMask)) == opTreaty &&
static_cast<int>(offer.Price[0] - opTreaty) > treaty) {
switch (offer.Price[0] - opTreaty) {
case trPeace: result += target.tPhrase(QStringLiteral("FRPEACE")); break;
case trFriendlyContact: result += target.tPhrase(QStringLiteral("FRFRIENDLY")); break;
case trAlliance: result += target.tPhrase(QStringLiteral("FRALLIANCE")); break;
default: break;
}
} else if (offer.nDeliver == 0) {
// The original also has a Phrases2-based "soft" demand variant
// here, unreachable without a StringTables.pas port.
result += target.tPhrase(QStringLiteral("FRDEMAND_STRONG")).arg(sCost);
doIntro = false;
} else if (offer.nCost == 0) {
result += target.tPhrase(QStringLiteral("FRPRESENT")).arg(sDeliver);
} else if (offer.nDeliver == 1 && offer.Price[0] == opChoose) {
result += phrase(QStringLiteral("FRDELCHOICE")).arg(sCost);
} else if (offer.nCost == 1 && offer.Price[offer.nDeliver] == opChoose) {
result += phrase(QStringLiteral("FRCOSTCHOICE")).arg(sDeliver);
} else {
result += phrase(QStringLiteral("FROFFER")).arg(sDeliver, sCost);
}
break;
}
default:
break;
}
if (doIntro) {
if (treaty < trPeace)
result = sender.tPhrase(QStringLiteral("FRSTART_NOTREATY")) + QLatin1Char(' ') + result;
else
result = sender.tPhrase(QStringLiteral("FRSTART_PEACE")) + QLatin1Char(' ') + result;
}
return result;
}
} // namespace Diplomacy
} // namespace Cevo