Ticket #1582: civ_names.patch

File civ_names.patch, 3.7 KB (added by Doménique, 12 years ago)

Adds a piece of code that adjusts default player names, if possible with names from a list

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

     
    759759    var numPlayers = g_GameAttributes.settings.PlayerData.length;
    760760    // Assign random civilizations to players with that choice
    761761    //  (this is synchronized because we're the host)
    762     var civs = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     762    var civs = [ civ for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
    763763    for (var i = 0; i < numPlayers; ++i)
    764     {
    765         if (g_GameAttributes.settings.PlayerData[i].Civ == "random")
    766             g_GameAttributes.settings.PlayerData[i].Civ = civs[Math.floor(Math.random()*civs.length)];
     764    {
     765        if (g_GameAttributes.settings.PlayerData[i].Civ == "random")
     766            g_GameAttributes.settings.PlayerData[i].Civ = civs[Math.floor(Math.random()*civs.length)].Code;
     767       
     768        // Assign a name to any player if it matches a default of "Player <number>"
     769        if ((g_GameAttributes.mapType !== "scenario") && (/^Player \d+$/).test(g_GameAttributes.settings.PlayerData[i].Name))
     770        {
     771            // Find the full civ info (object) based on the assigned civ code
     772            var playerCiv;
     773            civs.forEach(function (testedCiv)
     774            {
     775                if (testedCiv.Code === g_GameAttributes.settings.PlayerData[i].Civ)
     776                    playerCiv = testedCiv;
     777            });
     778
     779            // Only set name it if civ info is found to avoid trouble
     780            if (playerCiv !== undefined)
     781            {
     782                // When a list of AI names is available (set in civ JSON file) pick one
     783                // (at least 8 names required to avoid the need for doubles)
     784                if (playerCiv.AINames !== undefined && playerCiv.AINames.length >= 8)
     785                {
     786                    var nameIsNotUnique = true;
     787                    while (nameIsNotUnique) // loop needed for checking potential doubles
     788                    {
     789                        // Pick a name at random
     790                        g_GameAttributes.settings.PlayerData[i].Name = playerCiv.AINames[Math.floor(Math.random()*playerCiv.AINames.length)];
     791
     792                        // check for doubles, but skip the first time as it is always unique
     793                        if (i===0) break;
     794                        for (var k = 0; k < i; ++k)
     795                        {
     796                            if (g_GameAttributes.settings.PlayerData[k].Name === g_GameAttributes.settings.PlayerData[i].Name)
     797                            {
     798                                nameIsNotUnique = true;
     799                                break; // hope the next loop with a different name goes better
     800                            }
     801                            else
     802                                nameIsNotUnique = false; // unique name, move on
     803                        }
     804                    }
     805                    // Add civ name for clarity (a ruler's name only is hard to infer civ from)
     806                    g_GameAttributes.settings.PlayerData[i].Name += " ("+playerCiv.Name+")";
     807                }
     808                else // default to generic civ name
     809                {
     810                    g_GameAttributes.settings.PlayerData[i].Name = playerCiv.Name;
     811                   
     812                    // check for doubles, add number at end if necessary
     813                    var sameCivNameCounter = 0;
     814                    for (var k = 0; k < i; ++k)
     815                    {
     816                        if ( g_GameAttributes.settings.PlayerData[k].Name.indexOf(g_GameAttributes.settings.PlayerData[i].Name) !== -1)
     817                            sameCivNameCounter++;
     818                    }
     819                    if (sameCivNameCounter > 0)
     820                        g_GameAttributes.settings.PlayerData[i].Name += " " + getRomanNumber(sameCivNameCounter+1);
     821                }
     822            }
     823        }
    767824    }
    768825   
    769826    if (g_IsNetworked)
     
    13191376    return false;
    13201377}
    13211378
    1322 
    1323 
    1324 
     1379// A function to get roman numbers from 1 to 8 which is needed for AI name assignment
     1380function getRomanNumber(number)
     1381{
     1382    switch (number)
     1383    {
     1384    case 1:
     1385        return "I";
     1386   
     1387    case 2:
     1388        return "II";
     1389 
     1390    case 3:
     1391        return "III";
     1392 
     1393    case 4:
     1394        return "IV";
     1395       
     1396    case 5:
     1397        return "V";
     1398       
     1399    case 6:
     1400        return "VI";
     1401       
     1402    case 7:
     1403        return "VII";
     1404       
     1405    case 8:
     1406        return "VIII";
     1407       
     1408    default:
     1409        return number;
     1410    }
     1411}