From 1cb6eb5dd02ab568059131a95dc162b830fa7a95 Mon Sep 17 00:00:00 2001 From: zorman2000 Date: Fri, 13 Jan 2017 07:12:13 -0500 Subject: [PATCH] Actions: Add action to freeze/unfreeze mobs redo API. --- actions/actions.lua | 27 +++++++++++++++++++++++++++ npc.lua | 29 ++++++++++++++++------------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/actions/actions.lua b/actions/actions.lua index 110d079..8a35bb5 100644 --- a/actions/actions.lua +++ b/actions/actions.lua @@ -35,6 +35,33 @@ npc.actions.const = { } } +--------------------------------------------------------------------------------------- +-- Actions +--------------------------------------------------------------------------------------- +-- The following action alters the timer interval for executing actions, therefore +-- making waits and pauses possible, or increase timing when some actions want to +-- be performed faster, like walking. +function npc.actions.set_interval(args) + local self = args.self + local new_interval = args.interval + local freeze_mobs_api = args.freeze + + self.actions.action_interval = new_interval + return not freeze_mobs_api +end + +-- The following action is for allowing the rest of mobs redo API to be executed +-- after this action ends. This is useful for times when no action is needed +-- and the NPC is allowed to roam freely. +function npc.actions.freeze(args) + local freeze_mobs_api = args.freeze + minetest.log("Received: "..dump(freeze_mobs_api)) + minetest.log("Returning: "..dump(not(freeze_mobs_api))) + return not(freeze_mobs_api) +end + +-- This action is to rotate to mob to a specifc direction. Currently, the code +-- contains also for diagonals, but remaining in the orthogonal domain is preferrable. function npc.actions.rotate(args) local self = args.self local dir = args.dir diff --git a/npc.lua b/npc.lua index 4018c7c..85f28e1 100755 --- a/npc.lua +++ b/npc.lua @@ -186,7 +186,7 @@ end -- This function adds a function to the action queue. -- Actions should be added in strict order for tasks to work as expected. function npc.add_action(self, action, arguments) - self.freeze = true + --self.freeze = true --minetest.log("Current Pos: "..dump(self.object:getpos())) local action_entry = {action=action, args=arguments} --minetest.log(dump(action_entry)) @@ -197,13 +197,14 @@ end -- and then exexcutes it function npc.execute_action(self) if table.getn(self.actions.queue) == 0 then - return false + -- Keep state the same if there are no more actions in actions queue + return self.freeze end minetest.log("Executing action") local action_obj = self.actions.queue[1] - action_obj.action(action_obj.args) + local result = action_obj.action(action_obj.args) table.remove(self.actions.queue, 1) - return true + return result end @@ -338,19 +339,20 @@ local function npc_spawn(self, pos) -- Current value of the action timer action_timer = 0, -- Determines the interval for each action in the action queue + -- Default is 1. This can be changed via actions action_interval = 1 } -- This flag is checked on every step. If it is true, the rest of -- Mobs Redo API is not executed - ent.freeze = false + ent.freeze = nil -- This map will hold all the places for the NPC -- Map entries should be like: "bed" = {x=1, y=1, z=1} ent.places_map = {} -- Temporary initialization of actions for testing - local nodes = npc.places.find_sittable_nodes_nearby(ent.object:getpos(), 5) + local nodes = npc.places.find_sittable_nodes_nearby(ent.object:getpos(), 20) minetest.log("Found nodes: "..dump(nodes)) --local path = pathfinder.find_path(ent.object:getpos(), nodes[1], 20) @@ -366,6 +368,7 @@ local function npc_spawn(self, pos) -- npc.add_action(ent, npc.actions.lay, {self = ent}) -- npc.add_action(ent, npc.actions.lay, {self = ent}) npc.actions.use_sittable(ent, nodes[1], npc.actions.const.sittable.GET_UP) + npc.add_action(ent, npc.actions.freeze, {freeze = false}) end -- npc.add_action(ent, npc.action.stand, {self = ent}) @@ -427,7 +430,7 @@ mobs:register_mob("advanced_npc:npc", { -- Added walk chance walk_chance = 30, -- Added stepheight - stepheight = 1, + stepheight = 0.6, walk_velocity = 2, run_velocity = 3, jump = true, @@ -541,15 +544,15 @@ mobs:register_mob("advanced_npc:npc", { -- Reset action timer self.actions.action_timer = 0 -- Execute action - npc.execute_action(self) - -- Check if there are more actions to execute - if table.getn(self.actions.queue) == 0 then - -- Unfreeze NPC so the rest of Mobs API work - --self.freeze = false + self.freeze = npc.execute_action(self) + + if self.freeze == nil and table.getn(self.actions.queue) > 0 then + self.freeze = false end + end - return not self.freeze + return self.freeze end })