help page changed, farming reworked to a 3x3 field, farming sign added

master
Joachim Stolberg 2019-04-06 23:27:37 +02:00
parent ae3df2fd5a
commit 312f66b7ca
9 changed files with 192 additions and 125 deletions

View File

@ -22,9 +22,25 @@ local I,_ = dofile(MP.."/intllib.lua")
local lib = signs_bot.lib
local function planting(base_pos, pos, stack)
if lib.not_protected(base_pos, pos) and lib.is_air_like(pos) then
local item = signs_bot.FarmingSeed[stack:get_name()]
local function inv_get_item(pos, slot)
local inv = minetest.get_inventory({type="node", pos=pos})
return lib.get_inv_items(inv, "main", slot, 1)
end
local function inv_put_item(pos, mem, name)
local inv = minetest.get_inventory({type="node", pos=pos})
local leftover = inv:add_item("main", ItemStack(name))
if leftover:get_count() > 0 then
lib.drop_items(mem.robot_pos, leftover)
end
end
local function planting(base_pos, mem, slot)
local pos = mem.pos_tbl and mem.pos_tbl[mem.steps]
mem.steps = (mem.steps or 1) + 1
if pos and lib.not_protected(base_pos, pos) and lib.is_air_like(pos) then
local stack = inv_get_item(base_pos, slot)
local item = stack and signs_bot.FarmingSeed[stack:get_name()]
if item and item.seed then
minetest.set_node(pos, {name = item.seed, paramtype2 = "wallmounted", param2 = 1})
if item.t1 ~= nil then
@ -33,14 +49,12 @@ local function planting(base_pos, pos, stack)
-- be used.
minetest.get_node_timer(pos):start(math.random(item.t1, item.t2))
end
return true
end
end
return false
end
signs_bot.register_botcommand("plant_seed", {
mod = "core",
mod = "farming",
params = "<slot>",
description = I("Plant farming seeds\nin front of the robot"),
check = function(slot)
@ -49,46 +63,73 @@ signs_bot.register_botcommand("plant_seed", {
end,
cmnd = function(base_pos, mem, slot)
slot = tonumber(slot)
local pos = lib.dest_pos(mem.robot_pos, mem.robot_param2, {0})
local inv = minetest.get_inventory({type="node", pos=base_pos})
local item = lib.get_inv_items(inv, "main", slot, 1)
if item then
if not planting(base_pos, pos, item) then
lib.put_inv_items(inv, "main", slot, item)
end
if not mem.steps then
mem.pos_tbl = signs_bot.lib.gen_position_table(mem.robot_pos, mem.robot_param2, 3, 3, 0)
mem.steps = 1
end
mem.pos_tbl = mem.pos_tbl or {}
planting(base_pos, mem, slot)
if mem.steps > #mem.pos_tbl then
mem.steps = nil
return true
end
return true
end,
})
local function harvesting(base_pos, pos, inv, slot)
if lib.not_protected(base_pos, pos) then
local function harvesting(base_pos, mem)
local pos = mem.pos_tbl and mem.pos_tbl[mem.steps]
mem.steps = (mem.steps or 1) + 1
if pos and lib.not_protected(base_pos, pos) then
local node = minetest.get_node_or_nil(pos)
local item = signs_bot.FarmingCrop[node.name]
if item and item.inv_crop then
if item and item.inv_crop and item.inv_seed then
minetest.remove_node(pos)
lib.put_inv_items(inv, "main", slot, ItemStack(item.inv_crop))
if item.inv_seed then
lib.put_inv_items(inv, "main", slot, ItemStack(item.inv_seed))
end
inv_put_item(base_pos, mem, item.inv_crop)
inv_put_item(base_pos, mem, item.inv_seed)
end
end
end
signs_bot.register_botcommand("harvest", {
mod = "core",
params = "<slot>",
description = I("Harvest farming products\nin front of the robot"),
check = function(slot)
slot = tonumber(slot)
return slot and slot > 0 and slot < 9
end,
cmnd = function(base_pos, mem, slot)
slot = tonumber(slot)
local pos = lib.dest_pos(mem.robot_pos, mem.robot_param2, {0})
local inv = minetest.get_inventory({type="node", pos=base_pos})
harvesting(base_pos, pos, inv, slot)
return true
mod = "farming",
params = "",
description = I("Harvest farming products\nin front of the robot\non a 3x3 field."),
cmnd = function(base_pos, mem)
if not mem.steps then
mem.pos_tbl = signs_bot.lib.gen_position_table(mem.robot_pos, mem.robot_param2, 3, 3, 0)
mem.steps = 1
end
mem.pos_tbl = mem.pos_tbl or {}
harvesting(base_pos, mem)
if mem.steps > #mem.pos_tbl then
mem.steps = nil
return true
end
end,
})
local CMD = [[dig_sign 1
move
harvest
plant_seed 1
backward
place_sign 1
turn_off]]
signs_bot.register_sign({
name = "farming",
description = I('Sign "farming"'),
commands = CMD,
image = "signs_bot_sign_farming.png",
})
minetest.register_craft({
output = "signs_bot:farming 2",
recipe = {
{"group:wood", "default:stick", "group:wood"},
{"dye:black", "default:stick", "dye:yellow"},
{"dye:grey", "", ""}
}
})

View File

@ -119,7 +119,7 @@ function signs_bot.robot_add(base_pos, robot_pos, param2, num, slot, is_fuel)
end
signs_bot.register_botcommand("take_item", {
mod = "core",
mod = "item",
params = "<num> <slot>",
description = I("Take <num> items from a chest like node\nand put it into the item inventory.\n"..
"<slot> is the inventory slot (1..8)"),
@ -143,7 +143,7 @@ signs_bot.register_botcommand("take_item", {
})
signs_bot.register_botcommand("add_item", {
mod = "core",
mod = "item",
params = "<num> <slot>",
description = I("Add <num> items to a chest like node\ntaken from the item inventory.\n"..
"<slot> is the inventory slot (1..8)"),
@ -167,7 +167,7 @@ signs_bot.register_botcommand("add_item", {
})
signs_bot.register_botcommand("add_fuel", {
mod = "core",
mod = "item",
params = "<num> <slot>",
description = I("Add <num> fuel items to a furnace like node\ntaken from the item inventory.\n"..
"<slot> is the inventory slot (1..8)"),
@ -191,7 +191,7 @@ signs_bot.register_botcommand("add_fuel", {
})
signs_bot.register_botcommand("pickup_items", {
mod = "core",
mod = "item",
params = "<slot>",
description = I("Pick up all objects\n"..
"in a 3x3 field.\n"..
@ -218,7 +218,7 @@ signs_bot.register_botcommand("pickup_items", {
})
signs_bot.register_botcommand("drop_items", {
mod = "core",
mod = "item",
params = "<num> <slot>",
description = I("Drop items in front of the bot.\n"..
"<slot> is the inventory slot (1..8)"),

View File

@ -97,7 +97,7 @@ local function backward_robot(pos, param2)
end
signs_bot.register_botcommand("backward", {
mod = "core",
mod = "move",
params = "",
description = I("Move the robot one step back"),
cmnd = function(base_pos, mem)
@ -121,7 +121,7 @@ local function turn_robot(pos, param2, dir)
end
signs_bot.register_botcommand("turn_left", {
mod = "core",
mod = "move",
params = "",
description = I("Turn the robot to the left"),
cmnd = function(base_pos, mem)
@ -131,7 +131,7 @@ signs_bot.register_botcommand("turn_left", {
})
signs_bot.register_botcommand("turn_right", {
mod = "core",
mod = "move",
params = "",
description = I("Turn the robot to the right"),
cmnd = function(base_pos, mem)
@ -141,7 +141,7 @@ signs_bot.register_botcommand("turn_right", {
})
signs_bot.register_botcommand("turn_around", {
mod = "core",
mod = "move",
params = "",
description = I("Turn the robot around"),
cmnd = function(base_pos, mem)
@ -176,7 +176,7 @@ local function robot_up(pos, param2)
end
signs_bot.register_botcommand("move_up", {
mod = "core",
mod = "move",
params = "",
description = I("Move the robot upwards"),
cmnd = function(base_pos, mem)
@ -210,7 +210,7 @@ local function robot_down(pos, param2)
end
signs_bot.register_botcommand("move_down", {
mod = "core",
mod = "move",
params = "",
description = I("Move the robot down"),
cmnd = function(base_pos, mem)
@ -223,7 +223,7 @@ signs_bot.register_botcommand("move_down", {
})
signs_bot.register_botcommand("pause", {
mod = "core",
mod = "move",
params = "<sec>",
description = I("Stop the robot for <sec> seconds\n(1..9999)"),
check = function(sec)
@ -243,7 +243,7 @@ signs_bot.register_botcommand("pause", {
})
signs_bot.register_botcommand("stop", {
mod = "core",
mod = "move",
params = "",
description = I("Stop the robot."),
cmnd = function(base_pos, mem, slot)
@ -252,7 +252,7 @@ signs_bot.register_botcommand("stop", {
})
signs_bot.register_botcommand("turn_off", {
mod = "core",
mod = "move",
params = "",
description = I("Turn the robot off\n"..
"and put it back in the box."),

View File

@ -96,42 +96,6 @@ local function inv_put_item(pos, mem, name)
end
end
--
-- Determine the field positions
--
local function start_pos(robot_pos, robot_param2, x_size, lvl_offs)
local pos = lib.next_pos(robot_pos, robot_param2)
pos = {x=pos.x, y=pos.y+lvl_offs, z=pos.z}
if x_size == 5 then
return lib.dest_pos(pos, robot_param2, {3,3})
else
return lib.dest_pos(pos, robot_param2, {3})
end
end
--
-- Return a table with all positions to copy
--
local function gen_position_table(robot_pos, robot_param2, x_size, z_size, lvl_offs)
local tbl = {}
local pos = start_pos(robot_pos, robot_param2, x_size, lvl_offs)
tbl[#tbl+1] = pos
z_size = math.min(z_size, 5)
for z = 1,z_size do
for x = 1,x_size-1 do
local dir = (z % 2) == 0 and 3 or 1
pos = lib.dest_pos(pos, robot_param2, {dir})
tbl[#tbl+1] = pos
end
if z < z_size then
pos = lib.dest_pos(pos, robot_param2, {0})
tbl[#tbl+1] = pos
end
end
return tbl
end
local function pattern_copy(base_pos, mem)
local src_pos = mem.src_pos_tbl[mem.steps]
local dst_pos = mem.dst_pos_tbl[mem.steps]
@ -150,7 +114,7 @@ end
signs_bot.register_botcommand("pattern", {
mod = "clone",
mod = "copy",
params = "",
description = I("Store pattern to be cloned."),
cmnd = function(base_pos, mem)
@ -161,7 +125,7 @@ signs_bot.register_botcommand("pattern", {
})
signs_bot.register_botcommand("copy", {
mod = "core",
mod = "copy",
params = "<size> <lvl>",
description = I("Copy the nodes from\n"..
"the stored pattern position\n"..
@ -176,13 +140,14 @@ signs_bot.register_botcommand("copy", {
return ValidSizes[size]
end,
cmnd = function(base_pos, mem, size, lvl)
if not mem.pttrn_pos then return true end
if not mem.steps then
local x,z = size:match('(%d)x(%d)')
lvl = tonumber(lvl or 0)
mem.x_size = tonumber(x)
mem.z_size = tonumber(z)
mem.src_pos_tbl = gen_position_table(mem.pttrn_pos, mem.pttrn_param2, x, z, lvl)
mem.dst_pos_tbl = gen_position_table(mem.robot_pos, mem.robot_param2, x, z, 0)
mem.src_pos_tbl = signs_bot.lib.gen_position_table(mem.pttrn_pos, mem.pttrn_param2, x, z, lvl)
mem.dst_pos_tbl = signs_bot.lib.gen_position_table(mem.robot_pos, mem.robot_param2, x, z, 0)
mem.dir_offs = mem.robot_param2 - mem.pttrn_param2
mem.steps = 1
end

View File

@ -70,7 +70,7 @@ local function place_item(base_pos, robot_pos, param2, slot, dir, level)
end
signs_bot.register_botcommand("place_front", {
mod = "core",
mod = "place",
params = "<slot> <lvl>",
description = I("Place an item in front of the robot\n"..
"<slot> is the inventory slot (1..8)\n"..
@ -91,7 +91,7 @@ signs_bot.register_botcommand("place_front", {
})
signs_bot.register_botcommand("place_left", {
mod = "core",
mod = "place",
params = "<slot> <lvl>",
description = I("Place an item on the left side\n"..
"<slot> is the inventory slot (1..8)\n"..
@ -112,7 +112,7 @@ signs_bot.register_botcommand("place_left", {
})
signs_bot.register_botcommand("place_right", {
mod = "core",
mod = "place",
params = "<slot> <lvl>",
description = I("Place an item on the right side\n"..
"<slot> is the inventory slot (1..8)\n"..
@ -145,7 +145,7 @@ local function dig_item(base_pos, robot_pos, param2, slot, dir, level)
end
signs_bot.register_botcommand("dig_front", {
mod = "core",
mod = "place",
params = "<slot> <lvl>",
description = I("Dig an item in front of the robot\n"..
"<slot> is the inventory slot (1..8)\n"..
@ -166,7 +166,7 @@ signs_bot.register_botcommand("dig_front", {
})
signs_bot.register_botcommand("dig_left", {
mod = "core",
mod = "place",
params = "<slot> <lvl>",
description = I("Dig an item on the left side\n"..
"<slot> is the inventory slot (1..8)\n"..
@ -187,7 +187,7 @@ signs_bot.register_botcommand("dig_left", {
})
signs_bot.register_botcommand("dig_right", {
mod = "core",
mod = "place",
params = "<slot> <lvl>",
description = I("Dig an item on the right side\n"..
"<slot> is the inventory slot (1..8)\n"..
@ -220,7 +220,7 @@ local function rotate_item(base_pos, robot_pos, param2, dir, level, steps)
end
signs_bot.register_botcommand("rotate_item", {
mod = "core",
mod = "place",
params = "<lvl> <steps>",
description = I("Rotate an item in front of the robot\n"..
"<lvl> is one of: -1 0 +1\n"..

View File

@ -35,15 +35,18 @@ Supported commands:
]])
local lHelp = {}
local sHelp = ""
local function gen_help_text()
local text = HELP..signs_bot.get_help_text()
text = minetest.formspec_escape(text)
sHelp = text:gsub("\n", ", ")
lHelp = string.split(sHelp, ",")
end
minetest.after(2, gen_help_text)
local sCmnds = ""
local lCmnds = {}
local tCmndIdx = {}
minetest.after(2, function()
for idx,cmnd in ipairs(signs_bot.get_commands()) do
cmnd = minetest.formspec_escape(cmnd)
lCmnds[#lCmnds+1] = cmnd
tCmndIdx[cmnd] = idx
end
sCmnds = table.concat(lCmnds, ",")
end)
local function formspec1(meta)
@ -64,14 +67,15 @@ local function formspec1(meta)
"button[7,7.5;2,1;check;"..I("Check").."]"
end
local function formspec2()
local function formspec2(pos, text)
return "size[9,8]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"tabheader[0,0;tab;"..I("Commands,Help")..";2;;true]"..
"table[0.1,0.1;8.6,7.2;help;"..sHelp..";1]"..
"button[3.5,7.5;2,1;copy;"..I("Copy").."]"
"table[0.1,0;8.6,4;command;"..sCmnds..";"..pos.."]"..
"textarea[0.3,4.5;9,3.5;help;Help:;"..text.."]"..
"button[3,7.5;3,1;copy;"..I("Copy Cmnd").."]"
end
local function trim_text(text)
@ -148,18 +152,23 @@ minetest.register_node("signs_bot:sign_cmnd", {
elseif fields.key_enter_field then
check_and_store(pos, meta, fields)
elseif fields.copy then
append_line(pos, meta, lHelp[meta:get_int("help_pos")])
append_line(pos, meta, lCmnds[meta:get_int("help_pos")])
elseif fields.tab == "1" then
meta:set_string("formspec", formspec1(meta))
elseif fields.tab == "2" then
check_and_store(pos, meta, fields)
meta:set_string("formspec", formspec2(meta))
elseif fields.help then
local evt = minetest.explode_table_event(fields.help)
local pos = meta:get_int("help_pos")
local cmnd = lCmnds[pos] or ""
meta:set_string("formspec", formspec2(pos, signs_bot.get_help_text(cmnd)))
elseif fields.command then
local evt = minetest.explode_table_event(fields.command)
if evt.type == "DCL" then
append_line(pos, meta, lHelp[tonumber(evt.row)])
append_line(pos, meta, lCmnds[tonumber(evt.row)])
elseif evt.type == "CHG" then
meta:set_int("help_pos", tonumber(evt.row))
local pos = tonumber(evt.row)
meta:set_int("help_pos", pos)
local cmnd = lCmnds[pos] or ""
meta:set_string("formspec", formspec2(pos, signs_bot.get_help_text(cmnd)))
end
end
end,
@ -226,7 +235,7 @@ local function place_sign(base_pos, robot_pos, param2, slot)
end
signs_bot.register_botcommand("place_sign", {
mod = "core",
mod = "sign",
params = "<slot>",
description = I("Place a sign in front of the robot\ntaken from the signs inventory\n"..
"<slot> is the inventory slot (1..6)"),
@ -259,7 +268,7 @@ local function place_sign_behind(base_pos, robot_pos, param2, slot)
end
signs_bot.register_botcommand("place_sign_behind", {
mod = "core",
mod = "sign",
params = "<slot>",
description = I("Place a sign behind the robot\ntaken from the signs inventory\n"..
"<slot> is the inventory slot (1..6)"),
@ -298,7 +307,7 @@ local function dig_sign(base_pos, robot_pos, param2, slot)
end
signs_bot.register_botcommand("dig_sign", {
mod = "core",
mod = "sign",
params = "<slot>",
description = I("Dig the sign in front of the robot\n"..
"and add it to the signs inventory.\n"..
@ -331,7 +340,7 @@ local function trash_sign(base_pos, robot_pos, param2, slot)
end
signs_bot.register_botcommand("trash_sign", {
mod = "core",
mod = "sign",
params = "<slot>",
description = I("Dig the sign in front of the robot\n"..
"and add the cleared sign to\nthe item iventory.\n"..

View File

@ -29,7 +29,7 @@ local tPos2Dir = {l = "l", r = "r", L = "l", R = "l", f = "f", F = "f"}
local tCommands = {}
local SortedKeys = {}
local SortedMods = {}
local tMods = {}
--
-- Command register API function
--
@ -40,6 +40,7 @@ function signs_bot.register_botcommand(name, def)
if not SortedKeys[def.mod] then
SortedKeys[def.mod] = {}
SortedMods[#SortedMods+1] = def.mod
tMods[#tMods+1] = def.mod
end
local idx = #SortedKeys[def.mod] + 1
SortedKeys[def.mod][idx] = name
@ -115,7 +116,7 @@ local function move(base_pos, mem)
end
signs_bot.register_botcommand("move", {
mod = "core",
mod = "move",
params = "<steps>",
description = I("Move the robot 1..99 steps forward."),
check = function(steps)
@ -184,17 +185,27 @@ function signs_bot.run_next_command(base_pos, mem)
return res -- true if ok, false if error or finished
end
function signs_bot.get_help_text()
function signs_bot.get_commands()
local tbl = {}
for _,mod in ipairs(SortedMods) do
tbl[#tbl+1] = mod..I(" commands:")
for _,cmnd in ipairs(SortedKeys[mod]) do
local item = tCommands[cmnd]
tbl[#tbl+1] = item.name.." "..item.params
local text = string.gsub(item.description, "\n", "\n -- ")
tbl[#tbl+1] = " -- "..text
tbl[#tbl+1] = " "..item.name.." "..item.params
end
end
return table.concat(tbl, "\n")
return tbl
end
function signs_bot.get_help_text(cmnd)
if cmnd then
cmnd = unpack(string.split(cmnd, " "))
local item = tCommands[cmnd]
if item then
return item.description
end
end
return I("unknown command")
end

40
lib.lua
View File

@ -64,6 +64,8 @@ function signs_bot.lib.get_node_lvm(pos)
return node
end
local next_pos = signs_bot.lib.next_pos
local dest_pos = signs_bot.lib.dest_pos
local get_node_lvm = signs_bot.lib.get_node_lvm
-- check if posA == air-like and posB == solid and no player around
@ -205,3 +207,41 @@ function signs_bot.lib.activate_extender_nodes(pos, is_sensor)
activate_extender_node({x=pos.x, y=pos.y, z=pos.z-1})
activate_extender_node({x=pos.x, y=pos.y, z=pos.z+1})
end
--
-- Determine the field positions
--
local function start_pos(robot_pos, robot_param2, x_size, lvl_offs)
local pos = next_pos(robot_pos, robot_param2)
pos = {x=pos.x, y=pos.y+lvl_offs, z=pos.z}
if x_size == 5 then
return dest_pos(pos, robot_param2, {3,3})
else
return dest_pos(pos, robot_param2, {3})
end
end
--
-- Return a table with all positions to copy
--
function signs_bot.lib.gen_position_table(robot_pos, robot_param2, x_size, z_size, lvl_offs)
local tbl = {}
if robot_pos and robot_param2 and x_size and z_size and lvl_offs then
local pos = start_pos(robot_pos, robot_param2, x_size, lvl_offs)
tbl[#tbl+1] = pos
z_size = math.min(z_size, 5)
for z = 1,z_size do
for x = 1,x_size-1 do
local dir = (z % 2) == 0 and 3 or 1
pos = dest_pos(pos, robot_param2, {dir})
tbl[#tbl+1] = pos
end
if z < z_size then
pos = dest_pos(pos, robot_param2, {0})
tbl[#tbl+1] = pos
end
end
end
return tbl
end

View File

@ -56,6 +56,7 @@ local function register_sign(def)
local meta = minetest.get_meta(pos)
meta:set_string("signs_bot_cmnd", def.commands)
meta:set_string("formspec", formspec(def.commands))
meta:set_string("infotext", def.description)
end,
on_rotate = screwdriver.disallow,
paramtype = "light",