added yl_speak_up.debug_msg plus chat command /npc_talk_debug

This commit is contained in:
Sokomine 2021-06-10 22:49:24 +02:00
parent 8a69b4caeb
commit 773be49a4e
2 changed files with 87 additions and 7 deletions

View File

@ -1,4 +1,78 @@
-- store which player is monitoring the NPC (for preconditions and
-- effects)
yl_speak_up.debug_mode_set_by_player = {}
-- for sending debug information about preconditions and effects to
-- the player who is monitoring the NPC
-- (sometimes it is not easy/obvious to see why something failed)
yl_speak_up.debug_msg = function(player, n_id, o_id, text)
local dname = yl_speak_up.debug_mode_set_by_player[ n_id ]
-- nobody cares
if(not(dname)) then
return
end
local pname = player:get_player_name()
local d_id = yl_speak_up.speak_to[pname].d_id
minetest.chat_send_player(dname, "[NPC "..tostring(n_id)..": "..
tostring(pname).."] <"..tostring(d_id).." "..tostring(o_id)..
"> "..tostring(text))
end
-- a chat command for entering and leaving debug mode; needs to be a chat command
-- because the player may have wandered off from his NPC and get too many messages
-- without a quick way to get rid of them otherwise
minetest.register_chatcommand( 'npc_talk_debug', {
description = "Sets you as debugger for the yl_speak_up-NPC with the ID <n_id>.\n"..
" <list> lists the NPC you are currently debugging.\n"..
" <off> turns debug mode off again.",
privs = {npc_talk_owner},
func = function(pname, param)
if(param and param == "off") then
local count = 0
for k, v in pairs(yl_speak_up.debug_mode_set_by_player) do
if(v and v == pname) then
yl_speak_up.debug_mode_set_by_player[ k ] = nil
count = count + 1
minetest.chat_send_player(pname, "You are no longer "..
"debugging the NPC with the ID "..tostring(k)..".")
end
end
minetest.chat_send_player(pname, "Removed you as debugger of "..
tostring(count).." NPCs.")
return
elseif(not(param) or param == "" or param == "list") then
local count = 0
local text = "You are currently debugging the NPCs with the following IDs:\n"
for k, v in pairs(yl_speak_up.debug_mode_set_by_player) do
if(v and v == pname) then
count = count + 1
text = text.." "..tostring(k)
end
end
if(count == 0) then
text = text.." - none -"
else
text = text.."\nTo turn debugging off, call this command with the "..
"parameter <off>."
end
minetest.chat_send_player(pname, text)
return
elseif(not(yl_speak_up.may_edit_npc(minetest.get_player_by_name(pname), n_id))) then
minetest.chat_send_player(pname, "You do not have the necessary privs to "..
"edit that NPC.")
return
else
yl_speak_up.debug_mode_set_by_player[ param ] = pname
minetest.chat_send_player(pname, "You are now receiving debug information "..
"for NPC "..tostring(param)..".\nTo turn that off, type "..
"\"/npc_talk_debug off\".")
end
end
});
-- evaluate those preconditions of type "function" (set by the staff)
-- and execute those effects/results of type "function" (set by the staff)
-- WARNING: This is extremly powerful!
@ -494,7 +568,7 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
if(not(x_id)) then
x_id = "new"
end
local save_button = "button[5.0,9.0;1,0.7;save_element;Save]"
local save_button = "button[5.0,9.3;1,0.7;save_element;Save]"
local formspec =
"formspec_version[3]"..
"size[20,10]"..
@ -504,7 +578,11 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
"dropdown[4.0,1.8;14.0,0.6;select_what;"..
table.concat(check_what, ",")..";"..
tostring(data.what)..";]"..
"button[3.0,9.0;1,0.7;back;Abort]"
"label[1,8.5;If you are unsure if your setup of pre(C)onditions and (Ef)fects "..
"works as intended,\ntype \"/npc_talk_debug "..tostring(n_id).."\" "..
"in chat in order to enter debug mode. You can leave it with "..
"\"/npc_talk_debug off\".]"..
"button[3.0,9.3;1,0.7;back;Abort]"
-- "an internal state (i.e. of a quest)", -- 2
-- (state is the second offered option in both preconditions and effects list)

View File

@ -198,23 +198,25 @@ end
-- true (or false).
yl_speak_up.eval_all_preconditions = function(player, prereq, o_id)
if(not(prereq)) then
minetest.chat_send_player(player:get_player_name(), "No preconditions given for "..tostring(o_id)..".")
yl_speak_up.debug_msg(player, n_id, o_id, "No preconditions given.")
-- no prerequirements? then they are automaticly fulfilled
return true
end
local pname = player:get_player_name()
minetest.chat_send_player(pname, "Checking preconditions for "..tostring(o_id).."..") -- TODO
local n_id = yl_speak_up.speak_to[pname].n_id
yl_speak_up.debug_msg(player, n_id, o_id, "Checking preconditions..")
for k, p in pairs(prereq) do
minetest.chat_send_player(pname, "..checking "..tostring(p.p_id)..": "..yl_speak_up.show_precondition(p)) -- TODO
yl_speak_up.debug_msg(player, n_id, o_id, "..checking "..
tostring(p.p_id)..": "..yl_speak_up.show_precondition(p))
if(not(yl_speak_up.eval_precondition(player, n_id, p))) then
yl_speak_up.debug_msg(player, n_id, o_id, tostring(p.p_id)..
" -> is false. Aborting.")
-- no need to look any further - once we hit a false, it'll stay false
minetest.chat_send_player(pname, " -> is false! Aborting.") -- TODO
return false
end
end
-- all preconditions are true
minetest.chat_send_player(pname, "All preconditions true for "..tostring(o_id)..".") -- TODO
yl_speak_up.debug_msg(player, n_id, o_id, "OK. All preconditions true.")
return true
end