Mods/basic_robot_csm

From Minetest


basic_robot_csm
Blank.png
A mod
Mod Type Robots
Author rnd
Latest version 2017-08-19
Forum topic Forum
Source code GitHub
ContentDB N/A
Technical name basic_robot_csm

This is the user-documentation about the mod basic_robot_csm by rnd - Forum github,
a lua-sandbox / chat-bot / calculator.

Intro

basic_robot_csm is a lightweight robot mod running on the minetest-client.

Users can write programs for this bot in Lua.

For safety, these programs are executed inside a sandbox that only allows certain lua-commands.

Input & Output happens mainly via chat, but it is also possible to play sounds,
and to process formspecs.
There is no robot-entity that moves independent of the player like in the mod basic_robot.

How to get

  • install a minetest-version 0.4.16dev, 0.5.0 or later
  • download mod from github
  • unpack in "minetest DIR"/clientmods/
  • enable client mods in advanced settings menu or in minetest.conf
  • inside /clientmods/mods.conf there should be a line:
load_mod_basic_robot_csm = true
  • while playing say ".bot"

How to use

The chat-command '.bot' will present an editor-form with tabs and some buttons:

  • Tabs 1-8 for source-code.
  • Button "Start" starts executing the code of the program in the current tab.
  • Button "Stop" stops the program.
  • Button "Help" shows some helptext.
  • Button "Save" saves the program of the current tab (using mod-storage).

Select a tab, enter a program (or use cut&paste)

e.g. say("Hi")

press SAVE-button, press START-button.

The first line of text is used as the tab-title.

Commands

See commands in init.lua, function getSandboxEnv().
See also

Lua-docs:

misc

  • robot_version
  • say
  • colorize
  • tonumber
  • pairs
  • ipairs
  • error
  • type
  • rom
  • minetest
see Lua Modding API Reference, 'Helper functions' and below

self

  • self.name
  • self.pos
  • self.viewdir
  • self.listen_msg
  • self.sent_msg
  • self.read_form
  • self.sound
  • self.sound_stop
  • self.remove

code

  • code.set
  • code.run

os

  • os.date
  • os.clock
  • os.time
  • os.difftime

table

  • table.maxn
  • table.concat
  • table.insert
  • table.remove
  • table.sort

string

  • string.byte
  • string.char
  • string.len
  • string.find
  • string.gmatch
  • string.format
  • string.lower
  • string.upper
  • string.rep
  • string.reverse
  • string.sub
  • string.gsub

math

  • math.abs
  • math.min
  • math.max
  • math.ceil
  • math.floor
  • math.huge
  • math.modf
  • math.fmod
  • math.random
  • math.exp
  • math.frexp
  • math.ldexp
  • math.log
  • math.log10
  • math.pow
  • math.sqrt
  • math.pi
  • math.deg
  • math.rad
  • math.acos
  • math.cos
  • math.cosh
  • math.asin
  • math.sin
  • math.sinh
  • math.tan
  • math.tanh
  • math.atan
  • math.atan2

...

Example programs

When a program is started, it is executed repeatedly (about once per second) until stopped.
To stop it, press the STOP-button, or use the command

self.remove()

in the program.

HelloWorld

--HW
say("Hi")
self.remove()

Execute command

The second parameter for say() specifies that the text goes 'to server'.

--Cmd
say("/time",true)
say("/who",true)
self.remove()

Report time

--TimeDate
td = os.date("%Y-%m-%d  %H:%M:%S")
say( "Time&Date: "..td )
-- this will repeat

Report position

Show the position of the player:

--Pos.
p = self.pos()  -- returns a table, with elements x, y, and z
say("Position: x=" .. p.x .. ", y=" ..  p.y .. ", z=" ..  p.z)
-- this will repeat

Play sound

This can only play soundfiles that are already on the server.

--Sound
self.sound("default_break_glass")
self.remove()

Screenshot

-- sc
minetest.take_screenshot()
self.remove()

MiniMap

Switch the minimap on / off:

--mm
local mmm=minetest.ui.minimap:get_mode()
say('minimap-mode='..mmm)  -- 0:off, 1..6

if mmm==0 then
  minetest.ui.minimap:show()
else
  minetest.ui.minimap:hide()
end
self.remove()

FormSpec

-- todo

Calculations

--Calc
if not i then i=1 end
x=17/i
say(i.." --> "..x)
i=i+1
if i>20 then say("Stop."); self.remove() end

Math

--Math
if not i then
  i=0
  say("pi="..math.pi)
end
i=i+1
x=math.random(1,6)
say(x)
if i>=5 then say("Stop."); self.remove() end
-- Todo: statistics

Chat-listener

Check the chat-messages for keywords, and answer with a message.

--Chat
msg = self.listen_msg();
if msg then
  p=string.find(msg,"help")
  if p then say("What is the problem ?") end
  --
  p=string.find(msg,"wiki")
  www="http://wiki.minetest.net/ROBOTS_SKYBLOCK"
  if p then say("see the wiki at "..www) end
  --
end
Note: this is an example, not ready for regular use, because it will find
those keywords in a lot of places, where triggering is unintended.


Chat-bell

Plays a sound when your name appears in the text on chat.

--chatbell
if not myname then 
  myname="hajo"
  bell  ="default_break_glass" -- can only play soundfiles from the server
end

msg = self.listen_msg();
if msg then
  p1=string.find(msg,">")
  if p1 then
    p2=string.find(msg,myname) or 0
    if p2>p1 then  -- don't trigger on your own chat
      self.sound(bell) 
    end
  end
end

Chat-bell2

Plays a sound when your name appears in the text on chat, but ignore some players.

--todo
--chatbell2
if not bell then 
  bell1 = "note_a" -- can only play soundfiles from the server
  bell2 = "fire_flint_and_steel"
  bell3 = "default_break_glass"
  myname= self.name()
  ignorelist="x y z"
end

msg = self.listen_msg();
if msg then
  p=string.find(msg,">")
  if p then
    who=string.sub(msg,2,p-1)
    msg=string.sub(msg,p+1)
    bell=""
    p=string.find(msg,myname) or 0
    p=string.find(ignorelist,who) or 0
    say(who..">"..p)
--todo...
  end
end

Quiz-Solver

At the tutorial on the server ROBOTS_SKYBLOCK, the robot #18 runs a math-quiz,
asking questions like "WHAT IS 12*13", via chat.
The following program watches for these questions, calculates the result, and answers via chat.

-- Quiz-solver - 2017-09-03
-- for tutorial-robot #18
if not q then q=0 end

msg = self.listen_msg();
if msg then
  p=string.find(msg,"WHAT IS")
  if p then  -- get the numbers of the math-question:
    q1=string.sub(msg,p+8,p+9)
    q2=string.sub(msg,p+11,p+12)
  --say(p..">"..q1..">>"..q2)
    n1=tonumber(q1)
    n2=tonumber(q2)
    say( n1*n2, true )
    q=q+1
    if q>10 then say("Stop."); self.remove() end
  end
end

Questions/Ideas/Suggestions

  • move SAVE-button to top
  • strip "--" from tab-text
  • ".bot 1" .. ".bot 8" run code from corresponding source-tab
  • ".bot 0" - stop program
  • ".bot -1" - pause/unpause program
  • ".bot anyText" - use as input to current running program
  • command / special tab for autostart-program
eg. to do settings / commands when joining a server
  • if mod-storage is empty, create short demo-program in first tab
  • say(text,target) - extend target-spec (from only 'toserver' now) to 'owner' and 'userX'
eg. to show debug-messages only to owner, or send PM to a certain user
  • how to catch chat-messages starting with "***" ?
  • write text to file, eg. chatlog ?
  • set player-attributes, e.g. skin
  • error when soundfile not found