api udpated, chicken replace rate increased

This commit is contained in:
tenplus1 2015-03-29 20:00:18 +01:00
parent 6af26a3b53
commit 682a6ad9c4
2 changed files with 53 additions and 57 deletions

105
api.lua
View File

@ -1,4 +1,4 @@
-- Mobs Api (24th March 2015) -- Mobs Api (29th March 2015)
mobs = {} mobs = {}
mobs.mod = "redo" mobs.mod = "redo"
@ -48,7 +48,6 @@ function mobs:register_mob(name, def)
passive = def.passive or false, passive = def.passive or false,
recovery_time = def.recovery_time or 0.5, recovery_time = def.recovery_time or 0.5,
knock_back = def.knock_back or 3, knock_back = def.knock_back or 3,
blood_offset = def.blood_offset or 0,
blood_amount = def.blood_amount or 5, blood_amount = def.blood_amount or 5,
blood_texture = def.blood_texture or "mobs_blood.png", blood_texture = def.blood_texture or "mobs_blood.png",
shoot_offset = def.shoot_offset or 0, shoot_offset = def.shoot_offset or 0,
@ -207,8 +206,7 @@ function mobs:register_mob(name, def)
if self.fall_damage == 1 and self.object:getvelocity().y == 0 then if self.fall_damage == 1 and self.object:getvelocity().y == 0 then
local d = self.old_y - self.object:getpos().y local d = self.old_y - self.object:getpos().y
if d > 5 then if d > 5 then
local damage = math.floor(d - 5) self.object:set_hp(self.object:get_hp() - math.floor(d - 5))
self.object:set_hp(self.object:get_hp()-damage)
check_for_death(self) check_for_death(self)
end end
self.old_y = self.object:getpos().y self.old_y = self.object:getpos().y
@ -240,12 +238,12 @@ function mobs:register_mob(name, def)
local pos = self.object:getpos() local pos = self.object:getpos()
local n = minetest.get_node(pos) local n = minetest.get_node(pos)
local lit = minetest.get_node_light(pos) or 0
local tod = minetest.get_timeofday() local tod = minetest.get_timeofday()
pos.y = pos.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
if self.light_damage and self.light_damage ~= 0 if self.light_damage and self.light_damage ~= 0
and pos.y > 0 and pos.y > 0
and lit > 10 -- direct sunlight (was 4) and (minetest.get_node_light(pos) or 0) > 10 -- direct sunlight (was 4)
and tod > 0.2 and tod < 0.8 then and tod > 0.2 and tod < 0.8 then
self.object:set_hp(self.object:get_hp()-self.light_damage) self.object:set_hp(self.object:get_hp()-self.light_damage)
effect(pos, 5, "tnt_smoke.png") effect(pos, 5, "tnt_smoke.png")
@ -278,17 +276,17 @@ function mobs:register_mob(name, def)
if self.type == "monster" and damage_enabled and self.state ~= "attack" then if self.type == "monster" and damage_enabled and self.state ~= "attack" then
local s = self.object:getpos() local s = self.object:getpos()
local inradius = minetest.get_objects_inside_radius(s,self.view_range)
local player = nil local player = nil
local type = nil local type = nil
local obj = nil
for _,oir in ipairs(inradius) do for _,oir in ipairs(minetest.get_objects_inside_radius(s,self.view_range)) do
if oir:is_player() then if oir:is_player() then
player = oir player = oir
type = "player" type = "player"
else else
local obj = oir:get_luaentity() obj = oir:get_luaentity()
if obj then if obj then
player = obj.object player = obj.object
type = obj.type type = obj.type
@ -315,38 +313,23 @@ function mobs:register_mob(name, def)
-- NPC FIND A MONSTER TO ATTACK -- NPC FIND A MONSTER TO ATTACK
if self.type == "npc" and self.attacks_monsters and self.state ~= "attack" then if self.type == "npc" and self.attacks_monsters and self.state ~= "attack" then
local s = self.object:getpos() local s = self.object:getpos()
local inradius = minetest.get_objects_inside_radius(s,self.view_range) local obj = nil
for _, oir in pairs(inradius) do local p, dist
local obj = oir:get_luaentity() for _, oir in pairs(minetest.get_objects_inside_radius(s,self.view_range)) do
obj = oir:get_luaentity()
if obj and obj.type == "monster" then if obj and obj.type == "monster" then
-- attack monster -- attack monster
local p = obj.object:getpos() p = obj.object:getpos()
local dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5 dist = ((p.x-s.x)^2 + (p.y-s.y)^2 + (p.z-s.z)^2)^0.5
self.do_attack(self,obj.object,dist) self.do_attack(self,obj.object,dist)
break break
end end
end end
end end
if self.follow ~= "" and not self.following then
for _,player in pairs(minetest.get_connected_players()) do
local s = self.object:getpos()
local p = player:getpos()
local 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
self.following = player ; self.following_player = true
break
else self.following_player = nil
end
end
end
-- horny animal can mate for 40 seconds, afterwards horny animal cannot mate again for 200 seconds -- horny animal can mate for 40 seconds, afterwards horny animal cannot mate again for 200 seconds
if self.horny == true and self.hornytimer < 240 and self.child == false then if self.horny == true and self.hornytimer < 240 and self.child == false then
self.hornytimer = self.hornytimer + 1 self.hornytimer = self.hornytimer + 1
if self.hornytimer <= 40 then
effect(self.object:getpos(), 4, "heart.png")
end
if self.hornytimer >= 240 then if self.hornytimer >= 240 then
self.hornytimer = 0 self.hornytimer = 0
self.horny = false self.horny = false
@ -363,6 +346,7 @@ function mobs:register_mob(name, def)
textures = self.base_texture, textures = self.base_texture,
mesh = self.base_mesh, mesh = self.base_mesh,
visual_size = {x=self.visual_size.x,y=self.visual_size.y}, visual_size = {x=self.visual_size.x,y=self.visual_size.y},
collisionbox = self.collisionbox,
}) })
end end
end end
@ -370,15 +354,16 @@ function mobs:register_mob(name, def)
-- if animal is horny, find another same animal who is horny and mate -- if animal is horny, find another same animal who is horny and mate
if self.horny == true and self.hornytimer <= 40 then if self.horny == true and self.hornytimer <= 40 then
local pos = self.object:getpos() local pos = self.object:getpos()
effect(pos, 4, "heart.png")
local ents = minetest.get_objects_inside_radius(pos, self.view_range) local ents = minetest.get_objects_inside_radius(pos, self.view_range)
local num = 0 local num = 0
local ent = nil
for i,obj in ipairs(ents) do for i,obj in ipairs(ents) do
local ent = obj:get_luaentity() ent = obj:get_luaentity()
if ent and ent.name == self.name and ent.horny == true and ent.hornytimer <= 40 then num = num + 1 end if ent and ent.name == self.name and ent.horny == true and ent.hornytimer <= 40 then num = num + 1 end
if num > 1 then if num > 1 then
--print("2 horny "..name)
self.following = ent self.following = ent
ent.following = self ent.following = self
self.horny = false self.horny = false
@ -387,22 +372,22 @@ function mobs:register_mob(name, def)
ent.horny = false ent.horny = false
ent.following = nil ent.following = nil
ent.hornytimer = 0 ent.hornytimer = 0
minetest.after(7, function(dtime) minetest.after(7, function(dtime)
--print ("spawned baby:",self.name)
local mob = minetest.add_entity(pos, self.name) local mob = minetest.add_entity(pos, self.name)
local ent2 = mob:get_luaentity() local ent2 = mob:get_luaentity()
local textures = self.base_texture local textures = self.base_texture
if def.child_texture then if def.child_texture then
print ("child texture detected")
textures = def.child_texture[1] textures = def.child_texture[1]
end end
mob:set_properties({ mob:set_properties({
textures = textures, textures = textures,
visual_size = {x=self.visual_size.x/2,y=self.visual_size.y/2}, visual_size = {x=self.visual_size.x/2,y=self.visual_size.y/2},
collisionbox = {self.collisionbox[1]/2, self.collisionbox[2]/2, self.collisionbox[3]/2,
self.collisionbox[4]/2, self.collisionbox[5]/2, self.collisionbox[6]/2},
}) })
ent2.child = true ent2.child = true
ent2.tamed = true ent2.tamed = true
ent2.following = ent -- follow mother
end) end)
num = 0 num = 0
break break
@ -410,7 +395,21 @@ function mobs:register_mob(name, def)
end end
end end
if self.following and self.following:is_player() and self.following:get_wielded_item():get_name() ~= self.follow then if self.follow ~= "" and not self.following 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
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.following = nil
self.v_start = false self.v_start = false
end end
@ -445,9 +444,9 @@ function mobs:register_mob(name, def)
self.v_start = true self.v_start = true
self.set_velocity(self, self.walk_velocity) self.set_velocity(self, self.walk_velocity)
else else
if self.jump and self.get_velocity(self) <= 0.5 and self.object:getvelocity().y == 0 then -- 0.5 was 1.5 if self.jump and self.get_velocity(self) <= 0.5 and self.object:getvelocity().y == 0 then
local v = self.object:getvelocity() local v = self.object:getvelocity()
v.y = 6 v.y = 6.5 -- 6
self.object:setvelocity(v) self.object:setvelocity(v)
end end
self.set_velocity(self, self.walk_velocity) self.set_velocity(self, self.walk_velocity)
@ -721,6 +720,7 @@ function mobs:register_mob(name, def)
local textures = self.base_texture local textures = self.base_texture
local mesh = self.base_mesh local mesh = self.base_mesh
local vis_size = self.visual_size local vis_size = self.visual_size
local colbox = self.collisionbox
-- specific texture if gotten -- specific texture if gotten
if self.gotten == true and def.gotten_texture then if self.gotten == true and def.gotten_texture then
textures = def.gotten_texture textures = def.gotten_texture
@ -735,6 +735,8 @@ function mobs:register_mob(name, def)
if def.child_texture then if def.child_texture then
textures = def.child_texture[1] textures = def.child_texture[1]
end end
colbox = {self.collisionbox[1]/2, self.collisionbox[2]/2, self.collisionbox[3]/2,
self.collisionbox[4]/2, self.collisionbox[5]/2, self.collisionbox[6]/2}
end end
-- remember settings -- remember settings
local tmp = { local tmp = {
@ -748,6 +750,7 @@ function mobs:register_mob(name, def)
textures = textures, textures = textures,
visual_size = vis_size, visual_size = vis_size,
base_texture = self.base_texture, base_texture = self.base_texture,
collisionbox = colbox,
} }
self.object:set_properties(tmp) self.object:set_properties(tmp)
return minetest.serialize(tmp) return minetest.serialize(tmp)
@ -756,14 +759,13 @@ function mobs:register_mob(name, def)
on_punch = function(self, hitter, tflp, tool_capabilities, dir) on_punch = function(self, hitter, tflp, tool_capabilities, dir)
process_weapon(hitter,tflp,tool_capabilities) process_weapon(hitter,tflp,tool_capabilities)
local pos = self.object:getpos()
check_for_death(self) check_for_death(self)
--blood_particles --blood_particles
local pos = self.object:getpos()
pos.y = pos.y + (self.collisionbox[2] + self.collisionbox[5]) / 2
if self.blood_amount > 0 and pos then if self.blood_amount > 0 and pos then
local p = pos effect(pos, self.blood_amount, self.blood_texture)
p.y = p.y + self.blood_offset
effect(p, self.blood_amount, self.blood_texture)
end end
-- knock back effect, adapted from blockmen's pyramids mod -- knock back effect, adapted from blockmen's pyramids mod
@ -790,10 +792,10 @@ function mobs:register_mob(name, def)
if self.state ~= "attack" then if self.state ~= "attack" then
self.do_attack(self,hitter,1) self.do_attack(self,hitter,1)
end end
-- alert other NPCs to the attack -- alert others to the attack
local inradius = minetest.get_objects_inside_radius(hitter:getpos(),5) local obj = nil
for _, oir in pairs(inradius) do for _, oir in pairs(minetest.get_objects_inside_radius(hitter:getpos(),5)) do
local obj = oir:get_luaentity() obj = oir:get_luaentity()
if obj then if obj then
if obj.group_attack == true and obj.state ~= "attack" then if obj.group_attack == true and obj.state ~= "attack" then
obj.do_attack(obj,hitter,1) obj.do_attack(obj,hitter,1)
@ -883,10 +885,10 @@ function check_for_death(self)
local pos = self.object:getpos() local pos = self.object:getpos()
pos.y = pos.y + 0.5 -- drop items half a block higher pos.y = pos.y + 0.5 -- drop items half a block higher
self.object:remove() self.object:remove()
local obj = nil
for _,drop in ipairs(self.drops) do for _,drop in ipairs(self.drops) do
if math.random(1, drop.chance) == 1 then if math.random(1, drop.chance) == 1 then
local d = ItemStack(drop.name.." "..math.random(drop.min, drop.max)) obj = minetest.add_item(pos, ItemStack(drop.name.." "..math.random(drop.min, drop.max)))
local obj = minetest.add_item(pos, d)
if obj then if obj then
obj:setvelocity({x=math.random(-1,1), y=5, z=math.random(-1,1)}) obj:setvelocity({x=math.random(-1,1), y=5, z=math.random(-1,1)})
end end
@ -935,13 +937,6 @@ local weapon = player:get_wielded_item()
weapon:add_wear(wear) weapon:add_wear(wear)
player:set_wielded_item(weapon) player:set_wielded_item(weapon)
end end
-- if weapon:get_definition().sounds ~= nil then
-- local s = math.random(0,#weapon:get_definition().sounds)
-- minetest.sound_play(weapon:get_definition().sounds[s], {object=player,})
-- else
-- minetest.sound_play("default_sword_wood", {object = player,})
-- end
end end
-- Spawn Egg -- Spawn Egg

View File

@ -54,7 +54,7 @@ mobs:register_mob("mobs:chicken", {
-- follows wheat -- follows wheat
follow = "farming:seed_wheat", view_range = 5, follow = "farming:seed_wheat", view_range = 5,
-- replace air with egg (lay) -- replace air with egg (lay)
replace_rate = 1000, replace_rate = 2000,
replace_what = {"air"}, replace_what = {"air"},
replace_with = "mobs:egg", replace_with = "mobs:egg",
-- right click to pick up chicken -- right click to pick up chicken
@ -80,7 +80,8 @@ mobs:register_mob("mobs:chicken", {
return tool return tool
end end
if clicker:is_player() and clicker:get_inventory() and clicker:get_inventory():room_for_item("main", "mobs:chicken") then if clicker:is_player() and clicker:get_inventory() and self.child == false
and clicker:get_inventory():room_for_item("main", "mobs:chicken") then
clicker:get_inventory():add_item("main", "mobs:chicken") clicker:get_inventory():add_item("main", "mobs:chicken")
self.object:remove() self.object:remove()
end end