human readable texts for the preconditions

This commit is contained in:
Sokomine 2021-06-07 04:00:06 +02:00
parent 2bd1863e79
commit 8157512e6b
2 changed files with 63 additions and 9 deletions

View File

@ -152,7 +152,8 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id)
list_of_preconditions = list_of_preconditions..
minetest.formspec_escape(v.p_id)..",#FFFF00,"..
minetest.formspec_escape(v.p_type)..","..
minetest.formspec_escape(v.p_value)..","
minetest.formspec_escape(
yl_speak_up.show_precondition(v))..","
end
end
list_of_preconditions = list_of_preconditions..",#00FF00,add,Add a new pre(C)ondition"

View File

@ -13,7 +13,7 @@ local check_what = {
}
-- how to store these as p_type in the precondition:
local values_what = {nil, "state", "block", "trade", "player_inv", "npc_inv"}
local values_what = {"", "state", "block", "trade", "player_inv", "npc_inv"}
-- options for "a trade"
local check_trade = {
@ -25,7 +25,7 @@ local check_trade = {
}
-- how to store these as p_value:
local values_trade = {nil, "npc_can_sell", "player_can_buy", "npc_is_out_of_stock", "player_has_not_enough"}
local values_trade = {"", "npc_can_sell", "player_can_buy", "npc_is_out_of_stock", "player_has_not_enough"}
-- options for "the inventory of " (either player or NPC; perhaps blocks later on)
local check_inv = {
@ -37,7 +37,7 @@ local check_inv = {
}
-- how to store these as p_value (the actual itemstack gets stored as p_itemstack):
local values_inv = {nil, "inv_contains", "inv_does_not_contain", "has_room_for", "inv_is_empty"}
local values_inv = {"", "inv_contains", "inv_does_not_contain", "has_room_for", "inv_is_empty"}
local check_block = {
"- please select -",
@ -50,7 +50,7 @@ local check_block = {
-- how to store these as p_value (the actual node data gets stored as p_node, p_param2 and p_pos):
-- Note: "node_is_like" occours twice because it is used to cover blocks that
-- cannot be punched as well as normal blocks.
local values_block = {nil, "node_is_like", "node_is_air", "node_is_diffrent_from", "node_is_like"}
local values_block = {"", "node_is_like", "node_is_air", "node_is_diffrent_from", "node_is_like"}
-- comparison operators for variables
local check_operator = {
@ -67,7 +67,7 @@ local check_operator = {
}
-- how to store these as p_value (the actual variable is stored in p_variable, and the value in p_cmp_value):
local values_operator = {nil, "==", "~=", ">=", ">", "<=", "<", "not", "is_set", "is_unset"}
local values_operator = {"", "==", "~=", ">=", ">", "<=", "<", "not", "is_set", "is_unset"}
-- some internal ones...
local check_variable = {
@ -94,6 +94,58 @@ local check_variable = {
-- -> better write a function for each one that can be called when needed
-- returns a human-readable text as description of the precondition
-- (as shown in the edit options dialog and in the edit precondition formspec)
yl_speak_up.show_precondition = function(p)
if(p.p_type == "state") then
if(p.p_operator == "not") then
return "not( "..tostring(p.p_variable).." )"
elseif(p.p_operator == "is_set") then
return tostring(p.p_variable).." ~= nil (is_set)"
elseif(p.p_operator == "is_unset") then
return tostring(p.p_variable).." == nil (is_unset)"
end
return tostring(p.p_variable).." "..tostring(p.p_operator).." "..
tostring(p.p_var_cmp_value)
elseif(p.p_type == "block") then
if(not(p.p_pos) or type(p.p_pos) ~= "table"
or not(p.p_pos.x) or not(p.p_pos.y) or not(p.p_pos.z)) then
return "ERROR: p.p_pos is "..minetest.serialize(p.p_pos)
elseif(p.p_value == "node_is_like") then
return "The block at "..minetest.pos_to_string(p.p_pos).." is \""..
tostring(p.p_node).."\" with param2: "..tostring(p.p_param2).."."
elseif(p.p_value == "node_is_air") then
return "There is no block at "..minetest.pos_to_string(p.p_pos).."."
elseif(p.p_value == "node_is_diffrent_from") then
return "There is another block than \""..tostring(p.p_node).."\" at "..
minetest.pos_to_string(p.p_pos)..", or it is at least "..
"rotated diffrently (param2 is not "..tostring(p.p_param2)..")."
end
elseif(p.p_type == "trade") then
local nr = table.indexof(values_trade, p.p_value)
if(nr and check_trade[ nr ]) then
return check_trade[ nr ]
end
elseif(p.p_type == "player_inv" or p.p_type == "npc_inv") then
local who = "The player"
if(p.p_type == "npc_inv") then
who = "The NPC"
end
if(p.p_value == "inv_contains") then
return who.." has \""..tostring(p.p_itemstack).."\" in his inventory."
elseif(p.p_value == "inv_does_not_contain") then
return who.." does not have \""..tostring(p.p_itemstack).."\" in his inventory."
elseif(p.p_value == "has_room_for") then
return who.." has room for \""..tostring(p.p_itemstack).."\" in his inventory."
elseif(p.p_value == "inv_is_empty") then
return who.." has an empty inventory."
end
end
-- fallback
return tostring(p.p_value)
end
yl_speak_up.input_fs_edit_preconditions = function(player, formname, fields)
if(not(player)) then
return
@ -208,10 +260,10 @@ yl_speak_up.input_fs_edit_preconditions = function(player, formname, fields)
pq.p_param2 = data.node_data.param2
end
-- we also need to store the position of the node
pq.p_pos = data.block_pos
pq.p_pos = {x = data.block_pos.x, y = data.block_pos.y, z = data.block_pos.z }
-- "I can't punch it. The block is as the block *above* the one I punched.",
if(data.block == 5) then
pq.p_pos = pq.p_pos.y + 1
pq.p_pos.y = pq.p_pos.y + 1
end
-- "a trade", -- 4
@ -358,7 +410,8 @@ yl_speak_up.get_fs_edit_preconditions = function(player, table_click_result)
"table[0.2,0.8;19.6,0.7;table_of_preconditions;"..
minetest.formspec_escape(prereq[ p_id ].p_id)..",#FFFF00,"..
minetest.formspec_escape(prereq[ p_id ].p_type)..","..
minetest.formspec_escape(prereq[ p_id ].p_value)..";0]"..
minetest.formspec_escape(
yl_speak_up.show_precondition(prereq[ p_id ]))..";0]"..
"button[2.0,1.8;1.5,0.9;delete_prereq;Delete]"..
"button[4.0,1.8;1.5,0.9;change_prereq;Change]"..
"button[6.0,1.8;1,0.9;back;Back]"