Skip to content

Instantly share code, notes, and snippets.

@tomjn
Created May 29, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/86e60cae23e57a3287cf20cf2246f37e to your computer and use it in GitHub Desktop.
Save tomjn/86e60cae23e57a3287cf20cf2246f37e to your computer and use it in GitHub Desktop.
ai = ....
p = {x=0,y=0,z=0}
units = UnitQuery( ai, {
within={
position=p
radius=100
},
canMove=false
})
for k,unit in ipairs(args) do
-- do stuff
end
local distance = function(pos1 ,pos2)
dx = math.abs(pos2.x - pos1.x)
dz = math.abs(pos2.z - pos1.z)
return math.sqrt((dx ^ 2) + (dz ^ 2))
end
function UnitQuery( ai, args)
-- setup the default arguements/options
local options = {
team = game:GetTeamID()
}
-- merge args into default options and overwrite
for k,v in pairs(args) do
options[k] = v
end
objects = ai.unithandler.units
local matches = {}
for k,object in pairs(objects) do
-- test each object based on the options given, if it matches, add to the matches table and return
local pass = true
local engineUnit = object:Internal()
if (options['unit_type'] ~= nil) and pass then
local utype = engineUnit:Name()
if utype ~= options['unit_type'] then
pass = false
end
end
if (options['team'] ~= nil) and pass then
local uteam = engineUnit:Team()
if uteam ~= options['uteam'] then
pass = false
end
end
if (options['within'] ~= nil) and pass then
if 'function' == type(options['within']) then
local f = options['within']
pass = f(engineUnit:GetPosition())
else
if 'table' == type(options['within']) then
local p = options['within']['position']
local d = options['within']['radius']
if(d > distance(p,engineUnit:GetPosition()) ) then
pass = false
end
end
end
end
if (options['canMove'] ~= nil) and pass then
if engineUnit:CanMove() ~= options['canMove'] then
pass = false
end
end
--
if pass == true then
table.insert(matches,object)
end
end
return matches
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment