Assorted spawn_children_on_die fixes.

* Use proper vector semantics.
* Optimize away superfluous temp variables and repetitive local variable
  declarations.
This commit is contained in:
kabou 2022-12-22 01:47:38 +01:00 committed by Gitea
parent 95cbac78a8
commit ea19f02e14

View file

@ -11,30 +11,31 @@ local S = minetest.get_translator("mobs_mc")
-- eject_speed: Initial speed of child mob away from "mother" mob -- eject_speed: Initial speed of child mob away from "mother" mob
local spawn_children_on_die = function(child_mob, spawn_distance, eject_speed) local spawn_children_on_die = function(child_mob, spawn_distance, eject_speed)
return function(self, pos) return function(self, pos)
local angle, posadd, newpos, dir local posadd, newpos, dir
if not eject_speed then if not eject_speed then
eject_speed = 1 eject_speed = 1
end end
local mndef = minetest.registered_nodes[minetest.get_node(pos).name] local mndef = minetest.registered_nodes[minetest.get_node(pos).name]
local mother_stuck = mndef and mndef.walkable local mother_stuck = mndef and mndef.walkable
angle = math.random(0, math.pi*2) local angle = math.random(0, math.pi*2)
local children = {} local children = {}
local spawn_count = math.random(2, 4) local spawn_count = math.random(2, 4)
for i = 1, spawn_count do for i = 1, spawn_count do
dir = {x=math.cos(angle),y=0,z=math.sin(angle)} dir = vector.new(math.cos(angle), 0, math.sin(angle))
posadd = vector.multiply(vector.normalize(dir), spawn_distance) posadd = vector.normalize(dir) * spawn_distance
newpos = vector.add(pos, posadd) newpos = pos + posadd
-- If child would end up in a wall, use position of the "mother", unless -- If child would end up in a wall, use position of the "mother", unless
-- the "mother" was stuck as well -- the "mother" was stuck as well
local speed_penalty = 1 if not mother_stuck then
local cndef = minetest.registered_nodes[minetest.get_node(newpos).name] local cndef = minetest.registered_nodes[minetest.get_node(newpos).name]
if (not mother_stuck) and cndef and cndef.walkable then if cndef and cndef.walkable then
newpos = pos newpos = pos
speed_penalty = 0.5 eject_speed = eject_speed * 0.5
end
end end
local mob = minetest.add_entity(newpos, child_mob) local mob = minetest.add_entity(newpos, child_mob)
if (not mother_stuck) then if not mother_stuck then
mob:set_velocity(vector.multiply(dir, eject_speed * speed_penalty)) mob:set_velocity(dir * eject_speed)
end end
mob:set_yaw(angle - math.pi/2) mob:set_yaw(angle - math.pi/2)
table.insert(children, mob) table.insert(children, mob)
@ -43,10 +44,11 @@ local spawn_children_on_die = function(child_mob, spawn_distance, eject_speed)
-- If mother was murdered, children attack the killer after 1 second -- If mother was murdered, children attack the killer after 1 second
if self.state == "attack" then if self.state == "attack" then
minetest.after(1.0, function(children, enemy) minetest.after(1.0, function(children, enemy)
for c=1, #children do local child, le
local child = children[c] for c = 1, #children do
local le = child:get_luaentity() child = children[c]
if le ~= nil then le = childdren[c]:get_luaentity()
if le then
le.state = "attack" le.state = "attack"
le.attack = enemy le.attack = enemy
end end