Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions apps/oc4core/AutoSaveStem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once
// The start of OpenCiv4's own autosave names: AutoSave_<civilization>_<game>, so that each game
// keeps its own files (Documents\My Games\openciv4\Saves\auto).
//
// The civilization is the active player's short name as the game shows it
// (CvPlayer::getCivilizationShortDescription: the init core's name, else the civilization's own).
// The init core's name alone is empty for most games, and the init core's map seed is not in a
// save (CvInitCore::read/write), so after a load both were empty and every game shared
// AutoSave__00000000_<n>. The game is now told apart by its map: a hash of every plot's plot type
// (land, hills, peak, water), which a save keeps and which nothing in BtS changes during a game.

#include "CvGameCoreDLL.h"
#include "CvGameAI.h"
#include "CvGlobals.h"
#include "CvMap.h"
#include "CvPlayerAI.h"
#include "CvPlot.h"

#include <cstdint>
#include <cwctype>
#include <format>
#include <string>

namespace oc4::host
{
// FNV-1a over the map's size and its plots' types.
inline std::uint32_t mapLayoutHash(const CvMap& map)
{
std::uint32_t h = 2166136261u;
const auto add = [&h](int v) {
for (int i = 0; i < 4; ++i)
{
h ^= std::uint32_t(v >> (8 * i)) & 0xFFu;
h *= 16777619u;
}
};
add(map.getGridWidth());
add(map.getGridHeight());
// (the plot type from the exported isPeak/isHills/isWater, as PlotTypes numbers them:
// CvPlot::getPlotType is not exported from the Windows game core)
for (int i = 0; i < map.numPlots(); ++i)
if (const CvPlot* plot = map.plotByIndex(i))
add(plot->isPeak() ? int(PLOT_PEAK) : plot->isHills() ? int(PLOT_HILLS) : plot->isWater() ? int(PLOT_OCEAN) : int(PLOT_LAND));
return h;
}

// AutoSave_<civilization>_<game>, the civilization's name with anything but letters and digits as _.
inline std::wstring autoSaveStem(const CvGame& game)
{
std::wstring civ;
if (const PlayerTypes active = game.getActivePlayer(); active != NO_PLAYER)
for (const wchar_t* c = CvPlayerAI::getPlayerNonInl(active).getCivilizationShortDescription(); c && *c; ++c)
civ += std::iswalnum(*c) ? *c : L'_';
return std::format(L"AutoSave_{}_{:08X}", civ, mapLayoutHash(GC.getMap()));
}
}
11 changes: 4 additions & 7 deletions apps/oc4core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
// script and lets the AI play N turns.

#include "AutoEndTurn.h"
#include "AutoSaveStem.h"
#include "CrashHandler.h"
#include "EngineClasses.h"
#include "GameCoreImports.h"
Expand Down Expand Up @@ -910,13 +911,9 @@ namespace
return;
const fs::path folder = fs::path(documents) / L"My Games" / L"openciv4" / L"Saves" / L"auto";
CoTaskMemFree(documents);
// (each game its own: the active civilization and the game's map seed in the name;
// every 25th turn kept for good)
std::wstring civ;
if (const PlayerTypes active = game.getActivePlayer(); active != NO_PLAYER)
for (const wchar_t c : std::wstring(GC.getInitCore().getCivShortDesc(active).GetCString()))
civ += std::iswalnum(c) ? c : L'_';
const std::wstring stem = std::format(L"AutoSave_{}_{:08X}", civ, GC.getInitCore().getMapRandSeed());
// (each game its own: the active civilization and the game's map in the name,
// AutoSaveStem.h; every 25th turn kept for good)
const std::wstring stem = host::autoSaveStem(game);
const fs::path path = folder / std::format(L"{}_{}.CivBeyondSwordSave", stem, game.getGameTurn() % 10);
try
{
Expand Down
Loading