added entity_type precondition to readme.md

This commit is contained in:
Sokomine 2024-02-17 19:52:39 +01:00
parent 2329724ef2
commit 3eab65d2ee
5 changed files with 66 additions and 1 deletions

View File

@ -628,6 +628,13 @@ yl_speak_up.save_element_p_or_a_or_e = function(
v[ "a_on_failure" ] = sorted_dialog_list[ data.action_failure_dialog ]
end
-- "the type of the entity of the NPC",
-- precondition: (last entry)
elseif(what_type == "entity_type" and id_prefix == "p_") then
-- Note: We might check for minetest.registered_entities[data.entity_type] - but
-- that doesn't really help much - so we skip that.
v[ "p_value" ] = data.entity_type
-- "The preconditions of another dialog option are fulfilled/not fulfilled.", -- 10
-- precondition: 10
elseif(what_type == "other" and id_prefix == "p_") then
@ -906,6 +913,10 @@ yl_speak_up.handle_input_fs_edit_option_related = function(player, formname, fie
if(fields.lua_code) then
data.lua_code = fields.lua_code
end
-- select the type of the entity for "the type of the entity of the NPC"
if(fields.entity_type) then
data.entity_type = fields.entity_type
end
-- if the type of operator is changed: store any new values that may need storing
if(what_type == "evaluate"
and (fields.set_param0 or fields.set_param1 or fields.set_param2 or fields.set_param3
@ -1139,6 +1150,7 @@ yl_speak_up.handle_input_fs_edit_option_related = function(player, formname, fie
or fields.select_other_o_id
or fields.select_fulfilled
or fields.select_function_name
or fields.entity_type
or was_changed
or fields.key_enter
or fields.quit
@ -1380,6 +1392,13 @@ yl_speak_up.build_fs_edit_option_related = function(player, table_click_result,
pname, dialog, formspec, data, id_prefix, save_button, e,
values_trade, check_trade)
-- "the type of the entity of the NPC",
-- (entity_type - only for preconditions)
elseif(data.what and id_prefix == "p_" and what_type == "entity_type") then
return yl_speak_up.get_sub_fs_edit_option_precondition_entity_type(
pname, dialog, formspec, data, id_prefix, save_button, e,
values_trade, check_trade)
-- "the inventory of the player", -- 5
-- "the inventory of the NPC", -- 6
-- "the inventory of a block somewhere", -- 7
@ -1852,6 +1871,34 @@ yl_speak_up.get_sub_fs_edit_option_precondition_trade = function(
end
-- "the type of the entity of the NPC",
-- (entity_type - only for preconditions)
yl_speak_up.get_sub_fs_edit_option_precondition_entity_type = function(
pname, dialog, formspec, data, id_prefix, save_button, e,
values_trade, check_trade)
if(e) then
data.entity_type = e[ "p_value" ]
end
if(not(data.entity_type) or data.entity_type == "") then
-- get the name/type of the current entity
if(yl_speak_up.speak_to[pname].obj) then
local obj = yl_speak_up.speak_to[pname].obj
if(obj) then
local entity = obj:get_luaentity()
if(entity) then
data.entity_type = entity.name
end
end
end
end
return formspec..
"label[0.2,3.3;The entity (this NPC) is of type:]"..
"field[5.0,3.0;6.0,0.6;entity_type;;"..(data.entity_type or "").."]"..
"label[0.2,4.3;Note: This is only really useful for generic NPCs.]"..
save_button
end
-- "the inventory of the player", -- 5
-- "the inventory of the NPC", -- 6
-- "the inventory of a block somewhere", -- 7

View File

@ -63,6 +63,9 @@
-- depends on another option:
-- p_value name of the other option of this dialog that is considered
-- p_fulfilled shall option p_value be true or false?
--
-- the type of the entity of the NPC:
-- p_value name of the entity (i.e. npc_talk:talking_npc)
-- some helper lists for creating the formspecs and evaulating
@ -84,6 +87,7 @@ local check_what = {
"The preconditions of another dialog option are fulfilled/not fulfilled.", -- 9 -> 11
"nothing - always true (useful for generic dialogs)",
"nothing - always false (useful for temporally deactivating an option)",
"the type of the entity of the NPC",
}
-- how to store these as p_type in the precondition:
@ -94,7 +98,8 @@ local values_what = {"", "state", "property", "evaluate", "block", "trade",
"function",
-- depends on the preconditions of another option
"other",
"true", "false"}
"true", "false",
"entity_type"}
-- options for "a trade"
local check_trade = {
@ -321,6 +326,8 @@ yl_speak_up.show_precondition = function(p, pname)
end
return "The preconditions for dialog option \""..tostring(p.p_value).."\" are "..
fulfilled.."."
elseif(p.p_type == "entity_tpye") then
return "the type of the entity of the NPC is: \""..tostring(p.p_value).."\"."
end
-- fallback
return tostring(p.p_value)

View File

@ -413,6 +413,13 @@ yl_speak_up.eval_precondition = function(player, n_id, p, other_options_true_or_
and other_options_true_or_false
and other_options_true_or_false[ p.p_value ] ~= nil
and tostring(other_options_true_or_false[ p.p_value ]) == tostring(p.p_fulfilled))
elseif(p.p_type == "entity_type") then
local pname = player:get_player_name()
-- is the NPC type the same as the requested entity_type?
return (p.p_value
and yl_speak_up.speak_to[pname]._self
and yl_speak_up.speak_to[pname]._self.name
and yl_speak_up.speak_to[pname]._self.name == p.p_value)
end
-- fallback - unknown type
return false

View File

@ -625,6 +625,8 @@ function yl_speak_up.talk(self, clicker)
yl_speak_up.speak_to[pname].option_index = 1
-- the object itself may be needed in load_dialog for adding generic dialogs
yl_speak_up.speak_to[pname].obj = self.object
-- this makes it a bit easier to access some values later on:
yl_speak_up.speak_to[pname]._self = self
-- Load the dialog and see what we can do with it
-- this inculdes generic dialog parts;
yl_speak_up.speak_to[pname].dialog = yl_speak_up.load_dialog(n_id, clicker)

View File

@ -555,6 +555,8 @@ That's where _generic dialogs_ come in. You can create a new type of generic dia
[Properties](#properties) are very helpful for determining if a particular NPC ought
to offer a generic dialog.
The _`entity_type`_ precondition can also be helpful in this case. It allows to add dialogs for specific types of mobs only (i.e. `npc_talk:talking_npc`).
Requirements for a generic dialog to work:
* _Generic dialogs_ have to [start with a dialog](#start_dialog) with _just one option_.
* This _option_ has to be set to _"automaticly"_ (see [Autoanswer](#autoselect_autoanswer)).