Actions: Add action to freeze/unfreeze mobs redo API.

This commit is contained in:
zorman2000 2017-01-13 07:12:13 -05:00
parent e3fc7be783
commit 1cb6eb5dd0
2 changed files with 43 additions and 13 deletions

View File

@ -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) function npc.actions.rotate(args)
local self = args.self local self = args.self
local dir = args.dir local dir = args.dir

29
npc.lua
View File

@ -186,7 +186,7 @@ end
-- This function adds a function to the action queue. -- This function adds a function to the action queue.
-- Actions should be added in strict order for tasks to work as expected. -- Actions should be added in strict order for tasks to work as expected.
function npc.add_action(self, action, arguments) function npc.add_action(self, action, arguments)
self.freeze = true --self.freeze = true
--minetest.log("Current Pos: "..dump(self.object:getpos())) --minetest.log("Current Pos: "..dump(self.object:getpos()))
local action_entry = {action=action, args=arguments} local action_entry = {action=action, args=arguments}
--minetest.log(dump(action_entry)) --minetest.log(dump(action_entry))
@ -197,13 +197,14 @@ end
-- and then exexcutes it -- and then exexcutes it
function npc.execute_action(self) function npc.execute_action(self)
if table.getn(self.actions.queue) == 0 then 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 end
minetest.log("Executing action") minetest.log("Executing action")
local action_obj = self.actions.queue[1] 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) table.remove(self.actions.queue, 1)
return true return result
end end
@ -338,19 +339,20 @@ local function npc_spawn(self, pos)
-- Current value of the action timer -- Current value of the action timer
action_timer = 0, action_timer = 0,
-- Determines the interval for each action in the action queue -- Determines the interval for each action in the action queue
-- Default is 1. This can be changed via actions
action_interval = 1 action_interval = 1
} }
-- This flag is checked on every step. If it is true, the rest of -- This flag is checked on every step. If it is true, the rest of
-- Mobs Redo API is not executed -- Mobs Redo API is not executed
ent.freeze = false ent.freeze = nil
-- This map will hold all the places for the NPC -- This map will hold all the places for the NPC
-- Map entries should be like: "bed" = {x=1, y=1, z=1} -- Map entries should be like: "bed" = {x=1, y=1, z=1}
ent.places_map = {} ent.places_map = {}
-- Temporary initialization of actions for testing -- 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)) minetest.log("Found nodes: "..dump(nodes))
--local path = pathfinder.find_path(ent.object:getpos(), nodes[1], 20) --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.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.actions.use_sittable(ent, nodes[1], npc.actions.const.sittable.GET_UP)
npc.add_action(ent, npc.actions.freeze, {freeze = false})
end end
-- npc.add_action(ent, npc.action.stand, {self = ent}) -- npc.add_action(ent, npc.action.stand, {self = ent})
@ -427,7 +430,7 @@ mobs:register_mob("advanced_npc:npc", {
-- Added walk chance -- Added walk chance
walk_chance = 30, walk_chance = 30,
-- Added stepheight -- Added stepheight
stepheight = 1, stepheight = 0.6,
walk_velocity = 2, walk_velocity = 2,
run_velocity = 3, run_velocity = 3,
jump = true, jump = true,
@ -541,15 +544,15 @@ mobs:register_mob("advanced_npc:npc", {
-- Reset action timer -- Reset action timer
self.actions.action_timer = 0 self.actions.action_timer = 0
-- Execute action -- Execute action
npc.execute_action(self) self.freeze = npc.execute_action(self)
-- Check if there are more actions to execute
if table.getn(self.actions.queue) == 0 then if self.freeze == nil and table.getn(self.actions.queue) > 0 then
-- Unfreeze NPC so the rest of Mobs API work self.freeze = false
--self.freeze = false
end end
end end
return not self.freeze return self.freeze
end end
}) })