Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function closestLocationInRange(position, parent, range, exclude)
- local locations = util.map(celestial.children(parent), function(p) return {"coordinate", p} end)
- local objectOrbits = {}
- if compare(celestial.currentSystem(), coordinateSystem(parent)) then
- -- current system, use all current objects even temporary
- for _,uuid in pairs(celestial.systemObjects()) do
- local orbit = celestial.objectOrbit(uuid)
- if orbit then
- objectOrbits[uuid] = orbit
- table.insert(locations, {"object", uuid})
- end
- end
- elseif parent.planet == 0 then
- -- another system, use permanent mapped objects
- for uuid,object in pairs(player.mappedObjects(parent)) do
- if celestial.objectTypeConfig(object.typeName).permanent then
- objectOrbits[uuid] = object.orbit
- table.insert(locations, {"object", uuid})
- end
- end
- end
- -- include parent if it's a planet
- if parent.planet > 0 then
- table.insert(locations, {"coordinate", parent})
- end
- locations = util.filter(locations, function(location)
- --if location[1] == "coordinate" then
- --local parameters = celestial.planetParameters(location[2])
- --if parameters and parameters.worldType == "Asteroids" then
- --return false
- --end
- --end
- return true
- end)
- local distance = function(location, first)
- local second
- if location[1] == "coordinate" then
- second = celestial.planetPosition(location[2])
- elseif location[1] == "object" then
- second = celestial.orbitPosition(objectOrbits[location[2]])
- end
- return vec2.mag(vec2.sub(first, second))
- end
- locations = util.filter(locations, function(s)
- return distance(s, position) < range and not compare(s, exclude)
- end)
- table.sort(locations, function(a, b)
- return distance(a, position) < distance(b, position)
- end)
- return locations[1]
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement