npc's follow and stand by order

This commit is contained in:
tenplus1 2015-04-02 16:12:04 +01:00
parent e41a111a83
commit 07ef178892
3 changed files with 57 additions and 20 deletions

View File

@ -29,6 +29,7 @@ This mod contains the following additions:
Changelog:
beta- Npc mob added, kills monsters, attacks player when punched, right click with food to heal or gold lump for drop
1.07- Npc's can now be set to follow player or stand by using self.order and self.owner variables
1.06- Changed recovery times after breeding, and time taken to grow up (can be sped up by feeding baby animal)
1.05- Added ExeterDad's bunny's which can be picked up and tamed with 4 carrots from farming redo or farming_plus, also shears added to get wool from sheep and lastly Jordach/BSD's kitten
1.04- Added mating for sheep, cows and hogs... feed animals to make horny and hope for a baby which is half size, will grow up quick though :)

57
api.lua
View File

@ -1,4 +1,4 @@
-- Mobs Api (31st March 2015)
-- Mobs Api (2nd April 2015)
mobs = {}
mobs.mod = "redo"
@ -12,6 +12,10 @@ local peaceful_only = minetest.setting_getbool("only_peaceful_mobs")
function mobs:register_mob(name, def)
minetest.register_entity(name, {
name = name,
owner = def.owner,
order = def.order or "",
hp_min = def.hp_min or 5,
hp_max = def.hp_max or 10,
physical = true,
@ -20,7 +24,7 @@ function mobs:register_mob(name, def)
visual_size = def.visual_size or {x=1, y=1},
mesh = def.mesh,
makes_footstep_sound = def.makes_footstep_sound,
view_range = def.view_range,
view_range = def.view_range or 5,
walk_velocity = def.walk_velocity,
run_velocity = def.run_velocity,
damage = def.damage,
@ -395,34 +399,43 @@ function mobs:register_mob(name, def)
end
end
if self.follow ~= "" and not self.following then
if (self.follow ~= "" or self.order == "follow") and not self.following and self.state ~= "attack" then
local s, p, dist
for _,player in pairs(minetest.get_connected_players()) do
s = self.object:getpos()
p = player:getpos()
dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
if self.view_range and dist < self.view_range then
if dist < self.view_range then
self.following = player
break
end
end
end
--if self.following and self.following:is_player() and self.following:get_wielded_item():get_name() ~= self.follow then
if self.following and self.following.is_player and self.following:get_wielded_item():get_name() ~= self.follow then
self.following = nil
self.v_start = false
if self.type == "npc" and self.order == "follow" and self.state ~= "attack" then
-- npc stop following player if not owner
if self.following and self.type == "npc" and self.owner and self.owner ~= self.following:get_player_name() then
self.following = nil
self.v_start = false
end
else
-- stop following player if not holding specific item
if self.following and self.following.is_player and self.following:get_wielded_item():get_name() ~= self.follow then
self.following = nil
self.v_start = false
end
end
if self.following then
local s = self.object:getpos()
local p
if self.following.is_player and self.following:is_player() then
local s = self.object:getpos()
local p
if self.following.is_player and self.following:is_player() then
p = self.following:getpos()
elseif self.following.object then
p = self.following.object:getpos()
end
end
if p then
local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
@ -439,7 +452,9 @@ function mobs:register_mob(name, def)
yaw = yaw+math.pi
end
self.object:setyaw(yaw)
if dist > 2 then
-- anyone but standing npc's can move along
if dist > 2 and self.order ~= "stand" then
if not self.v_start then
self.v_start = true
self.set_velocity(self, self.walk_velocity)
@ -456,6 +471,7 @@ function mobs:register_mob(name, def)
self.v_start = false
self.set_velocity(self, 0)
self:set_animation("stand")
end
return
end
@ -499,10 +515,17 @@ function mobs:register_mob(name, def)
self.set_velocity(self, 0)
self.set_animation(self, "stand")
if math.random(1, 100) <= self.walk_chance then
self.set_velocity(self, self.walk_velocity)
self.state = "walk"
self.set_animation(self, "walk")
-- npc's ordered to stand stay standing
if self.type == "npc" and self.order == "stand" then
self.set_velocity(self, 0)
self.state = "stand"
self:set_animation("stand")
else
if math.random(1, 100) <= self.walk_chance then
self.set_velocity(self, self.walk_velocity)
self.state = "walk"
self.set_animation(self, "walk")
end
end
elseif self.state == "walk" then

19
npc.lua
View File

@ -8,7 +8,7 @@ mobs:register_mob("mobs:npc", {
type = "npc",
-- aggressive, deals 2 damage to player/monster when hit
passive = false,
damage = 2,
damage = 3,
attack_type = "dogfight",
attacks_monsters = true,
-- health & armor
@ -26,8 +26,8 @@ mobs:register_mob("mobs:npc", {
makes_footstep_sound = true,
sounds = {},
-- speed and jump
walk_velocity = 1,
run_velocity = 2,
walk_velocity = 2,
run_velocity = 3,
jump = true,
-- drops wood and chance of apples when dead
drops = {
@ -45,6 +45,9 @@ mobs:register_mob("mobs:npc", {
-- follow diamond
follow = "default:diamond",
view_range = 15,
-- set owner and order
owner = "",
order = "follow",
-- model animation
animation = {
speed_normal = 30, speed_run = 30,
@ -73,6 +76,16 @@ mobs:register_mob("mobs:npc", {
local pos = self.object:getpos()
pos.y = pos.y + 0.5
minetest.add_item(pos, {name = mobs.npc_drops[math.random(1,#mobs.npc_drops)]})
else
if self.owner == "" then
self.owner = clicker:get_player_name()
else
if self.order == "follow" then
self.order = "stand"
else
self.order = "follow"
end
end
end
end,
})