Optimize fall damage, Fix height checks

This commit is contained in:
ElCeejo 2022-07-07 14:26:58 -07:00
parent d69fae95d5
commit d7f019083a
2 changed files with 40 additions and 33 deletions

View file

@ -126,7 +126,15 @@ function creatura.action_move(self, pos2, timeout, method, speed_factor, anim)
local function func(_self)
timer = timer - _self.dtime
self:animate(anim or "walk")
local safe = true
if _self.max_fall
and _self.max_fall > 0 then
local pos = self.object:get_pos()
if not pos then return end
safe = _self:is_pos_safe(pos2)
end
if timer <= 0
or not safe
or _self:move_to(pos2, method or "creatura:obstacle_avoidance", speed_factor or 0.5) then
return true
end

View file

@ -159,21 +159,18 @@ end
-- Turn to specified yaw
local function lerp_rad(a, b, w)
local cs = (1 - w) * cos(a) + w * cos(b)
local sn = (1 - w) * sin(a) + w * sin(b)
return atan2(sn, cs)
end
function mob:turn_to(tyaw, rate)
self._tyaw = tyaw
local weight = rate or 10
rate = rate or 5
local yaw = self.object:get_yaw()
yaw = yaw + pi
tyaw = (tyaw + pi) % pi2
local step = math.min(self.dtime * weight, abs(tyaw - yaw) % pi2)
local dir = abs(tyaw - yaw) > pi and -1 or 1
dir = tyaw > yaw and dir * 1 or dir * -1
local nyaw = (yaw + step * dir) % pi2
self.object:set_yaw(nyaw - pi)
local step = math.min(self.dtime * rate, abs(diff(yaw, tyaw)) % (pi2))
self.object:set_yaw(lerp_rad(yaw, tyaw, step))
self.last_yaw = self.object:get_yaw()
end
@ -383,6 +380,7 @@ end
function mob:is_pos_safe(pos)
local mob_pos = self.object:get_pos()
if not pos then return end
local node = minetest.get_node(pos)
if not node then return false end
if minetest.get_item_group(node.name, "igniter") > 0
@ -1102,22 +1100,22 @@ end
function mob:_vitals()
local stand_pos = self.object:get_pos()
if not stand_pos then return end
local max_fall = self.max_fall or 0
if max_fall > 0 then
local fall_start = self._fall_start
if self.is_falling
and not fall_start
and self.max_fall > 0 then
and not fall_start then
self._fall_start = stand_pos.y
elseif fall_start
and self.max_fall > 0 then
elseif fall_start then
if self.touching_ground
and not self.in_liquid then
local damage = fall_start - stand_pos.y
if damage < (self.max_fall or 3) then
if damage < max_fall then
self._fall_start = nil
return
end
local resist = self.fall_resistance or 0
self:hurt(damage - (damage * (resist * 0.1)))
self:hurt(damage - (damage * resist))
self:indicate_damage()
if random(4) < 2 then
self:play_sound("hurt")
@ -1127,6 +1125,7 @@ function mob:_vitals()
self._fall_start = nil
end
end
end
if self:timer(1) then
local head_pos = vec_raise(stand_pos, self.height)
local head_node = minetest.get_node(head_pos)