From 199cf2479c705648d029cd743af9ec98b88083e6 Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Tue, 3 Nov 2026 11:02:44 +0100 Subject: [PATCH] added tribes and keybinding --- CMakeLists.txt | 9 + game/Global.cpp | 10 + game/Global.h | 11 + game/localplayer/KeyBindings.cpp | 466 ++++++++++++++++++++++++++ game/localplayer/KeyBindings.h | 197 +++++++++++ game/localplayer/Tribes.cpp | 552 +++++++++++++++++++++++++++++++ game/localplayer/Tribes.h | 138 ++++++++ 7 files changed, 1383 insertions(+) create mode 100644 game/localplayer/KeyBindings.cpp create mode 100644 game/localplayer/KeyBindings.h create mode 100644 game/localplayer/Tribes.cpp create mode 100644 game/localplayer/Tribes.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ae7f44b..bcc10e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,6 +115,10 @@ set(PROJECT_SOURCES game/Messg.cpp game/Messg.h game/Messg.ui + game/localplayer/KeyBindings.cpp + game/localplayer/KeyBindings.h + game/localplayer/Tribes.cpp + game/localplayer/Tribes.h ${TS_FILES} ) @@ -147,6 +151,11 @@ endif() target_link_libraries(c-evo-evo PRIVATE Qt${QT_VERSION_MAJOR}::Widgets dpiwidgets cevowidgets uosmedia cevocommon cevogame cevoai) +# Lets flat game/ files use simple quoted includes (e.g. "Global.h", +# "localplayer/Tribes.h") regardless of which game/ subdirectory the +# including file itself lives in. +target_include_directories(c-evo-evo PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/game) + # dpiwidgets/cevowidgets/uosmedia are installed to lib/c-evo-evo (not the # default library directory), so the installed binary needs an explicit # rpath to find them at runtime. diff --git a/game/Global.cpp b/game/Global.cpp index e60d175..2bd2071 100644 --- a/game/Global.cpp +++ b/game/Global.cpp @@ -32,6 +32,7 @@ namespace Cevo { bool fullScreen = false; bool musicEnabled = true; +bool generateNames = true; QString getGraphicsDir() { @@ -51,4 +52,13 @@ QString getMusicDir() #endif } +QString localizedFilePath(const QString &path) +{ +#ifdef CEVO_RESOURCE_DIR + return QStringLiteral(CEVO_RESOURCE_DIR "/") + path; +#else + return path; +#endif +} + } // namespace Cevo diff --git a/game/Global.h b/game/Global.h index a58a6d2..9274aa5 100644 --- a/game/Global.h +++ b/game/Global.h @@ -64,6 +64,17 @@ QString getGraphicsDir(); // GetMusicDir(). QString getMusicDir(); +// Minimal equivalent of Directories.pas's LocalizedFilePath(): always +// resolves to CEVO_RESOURCE_DIR/path, since localization (StringTables.pas +// and LocaleCode handling) isn't ported - this is the non-localized ("en") +// fallback branch the original always takes once no localized file exists. +QString localizedFilePath(const QString &path); + +// Equivalent of a user-settings `GenerateNames` flag (Start.pas - not +// ported - whether cities get tribe-script-generated names or plain +// "Founder.Index" numeric ones). +extern bool generateNames; + // ScreenTools.pas's PngExt/JpgExt constants. constexpr const char *PngExt = ".png"; constexpr const char *JpgExt = ".jpg"; diff --git a/game/localplayer/KeyBindings.cpp b/game/localplayer/KeyBindings.cpp new file mode 100644 index 0000000..eaf0738 --- /dev/null +++ b/game/localplayer/KeyBindings.cpp @@ -0,0 +1,466 @@ +// 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 "KeyBindings.h" + +#include +#include + +#include + +namespace Cevo { +namespace KeyBindings { + +bool KeyBinding::test(const QKeySequence &aShortCut) const +{ + return aShortCut == shortCut || aShortCut == shortCut2; +} + +void KeyBinding::assign(const KeyBinding &source) +{ + shortName = source.shortName; + fullName = source.fullName; + shortCut = source.shortCut; + shortCut2 = source.shortCut2; + defaultShortCut = source.defaultShortCut; + defaultShortCut2 = source.defaultShortCut2; +} + +void KeyBinding::setDefault() +{ + shortCut = defaultShortCut; + shortCut2 = defaultShortCut2; +} + +KeyBinding &KeyBindingList::addItem(const QString &shortName, const QString &fullName, const QKeySequence &shortCut, + const QKeySequence &shortCut2) +{ + auto item = std::make_unique(); + item->shortName = shortName; + item->fullName = fullName; + item->shortCut = shortCut; + item->shortCut2 = shortCut2; + item->defaultShortCut = shortCut; + item->defaultShortCut2 = shortCut2; + m_items.push_back(std::move(item)); + return *m_items.back(); +} + +KeyBinding &KeyBindingList::addItem(const QString &shortName, const QString &fullName, const QString &shortCutText, + const QString &shortCutText2) +{ + return addItem(shortName, fullName, textToShortCut(shortCutText), textToShortCut(shortCutText2)); +} + +KeyBinding *KeyBindingList::search(const QString &shortName) const +{ + for (const auto &item : m_items) + if (item->shortName == shortName) + return item.get(); + return nullptr; +} + +void KeyBindingList::loadFromSettings(QSettings &settings, const QString &group) +{ + settings.beginGroup(group); + for (auto &item : m_items) { + if (settings.contains(item->shortName)) { + const QString text = settings.value(item->shortName).toString(); + const int commaPos = text.indexOf(QLatin1Char(',')); + if (commaPos >= 0) { + item->shortCut2 = textToShortCut(text.mid(commaPos + 1)); + item->shortCut = textToShortCut(text.left(commaPos)); + } else { + item->shortCut = textToShortCut(text); + item->shortCut2 = QKeySequence(); + } + } else { + QString text = shortCutToText(item->shortCut); + if (!item->shortCut2.isEmpty()) + text += QLatin1Char(',') + shortCutToText(item->shortCut2); + settings.setValue(item->shortName, text); + } + } + settings.endGroup(); +} + +void KeyBindingList::saveToSettings(QSettings &settings, const QString &group) const +{ + settings.beginGroup(group); + for (const auto &item : m_items) { + QString text = shortCutToText(item->shortCut); + if (!item->shortCut2.isEmpty()) + text += QLatin1Char(',') + shortCutToText(item->shortCut2); + settings.setValue(item->shortName, text); + } + settings.endGroup(); +} + +QStringList KeyBindingList::toDisplayStrings() const +{ + QStringList result; + result.reserve(count()); + for (const auto &item : m_items) { + QString text; + if (!item->shortCut.isEmpty()) + text += shortCutToText(item->shortCut); + if (!item->shortCut2.isEmpty()) { + if (!text.isEmpty()) + text += QLatin1String(", "); + text += shortCutToText(item->shortCut2); + } + result.append(text.isEmpty() ? item->fullName : item->fullName + QLatin1String(" (") + text + QLatin1Char(')')); + } + return result; +} + +void KeyBindingList::assign(const KeyBindingList &source) +{ + while (count() < source.count()) + m_items.push_back(std::make_unique()); + while (count() > source.count()) + m_items.pop_back(); + for (int i = 0; i < count(); ++i) + (*this)[i].assign(source[i]); +} + +void KeyBindingList::resetToDefault() +{ + for (auto &item : m_items) + item->setDefault(); +} + +void KeyBindingList::removeShortCut(const QKeySequence &shortCut) +{ + for (auto &item : m_items) { + if (item->shortCut == shortCut) + item->shortCut = QKeySequence(); + if (item->shortCut2 == shortCut) + item->shortCut2 = QKeySequence(); + } +} + +void KeyBindingList::sortAlpha() +{ + std::sort(m_items.begin(), m_items.end(), + [](const std::unique_ptr &a, const std::unique_ptr &b) { + return a->fullName.compare(b->fullName) < 0; + }); +} + +namespace { + +// LCL's Num0-Num9/NumPlus/.../NumEnter key names have no direct QKeySequence +// equivalent (Qt expresses numpad keys as a plain key name plus +// Qt::KeypadModifier); these tables translate just that difference. +const QHash &numpadAliasToBaseName() +{ + static const QHash table = { + {QStringLiteral("Num0"), QStringLiteral("0")}, {QStringLiteral("Num1"), QStringLiteral("1")}, + {QStringLiteral("Num2"), QStringLiteral("2")}, {QStringLiteral("Num3"), QStringLiteral("3")}, + {QStringLiteral("Num4"), QStringLiteral("4")}, {QStringLiteral("Num5"), QStringLiteral("5")}, + {QStringLiteral("Num6"), QStringLiteral("6")}, {QStringLiteral("Num7"), QStringLiteral("7")}, + {QStringLiteral("Num8"), QStringLiteral("8")}, {QStringLiteral("Num9"), QStringLiteral("9")}, + {QStringLiteral("NumPlus"), QStringLiteral("+")}, {QStringLiteral("NumMinus"), QStringLiteral("-")}, + {QStringLiteral("NumTimes"), QStringLiteral("*")}, {QStringLiteral("NumDivide"), QStringLiteral("/")}, + {QStringLiteral("NumEnter"), QStringLiteral("Enter")}, + }; + return table; +} + +const QHash &baseKeyToNumpadAlias() +{ + static const QHash table = { + {Qt::Key_0, QStringLiteral("Num0")}, {Qt::Key_1, QStringLiteral("Num1")}, + {Qt::Key_2, QStringLiteral("Num2")}, {Qt::Key_3, QStringLiteral("Num3")}, + {Qt::Key_4, QStringLiteral("Num4")}, {Qt::Key_5, QStringLiteral("Num5")}, + {Qt::Key_6, QStringLiteral("Num6")}, {Qt::Key_7, QStringLiteral("Num7")}, + {Qt::Key_8, QStringLiteral("Num8")}, {Qt::Key_9, QStringLiteral("Num9")}, + {Qt::Key_Plus, QStringLiteral("NumPlus")}, {Qt::Key_Minus, QStringLiteral("NumMinus")}, + {Qt::Key_Asterisk, QStringLiteral("NumTimes")}, {Qt::Key_Slash, QStringLiteral("NumDivide")}, + {Qt::Key_Enter, QStringLiteral("NumEnter")}, + }; + return table; +} + +} // namespace + +QKeySequence textToShortCut(const QString &text) +{ + if (text.isEmpty()) + return QKeySequence(); + + const int lastPlus = text.lastIndexOf(QLatin1Char('+')); + const QString prefix = lastPlus >= 0 ? text.left(lastPlus + 1) : QString(); + QString base = lastPlus >= 0 ? text.mid(lastPlus + 1) : text; + + bool keypad = false; + const auto numpadIt = numpadAliasToBaseName().constFind(base); + if (numpadIt != numpadAliasToBaseName().constEnd()) { + keypad = true; + base = numpadIt.value(); + } else if (base.compare(QLatin1String("PgDn"), Qt::CaseInsensitive) == 0) { + base = QStringLiteral("PgDown"); + } + + const QKeySequence seq(prefix + base); + if (seq.isEmpty()) + return QKeySequence(); + const QKeyCombination kc = seq[0]; + if (!keypad) + return QKeySequence(kc); + return QKeySequence(QKeyCombination(kc.keyboardModifiers() | Qt::KeypadModifier, kc.key())); +} + +QString shortCutToText(const QKeySequence &shortCut) +{ + if (shortCut.isEmpty()) + return QString(); + + const QKeyCombination kc = shortCut[0]; + QString text; + if (kc.keyboardModifiers() & Qt::ControlModifier) + text += QLatin1String("Ctrl+"); + if (kc.keyboardModifiers() & Qt::ShiftModifier) + text += QLatin1String("Shift+"); + if (kc.keyboardModifiers() & Qt::AltModifier) + text += QLatin1String("Alt+"); + + if (kc.keyboardModifiers() & Qt::KeypadModifier) { + const auto it = baseKeyToNumpadAlias().constFind(kc.key()); + text += it != baseKeyToNumpadAlias().constEnd() ? it.value() : QKeySequence(kc.key()).toString(); + return text; + } + + QString baseName = QKeySequence(kc.key()).toString(); + if (baseName.compare(QLatin1String("PgDown"), Qt::CaseInsensitive) == 0) + baseName = QStringLiteral("PgDn"); + text += baseName; + return text; +} + +KeyBindingList keyBindings; + +KeyBinding *bEndTurn = nullptr; +KeyBinding *bHelp = nullptr; +KeyBinding *bUnitStat = nullptr; +KeyBinding *bCityStat = nullptr; +KeyBinding *bScienceStat = nullptr; +KeyBinding *bEUnitStat = nullptr; +KeyBinding *bDiagram = nullptr; +KeyBinding *bWonders = nullptr; +KeyBinding *bShips = nullptr; +KeyBinding *bNations = nullptr; +KeyBinding *bEmpire = nullptr; +KeyBinding *bFullScreen = nullptr; +KeyBinding *bResign = nullptr; +KeyBinding *bRandomMap = nullptr; +KeyBinding *bFillMap = nullptr; +KeyBinding *bDisbandUnit = nullptr; +KeyBinding *bFortify = nullptr; +KeyBinding *bCenterUnit = nullptr; +KeyBinding *bStay = nullptr; +KeyBinding *bNoOrders = nullptr; +KeyBinding *bPrevUnit = nullptr; +KeyBinding *bNextUnit = nullptr; +KeyBinding *bCancel = nullptr; +KeyBinding *bPillage = nullptr; +KeyBinding *bSelectTransport = nullptr; +KeyBinding *bTechTree = nullptr; +KeyBinding *bWait = nullptr; +KeyBinding *bJump = nullptr; +KeyBinding *bMapBtn0 = nullptr; +KeyBinding *bMapBtn1 = nullptr; +KeyBinding *bMapBtn4 = nullptr; +KeyBinding *bMapBtn5 = nullptr; +KeyBinding *bMapBtn6 = nullptr; +KeyBinding *bSetDebugMap0 = nullptr; +KeyBinding *bSetDebugMap1 = nullptr; +KeyBinding *bSetDebugMap2 = nullptr; +KeyBinding *bSetDebugMap3 = nullptr; +KeyBinding *bSetDebugMap4 = nullptr; +KeyBinding *bSetDebugMap5 = nullptr; +KeyBinding *bSetDebugMap6 = nullptr; +KeyBinding *bSetDebugMap7 = nullptr; +KeyBinding *bSetDebugMap8 = nullptr; +KeyBinding *bSetDebugMap9 = nullptr; +KeyBinding *bDebugMap = nullptr; +KeyBinding *bLocCodes = nullptr; +KeyBinding *bLogDlg = nullptr; +KeyBinding *bNames = nullptr; +KeyBinding *bRun = nullptr; +KeyBinding *bTestMapRepaint = nullptr; +KeyBinding *bSetViewpoint0 = nullptr; +KeyBinding *bSetViewpoint1 = nullptr; +KeyBinding *bSetViewpoint2 = nullptr; +KeyBinding *bSetViewpoint3 = nullptr; +KeyBinding *bSetViewpoint4 = nullptr; +KeyBinding *bSetViewpoint5 = nullptr; +KeyBinding *bSetViewpoint6 = nullptr; +KeyBinding *bSetViewpoint7 = nullptr; +KeyBinding *bSetViewpoint8 = nullptr; +KeyBinding *bSetViewpoint9 = nullptr; +KeyBinding *bAirBase = nullptr; +KeyBinding *bBuildCity = nullptr; +KeyBinding *bEnhance = nullptr; +KeyBinding *bGoOn = nullptr; +KeyBinding *bHome = nullptr; +KeyBinding *bFarmClearIrrigation = nullptr; +KeyBinding *bLoad = nullptr; +KeyBinding *bAfforestMine = nullptr; +KeyBinding *bCanal = nullptr; +KeyBinding *bTrans = nullptr; +KeyBinding *bPollution = nullptr; +KeyBinding *bRailRoad = nullptr; +KeyBinding *bUnload = nullptr; +KeyBinding *bRecover = nullptr; +KeyBinding *bUtilize = nullptr; +KeyBinding *bUnitMoveLeftDown = nullptr; +KeyBinding *bUnitMoveDown = nullptr; +KeyBinding *bUnitMoveRightDown = nullptr; +KeyBinding *bUnitMoveRight = nullptr; +KeyBinding *bUnitMoveRightUp = nullptr; +KeyBinding *bUnitMoveUp = nullptr; +KeyBinding *bUnitMoveLeftUp = nullptr; +KeyBinding *bUnitMoveLeft = nullptr; +KeyBinding *bMapMoveLeftDown = nullptr; +KeyBinding *bMapMoveDown = nullptr; +KeyBinding *bMapMoveRightDown = nullptr; +KeyBinding *bMapMoveRight = nullptr; +KeyBinding *bMapMoveRightUp = nullptr; +KeyBinding *bMapMoveUp = nullptr; +KeyBinding *bMapMoveLeftUp = nullptr; +KeyBinding *bMapMoveLeft = nullptr; + +void init() +{ + bEndTurn = &keyBindings.addItem(QStringLiteral("EndTurn"), QStringLiteral("End turn"), QStringLiteral("NumPlus")); + bHelp = &keyBindings.addItem(QStringLiteral("Help"), QStringLiteral("Help"), QStringLiteral("F1")); + bUnitStat = &keyBindings.addItem(QStringLiteral("UnitStat"), QStringLiteral("Unit stat"), QStringLiteral("F2")); + bCityStat = &keyBindings.addItem(QStringLiteral("CityStat"), QStringLiteral("City stat"), QStringLiteral("F3")); + bScienceStat = &keyBindings.addItem(QStringLiteral("ScienceStat"), QStringLiteral("Science research"), QStringLiteral("F4")); + bEUnitStat = &keyBindings.addItem(QStringLiteral("EnemyUnitStat"), QStringLiteral("Enemy unit stat"), QStringLiteral("F5")); + bDiagram = &keyBindings.addItem(QStringLiteral("Diagram"), QStringLiteral("Charts"), QStringLiteral("F6")); + bWonders = &keyBindings.addItem(QStringLiteral("Wonders"), QStringLiteral("World wonders"), QStringLiteral("F7")); + bShips = &keyBindings.addItem(QStringLiteral("Ships"), QStringLiteral("Colonization ships"), QStringLiteral("F8")); + bNations = &keyBindings.addItem(QStringLiteral("Nations"), QStringLiteral("Nations"), QStringLiteral("F9")); + bEmpire = &keyBindings.addItem(QStringLiteral("Empire"), QStringLiteral("Empire"), QStringLiteral("F10")); + bFullScreen = &keyBindings.addItem(QStringLiteral("FullScreen"), QStringLiteral("Full screen"), QStringLiteral("F11")); + bResign = &keyBindings.addItem(QStringLiteral("Resign"), QStringLiteral("Resign"), QStringLiteral("Ctrl+Q")); + bRandomMap = &keyBindings.addItem(QStringLiteral("RandomMap"), QStringLiteral("Random map"), QStringLiteral("Ctrl+R")); + bFillMap = &keyBindings.addItem(QStringLiteral("FillMap"), QStringLiteral("Fill map"), QStringLiteral("Ctrl+F")); + bDisbandUnit = &keyBindings.addItem(QStringLiteral("DisbandUnit"), QStringLiteral("Disband unit"), QStringLiteral("Ctrl+D")); + bFortify = &keyBindings.addItem(QStringLiteral("Fortify"), QStringLiteral("Fortify"), QStringLiteral("F")); + bCenterUnit = &keyBindings.addItem(QStringLiteral("CenterUnit"), QStringLiteral("Center"), QStringLiteral("C")); + bStay = &keyBindings.addItem(QStringLiteral("Stay"), QStringLiteral("Stay"), QStringLiteral("S")); + bNoOrders = &keyBindings.addItem(QStringLiteral("NoOrders"), QStringLiteral("No orders"), QStringLiteral("Space")); + bPrevUnit = &keyBindings.addItem(QStringLiteral("PrevUnit"), QStringLiteral("Previous unit"), QStringLiteral("Del")); + bNextUnit = &keyBindings.addItem(QStringLiteral("NextUnit"), QStringLiteral("Next unit"), QStringLiteral("Ins")); + bCancel = &keyBindings.addItem(QStringLiteral("Cancel"), QStringLiteral("Cancel"), QStringLiteral("Ctrl+C")); + bPillage = &keyBindings.addItem(QStringLiteral("Pillage"), QStringLiteral("Pillage"), QStringLiteral("Ctrl+P")); + bSelectTransport = &keyBindings.addItem(QStringLiteral("SelectTransport"), QStringLiteral("Select transport"), QStringLiteral("Ctrl+T")); + bTechTree = &keyBindings.addItem(QStringLiteral("TechTree"), QStringLiteral("Tech tree"), QStringLiteral("T")); + bWait = &keyBindings.addItem(QStringLiteral("Wait"), QStringLiteral("Wait"), QStringLiteral("W")); + bJump = &keyBindings.addItem(QStringLiteral("Jump"), QStringLiteral("Jump 20 turns"), QStringLiteral("Ctrl+J")); + bMapBtn0 = &keyBindings.addItem(QStringLiteral("MapButton1"), QStringLiteral("Map button 1"), QStringLiteral("1")); + bMapBtn1 = &keyBindings.addItem(QStringLiteral("MapButton2"), QStringLiteral("Map button 2"), QStringLiteral("2")); + bMapBtn4 = &keyBindings.addItem(QStringLiteral("MapButton3"), QStringLiteral("Map button 3"), QStringLiteral("3")); + bMapBtn5 = &keyBindings.addItem(QStringLiteral("MapButton4"), QStringLiteral("Map button 4"), QStringLiteral("4")); + bMapBtn6 = &keyBindings.addItem(QStringLiteral("MapButton5"), QStringLiteral("Map button 5"), QStringLiteral("5")); + bSetDebugMap0 = &keyBindings.addItem(QStringLiteral("SetDebugMap0"), QStringLiteral("Set debug map 0"), QStringLiteral("0")); + bSetDebugMap1 = &keyBindings.addItem(QStringLiteral("SetDebugMap1"), QStringLiteral("Set debug map 1"), QStringLiteral("1")); + bSetDebugMap2 = &keyBindings.addItem(QStringLiteral("SetDebugMap2"), QStringLiteral("Set debug map 2"), QStringLiteral("2")); + bSetDebugMap3 = &keyBindings.addItem(QStringLiteral("SetDebugMap3"), QStringLiteral("Set debug map 3"), QStringLiteral("3")); + bSetDebugMap4 = &keyBindings.addItem(QStringLiteral("SetDebugMap4"), QStringLiteral("Set debug map 4"), QStringLiteral("4")); + bSetDebugMap5 = &keyBindings.addItem(QStringLiteral("SetDebugMap5"), QStringLiteral("Set debug map 5"), QStringLiteral("5")); + bSetDebugMap6 = &keyBindings.addItem(QStringLiteral("SetDebugMap6"), QStringLiteral("Set debug map 6"), QStringLiteral("6")); + bSetDebugMap7 = &keyBindings.addItem(QStringLiteral("SetDebugMap7"), QStringLiteral("Set debug map 7"), QStringLiteral("7")); + bSetDebugMap8 = &keyBindings.addItem(QStringLiteral("SetDebugMap8"), QStringLiteral("Set debug map 8"), QStringLiteral("8")); + bSetDebugMap9 = &keyBindings.addItem(QStringLiteral("SetDebugMap9"), QStringLiteral("Set debug map 9"), QStringLiteral("9")); + bDebugMap = &keyBindings.addItem(QStringLiteral("DebugMap"), QStringLiteral("Debug map"), QStringLiteral("Ctrl+K")); + bLocCodes = &keyBindings.addItem(QStringLiteral("LocCodes"), QStringLiteral("Location codes"), QStringLiteral("Ctrl+L")); + bLogDlg = &keyBindings.addItem(QStringLiteral("LogDlg"), QStringLiteral("Log dialog"), QStringLiteral("Ctrl+M")); + bNames = &keyBindings.addItem(QStringLiteral("ShowNames"), QStringLiteral("Show names"), QStringLiteral("Ctrl+N")); + bRun = &keyBindings.addItem(QStringLiteral("Run"), QStringLiteral("Run"), QStringLiteral("Ctrl+R")); + bTestMapRepaint = &keyBindings.addItem(QStringLiteral("TestMapRepaint"), QStringLiteral("Test map reapaint"), QStringLiteral("Ctrl+Space")); + bSetViewpoint0 = &keyBindings.addItem(QStringLiteral("SetViewpoint0"), QStringLiteral("Set viewpoint 0"), QStringLiteral("Ctrl+0")); + bSetViewpoint1 = &keyBindings.addItem(QStringLiteral("SetViewpoint1"), QStringLiteral("Set viewpoint 1"), QStringLiteral("Ctrl+1")); + bSetViewpoint2 = &keyBindings.addItem(QStringLiteral("SetViewpoint2"), QStringLiteral("Set viewpoint 2"), QStringLiteral("Ctrl+2")); + bSetViewpoint3 = &keyBindings.addItem(QStringLiteral("SetViewpoint3"), QStringLiteral("Set viewpoint 3"), QStringLiteral("Ctrl+3")); + bSetViewpoint4 = &keyBindings.addItem(QStringLiteral("SetViewpoint4"), QStringLiteral("Set viewpoint"), QStringLiteral("Ctrl+4")); + // NOTE: ShortName duplicates (SetViewpoint0-4 reused for 5-9) are copied + // verbatim from the original - KeyBindings.pas has this bug too. + bSetViewpoint5 = &keyBindings.addItem(QStringLiteral("SetViewpoint0"), QStringLiteral("Set viewpoint"), QStringLiteral("Ctrl+5")); + bSetViewpoint6 = &keyBindings.addItem(QStringLiteral("SetViewpoint1"), QStringLiteral("Set viewpoint"), QStringLiteral("Ctrl+6")); + bSetViewpoint7 = &keyBindings.addItem(QStringLiteral("SetViewpoint2"), QStringLiteral("Set viewpoint"), QStringLiteral("Ctrl+7")); + bSetViewpoint8 = &keyBindings.addItem(QStringLiteral("SetViewpoint3"), QStringLiteral("Set viewpoint"), QStringLiteral("Ctrl+8")); + bSetViewpoint9 = &keyBindings.addItem(QStringLiteral("SetViewpoint4"), QStringLiteral("Set viewpoint"), QStringLiteral("Ctrl+9")); + bAirBase = &keyBindings.addItem(QStringLiteral("AirBase"), QStringLiteral("Air base"), QStringLiteral("A")); + bBuildCity = &keyBindings.addItem(QStringLiteral("BuildCity"), QStringLiteral("Build city"), QStringLiteral("B")); + bEnhance = &keyBindings.addItem(QStringLiteral("Enhance"), QStringLiteral("Enhance"), QStringLiteral("E")); + bGoOn = &keyBindings.addItem(QStringLiteral("GoOn"), QStringLiteral("Go on"), QStringLiteral("G")); + bHome = &keyBindings.addItem(QStringLiteral("Home"), QStringLiteral("Home"), QStringLiteral("H")); + bFarmClearIrrigation = &keyBindings.addItem(QStringLiteral("FarmClearIrrigation"), QStringLiteral("Farm/Clear/Irrigation"), QStringLiteral("I")); + bLoad = &keyBindings.addItem(QStringLiteral("Load"), QStringLiteral("Load"), QStringLiteral("L")); + bAfforestMine = &keyBindings.addItem(QStringLiteral("AfforestMine"), QStringLiteral("Afforest/Mine"), QStringLiteral("M")); + bCanal = &keyBindings.addItem(QStringLiteral("Canal"), QStringLiteral("Canal"), QStringLiteral("N")); + bTrans = &keyBindings.addItem(QStringLiteral("Trans"), QStringLiteral("Trans"), QStringLiteral("O")); + bPollution = &keyBindings.addItem(QStringLiteral("Pollution"), QStringLiteral("Pollution"), QStringLiteral("P")); + bRailRoad = &keyBindings.addItem(QStringLiteral("RailRoad"), QStringLiteral("Rails/Road"), QStringLiteral("R")); + bUnload = &keyBindings.addItem(QStringLiteral("Unload"), QStringLiteral("Unload"), QStringLiteral("U")); + bRecover = &keyBindings.addItem(QStringLiteral("Recover"), QStringLiteral("Recover"), QStringLiteral("V")); + bUtilize = &keyBindings.addItem(QStringLiteral("Utilize"), QStringLiteral("Utilize"), QStringLiteral("Z")); + // Unit moves + bUnitMoveLeftDown = &keyBindings.addItem(QStringLiteral("MoveLeftDown"), QStringLiteral("Move unit left-down"), QStringLiteral("Num1"), QStringLiteral("End")); + bUnitMoveDown = &keyBindings.addItem(QStringLiteral("MoveDown"), QStringLiteral("Move unit down"), QStringLiteral("Num2"), QStringLiteral("Down")); + bUnitMoveRightDown = &keyBindings.addItem(QStringLiteral("MoveRightDown"), QStringLiteral("Move unit right-down"), QStringLiteral("Num3"), QStringLiteral("PgDn")); + bUnitMoveRight = &keyBindings.addItem(QStringLiteral("MoveRight"), QStringLiteral("Move unit right"), QStringLiteral("Num6"), QStringLiteral("Right")); + bUnitMoveRightUp = &keyBindings.addItem(QStringLiteral("MoveRightUp"), QStringLiteral("Move unit right-up"), QStringLiteral("Num9"), QStringLiteral("PgUp")); + bUnitMoveUp = &keyBindings.addItem(QStringLiteral("MoveUp"), QStringLiteral("Move unit up"), QStringLiteral("Num8"), QStringLiteral("Up")); + bUnitMoveLeftUp = &keyBindings.addItem(QStringLiteral("MoveLeftUp"), QStringLiteral("Move unit left-up"), QStringLiteral("Num7"), QStringLiteral("Home")); + bUnitMoveLeft = &keyBindings.addItem(QStringLiteral("MoveLeft"), QStringLiteral("Move unit left"), QStringLiteral("Num4"), QStringLiteral("Left")); + // Map moves + bMapMoveLeftDown = &keyBindings.addItem(QStringLiteral("MapMoveLeftDown"), QStringLiteral("Move map left-down"), QStringLiteral("Ctrl+Num1"), QStringLiteral("Ctrl+End")); + bMapMoveDown = &keyBindings.addItem(QStringLiteral("MapMoveDown"), QStringLiteral("Move map down"), QStringLiteral("Ctrl+Num2"), QStringLiteral("Ctrl+Down")); + bMapMoveRightDown = &keyBindings.addItem(QStringLiteral("MapMoveRightDown"), QStringLiteral("Move map right-down"), QStringLiteral("Ctrl+Num3"), QStringLiteral("Ctrl+PgDn")); + bMapMoveRight = &keyBindings.addItem(QStringLiteral("MapMoveRight"), QStringLiteral("Move map right"), QStringLiteral("Ctrl+Num6"), QStringLiteral("Ctrl+Right")); + bMapMoveRightUp = &keyBindings.addItem(QStringLiteral("MapMoveRightUp"), QStringLiteral("Move map right-up"), QStringLiteral("Ctrl+Num9"), QStringLiteral("Ctrl+PgUp")); + bMapMoveUp = &keyBindings.addItem(QStringLiteral("MapMoveUp"), QStringLiteral("Move map up"), QStringLiteral("Ctrl+Num8"), QStringLiteral("Ctrl+Up")); + bMapMoveLeftUp = &keyBindings.addItem(QStringLiteral("MapMoveLeftUp"), QStringLiteral("Move map left-up"), QStringLiteral("Ctrl+Num7"), QStringLiteral("Ctrl+Home")); + bMapMoveLeft = &keyBindings.addItem(QStringLiteral("MapMoveLeft"), QStringLiteral("Move map left"), QStringLiteral("Ctrl+Num4"), QStringLiteral("Ctrl+Left")); + + keyBindings.sortAlpha(); +} + +void done() +{ + keyBindings = KeyBindingList(); +} + +} // namespace KeyBindings +} // namespace Cevo diff --git a/game/localplayer/KeyBindings.h b/game/localplayer/KeyBindings.h new file mode 100644 index 0000000..58345ef --- /dev/null +++ b/game/localplayer/KeyBindings.h @@ -0,0 +1,197 @@ +// 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 +#include +#include + +#include +#include + +class QSettings; + +namespace Cevo { +namespace KeyBindings { + +// TKeyBinding equivalent. ShortCut/ShortCut2 are represented as +// QKeySequence rather than Delphi's packed-Word TShortCut - the only +// persisted form is always the human-readable text produced by +// shortCutToText()/textToShortCut() below, so there is no binary format to +// stay bit-compatible with. +class KeyBinding { +public: + QString shortName; + QString fullName; + QKeySequence shortCut; + QKeySequence shortCut2; + QKeySequence defaultShortCut; + QKeySequence defaultShortCut2; + + bool test(const QKeySequence &aShortCut) const; + void assign(const KeyBinding &source); + void setDefault(); +}; + +// TKeyBindings equivalent: an owning, ordered list of KeyBindings. +// Composition over std::vector>, same pattern +// as Brains/GraphicSets. +class KeyBindingList { +public: + KeyBinding &addItem(const QString &shortName, const QString &fullName, const QKeySequence &shortCut, + const QKeySequence &shortCut2 = QKeySequence()); + KeyBinding &addItem(const QString &shortName, const QString &fullName, const QString &shortCutText, + const QString &shortCutText2 = QString()); + KeyBinding *search(const QString &shortName) const; + + // Registry.pas's TRegistry is Windows-only; QSettings is the natural, + // already cross-platform Qt equivalent used in its place. `group` + // replaces LoadFromRegistry/SaveToRegistry's `Key` path argument. + void loadFromSettings(QSettings &settings, const QString &group); + void saveToSettings(QSettings &settings, const QString &group) const; + + QStringList toDisplayStrings() const; + void assign(const KeyBindingList &source); + void resetToDefault(); + void removeShortCut(const QKeySequence &shortCut); + void sortAlpha(); + + int count() const { return static_cast(m_items.size()); } + KeyBinding &operator[](int index) { return *m_items[static_cast(index)]; } + const KeyBinding &operator[](int index) const { return *m_items[static_cast(index)]; } + +private: + std::vector> m_items; +}; + +// Parses LCL-style shortcut text ("Ctrl+Q", "F1", "NumPlus", "Ctrl+Num1", +// "PgDn", ...) into a QKeySequence; shortCutToText() is its inverse. +QKeySequence textToShortCut(const QString &text); +QString shortCutToText(const QKeySequence &shortCut); + +extern KeyBindingList keyBindings; + +extern KeyBinding *bEndTurn; +extern KeyBinding *bHelp; +extern KeyBinding *bUnitStat; +extern KeyBinding *bCityStat; +extern KeyBinding *bScienceStat; +extern KeyBinding *bEUnitStat; +extern KeyBinding *bDiagram; +extern KeyBinding *bWonders; +extern KeyBinding *bShips; +extern KeyBinding *bNations; +extern KeyBinding *bEmpire; +extern KeyBinding *bFullScreen; +extern KeyBinding *bResign; +extern KeyBinding *bRandomMap; +extern KeyBinding *bFillMap; +extern KeyBinding *bDisbandUnit; +extern KeyBinding *bFortify; +extern KeyBinding *bCenterUnit; +extern KeyBinding *bStay; +extern KeyBinding *bNoOrders; +extern KeyBinding *bPrevUnit; +extern KeyBinding *bNextUnit; +extern KeyBinding *bCancel; +extern KeyBinding *bPillage; +extern KeyBinding *bSelectTransport; +extern KeyBinding *bTechTree; +extern KeyBinding *bWait; +extern KeyBinding *bJump; +extern KeyBinding *bMapBtn0; +extern KeyBinding *bMapBtn1; +extern KeyBinding *bMapBtn4; +extern KeyBinding *bMapBtn5; +extern KeyBinding *bMapBtn6; +extern KeyBinding *bSetDebugMap0; +extern KeyBinding *bSetDebugMap1; +extern KeyBinding *bSetDebugMap2; +extern KeyBinding *bSetDebugMap3; +extern KeyBinding *bSetDebugMap4; +extern KeyBinding *bSetDebugMap5; +extern KeyBinding *bSetDebugMap6; +extern KeyBinding *bSetDebugMap7; +extern KeyBinding *bSetDebugMap8; +extern KeyBinding *bSetDebugMap9; +extern KeyBinding *bDebugMap; +extern KeyBinding *bLocCodes; +extern KeyBinding *bLogDlg; +extern KeyBinding *bNames; +extern KeyBinding *bRun; +extern KeyBinding *bTestMapRepaint; +extern KeyBinding *bSetViewpoint0; +extern KeyBinding *bSetViewpoint1; +extern KeyBinding *bSetViewpoint2; +extern KeyBinding *bSetViewpoint3; +extern KeyBinding *bSetViewpoint4; +extern KeyBinding *bSetViewpoint5; +extern KeyBinding *bSetViewpoint6; +extern KeyBinding *bSetViewpoint7; +extern KeyBinding *bSetViewpoint8; +extern KeyBinding *bSetViewpoint9; +extern KeyBinding *bAirBase; +extern KeyBinding *bBuildCity; +extern KeyBinding *bEnhance; +extern KeyBinding *bGoOn; +extern KeyBinding *bHome; +extern KeyBinding *bFarmClearIrrigation; +extern KeyBinding *bLoad; +extern KeyBinding *bAfforestMine; +extern KeyBinding *bCanal; +extern KeyBinding *bTrans; +extern KeyBinding *bPollution; +extern KeyBinding *bRailRoad; +extern KeyBinding *bUnload; +extern KeyBinding *bRecover; +extern KeyBinding *bUtilize; +extern KeyBinding *bUnitMoveLeftDown; +extern KeyBinding *bUnitMoveDown; +extern KeyBinding *bUnitMoveRightDown; +extern KeyBinding *bUnitMoveRight; +extern KeyBinding *bUnitMoveRightUp; +extern KeyBinding *bUnitMoveUp; +extern KeyBinding *bUnitMoveLeftUp; +extern KeyBinding *bUnitMoveLeft; +extern KeyBinding *bMapMoveLeftDown; +extern KeyBinding *bMapMoveDown; +extern KeyBinding *bMapMoveRightDown; +extern KeyBinding *bMapMoveRight; +extern KeyBinding *bMapMoveRightUp; +extern KeyBinding *bMapMoveUp; +extern KeyBinding *bMapMoveLeftUp; +extern KeyBinding *bMapMoveLeft; + +// Populates keyBindings and all the b* globals above (Pascal's +// `initialization` section). done() frees them (`finalization`). +void init(); +void done(); + +} // namespace KeyBindings +} // namespace Cevo diff --git a/game/localplayer/Tribes.cpp b/game/localplayer/Tribes.cpp new file mode 100644 index 0000000..5daef94 --- /dev/null +++ b/game/localplayer/Tribes.cpp @@ -0,0 +1,552 @@ +// 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 "Tribes.h" + +#include "Cevo/CevoScreenTools.h" +#include "Global.h" + +#include +#include +#include + +#include +#include + +namespace Cevo { +namespace Tribes { + +std::array, MaxPlayers> tribe; +GraphicSet *hGrStdUnits = nullptr; + +namespace { + +// Shared single-line parser cursor, exactly like Tribes.pas's module-level +// `Input` + Get/GetNum - populated with one script line at a time by the +// various scanning loops below. +QString input; + +QString get() +{ + while (!input.isEmpty() && (input.at(0) == QLatin1Char(' ') || input.at(0) == QLatin1Char('\t'))) + input.remove(0, 1); + int p = input.indexOf(QLatin1Char(',')); + if (p < 0) + p = input.length(); + const QString result = input.left(p); + input.remove(0, p + 1); + return result; +} + +int getNum() +{ + bool ok = false; + const int value = get().toInt(&ok); + return ok ? value : 0; +} + +QStringList loadScriptLines(const QString &fileName) +{ + QStringList lines; + QFile file(fileName); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream stream(&file); + while (!stream.atEnd()) + lines.append(stream.readLine()); + } + return lines; +} + +// TChosenModelPictureInfo equivalent. +struct ChosenModelPictureInfo { + int hash = 0; + GraphicSet *hGr = nullptr; + int pix = 0; + QString modelName; +}; + +QStringList stdUnitScript; +std::vector pictureList; + +} // namespace + +std::uint8_t ModelPictureInfo::commandDataSize() const +{ + // 4 ints (trix/mix/pix/Hash) + a 1-byte length prefix + the (UTF-8) + // name bytes, matching the original's SizeOf()-based ShortString size. + return static_cast(4 * sizeof(std::int32_t) + 1 + + static_cast(grName.toUtf8().size())); +} + +void init() +{ + stdUnitScript = loadScriptLines(localizedFilePath(QStringLiteral("Tribes/StdUnits.txt"))); + pictureList.clear(); +} + +void done() +{ + pictureList.clear(); + stdUnitScript.clear(); +} + +QString cityName(int founder) +{ + if (!generateNames) + return QStringLiteral("%1.%2").arg(founder >> 12).arg(founder & 0xFFF); + const int founderTribe = founder >> 12; + if (founderTribe < 0 || founderTribe >= MaxPlayers || !tribe[static_cast(founderTribe)]) + return QStringLiteral("%1.%2").arg(founder >> 12).arg(founder & 0xFFF); + return tribe[static_cast(founderTribe)]->getCityName(founder & 0xFFF); +} + +int modelCode(const TModelInfo &modelInfo) +{ + int result = 0; + switch (modelInfo.Kind) { + case mkSelfDeveloped: + case mkEnemyDeveloped: + switch (modelInfo.Domain) { + case dGround: + if ((modelInfo.Attack >= modelInfo.Defense * 4) || + (modelInfo.Attack > 0 && modelInfo.MaxUpgrade < 10 && + (modelInfo.Cap & (1u << (mcArtillery - mcFirstNonCap))) != 0)) { + result = 170; + if (modelInfo.MaxUpgrade >= 12) + result += 3; + else if (modelInfo.MaxUpgrade >= 10 || modelInfo.Weight > 7) + result += 2; + else if (modelInfo.MaxUpgrade >= 4) + result += 1; + } else { + result = 100; + if (modelInfo.MaxUpgrade >= 12) + result += 6; + else if (modelInfo.MaxUpgrade >= 10 || modelInfo.Weight > 7) + result += 5; + else if (modelInfo.MaxUpgrade >= 6) + result += 4; + else if (modelInfo.MaxUpgrade >= 4) + result += 3; + else if (modelInfo.MaxUpgrade >= 2) + result += 2; + else if (modelInfo.MaxUpgrade >= 1) + result += 1; + if (modelInfo.Speed >= 250) { + if (result >= 105 && modelInfo.Attack <= modelInfo.Defense) + result = 110; + else + result += 30; + } + } + break; + case dSea: + result = 200; + if (modelInfo.MaxUpgrade >= 8) + result += 3; + else if (modelInfo.MaxUpgrade >= 6) + result += 2; + else if (modelInfo.MaxUpgrade >= 3) + result += 1; + if ((modelInfo.Cap & (1u << (mcSub - mcFirstNonCap))) != 0) + result = 240; + else if (modelInfo.ATrans_Fuel > 0) + result = 220; + else if (result >= 202 && modelInfo.Attack == 0 && modelInfo.TTrans > 0) + result = 210; + break; + case dAir: + result = 300; + if (modelInfo.Bombs > 0 || modelInfo.TTrans > 0) + result += 10; + if (modelInfo.Speed > 850) + result += 1; + break; + } + break; + case mkSpecial_TownGuard: + result = 41; + break; + case mkSpecial_Boat: + result = 64; + break; + case mkSpecial_SubCabin: + result = 71; + break; + case mkSpecial_Glider: + result = 73; + break; + case mkSlaves: + result = 74; + break; + case mkSettler: + result = modelInfo.Speed > 150 ? 11 : 10; + break; + case mkDiplomat: + result = 21; + break; + case mkCaravan: + result = 30; + break; + } + return result; +} + +void findStdModelPicture(int code, int &pix, QString &name) +{ + for (const QString &line : stdUnitScript) { + input = line; + pix = getNum(); + if (code == getNum()) { + name = get(); + return; + } + } + pix = -1; +} + +bool getTribeInfo(const QString &fileName, QString &name, QColor &color) +{ + name.clear(); + color = colorFromDelphi(0xFFFFFF); + int found = 0; + + QFile file(localizedFilePath(QStringLiteral("Tribes/") + fileName + CevoTribeExt)); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + return false; + QTextStream stream(&file); + while (!stream.atEnd()) { + const QString line = stream.readLine(); + if (line.left(7) == QLatin1String("#CHOOSE")) { + name = line.mid(8); + found |= 1; + if (found == 3) + break; + } else if (line.left(6) == QLatin1String("#COLOR")) { + color = hexStringToColor(line.mid(6)); + found |= 2; + if (found == 3) + break; + } + } + return found == 3; +} + +void findPosition(GraphicSet *hGr, int x, int y, int xmax, int ymax, QColor mark, int &xp, int &yp) +{ + xp = 0; + yp = 0; + if (!hGr) + return; + const QRgb markRgb = mark.rgb() & 0xFFFFFFu; + const QImage &image = hGr->data.image(); + while (xp < xmax && (static_cast(image.pixel(x + 1 + xp, y)) & 0xFFFFFFu) != markRgb) + ++xp; + while (yp < ymax && (static_cast(image.pixel(x, y + 1 + yp)) & 0xFFFFFFu) != markRgb) + ++yp; +} + +Tribe::Tribe(const QString &fileName) +{ + m_name.fill(QString()); + m_script = loadScriptLines(fileName); + m_cityLine0 = 0; + m_nCityLines = 0; + + for (int line = 0; line < m_script.size(); ++line) { + input = m_script.at(line); + if (m_cityLine0 > 0 && m_nCityLines == 0 && (input.isEmpty() || input.at(0) == QLatin1Char('#'))) + m_nCityLines = line - m_cityLine0; + + if (input.length() >= 3 && input.at(0) == QLatin1Char('#') && input.at(1) >= QLatin1Char('a') && + input.at(1) <= QLatin1Char('z') && input.at(2) == QLatin1Char(' ')) { + m_name[static_cast(input.at(1).unicode() - QLatin1Char('a').unicode())] = input.mid(3); + } else if (input.left(6) == QLatin1String("#COLOR")) { + color = hexStringToColor(input.mid(6)); + } else if (input.left(7) == QLatin1String("#CITIES")) { + m_cityLine0 = line + 1; + } else if (input.left(8) == QLatin1String("#SYMBOLS")) { + input.remove(0, 9); + const QString item = get(); + symPix = getNum(); + symHGr = loadGraphicSet(item + PngExt); + } + } + + m_modelPicture.fill(ModelPicture()); + m_modelName.fill(QString()); + numberName = -1; + cAge = -1; + mixSlaves = -1; +} + +Tribe::~Tribe() = default; + +QString Tribe::getCityName(int i) const +{ + QString result; + if (m_nCityLines > i) { + result = m_script.at(m_cityLine0 + i); + while (!result.isEmpty() && (result.at(0) == QLatin1Char(' ') || result.at(0) == QLatin1Char('\t'))) + result.remove(0, 1); + } else { + result = tPhrase(QStringLiteral("GENCITY")).arg(i + 1); + } + return result; +} + +void Tribe::setCityName(int i, const QString &newName) +{ + while (m_nCityLines <= i) { + m_script.insert(m_cityLine0 + m_nCityLines, tPhrase(QStringLiteral("GENCITY")).arg(m_nCityLines + 1)); + ++m_nCityLines; + } + m_script[m_cityLine0 + i] = newName; +} + +QString Tribe::tString(QString templateText) const +{ + for (;;) { + int p = templateText.indexOf(QLatin1Char('#')); + if (p < 0 || p == templateText.length() - 1) + break; + QChar variant = templateText.at(p + 1); + const bool caseUp = variant >= QLatin1Char('A') && variant <= QLatin1Char('Z'); + if (caseUp) + variant = QChar(variant.unicode() + 32); + templateText.remove(p, 2); + if (variant >= QLatin1Char('a') && variant <= QLatin1Char('z')) { + const QString replacement = numberName < 0 + ? m_name[static_cast(variant.unicode() - QLatin1Char('a').unicode())] + : QStringLiteral("P%1").arg(numberName); + templateText.insert(p, replacement); + if (caseUp && templateText.length() > p) { + const QChar c = templateText.at(p); + if ((c >= QLatin1Char('a') && c <= QLatin1Char('z')) || (c.unicode() >= 0xE0 && c.unicode() <= 0xFF)) + templateText[p] = QChar(c.unicode() - 32); + } + } + } + return templateText; +} + +QString Tribe::tPhrase(const QString &item) const +{ + // Phrases/Phrases2 (StringTables.pas) aren't ported. "GENCITY" is the + // only phrase this file ever looks up (a generated-city-name fallback + // once a tribe's scripted name list runs out) - fall back to a plain + // English template rather than failing on an unrecognized key. + Q_UNUSED(item); + return tString(QStringLiteral("City %1")); +} + +void Tribe::initAge(int age) +{ + if (age == cAge) + return; + cAge = age; + + const QString marker = QStringLiteral("#AGE") + QString::number(age) + QLatin1Char(' '); + int i = 0; + while (i < m_script.size() && m_script.at(i).left(6) != marker) + ++i; + if (i >= m_script.size()) + return; + + input = m_script.at(i); + input.remove(0, 6); + QString item = get(); + cPix = getNum(); + + if (age < 2) { + if (item.compare(QLatin1String("stdcities"), Qt::CaseInsensitive) == 0) { + if (cPix == 3) { + cPix = 0; + } else if (cPix == 6) { + cPix = 0; + item = QStringLiteral("Nation2"); + } + } + cHGr = loadGraphicSet(item + PngExt); + for (int x = 0; x < 4; ++x) { + findPosition(cHGr, x * 65, cPix * 49, 63, 47, colorFromDelphi(0x00FFFF), cityPicture[static_cast(x)].xShield, + cityPicture[static_cast(x)].yShield); + } + } else { + cHGr = nullptr; + } + + get(); + getNum(); + item = get(); + if (item.isEmpty()) { + faceHGr = nullptr; + } else { + faceHGr = loadGraphicSet(item + PngExt); + facePix = getNum(); + if (faceHGr) { + const int px = facePix % 10 * 65; + const int py = facePix / 10 * 49 + 48; + const QRgb markRgb = colorFromDelphi(0x00FFFF).rgb() & 0xFFFFFFu; + if ((static_cast(faceHGr->data.image().pixel(px, py)) & 0xFFFFFFu) == markRgb) { + faceHGr->data.image().setPixel(px, py, qRgb(0, 0, 0)); + constexpr int gray = 0xB8B8B8; + imageOpBCC(faceHGr->data, Templates->data, facePix % 10 * 65 + 1, facePix / 10 * 49 + 1, 1, 25, 64, 48, + gray, colorToDelphi(color)); + } + } + } +} + +void Tribe::setModelPicture(const ModelPictureInfo &info, bool isNew) +{ + if (!isNew) { + int i = static_cast(pictureList.size()) - 1; + while (i >= 0 && pictureList[static_cast(i)].hash != info.hash) + --i; + Q_ASSERT(i >= 0); + Q_ASSERT(pictureList[static_cast(i)].hGr == loadGraphicSet(info.grName)); + Q_ASSERT(pictureList[static_cast(i)].pix == info.pix); + ModelPicture &mp = modelPictureAt(info.mix); + mp.hGr = pictureList[static_cast(i)].hGr; + mp.pix = pictureList[static_cast(i)].pix; + modelNameAt(info.mix) = pictureList[static_cast(i)].modelName; + } else { + ModelPicture &mp = modelPictureAt(info.mix); + mp.hGr = loadGraphicSet(info.grName); + mp.pix = info.pix; + if (mp.hGr && info.pix >= 0 && info.pix < static_cast(mp.hGr->pixUsed.size())) + ++mp.hGr->pixUsed[static_cast(info.pix)]; + modelNameAt(info.mix).clear(); + + bool ok = false; + const QString unitsMarker = QStringLiteral("#UNITS ") + QFileInfo(info.grName).completeBaseName(); + for (const QString &line : m_script) { + input = line; + if (input == unitsMarker) + ok = true; + else if (!input.isEmpty() && input.at(0) == QLatin1Char('#')) + ok = false; + else if (ok && getNum() == info.pix) { + get(); + modelNameAt(info.mix) = get(); + } + } + + if (modelNameAt(info.mix).isEmpty()) { + for (const QString &line : stdUnitScript) { + input = line; + if (getNum() == info.pix) { + get(); + modelNameAt(info.mix) = get(); + } + } + } + + if (info.hash != 0) { + ChosenModelPictureInfo newInfo; + newInfo.hash = info.hash; + newInfo.hGr = modelPictureAt(info.mix).hGr; + newInfo.pix = info.pix; + newInfo.modelName = modelNameAt(info.mix); + pictureList.push_back(newInfo); + } + } + + ModelPicture &finalMp = modelPictureAt(info.mix); + findPosition(finalMp.hGr, finalMp.pix % 10 * 65, finalMp.pix / 10 * 49, 63, 47, colorFromDelphi(0xFFFFFF), + finalMp.xShield, finalMp.yShield); +} + +bool Tribe::chooseModelPicture(ModelPictureInfo &picture, int code, int turn, bool forceNew) +{ + if (!forceNew && picture.hash > 0) { + for (const auto &entry : pictureList) { + if (entry.hash == picture.hash) { + picture.grName = entry.hGr ? entry.hGr->name : QString(); + picture.pix = entry.pix; + return false; + } + } + } + + picture.pix = 0; + ModelPictureInfo testPic = picture; + int leastUsed = std::numeric_limits::max(); + int cnt = 0; + GraphicSet *hGr = hGrStdUnits; + + auto check = [&]() { + testPic.pix = getNum(); + if (code == getNum()) { + int used; + if (forceNew || !hGr) { + used = 0; + } else { + used = (testPic.pix >= 0 && testPic.pix < static_cast(hGr->pixUsed.size())) + ? 4 * hGr->pixUsed[static_cast(testPic.pix)] + : 0; + if (hGr == hGrStdUnits) + used += 2; + } + if (used < leastUsed) { + cnt = 0; + leastUsed = used; + } + if (used == leastUsed) { + ++cnt; + if (turn % cnt == 0) + picture = testPic; + } + } + }; + + testPic.grName = QStringLiteral("StdUnits"); + for (const QString &line : stdUnitScript) { + input = line; + check(); + } + + bool ok = false; + for (const QString &line : m_script) { + input = line; + if (input.left(6) == QLatin1String("#UNITS")) { + ok = true; + testPic.grName = input.mid(7); + hGr = grExt.searchByName(testPic.grName); + } else if (!input.isEmpty() && input.at(0) == QLatin1Char('#')) { + ok = false; + } else if (ok) { + check(); + } + } + return true; +} + +} // namespace Tribes +} // namespace Cevo diff --git a/game/localplayer/Tribes.h b/game/localplayer/Tribes.h new file mode 100644 index 0000000..9f9b1f4 --- /dev/null +++ b/game/localplayer/Tribes.h @@ -0,0 +1,138 @@ +// 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/Protocol.h" + +#include +#include +#include + +#include +#include +#include + +namespace Cevo { + +class GraphicSet; + +namespace Tribes { + +// TCityPicture equivalent. +struct CityPicture { + int xShield = 0; + int yShield = 0; +}; + +// TModelPicture equivalent. +struct ModelPicture { + GraphicSet *hGr = nullptr; + int pix = 0; + int xShield = 0; + int yShield = 0; +}; + +// TModelPictureInfo equivalent. +struct ModelPictureInfo { + int trix = 0; + int mix = 0; + int pix = 0; + int hash = 0; + QString grName; + + // Size this record would take in the (not yet ported) network command + // stream: 4 ints + a 1-byte length prefix + the (UTF-8) name bytes. + std::uint8_t commandDataSize() const; +}; + +// TTribe equivalent. Holds a tribe's graphic assets (nation symbol/ruler +// face/city sprites), the parsed `.tribe.txt` name-template script, and the +// per-model picture assignments made for this game. +class Tribe { +public: + explicit Tribe(const QString &fileName); + ~Tribe(); + Tribe(const Tribe &) = delete; + Tribe &operator=(const Tribe &) = delete; + + GraphicSet *symHGr = nullptr; + int symPix = 0; + GraphicSet *faceHGr = nullptr; + int facePix = 0; + GraphicSet *cHGr = nullptr; + int cPix = 0; + int cAge = -1; + int mixSlaves = -1; + QColor color; + int numberName = -1; + std::array cityPicture; + + // ModelPicture/ModelName are indexed -1..256 in the original (-1 is the + // "building site" picture); use these accessors rather than the raw + // (0-based, +1-shifted) backing arrays. + ModelPicture &modelPictureAt(int mix) { return m_modelPicture[static_cast(mix + 1)]; } + const ModelPicture &modelPictureAt(int mix) const { return m_modelPicture[static_cast(mix + 1)]; } + QString &modelNameAt(int mix) { return m_modelName[static_cast(mix + 1)]; } + const QString &modelNameAt(int mix) const { return m_modelName[static_cast(mix + 1)]; } + + QString getCityName(int i) const; + void setCityName(int i, const QString &newName); + QString tString(QString templateText) const; + // Phrases/Phrases2 (StringTables.pas) aren't ported; falls back to a + // plain-English template for the one phrase this file needs (city-name + // generation past the end of a tribe's scripted name list). + QString tPhrase(const QString &item) const; + void setModelPicture(const ModelPictureInfo &info, bool isNew); + bool chooseModelPicture(ModelPictureInfo &picture, int code, int turn, bool forceNew); + void initAge(int age); + +private: + int m_cityLine0 = 0; + int m_nCityLines = 0; + std::array m_name; // Name['a'..'z'] + QStringList m_script; + std::array m_modelPicture; + std::array m_modelName; +}; + +constexpr int MaxPlayers = nPl; + +extern std::array, MaxPlayers> tribe; +extern GraphicSet *hGrStdUnits; + +void init(); +void done(); +QString cityName(int founder); +int modelCode(const TModelInfo &modelInfo); +void findStdModelPicture(int code, int &pix, QString &name); +bool getTribeInfo(const QString &fileName, QString &name, QColor &color); +void findPosition(GraphicSet *hGr, int x, int y, int xmax, int ymax, QColor mark, int &xp, int &yp); + +} // namespace Tribes +} // namespace Cevo