NPC: After interacting with NPC, original yaw is restored.

This commit is contained in:
Hector Franqui 2017-10-19 21:47:35 -04:00
parent 205bdf6eb1
commit ec0392096f
3 changed files with 279 additions and 260 deletions

View File

@ -368,14 +368,19 @@ end
-- contains also for diagonals, but remaining in the orthogonal domain is preferrable.
function npc.actions.rotate(self, args)
local dir = args.dir
local yaw = args.yaw or 0
local start_pos = args.start_pos
local end_pos = args.end_pos
-- Calculate dir if positions are given
if start_pos and end_pos and not dir then
dir = npc.actions.get_direction(start_pos, end_pos)
end
-- Only yaw was given
if yaw and not dir and not start_pos and not end_pos then
self.object:setyaw(yaw)
return
end
local yaw = 0
self.rotate = 0
if dir == npc.direction.north then
yaw = 0
@ -990,6 +995,10 @@ function npc.actions.use_sittable(self, args)
end
self.actions.move_state.is_sitting = true
else
if self.actions.move_state.is_sitting == false then
npc.log("DEBUG_ACTION", "NPC "..self.npc_name.." attempted to get up from sit when it is not sitting.")
return
end
-- Find empty areas around chair
local dir = node.param2 + 2 % 4
-- Default it to the current position in case it can't find empty

View File

@ -79,30 +79,24 @@ local basic_def = {
depends = {1}
},
-- Stay put into place
[3] = {action = npc.actions.cmd.SET_INTERVAL, args = {
freeze = true,
interval = 35
},
[3] = {
action = npc.actions.cmd.FREEZE, args = {freeze = true},
depends = {2}
},
[4] = {action = npc.actions.cmd.SET_INTERVAL, args = {
freeze = true,
interval = npc.actions.default_interval
},
depends = {3}
},
-- Get up from sit
[5] = {action = npc.actions.cmd.USE_SITTABLE, args = {
pos = npc.places.PLACE_TYPE.CALCULATED.TARGET,
action = npc.actions.const.sittable.GET_UP
},
--depends = {4}
}
},
-- Schedule entry for 1 in the afternoon
[13] = {
-- Get up from sit
[1] = {
action = npc.actions.cmd.USE_SITTABLE, args = {
pos = npc.places.PLACE_TYPE.CALCULATED.TARGET,
action = npc.actions.const.sittable.GET_UP
},
},
-- Give NPC money to buy from player
[1] = {property = npc.schedule_properties.put_multiple_items, args = {
[2] = {
property = npc.schedule_properties.put_multiple_items,
args = {
itemlist = {
{name="default:iron_lump", random=true, min=2, max=4}
}
@ -110,18 +104,22 @@ local basic_def = {
chance = 75
},
-- Change trader status to "casual trader"
[2] = {property = npc.schedule_properties.trader_status, args = {
[3] = {
property = npc.schedule_properties.trader_status,
args = {
status = npc.trade.CASUAL
},
chance = 75
},
[3] = {property = npc.schedule_properties.can_receive_gifts, args = {
[4] = {
property = npc.schedule_properties.can_receive_gifts,
args = {
can_receive_gifts = false
},
depends = {1}
},
-- Allow mobs_redo wandering
[4] = {action = npc.actions.cmd.FREEZE, args = {freeze = false}}
[5] = {action = npc.actions.cmd.FREEZE, args = {freeze = false}}
},
-- Schedule entry for 6 in the evening
[18] = {

266
npc.lua
View File

@ -817,12 +817,6 @@ function npc.lock_actions(self)
if self.actions.action_timer_lock == true then
return
end
-- Check if NPC is in unmovable state
if self.actions.move_state
and (self.actions.move_state.is_sitting == true or self.actions.move_state.is_laying == true) then
-- Can't lock actions since NPC is in a non-movable state
return
end
local pos = self.object:getpos()
@ -837,8 +831,12 @@ function npc.lock_actions(self)
end
pos.y = self.object:getpos().y
end
-- Check if NPC is in unmovable state
if self.actions.move_state
and self.actions.move_state.is_sitting == false and self.actions.move_state.is_laying == false then
-- Stop NPC
npc.actions.execute(self, npc.actions.cmd.STAND, {pos=pos})
end
-- Avoid all timer execution
self.actions.action_timer_lock = true
-- Reset timer so that it has some time after interaction is done
@ -860,14 +858,23 @@ function npc.lock_actions(self)
end
function npc.unlock_actions(self)
if self.yaw_before_interaction ~= nil then
minetest.after(1, function(ent, yaw)
ent.object:setyaw(yaw)
end, self, self.yaw_before_interaction)
self.yaw_before_interaction = nil
end
-- Allow timers to execute
self.actions.action_timer_lock = false
-- Check if the NPC is sitting or laying states
if self.actions.move_state
and (self.actions.move_state.is_sitting == true or self.actions.move_state.is_laying == true) then
-- Can't unlock actions since NPC is in a non-movable state
return
end
-- Allow timers to execute
self.actions.action_timer_lock = false
-- Restore the value of self.freeze
self.freeze = self.actions.state_before_lock.freeze
@ -1291,7 +1298,27 @@ end
---------------------------------------------------------------------------------------
-- The following functions make up the definitions of on_rightclick(), do_custom()
-- and other functions that are assigned to the Lua entity definition
-- This function is executed each time the NPC is loaded
function npc.after_activate(self)
-- Reset animation
if self.actions and self.actions.move_state then
if self.actions.move_state.is_sitting == true then
npc.actions.execute(self, npc.actions.cmd.SIT, {pos=self.object:getpos()})
elseif self.actions.move_state.is_laying == true then
npc.actions.execute(self, npc.actions.cmd.LAY, {pos=self.object:getpos()})
end
-- Reset yaw if available
if self.yaw_before_interaction then
self.object:setyaw(self.yaw_before_interaction)
end
end
end
-- This function is executed on right-click
function npc.rightclick_interaction(self, clicker)
-- Store original yaw
self.yaw_before_interaction = self.object:getyaw()
-- Rotate NPC toward its clicker
npc.dialogue.rotate_npc_to_player(self)
@ -1330,112 +1357,7 @@ function npc.rightclick_interaction(self, clicker)
end
end
---------------------------------------------------------------------------------------
-- NPC Definition
---------------------------------------------------------------------------------------
mobs:register_mob("advanced_npc:npc", {
type = "npc",
passive = false,
damage = 3,
attack_type = "dogfight",
attacks_monsters = true,
-- Added group attack
group_attack = true,
-- Pathfinder = 2 to make NPCs more smart when attacking
pathfinding = 2,
hp_min = 10,
hp_max = 20,
armor = 100,
collisionbox = {-0.20,0,-0.20, 0.20,1.8,0.20},
--collisionbox = {-0.20,-1.0,-0.20, 0.20,0.8,0.20},
--collisionbox = {-0.35,-1.0,-0.35, 0.35,0.8,0.35},
visual = "mesh",
mesh = "character.b3d",
drawtype = "front",
textures = {
{"npc_male1.png"},
{"npc_male2.png"},
{"npc_male3.png"},
{"npc_male4.png"},
{"npc_male5.png"},
{"npc_male6.png"},
{"npc_male7.png"},
{"npc_male8.png"},
{"npc_male9.png"},
{"npc_male10.png"},
{"npc_male11.png"},
{"npc_male12.png"},
{"npc_male13.png"},
{"npc_male14.png"},
{"npc_female1.png"}, -- female by nuttmeg20
{"npc_female2.png"},
{"npc_female3.png"},
{"npc_female4.png"},
{"npc_female5.png"},
{"npc_female6.png"},
{"npc_female7.png"},
{"npc_female8.png"},
{"npc_female9.png"},
{"npc_female10.png"},
{"npc_female11.png"},
},
child_texture = {
{"npc_child_male1.png"},
{"npc_child_female1.png"},
},
makes_footstep_sound = true,
sounds = {},
-- Added walk chance
walk_chance = 20,
-- Added stepheight
stepheight = 0.6,
walk_velocity = 1,
run_velocity = 3,
jump = false,
drops = {
{name = "default:wood", chance = 1, min = 1, max = 3},
{name = "default:apple", chance = 2, min = 1, max = 2},
{name = "default:axe_stone", chance = 5, min = 1, max = 1},
},
water_damage = 0,
lava_damage = 2,
light_damage = 0,
--follow = {"farming:bread", "mobs:meat", "default:diamond"},
view_range = 15,
owner = "",
order = "follow",
--order = "stand",
fear_height = 3,
animation = {
speed_normal = 30,
speed_run = 30,
stand_start = 0,
stand_end = 79,
walk_start = 168,
walk_end = 187,
run_start = 168,
run_end = 187,
punch_start = 200,
punch_end = 219,
},
after_activate = function(self, staticdata, def, dtime)
-- Reset animation
if self.actions and self.actions.move_state then
if self.actions.move_state.is_sitting == true then
npc.actions.sit(self, {pos=self.object:getpos()})
elseif self.actions.move_state.is_laying == true then
npc.actions.lay(self, {pos=self.object:getpos()})
end
end
end,
on_rightclick = function(self, clicker)
-- Check if right-click interaction is enabled
if self.enable_rightclick_interaction == true then
npc.rightclick_interaction(self, clicker)
end
end,
do_custom = function(self, dtime)
function npc.step(self, dtime)
if self.initialized == nil then
-- Initialize NPC if spawned using the spawn egg built in from
-- mobs_redo. This functionality will be removed in the future in
@ -1635,21 +1557,111 @@ mobs:register_mob("advanced_npc:npc", {
end
return self.freeze
end
---------------------------------------------------------------------------------------
-- NPC Definition
---------------------------------------------------------------------------------------
mobs:register_mob("advanced_npc:npc", {
type = "npc",
passive = false,
damage = 3,
attack_type = "dogfight",
attacks_monsters = true,
-- Added group attack
group_attack = true,
-- Pathfinder = 2 to make NPCs more smart when attacking
pathfinding = 2,
hp_min = 10,
hp_max = 20,
armor = 100,
collisionbox = {-0.20,0,-0.20, 0.20,1.8,0.20},
--collisionbox = {-0.20,-1.0,-0.20, 0.20,0.8,0.20},
--collisionbox = {-0.35,-1.0,-0.35, 0.35,0.8,0.35},
visual = "mesh",
mesh = "character.b3d",
drawtype = "front",
textures = {
{"npc_male1.png"},
{"npc_male2.png"},
{"npc_male3.png"},
{"npc_male4.png"},
{"npc_male5.png"},
{"npc_male6.png"},
{"npc_male7.png"},
{"npc_male8.png"},
{"npc_male9.png"},
{"npc_male10.png"},
{"npc_male11.png"},
{"npc_male12.png"},
{"npc_male13.png"},
{"npc_male14.png"},
{"npc_female1.png"}, -- female by nuttmeg20
{"npc_female2.png"},
{"npc_female3.png"},
{"npc_female4.png"},
{"npc_female5.png"},
{"npc_female6.png"},
{"npc_female7.png"},
{"npc_female8.png"},
{"npc_female9.png"},
{"npc_female10.png"},
{"npc_female11.png"},
},
child_texture = {
{"npc_child_male1.png"},
{"npc_child_female1.png"},
},
makes_footstep_sound = true,
sounds = {},
-- Added walk chance
walk_chance = 20,
-- Added stepheight
stepheight = 0.6,
walk_velocity = 1,
run_velocity = 3,
jump = false,
drops = {
{name = "default:wood", chance = 1, min = 1, max = 3},
{name = "default:apple", chance = 2, min = 1, max = 2},
{name = "default:axe_stone", chance = 5, min = 1, max = 1},
},
water_damage = 0,
lava_damage = 2,
light_damage = 0,
--follow = {"farming:bread", "mobs:meat", "default:diamond"},
view_range = 15,
owner = "",
order = "follow",
--order = "stand",
fear_height = 3,
animation = {
speed_normal = 30,
speed_run = 30,
stand_start = 0,
stand_end = 79,
walk_start = 168,
walk_end = 187,
run_start = 168,
run_end = 187,
punch_start = 200,
punch_end = 219,
},
after_activate = function(self, staticdata, def, dtime)
npc.after_activate(self)
end,
on_rightclick = function(self, clicker)
-- Check if right-click interaction is enabled
if self.enable_rightclick_interaction == true then
npc.rightclick_interaction(self, clicker)
end
end,
do_custom = function(self, dtime)
return npc.step(self, dtime)
end
})
-- Spawn
-- mobs:spawn({
-- name = "advanced_npc:npc",
-- nodes = {"advanced_npc:plotmarker_auto_spawner", "mg_villages:plotmarker"},
-- min_light = 3,
-- active_object_count = 1,
-- interval = 5,
-- chance = 1,
-- --max_height = 0,
-- on_spawn = npc.initialize
-- })
-------------------------------------------------------------------------
-- Item definitions
-------------------------------------------------------------------------