code tweak and tidy

master
TenPlus1 2020-05-06 21:21:20 +01:00
parent 18906fafeb
commit 8ce8490e78
1 changed files with 43 additions and 41 deletions

74
api.lua
View File

@ -299,8 +299,7 @@ end
-- set defined animation -- set defined animation
function mob_class:set_animation(anim, force) function mob_class:set_animation(anim, force)
if not self.animation if not self.animation or not anim then return end
or not anim then return end
self.animation.current = self.animation.current or "" self.animation.current = self.animation.current or ""
@ -378,10 +377,9 @@ local line_of_sight = function(self, pos1, pos2, stepsize)
local ad = 0 local ad = 0
-- It continues to advance in the line of sight in search of a real -- It continues to advance in the line of sight in search of a real
-- obstruction which counts as 'normal' nodebox. -- obstruction which counts as 'walkable' nodebox.
while minetest.registered_nodes[nn] while minetest.registered_nodes[nn]
and (minetest.registered_nodes[nn].walkable == false) do and (minetest.registered_nodes[nn].walkable == false) do
-- or minetest.registered_nodes[nn].drawtype == "nodebox") do
-- Check if you can still move forward -- Check if you can still move forward
if td < ad + stepsize then if td < ad + stepsize then
@ -444,10 +442,9 @@ local new_line_of_sight = function(self, pos1, pos2, stepsize)
local nn = minetest.get_node(pos).name local nn = minetest.get_node(pos).name
-- It continues to advance in the line of sight in search of a real -- It continues to advance in the line of sight in search of a real
-- obstruction which counts as 'normal' nodebox. -- obstruction which counts as 'walkable' nodebox.
while minetest.registered_nodes[nn] while minetest.registered_nodes[nn]
and (minetest.registered_nodes[nn].walkable == false) do and (minetest.registered_nodes[nn].walkable == false) do
-- or minetest.registered_nodes[nn].drawtype == "nodebox") do
npos1 = vector.add(npos1, stepv) npos1 = vector.add(npos1, stepv)
@ -679,11 +676,8 @@ end
-- drop items -- drop items
function mob_class:item_drop() function mob_class:item_drop()
-- no drops if disabled by setting -- no drops if disabled by setting or mob is child
if not mobs_drop_items then return end if not mobs_drop_items or self.child then return end
-- no drops for child mobs
if self.child then return end
local pos = self.object:get_pos() local pos = self.object:get_pos()
@ -719,7 +713,7 @@ function mob_class:item_drop()
end end
end end
-- only drop rare items (drops.min=0) if killed by player -- only drop rare items (drops.min = 0) if killed by player
if death_by_player then if death_by_player then
obj = minetest.add_item(pos, ItemStack(item .. " " .. num)) obj = minetest.add_item(pos, ItemStack(item .. " " .. num))
@ -810,7 +804,7 @@ function mob_class:check_for_death(cmi_cause)
return true return true
end end
-- default death function and die animation (if defined) -- check for custom death function and die animation
if self.animation if self.animation
and self.animation.die_start and self.animation.die_start
and self.animation.die_end then and self.animation.die_end then
@ -909,22 +903,21 @@ function mob_class:is_at_cliff()
{x = pos.x + dir_x, y = ypos, z = pos.z + dir_z}, {x = pos.x + dir_x, y = ypos, z = pos.z + dir_z},
{x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z}) {x = pos.x + dir_x, y = ypos - self.fear_height, z = pos.z + dir_z})
-- check for straight drop, drop onto danger or walkable node -- check for straight drop
if free_fall then if free_fall then
return true return true
else end
local bnode = node_ok(blocker) local bnode = node_ok(blocker)
-- will we drop onto dangerous node?
if is_node_dangerous(self, bnode.name) then if is_node_dangerous(self, bnode.name) then
return true return true
else end
local def = minetest.registered_nodes[bnode.name] local def = minetest.registered_nodes[bnode.name]
return (not def and def.walkable) return (not def and def.walkable)
end
end
return false
end end
@ -951,7 +944,9 @@ function mob_class:do_env_damage()
-- remove mob if standing inside ignore node -- remove mob if standing inside ignore node
if self.standing_in == "ignore" then if self.standing_in == "ignore" then
self.object:remove() self.object:remove()
return true return true
end end
@ -976,8 +971,7 @@ function mob_class:do_env_damage()
pos.y = pos.y + 1 -- for particle effect position pos.y = pos.y + 1 -- for particle effect position
-- water -- water
if self.water_damage if self.water_damage and nodef.groups.water then
and nodef.groups.water then
if self.water_damage ~= 0 then if self.water_damage ~= 0 then
@ -989,12 +983,8 @@ function mob_class:do_env_damage()
pos = pos, node = self.standing_in}) then return true end pos = pos, node = self.standing_in}) then return true end
end end
-- lava or fire or ignition source -- ignition source (fire or lava)
elseif self.lava_damage elseif self.lava_damage and nodef.groups.igniter then
and nodef.groups.igniter then
-- and (nodef.groups.lava
-- or self.standing_in == node_fire
-- or self.standing_in == node_permanent_flame) then
if self.lava_damage ~= 0 then if self.lava_damage ~= 0 then
@ -1025,6 +1015,7 @@ function mob_class:do_env_damage()
and (nodef.groups.disable_suffocation ~= 1) then and (nodef.groups.disable_suffocation ~= 1) then
local damage local damage
if self.suffocation == true then if self.suffocation == true then
damage = 2 damage = 2
else else
@ -1147,14 +1138,14 @@ function mob_class:do_jump()
and (self.facing_fence or blocked) then and (self.facing_fence or blocked) then
self.jump_count = (self.jump_count or 0) + 1 self.jump_count = (self.jump_count or 0) + 1
--print ("----", self.jump_count)
if self.jump_count > 4 then if self.jump_count > 4 then
local yaw = self.object:get_yaw() or 0 local yaw = self.object:get_yaw() or 0
local turn = random(0, 2) + 1.35 local turn = random(0, 2) + 1.35
yaw = self:set_yaw(yaw + turn, 12) yaw = self:set_yaw(yaw + turn, 12)
--print ("---- turn", turn)
self.jump_count = 0 self.jump_count = 0
end end
end end
@ -1460,11 +1451,11 @@ end
local los_switcher = false local los_switcher = false
local height_switcher = false local height_switcher = false
-- path finding and smart mob routine by rnd, line_of_sight and other edits by Elkien3 -- path finding and smart mob routine by rnd,
-- line_of_sight and other edits by Elkien3
function mob_class:smart_mobs(s, p, dist, dtime) function mob_class:smart_mobs(s, p, dist, dtime)
local s1 = self.path.lastpos local s1 = self.path.lastpos
local target_pos = self.attack:get_pos() local target_pos = self.attack:get_pos()
-- is it becoming stuck? -- is it becoming stuck?
@ -1563,7 +1554,6 @@ function mob_class:smart_mobs(s, p, dist, dtime)
-- round position to center of node to avoid stuck in walls -- round position to center of node to avoid stuck in walls
-- also adjust height for player models! -- also adjust height for player models!
s.x = floor(s.x + 0.5) s.x = floor(s.x + 0.5)
-- s.y = floor(s.y + 0.5) - sheight
s.z = floor(s.z + 0.5) s.z = floor(s.z + 0.5)
local ssight, sground = minetest.line_of_sight(s, { local ssight, sground = minetest.line_of_sight(s, {
@ -1581,9 +1571,11 @@ function mob_class:smart_mobs(s, p, dist, dtime)
p1.z = floor(p1.z + 0.5) p1.z = floor(p1.z + 0.5)
local dropheight = 6 local dropheight = 6
if self.fear_height ~= 0 then dropheight = self.fear_height end if self.fear_height ~= 0 then dropheight = self.fear_height end
local jumpheight = 0 local jumpheight = 0
if self.jump and self.jump_height >= 4 then if self.jump and self.jump_height >= 4 then
jumpheight = min(math.ceil(self.jump_height / 4), 4) jumpheight = min(math.ceil(self.jump_height / 4), 4)
elseif self.stepheight > 0.5 then elseif self.stepheight > 0.5 then
@ -2040,6 +2032,7 @@ function mob_class:follow_flop()
self:set_animation("stand") self:set_animation("stand")
return return
elseif self.state == "flop" then elseif self.state == "flop" then
self.state = "stand" self.state = "stand"
end end
@ -2153,10 +2146,19 @@ function mob_class:do_states(dtime)
if lp then if lp then
-- if mob in dangerous node then look for land -- if mob in dangerous node then look for land
if is_node_dangerous(self, self.standing_in) then if not is_node_dangerous(self, self.standing_in) then
lp = minetest.find_node_near(s, 5, {"group:soil", "group:stone", -- lp = minetest.find_node_near(s, 5, {"group:soil", "group:stone",
"group:sand", node_ice, node_snowblock}) -- "group:sand", node_ice, node_snowblock})
lp = minetest.find_nodes_in_area_under_air(
{s.x - 5, s.y - 5, s.z - 5},
{s.x + 5, s.y + 5, s.z + 5},
{"group:soil", "group:stone", "group:sand",
node_ice, node_snowblock})
-- select position of random block to climb onto
lp = #lp > 0 and lp[random(#lp)]
-- did we find land? -- did we find land?
if lp then if lp then