Minor performance and stability improvements

This commit is contained in:
ElCeejo 2022-06-16 14:57:06 -07:00 committed by GitHub
parent ad475bb790
commit 13ebe1c37d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1196 additions and 1372 deletions

42
api.lua
View file

@ -6,20 +6,9 @@ creatura.api = {}
-- Math --
local pi = math.pi
local pi2 = pi * 2
local abs = math.abs
local floor = math.floor
local random = math.random
local sin = math.sin
local cos = math.cos
local atan2 = math.atan2
local function diff(a, b) -- Get difference between 2 angles
return math.atan2(math.sin(b - a), math.cos(b - a))
end
local function clamp(val, min, max)
if val < min then
val = min
@ -29,10 +18,9 @@ local function clamp(val, min, max)
return val
end
local vec_dir = vector.direction
local vec_dist = vector.distance
local vec_multi = vector.multiply
local vec_sub = vector.subtract
local vec_equals = vector.equals
local vec_add = vector.add
local function vec_center(v)
@ -44,12 +32,6 @@ local function vec_raise(v, n)
return {x = v.x, y = v.y + n, z = v.z}
end
local function dist_2d(pos1, pos2)
local a = {x = pos1.x, y = 0, z = pos1.z}
local b = {x = pos2.x, y = 0, z = pos2.z}
return vec_dist(a, b)
end
---------------
-- Local API --
---------------
@ -135,9 +117,8 @@ function creatura.get_ground_level(pos2, max_diff)
if walkable then
return pos2
end
local diff = 0
if not creatura.get_node_def(node_under.name).walkable then
for i = 1, max_diff do
for _ = 1, max_diff do
pos2.y = pos2.y - 1
node = minetest.get_node(pos2)
node_under = minetest.get_node({
@ -149,7 +130,7 @@ function creatura.get_ground_level(pos2, max_diff)
if walkable then break end
end
else
for i = 1, max_diff do
for _ = 1, max_diff do
pos2.y = pos2.y + 1
node = minetest.get_node(pos2)
node_under = minetest.get_node({
@ -221,8 +202,8 @@ function creatura.get_next_move(self, pos2)
if last_move
and last_move.pos then
local last_call = minetest.get_position_from_hash(last_move.pos)
local last_move = minetest.get_position_from_hash(last_move.move)
if vector.equals(vec_center(last_call), vec_center(pos)) then
last_move = minetest.get_position_from_hash(last_move.move)
if vec_equals(vec_center(last_call), vec_center(pos)) then
return last_move
end
end
@ -243,7 +224,7 @@ function creatura.get_next_move(self, pos2)
for i = 1, #neighbors do
local neighbor = neighbors[i]
local can_move = fast_ray_sight(pos, neighbor)
if vector.equals(neighbor, pos2) then
if vec_equals(neighbor, pos2) then
can_move = true
end
if can_move
@ -286,8 +267,8 @@ function creatura.get_next_move_3d(self, pos2)
if last_move
and last_move.pos then
local last_call = minetest.get_position_from_hash(last_move.pos)
local last_move = minetest.get_position_from_hash(last_move.move)
if vector.equals(vec_center(last_call), vec_center(pos)) then
last_move = minetest.get_position_from_hash(last_move.move)
if vec_equals(vec_center(last_call), vec_center(pos)) then
return last_move
end
end
@ -311,10 +292,9 @@ function creatura.get_next_move_3d(self, pos2)
if not moveable(vec_raise(neighbor, 0.6), width, height) then
can_move = false
end
if vector.equals(neighbor, pos2) then
if vec_equals(neighbor, pos2) then
can_move = true
end
local dist = vec_dist(neighbor, pos2)
if can_move then
next = neighbor
break
@ -464,9 +444,9 @@ function creatura.basic_punch_func(self, puncher, time_from_last_punch, tool_cap
and is_value_in_table(self.immune_to, tool)) then
return
end
local dir = vec_dir(puncher:get_pos(), self:get_center_pos())
local dir = vec_multi(direction, -1)
self:apply_knockback(dir)
self:hurt(tool_capabilities.damage_groups.fleshy or 2)
self:hurt((tool_capabilities.damage_groups.fleshy or damage) or 2)
if random(4) < 2 then
self:play_sound("hurt")
end

View file

@ -23,14 +23,9 @@ end
local vec_dist = vector.distance
local vec_dir = vector.direction
local vec_len = vector.length
local vec_add = vector.add
local vec_multi = vector.multiply
local vec_normal = vector.normalize
local vec_divide = vector.divide
local function vec_raise(v, n)
return {x = v.x, y = v.y + n, z = v.z}
end
local function get_average_pos(vectors)
local sum = {x = 0, y = 0, z = 0}
@ -55,10 +50,6 @@ end
local yaw2dir = minetest.yaw_to_dir
local dir2yaw = minetest.dir_to_yaw
-- Refresh Boid Leader --
local last_boid_refresh = minetest.get_us_time()
-- Get Boid Members --
-- This function scans within
@ -88,19 +79,9 @@ end
-- Calculate Boid angles and offsets.
local function debugpart(pos, time, part)
minetest.add_particle({
pos = pos,
expirationtime = time or 0.2,
size = 8,
glow = 16,
texture = part or "creatura_particle_red.png"
})
end
function creatura.get_boid_angle(self, boid, range) -- calculates boid angle based on seperation, alignment, and cohesion
function creatura.get_boid_angle(self, _boids, range)
local pos = self.object:get_pos()
local boids = boid or creatura.get_boid_members(pos, range or 4, self.name)
local boids = _boids or creatura.get_boid_members(pos, range or 4, self.name)
if #boids < 3 then return end
local yaw = self.object:get_yaw()
local lift = self.object:get_velocity().y
@ -113,7 +94,6 @@ function creatura.get_boid_angle(self, boid, range) -- calculates boid angle bas
local boid = boids[i]
if boid:get_pos() then
local boid_pos = boid:get_pos()
local boid_yaw = boid:get_yaw()
table.insert(positions, boid_pos)
if boid ~= self.object then
table.insert(lifts, vec_normal(boid:get_velocity()).y)

View file

@ -3,17 +3,17 @@
-------------
local pi = math.pi
local pi2 = pi * 2
local abs = math.abs
local ceil = math.ceil
local floor = math.floor
local random = math.random
local rad = math.rad
local atan2 = math.atan2
local sin = math.sin
local cos = math.cos
local function diff(a, b) -- Get difference between 2 angles
return math.atan2(math.sin(b - a), math.cos(b - a))
return atan2(sin(b - a), cos(b - a))
end
local function clamp(val, min, max)
@ -29,18 +29,13 @@ local function vec_center(v)
return {x = floor(v.x + 0.5), y = floor(v.y + 0.5), z = floor(v.z + 0.5)}
end
local function vec_raise(v, n)
return {x = v.x, y = v.y + n, z = v.z}
end
local vec_dir = vector.direction
local vec_dist = vector.distance
local vec_multi = vector.multiply
local vec_add = vector.add
local yaw2dir = minetest.yaw_to_dir
local dir2yaw = minetest.dir_to_yaw
local function debugpart(pos, time, tex)
--[[local function debugpart(pos, time, tex)
minetest.add_particle({
pos = pos,
texture = tex or "creatura_particle_red.png",
@ -48,7 +43,7 @@ local function debugpart(pos, time, tex)
glow = 6,
size = 12
})
end
end]]
---------------------
-- Local Utilities --
@ -60,17 +55,17 @@ local function get_collision(self, yaw)
local pos = self.object:get_pos()
pos.y = pos.y + 1
local pos2 = vec_add(pos, vec_multi(yaw2dir(yaw), width + 5))
for x = -width, width, width / math.ceil(width) do
for y = 0, height, height / math.ceil(height) do
for x = -width, width, width / ceil(width) do
for y = 0, height, height / ceil(height) do
local vec1 = {
x = math.cos(yaw) * ((pos.x + x) - pos.x) + pos.x,
x = cos(yaw) * ((pos.x + x) - pos.x) + pos.x,
y = pos.y + y,
z = math.sin(yaw) * ((pos.x + x) - pos.x) + pos.z
z = sin(yaw) * ((pos.x + x) - pos.x) + pos.z
}
local vec2 = {
x = math.cos(yaw) * ((pos2.x + x) - pos2.x) + pos2.x,
x = cos(yaw) * ((pos2.x + x) - pos2.x) + pos2.x,
y = vec1.y,
z = math.sin(yaw) * ((pos2.x + x) - pos2.x) + pos2.z
z = sin(yaw) * ((pos2.x + x) - pos2.x) + pos2.z
}
local ray = minetest.raycast(vec1, vec2, false, true)
for pointed_thing in ray do
@ -96,18 +91,18 @@ end
function creatura.action_walk(self, pos2, timeout, method, speed_factor, anim)
local timer = timeout or 4
local move_init = false
local function func(self)
local function func(_self)
if not pos2
or (move_init
and not self._movement_data.goal) then return true end
local pos = self.object:get_pos()
timer = timer - self.dtime
and not _self._movement_data.goal) then return true end
local pos = _self.object:get_pos()
timer = timer - _self.dtime
if timer <= 0
or self:pos_in_box({x = pos2.x, y = pos.y + 0.1, z = pos2.z}) then
self:halt()
or _self:pos_in_box({x = pos2.x, y = pos.y + 0.1, z = pos2.z}) then
_self:halt()
return true
end
self:move(pos2, method or "creatura:neighbors", speed_factor or 0.5, anim)
_self:move(pos2, method or "creatura:neighbors", speed_factor or 0.5, anim)
move_init = true
end
self:set_action(func)
@ -116,18 +111,17 @@ end
function creatura.action_fly(self, pos2, timeout, method, speed_factor, anim)
local timer = timeout or 4
local move_init = false
local function func(self)
local function func(_self)
if not pos2
or (move_init
and not self._movement_data.goal) then return true end
local pos = self.object:get_pos()
timer = timer - self.dtime
and not _self._movement_data.goal) then return true end
timer = timer - _self.dtime
if timer <= 0
or self:pos_in_box(pos2) then
self:halt()
or _self:pos_in_box(pos2) then
_self:halt()
return true
end
self:move(pos2, method, speed_factor or 0.5, anim)
_self:move(pos2, method, speed_factor or 0.5, anim)
move_init = true
end
self:set_action(func)
@ -137,11 +131,11 @@ end
function creatura.action_idle(self, time, anim)
local timer = time
local function func(self)
self:set_gravity(-9.8)
self:halt()
self:animate(anim or "stand")
timer = timer - self.dtime
local function func(_self)
_self:set_gravity(-9.8)
_self:halt()
_self:animate(anim or "stand")
timer = timer - _self.dtime
if timer <= 0 then
return true
end
@ -155,19 +149,19 @@ function creatura.action_fallover(self)
local zrot = 0
local init = false
local dir = 1
local function func(self)
local function func(_self)
if not init then
self:animate("stand")
_self:animate("stand")
if random(2) < 2 then
dir = -1
end
init = true
end
local rot = self.object:get_rotation()
local rot = _self.object:get_rotation()
local goal = (pi * 0.5) * dir
local dif = abs(rot.z - goal)
zrot = rot.z + (dif * dir) * 0.15
self.object:set_rotation({x = rot.x, y = rot.y, z = zrot})
_self.object:set_rotation({x = rot.x, y = rot.y, z = zrot})
if (dir > 0 and zrot >= goal)
or (dir < 0 and zrot <= goal) then return true end
end
@ -324,13 +318,12 @@ local function get_obstacle_avoidance(self, goal)
local pos = self.object:get_pos()
pos.y = pos.y + 1
local yaw2goal = dir2yaw(vec_dir(pos, goal))
local outset = vec_add(pos, vec_multi(yaw2dir(yaw2goal), width))
local collide, col_pos = get_collision(self, yaw2goal)
if not collide then return end
local avd_pos
if collide then
for i = 45, 180, 45 do
angle = rad(i)
dir = vec_multi(yaw2dir(yaw2goal + angle), width)
local angle = rad(i)
local dir = vec_multi(yaw2dir(yaw2goal + angle), width)
avd_pos = vec_center(vec_add(pos, dir))
if not get_collision(self, yaw2goal) then
break
@ -347,7 +340,6 @@ local function get_obstacle_avoidance(self, goal)
elseif (pos.y + height * 0.5) - col_pos.y > 1 then
avd_pos.y = avd_pos.y + 3
end
end
return avd_pos
end

View file

@ -15,12 +15,7 @@ local cos = math.cos
local atan2 = math.atan2
local function diff(a, b) -- Get difference between 2 angles
return math.atan2(math.sin(b - a), math.cos(b - a))
end
local function round(n, dec)
local x = 10^(dec or 0)
return math.floor(n * x + 0.5) / x
return atan2(sin(b - a), cos(b - a))
end
local vec_dir = vector.direction
@ -50,82 +45,6 @@ end
-- Local Utilities --
local default_node_def = {walkable = true} -- both ignore and unknown nodes are walkable
local function get_node_height(name)
local def = minetest.registered_nodes[name]
if not def then return 0.5 end
if def.walkable then
if def.drawtype == "nodebox" then
if def.node_box
and def.node_box.type == "fixed" then
if type(def.node_box.fixed[1]) == "number" then
return 0.5 + def.node_box.fixed[5]
elseif type(def.node_box.fixed[1]) == "table" then
return 0.5 + def.node_box.fixed[1][5]
else
return 1
end
else
return 1
end
else
return 1
end
else
return 1
end
end
local function get_node_def(name)
local def = minetest.registered_nodes[name] or default_node_def
if def.walkable
and get_node_height(name) < 0.26 then
def.walkable = false -- workaround for nodes like snow
end
return def
end
local function get_ground_level(pos2, max_diff)
local node = minetest.get_node(pos2)
local node_under = minetest.get_node({
x = pos2.x,
y = pos2.y - 1,
z = pos2.z
})
local walkable = get_node_def(node_under.name) and not get_node_def(node.name)
if walkable then
return pos2
end
local diff = 0
if not get_node_def(node_under.name) then
for i = 1, max_diff do
pos2.y = pos2.y - 1
node = minetest.get_node(pos2)
node_under = minetest.get_node({
x = pos2.x,
y = pos2.y - 1,
z = pos2.z
})
walkable = get_node_def(node_under.name) and not get_node_def(node.name)
if walkable then break end
end
else
for i = 1, max_diff do
pos2.y = pos2.y + 1
node = minetest.get_node(pos2)
node_under = minetest.get_node({
x = pos2.x,
y = pos2.y - 1,
z = pos2.z
})
walkable = get_node_def(node_under.name) and not get_node_def(node.name)
if walkable then break end
end
end
return pos2
end
local function is_value_in_table(tbl, val)
for _, v in pairs(tbl) do
if v == val then
@ -204,7 +123,7 @@ end
function mob:indicate_damage()
self._original_texture_mod = self._original_texture_mod or self.object:get_texture_mod()
self.object:set_texture_mod(self._original_texture_mod .. "^[colorize:#FF000040")
core.after(0.2, function()
minetest.after(0.2, function()
if creatura.is_alive(self) then
self.object:set_texture_mod(self._original_texture_mod)
end
@ -265,8 +184,8 @@ end
-- Sets Velocity to desired speed in mobs current look direction
function mob:set_forward_velocity(speed)
local speed = speed or self._movement_data.speed
function mob:set_forward_velocity(_speed)
local speed = _speed or self._movement_data.speed
local dir = minetest.yaw_to_dir(self.object:get_yaw())
local vel = vec_multi(dir, speed)
vel.y = self.object:get_velocity().y
@ -367,12 +286,11 @@ end
function mob:get_wander_pos(min_range, max_range, dir)
local pos = vec_center(self.object:get_pos())
pos.y = floor(pos.y + 0.5)
local node = minetest.get_node(pos)
if get_node_def(node.name).walkable then -- Occurs if small mob is touching a fence
if creatura.get_node_def(pos).walkable then -- Occurs if small mob is touching a fence
local offset = vector.add(pos, vec_multi(vec_dir(pos, self.object:get_pos()), 1.5))
pos.x = floor(offset.x + 0.5)
pos.z = floor(offset.z + 0.5)
pos = get_ground_level(pos, 1)
pos = creatura.get_ground_level(pos, 1)
end
local width = self.width
local outset = random(min_range, max_range)
@ -383,16 +301,16 @@ function mob:get_wander_pos(min_range, max_range, dir)
z = random(-10, 10) * 0.1
})
local pos2 = vec_add(pos, vec_multi(move_dir, width))
if get_node_def(minetest.get_node(pos2).name).walkable
if creatura.get_node_def(pos2).walkable
and not dir then
for i = 1, 3 do
for _ = 1, 3 do
move_dir = {
x = move_dir.z,
y = 0,
z = move_dir.x * -1
}
pos2 = vec_add(pos, vec_multi(move_dir, width))
if not get_node_def(minetest.get_node(pos2).name).walkable then
if not creatura.get_node_def(pos2).walkable then
break
end
end
@ -401,14 +319,12 @@ function mob:get_wander_pos(min_range, max_range, dir)
end
for i = 1, outset do
local a_pos = vec_add(pos2, vec_multi(move_dir, i))
local a_node = minetest.get_node(a_pos)
local b_pos = {x = a_pos.x, y = a_pos.y - 1, z = a_pos.z}
local b_node = minetest.get_node(b_pos)
if get_node_def(a_node.name).walkable
or not get_node_def(b_node.name).walkable then
a_pos = get_ground_level(a_pos, floor(self.stepheight or 1))
if creatura.get_node_def(a_pos).walkable
or not creatura.get_node_def(b_pos).walkable then
a_pos = creatura.get_ground_level(a_pos, floor(self.stepheight or 1))
end
if not get_node_def(a_node.name).walkable then
if not creatura.get_node_def(a_pos).walkable then
pos2 = a_pos
else
break
@ -419,12 +335,11 @@ end
function mob:get_wander_pos_3d(min_range, max_range, dir, vert_bias)
local pos = vec_center(self.object:get_pos())
local node = minetest.get_node(pos)
if get_node_def(node.name).walkable then -- Occurs if small mob is touching a fence
if creatura.get_node_def(pos).walkable then -- Occurs if small mob is touching a fence
local offset = vector.add(pos, vec_multi(vec_dir(pos, self.object:get_pos()), 1.5))
pos.x = floor(offset.x + 0.5)
pos.z = floor(offset.z + 0.5)
pos = get_ground_level(pos, 1)
pos = creatura.get_ground_level(pos, 1)
end
local width = self.width
local outset = random(min_range, max_range)
@ -435,16 +350,16 @@ function mob:get_wander_pos_3d(min_range, max_range, dir, vert_bias)
z = random(-10, 10) * 0.1
})
local pos2 = vec_add(pos, vec_multi(move_dir, width))
if get_node_def(minetest.get_node(pos2).name).walkable
if creatura.get_node_def(pos2).walkable
and not dir then
for i = 1, 3 do
for _ = 1, 3 do
move_dir = {
x = move_dir.z,
y = move_dir.y,
z = move_dir.x * -1
}
pos2 = vec_add(pos, vec_multi(move_dir, width))
if not get_node_def(minetest.get_node(pos2).name).walkable then
if not creatura.get_node_def(pos2).walkable then
break
end
end
@ -453,11 +368,10 @@ function mob:get_wander_pos_3d(min_range, max_range, dir, vert_bias)
end
for i = 1, outset do
local a_pos = vec_add(pos2, vec_multi(move_dir, i))
local a_node = minetest.get_node(a_pos)
if get_node_def(a_node.name).walkable then
a_pos = get_ground_level(a_pos, floor(self.stepheight or 1))
if creatura.get_node_def(a_pos).walkable then
a_pos = creatura.get_ground_level(a_pos, floor(self.stepheight or 1))
end
if not get_node_def(a_node.name).walkable then
if not creatura.get_node_def(a_pos).walkable then
pos2 = a_pos
else
break
@ -471,8 +385,8 @@ function mob:is_pos_safe(pos)
local node = minetest.get_node(pos)
if not node then return false end
if minetest.get_item_group(node.name, "igniter") > 0
or get_node_def(node.name).drawtype == "liquid"
or get_node_def(minetest.get_node(vec_raise(pos, -1)).name).drawtype == "liquid" then return false end
or creatura.get_node_def(node.name).drawtype == "liquid"
or creatura.get_node_def(vec_raise(pos, -1)).drawtype == "liquid" then return false end
local fall_safe = false
if self.max_fall ~= 0 then
for i = 1, self.max_fall or 3 do
@ -481,7 +395,7 @@ function mob:is_pos_safe(pos)
y = floor(mob_pos.y + 0.5) - i,
z = pos.z
}
if get_node_def(minetest.get_node(fall_pos).name).walkable then
if creatura.get_node_def(fall_pos).walkable then
fall_safe = true
break
end
@ -809,9 +723,9 @@ function mob:activate(staticdata, dtime)
if self.timer
and type(self.timer) == "number" then -- fix crash for converted mobs_redo mobs
self.timer = function(self, n)
local t1 = floor(self.active_time)
local t2 = floor(self.active_time + self.dtime)
self.timer = function(_self, n)
local t1 = floor(_self.active_time)
local t2 = floor(_self.active_time + _self.dtime)
if t2 > t1 and t2%n == 0 then return true end
end
end
@ -830,7 +744,6 @@ function mob:staticdata()
end
function mob:on_step(dtime, moveresult)
--local us_time = minetest.get_us_time()
if not self.hp then return end
self.dtime = dtime or 0.09
self.moveresult = moveresult or {}
@ -851,11 +764,12 @@ function mob:on_step(dtime, moveresult)
self.width = self:get_hitbox()[4] or 0.5
self.height = self:get_height() or 1
end
self:_light_physics()
--local us_time = minetest.get_us_time()
-- Movement Control
if self._move then
self:_move()
end
--minetest.chat_send_all(minetest.get_us_time() - us_time)
if self.utility_stack
and self._execute_utilities then
self:_execute_utilities()
@ -912,7 +826,9 @@ local function do_step(self)
and abs(vel.x + vel.z) > 0 then
local border = self._border
local yaw_offset = vec_add(pos, vec_multi(minetest.yaw_to_dir(self.object:get_yaw()), self.width + 0.7))
table.sort(border, function(a, b) return vec_dist(vec_add(pos, a), yaw_offset) < vec_dist(vec_add(pos, b), yaw_offset) end)
table.sort(border, function(a, b)
return vec_dist(vec_add(pos, a), yaw_offset) < vec_dist(vec_add(pos, b), yaw_offset)
end)
local step_pos = vec_center(vec_add(pos, border[1]))
local halfway = vec_add(pos, vec_multi(vec_dir(pos, step_pos), 0.5))
halfway.y = step_pos.y
@ -923,7 +839,6 @@ local function do_step(self)
end
end
else
local vel = self.object:get_velocity()
self.object:set_velocity(vector.new(vel.x, 7, vel.z))
if self._step.y < pos.y - 0.5 then
self.object:set_velocity(vector.new(vel.x, 0.5, vel.z))
@ -943,7 +858,6 @@ local function collision_detection(self)
local width = self.width + 0.25
local objects = minetest.get_objects_in_area(vec_sub(pos, width), vec_add(pos, width))
if #objects < 2 then return end
local col_no = 0
for i = 2, #objects do
local object = objects[i]
if creatura.is_alive(object)
@ -975,7 +889,6 @@ local function water_physics(self)
floor_pos.y = floor_pos.y + 0.01
local surface_pos = floor_pos
local floor_node = minetest.get_node(floor_pos)
local surface_node = minetest.get_node(surface_pos)
if minetest.get_item_group(floor_node.name, "liquid") < 1 then
self.object:set_acceleration({
x = 0,
@ -1012,7 +925,7 @@ local function water_physics(self)
})
local hydrodynamics = self.hydrodynamics_multiplier or 0.7
local vel_y = vel.y
if self.bouyancy_multiplier == 0 then -- if bouyancy is disabled drag will be applied to keep awuatic mobs from drifting
if self.bouyancy_multiplier == 0 then
vel_y = vel.y * hydrodynamics
end
self.object:set_velocity({
@ -1084,7 +997,6 @@ end
function mob:_execute_actions()
if not self.object then return end
local task = self._task
if #self._task > 0 then
local func = self._task[#self._task].func
if func(self) then
@ -1128,7 +1040,7 @@ function mob:_execute_utilities()
func = nil,
score = 0
}
if (self:timer(self.task_timer or 1)
if (self:timer(self.util_timer or 1)
or not self._utility_data.func)
and is_alive then
for i = 1, #self.utility_stack do
@ -1163,7 +1075,8 @@ function mob:_execute_utilities()
self._utility_data = loop_data
else
local no_data = not self._utility_data.utility and not self._utility_data.args
local new_util = self._utility_data.utility ~= loop_data.utility or not tbl_equals(self._utility_data.args, loop_data.args)
local same_args = tbl_equals(self._utility_data.args, loop_data.args)
local new_util = self._utility_data.utility ~= loop_data.utility or not same_args
if no_data
or new_util then -- if utilities are different or utilities are the same and args are different set new data
self._utility_data = loop_data
@ -1218,9 +1131,10 @@ function mob:_vitals()
end
if self:timer(1) then
local head_pos = vec_raise(stand_pos, self.height)
local head_def = get_node_def(minetest.get_node(head_pos).name)
local head_node = minetest.get_node(head_pos)
local head_def = creatura.get_node_def(head_node.name)
if head_def.drawtype == "liquid"
and minetest.get_item_group(minetest.get_node(head_pos).name, "water") > 0 then
and minetest.get_item_group(head_node.name, "water") > 0 then
if self._breath <= 0 then
self:hurt(1)
self:indicate_damage()
@ -1232,8 +1146,9 @@ function mob:_vitals()
self:memorize("_breath", self._breath)
end
end
local stand_def = get_node_def(minetest.get_node(stand_pos).name)
if minetest.get_item_group(minetest.get_node(stand_pos).name, "fire") > 0
local stand_node = minetest.get_node(stand_pos)
local stand_def = creatura.get_node_def(stand_node.name)
if minetest.get_item_group(stand_node.name, "fire") > 0
and stand_def.damage_per_second then
local damage = stand_def.damage_per_second
local resist = self.fire_resistance or 0.5

View file

@ -8,53 +8,10 @@ local theta_star_alloted_time = tonumber(minetest.settings:get("creatura_theta_s
local floor = math.floor
local abs = math.abs
local function is_node_walkable(name)
local def = minetest.registered_nodes[name]
return def and def.walkable
end
local function is_node_liquid(name)
local def = minetest.registered_nodes[name]
return def and def.drawtype == "liquid"
end
local vec_dist = vector.distance
local moveable = creatura.is_pos_moveable
local function get_ground_level(pos2, max_height)
local node = minetest.get_node(pos2)
local node_under = minetest.get_node({
x = pos2.x,
y = pos2.y - 1,
z = pos2.z
})
local height = 0
local walkable = creatura.get_node_def(node_under.name).walkable and not creatura.get_node_def(node.name).walkable
if walkable then
return pos2
elseif not walkable then
if not creatura.get_node_def(node_under.name).walkable then
while not creatura.get_node_def(node_under.name).walkable
and height < max_height do
pos2.y = pos2.y - 1
node_under = minetest.get_node({
x = pos2.x,
y = pos2.y - 1,
z = pos2.z
})
height = height + 1
end
else
while creatura.get_node_def(node.name).walkable
and height < max_height do
pos2.y = pos2.y + 1
node = minetest.get_node(pos2)
height = height + 1
end
end
return pos2
end
end
local function get_distance(start_pos, end_pos)
local distX = abs(start_pos.x - end_pos.x)
local distZ = abs(start_pos.z - end_pos.z)
@ -84,7 +41,7 @@ local function is_on_ground(pos)
y = pos.y - 1,
z = pos.z
}
if is_node_walkable(minetest.get_node(ground).name) then
if creatura.get_node_def(ground).walkable then
return true
end
return false
@ -94,6 +51,38 @@ local function vec_raise(v, n)
return {x = v.x, y = v.y + n, z = v.z}
end
local function get_line_of_sight(a, b)
local steps = floor(vec_dist(a, b))
local line = {}
for i = 0, steps do
local pos
if steps > 0 then
pos = {
x = a.x + (b.x - a.x) * (i / steps),
y = a.y + (b.y - a.y) * (i / steps),
z = a.z + (b.z - a.z) * (i / steps)
}
else
pos = a
end
table.insert(line, pos)
end
if #line < 1 then
return false
else
for i = 1, #line do
local node = minetest.get_node(line[i])
if creatura.get_node_def(node.name).walkable then
return false
end
end
end
return true
end
-- Find a path from start to goal
function creatura.find_path(self, start, goal, obj_width, obj_height, max_open, climb, fly, swim)
@ -141,7 +130,7 @@ function creatura.find_path(self, start, goal, obj_width, obj_height, max_open,
if neighbor.y == pos.y
and not fly
and not swim then
neighbor = get_ground_level(neighbor, 1)
neighbor = creatura.get_ground_level(neighbor, 1)
end
local can_move = get_line_of_sight({x = pos.x, y = neighbor.y, z = pos.z}, neighbor)
if swim then
@ -169,30 +158,30 @@ function creatura.find_path(self, start, goal, obj_width, obj_height, max_open,
and neighbor.z == pos.z
and climb))
and (not swim
or is_node_liquid(minetest.get_node(neighbor).name)) then
or creatura.get_node_def(neighbor).drawtype == "liquid") then
table.insert(result, neighbor)
end
end
return result
end
local function find_path(start, goal)
local function find_path(_start, _goal)
local us_time = minetest.get_us_time()
start = {
x = floor(start.x + 0.5),
y = floor(start.y + 0.5),
z = floor(start.z + 0.5)
_start = {
x = floor(_start.x + 0.5),
y = floor(_start.y + 0.5),
z = floor(_start.z + 0.5)
}
goal = {
x = floor(goal.x + 0.5),
y = floor(goal.y + 0.5),
z = floor(goal.z + 0.5)
_goal = {
x = floor(_goal.x + 0.5),
y = floor(_goal.y + 0.5),
z = floor(_goal.z + 0.5)
}
if goal.x == start.x
and goal.z == start.z then -- No path can be found
if _goal.x == _start.x
and _goal.z == _start.z then -- No path can be found
return nil
end
@ -200,13 +189,13 @@ function creatura.find_path(self, start, goal, obj_width, obj_height, max_open,
local closedSet = self._path_data.closed or {}
local start_index = minetest.hash_node_position(start)
local start_index = minetest.hash_node_position(_start)
openSet[start_index] = {
pos = start,
pos = _start,
parent = nil,
gScore = 0,
fScore = get_distance(start, goal)
fScore = get_distance(_start, _goal)
}
local count = self._path_data.count or 1
@ -214,7 +203,7 @@ function creatura.find_path(self, start, goal, obj_width, obj_height, max_open,
while count > 0 do
if minetest.get_us_time() - us_time > a_star_alloted_time then
self._path_data = {
start = start,
start = _start,
open = openSet,
closed = closedSet,
count = count
@ -248,16 +237,16 @@ function creatura.find_path(self, start, goal, obj_width, obj_height, max_open,
self._path_data.closedSet = closedSet
-- Reconstruct path if end is reached
if ((is_on_ground(goal)
if ((is_on_ground(_goal)
or fly)
and current_id == minetest.hash_node_position(goal))
and current_id == minetest.hash_node_position(_goal))
or (not fly
and not is_on_ground(goal)
and goal.x == current.pos.x
and goal.z == current.pos.z) then
and not is_on_ground(_goal)
and _goal.x == current.pos.x
and _goal.z == current.pos.z) then
local path = {}
local fail_safe = 0
for k, v in pairs(closedSet) do
for _ in pairs(closedSet) do
fail_safe = fail_safe + 1
end
repeat
@ -296,7 +285,7 @@ function creatura.find_path(self, start, goal, obj_width, obj_height, max_open,
if not openSet[minetest.hash_node_position(neighbor.pos)] then
count = count + 1
end
local hCost = get_distance_to_neighbor(neighbor.pos, goal)
local hCost = get_distance_to_neighbor(neighbor.pos, _goal)
neighbor.gScore = temp_gScore
neighbor.fScore = temp_gScore + hCost
openSet[minetest.hash_node_position(neighbor.pos)] = neighbor
@ -318,38 +307,6 @@ end
-- Theta* --
------------
function get_line_of_sight(a, b)
local steps = floor(vector.distance(a, b))
local line = {}
for i = 0, steps do
local pos
if steps > 0 then
pos = {
x = a.x + (b.x - a.x) * (i / steps),
y = a.y + (b.y - a.y) * (i / steps),
z = a.z + (b.z - a.z) * (i / steps)
}
else
pos = a
end
table.insert(line, pos)
end
if #line < 1 then
return false
else
for i = 1, #line do
local node = minetest.get_node(line[i])
if creatura.get_node_def(node.name).walkable then
return false
end
end
end
return true
end
function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_open, climb, fly, swim)
climb = climb or false
fly = fly or false
@ -391,7 +348,7 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
if neighbor.y == pos.y
and not fly
and not swim then
neighbor = get_ground_level(neighbor, 1)
neighbor = creatura.get_ground_level(neighbor, 1)
end
local can_move = get_line_of_sight({x = pos.x, y = neighbor.y, z = pos.z}, neighbor)
if swim then
@ -419,30 +376,30 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
and neighbor.z == pos.z
and climb))
and (not swim
or is_node_liquid(minetest.get_node(neighbor).name)) then
or creatura.get_node_def(neighbor).drawtype == "liquid") then
table.insert(result, neighbor)
end
end
return result
end
local function find_path(start, goal)
local function find_path(_start, _goal)
local us_time = minetest.get_us_time()
start = {
x = floor(start.x + 0.5),
y = floor(start.y + 0.5),
z = floor(start.z + 0.5)
_start = {
x = floor(_start.x + 0.5),
y = floor(_start.y + 0.5),
z = floor(_start.z + 0.5)
}
goal = {
x = floor(goal.x + 0.5),
y = floor(goal.y + 0.5),
z = floor(goal.z + 0.5)
_goal = {
x = floor(_goal.x + 0.5),
y = floor(_goal.y + 0.5),
z = floor(_goal.z + 0.5)
}
if goal.x == start.x
and goal.z == start.z then -- No path can be found
if _goal.x == _start.x
and _goal.z == _start.z then -- No path can be found
return nil
end
@ -450,13 +407,13 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
local closedSet = self._path_data.closed or {}
local start_index = minetest.hash_node_position(start)
local start_index = minetest.hash_node_position(_start)
openSet[start_index] = {
pos = start,
pos = _start,
parent = nil,
gScore = 0,
fScore = get_distance(start, goal)
fScore = get_distance(_start, _goal)
}
local count = self._path_data.count or 1
@ -464,7 +421,7 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
while count > 0 do
if minetest.get_us_time() - us_time > theta_star_alloted_time then
self._path_data = {
start = start,
start = _start,
open = openSet,
closed = closedSet,
count = count
@ -496,14 +453,14 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
closedSet[current_id] = current
-- Reconstruct path if end is reached
if (is_on_ground(goal)
and current_id == minetest.hash_node_position(goal))
or (not is_on_ground(goal)
and goal.x == current.pos.x
and goal.z == current.pos.z) then
if (is_on_ground(_goal)
and current_id == minetest.hash_node_position(_goal))
or (not is_on_ground(_goal)
and _goal.x == current.pos.x
and _goal.z == current.pos.z) then
local path = {}
local fail_safe = 0
for k, v in pairs(closedSet) do
for _ in pairs(closedSet) do
fail_safe = fail_safe + 1
end
repeat
@ -545,7 +502,7 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
new_gScore = openSet[minetest.hash_node_position(neighbor.pos)].gScore
end
if temp_gScore < new_gScore then
local hCost = get_distance_to_neighbor(neighbor.pos, goal)
local hCost = get_distance_to_neighbor(neighbor.pos, _goal)
neighbor.gScore = temp_gScore
neighbor.fScore = temp_gScore + hCost
neighbor.parent = minetest.hash_node_position(current_parent.pos)
@ -562,7 +519,7 @@ function creatura.find_theta_path(self, start, goal, obj_width, obj_height, max_
new_gScore = openSet[minetest.hash_node_position(neighbor.pos)].gScore
end
if temp_gScore < new_gScore then
local hCost = get_distance_to_neighbor(neighbor.pos, goal)
local hCost = get_distance_to_neighbor(neighbor.pos, _goal)
neighbor.gScore = temp_gScore
neighbor.fScore = temp_gScore + hCost
if openSet[minetest.hash_node_position(neighbor.pos)] then