From bb5080aad9b8f3c672039a18b448b9c90455a1fe Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Mon, 9 Nov 2026 13:35:20 +0100 Subject: [PATCH] finished help system --- game/Global.h | 2 + game/localplayer/Help.cpp | 248 ++++++++++++++++++++++++++++++++++++++ game/localplayer/Help.h | 8 ++ 3 files changed, 258 insertions(+) diff --git a/game/Global.h b/game/Global.h index fd6aaf4..ec7450e 100644 --- a/game/Global.h +++ b/game/Global.h @@ -45,7 +45,9 @@ constexpr const char *CevoExt = ".cevo"; constexpr const char *CevoTribeExt = ".tribe.txt"; constexpr const char *CevoHomepageShort = "app.zdechov.net/c-evo"; constexpr const char *CevoHomepage = "https://app.zdechov.net/c-evo"; +constexpr const char *CevoContactShort = "app.zdechov.net/c-evo#Support"; constexpr const char *CevoContactBug = "https://app.zdechov.net/c-evo/report/1"; +constexpr const char *AITemplateManual = "AI development manual"; constexpr const char *AITemplateFileName = "AI Template/AI development manual.html"; constexpr int CevoVersionMajor = 1; diff --git a/game/localplayer/Help.cpp b/game/localplayer/Help.cpp index b1c45fa..bcab1d9 100644 --- a/game/localplayer/Help.cpp +++ b/game/localplayer/Help.cpp @@ -67,6 +67,7 @@ std::function showNewContent; namespace { constexpr int MaxHist = 16; +constexpr int ScrollBarWidth = 16; // matches Dpi::ScrollBar's fixed width // Phrases/Phrases2 (StringTables.pas) aren't ported; disclosed // plain-English placeholders for this dialog's own chrome text. @@ -336,6 +337,8 @@ void HelpDlg::prepare(int sbPos) prepareMiscJobList(); } else if (kind == LinkCategory::Misc && static_cast(no) == MiscLinkIndex::Credits) { prepareMiscCredits(); + } else if (kind == LinkCategory::Text) { + addTextual(helpText.lookupByHandle(no)); } else { mainText.addLine(QStringLiteral("Detailed help content isn't available in this build.")); } @@ -750,6 +753,251 @@ void HelpDlg::prepareModel(int) mainText.addLine(helpText.lookup(QStringLiteral("MODELNOTE"))); } +void HelpDlg::decodeItem(const QString &item, LinkCategory &category, int &index) const +{ + // item is e.g. ":A47" (coded category+number) or "QUICK" (a plain + // Help.txt key name) - the caller already stripped the outer ':'/';' + // that marks a link token; a SECOND leading ':' here means "coded". + if (!item.isEmpty() && item.at(0) == QLatin1Char(':')) { + category = LinkCategory::Misc; + index = 0; + for (int i = 2; i < item.size(); ++i) + index = index * 10 + item.at(i).unicode() - QLatin1Char('0').unicode(); + if (item.size() >= 2) { + switch (item.at(1).toLatin1()) { + case 'A': category = LinkCategory::Adv; break; + case 'B': category = LinkCategory::Imp; break; + case 'T': category = LinkCategory::Ter; break; + case 'F': category = LinkCategory::Feature; break; + case 'E': category = LinkCategory::Internet; break; + case 'S': category = LinkCategory::Model; break; + case 'C': index = static_cast(MiscLinkIndex::Credits); break; + case 'J': index = static_cast(MiscLinkIndex::JobList); break; + case 'G': index = static_cast(MiscLinkIndex::GovList); break; + default: break; + } + } + if (category != LinkCategory::Misc && index == 0) + index = 200; + } else { + category = LinkCategory::Text; + index = helpText.getHandle(item.left(255)); + } +} + +void HelpDlg::addTextual(const QString &text) +{ + // Throwaway canvas just for measuring word-wrap widths (same pattern as + // BaseMessgDlg::splitText: never painted, just needed for text metrics). + QPixmap dummy(1, 1); + QPainter painter(&dummy); + Dpi::Canvas canvas; + canvas.bind(&painter); + canvas.font().assign(UniFont[static_cast(FontType::Normal)]); + + const auto charAt = [](const QString &str, int k) -> QChar { // Pascal S[k], 1-based, safe past end + return (k >= 1 && k <= str.size()) ? str.at(k - 1) : QChar(); + }; + + const int rightMargin = innerWidth - 16 - ScrollBarWidth; + QString s = text; + TextFormat followFormat = TextFormat::Normal; + + while (!s.isEmpty()) { + LinkCategory linkCategory = LinkCategory::NoLink; + int linkIndex = 0; + TextFormat currentFormat; + bool crossLink = false; + + if (s.at(0) == QLatin1Char('$')) { // window caption + int p = 1; + while (p < s.size() && s.at(p) != QLatin1Char('\\')) + ++p; + s.remove(0, p + 1); + continue; + } + if (s.at(0) == QLatin1Char('&')) { // standard block - not ported, skip + int p = 1; + while (p < s.size() && s.at(p) != QLatin1Char('\\')) + ++p; + s.remove(0, p + 1); + continue; + } + if (s.at(0) == QLatin1Char('@')) { // image - not rendered, skip + int p = s.size() >= 2 && s.at(1) == QLatin1Char('@') ? 2 : 1; + while (p < s.size() && s.at(p) != QLatin1Char('\\')) + ++p; + s.remove(0, p + 1); + continue; + } + + const QChar c0 = s.at(0); + if (c0 == QLatin1Char(':') || c0 == QLatin1Char(';')) { + int p = 1; // Pascal 1-based P + do { + ++p; + } while (!(p > s.size() || charAt(s, p) == QLatin1Char('\\') || charAt(s, p) == QLatin1Char(' '))); + decodeItem(s.mid(1, p - 2), linkCategory, linkIndex); + currentFormat = TextFormat::Normal; + if (linkCategory != LinkCategory::Text && linkIndex < 200) { + switch (linkCategory) { + case LinkCategory::Adv: currentFormat = TextFormat::AdvIcon; break; + case LinkCategory::Imp: currentFormat = TextFormat::SmallIcon; break; + case LinkCategory::Ter: currentFormat = TextFormat::Ter; break; + case LinkCategory::Feature: currentFormat = TextFormat::Feature; break; + case LinkCategory::Model: currentFormat = TextFormat::Model; break; + default: break; + } + } + crossLink = (c0 == QLatin1Char(':')); + if (p > s.size() || charAt(s, p) == QLatin1Char(' ')) + s.remove(0, p); + else + s.remove(0, p - 1); + } else if (c0 == QLatin1Char('!')) { + if (s.size() >= 2 && s.at(1) == QLatin1Char('!')) { + if (mainText.count() > 1) + mainText.lineFeed(); + followFormat = TextFormat::Caption; + currentFormat = TextFormat::Caption; + s.remove(0, 2); + } else { + followFormat = TextFormat::Section; + currentFormat = TextFormat::Section; + s.remove(0, 1); + } + } else if (c0 == QLatin1Char('-')) { + followFormat = TextFormat::NormalDot; + currentFormat = TextFormat::Dot; + s.remove(0, 1); + } else { + currentFormat = followFormat; + } + + if (s.isEmpty()) + break; + + const int ofs = followFormat == TextFormat::NormalDot ? (20 + 4 + 8) : 8; + int p = 0; + int l = 0; + do { + do { + ++p; + } while (p <= s.size() && charAt(s, p) != QLatin1Char(' ') && charAt(s, p) != QLatin1Char('\\')); + if (biColorTextWidth(canvas, s.left(p - 1)) <= rightMargin - ofs) + l = p - 1; + else + break; + } while (!(p >= s.size() || charAt(s, l + 1) == QLatin1Char('\\'))); + + const QString lineText = s.left(l); + if (linkCategory == LinkCategory::Internet) { + mainText.addLine(linkIndex == 1 ? QString::fromLatin1(AITemplateManual) + : linkIndex == 2 ? QString::fromLatin1(CevoHomepageShort) + : QString::fromLatin1(CevoContactShort), + currentFormat, 0, linkCategory, linkIndex, crossLink); + } else { + mainText.addLine(lineText, currentFormat, 0, linkCategory, linkIndex, crossLink); + } + if (l < s.size() && charAt(s, l + 1) == QLatin1Char('\\')) + followFormat = TextFormat::Normal; + s.remove(0, l + 1); + } +} + +void HelpDlg::prepareMiscMain() +{ + const auto addLink = [this](const QString &titleKey, LinkCategory category, int index) { + mainText.addLine(helpText.lookup(titleKey), TextFormat::Normal, 0, category, index, true); + mainText.lineFeed(); + }; + addLink(QStringLiteral("HELPTITLE_QUICKSTART"), LinkCategory::Text, textIndex(QStringLiteral("QUICK"))); + addLink(QStringLiteral("HELPTITLE_CONCEPTS"), LinkCategory::Text, textIndex(QStringLiteral("CONCEPTS"))); + addLink(QStringLiteral("HELPTITLE_TERLIST"), LinkCategory::Ter, 200); + addLink(QStringLiteral("HELPTITLE_JOBLIST"), LinkCategory::Misc, static_cast(MiscLinkIndex::JobList)); + addLink(QStringLiteral("HELPTITLE_TECHLIST"), LinkCategory::Adv, 200); + addLink(QStringLiteral("HELPTITLE_MODELLIST"), LinkCategory::Model, 0); + addLink(QStringLiteral("HELPTITLE_FEATURELIST"), LinkCategory::Feature, 200); + addLink(QStringLiteral("HELPTITLE_IMPLIST"), LinkCategory::Imp, 200); + addLink(QStringLiteral("HELPTITLE_UNIQUELIST"), LinkCategory::Imp, 201); + addLink(QStringLiteral("HELPTITLE_WONDERLIST"), LinkCategory::Imp, 202); + addLink(QStringLiteral("HELPTITLE_GOVLIST"), LinkCategory::Misc, static_cast(MiscLinkIndex::GovList)); + addLink(QStringLiteral("HELPTITLE_KEYS"), LinkCategory::Text, textIndex(QStringLiteral("HOTKEYS"))); + addLink(QStringLiteral("HELPTITLE_ABOUT"), LinkCategory::Text, textIndex(QStringLiteral("ABOUT"))); + mainText.addLine(helpText.lookup(QStringLiteral("HELPTITLE_CREDITS")), TextFormat::Normal, 0, LinkCategory::Misc, + static_cast(MiscLinkIndex::Credits), true); +} + +void HelpDlg::prepareMiscCredits() +{ + addTextual(helpText.lookup(QStringLiteral("CREDITS"))); + mainText.lineFeed(); + mainText.lineFeed(); + mainText.addLine(helpText.lookup(QStringLiteral("CRED_CAPAI")), TextFormat::Section); + const char *aiCredits = nullptr; + ClientTools::server(sGetAICredits, 0, 0, &aiCredits); + if (aiCredits) { + const QStringList lines = QString::fromUtf8(aiCredits).split(QLatin1Char('\n')); + for (const QString &creditLine : lines) + mainText.addLine(creditLine); + } + mainText.lineFeed(); + mainText.addLine(helpText.lookup(QStringLiteral("CRED_CAPLANG")), TextFormat::Section); + addTextual(helpText.lookup(QStringLiteral("AUTHOR"))); +} + +void HelpDlg::prepareMiscGovList() +{ + for (int i = 1; i <= nGov; ++i) { + const int gov = i % nGov; + mainText.addLine(phrases.lookup(QStringLiteral("GOVERNMENT"), gov), TextFormat::Section); + mainText.lineFeed(); + mainText.lineFeed(); + addTextual(helpText.lookup(QStringLiteral("GOVHELP"), gov)); + if (gov >= 2) { + mainText.addLine(phrases.lookup(QStringLiteral("ADVANCES"), GovPreq[gov]), TextFormat::AdvIconAsPreq, 0, + LinkCategory::Adv, GovPreq[gov], true); + mainText.text(mainText.count() - 1) = + formatS(helpText.lookup(QStringLiteral("REQUIRED")), mainText.text(mainText.count() - 1)); + } + if (i < nGov) { + mainText.lineFeed(); + mainText.lineFeed(); + } + } +} + +void HelpDlg::prepareMiscJobList() +{ + constexpr int nJobHelp = sizeof(JobHelp) / sizeof(JobHelp[0]); + for (int i = 0; i < nJobHelp; ++i) { + if (i > 0) { + mainText.lineFeed(); + mainText.lineFeed(); + } + const int job = JobHelp[i]; + mainText.addLine(phrases.lookup(QStringLiteral("JOBRESULT"), job), TextFormat::Section); + mainText.lineFeed(); + mainText.lineFeed(); + addTextual(helpText.lookup(QStringLiteral("JOBHELP"), i)); + int jobCost = -1; + switch (job) { + case jCanal: jobCost = CanalWork; break; + case jFort: jobCost = FortWork; break; + case jBase: jobCost = BaseWork; break; + default: break; + } + mainText.addLine(jobCost >= 0 ? formatS(helpText.lookup(QStringLiteral("JOBCOST")), movementToString(jobCost)) + : helpText.lookup(QStringLiteral("JOBCOSTVAR"))); + if (JobPreq[job] != preNone) { + mainText.addLine(phrases.lookup(QStringLiteral("ADVANCES"), JobPreq[job]), TextFormat::AdvIconAsPreq, 0, + LinkCategory::Adv, JobPreq[job], true); + mainText.text(mainText.count() - 1) = + formatS(helpText.lookup(QStringLiteral("REQUIRED")), mainText.text(mainText.count() - 1)); + } + } +} + void HelpDlg::line(Dpi::Canvas &canvas, int i, bool lit) { const int idx = scrollBar->position() + i; diff --git a/game/localplayer/Help.h b/game/localplayer/Help.h index c1de978..fdbc566 100644 --- a/game/localplayer/Help.h +++ b/game/localplayer/Help.h @@ -150,6 +150,14 @@ private: void prepareMiscGovList(); void prepareMiscJobList(); void prepareMiscCredits(); + // Help.pas's DecodeItem(): decodes a ":KEY"/":Xnn" link token (used + // inside addTextual()'s markup) into a category + index. + void decodeItem(const QString &item, LinkCategory &category, int &index) const; + // Help.pas's AddTextual(): parses Help.txt's inline markup (links, + // section/caption headers, bullet lists, line breaks) and word-wraps + // the result into mainText lines. Used by LinkCategory::Text pages and + // a few AddItem()-style single paragraphs elsewhere. + void addTextual(const QString &text); void search(const QString &searchString); void line(Dpi::Canvas &canvas, int i, bool lit); void paintTerrIcon(int x, int y, int xSrc, int ySrc);