civ2units
Emperor
Currently I'm using Prof.Garfield's LUA template. It makes things easier.
Would it be possible to create a lua script were certain units will give adjacent units a damage each turn?
For example I'm using non moveable fortress units. Every time when an enemy unit will move next to the fortress unit the enemy unit should suffering a small damage.
[/URL]![]()
Would it be easy to achieve a script like that? In best case an easy script which I will be able to understand how it works? I'm not a programmer so I would like to ask the experts here.
Here is some untested code that I think will do what you want.Well, the enemy unit should only suffering a damage each turn as long as it will stay right next to the fortress.
discreteEvents.onTribeTurnBegin(function (turn, tribe)
for fortress in civ.iterateUnits() do
if fortress.owner == tribe and fortress.type == object.uFortress then
for adjacentUnit in gen.nearbyUnits(fortress.location,1,fortress.location.z) do
if diplomacy.warExists(fortress.owner,adjacentUnit.owner) then
adjacentUnit.damage = adjacentUnit.damage + 1
if adjacentUnit.damage >= adjacentUnit.type.hitpoints then
gen.defeatUnit(adjacentUnit,fortress,fortress,adjacentUnit,adjacentUnit.location,fortress.veteran,adjacentUnit.veteran)
end
end
end
end
end
end)
console.onTribeTurnBegin()
Here is some untested code that I think will do what you want.
Code:discreteEvents.onTribeTurnBegin(function (turn, tribe) for fortress in civ.iterateUnits() do if fortress.owner == tribe and fortress.type == object.uFortress then for adjacentUnit in gen.nearbyUnits(fortress.location,1,fortress.location.z) do if diplomacy.warExists(fortress.owner,adjacentUnit.owner) then end adjacentUnit.damage = adjacentUnit.damage + 1 if adjacentUnit.damage >= adjacentUnit.type.hitpoints then gen.defeatUnit(adjacentUnit,fortress,fortress,adjacentUnit,adjacentUnit.location,fortress.veteran,adjacentUnit.veteran) end end end end end)
You will have to change object.uFortress to whatever the actual fortress unit type is, and I just set the damage to 1.
You can test this code without ending the turn using this console command
Code:console.onTribeTurnBegin()
Let me know if you have any questions or difficulties.
You're right. I'll edit the post.Shouldn't the end of "diplomacy.warExists" condition met be written after damage is dealt (and its potential consequences) ?
if turn >= 1 then
for fortress in civ.iterateUnits() do
if (fortress.owner == object.pFrench or fortress.owner == object.pOttomanEmpire or fortress.owner == object.pSpanishHabsburgEmpire) and fortress.type == object.uFortress then
for adjacentUnit in gen.nearbyUnits(fortress.location,1,fortress.location.z) do
if diplomacy.warExists(fortress.owner,adjacentUnit.owner) then
adjacentUnit.damage = adjacentUnit.damage + 10
if adjacentUnit.damage >= adjacentUnit.type.hitpoints then
gen.defeatUnit(adjacentUnit,fortress,fortress,adjacentUnit,adjacentUnit.location,fortress.veteran,adjacentUnit.veteran)
end
civ.ui.text("Our troops are under attack from a nearby fortress")
end
end
end
end
end
Have you made sure that the two tribes are at war? Actually at war, not just no peace treaty. That's what diplomacy.warExists checks for.I've tested it but it doesn't seem to work. The nearby unit won't be damaged. LUA itself doesn't show me any errors.
fortress.owner ~= adjacentUnit.owner
Time to add some 'print("code location")' in your code to see with the console what is not trigerringSorry for asking you again Prof.Garfield, but the code doesn't work.
I've an event which declares war to both civs, so they are actually at war. But the unit right next to the fortress unit doesn't suffering any damage.
I`m a little bit confused because LUA doesn't show me any errors.
Now, as it is located in the right directory the LUA console shows me an error
[/URL]![]()
"Attempt to call a nil value" means that you're trying to use a variable or table value like a function, but that value is actually nil. "field warExists" means that the table value associated with the key 'warExists' is causing the error.Unfortunately I don't understand the error
if diplomacy.warExists(fortress.owner,adjacentUnit.owner) then
local diplomacy = require("diplomacy")
local function fortress()
if turn >= 1 then
local function fortress()
for fortress in civ.iterateUnits() do
if fortress.owner == tribe and fortress.type == object.uFortress then
for adjacentUnit in gen.nearbyUnits(fortress.location,1,fortress.location.z) do
if diplomacy.warExists(fortress.owner,adjacentUnit.owner) then
adjacentUnit.damage = adjacentUnit.damage + 10
if adjacentUnit.damage >= adjacentUnit.type.hitpoints then
gen.defeatUnit(adjacentUnit,fortress,fortress,adjacentUnit,adjacentUnit.location,fortress.veteran,adjacentUnit.veteran)
end
civ.ui.text("Our troops are under attack from a nearby fortress")
end
end
end
end
end
end
local function fortress