Page 1 of 1

Questions from Furbuggy

PostPosted: Mon Jun 21, 2010 7:48 pm
by 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?

Re: Questions from Furbuggy

PostPosted: Mon Jun 21, 2010 9:38 pm
by karamazovapy
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.

Re: Questions from Furbuggy

PostPosted: Tue Jun 22, 2010 11:27 am
by watusimoto
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
 

Re: Questions from Furbuggy

PostPosted: Fri Jun 25, 2010 1:44 pm
by 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

Re: Questions from Furbuggy

PostPosted: Fri Jun 25, 2010 2:21 pm
by watusimoto
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.

Re: Questions from Furbuggy

PostPosted: Fri Jun 25, 2010 5:40 pm
by 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.