Need Help to shorten and Improve LUA Script

I have a working LUA script that iam sure can be improved quite a bit

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

Need Help to shorten and Improve LUA Script

Post by Zeruel87 » Wed Aug 14, 2019 7:58 am

Greetings,

i had a similiar post in the past.
Meanwhile i have a working LUA script that tells AI players to sent Money Delivering Trucks to allied players to share their money.

Now, the LUA looks like this (this is a shortened version because the original LUA is thousands of lines long and thats the issue)

Code: Select all

WorldLoaded = function()
	p1cash()

p1cash = function()
	if p1 and p1.IsBot then
		citybottruck = p1.GetActorsByType("citybottruck")
		for _,trucks in pairs(citybottruck) do
			if p2 and p1.IsAlliedWith(p2) then
				fact = p2.GetActorsByType("fact")
				pyle = p2.GetActorsByType("pyle")
				hand = p2.GetActorsByType("hand")
				afld = p2.GetActorsByType("afld")
				weap = p2.GetActorsByType("weap")
				hpad = p2.GetActorsByType("hpad")
				for _,fact in pairs(fact) do trucks.DeliverCash(fact) end
				for _,pyle in pairs(pyle) do trucks.DeliverCash(pyle) end
				for _,hand in pairs(hand) do trucks.DeliverCash(hand) end
				for _,afld in pairs(afld) do trucks.DeliverCash(afld) end
				for _,weap in pairs(weap) do trucks.DeliverCash(weap) end
				for _,hpad in pairs(hpad) do trucks.DeliverCash(hpad) end
			end
			if p3 and p1.IsAlliedWith(p3) then
				fact = p3.GetActorsByType("fact")
				pyle = p3.GetActorsByType("pyle")
				hand = p3.GetActorsByType("hand")
				afld = p3.GetActorsByType("afld")
				weap = p3.GetActorsByType("weap")
				hpad = p3.GetActorsByType("hpad")
				for _,fact in pairs(fact) do trucks.DeliverCash(fact) end
				for _,pyle in pairs(pyle) do trucks.DeliverCash(pyle) end
				for _,hand in pairs(hand) do trucks.DeliverCash(hand) end
				for _,afld in pairs(afld) do trucks.DeliverCash(afld) end
				for _,weap in pairs(weap) do trucks.DeliverCash(weap) end
				for _,hpad in pairs(hpad) do trucks.DeliverCash(hpad) end
			end
		end
	end
	Trigger.AfterDelay(DateTime.Seconds(5), function()
		p1cash()
	end)
end
my knowledge of LUA is very small, can someone tell me how to improve / shorten the LUA?
iam sure there is a way, at the moment iam doing this script for many more buildings and not just 1 but 16 players that all have their own function and check each of the other not 3 but 15 players. if they exist and are allied with them and if so send them their trucks in the respective buildings.

maybe it is possible to do this part only once and implement it somehow else in each function

Code: Select all

				for _,fact in pairs(fact) do trucks.DeliverCash(fact) end
				for _,pyle in pairs(pyle) do trucks.DeliverCash(pyle) end
				for _,hand in pairs(hand) do trucks.DeliverCash(hand) end
				for _,afld in pairs(afld) do trucks.DeliverCash(afld) end
				for _,weap in pairs(weap) do trucks.DeliverCash(weap) end
				for _,hpad in pairs(hpad) do trucks.DeliverCash(hpad) end
or maybe it is eben pssobible to check each individual other of the 15 players but do one part that tells the AI to check Play 1,2,3... and so on.

any idea would be very much appreciated.


if you are curious, this is the real script as it looks at the moment:
https://github.com/Zeruel87/AImoneytruc ... 4fc0426c8f



the third and last thing i can think of that can possible be improved is that the whole script does not have to run every 5 seconds but checks if a Truck enters the game/map and then orders it to deliver its money to the allied building.
Check out my CnC mod at:
https://www.moddb.com/mods/cameo

Post Reply