diff --git a/overlay/rootfs/usr/bin/remedy b/overlay/rootfs/usr/bin/remedy index 812c3ff..a4a41de 100755 Binary files a/overlay/rootfs/usr/bin/remedy and b/overlay/rootfs/usr/bin/remedy differ diff --git a/overlay/rootfs/usr/bin/scoundrel b/overlay/rootfs/usr/bin/scoundrel new file mode 100755 index 0000000..23a5a19 Binary files /dev/null and b/overlay/rootfs/usr/bin/scoundrel differ diff --git a/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_games.sh b/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_games.sh index f99282c..e2e9d83 100755 --- a/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_games.sh +++ b/overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_games.sh @@ -33,5 +33,6 @@ write_line_app() { write_line_app snake "$icon_gam Snake" "snake" write_line_app tetris "$icon_gam Tetris" "tetris" +write_line_app scoundrel "$icon_gam Scoundrel" "scoundrel" write_line_app gambatte "$icon_gam Game Boy (Gambatte)" "gambatte" write_line_app remedy "$icon_gam ReMEdy (J2ME)" "remedy" diff --git a/prebuilt/games/usr/bin/remedy b/prebuilt/games/usr/bin/remedy index 812c3ff..a4a41de 100755 Binary files a/prebuilt/games/usr/bin/remedy and b/prebuilt/games/usr/bin/remedy differ diff --git a/prebuilt/games/usr/bin/scoundrel b/prebuilt/games/usr/bin/scoundrel new file mode 100755 index 0000000..23a5a19 Binary files /dev/null and b/prebuilt/games/usr/bin/scoundrel differ diff --git a/scripts/build-games.sh b/scripts/build-games.sh index c9b355e..c9d83f0 100755 --- a/scripts/build-games.sh +++ b/scripts/build-games.sh @@ -62,13 +62,19 @@ EOF mkdir -p /bananarchy/prebuilt/games/usr/bin - echo "[3/4] Compiling Snake, Tetris, Gambatte (GB/GBC), and ReMEdy (J2ME MIDP 2.0 / M3G 3D)..." + echo "[3/4] Compiling Snake, Tetris, Scoundrel, Gambatte (GB/GBC), and ReMEdy (J2ME MIDP 2.0 / M3G 3D)..." $CXX $OPT_CFLAGS -std=c++17 -Wall -Wextra \ /bananarchy/src/games/snake.cpp $(pkgconf --libs x11) \ -o /bananarchy/prebuilt/games/usr/bin/snake $CXX $OPT_CFLAGS -std=c++17 -Wall -Wextra \ /bananarchy/src/games/tetris.cpp $(pkgconf --libs x11) \ -o /bananarchy/prebuilt/games/usr/bin/tetris + $CXX $OPT_CFLAGS -std=c++20 -Wall -Wextra \ + $(pkgconf --cflags x11 xft pangoxft pango glib-2.0 gobject-2.0) \ + /bananarchy/src/games/scoundrel.cpp \ + -L"$SYSROOT/usr/lib" -lneoui \ + $(pkgconf --libs x11 xft pangoxft pango glib-2.0 gobject-2.0) \ + -o /bananarchy/prebuilt/games/usr/bin/scoundrel GAMBATTE_DIR="/bananarchy/src/games/gambatte" $CXX $OPT_CFLAGS -std=c++17 -DHAVE_STDINT_H -D__LIBRETRO__ \ @@ -100,6 +106,7 @@ EOF $STRIP --strip-unneeded \ /bananarchy/prebuilt/games/usr/bin/snake \ /bananarchy/prebuilt/games/usr/bin/tetris \ + /bananarchy/prebuilt/games/usr/bin/scoundrel \ /bananarchy/prebuilt/games/usr/bin/gambatte \ /bananarchy/prebuilt/games/usr/bin/remedy @@ -114,5 +121,5 @@ EOF /bananarchy/prebuilt/games \ /bananarchy/overlay/rootfs/usr/bin \ /bananarchy/src/games - ls -lh /bananarchy/prebuilt/games/usr/bin/snake /bananarchy/prebuilt/games/usr/bin/tetris /bananarchy/prebuilt/games/usr/bin/gambatte /bananarchy/prebuilt/games/usr/bin/remedy + ls -lh /bananarchy/prebuilt/games/usr/bin/snake /bananarchy/prebuilt/games/usr/bin/tetris /bananarchy/prebuilt/games/usr/bin/scoundrel /bananarchy/prebuilt/games/usr/bin/gambatte /bananarchy/prebuilt/games/usr/bin/remedy ' diff --git a/src/games/scoundrel.cpp b/src/games/scoundrel.cpp new file mode 100644 index 0000000..4083c2b --- /dev/null +++ b/src/games/scoundrel.cpp @@ -0,0 +1,1111 @@ +// SPDX-License-Identifier: CC0-1.0 OR GPL-2.0-only +// scoundrel.cpp - Native 240x320 X11/NeoUI port of the Scoundrel roguelike +// solitaire card game for Bananarchy Linux. +// Based on the official Scoundrel ruleset (Zach Gage & Kurt Bieg, 2011) and +// the C89/Game Boy ports by Luxferre (2026). + +#include + +#include +#include +#include +#include +#include +#include +#ifndef _EVDEVK +#define _EVDEVK(_v) (0x10081000 + (_v)) +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace bananarchy; + +namespace { + +volatile sig_atomic_t gRunning = 1; +volatile sig_atomic_t gReloadTheme = 0; + +void handleTerm(int) { + gRunning = 0; +} + +void handleUsr1(int) { + gReloadTheme = 1; +} + +bool isDuressActive() { + return ::access("/tmp/bananarchy_duress_mode", F_OK) == 0; +} + +std::string getScoreFilePath() { + if (isDuressActive()) { + ::mkdir("/tmp/duress_home", 0700); + ::mkdir("/tmp/duress_home/.local", 0700); + ::mkdir("/tmp/duress_home/.local/share", 0700); + return "/tmp/duress_home/.local/share/scoundrel_scores.txt"; + } + const char* home = std::getenv("HOME"); + std::string base = (home && home[0]) ? home : "/home/user"; + std::string localDir = base + "/.local"; + std::string shareDir = localDir + "/share"; + ::mkdir(localDir.c_str(), 0755); + ::mkdir(shareDir.c_str(), 0755); + return shareDir + "/scoundrel_scores.txt"; +} + +struct SavedStats { + int bestScore = -208; + int gamesPlayed = 0; + int gamesWon = 0; +}; + +SavedStats loadStats() { + SavedStats st; + std::ifstream in(getScoreFilePath()); + if (in.is_open()) { + in >> st.bestScore >> st.gamesPlayed >> st.gamesWon; + } + return st; +} + +void saveStats(const SavedStats& st) { + std::ofstream out(getScoreFilePath(), std::ios::trunc); + if (out.is_open()) { + out << st.bestScore << " " << st.gamesPlayed << " " << st.gamesWon << "\n"; + } +} + +enum class CardType { + Empty = 0, + MonsterSpade, + MonsterClub, + WeaponDiamond, + PotionHeart +}; + +struct CardInfo { + CardType type = CardType::Empty; + int level = 0; + std::string rankStr; +}; + +// Deck encoding matching Luxferre's scoundrel.c: +// 0 : empty +// 1 .. 13 : Monster (Spades, level = code + 1 -> 2..14) +// 14 .. 22 : Weapon (Diamonds, level = code - 12 -> 2..10) +// 23 .. 31 : Potion (Hearts, level = code - 21 -> 2..10) +// 32 .. 44 : Monster (Clubs, level = code - 30 -> 2..14) +CardInfo decodeCard(int code) { + CardInfo info; + if (code <= 0) { + return info; + } + if (code >= 1 && code <= 13) { + info.type = CardType::MonsterSpade; + info.level = code + 1; + } else if (code >= 14 && code <= 22) { + info.type = CardType::WeaponDiamond; + info.level = code - 12; + } else if (code >= 23 && code <= 31) { + info.type = CardType::PotionHeart; + info.level = code - 21; + } else if (code >= 32 && code <= 44) { + info.type = CardType::MonsterClub; + info.level = code - 30; + } + switch (info.level) { + case 11: info.rankStr = "J"; break; + case 12: info.rankStr = "Q"; break; + case 13: info.rankStr = "K"; break; + case 14: info.rankStr = "A"; break; + default: info.rankStr = std::to_string(info.level); break; + } + return info; +} + +const char* monsterNameForLevel(int level) { + switch (level) { + case 2: return "Rat"; + case 3: return "Bat"; + case 4: return "Kobold"; + case 5: return "Goblin"; + case 6: return "Skeleton"; + case 7: return "Orc"; + case 8: return "Bandit"; + case 9: return "Wraith"; + case 10: return "Gargoyle"; + case 11: return "Troll (J)"; + case 12: return "Vampire (Q)"; + case 13: return "Lich (K)"; + case 14: return "Dragon (A)"; + default: return "Monster"; + } +} + +const char* weaponNameForLevel(int level) { + switch (level) { + case 2: return "Dagger"; + case 3: return "Shortsword"; + case 4: return "Mace"; + case 5: return "Scimitar"; + case 6: return "Broadsword"; + case 7: return "Warhammer"; + case 8: return "Halberd"; + case 9: return "Greatsword"; + case 10: return "Runeblade"; + default: return "Weapon"; + } +} + +const char* winRankForScore(int score) { + if (score > 29) return "God"; + if (score > 24) return "Legend"; + if (score > 19) return "Epic"; + if (score > 9) return "Master"; + return "Survivor"; +} + +enum class UiModal { + Idle = 0, + WeaponPrompt, + RulesHelp, + QuitConfirm, + GameOver +}; + +class ScoundrelGame { + public: + int deck[44] {}; + int room[4] {}; + int deckIdx = 0; + int hp = 20; + int weapon = 0; + int durability = 0; + bool canRun = true; + int engaged = 0; + bool potionDrank = false; + int lastPotionVal = 0; + int roomsCleared = 0; + int score = -208; + bool lastRoom = false; + bool lastRoomClr = false; + bool wonGame = false; + + int selectedSlot = 0; + int pendingMonsterSlot = -1; + int pendingMonsterLevel = 0; + int weaponChoiceIdx = 0; // 0 = Use Weapon, 1 = Barehanded + UiModal modal = UiModal::Idle; + std::string statusLine1; + std::string statusLine2; + SavedStats stats; + + ScoundrelGame() { + seedRng(); + stats = loadStats(); + startNewGame(); + } + + void seedRng() { + unsigned int seed = static_cast(std::time(nullptr)) ^ + static_cast(::getpid()); + std::FILE* urnd = std::fopen("/dev/urandom", "rb"); + if (urnd) { + (void)std::fread(&seed, sizeof(seed), 1, urnd); + std::fclose(urnd); + } + std::srand(seed); + } + + static void shuffleArray(int* arr, int n) { + if (n > 1) { + for (int i = n - 1; i > 0; --i) { + int j = std::rand() % (i + 1); + int tmp = arr[j]; + arr[j] = arr[i]; + arr[i] = tmp; + } + } + } + + int remainingDeckCards() const { + int count = 0; + for (int i = 0; i < 44; ++i) { + if (deck[i] != 0) { + ++count; + } + } + return count; + } + + int remainingRoomCards() const { + int count = 0; + for (int i = 0; i < 4; ++i) { + if (room[i] != 0) { + ++count; + } + } + return count; + } + + void ensureValidSelection() { + if (selectedSlot >= 0 && selectedSlot < 4 && room[selectedSlot] != 0) { + return; + } + for (int i = 0; i < 4; ++i) { + if (room[i] != 0) { + selectedSlot = i; + return; + } + } + selectedSlot = 0; + } + + void dealRoomIfNeeded() { + if (!engaged) { + for (int i = 0; i < 4; ++i) { + if (!room[i]) { + if (deckIdx >= 44 || deck[deckIdx] == 0) { + lastRoom = true; + canRun = false; + } else { + room[i] = deck[deckIdx]; + deck[deckIdx] = 0; + ++deckIdx; + if (deckIdx >= 44 || deck[deckIdx] == 0) { + lastRoom = true; + canRun = false; + } + } + } + } + } + ensureValidSelection(); + } + + void startNewGame() { + deckIdx = 0; + for (int i = 0; i < 13; ++i) { + deck[i] = i + 1; // Spades Monsters (2..14) + deck[i + 13] = i + 32; // Clubs Monsters (2..14) + } + for (int i = 26; i < 44; ++i) { + deck[i] = i - 12; // 14..22 Weapons (2..10), 23..31 Potions (2..10) + } + shuffleArray(deck, 44); + + hp = 20; + weapon = 0; + durability = 0; + canRun = true; + engaged = 0; + potionDrank = false; + lastPotionVal = 0; + roomsCleared = 0; + score = -208; + lastRoom = false; + lastRoomClr = false; + wonGame = false; + modal = UiModal::Idle; + pendingMonsterSlot = -1; + pendingMonsterLevel = 0; + for (int i = 0; i < 4; ++i) { + room[i] = 0; + } + dealRoomIfNeeded(); + statusLine1 = "Entered the dungeon. Pick 3 of 4 cards."; + statusLine2 = "1-4/D-Pad: Card | *: Run | #: Rules"; + } + + void finishGame(bool won) { + wonGame = won; + if (won && hp > 0) { + score = hp; + if (hp == 20) { + score += lastPotionVal; + } + } + stats.gamesPlayed++; + if (won) { + stats.gamesWon++; + } + if (stats.gamesPlayed == 1 || score > stats.bestScore) { + stats.bestScore = score; + } + saveStats(stats); + modal = UiModal::GameOver; + } + + void checkRoomAdvance() { + int roomSum = 0; + for (int i = 0; i < 4; ++i) { + roomSum += room[i]; + } + if (hp <= 0) { + finishGame(false); + return; + } + if (roomSum == 0) { + if (lastRoom) { + lastRoomClr = true; + finishGame(true); + return; + } + engaged = 0; + potionDrank = false; + canRun = true; + ++roomsCleared; + statusLine2 = "Room cleared! Moving deeper..."; + dealRoomIfNeeded(); + } else if (!lastRoom && engaged == 3) { + engaged = 0; + potionDrank = false; + canRun = true; + ++roomsCleared; + statusLine2 = "Room cleared! 1 card carried over."; + dealRoomIfNeeded(); + } else { + ensureValidSelection(); + } + } + + void resolveMonsterCombat(int slot, int val, bool useWeapon) { + room[slot] = 0; + ++engaged; + canRun = false; + + int healthLost = val; + if (useWeapon && weapon > 0 && val <= durability) { + int diff = val - weapon; + if (diff < 0) diff = 0; + healthLost = diff; + durability = val - 1; + if (durability < 2) { + statusLine2 = "Weapon shattered after the blow!"; + durability = 0; + weapon = 0; + } else { + statusLine2 = "Weapon max monster level is now " + std::to_string(durability) + "."; + } + } else { + statusLine2 = "Fought barehanded (-" + std::to_string(healthLost) + " HP)."; + } + + hp -= healthLost; + if (hp < 0) hp = 0; + score += val; + statusLine1 = std::string("Slain ") + monsterNameForLevel(val) + + " (Lv." + std::to_string(val) + "), lost " + + std::to_string(healthLost) + " HP."; + checkRoomAdvance(); + } + + void playSlot(int slot) { + if (slot < 0 || slot >= 4 || room[slot] == 0) { + statusLine2 = "Empty card slot!"; + return; + } + selectedSlot = slot; + CardInfo ci = decodeCard(room[slot]); + + if (ci.type == CardType::MonsterSpade || ci.type == CardType::MonsterClub) { + if (weapon > 0 && ci.level <= durability) { + pendingMonsterSlot = slot; + pendingMonsterLevel = ci.level; + weaponChoiceIdx = 0; + modal = UiModal::WeaponPrompt; + return; + } + resolveMonsterCombat(slot, ci.level, false); + } else if (ci.type == CardType::WeaponDiamond) { + room[slot] = 0; + ++engaged; + canRun = false; + weapon = ci.level; + durability = 14; + statusLine1 = std::string("Equipped ") + weaponNameForLevel(weapon) + + " (Lv." + std::to_string(weapon) + ")."; + statusLine2 = "Weapon durability reset to max (Lv.14)."; + checkRoomAdvance(); + } else if (ci.type == CardType::PotionHeart) { + room[slot] = 0; + ++engaged; + canRun = false; + if (potionDrank) { + statusLine1 = "Potion discarded! (Max 1 per room)"; + statusLine2 = "Already drank a potion in this room."; + } else { + potionDrank = true; + int prevHp = hp; + hp += ci.level; + if (hp > 20) hp = 20; + if (deckIdx >= 44 || deck[deckIdx] == 0) { + lastPotionVal = ci.level; + } + statusLine1 = "Drank Lv." + std::to_string(ci.level) + + " Potion (+" + std::to_string(hp - prevHp) + " HP)."; + statusLine2 = "HP is now " + std::to_string(hp) + "/20."; + } + checkRoomAdvance(); + } + } + + void runFromRoom() { + if (!canRun || engaged > 0 || lastRoom) { + if (lastRoom) { + statusLine2 = "Cannot run from the final room!"; + } else if (engaged > 0) { + statusLine2 = "Cannot run after engaging a card!"; + } else { + statusLine2 = "Cannot run twice in a row!"; + } + return; + } + + int tempDeck[44] {}; + int tIdx = 0; + for (int i = 0; i < 44; ++i) { + if (deck[i] != 0) { + tempDeck[tIdx++] = deck[i]; + } + } + shuffleArray(room, 4); + for (int i = 0; i < 4; ++i) { + if (room[i] != 0) { + tempDeck[tIdx++] = room[i]; + room[i] = 0; + } + } + std::memset(deck, 0, sizeof(deck)); + for (int i = 0; i < tIdx; ++i) { + deck[i] = tempDeck[i]; + } + + deckIdx = 0; + canRun = false; + engaged = 0; + lastRoom = false; + dealRoomIfNeeded(); + statusLine1 = "Fled the room! Cards placed at bottom."; + statusLine2 = "Must clear this room before running again."; + } +}; + +void drawSuitSymbol(Display* dpy, Drawable d, GC gc, CardType type, int cx, int cy, int r) { + if (type == CardType::WeaponDiamond) { + XPoint pts[4] = { + {static_cast(cx), static_cast(cy - r)}, + {static_cast(cx + r), static_cast(cy)}, + {static_cast(cx), static_cast(cy + r)}, + {static_cast(cx - r), static_cast(cy)} + }; + XFillPolygon(dpy, d, gc, pts, 4, Convex, CoordModeOrigin); + } else if (type == CardType::PotionHeart) { + int hr = std::max(2, r / 2); + XFillArc(dpy, d, gc, cx - r, cy - r + 1, r, r, 0, 360 * 64); + XFillArc(dpy, d, gc, cx, cy - r + 1, r, r, 0, 360 * 64); + XPoint pts[3] = { + {static_cast(cx - r), static_cast(cy - hr + 2)}, + {static_cast(cx + r), static_cast(cy - hr + 2)}, + {static_cast(cx), static_cast(cy + r)} + }; + XFillPolygon(dpy, d, gc, pts, 3, Convex, CoordModeOrigin); + } else if (type == CardType::MonsterSpade) { + XPoint pts[3] = { + {static_cast(cx), static_cast(cy - r)}, + {static_cast(cx - r), static_cast(cy + 1)}, + {static_cast(cx + r), static_cast(cy + 1)} + }; + XFillPolygon(dpy, d, gc, pts, 3, Convex, CoordModeOrigin); + XFillArc(dpy, d, gc, cx - r, cy - 2, r, r - 1, 0, 360 * 64); + XFillArc(dpy, d, gc, cx, cy - 2, r, r - 1, 0, 360 * 64); + XFillRectangle(dpy, d, gc, cx - 2, cy + 2, 4, r - 1); + } else if (type == CardType::MonsterClub) { + int cr = std::max(3, (r * 3) / 5); + XFillArc(dpy, d, gc, cx - cr, cy - r, cr * 2, cr * 2, 0, 360 * 64); + XFillArc(dpy, d, gc, cx - r, cy - 2, cr * 2, cr * 2, 0, 360 * 64); + XFillArc(dpy, d, gc, cx + r - cr * 2, cy - 2, cr * 2, cr * 2, 0, 360 * 64); + XFillRectangle(dpy, d, gc, cx - 2, cy + 1, 4, r); + } +} + +void drawCardIllustration(Display* dpy, Drawable d, GC gc, const PluginTheme& theme, + const CardInfo& ci, int x, int y, int w, int h) { + int cx = x + w / 2; + int cy = y + h / 2; + if (ci.type == CardType::MonsterSpade || ci.type == CardType::MonsterClub) { + // Stylized monster skull / helm emblem + XSetForeground(dpy, gc, theme.spec.danger); + XFillArc(dpy, d, gc, cx - 12, cy - 13, 24, 20, 0, 360 * 64); + XFillRectangle(dpy, d, gc, cx - 8, cy + 1, 16, 8); + // Horns for high-level monsters (J/Q/K/A) + if (ci.level >= 11) { + XPoint lHorn[3] = { + {static_cast(cx - 10), static_cast(cy - 8)}, + {static_cast(cx - 18), static_cast(cy - 16)}, + {static_cast(cx - 6), static_cast(cy - 12)} + }; + XPoint rHorn[3] = { + {static_cast(cx + 10), static_cast(cy - 8)}, + {static_cast(cx + 18), static_cast(cy - 16)}, + {static_cast(cx + 6), static_cast(cy - 12)} + }; + XFillPolygon(dpy, d, gc, lHorn, 3, Convex, CoordModeOrigin); + XFillPolygon(dpy, d, gc, rHorn, 3, Convex, CoordModeOrigin); + } + // Eyes and jaw teeth + XSetForeground(dpy, gc, theme.spec.bgMain); + XFillRectangle(dpy, d, gc, cx - 7, cy - 5, 5, 4); + XFillRectangle(dpy, d, gc, cx + 2, cy - 5, 5, 4); + XDrawLine(dpy, d, gc, cx - 4, cy + 4, cx - 4, cy + 8); + XDrawLine(dpy, d, gc, cx, cy + 4, cx, cy + 8); + XDrawLine(dpy, d, gc, cx + 4, cy + 4, cx + 4, cy + 8); + } else if (ci.type == CardType::WeaponDiamond) { + // Stylized crossed sword / blade emblem + XSetForeground(dpy, gc, theme.spec.info); + XPoint blade[4] = { + {static_cast(cx), static_cast(cy - 15)}, + {static_cast(cx + 5), static_cast(cy - 8)}, + {static_cast(cx + 3), static_cast(cy + 5)}, + {static_cast(cx - 3), static_cast(cy + 5)} + }; + XFillPolygon(dpy, d, gc, blade, 4, Convex, CoordModeOrigin); + XSetForeground(dpy, gc, theme.spec.warning); + XFillRectangle(dpy, d, gc, cx - 10, cy + 5, 20, 3); + XFillRectangle(dpy, d, gc, cx - 2, cy + 8, 4, 6); + XFillArc(dpy, d, gc, cx - 4, cy + 12, 8, 5, 0, 360 * 64); + } else if (ci.type == CardType::PotionHeart) { + // Stylized healing potion flask + XSetForeground(dpy, gc, theme.spec.fgMain); + XFillRectangle(dpy, d, gc, cx - 4, cy - 15, 8, 4); + XFillRectangle(dpy, d, gc, cx - 3, cy - 11, 6, 4); + XSetForeground(dpy, gc, theme.spec.success); + XFillArc(dpy, d, gc, cx - 12, cy - 7, 24, 22, 0, 360 * 64); + // Plus cross inside flask + XSetForeground(dpy, gc, theme.spec.bgMain); + XFillRectangle(dpy, d, gc, cx - 2, cy - 1, 4, 10); + XFillRectangle(dpy, d, gc, cx - 5, cy + 2, 10, 4); + } +} + +} // namespace + +int main() { + struct sigaction sa {}; + sa.sa_handler = handleTerm; + sigemptyset(&sa.sa_mask); + sigaction(SIGTERM, &sa, nullptr); + sigaction(SIGINT, &sa, nullptr); + + struct sigaction saUsr1 {}; + saUsr1.sa_handler = handleUsr1; + sigemptyset(&saUsr1.sa_mask); + sigaction(SIGUSR1, &saUsr1, nullptr); + + NeoWindow neo; + if (!neo.create("scoundrel", "Scoundrel", "Scoundrel", + 0, 24, 240, 296, false)) { + return 1; + } + + Display* dpy = neo.dpy; + Window win = neo.win; + Pixmap pix = neo.pix; + GC gc = neo.gc; + PangoLayout* layoutMain = neo.layoutMain; + PangoLayout* layoutSmall = neo.layoutSmall; + PangoLayout* layoutNum = neo.layoutNum; + PluginTheme& theme = neo.theme; + + Atom wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False); + XSetWMProtocols(dpy, win, &wmDelete, 1); + + XMapRaised(dpy, win); + XFlush(dpy); + + ScoundrelGame game; + + auto render = [&]() { + constexpr int W = 240; + constexpr int H = 296; + + XSetForeground(dpy, gc, theme.spec.bgMain); + XFillRectangle(dpy, pix, gc, 0, 0, W, H); + + // 1. Top HUD Bar (Y=0..36) + XSetForeground(dpy, gc, theme.spec.bgHeader); + XFillRectangle(dpy, pix, gc, 0, 0, W, 36); + XSetForeground(dpy, gc, theme.spec.border); + XFillRectangle(dpy, pix, gc, 0, 35, W, 1); + + // HP Bar + Value + XftColor* hpCol = (game.hp >= 12) ? &theme.colGreen + : ((game.hp >= 6) ? &theme.colYellow : &theme.colRed); + std::string hpText = "HP " + std::to_string(game.hp) + "/20"; + neo.drawPango(layoutSmall, hpCol, 6, 2, 16, hpText, 82); + + int barX = 6, barY = 20, barW = 74, barH = 10; + XSetForeground(dpy, gc, theme.spec.bgAltOdd); + XFillRectangle(dpy, pix, gc, barX, barY, barW, barH); + int fillW = ( std::clamp(game.hp, 0, 20) * (barW - 2) ) / 20; + unsigned long hpPx = (game.hp >= 12) ? theme.spec.success + : ((game.hp >= 6) ? theme.spec.warning : theme.spec.danger); + if (fillW > 0) { + XSetForeground(dpy, gc, hpPx); + XFillRectangle(dpy, pix, gc, barX + 1, barY + 1, fillW, barH - 2); + } + XSetForeground(dpy, gc, theme.spec.border); + XDrawRectangle(dpy, pix, gc, barX, barY, barW - 1, barH - 1); + + // Center: Deck & Rooms cleared + std::string deckStr = "DECK: " + std::to_string(game.remainingDeckCards()); + std::string rmStr = "ROOMS: " + std::to_string(game.roomsCleared); + neo.drawPango(layoutSmall, &theme.colLight, 86, 2, 16, deckStr, 72, 1); + neo.drawPango(layoutSmall, &theme.colMuted, 86, 18, 16, rmStr, 72, 1); + + // Right: Equipped Weapon & Durability + if (game.weapon > 0) { + std::string wpnStr = "WPN +" + std::to_string(game.weapon); + std::string durStr = "MAX LV:" + std::to_string(game.durability); + neo.drawPango(layoutSmall, &theme.colBlue, 160, 2, 16, wpnStr, 74, 2); + neo.drawPango(layoutSmall, &theme.colYellow, 160, 18, 16, durStr, 74, 2); + } else { + neo.drawPango(layoutSmall, &theme.colMuted, 160, 2, 16, "NO WEAPON", 74, 2); + neo.drawPango(layoutSmall, &theme.colMuted, 160, 18, 16, "BAREHAND", 74, 2); + } + + // 2. Dungeon Room (4 Cards side-by-side, Y=42..166) + constexpr int cardY = 42; + constexpr int cardW = 54; + constexpr int cardH = 122; + for (int i = 0; i < 4; ++i) { + int cardX = 6 + i * 58; + int code = game.room[i]; + bool isSel = (i == game.selectedSlot && game.modal == UiModal::Idle); + + if (code == 0) { + XSetForeground(dpy, gc, theme.spec.bgAltEven); + XFillRectangle(dpy, pix, gc, cardX, cardY, cardW, cardH); + XSetForeground(dpy, gc, theme.spec.border); + XDrawRectangle(dpy, pix, gc, cardX, cardY, cardW - 1, cardH - 1); + neo.drawPango(layoutSmall, &theme.colMuted, cardX, cardY + 50, 20, + "EMPTY", cardW, 1); + continue; + } + + CardInfo ci = decodeCard(code); + unsigned long cardBg = isSel ? theme.spec.bgInputFocus : theme.spec.bgAltOdd; + XSetForeground(dpy, gc, cardBg); + XFillRectangle(dpy, pix, gc, cardX, cardY, cardW, cardH); + + unsigned long borderPx = theme.spec.border; + XftColor* suitCol = &theme.colWhite; + unsigned long suitPx = theme.spec.fgBright; + std::string typeTag; + if (ci.type == CardType::MonsterSpade || ci.type == CardType::MonsterClub) { + suitCol = &theme.colRed; + suitPx = theme.spec.danger; + typeTag = "MNST"; + } else if (ci.type == CardType::WeaponDiamond) { + suitCol = &theme.colBlue; + suitPx = theme.spec.info; + typeTag = "WEAP"; + } else if (ci.type == CardType::PotionHeart) { + suitCol = &theme.colGreen; + suitPx = theme.spec.success; + typeTag = "HEAL"; + } + + if (isSel) { + borderPx = theme.spec.accent; + XSetForeground(dpy, gc, borderPx); + XDrawRectangle(dpy, pix, gc, cardX, cardY, cardW - 1, cardH - 1); + XDrawRectangle(dpy, pix, gc, cardX + 1, cardY + 1, cardW - 3, cardH - 3); + } else { + XSetForeground(dpy, gc, borderPx); + XDrawRectangle(dpy, pix, gc, cardX, cardY, cardW - 1, cardH - 1); + } + + // Top-left rank + top-right suit symbol + neo.drawPango(layoutMain, suitCol, cardX + 4, cardY + 2, 20, ci.rankStr, 28); + XSetForeground(dpy, gc, suitPx); + drawSuitSymbol(dpy, pix, gc, ci.type, cardX + cardW - 12, cardY + 12, 6); + + // Center illustration + drawCardIllustration(dpy, pix, gc, theme, ci, cardX, cardY + 24, cardW, 44); + + // Type label & value + neo.drawPango(layoutSmall, &theme.colLight, cardX + 2, cardY + 72, 16, + typeTag, cardW - 4, 1); + + std::string effectStr; + XftColor* effCol = &theme.colYellow; + if (ci.type == CardType::MonsterSpade || ci.type == CardType::MonsterClub) { + if (game.weapon > 0 && ci.level <= game.durability) { + int dmg = std::max(0, ci.level - game.weapon); + effectStr = "-" + std::to_string(dmg) + " HP*"; + effCol = &theme.colYellow; + } else { + effectStr = "-" + std::to_string(ci.level) + " HP"; + effCol = &theme.colRed; + } + } else if (ci.type == CardType::WeaponDiamond) { + effectStr = "ATK " + std::to_string(ci.level); + effCol = &theme.colBlue; + } else if (ci.type == CardType::PotionHeart) { + effectStr = game.potionDrank ? "WASTE" : ("+" + std::to_string(ci.level) + " HP"); + effCol = game.potionDrank ? &theme.colMuted : &theme.colGreen; + } + neo.drawPango(layoutSmall, effCol, cardX + 2, cardY + 87, 16, + effectStr, cardW - 4, 1); + + // Key badge [1]..[4] at bottom of card + XSetForeground(dpy, gc, isSel ? theme.spec.selBg : theme.spec.bgAltOdd); + XFillRectangle(dpy, pix, gc, cardX + 2, cardY + cardH - 17, cardW - 4, 15); + std::string keyBadge = "[" + std::to_string(i + 1) + "]"; + neo.drawPango(layoutSmall, isSel ? &theme.colWhite : &theme.colMuted, + cardX + 2, cardY + cardH - 17, 15, keyBadge, cardW - 4, 1); + } + + // 3. Selected Card Inspector & Dungeon Log Panel (Y=170..268) + XSetForeground(dpy, gc, theme.spec.bgAltEven); + XFillRectangle(dpy, pix, gc, 6, 170, W - 12, 96); + XSetForeground(dpy, gc, theme.spec.border); + XDrawRectangle(dpy, pix, gc, 6, 170, W - 13, 95); + + std::string inspectHeader = "Slot [" + std::to_string(game.selectedSlot + 1) + "]: "; + int selCode = (game.selectedSlot >= 0 && game.selectedSlot < 4) + ? game.room[game.selectedSlot] + : 0; + if (selCode == 0) { + inspectHeader += "Empty"; + } else { + CardInfo sci = decodeCard(selCode); + if (sci.type == CardType::MonsterSpade || sci.type == CardType::MonsterClub) { + inspectHeader += std::string(monsterNameForLevel(sci.level)) + + " (Lv." + std::to_string(sci.level) + ")"; + } else if (sci.type == CardType::WeaponDiamond) { + inspectHeader += std::string(weaponNameForLevel(sci.level)) + + " (+" + std::to_string(sci.level) + " ATK)"; + } else if (sci.type == CardType::PotionHeart) { + inspectHeader += "Health Potion (+" + std::to_string(sci.level) + " HP)"; + } + } + neo.drawPango(layoutSmall, &theme.colYellow, 12, 174, 18, inspectHeader, W - 24); + + // Progress in current room + Run availability + Score + int cardsNeeded = game.lastRoom ? game.remainingRoomCards() : (3 - game.engaged); + if (cardsNeeded < 0) cardsNeeded = 0; + std::string roomProg = "Turn: " + std::to_string(game.engaged) + "/3 (" + + std::to_string(cardsNeeded) + " left) | "; + if (game.canRun && game.engaged == 0 && !game.lastRoom) { + roomProg += "*:Run OK"; + } else { + roomProg += "No Run"; + } + neo.drawPango(layoutSmall, &theme.colLight, 12, 193, 18, roomProg, W - 24); + + XSetForeground(dpy, gc, theme.spec.border); + XDrawLine(dpy, pix, gc, 10, 213, W - 11, 213); + + neo.drawPango(layoutSmall, &theme.colWhite, 12, 216, 18, game.statusLine1, W - 24); + neo.drawPango(layoutSmall, &theme.colMuted, 12, 234, 18, game.statusLine2, W - 24); + + std::string scoreFooter = "Score: " + std::to_string(game.score) + + " Best: " + std::to_string(game.stats.bestScore) + + " W/P: " + std::to_string(game.stats.gamesWon) + + "/" + std::to_string(game.stats.gamesPlayed); + neo.drawPango(layoutSmall, &theme.colBlue, 12, 250, 15, scoreFooter, W - 24); + + // 4. Modals + if (game.modal == UiModal::WeaponPrompt) { + int mx = 14, my = 56, mw = W - 28, mh = 176; + XSetForeground(dpy, gc, theme.spec.bgHeader); + XFillRectangle(dpy, pix, gc, mx, my, mw, mh); + XSetForeground(dpy, gc, theme.spec.accent); + XDrawRectangle(dpy, pix, gc, mx, my, mw - 1, mh - 1); + XDrawRectangle(dpy, pix, gc, mx + 1, my + 1, mw - 3, mh - 3); + + std::string title = std::string("Fight ") + + monsterNameForLevel(game.pendingMonsterLevel) + + " (Lv." + std::to_string(game.pendingMonsterLevel) + ")"; + neo.drawPango(layoutMain, &theme.colYellow, mx + 8, my + 8, 22, title, mw - 16, 1); + + int wpnDmg = std::max(0, game.pendingMonsterLevel - game.weapon); + int newDur = game.pendingMonsterLevel - 1; + std::string durEffect = (newDur < 2) + ? "Weapon breaks!" + : ("Max Lv -> " + std::to_string(newDur)); + + for (int opt = 0; opt < 2; ++opt) { + int oy = my + 40 + opt * 56; + bool sel = (game.weaponChoiceIdx == opt); + XSetForeground(dpy, gc, sel ? theme.spec.selBg : theme.spec.bgAltOdd); + XFillRectangle(dpy, pix, gc, mx + 10, oy, mw - 20, 48); + XSetForeground(dpy, gc, sel ? theme.spec.accent : theme.spec.border); + XDrawRectangle(dpy, pix, gc, mx + 10, oy, mw - 21, 47); + + if (opt == 0) { + std::string l1 = "[1] Use Weapon (+" + std::to_string(game.weapon) + ")"; + std::string l2 = "Lose " + std::to_string(wpnDmg) + " HP | " + durEffect; + neo.drawPango(layoutMain, &theme.colWhite, mx + 16, oy + 4, 20, l1, mw - 32); + neo.drawPango(layoutSmall, &theme.colYellow, mx + 16, oy + 25, 18, l2, mw - 32); + } else { + std::string l1 = "[2] Fight Barehanded"; + std::string l2 = "Lose " + std::to_string(game.pendingMonsterLevel) + + " HP | Keep Weapon"; + neo.drawPango(layoutMain, &theme.colWhite, mx + 16, oy + 4, 20, l1, mw - 32); + neo.drawPango(layoutSmall, &theme.colRed, mx + 16, oy + 25, 18, l2, mw - 32); + } + } + neo.drawSoftbar("Confirm", "Select", "Cancel", &theme.colYellow); + } else if (game.modal == UiModal::RulesHelp) { + int mx = 10, my = 38, mw = W - 20, mh = 228; + XSetForeground(dpy, gc, theme.spec.bgHeader); + XFillRectangle(dpy, pix, gc, mx, my, mw, mh); + XSetForeground(dpy, gc, theme.spec.accent); + XDrawRectangle(dpy, pix, gc, mx, my, mw - 1, mh - 1); + + neo.drawPango(layoutMain, &theme.colYellow, mx + 8, my + 6, 20, + "Scoundrel Rules", mw - 16, 1); + static const char* kRules[] = { + "Deck: 26 Monsters (2-14),", + "9 Weapons (2-10), 9 Potions.", + "Play 3 of 4 cards per room;", + "4th card carries to next room.", + "Weapons reduce Monster dmg,", + "but can only hit Monsters <=", + "last slain Monster's level - 1.", + "Max 1 Potion heals per room.", + "*: Run (puts 4 cards at bottom,", + "cannot run 2 rooms in a row).", + "Keys: 1-4/Arrows, *:Run, 0:New" + }; + for (int i = 0; i < 11; ++i) { + neo.drawPango(layoutSmall, &theme.colLight, mx + 8, my + 28 + i * 17, + 16, kRules[i], mw - 16); + } + neo.drawSoftbar("Resume", "OK", "Close", &theme.colYellow); + } else if (game.modal == UiModal::QuitConfirm) { + int mx = 20, my = 84, mw = W - 40, mh = 116; + XSetForeground(dpy, gc, theme.spec.bgHeader); + XFillRectangle(dpy, pix, gc, mx, my, mw, mh); + XSetForeground(dpy, gc, theme.spec.accent); + XDrawRectangle(dpy, pix, gc, mx, my, mw - 1, mh - 1); + + neo.drawPango(layoutMain, &theme.colYellow, mx + 8, my + 12, 22, + "Scoundrel Menu", mw - 16, 1); + neo.drawPango(layoutSmall, &theme.colWhite, mx + 12, my + 42, 18, + "[1 / L-Soft] New Game", mw - 24); + neo.drawPango(layoutSmall, &theme.colRed, mx + 12, my + 64, 18, + "[2 / OK] Quit to Desktop", mw - 24); + neo.drawPango(layoutSmall, &theme.colMuted, mx + 12, my + 86, 18, + "[R-Soft / Back] Cancel", mw - 24); + neo.drawSoftbar("New Game", "Quit", "Cancel", &theme.colYellow); + } else if (game.modal == UiModal::GameOver) { + int mx = 14, my = 52, mw = W - 28, mh = 188; + XSetForeground(dpy, gc, theme.spec.bgHeader); + XFillRectangle(dpy, pix, gc, mx, my, mw, mh); + unsigned long frameCol = game.wonGame ? theme.spec.success : theme.spec.danger; + XSetForeground(dpy, gc, frameCol); + XDrawRectangle(dpy, pix, gc, mx, my, mw - 1, mh - 1); + XDrawRectangle(dpy, pix, gc, mx + 1, my + 1, mw - 3, mh - 3); + + neo.drawPango(layoutNum, game.wonGame ? &theme.colGreen : &theme.colRed, + mx + 8, my + 10, 28, + game.wonGame ? "DUNGEON CLEARED!" : "YOU DIED!", mw - 16, 1); + + std::string subtitle = game.wonGame + ? (std::string("Rank: ") + winRankForScore(game.score)) + : game.statusLine1; + neo.drawPango(layoutMain, &theme.colYellow, mx + 8, my + 44, 22, + subtitle, mw - 16, 1); + + std::string scStr = "Final Score: " + std::to_string(game.score); + std::string bestStr = "Best Score: " + std::to_string(game.stats.bestScore); + std::string rmStr2 = "Rooms Cleared: " + std::to_string(game.roomsCleared); + std::string winRate = "Wins: " + std::to_string(game.stats.gamesWon) + + " / " + std::to_string(game.stats.gamesPlayed); + + neo.drawPango(layoutMain, &theme.colWhite, mx + 16, my + 76, 20, scStr, mw - 32); + neo.drawPango(layoutSmall, &theme.colBlue, mx + 16, my + 100, 18, bestStr, mw - 32); + neo.drawPango(layoutSmall, &theme.colLight, mx + 16, my + 120, 18, rmStr2, mw - 32); + neo.drawPango(layoutSmall, &theme.colMuted, mx + 16, my + 140, 18, winRate, mw - 32); + neo.drawPango(layoutSmall, &theme.colYellow, mx + 8, my + 164, 18, + "Orig: Z.Gage & K.Bieg | Port: Luxferre", mw - 16, 1); + + neo.drawSoftbar("New Game", "Play", "Quit", &theme.colGreen); + } else { + neo.drawSoftbar((game.canRun && game.engaged == 0 && !game.lastRoom) ? "Run (*)" : "Rules (#)", + "Play", + "Menu", + &theme.colYellow); + } + + neo.present(); + }; + + render(); + + int xfd = ConnectionNumber(dpy); + while (gRunning) { + if (gReloadTheme) { + gReloadTheme = 0; + neo.reloadTheme(); + render(); + } + + while (XPending(dpy) > 0) { + XEvent ev; + XNextEvent(dpy, &ev); + if (ev.type == ClientMessage && + static_cast(ev.xclient.data.l[0]) == wmDelete) { + gRunning = 0; + break; + } + if (ev.type == Expose && ev.xexpose.count == 0) { + neo.present(); + continue; + } + if (ev.type != KeyPress) { + continue; + } + + KeySym ks = XLookupKeysym(&ev.xkey, 0); + bool isLeftSoft = (ks == XK_F1 || ks == XK_Menu || + ks == _EVDEVK(0x08b) || ev.xkey.keycode == 147); + bool isRightSoft = (ks == XK_F2 || ks == XK_BackSpace || + ks == XF86XK_Back || ks == _EVDEVK(0x09e) || + ev.xkey.keycode == 166 || ev.xkey.keycode == 22); + bool isOk = (ks == XK_Return || ks == XK_KP_Enter || ks == XK_space); + bool isEnd = (ks == XK_Escape || ks == XF86XK_AudioLowerVolume || + ks == XF86XK_PowerOff || ks == _EVDEVK(0x197)); + + if (game.modal == UiModal::WeaponPrompt) { + if (ks == XK_Up || ks == XK_Down || ks == XK_Left || ks == XK_Right) { + game.weaponChoiceIdx = 1 - game.weaponChoiceIdx; + } else if (ks == XK_1) { + game.modal = UiModal::Idle; + game.resolveMonsterCombat(game.pendingMonsterSlot, + game.pendingMonsterLevel, true); + } else if (ks == XK_2) { + game.modal = UiModal::Idle; + game.resolveMonsterCombat(game.pendingMonsterSlot, + game.pendingMonsterLevel, false); + } else if (isOk || ks == XK_5 || isLeftSoft) { + bool useWpn = (game.weaponChoiceIdx == 0); + game.modal = UiModal::Idle; + game.resolveMonsterCombat(game.pendingMonsterSlot, + game.pendingMonsterLevel, useWpn); + } else if (isRightSoft || isEnd) { + game.modal = UiModal::Idle; + } + render(); + continue; + } + + if (game.modal == UiModal::RulesHelp) { + if (isOk || isLeftSoft || isRightSoft || isEnd || ks == XK_numbersign) { + game.modal = UiModal::Idle; + render(); + } + continue; + } + + if (game.modal == UiModal::QuitConfirm) { + if (ks == XK_1 || isLeftSoft) { + game.startNewGame(); + render(); + } else if (ks == XK_2 || isOk || ks == XK_5 || ks == XK_q || ks == XK_Q) { + gRunning = 0; + break; + } else if (isRightSoft || isEnd) { + game.modal = UiModal::Idle; + render(); + } + continue; + } + + if (game.modal == UiModal::GameOver) { + if (isOk || ks == XK_5 || ks == XK_1 || isLeftSoft) { + game.startNewGame(); + render(); + } else if (isRightSoft || isEnd || ks == XK_q || ks == XK_Q) { + gRunning = 0; + break; + } + continue; + } + + // Normal gameplay state + if (ks == XK_Left) { + for (int step = 1; step <= 4; ++step) { + int cand = (game.selectedSlot + 4 - step) % 4; + if (game.room[cand] != 0) { + game.selectedSlot = cand; + break; + } + } + } else if (ks == XK_Right) { + for (int step = 1; step <= 4; ++step) { + int cand = (game.selectedSlot + step) % 4; + if (game.room[cand] != 0) { + game.selectedSlot = cand; + break; + } + } + } else if (ks == XK_Up) { + game.selectedSlot = (game.selectedSlot + 3) % 4; + } else if (ks == XK_Down) { + game.selectedSlot = (game.selectedSlot + 1) % 4; + } else if (ks == XK_1 || ks == XK_a || ks == XK_A) { + game.playSlot(0); + } else if (ks == XK_2 || ks == XK_b || ks == XK_B) { + game.playSlot(1); + } else if (ks == XK_3 || ks == XK_c || ks == XK_C) { + game.playSlot(2); + } else if (ks == XK_4 || ks == XK_d || ks == XK_D) { + game.playSlot(3); + } else if (isOk || ks == XK_5) { + game.playSlot(game.selectedSlot); + } else if (ks == XK_asterisk || ks == XK_r || ks == XK_R) { + game.runFromRoom(); + } else if (ks == XK_0) { + game.modal = UiModal::QuitConfirm; + } else if (ks == XK_numbersign || ks == XK_h || ks == XK_H) { + game.modal = UiModal::RulesHelp; + } else if (isLeftSoft) { + if (game.canRun && game.engaged == 0 && !game.lastRoom) { + game.runFromRoom(); + } else { + game.modal = UiModal::RulesHelp; + } + } else if (isRightSoft || isEnd || ks == XK_q || ks == XK_Q) { + game.modal = UiModal::QuitConfirm; + } + + render(); + } + + if (!gRunning) { + break; + } + + struct pollfd pfd { xfd, POLLIN, 0 }; + ::poll(&pfd, 1, -1); + } + + neo.destroy(); + return 0; +}