Mods/geass csm

From Minetest


geass_csm
Blank.png
A mod
Mod Type Misc
Author quater
Latest version 2018-01-05
Forum topic Forum
Source code GitHub
ContentDB N/A
Technical name geass_csm

This is the user-documentation about the mod geass_csm by quater. This mod is inspired by LuaCmd and basic_robot_csm

Intro

geass_csm is a mod running on the minetest-client.

Users can write programs on this mod in Lua.

These programs are executed inside a sandbox.

Input & Output happens mainly via chat, but it is also possible to play sounds,
and to process formspecs.

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_geass_csm = true
  • while playing say ".luac" or '.lua [code]'

How to use

The chat-command '.luac' will present an editor-form and some buttons:

  • Button "Help" shows some helptext.
  • Button "Halt" stops the current program.
  • Button "Run" starts executing the code of the program and saves the program.
  • Button "Apply" saves the program of the current tab (using mod-storage).

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

e.g. core.display_chat_message("Hello world !")

press Run-button.

Commands

Available commands: .lua, .luaclear, .luac
See commands in init.lua.
See also

Lua-docs:

Miscellaneous

Available subcommands for .luac: edit, exec, halt, load, cat, title, copy, remove, unregister, history
Specials contents in mod_storage:

  • on_connect
  • titles

Specials variables and function:

  • _halt()
  • _running_script
  • require()

Example of 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 function: _halt() in the program.

Hello world

-- First example
core.display_chat_message("Hello world !")
_halt()

Morse

A interessing example:
First type in the chat: .luac edit morse
To edit a new script titled 'morse'
Then copy & paste the code below and run it (this is a code for minetest 0.5.0)
If you want to remove this 'fonctionnality' do: .luac unregister morse

local to_morse = {
  a = '.-',  b = '-...', c = '-.-.', d = '-..', e = '.', f = '..-.', g = '--.',
  h = '....', i = '..', j = '.---', k = '-.-', l = '.-..', m = '--', n = '-.',
  o = '---', p = '.--.', q = '--.-', r = '.-.', s = '...', t = '-', u = '..-',
  v = '...-', w = '.--', x = '-..-', y = '-.--', z = '--..', [1] = '.----',
  [2] = '..---', [3] = '...--', [4] = '....-', [5] = '.....', [6] = '-....',
  [7] = '--...', [8] = '---..', [9] = '----.', [0] = '-----', [' '] = '   '
}
local from_morse = {}
for k, v in pairs(to_morse) do
  from_morse[v] = k
end
core.register_on_sending_chat_message(
function(message)
  if message:sub(1, 1) == '!' then
    message = message:sub(2)
    message:lower()
    local to_send = ''
    for i = 1, #message do
      local m = to_morse[message:sub(i, i)]
      if m then
        to_send = to_send .. m .. ' '
      else
        core.display_chat_message(core.colorize('red', 'Can\'t translate: ')
          .. message:sub(i, i))
      end
    end
    core.send_chat_message(to_send)
    return true
  end
  return false
end)

core.register_on_receiving_chat_message(
function(message)
  core.display_chat_message(message)
  local name, morse = message:match('^<([^>]*)>%s(.*)')
  if morse and (morse:sub(1, 1) == '.' or morse:sub(1, 1) == '-'
    or morse:sub(1, 3) == '   ') then
    local message = ''
    morse = morse:gsub('%s%s%s', ' %% ')
    for m in morse:gmatch('[^%s]+') do
      if m == '%' then
        m = '   '
      end
      local c = from_morse[m]
      if c then
        message = message .. c
      else
        break
      end
    end
    core.display_chat_message(core.colorize('red', 'Message: ')
      .. core.colorize('yellow', message))
  end
  return true
end)

_halt()

rot13

An other example: Proceed as previous example

function rot13(s)
  return (s:gsub("%a", function(c) c=c:byte() return string.char(c+(c%32<14 and 13 or -13)) end))
end

core.register_on_sending_chat_message(
function(message)
  if message:sub(1, 5) == '/msg ' then
    message = message:sub(6)
    local name, msg = message:match('([^%s]+)%s%?(.+)')
    if not name then
      return false
    end
    core.send_chat_message('/msg ' .. name .. ' @' .. rot13(msg))
    return true
  elseif message:sub(1, 1) == '?' and message ~= '?' then
    message = message:sub(2)
    local to_send = rot13(message)
    core.send_chat_message('@' .. to_send)
    return true
  end
  return false
end)

core.register_on_receiving_chat_message(
function(ciphertext)
  core.display_chat_message(ciphertext)
  local name, c_msg = ciphertext:match('^<(.*)>%s@(.*)')
  if ciphertext:match('^PM%sfrom%s') then
    local name, c_msg = ciphertext:match('^PM%sfrom%s([^%s]*):%s@(.*)')
    if name then
      core.display_chat_message(core.colorize('red', 'Message: ')
        .. core.colorize('yellow', rot13(c_msg)))
    end
  elseif name then
    core.display_chat_message(core.colorize('red', 'Message: ')
      .. core.colorize('yellow', rot13(c_msg)))
    return true
  end
  return true
end)

_halt()

Standard library

The player can make it own library and load it.
As: .luac edit std
And press 'Apply'

print = core.display_chat_message

Then: .luac edit lua_code

require('std')
print('Loaded')
_halt()

Or in the chat: .lua require'std'; print('Quick Test')

Questions / Ideas / Suggestions

  • A better error system