// 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); if (!painter.isActive()) return; 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