2016-05-31 06:26:54 +02:00
|
|
|
------------------------------------------------------------
|
|
|
|
-- Copyright (c) 2016 tacigar
|
|
|
|
-- https://github.com/tacigar/maidroid
|
|
|
|
------------------------------------------------------------
|
|
|
|
|
|
|
|
maidroid.modules._aux = {}
|
|
|
|
|
2016-06-09 07:44:44 +02:00
|
|
|
local velocity = 3
|
|
|
|
|
|
|
|
-- change direction to destination and velocity vector
|
|
|
|
function maidroid.modules._aux.change_dir_to(self, dest)
|
|
|
|
local pos = self.object:getpos()
|
|
|
|
local dir = vector.subtract(dest, pos)
|
|
|
|
local vel = vector.multiply(vector.normalize(dir), velocity)
|
|
|
|
self.object:setvelocity(vel)
|
|
|
|
self.object:setyaw(math.atan2(vel.z, vel.x) + math.pi / 2)
|
|
|
|
end
|
|
|
|
|
2016-06-02 06:18:58 +02:00
|
|
|
-- change direction and velocity vector
|
2016-05-31 06:26:54 +02:00
|
|
|
function maidroid.modules._aux.change_dir(self)
|
|
|
|
local rnd = function() return math.random(0, 5) * 2 - 5 end
|
|
|
|
local dir = {x = rnd(), y = 0, z = rnd()}
|
2016-06-09 07:44:44 +02:00
|
|
|
local vel = vector.multiply(vector.normalize(dir), velocity)
|
2016-05-31 06:26:54 +02:00
|
|
|
self.object:setvelocity(vel)
|
|
|
|
self.object:setyaw(math.atan2(vel.z, vel.x) + math.pi / 2)
|
|
|
|
end
|
|
|
|
|
2016-06-02 06:18:58 +02:00
|
|
|
-- get direction vector by yaw
|
2016-05-31 06:26:54 +02:00
|
|
|
function maidroid.modules._aux.get_forward(yaw)
|
|
|
|
return { x = math.sin(yaw), y = 0.0, z = -math.cos(yaw) }
|
|
|
|
end
|
|
|
|
|
2016-06-02 06:18:58 +02:00
|
|
|
-- round direction vector
|
2016-05-31 06:26:54 +02:00
|
|
|
function maidroid.modules._aux.get_round_forward(forward)
|
|
|
|
local rforward = { x = 0, y = 0, z = 0}
|
|
|
|
if math.abs((forward.x / (math.abs(forward.x) + math.abs(forward.z)))) > 0.5 then
|
|
|
|
if forward.x > 0 then rforward.x = 1
|
|
|
|
else rforward.x = -1 end
|
|
|
|
end
|
|
|
|
if math.abs((forward.z / (math.abs(forward.x) + math.abs(forward.z)))) > 0.5 then
|
|
|
|
if forward.z > 0 then rforward.z = 1
|
|
|
|
else rforward.z = -1 end
|
|
|
|
end
|
|
|
|
return rforward
|
|
|
|
end
|
|
|
|
|
2016-06-09 07:44:44 +02:00
|
|
|
|
2016-05-31 06:26:54 +02:00
|
|
|
function maidroid.modules._aux.get_under_pos(vec)
|
|
|
|
return { x = vec.x, y = vec.y - 1, z = vec.z }
|
|
|
|
end
|
|
|
|
|
2016-06-09 07:44:44 +02:00
|
|
|
|
2016-05-31 06:26:54 +02:00
|
|
|
function maidroid.modules._aux.get_upper_pos(vec)
|
|
|
|
return { x = vec.x, y = vec.y + 1, z = vec.z }
|
|
|
|
end
|
|
|
|
|
2016-06-02 06:18:58 +02:00
|
|
|
-- pickup droped items
|
2016-06-01 04:15:14 +02:00
|
|
|
function maidroid.modules._aux.pickup_item(self, radius, target_pred)
|
2016-05-31 06:26:54 +02:00
|
|
|
local pos = self.object:getpos()
|
2016-06-01 13:51:15 +02:00
|
|
|
local pred = target_pred or (function(itemstring) return true end)
|
2016-05-31 06:26:54 +02:00
|
|
|
local all_objects = minetest.get_objects_inside_radius(pos, radius)
|
|
|
|
for _, obj in ipairs(all_objects) do
|
|
|
|
if not obj:is_player() and obj:get_luaentity() then
|
|
|
|
local itemstring = obj:get_luaentity().itemstring
|
|
|
|
if itemstring then
|
2016-06-08 07:48:06 +02:00
|
|
|
if pred(itemstring) then
|
|
|
|
local inv = maidroid._aux.get_maidroid_inventory(self)
|
|
|
|
local stack = ItemStack(itemstring)
|
|
|
|
local leftover = inv:add_item("main", stack)
|
|
|
|
minetest.add_item(obj:getpos(), leftover)
|
|
|
|
obj:get_luaentity().itemstring = ""
|
|
|
|
obj:remove()
|
|
|
|
end
|
2016-05-31 06:26:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-09 07:44:44 +02:00
|
|
|
|
|
|
|
-- search surrounding nodes
|
|
|
|
function maidroid.modules._aux.search_surrounding(self, lenvec, pred)
|
|
|
|
local pos = vector.round(self.object:getpos())
|
|
|
|
for xi = -lenvec.x, lenvec.x do
|
|
|
|
for yi = -lenvec.y, lenvec.y do
|
|
|
|
for zi = -lenvec.z, lenvec.z do
|
|
|
|
local p = {x = pos.x + xi, y = pos.y + yi, z = pos.z + zi}
|
2016-06-09 16:54:33 +02:00
|
|
|
local node = minetest.get_node(p)
|
2016-06-09 07:44:44 +02:00
|
|
|
if pred(self, p, node) then return true, p, node end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false, nil, nil
|
|
|
|
end
|