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

526 lines
23 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 "Draft.h"
#include "ui_Draft.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Global.h"
#include "Help.h"
#include "Term.h"
#include "Tribes.h"
#include <QHash>
#include <QMouseEvent>
namespace Cevo {
namespace Draft {
namespace {
constexpr int MaxLines0 = 11;
constexpr int LinePitch = 20;
constexpr int xDomain = 30;
constexpr int yDomain0 = 464;
constexpr int DomainPitch = 40;
constexpr int xFeature = 38;
constexpr int yFeature0 = 42;
constexpr int xWeight = 100;
constexpr int yWeight0 = 271;
constexpr int xTotal = 20;
constexpr int xTotal2 = 34;
constexpr int yTotal0 = 354;
constexpr int xView = 17;
constexpr int yView0 = 283;
// 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("TITLE_DRAFT"), QStringLiteral("Unit Design")},
{QStringLiteral("BTN_OK"), QStringLiteral("OK")},
{QStringLiteral("DOMAIN0"), QStringLiteral("Ground units")},
{QStringLiteral("DOMAIN1"), QStringLiteral("Sea units")},
{QStringLiteral("DOMAIN2"), QStringLiteral("Air units")},
{QStringLiteral("UNITSTRENGTH"), QStringLiteral("Strength")},
{QStringLiteral("UNITTRANSPORT"), QStringLiteral("Transport")},
{QStringLiteral("UNITSPEED"), QStringLiteral("Speed")},
{QStringLiteral("UNITCOST"), QStringLiteral("Cost")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? it.value() : key;
}
QString phraseFeature(int code)
{
return phrases.lookup(QStringLiteral("FEATURES"), code);
}
QString phraseCostDiff(int difficulty)
{
return QStringLiteral("Cost (difficulty %1)").arg(difficulty);
}
void imageOpBCCItem(Dpi::Bitmap &dst, Dpi::Bitmap &src, const QPoint &pos, const QRect &srcRect, int color1,
int color2)
{
imageOpBCC(dst, src, pos.x(), pos.y(), srcRect.left(), srcRect.top(), srcRect.width(), srcRect.height(), color1,
color2);
}
bool isFeatureAuto(int feature)
{
return (AutoFeature & (1u << feature)) != 0;
}
} // namespace
DraftDlg::DraftDlg(QWidget *parent) : BufferedDrawDlg(parent), ui(new Ui::DraftDlg)
{
ui->setupUi(this);
initButtons();
helpContext = QStringLiteral("CLASSES");
setWindowTitle(phrase(QStringLiteral("TITLE_DRAFT")));
ui->okBtn->setCaption(phrase(QStringLiteral("BTN_OK")));
ui->groundArea->setToolTip(phrase(QStringLiteral("DOMAIN0")));
ui->seaArea->setToolTip(phrase(QStringLiteral("DOMAIN1")));
ui->airArea->setToolTip(phrase(QStringLiteral("DOMAIN2")));
back.setSize(width(), height());
back.canvas().begin();
back.canvas().fillRect(QRect(0, 0, back.width(), back.height()));
back.canvas().end();
tmpl.loadFromFile(getGraphicsDir() + QStringLiteral("/MiliRes") + PngExt);
ui->closeBtn->onClick = [this] {
setModalResult(Dpi::ModalResult::Cancel);
close();
};
ui->okBtn->onClick = [this] {
setModalResult(Dpi::ModalResult::Ok);
close();
};
onShow = [this] {
domain = dGround;
while (domain < dAir && Upgrade[domain][0].Preq != preNone &&
ClientTools::myRO->Tech[Upgrade[domain][0].Preq] < tsApplicable)
++domain;
maxLines = 0;
for (int d = 0; d < nDomains; ++d) {
if (Upgrade[d][0].Preq == preNone || ClientTools::myRO->Tech[Upgrade[d][0].Preq] >= tsApplicable) {
int count = 0;
for (int i = 0; i < nFeature; ++i)
if (isFeatureInList(d, i))
++count;
if (count > maxLines)
maxLines = count;
}
}
cut = (MaxLines0 - maxLines) * LinePitch;
ui->okBtn->move(ui->okBtn->x(), 477 - cut);
yDomain = yDomain0 - cut;
yWeight = yWeight0 - cut;
yTotal = yTotal0 - cut;
yView = yView0 - cut;
if (windowMode() == WindowMode::Modal)
centerToScreen(tmpl.width(), tmpl.height() - cut);
setDomain(domain);
ClientTools::server(sCreateDevModel, ClientTools::me, domain, nullptr);
ClientTools::myModel[ClientTools::myRO->nModel] = ClientTools::myRO->DevModel;
Term::initMyModel(ClientTools::myRO->nModel, false);
offscreenPaint();
incCap = -1;
decCap = -1;
};
}
DraftDlg::~DraftDlg()
{
delete ui;
}
void DraftDlg::showNewContent(WindowMode newMode)
{
BufferedDrawDlg::showNewContent(newMode);
}
bool DraftDlg::isFeatureInList(int d, int i) const
{
return !isFeatureAuto(i) && (1u << d & Feature[i].Domains) != 0 && Feature[i].Preq != preNA &&
(Feature[i].Preq == preNone ||
(Feature[i].Preq == preSun && ClientTools::myRO->Wonder[woSun].EffectiveOwner == ClientTools::me) ||
(Feature[i].Preq >= 0 && ClientTools::myRO->Tech[Feature[i].Preq] >= tsApplicable));
}
void DraftDlg::setDomain(int d)
{
auto prio = [](int fix) {
const int featurePreq = Feature[fix].Preq;
int result;
if (fix < mcFirstNonCap)
result = 10000 + fix;
else if (featurePreq == preNone)
result = 20000;
else if (featurePreq < 0)
result = 40000;
else
result = 30000 + ClientTools::advValue[static_cast<std::size_t>(featurePreq)];
if (!isFeatureAuto(fix))
result += 90000;
return result;
};
domain = d;
lines = 0;
for (int i = 0; i < nFeature; ++i) {
if (isFeatureInList(domain, i)) {
code[static_cast<std::size_t>(lines)] = i;
++lines;
}
}
yFeature = yFeature0 + (maxLines - lines) * LinePitch / 2;
for (int i = 0; i < lines - 1; ++i)
for (int j = i + 1; j < lines; ++j)
if (prio(code[static_cast<std::size_t>(i)]) > prio(code[static_cast<std::size_t>(j)]))
std::swap(code[static_cast<std::size_t>(i)], code[static_cast<std::size_t>(j)]);
}
void DraftDlg::offscreenPaint()
{
BufferedDrawDlg::offscreenPaint();
if (!MainTexture)
return;
unshareBitmap(back);
resize(width(), tmpl.height() - cut);
if (height() > MainTexture->height()) {
bitBltBitmap(back, 0, 0, width(), 64, MainTexture->image(), (MainTexture->width() - width()) / 2,
MainTexture->height() - 64);
bitBltBitmap(back, 0, 64, width(), height() - 64, MainTexture->image(), (MainTexture->width() - width()) / 2,
0);
} else {
bitBltBitmap(back, 0, 0, width(), height(), MainTexture->image(), (MainTexture->width() - width()) / 2,
(MainTexture->height() - height()) / 2);
}
imageOpB(back, tmpl, 0, 0, 0, 0, tmpl.width(), 64);
imageOpB(back, tmpl, 0, 64, 0, 64 + cut, tmpl.width(), tmpl.height() - 64 - cut);
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, 10, 7, windowTitle());
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
auto domainAvailable = [](int d) {
return Upgrade[d][0].Preq == preNone || ClientTools::myRO->Tech[Upgrade[d][0].Preq] >= tsApplicable;
};
int domainCount = 0;
for (int d = 0; d < nDomains; ++d)
if (domainAvailable(d))
++domainCount;
if (domainCount > 1) {
for (int d = 0; d < nDomains; ++d) {
if (domainAvailable(d)) {
const int x = xDomain + d * DomainPitch;
imageOpBCC(offscreen, Templates->data, x, yDomain, 142, 246 + 37 * d, 36, 36, 0,
d == domain ? 0x00C0FF : 0x606060);
}
}
frame(canvas, xDomain - 11, yDomain - 3, xDomain + 2 * DomainPitch + 46, yDomain + 38,
colorFromDelphi(0xB0B0B0), Qt::white);
rframe(canvas, xDomain - 12, yDomain - 4, xDomain + 2 * DomainPitch + 47, yDomain + 39, Qt::white,
colorFromDelphi(0xB0B0B0));
}
ui->groundArea->move(ui->groundArea->x(), yDomain);
ui->groundArea->setVisible(domainAvailable(dGround));
ui->seaArea->move(ui->seaArea->x(), yDomain);
ui->seaArea->setVisible(domainAvailable(dSea));
ui->airArea->move(ui->airArea->x(), yDomain);
ui->airArea->setVisible(domainAvailable(dAir));
const TModel &devModel = ClientTools::myRO->DevModel;
// strength bar
{
int y = yTotal;
darkGradient(canvas, xTotal - 6, y + 1, 184, 2);
darkGradient(canvas, xTotal2 + 172, y + 1, 95, 2);
risedTextOut(canvas, xTotal - 2, y, phrase(QStringLiteral("UNITSTRENGTH")));
risedTextOut(canvas, xTotal + 112 + 30, y, QStringLiteral("x%1").arg(devModel.MStrength));
risedTextOut(canvas, xTotal2 + 148 + 30, y, QStringLiteral("="));
const QString s = QStringLiteral("%1/%2").arg(devModel.Attack).arg(devModel.Defense);
risedTextOut(canvas, xTotal2 + 170 + 64 + 30 - biColorTextWidth(canvas, s), y, s);
// transport bar
if (devModel.MTrans > 0) {
y = yTotal + 19;
darkGradient(canvas, xTotal - 6, y + 1, 184, 1);
darkGradient(canvas, xTotal2 + 172, y + 1, 95, 1);
risedTextOut(canvas, xTotal - 2, y, phrase(QStringLiteral("UNITTRANSPORT")));
risedTextOut(canvas, xTotal + 112 + 30, y, QStringLiteral("x%1").arg(devModel.MTrans));
risedTextOut(canvas, xTotal2 + 148 + 30, y, QStringLiteral("="));
canvas.font().setColor(Qt::black);
int dx = -237 - 30;
for (int i = mcFirstNonCap - 1; i >= 3; --i) {
if (i == mcSeaTrans || i == mcCarrier || i == mcAirTrans) {
const int num = devModel.Cap[i] * devModel.MTrans;
if (num > 0) {
dx += 15;
canvas.brush().setColor(colorFromDelphi(0xC0C0C0));
canvas.frameRect(QRect(xTotal2 - 3 - dx, y + 2, 14, 14));
canvas.brush().setStyle(Qt::NoBrush);
sprite(canvas, *HGrSystem, xTotal2 - 1 - dx, y + 4, 10, 10, 66 + i % 11 * 11,
137 + i / 11 * 11);
if (num > 1) {
const QString countText = QString::number(num);
const int w = canvas.textWidth(countText);
dx += w + 1;
canvas.brush().setColor(Qt::white);
canvas.fillRect(QRect(xTotal2 - 3 - dx, y + 2, w + 2, 14));
canvas.brush().setStyle(Qt::NoBrush);
canvas.textOut(xTotal2 - 3 - dx + 1, y, countText);
}
}
}
}
}
// speed bar
y = yTotal + 38;
loweredTextOut(canvas, -1, *MainTexture, xTotal - 2, y, phrase(QStringLiteral("UNITSPEED")));
dLine(canvas, xTotal - 2, xTotal + 174, y + 16, MainTexture->colorBevelShade, MainTexture->colorBevelLight);
dLine(canvas, xTotal2 + 176, xTotal2 + 263, y + 16, MainTexture->colorBevelShade,
MainTexture->colorBevelLight);
QString s2 = movementToString(devModel.Speed);
risedTextOut(canvas, xTotal2 + 170 + 64 + 30 - canvas.textWidth(s2), y, s2);
// cost bar
y = yTotal + 57;
loweredTextOut(canvas, -1, *MainTexture, xTotal - 2, y, phrase(QStringLiteral("UNITCOST")));
loweredTextOut(canvas, -1, *MainTexture, xTotal + 112 + 30, y, QStringLiteral("x%1").arg(devModel.MCost));
loweredTextOut(canvas, -1, *MainTexture, xTotal2 + 148 + 30, y, QStringLiteral("="));
dLine(canvas, xTotal - 2, xTotal + 174, y + 16, MainTexture->colorBevelShade, MainTexture->colorBevelLight);
dLine(canvas, xTotal2 + 176, xTotal2 + 263, y + 16, MainTexture->colorBevelShade,
MainTexture->colorBevelLight);
QString s3 = QString::number(devModel.Cost);
risedTextOut(canvas, xTotal2 + 170 + 64 + 30 - 12 - canvas.textWidth(s3), y, s3);
sprite(canvas, *HGrSystem, xTotal2 + 170 + 54 + 30, y + 4, 10, 10, 88, 115);
if (ClientTools::g.Difficulty[ClientTools::me] != 2) {
y = yTotal + 76;
loweredTextOut(canvas, -1, *MainTexture, xTotal - 2, y,
phraseCostDiff(ClientTools::g.Difficulty[ClientTools::me]));
loweredTextOut(canvas, -1, *MainTexture, xTotal2 + 148 + 30, y, QStringLiteral("="));
dLine(canvas, xTotal - 2, xTotal + 174, y + 16, MainTexture->colorBevelShade,
MainTexture->colorBevelLight);
dLine(canvas, xTotal2 + 176, xTotal2 + 263, y + 16, MainTexture->colorBevelShade,
MainTexture->colorBevelLight);
QString s4 = QString::number(devModel.Cost * BuildCostMod[ClientTools::g.Difficulty[ClientTools::me]] / 12);
risedTextOut(canvas, xTotal2 + 170 + 64 + 30 - 12 - canvas.textWidth(s4), y, s4);
sprite(canvas, *HGrSystem, xTotal2 + 170 + 54 + 30, y + 4, 10, 10, 88, 115);
}
}
// weight display
GraphicSetItem *weightOn = Templates->searchByName(QStringLiteral("WeightOn"));
GraphicSetItem *weightOff = Templates->searchByName(QStringLiteral("WeightOff"));
for (int i = 0; i < devModel.MaxWeight; ++i) {
GraphicSetItem *item = i < devModel.Weight ? weightOn : weightOff;
if (item)
imageOpBCCItem(offscreen, Templates->data, QPoint(xWeight + 20 * i, yWeight), item->boundsRect(), 0,
0x949494);
}
for (int i = 0; i < lines; ++i) {
const int c = code[static_cast<std::size_t>(i)];
if (!isFeatureAuto(c)) {
if (c < mcFirstNonCap) {
bitBltBitmap(offscreen, xFeature - 21, yFeature + 2 + LinePitch * i, 12, 12, HGrSystem->data, 169,
172);
bitBltBitmap(offscreen, xFeature - 9, yFeature + 2 + LinePitch * i, 12, 12, HGrSystem->data, 169, 159);
rframe(canvas, xFeature - 22, yFeature + 1 + LinePitch * i, xFeature + 3, yFeature + 14 + LinePitch * i,
MainTexture->colorBevelShade, MainTexture->colorBevelLight);
} else {
bitBltBitmap(offscreen, xFeature - 9, yFeature + 2 + LinePitch * i, 12, 12, HGrSystem->data, 169,
185 + 13 * devModel.Cap[c]);
rframe(canvas, xFeature - 10, yFeature + 1 + LinePitch * i, xFeature + 3, yFeature + 14 + LinePitch * i,
MainTexture->colorBevelShade, MainTexture->colorBevelLight);
}
lightGradient(canvas, xFeature + 34, yFeature + LinePitch * i, 50, HGrSystem->data.image().pixelColor(187, 137));
int capWeight = (domain == dGround && c == mcDefense) ? 2 : Feature[c].Weight;
const int n = capWeight + Feature[c].Cost;
int d = 6;
while (n > 0 && (n - 1) * d * 2 > 48 - 10)
--d;
for (int j = 0; j < n; ++j) {
if (j < capWeight)
sprite(canvas, *HGrSystem, xFeature + 54 + (j * 2 + 1 - n) * d, yFeature + 2 + LinePitch * i + 1,
10, 10, 88, 126);
else
sprite(canvas, *HGrSystem, xFeature + 54 + (j * 2 + 1 - n) * d, yFeature + 2 + LinePitch * i + 1,
10, 10, 88, 115);
}
}
darkGradient(canvas, xFeature + 17, yFeature + LinePitch * i, 16, 1);
frame(canvas, xFeature + 18, yFeature + 1 + LinePitch * i, xFeature + 31, yFeature + 14 + LinePitch * i,
colorFromDelphi(0xC0C0C0), colorFromDelphi(0xC0C0C0));
sprite(canvas, *HGrSystem, xFeature + 20, yFeature + 3 + LinePitch * i, 10, 10, 66 + c % 11 * 11,
137 + c / 11 * 11);
const int textColor = devModel.Cap[c] > 0 ? colorToDelphi(MainTexture->colorLitText) : -1;
if (c < mcFirstNonCap)
loweredTextOut(canvas, textColor, *MainTexture, xFeature + 7, yFeature + LinePitch * i - 1,
QString::number(devModel.Cap[c]));
loweredTextOut(canvas, textColor, *MainTexture, xFeature + 88, yFeature + LinePitch * i - 1, phraseFeature(c));
}
// free features
int j = 0;
for (int i = 0; i < nFeature; ++i) {
if (isFeatureAuto(i) && (1u << domain & Feature[i].Domains) != 0 && Feature[i].Preq != preNA &&
((Feature[i].Preq == preSun && ClientTools::myRO->Wonder[woSun].EffectiveOwner == ClientTools::me) ||
(Feature[i].Preq >= 0 && ClientTools::myRO->Tech[Feature[i].Preq] >= tsApplicable)) &&
!(Feature[i].Preq == adSteamEngine && ClientTools::myRO->Tech[adNuclearPower] >= tsApplicable)) {
darkGradient(canvas, xWeight + 4, yWeight + 32 + LinePitch * j, 16, 1);
frame(canvas, xWeight + 5, yWeight + 33 + LinePitch * j, xWeight + 18, yWeight + 47 + LinePitch * j,
colorFromDelphi(0xC0C0C0), colorFromDelphi(0xC0C0C0));
sprite(canvas, *HGrSystem, xWeight + 7, yWeight + 36 + LinePitch * j, 10, 10, 66 + i % 11 * 11,
137 + i / 11 * 11);
loweredTextOut(canvas, -1, *MainTexture, xWeight + 26, yWeight + 31 + LinePitch * j, phraseFeature(i));
++j;
}
}
if (bigImp) {
Tribes::Tribe &me = *Tribes::tribe[static_cast<std::size_t>(ClientTools::me)];
Tribes::ModelPicture &mp = me.modelPictureAt(ClientTools::myRO->nModel);
frameImage(canvas, *bigImp, xView + 4, yView + 4, xSizeBig, ySizeBig, 0, 0);
if (mp.hGr)
sprite(canvas, *mp.hGr, xView, yView, 64, 44, mp.pix % 10 * 65 + 1, mp.pix / 10 * 49 + 1);
}
canvas.end();
markUsedOffscreen(width(), height());
}
void DraftDlg::resizeEvent(QResizeEvent *event)
{
BufferedDrawDlg::resizeEvent(event);
ui->closeBtn->move(width() - 31, ui->closeBtn->y());
}
void DraftDlg::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
const int x = event->pos().x();
const int y = event->pos().y();
auto domainAvailable = [](int d) {
return Upgrade[d][0].Preq == preNone || ClientTools::myRO->Tech[Upgrade[d][0].Preq] >= tsApplicable;
};
for (int d = 0; d < nDomains; ++d) {
if (d != domain && domainAvailable(d) && x >= xDomain + d * DomainPitch &&
x < xDomain + d * DomainPitch + 36 && y >= yDomain && y < yDomain + 36) {
setDomain(d);
ClientTools::server(sCreateDevModel, ClientTools::me, domain, nullptr);
ClientTools::myModel[ClientTools::myRO->nModel] = ClientTools::myRO->DevModel;
Term::initMyModel(ClientTools::myRO->nModel, false);
smartUpdateContent();
}
}
if (y >= yFeature && y < yFeature + LinePitch * lines) {
const int i = (y - yFeature) / LinePitch;
const int c = code[static_cast<std::size_t>(i)];
if (x >= xFeature - 21 && x < width() && (event->modifiers() & Qt::ShiftModifier)) {
if (auto *dlg = Term::mainScreenHelpDlg())
dlg->showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Feature, c);
} else if (!isFeatureAuto(c)) {
if (c < mcFirstNonCap && x >= xFeature - 21 && x < xFeature - 21 + 12) {
incCap = c;
bitBltBitmap(offscreen, xFeature - 21, yFeature + 2 + LinePitch * i, 12, 12, HGrSystem->data, 182,
172);
smartInvalidate();
} else if (x >= xFeature - 9 && x < xFeature - 9 + 12) {
decCap = c;
if (c < mcFirstNonCap)
bitBltBitmap(offscreen, xFeature - 9, yFeature + 2 + LinePitch * i, 12, 12, HGrSystem->data,
182, 159);
else
bitBltBitmap(offscreen, xFeature - 9, yFeature + 2 + LinePitch * i, 12, 12, HGrSystem->data,
182, 185 + 13 * ClientTools::myRO->DevModel.Cap[c]);
smartInvalidate();
}
}
}
}
BufferedDrawDlg::mousePressEvent(event);
}
void DraftDlg::mouseReleaseEvent(QMouseEvent *event)
{
if (incCap >= 0) {
const int newValue = ClientTools::myRO->DevModel.Cap[incCap] + 1;
ClientTools::server(sSetDevModelCap + (newValue << 4), ClientTools::me, incCap, nullptr);
ClientTools::myModel[ClientTools::myRO->nModel] = ClientTools::myRO->DevModel;
Term::initMyModel(ClientTools::myRO->nModel, false);
smartUpdateContent();
incCap = -1;
} else if (decCap >= 0) {
if (decCap >= mcFirstNonCap || ClientTools::myRO->DevModel.Cap[decCap] > 0) {
int newValue = ClientTools::myRO->DevModel.Cap[decCap] - 1;
if (decCap >= mcFirstNonCap)
newValue = -newValue;
ClientTools::server(sSetDevModelCap + (newValue << 4), ClientTools::me, decCap, nullptr);
ClientTools::myModel[ClientTools::myRO->nModel] = ClientTools::myRO->DevModel;
Term::initMyModel(ClientTools::myRO->nModel, false);
}
smartUpdateContent();
decCap = -1;
}
BufferedDrawDlg::mouseReleaseEvent(event);
}
} // namespace Draft
} // namespace Cevo