Skip to content

Exit to Main Menu and Load Game stay in the same window - #177

Open
MortenLyhr wants to merge 1 commit into
bssth:masterfrom
MortenLyhr:fix/exit-to-menu-in-place
Open

MortenLyhr wants to merge 1 commit into
bssth:masterfrom
MortenLyhr:fix/exit-to-menu-in-place

Conversation

@MortenLyhr

@MortenLyhr MortenLyhr commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Clean room (#181): checked on 2026-11-25 against #181's clean-room rules and guides (CONTRIBUTING.md "Clean room", AGENTS.md). This PR was written before #181; its sources are below, in #181's terms.

What it is based on: the open-source game core (CvDLLButtonPopup.cpp, CvGameInterface.cpp, the players', teams' and map's reset). This exact flow was not checked in Wine, as the description says.

  • clean room: neither I nor any tool or AI agent I used looked inside the original game's executables or libraries (no disassembly, decompiling, debugging, strings), and nothing here comes from someone else's (CONTRIBUTING)

In the game menu, Exit to Main Menu and Load Game (also Ctrl+L, a quick load, and a load the game core asks for when it leaves) quit OpenCiv4 and started it again: the window closed, a new process started (--menu, --menu --menu-page load or --play --load <save>) and a new window opened. The original goes back to its main menu, or on to the loaded game, in the window it is in.

Why it restarted. This was the host's design from the start (main.cpp: g_relaunch, startAgain): wmain sets up one game and returns, so the next game was a new process. Nothing in the game core needs that. CvGame::init/read, CvMap::init/read, CvTeam::init/read and CvPlayer::init/read reset their objects first, and CvGame, CvMap, CvTeam, CvPlayer and CvInitCore export reset/init for the engine to tear a game down. A new process is only needed where the game core itself would change: a save of another mod (its own DLL), which the original also answers by restarting into that mod.

What changes

  • wmain plays games in a loop. When a game is left for the main menu (on its load page for Load Game) or for a save, the game is torn down and the menu or the save comes next, in the same MapWindow. --keys go on into the menu and the next game, so a whole session can be scripted.
  • GameSession::endGame puts the game core back as it was before the first game: each player's and team's reset, CvInitCore::init(NO_GAMEMODE), CvMap::reset with an empty CvMapInitData (no plots; the last game's map script is not asked), CvGame::reset. The init core matters: its active player is not saved, and CvGame::read claims the human's slot through CvGame::setActivePlayer, which does nothing when the active player is already that player. Without the reset, a save written after loading from the menu had the human's slot SLOTCLAIM_RESERVED instead of SLOTCLAIM_ASSIGNED.
  • Utility::endGame clears what the host kept of the game: the queued game messages, the diplomacy screen, and in the interface the selection, popups (queued and shown), messages, city screen, interface mode, WorldBuilder state and the camera's requests; the entities' walks, battles, actions, foundings, damage times and siege towers; the engine's plot marks, signs, effects, globe and camera turn. The display choices the player made (grid, resource and unit icons, yields, bare map) stay, as options do.
  • oc4screens.endGame (Python) closes every screen the game had up, the main interface too, and forgets their widgets, so the menu doesn't draw the last game's interface (it did: the scoreboard's flag widget asked a player with no civilization and crashed).
  • MapWindow::clearMap (and MapView::clear) empties the window for the menu: no map, no popup, no action buttons, help or messages, the camera unframed so the next game frames its own start. It also drops the game's helpers (action, popup and plot help), which pointed into locals of the game that was.
  • Unchanged: a save of another mod starts that mod's program (loadElsewhere, as before and as the original). A multiplayer game (hosted or joined) still starts the program again for whatever comes next, since its connections belong to the process.
  • The save format doesn't change.

Sources: the open-source game core (CvDLLButtonPopup.cpp: the game menu's buttons, BUTTONPOPUP_CONFIRM_MENU → exitingToMainMenu(), Load Game → CONTROL_LOAD_GAME; CvGameInterface.cpp: CONTROL_LOAD_GAME → gDLL->LoadGame(), CONTROL_QUICK_LOAD; CvInitCore.cpp: read turns SLOTCLAIM_ASSIGNED into SLOTCLAIM_RESERVED, setActivePlayer claims the slot; CvGame.cpp: read and setActivePlayer; CvMap.cpp: reset). The restart into a save's mod was seen in Wine earlier: loading a BUG save, the original offered to restart with BUG Mod (docs/wine-vs-openciv4-backlog.md, item 1). That the original stays in its window for Exit to Main Menu and Load Game is the user's report of the original; it was not filmed in Wine for this PR.

Checks

  • ctest 15/15. Checksum at turn 100 (--turns 100 --map Fractal --size STANDARD --seed 7): D7D43800, unchanged.
  • Before (master 8b7d8557, --play --load "TestMap Caesar" with BUG Mod, keys ~#1#0: Esc, Exit to Main Menu, Yes): the process ends and a new oc4core … --menu process is running (out/before.log).
  • After, scripted with --keys (BUG Mod, OC4_PLAY_LOG=1), all in one process and one window:
    • ~#1#0: the main menu, nothing of the game left on it (menu-after-exit.png; before clearMap dropped the window's popup, the game's "Are you sure?" stayed over it).
    • ~#1#0{MenuLoad}{MenuLoadGo}>>: back in the menu, the save loaded again, two turns played (t1.log, reload-played.png).
    • ^l{MenuLoadGo}…: Ctrl+L (the game menu's Load Game) opens the menu on its load page; the save loads (c1.log).
    • ~#1#0{MenuNew}{MenuLaunch}: a new game (Continents, 84x52) after the 150x104 save, framed on its settler, minimap as in a fresh new game (new-game-after-exit.png).
    • A save written right after loading from the menu (~#1#0{MenuLoad}{MenuLoadGo}^s) equals one written after a fresh load (^s), part by part (savediff.py): init core and Python data the same, the game data different only in byte 20, which also differs between two fresh loads (the turn's timing). After three turns played (>>>^s), the two differ in the same 11 bytes as two fresh runs do.
  • Three exits in a row and a new game in one process (cycles.log): load, menu, load, menu, load, menu, new game, two turns.
  • Also run on a build with our other open PRs merged (bundles Bundle 4/6: the options screen #148–Bundle 6/6: screens and advisors #150, Popups wait while a screen of its own is up (a new tech's splash) #111/Keys and clicks go to the screen up, not the map under it (Enter on a tech's splash) #167, Map yields: faded on unworked plots, larger on worked ones, as the original's #164, City screen: Left and Right go to the previous and the next city, as the original's #168–Game Details and Your Details open their popups #176), BUG + BULL: exit/new game and load/load cycles, nothing of the old game left (screens, dot map, globe, waiting popups, city screen, selection).
  • Not checked: the original in Wine for this exact flow; quick load (Shift+F8, which --keys can't type), a load the game core asks for with a file (exitingToMainMenu(file)), loading another mod's save from inside a game (the menu's own path for that is unchanged), multiplayer.

🤖 Generated with Claude Code

The game menu's Exit to Main Menu, its Load Game (and Ctrl+L), a quick load
and a load from the game left the program and started it again: the window
closed and a new one opened. The original goes back to its main menu, or on
to the loaded game, in the same window.

Now the game is torn down in the same process and the next one starts there:
- GameSession::endGame resets the game core as at the start (players, teams,
  init core, map, game: the DLL's exported resets). The init core's active
  player must go too: else a loaded game's setActivePlayer does nothing and its
  slot stays reserved instead of assigned (a save written after differs).
- Utility::endGame clears what the host kept of the game (queued messages,
  selection, popups, messages, city screen, battles and walks, plot marks,
  signs, globe, camera turn).
- oc4screens.endGame closes the game's screens and the main interface.
- MapWindow::clearMap empties the window for the menu and drops the game's
  helpers (popup, action and plot help), which pointed into the game that was.
Another mod's save still starts that mod's program, as the original restarts
into a save's mod; a multiplayer game still starts again (its connections).

Checked: a save written right after a load from the menu, and after three
turns played from there, equals one written after a fresh load (but for the
turn timing bytes, which also differ between two fresh loads).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant