[TOTPP] Lua Scenario Template

Is there anywhere to DL the new Lua template as a single zip?
 
Got it.
Cheers, dude.

BTW - I have been doing the classic rome tutorial, and it has opened up a lot of excellent features and potentials.
Just wondering though, the "events" file that packs with the Lua template is quite full compared to the tutorial version.

What is the more stripped down version of the events file you would recommend?

I prefer to start with an almost clean events sheet and add in what I need.
Refs like munitions are never going to be a concern for the scenario, so can I remove these
from the trigger events, and wipe the events file of the extra refs too?
 
Got it.
Cheers, dude.

BTW - I have been doing the classic rome tutorial, and it has opened up a lot of excellent features and potentials.
Just wondering though, the "events" file that packs with the Lua template is quite full compared to the tutorial version.

What is the more stripped down version of the events file you would recommend?

I prefer to start with an almost clean events sheet and add in what I need.
Refs like munitions are never going to be a concern for the scenario, so can I remove these
from the trigger events, and wipe the events file of the extra refs too?

First, understand that the lua lessons were written well before I envisioned the kind of template that currently exists. It should help you learn the basics (and some not so basics) of programming in Lua for Civ II, but we may have come up with better ideas since then. Of note is that there are functions in the General Library to cover most of the functionality that requires bitwise operations.

Don't change events.lua in the template version. That sort of ties everything else together, and adding new features to the template might change that file (I also recommend not changing stuff in Lua Core, so that fixes can simply replace existing files). For every kind of event, there should be a more or less empty file for you to use in either LuaRulesEvents or one of the subfolders of LuaTriggerEvents.

I (slightly arbitrarily) divided the Lua events in the template into "rules events" and "trigger events." The single lua file that most resembles what you seem to be looking for is in the LuaTriggerEvents directory and is called triggerEvents.lua. This file ties together the 'context events' (events that should change based on some "context", such as single player versus multiplayer), 'universal events' (events that should happen in all "contexts"), and the legacy events. You can write your events here instead of in the dedicated files of the universal events. That's what @JPetroski did in the Boudicca example.

Part of the reason for this use of many files is that we discovered while writing Over the Reich that a single events file can get very disorganised, so much so that I often searched for something related to what I wanted to do, so that I could find the name of the function that I actually wanted to change, and then search for that. We also used up all 200 local variables, which was a pain. (Each function can have 200 local variables, and we ran out in the 'function' that is the entire file.) To facilitate referencing stuff between files, the general library has the functions:

--#gen.getState()-->table
--#gen.getEphemeralTable()-->table
The ephemeral table is a table that doesn't persist between saved games, while the state table does persist.

I'd advise leaving everything in the template that is there. If you don't fill the tables in, they don't do anything anyway. It is possible that the parts are tied together to mesh multiple functionalities, though I can't think of an example off the top of my head. Part of the reason for the template is so that I don't have to explain exactly where everything needs to be tied into everything else.
 
Thanks for the response, Prof.
To be honest, facing the plethora of files, folders and evolving changes is daunting, compared to one macro file (limited and nerfed as it was).

As a future idea, It might be useful to update the ClassicRome example to reflect the current Lua setup, or at least the opening lessons...
I fear many potential CIV2 designers will be put off by not really knowing how to get started...A simple and clear excercise will help..

So forgive my pestering. Yourself and @JPetroski are the master wizards of this language. I'm still a pleb on the primer...:)

My goal was to find a good starting point and add simple events features like those in the early ClassicRome lessons, and apply them to my Imperialism scenario.
So given your advice, I will switch my efforts to the triggerEvents, and leave the main scenario folder events file untouched.

One more thing I wish to ask, sir.

I like the fact that most of the events are in their own file areas...I like the organisation.

But, which folder - ContextTriggerEvents, and the MainScenario folder within? (or the context folders?)
Or do I use UniversalTriggerEvents? Can you outline which to one I should use?

This is part of the what is confusing for me...:)
But I only need to ask once...The knowledge will be retained.

Your help is much appreciated.
 
I like the fact that most of the events are in their own file areas...I like the organisation.

But, which folder - ContextTriggerEvents, and the MainScenario folder within? (or the context folders?)
Or do I use UniversalTriggerEvents? Can you outline which to one I should use?

Use the files in UniversalTriggerEvents (by default, the context events are disabled anyway). The 'context' events are for something like the situation in @JPetroski's Cold War scenario. In that scenario, there are some substantial differences in events between Single Player and Multiplayer, and the context events are a way to avoid using batch files to change events.

You are correct that we need a proper tutorial that uses the template system.
 
Hi, @Prof. Garfield !

I tried to run some experiments in the onUnitKIlled file, and met with no effects when testing in a save.
I tried to recreate the "capture settler" and campaign combat cost from the Classic Rome lessons, which I had working fine in that system.

I have no doubt I am doing something drastically wrong, I have attached the Object and Events file for your inspection.
 

Attachments

Place your code inside the existing unitKilledEvents.unitKilledInCombat function in that file, to get
Code:
function unitKilledEvents.unitKilledInCombat(loser,winner,aggressor,victim,loserLocation,winnerVetStatus,loserVetSatus)
    for __,unit in pairs({loser,winner}) do
        if unit.type == uMilitia then
        unit.owner.money = math.max(unit.owner.money-militiaCampaignCost,0)
    elseif unit.type == uMarines then
        unit.owner.money = math.max(unit.owner.money-marinesCampaignCost,0)
    end
    end
    if loser.type == uLabourers then
        local newLabourers = civ.createUnit(uLabourers, winner.owner, winner.location)
    end
end

Also, omit the line
Code:
civ.scen.onUnitKilled(doWhenUnitKilled)
The equivalent line is in the events.lua file, so you don't have to worry about it. Having it twice means that the wrong function might be registered to run when a unit is killed. This version has a few extra available parameters, including loserLocation, since loser.location doesn't work if the attacker is the loser, and that had to be stored from the combat resolution function.
 
Nice one, and thanks...!

I updated the file, and it makes sense now...Always clearer when an expert explains. :)

I tested this in a save, (marine attacking a barb labourer) and it said the following error concerning the global table:

... TOT\1_Imperialism III Update\LuaCore\generalLibrary.lua:2729: The Global table doesn't have a value associated with Militia.
stack traceback:
[C]: in function 'error'
... TOT\1_Imperialism III Update\LuaCore\generalLibrary.lua:2729: in metamethod '__index'
...LuaTriggerEvents\UniversalTriggerEvents\onUnitKilled.lua:9: in function 'UniversalTriggerEvents\onUnitKilled.unitKilledInCombat'
...mperialism III Update\LuaTriggerEvents\triggerEvents.lua:113: in function 'triggerEvents.unitKilledInCombat'
N:\CIV2 TOT\1_Imperialism III Update\events.lua:201: in upvalue 'doOnUnitDefeatedInCombat'
N:\CIV2 TOT\1_Imperialism III Update\events.lua:269: in function <N:\CIV2 TOT\1_Imperialism III Update\events.lua:252>
 
I didn't notice when copying the code, but you'll need a couple more things.

At the top of the file, you'll need the line
Code:
local object = require("object")
and you'll need to replace
Code:
        if unit.type == uMilitia then
with
Code:
        if unit.type == object.uMilitia then
There are a couple other lines that need to be fixed like this. (The object file must also have the unit type defined.)

In programming, if you want to be able to access the same piece of memory from code defined in multiple files, "global variables" are used, and Lua does this with a special global table. Unfortunately, the way Lua handles global variables is that if you write a variable name that doesn't match a local variable you've declared, Lua assumes that you meant the entry in the global table with that key. So, rather than getting a message that would indicate you made a typo, you get whatever happens if that variable is evaluated as nil (which is often that the code doesn't generate errors, just that it doesn't do what you want).

As part of the template, I generate an error (The Global table doesn't have a value associated with Militia.) with most uses of the Global table, but I didn't know how to do this while writing the Lua lessons.
 
Nice! I got the campaign cost code to work, thanks! :)
This is one of the little changes which Lua brings, that has a big effect in the scenario.

Still getting errors with the settler capture event...How would you recommend recreating that code for the current Lua build?
 
These lines should work:
Code:
if loser.type == object.uLabourers then
    local newLabourers = civ.createUnit(object.uLabourers,winner.owner,winner.location)
end
 
Thank you, sir!

I have campaign cost tables for all the individual units, which are working nicely, although I realise many infantry units would all have the same cost.
I'd like to streamline the costs like in your ClassicRome lesson, except for certain powerful or elite units.

Can we put a cover-everything campaign cost for all attacks (like 10 gold) and add specific costs for big units like battleships, etc.
I recall this was what you done with the catapults and elephants in the tutorial, but I cannot get it to fire in current Lua.
 
I'd have to look at your current code to get an idea why it isn't working, but you'll want something like this

Code:
-- near top of file
local object = require("object")
local defaultCost = 5
local campaignCost = {}
campaignCost[object.uMyFirstUnit.id] = 7
campaignCost[object.uMySecondUnit.id] = 4

-- inside the unit killed code

for __,unit in pairs({winner,loser}) do
    fightingCost = campaignCost[unit.type.id] or defaultCost
    unit.owner.money = math.max(0,unit.owner.money-fightingCost)
end
What this does is specify a campaign cost for each unit type, indexed by id number. If a unit type is left out, the defaultCost is used. In lua A or B returns B if A is false or nil, so the fightingCost line returns the default if there is no value in the table.

If you want different values for attack and defence, get rid of the loop, and use winner and loser in place of unit. You'll also need another table of values.
 
I'd have to look at your current code to get an idea why it isn't working, but you'll want something like this

Code:
-- near top of file
local object = require("object")
local defaultCost = 5
local campaignCost = {}
campaignCost[object.uMyFirstUnit.id] = 7
campaignCost[object.uMySecondUnit.id] = 4

-- inside the unit killed code

for __,unit in pairs({winner,loser}) do
    fightingCost = campaignCost[unit.type.id] or defaultCost
    unit.owner.money = math.max(0,unit.owner.money-fightingCost)
end
What this does is specify a campaign cost for each unit type, indexed by id number. If a unit type is left out, the defaultCost is used. In lua A or B returns B if A is false or nil, so the fightingCost line returns the default if there is no value in the table.

If you want different values for attack and defence, get rid of the loop, and use winner and loser in place of unit. You'll also need another table of values.

Excellent, Prof - I will try this out, a simple and elegant solution. I will reserve the heavy costs for the big or exotic units. Ships, atom bombs, and the like.

Thanks once again for your help. Youself and Mr Petroski are definitely on the credits for my scenarios going forward.
 
Is it possible to change the city style of the barbarian civ? Maybe something like forcing them to use a specific style?

And is it also possible to set diplomatic relationships via LUA which won't change during the game?
I've a Christian Nations civ, which represents all the minor European Nations during the 16th century. They should only get into war with the Ottoman Empire. With all other major European Nations there should be a peace treaty during the complete game which can't be change.
 
With barbs, just reserve the classical style for them, and make it graphically what you wish. Unless you mean making them use a different model mid-game.

I'm sure @Prof. Garfield can offer something better, but I am sure Lua allows different models of civ-relations, using humanonly and computeronly talkers.
Meaning you can openly speak as the player to the Ottoman AI (for instance) and the computer player is restricted...As I said, he no doubt can offer a better explanation.
 
With barbs, just reserve the classical style for them, and make it graphically what you wish. Unless you mean making them use a different model mid-game.

I'm sure @Prof. Garfield can offer something better, but I am sure Lua allows different models of civ-relations, using humanonly and computeronly talkers.
Meaning you can openly speak as the player to the Ottoman AI (for instance) and the computer player is restricted...As I said, he no doubt can offer a better explanation.

Many thanks with the tip for the barbarian city style. It works now :thumbsup:

If LUA could realize something like you mentioned it would be great. I know that the relationships could be edited with the old macro language but I don't want to use it anymore.
I try to make everything available with LUA only as I see that it offers much more than the macro language ever could.
 
No problem, sir. :)
I am hoping for some guidance too on the negotiations in codes too.
And I totally agree on the macro thing. I want to slowly replace all events with Lua, as it is much more versatile.
 
Back
Top Bottom