added draft and natstat with their requirements

This commit is contained in:
Geoffray Levasseur committed 2026-11-03 14:25:34 +01:00
1 parent 6513c6f8ec
commit 1667e18dda
10 files changed
+1542

No files matched your search

+483
View File
@@ -0,0 +1,483 @@
// 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 "NatStat.h"
#include "ui_NatStat.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "ClientTools.h"
#include "Global.h"
#include "IsoEngine.h"
#include "Term.h"
#include "Tribes.h"
#include <QHash>
#include <QKeyEvent>
#include <QMenu>
#include <vector>
namespace Cevo {
namespace NatStat {
namespace {
constexpr int xIcon = 326;
constexpr int yIcon = 49;
constexpr int xAttrib = 96;
constexpr int yAttrib = 40;
constexpr int xRelation = 16;
constexpr int yRelation = 110;
constexpr int PaperShade = 3;
constexpr int ReportLines = 12;
constexpr int LineSpacing = 22;
constexpr int xReport = 24;
constexpr int yReport = 165;
constexpr int wReport = 352;
constexpr int hReport = ReportLines * LineSpacing;
// Phrases/Phrases2 (StringTables.pas) aren't ported; disclosed
// plain-English placeholders for this dialog's own text.
QString phrase(const QString &key)
{
static const QHash<QString, QString> table = {
{QStringLiteral("BTN_SELECT"), QStringLiteral("Select nation")},
{QStringLiteral("BTN_DIALOG"), QStringLiteral("Negotiate")},
{QStringLiteral("FRSTATPOP"), QStringLiteral("Population: %1")},
{QStringLiteral("FRSTATTER"), QStringLiteral("Territory: %1")},
{QStringLiteral("FRSTATTECH"), QStringLiteral("Advances researched: %1")},
{QStringLiteral("FRSTATEXP"), QStringLiteral("Land explored: %1%")},
{QStringLiteral("FROLDCIVILREP"), QStringLiteral("Last report: %1")},
{QStringLiteral("FRTREASURY"), QStringLiteral("Treasury: %1")},
{QStringLiteral("FRRELATIONS"), QStringLiteral("Relations:")},
{QStringLiteral("FRCOLDWAR"), QStringLiteral("We are in a cold war.")},
{QStringLiteral("FRMYANARCHY"), QStringLiteral("We are in anarchy.")},
{QStringLiteral("FRANARCHY"), QStringLiteral("They are in anarchy.")},
{QStringLiteral("FREXTINCT"), QStringLiteral("Extinct since")},
{QStringLiteral("FRCREDIBILITY"), QStringLiteral("Credibility: %1")},
{QStringLiteral("FRNOCONTACT"), QStringLiteral("No contact")},
{QStringLiteral("FRTREATY"), QStringLiteral("Treaty:")},
{QStringLiteral("FRNOVISIT"), QStringLiteral("Never visited")},
{QStringLiteral("FRLASTCONTACT"), QStringLiteral("Last contact:")},
{QStringLiteral("FRNOCIVILREP"), QStringLiteral("No civil report available.")},
{QStringLiteral("MENU_TELLAI"), QStringLiteral("Tell AI")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? it.value() : key;
}
QString phraseGovernment(int government)
{
return QStringLiteral("government #%1 ").arg(government);
}
QString phraseCredibility(int roughCredibility)
{
static const QHash<int, QString> table = {
{0, QStringLiteral("poor")}, {1, QStringLiteral("fair")}, {2, QStringLiteral("good")},
{3, QStringLiteral("perfect")},
};
const auto it = table.constFind(roughCredibility);
return it != table.constEnd() ? it.value() : QString();
}
QString phraseTreaty(int treaty)
{
static const QHash<int, QString> table = {
{trNoContact, QStringLiteral("no contact")}, {trNone, QStringLiteral("none")},
{trCeaseFire, QStringLiteral("cease-fire")}, {trPeace, QStringLiteral("peace")},
{trFriendlyContact, QStringLiteral("friendly")}, {trAlliance, QStringLiteral("alliance")},
};
const auto it = table.constFind(treaty);
return it != table.constEnd() ? it.value() : QString();
}
} // namespace
NatStatDlg::NatStatDlg(QWidget *parent) : BufferedDrawDlg(parent), ui(new Ui::NatStatDlg)
{
ui->setupUi(this);
initButtons();
ui->contactBtn->setTemplate(Templates ? &Templates->data : nullptr);
ui->contactBtn->setButtonIndex(3);
helpContext = QStringLiteral("DIPLOMACY");
ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_SELECT")));
ui->contactBtn->setToolTip(phrase(QStringLiteral("BTN_DIALOG")));
back.setSize(width(), height());
back.canvas().begin();
back.canvas().fillRect(QRect(0, 0, back.width(), back.height()));
back.canvas().end();
tmpl.loadFromFile(getGraphicsDir() + QStringLiteral("/Nation") + PngExt);
popup = new QMenu(this);
ui->closeBtn->onClick = [this] { close(); };
ui->toggleBtn->onClick = [this] { toggleBtnClicked(); };
ui->contactBtn->onClick = [this] { dialogBtnClicked(); };
ui->scrollUpBtn->onClick = [this] {
if (linesDown > 0) {
--linesDown;
smartUpdateContent();
}
};
ui->scrollDownBtn->onClick = [this] {
if (linesDown + ReportLines < reportText.size()) {
++linesDown;
smartUpdateContent();
}
};
ui->tellAIBtn->onClick = [this] { tellAIBtnClicked(); };
onShow = [this] {
if (pView == ClientTools::me) {
selfReport.TurnOfCivilReport = ClientTools::myRO->Turn;
selfReport.TurnOfMilReport = ClientTools::myRO->Turn;
std::copy(std::begin(ClientTools::myRO->Treaty), std::end(ClientTools::myRO->Treaty),
std::begin(selfReport.Treaty));
selfReport.Government = ClientTools::myRO->Government;
selfReport.Money = ClientTools::myRO->Money;
currentReport = &selfReport;
} else {
currentReport = ClientTools::myRO->EnemyReport[pView];
}
if (currentReport->TurnOfCivilReport >= 0)
generateReportText();
showContact = pView != ClientTools::me && !(Term::supervising && ClientTools::me == 0);
contactEnabled =
showContact && !Term::supervising && (1u << pView & static_cast<unsigned>(ClientTools::myRO->Alive)) != 0;
ui->contactBtn->setVisible(contactEnabled && (ClientTools::myRO->Happened & phGameEnd) == 0 &&
IsoEngine::clientMode < scContact);
const bool showScroll =
currentReport->TurnOfCivilReport >= 0 && reportText.size() > ReportLines;
ui->scrollUpBtn->setVisible(showScroll);
ui->scrollDownBtn->setVisible(showScroll);
ui->tellAIBtn->setButtonIndex(
ClientTools::hasSaveOption(Term::optionChecked, ClientTools::soTellAI) ? 3 : 2);
setWindowTitle(Tribes::tribe[static_cast<std::size_t>(pView)]->tPhrase(QStringLiteral("TITLE_NATION")));
linesDown = 0;
offscreenPaint();
};
}
NatStatDlg::~NatStatDlg()
{
delete ui;
}
void NatStatDlg::checkAge()
{
if (!MainTexture)
return;
if (MainTexture->age() != agePrepared) {
agePrepared = MainTexture->age();
bitBltBitmap(back, 0, 0, width(), height(), MainTexture->image(), (MainTexture->width() - width()) / 2,
(MainTexture->height() - height()) / 2);
imageOpB(back, tmpl, 0, 0, 0, 0, width(), height());
}
}
void NatStatDlg::showNewContent(WindowMode newMode, int p)
{
if (p < 0) {
if (IsoEngine::clientMode >= scContact) {
pView = Term::dipMem[static_cast<std::size_t>(ClientTools::me)].pContact;
} else {
pView = 0;
while (pView < nPl && (ClientTools::myRO->Treaty[pView] < trNone ||
(1u << pView & static_cast<unsigned>(ClientTools::myRO->Alive)) == 0))
++pView;
if (pView >= nPl)
pView = ClientTools::me;
}
} else {
pView = p;
}
BufferedDrawDlg::showNewContent(newMode);
}
void NatStatDlg::playerClicked(int p)
{
showNewContent(windowMode(), p);
}
void NatStatDlg::generateReportText()
{
std::vector<std::int32_t> list(static_cast<std::size_t>(ClientTools::myRO->Turn + 2));
auto statText = [&](int no) -> QString {
if (currentReport->TurnOfCivilReport >= 0 &&
ClientTools::server(sGetChart + (no << 4), ClientTools::me, pView, list.data()) >= rExecuted) {
const int i = list[static_cast<std::size_t>(currentReport->TurnOfCivilReport)];
switch (no) {
case stPop: return phrase(QStringLiteral("FRSTATPOP")).arg(i);
case stTerritory: return phrase(QStringLiteral("FRSTATTER")).arg(i);
case stScience: return phrase(QStringLiteral("FRSTATTECH")).arg(i / nAdv);
case stExplore:
return phrase(QStringLiteral("FRSTATEXP")).arg(i * 100 / (ClientTools::g.lx * ClientTools::g.ly));
default: return QString();
}
}
return QString();
};
reportText.clear();
reportText.append(QString());
if (ClientTools::myRO->Turn - currentReport->TurnOfCivilReport > 1 &&
(1u << pView & static_cast<unsigned>(ClientTools::myRO->Alive)) != 0) {
reportText.append(phrase(QStringLiteral("FROLDCIVILREP")).arg(Term::turnToString(currentReport->TurnOfCivilReport)));
reportText.append(QString());
}
if ((1u << pView & static_cast<unsigned>(ClientTools::myRO->Alive)) != 0) {
reportText.append(phrase(QStringLiteral("FRTREASURY")).arg(currentReport->Money));
reportText.append(statText(stPop));
reportText.append(statText(stTerritory));
}
reportText.append(statText(stScience));
reportText.append(statText(stExplore));
bool hasContact = false;
for (int p1 = 0; p1 < nPl; ++p1)
if (p1 != ClientTools::me && currentReport->Treaty[p1] > trNoContact) {
hasContact = true;
break;
}
if (hasContact) {
reportText.append(QString());
reportText.append(phrase(QStringLiteral("FRRELATIONS")));
for (int extinctPart = 0; extinctPart <= 1; ++extinctPart) {
for (int treaty = trAlliance; treaty >= trNone; --treaty) {
for (int p1 = 0; p1 < nPl; ++p1) {
if (p1 != ClientTools::me && currentReport->Treaty[p1] == treaty &&
((1u << p1 & static_cast<unsigned>(ClientTools::myRO->Alive)) == 0) == (extinctPart != 0)) {
QString s = Tribes::tribe[static_cast<std::size_t>(p1)]->tString(phraseTreaty(treaty));
if (extinctPart)
s = QLatin1Char('(') + s + QLatin1Char(')');
reportText.append(s);
}
}
}
}
}
reportText.append(QString());
}
void NatStatDlg::offscreenPaint()
{
BufferedDrawDlg::offscreenPaint();
checkAge();
const bool extinct = (1u << pView & static_cast<unsigned>(ClientTools::myRO->Alive)) == 0;
bitBltBitmap(offscreen, 0, 0, width(), height(), back, 0, 0);
Dpi::Canvas &canvas = offscreen.canvas();
canvas.begin();
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Caption)]);
risedTextOut(canvas, 40, 7, windowTitle());
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
Tribes::Tribe &tribe = *Tribes::tribe[static_cast<std::size_t>(pView)];
tribe.initAge(ClientTools::getAge(pView));
if (tribe.faceHGr) {
bitBltBitmap(offscreen, 18, yIcon - 4, 64, 48, tribe.faceHGr->data, 1 + tribe.facePix % 10 * 65,
1 + tribe.facePix / 10 * 49);
frame(canvas, 18 - 1, yIcon - 4 - 1, 18 + 64, yIcon - 4 + 48, Qt::black, Qt::black);
}
if (pView == ClientTools::me || !extinct)
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib,
phraseGovernment(currentReport->Government) + QStringLiteral("and"));
if (pView == ClientTools::me) {
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib + 19,
phraseCredibility(Term::roughCredibility(ClientTools::myRO->Credibility)));
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib + 38,
phrase(QStringLiteral("FRCREDIBILITY")).arg(ClientTools::myRO->Credibility));
} else {
if (extinct) {
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib + 9, phrase(QStringLiteral("FREXTINCT")));
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib + 28,
Term::turnToString(currentReport->TurnOfCivilReport));
} else {
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib + 19,
phraseCredibility(Term::roughCredibility(currentReport->Credibility)));
loweredTextOut(canvas, -1, *MainTexture, xAttrib, yAttrib + 38,
phrase(QStringLiteral("FRCREDIBILITY")).arg(currentReport->Credibility));
}
if (ClientTools::myRO->Treaty[pView] == trNoContact) {
const QString s = phrase(QStringLiteral("FRNOCONTACT"));
loweredTextOut(canvas, -1, *MainTexture, (width() - biColorTextWidth(canvas, s)) / 2, yRelation + 9, s);
} else if (showContact) {
loweredTextOut(canvas, -1, *MainTexture, xRelation, yRelation, phrase(QStringLiteral("FRTREATY")));
loweredTextOut(canvas, -1, *MainTexture, width() / 2, yRelation,
phraseTreaty(ClientTools::myRO->Treaty[pView]));
if (currentReport->TurnOfContact < 0) {
loweredTextOut(canvas, -1, *MainTexture, width() / 2, yRelation + 19,
phrase(QStringLiteral("FRNOVISIT")));
} else {
loweredTextOut(canvas, -1, *MainTexture, xRelation, yRelation + 19,
phrase(QStringLiteral("FRLASTCONTACT")));
loweredTextOut(canvas, -1, *MainTexture, width() / 2, yRelation + 19,
Term::turnToString(currentReport->TurnOfContact));
}
}
if (extinct && bigImp)
frameImage(canvas, *bigImp, xIcon, yIcon, xSizeBig, ySizeBig, 0, 200, false);
}
if (currentReport->TurnOfCivilReport >= 0) {
fillSeamless(canvas, xReport, yReport, wReport, hReport, 0, 0, paper);
canvas.brush().setColor(MainTexture->colorBevelShade);
canvas.fillRect(QRect(xReport + wReport, yReport + PaperShade, PaperShade, hReport));
canvas.fillRect(QRect(xReport + PaperShade, yReport + hReport, wReport + PaperShade - PaperShade, PaperShade));
canvas.brush().setStyle(Qt::NoBrush);
int y = 0;
for (int i = 0; i < reportText.size(); ++i) {
if (i >= linesDown && i < linesDown + ReportLines) {
const QString &s = reportText.at(i);
if (!s.isEmpty())
biColorTextOut(canvas, Colors->image().pixelColor(clkMisc, cliPaperText), colorFromDelphi(0x7F007F),
xReport + 8, yReport + LineSpacing * y, s);
++y;
}
}
} else {
const QString s = phrase(QStringLiteral("FRNOCIVILREP"));
risedTextOut(canvas, (width() - biColorTextWidth(canvas, s)) / 2, yReport + hReport / 2 - 10, s);
}
if (ClientTools::hasSaveOption(Term::optionChecked, ClientTools::soTellAI)) {
// Server(sGetAIInfo,...) returning a live AI-credits string isn't wired here yet.
loweredTextOut(canvas, -1, *MainTexture, 42, 445, QString());
} else {
loweredTextOut(canvas, -2, *MainTexture, 42, 445, phrase(QStringLiteral("MENU_TELLAI")));
}
ui->contactBtn->setBack(canvas, ui->contactBtn->x(), ui->contactBtn->y());
canvas.end();
markUsedOffscreen(width(), height());
}
void NatStatDlg::dialogBtnClicked()
{
const int contactResult = Term::mainScreenDipCall(scContact + (pView << 4));
if (contactResult < rExecuted) {
if (contactResult == eColdWar) {
// SoundMessage(...) - Messg.pas's sound-message variant isn't wired here yet.
} else if (ClientTools::myRO->Government == gAnarchy) {
// "We are in anarchy" - Messg.pas sound message, not wired here yet.
} else if (contactResult == eAnarchy) {
if (ClientTools::myRO->Treaty[pView] >= trPeace) {
if (Term::mainScreenContactRefused(pView, QStringLiteral("FRANARCHY")))
smartUpdateContent();
}
}
} else {
close();
}
}
void NatStatDlg::toggleBtnClicked()
{
popup->clear();
if (ClientTools::g.Difficulty[ClientTools::me] != 0) {
QAction *action = popup->addAction(
Tribes::tribe[static_cast<std::size_t>(ClientTools::me)]->tPhrase(QStringLiteral("TITLE_NATION")));
action->setCheckable(true);
action->setChecked(ClientTools::me == pView);
connect(action, &QAction::triggered, this, [this] { playerClicked(ClientTools::me); });
}
for (int extinctPart = 0; extinctPart <= 1; ++extinctPart) {
const int startCount = popup->actions().size();
for (int p1 = 0; p1 < nPl; ++p1) {
const bool aliveMatch = (1u << p1 & static_cast<unsigned>(ClientTools::myRO->Alive)) != 0 &&
ClientTools::myRO->Treaty[p1] >= trNone;
const bool extinctMatch = ClientTools::g.Difficulty[p1] > 0 &&
(1u << p1 & static_cast<unsigned>(ClientTools::myRO->Alive)) == 0;
if ((extinctPart != 0 && extinctMatch) || (extinctPart == 0 && aliveMatch)) {
QString caption = Tribes::tribe[static_cast<std::size_t>(p1)]->tPhrase(QStringLiteral("TITLE_NATION"));
if (extinctPart)
caption = QLatin1Char('(') + caption + QLatin1Char(')');
QAction *action = popup->addAction(caption);
action->setCheckable(true);
action->setChecked(p1 == pView);
connect(action, &QAction::triggered, this, [this, p1] { playerClicked(p1); });
}
}
if (startCount > 0 && popup->actions().size() > startCount)
popup->insertSeparator(popup->actions().at(startCount));
}
popup->popup(mapToGlobal(QPoint(ui->toggleBtn->x(), ui->toggleBtn->y() + ui->toggleBtn->height())));
}
void NatStatDlg::tellAIBtnClicked()
{
Term::optionChecked ^= (1u << ClientTools::soTellAI);
ui->tellAIBtn->setButtonIndex(ClientTools::hasSaveOption(Term::optionChecked, ClientTools::soTellAI) ? 3 : 2);
smartUpdateContent();
}
void NatStatDlg::ecoChange()
{
if (isVisible() && pView == ClientTools::me) {
selfReport.Government = ClientTools::myRO->Government;
selfReport.Money = ClientTools::myRO->Money;
smartUpdateContent();
}
}
void NatStatDlg::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_F9) {
int i = 0;
do {
pView = (pView + 1) % nPl;
++i;
} while (i < nPl && !((1u << pView & static_cast<unsigned>(ClientTools::myRO->Alive)) != 0 &&
ClientTools::myRO->Treaty[pView] >= trNone));
if (i >= nPl)
pView = ClientTools::me;
playerClicked(pView);
} else {
BufferedDrawDlg::keyPressEvent(event);
}
}
} // namespace NatStat
} // namespace Cevo