Ticket #3044: refreshMapData.patch

File refreshMapData.patch, 1.7 KB (added by Alex, 9 years ago)

Note: This patch goes beyond refreshing the map data by validating player civ's and AI-assignments as well

  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

    diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js
    index fd9d9be..166b3a6 100644
    a b function loadGameAttributes()  
    715715    var mapName = attrs.map || "";
    716716    var mapSettings = attrs.settings;
    717717
     718    g_GameAttributes = attrs;
     719
    718720    // Assign new seeds and match id
    719721    attrs.matchID = Engine.GetMatchID();
    720722    mapSettings.Seed = Math.floor(Math.random() * 65536);
    721723    mapSettings.AISeed = Math.floor(Math.random() * 65536);
    722724
    723     // TODO: Check new attributes for being semantically correct.
    724     g_GameAttributes = attrs;
     725    var aiCodes = [ ai.id for each (ai in g_AIs) ];
     726    var civListCodes = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     727    civListCodes.push("random");
     728
     729    var playerData = mapSettings.PlayerData;
     730    if (playerData)
     731    {
     732        for (var i = 0; i < playerData.length; ++i)
     733        {
     734            // Discard previously unassigned/player slots
     735            if (aiCodes.indexOf(playerData[i].AI) < 0)
     736                playerData[i].AI = aiCodes[0];
     737
     738            // Validate player civs.
     739            if (civListCodes.indexOf(playerData[i].Civ) <0)
     740                playerData[i].Civ = "random";
     741        }
     742    }
     743
     744    // Refresh probably obsoleted/incomplete map data.
     745    var newMapData = loadMapData(mapName);
     746    if (newMapData && newMapData.settings)
     747    {
     748        for (var prop in newMapData.settings)
     749            mapSettings[prop] = newMapData.settings[prop];
     750
     751        // Keep player data
     752        if (playerData)
     753            mapSettings.PlayerData = playerData;
     754    }
    725755
    726756    var mapFilterSelection = Engine.GetGUIObjectByName("mapFilterSelection");
    727757    mapFilterSelection.selected = mapFilterSelection.list_data.indexOf(attrs.mapFilter);