added put_into_block_inv and take_from_block_inv formspec and storing

This commit is contained in:
Sokomine 2021-12-26 22:43:06 +01:00
parent aae92b7da3
commit f01744aa70
2 changed files with 89 additions and 22 deletions

View File

@ -45,8 +45,8 @@ local check_what = {
"- please select -",
"an internal state (i.e. of a quest)", -- 2
"a block somewhere", -- 3
minetest.formspec_escape("put item from the NPC's inventory into a chest, furnace etc."), -- 4
minetest.formspec_escape("take item from a chest, furnace etc. and put it into the NPC's inventory"),
"put item from the NPC's inventory into a chest etc.", -- 4
"take item from a chest etc. and put it into the NPC's inventory",
-- 5
"NPC crafts something", -- 6
"go to other dialog if the previous effect failed", -- 7
@ -237,6 +237,23 @@ yl_speak_up.show_effect = function(r, pname)
return "Send chat message: \""..tostring(r.r_value).."\""
elseif(r.r_type == "custom") then
return "Call custom function with param: \""..tostring(r.r_value).."\"."
elseif(r.r_type == "put_into_block_inv") then
if(not(r.r_pos) or type(r.r_pos) ~= "table"
or not(r.r_pos.x) or not(r.r_pos.y) or not(r.r_pos.z)) then
return "ERROR: r.r_pos is "..minetest.serialize(r.r_pos)
end
return "Put item \""..tostring(r.r_itemstack).."\" from NPC inv into block at "..
minetest.pos_to_string(r.r_pos)..
" in inventory list \""..tostring(r.r_inv_list_name).."\"."
elseif(r.r_type == "take_from_block_inv") then
if(not(r.r_pos) or type(r.r_pos) ~= "table"
or not(r.r_pos.x) or not(r.r_pos.y) or not(r.r_pos.z)) then
return "ERROR: r.r_pos is "..minetest.serialize(r.r_pos)
end
return "Take item \""..tostring(r.r_itemstack).."\" from block at "..
minetest.pos_to_string(r.r_pos)..
" out of inventory list \""..tostring(r.r_inv_list_name)..
"\" and put it into the NPC's inventory."
end
-- fallback
return tostring(r.r_value)
@ -328,6 +345,10 @@ yl_speak_up.execute_effect = function(player, n_id, o_id, r)
elseif(r.r_type == "auto" or r.r_type == "trade") then
-- these effects don't do anything
return true
elseif(r.r_type == "put_into_block_inv"
or r.r_type == "take_from_block_inv") then
-- TODO: implement
return false
elseif(r.r_type == "dialog"
or r.r_type == "on_failure") then
-- this needs to be handled in the calling function

View File

@ -327,8 +327,20 @@ yl_speak_up.save_element_p_or_a_or_e = function(
-- "the inventory of the NPC", -- 6
-- "the inventory of a block somewhere", -- 7
-- (only for preconditions; not for effects)
elseif((what_type == "player_inv" or what_type == "npc_inv" or what_type == "block_inv") and id_prefix == "p_") then
v.p_value = values_inv[ data.inv ]
elseif((id_prefix == "p_"
and (what_type == "player_inv" or what_type == "npc_inv" or what_type == "block_inv"))
or(id_prefix == "r_"
and (what_type == "put_into_block_inv" or what_type == "take_from_block_inv"))) then
-- changing the inventory of a block? we need to set p_value to something
if(id_prefix == "r_") then
-- just to be sure something is stored there...
v.r_value = data.inv_stack_name
-- for easier access in the formspec
v.r_itemstack = data.inv_stack_name
-- store in p_value what we want to check regarding the inv (contains/contains not/empty/..)
else
v.p_value = values_inv[ data.inv ]
end
if(v.p_value and v.p_value ~= "inv_is_empty") then
if(not(data.inv_stack_name) or data.inv_stack_name == "") then
yl_speak_up.show_fs(player, "msg", {
@ -340,7 +352,7 @@ yl_speak_up.save_element_p_or_a_or_e = function(
return
end
-- we have checked this value earlier on
v[ "p_itemstack" ] = data.inv_stack_name
v[ id_prefix.."itemstack" ] = data.inv_stack_name
end
if(not(data.inv_list_name) or data.inv_list_name == "") then
@ -353,10 +365,12 @@ yl_speak_up.save_element_p_or_a_or_e = function(
return
end
-- the name of the inventory list we want to access
v[ "p_inv_list_name" ] = data.inv_list_name
v[ id_prefix.."inv_list_name" ] = data.inv_list_name
-- the inventory of a block
if(what_type == "block_inv") then
if(what_type == "block_inv"
or what_type == "put_into_block_inv"
or what_type == "take_from_block_inv") then
if(not(data.block_pos) or not(data.node_data) or not(data.node_data.name)) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
@ -796,10 +810,13 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
if(((fields.inv_stack_name and fields.inv_stack_name ~= "")
or (fields.store_item_name and fields.store_item_name ~= ""))
and data and data.what
and ((id_prefix == "p_" and (what_type == "player_inv" or what_type == "npc_inv" or what_type == "block_inv"))
and ((id_prefix == "p_"
and (what_type == "player_inv" or what_type == "npc_inv" or what_type == "block_inv"))
-- "give item (created out of thin air) to player (requires npc_master priv)", -- 9
-- "take item from player and destroy it (requires npc_master priv)", -- 10
or (id_prefix == "r_" and (what_type == "give_item" or what_type == "take_item")))) then
or (id_prefix == "r_"
and (what_type == "give_item" or what_type == "take_item"
or what_type == "put_into_block_inv" or what_type == "take_from_block_inv")))) then
local wanted = ""
local wanted_name = ""
if(not(fields.store_item_name)) then
@ -1336,9 +1353,13 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
-- "the inventory of the player", -- 5
-- "the inventory of the NPC", -- 6
-- "the inventory of a block somewhere", -- 7
-- "put item from the NPC's inventory into a chest etc.", -- 4 (effect)
-- "take item from a chest etc. and put it into the NPC's inventory", -- 5 (effect)
-- (inventory - only for preconditions; effects have something else here)
elseif(data.what and id_prefix == "p_"
and (what_type == "player_inv" or what_type == "npc_inv" or what_type == "block_inv")) then
elseif((data.what and id_prefix == "p_"
and (what_type == "player_inv" or what_type == "npc_inv" or what_type == "block_inv"))
or (data.what and id_prefix == "r_"
and (what_type == "put_into_block_inv" or what_type == "take_from_block_inv"))) then
-- the inventory of a block needs more input options (in particular block selection)
data.what_type = what_type
return yl_speak_up.get_fs_edit_option_precondition_inv(
@ -1492,12 +1513,20 @@ end
-- helper function for:
-- yl_speak_up.get_fs_edit_option_p_and_e_block
yl_speak_up.get_block_pos_info = function(pname, data, id_prefix, e, values_block, ignore_protection)
-- are we more intrested in the inventory of the block or in the block itself?
local looking_at_inventory = false
if(data and data.what_type
and (data.what_type == "block_inv"
or data.what_type == "put_into_block_inv"
or data.what_type == "take_from_block_inv")) then
looking_at_inventory = true
end
-- did the player get here through punching a block in the meantime?
local block_pos = yl_speak_up.speak_to[pname].block_punched
yl_speak_up.speak_to[pname].block_punched = nil
if(e) then
-- if we are not looking for the inventory of a block (data.what == 7):
if(data.what ~= 7) then
if(e and e[id_prefix.."pos"]) then
-- if we are not looking for the inventory of a block:
if(looking_at_inventory) then
data.block = math.max(1,table.indexof(values_block, e[ id_prefix.."value" ]))
end
data.node_data = {}
@ -1555,7 +1584,7 @@ yl_speak_up.get_block_pos_info = function(pname, data, id_prefix, e, values_bloc
show_save_button = false
end
-- if we are dealing with the *inventory* of a block, the state of the block is of no intrest here
if(data.what ~= 7 and (not(data.block) or data.block == 1)) then
if(not(looking_at_inventory) and (not(data.block) or data.block == 1)) then
data.block = 1
-- not enough selected yet for saving
show_save_button = false
@ -1624,21 +1653,26 @@ end
-- "the inventory of the player", -- 5
-- "the inventory of the NPC", -- 6
-- "the inventory of a block somewhere", -- 7
-- "put item from the NPC's inventory into a chest etc.", -- 4 (effect)
-- "take item from a chest etc. and put it into the NPC's inventory", -- 5 (effect)
-- (inventory - only for preconditions; effects have something else here)
yl_speak_up.get_fs_edit_option_precondition_inv = function(
pname, dialog, formspec, data, id_prefix, save_button, e,
values_inv, check_inv, values_block)
if(e) then
data.inv = math.max(1,table.indexof(values_inv, e["p_value"]))
data.inv_stack_name = e[ "p_itemstack" ]
data.inv_stack_name = e[ id_prefix.."itemstack" ]
end
if(not(data.inv) or data.inv == 1) then
if(id_prefix == "p_" and (not(data.inv) or data.inv == 1)) then
data.inv = 1
-- not enough selected yet for saving
save_button = ""
end
local block_selection = ""
if(data and data.what_type and data.what_type == "block_inv") then
if(data and data.what_type
and (data.what_type == "block_inv"
or data.what_type == "put_into_block_inv"
or data.what_type == "take_from_block_inv")) then
local inv_list_name = ""
if(e) then
-- not really relevant here but needed for getting the position
@ -1669,11 +1703,23 @@ yl_speak_up.get_fs_edit_option_precondition_inv = function(
"the block at the position you want to check or change.\n"..
"After punching it, you will be returned to this menu.]"
end
local intro = ""
-- for preconditions: contain/does not contain item, is empty, ..
if(id_prefix == "p_") then
intro = "label[0.2,3.0;The following shall be true about the inventory:]"..
"dropdown[4.0,3.2;16.0,0.6;select_inv;"..
table.concat(check_inv, ",")..";"..
tostring(data.inv)..";]"
-- for results/effects:
elseif(data.what_type == "put_into_block_inv") then
intro = "label[0.2,3.0;The NPC shall put the following item from his inventory "..
"into the given block's inventory:]"
elseif(data.what_type == "take_from_block_inv") then
intro = "label[0.2,3.0;The NPC shall take the following item from the given block's "..
"inventory and put it into his own inventory:]"
end
return formspec..
"label[0.2,3.0;The following shall be true about the inventory:]"..
"dropdown[4.0,3.2;16.0,0.6;select_inv;"..
table.concat(check_inv, ",")..";"..
tostring(data.inv)..";]"..
intro..
"label[0.2,4.2;Name of the item(stack):]"..
"field[4.0,4.0;16.0,0.6;inv_stack_name;;"..(data.inv_stack_name or "").."]"..
"tooltip[inv_stack_name;Enter name of the block and amount.\n"..