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

367 lines
15 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 "Diagram.h"
#include "ui_Diagram.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Cevo/Database.h"
#include "ClientTools.h"
#include "Tribes.h"
#include <QHash>
#include <QKeyEvent>
#include <QMenu>
#include <vector>
namespace Cevo {
namespace Diagram {
namespace {
// 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_PAGE"), QStringLiteral("Next chart")},
{QStringLiteral("BTN_SELECT"), QStringLiteral("Select nation")},
{QStringLiteral("TITLE_DIAGRAMS"), QStringLiteral("Charts")},
{QStringLiteral("TITLE_SHIPS"), QStringLiteral("Colonization Ships")},
{QStringLiteral("SHARE"), QStringLiteral("%1/%2")},
{QStringLiteral("SHIPHAB"), QStringLiteral("Habitation")},
{QStringLiteral("SHIPPOW"), QStringLiteral("Power")},
{QStringLiteral("SHIPCOMP"), QStringLiteral("Components")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? it.value() : key;
}
QString phraseDiagram(int mode)
{
static const QHash<int, QString> table = {
{stPop, QStringLiteral("Population")}, {stTerritory, QStringLiteral("Territory")},
{stMil, QStringLiteral("Military")}, {stScience, QStringLiteral("Science")},
{stExplore, QStringLiteral("Explored")}, {stWork, QStringLiteral("Workers")},
};
const auto it = table.constFind(mode);
return it != table.constEnd() ? it.value() : QString();
}
constexpr int Border = 24;
constexpr int RoundPixels[nStat] = { 0, 0, 0, 5, 5, 5 };
constexpr int yArea = 48;
constexpr int xComp[6] = { -60, -28, 4, 4, 36, 68 };
constexpr int yComp[6] = { -40, -40, -79, -1, -40, -40 };
constexpr int xPow[4] = { -116, -116, -116, -116 };
constexpr int yPow[4] = { -28, 0, -44, 16 };
constexpr int xHab[2] = { 23, 23 };
constexpr int yHab[2] = { -81, 1 };
void shareBar(Dpi::Canvas &canvas, int x, int y, const QString &cap, int val0, int val1)
{
loweredTextOut(canvas, -1, *MainTexture, x - 2, y, cap);
dLine(canvas, x - 2, x + 169, y + 16, MainTexture->colorTextShade, MainTexture->colorTextLight);
const QString s = val0 > 0 ? phrase(QStringLiteral("SHARE")).arg(val0).arg(val1) : QStringLiteral("0");
risedTextOut(canvas, x + 170 - biColorTextWidth(canvas, s), y, s);
}
} // namespace
void paintColonyShip(Dpi::Canvas &canvas, int player, int left, int width, int top)
{
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(left, top, width, 200));
canvas.brush().setStyle(Qt::NoBrush);
frame(canvas, left - 1, top - 1, left + width, top + 200, MainTexture->colorBevelShade,
MainTexture->colorBevelLight);
rframe(canvas, left - 2, top - 2, left + width + 1, top + 200 + 1, MainTexture->colorBevelShade,
MainTexture->colorBevelLight);
// stars
Database::delphiRandSeed = player * 11111;
for (int i = 1; i <= width - 16; ++i) {
const int x = Database::delphiRandom((width - 16) * 200);
const int r = Database::delphiRandom(13) + 28;
const int grey = std::min(255, (r * r * r * r) / 10001);
canvas.setPixel(x / 200 + 8, x % 200 + top, QColor(grey, grey, grey));
}
int nComp = static_cast<int>(ClientTools::myRO->Ship[player].Parts[spComp]);
int nPow = static_cast<int>(ClientTools::myRO->Ship[player].Parts[spPow]);
int nHab = static_cast<int>(ClientTools::myRO->Ship[player].Parts[spHab]);
if (nComp > 6) nComp = 6;
if (nPow > 4) nPow = 4;
if (nHab > 2) nHab = 2;
for (int i = 0; i < nHab; ++i)
sprite(canvas, *HGrSystem2, left + width / 2 + xHab[i], top + 100 + yHab[i], 80, 80, 34, 1);
for (int i = 0; i < nComp; ++i)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[i], top + 100 + yComp[i], 32, 80, 1, 1);
if (nComp > 0)
for (int i = 3; i >= nPow; --i)
sprite(canvas, *HGrSystem2, left + width / 2 + xPow[i] + 40, top + 100 + yPow[i], 16, 27, 1, 82);
for (int i = nPow - 1; i >= 0; --i)
sprite(canvas, *HGrSystem2, left + width / 2 + xPow[i], top + 100 + yPow[i], 56, 28, 58, 82);
if (nComp < 3 && nHab >= 1)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[2] + 32 - 16, top + 100 + 7 + yComp[2], 16, 27, 1, 82);
if (nComp >= 3 && nHab < 1)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[2] + 32, top + 100 + 7 + yComp[2], 16, 27, 18, 82);
if (nComp < 4 && nHab >= 2)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[3] + 32 - 16, top + 100 + 46 + yComp[3], 16, 27, 1, 82);
if (nComp >= 4 && nHab < 2)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[3] + 32, top + 100 + 46 + yComp[3], 16, 27, 18, 82);
if (nComp != 6 && nComp != 2 && !(nComp == 0 && nPow < 1))
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[nComp], top + 100 + 7 + yComp[nComp], 16, 27, 18, 82);
if (nComp != 6 && nComp != 3 && !(nComp == 0 && nPow < 2))
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[nComp], top + 100 + 46 + yComp[nComp], 16, 27, 18, 82);
if (nComp == 2)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[3], top + 100 + 7 + yComp[3], 16, 27, 18, 82);
if (nComp == 3)
sprite(canvas, *HGrSystem2, left + width / 2 + xComp[4], top + 100 + 7 + yComp[4], 16, 27, 18, 82);
}
DiaDlg::DiaDlg(QWidget *parent) : FramedDlg(parent), ui(new Ui::DiaDlg)
{
ui->setupUi(this);
ui->closeBtn->setButtonIndex(0);
ui->toggleBtn->setButtonIndex(15);
popup = new QMenu(this);
titleHeight = WideFrame + 20;
innerHeight = height() - titleHeight - NarrowFrame;
captionRight = ui->closeBtn->x();
captionLeft = ui->toggleBtn->x() + ui->toggleBtn->width();
initButtons();
ui->closeBtn->onClick = [this] { close(); };
ui->toggleBtn->onClick = [this] { toggleBtnClicked(); };
}
DiaDlg::~DiaDlg()
{
delete ui;
}
void DiaDlg::offscreenPaint()
{
FramedDlg::offscreenPaint();
Dpi::Canvas &canvas = offscreen.canvas();
canvas.begin();
if (kind == DiagramKind::Chart) {
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Tiny)]);
canvas.font().setColor(QColor(0x80, 0x80, 0x80));
const int roundRange = RoundPixels[mode] * (ClientTools::myRO->Turn - 1) / (innerWidth - 2 * Border);
std::vector<std::int32_t> list(static_cast<std::size_t>(ClientTools::myRO->Turn + 2));
auto roundValue = [&](int t) {
const int n = std::min(t, roundRange);
int sum = 0;
for (int i = t - n; i <= t; ++i)
sum += list[static_cast<std::size_t>(i)];
return sum / (n + 1);
};
int max = 0;
if (mode == stExplore) {
max = ClientTools::g.lx * ClientTools::g.ly;
} else {
max = -1;
for (int p = 0; p < nPl; ++p) {
if (ClientTools::g.Difficulty[p] > 0 &&
ClientTools::server(sGetChart + (mode << 4), ClientTools::me, p, list.data()) >= rExecuted) {
for (int t = 0; t < ClientTools::myRO->Turn; ++t) {
const int r = roundValue(t);
if (r > max)
max = r;
}
}
}
}
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(0, 0, innerWidth, innerHeight));
canvas.brush().setStyle(Qt::NoBrush);
canvas.pen().setColor(QColor(0x60, 0x60, 0x60));
canvas.moveTo(Border, innerHeight - Border);
canvas.lineTo(innerWidth - Border, innerHeight - Border);
const int lineStep = ClientTools::myRO->Turn >= 800 ? 200 : ClientTools::myRO->Turn >= 400 ? 100 : 50;
for (int t = 0; t <= (ClientTools::myRO->Turn - 1) / lineStep; ++t) {
const int x = Border + (innerWidth - 2 * Border) * t * lineStep / (ClientTools::myRO->Turn - 1);
canvas.moveTo(x, Border);
canvas.lineTo(x, innerHeight - Border);
const QString s = QString::number(std::abs(turnToYear(t * lineStep)));
canvas.textOut(x - canvas.textWidth(s) / 2, Border - 16, s);
}
int y0 = 0;
if (max > 0) {
for (int p = 0; p < nPl; ++p) {
if (ClientTools::g.Difficulty[p] > 0 &&
ClientTools::server(sGetChart + (mode << 4), ClientTools::me, p, list.data()) >= rExecuted) {
canvas.pen().setColor(Tribes::tribe[static_cast<std::size_t>(p)]->color);
int stop = ClientTools::myRO->Turn - 1;
while (stop > 0 && list[static_cast<std::size_t>(stop)] == 0)
--stop;
for (int t = 0; t <= stop; ++t) {
const int r = roundValue(t);
const int x = Border + (innerWidth - 2 * Border) * t / (ClientTools::myRO->Turn - 1);
const int y = innerHeight - Border - (innerHeight - 2 * Border) * r / max;
if (t == 0)
canvas.moveTo(x, y);
else if (RoundPixels[mode] == 0) {
if (y != y0 || t == stop)
canvas.lineTo(x, y);
} else {
canvas.lineTo(x, y);
}
y0 = y;
}
}
}
}
} else {
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
fillOffscreen(0, 0, innerWidth, innerHeight);
paintColonyShip(canvas, player, 8, innerWidth - 16, yArea);
shareBar(canvas, innerWidth / 2 - 85, innerHeight - 62, phrase(QStringLiteral("SHIPHAB")),
ClientTools::myRO->Ship[player].Parts[spHab], 2);
shareBar(canvas, innerWidth / 2 - 85, innerHeight - 43, phrase(QStringLiteral("SHIPPOW")),
ClientTools::myRO->Ship[player].Parts[spPow], 4);
shareBar(canvas, innerWidth / 2 - 85, innerHeight - 24, phrase(QStringLiteral("SHIPCOMP")),
ClientTools::myRO->Ship[player].Parts[spComp], 6);
}
canvas.end();
markUsedOffscreen(innerWidth, innerHeight);
}
void DiaDlg::vpaint(Dpi::Canvas &canvas)
{
FramedDlg::vpaint(canvas);
if (!MainTexture)
return;
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
const QString s = kind == DiagramKind::Chart
? phraseDiagram(mode)
: Tribes::tribe[static_cast<std::size_t>(player)]->tPhrase(QStringLiteral("SHORTNAME"));
loweredTextOut(canvas, -1, *MainTexture, (width() - biColorTextWidth(canvas, s)) / 2, 31, s);
}
void DiaDlg::showEvent(QShowEvent *event)
{
FramedDlg::showEvent(event);
if (windowMode() == WindowMode::Modal)
centerToScreen();
offscreenPaint();
}
void DiaDlg::resizeEvent(QResizeEvent *event)
{
FramedDlg::resizeEvent(event);
ui->closeBtn->move(width() - 38, ui->closeBtn->y());
}
void DiaDlg::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_F6 && kind == DiagramKind::Chart) {
toggleBtnClicked();
} else if (event->key() == Qt::Key_F8 && kind == DiagramKind::Ship) {
// Swallow (matches the original's empty branch).
} else {
FramedDlg::keyPressEvent(event);
}
}
void DiaDlg::showNewContentCharts(WindowMode newMode)
{
kind = DiagramKind::Chart;
mode = stPop;
ui->toggleBtn->setButtonIndex(15);
ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_PAGE")));
setWindowTitle(phrase(QStringLiteral("TITLE_DIAGRAMS")));
showNewContent(newMode);
}
void DiaDlg::showNewContentShip(WindowMode newMode, int p)
{
kind = DiagramKind::Ship;
if (p < 0) {
player = ClientTools::me;
while (ClientTools::myRO->Ship[player].Parts[spComp] + ClientTools::myRO->Ship[player].Parts[spPow] +
ClientTools::myRO->Ship[player].Parts[spHab] ==
0)
player = (player + 1) % nPl;
} else {
player = p;
}
ui->toggleBtn->setButtonIndex(28);
ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_SELECT")));
setWindowTitle(phrase(QStringLiteral("TITLE_SHIPS")));
showNewContent(newMode);
}
void DiaDlg::toggleBtnClicked()
{
if (kind == DiagramKind::Chart) {
mode = (mode + 1) % nStat;
offscreenPaint();
update();
} else {
popup->clear();
for (int p1 = 0; p1 < nPl; ++p1) {
if (ClientTools::myRO->Ship[p1].Parts[spComp] + ClientTools::myRO->Ship[p1].Parts[spPow] +
ClientTools::myRO->Ship[p1].Parts[spHab] >
0) {
QAction *action =
popup->addAction(Tribes::tribe[static_cast<std::size_t>(p1)]->tPhrase(QStringLiteral("SHORTNAME")));
action->setCheckable(true);
action->setChecked(p1 == player);
connect(action, &QAction::triggered, this, [this, p1] { playerClicked(p1); });
}
}
popup->popup(mapToGlobal(QPoint(ui->toggleBtn->x(), ui->toggleBtn->y() + ui->toggleBtn->height())));
}
}
void DiaDlg::playerClicked(int p)
{
showNewContentShip(windowMode(), p);
}
} // namespace Diagram
} // namespace Cevo