Skip to content

Commit

Permalink
fix snake crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Gundul42 committed Dec 25, 2021
1 parent f089c50 commit f93e768
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 84 deletions.
105 changes: 37 additions & 68 deletions animals/snake.lua
Expand Up @@ -9,10 +9,6 @@ local pow = math.pow
local sign = math.sign
local rad = math.rad





local function snake_brain(self)

-- handling death
Expand All @@ -23,54 +19,46 @@ local function snake_brain(self)
return
end


-- handling life in water and on land
if mobkit.timer(self,5) then

local land = mobkit.recall(self,"landlife")
local water = mobkit.recall(self,"waterlife")

if land then
land = math.floor(os.time()-land)
--minetest.chat_send_all(dump(land))
land = math.floor(os.time() - land)
if random(240,360) < land then
--minetest.chat_send_all("Go to water")
mobkit.clear_queue_high(self)
water_life.hq_go2water(self,15)

water_life.hq_go2water(self, 15)
end
end

if water then
water = math.floor(os.time()-water)
if random (60,120) < water then
--minetest.chat_send_all("Go to land")
mobkit.clear_queue_high(self)
water_life.hq_go2land(self,25)
water_life.hq_go2land(self, 25)
end
end

--minetest.chat_send_all("Land: "..dump(land).." : Water: "..dump(water))
end



if mobkit.timer(self,1) then

if not mobkit.recall(self,"landlife") and not mobkit.recall(self,"waterlife") then
mobkit.remember(self,"waterlife",os.time())
if not mobkit.recall(self,"landlife") and
not mobkit.recall(self,"waterlife") then
mobkit.remember(self,"waterlife", os.time())
end

if self.isinliquid then
if mobkit.recall(self,"landlife") then
mobkit.remember(self,"waterlife",os.time())
mobkit.remember(self,"waterlife", os.time())
mobkit.forget(self,"landlife")
end
end

if self.isonground then
if mobkit.recall(self,"waterlife") then
mobkit.remember(self,"landlife",os.time())
mobkit.remember(self,"landlife", os.time())
mobkit.forget(self,"waterlife")
end
end
Expand All @@ -79,10 +67,12 @@ local function snake_brain(self)

if prty < 20 then
local target = mobkit.get_nearby_player(self)
local aliveinwater = target and mobkit.is_alive(target) and mobkit.is_in_deep(target)
local aliveinwater = target and mobkit.is_alive(target)
and mobkit.is_in_deep(target)


if target and mobkit.is_alive(target) and target:get_attach() == nil and aliveinwater then
if target and mobkit.is_alive(target)
and target:get_attach() == nil
and aliveinwater then

local dist = water_life.dist2tgt(self,target)
if dist < self.view_range then
Expand All @@ -92,75 +82,52 @@ local function snake_brain(self)


if self.isinliquid then

mobkit.clear_queue_high(self)
mobkit.clear_queue_low(self)
water_life.hq_aqua_roam(self,21,1,"swim")

--[[
if target and mobkit.is_alive(target) and target:get_attach() == nil and not water_life.isinliquid(target) then --.is_in_deep(target) then
local dist = water_life.dist2tgt(self,target)
if dist < 10 then
mobkit.clear_queue_high(self)
water_life.hq_go2land(self,20,target)
end
end]]

water_life.hq_aqua_roam(self, 21, 1, "swim")
end

if self.isonground then

if target and mobkit.is_alive(target) then
local action = mobkit.recall(self,"warned")
local pname = target:get_player_name()
local dist = water_life.dist2tgt(self,target)
local dist = water_life.dist2tgt(self, target)

if dist > 4 and dist < self.view_range and not action then
mobkit.clear_queue_high(self)
mobkit.clear_queue_low(self)
water_life.hq_snake_warn(self,target,30,8)
water_life.hq_snake_warn(self, target, 30, 8)
elseif dist < 5 or action == pname then
mobkit.forget(self,"warned")
mobkit.forget(self, "warned")

local meta = target:get_meta()
--minetest.chat_send_all(dump(action).." "..pname.." poison level = "..dump(meta:get_int("snakepoison")).." : "..
-- dump(meta:get_int("bitten")))
if meta:get_int("snakepoison") > 0 or meta:get_int("bitten") > 0 then
water_life.hq_snakerun(self,31,target)
water_life.hq_snakerun(self, 31, target)
else
water_life.hq_hunt(self,31,target)
water_life.hq_hunt(self, 31, target)
end
end
end

end

end
end

if mobkit.is_queue_empty_high(self) then
if self.isinliquid then water_life.hq_aqua_roam(self,21,1,"swim") end
if self.isinliquid then water_life.hq_aqua_roam(self, 21, 1, "swim") end
if self.isonground then

water_life.hq_snake_move(self,10)
water_life.hq_idle(self,9,random(60,180),"sleep")


water_life.hq_snake_move(self, 10)
water_life.hq_idle(self, 9, random(60,180), "sleep")
end
end



end



minetest.register_entity("water_life:snake",{
-- common props
-- common props
physical = true,
stepheight = 0.1, --EVIL!
stepheight = 0.1,
collide_with_objects = false,
collisionbox = {-0.35, -0.01, -0.35, 0.35, 0.2, 0.35},
visual = "mesh",
Expand All @@ -169,16 +136,14 @@ minetest.register_entity("water_life:snake",{
visual_size = {x = 0.05, y = 0.05},
static_save = true,
makes_footstep_sound = false,
on_step = mobkit.stepfunc, -- required
on_activate = mobkit.actfunc, -- required
on_step = mobkit.stepfunc,
on_activate = mobkit.actfunc,
get_staticdata = mobkit.statfunc,
-- api props
springiness=0,
buoyancy = 0.99, -- portion of hitbox submerged
buoyancy = 0.99,
max_speed = 7,
jump_height = 2.5, --1.26,
jump_height = 2.5,
view_range = 7,
-- lung_capacity = 0, -- seconds
max_hp = 20,
timeout=-300,
drops = {
Expand Down Expand Up @@ -213,8 +178,8 @@ minetest.register_entity("water_life:snake",{
if water_life.bloody then water_life.spilltheblood(self.object) end
mobkit.hurt(self,tool_capabilities.damage_groups.fleshy or 1)

if type(puncher)=='userdata' and puncher:is_player() then -- if hit by a player
mobkit.clear_queue_high(self) -- abandon whatever they've been doing
if type(puncher)=='userdata' and puncher:is_player() then
mobkit.clear_queue_high(self)
if self.isinliquid then
water_life.hq_water_attack(self,puncher,31,4,true)
end
Expand All @@ -231,9 +196,13 @@ minetest.register_entity("water_life:snake",{
local item = clicker:get_wielded_item()
local name = clicker:get_player_name()

if not item or (item:get_name() ~= "fireflies:bug_net" and item:get_name() ~= water_life.catchNet) then return end
if not inv:room_for_item("main", "water_life:snake_item") then return end

if not item or (item:get_name() ~= "fireflies:bug_net"
and item:get_name() ~= water_life.catchNet) then
return
end
if not inv:room_for_item("main", "water_life:snake_item") then
return
end
if random(1000) < 333 then
inv:add_item("main", "water_life:snake_item")
self.object:remove()
Expand Down
6 changes: 5 additions & 1 deletion api.lua
Expand Up @@ -211,7 +211,11 @@ end
function water_life.dist2tgt(self,tgt)
local pos = mobkit.get_stand_pos(self)
local tpos = tgt:get_pos()
return vector.distance(pos,tpos)
if (not tgt or not self) then
return 100
else
return vector.distance(pos,tpos)
end
end


Expand Down
32 changes: 18 additions & 14 deletions behaviors.lua
Expand Up @@ -245,12 +245,11 @@ function water_life.lq_dumbwalk(self,dest,speed_factor)
local pos = mobkit.get_stand_pos(self)
local y = self.object:get_velocity().y
local dir = vector.normalize(vector.direction({x=pos.x,y=0,z=pos.z},
{x=dest.x,y=0,z=dest.z}))
{x=dest.x,y=0,z=dest.z}))
dir = vector.multiply(dir,self.max_speed*speed_factor)
mobkit.turn2yaw(self,minetest.dir_to_yaw(dir))
dir.y = y
self.object:set_velocity(dir)

end
mobkit.queue_low(self,func)
end
Expand Down Expand Up @@ -300,21 +299,22 @@ function water_life.lq_jumpattack(self,height,target,extra)
meta:set_int("snakepoison",1)
water_life.change_hud(target,"poison")
else
local left = water_life.newplayerbonus - math.floor((os.time() - join)/86400*100)/100
minetest.chat_send_player(target:get_player_name(),minetest.colorize('#fd4000',">>> A rattlesnake bit you. New player bonus of "..left..
" days left. Catch 3 snakes to craft antiserum"))
meta:set_int("bitten",1)

local left = water_life.newplayerbonus -
math.floor((os.time() - join) / 86400 * 100) / 100
minetest.chat_send_player(target:get_player_name(),
minetest.colorize('#fd4000',
">>> A rattlesnake bit you. New player bonus of "..left..
" days left. Catch 3 snakes to craft antiserum"))
meta:set_int("bitten", 1)
minetest.after(10,function()
meta:set_int("bitten",0)
end,meta)
end
end
end
-- bounce off
-- bounce off
local vy = self.object:get_velocity().y
self.object:set_velocity({x=dir.x*-3,y=vy,z=dir.z*-3})
-- play attack sound if defined
mobkit.make_sound(self,'attack')
phase=4
end
Expand Down Expand Up @@ -527,13 +527,12 @@ function water_life.hq_attack(self,prty,tgtobj)
end


function water_life.hq_hunt(self,prty,tgtobj,lost,anim)
function water_life.hq_hunt(self, prty, tgtobj, lost, anim)
if not lost then lost = self.view_range end
if random(100) < 20 then mobkit.make_sound(self,"attack") end


local func = function(self)
if not mobkit.is_alive(tgtobj) then return true end
if not mobkit.is_alive(tgtobj) or not tgtobj then return true end
if self.isinliquid then return true end
if mobkit.is_queue_empty_low(self) and self.isonground then
local pos = mobkit.get_stand_pos(self)
Expand All @@ -552,7 +551,7 @@ function water_life.hq_hunt(self,prty,tgtobj,lost,anim)
if poison > 0 or noob > 0 then return true end

if mobkit.is_in_deep(tgtobj) then
return true --water_life.hq_water_attack(self,tgtobj,prty+1,7)
return true
end
if dist > lost or math.abs(pos.y - opos.y) > 5 then
return true
Expand Down Expand Up @@ -1211,6 +1210,7 @@ function water_life.hq_snake_warn(self,target,prty,duration,anim)
local init = true

local func=function(self)
local dist = 100
if init then
mobkit.make_sound(self,"warn")
minetest.after(1,function(anim)
Expand All @@ -1222,7 +1222,11 @@ function water_life.hq_snake_warn(self,target,prty,duration,anim)
return true
end
local yaw = water_life.get_yaw_to_object(self,target)
local dist = water_life.dist2tgt(self,target)
if (not target:get_pos()) then
return true
else
dist = water_life.dist2tgt(self,target)
end
self.object:set_yaw(yaw)
duration = duration-self.dtime
if dist > self.view_range then
Expand Down
2 changes: 1 addition & 1 deletion init.lua
Expand Up @@ -6,7 +6,7 @@
-----------------------------------------------------------

water_life = {}
water_life.version = "211111"
water_life.version = "211225"
water_life.shark_food = {}
water_life.repellant = {}
water_life.gull_bait = {}
Expand Down

0 comments on commit f93e768

Please sign in to comment.