| 03-04-2005, 05:48 PM | #1 |
What's the best, most efficient way of finding the nearest or furthest unit in relation to another unit? Like for faking resource gathering movement. Thanks |
| 03-04-2005, 07:50 PM | #2 |
Blizzard.j already has this functionality -- if limited strictly to gold mines. I've successfully adapted the functions for use similar to what you want, though. Here's functions as they are in blizzard.j: Code:
//===========================================================================
function MeleeEnumFindNearestMine takes nothing returns nothing
local unit enumUnit = GetEnumUnit()
local real dist
local location unitLoc
if (GetUnitTypeId(enumUnit) == 'ngol') then
set unitLoc = GetUnitLoc(enumUnit)
set dist = DistanceBetweenPoints(unitLoc, bj_meleeNearestMineToLoc)
call RemoveLocation(unitLoc)
// If this is our first mine, or the closest thusfar, use it instead.
if (bj_meleeNearestMineDist < 0) or (dist < bj_meleeNearestMineDist) then
set bj_meleeNearestMine = enumUnit
set bj_meleeNearestMineDist = dist
endif
endif
endfunction
//===========================================================================
function MeleeFindNearestMine takes location src, real range returns unit
local group nearbyMines
set bj_meleeNearestMine = null
set bj_meleeNearestMineDist = -1
set bj_meleeNearestMineToLoc = src
set nearbyMines = CreateGroup()
call GroupEnumUnitsInRangeOfLoc(nearbyMines, src, range, null)
call ForGroup(nearbyMines, function MeleeEnumFindNearestMine)
call DestroyGroup(nearbyMines)
return bj_meleeNearestMine
endfunctionCode:
local unit myMine local unit myTownHallLoc set myTownHallLoc = <code to get the player's start location> set myMine = MeleeFindNearestMine(myTownHallLoc, 700)
Now, you asked what's the best, most efficient way. I don't know if that's the best way, but if it's good enough for Blizzard, it'll likely be fine. And it reuses globals that otherwise just take up space. As for the best way to 'fake resource gathering', the best method of that depends on exactly what you're trying to accomplish. |
| 03-04-2005, 08:21 PM | #3 |
Thanks, I'll try it out later. |
| 03-04-2005, 08:32 PM | #4 |
| 03-04-2005, 08:43 PM | #5 |
I knew there was somewhere I didn't think to check... thanks Mike |
