Advertisement
Guest User

MPI closestLocationInRange function

a guest
Aug 23rd, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. function closestLocationInRange(position, parent, range, exclude)
  2. local locations = util.map(celestial.children(parent), function(p) return {"coordinate", p} end)
  3. local objectOrbits = {}
  4.  
  5. if compare(celestial.currentSystem(), coordinateSystem(parent)) then
  6. -- current system, use all current objects even temporary
  7. for _,uuid in pairs(celestial.systemObjects()) do
  8. local orbit = celestial.objectOrbit(uuid)
  9. if orbit then
  10. objectOrbits[uuid] = orbit
  11. table.insert(locations, {"object", uuid})
  12. end
  13. end
  14. elseif parent.planet == 0 then
  15. -- another system, use permanent mapped objects
  16. for uuid,object in pairs(player.mappedObjects(parent)) do
  17. if celestial.objectTypeConfig(object.typeName).permanent then
  18. objectOrbits[uuid] = object.orbit
  19. table.insert(locations, {"object", uuid})
  20. end
  21. end
  22. end
  23. -- include parent if it's a planet
  24. if parent.planet > 0 then
  25. table.insert(locations, {"coordinate", parent})
  26. end
  27.  
  28. locations = util.filter(locations, function(location)
  29. --if location[1] == "coordinate" then
  30. --local parameters = celestial.planetParameters(location[2])
  31. --if parameters and parameters.worldType == "Asteroids" then
  32. --return false
  33. --end
  34. --end
  35. return true
  36. end)
  37.  
  38. local distance = function(location, first)
  39. local second
  40. if location[1] == "coordinate" then
  41. second = celestial.planetPosition(location[2])
  42. elseif location[1] == "object" then
  43. second = celestial.orbitPosition(objectOrbits[location[2]])
  44. end
  45.  
  46. return vec2.mag(vec2.sub(first, second))
  47. end
  48. locations = util.filter(locations, function(s)
  49. return distance(s, position) < range and not compare(s, exclude)
  50. end)
  51. table.sort(locations, function(a, b)
  52. return distance(a, position) < distance(b, position)
  53. end)
  54. return locations[1]
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement