Plz help: LUA tables and For Loops

Come in here to talk about your sky-net style world-destroying super bots!

Moderator: Defcon moderators

User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Plz help: LUA tables and For Loops

Postby roflamingo » Thu Feb 25, 2010 7:40 am

I cannot figure this out. So maybe one of you can take a second and tell me what the right thing is here.

The specific line below that is grief is the 'IsValidTerritory'. we have the allteams table. the first argument in IsValidTerritory is a teamid. But the line dies and says I am supplying nil.

What am I supposed to put there so that I iterate through all the team IDs to check this particular part of the globe?

[code]
allteams = GetAllTeamIDs()
SendChat( # allteams)
counter = 0

for i, team in next, allteams do
SendChat(i)
if IsValidTerritory (team:GetTeamID(), x+dx, y+dy, True) then
counter = counter + 1
end
end
User avatar
Ace Rimmer
level5
level5
Posts: 10803
Joined: Thu Dec 07, 2006 9:46 pm
Location: The Multiverse

Postby Ace Rimmer » Thu Feb 25, 2010 8:47 am

GetTeamID (someID)
cityID:GetTeamID ()
unitID:GetTeamID ()

You are trying to pass TeamID as cityID or unitID, that's the problem. In short:

Code: Select all

teamID:GetTeamID() 


I'm sure others will come along and give you a real answer, but here goes nothing...

Code: Select all

function TestTest2()
allteams = GetAllTeamIDs()
SendChat( # allteams)
   counter = 0
   cities = GetCityIDs()
   for i, city in ipairs(cities) do
      SendChat(i)
      if IsValidTerritory (city:GetTeamID(), math.random(-180,180), math.random(-90,90), True) then
         counter = counter + 1
      end
   end
   SendChat(counter .. " counter")
end
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Thu Feb 25, 2010 9:05 am

I just want it to loop against the TeamIDs... your code is looping through every city.

I know I have 2 TeamIDs in my allteams{} table, but I don't know how to iterate and present them as the first argument in IsValidTerritory.
User avatar
Ace Rimmer
level5
level5
Posts: 10803
Joined: Thu Dec 07, 2006 9:46 pm
Location: The Multiverse

Postby Ace Rimmer » Thu Feb 25, 2010 9:29 am

Well, why didn't you say so?!

Code: Select all

function TestTest2()
allteams = GetAllTeamIDs()
SendChat( # allteams)
counter = 0

for i, team in next, allteams do
SendChat(i)
if IsValidTerritory (team, math.random(-180,180), math.random(-90,90), True) then
counter = counter + 1
end
end
   SendChat(counter .. " counter")
end


Actually, I should have seen that. :roll: :P

Edit: I did test it and it works, of course ipairs would do it in 'order'.
Smoke me a kipper, I'll be back for breakfast...
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Thu Feb 25, 2010 10:00 am

Does counter ever go over 1? It doesn't seem to :shock:

Ya the proper argument is <team>.

If I am not precise with my terms, I apologize... I guess I am not used to talking about coding! :!:
User avatar
Ace Rimmer
level5
level5
Posts: 10803
Joined: Thu Dec 07, 2006 9:46 pm
Location: The Multiverse

Postby Ace Rimmer » Thu Feb 25, 2010 3:48 pm

roflamingo wrote:Does counter ever go over 1? It doesn't seem to :shock:

Ya the proper argument is <team>.

If I am not precise with my terms, I apologize... I guess I am not used to talking about coding! :!:

Well, it will if it (in my code anyway) hits a random spot that meets the criteria. Mine uses math.random because I didn't know what x+dx, y+dy were. It only loops once for each team, so it only get's one shot per team.

I have been able to get the counter up to four with six players.
Smoke me a kipper, I'll be back for breakfast...
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Thu Feb 25, 2010 9:53 pm

Problem #2

I build an array called MyWaterMap. In that array I (am hopefully) inserting co-ordinates of my ocean as follows:

Code: Select all

MyWaterMap = {}

Outer Loop that does stuff
if IsValidPlacementLocation (x + dx, y+ dy, "Sub") then
        DrawWhiteboardCross(x + dx, y + dy, 0.2)
        table.insert(MyWaterMap, x+dx, y+dy)
        end


Now I want to look at all the points in my array iteratively to evaluate if those points are shared points. How do I write my FOR loop and my IF statement?

Below: FOR/IF not working...

Code: Select all

for i, ocean in next, MyWaterMap do      
   if IsValidTerritory (team, ocean[1], ocean[2], True) then 
   counter = counter + 1
   end
   Multithreading.YieldLongTask()
end   
   
User avatar
Montyphy
level5
level5
Posts: 6745
Joined: Tue Apr 19, 2005 2:28 pm
Location: London, England

Postby Montyphy » Thu Feb 25, 2010 9:57 pm

Guessing you want:

Code: Select all

table.insert(MyWaterMap, {x+dx, y+dy})


and not:

Code: Select all

table.insert(MyWaterMap, x+dx, y+dy)
Uplink help: Check out the Guide or FAQ.
Latest Uplink patch is v1.55.
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Thu Feb 25, 2010 10:03 pm

I want each separate line in the table to be x,y co-ordinates...

and then to be able to iterate through each x,y co-ordinate pair to evaluate for a condition. And perhaps after build a new table if that condition is true with the elements that meet the condition.
User avatar
Montyphy
level5
level5
Posts: 6745
Joined: Tue Apr 19, 2005 2:28 pm
Location: London, England

Postby Montyphy » Thu Feb 25, 2010 10:12 pm

roflamingo wrote:I want each separate line in the table to be x,y co-ordinates...

and then to be able to iterate through each x,y co-ordinate pair to evaluate for a condition. And perhaps after build a new table if that condition is true with the elements that meet the condition.


Yeah, but you're using table.insert wrong. Here are the definitions for it:

table.insert(table, val)
table.insert(table, pos, val)

By giving 3 arguments your x value is setting the position of your y value in the table. What you need to do is insert the values as a table (by enclosing them with curly braces). Your for loop already expects each item in your table to be a table, hence ocean[1] and ocean[2].

May be this will make it clearer:

Code: Select all

ocean = {x+dx, y+dy}
table.insert(MyWaterMap, ocean)


Also, what's the next in the for loop?
Uplink help: Check out the Guide or FAQ.

Latest Uplink patch is v1.55.
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Fri Feb 26, 2010 12:22 am

Got it figured out...finally. :D
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Fri Feb 26, 2010 12:41 am

Ace Rimmer wrote:I have been able to get the counter up to four with six players.


Using this code? My counter is always either 0 or 1 :cry:

Code: Select all

for i = 1, # MyWaterMap do
      
      counter = 0
      for j, team in next, allteams do
         if IsValidTerritory (team, MyWaterMap[i].oceanLong, MyWaterMap[i].oceanLong, True) then 
         counter = counter + 1
         Multithreading.YieldLongTask()
         end
         end
      
       SendChat( i .. " - " .. counter .. " - " .. MyWaterMap[i].oceanLong .. " - " .. MyWaterMap[i].oceanLat)

      
       if counter > 1  then
      SendChat("Bonzai")
        DrawWhiteboardCross(MyWaterMap[i].oceanLong, MyWaterMap[i].oceanLong, 3)
      end
      
      
   Multithreading.YieldLongTask()
   end
User avatar
Ace Rimmer
level5
level5
Posts: 10803
Joined: Thu Dec 07, 2006 9:46 pm
Location: The Multiverse

Postby Ace Rimmer » Fri Feb 26, 2010 12:49 am

Thought you had it figured out?

Also, can somebody join and spec? Whomever BOT TestBot is keeps getting kicked/dropped and I want to see if it happens to everybody. Bonus: my fleet movement basic combat code pseudo works. That is, ships react and launch bombers.
Smoke me a kipper, I'll be back for breakfast...
User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Postby roflamingo » Fri Feb 26, 2010 12:56 am

Figured out 4 different things. and just figured this one out. Baaad typo in the code I just posted. LOL! :D

Return to “AI Bots”

Who is online

Users browsing this forum: No registered users and 0 guests