• Our friends from AlphaCentauri2.info are in need of technical assistance. If you have experience with the LAMP stack and some hours to spare, please help them out and post here.

[TOTPP] Prof. Garfield's Lua Code Thread

I found the error. Again a stupid mistake by myself. I just forgot to add the brackets after "countBlacksmiths". now it works :)
Code:
if turn >=1 and turn <= 300 and object.pSpanishHabsburgEmpire.isHuman == true and countBlacksmiths() >= 2 then
        flag.setTrue("SpanishBlacksmithsBonus")
    end
You should be using countBlacksmiths(object.pSpanishHabsburgEmpire). Otherwise, you're counting cities with blacksmiths which also have nil owner.
 
@Prof. Garfield, would it also be possible to add permanent additional happy people via lua to the player's cities if a certain event is fired?
Have a look here. Note that this line
Code:
local defaultSizeUnhappiness = changeRules.authoritativeDefaultRules["civ.cosmic"].sizeUnhappiness
probably won't work for you, since I think your scenario predates the changeRules module. I think you would just use
Code:
local defaultSizeUnhappiness = 7
Of course, you must also change the if line to something suitable for your situation. Let me know if you need more help.
 
Hello @Prof. Garfield,

I've tried to use the code 'city.numHappy -> integer' for adding happy people in certain cities but unfortunately it doesn't work.
Currently I'm using the following code:
Code:
if object.pOttomanEmpire.isHuman == true and object.cMedina.owner == object.pOttomanEmpire and object.cMecca.owner == object.pOttomanEmpire then
    gen.justOnce("'Servant of the Two Holy Places'", function()
    local dialog = civ.ui.createDialog()
        dialog.title = "'Servant of the Two Holy Places'"
        dialog.width = 700
    local multiLineText = "After the succesful campaign against the Mamluk Sultanate ..."
        text.addMultiLineTextToDialog(multiLineText,dialog)
        dialog:addOption("It's a honour for us! (Mosques makes two additional people content)", 1)   
    local choice = dialog:show()
    if choice == 1 then           
    flag.setTrue("ServantoftheTwoHolyPlaces")   
    end   
    end)
end   

if object.pOttomanEmpire.isHuman == true and flag.value("ServantoftheTwoHolyPlaces") == true then
    local MosqueMakes2Happy   
    for city in civ.iterateCities() do
        if city.owner == object.pOttomanEmpire and
            city:hasImprovement(object.iMosque) then
            city.numHappy = city.numHappy + 10
        end
    end
end

Lua shows me the following error:
1684343771936.png
 
I've tried to use the code 'city.numHappy -> integer' for adding happy people in certain cities but unfortunately it doesn't work.
Currently I'm using the following code:
I just realised that you're trying to add happy citizens, not content citizens, and that might be impossible. city.numHappy is a field the game stores, so it can display the happiness adviser without recalculating all the cities. If you're going to do anything like this, it will have to be in the onCalculateCityYield event.

Maybe you could change the owner of hanging gardens and cure for cancer based on whether the city in question meets the special criteria or not.
 
Would it be possible to turn unhappy citizens into content ones?
That's what I actually would like to achieve

Unfortunately I have only found the two codes 'city.numHappy -> integer' and 'city.numUnhappy -> integer' in the Lua Function Reference table. And both doesn't work.
 
Would it be possible to turn unhappy citizens into content ones?
That's what I actually would like to achieve

Unfortunately I have only found the two codes 'city.numHappy -> integer' and 'city.numUnhappy -> integer' in the Lua Function Reference table. And both doesn't work.
You have to use the onCalculateCityYield execution point. This has its own file in the mechanics folder.

Within the onCalculateCityYield function, you change civ.cosmic.sizeUnhappiness. This will change how that individual city's happiness is computed. Something like this:
Code:
function cityYield.onCalculateCityYield(city,food,shields,trade)
    -- some code
    local defaultSizeUnhappiness = 7
    if city:hasImprovement(gen.original.iSAMMissileBattery) then
        civ.cosmic.sizeUnhappiness = defaultSizeUnhappiness +2
    else
      civ.cosmic.sizeUnhappiness = defaultSizeUnhappiness
    end
    -- more code
In this case, a SAM gives +2 content citizens, but you can change the condition to something else.
 
Just one question:
What does the value 'local defaultSizeUnhappiness = 7' actually mean?
If cities have the Mosque improvement, the unhappiness starts at a city size of 7?
No, this is a reference to this line in the rules.txt file:
Code:
7       ; City size for first unhappiness at Chieftain level
I just copy/pasted the code from a question I answered before. Originally, that line referenced the changeRules module, but I wasn't sure if your scenario has that module.

Basically, we need to know what value civ.cosmic.sizeUnhappiness "should" be, either to reset it, or to add the bonus to it.
 
@Prof. Garfield
@JPetroski
Hello, guys!
Been making strides with overlords max, but hit a little bug - !
1687700753883.png


The event that is causing the error is below - Not sure if I need something added in the Lua library to make it fire?

Code:
--If the British civ researches "African Invasion", the event will spawn British Empire forces to attack North Africa...
if civ.hasTech(object.tBritish, object.aBritAfricanInvasion) and object.tBritish.isHuman == false then
gen.justOnce("Britain Attacks Africa",function()
    --British Empire North African Forces
    civlua.createUnit(object.uInfantry, object.tBritish, {{283,67,0},{286,68,0},{2,66,0}}, {count=30, randomize=true, veteran=true})
    civlua.createUnit(object.uAnzacs, object.tBritish, {{283,67,0},{286,68,0},{2,66,0}}, {count=10, randomize=true, veteran=true})
    civlua.createUnit(object.uCanadianArmy, object.tBritish, {{283,67,0},{286,68,0},{2,66,0}}, {count=10, randomize=true, veteran=true})
    civlua.createUnit(object.uQF25Pdr, object.tBritish, {{283,67,0},{286,68,0},{2,66,0}}, {count=20, randomize=true, veteran=true})
    civlua.createUnit(object.uMatildaII, object.tBritish, {{283,67,0},{286,68,0},{2,66,0}}, {count=10, randomize=true, veteran=true})
    civlua.createUnit(object.uHurricane, object.tBritish, {{283,67,0},{286,68,0},{2,66,0}}, {count=10, randomize=true, veteran=true})
    civlua.createUnit(object.uLiner, object.tBritish, {{279,71,0},{282,68,0},{285,67,0}}, {count=2, randomize=false, veteran=true})
    civlua.createUnit(object.uDestroyer, object.tBritish, {{279,71,0},{282,68,0},{285,67,0}}, {count=4, randomize=false, veteran=true})
    civlua.createUnit(object.uBattleship, object.tBritish, {{279,71,0},{282,68,0},{285,67,0}}, {count=2, randomize=false, veteran=true})
    civlua.createUnit(object.uAircraftCarrier, object.tBritish, {{279,71,0},{282,68,0},{285,67,0}}, {count=1, randomize=false, veteran=true})
    civ.ui.text("Axis commanders radio Rome and Berlin, warning that British forces are landing in North Africa...")
    civ.playSound("ALLIED.wav")
    civ.ui.text("The British Empire lands in Africa! With the Soviets urging for a new theatre in the West, Commonwealth forces launch an invasion of North Africa...")
    end)
end

I suspect I have not set up the "justOnce" section correctly...

Code:
gen.justOnce("Britain Attacks Africa",function()

Does the function part need to reference something/somewhere in the Lua files?

Any help is most welcome!
 
Can you please upload the version of generalLibrary.lua that you are using for this scenario? (Should be in the LuaCore folder.)
 
OK, looks like I'll need your events.lua file. There's a function that links the "state table" (a table which is saved to the end of the game file) with the code in generalLibrary.lua, and it looks like that function hasn't been called for some reason.
 
Thanks!
I had a feeling something was missing...:)
No probs - Here are the TriggerEvents.
Sorry, I meant the events.lua file in your scenario's main directory, (same directory that your rules.txt file is in).
 
Try replacing your version of the general library with this. (My hypothesis about events.lua looks incorrect.) If it doesn't work, I'll need your entire scenario in order to track down the bug.
 

Attachments

@Prof. Garfield

Loaded with the new file, and Lau is launching a lot of "doAfterProduction" errors...I suspect my version of Lua might be very out of date.

Posting the prototype scen here, so you can check out the files. Thanks for your time on this. :)
 

Attachments

The scen has a save game as the neutrals, where the British have just gained the tech that is causing the error initially.
 
Back
Top Bottom