added enhance

This commit is contained in:
Geoffray Levasseur committed 2026-11-03 17:37:37 +01:00
1 parent 65aeb03394
commit 8e7d483471
4 files changed
+601

No files matched your search

+363
View File
@@ -0,0 +1,363 @@
// 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 "Enhance.h"
#include "ui_Enhance.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "ClientTools.h"
#include "Help.h"
#include "KeyBindings.h"
#include "Term.h"
#include <QKeyEvent>
#include <QPainter>
#include <array>
#include <cstring>
#include <set>
namespace Cevo {
namespace Enhance {
namespace {
// TODO: needs Phrases[] (StringTables.pas not ported) - plain English
// literals until localization is ported.
QString phrase(const QString &key)
{
static const QHash<QString, QString> table = {
{QStringLiteral("TITLE_ENHANCE"), QStringLiteral("Enhance terrain")},
{QStringLiteral("BTN_SELECT"), QStringLiteral("Select terrain")},
{QStringLiteral("TWOTERRAINS"), QStringLiteral("%1/%2")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? *it : key;
}
QString terrainName(int terrType)
{
return QStringLiteral("Terrain #%1").arg(terrType);
}
QString enhanceLabel(const QString &terrainLabel, const QString &workTime)
{
return QStringLiteral("%1 (%2)").arg(terrainLabel, workTime);
}
} // namespace
EnhanceDlg::EnhanceDlg(QWidget *parent) : FramedDlg(parent), ui(new Ui::EnhanceDlg)
{
ui->setupUi(this);
noMap = std::make_unique<IsoEngine::IsoMap>();
captionRight = ui->closeBtn->x();
captionLeft = ui->toggleBtn->x() + ui->toggleBtn->width();
initButtons();
setWindowTitle(phrase(QStringLiteral("TITLE_ENHANCE")));
ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_SELECT")));
popup = new QMenu(this);
for (int terrType = fGrass; terrType <= fMountains; ++terrType) {
if (terrType == fJungle)
continue;
QString caption;
if (terrType == fGrass)
caption = phrase(QStringLiteral("TWOTERRAINS")).arg(terrainName(fGrass)).arg(terrainName(fGrass + 12));
else if (terrType == fForest)
caption = phrase(QStringLiteral("TWOTERRAINS")).arg(terrainName(fForest)).arg(terrainName(fJungle));
else
caption = terrainName(terrType);
QAction *action = popup->addAction(caption);
action->setCheckable(true);
action->setData(terrType);
connect(action, &QAction::triggered, this, [this, terrType] {
page = terrType;
smartUpdateContent();
});
}
ui->closeBtn->onClick = [this] { close(); };
ui->toggleBtn->onClick = [this] {
for (QAction *action : popup->actions())
action->setChecked(action->data().toInt() == page);
popup->popup(mapToGlobal(QPoint(ui->toggleBtn->x(), ui->toggleBtn->y() + ui->toggleBtn->height())));
};
auto jobClickHandler = [this](Cevo::ButtonC *button) {
button->onClick = [this, button] { jobClicked(button->property("jobTag").toInt()); };
};
ui->job1->setProperty("jobTag", jRoad);
ui->job2->setProperty("jobTag", jRR);
ui->job4->setProperty("jobTag", jIrr);
ui->job5->setProperty("jobTag", jFarm);
ui->job7->setProperty("jobTag", jMine);
ui->job3->setProperty("jobTag", jClear);
ui->job6->setProperty("jobTag", jAfforest);
ui->job9->setProperty("jobTag", jTrans);
jobClickHandler(ui->job1);
jobClickHandler(ui->job2);
jobClickHandler(ui->job4);
jobClickHandler(ui->job5);
jobClickHandler(ui->job7);
jobClickHandler(ui->job3);
jobClickHandler(ui->job6);
jobClickHandler(ui->job9);
onShow = [this] { offscreenPaint(); };
}
EnhanceDlg::~EnhanceDlg()
{
delete ui;
}
void EnhanceDlg::showNewContent(WindowMode newMode, int terrType)
{
page = (terrType < fGrass || terrType > fMountains) ? fGrass : terrType;
FramedDlg::showNewContent(newMode);
}
void EnhanceDlg::offscreenPaint()
{
FramedDlg::offscreenPaint();
if (!Term::myData || !HGrSystem || !HGrTerrain)
return;
Dpi::Canvas &canvas = offscreen.canvas();
canvas.begin();
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
fillOffscreen(0, 0, innerWidth, innerHeight);
int endStage = 0;
while (endStage < 5 && Term::myData->enhancementJobs[page][endStage] != jNone)
++endStage;
int x = innerWidth / 2 - noMap->xxt - (noMap->xxt + 3) * endStage;
int terrType = page;
int tileImp = 0;
std::set<int> done;
int cost = 0;
for (int stage = 0; stage <= endStage; ++stage) {
if (stage > 0) {
sprite(canvas, *HGrSystem, x - 10, 66, 14, 14, 80, 1);
const int currentJob = Term::myData->enhancementJobs[page][stage - 1];
switch (currentJob) {
case jRoad:
cost += Terrain[terrType].MoveCost * RoadWork;
tileImp |= fRoad;
break;
case jRR:
cost += Terrain[terrType].MoveCost * RRWork;
tileImp |= fRR;
break;
case jIrr:
cost += Terrain[terrType].IrrClearWork;
tileImp = (tileImp & ~fTerImp) | tiIrrigation;
break;
case jFarm:
cost += Terrain[terrType].IrrClearWork * FarmWork;
tileImp = (tileImp & ~fTerImp) | tiFarm;
break;
case jMine:
cost += Terrain[terrType].MineAfforestWork;
tileImp = (tileImp & ~fTerImp) | tiMine;
break;
case jClear:
cost += Terrain[terrType].IrrClearWork;
terrType = Terrain[terrType].ClearTerrain;
break;
case jAfforest:
cost += Terrain[terrType].MineAfforestWork;
terrType = Terrain[terrType].AfforestTerrain;
break;
case jTrans:
cost += Terrain[terrType].TransWork;
terrType = Terrain[terrType].TransTerrain;
break;
default:
break;
}
done.insert(currentJob);
}
const int xxt = noMap->xxt;
const int yyt = noMap->yyt;
if (terrType < fForest) {
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + terrType * (xxt * 2 + 1), 1 + yyt);
} else {
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 2 * (yyt * 3 + 1));
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 7 * (xxt * 2 + 1),
1 + yyt + 2 * (2 + terrType - fForest) * (yyt * 3 + 1));
}
if ((tileImp & fTerImp) == tiFarm)
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + (xxt * 2 + 1),
1 + yyt + 12 * (yyt * 3 + 1));
else if ((tileImp & fTerImp) == tiIrrigation)
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1, 1 + yyt + 12 * (yyt * 3 + 1));
if (tileImp & fRR) {
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 6 * (xxt * 2 + 1),
1 + yyt + 10 * (yyt * 3 + 1));
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 10 * (yyt * 3 + 1));
} else if (tileImp & fRoad) {
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 6 * (xxt * 2 + 1),
1 + yyt + 9 * (yyt * 3 + 1));
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 9 * (yyt * 3 + 1));
}
if ((tileImp & fTerImp) == tiMine)
sprite(canvas, *HGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 12 * (yyt * 3 + 1));
x += xxt * 2 + 6;
}
QString s;
for (QAction *action : popup->actions())
if (action->data().toInt() == page)
s = action->text();
if (cost > 0)
s = enhanceLabel(s, movementToString(cost));
loweredTextOut(canvas, -1, *MainTexture, (innerWidth - biColorTextWidth(canvas, s)) / 2, 12, s);
const int lastJob = endStage > 0 ? Term::myData->enhancementJobs[page][endStage - 1] : jNone;
ui->job1->setButtonIndex(done.count(jRoad) ? 3 : 2);
ui->job2->setButtonIndex(done.count(jRR) ? 3 : 2);
ui->job4->setButtonIndex(done.count(jIrr) ? 3 : 2);
ui->job5->setButtonIndex(done.count(jFarm) ? 3 : 2);
ui->job7->setButtonIndex(done.count(jMine) ? 3 : 2);
ui->job3->setButtonIndex(lastJob == jClear ? 3 : 2);
ui->job6->setButtonIndex(lastJob == jAfforest ? 3 : 2);
ui->job9->setButtonIndex(lastJob == jTrans ? 3 : 2);
const bool typeChanged = lastJob == jClear || lastJob == jAfforest || lastJob == jTrans;
ui->job1->setVisible(done.count(jRoad) || !typeChanged);
ui->job2->setVisible(done.count(jRR) || !typeChanged);
ui->job4->setVisible(done.count(jIrr) || (!typeChanged && Terrain[terrType].IrrEff > 0));
ui->job5->setVisible(done.count(jFarm) || (!typeChanged && Terrain[terrType].IrrEff > 0));
ui->job7->setVisible(done.count(jMine) || (!typeChanged && Terrain[terrType].MineEff > 0));
ui->job3->setVisible((!typeChanged && Terrain[terrType].ClearTerrain >= 0 &&
(terrType != fDesert || ClientTools::myRO->Wonder[woGardens].EffectiveOwner == ClientTools::me)) ||
lastJob == jClear);
ui->job6->setVisible((!typeChanged && Terrain[terrType].AfforestTerrain >= 0) || lastJob == jAfforest);
ui->job9->setVisible((!typeChanged && Terrain[terrType].TransTerrain >= 0) || lastJob == jTrans);
canvas.end();
markUsedOffscreen(innerWidth, innerHeight);
}
void EnhanceDlg::jobClicked(int newJob)
{
if (!Term::myData)
return;
auto &jobs = Term::myData->enhancementJobs[page];
auto removeJob = [&](int j) {
int stage = 0;
while (stage < 5 && jobs[stage] != jNone) {
if (jobs[stage] == j || (j == jRoad && jobs[stage] == jRR) || (j == jIrr && jobs[stage] == jFarm)) {
if (stage < 4)
std::memmove(&jobs[stage], &jobs[stage + 1], 4 - stage);
jobs[4] = jNone;
} else {
++stage;
}
}
};
std::set<int> done;
int stage = 0;
while (stage < 5 && jobs[stage] != jNone) {
done.insert(jobs[stage]);
++stage;
}
if (done.count(newJob)) {
removeJob(newJob);
} else {
if (newJob == jMine || newJob == jAfforest)
removeJob(jIrr);
if (newJob == jIrr || newJob == jFarm || newJob == jTrans)
removeJob(jMine);
if (newJob == jRR && !done.count(jRoad)) {
jobs[stage] = jRoad;
++stage;
}
if (newJob == jFarm && !done.count(jIrr)) {
jobs[stage] = jIrr;
++stage;
}
jobs[stage] = static_cast<std::uint8_t>(newJob);
}
smartUpdateContent();
}
void EnhanceDlg::paintEvent(QPaintEvent *event)
{
FramedDlg::paintEvent(event);
if (!MainTexture)
return;
QPainter painter(this);
Dpi::Canvas canvas;
canvas.bind(&painter);
btnFrame(canvas, QRect(ui->job1->x(), ui->job1->y(), ui->job7->x() + ui->job7->width() - ui->job1->x(),
ui->job1->height()),
*MainTexture);
btnFrame(canvas, QRect(ui->job3->x(), ui->job3->y(), ui->job9->x() + ui->job9->width() - ui->job3->x(),
ui->job3->height()),
*MainTexture);
if (HGrSystem) {
const auto children = findChildren<Cevo::ButtonC *>();
for (Cevo::ButtonC *child : children) {
const int tag = child->property("jobTag").toInt();
canvas.copyRect(QRect(child->x() + 2, child->y() - 11, 8, 8), HGrSystem->data.canvas(),
QRect(121 + tag % 7 * 9, 1 + tag / 7 * 9, 8, 8));
}
}
canvas.unbind();
}
void EnhanceDlg::resizeEvent(QResizeEvent *event)
{
FramedDlg::resizeEvent(event);
ui->closeBtn->move(width() - 38, ui->closeBtn->y());
}
void EnhanceDlg::keyPressEvent(QKeyEvent *event)
{
const QKeySequence pressed(event->key() | event->modifiers());
if (KeyBindings::bHelp && pressed.matches(KeyBindings::bHelp->shortCut) == QKeySequence::ExactMatch)
// TODO: needs a live HelpDlg instance's textIndex() (Term.pas's
// MainScreen.HelpDlg isn't ported yet) - placeholder text index.
Help::showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Text, 0);
else
FramedDlg::keyPressEvent(event);
}
} // namespace Enhance
} // namespace Cevo
+76
View File
@@ -0,0 +1,76 @@
// 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/CevoBaseWin.h"
#include "Cevo/Protocol.h"
#include "IsoEngine.h"
#include <QMenu>
#include <memory>
QT_BEGIN_NAMESPACE
namespace Ui {
class EnhanceDlg;
}
QT_END_NAMESPACE
namespace Cevo {
namespace Enhance {
// TEnhanceDlg equivalent: previews the result of a chain of terrain-
// improvement jobs (road/railroad/irrigation/farmland/mine/clear/afforest/
// transform) queued for the currently selected settler/engineer unit.
class EnhanceDlg : public FramedDlg {
Q_OBJECT
public:
explicit EnhanceDlg(QWidget *parent = nullptr);
~EnhanceDlg() override;
void offscreenPaint() override;
void showNewContent(WindowMode newMode, int terrType = -1);
protected:
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
private:
void jobClicked(int newJob);
std::unique_ptr<IsoEngine::IsoMap> noMap;
QMenu *popup = nullptr;
int page = fGrass;
Ui::EnhanceDlg *ui;
};
} // namespace Enhance
} // namespace Cevo
+156
View File
@@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<ui version="4.0">
<class>EnhanceDlg</class>
<widget class="QWidget" name="EnhanceDlg">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>189</height>
</rect>
</property>
<property name="windowTitle">
<string>C-evo</string>
</property>
<widget class="Cevo::ButtonB" name="toggleBtn">
<property name="geometry">
<rect>
<x>13</x>
<y>6</y>
<width>25</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonB" name="closeBtn">
<property name="geometry">
<rect>
<x>602</x>
<y>6</y>
<width>25</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job1">
<property name="geometry">
<rect>
<x>267</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job2">
<property name="geometry">
<rect>
<x>279</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job4">
<property name="geometry">
<rect>
<x>291</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job5">
<property name="geometry">
<rect>
<x>303</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job7">
<property name="geometry">
<rect>
<x>315</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job3">
<property name="geometry">
<rect>
<x>339</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job6">
<property name="geometry">
<rect>
<x>351</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonC" name="job9">
<property name="geometry">
<rect>
<x>363</x>
<y>144</y>
<width>12</width>
<height>12</height>
</rect>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>Cevo::ButtonB</class>
<extends>QWidget</extends>
<header>Cevo/CevoButtonB.h</header>
</customwidget>
<customwidget>
<class>Cevo::ButtonC</class>
<extends>QWidget</extends>
<header>Cevo/CevoButtonC.h</header>
</customwidget>
</customwidgets>
<resources/>
</ui>