
I need some help with some code. I can only get it to do part of what I want, and I can't figure out what I'm doing wrong. Or how to do if differently.
I wrote 2 different versions, 1 based on the code you wrote for BEF, the other based on Gallieni. They neither do exactly what I want, and I've been trying to merge them, but it doesn't work.
It should work as follows: When the event triggers, an American unit should spawn in Verdun, if Verdun is occupied (which it most likely will be), 1 American unit should spawn in 1 hex range of Verdun as long as there is an unoccupied Entente hex available. If there's no unoccupied hex or no unoccupied Entente hex available, the unit should spawn in the port of Brest, if Brest's port is also unavailable, it should spawn in Nantes.
The code based on the BEF does only the latter part, if Verdun is full, a unit will spawn in Brest, if Brest is full, it'll spawn in Nantes.
Code: Select all
--------------------------------------------
if eventname == "BuildUSArmy2" then
local brest = GetHex(70, 31)
local brestport = GetHex(69, 30)
local nantes = GetHex(74, 34)
local nantesport = GetHex(73, 34)
local cherbourg = GetHex(76, 28)
local cherbourgport = GetHex(75, 28)
local portsmouth = GetHex(76, 25)
local portsmouthport = GetHex(77, 25)
local paris = GetHex(82, 30)
local verdun = GetHex(87, 29)
--local Gvalue = 1
--local success = 0
if MatchType() <= 2 then
--local unitcounter = 1
if verdun.unit == nil
and verdun.alliance.id == 1 then
if GetEvent("VerdunCaptured") <= 0 then
GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
end
elseif brestport.unit == nil
and brest.alliance.id == 1 then
GetPlayer(SpawnUnit(1, usa, 69, 30, 1, 100))
elseif nantesport.unit == nil
and nantes.alliance.id == 1 then
GetPlayer(SpawnUnit(1, usa, 73, 34, 1, 100))
end
end
if MatchType() == 3 then
usa.luaData.BuildUSArmy2 = 1
end
end
Code: Select all
if eventname == "BuildUSArmy2" then
local brest = GetHex(70, 31)
local brestport = GetHex(69, 30)
local nantes = GetHex(74, 34)
local nantesport = GetHex(73, 34)
local cherbourg = GetHex(76, 28)
local cherbourgport = GetHex(75, 28)
local portsmouth = GetHex(76, 25)
local portsmouthport = GetHex(77, 25)
local paris = GetHex(82, 30)
local verdun = GetHex(87, 29)
if MatchType() <= 2 then
if verdun.unit == nil
and verdun.alliance.id == 1 then
if GetEvent("VerdunCaptured") <= 0 then
GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
end
elseif verdun.unit ~= nil
and verdun.alliance.id == 1 then
local hexes = game.map:GetHexesInRange(verdun, 1)
for _, hex in pairs(hexes) do
if hex.unit == nil then
if hex.alliance.id == 1 then
GetPlayer(SpawnUnit(1, usa, hex.x, hex.y, 1, 100))
break
end
end
end
end
end
if MatchType() == 3 then
usa.luaData.BuildUSArmy2 = 1
end
end
I tried to merge them in various ways, but it usually means the same result as the code based on Gallieni, though in some variations I got the same result as the one based on BEF (even when there were empty Entente hexes 1 range in Verdun).
This is an example of the combination:
Code: Select all
if eventname == "BuildUSArmy2" then
local brest = GetHex(70, 31)
local brestport = GetHex(69, 30)
local nantes = GetHex(74, 34)
local nantesport = GetHex(73, 34)
local cherbourg = GetHex(76, 28)
local cherbourgport = GetHex(75, 28)
local portsmouth = GetHex(76, 25)
local portsmouthport = GetHex(77, 25)
local paris = GetHex(82, 30)
local verdun = GetHex(87, 29)
if MatchType() <= 2 then
if verdun.unit == nil
and verdun.alliance.id == 1 then
if GetEvent("VerdunCaptured") <= 0 then
GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
end
elseif verdun.unit ~= nil
and verdun.alliance.id == 1 then
local hexes = game.map:GetHexesInRange(verdun, 1)
for _, hex in pairs(hexes) do
if hex.unit == nil then
if hex.alliance.id == 1 then
GetPlayer(SpawnUnit(1, usa, hex.x, hex.y, 1, 100))
break
end
end
end
elseif brestport.unit == nil
and brest.alliance.id == 1 then
GetPlayer(SpawnUnit(1, usa, 69, 30, 1, 100))
elseif nantesport.unit == nil
and nantes.alliance.id == 1 then
GetPlayer(SpawnUnit(1, usa, 73, 34, 1, 100))
end
end
if MatchType() == 3 then
usa.luaData.BuildUSArmy2 = 1
end
end