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

1415 lines
63 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 "Sound.h"
#include "CityScreen.h"
#include "ui_CityScreen.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "ClientTools.h"
#include "Global.h"
#include "Help.h"
#include "Term.h"
#include "Tribes.h"
#include <QCloseEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
#include <algorithm>
#include <cmath>
namespace Cevo {
namespace CityScreen {
namespace {
constexpr int wBar = 106;
constexpr int xDiv = 400;
constexpr int xService = 296;
constexpr int xmArea = 197;
constexpr int ymArea = 170;
constexpr int xView = 326;
constexpr int yView = 275;
constexpr int dxBar = wBar + 12;
constexpr int dyBar = 39;
constexpr int xHapp = 404;
constexpr int yHapp = 9;
constexpr int xFood = 404;
constexpr int yFood = yHapp + 3 * dyBar + 6;
constexpr int xProd = 404;
constexpr int yProd = yFood + 3 * dyBar + 6;
constexpr int xTrade = 404;
constexpr int yTrade = yProd + 2 * dyBar + 22;
constexpr int xPoll = xmArea - 186;
constexpr int yPoll = ymArea + 64;
constexpr int xmOpt = 40;
constexpr int ymOpt = ymArea + 96 + 34;
constexpr int xSmallMap = 271;
constexpr int ySmallMap = 339;
constexpr int wSmallMap = 98;
constexpr int hSmallMap = 74;
constexpr int xSupport = xSmallMap;
constexpr int ySupport = ySmallMap + hSmallMap + 2;
constexpr int wSupport = 64;
constexpr int hSupport = 18;
constexpr int xZoomMap = 34;
constexpr int yZoomMap = 338;
constexpr int wZoomMap = 228;
constexpr int hZoomMap = 124;
constexpr int wZoomEnvironment = 68;
constexpr int CaptionX = 42;
constexpr int CaptionY = 7;
// ImpPosition[28..nImp-1]: position of each improvement's status dot in the
// small city-map's improvement grid (-1: not shown there).
constexpr std::array<int, nImp - 28> ImpPosition = {
-1, 21, 6, 1, 7, 14, 8, 18, 10, 11, 5, 13, 29, 2, 24, 25, 28, 27,
27, 27, 26, 8, 10, 3, 17, 15, 19, 23, 22, 20, 8, -1, 4, 16, 21, -1,
-1, 9, -1, -1, -1, -1,
};
QString impName(int iix)
{
return phrases.lookup(QStringLiteral("IMPROVEMENTS"), iix);
}
QString modelName(int cOwner, int mix)
{
return Tribes::tribe[static_cast<std::size_t>(cOwner)]->modelNameAt(mix);
}
QString governmentName(int gov)
{
return phrases.lookup(QStringLiteral("GOVERNMENT"), gov);
}
void play(const QString &item)
{
Sound::play(item);
}
} // namespace
CityDlg::CityDlg(QWidget *parent) : BufferedDrawDlg(parent), ui(new Ui::CityDlg)
{
ui->setupUi(this);
initButtons();
redTex.image(); // ensure Texture's lazily-constructed image exists
barTex.image();
noMap = std::make_unique<IsoEngine::IsoMap>();
areaMap = std::make_unique<IsoEngine::IsoMap>();
areaMap->setOutput(&offscreen);
areaMap->setPaintBounds(xmArea - 192, ymArea - 96 - 32, xmArea + 192, ymArea + 96);
reset();
prodHint = false;
restoreUnFocus = -1;
openSoundEvent = -1;
agePrepared = -2;
optimizeCixTileChange = -1;
ui->closeBtn->setCaption(phrases.lookup(QStringLiteral("BTN_OK")));
ui->buyBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_BUY")));
ui->supportArea->setToolTip(phrases.lookup(QStringLiteral("SUPUNITS")));
back.setTransparent(false);
back.setSize(width(), height());
back.canvas().begin();
back.canvas().fillRect(QRect(0, 0, back.width(), back.height()));
back.canvas().end();
tmpl.loadFromFile(getGraphicsDir() + QStringLiteral("/City") + PngExt);
cityMapTemplate.loadFromFile(getGraphicsDir() + QStringLiteral("/BigCityMap") + PngExt);
smallCityMapTemplate.loadFromFile(getGraphicsDir() + QStringLiteral("/SmallCityMap") + PngExt);
smallCityMap.setSize(wSmallMap, hSmallMap);
smallCityMap.canvas().begin();
smallCityMap.canvas().fillRect(QRect(0, 0, smallCityMap.width(), smallCityMap.height()));
smallCityMap.canvas().end();
zoomCityMap.setSize(wZoomMap, hZoomMap);
zoomCityMap.canvas().begin();
zoomCityMap.canvas().fillRect(QRect(0, 0, zoomCityMap.width(), zoomCityMap.height()));
zoomCityMap.canvas().end();
ui->closeBtn->onClick = [this] { close(); };
ui->buyBtn->onClick = [this] { buyClick(); };
ui->nextCityBtn->onClick = [this] { changeCity(+1); };
ui->prevCityBtn->onClick = [this] { changeCity(-1); };
ui->pageUpBtn->onClick = [this] {
if (page > 0) {
--page;
smartUpdateContent();
}
};
ui->pageDownBtn->onClick = [this] {
if (page < pageCount - 1) {
++page;
smartUpdateContent();
}
};
timer1 = new QTimer(this);
timer1->setInterval(50);
connect(timer1, &QTimer::timeout, this, [this] {
if (!prodHint)
return;
blinkTime = (blinkTime + 1) % 12;
QPainter painter(this);
if (!painter.isActive())
return;
Dpi::Canvas canvas;
canvas.bind(&painter);
if (blinkTime == 0) {
canvas.copyRect(QRect(xView + 5, yView + 1, 64, 2), back.canvas(), QRect(xView + 5, yView + 1, 64, 2));
canvas.copyRect(QRect(xView + 5, yView + 3, 2, 42), back.canvas(), QRect(xView + 5, yView + 3, 2, 42));
canvas.copyRect(QRect(xView + 5 + 62, yView + 3, 2, 42), back.canvas(),
QRect(xView + 5 + 62, yView + 3, 2, 42));
frame(canvas, xView + 9 - 1, yView + 5 - 1, xView + 9 + xSizeBig, yView + 5 + ySizeBig,
colorFromDelphi(0xB0B0B0), colorFromDelphi(0xFFFFFF));
rframe(canvas, xView + 9 - 2, yView + 5 - 2, xView + 9 + xSizeBig + 1, yView + 5 + ySizeBig + 1,
colorFromDelphi(0xFFFFFF), colorFromDelphi(0xB0B0B0));
canvas.brush().setStyle(Qt::SolidPattern);
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(xView + 9, yView + 5, 72 - 8 - 9 + 1, 40));
canvas.brush().setStyle(Qt::NoBrush);
} else if (blinkTime == 6) {
if (allowChange && (c.Status & 7) != 0) {
if (bigImp)
frameImage(canvas, *bigImp, xView + 9, yView + 5, xSizeBig, ySizeBig,
((c.Status & 7) - 1 + 3) * xSizeBig, 0, true);
} else if ((c.Project0 & cpImp) == 0) {
if (bigImp)
canvas.copyRect(QRect(xView + 9, yView + 5, xSizeBig, ySizeBig), bigImp->canvas(),
QRect(0, 0, xSizeBig, ySizeBig));
const Tribes::ModelPicture &mp =
Tribes::tribe[static_cast<std::size_t>(cOwner)]->modelPictureAt(c.Project0 & cpIndex);
if (mp.hGr)
sprite(canvas, *mp.hGr, xView + 5, yView + 1, 64, 44, mp.pix % 10 * 65 + 1,
mp.pix / 10 * 49 + 1);
} else {
Term::impImage(canvas, xView + 9, yView + 5, c.Project0 & cpIndex, cGov, true);
}
}
canvas.unbind();
});
onShow = [this] {
blinkTime = 5;
if (cix >= 0) { // own city
c = ClientTools::myCity[cix];
cOwner = ClientTools::me;
cGov = ClientTools::myRO->Government;
prodHint = cGov != gAnarchy &&
(happened & (chProduction | chFounded | chCaptured | Term::chAllImpsMade)) != 0;
ClientTools::server(sGetCityAreaInfo, ClientTools::me, cix, &cityAreaInfo);
ui->nextCityBtn->setVisible(windowMode() == WindowMode::Persistent);
ui->prevCityBtn->setVisible(windowMode() == WindowMode::Persistent);
} else { // enemy city
smallMapMode = SmallMapMode::Improvements;
TGetCityData getCityData{};
ClientTools::server(sGetCity, ClientTools::me, cLoc, &getCityData);
c = getCityData.C;
cOwner = getCityData.Owner;
cGov = ClientTools::myRO->EnemyReport[cOwner]->Government;
happened = static_cast<std::uint32_t>(c.Flags) & 0x7FFFFFFFu;
prodHint = false;
ClientTools::server(sGetEnemyCityAreaInfo, ClientTools::me, cLoc, &cityAreaInfo);
if ((c.Project & cpImp) == 0) {
emix = ClientTools::myRO->nEnemyModel - 1;
while (emix > 0 &&
((*ClientTools::myRO->EnemyModel)[emix].Owner != cOwner ||
(*ClientTools::myRO->EnemyModel)[emix].mix != (c.Project & cpIndex)))
--emix;
if (!Tribes::tribe[static_cast<std::size_t>(cOwner)]->modelPictureAt(c.Project & cpIndex).hGr)
IsoEngine::invokeInitEnemyModel(emix);
}
ui->nextCityBtn->setVisible(false);
ui->prevCityBtn->setVisible(false);
}
page = 0;
if (c.Size < 5)
sizeClass = 0;
else if (c.Size < 9)
sizeClass = 1;
else if (c.Size < 13)
sizeClass = 2;
else
sizeClass = 3;
isPort = false;
for (int dx = -2; dx <= 2 && !isPort; ++dx)
for (int dy = -2; dy <= 2 && !isPort; ++dy)
if (std::abs(dx) + std::abs(dy) == 2) {
const int loc1 = ClientTools::dLoc(cLoc, dx, dy);
if (loc1 >= 0 && loc1 < ClientTools::g.lx * ClientTools::g.ly &&
(ClientTools::myMap[loc1] & fTerrain) < fGrass)
isPort = true;
}
if (windowMode() == WindowMode::Modal)
centerToScreen();
setWindowTitle(Tribes::cityName(c.ID));
initSmallCityMap();
initZoomCityMap();
openSoundEvent = -1;
offscreenPaint();
timer1->start();
};
}
CityDlg::~CityDlg()
{
delete ui;
}
void CityDlg::reset()
{
smallMapMode = SmallMapMode::Improvements;
zoomArea = 1;
}
void CityDlg::checkAge()
{
if (!MainTexture)
return;
if (MainTexture->age() != agePrepared) {
agePrepared = MainTexture->age();
unshareBitmap(back);
back.canvas().begin();
bitBltBitmap(back, 0, 0, width(), height(), MainTexture->image(), 0, 0);
back.canvas().end();
imageOpB(back, tmpl, 0, 0, 0, 0, width(), height());
}
}
void CityDlg::initSmallCityMap()
{
if (cix >= 0)
c = ClientTools::myCity[cix];
int cli1 = cliPlains;
switch (ClientTools::myMap[cLoc] & fTerrain) {
case fPrairie:
cli1 = cliPrairie;
break;
case fHills:
cli1 = cliHills;
break;
case fTundra:
cli1 = cliTundra;
break;
default:
cli1 = cliPlains;
break;
}
if (!Colors)
return;
QImage &palette = Colors->image();
const QColor color0 = palette.pixelColor(clkAge0 + Term::age, cliRoad);
QColor color1 = palette.pixelColor(clkCity, cli1);
QColor color2 = palette.pixelColor(clkAge0 + Term::age, cliHouse);
smallCityMap.canvas().begin();
smallCityMap.canvas().fillRect(QRect(0, 0, smallCityMap.width(), smallCityMap.height()));
smallCityMap.canvas().end();
bitBltBitmap(smallCityMap, 0, 0, 83, hSmallMap, smallCityMapTemplate, 83 * sizeClass, 0);
if (isPort) {
bitBltBitmap(smallCityMap, 83, 0, 15, hSmallMap, smallCityMapTemplate, 332 + 15, 0);
imageOpCCC(smallCityMap, 0, 0, 83, hSmallMap, colorToDelphi(color0), colorToDelphi(color1),
colorToDelphi(color2));
color2 = palette.pixelColor(clkCity, cliWater);
imageOpCCC(smallCityMap, 83, 0, 15, hSmallMap, colorToDelphi(color0), colorToDelphi(color1),
colorToDelphi(color2));
} else {
bitBltBitmap(smallCityMap, 83, 0, 15, hSmallMap, smallCityMapTemplate, 332, 0);
imageOpCCC(smallCityMap, 0, 0, wSmallMap, hSmallMap, colorToDelphi(color0), colorToDelphi(color1),
colorToDelphi(color2));
}
Dpi::Canvas &canvas = smallCityMap.canvas();
canvas.begin();
canvas.brush().setStyle(Qt::SolidPattern);
canvas.brush().setColor(palette.pixelColor(clkAge0 + Term::age, cliImp));
int i = 0;
for (i = 0; i <= 29; ++i) {
bool drawn = false;
for (int iix = nWonder; iix < nImp && !drawn; ++iix)
if (ImpPosition[static_cast<std::size_t>(iix - 28)] == i && c.Built[iix] > 0) {
canvas.fillRect(QRect(5 + 16 * (i % 3) + 48 * (i / 18), 3 + 12 * (i % 18 / 3),
8, 8));
drawn = true;
}
}
i = 30;
for (int iix = 0; iix <= nImp && i < 36; ++iix)
if (c.Built[iix] > 0 &&
(iix < nWonder || ImpPosition[static_cast<std::size_t>(iix - 28)] < 0)) {
canvas.fillRect(QRect(5 + 16 * (i % 3) + 48 * (i / 18), 3 + 12 * (i % 18 / 3), 8, 8));
++i;
}
if ((c.Project & cpImp) != 0) {
const int iix = c.Project & cpIndex;
if (iix != imTrGoods) {
if (iix >= nWonder && ImpPosition[static_cast<std::size_t>(iix - 28)] >= 0)
i = ImpPosition[static_cast<std::size_t>(iix - 28)];
if (i < 36) {
canvas.brush().setColor(palette.pixelColor(clkAge0 + Term::age, cliImpProject));
canvas.fillRect(QRect(5 + 16 * (i % 3) + 48 * (i / 18), 3 + 12 * (i % 18 / 3), 8, 8));
}
}
}
canvas.brush().setStyle(Qt::NoBrush);
canvas.end();
}
void CityDlg::initZoomCityMap()
{
unshareBitmap(zoomCityMap);
zoomCityMap.canvas().begin();
bitBltBitmap(zoomCityMap, 0, 0, wZoomMap, hZoomMap, back, xZoomMap, yZoomMap);
if (smallMapMode == SmallMapMode::Improvements) {
if (zoomArea < 3) {
imageOpB(zoomCityMap, cityMapTemplate, 0, 0, 376 * sizeClass, 112 * zoomArea, wZoomMap, hZoomMap);
} else {
imageOpB(zoomCityMap, cityMapTemplate, 0, 0, 376 * sizeClass + 216, 112 * (zoomArea - 3),
wZoomMap - wZoomEnvironment, hZoomMap);
imageOpB(zoomCityMap, cityMapTemplate, wZoomMap - wZoomEnvironment, 0,
1504 + wZoomEnvironment * (isPort ? 1 : 0), 112 * (zoomArea - 3), wZoomEnvironment, hZoomMap);
}
}
zoomCityMap.canvas().end();
}
void CityDlg::fillBar(int x, int y, int pos, int growth, int max, int kind, bool indicateComplete)
{
barTex.assign(*MainTexture);
if (kind == 3 && HGrSystem) {
const QColor c0 = HGrSystem->data.image().pixelColor(104, 36);
barTex.colorBevelLight = c0;
barTex.colorBevelShade = c0;
}
paintRelativeProgressBar(offscreen.canvas(), kind, x - 3, y, wBar - 4, pos, growth, max, indicateComplete, barTex);
}
void CityDlg::paintResources(int x, int y, int loc, bool add4Happy)
{
TTileInfo tileInfo{};
if (ClientTools::server(sGetCityTileInfo, ClientTools::me, loc, &tileInfo) != eOK) {
Q_ASSERT(cix < 0);
return;
}
int total = tileInfo.Food + tileInfo.Prod + tileInfo.Trade;
const bool rare = (ClientTools::myMap[loc] & 0x06000000u) != 0;
if (rare)
++total;
if (add4Happy)
total += 4;
int d = 1;
if (total > 1)
d = (areaMap->xxt - 11) / (total - 1);
if (d < 1)
d = 1;
if (d > 4)
d = 4;
for (int i = 0; i < total; ++i) {
int yGr = 115;
int xGr;
if (add4Happy && i >= total - 4) {
xGr = 132;
yGr = 126;
} else if (rare && i == total - 1) {
xGr = 66 + 110;
} else if (i >= tileInfo.Food + tileInfo.Prod) {
xGr = 66 + 44;
} else if (i >= tileInfo.Prod) {
xGr = 66;
} else {
xGr = 66 + 22;
}
if (HGrSystem)
sprite(offscreen.canvas(), *HGrSystem, x + areaMap->xxt - 5 + d * (2 * i + 1 - total), y + areaMap->yyt - 5,
10, 10, xGr, yGr);
}
}
void CityDlg::offscreenPaint()
{
BufferedDrawDlg::offscreenPaint();
if (!MainTexture || !bigImp || !HGrSystem || !HGrSystem2)
return;
if (cix >= 0)
c = ClientTools::myCity[cix];
report.HypoTiles = -1;
report.HypoTaxRate = -1;
report.HypoLuxuryRate = -1;
if (cix >= 0)
ClientTools::server(sGetCityReportNew, ClientTools::me, cix, &report); // own city
else
ClientTools::server(sGetEnemyCityReportNew, ClientTools::me, cLoc, &report); // enemy city
int trueFood = c.Food;
int trueProd = c.Prod;
int truePoll = c.Pollution;
if (Term::supervising || cix < 0) { // normalize city from after-turn state
trueFood -= report.FoodSurplus;
if (trueFood < 0)
trueFood = 0;
trueProd -= report.Production;
if (trueProd < 0)
trueProd = 0;
truePoll -= report.AddPollution;
if (truePoll < 0)
truePoll = 0;
}
const bool isCityAlive = cGov != gAnarchy && (c.Flags & chCaptured) == 0;
if (!isCityAlive)
report.Working = c.Size;
redTex.assign(*MainTexture);
redTex.colorBevelLight = colorFromDelphi(0x0000FF);
redTex.colorBevelShade = colorFromDelphi(0x000000);
redTex.colorTextLight = colorFromDelphi(0x000000);
redTex.colorTextShade = colorFromDelphi(0x0000FF);
Dpi::Canvas &canvas = offscreen.canvas();
canvas.begin();
canvas.copyRect(QRect(0, 0, 640, 480), back.canvas(), QRect(0, 0, 640, 480));
// Show city size and name
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Caption)]);
QString s = windowTitle();
const int ellipsisWidth = canvas.textWidth(QStringLiteral("..."));
if (s.length() > 1 && canvas.textWidth(s) > ui->prevCityBtn->x() - CaptionX) {
while (s.length() > 1 && canvas.textWidth(s) > ui->prevCityBtn->x() - CaptionX - ellipsisWidth)
s.chop(1);
s += QStringLiteral("...");
}
risedTextOut(canvas, CaptionX, CaptionY, s);
{ // city size
canvas.brush().setStyle(Qt::SolidPattern);
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(8 + 1, 7 + 1, 28, 25));
canvas.brush().setColor(Qt::white);
canvas.fillRect(QRect(8, 7, 28, 25));
canvas.brush().setStyle(Qt::NoBrush);
canvas.font().setColor(Qt::black);
const QString sizeText = QString::number(c.Size);
canvas.textOut(8 + 14 - canvas.textWidth(sizeText) / 2, 7, sizeText);
}
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
if (!isCityAlive) {
makeRed(offscreen, 18, 280, 298, 40);
s = cGov == gAnarchy ? governmentName(gAnarchy) : phrases.lookup(QStringLiteral("CITYEVENTS"), 14);
risedTextOut(canvas, 167 - biColorTextWidth(canvas, s) / 2, ymOpt - 9, s);
} else if (allowChange) {
const int optiType = c.Status >> 4 & 0x0F;
sprite(canvas, *HGrSystem2, xmOpt - 32, ymOpt - 32, 64, 64, 1 + optiType % 3 * 64, 217 + optiType / 3 * 64);
int messageCount = 0;
for (int i = 0; i <= 31; ++i)
if ((happened & ~static_cast<std::uint32_t>(chCaptured)) & (1u << i))
++messageCount;
if (messageCount > 3)
messageCount = 3;
if (messageCount > 0) {
makeBlue(offscreen, 74, 280, 242, 40);
int line = 0;
auto checkMessage = [&](int flag) {
if ((happened & static_cast<std::uint32_t>(flag)) == 0 || line >= messageCount)
return;
int i = 0;
std::uint32_t test = 1;
while (test < static_cast<std::uint32_t>(flag)) {
++i;
test += test;
}
if (allowChange && openSoundEvent == -1) {
const QString soundItem = Term::CityEventSoundItem[static_cast<std::size_t>(i)];
if (flag == chProduction || !soundItem.isEmpty())
openSoundEvent = i;
}
const QString text = Term::cityEventName(i);
risedTextOut(canvas, xmOpt + 40, ymOpt - 1 - 8 * messageCount + 16 * line, text);
++line;
};
for (int i = 0; i < Term::nCityEventPriority && line < messageCount; ++i)
checkMessage(Term::CityEventPriority[static_cast<std::size_t>(i)]);
} else {
s = phrases.lookup(QStringLiteral("CITYMANAGETYPE"), optiType);
const int j = s.indexOf(QLatin1Char('\\'));
if (j < 0) {
loweredTextOut(canvas, -1, *MainTexture, xmOpt + 40, ymOpt - 9, s);
} else {
loweredTextOut(canvas, -1, *MainTexture, xmOpt + 40, ymOpt - 17, s.left(j));
loweredTextOut(canvas, -1, *MainTexture, xmOpt + 40, ymOpt - 1, s.mid(j + 1));
}
}
}
{
IsoEngine::IsoMap &map = *areaMap;
const int rx = (192 + map.xxt * 2 - 1) / (map.xxt * 2);
const int ry = (96 + map.yyt * 2 - 1) / (map.yyt * 2);
map.paint(xmArea - map.xxt * 2 * rx, ymArea - map.yyt * 2 * ry - 3 * map.yyt,
ClientTools::dLoc(cLoc, -2 * rx + 1, -2 * ry - 1), 4 * rx - 1, 4 * ry + 1, cLoc, cOwner, false,
allowChange && isCityAlive && (c.Status & Term::csResourceWeightsMask) == 0);
canvas.copyRect(QRect(xmArea + 102, 42, 90, 33), back.canvas(), QRect(xmArea + 102, 42, 90, 33));
if (isCityAlive)
for (int dy = -3; dy <= 3; ++dy)
for (int dx = -3; dx <= 3; ++dx)
if (((dx + dy) & 1) == 0 && dx * dx * dy * dy < 81) {
const int loc1 = ClientTools::dLoc(cLoc, dx, dy);
const int av = cityAreaInfo.Available[(dy + 3) * 4 + ((dx + 3) >> 1)];
if ((av == faNotAvailable || av == faTreaty || av == faInvalid) &&
(loc1 < 0 || loc1 >= ClientTools::g.lx * ClientTools::g.ly ||
(ClientTools::myMap[loc1] & fCity) == 0))
sprite(canvas, *map.hGrTerrain, xmArea - map.xxt + map.xxt * dx, ymArea - map.yyt + map.yyt * dy,
map.xxt * 2, map.yyt * 2, 1 + 5 * (map.xxt * 2 + 1),
1 + map.yyt + 15 * (map.yyt * 3 + 1));
if ((1u << ((dy + 3) * 4 + ((dx + 3) >> 1)) & c.Tiles) != 0)
paintResources(xmArea - map.xxt + map.xxt * dx, ymArea - map.yyt + map.yyt * dy, loc1,
dx == 0 && dy == 0);
}
}
int d = 0;
if (report.Working > 1)
d = (xService - (xmArea - 192) - 8 - 32) / (report.Working - 1);
if (d > 28)
d = 28;
for (int i = report.Working - 1; i >= 0; --i) {
const int xGr = isCityAlive ? 29 : 141;
bitBltBitmap(offscreen, xmArea - 192 + 5 + i * d, ymArea - 96 - 29, 27, 30, HGrSystem->mask, xGr, 171,
RasterOp::And);
sprite(canvas, *HGrSystem, xmArea - 192 + 4 + i * d, ymArea - 96 - 30, 27, 30, xGr, 171);
}
d = 0;
if (c.Size - report.Working > 1)
d = (xmArea + 192 - xService - 32) / (c.Size - report.Working - 1);
if (d > 28)
d = 28;
for (int i = 0; i < c.Size - report.Working; ++i) {
const int xGr = 1 + 112;
bitBltBitmap(offscreen, xmArea + 192 - 27 + 1 - i * d, 29 + 1, 27, 30, HGrSystem->mask, xGr, 171,
RasterOp::And);
sprite(canvas, *HGrSystem, xmArea + 192 - 27 - i * d, 29, 27, 30, xGr, 171);
sprite(canvas, *HGrSystem, xmArea + 192 - 27 + 4 - i * d, 29 + 32, 10, 10, 121, 126);
sprite(canvas, *HGrSystem, xmArea + 192 - 27 + 13 - i * d, 29 + 32, 10, 10, 121, 126);
}
QString prName =
(c.Project & cpImp) == 0 ? modelName(cOwner, c.Project & cpIndex) : impName(c.Project & cpIndex);
int prCost = report.ProjectCost;
// happiness section
if (isCityAlive) {
if (cGov == gFundamentalism) {
countBar(offscreen, xHapp, yHapp + dyBar, wBar, 17, phrases.lookup(QStringLiteral("FAITH")),
report.CollectedControl, *MainTexture);
} else {
countBar(offscreen, xHapp, yHapp + dyBar, wBar, 17, phrases.lookup(QStringLiteral("HAPPINESS")), report.Morale,
*MainTexture);
countBar(offscreen, xHapp, yHapp + 2 * dyBar, wBar, 16, phrases.lookup(QStringLiteral("CONTROL")),
report.CollectedControl, *MainTexture);
}
countBar(offscreen, xHapp, yHapp, wBar, 8, phrases.lookup(QStringLiteral("LUX")), report.Luxury, *MainTexture);
countBar(offscreen, xHapp + dxBar, yHapp, wBar, 19, phrases.lookup(QStringLiteral("UNREST")), 2 * report.Deployed,
*MainTexture);
countBar(offscreen, xHapp + dxBar, yHapp + dyBar, wBar, 17, phrases.lookup(QStringLiteral("HAPPINESSDEMAND")),
c.Size, *MainTexture);
if (report.HappinessBalance >= 0) {
countBar(offscreen, xHapp + dxBar, yHapp + 2 * dyBar, wBar, 17, phrases.lookup(QStringLiteral("HAPPINESSPLUS")),
report.HappinessBalance, *MainTexture);
} else {
makeRed(offscreen, xHapp + dxBar - 6, yHapp + 2 * dyBar, wBar + 10, 38);
countBar(offscreen, xHapp + dxBar, yHapp + 2 * dyBar, wBar, 18, phrases.lookup(QStringLiteral("LACK")),
-report.HappinessBalance, redTex);
}
}
// food section
if (isCityAlive) {
countBar(offscreen, xFood, yFood + dyBar / 2, wBar, 0, phrases.lookup(QStringLiteral("FOOD")), report.CollectedFood,
*MainTexture);
countBar(offscreen, xFood + dxBar, yFood + dyBar, wBar, 0, phrases.lookup(QStringLiteral("DEMAND")), 2 * c.Size,
*MainTexture);
countBar(offscreen, xFood + dxBar, yFood, wBar, 0, phrases.lookup(QStringLiteral("SUPPORT")), report.FoodSupport,
*MainTexture);
if (report.FoodSurplus >= 0) {
if (cGov == gFuture || (c.Size >= NeedAqueductSize && report.FoodSurplus < 2))
countBar(offscreen, xFood + dxBar, yFood + 2 * dyBar, wBar, 6, phrases.lookup(QStringLiteral("PROFIT")),
report.FoodSurplus, *MainTexture);
else
countBar(offscreen, xFood + dxBar, yFood + 2 * dyBar, wBar, 0, phrases.lookup(QStringLiteral("SURPLUS")),
report.FoodSurplus, *MainTexture);
} else {
makeRed(offscreen, xFood + dxBar - 6, yFood + 2 * dyBar, wBar + 10, 38);
countBar(offscreen, xFood + dxBar, yFood + 2 * dyBar, wBar, 1, phrases.lookup(QStringLiteral("LACK")),
-report.FoodSurplus, redTex);
}
}
const bool canGrow = c.Size < MaxCitySize && cGov != gFuture && report.FoodSurplus > 0 &&
(c.Size < NeedAqueductSize || (c.Built[imAqueduct] == 1 && c.Size < NeedSewerSize) ||
c.Built[imSewer] == 1);
fillBar(xFood + 3, yFood + 102, trueFood, ClientTools::cutCityFoodSurplus(report.FoodSurplus, isCityAlive, cGov, c.Size),
report.Storage, 1, canGrow);
loweredTextOut(canvas, -1, *MainTexture, xFood + 3 - 5, yFood + 102 - 20,
QStringLiteral("%1/%2").arg(trueFood).arg(report.Storage));
loweredTextOut(canvas, -1, *MainTexture, xFood - 2, yFood + 66, phrases.lookup(QStringLiteral("STORAGE")));
// production section
if (isCityAlive) {
countBar(offscreen, xProd, yProd, wBar, 2, phrases.lookup(QStringLiteral("MATERIAL")), report.CollectedMaterial,
*MainTexture);
countBar(offscreen, xProd + dxBar, yProd, wBar, 2, phrases.lookup(QStringLiteral("SUPPORT")), report.MaterialSupport,
*MainTexture);
if (report.Production >= 0) {
if ((c.Project & (cpImp + cpIndex)) == cpImp + imTrGoods)
countBar(offscreen, xProd + dxBar, yProd + dyBar + 16, wBar, 6, phrases.lookup(QStringLiteral("PROFIT")),
report.Production, *MainTexture);
else
countBar(offscreen, xProd + dxBar, yProd + dyBar + 16, wBar, 2, phrases.lookup(QStringLiteral("PROD")),
report.Production, *MainTexture);
} else {
makeRed(offscreen, xProd + dxBar - 6, yProd + dyBar + 17, wBar + 10, 38);
countBar(offscreen, xProd + dxBar, yProd + dyBar + 16, wBar, 3, phrases.lookup(QStringLiteral("LACK")),
-report.Production, redTex);
}
}
if ((c.Project & (cpImp + cpIndex)) != cpImp + imTrGoods) {
int prod = report.Production;
if (prod < 0 || !isCityAlive)
prod = 0;
fillBar(xProd + 3, yProd + 16 + 63, trueProd, prod, prCost, 4, true);
loweredTextOut(canvas, -1, *MainTexture, xProd + 3 - 5, yProd + 16 + 43,
QStringLiteral("%1/%2").arg(trueProd).arg(prCost));
if (biColorTextWidth(canvas, prName) > wBar + dxBar) {
do {
prName.chop(1);
} while (biColorTextWidth(canvas, prName) > wBar + dxBar);
prName += QLatin1Char('.');
}
}
risedTextOut(canvas, xProd - 2, yProd + 36, prName);
// pollution section
if (isCityAlive && report.AddPollution > 0) {
fillBar(xPoll + 3, yPoll + 20, truePoll, report.AddPollution, MaxPollution, 3, true);
risedTextOut(canvas, xPoll + 3 - 5, yPoll + 20 - 20, phrases.lookup(QStringLiteral("POLL")));
}
// trade section
if (isCityAlive && report.CollectedTrade > 0) {
countBar(offscreen, xTrade, yTrade + dyBar / 2, wBar, 4, phrases.lookup(QStringLiteral("TRADE")),
report.CollectedTrade, *MainTexture);
countBar(offscreen, xTrade + dxBar, yTrade + 2 * dyBar, wBar, 5, phrases.lookup(QStringLiteral("CORR")),
report.Corruption, *MainTexture);
countBar(offscreen, xTrade + dxBar, yTrade, wBar, 6, phrases.lookup(QStringLiteral("TAX")), report.Tax, *MainTexture);
countBar(offscreen, xTrade + dxBar, yTrade + dyBar, wBar, 12, phrases.lookup(QStringLiteral("SCIENCE")),
report.Science, *MainTexture);
}
// small map
canvas.copyRect(QRect(xSmallMap, ySmallMap, wSmallMap, hSmallMap), smallCityMap.canvas(),
QRect(0, 0, wSmallMap, hSmallMap));
if (smallMapMode == SmallMapMode::Improvements)
frame(canvas, xSmallMap + 48 * (zoomArea / 3), ySmallMap + 24 * (zoomArea % 3),
xSmallMap + 48 * (zoomArea / 3) + 49, ySmallMap + 24 * (zoomArea % 3) + 25, MainTexture->colorMark,
MainTexture->colorMark);
frame(canvas, xSmallMap - 1, ySmallMap - 1, xSmallMap + wSmallMap, ySmallMap + hSmallMap,
colorFromDelphi(0xB0B0B0), colorFromDelphi(0xFFFFFF));
rframe(canvas, xSmallMap - 2, ySmallMap - 2, xSmallMap + wSmallMap + 1, ySmallMap + hSmallMap + 1,
colorFromDelphi(0xFFFFFF), colorFromDelphi(0xB0B0B0));
frame(canvas, xSupport - 1, ySupport - 1, xSupport + wSupport, ySupport + hSupport, colorFromDelphi(0xB0B0B0),
colorFromDelphi(0xFFFFFF));
rframe(canvas, xSupport - 2, ySupport - 2, xSupport + wSupport + 1, ySupport + hSupport + 1,
colorFromDelphi(0xFFFFFF), colorFromDelphi(0xB0B0B0));
int x = xSupport + wSupport / 2;
int y = ySupport + hSupport / 2;
if (smallMapMode == SmallMapMode::SupportedUnits) {
canvas.brush().setStyle(Qt::SolidPattern);
canvas.brush().setColor(MainTexture->colorMark);
canvas.fillRect(QRect(x - 27, y - 6, 54, 12));
canvas.brush().setStyle(Qt::NoBrush);
}
sprite(canvas, *HGrSystem, x - 16, y - 5, 10, 10, 88, 115);
sprite(canvas, *HGrSystem, x - 5, y - 5, 10, 10, 66, 115);
sprite(canvas, *HGrSystem, x + 6, y - 5, 10, 10, 154, 126);
canvas.copyRect(QRect(xZoomMap, yZoomMap, wZoomMap, hZoomMap), zoomCityMap.canvas(),
QRect(0, 0, wZoomMap, hZoomMap));
for (int i = 0; i <= 5; ++i)
imix[static_cast<std::size_t>(i)] = -1;
if (smallMapMode == SmallMapMode::Improvements) {
if (zoomArea == 5) {
int cnt = 0;
for (int iix = 0; iix < nImp; ++iix)
if ((iix < nWonder || ImpPosition[static_cast<std::size_t>(iix - 28)] < 0) && c.Built[iix] > 0) {
const int i = cnt - page * 6;
if (i >= 0 && i < 6)
imix[static_cast<std::size_t>(i)] = iix;
++cnt;
}
pageCount = (cnt + 5) / 6;
} else {
for (int iix = nWonder; iix < nImp; ++iix) {
const int i = ImpPosition[static_cast<std::size_t>(iix - 28)] - 6 * zoomArea;
if (i >= 0 && i < 6 && c.Built[iix] > 0)
imix[static_cast<std::size_t>(i)] = iix;
}
pageCount = 0;
}
int happyGain = 0;
for (int i = 0; i <= 5; ++i)
if (imix[static_cast<std::size_t>(i)] >= 0) {
const int iix = imix[static_cast<std::size_t>(i)];
x = xZoomMap + 14 + 72 * (i % 3);
y = yZoomMap + 14 + 56 * (i / 3);
Term::impImage(canvas, x, y, iix, cGov, allowChange && IsoEngine::clientMode < scContact);
if (isCityAlive) {
if (iix == imColosseum) {
sprite(canvas, *HGrSystem, x + 46, y, 14, 14, 82, 100);
} else {
happyGain = 0;
if (iix <= 27 || iix == imTemple)
happyGain = 2;
else if (iix == imTheater)
happyGain = 4;
else if (iix == imCathedral)
happyGain = ClientTools::myRO->Wonder[woBach].EffectiveOwner == cOwner ? 6 : 4;
int gainStep = 10;
if (happyGain > 1) {
gainStep = 30 / (happyGain - 1);
if (gainStep > 10)
gainStep = 10;
}
for (int j = 0; j < happyGain; ++j)
sprite(canvas, *HGrSystem, x + 50, y + gainStep * j, 10, 10, 132, 126);
}
for (int j = 0; j < Imp[iix].Maint; ++j)
sprite(canvas, *HGrSystem, x - 4, y + 29 - 3 * j, 10, 10, 132, 115);
}
}
ui->imp0Area->setToolTip(imix[0] >= 0 ? impName(imix[0]) : QString());
ui->imp1Area->setToolTip(imix[1] >= 0 ? impName(imix[1]) : QString());
ui->imp2Area->setToolTip(imix[2] >= 0 ? impName(imix[2]) : QString());
ui->imp3Area->setToolTip(imix[3] >= 0 ? impName(imix[3]) : QString());
ui->imp4Area->setToolTip(imix[4] >= 0 ? impName(imix[4]) : QString());
ui->imp5Area->setToolTip(imix[5] >= 0 ? impName(imix[5]) : QString());
} else { // smSupportedUnits
loweredTextOut(canvas, -1, *MainTexture, xZoomMap + 6, yZoomMap + 2, phrases.lookup(QStringLiteral("SUPUNITS")));
int freeSupp = c.Size * SupportFree[cGov] >> 1;
int cnt = 0;
for (int i = 0; i < ClientTools::myRO->nUn; ++i)
if (ClientTools::myUn[i].Loc >= 0 && ClientTools::myUn[i].Home == cix) {
const TModel &model = ClientTools::myModel[ClientTools::myUn[i].mix];
Q_UNUSED(model);
TUnitReport unitReport{};
ClientTools::server(sGetUnitReport, ClientTools::me, i, &unitReport);
if (cnt >= 6 * page && cnt < 6 * (page + 1)) {
imix[static_cast<std::size_t>(cnt - 6 * page)] = i;
x = (cnt - 6 * page) % 3 * 64 + xZoomMap;
y = (cnt - 6 * page) / 3 * 52 + yZoomMap + 20;
TUnitInfo unitInfo{};
MakeUnitInfo(ClientTools::me, ClientTools::myUn[i], unitInfo);
noMap->setOutput(&offscreen);
noMap->paintUnit(x, y, unitInfo, ClientTools::myUn[i].Status);
for (int j = 0; j < unitReport.FoodSupport; ++j)
sprite(canvas, *HGrSystem, x + 38 + 11 * j, y + 40, 10, 10, 66, 115);
for (int j = 0; j < unitReport.ProdSupport; ++j) {
if (freeSupp > 0 && (unitReport.ReportFlags & urfAlwaysSupport) == 0) {
sprite(canvas, *HGrSystem, x + 16 - 11 * j, y + 40, 10, 10, 143, 115);
--freeSupp;
} else {
sprite(canvas, *HGrSystem, x + 16 - 11 * j, y + 40, 10, 10, 88, 115);
}
}
if (unitReport.ReportFlags & urfDeployed)
for (int j = 0; j <= 1; ++j)
sprite(canvas, *HGrSystem, x + 27 + 11 * j, y + 40, 10, 10, 154, 126);
} else {
freeSupp -= unitReport.ProdSupport;
}
++cnt;
}
pageCount = (cnt + 5) / 6;
ui->imp0Area->setToolTip(QString());
ui->imp1Area->setToolTip(QString());
ui->imp2Area->setToolTip(QString());
ui->imp3Area->setToolTip(QString());
ui->imp4Area->setToolTip(QString());
ui->imp5Area->setToolTip(QString());
}
ui->pageUpBtn->setVisible(pageCount > 1);
ui->pageDownBtn->setVisible(pageCount > 1);
{ // display project now
dLine(canvas, xView + 9 + xSizeBig, xProd + 2 * wBar + 10, yProd + dyBar + 16, colorFromDelphi(0xFFFFFF),
colorFromDelphi(0xB0B0B0));
if (prodHint) {
frame(canvas, xView + 9 - 1, yView + 5 - 1, xView + 9 + xSizeBig, yView + 5 + ySizeBig,
colorFromDelphi(0xB0B0B0), colorFromDelphi(0xFFFFFF));
rframe(canvas, xView + 9 - 2, yView + 5 - 2, xView + 9 + xSizeBig + 1, yView + 5 + ySizeBig + 1,
colorFromDelphi(0xFFFFFF), colorFromDelphi(0xB0B0B0));
canvas.brush().setStyle(Qt::SolidPattern);
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(xView + 9, yView + 5, 72 - 8 - 9 + 1, 40));
canvas.brush().setStyle(Qt::NoBrush);
} else if (allowChange && (c.Status & 7) != 0) { // city type autobuild
frameImage(canvas, *bigImp, xView + 9, yView + 5, xSizeBig, ySizeBig, ((c.Status & 7) - 1 + 3) * xSizeBig,
0, cix >= 0 && IsoEngine::clientMode < scContact);
} else if ((c.Project & cpImp) == 0) { // project is unit
frameImage(canvas, *bigImp, xView + 9, yView + 5, xSizeBig, ySizeBig, 0, 0,
allowChange && IsoEngine::clientMode < scContact);
const Tribes::ModelPicture &mp =
Tribes::tribe[static_cast<std::size_t>(cOwner)]->modelPictureAt(c.Project & cpIndex);
if (mp.hGr)
sprite(canvas, *mp.hGr, xView + 5, yView + 1, 64, 44, mp.pix % 10 * 65 + 1, mp.pix / 10 * 49 + 1);
} else { // project is building
const int paintiix = prodHint ? (c.Project0 & cpIndex) : (c.Project & cpIndex);
Term::impImage(canvas, xView + 9, yView + 5, paintiix, cGov, allowChange && IsoEngine::clientMode < scContact);
}
}
if (allowChange && IsoEngine::clientMode < scContact) {
const int r = ClientTools::server(sBuyCityProject - sExecute, ClientTools::me, cix, nullptr);
ui->buyBtn->setVisible(r == eOK || r == eViolation);
} else {
ui->buyBtn->setVisible(false);
}
canvas.end();
markUsedOffscreen(width(), height());
}
void CityDlg::showNewContent(WindowMode newMode, int loc, std::uint32_t showEvent)
{
if (ClientTools::myMap[loc] & fOwned) { // own city
cix = ClientTools::myRO->nCity - 1;
while (cix >= 0 && ClientTools::myCity[cix].Loc != loc)
--cix;
Q_ASSERT(cix >= 0);
if (optimizeCixTileChange >= 0) {
const std::uint32_t released =
optimizeTilesBeforeChange & ~ClientTools::myCity[optimizeCixTileChange].Tiles;
if (released != 0) {
ClientTools::cityOptimizer_ReleaseCityTiles(optimizeCixTileChange, static_cast<int>(released));
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
}
}
optimizeCixTileChange = cix;
optimizeTilesBeforeChange = ClientTools::myCity[cix].Tiles;
} else {
cix = -1;
}
allowChange = !Term::supervising && cix >= 0;
cLoc = loc;
happened = showEvent;
BufferedDrawDlg::showNewContent(newMode);
}
void CityDlg::closeEvent(QCloseEvent *event)
{
timer1->stop();
prodHint = false;
// MarkCityLoc := -1; -- no consumer has been ported yet, see repo memory.
if (optimizeCixTileChange >= 0) {
const std::uint32_t released =
optimizeTilesBeforeChange & ~ClientTools::myCity[optimizeCixTileChange].Tiles;
if (released != 0) {
ClientTools::cityOptimizer_ReleaseCityTiles(optimizeCixTileChange, static_cast<int>(released));
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
}
optimizeCixTileChange = -1;
}
if (closeAction != CityCloseAction::None)
Term::mainScreenCityClosed(restoreUnFocus, closeAction == CityCloseAction::StepFocus, false);
restoreUnFocus = -1;
BufferedDrawDlg::closeEvent(event);
}
void CityDlg::keyPressEvent(QKeyEvent *event)
{
const int key = event->key();
if (key == Qt::Key_P) {
if (prodHint) {
prodHint = false;
smartUpdateContent();
}
chooseProject();
} else if (key == Qt::Key_Return || key == Qt::Key_Enter) {
close();
} else if (key == Qt::Key_Up && cix >= 0 && windowMode() == WindowMode::Persistent) {
changeCity(-1);
} else if (key == Qt::Key_Down && cix >= 0 && windowMode() == WindowMode::Persistent) {
changeCity(+1);
} else {
BufferedDrawDlg::keyPressEvent(event);
}
}
void CityDlg::paintEvent(QPaintEvent *event)
{
BufferedDrawDlg::paintEvent(event);
if (openSoundEvent >= 0)
QTimer::singleShot(0, this, [this] { onPlaySound(); });
}
void CityDlg::onPlaySound()
{
if ((1 << openSoundEvent) == chProduction) {
if ((c.Project0 & cpImp) != 0) {
if ((c.Project0 & cpIndex) >= 28) {
if (Imp[c.Project0 & cpIndex].Kind == ikShipPart)
play(QStringLiteral("SHIP_BUILT"));
else
play(QStringLiteral("CITY_IMPCOMPLETE"));
}
} else {
play(QStringLiteral("CITY_UNITCOMPLETE"));
}
} else if (openSoundEvent >= 0) {
play(Term::CityEventSoundItem[static_cast<std::size_t>(openSoundEvent)]);
}
openSoundEvent = -2;
}
void CityDlg::changeCity(int d)
{
int cixNew = cix;
do {
cixNew = (cixNew + ClientTools::myRO->nCity + d) % ClientTools::myRO->nCity;
} while (ClientTools::myCity[cixNew].Loc < 0 && cixNew != cix);
if (cixNew != cix)
Term::mainScreenZoomToCity(ClientTools::myCity[cixNew].Loc, false, 0);
}
void CityDlg::changeResourceWeights(int resourceWeights)
{
Q_ASSERT(!Term::supervising);
Q_ASSERT(cix >= 0);
ClientTools::myCity[cix].Status =
(ClientTools::myCity[cix].Status & ~Term::csResourceWeightsMask) | (resourceWeights << 4);
c.Status = ClientTools::myCity[cix].Status;
if (resourceWeights > 0) {
TCityTileAdviceData advice{};
advice.ResourceWeights = ClientTools::OfferedResourceWeights[resourceWeights];
ClientTools::server(sGetCityTileAdvice, ClientTools::me, cix, &advice);
if (advice.Tiles != ClientTools::myCity[cix].Tiles)
ClientTools::server(sSetCityTiles, ClientTools::me, cix, &advice.Tiles);
}
}
void CityDlg::buyClick()
{
if (cix < 0 || IsoEngine::clientMode >= scContact)
return;
auto *dlg = Term::mainScreenMessgExDlg();
if (!dlg)
return;
const TCity &city = ClientTools::myCity[cix];
int cost = report.ProjectCost;
int nextProd = report.Production;
if (nextProd < 0)
nextProd = 0;
cost = cost - city.Prod - nextProd;
if (ClientTools::myRO->Wonder[woMich].EffectiveOwner == ClientTools::me && (city.Project & cpImp) != 0)
cost *= 2;
else
cost *= 4;
if (cost <= 0 && report.HappinessBalance >= 0) {
dlg->messageText = phrases.lookup(QStringLiteral("READY"));
dlg->kind = MessageKind::Ok;
} else if (cost > ClientTools::myRO->Money) {
dlg->openSound = QStringLiteral("MSG_DEFAULT");
dlg->messageText = formatPhrase(QStringLiteral("NOMONEY"),
{ QString::number(cost), QString::number(ClientTools::myRO->Money) });
dlg->kind = MessageKind::Ok;
} else {
dlg->messageText = formatPhrase(QStringLiteral("BUY"), { QString::number(cost) });
dlg->kind = MessageKind::YesNo;
}
const MessageKind queriedKind = dlg->kind;
const Dpi::ModalResult result = dlg->showModal();
if (queriedKind == MessageKind::YesNo && result == Dpi::ModalResult::Ok) {
if (ClientTools::server(sBuyCityProject, ClientTools::me, cix, nullptr) >= rExecuted) {
play(QStringLiteral("CITY_BUYPROJECT"));
smartUpdateContent();
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
}
}
}
void CityDlg::chooseProject()
{
enum class ProjectKind { Select, TrGoods, Un, Caravan, Imp, Wonder, Ship, Invalid };
auto projectType = [](int project) {
if (project & cpCompleted)
return ProjectKind::Select;
if ((project & (cpImp + cpIndex)) == cpImp + imTrGoods)
return ProjectKind::TrGoods;
if ((project & cpImp) == 0)
return ClientTools::myModel[project & cpIndex].Kind == mkCaravan ? ProjectKind::Caravan : ProjectKind::Un;
if ((project & cpIndex) >= nImp)
return ProjectKind::Invalid;
if (Imp[project & cpIndex].Kind == ikWonder)
return ProjectKind::Wonder;
if (Imp[project & cpIndex].Kind == ikShipPart)
return ProjectKind::Ship;
return ProjectKind::Imp;
};
Q_ASSERT(!Term::supervising);
const int result = Term::modalSelectDlgShowCityProject(cix);
if (result == -1)
return;
if (result & Term::cpType) {
ClientTools::myCity[cix].Status = (ClientTools::myCity[cix].Status & ~7) | (1 + (result & cpIndex));
ClientTools::autoBuild(cix, Term::myData->impOrder[result & cpIndex]);
} else {
int newProject = result;
bool queryOk = true;
if ((newProject & cpImp) != 0 && (newProject & cpIndex) >= 28 &&
ClientTools::myRO->NatBuilt[newProject & cpIndex] > 0) {
if (auto *dlg = Term::mainScreenMessgExDlg()) {
int cix1 = ClientTools::myRO->nCity - 1;
while (cix1 >= 0 && ClientTools::myCity[cix1].Built[newProject & cpIndex] == 0)
--cix1;
dlg->messageText = formatPhrase(QStringLiteral("DOUBLESTATEIMP"),
{ impName(newProject & cpIndex),
Tribes::cityName(ClientTools::myCity[cix1].ID) });
dlg->openSound = QStringLiteral("MSG_DEFAULT");
dlg->kind = MessageKind::OkCancel;
dlg->iconKind = MessgEx::MessageIconKind::Imp;
dlg->iconIndex = newProject & cpIndex;
queryOk = dlg->showModal() == Dpi::ModalResult::Ok;
}
}
if (!queryOk)
return;
if (ClientTools::myCity[cix].Prod > 0) {
const ProjectKind pt0 = projectType(ClientTools::myCity[cix].Project0);
const ProjectKind pt1 = projectType(newProject);
if (pt0 != ProjectKind::Select && pt1 != ProjectKind::TrGoods) {
if ((newProject & (cpImp | cpIndex)) != (ClientTools::myCity[cix].Project0 & (cpImp | cpIndex))) {
if (pt1 == ProjectKind::TrGoods || pt1 == ProjectKind::Ship ||
(pt1 != pt0 && pt0 != ProjectKind::Caravan)) {
queryOk = MessgEx::simpleQuery(
MessageKind::OkCancel,
formatPhrase(QStringLiteral("LOSEMAT"),
{ QString::number(ClientTools::myCity[cix].Prod0),
QString::number(ClientTools::myCity[cix].Prod0) }),
QStringLiteral("MSG_DEFAULT")) == Dpi::ModalResult::Ok;
} else if ((ClientTools::myCity[cix].Project & (cpImp | cpIndex)) ==
(ClientTools::myCity[cix].Project0 & (cpImp | cpIndex))) {
queryOk = MessgEx::simpleQuery(MessageKind::OkCancel, phrases.lookup(QStringLiteral("LOSEMAT3")),
QStringLiteral("MSG_DEFAULT")) == Dpi::ModalResult::Ok;
}
}
}
}
if (!queryOk)
return;
const int oldMoney = ClientTools::myRO->Money;
ClientTools::myCity[cix].Status &= ~7;
if ((newProject & cpImp) == 0 &&
((ClientTools::myCity[cix].Size < 4 && ClientTools::myModel[newProject & cpIndex].Kind == mkSettler) ||
(ClientTools::myCity[cix].Size < 3 &&
(ClientTools::myModel[newProject & cpIndex].Kind == mkSlaves ||
(newProject & cpConscripts) != 0)))) {
if (MessgEx::simpleQuery(MessageKind::YesNo, phrases.lookup(QStringLiteral("EMIGRATE")),
QStringLiteral("MSG_DEFAULT")) != Dpi::ModalResult::Ok)
newProject |= cpDisbandCity;
}
ClientTools::server(sSetCityProject, ClientTools::me, cix, &newProject);
c.Project = ClientTools::myCity[cix].Project;
if (ClientTools::myRO->Money > oldMoney)
play(QStringLiteral("CITY_SELLIMP"));
}
ClientTools::cityOptimizer_CityChange(cix);
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
initSmallCityMap();
smartUpdateContent();
}
void CityDlg::mousePressEvent(QMouseEvent *event)
{
const int x = event->position().toPoint().x();
const int y = event->position().toPoint().y();
const bool shift = event->modifiers() & Qt::ShiftModifier;
if (event->button() == Qt::LeftButton && x >= xSmallMap && x < xSmallMap + wSmallMap && y >= ySmallMap &&
y < ySmallMap + hSmallMap) {
smallMapMode = SmallMapMode::Improvements;
zoomArea = (y - ySmallMap) * 3 / hSmallMap + 3 * ((x - xSmallMap) * 2 / wSmallMap);
page = 0;
initZoomCityMap();
smartUpdateContent();
return;
}
if (event->button() == Qt::LeftButton && x >= xSupport && x < xSupport + wSupport && y >= ySupport &&
y < ySupport + hSupport) {
smallMapMode = SmallMapMode::SupportedUnits;
page = 0;
initZoomCityMap();
smartUpdateContent();
return;
}
if (!allowChange)
return;
if (event->button() == Qt::RightButton) {
if (x >= CaptionX && y >= CaptionY && x <= ui->prevCityBtn->x() &&
y <= ui->nextCityBtn->y() + ui->nextCityBtn->height()) {
if (Term::listDlgRenameCity(cix)) {
smartUpdateContent();
Term::mainScreenRepaintAll();
}
}
return;
}
if (event->button() != Qt::LeftButton)
return;
if (IsoEngine::clientMode < scContact && x >= xView && y >= yView && x < xView + 73 && y < yView + 50) {
if (cGov == gAnarchy) {
if (auto *dlg = Term::mainScreenMessgExDlg()) {
dlg->messageText = phrases.lookup(QStringLiteral("NOCHANGEINANARCHY"));
dlg->kind = MessageKind::Ok;
dlg->showModal();
}
} else {
if (prodHint) {
prodHint = false;
smartUpdateContent();
}
chooseProject();
}
} else if (smallMapMode == SmallMapMode::Improvements && x >= xZoomMap && x < xZoomMap + wZoomMap &&
y >= yZoomMap && y < yZoomMap + hZoomMap) {
int i = 5;
while (i >= 0 && !(x >= xZoomMap + 14 + 72 * (i % 3) && x < xZoomMap + 14 + 56 + 72 * (i % 3) &&
y >= yZoomMap + 14 + 56 * (i / 3) && y < yZoomMap + 14 + 40 + 56 * (i / 3)))
--i;
if (i >= 0) {
int iix = imix[static_cast<std::size_t>(i)];
if (iix >= 0) {
if (shift) {
if (auto *dlg = Term::mainScreenHelpDlg())
dlg->showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Imp, iix);
} else if (IsoEngine::clientMode < scContact) {
if (auto *dlg = Term::mainScreenMessgExDlg()) {
dlg->iconKind = MessgEx::MessageIconKind::Imp;
dlg->iconIndex = iix;
if (iix == imPalace || Imp[iix].Kind == ikWonder) {
dlg->messageText = impName(iix);
if (iix == woOracle)
dlg->messageText +=
QStringLiteral("\n") +
formatPhrase(QStringLiteral("ORACLEINCOME"),
{ QString::number(ClientTools::myRO->OracleIncome) });
dlg->kind = MessageKind::Ok;
dlg->showModal();
} else {
const int sellResult =
ClientTools::server(sSellCityImprovement - sExecute, ClientTools::me, cix, &iix);
if (sellResult < rExecuted) {
dlg->messageText = formatPhrase(sellResult == eOnlyOnce ? QStringLiteral("NOSELLAGAIN")
: QStringLiteral("OUTOFCONTROL"),
{ impName(iix) });
dlg->kind = MessageKind::Ok;
dlg->showModal();
} else {
bool rebuild;
int newIix = iix;
if (ClientTools::server(sRebuildCityImprovement - sExecute, ClientTools::me, cix,
&iix) < rExecuted) {
rebuild = false;
dlg->messageText = formatPhrase(QStringLiteral("SELL"), { impName(iix) });
if (iix == imSpacePort) {
const auto &ship = ClientTools::myRO->Ship[ClientTools::me];
if (ship.Parts[0] + ship.Parts[1] + ship.Parts[2] > 0)
dlg->messageText +=
QStringLiteral(" ") + phrases.lookup(QStringLiteral("SPDESTRUCTQUERY"));
}
dlg->kind = MessageKind::YesNo;
if (dlg->showModal() != Dpi::ModalResult::Ok)
newIix = -1;
} else {
rebuild = true;
dlg->messageText = formatPhrase(QStringLiteral("DISPOSE"), { impName(iix) });
if (iix == imSpacePort) {
const auto &ship = ClientTools::myRO->Ship[ClientTools::me];
if (ship.Parts[0] + ship.Parts[1] + ship.Parts[2] > 0)
dlg->messageText +=
QStringLiteral(" ") + phrases.lookup(QStringLiteral("SPDESTRUCTQUERY"));
}
dlg->kind = MessageKind::YesNo;
if (dlg->showModal() != Dpi::ModalResult::Ok)
newIix = -1;
}
if (newIix >= 0) {
if (rebuild) {
play(QStringLiteral("CITY_REBUILDIMP"));
ClientTools::server(sRebuildCityImprovement, ClientTools::me, cix, &newIix);
} else {
play(QStringLiteral("CITY_SELLIMP"));
ClientTools::server(sSellCityImprovement, ClientTools::me, cix, &newIix);
}
ClientTools::cityOptimizer_CityChange(cix);
initSmallCityMap();
smartUpdateContent();
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
}
}
}
}
}
}
}
} else if (smallMapMode == SmallMapMode::SupportedUnits && x >= xZoomMap && x < xZoomMap + wZoomMap &&
y >= yZoomMap && y < yZoomMap + hZoomMap) {
int i = 5;
while (i >= 0 && !(x >= xZoomMap + 64 * (i % 3) && x < xZoomMap + 64 + 64 * (i % 3) &&
y >= yZoomMap + 20 + 48 * (i / 3) && y < yZoomMap + 20 + 52 + 48 * (i / 3)))
--i;
if (i >= 0 && imix[static_cast<std::size_t>(i)] >= 0) {
if (!shift && cix >= 0 && IsoEngine::clientMode < scContact && windowMode() != WindowMode::Modal) {
closeAction = CityCloseAction::None;
const int uix = imix[static_cast<std::size_t>(i)];
close();
Term::mainScreenCityClosed(uix, false, true);
}
}
} else if (x >= xmArea - 192 && x < xmArea + 192 && y >= ymArea - 96 && y < ymArea + 96) {
const int tileX = areaMap->xxt;
const int tileY = areaMap->yyt;
const int qx =
(4000 * tileX * tileY + (x - xmArea) * (tileY * 2) + (y - ymArea + tileY) * (tileX * 2)) /
(tileX * tileY * 4) -
1000;
const int qy =
(4000 * tileX * tileY + (y - ymArea + tileY) * (tileX * 2) - (x - xmArea) * (tileY * 2)) /
(tileX * tileY * 4) -
1000;
const int dx = qx - qy;
const int dy = qx + qy;
if (dx >= -3 && dx <= 3 && dy >= -3 && dy <= 3 && dx * dx * dy * dy < 81 && (dx != 0 || dy != 0)) {
if (shift) { // terrain help
const int loc1 = ClientTools::dLoc(cLoc, dx, dy);
if (loc1 >= 0 && loc1 < ClientTools::g.lx * ClientTools::g.ly)
Term::helpOnTerrain(loc1, windowModeMakePersistent(windowMode()));
} else if (IsoEngine::clientMode < scContact && cGov != gAnarchy && (c.Flags & chCaptured) == 0) {
// toggle exploitation
Q_ASSERT(!Term::supervising);
if ((c.Status & Term::csResourceWeightsMask) != 0) {
if (auto *dlg = Term::mainScreenMessgExDlg()) {
dlg->messageText = phrases.lookup(QStringLiteral("CITYMANAGEOFF"));
dlg->openSound = QStringLiteral("MSG_DEFAULT");
dlg->kind = MessageKind::OkCancel;
dlg->iconKind = MessgEx::MessageIconKind::FullControl;
const Dpi::ModalResult result = dlg->showModal();
if (result == Dpi::ModalResult::Ok) {
ClientTools::myCity[cix].Status &= ~Term::csResourceWeightsMask;
c.Status = ClientTools::myCity[cix].Status;
smartUpdateContent();
}
}
return;
}
const int fix = (dy + 3) * 4 + ((dx + 3) >> 1);
std::uint32_t newTiles = ClientTools::myCity[cix].Tiles ^ (1u << fix);
if (ClientTools::server(sSetCityTiles, ClientTools::me, cix, &newTiles) >= rExecuted) {
smartUpdateContent();
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
}
}
}
} else if (IsoEngine::clientMode < scContact && cGov != gAnarchy && (c.Flags & chCaptured) == 0 &&
x >= xmOpt - 32 && x < xmOpt + 32 && y >= ymOpt - 32 && y < ymOpt + 32) {
const int clickRadius = (x - xmOpt) * (x - xmOpt) + (y - ymOpt) * (y - ymOpt);
if (clickRadius <= 32 * 32) {
int i;
if (clickRadius < 16 * 16) { // inner area clicked
if ((c.Status & Term::csResourceWeightsMask) != 0)
i = (c.Status >> 4 & 0x0F) % 5 + 1; // rotate except off
else
i = 3; // rwGrowth
} else {
const int angle = static_cast<int>(
std::atan2(static_cast<double>(x - xmOpt), static_cast<double>(ymOpt - y)) * 180.0 / M_PI);
if (angle >= -25 - 52 * 2 && angle <= -26 - 52)
i = 1;
else if (angle >= -25 - 52 && angle <= -26)
i = 2;
else if (angle >= -25 && angle <= 25)
i = 3;
else if (angle >= 26 && angle <= 25 + 52)
i = 4;
else if (angle >= 26 + 52 && angle <= 25 + 52 * 2)
i = 5;
else if ((angle >= 180 - 26 && angle <= 180) || (angle >= -180 && angle <= -180 + 26))
i = 0;
else
i = -1;
}
if (i >= 0) {
changeResourceWeights(i);
smartUpdateContent();
if (windowMode() != WindowMode::Modal)
Term::mainScreenUpdateViews(false);
}
}
}
}
} // namespace CityScreen
} // namespace Cevo