remote control now has "manual" mode - when code is empty it will display basic controls for robot

This commit is contained in:
rnd1 2016-11-22 14:28:49 +01:00
parent 9b894936e8
commit 374c1bed5c
2 changed files with 80 additions and 23 deletions

View File

@ -78,6 +78,13 @@ basic_robot.commands.turn = function (name, angle)
end end
basic_robot.commands.dig = function(name,dir) basic_robot.commands.dig = function(name,dir)
local digcount = 0;
if basic_robot.maxdig~=0 then
digcount = basic_robot.data[name].digcount;
if digcount > basic_robot.maxdig then return false end
end
local obj = basic_robot.data[name].obj; local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir) local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity(); local luaent = obj:get_luaentity();
@ -93,7 +100,7 @@ basic_robot.commands.dig = function(name,dir)
basic_robot.give_drops(nodename, inv); basic_robot.give_drops(nodename, inv);
minetest.set_node(pos,{name = "air"}) minetest.set_node(pos,{name = "air"})
basic_robot.data[name].digcount = digcount+1;
--DS: sounds --DS: sounds
local sounds = minetest.registered_nodes[nodename].sounds local sounds = minetest.registered_nodes[nodename].sounds

View File

@ -6,7 +6,11 @@ basic_robot.call_limit = 32; -- how many execution calls per script execution al
basic_robot.bad_inventory_blocks = { basic_robot.bad_inventory_blocks = {
["craft_guide:sign_wall"] = true, ["craft_guide:sign_wall"] = true,
} }
basic_robot.version = "11/14a"; basic_robot.maxdig = 1; -- how many digs allowed per execution, 0 = unlimited
basic_robot.version = "11/22a";
basic_robot.data = {}; basic_robot.data = {};
@ -417,6 +421,8 @@ local function runSandbox( name)
end end
basic_robot.data[name].ccounter = 0; basic_robot.data[name].ccounter = 0;
basic_robot.data[name].digcount = 1;
setfenv( ScriptFunc, basic_robot.data[name].sandbox ) setfenv( ScriptFunc, basic_robot.data[name].sandbox )
local Result, RuntimeError = pcall( ScriptFunc ) local Result, RuntimeError = pcall( ScriptFunc )
@ -529,7 +535,7 @@ minetest.register_entity("basic_robot:robot",{
local meta = minetest.get_meta(pos); local meta = minetest.get_meta(pos);
if meta then self.code = meta:get_string("code") end -- remember code if meta then self.code = meta:get_string("code") end -- remember code
if not self.code or self.code == "" then if not self.code or self.code == "" then
minetest.chat_send_player(self.owner, "#ROBOT INIT: no code fount") minetest.chat_send_player(self.owner, "#ROBOT INIT: no code found")
self.object:remove(); self.object:remove();
end end
@ -813,6 +819,35 @@ minetest.register_on_player_receive_fields(
return return
end end
local robot_formname = "robot_manual_control_";
if string.find(formname,robot_formname) then
local name = string.sub(formname, string.len(robot_formname)+1);
local sender = minetest.get_player_by_name(name); if not sender then return end
local commands = basic_robot.commands;
if fields.turnleft then
pcall(function () commands.turn(name,math.pi/2) end)
elseif fields.turnright then
pcall(function () commands.turn(name,-math.pi/2) end)
elseif fields.forward then
pcall(function () commands.move(name,3) end)
elseif fields.back then
pcall(function () commands.move(name,4) end)
elseif fields.left then
pcall(function () commands.move(name,1) end)
elseif fields.right then
pcall(function () commands.move(name,2) end)
elseif fields.dig then
pcall(function () commands.dig(name,3) end)
elseif fields.up then
pcall(function () commands.move(name,5) end)
elseif fields.down then
pcall(function () commands.move(name,6) end)
elseif fields.digdown then
pcall(function () commands.dig(name,6) end)
end
return
end
end end
) )
@ -886,6 +921,27 @@ minetest.register_node("basic_robot:spawner", {
}) })
local get_manual_control_form = function(name)
local form =
"size[2.5,3]" .. -- width, height
"button[-0.25,-0.25;1.,1;turnleft;TLeft]"..
"button[0.75,-0.25;1.,1;forward;GO]"..
"button[1.75,-0.25;1.,1;turnright;TRight]"..
"button[-0.25,0.75;1.,1;left;LEFT]"..
"button[0.75,0.75;1.,1;dig;DIG]"..
"button[1.75,0.75;1.,1;right;RIGHT]"..
"button[-0.25,1.75;1.,1;down;DOWN]"..
"button[0.75,1.75;1.,1;back;BACK]"..
"button[1.75,1.75;1.,1;up;UP]"..
"button[0.75,2.75;1.,1;digdown;DDown]";
return form;
end
-- remote control -- remote control
minetest.register_craftitem("basic_robot:control", { minetest.register_craftitem("basic_robot:control", {
description = "Robot remote control", description = "Robot remote control",
@ -908,16 +964,6 @@ minetest.register_craftitem("basic_robot:control", {
on_use = function(itemstack, user, pointed_thing) on_use = function(itemstack, user, pointed_thing)
local name = user:get_player_name(); local name = user:get_player_name();
if user:get_player_control().sneak then
local code = minetest.formspec_escape(itemstack:get_metadata());
local form =
"size[9.5,1]" .. -- width, height
"textarea[1.25,-0.25;8.75,3;code;;".. code.."]"..
"button_exit[-0.25,-0.25;1.25,1;OK;SAVE]";
minetest.show_formspec(name, "robot_control_" .. name, form);
return
end
if basic_robot.data[name] and basic_robot.data[name].sandbox then if basic_robot.data[name] and basic_robot.data[name].sandbox then
@ -926,7 +972,20 @@ minetest.register_craftitem("basic_robot:control", {
return return
end end
local t0 = basic_robot.data[name].remoteuse or 0; -- prevent too fast remote use
local t1 = minetest.get_gametime();
if t1-t0<1 then return end
basic_robot.data[name].remoteuse = t1;
script = itemstack:get_metadata(); script = itemstack:get_metadata();
if script == "" then
--display control form
minetest.show_formspec(name, "robot_manual_control_" .. name, get_manual_control_form(name));
return
end
local ScriptFunc, CompileError = loadstring( script ) local ScriptFunc, CompileError = loadstring( script )
if CompileError then if CompileError then
minetest.chat_send_player(name, "#remote control: compile error " .. CompileError ) minetest.chat_send_player(name, "#remote control: compile error " .. CompileError )
@ -981,13 +1040,6 @@ minetest.register_entity(
end end
}) })
minetest.register_craft({ minetest.register_craft({
output = "basic_robot:control", output = "basic_robot:control",
recipe = { recipe = {
@ -996,8 +1048,6 @@ minetest.register_craft({
} }
}) })
minetest.register_craft({ minetest.register_craft({
output = "basic_robot:spawner", output = "basic_robot:spawner",
recipe = { recipe = {