Ticket #1582: playernamespatch.patch

File playernamespatch.patch, 2.6 KB (added by O.Davoodi, 12 years ago)
  • common/functions_utility.js

     
    239239        }
    240240    }
    241241}
     242
     243// ====================================================================
     244// "Inside-out" implementation of Fisher-Yates shuffle
     245function shuffleArray(source)
     246{
     247    if (!source.length)
     248        return [];
     249
     250    var result = [source[0]];
     251    for (var i = 1; i < source.length; ++i)
     252    {
     253        var j = Math.floor(Math.random() * i);
     254        result[i] = result[j];
     255        result[j] = source[i];
     256    }
     257    return result;
     258}
     259 No newline at end of file
  • gamesetup/gamesetup.js

     
    767767    // Assign random civilizations to players with that choice
    768768    //  (this is synchronized because we're the host)
    769769    var civs = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     770   
     771    // Get civ specific player names
     772    var civAINames = [ civ.AINames for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     773    for (var j = 0; j < civs.length; ++j)
     774        if (civAINames[j] !== undefined)
     775            shuffleArray(civAINames[j])
     776        else
     777            civAINames[j] = ["Player"];
     778       
    770779    for (var i = 0; i < numPlayers; ++i)
    771780    {
    772781        if (g_GameAttributes.settings.PlayerData[i].Civ == "random")
    773782            g_GameAttributes.settings.PlayerData[i].Civ = civs[Math.floor(Math.random()*civs.length)];
     783        // Check if the player is AI and the match is not a scenario
     784        if ((g_GameAttributes.mapType !== "scenario")&&(g_GameAttributes.settings.PlayerData[i].AI))
     785        {
     786            var civIndex;
     787            for (var j = 0; j < civs.length; ++j)
     788            {
     789                if (civs[j] == g_GameAttributes.settings.PlayerData[i].Civ)
     790                {
     791                    civIndex = j;
     792                    break;
     793                }
     794            }
     795           
     796            // Choose the name
     797            var usedName = 0;
     798            if (i < civAINames[civIndex].length)
     799                var chosenName = civAINames[civIndex][i];
     800            else
     801                var chosenName = civAINames[civIndex][Math.floor(Math.random() * civAINames[civIndex].length)];
     802            for (var j = 0; j < numPlayers; ++j)
     803                if (g_GameAttributes.settings.PlayerData[j].Name.indexOf(chosenName) !== -1)
     804                    usedName++;
     805           
     806            // Assign civ specific names to AI players
     807            if (usedName)
     808                g_GameAttributes.settings.PlayerData[i].Name = chosenName + " " + [undefined, "I", "II", "III", "IV", "V", "VI", "VII", "VIII"][usedName+1];
     809            else
     810                g_GameAttributes.settings.PlayerData[i].Name = chosenName;
     811        }
    774812    }
    775813   
    776814    if (g_IsNetworked)
     
    13561394    }
    13571395    return false;
    13581396}
    1359 
    1360 
    1361 
    1362