bugfix: code comment removal

master
rnd 2018-07-22 18:29:30 +02:00
parent e5382fd12e
commit db15170554
4 changed files with 201 additions and 5 deletions

View File

@ -651,20 +651,21 @@ end
preprocess_code = function(script) -- version 07/22/2018
--[[ idea: in each local a = function (args) ... end insert counter like:
local a = function (args) counter() ... end
local a = function (args) counter_check_code ... end
when counter exceeds limit exit with error
--]]
script = script:gsub("%-%-%[%[.*%-%-%]%]",""):gsub("%-%-.*\n","\n") -- strip comments in fancy way
script = script:gsub("%-%-%[%[.*%-%-%]%]",""):gsub("%-%-[^\n]*\n","\n") -- strip comments in fancy way
script="_ccounter = 0; " .. script;
local i1 -- process script to insert call counter in every function
local i1
-- process script to insert call counter in every function
local _increase_ccounter = " if _ccounter > " .. basic_robot.call_limit ..
" then error(\"Execution count \".. _ccounter .. \" exceeded ".. basic_robot.call_limit .. "\") end _ccounter = _ccounter + 1; "
local i1=0; local i2 = 0;
local i1=0; local i2 = 0;
local found = true;
@ -675,7 +676,7 @@ preprocess_code = function(script) -- version 07/22/2018
found = false;
i2 = nil;
-- i1 = where its looking
-- i1 = where its looking in current pass, i2 = hit position
i2=string.find (script, "while%s", i1) -- fix while OK
if i2 then

15
scripts/farm_walker.lua Normal file
View File

@ -0,0 +1,15 @@
if not init then
init = true
angle = 90
walk = {["default:dirt"] = 1}
stop = {["wool:white"] = 1}
end
node = read_node.forward_down()
if walk[node] then
move.forward()
elseif stop[node] then
self.reset(); angle = 90
else
turn.angle(angle);move.forward(); turn.angle(angle); angle = - angle
end

View File

@ -0,0 +1,138 @@
-- maze generation by rnd
-- http://en.wikipedia.org/wiki/Maze_generation_algorithm#Depth-first_search, recursive backtracker
-- representation of node coordinate (row,coloumn)=(i,j) -> (i-1)*n+j, i=1..n, j=1...m
-- representation of walls: below node k --> k, left of node k --> k+m.n
-- good overview of maze generation algorithms using javascript/html5
-- http://www.jamisbuck.org/presentations/rubyconf2011/index.html#recursive-backtracker
-- helper functions
--stack in lua
local stack={};
function stack.push(s,e) s[#s+1]=e end
function stack.pop(s) local r = s[#s];s[#s]=nil;return r end
--function table2string(s) local r = ""; for i,v in pairs(s) do r = r.. " ["..i.."]=".. v ; end return r end
function maze_deep_first_search(m,n,start,seed) -- returns a table of strings representing line renders
local steps,maxsteps; steps= 0; maxsteps = 999999;
local maze = {}
maze.m = m; maze.n = n;
maze.unvisited = {};maze.stack = {}; maze.walls = {};
maze.free = maze.m*maze.n;
local i,j,k
local nb,wall -- unvisited neighbbors, walls
--init structures
for i=1,maze.m do
for j =1,maze.n do
k=(i-1)*maze.n+j;maze.unvisited[k]=true -- initially all cells unvisited
maze.walls[k]=true;maze.walls[k+maze.n*maze.m]=true; -- walls are there
end
end
_G.math.randomseed(seed)
maze.current = start
maze.unvisited [ maze.current ] = false;
maze.free = maze.free-1; maze.stack[1+#maze.stack] = maze.current
while maze.free>0 and steps<maxsteps do -- main loop
steps=steps+1
-- check current node neighbors
k=maze.current
j = k % maze.n;i=math.ceil(k/maze.n); -- get coords back from index
if j==0 then j = maze.n end
--print("coords current node "..k .. " = " .. i .. " " ..j)
nb={};wall={}-- check unvisited neighbors & wall removals
if i>1 then -- down
k=(i-2)*maze.n+j; if maze.unvisited[k] then wall[#wall+1]=k+maze.n;nb[#nb+1]=k end
end
if i<maze.m then -- up
k=(i)*maze.n+j; if maze.unvisited[k] then wall[#wall+1]=k;nb[#nb+1]=k end
end
if j<maze.n then --right
k=(i-1)*maze.n+j+1; if maze.unvisited[k] then wall[#wall+1]=k+maze.n*maze.m; nb[#nb+1]=k end
end
if j>1 then --left
k=(i-1)*maze.n+j-1; if maze.unvisited[k] then wall[#wall+1]=k+1+maze.n*maze.m;nb[#nb+1]=k end
end
--print(" unvisited neighbors " .. table2string(nb))
if (#nb)>0 then -- if unvisited neighbors, choose random one as next current node
stack.push(maze.stack,maze.current) -- remember previous current node
k=math.random(#nb); -- pick random unvisited neighbor
maze.walls[wall[k]]=false; -- remove wall
--print(" removed wall ".. wall[k])
k=nb[k];
maze.current = k; -- new current cell
maze.unvisited[k]=false; maze.free = maze.free-1 -- one less unvisited
--print("new explore " .. k);
elseif (#maze.stack)~=0 then -- no unvisited neighbors, backtrack using stack
maze.current = stack.pop(maze.stack)
--print("backtrack to "..maze.current)
else -- even stack is empty, just pick random unvisited cell
k = math.random(maze.free); j=1;
for i =1,maze.m*maze.n do
if maze.unvisited[i] then
if j==k then k=i; break end -- pick node
j=j+1
end
end
--print(" stack empty, random pick " ..k)
maze.current=k;maze.unvisited[k]=false; maze.free = maze.free -1;
end
end -- of do
-- render maze with chars, row by row
maze.ret = {};
local hor;local vert;
local wall = "1"
for i=1,maze.m do
hor="";vert="";
k= (i-1)*maze.n;
-- horizontal
for j = 1, maze.n do
k=k+1;
-- if maze.walls[k+maze.n*maze.m] then vert=vert.."X." else vert=vert.. "0." end
-- if maze.walls[k] then hor=hor.."XX" else hor=hor.."X0" end
if maze.walls[k+maze.n*maze.m] then vert=vert..wall.."0" else vert=vert.. "00" end
if maze.walls[k] then hor=hor..wall..wall else hor=hor..wall.."0" end
end
maze.ret[1+#maze.ret]=hor..wall;maze.ret[1+#maze.ret]=vert..wall;
end
maze.ret[1+#maze.ret] = string.rep(wall,2*maze.n+1)
return maze.ret
end
-- RUN PROGRAM
local maze=maze_deep_first_search(10,30,1,2015)
--for _,v in pairs(maze) do print(v) end
make_maze = function(m,n,start,seed)
local pos = self.spawnpos();pos.y=pos.y+1
local p
local maze=maze_deep_first_search(m,n,start,seed) -- m,n,start,seed
local i,j,k;local p = {x=pos.x,y=pos.y,z=pos.z};
for i,v in pairs(maze) do
p.x = pos.x+i
for k = 1,string.len(v) do
p.z=pos.z+k
if string.sub(v,k,k)=="1" then
minetest.set_node(p,{name="default:brick"})
else minetest.set_node(p,{name="air"})
end
end
end
end
make_maze(10,10,1,1)
self.remove()

View File

@ -0,0 +1,42 @@
-- with current mods there are 4 registered chat responses so we add 5th
-- CHANGE COLOR OF CHAT FOR CERTAIN PLAYERS
if not rom.color_chat_messages then rom.color_chat_messages = 1+#minetest.registered_on_chat_messages end
colors = {"cyan", "LawnGreen"}
chatgroup = {}; -- players in here will see chat without colors
--say("chat " .. rom.chat_messages)
minetest.registered_on_chat_messages[rom.color_chat_messages] =
function(name,message)
if message == "nocolor" then
chatgroup[name] = not chatgroup[name]
minetest.chat_send_all("colored chat display " .. (chatgroup[name] and "DISABLED" or "ENABLED") .. " for " .. name)
return false
else
--message = os.date("%X") .. " " .. name .." <> " .. message;
local newmessage = "["..name .."] " .. message;
local player = minetest.get_player_by_name(name);
local pos1 = player:get_pos();
for _,player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local pos2 = player:get_pos();
local dist = math.sqrt((pos2.x-pos1.x)^2+(pos2.y-pos1.y)^2+ (pos2.z-pos1.z)^2)
local length = string.len(name);
local color = 1; -- default
if (chatgroup[name] or dist>32 or dist == 0) then color = 0 end
if string.find(message,string.lower(name)) then color = 2 end
if color == 0 then
minetest.chat_send_player(name, newmessage)
else
minetest.chat_send_player(name, minetest.colorize(colors[color], newmessage))
end
end
end
return true
end
self.remove()