Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mobs = {}
- -- Returns the nearest position of the
- -- position list.
- -- pos - position of the reference point
- -- positions - list of positions
- -- Returns a table with x, y, z, and dist which
- -- contains the distance.
- local function get_nearest_position(pos, positions, starti)
- local nearest_dist
- local nearest_pos
- for i in pairs(positions) do
- i = i + starti
- local position = positions[i]
- dist = vector.distance(pos, position)
- if not nearest_dist then
- nearest_dist = dist
- end
- if dist <= nearest_dist then
- nearest_dist = dist
- nearest_pos = position
- end
- end
- --nearest_pos.dist = nearest_dist
- return nearest_pos
- end
- -- Finds all nodes of given name inside a box.
- -- pos1 - First corner of the box
- -- pos2 - Second corner
- -- name - name of the node (eg. "default:cobble")
- -- Returns a table with positions of the given node.
- local function find_node_inside_box(pos1, pos2, name)
- local result = {}
- for x1 = pos2.x, pos1.x do
- for y1 = pos2.y, pos1.y do
- for z1 = pos2.z, pos1.z do
- local pos = {x = x1, y = y1, z = z1}
- if minetest.get_node(pos).name == name then
- table.insert(result, pos)
- end
- end
- end
- end
- return result
- end
- -- Is the node reachable?
- -- pos - position
- -- Returns true if there is air above it, false otherwise
- local function is_reachable(pos)
- local pos2 = {pos.x, pos.y + 1, pos.z}
- if minetest.get_node(pos2) == "air" then
- return true
- else
- return false
- end
- end
- local function fix_pos(pos)
- pos.x = math.floor(pos.x + 0.5)
- pos.y = math.floor(pos.y + 0.5)
- pos.z = math.floor(pos.z + 0.5)
- return pos
- end
- -- Locates nearest node of the required type, and returns
- -- path towards it.
- -- startpos - Starting position
- -- name - Name of the node
- -- self - The entity moving
- -- Returns path or -1
- local function start_gather(startpos, name, self)
- local starti = 0
- local radius = {x=10, y=10, z=10}
- local corner_1 = vector.add(startpos, radius)
- local corner_2 = vector.subtract(startpos, radius)
- local reachable = 0
- local positions = find_node_inside_box(corner_1, corner_2, name)
- local nearest = {}
- while reachable == 0 do
- nearest = get_nearest_position(startpos, positions, starti)
- starti = starti + 1
- if starti > #positions then
- return -1
- end
- reachable = is_reachable(nearest)
- end
- local path = minetest.find_path(fix_pos(startpos), fix_pos(nearest), 20, 6, 6, "Dijkstra")
- print(dump(startpos), dump(nearest))
- return path
- end
- minetest.register_entity("mobs_lisac:npc", {
- on_activate = function(self, staticdata, dtime_s)
- minetest.chat_send_all("I'M ALIVE!")
- end,
- on_step = function(self, dtime)
- local pos = self.object:get_pos() -- Mobs position
- if not self._path[0] then
- local result = start_gather(pos, "default:dirt", self)
- if result == -1 then
- print("No dirt, can't gather.")
- return true
- elseif result == nil then
- print("No path.")
- return true
- else
- print("We got the results!")
- print(dump(result))
- self._path = result
- end
- end
- print(dump(self._path))
- self.object.move_to(self._path[0])
- table.remove(self._path, 0)
- end,
- _goal = "gather",
- _material = "default:dirt",
- _path = {},
- })
Advertisement
Add Comment
Please, Sign In to add comment