From fc10be7d0e4132c7c681bac0f4fc90a651da4ea5 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Fri, 7 Jan 2022 21:22:41 +0100 Subject: [PATCH] extracted interface_mobs_api.lua from functions.lua into a seperate file --- fs_talkdialog.lua | 55 ------------------------ functions.lua | 94 ----------------------------------------- init.lua | 2 + interface_mobs_api.lua | 95 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 149 deletions(-) create mode 100644 interface_mobs_api.lua diff --git a/fs_talkdialog.lua b/fs_talkdialog.lua index 11e1acf..9ee0cbe 100644 --- a/fs_talkdialog.lua +++ b/fs_talkdialog.lua @@ -271,61 +271,6 @@ yl_speak_up.input_talk = function(player, formname, fields) end -function yl_speak_up.on_rightclick(self, clicker) - --local item = clicker:get_wielded_item() - local name = clicker:get_player_name() - - -- Take the mob only with net or lasso - if self.owner and self.owner == name then - if mobs:capture_mob(self, clicker, nil, 100, 100, true, nil) then - return - end - end - - -- protect npc with mobs:protector - if mobs:protect(self, clicker) then - return - end - - -- bring up the dialog options - if clicker then - yl_speak_up.talk(self, clicker) - return - end -end - -function yl_speak_up.on_spawn(self) - --Let's assign an ID - - local m_talk = yl_speak_up.talk_after_spawn or true - local m_id = yl_speak_up.number_of_npcs + 1 - - yl_speak_up.number_of_npcs = m_id - yl_speak_up.modstorage:set_int("amount", m_id) - - yl_speak_up.mob_table[m_id] = "yl_speak_up:test_npc" - - self.yl_speak_up = { - talk = m_talk, - id = m_id, - textures = self.textures - } - - --Let's protect it - self.protected = true - self.tamed = true - self.object:set_armor_groups({immortal = 100}) - - minetest.log( - "action", - "[MOD] yl_speak_up: NPC with ID n_" .. - self.yl_speak_up.id .. " spawned at " .. minetest.pos_to_string(self.object:get_pos(), 0) - ) - - --Let's do it only once - return true -end - -- recursion_depth is increased each time autoanswer is automaticly selected yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, recursion_depth) local pname = player:get_player_name() diff --git a/functions.lua b/functions.lua index 43cd0bb..ee6ffdc 100644 --- a/functions.lua +++ b/functions.lua @@ -381,100 +381,6 @@ end -- Make the NPC talk ---### --- Mob functions ---### - -function yl_speak_up.on_rightclick(self, clicker) - --local item = clicker:get_wielded_item() - local name = clicker:get_player_name() - - -- Take the mob only with net or lasso - if self.owner and self.owner == name then - if mobs:capture_mob(self, clicker, nil, 100, 100, true, nil) then - return - end - end - - -- protect npc with mobs:protector - if mobs:protect(self, clicker) then - return - end - - -- bring up the dialog options - if clicker then - yl_speak_up.talk(self, clicker) - return - end -end - -function yl_speak_up.on_spawn(self) - --Let's assign an ID - - local m_talk = yl_speak_up.talk_after_spawn or true - local m_id = yl_speak_up.number_of_npcs + 1 - - yl_speak_up.number_of_npcs = m_id - yl_speak_up.modstorage:set_int("amount", m_id) - - yl_speak_up.mob_table[m_id] = "yl_speak_up:test_npc" - - self.yl_speak_up = { - talk = m_talk, - id = m_id, - textures = self.textures - } - - --Let's protect it - self.protected = true - self.tamed = true - self.object:set_armor_groups({immortal = 100}) - - minetest.log( - "action", - "[MOD] yl_speak_up: NPC with ID n_" .. - self.yl_speak_up.id .. " spawned at " .. minetest.pos_to_string(self.object:get_pos(), 0) - ) - - --Let's do it only once - return true -end - -function yl_speak_up.after_activate(self, staticdata, def, dtime) - minetest.log( - "action", - "[MOD] yl_speak_up: NPC with ID n_" .. - self.yl_speak_up.id .. " activated at " .. minetest.pos_to_string(self.object:get_pos(), 0) - ) - - if yl_speak_up.status == 2 then - self.object:remove() - return true - end - - if self.yl_speak_up and self.yl_speak_up.skin then - local tex = self.yl_speak_up.skin - self.object:set_properties({textures = {tex[1], tex[2], tex[3], tex[4]}}) - end - - if yl_speak_up.infotext then - local i_text = "" - if self.yl_speak_up.npc_name then - i_text = i_text .. self.yl_speak_up.npc_name .. "\n" - end - if self.yl_speak_up.npc_description then - i_text = i_text .. self.yl_speak_up.npc_description .. "\n" - end - i_text = i_text .. yl_speak_up.infotext - self.object:set_properties({infotext = i_text}) - end - - yl_speak_up.update_nametag(self) - - -- create a detached inventory for the npc and load its inventory - yl_speak_up.load_npc_inventory("n_"..tostring(self.yl_speak_up.id)) -end - function yl_speak_up.talk(self, clicker) if not clicker and not clicker:is_player() then diff --git a/init.lua b/init.lua index ebbc497..0c71d5e 100644 --- a/init.lua +++ b/init.lua @@ -17,6 +17,8 @@ yl_speak_up.speak_to = {} dofile(modpath .. "config.lua") dofile(modpath .. "privs.lua") +-- react to right-click etc. +dofile(modpath .. "interface_mobs_api.lua") -- handle on_player_receive_fields and showing of formspecs dofile(modpath .. "show_fs.lua") -- the formspec and input handling for the main dialog diff --git a/interface_mobs_api.lua b/interface_mobs_api.lua new file mode 100644 index 0000000..1c5cd33 --- /dev/null +++ b/interface_mobs_api.lua @@ -0,0 +1,95 @@ +-- Make the NPC talk (mobs_redo interface) + +--### +-- Mob functions +--### + +function yl_speak_up.on_rightclick(self, clicker) + --local item = clicker:get_wielded_item() + local name = clicker:get_player_name() + + -- Take the mob only with net or lasso + if self.owner and self.owner == name then + if mobs:capture_mob(self, clicker, nil, 100, 100, true, nil) then + return + end + end + + -- protect npc with mobs:protector + if mobs:protect(self, clicker) then + return + end + + -- bring up the dialog options + if clicker then + yl_speak_up.talk(self, clicker) + return + end +end + +function yl_speak_up.on_spawn(self) + --Let's assign an ID + + local m_talk = yl_speak_up.talk_after_spawn or true + local m_id = yl_speak_up.number_of_npcs + 1 + + yl_speak_up.number_of_npcs = m_id + yl_speak_up.modstorage:set_int("amount", m_id) + + yl_speak_up.mob_table[m_id] = "yl_speak_up:test_npc" + + self.yl_speak_up = { + talk = m_talk, + id = m_id, + textures = self.textures + } + + --Let's protect it + self.protected = true + self.tamed = true + self.object:set_armor_groups({immortal = 100}) + + minetest.log( + "action", + "[MOD] yl_speak_up: NPC with ID n_" .. + self.yl_speak_up.id .. " spawned at " .. minetest.pos_to_string(self.object:get_pos(), 0) + ) + + --Let's do it only once + return true +end + +function yl_speak_up.after_activate(self, staticdata, def, dtime) + minetest.log( + "action", + "[MOD] yl_speak_up: NPC with ID n_" .. + self.yl_speak_up.id .. " activated at " .. minetest.pos_to_string(self.object:get_pos(), 0) + ) + + if yl_speak_up.status == 2 then + self.object:remove() + return true + end + + if self.yl_speak_up and self.yl_speak_up.skin then + local tex = self.yl_speak_up.skin + self.object:set_properties({textures = {tex[1], tex[2], tex[3], tex[4]}}) + end + + if yl_speak_up.infotext then + local i_text = "" + if self.yl_speak_up.npc_name then + i_text = i_text .. self.yl_speak_up.npc_name .. "\n" + end + if self.yl_speak_up.npc_description then + i_text = i_text .. self.yl_speak_up.npc_description .. "\n" + end + i_text = i_text .. yl_speak_up.infotext + self.object:set_properties({infotext = i_text}) + end + + yl_speak_up.update_nametag(self) + + -- create a detached inventory for the npc and load its inventory + yl_speak_up.load_npc_inventory("n_"..tostring(self.yl_speak_up.id)) +end