LUA table question

Thanks to @EatThePath I solved my original question so replacing question with the answer and results.

EDIT fixed and pasted working code. This works for multiplayer with multi AI players and humans. Map needs 10 generic volumes around 10 important locations.

intel_table = {}

---Gather Intel on each player and plance in table for later use.
--Tables key are volumes
--@param playerID = Players table
--@param target = 1,2,3 where 1 being the best.
--@param antiship = 1,2,3 where 1 = fighters and flack 2 = DD and BC and 3 = both, NO GO!
function Nmy_Intel()

	SobGroup_CreateIfNotExist("Sob_Nmytemp1")
	SobGroup_Clear("Sob_Nmytemp1")
	
	--i = AI
	--j = volumes
	--playerIndex
	for i,v in playerLOD do
		if (v == 1) then
			--Cyle through the Volumes
			if (g_levelName == "3p_dantes_requiem") then
				local playerIndex = 0;
				playerCount = Universe_PlayerCount()
				while (playerIndex < playerCount) do --Cycle for each NMY player
					if Player_IsAlive(playerIndex) then
						if (AreAllied(playerIndex, i) == 0) then
							local c = 1 --??? ove writing my data 
							for j=0, 6 do --Cycle through Resoruce locaitons
								
								SobGroup_Clear("Sob_Nmytemp1")
     							SobGroup_FillSobGroupInVolume("Sob_Nmytemp1", "Player_Ships"..playerIndex, "SphereResourceLRG"..j)
     							
     							local shipCount = SobGroup_Count("Sob_Nmytemp1")
     							local volName = "SphereResourceLRG"..j
     							
     							local target = 0
     							local antiship = 0
     							local cap = 0
     							
     							if shipCount > 0 then
     								if SobGroup_AreAnyOfTheseTypes("Sob_Nmytemp1", "hgn_resourcecontroller, hgn_resourcecollector, vgr_resourcecollector, vgr_resourcecollector")==1 then
     									--HIGH RESOURCE TARGET
     									target = 1
     								end
     								if SobGroup_AreAnyOfTheseTypes("Sob_Nmytemp1", "hgn_shipyard, hgn_mothership, hgn_carrier, vgr_carrier, vgr_mothership, vgr_shipyard")==1 then
     									--MED RESOURCE TARGET
     									--HIGH SUBSYSTEM TARGET
     									cap = 1
     								end
     								if SobGroup_AreAnyOfTheseTypes("Sob_Nmytemp1", "hgn_assaultfrigate, hgn_interceptor, vgr_interceptor, vgr_assaultfrigate")==1 then
     									--LOW RESOURCE TARGET
     									--LOW SUBSYSTEM TARGET
     									--HIGH DEF
     									
     									antiship = 1
     								end
     								if SobGroup_AreAnyOfTheseTypes("Sob_Nmytemp1", "hgn_destroyer, vgr_destroyer")==1 then
     									--LOW RESOURCE TARGET
     									--LOW SUBSYSTEM TARGET
     									--HIGH DEF
     									antiship = antiship + 2 --1 fighter 2 DD 3 both
     								end
     								
     								
     							end
     								intel_index = "Nmy_Intel"..playerIndex
									if intel_table.intel_index == nil then
   										intel_table.intel_index={}
									end
									intel_table.intel_index[playerIndex..volName] = playerIndex.." "..volName.." "..target.." "..cap.." "..antiship.." "..shipCount
     								
     								c = c + 1
     						end
						end
					end
								
				playerIndex = playerIndex + 1
				end
			end
		end
	end

	print("print INTEL Table")
	for k, v in (intel_table) do 
	
		for j, i in (v) do
			print(j,i) 
		end
	
	print(k,v) 
	end
	
end

End result are (from log with human translation added):

0 SphereResourceLRG4 0 0 0 0 --Sphere around resource 4 has nothing in it.
0 SphereResourceLRG3 0 0 0 0
0 SphereResourceLRG2 1 0 0 2 --Human player at resource 2 has 2 resource units and nothing else.
0 SphereResourceLRG1 1 1 0 3 --Human player at resource 1 has coll and cap ship. No Defense.
0 SphereResourceLRG0 1 0 2 3 --Human player has a destroyer here and nothing else.
1 SphereResourceLRG6 1 0 0 2 --AI player collectors here.
1 SphereResourceLRG3 1 1 1 29 --AI player has collectors, anti bomber units, cap ship… totaling 29 units.

Note: This also has the advantage of seeing when other players (ai or human) are fighting (if enemies) or teaming (if allies) so my AI can take advantage of that info either to get maximum value or to work in conjunction with another allied AI of my own.

This is first pass but the goal here is to have a function that periodically gathers Intel for AI processing. With the Intel above, an AI intelligence can be increased when calling say an Attack function. To prevent the AI from cheating, include a CrossRef of units in sensor range.

This will also make an AI player more human like in deciding who to attack… as in don’t focus on just 1 player blindly, lets play to WIN!

Example:
have attack group, gather intel and find a suitable target, send group to attack a specific sub group inside the volume… e.g. just collectors, or just refinery or just their frigate production system.

My concern is how hard is this going to be on the PC? That is why I gather some generic data so that when ready to issue orders I can do that processing at that time. And since the Intel is not always UP TO DATE, it will add a human element to the AI.

-B8

Note: Since i am new to LUA i write my code to be readable as it can for me. Once I get the MOD done I go back and focus on cleaning up functions and optimizing where I can. Keep that in mind if you are using the code above.

2 Likes

The line two above it declares it as a string, so accessing it as a table doesn’t work. Based on you’re description of what you’re trying to do, it sounds like you might want to have a master table of tables, then construct your index the way you are doing

--in preceeding init code
intel_table = {}
--in your actual
intel_index = "Nmy_Intel"..playerIndex
if intel_table.intel_index == nil then
   intel_table.intel_index={}
end
intel_table.intel_index[volName] = target..antiship

Warning, I haven’t yet had my coffee this morning, but I think that should work.

2 Likes

That did the trick, I was not sure how to do table in table… in lua research online I did not get led down the path you showed me, thanks!

I fixed code above and added results… in case any other newbie lua devs (like me) wanted something to study!

1 Like

just want to say intel_table.intel_index = intel_table.intel_index or {} is a shorthand version of the if == nil statement. Also if used as an iif, it will need brackets ( eg return (intel_table.intel_index or 0) or g = 4 +(var_is_nil or 0)* 2) )

2 Likes