Ticket #1582: playernames.2.patch

File playernames.2.patch, 2.8 KB (added by O.Davoodi, 12 years ago)
  • gamesetup.js

     
    269269                updateGameAttributes();
    270270            }
    271271        };
    272        
    273272
    274273        // Set events
    275274        var civ = getGUIObjectByName("playerCiv["+i+"]");
     
    760759    // Assign random civilizations to players with that choice
    761760    //  (this is synchronized because we're the host)
    762761    var civs = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     762   
     763    // Get civ specific player names
     764    var civAINames = [ civ.AINames for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     765    for (var j = 0; j < civs.length; ++j)
     766        shuffleArray(civAINames[j])
     767   
    763768    for (var i = 0; i < numPlayers; ++i)
    764769    {
    765770        if (g_GameAttributes.settings.PlayerData[i].Civ == "random")
    766771            g_GameAttributes.settings.PlayerData[i].Civ = civs[Math.floor(Math.random()*civs.length)];
     772        // Check if the player is AI and the match is not a scenario
     773        if ((g_GameAttributes.mapType !== "scenario")&&(g_GameAttributes.settings.PlayerData[i].AI))
     774        {
     775            var civIndex;
     776            for (var j = 0; j < civs.length; ++j)
     777            {
     778                if (civs[j] == g_GameAttributes.settings.PlayerData[i].Civ)
     779                {
     780                    civIndex = j;
     781                    break;
     782                }
     783            }
     784           
     785            // Choose the name
     786            var usedName = 0;
     787            if (i < civAINames[civIndex].length);
     788            {
     789                var chosenName = civAINames[civIndex][i];
     790            }
     791            else
     792            {
     793                var chosenName = civAINames[civIndex][Math.floor(Math.random() * civAINames[civIndex].length)];
     794            }
     795            for (var j = 0; j < numPlayers; ++j)
     796            {
     797                if (g_GameAttributes.settings.PlayerData[j].Name.indexOf(chosenName) > -1)
     798                {
     799                    usedName++;
     800                }
     801            }
     802           
     803            // Assign civ specific names to AI players
     804            if (usedName)
     805            {
     806                g_GameAttributes.settings.PlayerData[i].Name = chosenName + " " + getRomanNumber(usedName+1);
     807            }
     808            else
     809            {
     810                g_GameAttributes.settings.PlayerData[i].Name = chosenName;
     811            }
     812        }
    767813    }
    768814   
    769815    if (g_IsNetworked)
     
    13191365    return false;
    13201366}
    13211367
     1368// "Inside-out" implementation of Fisher-Yates shuffle
     1369function shuffleArray(source)
     1370{
     1371    if (!source.length)
     1372        return [];
    13221373
     1374    var result = [source[0]];
     1375    for (var i = 1; i < source.length; i++)
     1376    {
     1377        var j = Math.floor(Math.random() * i);
     1378        result[i] = result[j];
     1379        result[j] = source[i];
     1380    }
     1381    return result;
     1382}
    13231383
     1384// A function to get roman numbers from 1 to 8 which is needed for AI name assignment
     1385function getRomanNumber(number)
     1386{
     1387    switch (number)
     1388    {
     1389    case 1:
     1390        return "I";
     1391   
     1392    case 2:
     1393        return "II";
    13241394
     1395    case 3:
     1396        return "III";
     1397
     1398    case 4:
     1399        return "IV";
     1400       
     1401    case 5:
     1402        return "V";
     1403       
     1404    case 6:
     1405        return "VI";
     1406       
     1407    case 7:
     1408        return "VII";
     1409       
     1410    case 8:
     1411        return "VIII";
     1412       
     1413    default:
     1414        return number;
     1415    }
     1416}
     1417
     1418
     1419