// 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. // TMainScreen.Client() (Term.pas): the command dispatcher the server calls // for the human player. // // Fully ported: every command handled by the original's top-level case // statement is handled here too, including the cShowXxx move/combat // feedback commands (delegated to clientShow(), see Term.MainScreen.Show.cpp) // and cRefreshDebugMap. #include "Term.MainScreen.h" #include "Cevo/CevoGraphicSet.h" #include "Cevo/CevoScreenTools.h" #include "Cevo/CevoTexture.h" #include "Cevo/CmdList.h" #include "Global.h" #include "Messg.h" #include "Sound.h" #include "Tribes.h" #include #include #include #include #include namespace Cevo { namespace Term { static_assert(std::is_trivially_copyable_v, "PersistentData lives in server-allocated raw memory (and savegames)"); namespace { QString negoOfferText(const TShowNegoData &nego) { const TOffer &offer = nego.Offer; QString s = QStringLiteral("P%1 to P%2: ").arg(nego.pSender).arg(nego.pTarget); if (nego.Action == scDipOffer && offer.nDeliver + offer.nCost > 0) { s += QStringLiteral("Offer "); for (int i = 0; i < offer.nDeliver + offer.nCost; ++i) { if (i == offer.nDeliver) s += QStringLiteral(" for "); else if (i > 0) s += QLatin1Char('+'); const int price = offer.Price[i]; const int value = price & 0xFFFFFF; switch (price & opMask) { case opChoose: s += QStringLiteral("Price of choice"); break; case opCivilReport: s += QStringLiteral("State report"); break; case opMilReport: s += QStringLiteral("Military report"); break; case opMap: s += QStringLiteral("Map"); break; case opTreaty: s += QStringLiteral("Treaty"); break; case opShipParts: s += QStringLiteral("Ship part"); break; case opMoney: s += QString::number(value) + QLatin1Char('o'); break; case opTribute: s += QString::number(value) + QStringLiteral("o tribute"); break; case opTech: s += phrases.lookup(QStringLiteral("ADVANCES"), value); break; case opAllTech: s += QStringLiteral("All advances"); break; case opModel: s += Tribes::tribe[static_cast(nego.pSender)]->modelNameAt(value); break; case opAllModel: s += QStringLiteral("All models"); break; default: break; } } } else if (nego.Action == scDipAccept) { s += QStringLiteral("--- ACCEPTED! ---"); } else { return QString(); } return s; } // Initial per-player persistent data of a new game. void initPersistentData(PersistentData &data) { const TNewGameData &g = ClientTools::g; data.farTech = adNone; for (auto &row : data.enhancementJobs) std::fill(std::begin(row), std::end(row), static_cast(jNone)); for (auto &order : data.impOrder) order.fill(-1); data.coldWarStart = -ColdWarTurns - 1; data.toldAge = -1; data.toldModels = 3; data.toldAlive = 0; data.toldContact = 0; data.toldOwnCredibility = InitialCredibility; for (int i = 0; i < nPl; ++i) { if (g.Difficulty[i] > 0) data.toldAlive += 1 << i; } data.peaceEvaHappened = 0; for (auto &wonder : data.toldWonders) { wonder.CityID = -1; wonder.EffectiveOwner = -1; } data.toldTech.fill(static_cast(tsNA)); } } // namespace void MainScreen::client(int command, int newPlayer, void *data) { TNewGameData &g = ClientTools::g; const auto intData = [data] { return *static_cast(data); }; const auto isLocalSession = [] { return !ClientTools::isMultiPlayerGame(); }; if (command == cTurn || command == cResume || command == cContinue || command == cMovieTurn || command == scContact || (command >= scDipStart && command <= scDipBreak)) { supervising = g.Difficulty[newPlayer] == 0; arrangeMidPanel(); } switch (command) { case cDebugMessage: logDlg().add(newPlayer, g.RO[0]->Turn, QString::fromLocal8Bit(static_cast(data))); return; case cShowNego: { const QString text = negoOfferText(*static_cast(data)); if (!text.isEmpty()) logDlg().add(newPlayer, g.RO[0]->Turn, text); return; } case cInitModule: { auto *initData = static_cast(data); ClientTools::server = initData->Server; initModule(); initData->DataSize = sizeof(PersistentData); initData->Flags = aiThreaded; return; } case cReleaseModule: doneModule(); return; case cHelpOnly: case cStartHelp: case cStartCredits: { age = 0; if (command == cHelpOnly) MainTexture->setAge(-1); Tribes::init(); const QRect screen = Dpi::Screen::primaryMonitor().boundsRect(); helpDlg().userLeft = screen.left() + (screen.width() - helpDlg().width()) / 2; helpDlg().userTop = screen.top() + (screen.height() - helpDlg().height()) / 2; helpDlg().difficulty = 0; helpDlg().showNewContent(WindowMode::Modal, Help::LinkCategory::Misc, static_cast(command == cStartCredits ? Help::MiscLinkIndex::Credits : Help::MiscLinkIndex::Main)); Tribes::done(); return; } case cNewGame: case cLoadGame: case cMovie: case cNewMap: // generateNames follows the "Names" menu item (mNames), which isn't ported: keep the current setting. m_gameOK = true; g = *static_cast(data); ClientTools::me = -1; m_pLogo = -1; setClientMode(-1); setMapOptions(); m_mainMap->pDebugMap = -1; m_idle = false; m_jump.fill(0); if (m_startRunning) m_jump[0] = 999999; gameMode = command; grExt.resetPixUsed(); m_mainMap->reset(); m_noMap->reset(); Tribes::init(); getTribeList(); for (int p1 = 0; p1 < nPl; ++p1) { if (g.RO[p1] && g.RO[p1]->Data) initPersistentData(*static_cast(g.RO[p1]->Data)); } arrangeDialogs(); age = 0; m_movieSpeed = 1; logDlg().setSlotItemVisible(true); logDlg().host = this; if (m_helpDlg) m_helpDlg->clearHistory(); if (m_cityDlg) m_cityDlg->reset(); m_miniMap->size = QPoint(g.lx, g.ly); m_miniMap->bitmap.setSize(m_miniMap->size.x() * 2, m_miniMap->size.y()); for (int i = 0; i < nPl; ++i) { Tribes::tribe[static_cast(i)].reset(); tribeOriginal[static_cast(i)] = false; } toldSlavery = -1; m_repaintOnResize = false; m_closable = false; m_firstMovieTurn = true; m_menuArea->setVisible(gameMode != cMovie); m_treasuryArea->setVisible(gameMode < cMovie); m_researchArea->setVisible(gameMode < cMovie); m_managementArea->setVisible(gameMode < cMovie); return; case cGetReady: case cReplay: if (newPlayer == 0) { int missing = 0; for (int p1 = 0; p1 < nPl; ++p1) { if (g.Difficulty[p1] > 0 && !Tribes::tribe[static_cast(p1)]) ++missing; } if (missing > static_cast(unusedTribeFiles.size())) { m_gameOK = false; simpleMessage(phrases.lookup(QStringLiteral("TOOFEWTRIBES"))); } else { const auto setTribe = [&](int trix, const QString &fileName) { if (gameMode == cLoadGame) { createTribe(trix, fileName, false); } else { TribeInfo info; info.trix = trix; info.fileName = fileName; QByteArray packed = info.pack(); ClientTools::server(commandWithData(cSetTribe, info.commandDataSize()), 0, 0, packed.data()); } }; for (int p1 = 0; p1 < nPl; ++p1) { // let player select own tribes if (!(g.Difficulty[p1] > 0 && !Tribes::tribe[static_cast(p1)] && g.RO[p1])) continue; tribeNames.clear(); for (const UnusedTribe &unused : unusedTribeFiles) { QString name; QColor color; Tribes::getTribeInfo(unused.fileName, name, color); tribeNames.push_back({name, color}); } Q_ASSERT(!tribeNames.empty()); modalSelectDlg().showNewContent(WindowMode::Modal, Select::ListKind::Tribe); QCoreApplication::processEvents(); const int chosen = modalSelectDlg().result; const QString fileName = unusedTribeFiles[static_cast(chosen)].fileName; unusedTribeFiles.erase(unusedTribeFiles.begin() + chosen); setTribe(p1, fileName); } for (int p1 = 0; p1 < nPl; ++p1) { // autoselect enemy tribes if (!(g.Difficulty[p1] > 0 && !Tribes::tribe[static_cast(p1)] && !g.RO[p1])) continue; const int chosen = chooseUnusedTribe(); const QString fileName = unusedTribeFiles[static_cast(chosen)].fileName; unusedTribeFiles.erase(unusedTribeFiles.begin() + chosen); setTribe(p1, fileName); } } if (!generateNames) { for (int p1 = 0; p1 < nPl; ++p1) { if (Tribes::tribe[static_cast(p1)]) Tribes::tribe[static_cast(p1)]->numberName = p1; } } } return; case cBreakGame: saveSettings(); saveConfig(); if (m_cityDlg) m_cityDlg->closeAction = CityScreen::CityCloseAction::None; applyToVisibleForms(FormAction::Close); if (logDlg().isVisible()) logDlg().close(); logDlg().clear(); m_startRunning = !m_idle && m_jump[0] > 0; // AI called Reload ClientTools::me = -1; m_idle = false; setClientMode(-1); m_unitInfoBtn->setVisible(false); m_unitBtn->setVisible(false); m_terrainBtn->setVisible(false); for (ButtonB *button : m_movieSpeedBtn) button->setVisible(false); m_eot->setVisible(false); for (ButtonC *button : { m_mapBtn0, m_mapBtn1, m_mapBtn4, m_mapBtn5, m_mapBtn6 }) button->setVisible(false); if (m_verticalScrollBar) m_verticalScrollBar->init(0, 1); for (auto &tribe : Tribes::tribe) tribe.reset(); Tribes::done(); m_repaintOnResize = false; m_closable = true; close(); return; case cShowGame: for (Dpi::Bitmap *bitmap : { &m_panel, &m_topBar }) { Dpi::Canvas &canvas = bitmap->canvas(); canvas.brush().setColor(Qt::black); canvas.fillRect(QRect(0, 0, bitmap->width(), bitmap->height())); canvas.brush().setStyle(Qt::NoBrush); canvas.end(); } if (fullScreen) { m_restoredBounds = termBounds; m_formFullScreen = true; showFullScreen(); } else { m_formFullScreen = false; setGeometry(termBounds); show(); } updateNow(); resizeControls(); m_repaintOnResize = true; m_xw = 0; m_yw = m_ywcenter; return; case cShowTurnChange: if (intData() >= 0) { m_pLogo = intData(); update(m_xRightPanel + 10 - (16 + 64), height() - PanelHeight, 64, 64); } return; case cTurn: case cResume: case cContinue: if (!m_gameOK) { ClientTools::server(sResign, newPlayer, 0, nullptr); return; } setClientMode(command); m_pTurn = newPlayer; m_pLogo = newPlayer; if (command == cResume) { // init non-original model pictures (maybe tribes not found) for (int p1 = 0; p1 < nPl; ++p1) { if (!g.RO[p1]) continue; ClientTools::itsMeAgain(p1); for (int mix = 0; mix < ClientTools::myRO->nModel; ++mix) { if (!Tribes::tribe[static_cast(ClientTools::me)]->modelPictureAt(mix).hGr) initMyModel(mix, true); } } ClientTools::me = -1; } if (m_jump[static_cast(m_pTurn)] > 0) QCoreApplication::processEvents(); if (m_jump[static_cast(m_pTurn)] > 0) { if ((g.RO[newPlayer]->Happened & phGameEnd) != 0) m_jump[static_cast(m_pTurn)] = 0; else --m_jump[static_cast(m_pTurn)]; } { bool skipTurn = m_jump[static_cast(m_pTurn)] > 0; if (skipTurn) { ClientTools::itsMeAgain(newPlayer); myData = static_cast(g.RO[newPlayer]->Data); setTroopLoc(-1); miniMapPaint(); initAllEnemyModels(); // necessary for correct replay if (!endTurn(true)) skipTurn = false; } if (!skipTurn) { if ((clientMode < scDipStart || clientMode > scDipBreak) && m_negoDlg && m_negoDlg->isVisible()) m_negoDlg->close(); m_skipped = false; // always show my moves during my turn m_idle = true; initTurn(newPlayer); dipMem[static_cast(ClientTools::me)].pContact = -1; } } return; case cMovieTurn: setClientMode(command); m_pTurn = newPlayer; m_pLogo = -1; m_skipped = false; // always show my moves during my turn m_idle = true; if (m_firstMovieTurn) { checkMovieSpeedBtnState(); m_firstMovieTurn = false; } initTurn(newPlayer); QCoreApplication::processEvents(); if (m_movieSpeed == 4) { QThread::msleep(75); // this break will ensure speed of fast forward does not depend on cpu speed QCoreApplication::processEvents(); } return; case cMovieEndTurn: rememberPeaceViolation(); m_pTurn = -1; m_pLogo = -1; m_mapValid = false; setClientMode(-1); m_idle = false; m_skipped = false; return; case cEditMap: setClientMode(cEditMap); setMapOptions(); m_mainMap->pDebugMap = -1; ClientTools::itsMeAgain(0); myData = nullptr; m_unitInfoBtn->setVisible(false); m_unitBtn->setVisible(false); m_terrainBtn->setVisible(false); for (ButtonB *button : m_movieSpeedBtn) button->setVisible(false); m_eot->setVisible(false); helpDlg().difficulty = 0; m_brushType = fGrass; m_brushLoc = -1; m_edited = false; IsoEngine::unFocus = -1; m_markCityLoc = -1; m_tracking = false; m_turnComplete = false; m_mapValid = false; resizeControls(); setTroopLoc(-1); m_idle = true; return; case scContact: dipMem[static_cast(newPlayer)].pContact = intData(); if (m_jump[static_cast(newPlayer)] > 0) { dipCall(scReject); } else { setClientMode(command); initTurn(newPlayer); myData->toldContact |= 1 << intData(); // don't tell about new nation when already contacted by them MessgEx::MessgExDlg &dlg = messgExDlg(); dlg.openSound = QStringLiteral("CONTACT_%1").arg(ClientTools::myRO->EnemyReport[intData()]->Attitude); dlg.messageText = Tribes::tribe[static_cast(intData())]->tPhrase(QStringLiteral("FRCONTACT")); dlg.kind = MessageKind::YesNo; dlg.iconKind = MessgEx::MessageIconKind::Tribe; dlg.iconIndex = intData(); if (dlg.showModal() == Dpi::ModalResult::Ok) { negoDlg().respond(); DipMemEntry &mem = dipMem[static_cast(ClientTools::me)]; mem.deliveredPrices.reset(); mem.receivedPrices.reset(); dipCall(scDipStart); } else { dipCall(scReject); endNego(); } } return; case scDipStart: case scDipNotice: case scDipAccept: case scDipCancelTreaty: case scDipOffer: case scDipBreak: setClientMode(command); initTurn(newPlayer); if (command == scDipOffer) { receivedOffer = *static_cast(data); initAllEnemyModels(); } if (command == scDipStart) Sound::play(QStringLiteral("CONTACT_%1").arg(ClientTools::myRO->Attitude[dipMem[static_cast(newPlayer)].pContact])); else if (command == scDipCancelTreaty) Sound::play(QStringLiteral("CANCELTREATY")); if (command == scDipAccept) { // remember delivered and received prices DipMemEntry &mem = dipMem[static_cast(ClientTools::me)]; for (int i = 0; i < mem.sentOffer.nDeliver; ++i) mem.deliveredPrices.set(static_cast(mem.sentOffer.Price[i] >> 24)); for (int i = 0; i < mem.sentOffer.nCost; ++i) mem.receivedPrices.set(static_cast(mem.sentOffer.Price[mem.sentOffer.nDeliver + i] >> 24)); bool isTreatyDeal = false; for (int i = 0; i < receivedOffer.nDeliver + receivedOffer.nCost; ++i) { if ((mem.sentOffer.Price[i] & opMask) == opTreaty) { isTreatyDeal = true; break; } } Sound::play(isTreatyDeal ? QStringLiteral("NEWTREATY") : QStringLiteral("ACCEPTOFFER")); } negoDlg().start(); m_idle = true; return; case cShowCancelTreaty: if (isLocalSession()) { const int other = intData(); QString key; switch (g.RO[newPlayer]->Treaty[other]) { case trPeace: key = QStringLiteral("FRCANCELBYREJECT_PEACE"); break; case trFriendlyContact: key = QStringLiteral("FRCANCELBYREJECT_FRIENDLY"); break; case trAlliance: key = QStringLiteral("FRCANCELBYREJECT_ALLIANCE"); break; default: break; } MessgEx::tribeMessage(other, Tribes::tribe[static_cast(other)]->tPhrase(key), QStringLiteral("CANCELTREATY")); } return; case cShowCancelTreatyByAlliance: if (m_idle && newPlayer == ClientTools::me) { MessgEx::tribeMessage(intData(), Tribes::tribe[static_cast(intData())]->tPhrase( QStringLiteral("FRENEMYALLIANCE")), QStringLiteral("CANCELTREATY")); } return; case cShowSupportAllianceAgainst: if (isLocalSession() && m_jump[0] == 0) { const int ally = intData() & 0xF; const int enemy = intData() >> 4; MessgEx::tribeMessage(ally, Tribes::tribe[static_cast(ally)]->tPhrase(QStringLiteral("FRMYALLIANCE1")) + QLatin1Char(' ') + Tribes::tribe[static_cast(enemy)]->tPhrase( QStringLiteral("FRMYALLIANCE2")), QStringLiteral("CANCELTREATY")); } return; case cShowPeaceViolation: if (isLocalSession() && m_jump[0] == 0) { MessgEx::tribeMessage( intData(), Tribes::tribe[static_cast(intData())]->tPhrase(QStringLiteral("EVIOLATION")).arg( turnToString(ClientTools::myRO->Turn + PeaceEvaTurns - 1)), QStringLiteral("MSG_WITHDRAW")); } return; case cShowUnitChanged: case cShowCityChanged: case cShowAfterMove: case cShowAfterAttack: case cShowMoving: case cShowCapturing: case cShowAttacking: case cRefreshDebugMap: clientShow(command, newPlayer, data); return; case cShowEndContact: endNego(); return; case cShowMissionResult: { const auto info = *static_cast(data); if (info == 0) { MessgEx::soundMessageEx(phrases.lookup(QStringLiteral("NOFOREIGNINFO")), QString()); } else { QString s = phrases.lookup(QStringLiteral("FOREIGNINFO")); for (int p1 = 0; p1 < nPl; ++p1) { if ((3u << (p1 * 2)) & info) s += QLatin1Char('\\') + Tribes::tribe[static_cast(p1)]->tPhrase(QStringLiteral("SHORTNAME")); } MessgEx::soundMessageEx(s, QString()); } return; } case cShowShipChange: if (isLocalSession() && m_jump[0] == 0) showEnemyShipChange(*static_cast(data)); return; case cShowGreatLibTech: if (isLocalSession() && m_jump[0] == 0) { MessgEx::MessgExDlg &dlg = messgExDlg(); dlg.messageText = formatPhrase(QStringLiteral("GRLIB_GENERAL"), { phrases.lookup(QStringLiteral("ADVANCES"), intData()) }); dlg.openSound = QStringLiteral("NEWADVANCE_GRLIB"); dlg.kind = MessageKind::Ok; dlg.iconKind = MessgEx::MessageIconKind::Imp; dlg.iconIndex = woGrLibrary; dlg.showModal(); } return; default: break; } if (command >= cClientEx) { switch (command & ~static_cast(CommandDataElementCountMask)) { case cSetTribe: { const TribeInfo info = unpackTribeInfo(data); const auto it = std::find_if(unusedTribeFiles.begin(), unusedTribeFiles.end(), [&](const UnusedTribe &unused) { return unused.fileName == info.fileName; }); if (it != unusedTribeFiles.end()) unusedTribeFiles.erase(it); createTribe(info.trix, info.fileName, true); return; } case cSetNewModelPicture: case cSetModelPicture: { const Tribes::ModelPictureInfo info = unpackModelPictureInfo(data); if (tribeOriginal[static_cast(info.trix)]) { Tribes::tribe[static_cast(info.trix)]->setModelPicture( info, (command & ~static_cast(CommandDataElementCountMask)) == cSetNewModelPicture); } return; } case cSetSlaveIndex: Tribes::tribe[static_cast(intData() >> 16)]->mixSlaves = intData() & 0xFFFF; return; case cSetCityName: { const CityNameInfo info = unpackCityNameInfo(data); if (tribeOriginal[static_cast(info.id >> 12)]) Tribes::tribe[static_cast(info.id >> 12)]->setCityName(info.id & 0xFFF, info.newName); return; } case cSetModelName: { const ModelNameInfo info = unpackModelNameInfo(data); if (tribeOriginal[static_cast(newPlayer)]) Tribes::tribe[static_cast(newPlayer)]->modelNameAt(info.mix) = info.newName; return; } default: break; } } } } // namespace Term } // namespace Cevo