Save map settings. Patch by @aBothe. Fixes #2963.

This was SVN commit r16089.
This commit is contained in:
leper 2014-12-31 00:21:24 +00:00
parent 8f387e0044
commit 2c20936ce3
4 changed files with 86 additions and 2 deletions

View File

@ -32,6 +32,9 @@ splashscreenversion = 0
; Pause the game on window focus loss (Only applicable to single player mode)
pauseonfocusloss = true
; Persist settings after leaving the game setup screen
persistmatchsettings = true
; Default player name to use in multiplayer
; playername = "anonymous"

View File

@ -401,9 +401,13 @@ function initMain()
// to allow easy keyboard selection of maps
Engine.GetGUIObjectByName("mapSelection").focus();
}
// Sync g_GameAttributes to everyone.
if (g_IsController)
{
loadGameAttributes();
// Sync g_GameAttributes to everyone.
updateGameAttributes();
}
}
function handleNetMessage(message)
@ -688,11 +692,74 @@ function loadMapData(name)
return g_MapData[name];
}
const FILEPATH_MATCHSETTINGS_SP = "config/matchsettings.json";
const FILEPATH_MATCHSETTINGS_MP = "config/matchsettings.mp.json";
function loadGameAttributes()
{
if (Engine.ConfigDB_GetValue("user", "persistmatchsettings") != "true")
return;
var settingsFile = g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP;
if (!Engine.FileExists(settingsFile))
return;
var attrs = Engine.ReadJSONFile(settingsFile);
if (!attrs || !attrs.settings)
return;
g_IsInGuiUpdate = true;
var mapName = attrs.map || "";
var mapSettings = attrs.settings;
// Assign new seeds and match id
attrs.matchID = Engine.GetMatchID();
mapSettings.Seed = Math.floor(Math.random() * 65536);
mapSettings.AISeed = Math.floor(Math.random() * 65536);
// TODO: Check new attributes for being semantically correct.
g_GameAttributes = attrs;
var mapFilterSelection = Engine.GetGUIObjectByName("mapFilterSelection");
mapFilterSelection.selected = mapFilterSelection.list_data.indexOf(attrs.mapFilter);
var mapTypeSelection = Engine.GetGUIObjectByName("mapTypeSelection");
mapTypeSelection.selected = mapTypeSelection.list_data.indexOf(attrs.mapType);
initMapNameList();
var mapSelectionBox = Engine.GetGUIObjectByName("mapSelection");
mapSelectionBox.selected = mapSelectionBox.list_data.indexOf(mapName);
if (mapSettings.PopulationCap)
{
var populationCapBox = Engine.GetGUIObjectByName("populationCap");
populationCapBox.selected = populationCapBox.list_data.indexOf(mapSettings.PopulationCap);
}
if (mapSettings.StartingResources)
{
var startingResourcesBox = Engine.GetGUIObjectByName("startingResources");
startingResourcesBox.selected = startingResourcesBox.list_data.indexOf(mapSettings.StartingResources);
}
g_IsInGuiUpdate = false;
onGameAttributesChange();
}
function saveGameAttributes()
{
var attributes = Engine.ConfigDB_GetValue("user", "persistmatchsettings") == "true" ? g_GameAttributes : {};
Engine.WriteJSONFile(g_IsNetworked ? FILEPATH_MATCHSETTINGS_MP : FILEPATH_MATCHSETTINGS_SP, attributes);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// GUI event handlers
function cancelSetup()
{
if (g_IsController)
saveGameAttributes();
Engine.DisconnectNetworkGame();
if (Engine.HasXmppClient())
@ -803,7 +870,6 @@ function selectMapType(type)
{
case "scenario":
// Set a default map
// TODO: This should be remembered from the last session
g_GameAttributes.mapPath = "maps/scenarios/";
g_GameAttributes.map = g_GameAttributes.mapPath + (g_IsNetworked ? DEFAULT_NETWORKED_MAP : DEFAULT_OFFLINE_MAP);
g_GameAttributes.settings.AISeed = Math.floor(Math.random() * 65536);
@ -984,6 +1050,8 @@ function launchGame()
}
}
saveGameAttributes();
if (g_IsNetworked)
{
Engine.SetNetworkGameAttributes(g_GameAttributes);

View File

@ -13,6 +13,7 @@ var options = {
[translate("FPS Overlay"), translate("Show frames per second in top right corner."), {"config":"overlay.fps"}, "boolean"],
[translate("Realtime Overlay"), translate("Show current system time in top right corner."), {"config":"overlay.realtime"}, "boolean"],
[translate("Gametime Overlay"), translate("Show current simulation time in top right corner."), {"config":"gui.session.timeelapsedcounter"}, "boolean"],
[translate("Persist match settings"), translate("Save and restore match settings for quick reuse when hosting an other game"), {"config":"persistmatchsettings"}, "boolean"],
],
"graphicsSetting":
[

View File

@ -842,6 +842,17 @@ CScriptVal ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring fil
return out.get();
}
void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring filePath, CScriptVal scriptVal)
{
JS::RootedValue val(pCxPrivate->pScriptInterface->GetContext(), scriptVal.get());
std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false));
VfsPath path(filePath);
WriteBuffer buf;
buf.Append(str.c_str(), str.length());
g_VFS->CreateFile(path, buf.Data(), buf.Size());
}
CParamNode GetTemplate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName)
{
return g_GUI->GetTemplate(templateName);
@ -1007,6 +1018,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface)
scriptInterface.RegisterFunction<int, &GetFps>("GetFPS");
scriptInterface.RegisterFunction<std::wstring, int, &GetBuildTimestamp>("GetBuildTimestamp");
scriptInterface.RegisterFunction<CScriptVal, std::wstring, &ReadJSONFile>("ReadJSONFile");
scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &WriteJSONFile>("WriteJSONFile");
scriptInterface.RegisterFunction<CParamNode, std::string, &GetTemplate>("GetTemplate");
// User report functions