obj init rewrite
This commit is contained in:
parent
ed4a66c5da
commit
8be654c25b
26
commands.lua
26
commands.lua
@ -26,7 +26,8 @@ local function pos_in_dir(obj, dir) -- position after we move in specified direc
|
||||
return pos
|
||||
end
|
||||
|
||||
basic_robot.commands.move = function(obj,dir)
|
||||
basic_robot.commands.move = function(name,dir)
|
||||
local obj = basic_robot.data[name].obj;
|
||||
local pos = pos_in_dir(obj, dir)
|
||||
|
||||
if minetest.get_node(pos).name ~= "air" then return end
|
||||
@ -40,13 +41,15 @@ basic_robot.commands.move = function(obj,dir)
|
||||
end
|
||||
|
||||
|
||||
basic_robot.commands.turn = function (obj, angle)
|
||||
basic_robot.commands.turn = function (name, angle)
|
||||
local obj = basic_robot.data[name].obj;
|
||||
local yaw = obj:getyaw()+angle;
|
||||
obj:setyaw(yaw);
|
||||
end
|
||||
|
||||
|
||||
basic_robot.commands.dig = function(obj,dir)
|
||||
basic_robot.commands.dig = function(name,dir)
|
||||
local obj = basic_robot.data[name].obj;
|
||||
local pos = pos_in_dir(obj, dir)
|
||||
local luaent = obj:get_luaentity();
|
||||
if minetest.is_protected(pos,luaent.owner ) then return end
|
||||
@ -62,23 +65,30 @@ basic_robot.commands.dig = function(obj,dir)
|
||||
minetest.set_node(pos,{name = "air"})
|
||||
end
|
||||
|
||||
basic_robot.commands.read_node = function(obj,dir)
|
||||
basic_robot.commands.read_node = function(name,dir)
|
||||
local obj = basic_robot.data[name].obj;
|
||||
local pos = pos_in_dir(obj, dir)
|
||||
return minetest.get_node(pos).name or ""
|
||||
end
|
||||
|
||||
|
||||
basic_robot.commands.place = function(obj,nodename, dir)
|
||||
basic_robot.commands.place = function(name,nodename, dir)
|
||||
local obj = basic_robot.data[name].obj;
|
||||
local pos = pos_in_dir(obj, dir)
|
||||
local luaent = obj:get_luaentity();
|
||||
if minetest.is_protected(pos,luaent.owner ) then return end
|
||||
if minetest.get_node(pos).name~="air" then return end
|
||||
|
||||
local spos = obj:get_luaentity().spawnpos;
|
||||
local inv = minetest.get_meta(spos):get_inventory();
|
||||
if not inv then return end
|
||||
if not inv:contains_item("main", ItemStack(nodename)) then return end
|
||||
|
||||
local meta = minetest.get_meta(spos);
|
||||
|
||||
|
||||
local inv = meta:get_inventory();
|
||||
if not inv then return end
|
||||
if not inv:contains_item("main", ItemStack(nodename)) and meta:get_int("admin")~=1 then return end
|
||||
inv:remove_item("main", ItemStack(nodename));
|
||||
|
||||
|
||||
minetest.set_node(pos,{name = nodename})
|
||||
end
|
82
init.lua
82
init.lua
@ -14,70 +14,69 @@ dofile(minetest.get_modpath("basic_robot").."/commands.lua")
|
||||
-- SANDBOX for running lua code isolated and safely
|
||||
|
||||
function getSandboxEnv (name)
|
||||
local obj = basic_robot.data[name].obj; -- bug: this doesnt refresh always??
|
||||
local commands = basic_robot.commands;
|
||||
return
|
||||
{
|
||||
pcall=pcall,
|
||||
ram = basic_robot.data[name].ram, -- "ram" - used to store variables
|
||||
move = { -- changes position of robot
|
||||
left = function() commands.move(obj, 1) end,
|
||||
right = function() commands.move(obj, 2) end,
|
||||
forward = function() commands.move(obj, 3) end,
|
||||
backward = function() commands.move(obj, 4) end,
|
||||
up = function() commands.move(obj,5) end,
|
||||
down = function() commands.move(obj,6) end,
|
||||
left = function() commands.move(name,1) end,
|
||||
right = function() commands.move(name,2) end,
|
||||
forward = function() commands.move(name,3) end,
|
||||
backward = function() commands.move(name,4) end,
|
||||
up = function() commands.move(name,5) end,
|
||||
down = function() commands.move(name,6) end,
|
||||
},
|
||||
|
||||
turn = {
|
||||
left = function() commands.turn(obj,math.pi/2) end,
|
||||
right = function() commands.turn(obj,-math.pi/2) end,
|
||||
angle = function(angle) commands.turn(obj,angle*math.pi/180) end,
|
||||
left = function() commands.turn(name,math.pi/2) end,
|
||||
right = function() commands.turn(name,-math.pi/2) end,
|
||||
angle = function(angle) commands.turn(name,angle*math.pi/180) end,
|
||||
},
|
||||
|
||||
dig = {
|
||||
left = function() commands.dig(obj, 1) end,
|
||||
right = function() commands.dig(obj, 2) end,
|
||||
forward = function() commands.dig(obj, 3) end,
|
||||
backward = function() commands.dig(obj, 4) end,
|
||||
down = function() commands.dig(obj, 6) end,
|
||||
up = function() commands.dig(obj, 5) end,
|
||||
left = function() commands.dig(name,1) end,
|
||||
right = function() commands.dig(name,2) end,
|
||||
forward = function() commands.dig(name,3) end,
|
||||
backward = function() commands.dig(name,4) end,
|
||||
down = function() commands.dig(name,6) end,
|
||||
up = function() commands.dig(name,5) end,
|
||||
},
|
||||
|
||||
place = {
|
||||
left = function(nodename) commands.place(obj, nodename, 1) end,
|
||||
right = function(nodename) commands.place(obj,nodename, 2) end,
|
||||
forward = function(nodename) commands.place(obj,nodename, 3) end,
|
||||
backward = function(nodename) commands.place(obj,nodename, 4) end,
|
||||
down = function(nodename) commands.place(obj,nodename, 6) end,
|
||||
up = function(nodename) commands.place(obj,nodename, 5) end,
|
||||
left = function(nodename) commands.place(name,nodename, 1) end,
|
||||
right = function(nodename) commands.place(name,nodename, 2) end,
|
||||
forward = function(nodename) commands.place(name,nodename, 3) end,
|
||||
backward = function(nodename) commands.place(name,nodename, 4) end,
|
||||
down = function(nodename) commands.place(name,nodename, 6) end,
|
||||
up = function(nodename) commands.place(name,nodename, 5) end,
|
||||
},
|
||||
|
||||
insert = { -- insert item from inventory into another inventory TODO
|
||||
forward = function(item, inventory) robot_insert(obj, item, inventory,1) end,
|
||||
backward = function(item, inventory) robot_insert(obj, item, inventory,2) end,
|
||||
down = function(item, inventory) robot_insert(obj, item, inventory,3) end,
|
||||
up = function(item, inventory) robot_insert(obj, item, inventory,4) end,
|
||||
forward = function(item, inventory) robot_insert(name,item, inventory,1) end,
|
||||
backward = function(item, inventory) robot_insert(name,item, inventory,2) end,
|
||||
down = function(item, inventory) robot_insert(name,item, inventory,3) end,
|
||||
up = function(item, inventory) robot_insert(name,item, inventory,4) end,
|
||||
},
|
||||
|
||||
take = {}, -- take item from inventory TODO
|
||||
|
||||
selfpos = function() return obj:getpos() end,
|
||||
selfpos = function() return basic_robot.data[name].obj:getpos() end,
|
||||
|
||||
find_nodes =
|
||||
function(nodename,r)
|
||||
if r>8 then return false end
|
||||
return (minetest.find_node_near(obj:getpos(), r, nodename)~=nil)
|
||||
return (minetest.find_node_near(basic_robot.data[name].obj:getpos(), r, nodename)~=nil)
|
||||
end, -- in radius around position
|
||||
|
||||
|
||||
read_node = { -- returns node name
|
||||
left = function() return commands.read_node(obj, 1) end,
|
||||
right = function() return commands.read_node(obj, 2) end,
|
||||
forward = function() return commands.read_node(obj, 3) end,
|
||||
backward = function() return commands.read_node(obj, 4) end,
|
||||
down = function() return commands.read_node(obj, 6) end,
|
||||
up = function() return commands.read_node(obj, 5) end,
|
||||
left = function() return commands.read_node(name,1) end,
|
||||
right = function() return commands.read_node(name,2) end,
|
||||
forward = function() return commands.read_node(name,3) end,
|
||||
backward = function() return commands.read_node(name,4) end,
|
||||
down = function() return commands.read_node(name,6) end,
|
||||
up = function() return commands.read_node(name,5) end,
|
||||
},
|
||||
|
||||
say = function(text)
|
||||
@ -214,7 +213,7 @@ local robot_spawner_update_form = function (pos, mode)
|
||||
end
|
||||
|
||||
local function init_robot(self)
|
||||
basic_robot.data[self.owner].obj = self.object;
|
||||
basic_robot.data[self.owner].obj = self.object; -- BUG: some problems with functions using object later??
|
||||
self.object:set_properties({infotext = "robot " .. self.owner});
|
||||
self.object:set_properties({nametag = "robot " .. self.owner,nametag_color = "LawnGreen"});
|
||||
initSandbox ( self.owner )
|
||||
@ -250,7 +249,7 @@ minetest.register_entity("basic_robot:robot",{
|
||||
return;
|
||||
end
|
||||
|
||||
|
||||
self.spawnpos = basic_robot.data[self.owner].spawnpos;
|
||||
init_robot(self);
|
||||
self.running = 1;
|
||||
|
||||
@ -268,6 +267,7 @@ minetest.register_entity("basic_robot:robot",{
|
||||
basic_robot.data[self.owner] = {};
|
||||
end
|
||||
|
||||
basic_robot.data[self.owner].spawnpos = {x=self.spawnpos.x,y=self.spawnpos.y,z=self.spawnpos.z};
|
||||
init_robot(self); -- set properties, init sandbox
|
||||
|
||||
local err = setCode( self.owner, self.code ); -- compile code
|
||||
@ -276,7 +276,7 @@ minetest.register_entity("basic_robot:robot",{
|
||||
self.running = 0; -- stop execution
|
||||
self.object:remove();
|
||||
end
|
||||
basic_robot.data[self.owner].spawnpos = {x=self.spawnpos.x,y=self.spawnpos.y,z=self.spawnpos.z};
|
||||
|
||||
self.running = 1
|
||||
|
||||
end
|
||||
@ -480,11 +480,17 @@ minetest.register_node("basic_robot:spawner", {
|
||||
return stack:get_count();
|
||||
end,
|
||||
|
||||
|
||||
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
||||
return 0;
|
||||
end,
|
||||
|
||||
can_dig = function(pos, player)
|
||||
if minetest.is_protected(pos, player:get_player_name()) then return false end
|
||||
local meta = minetest.get_meta(pos);
|
||||
if not meta:get_inventory():is_empty("main") then return false end
|
||||
return true
|
||||
end
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user