if not x then
x=0 y = 0 h = 0 state=0
f = function(x)
return (math.sin(x/5*3.14)+1)*0.5*5 end
end
if state == 0 then -- move up
y=f(x)
h=h+1
if h>y then place.left("default:dirt") state = 1 end
dig.up();move.up(); place.down("default:glass")
end
if state == 1 then -- move down
h=h-1
if h>=0 then
dig.down() move.down()
else
state = 0; move.forward(); h=0; x=x+1;
end
end
rnd wrote:basic_robot is a lightweight minetest programmable robot for multiplayer.
instructions:
place spawner (basic_robot:spawner) and right click it to write program
say("Hi")
if (i==nil) then say("Hi !"); i=0 end
turn.left()
i = i+1
say(i)
move.up()
place.down("default:dirt")
f = find_nodes(3,"default:stone_with_coal")
#ROBOT ERROR: ... init.lua 69: attempt to compare number with string
move.forward()
node = read_node.down();
road = "default:cobble";
if node~=road then
move.backward()
move.left();
if read_node.down()~=road then
move.right(); move.right();
if read_node.down()~=road then
move.left(); turn.angle(180);
else
turn.right();
end
else
turn.left();
end
end
DS-minetest wrote:some of hajo's suggestions were nice,
others werent. for example emitting light would cause much lag
hajo wrote:DS-minetest wrote:some of hajo's suggestions were nice,
others werent. for example emitting light would cause much lag
You think placing a torch on every move would be better ?
BTW, can the robot detect the lightlevel, or the time-of-day ?
Also, his own hitpoints ?
rnd wrote:updates: bugfixes, has inventory, names ,...
move.forward()
place.down("default:dirt")
hajo wrote:I found some funny effects when placing a torch after every move:
* the torches hang in mid-air
* when collecting one of them, they all go out, and fall down
function work( n )
ok=0
--for i=1,n do
i=1
say("work:"..i)
dig.forward()
move.forward()
--end
return ok
end
if done==nil then
turn.left()
say("Working...")
work(3)
say("Done.")
done=1
end
local function check_code(code)
local bad_code = {"while ", "for ", "do ", "repeat ", "until ", "goto "}
...
Also, is there a better way to check for success of an action
then to wrap every action into query/action/query/compare ?
if not move.forward() then say("im stuck") end
What's wrong with these commands ?
for i=1,999999999999999999999999999999999 do ... end
while true do ... end
a: .... goto b
b: goto a
if not state then state = 1 i=1 end
if state==1 and i<10 then do STUFF else state = 2 end
i=i+1
local function blah()
blah()
end
a = function() say(1) b() end
b = function() a() end
a()
setfenv( ScriptFunc, basic_robot.data[name].sandbox )
pcall(
function()
debug.sethook(error,"l") -- raises error when next line is called
local Result, RuntimeError = pcall( ScriptFunc )
if RuntimeError then
return RuntimeError
end
debug.sethook(); -- clears hook
end)
pcall(
function()
error()
end)
)
local function blah()
return blah()
end
say(1)
local function blah()
return blah()
end
blah()
say(2)
pcall(
function()
debug.sethook(error,"l") -- raises error when next line is called
local Result, RuntimeError = pcall( ScriptFunc )
if RuntimeError then
return RuntimeError
end
end)
terminate called after throwing an instance of 'LuaError'
what(): Runtime error from mod 'basic_robot' in callback on_shutdown(): line
stack traceback:
[C]: in function '__index'
...\rpg\minetest0414server\bin\..\builtin\game\register.lua:350: in function <...\rpg\minetest0414server\bin\..\builtin\game\register.lua:349>
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
rnd wrote:was this supposed to be harmful, cause it does absolutely nothing, no problems whatsoever:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
say(1)
local function blah()
return blah()
end
say(2)
can you look at sethook in previous post, i tried raising error inside another pcall and even tried raising error inside script in sandbox but in both cases pcall didnt catch error and it crashed minetest.
local function blah()
return blah()
end
blah()
rnd wrote:hajo wrote:What's wrong with these commands ?Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
for i=1,999999999999999999999999999999999 do ... end
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
while true do ... end
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
a: .... goto b
b: goto a
Whats wrong is your server will freeze after 5 minutes of multiplayer... welcome to real world ;)
you could replace for i = 1, 10 loop with something
2016-11-10 21:39:10: ACTION[Server]: singleplayer right-clicks object 2: LuaEntitySAO at (69,7,26)
2016-11-10 21:39:10: ERROR[Main]: ServerError: Lua: Runtime error from mod 'basic_robot' in callback luaentity_Rightclick(): Invalid position (expected table got string).
2016-11-10 21:39:10: ERROR[Main]: stack traceback:
2016-11-10 21:39:10: ERROR[Main]: [C]: in function 'get_meta'
2016-11-10 21:39:10: ERROR[Main]: E:\temp\minetest-0.4.14\bin\..\mods\basic_robot\init.lua:194: in function 'robot_spawner_update_form'
2016-11-10 21:39:10: ERROR[Main]: E:\temp\minetest-0.4.14\bin\..\mods\basic_robot\init.lua:316: in function <E:\temp\minetest-0.4.14\bin\..\mods\basic_robot\init.lua:314>
--crash--
rnd wrote:right, this is harmful:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
say(1)
local function blah()
return blah()
end
blah()
say(2)
One way to prevent this would be set up hook to count how many lines are executed and then throw error when exceeded. Tried thatYour phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
pcall(
function()
debug.sethook(error,"l") -- raises error when next line is called
local Result, RuntimeError = pcall( ScriptFunc )
if RuntimeError then
return RuntimeError
end
end)
but for some reasons error isnt caught by pcalls, this is what happens:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
terminate called after throwing an instance of 'LuaError'
what(): Runtime error from mod 'basic_robot' in callback on_shutdown(): line
stack traceback:
[C]: in function '__index'
...\rpg\minetest0414server\bin\..\builtin\game\register.lua:350: in function <...\rpg\minetest0414server\bin\..\builtin\game\register.lua:349>
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I tried raising error inside another pcall and even tried raising error inside script in sandbox but in both cases pcall didnt catch error and it crashed minetest.
learned something new: "Tail call optimization" ..
so if you call same function as last instruction you dont need to add new stack level, just pass the arguments and continue execution:
f = function(arg) do_stuff f(arg1) end
-- ADVANCED tree harvester/explorer
tree = "default:pine_tree";
leaves = "default:pine_needles";
if read_node.up() == tree then -- dig up
dig.up();
move.up();
place.down(leaves)
else -- go down if can
node = read_node.down()
if node == leaves or node == tree then
dig.down(); move.down()
elseif node == "air" then
move.down()
else -- walk around
node = read_node.forward();
if node == tree then
dig.forward()
elseif node~="air" then
move.up()
end
if not move.forward() then
if math.random(2)==1 then
turn.left()
else
turn.right()
end
end
end
end
dig.down()
move.down()
2016-11-12 20:39:45: WARNING[Main]: Map::getNodeMetadata(): Block not found
2016-11-12 20:39:45: WARNING[Main]: GUIFormSpecMenu::drawList(): The inventory location "nodemeta:109,16,47" doesn't exist
while (function() while false do end return true end)() do
-- spin
end
I would suggest just using sethook to set up a count hook. It will run much faster than inserting function calls everywhere,
local function bad()
while true do
end
end
-- Need this if you have luajit (only turns it off for the function)
if jit then
jit.off(bad, true)
end
local thread = coroutine.create(bad)
debug.sethook(thread, function() error("Spent too much time") end, "", 10000)
print(coroutine.resume(thread))
➜ temp luajit blah.lua
false blah.lua:12: Spent too much time
rnd wrote:update: fixes, can sense players around, new direction forward_down for place and read_node..
Users browsing this forum: No registered users and 3 guests