[Mod] basic_robot [basic_robot]
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
[Mod] basic_robot [basic_robot]
License: GPL 3.0 or later
Dependencies: default
Download: https://github.com/ac-minetest/basic_ro ... master.zip
Source: https://github.com/ac-minetest/basic_robot/
WIKI http://wiki.minetest.net/Mods/basic_robot by hajo
------------------------------------------------------------------------------------
instructions:
1. place spawner (basic_robot:spawner) and right click it to write program.
To save program click "ok".
2.START to run program, STOP to stop program/remove robot, clear to delete program
Spawner can also be activated with signal ( mesecons mod, basic_machines mod)
3. ingame help: right click spawner and click help button
robot walking along cobble road: robot can only sense nearby nodes and has to decide when to turn
code for pine tree harvesting
There is built in book browser, editor and loader
Robot can write text to standard minetest books in its library, also it can read books and execute code in books
- Attachments
-
- robot3.jpg (74.49 KiB) Viewed 3716 times
-
- robot2.jpg (73.38 KiB) Viewed 3716 times
-
- robot1.jpg (70.1 KiB) Viewed 3716 times
- Desour
- Member
- Posts: 1473
- Joined: Thu Jun 19, 2014 19:49
- GitHub: Desour
- IRC: Desour
- In-game: DS
- Location: I'm scared that if this is too exact, I will be unable to use my keyboard.
Re: [Mod] basic_robot [basic_robot]
Edit: nice!
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
Code: Select all
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
Re: [Mod] basic_robot [basic_robot]
When pointing at a robot, It would be nice if a textrnd wrote:basic_robot is a lightweight minetest programmable robot for multiplayer.
like "basic_robot belongs to playerX" would be shown
(instead of "GenericCAO ...").
Giving a name to the bot would be nice and probably easy (*1).
Different colors for the bots of each player would be nice too, or maybe skins.
Could the robot emit light (i.e. like a mese-lamp, or front-only, i.e. headlights) ?
eg.instructions:
place spawner (basic_robot:spawner) and right click it to write program
Code: Select all
say("Hi")
* press "OK"-button to save the program text
* press "Spawn" to create a robot, to execute the program (*2)
(changes to the code are ignored while the bot is running)
* press "Remove" to dismiss the robot.
Only one bot at a time per spawner (*3).
BTW, it would be nice to have a different picture on the front side of the CPU-box.
Also, a different texture for "ready" and "working" (maybe green/red). (*3)
The robot can 'fly' one space away from any solid nodes,
and moves right thru the player (and an RC Car, (and likely mobs too).
No inventory (yet?).
The player can stand on it, but can not ride it (like a boat).
Also, a right-click on the bot does nothing (maybe pause/resume, or tell status?)
It looks like the execution of the program is repeated, about once every second:
Code: Select all
if (i==nil) then say("Hi !"); i=0 end
turn.left()
i = i+1
say(i)
(*1) Perhaps just a command/setting like 'robotname="Hugo"'.
How about a "personal library", i.e. a book that the player creates,
with a title like "basic_robot lib#1", where the text from that book
would be included into the code for the robot (e.g. with "#include lib#1") ?
So people could have their own code for customizing their bots,
included every time, without much typing/copy/paste.
(*2) So, the buttons "Spawn" and "Remove" would better be placed at the top right,
and "Reset" renamed to "Clear".
(*3) If the spawner is picked up while the bot is running,
and placed again, it shows a form with an empty code-area,
and gives an error "robot already active".
BTW, how could the robot read button-presses on a remote-controller ?
E.g. the controller as a formspec, with some buttons like left/right/forward/action.
(*4) That means, it can build itself into the sky on top of a column, with
Code: Select all
move.up()
place.down("default:dirt")
Also how to check for success of an action ?
And it would be nice to have a view from the robot, e.g. in the place of the minimap.
Edit:
When using "find_nodes()", eg.
Code: Select all
f = find_nodes(3,"default:stone_with_coal")
Code: Select all
#ROBOT ERROR: ... init.lua 69: attempt to compare number with string
Map-Database
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
about library: just paste code on sign and then copy it. Might make it be able to read books though, like have 10 books in its book inventory and then you will be able to say which book to run
example code, making robot follow cobble road:
Code: Select all
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
- Attachments
-
- follow_road.jpg (64.41 KiB) Viewed 3716 times
- Desour
- Member
- Posts: 1473
- Joined: Thu Jun 19, 2014 19:49
- GitHub: Desour
- IRC: Desour
- In-game: DS
- Location: I'm scared that if this is too exact, I will be unable to use my keyboard.
Re: [Mod] basic_robot [basic_robot]
Edit: @rnd: did you notice my PR?
Re: [Mod] basic_robot [basic_robot]
You think placing a torch on every move would be better ?DS-minetest wrote:some of hajo's suggestions were nice,
others werent. for example emitting light would cause much lag
BTW, can the robot detect the lightlevel, or the time-of-day ?
Also, his own hitpoints ?
Map-Database
- Desour
- Member
- Posts: 1473
- Joined: Thu Jun 19, 2014 19:49
- GitHub: Desour
- IRC: Desour
- In-game: DS
- Location: I'm scared that if this is too exact, I will be unable to use my keyboard.
Re: [Mod] basic_robot [basic_robot]
as much as i know placing a light emitting node (like a torch but invisible) is the only way to make an entity make lighthajo wrote:You think placing a torch on every move would be better ?DS-minetest wrote:some of hajo's suggestions were nice,
others werent. for example emitting light would cause much lag
BTW, can the robot detect the lightlevel, or the time-of-day ?
Also, his own hitpoints ?
and the robot only detects the name of the node, not more (it would be nice if the "detect_node.direction()" command would return a table, not only the node name)
also, it cant get its own health
Re: [Mod] basic_robot [basic_robot]
That was quick, thanks !rnd wrote:updates: bugfixes, has inventory, names ,...
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
Also, the robot doesn't notice when it gets punched, and reaches 0 hp.
About bridge-building at an edge, as in
Code: Select all
move.forward()
place.down("default:dirt")
The player can do this by sneaking to the edge, looking down,
and building a new block at the downward edge of his block.
Perhaps new directions, like "forward-down" and "forward-up" ?
Also, is there a better way to check for success of an action
then to wrap every action into query/action/query/compare ?
Map-Database
- Desour
- Member
- Posts: 1473
- Joined: Thu Jun 19, 2014 19:49
- GitHub: Desour
- IRC: Desour
- In-game: DS
- Location: I'm scared that if this is too exact, I will be unable to use my keyboard.
Re: [Mod] basic_robot [basic_robot]
this was clear, you could also set torches with worldedit, it would have the same effecthajo 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
when a player places a torch he gives a parameter
when the torch is wallmounted onto no wall and you update it (for example by digging a nearby node) it will drop because it notices that theres no wall
Re: [Mod] basic_robot [basic_robot]
Code: Select all
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
There is a check in init.lua:
Code: Select all
local function check_code(code)
local bad_code = {"while ", "for ", "do ", "repeat ", "until ", "goto "}
...
Map-Database
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
now move.. returns true if it can move or false if it cant ( going over edge for example)Also, is there a better way to check for success of an action
then to wrap every action into query/action/query/compare ?
so you can do do stuff like
Code: Select all
if not move.forward() then say("im stuck") end
What's wrong with these commands ?
Code: Select all
for i=1,999999999999999999999999999999999 do ... end
Code: Select all
while true do ... end
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 like
Code: Select all
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
-
- Member
- Posts: 818
- Joined: Tue Apr 14, 2015 01:59
- GitHub: raymoo
- IRC: Hijiri
- In-game: Raymoo + Clownpiece
Re: [Mod] basic_robot [basic_robot]
Code: Select all
local function blah()
blah()
end
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
Code: Select all
a = function() say(1) b() end
b = function() a() end
a()
Btw i know about debug.sethook which can be used to call other functions when stuff like function calls happen so i set it inside pcall to raise an error with error(), which should be intercepted by pcall.. but what happened it escaped outer pcall and caused minetest crash..
Code: Select all
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)
Code: Select all
pcall(
function()
error()
end)
)
It seems default stack size is 20, and there doesnt seem to be option to decrease that
- Attachments
-
- stack_overflow.jpg (66.21 KiB) Viewed 3716 times
-
- Member
- Posts: 818
- Joined: Tue Apr 14, 2015 01:59
- GitHub: raymoo
- IRC: Hijiri
- In-game: Raymoo + Clownpiece
Re: [Mod] basic_robot [basic_robot]
Code: Select all
local function blah()
return blah()
end
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
Code: Select all
say(1)
local function blah()
return blah()
end
blah()
say(2)
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)
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.
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
-
- Member
- Posts: 818
- Joined: Tue Apr 14, 2015 01:59
- GitHub: raymoo
- IRC: Hijiri
- In-game: Raymoo + Clownpiece
Re: [Mod] basic_robot [basic_robot]
rnd wrote:was this supposed to be harmful, cause it does absolutely nothing, no problems whatsoever:
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.Code: Select all
say(1) local function blah() return blah() end say(2)
Code: Select all
local function blah()
return blah()
end
blah()
Re: [Mod] basic_robot [basic_robot]
Perfectly valid, just needs a break (or timeslicing, or priority,rnd wrote:hajo wrote:What's wrong with these commands ?Code: Select all
for i=1,999999999999999999999999999999999 do ... end
Code: Select all
while true do ... end
or preemptive scheduling, or bots using energy, etc.)
only allow forward-goto, such as "goto done", "goto errorAbort".Code: Select all
a: .... goto b b: goto a
Sorry for trying to do useful stuff, as opposed toWhats wrong is your server will freeze after 5 minutes of multiplayer... welcome to real world ;)
grief/exploit/wasting other people's resources :)
I'm aware there are several possible workarounds.you could replace for i = 1, 10 loop with something
But the premise was to use such bots as an introduction to programming,
for teaching, and a fun way to get kids interested in programming.
(Also, well structured code, readable, etc. etc.)
With half the control-structures disabled, this will get hard...
I agree that addressing inefficent code/abuse etc. is important,
but maybe as a 2nd course (after everyone has seen how bad it is).
Ok, on a secured, walled-off server...
So, I would go for monitoring, reporting ("robot x crashed ..."),
and maybe disabling bots, as opposed to preventing every abuse.
There is a game trAInsported with competing bots as AI-trains,
that also uses lua as scripting language, and these bots also
get sandboxed and limited execution-time (or line-count).
Map-Database
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
basic_robot.call_limit = 32
Here is what happens if you make mistake in while loop condition, causing infinite loop
example with infinite recursion (even tail recursion), function calling itself too much
- Attachments
-
- infinite_recursion.jpg (36.8 KiB) Viewed 3716 times
-
- while_loop.jpg (41.2 KiB) Viewed 3716 times
Re: [Mod] basic_robot [basic_robot]
weird behaviour after syntax-errors were found when pressing "Start".
E.g. after correcting the typo / missing "end" / whatever, and pressing "Start"
to try again, it says "robot already active" (but model is not visible).
Another bug was when pressing "Stop", the robot didn't get removed,
it didn't react to punching, and the game crashed when rightclicking it.
Logfile:
Code: Select all
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--
Also, it might help to make an "ACTION"-entry into the logfile when a robot is spawned/stopped.
Map-Database
-
- Member
- Posts: 818
- Joined: Tue Apr 14, 2015 01:59
- GitHub: raymoo
- IRC: Hijiri
- In-game: Raymoo + Clownpiece
Re: [Mod] basic_robot [basic_robot]
Nice edit, next time could you reply normally so you don't make me look like a dummy? You could also have explicitly mentioned that you edited your post, and preserved the original content somewhere in it.rnd wrote:right, this is harmful:
One way to prevent this would be set up hook to count how many lines are executed and then throw error when exceeded. Tried thatCode: Select all
say(1) local function blah() return blah() end blah() say(2)
but for some reasons error isnt caught by pcalls, this is what happens: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)
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.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.
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
EDIT: Thanks for the info about sethook though, it looks like it could be used with coroutines to implement preemptive scheduling. Only issue then would be how to limit the memory consumption of a thread.
- rnd
- Member
- Posts: 220
- Joined: Sun Dec 28, 2014 12:24
- GitHub: ac-minetest
- IRC: ac_minetest
- In-game: rnd
Re: [Mod] basic_robot [basic_robot]
Examples:
explorer/tree harvester robot:
This will walk around, climb stairs .. It never gives up, if there is a way it will find it eventually.
When it finds pine tree it will harvest it all - if it cant reach top it will place leaves below itself so it can climb up to top of tree, after its done it digs itself back down to floor and continues exploration
Code: Select all
-- 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
- Attachments
-
- greeter_robot_with_memory.jpg (66.48 KiB) Viewed 3716 times
Re: [Mod] basic_robot [basic_robot]
Code: Select all
dig.down()
move.down()
Code: Select all
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
it will continue to hang in midair.
Same effect if the player digs away all his supporting blocks.
Map-Database
-
- Member
- Posts: 818
- Joined: Tue Apr 14, 2015 01:59
- GitHub: raymoo
- IRC: Hijiri
- In-game: Raymoo + Clownpiece
Re: [Mod] basic_robot [basic_robot]
Code: Select all
while (function() while false do end return true end)() do
-- spin
end
Who is online
Users browsing this forum: No registered users and 38 guests