gui fixes

master
ac-minetest 2021-02-03 21:32:21 +01:00
parent bf7002b1c3
commit c3a021d1fc
5 changed files with 130 additions and 33 deletions

View File

@ -18,7 +18,7 @@ end
local pi = math.pi;
local function pos_in_dir(obj, dir) -- position after we move in specified direction
local yaw = obj:getyaw();
local yaw = obj:get_yaw();
local pos = obj:get_pos();
if dir == 1 then -- left
@ -105,9 +105,9 @@ basic_robot.commands.turn = function (name, angle)
local yaw;
-- more precise turns by 1 degree resolution
local mult = math.pi/180;
local yaw = obj:getyaw();
local yaw = obj:get_yaw();
yaw = math.floor((yaw+angle)/mult+0.5)*mult;
obj:setyaw(yaw);
obj:set_yaw(yaw);
end
@ -290,7 +290,7 @@ basic_robot.commands.pickup = function(r,name)
picklist[#picklist+1]=detected_obj;
if inv:room_for_item("main", stack) then
inv:add_item("main", stack);
obj:setpos({x=0,y=0,z=0}) -- no dupe
obj:set_pos({x=0,y=0,z=0}) -- no dupe
end
obj:remove();
end
@ -941,7 +941,7 @@ basic_robot.commands.walk_path = function(name)
end
end
yaw = yaw - math.pi/2
obj:setyaw(yaw);
obj:set_yaw(yaw);
pathdata[1] = idx + 1 -- target next node
obj:moveto(pos2, true)
@ -1206,6 +1206,52 @@ basic_robot.commands.machine = {
return true
end,
place_seed = function(name,dir,seedbookname) -- use basic_farming seedbook to place seed
if not basic_farming then return end
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
local spos = obj:get_luaentity().spawnpos;
local inv = minetest.get_meta(spos):get_inventory();
local idx = 0; -- where is seedbook?
for i = 1,inv:get_size("main") do -- where in inventory is seedbook?
if inv:get_stack("main", i):get_name() == seedbookname then idx = i; break end
end
if idx == 0 then return end -- no book in inventory!
local itemstack = basic_farming.seed_on_place(inv:get_stack("main", idx), nil, {type = "node",above = pos})
inv:set_stack("main", idx, itemstack) -- refresh stack in inventory
end,
dig_seed = function(name, dir)
if not basic_farming then return end
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
local nodename = minetest.get_node(pos).name;
local basename,stage
basename,stage=string.match(nodename,"%w+:(%w+)_(%d+)") -- modname:basename_stage
if not basename then return end
local spos = basic_robot.data[name].spawnpos; -- position of spawner block
local inv = minetest.get_meta(spos):get_inventory();
local itemstack = ItemStack("basic_farming:seedbook_" .. basename)
local seeds = tostring(minetest.get_meta(pos):get_int("gene"));
-- possibly several seeds?
local count = minetest.get_meta(pos):get_int("count"); if count == 0 then count = 1 end
local data = {name = basename, items = string.rep(seeds.. " ",count-1) .. seeds }
minetest.set_node(pos,{name = "air"})
local meta = itemstack:get_meta()
meta:from_table({fields = data})
inv:add_item("main",itemstack);
end,
}
-- CRPYTOGRAPHY

View File

@ -26,7 +26,7 @@ basic_robot.bad_inventory_blocks = { -- disallow taking from these nodes invento
basic_robot.http_api = minetest.request_http_api();
basic_robot.version = "2020/11/27a";
basic_robot.version = "2021/03/02a";
basic_robot.gui = {}; local robogui = basic_robot.gui -- gui management
basic_robot.data = {}; -- stores all robot related data
@ -280,6 +280,11 @@ function getSandboxEnv (name)
if player then return player:get_pos() else return nil end
end,
getview = function(name)
local player = minetest.get_player_by_name(name);
if player then return player:get_look_dir() else return nil end
end,
connected = function()
local players = minetest.get_connected_players();
local plist = {}
@ -460,6 +465,14 @@ function getSandboxEnv (name)
for dir, dir_id in pairs(directions) do
env.write_text[dir] = function(text) return commands.write_text(name, dir_id,text) end
end
--farming specials
env.machine.place_seed = {}; local env_machine_place_seed = env.machine.place_seed
env.machine.dig_seed = {}; local env_machine_dig_seed = env.machine.dig_seed;
for dir, dir_id in pairs(directions) do
env_machine_place_seed[dir] = function(seedbookname) return commands.machine.place_seed(name,dir_id,seedbookname) end
env_machine_dig_seed[dir] = function(dir) return commands.machine.dig_seed(name,dir_id) end
end
if authlevel>=1 then -- robot privs
@ -830,25 +843,25 @@ local robot_spawner_update_form = function (pos, mode)
form =
"size[9.5,8]" .. -- width, height
"textarea[1.25,-0.25;8.75,9.8;code;;".. code.."]"..
"button[-0.25,7.5;1.25,1;EDIT;EDIT]"..
"button[-0.25,-0.25;1.25,1;OK;SAVE]"..
"button_exit[-0.25, 0.75;1.25,1;spawn;START]"..
"button[-0.25, 1.75;1.25,1;despawn;STOP]"..
"field[0.25,3.;1.,1;id;id;"..id.."]"..
"button[-0.25, 3.6;1.25,1;inventory;storage]"..
"button[-0.25, 4.6;1.25,1;library;library]"..
"button[-0.25, 5.6;1.25,1;help;help]";
"textarea[1.25,-0.25;8.75,10.25;code;;".. code.."]"..
"button[-0.15,7.5;1.25,1;EDIT;EDIT]"..
"button[-0.15,-0.25;1.25,1;OK;"..minetest.colorize("yellow","SAVE").."]"..
"button_exit[-0.15, 0.75;1.25,1;spawn;"..minetest.colorize("green","START").."]"..
"button[-0.15, 1.75;1.25,1;despawn;"..minetest.colorize("red","STOP").."]"..
"field[0.15,3.;1.2,1;id;id;"..id.."]"..
"button[-0.15, 3.6;1.25,1;inventory;storage]"..
"button[-0.15, 4.6;1.25,1;library;library]"..
"button[-0.15, 5.6;1.25,1;help;help]";
else -- when robot clicked
form =
"size[9.5,8]" .. -- width, height
"textarea[1.25,-0.25;8.75,9.8;code;;".. code.."]"..
"button_exit[-0.25,-0.25;1.25,1;OK;SAVE]"..
"button[-0.25, 1.75;1.25,1;despawn;STOP]"..
"button[-0.25, 3.6;1.25,1;inventory;storage]"..
"button[-0.25, 4.6;1.25,1;library;library]"..
"button[-0.25, 5.6;1.25,1;help;help]";
"textarea[1.25,-0.25;8.75,10.25;code;;".. code.."]"..
"button_exit[-0.15,-0.25;1.25,1;OK;SAVE]"..
"button[-0.15, 1.75;1.25,1;despawn;STOP]"..
"button[-0.15, 3.6;1.25,1;inventory;storage]"..
"button[-0.15, 4.6;1.25,1;library;library]"..
"button[-0.15, 5.6;1.25,1;help;help]";
end
@ -1043,8 +1056,6 @@ minetest.register_entity("basic_robot:robot",{
end,
})
local spawn_robot = function(pos,node,ttl)
if type(ttl) ~= "number" then ttl = 0 end
if ttl<0 then return end
@ -1081,7 +1092,9 @@ local spawn_robot = function(pos,node,ttl)
if id <= 0 then -- just compile code and run it, no robot entity spawn
local codechange = false;
local codechange = false; -- was code changed by editing?
local poschange = false; -- are we running from different spawner?
if meta:get_int("codechange") == 1 then
meta:set_int("codechange",0);
codechange = true;
@ -1118,8 +1131,10 @@ local spawn_robot = function(pos,node,ttl)
end
end
local hashpos = minetest.hash_node_position(pos)
if data.lastpos~= hashpos then data.lastpos = hashpos; poschange = true end
if not data.bytecode then
if not data.bytecode or poschange then -- compile again
local script = meta:get_string("code");
if data.authlevel<3 then -- not admin

View File

@ -272,6 +272,9 @@ local help_pages = {
" materials",
" compress(input) - requires upgrades - energy intensive process",
" transfer_power(amount,target_robot_name)",
" dig_seed.direction digs seed node and transforms it to seed book",
" place_seed.direction(seedbookname) takes oldest seed from seed book",
" and plants it",
},
["CRYPTOGRAPHY"] = {

View File

@ -1,9 +1,33 @@
--battle bots
-- example program for team 4, other team is 5. each of players writes program and puts it in book,
-- book 1 program for team 4, book 2 program for team 5
--[[
DEMO PROGRAM: just move all your bots in direction 1,0 and attack while moving
for i = 1,#bots[TYPE] do -- move all bots of team 4 in direction {x=1,z=0}
if read_bots(TYPE,i) then -- is bot alive?
move_bot(i,1,0)
attack_bot(i,1,0) -- try to attack in move direction
end
end
--]]
if not s then
-- init
bots = {[4] = {}, [5] = {}}; -- [type] = {{1,1,10}, {3,2,10}}; -- {x,y,hp}
arena = {}; --[x][z] = {type, idx}
for i = -10,10 do arena[i] = {} for j=-10,10 do arena[i][j] = {0,0} end end
centerpos = self.spawnpos(); centerpos.y = centerpos.y+2
for i = -10,10 do arena[i] = {} for j=-10,10 do
arena[i][j] = {0,0}
keyboard.set({x=centerpos.x+i,y=centerpos.y-1,z=centerpos.z+j},1) -- build arena
keyboard.set({x=centerpos.x+i,y=centerpos.y,z=centerpos.z+j},0)
end end
TYPE = 4; -- 4,5 defines which bots are on the move/attack
DIR = 1
s=0
@ -67,7 +91,7 @@ prog1, _ = _G.loadstring( script1 ); prog2, _ = _G.loadstring( script2 );
end
end
if t%10 == 0 then
if t%10 == 0 then -- spawn new bot for each time every 10 seconds!
spawn_bot(0,-10,4)
spawn_bot(0,10,5)
end

View File

@ -1,8 +1,8 @@
-- return minetest object count for 5x5x5 blocks
-- minetest object listen in radius 100 around robot
if not init then init = true
if not init then init = false
local objs = minetest.get_objects_inside_radius(self.pos(), 30000);
local objs = minetest.get_objects_inside_radius(self.pos(), 100);
local ret = {};
local round = function(x) return math.floor(x/5)*5 end
@ -10,8 +10,17 @@ local ret = {};
for i = 1, #objs do
local p = objs[i]:get_pos();
local luaent = objs[i]:get_luaentity();
local entname = ""
if luaent then
entname = luaent.itemstring
if entname == "robot" then entname = entname .. " " .. luaent.name end
elseif objs[i]:is_player() then
entname = "PLAYER " .. objs[i]:get_player_name()
end
local phash = round(p.x) .. " " .. round(p.y) .. " " .. round(p.z);
ret[phash] = (ret[phash] or 0) + 1
ret[phash] = (ret[phash] or "") .. entname .. ", "
end
local out = {};
@ -19,10 +28,10 @@ for k,v in pairs(ret) do
out[#out+1] = {k,v}
end
table.sort(out, function(a,b) return a[2]>b[2] end)
--table.sort(out, function(a,b) return a[2]>b[2] end) -- additional stuff here - optional
local res = {};
for i = 1, #out do
res[#res+1] = out[i][1] .. "=" .. out[i][2]
res[#res+1] = out[i][1] .. " = " .. out[i][2]
end
self.label("#objects " .. #objs .. "\n" .. table.concat(res, "\n"))