diff --git a/CMakeLists.txt b/CMakeLists.txt index cbbf61f..ab4765e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -163,6 +163,9 @@ set(PROJECT_SOURCES game/localplayer/Select.cpp game/localplayer/Select.h game/localplayer/Select.ui + game/localplayer/Nego.cpp + game/localplayer/Nego.h + game/localplayer/Nego.ui ${TS_FILES} ) diff --git a/game/localplayer/Nego.cpp b/game/localplayer/Nego.cpp new file mode 100644 index 0000000..9861bb6 --- /dev/null +++ b/game/localplayer/Nego.cpp @@ -0,0 +1,909 @@ +// 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 "Nego.h" +#include "ui_Nego.h" + +#include "Cevo/CevoButtonN.h" +#include "Cevo/CevoScreenTools.h" +#include "Cevo/CevoTexture.h" +#include "ClientTools.h" +#include "Diplomacy.h" +#include "Inp.h" +#include "IsoEngine.h" +#include "MessgEx.h" +#include "Select.h" +#include "Term.h" +#include "Tribes.h" + +#include +#include +#include +#include +#include + +namespace Cevo { +namespace Nego { + +namespace { + +constexpr int xPadC = 140; +constexpr int yPadC = 427; +constexpr int xPad0 = 140; +constexpr int yPad0 = 13; +constexpr int xPad1 = 334; +constexpr int yPad1 = 13; +constexpr int wText = 300; +constexpr int hText = 256; +constexpr int xText0 = 14; +constexpr int yText0 = 154; +constexpr int xText1 = 326; +constexpr int yText1 = 154; +constexpr int xNationPicture0 = 20; +constexpr int xNationPicture1 = 556; +constexpr int yNationPicture = 40; +constexpr int xCred0 = 42; +constexpr int yCred0 = 92; +constexpr int xCred1 = 578; +constexpr int yCred1 = 92; +constexpr int PaperShade = 3; +constexpr int PaperBorder_Left = 12; +constexpr int PaperBorder_Right = 8; +constexpr int ListIndent = 24; + +const std::array RomanNo = { + QStringLiteral("I"), QStringLiteral("II"), QStringLiteral("III"), QStringLiteral("IV"), + QStringLiteral("V"), QStringLiteral("VI"), QStringLiteral("VII"), QStringLiteral("VIII"), + QStringLiteral("IX"), QStringLiteral("X"), QStringLiteral("XI"), QStringLiteral("XII"), + QStringLiteral("XIII"), QStringLiteral("XIV"), QStringLiteral("XV"), QStringLiteral("XVI"), +}; + +const std::array ButtonPrice = { + static_cast(opChoose), static_cast(opCivilReport), + static_cast(opMilReport), static_cast(opMap), + static_cast(opAllTech), static_cast(opAllTech), + static_cast(opAllModel), static_cast(opMoney), + static_cast(opTreaty), 0xFE000000u, // opLowTreaty (not used by any live case below) + static_cast(opShipParts), static_cast(opShipParts), +}; + +// Phrases/Phrases2 (StringTables.pas) aren't ported; disclosed +// plain-English placeholders for this dialog's own text. +QString phrase(const QString &key) +{ + static const QHash table = { + {QStringLiteral("BTN_OK"), QStringLiteral("OK")}, + {QStringLiteral("BTN_ACCEPT"), QStringLiteral("Accept")}, + {QStringLiteral("BTN_BREAK"), QStringLiteral("Break off")}, + {QStringLiteral("BTN_CNTREATY"), QStringLiteral("Cancel treaty")}, + {QStringLiteral("BTN_NOTICE"), QStringLiteral("Notice")}, + {QStringLiteral("BTN_PASS"), QStringLiteral("Pass")}, + {QStringLiteral("BTN_WANTPEACE"), QStringLiteral("Want peace")}, + {QStringLiteral("BTN_OFFERPEACE"), QStringLiteral("Offer peace")}, + {QStringLiteral("BTN_WANTFRIENDLY"), QStringLiteral("Want friendly relations")}, + {QStringLiteral("BTN_OFFERFRIENDLY"), QStringLiteral("Offer friendly relations")}, + {QStringLiteral("BTN_WANTALLIANCE"), QStringLiteral("Want alliance")}, + {QStringLiteral("BTN_OFFERALLIANCE"), QStringLiteral("Offer alliance")}, + {QStringLiteral("MAX2WANTS"), QStringLiteral("You can only ask for 2 things at once.")}, + {QStringLiteral("MAX2OFFERS"), QStringLiteral("You can only offer 2 things at once.")}, + {QStringLiteral("CANCELTREATYRUSH"), QStringLiteral("You can't cancel the treaty again so soon.")}, + {QStringLiteral("TITLE_AMOUNT"), QStringLiteral("Enter amount")}, + {QStringLiteral("TITLE_NUMBER"), QStringLiteral("Enter number")}, + {QStringLiteral("NOSPACEPORT"), QStringLiteral("We have no space port to ship parts from.")}, + {QStringLiteral("WANT_ANYTHING"), QStringLiteral("Anything")}, + {QStringLiteral("WANT_STATEREPORT"), QStringLiteral("State report")}, + {QStringLiteral("WANT_MILREPORT"), QStringLiteral("Military report")}, + {QStringLiteral("WANT_MAP"), QStringLiteral("Map")}, + {QStringLiteral("WANT_TECH"), QStringLiteral("Advance")}, + {QStringLiteral("WANT_MODEL"), QStringLiteral("Unit design")}, + {QStringLiteral("WANT_MONEY"), QStringLiteral("Money")}, + {QStringLiteral("WANT_HITREATY"), QStringLiteral("Improve treaty")}, + {QStringLiteral("WANT_LOTREATY"), QStringLiteral("Worsen treaty")}, + {QStringLiteral("WANT_SHIPPART"), QStringLiteral("Ship part")}, + {QStringLiteral("OFFER_ANYTHING"), QStringLiteral("Anything")}, + {QStringLiteral("OFFER_STATEREPORT"), QStringLiteral("State report")}, + {QStringLiteral("OFFER_MILREPORT"), QStringLiteral("Military report")}, + {QStringLiteral("OFFER_MAP"), QStringLiteral("Map")}, + {QStringLiteral("OFFER_TECH"), QStringLiteral("Advance")}, + {QStringLiteral("OFFER_MODEL"), QStringLiteral("Unit design")}, + {QStringLiteral("OFFER_MONEY"), QStringLiteral("Money")}, + {QStringLiteral("OFFER_HITREATY"), QStringLiteral("Improve treaty")}, + {QStringLiteral("OFFER_LOTREATY"), QStringLiteral("Worsen treaty")}, + {QStringLiteral("OFFER_SHIPPART"), QStringLiteral("Ship part")}, + }; + const auto it = table.constFind(key); + return it != table.constEnd() ? *it : key; +} + +} // namespace + +NegoDlg::NegoDlg(QWidget *parent) : BufferedDrawDlg(parent), ui(new Ui::NegoDlg) +{ + ui->setupUi(this); + initButtons(); + + wantButtons = {ui->wantAnythingBtn, ui->wantStateReportBtn, ui->wantMilReportBtn, ui->wantMapBtn, + ui->wantTech1Btn, ui->wantTech2Btn, ui->wantModelBtn, ui->wantMoneyBtn, + ui->wantHiTreatyBtn, ui->wantLoTreatyBtn, ui->wantShipPart1Btn, ui->wantShipPart2Btn}; + offerButtons = {ui->offerAnythingBtn, ui->offerStateReportBtn, ui->offerMilReportBtn, ui->offerMapBtn, + ui->offerTech1Btn, ui->offerTech2Btn, ui->offerModelBtn, ui->offerMoneyBtn, + ui->offerHiTreatyBtn, ui->offerLoTreatyBtn, ui->offerShipPart1Btn, ui->offerShipPart2Btn}; + + const std::array wantHints = { + phrase(QStringLiteral("WANT_ANYTHING")), phrase(QStringLiteral("WANT_STATEREPORT")), + phrase(QStringLiteral("WANT_MILREPORT")), phrase(QStringLiteral("WANT_MAP")), + phrase(QStringLiteral("WANT_TECH")), phrase(QStringLiteral("WANT_TECH")), + phrase(QStringLiteral("WANT_MODEL")), phrase(QStringLiteral("WANT_MONEY")), + phrase(QStringLiteral("WANT_HITREATY")), phrase(QStringLiteral("WANT_LOTREATY")), + phrase(QStringLiteral("WANT_SHIPPART")), phrase(QStringLiteral("WANT_SHIPPART")), + }; + const std::array offerHints = { + phrase(QStringLiteral("OFFER_ANYTHING")), phrase(QStringLiteral("OFFER_STATEREPORT")), + phrase(QStringLiteral("OFFER_MILREPORT")), phrase(QStringLiteral("OFFER_MAP")), + phrase(QStringLiteral("OFFER_TECH")), phrase(QStringLiteral("OFFER_TECH")), + phrase(QStringLiteral("OFFER_MODEL")), phrase(QStringLiteral("OFFER_MONEY")), + phrase(QStringLiteral("OFFER_HITREATY")), phrase(QStringLiteral("OFFER_LOTREATY")), + phrase(QStringLiteral("OFFER_SHIPPART")), phrase(QStringLiteral("OFFER_SHIPPART")), + }; + const std::array buttonIndex = {17, 6, 7, 8, 10, 10, 11, 12, 14, 15, 16, 16}; + + for (int i = 0; i < 12; ++i) { + ButtonN *w = wantButtons[static_cast(i)]; + ButtonN *o = offerButtons[static_cast(i)]; + if (HGrSystem) { + w->setGraphic(&HGrSystem->data); + w->setMask(&HGrSystem->mask); + o->setGraphic(&HGrSystem->data); + o->setMask(&HGrSystem->mask); + } + if (HGrSystem2) { + w->setBackGraphic(&HGrSystem2->data); + o->setBackGraphic(&HGrSystem2->data); + } + w->setButtonIndex(buttonIndex[static_cast(i)]); + o->setButtonIndex(buttonIndex[static_cast(i)]); + w->setSmartHint(wantHints[static_cast(i)]); + o->setSmartHint(offerHints[static_cast(i)]); + w->onClick = [this, i] { wantClicked(i); }; + o->onClick = [this, i] { offerClicked(i); }; + } + ui->acceptBtn->setButtonIndex(0); + ui->passBtn->setButtonIndex(1); + ui->exitBtn->setButtonIndex(2); + ui->cancelTreatyBtn->setButtonIndex(3); + for (ButtonN *b : {ui->acceptBtn, ui->passBtn, ui->exitBtn, ui->cancelTreatyBtn}) { + if (HGrSystem) { + b->setGraphic(&HGrSystem->data); + b->setMask(&HGrSystem->mask); + } + if (HGrSystem2) + b->setBackGraphic(&HGrSystem2->data); + } + ui->acceptBtn->onClick = [this] { fastClicked(scDipAccept - scDipStart); }; + ui->passBtn->onClick = [this] { fastClicked(scDipOffer - scDipStart); }; + ui->exitBtn->onClick = [this] { fastClicked(scDipBreak - scDipStart); }; + ui->cancelTreatyBtn->onClick = [this] { fastClicked(scDipCancelTreaty - scDipStart); }; + + helpContext = QStringLiteral("DIPLOMACY"); + ui->okBtn->setCaption(phrase(QStringLiteral("BTN_OK"))); + ui->acceptBtn->setSmartHint(phrase(QStringLiteral("BTN_ACCEPT"))); + ui->exitBtn->setSmartHint(phrase(QStringLiteral("BTN_BREAK"))); + ui->cancelTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_CNTREATY"))); + + romanFont.setName(QStringLiteral("Times New Roman")); + romanFont.setSize(20); + + ui->closeBtn->onClick = [this] { close(); }; + ui->bwdBtn->onClick = [this] { + page -= 2; + setButtonStates(); + smartUpdateContent(); + }; + ui->fwdBtn->onClick = [this] { + page += 2; + setButtonStates(); + smartUpdateContent(); + }; + ui->okBtn->onClick = [this] { + ++history[static_cast(ClientTools::me)].n; + if (dipCommand == scDipOffer) + Term::mainScreenOfferCall(currentOffer); + else + Term::mainScreenDipCall(dipCommand); + }; + + onShow = [this] { offscreenPaint(); }; +} + +NegoDlg::~NegoDlg() +{ + delete ui; +} + +void NegoDlg::resetCurrentOffer() +{ + currentOffer.nDeliver = 0; + currentOffer.nCost = 0; + costs.fill(0xFFFFFFFFu); + delivers.fill(0xFFFFFFFFu); +} + +void NegoDlg::buildCurrentOffer() +{ + currentOffer.nDeliver = 0; + currentOffer.nCost = 0; + for (std::uint32_t d : delivers) + if (d != 0xFFFFFFFFu) { + currentOffer.Price[currentOffer.nDeliver] = d; + ++currentOffer.nDeliver; + } + for (std::uint32_t c : costs) + if (c != 0xFFFFFFFFu) { + currentOffer.Price[currentOffer.nDeliver + currentOffer.nCost] = c; + ++currentOffer.nCost; + } +} + +void NegoDlg::showNewContent(WindowMode newMode) +{ + BufferedDrawDlg::showNewContent(newMode); + setButtonStates(); + const int pContact = Term::dipMem[static_cast(ClientTools::me)].pContact; + ui->passBtn->setSmartHint(IsoEngine::clientMode == scDipCancelTreaty || IsoEngine::clientMode == scDipBreak + ? phrase(QStringLiteral("BTN_NOTICE")) + : phrase(QStringLiteral("BTN_PASS"))); + switch (ClientTools::myRO->Treaty[pContact]) { + case trNone: + ui->wantHiTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_WANTPEACE"))); + ui->offerHiTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_OFFERPEACE"))); + break; + case trPeace: + ui->wantHiTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_WANTFRIENDLY"))); + ui->offerHiTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_OFFERFRIENDLY"))); + break; + case trFriendlyContact: + ui->wantHiTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_WANTALLIANCE"))); + ui->offerHiTreatyBtn->setSmartHint(phrase(QStringLiteral("BTN_OFFERALLIANCE"))); + break; + default: + break; + } +} + +void NegoDlg::start() +{ + const int me = ClientTools::me; + const int pContact = Term::dipMem[static_cast(me)].pContact; + if (IsoEngine::clientMode != scDipStart) { + History &h = history[static_cast(me)]; + if (h.n == MaxHistory) { + for (int i = 0; i < MaxHistory - 2; ++i) + h.text[static_cast(i)] = h.text[static_cast(i + 2)]; + h.n -= 2; + } + h.text[static_cast(h.n)] = + Diplomacy::dipCommandToString(pContact, me, Term::dipMem[static_cast(me)].formerTreaty, + Term::dipMem[static_cast(me)].sentCommand, + IsoEngine::clientMode, Term::dipMem[static_cast(me)].sentOffer, + Term::receivedOffer) + .left(255); + ++h.n; + } + Q_ASSERT(history[static_cast(me)].n % 2 == 1); + + page = history[static_cast(me)].n; + findAllowed(); + resetCurrentOffer(); + dipCommand = -1; + showNewContent(WindowMode::Persistent); +} + +void NegoDlg::initiate() +{ + history[static_cast(ClientTools::me)].n = 1; + history[static_cast(ClientTools::me)].text[0].clear(); +} + +void NegoDlg::respond() +{ + history[static_cast(ClientTools::me)].n = 0; +} + +void NegoDlg::findAllowed() +{ + const int me = ClientTools::me; + const int pContact = Term::dipMem[static_cast(me)].pContact; + + commandAllowed.reset(); + commandAllowed.set(static_cast(scDipOffer - scDipStart)); + if (IsoEngine::clientMode != scDipBreak) + commandAllowed.set(static_cast(scDipBreak - scDipStart)); + if (ClientTools::myRO->Treaty[pContact] >= trPeace) + commandAllowed.set(static_cast(scDipCancelTreaty - scDipStart)); + if (IsoEngine::clientMode == scDipOffer) { + int dummy = 0; + if (ClientTools::server(scDipAccept - sExecute, me, 0, &dummy) >= rExecuted) + commandAllowed.set(static_cast(scDipAccept - scDipStart)); + } + + myAllowed.reset(); + oppoAllowed.reset(); + myAllowed.set(static_cast(opChoose >> 24)); + myAllowed.set(static_cast(opMoney >> 24)); + oppoAllowed.set(static_cast(opChoose >> 24)); + oppoAllowed.set(static_cast(opMoney >> 24)); + + if (!ClientTools::isCivilReportNew(pContact)) { + myAllowed.set(static_cast(opCivilReport >> 24)); + for (int i = 0; i < nAdv; ++i) + if (ClientTools::myRO->Tech[i] >= tsApplicable) { + myAllowed.set(static_cast(opAllTech >> 24)); + break; + } + oppoAllowed.set(static_cast(opCivilReport >> 24)); + oppoAllowed.set(static_cast(opAllTech >> 24)); + } else { + for (int i = 0; i < nAdv; ++i) + if (!IsFutureTech(i)) { + if (ClientTools::myRO->Tech[i] < tsSeen && + ClientTools::myRO->EnemyReport[pContact]->Tech[i] >= tsApplicable) + oppoAllowed.set(static_cast(opAllTech >> 24)); + else if (ClientTools::myRO->EnemyReport[pContact]->Tech[i] < tsSeen && + ClientTools::myRO->Tech[i] >= tsApplicable) + myAllowed.set(static_cast(opAllTech >> 24)); + } + } + if (!ClientTools::isMilReportNew(pContact)) { + myAllowed.set(static_cast(opMilReport >> 24)); + if (ClientTools::myRO->nModel > 3) + myAllowed.set(static_cast(opAllModel >> 24)); + oppoAllowed.set(static_cast(opMilReport >> 24)); + oppoAllowed.set(static_cast(opAllModel >> 24)); + } else if (auto *dlg = Term::mainScreenModalSelectDlg()) { + if (dlg->onlyChoice(Select::ListKind::ChooseModel) != Select::mixAll) + myAllowed.set(static_cast(opAllModel >> 24)); + if (dlg->onlyChoice(Select::ListKind::ChooseEnemyModel) != Select::mixAll) + oppoAllowed.set(static_cast(opAllModel >> 24)); + } + if (ClientTools::myRO->Treaty[pContact] < trAlliance) { + myAllowed.set(static_cast(opTreaty >> 24)); + myAllowed.set(static_cast(opMap >> 24)); + oppoAllowed.set(static_cast(opTreaty >> 24)); + oppoAllowed.set(static_cast(opMap >> 24)); + } + for (int i = 0; i < nShipPart; ++i) { + if (ClientTools::myRO->Ship[me].Parts[i] > 0) + myAllowed.set(static_cast(opShipParts >> 24)); + if (ClientTools::myRO->Ship[pContact].Parts[i] > 0) + oppoAllowed.set(static_cast(opShipParts >> 24)); + } + + const std::array reportBits = {opAllTech >> 24, opAllModel >> 24, opCivilReport >> 24, + opMilReport >> 24, opMap >> 24}; + for (int bit : reportBits) { + if (Term::dipMem[static_cast(me)].deliveredPrices.test(static_cast(bit))) + myAllowed.reset(static_cast(bit)); + if (Term::dipMem[static_cast(me)].receivedPrices.test(static_cast(bit))) + oppoAllowed.reset(static_cast(bit)); + } +} + +void NegoDlg::paintNationPicture(int x, int y, int p) +{ + if (!HGrSystem) + return; + Dpi::Canvas &canvas = offscreen.canvas(); + canvas.pen().setColor(Qt::black); + canvas.brush().setStyle(Qt::SolidPattern); + canvas.brush().setColor(Tribes::tribe[static_cast(p)]->color); + canvas.rectangle(x - 6, y - 1, x + 70, y + 49); + canvas.brush().setColor(Qt::black); + Tribes::tribe[static_cast(p)]->initAge(ClientTools::getAge(p)); + if (Tribes::tribe[static_cast(p)]->faceHGr) + dump(offscreen, *Tribes::tribe[static_cast(p)]->faceHGr, x, y, 64, 48, + 1 + Tribes::tribe[static_cast(p)]->facePix % 10 * 65, + 1 + Tribes::tribe[static_cast(p)]->facePix / 10 * 49); + else + canvas.fillRect(QRect(x, y, 64, 48)); + canvas.brush().setStyle(Qt::NoBrush); + frame(canvas, x - 1, y - 1, x + 64, y + 48, Qt::black, Qt::black); +} + +void NegoDlg::setButtonStates() +{ + const bool isActionPage = page == history[static_cast(ClientTools::me)].n; + + ui->acceptBtn->setPossible(isActionPage && commandAllowed.test(static_cast(scDipAccept - scDipStart))); + ui->acceptBtn->setLit(dipCommand == scDipAccept); + ui->passBtn->setPossible(isActionPage && commandAllowed.test(static_cast(scDipOffer - scDipStart))); + ui->passBtn->setLit(dipCommand == scDipNotice || + (dipCommand == scDipOffer && currentOffer.nDeliver == 0 && currentOffer.nCost == 0)); + ui->exitBtn->setPossible(isActionPage && commandAllowed.test(static_cast(scDipBreak - scDipStart))); + ui->exitBtn->setLit(dipCommand == scDipBreak); + ui->cancelTreatyBtn->setPossible( + isActionPage && commandAllowed.test(static_cast(scDipCancelTreaty - scDipStart))); + ui->cancelTreatyBtn->setLit(dipCommand == scDipCancelTreaty); + + for (int i = 0; i < 12; ++i) { + const std::size_t priceBit = static_cast(ButtonPrice[static_cast(i)] >> 24); + offerButtons[static_cast(i)]->setPossible(isActionPage && oppoAllowed.test(priceBit)); + offerButtons[static_cast(i)]->setLit(costs[static_cast(i)] != 0xFFFFFFFFu); + wantButtons[static_cast(i)]->setPossible(isActionPage && myAllowed.test(priceBit)); + wantButtons[static_cast(i)]->setLit(delivers[static_cast(i)] != 0xFFFFFFFFu); + } +} + +void NegoDlg::offscreenPaint() +{ + BufferedDrawDlg::offscreenPaint(); + if (!MainTexture || !HGrSystem) + return; + + const int me = ClientTools::me; + const int pContact = Term::dipMem[static_cast(me)].pContact; + History &myHistory = history[static_cast(me)]; + + if (dipCommand >= 0 && page == myHistory.n) + myHistory.text[static_cast(myHistory.n)] = + Diplomacy::dipCommandToString(me, pContact, ClientTools::myRO->Treaty[pContact], IsoEngine::clientMode, + dipCommand, Term::receivedOffer, currentOffer) + .left(255); + + ui->fwdBtn->setVisible(page < myHistory.n); + ui->bwdBtn->setVisible(page >= 2); + bool okEnabled; + if (page < myHistory.n) + okEnabled = false; + else if (dipCommand == scDipOffer) + okEnabled = ClientTools::server(scDipOffer - sExecute, me, 0, ¤tOffer) >= rExecuted; + else + okEnabled = dipCommand >= 0; + ui->okBtn->setVisible(okEnabled); + + Dpi::Canvas &canvas = offscreen.canvas(); + canvas.begin(); + fill(canvas, 3, 3, width() - 6, height() - 6, (MainTexture->width() - width()) / 2, + (MainTexture->height() - height()) / 2); + frame(canvas, 0, 0, width() - 1, height() - 1, Qt::black, Qt::black); + frame(canvas, 1, 1, width() - 2, height() - 2, MainTexture->colorBevelLight, MainTexture->colorBevelShade); + frame(canvas, 2, 2, width() - 3, height() - 3, MainTexture->colorBevelLight, MainTexture->colorBevelShade); + corner(canvas, 1, 1, 0, *MainTexture); + corner(canvas, width() - 9, 1, 1, *MainTexture); + corner(canvas, 1, height() - 9, 2, *MainTexture); + corner(canvas, width() - 9, height() - 9, 3, *MainTexture); + + btnFrame(canvas, ui->okBtn->geometry(), *MainTexture); + btnFrame(canvas, ui->bwdBtn->geometry(), *MainTexture); + btnFrame(canvas, ui->fwdBtn->geometry(), *MainTexture); + btnFrame(canvas, ui->closeBtn->geometry(), *MainTexture); + + rframe(canvas, xPadC - 2, yPadC - 2, xPadC + 41 + 42 * 3, yPadC + 41, colorFromDelphi(0xFFFFFF), + colorFromDelphi(0xB0B0B0)); + rframe(canvas, xPad0 - 2, yPad0 - 2, xPad0 + 41 + 42 * 3, yPad0 + 41 + 42 * 2, colorFromDelphi(0xFFFFFF), + colorFromDelphi(0xB0B0B0)); + rframe(canvas, xPad1 - 2, yPad1 - 2, xPad1 + 41 + 42 * 3, yPad1 + 41 + 42 * 2, colorFromDelphi(0xFFFFFF), + colorFromDelphi(0xB0B0B0)); + + paintNationPicture(xNationPicture0, yNationPicture, pContact); + paintNationPicture(xNationPicture1, yNationPicture, me); + + if (!myHistory.text[static_cast(page - 1)].isEmpty()) { + fillSeamless(canvas, xText0, yText0, wText, hText, 0, 0, paper); + int i = page - 1; + if (myHistory.text[0].isEmpty()) + --i; + if (i < 16) { + canvas.font().assign(romanFont); + canvas.textOut(xText0 + (wText - canvas.textWidth(RomanNo[static_cast(i)])) / 2, + yText0 + (hText - canvas.textExtent(RomanNo[static_cast(i)]).height()) / 2, + RomanNo[static_cast(i)]); + } + } + fillSeamless(canvas, xText1, yText1, wText, hText, 0, 0, paper); + { + int i = page; + if (myHistory.text[0].isEmpty()) + --i; + if (i < 16) { + canvas.font().assign(romanFont); + canvas.textOut(xText1 + (wText - canvas.textWidth(RomanNo[static_cast(i)])) / 2, + yText1 + (hText - canvas.textExtent(RomanNo[static_cast(i)]).height()) / 2, + RomanNo[static_cast(i)]); + } + } + + canvas.brush().setStyle(Qt::SolidPattern); + canvas.brush().setColor(MainTexture->colorBevelShade); + if (!myHistory.text[static_cast(page - 1)].isEmpty()) { + canvas.fillRect(QRect(xText0 + wText, yText0 + PaperShade, PaperShade, hText + PaperShade)); + canvas.fillRect(QRect(xText0 + PaperShade, yText0 + hText, wText + PaperShade - PaperShade, PaperShade)); + } + canvas.fillRect(QRect(xText1 + wText, yText1 + PaperShade, PaperShade, hText + PaperShade)); + canvas.fillRect(QRect(xText1 + PaperShade, yText1 + hText, wText + PaperShade - PaperShade, PaperShade)); + canvas.brush().setStyle(Qt::NoBrush); + + canvas.font().assign(UniFont[static_cast(FontType::Normal)]); + + if (!myHistory.text[static_cast(page - 1)].isEmpty()) + splitText(myHistory.text[static_cast(page - 1)], + QRect(xText0, yText0, xText0 + wText, yText0 + hText)); + if (page < myHistory.n || okEnabled) + splitText(myHistory.text[static_cast(page)], QRect(xText1, yText1, xText1 + wText, yText1 + hText)); + + canvas.font().assign(UniFont[static_cast(FontType::Tiny)]); + int cred = ClientTools::myRO->EnemyReport[pContact]->Credibility; + int kind = cred <= 49 ? 3 : (cred <= 90 ? 0 : 1); + paintProgressBar(canvas, kind, xCred0, yCred0 + 17, (cred + 2) / 5, 0, 20, *MainTexture); + QString s = QString::number(cred); + risedTextOut(canvas, xCred0 + 10 - (biColorTextWidth(canvas, s) + 1) / 2, yCred0, s); + cred = ClientTools::myRO->Credibility; + kind = cred <= 49 ? 3 : (cred <= 90 ? 0 : 1); + paintProgressBar(canvas, kind, xCred1, yCred1 + 17, (cred + 2) / 5, 0, 20, *MainTexture); + s = QString::number(cred); + risedTextOut(canvas, xCred1 + 10 - (biColorTextWidth(canvas, s) + 1) / 2, yCred1, s); + + canvas.end(); + markUsedOffscreen(width(), height()); +} + +void NegoDlg::splitText(const QString &text, const QRect &bounds) +{ + if (!HGrSystem || !Colors) + return; + const QString t = QStringLiteral(" ") + text; // mirrors Pascal's 1-based string indexing + const int length = t.length() - 1; + Dpi::Canvas &canvas = offscreen.canvas(); + int nLines = 0; + for (int previewPass = 1; previewPass >= 0; --previewPass) { + const bool preview = previewPass != 0; + int start = 1; + int line = 0; + int indent = 0; + while (start < length) { + bool dot = false; + if (start == 1 || t[start - 1] == QLatin1Char('\\')) { + if (t[start] == QLatin1Char('-')) { + indent = ListIndent; + ++start; + if (start == length) + break; + dot = true; + } else { + indent = 0; + } + } + int stop = start; + while (stop < length && t[stop] != QLatin1Char('\\')) { + ++stop; + if (biColorTextWidth(canvas, t.mid(start, stop - start + 1)) > + bounds.right() - bounds.left() - PaperBorder_Left - PaperBorder_Right - indent) { + --stop; + break; + } + } + if (stop != length) { + int ordinaryStop = stop; + while (t[ordinaryStop + 1] != QLatin1Char(' ') && t[ordinaryStop + 1] != QLatin1Char('\\')) + --ordinaryStop; + if ((ordinaryStop + 1 - start) * 2 >= stop - start) + stop = ordinaryStop; + } + if (!preview) { + const int y = (bounds.top() + bounds.bottom()) / 2 - 10 * nLines + 20 * line - 1; + if (dot) + sprite(canvas, *HGrSystem, bounds.left() + PaperBorder_Left + (ListIndent - 14), y + 7, 8, 8, 90, + 16); + const QString s = t.mid(start, stop - start + 1); + biColorTextOut(canvas, Colors->image().pixelColor(clkMisc, cliPaperText), colorFromDelphi(0x7F007F), + bounds.left() + PaperBorder_Left + indent, y, s); + } + ++line; + start = stop + 2; + } + nLines = line; + } +} + +void NegoDlg::closeEvent(QCloseEvent *event) +{ + BufferedDrawDlg::closeEvent(event); +} + +void NegoDlg::mousePressEvent(QMouseEvent *event) +{ + const int x = event->position().toPoint().x(); + const int y = event->position().toPoint().y(); + if (x >= xNationPicture0 && x < xNationPicture0 + 64 && y >= yNationPicture && y < yNationPicture + 48) { + if (auto *dlg = Term::mainScreenNatStatDlg()) + dlg->showNewContent(windowModeMakePersistent(windowMode()), + Term::dipMem[static_cast(ClientTools::me)].pContact); + } else if (x >= xNationPicture1 && x < xNationPicture1 + 64 && y >= yNationPicture && y < yNationPicture + 48) { + if (auto *dlg = Term::mainScreenNatStatDlg()) + dlg->showNewContent(windowModeMakePersistent(windowMode()), ClientTools::me); + } +} + +void NegoDlg::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { + if (ui->okBtn->isVisible()) + ui->okBtn->onClick(); + } else { + BufferedDrawDlg::keyPressEvent(event); + } +} + +void NegoDlg::wantClicked(int priceIndex) +{ + const int me = ClientTools::me; + const int pContact = Term::dipMem[static_cast(me)].pContact; + if (page != history[static_cast(me)].n || IsoEngine::clientMode == scDipCancelTreaty || + IsoEngine::clientMode == scDipBreak) + return; + + std::uint32_t price; + if (costs[static_cast(priceIndex)] != 0xFFFFFFFFu) { + price = 0xFFFFFFFFu; // toggle off + } else { + if (currentOffer.nCost >= 2) { + MessgEx::soundMessageEx(phrase(QStringLiteral("MAX2WANTS")), QString()); + return; + } + price = ButtonPrice[static_cast(priceIndex)]; + if (!oppoAllowed.test(static_cast(price >> 24))) + return; + switch (price) { + case static_cast(opCivilReport): + case static_cast(opMilReport): + price += static_cast(pContact << 16) + static_cast(ClientTools::myRO->Turn); + break; + case static_cast(opMoney): { + InputDlg dlg; + dlg.setWindowTitle(phrase(QStringLiteral("TITLE_AMOUNT"))); + dlg.editInput()->setText(QString()); + dlg.centerToRect(geometry()); + if (dlg.showModal() != Dpi::ModalResult::Ok) + return; + bool ok = false; + const int a = dlg.editInput()->text().toInt(&ok); + if (!ok || a <= 0 || a >= MaxMoneyPrice) + return; + price += static_cast(a); + break; + } + case static_cast(opShipParts): { + if (ClientTools::myRO->NatBuilt[imSpacePort] == 0) { + if (auto *dlg = Term::mainScreenMessgExDlg()) { + dlg->openSound = QStringLiteral("WARNING_LOWSUPPORT"); + dlg->messageText = phrase(QStringLiteral("NOSPACEPORT")); + dlg->kind = MessageKind::YesNo; + dlg->iconKind = MessgEx::MessageIconKind::Imp; + dlg->iconIndex = imSpacePort; + if (dlg->showModal() != Dpi::ModalResult::Ok) + return; + } + } + auto *selectDlg = Term::mainScreenModalSelectDlg(); + if (!selectDlg) + return; + selectDlg->showNewContent(WindowMode::Modal, Select::ListKind::EnemyShipPart); + if (selectDlg->result < 0) + return; + price += static_cast(selectDlg->result << 16); + int max = ClientTools::myRO->Ship[pContact].Parts[selectDlg->result]; + InputDlg amountDlg; + amountDlg.setWindowTitle(phrase(QStringLiteral("TITLE_NUMBER"))); + amountDlg.editInput()->setText(QString()); + amountDlg.centerToRect(geometry()); + if (amountDlg.showModal() != Dpi::ModalResult::Ok) + return; + bool ok = false; + int a = amountDlg.editInput()->text().toInt(&ok); + if (!ok || a <= 0) + return; + if (a > max) + a = max; + if (a > MaxShipPartPrice) + a = MaxShipPartPrice; + price += static_cast(a); + break; + } + case static_cast(opAllTech): { + auto *selectDlg = Term::mainScreenModalSelectDlg(); + if (!selectDlg) + return; + selectDlg->showNewContent(WindowMode::Modal, Select::ListKind::ChooseEnemyTech); + if (selectDlg->result < 0) + return; + price = selectDlg->result == Select::adAll + ? static_cast(opAllTech) + : static_cast(opTech) + static_cast(selectDlg->result); + break; + } + case static_cast(opAllModel): { + auto *selectDlg = Term::mainScreenModalSelectDlg(); + if (!selectDlg) + return; + selectDlg->showNewContent(WindowMode::Modal, Select::ListKind::ChooseEnemyModel); + if (selectDlg->result < 0) + return; + price = selectDlg->result == Select::mixAll + ? static_cast(opAllModel) + : static_cast(opModel) + + static_cast((*ClientTools::myRO->EnemyModel)[selectDlg->result].mix); + break; + } + case static_cast(opTreaty): + price = ClientTools::myRO->Treaty[pContact] < trPeace + ? static_cast(opTreaty + trPeace) + : static_cast(opTreaty + ClientTools::myRO->Treaty[pContact] + 1); + break; + default: + break; + } + } + + costs[static_cast(priceIndex)] = price; + buildCurrentOffer(); + dipCommand = scDipOffer; + setButtonStates(); + smartUpdateContent(); +} + +void NegoDlg::offerClicked(int priceIndex) +{ + const int me = ClientTools::me; + const int pContact = Term::dipMem[static_cast(me)].pContact; + if (page != history[static_cast(me)].n || IsoEngine::clientMode == scDipCancelTreaty || + IsoEngine::clientMode == scDipBreak) + return; + + std::uint32_t price; + if (delivers[static_cast(priceIndex)] != 0xFFFFFFFFu) { + price = 0xFFFFFFFFu; // toggle off + } else { + if (currentOffer.nDeliver >= 2) { + MessgEx::soundMessageEx(phrase(QStringLiteral("MAX2OFFERS")), QString()); + return; + } + price = ButtonPrice[static_cast(priceIndex)]; + if (!myAllowed.test(static_cast(price >> 24))) + return; + switch (price) { + case static_cast(opCivilReport): + case static_cast(opMilReport): + price += static_cast(me << 16) + static_cast(ClientTools::myRO->Turn); + break; + case static_cast(opMoney): { + InputDlg dlg; + dlg.setWindowTitle(phrase(QStringLiteral("TITLE_AMOUNT"))); + dlg.editInput()->setText(QString()); + dlg.centerToRect(geometry()); + if (dlg.showModal() != Dpi::ModalResult::Ok) + return; + bool ok = false; + int a = dlg.editInput()->text().toInt(&ok); + if (!ok || a <= 0 || a >= MaxMoneyPrice) + return; + if (a > ClientTools::myRO->Money) + a = ClientTools::myRO->Money; + price += static_cast(a); + break; + } + case static_cast(opShipParts): { + auto *selectDlg = Term::mainScreenModalSelectDlg(); + if (!selectDlg) + return; + selectDlg->showNewContent(WindowMode::Modal, Select::ListKind::ShipPart); + if (selectDlg->result < 0) + return; + price += static_cast(selectDlg->result << 16); + int max = ClientTools::myRO->Ship[me].Parts[selectDlg->result]; + InputDlg amountDlg; + amountDlg.setWindowTitle(phrase(QStringLiteral("TITLE_NUMBER"))); + amountDlg.editInput()->setText(QString()); + amountDlg.centerToRect(geometry()); + if (amountDlg.showModal() != Dpi::ModalResult::Ok) + return; + bool ok = false; + int a = amountDlg.editInput()->text().toInt(&ok); + if (!ok || a <= 0) + return; + if (a > max) + a = max; + if (a > MaxShipPartPrice) + a = MaxShipPartPrice; + price += static_cast(a); + break; + } + case static_cast(opAllTech): { + auto *selectDlg = Term::mainScreenModalSelectDlg(); + if (!selectDlg) + return; + selectDlg->showNewContent(WindowMode::Modal, Select::ListKind::ChooseTech); + if (selectDlg->result < 0) + return; + price = selectDlg->result == Select::adAll + ? static_cast(opAllTech) + : static_cast(opTech) + static_cast(selectDlg->result); + break; + } + case static_cast(opAllModel): { + auto *selectDlg = Term::mainScreenModalSelectDlg(); + if (!selectDlg) + return; + selectDlg->showNewContent(WindowMode::Modal, Select::ListKind::ChooseModel); + if (selectDlg->result < 0) + return; + price = selectDlg->result == Select::mixAll + ? static_cast(opAllModel) + : static_cast(opModel) + static_cast(selectDlg->result); + break; + } + case static_cast(opTreaty): + price = ClientTools::myRO->Treaty[pContact] < trPeace + ? static_cast(opTreaty + trPeace) + : static_cast(opTreaty + ClientTools::myRO->Treaty[pContact] + 1); + break; + default: + break; + } + } + + delivers[static_cast(priceIndex)] = price; + buildCurrentOffer(); + dipCommand = scDipOffer; + setButtonStates(); + smartUpdateContent(); +} + +void NegoDlg::fastClicked(int commandOffset) +{ + if (page != history[static_cast(ClientTools::me)].n) + return; + const int newCommand = commandOffset + scDipStart; + if (!commandAllowed.test(static_cast(commandOffset))) + return; + if (newCommand == scDipCancelTreaty && + ClientTools::myRO->Turn < + ClientTools::myRO->LastCancelTreaty[Term::dipMem[static_cast(ClientTools::me)].pContact] + + CancelTreatyTurns) { + MessgEx::soundMessageEx(phrase(QStringLiteral("CANCELTREATYRUSH")), QString()); + return; + } + if (newCommand == scDipOffer && (IsoEngine::clientMode == scDipCancelTreaty || IsoEngine::clientMode == scDipBreak)) + dipCommand = scDipNotice; + else + dipCommand = newCommand; + resetCurrentOffer(); + setButtonStates(); + smartUpdateContent(); +} + +} // namespace Nego +} // namespace Cevo diff --git a/game/localplayer/Nego.h b/game/localplayer/Nego.h new file mode 100644 index 0000000..0c1a059 --- /dev/null +++ b/game/localplayer/Nego.h @@ -0,0 +1,104 @@ +// 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 +#include +#include + +QT_BEGIN_NAMESPACE +namespace Ui { +class NegoDlg; +} +QT_END_NAMESPACE + +namespace Cevo { +class ButtonN; + +namespace Nego { + +constexpr int MaxHistory = 62; + +// THistory equivalent: one negotiation session's turn-by-turn log text. +struct History { + int n = 0; + std::array text; +}; + +// TNegoDlg equivalent: the diplomacy negotiation screen (want/offer button +// grid, history pages, current-offer building). +class NegoDlg : public BufferedDrawDlg { + Q_OBJECT +public: + explicit NegoDlg(QWidget *parent = nullptr); + ~NegoDlg() override; + + void initiate(); // first turn of negotiation, initiate + void respond(); // first turn of negotiation, respond + void start(); // next turn of negotiation + void offscreenPaint() override; + void showNewContent(WindowMode newMode); + +protected: + void closeEvent(QCloseEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + +private: + void resetCurrentOffer(); + void buildCurrentOffer(); + void findAllowed(); + void splitText(const QString &text, const QRect &bounds); + void paintNationPicture(int x, int y, int p); + void setButtonStates(); + void wantClicked(int priceIndex); + void offerClicked(int priceIndex); + void fastClicked(int commandOffset); + + int page = 0; + int dipCommand = -1; + TOffer currentOffer{}; + std::bitset<256> myAllowed; + std::bitset<256> oppoAllowed; + std::bitset<256> commandAllowed; // indexed by (scDipXxx - scDipStart) + std::array history; + Dpi::Font romanFont; + std::array costs{}; + std::array delivers{}; + std::array wantButtons{}; + std::array offerButtons{}; + + Ui::NegoDlg *ui; +}; + +} // namespace Nego +} // namespace Cevo diff --git a/game/localplayer/Nego.ui b/game/localplayer/Nego.ui new file mode 100644 index 0000000..12ee56e --- /dev/null +++ b/game/localplayer/Nego.ui @@ -0,0 +1,165 @@ + + + + NegoDlg + + + + 0 + 0 + 640 + 480 + + + + C-evo + + + + 42644610025 + + + + + 134462525 + + + + + 6024462525 + + + + + 60262525 + + + + 139124242 + + + 181124242 + + + 223124242 + + + 265124242 + + + 139544242 + + + 181544242 + + + 223544242 + + + 265544242 + + + 139964242 + + + 181964242 + + + 223964242 + + + 265964242 + + + 333124242 + + + 375124242 + + + 417124242 + + + 459124242 + + + 333544242 + + + 375544242 + + + 417544242 + + + 459544242 + + + 333964242 + + + 375964242 + + + 417964242 + + + 459964242 + + + 1394264242 + + + 1814264242 + + + 2234264242 + + + 2654264242 + + + + + Cevo::ButtonA + QWidget +
Cevo/CevoButtonA.h
+
+ + Cevo::ButtonB + QWidget +
Cevo/CevoButtonB.h
+
+ + Cevo::ButtonN + QWidget +
Cevo/CevoButtonN.h
+
+
+ +
diff --git a/game/localplayer/Term.cpp b/game/localplayer/Term.cpp index c1cefe7..7c0198b 100644 --- a/game/localplayer/Term.cpp +++ b/game/localplayer/Term.cpp @@ -175,6 +175,10 @@ std::function mainScreenRepaintAll = [] {}; std::function modalSelectDlgShowCityProject = [](int) { return -1; }; std::function listDlgRenameCity = [](int) { return false; }; std::function mainScreenMessgExDlg = [] { return static_cast(nullptr); }; +std::function mainScreenModalSelectDlg = [] { return static_cast(nullptr); }; +std::function mainScreenNatStatDlg = [] { return static_cast(nullptr); }; +std::function mainScreenOfferCall = [](const TOffer &) { return eInvalid; }; +TOffer receivedOffer{}; } // namespace Term } // namespace Cevo diff --git a/game/localplayer/Term.h b/game/localplayer/Term.h index f2a8a67..71e45a0 100644 --- a/game/localplayer/Term.h +++ b/game/localplayer/Term.h @@ -33,10 +33,13 @@ #include "Cevo/Protocol.h" #include "ClientTools.h" #include "MessgEx.h" +#include "NatStat.h" +#include "Select.h" #include #include +#include #include #include @@ -109,13 +112,23 @@ extern ClientTools::SaveOptions optionChecked; // just observing rather than playing a nation). extern bool supervising; -// Term.pas's `TDipMem` (per-nation negotiation session memory) - only the -// one field (`pContact`) anything needs so far. +// Term.pas's `TDipMem` (per-nation negotiation session memory). struct DipMemEntry { int pContact = -1; + // Nego.pas's own negotiation-session bookkeeping (added for Nego.pas). + int formerTreaty = trNone; + int sentCommand = 0; + TOffer sentOffer{}; + // TPriceSet = set of $00..$FF (price-category bytes, i.e. opXxx >> 24). + std::bitset<256> deliveredPrices; + std::bitset<256> receivedPrices; }; extern std::array dipMem; +// Term.pas's `ReceivedOffer: TOffer;` (the opponent's current negotiation +// offer, as last received from the server). +extern TOffer receivedOffer; + // Term.pas's RoughCredibility(): buckets a 0..100 credibility value into a // 4-level rating (0..3) for display. int roughCredibility(int credibility); @@ -227,6 +240,14 @@ extern std::function listDlgRenameCity; // Mirrors `MainScreen.MessgExDlg` dialog access - returns nullptr by // default until Term.pas creates a real instance. extern std::function mainScreenMessgExDlg; +// Mirrors `MainScreen.ModalSelectDlg`/`MainScreen.NatStatDlg` dialog +// access (the general, non-city-project form - see also +// modalSelectDlgShowCityProject above for the city-project-only variant). +extern std::function mainScreenModalSelectDlg; +extern std::function mainScreenNatStatDlg; +// Mirrors `MainScreen.OfferCall(var Offer: TOffer): Integer` - returns +// eInvalid by default. +extern std::function mainScreenOfferCall; } // namespace Term } // namespace Cevo