Ticket #1582: playernames.3.patch

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

     
    274274                updateGameAttributes();
    275275            }
    276276        };
    277        
    278277
    279278        // Set events
    280279        var civ = getGUIObjectByName("playerCiv["+i+"]");
     
    765764    // Assign random civilizations to players with that choice
    766765    //  (this is synchronized because we're the host)
    767766    var civs = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     767   
     768    // Get civ specific player names
     769    var civAINames = [ civ.AINames for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
     770    for (var j = 0; j < civs.length; ++j)
     771        shuffleArray(civAINames[j])
     772   
    768773    for (var i = 0; i < numPlayers; ++i)
    769774    {
    770775        if (g_GameAttributes.settings.PlayerData[i].Civ == "random")
    771776            g_GameAttributes.settings.PlayerData[i].Civ = civs[Math.floor(Math.random()*civs.length)];
     777        // Check if the player is AI and the match is not a scenario
     778        if ((g_GameAttributes.mapType !== "scenario")&&(g_GameAttributes.settings.PlayerData[i].AI))
     779        {
     780            var civIndex;
     781            for (var j = 0; j < civs.length; ++j)
     782            {
     783                if (civs[j] == g_GameAttributes.settings.PlayerData[i].Civ)
     784                {
     785                    civIndex = j;
     786                    break;
     787                }
     788            }
     789           
     790            // Choose the name
     791            var usedName = 0;
     792            if (i < civAINames[civIndex].length)
     793            {
     794                var chosenName = civAINames[civIndex][i];
     795            }
     796            else
     797            {
     798                var chosenName = civAINames[civIndex][Math.floor(Math.random() * civAINames[civIndex].length)];
     799            }
     800            for (var j = 0; j < numPlayers; ++j)
     801            {
     802                if (g_GameAttributes.settings.PlayerData[j].Name.indexOf(chosenName) > -1)
     803                {
     804                    usedName++;
     805                }
     806            }
     807           
     808            // Assign civ specific names to AI players
     809            if (usedName)
     810            {
     811                g_GameAttributes.settings.PlayerData[i].Name = chosenName + " " + getRomanNumber(usedName+1);
     812            }
     813            else
     814            {
     815                g_GameAttributes.settings.PlayerData[i].Name = chosenName;
     816            }
     817        }
    772818    }
    773819   
    774820    if (g_IsNetworked)
     
    13271373    return false;
    13281374}
    13291375
     1376// "Inside-out" implementation of Fisher-Yates shuffle
     1377function shuffleArray(source)
     1378{
     1379    if (!source.length)
     1380        return [];
    13301381
     1382    var result = [source[0]];
     1383    for (var i = 1; i < source.length; i++)
     1384    {
     1385        var j = Math.floor(Math.random() * i);
     1386        result[i] = result[j];
     1387        result[j] = source[i];
     1388    }
     1389    return result;
     1390}
    13311391
     1392// A function to get roman numbers from 1 to 8 which is needed for AI name assignment
     1393function getRomanNumber(number)
     1394{
     1395    switch (number)
     1396    {
     1397    case 1:
     1398        return "I";
     1399   
     1400    case 2:
     1401        return "II";
    13321402
     1403    case 3:
     1404        return "III";
     1405
     1406    case 4:
     1407        return "IV";
     1408       
     1409    case 5:
     1410        return "V";
     1411       
     1412    case 6:
     1413        return "VI";
     1414       
     1415    case 7:
     1416        return "VII";
     1417       
     1418    case 8:
     1419        return "VIII";
     1420       
     1421    default:
     1422        return number;
     1423    }
     1424}
     1425
     1426
     1427