-craft(item) robot can now craft any item if it has ingredients for it ( group items are problem )

-read_text can now read (int) numbers from node meta
extra:
-robot can now fertilize farming:soil_wet by inserting farming:fertilizer into it
master
rnd1 2017-02-10 09:59:36 +01:00
parent 873f32dfbf
commit 4c169bb517
2 changed files with 85 additions and 16 deletions

View File

@ -3,7 +3,7 @@ basic_robot.commands = {};
-- set up nodes for planting (for example seeds -> plant) : [nodename] = plant_name
basic_robot.plant_table = {["farming:seed_barley"]="farming:barley_1",["farming:beans"]="farming:beanpole_1", -- so it works with farming redo mod
["farming:blueberries"]="farming:blueberry_1",["farming:carrot"]="farming:carrot_1",["farming:cocoa_beans"]="farming:cocoa_1",
["farming:coffee_beans"]="farming:coffee_1",["farming:corn"]="farming:corn_1",["farming:blueberries"]="farming:blueberry_1",
["farming:coffee_beans"]="farming:coffee_1",["farming:corn"]="farming:corn_1",
["farming:seed_cotton"]="farming:cotton_1",["farming:cucumber"]="farming:cucumber_1",["farming:grapes"]="farming:grapes_1",
["farming:melon_slice"]="farming:melon_1",["farming:potato"]="farming:potato_1",["farming:pumpkin_slice"]="farming:pumpkin_1",
["farming:raspberries"]="farming:raspberry_1",["farming:rhubarb"]="farming:rhubarb_1",["farming:tomato"]="farming:tomato_1",
@ -132,6 +132,20 @@ basic_robot.commands.insert_item = function(name,item, inventory,dir)
local tmeta = minetest.get_meta(tpos);
local inv = minetest.get_meta(pos):get_inventory();
-- fertilize if soil
if item == "farming:fertilizer" then
local stack = ItemStack(item);
if minetest.get_node(tpos).name == "farming:soil_wet" and (meta:get_int("admin")==1 or inv:contains_item("main", stack)) then
inv:remove_item("main", stack);
local nutrient = tmeta:get_int("nutrient"); nutrient = nutrient + 10; if nutrient>20 then nutrient = 20 end
tmeta:set_int("nutrient",nutrient);
minetest.set_node({x=tpos.x,y=tpos.y+1,z=tpos.z},{name = "air"})
return true
end
end
local tinv = minetest.get_meta(tpos):get_inventory();
if not inventory then inventory = "main"; end
@ -251,13 +265,17 @@ basic_robot.commands.read_node = function(name,dir)
return minetest.get_node(pos).name or ""
end
basic_robot.commands.read_text = function(name,dir,stringname)
basic_robot.commands.read_text = function(name,mode,dir,stringname)
if not mode then mode = 0 end
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
if stringname == nil then
stringname = "infotext"
end
return minetest.get_meta(pos):get_string(stringname) or ""
if mode == 1 then return minetest.get_meta(pos):get_int(stringname) else
return minetest.get_meta(pos):get_string(stringname) or "" end
end
basic_robot.commands.write_text = function(name,dir,text)
@ -548,4 +566,48 @@ basic_robot.commands.keyboard = {
end,
}
}
basic_robot.commands.craftcache = {};
basic_robot.commands.craft = function(item, name)
if not item then return end
local cache = basic_robot.commands.craftcache[name];
if not cache then basic_robot.commands.craftcache[name] = {}; cache = basic_robot.commands.craftcache[name] end
local itemlist = {};
if cache.item == item then-- read cache
itemlist = cache.itemlist;
else
--local table = minetest.registered_items[nodename];
local craft = minetest.get_craft_recipe(item);
if craft and craft.type == "normal" and craft.items then else return end
local items = craft.items;
for _,item in pairs(items) do
itemlist[item]=(itemlist[item] or 0)+1;
end
cache.item = item;
cache.itemlist = itemlist;
end
--minetest.chat_send_all(item)
--minetest.chat_send_all(dump(itemlist))
-- check if all items from itemlist..
-- craft item
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
local inv = minetest.get_meta(pos):get_inventory();
for item,quantity in pairs(itemlist) do
local stack = ItemStack(item .. " " .. quantity);
if not inv:contains_item("main",stack) then return false end
end
for item,quantity in pairs(itemlist) do
local stack = ItemStack(item .. " " .. quantity);
inv:remove_item("main",stack);
end
inv:add_item("main",ItemStack(item))
return true
end

View File

@ -13,7 +13,7 @@ basic_robot.maxenergy = 1; -- how much energy available per run, 0 = unlimited
basic_robot.version = "01/18a";
basic_robot.version = "02/07a";
basic_robot.data = {}; -- stores all robot data
--[[
@ -113,7 +113,10 @@ function getSandboxEnv (name)
pickup = function(r) -- pick up items around robot
return commands.pickup(r, name);
end,
craft = function(item)
return commands.craft(item, name)
end,
self = {
pos = function() return basic_robot.data[name].obj:getpos() end,
@ -221,7 +224,10 @@ function getSandboxEnv (name)
find_nodes =
function(nodename,r)
if r>8 then return false end
return (minetest.find_node_near(basic_robot.data[name].obj:getpos(), r, nodename)~=nil)
local q = minetest.find_node_near(basic_robot.data[name].obj:getpos(), r, nodename);
if q==nil then return false end
local p = basic_robot.data[name].obj:getpos()
return math.sqrt((p.x-q.x)^2+(p.y-q.y)^2+(p.z-q.z)^2)
end, -- in radius around position
find_player =
@ -269,12 +275,12 @@ function getSandboxEnv (name)
},
read_text = { -- returns text
left = function(stringname) return commands.read_text(name,1,stringname ) end,
right = function(stringname) return commands.read_text(name,2,stringname) end,
forward = function(stringname) return commands.read_text(name,3,stringname) end,
backward = function(stringname) return commands.read_text(name,4,stringname) end,
down = function(stringname) return commands.read_text(name,6,stringname) end,
up = function(stringname) return commands.read_text(name,5,stringname) end,
left = function(stringname,mode) return commands.read_text(name,mode,1,stringname ) end,
right = function(stringname,mode) return commands.read_text(name,mode,2,stringname) end,
forward = function(stringname,mode) return commands.read_text(name,mode,3,stringname) end,
backward = function(stringname,mode) return commands.read_text(name,mode,4,stringname) end,
down = function(stringname,mode) return commands.read_text(name,mode,6,stringname) end,
up = function(stringname,mode) return commands.read_text(name,mode,5,stringname) end,
},
write_text = { -- returns text
@ -1036,13 +1042,14 @@ local on_receive_robot_form = function(pos, formname, fields, sender)
" if index>0 it returns itemname\n"..
"activate.direction(mode) activates target block\n"..
"pickup(r) picks up all items around robot in radius r<8 and returns list or nil\n"..
"craft(item) crafts item if required materials are present in inventory\n"..
"take.direction(item, inventory) takes item from target inventory into robot inventory\n"..
"read_text.direction(stringname) reads text of signs, chests and other blocks, optional stringname for other meta\n"..
"write_text.direction(text) writes text to target block as infotext\n"..
"read_text.direction(stringname,mode) reads text of signs, chests and other blocks, optional stringname for other meta,\n mode 1 read number\n"..
"write_text.direction(text,mode) writes text to target block as infotext\n"..
" **BOOKS/CODE\ntitle,text=book.read(i) returns title,contents of book at i-th position in library \nbook.write(i,title,text) writes book at i-th position at spawner library\n"..
"code.run(text) compiles and runs the code in sandbox\n"..
"code.set(text) replaces current bytecode of robot\n"..
"find_nodes(\"default:dirt\",3) is true if node can be found at radius 3 around robot, otherwise false\n"..
"find_nodes(\"default:dirt\",3) returns distance to node in radius 3 around robot, or false if none\n"..
" **PLAYERS\n"..
"find_player(3) finds players in radius 3 around robot and returns list, if none returns nil\n"..
"attack(target) attempts to attack target player if nearby \n"..