basic_robot/commands.lua

311 lines
8.9 KiB
Lua
Raw Normal View History

2016-11-08 01:32:58 -08:00
basic_robot.commands = {};
2016-11-21 11:08:21 -08:00
-- 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: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",
["farming:seed_wheat"]="farming:wheat_1"}
local function tick(pos) -- needed for plants to start growing: minetest 0.4.14 farming
minetest.get_node_timer(pos):start(math.random(166, 286))
end
2016-11-08 01:32:58 -08:00
local pi = math.pi;
local function pos_in_dir(obj, dir) -- position after we move in specified direction
local yaw = obj:getyaw();
local pos = obj:getpos();
if dir == 1 then
yaw = yaw + pi/2;
elseif dir == 2 then
yaw = yaw - pi/2;
elseif dir == 3 then
elseif dir == 4 then
yaw = yaw+pi;
2016-11-14 08:20:04 -08:00
elseif dir == 5 then -- up
2016-11-08 01:32:58 -08:00
pos.y=pos.y+1
2016-11-14 08:20:04 -08:00
elseif dir == 6 then -- down
pos.y=pos.y-1
elseif dir == 7 then -- forward, down
2016-11-08 01:32:58 -08:00
pos.y=pos.y-1
end
2016-11-14 08:20:04 -08:00
if dir<5 or dir == 7 then -- left, right, back
2016-11-08 01:32:58 -08:00
pos.x = pos.x+math.cos(yaw)
pos.z = pos.z+math.sin(yaw)
end
2016-11-14 08:20:04 -08:00
2016-11-08 01:32:58 -08:00
return pos
end
2016-11-10 04:07:56 -08:00
basic_robot.commands.move = function(name,dir)
local obj = basic_robot.data[name].obj;
2016-11-08 01:32:58 -08:00
local pos = pos_in_dir(obj, dir)
2016-11-14 08:20:04 -08:00
-- can move through walkable nodes
if minetest.registered_nodes[minetest.get_node(pos).name].walkable then return end
2016-11-08 01:32:58 -08:00
-- up; no levitation!
if minetest.get_node({x=pos.x,y=pos.y-1,z=pos.z}).name == "air" and
minetest.get_node({x=pos.x,y=pos.y-2,z=pos.z}).name == "air" then
2016-11-14 08:20:04 -08:00
return false
2016-11-08 01:32:58 -08:00
end
2016-11-08 01:32:58 -08:00
obj:moveto(pos, true)
2016-11-14 08:20:04 -08:00
-- sit and stand up for model - doenst work for overwriten obj export
-- if dir == 5 then-- up
-- obj:set_animation({x=0,y=0})
-- elseif dir == 6 then -- down
-- obj:set_animation({x=81,y=160})
-- end
return true
2016-11-08 01:32:58 -08:00
end
2016-11-10 04:07:56 -08:00
basic_robot.commands.turn = function (name, angle)
local obj = basic_robot.data[name].obj;
2016-11-08 01:32:58 -08:00
local yaw = obj:getyaw()+angle;
obj:setyaw(yaw);
end
2016-11-10 04:07:56 -08:00
basic_robot.commands.dig = function(name,dir)
local digcount = 0;
if basic_robot.maxdig~=0 then
digcount = basic_robot.data[name].digcount;
if digcount > basic_robot.maxdig then return false end
end
2016-11-10 04:07:56 -08:00
local obj = basic_robot.data[name].obj;
2016-11-08 01:32:58 -08:00
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
2016-11-14 08:20:04 -08:00
if minetest.is_protected(pos,luaent.owner ) then return false end
local nodename = minetest.get_node(pos).name;
2016-11-14 08:20:04 -08:00
if nodename == "air" then return false end
local spos = obj:get_luaentity().spawnpos;
local inv = minetest.get_meta(spos):get_inventory();
if not inv then return end
2016-11-21 11:08:21 -08:00
--inv:add_item("main",ItemStack( nodename ));
2016-11-21 11:08:21 -08:00
basic_robot.give_drops(nodename, inv);
minetest.set_node(pos,{name = "air"})
basic_robot.data[name].digcount = digcount+1;
2016-11-21 11:08:21 -08:00
--DS: sounds
local sounds = minetest.registered_nodes[nodename].sounds
2016-11-14 08:20:04 -08:00
if sounds then
local sound = sounds.dug
if sound then
minetest.sound_play(sound,{object=obj, max_hear_distance = 10})
end
end
2016-11-21 11:08:21 -08:00
2016-11-14 08:20:04 -08:00
return true
2016-11-08 01:32:58 -08:00
end
2016-11-14 08:20:04 -08:00
basic_robot.commands.insert_item = function(name,item, inventory,dir)
local obj = basic_robot.data[name].obj;
local tpos = pos_in_dir(obj, dir); -- position of target block
local luaent = obj:get_luaentity();
if minetest.is_protected(tpos,luaent.owner ) then return false end
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
local meta = minetest.get_meta(pos);
local tmeta = minetest.get_meta(tpos);
local inv = minetest.get_meta(pos):get_inventory();
local tinv = minetest.get_meta(tpos):get_inventory();
if not inventory then inventory = "main"; end
--if not inv then return end
local stack = ItemStack(item);
if (not inv:contains_item("main", stack) or not tinv:room_for_item(inventory, stack)) and meta:get_int("admin")~=1 then
return false
end
tinv:add_item(inventory,stack);
inv:remove_item("main", stack);
return true
end
basic_robot.commands.take_item = function(name,item, inventory,dir)
local obj = basic_robot.data[name].obj;
local tpos = pos_in_dir(obj, dir); -- position of target block
local luaent = obj:get_luaentity();
if minetest.is_protected(tpos,luaent.owner ) then return false end
local pos = basic_robot.data[name].spawnpos; -- position of spawner block
if basic_robot.bad_inventory_blocks[ minetest.get_node(tpos).name ] then return false end -- dont allow take from
local meta = minetest.get_meta(pos);
local tmeta = minetest.get_meta(tpos);
local inv = minetest.get_meta(pos):get_inventory();
local tinv = minetest.get_meta(tpos):get_inventory();
if not inventory then inventory = "main"; end
--if not inv then return end
local stack = ItemStack(item);
if (not tinv:contains_item(inventory, stack) or not inv:room_for_item("main", stack)) and meta:get_int("admin")~=1 then
return false
end
inv:add_item("main",stack);
tinv:remove_item(inventory, stack);
return true
end
2016-11-10 04:07:56 -08:00
basic_robot.commands.read_node = function(name,dir)
local obj = basic_robot.data[name].obj;
2016-11-08 01:32:58 -08:00
local pos = pos_in_dir(obj, dir)
return minetest.get_node(pos).name or ""
end
2016-11-21 11:08:21 -08:00
basic_robot.commands.read_text = function(name,dir,stringname)
local obj = basic_robot.data[name].obj;
local pos = pos_in_dir(obj, dir)
2016-11-21 11:08:21 -08:00
if stringname == nil then
stringname = "infotext"
end
return minetest.get_meta(pos):get_string(stringname) or ""
end
2016-11-08 01:32:58 -08:00
2016-11-10 04:07:56 -08:00
basic_robot.commands.place = function(name,nodename, dir)
local obj = basic_robot.data[name].obj;
2016-11-08 01:32:58 -08:00
local pos = pos_in_dir(obj, dir)
local luaent = obj:get_luaentity();
2016-11-14 08:20:04 -08:00
if minetest.is_protected(pos,luaent.owner ) then return false end
if minetest.get_node(pos).name~="air" then return false end
local spos = obj:get_luaentity().spawnpos;
2016-11-10 04:07:56 -08:00
local meta = minetest.get_meta(spos);
local inv = meta:get_inventory();
2016-11-14 08:20:04 -08:00
if not inv then return false end
2016-11-10 04:07:56 -08:00
if not inv:contains_item("main", ItemStack(nodename)) and meta:get_int("admin")~=1 then return end
inv:remove_item("main", ItemStack(nodename));
2016-11-10 04:07:56 -08:00
2016-11-14 08:20:04 -08:00
--DS
local sounds = minetest.registered_nodes[nodename].sounds
if sounds then
local sound = sounds.place
if sound then
minetest.sound_play(sound,{object=obj, max_hear_distance = 10})
end
end
2016-11-21 11:08:21 -08:00
placename = basic_robot.plant_table[nodename];
if placename then
minetest.set_node(pos,{name = placename})
tick(pos); -- needed for seeds to grow
else
minetest.set_node(pos,{name = nodename})
end
2016-11-14 08:20:04 -08:00
return true
end
basic_robot.commands.attack = function(name, target) -- attack range 4, damage 5
local reach = 4;
local damage = 5;
local tplayer = minetest.get_player_by_name(target);
if not tplayer then return false end
local obj = basic_robot.data[name].obj;
local pos = obj:getpos();
local tpos = tplayer:getpos();
if math.abs(pos.x-tpos.x)> reach or math.abs(pos.y-tpos.y)> reach or math.abs(pos.z-tpos.z)> reach then
return false
end
tplayer:set_hp(tplayer:get_hp()-damage)
return true
end
basic_robot.commands.read_book = function (itemstack) -- itemstack should contain book
local data = minetest.deserialize(itemstack:get_metadata())
if data then
return data.text;
else
return nil
end
end
basic_robot.commands.write_book = function(name,text) -- returns itemstack containing book
local lpp = 14;
local new_stack = ItemStack("default:book_written")
local data = {}
data.title = "program book"
data.text = text
data.text_len = #data.text
data.page = 1
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
data.owner = name
local data_str = minetest.serialize(data)
new_stack:set_metadata(data_str);
return new_stack;
end
2016-11-21 11:08:21 -08:00
basic_robot.give_drops = function(nodename, inv) -- gives apropriate drops when node is dug
local table = minetest.registered_items[nodename];
local dropname;
if table~=nil then --put in chest
if table.drop~= nil then -- drop handling
if table.drop.items then
--handle drops better, emulation of drop code
local max_items = table.drop.max_items or 0;
if max_items==0 then -- just drop all the items (taking the rarity into consideration)
max_items = #table.drop.items or 0;
end
local drop = table.drop;
local i = 0;
for k,v in pairs(drop.items) do
if i > max_items then break end; i=i+1;
local rare = v.rarity or 1;
if math.random(1, rare)==1 then
dropname = v.items[math.random(1,#v.items)]; -- pick item randomly from list
inv:add_item("main",dropname);
end
end
else
inv:add_item("main",table.drop);
end
else
inv:add_item("main",nodename);
end
end
end