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 | } |