Guest User

Untitled

a guest
Oct 31st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. local mobs = {}
  2.  
  3. -- Returns the nearest position of the
  4. -- position list.
  5. -- pos - position of the reference point
  6. -- positions - list of positions
  7. -- Returns a table with x, y, z, and dist which
  8. -- contains the distance.
  9.  
  10. local function get_nearest_position(pos, positions, starti)
  11.     local nearest_dist
  12.     local nearest_pos
  13.     for i in pairs(positions) do
  14.         i = i + starti
  15.         local position = positions[i]  
  16.         dist = vector.distance(pos, position)
  17.         if not nearest_dist then
  18.             nearest_dist = dist
  19.         end
  20.         if dist <= nearest_dist then
  21.             nearest_dist = dist
  22.             nearest_pos = position
  23.         end
  24.     end
  25.     --nearest_pos.dist = nearest_dist
  26.     return nearest_pos
  27. end
  28.  
  29. -- Finds all nodes of given name inside a box.
  30. -- pos1 - First corner of the box
  31. -- pos2 - Second corner
  32. -- name - name of the node (eg. "default:cobble")
  33. -- Returns a table with positions of the given node.
  34.  
  35. local function find_node_inside_box(pos1, pos2, name)
  36.     local result = {}
  37.     for x1 = pos2.x, pos1.x do
  38.         for y1 = pos2.y, pos1.y do
  39.             for z1 = pos2.z, pos1.z do
  40.                 local pos = {x = x1, y = y1, z = z1}
  41.                 if minetest.get_node(pos).name == name then
  42.                     table.insert(result, pos)
  43.                 end
  44.             end
  45.         end
  46.     end
  47.     return result
  48. end
  49. -- Is the node reachable?
  50. -- pos - position
  51. -- Returns true if there is air above it, false otherwise
  52.  
  53. local function is_reachable(pos)
  54.     local pos2 = {pos.x, pos.y + 1, pos.z}
  55.     if minetest.get_node(pos2) == "air" then
  56.         return true
  57.     else
  58.         return false
  59.     end
  60. end
  61.  
  62. local function fix_pos(pos)
  63.     pos.x = math.floor(pos.x + 0.5)
  64.     pos.y = math.floor(pos.y + 0.5)
  65.     pos.z = math.floor(pos.z + 0.5)
  66.     return pos
  67. end
  68.  
  69. -- Locates nearest node of the required type, and returns
  70. -- path towards it.
  71. -- startpos - Starting position
  72. -- name - Name of the node
  73. -- self - The entity moving
  74. -- Returns path or -1
  75. local function start_gather(startpos, name, self)
  76.     local starti = 0
  77.     local radius = {x=10, y=10, z=10}
  78.     local corner_1 = vector.add(startpos, radius)
  79.     local corner_2 = vector.subtract(startpos, radius)
  80.     local reachable = 0
  81.     local positions = find_node_inside_box(corner_1, corner_2, name)
  82.     local nearest = {}
  83.     while reachable == 0 do
  84.         nearest = get_nearest_position(startpos, positions, starti)
  85.         starti = starti + 1
  86.         if starti > #positions then
  87.             return -1
  88.         end
  89.         reachable = is_reachable(nearest)
  90.     end
  91.  
  92.     local path = minetest.find_path(fix_pos(startpos), fix_pos(nearest), 20, 6, 6, "Dijkstra")
  93.     print(dump(startpos), dump(nearest))
  94.     return path
  95. end
  96.  
  97. minetest.register_entity("mobs_lisac:npc", {
  98.     on_activate = function(self, staticdata, dtime_s)
  99.         minetest.chat_send_all("I'M ALIVE!")
  100.     end,
  101.     on_step = function(self, dtime)
  102.         local pos = self.object:get_pos() -- Mobs position
  103.         if not self._path[0] then
  104.             local result = start_gather(pos, "default:dirt", self)
  105.             if result == -1 then
  106.                 print("No dirt, can't gather.")
  107.                 return true
  108.             elseif result == nil then
  109.                 print("No path.")
  110.                 return true
  111.             else
  112.                 print("We got the results!")
  113.                 print(dump(result))
  114.                 self._path = result
  115.             end
  116.         end
  117.         print(dump(self._path))
  118.         self.object.move_to(self._path[0])
  119.         table.remove(self._path, 0)
  120.     end,
  121.     _goal = "gather",
  122.     _material = "default:dirt",
  123.     _path = {},
  124. })
Advertisement
Add Comment
Please, Sign In to add comment