Possible to tell AI to use a Supply Truck to help allies?

Information and discussion for custom maps and mods.
User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

New Problem i encounter:
when i start a map and Slot 1 is empty you get a LUA error:

NOTE: i call "Multi0" - "p1" and "Multi1" - "p2"

Fatal Lua Error:
Stack Traceback
===============
(2) global C function 'error'
(3) Lua function '(anonymous)' at file 'C:\Program Files (x86)\OpenRA\/lua/sandbox.lua:150' (best guess)
Local variables:
arg = nil
ok = boolean: false
result = string: "[string \"simcity.lua\"]:12: attempt to index global 'p1' (a nil value)"
(4) tail call
(5) Lua function '?' at line 23 of chunk '"scriptwrapper.lua"]'
(6) global C function 'xpcall'
(7) Lua upvalue 'TryRunSandboxed' at line 23 of chunk '"scriptwrapper.lua"]'
Local variables:
fn = Lua function '(anonymous)' (defined at line 1 of chunk "simcity.lua"])
(8) Lua function '?' at line 31 of chunk '"scriptwrapper.lua"]'

Error message
===============
C:\Program Files (x86)\OpenRA\/lua/sandbox.lua:150: [string "simcity.lua"]:12: attempt to index global 'p1' (a nil value)
===============
bei OpenRA.Scripting.ScriptContext.FatalError(String message)
bei System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
bei System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bei Eluant.LuaRuntime.MethodWrapper.Invoke(Object[] parms)
bei Eluant.LuaRuntime.MakeManagedCall(IntPtr state, MethodWrapper wrapper)
bei Eluant.LuaRuntime.MethodWrapperCallCallback(IntPtr state)
bei Eluant.LuaRuntime.MethodWrapperCallCallbackWrapper(IntPtr state)
bei Eluant.LuaApi.lua_pcall(IntPtr L, Int32 nargs, Int32 nresults, Int32 errfunc)
bei Eluant.LuaRuntime.Call(IList`1 args)
bei Eluant.LuaRuntime.Call(LuaFunction fn, IList`1 args)
bei Eluant.LuaFunction.Call(IList`1 args)
bei OpenRA.Scripting.ScriptContext.WorldLoaded()
bei OpenRA.Mods.Common.Scripting.LuaScript.OpenRA.Traits.IWorldLoaded.WorldLoaded(World world, WorldRenderer worldRenderer)
bei OpenRA.World.LoadComplete(WorldRenderer wr)
bei OpenRA.Game.StartGame(String mapUID, WorldType type)
bei OpenRA.Network.UnitOrders.ProcessOrder(OrderManager orderManager, World world, Int32 clientId, Order order)
bei OpenRA.Network.OrderManager.TickImmediate()
bei OpenRA.Sync.<CheckSyncUnchanged>c__AnonStorey0.<>m__0()
bei OpenRA.Sync.CheckSyncUnchanged[T](World world, Func`1 fn)
bei OpenRA.Sync.CheckSyncUnchanged(World world, Action fn)
bei OpenRA.Game.InnerLogicTick(OrderManager orderManager)
bei OpenRA.Game.LogicTick()
bei OpenRA.Game.Loop()
bei OpenRA.Game.Run()
bei OpenRA.Game.InitializeAndRun(String[] args)
bei OpenRA.WindowsLauncher.RunGame(String[] args)
bei OpenRA.WindowsLauncher.Main(String[] args)
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

my logical guess is i have to add to the code that if p1 (Multi0) is not a bot but is not played at all then... either"do nothing or remove player from the game or something like that.

Or to previously check if p1 (Multi0) is in the game, i yes then execute the code, else do nothing.

any ideas?
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

SirCake
Posts: 393
Joined: Thu Feb 04, 2016 5:40 pm

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by SirCake »

Hi, did you do
‘p1 = Player.GetPlayer( “Multi1” )’ before using it in the Lua code?
If you didn’t declare what p1 is, the Lua Code will not know.
Only single Actors like ‘Actor143’ which you placed on the map before are known without declaring.

User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

this is the code i use at the moment:
https://github.com/Zeruel87/AiSupplyTru ... upplyTruck

it works as long as the first slot in any map has a player in it, it does not matter if it is a bot or a human player.
But as soon as it is empty (Open) i get the LUA error

is it a problem that i called it "p1" instead of "Multi0"? i dont think so.
Or is it a Problem that the WorldLoadad is on top of the code?
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

abcdefg30
Posts: 641
Joined: Mon Aug 18, 2014 6:00 pm

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by abcdefg30 »

"p1" won't be defined if there is no Multi0 player (i.e. nobody in the slot). You need to make sure "p1" is not "nil" (null in Lua terms) before using it. You are probably fine in your code example if you change the if check to just "if p1 and p1.IsBot then".

User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

abcdefg30 wrote:
Mon Nov 19, 2018 9:52 am
"p1" won't be defined if there is no Multi0 player (i.e. nobody in the slot). You need to make sure "p1" is not "nil" (null in Lua terms) before using it. You are probably fine in your code example if you change the if check to just "if p1 and p1.IsBot then".
just tested it - IT WORKS :lol: no more LUA error
thanks again!
i think and hope i now got everything what i needed to know :)
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

Allright, i got a code that works!
it is a monstrosity with 42332 lines that i think will most likely cause facepalms and disbelieve to others that have more experience with LUA, but like i said, at least the code works and does what it should do :)
so i want to share that working monstrosity:
https://raw.githubusercontent.com/Zerue ... upplyFinal

P.S.: Iam also sure the code can be written better and shorter, but i dont know how yet so i enjoy that it is working at all :lol:
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

at the moment the Script checks up to 16 players if they are played by a bot and then checks if that player is allied with all other 15 players.
if yes then he gets a supply truck (or other help for upgrading buildings, vehicles and infantry) to the first ally he finde.

Iam sure it can be shortened with something like:
If a SupplyTruck enters the Map - Check what player owns the SupplyTruck AND check if this player is a bot THEN check if that previously been checked player is allied with... the next player and THEN send the Supply Truck to that previously checked ally

well, i need to check if that is possible in that way if i imagine it, if someone has an idea, i would appreciate that feedback.
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

User avatar
Zeruel87
Posts: 56
Joined: Mon Oct 15, 2018 8:18 pm
Location: Austria
Contact:

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by Zeruel87 »

just for the record, meanwhile i managed to improve the LUA script significantly

https://github.com/Zeruel87/AiSupplyTru ... oneyLUAnew

the over 40.000 line long script is now about 400 line long :)
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

lawANDorder
Posts: 140
Joined: Tue Oct 24, 2017 3:20 pm

Re: Possible to tell AI to use a Supply Truck to help allies?

Post by lawANDorder »

Congrats!

Post Reply