From 65aeb03394f1f8d641e9e7960d38e148ec116338 Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Tue, 3 Nov 2026 17:35:22 +0100 Subject: [PATCH] added battle with requirements --- game/localplayer/Battle.cpp | 280 ++++++++++++++++++++++++++ game/localplayer/Battle.h | 74 +++++++ game/localplayer/Battle.ui | 71 +++++++ widget/include/Cevo/CevoScreenTools.h | 3 + widget/src/CevoScreenTools.cpp | 13 ++ 5 files changed, 441 insertions(+) create mode 100644 game/localplayer/Battle.cpp create mode 100644 game/localplayer/Battle.h create mode 100644 game/localplayer/Battle.ui diff --git a/game/localplayer/Battle.cpp b/game/localplayer/Battle.cpp new file mode 100644 index 0000000..f7ed6d6 --- /dev/null +++ b/game/localplayer/Battle.cpp @@ -0,0 +1,280 @@ +// 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 "Battle.h" +#include "ui_Battle.h" + +#include "Cevo/CevoScreenTools.h" +#include "Cevo/CevoTexture.h" +#include "ClientTools.h" +#include "Term.h" + +#include +#include +#include +#include + +#include + +namespace Cevo { +namespace Battle { + +namespace { +constexpr int Border = 3; +constexpr int MessageLineSpacing = 20; +constexpr QRgb DamageColor = 0x0000E0; +constexpr QRgb FanaticColor = 0x800080; +constexpr QRgb FirstStrikeColor = 0xA0A0A0; + +// TODO: needs Phrases[] (StringTables.pas not ported) - plain English literals. +QString phrase(const QString &key) +{ + static const QHash table = { + {QStringLiteral("BTN_YES"), QStringLiteral("Yes")}, + {QStringLiteral("BTN_NO"), QStringLiteral("No")}, + {QStringLiteral("TITLE_SUICIDE"), QStringLiteral("Hopeless attack")}, + {QStringLiteral("SUICIDE"), QStringLiteral("This attack has no chance of success.\\Attack anyway?")}, + }; + const auto it = table.constFind(key); + return it != table.constEnd() ? *it : key; +} +} // namespace + +BattleDlg::BattleDlg(QWidget *parent) : DrawDlg(parent), ui(new Ui::BattleDlg) +{ + ui->setupUi(this); + isoMap = std::make_unique(); + ui->okBtn->setCaption(phrase(QStringLiteral("BTN_YES"))); + ui->cancelBtn->setCaption(phrase(QStringLiteral("BTN_NO"))); + initButtons(); + + ui->okBtn->onClick = [this] { + setModalResult(Dpi::ModalResult::Ok); + close(); + }; + ui->cancelBtn->onClick = [this] { + setModalResult(Dpi::ModalResult::Cancel); + close(); + }; + + onShow = [this] { + if (isSuicideQuery) { + setGeometry(x(), y(), 300, 288); + ui->okBtn->setVisible(true); + ui->cancelBtn->setVisible(true); + centerToScreen(); + } else { + setGeometry(x(), y(), 178, 178); + ui->okBtn->setVisible(false); + ui->cancelBtn->setVisible(false); + } + }; +} + +BattleDlg::~BattleDlg() +{ + delete ui; +} + +void BattleDlg::paintBattleOutcome(Dpi::Canvas &canvas, int xm, int ym, int unitIx, int toLocation, + const TBattleForecastEx &forecastData) +{ + constexpr int maxBar = 65; + + int euix = 0; + TUnitInfo unitInfo{}; + ClientTools::getUnitInfo(toLocation, euix, unitInfo); + + const bool firstStrike = ClientTools::myModel[ClientTools::myUn[unitIx].mix].Cap[mcFirst] > 0 && + forecastData.DBaseDamage >= unitInfo.Health; + int aDamage = ClientTools::myUn[unitIx].Health - forecastData.EndHealthAtt; + if (firstStrike) + aDamage += forecastData.ABaseDamage / 2; + const int dDamage = unitInfo.Health - forecastData.EndHealthDef; + int strMax = forecastData.AStr > forecastData.DStr ? forecastData.AStr : forecastData.DStr; + int damageMax = aDamage > dDamage ? aDamage : dDamage; + if (forecastData.ABaseDamage > forecastData.DBaseDamage) + strMax = strMax * damageMax / forecastData.ABaseDamage; + else + strMax = strMax * damageMax / forecastData.DBaseDamage; + + const int lAStr = forecastData.AStr * maxBar / strMax; + const int lDStr = forecastData.DStr * maxBar / strMax; + const int lADamage = aDamage * maxBar / damageMax; + const int lABaseDamage = forecastData.ABaseDamage * maxBar / damageMax; + const int lAAvoidedDamage = firstStrike ? lABaseDamage / 2 : 0; + const int lDDamage = dDamage * maxBar / damageMax; + const int lDBaseDamage = forecastData.DBaseDamage * maxBar / damageMax; + + darkGradient(canvas, xm - 8 - lAStr, ym - 8, lAStr, 2); + vDarkGradient(canvas, xm - 8, ym - 8 - lDStr, lDStr, 2); + lightGradient(canvas, xm + 8, ym - 8, lDBaseDamage, DamageColor); + if (lDDamage > lDBaseDamage) + lightGradient(canvas, xm + 8 + lDBaseDamage, ym - 8, lDDamage - lDBaseDamage, FanaticColor); + if (lAAvoidedDamage > 0) + vLightGradient(canvas, xm - 8, ym + 8, lAAvoidedDamage, FirstStrikeColor); + vLightGradient(canvas, xm - 8, ym + 8 + lAAvoidedDamage, lABaseDamage - lAAvoidedDamage, DamageColor); + if (lADamage > lABaseDamage) + vLightGradient(canvas, xm - 8, ym + 8 + lABaseDamage, lADamage - lABaseDamage, FanaticColor); + if (HGrSystem) + sprite(canvas, *HGrSystem, xm - 12, ym - 12, 24, 24, 26, 146); + + QString labelText = QString::number(forecastData.AStr); + QSize textSize = canvas.textExtent(labelText); + if (textSize.width() / 2 + 2 > lAStr / 2) + risedTextOut(canvas, xm - 10 - textSize.width(), ym - (textSize.height() + 1) / 2, labelText); + else + risedTextOut(canvas, xm - 8 - (lAStr + textSize.width()) / 2, ym - (textSize.height() + 1) / 2, labelText); + + labelText = QString::number(forecastData.DStr); + textSize = canvas.textExtent(labelText); + if (textSize.height() / 2 > lDStr / 2) + risedTextOut(canvas, xm - (textSize.width() + 1) / 2, ym - 8 - textSize.height(), labelText); + else + risedTextOut(canvas, xm - (textSize.width() + 1) / 2, ym - 8 - (lDStr + textSize.height()) / 2, labelText); + + if (forecastData.EndHealthDef <= 0 && HGrSystem) + sprite(canvas, *HGrSystem, xm + 8 + lDDamage - 7, ym - 7, 14, 17, 51, 153); + labelText = QString::number(dDamage); + textSize = canvas.textExtent(labelText); + if (textSize.width() / 2 + 2 > lDDamage / 2) { + if (forecastData.EndHealthDef > 0) + risedTextOut(canvas, xm + 10, ym - (textSize.height() + 1) / 2, labelText); + } else { + risedTextOut(canvas, xm + 8 + (lDDamage - textSize.width()) / 2, ym - (textSize.height() + 1) / 2, labelText); + } + + if (forecastData.EndHealthAtt <= 0 && HGrSystem) + sprite(canvas, *HGrSystem, xm - 7, ym + 8 + lADamage - 7, 14, 17, 51, 153); + labelText = QString::number(ClientTools::myUn[unitIx].Health - forecastData.EndHealthAtt); + textSize = canvas.textExtent(labelText); + if (textSize.height() / 2 > (lADamage - lAAvoidedDamage) / 2 + lAAvoidedDamage) { + if (forecastData.EndHealthAtt > 0) + risedTextOut(canvas, xm - (textSize.width() + 1) / 2, ym + 8 + lAAvoidedDamage, labelText); + } else { + risedTextOut(canvas, xm - (textSize.width() + 1) / 2, + ym + 8 + lAAvoidedDamage + (lADamage - lAAvoidedDamage - textSize.height()) / 2, labelText); + } + + unshareBitmap(Term::buffer); + Term::buffer.setSize(std::max(Term::buffer.width(), 66), std::max(Term::buffer.height(), 48)); + + isoMap->setOutput(&Term::buffer); + unshareBitmap(Term::buffer); + Term::buffer.canvas().begin(); + Term::buffer.canvas().copyRect(QRect(0, 0, 66, 48), canvas, QRect(xm + 8 + 4, ym - 8 - 12 - 48, 66, 48)); + Term::buffer.canvas().end(); + isoMap->paintUnit(1, 0, unitInfo, 0); + canvas.copyRect(QRect(xm + 8 + 4, ym - 8 - 12 - 48, 66, 48), Term::buffer.canvas(), QRect(0, 0, 66, 48)); + + unshareBitmap(Term::buffer); + Term::buffer.canvas().begin(); + Term::buffer.canvas().copyRect(QRect(0, 0, 66, 48), canvas, QRect(xm - 8 - 4 - 66, ym + 8 + 12, 66, 48)); + Term::buffer.canvas().end(); + MakeUnitInfo(ClientTools::me, ClientTools::myUn[unitIx], unitInfo); + unitInfo.Flags &= ~unFortified; + isoMap->paintUnit(1, 0, unitInfo, 0); + canvas.copyRect(QRect(xm - 8 - 4 - 66, ym + 8 + 12, 66, 48), Term::buffer.canvas(), QRect(0, 0, 66, 48)); +} + +void BattleDlg::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + Dpi::Canvas canvas; + canvas.bind(&painter); + + canvas.brush().setStyle(Qt::SolidPattern); + canvas.brush().setColor(Qt::black); + canvas.fillRect(QRect(0, 0, width(), height())); + canvas.brush().setStyle(Qt::NoBrush); + paintBackground(canvas, 3 + Border, 3 + Border, width() - (6 + 2 * Border), height() - (6 + 2 * Border), width(), + height()); + if (MainTexture) { + frame(canvas, Border + 1, Border + 1, width() - (2 + Border), height() - (2 + Border), + MainTexture->colorBevelLight, MainTexture->colorBevelShade); + frame(canvas, 2 + Border, 2 + Border, width() - (3 + Border), height() - (3 + Border), + MainTexture->colorBevelLight, MainTexture->colorBevelShade); + } + + int ym; + if (isSuicideQuery) { + canvas.font().assign(UniFont[static_cast(FontType::Caption)]); + QString s = phrase(QStringLiteral("TITLE_SUICIDE")); + risedTextOut(canvas, (width() - biColorTextWidth(canvas, s)) / 2, 7 + Border, s); + canvas.font().assign(UniFont[static_cast(FontType::Normal)]); + s = phrase(QStringLiteral("SUICIDE")); + const int p = s.indexOf(QLatin1Char('\\')); + if (p < 0) { + risedTextOut(canvas, (width() - biColorTextWidth(canvas, s)) / 2, 205, s); + } else { + const QString s1 = s.left(p); + risedTextOut(canvas, (width() - biColorTextWidth(canvas, s1)) / 2, 205 - MessageLineSpacing / 2, s1); + const QString s2 = s.mid(p + 1); + risedTextOut(canvas, (width() - biColorTextWidth(canvas, s2)) / 2, + 205 + (MessageLineSpacing - MessageLineSpacing / 2), s2); + } + ym = 110; + } else { + ym = height() / 2; + } + canvas.font().assign(UniFont[static_cast(FontType::Small)]); + paintBattleOutcome(canvas, width() / 2, ym, uix, toLoc, forecast); + + if (MainTexture) { + const auto children = findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QWidget *child : children) + if (child->isVisible()) + btnFrame(canvas, child->geometry(), *MainTexture); + } + + canvas.unbind(); +} + +void BattleDlg::mousePressEvent(QMouseEvent *) +{ + if (!isSuicideQuery) + close(); +} + +void BattleDlg::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { + setModalResult(Dpi::ModalResult::Ok); + close(); + } else if (!isSuicideQuery && event->key() != Qt::Key_Shift) { + close(); + // The original also forwards unhandled keys to MainScreen.FormKeyDown() + // once the key isn't Escape - skipped, MainScreen's keyboard dispatch + // isn't ported yet. + } else { + DrawDlg::keyPressEvent(event); + } +} + +} // namespace Battle +} // namespace Cevo diff --git a/game/localplayer/Battle.h b/game/localplayer/Battle.h new file mode 100644 index 0000000..2792e62 --- /dev/null +++ b/game/localplayer/Battle.h @@ -0,0 +1,74 @@ +// 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. + +#pragma once + +#include "Cevo/CevoDrawDlg.h" +#include "Cevo/Protocol.h" +#include "IsoEngine.h" + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class BattleDlg; +} +QT_END_NAMESPACE + +namespace Cevo { +namespace Battle { + +// TBattleDlg equivalent: shows the attack-outcome forecast bar chart (and, +// for a suicide attack, a yes/no confirmation) before a unit attacks. +class BattleDlg : public DrawDlg { + Q_OBJECT +public: + explicit BattleDlg(QWidget *parent = nullptr); + ~BattleDlg() override; + + int uix = 0; + int toLoc = 0; + TBattleForecastEx forecast{}; + bool isSuicideQuery = false; + +protected: + void paintEvent(QPaintEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + +private: + void paintBattleOutcome(Dpi::Canvas &canvas, int xm, int ym, int unitIx, int toLocation, + const TBattleForecastEx &forecastData); + + std::unique_ptr isoMap; + + Ui::BattleDlg *ui; +}; + +} // namespace Battle +} // namespace Cevo diff --git a/game/localplayer/Battle.ui b/game/localplayer/Battle.ui new file mode 100644 index 0000000..763dda2 --- /dev/null +++ b/game/localplayer/Battle.ui @@ -0,0 +1,71 @@ + + + + BattleDlg + + + + 0 + 0 + 300 + 288 + + + + C-evo + + + + + 46 + 251 + 100 + 25 + + + + + + + 154 + 251 + 100 + 25 + + + + + + + Cevo::ButtonA + QWidget +
Cevo/CevoButtonA.h
+
+
+ +
diff --git a/widget/include/Cevo/CevoScreenTools.h b/widget/include/Cevo/CevoScreenTools.h index a2ea94b..eb0d5f8 100644 --- a/widget/include/Cevo/CevoScreenTools.h +++ b/widget/include/Cevo/CevoScreenTools.h @@ -231,6 +231,9 @@ QString movementToString(int movement); // swatch at row 137+kind (DarkGradient), ending in a black L-shaped border. void lightGradient(Dpi::Canvas &canvas, int x, int y, int width, QColor color); void darkGradient(Dpi::Canvas &canvas, int x, int y, int width, int kind); +// Vertical counterparts of lightGradient()/darkGradient(). +void vLightGradient(Dpi::Canvas &canvas, int x, int y, int height, QColor color); +void vDarkGradient(Dpi::Canvas &canvas, int x, int y, int height, int kind); // ScreenTools.pas's NumberBar(): a labeled stat row (title + DLine divider // + right-aligned number), skipped entirely if val <= 0. void numberBar(Dpi::Canvas &canvas, int x, int y, const QString &cap, int val, const Texture &texture); diff --git a/widget/src/CevoScreenTools.cpp b/widget/src/CevoScreenTools.cpp index a3fc18b..c681eae 100644 --- a/widget/src/CevoScreenTools.cpp +++ b/widget/src/CevoScreenTools.cpp @@ -868,6 +868,19 @@ void darkGradient(Dpi::Canvas &canvas, int x, int y, int width, int kind) gradient(canvas, x, y, 0, 1, width, 0, color, brightness); } +void vLightGradient(Dpi::Canvas &canvas, int x, int y, int height, QColor color) +{ + static constexpr int brightness[16] = { 16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -24, -28, -32, -36, -40, -44 }; + gradient(canvas, x, y, 1, 0, 0, height, color, brightness); +} + +void vDarkGradient(Dpi::Canvas &canvas, int x, int y, int height, int kind) +{ + static constexpr int brightness[16] = { 16, 12, 8, 4, 0, -4, -8, -36, 0, -20, -24, -28, -32, -36, -40, -44 }; + const QColor color = HGrSystem ? HGrSystem->data.image().pixelColor(187, 137 + kind) : QColor(Qt::black); + gradient(canvas, x, y, 1, 0, 0, height, color, brightness); +} + void numberBar(Dpi::Canvas &canvas, int x, int y, const QString &cap, int val, const Texture &texture) { if (val <= 0)