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

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

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

Post by Zeruel87 » Sat Nov 17, 2018 9:55 pm

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)

User avatar
Zeruel87
Posts: 22
Joined: Mon Oct 15, 2018 8:18 pm

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

Post by Zeruel87 » Sun Nov 18, 2018 8:05 am

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?

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

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

Post by SirCake » Mon Nov 19, 2018 7:17 am

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: 22
Joined: Mon Oct 15, 2018 8:18 pm

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

Post by Zeruel87 » Mon Nov 19, 2018 8:17 am

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?

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

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

Post by abcdefg30 » 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".

User avatar
Zeruel87
Posts: 22
Joined: Mon Oct 15, 2018 8:18 pm

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

Post by Zeruel87 » Mon Nov 19, 2018 8:25 pm

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 :)

User avatar
Zeruel87
Posts: 22
Joined: Mon Oct 15, 2018 8:18 pm

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

Post by Zeruel87 » Fri Nov 23, 2018 8:25 am

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:

User avatar
Zeruel87
Posts: 22
Joined: Mon Oct 15, 2018 8:18 pm

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

Post by Zeruel87 » Sat Nov 24, 2018 11:00 am

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.

Post Reply