More movement code improvements.

This commit is contained in:
kno10 2024-07-05 13:29:31 +02:00
parent 05d6a283a0
commit 33454d7545

@ -244,7 +244,7 @@ function mob_class:can_jump_cliff()
then then
--disable fear height while we make our jump --disable fear height while we make our jump
self._jumping_cliff = true self._jumping_cliff = true
-- minetest.log("Jumping cliff: " .. self.name) -- minetest.log("Jumping cliff: " .. self.name .. " nodes " .. nodLow.name .. " - " .. nodFar.name .. " - " .. nodFar2.name)
minetest.after(.01, function() minetest.after(.01, function()
if self and self.object then if self and self.object then
self._jumping_cliff = false self._jumping_cliff = false
@ -277,24 +277,23 @@ function mob_class:is_at_cliff_or_danger()
vector.new(pos.x + dir_x, ypos - self.fear_height, pos.z + dir_z)) vector.new(pos.x + dir_x, ypos - self.fear_height, pos.z + dir_z))
if free_fall then if free_fall then
return math.random() < 0.99 -- sometimes mobs make mistakes if math.random() < 0.98 then -- sometimes mobs make mistakes
return true
end
-- minetest.log(self.name.." takes a leap of faith.")
return false
end end
-- avoid routes where we cannot get back, be reluctant to drop -- avoid routes where we cannot get back, be reluctant to drop
local height = ypos + 0.5 - blocker.y local height = ypos + 0.5 - blocker.y
if height > 1.25 and math.random() < (self.jump_height or 4) / 4 / height / height then if self.runaway_timer == 0 and height > 1.25 and math.random() < (self.jump_height or 4) / 4 / height / height then
-- minetest.log("Avoiding drop of "..height.." chance "..((self.jump_height or 4) / 4 / height)) --minetest.log(self.name.." avoiding drop of "..height.." chance "..((self.jump_height or 4) / 4 / height / height))
return return
end end
local bnode = minetest.get_node(blocker) local bnode = minetest.get_node(blocker)
local danger = self:is_node_dangerous(bnode.name) or self:is_node_waterhazard(bnode.name) -- minetest.log("At cliff: " .. self.name .. " below " .. bnode.name)
if danger then if self:is_node_dangerous(bnode.name) or self:is_node_waterhazard(bnode.name) then
return true return bnode.name
end end
--local def = minetest.registered_nodes[bnode.name]
--if def and def.walkable then
-- return false
--end
return false return false
end end
@ -344,8 +343,6 @@ function mob_class:is_at_water_danger()
end end
function mob_class:env_danger_movement_checks(player_in_active_range) function mob_class:env_danger_movement_checks(player_in_active_range)
local yaw = 0
if not player_in_active_range then return end if not player_in_active_range then return end
if self.state == PATHFINDING if self.state == PATHFINDING
@ -363,8 +360,9 @@ function mob_class:env_danger_movement_checks(player_in_active_range)
self.state = "stand" self.state = "stand"
self:set_animation( "stand") self:set_animation( "stand")
end end
yaw = yaw + math.random() - 0.5 local yaw = self.object:get_yaw() or 0
yaw = self:set_yaw( yaw, 8) yaw = yaw + math.random(-0.5, 0.5)
self:set_yaw(yaw, 8)
return return
end end
end end
@ -376,7 +374,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range)
self:set_animation( "stand") self:set_animation( "stand")
end end
local yaw = self.object:get_yaw() or 0 local yaw = self.object:get_yaw() or 0
yaw = self:set_yaw( yaw + 0.78, 8) self:set_yaw( yaw + 0.78, 8)
end--]] end--]]
end end
@ -498,7 +496,7 @@ function mob_class:do_jump()
local yaw = self.object:get_yaw() or 0 local yaw = self.object:get_yaw() or 0
yaw = self:set_yaw( yaw + 1.35, 8) self:set_yaw( yaw + 1.35, 8)
self.jump_count = 0 self.jump_count = 0
end end
@ -677,21 +675,9 @@ function mob_class:check_runaway_from()
end end
if min_player then if min_player then
local lp = player:get_pos() local lp = player:get_pos()
local vec = { local yaw = atan2(s.x - lp.x, s.z - lp.z) - self.rotate -- away from player
x = lp.x - s.x, self:set_yaw( yaw, 4)
y = lp.y - s.y,
z = lp.z - s.z
}
local yaw = (atan2(vec.z, vec.x) + 3 *math.pi/ 2) - self.rotate
if lp.x > s.x then
yaw = yaw + math.pi
end
yaw = self:set_yaw( yaw, 4)
self.state = "runaway" self.state = "runaway"
self.runaway_timer = 3 self.runaway_timer = 3
self.following = nil self.following = nil
@ -748,14 +734,8 @@ function mob_class:check_follow()
if (not self:object_in_range(self.following)) then if (not self:object_in_range(self.following)) then
self.following = nil self.following = nil
else else
local vec = { local yaw = atan2(p.x - s.x, p.z - s.z) - self.rotate
x = p.x - s.x, self:set_yaw(yaw, 2.35)
z = p.z - s.z
}
local yaw = (atan2(vec.z, vec.x) +math.pi/ 2) - self.rotate
if p.x > s.x then yaw = yaw +math.pi end
self:set_yaw( yaw, 2.35)
-- anyone but standing npc's can move along -- anyone but standing npc's can move along
if dist > 3 and self.order ~= "stand" then if dist > 3 and self.order ~= "stand" then
@ -818,9 +798,7 @@ function mob_class:go_to_pos(b)
--self:set_velocity(0) --self:set_velocity(0)
return true return true
end end
local v = { x = b.x - s.x, z = b.z - s.z } local yaw = atan2(b.x - s.x, b.z - s.z) - self.rotate
local yaw = (atan2(v.z, v.x) +math.pi/ 2) - self.rotate
if b.x > s.x then yaw = yaw +math.pi end
self.object:set_yaw(yaw) self.object:set_yaw(yaw)
self:set_velocity(self.follow_velocity) self:set_velocity(self.follow_velocity)
self:set_animation("walk") self:set_animation("walk")
@ -897,43 +875,28 @@ function mob_class:do_states_walk()
{"group:solid"}) {"group:solid"})
lp = #lp > 0 and lp[math.random(#lp)] lp = #lp > 0 and lp[math.random(#lp)]
-- did we find land? -- did we find land?
if lp then if lp then
-- minetest.log(self.name .. " heading to land ".. tostring(minetest.get_node(lp).name or nil))
local vec = { yaw = atan2(lp.x - s.x, lp.z - s.z) - self.rotate
x = lp.x - s.x,
z = lp.z - s.z
}
yaw = (atan2(vec.z, vec.x) +math.pi/ 2) - self.rotate
if lp.x > s.x then yaw = yaw +math.pi end
-- look towards land and move in that direction -- look towards land and move in that direction
yaw = self:set_yaw( yaw, 6) self:set_yaw(yaw, 6)
self:set_velocity(self.walk_velocity) self:set_velocity(self.walk_velocity)
end end
end end
-- A danger is near but mob is not inside -- A danger is near but mob is not inside
else else
-- Randomly turn -- Randomly turn
if math.random(1, 100) <= 30 then if math.random(1, 100) <= 30 then
yaw = yaw + math.random() - 0.5 yaw = yaw + math.random(-0.5, 0.5)
yaw = self:set_yaw( yaw, 8) self:set_yaw(yaw, 8)
end end
end end
yaw = self:set_yaw( yaw, 8)
-- otherwise randomly turn -- otherwise randomly turn
elseif math.random(1, 100) <= 30 then elseif math.random(1, 100) <= 30 then
yaw = yaw + math.random() - 0.5 yaw = yaw + math.random() - 0.5
yaw = self:set_yaw( yaw, 8) yaw = self:set_yaw(yaw, 8)
end end
-- stand for great fall or danger or fence in front -- stand for great fall or danger or fence in front
@ -942,11 +905,14 @@ function mob_class:do_states_walk()
or cliff_or_danger or cliff_or_danger
or math.random(1, 100) <= 30 then or math.random(1, 100) <= 30 then
-- if cliff_or_danger then
-- minetest.log(self.name .. " turning away from danger "..tostring(self:is_at_cliff_or_danger() or "nil"))
-- end
self:set_velocity(0) self:set_velocity(0)
self.state = "stand" self.state = "stand"
self:set_animation( "stand") self:set_animation( "stand")
local yaw = self.object:get_yaw() or 0 yaw = yaw + 0.78
yaw = self:set_yaw( yaw + 0.78, 8) self:set_yaw( yaw, 8)
else else
self:set_velocity(self.walk_velocity) self:set_velocity(self.walk_velocity)
@ -979,20 +945,12 @@ function mob_class:do_states_stand(player_in_active_range)
-- look at any players nearby, otherwise turn randomly -- look at any players nearby, otherwise turn randomly
if lp and self.look_at_players then if lp and self.look_at_players then
yaw = atan2(lp.x - s.x, lp.z - s.z) - self.rotate
local vec = {
x = lp.x - s.x,
z = lp.z - s.z
}
yaw = (atan2(vec.z, vec.x) +math.pi/ 2) - self.rotate
if lp.x > s.x then yaw = yaw +math.pi end
else else
yaw = yaw + math.random() - 0.5 yaw = yaw + math.random() - 0.5
end end
yaw = self:set_yaw( yaw, 8) self:set_yaw( yaw, 8)
end end
if self.order == "sit" then if self.order == "sit" then
self:set_animation( "sit") self:set_animation( "sit")
@ -1032,8 +990,7 @@ function mob_class:do_states_runaway()
self:set_velocity(0) self:set_velocity(0)
self.state = "stand" self.state = "stand"
self:set_animation( "stand") self:set_animation( "stand")
local yaw = self.object:get_yaw() or 0 self:set_yaw( yaw + 0.78, 8)
yaw = self:set_yaw( yaw + 0.78, 8)
else else
self:set_velocity( self.run_velocity) self:set_velocity( self.run_velocity)
self:set_animation( "run") self:set_animation( "run")