diff --git a/init.lua b/init.lua index def49a9..92b86ca 100644 --- a/init.lua +++ b/init.lua @@ -625,8 +625,6 @@ is_inside_string = function(strings,pos) -- is position inside one of the string return strings[mid][1]<=pos and pos<=strings[mid][2] end --- COMPILATION - local find_outside_string = function(script, pattern, pos, strings) local length = string.len(script) local found = true; @@ -643,6 +641,8 @@ local find_outside_string = function(script, pattern, pos, strings) return nil end +-- COMPILATION + preprocess_code = function(script) -- version 07/24/2018 --[[ idea: in each local a = function (args) ... end insert counter like: @@ -699,6 +699,8 @@ preprocess_code = function(script) -- version 07/24/2018 end + table.sort(inserts) + -- add inserts local ret = {}; i1=1; for i = 1, #inserts do diff --git a/scripts/games/paint_game.lua b/scripts/games/paint_game.lua new file mode 100644 index 0000000..8c29108 --- /dev/null +++ b/scripts/games/paint_game.lua @@ -0,0 +1,76 @@ +-- paint canvas by rnd +-- TODO: add load/save button, save in book +-- save: ask for name (chat ) and save in book +-- load display list of names and ask which one (chat) +if not init then + + colors = { + "black","blue","brown","cyan","dark_green","dark_grey","green","grey", + "magenta","orange","pink","red","violet","white","yellow" + } + + color = 1; + size = 16; + + init = true + + local ent = _G.basic_robot.data[self.name()].obj:get_luaentity(); + ent.timestep = 0.5 + + players = find_player(5); if not players then self.remove() end + player = _G.minetest.get_player_by_name(players[1]) + self.label("-> " .. players[1]) + + spos = self.spawnpos(); spos.y=spos.y+1; + + canvasn = "wool:white" + for i = 1, size do + for j = 1, size do + minetest.set_node({x=spos.x +i , y = spos.y + j, z = spos.z },{name = canvasn}) + end + end + + + + for i = 1,#colors do + minetest.set_node({x=spos.x +i , y = spos.y , z = spos.z },{name = "wool:"..colors[i]}) + end + + vn = {x=0,y=0,z=1}; + T0 = {x=spos.x+0.5,y=spos.y+0.5,z=spos.z-0.5*vn.z}; + + get_intersect = function(vn, T0, p, v) + local a = (T0.x-p.x)*vn.x + (T0.y-p.y)*vn.y + (T0.z-p.z)*vn.z; + local b = vn.x*v.x + vn.y*v.y + vn.z*v.z + if b<=0 then return nil end + if a<=0 then return nil end + local t = a / b + return {x = p.x+v.x*t, y= p.y+v.y*t, z = p.z+v.z*t} + end + +end + +if player:get_player_control().LMB then + local v = player:get_look_dir(); + local p = player:get_pos(); p.y = p.y + 1.5 + local c = get_intersect(vn,T0,p,v); + if c then + + local x = c.x - T0.x; local y = c.y - T0.y + if x>0 and x-1 and y0 then + c.z = c.z+0.5 + minetest.set_node(c, {name = "wool:" .. colors[color]}) + else + x = 1+math.floor(x) + if colors[x] then + color = x; + self.label(colors[x]) + end + end + end + + end + +end diff --git a/scripts/utils/resource_display.lua b/scripts/utils/resource_display.lua new file mode 100644 index 0000000..22145b9 --- /dev/null +++ b/scripts/utils/resource_display.lua @@ -0,0 +1,16 @@ +-- SHOWS ACTIVE ROBOTS AND STATISTICS +if not init then init = true + local data = _G.basic_robot.data; + local ret = {}; + for k,v in pairs(data) do + if k~="listening" and v.obj then + local ent = v.obj:get_luaentity(); + local t = v.t or 0; if t< 100000 then t = math.floor(t * 10000)/10 else t = 0 end + if ent then ret[#ret+1] = k .. " " .. string.len(ent.code) .. " " .. string.len(_G.string.dump(v.bytecode)) .. " ~ " .. t end + end + end + mem1 = _G.collectgarbage("count") + self.label("memory used by lua (kbytes) ".. mem1 .. " ( delta " .. mem1 - (mem0 or 0) .. ")\n\nACTIVE ROBOTS\nrobot name | source code size | bytecode size | ~ time (ms)\n" .. table.concat(ret,"\n")) + mem0 = mem1 + init = false +end \ No newline at end of file