FAQ  •  Register  •  Login

Questions from Furbuggy

<<

furbuggy

Posts: 267

Joined: Mon Mar 08, 2010 5:34 pm

Location: Mesa AZ | Ithaca NY

Post Mon Jun 21, 2010 7:48 pm

Questions from Furbuggy

1) Is there anyway to filter an array? I'm really not sure if this is the easiest way of going about it, but so far I have a bunch of locations of ResourceItems placed in an array called "Resources". I want to arrive within 200 units of one ResourceItem and then switch targets to the next closest visible ResourceItem, but not to include the one it is right next to. Any ideas?
BAM! I'm back.
<<

karamazovapy

Posts: 1567

Joined: Tue Feb 23, 2010 7:52 pm

Post Mon Jun 21, 2010 9:38 pm

Re: Questions from Furbuggy

Well by the time you travel to within 200 units of anything (assuming your starting position has changed), the distance to everything else will have changed. Unless you want to determine a path from your starting position (which doesn't seem especially logical to me, although I don't know what you're doing), I think it would make sense to pick your destination from the initial array and then generate a new one upon arrival.

However, if you really want to cycle through an array (keeping in mind that I don't actually speak Lua/Python), my instinct would be to use a foreach to cycle through the appropriate key/value pairs.
<<

watusimoto

Site Admin

Posts: 1558

Joined: Tue Feb 23, 2010 7:07 pm

Location: Quartz's mom's house

Post Tue Jun 22, 2010 11:27 am

Re: Questions from Furbuggy

You could use a modified version of the closest function; note this is COMPLETELY UNTESTED!

  Code:

function findClosestButNotTooClose(items)

   local noCloserThan = 200
   local noCloserThanSq = noCloserThan * noCloserThan
   local closest = nil
   local minDist = 999999999
   local loc = bot:getLoc()

   for indx, item in ipairs(items) do   -- Iterate over list
    -- Use distSquared because it is less computationally expensive
    -- and works great for comparing distances
    local d = loc:distSquared(item:getLoc())    

    if d < minDist and d > noCloserThanSq then    -- Is it the closest yet?
       closest = item
       minDist = d
    end
   
   return closest
end
 
<<

furbuggy

Posts: 267

Joined: Mon Mar 08, 2010 5:34 pm

Location: Mesa AZ | Ithaca NY

Post Fri Jun 25, 2010 1:44 pm

Re: Questions from Furbuggy

@ K:
yeah, the idea is to have the ship fly to the 2nd closest resource item, that way I can string him along.

@Wat:
Nice, I'll mess around with it and give it a try
BAM! I'm back.
<<

watusimoto

Site Admin

Posts: 1558

Joined: Tue Feb 23, 2010 7:07 pm

Location: Quartz's mom's house

Post Fri Jun 25, 2010 2:21 pm

Re: Questions from Furbuggy

You could modify that loop a little to look for the second closest, if that's what you want. As written, it should look for the closest that is more than 200 from your location.
<<

furbuggy

Posts: 267

Joined: Mon Mar 08, 2010 5:34 pm

Location: Mesa AZ | Ithaca NY

Post Fri Jun 25, 2010 5:40 pm

Re: Questions from Furbuggy

oh, well those two things will be the same as long as the closest is less than 200 away- but you're right, actually that would be better, given it does both of my clauses in one action.
BAM! I'm back.

Return to Bots

Who is online

Users browsing this forum: No registered users and 1 guest