Question from the LUA masters

Okay... that I have my spaz attack out of the way...

I cannot find the lua file that has the code to actually build/found a city.

I found the button for it (the unit lua file)... I've found the rename city code... But I looked through all the lua files (I think) and couldn't find any code anywhere for the actual founding and initial naming of cities...

So;
1) what file has that info?
2) where did you find Players[ 0 ] : AddCityName ( "newcityname" );
3) Where the hell would I put it?

(I'm stubborn... sometimes. I will figure this out no matter how many keyboards I go through! :s)
 
1-this goes back to the start of the thread about hijacking the process, which theres no function to do that.. the settler founds and it pulls a name from the city list

2-i ran across AddCityName when going through the tuner's table browser. Players[ 0 ] is player 0 (the human) and if you look at most of the lua file, it's usually transformed into player:

3- Players[ 0 ] : AddCityName ( "newcityname" )
is a quick one line you can paste into the tuner console and plop a city and see what happens
 
Does the city name randomization work? I think it's a good idea, it's boring that you always get the same sequence of names when founding cities. So if it works please tell me what code I should add and to which file, as I'm going to use it in my mod :)
 
Does the city name randomization work? I think it's a good idea, it's boring that you always get the same sequence of names when founding cities. So if it works please tell me what code I should add and to which file, as I'm going to use it in my mod :)

In theory it'll work... but I simply don't have the LUA knowledge to make it work... yet...

Mummy has been leading me by the nose to it, but it's still frustrating. :(

If/When I get it to work, you're more than welcome to use it. :)
 
Code:
-- Test if a player has a city named....
function HasCityNamed(pPlayer, cityName)
        for city in pPlayer:Cities() do
             if(city:GetName() == cityName) then
                  return true;
             end
        end

        return false;
end

-- called whenever a city is founded
function OnCityCreated ( hexPos, playerID, cityID, cultureType, eraType, culture, population, size, fowState )
	
	local pPlayer = Players[playerID];
	local pCity = pPlayer:GetCityByID(cityID);
        local civType = pPlayer:GetCivilizationType();
        local civNamesWeighted = {};

        -- Add the civ's city names to a list, adding them multiple times based on the weight so higher weight is more likely to be picked.
        for k, v in GameInfo.MyCivNames("CivName = '"..civType.."'") do
            for i = 0, v.Weight+1, 1 do
                table.insert(civNamesWeighted, v.CityName);
            end
        end

        local chosenName = ""

        -- keep picking from weighted list till we get a unique one
        repeat      
             
            chosenName = civNamesWeighted[math.random(1, #civNamesWeighted )];
   
        until not HasCityNamed(pPlayer, chosenName)

        -- set the city name.
        pCity:SetName(chosenName);
	
end
Events.SerialEventCityCreated.Add( OnCityCreated );

Should do it (disclaimer, typed into reply box, may have syntax bugs :D)

Assuming your table is called 'MyCivNames' but just change that to suit... if you stick this in a lua file and make it a GameUIAddin or whatever (see Kael's guide) it should do the trick... if there are any errors reported in the tuner just let me know. (also, don't make the weights too big, this method of weighting means it's adding them multiple times to the list, which means if you did a weight of 1000 or something it would be damn slow. Negative numbers would also never be picked, so just use 0-10) I'm sure there are better ways to do the weighting but this is all that springs to mind.

You could optimize it by building the weighted city list at game start, but since it only occurs on city creation it probably won't be an issue.
 
thing with using citycreated is that the game will still say city original name got founded, and i'd guess on slower machines some people would actually see the orginal name before the switch. and then theres also a need to check if a city was captured or not if thats going to be applied to the renaming

build a weighted list at game start and add each with player:AddCityName(str) skips over trying to catch the city founding entirely. AddCityName clears out the existing list of citynames and replaces them with whatever gets added
 
Back
Top Bottom