Mobs API: Add NaN checks

This commit is contained in:
Wuzzy 2019-09-14 09:37:09 +02:00
parent a54d940c83
commit d3ad2f72cd

View File

@ -281,8 +281,8 @@ function mobs:register_mob(name, def)
local tmpyaw = self.object:get_yaw() local tmpyaw = self.object:get_yaw()
local yaw = self.rotate local yaw = self.rotate
if tmpyaw ~= tmpyaw then -- It's a nan value if tmpyaw ~= tmpyaw then -- It's a NaN value
minetest.log("warning", "[mod:mobs] object:get_yaw() nan shim used") minetest.log("warning", "[mobs] object:get_yaw(): NaN returned (pos="..minetest.pos_to_string(vector.round(self.object_get_pos())).."; entitystring="..self.name..")")
else else
yaw = yaw + tmpyaw yaw = yaw + tmpyaw
end end
@ -823,7 +823,9 @@ function mobs:register_mob(name, def)
if p.x > s.x then if p.x > s.x then
yaw = yaw + math.pi yaw = yaw + math.pi
end end
if yaw == yaw then -- NaN check
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
end
-- anyone but standing npc's can move along -- anyone but standing npc's can move along
if dist > 2 and self.order ~= "stand" then if dist > 2 and self.order ~= "stand" then
@ -878,8 +880,10 @@ function mobs:register_mob(name, def)
else else
yaw = self.object:get_yaw() + ((math.random(0, 360) - 180) / 180 * math.pi) yaw = self.object:get_yaw() + ((math.random(0, 360) - 180) / 180 * math.pi)
end end
if yaw == yaw then -- NaN check
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
end end
end
self.set_velocity(self, 0) self.set_velocity(self, 0)
self.set_animation(self, "stand") self.set_animation(self, "stand")
@ -920,11 +924,12 @@ function mobs:register_mob(name, def)
if lp then if lp then
local vec = {x = lp.x - s.x, y = lp.y - s.y, z = lp.z - s.z} local vec = {x = lp.x - s.x, y = lp.y - s.y, z = lp.z - s.z}
yaw = math.atan(vec.z / vec.x) + 3 * math.pi / 2 - self.rotate yaw = math.atan(vec.z / vec.x) + 3 * math.pi / 2 - self.rotate
if yaw == yaw then -- NaN check
if lp.x > s.x then if lp.x > s.x then
yaw = yaw + math.pi yaw = yaw + math.pi
end end
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
end
-- otherwise randomly turn -- otherwise randomly turn
elseif math.random(1, 100) <= 30 then elseif math.random(1, 100) <= 30 then
self.object:set_yaw(self.object:get_yaw() + ((math.random(0, 360) - 180) / 180 * math.pi)) self.object:set_yaw(self.object:get_yaw() + ((math.random(0, 360) - 180) / 180 * math.pi))
@ -978,7 +983,9 @@ function mobs:register_mob(name, def)
if p.x > s.x then if p.x > s.x then
yaw = yaw+math.pi yaw = yaw+math.pi
end end
if yaw == yaw then -- NaN check
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
end
if self.attack.dist > 3 then if self.attack.dist > 3 then
if not self.v_start then if not self.v_start then
self.v_start = true self.v_start = true
@ -1104,7 +1111,9 @@ function mobs:register_mob(name, def)
if p.x > s.x then if p.x > s.x then
yaw = yaw + math.pi yaw = yaw + math.pi
end end
if yaw == yaw then -- NaN check
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
end
-- attack distance is 2 + half of mob width so the bigger mobs can attack (like slimes) -- attack distance is 2 + half of mob width so the bigger mobs can attack (like slimes)
if self.attack.dist > ((-self.collisionbox[1] + self.collisionbox[4]) / 2) + 2 then if self.attack.dist > ((-self.collisionbox[1] + self.collisionbox[4]) / 2) + 2 then
-- jump attack -- jump attack
@ -1177,7 +1186,9 @@ function mobs:register_mob(name, def)
if p.x > s.x then if p.x > s.x then
yaw = yaw + math.pi yaw = yaw + math.pi
end end
if yaw == yaw then -- NaN check
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
end
self.set_velocity(self, 0) self.set_velocity(self, 0)
if self.shoot_interval and self.timer > self.shoot_interval and math.random(1, 100) <= 60 then if self.shoot_interval and self.timer > self.shoot_interval and math.random(1, 100) <= 60 then