From 87d13c857f041999085a8d0f032774b2d31efcbe Mon Sep 17 00:00:00 2001 From: tenplus1 Date: Sun, 3 Mar 2024 17:09:30 +0000 Subject: [PATCH] replace :distance with get_distance() function --- api.lua | 10 +++++----- spawner.lua | 12 +++++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/api.lua b/api.lua index c063375..23381a5 100644 --- a/api.lua +++ b/api.lua @@ -14,7 +14,7 @@ local use_vh1 = minetest.get_modpath("visual_harm_1ndicators") -- Global mobs = { mod = "redo", - version = "20240301", + version = "20240303", translate = S, invis = minetest.global_exists("invisibility") and invisibility or {}, node_snow = minetest.registered_aliases["mapgen_snow"] @@ -272,7 +272,7 @@ function mob_class:collision() pos2 = player:get_pos() - if pos2:distance(pos) < width then + if get_distance(pos2, pos) < width then vec = {x = pos.x - pos2.x, z = pos.z - pos2.z} force = (width + 0.5) - vector.distance( @@ -2253,7 +2253,7 @@ function mob_class:do_states(dtime) local player_pos = player:get_pos() - if player_pos:distance(s) <= 3 then + if get_distance(player_pos, s) <= 3 then lp = player_pos break end @@ -3342,7 +3342,7 @@ function mob_class:mob_expire(pos, dtime) -- only despawn away from player for _,player in pairs(minetest.get_connected_players()) do - if player:get_pos():distance(pos) <= 15 then + if get_distance(player:get_pos(), pos) <= 15 then self.lifetimer = 20 return end @@ -4069,7 +4069,7 @@ function mobs:spawn_specific(name, nodes, neighbors, min_light, max_light, inter -- only spawn a set distance away from player for _,player in pairs(minetest.get_connected_players()) do - if player:get_pos():distance(pos) <= mob_nospawn_range then + if get_distance(player:get_pos(), pos) <= mob_nospawn_range then --print("--- player too close", name) return end diff --git a/spawner.lua b/spawner.lua index 405dc5c..0ec0101 100644 --- a/spawner.lua +++ b/spawner.lua @@ -11,6 +11,16 @@ local function is_player(player) end +local get_distance = function(a, b) + + if not a or not b then return 50 end -- nil check and default distance + + local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z + + return square(x * x + y * y + z * z) +end + + -- mob spawner local spawner_default = "mobs_animal:pumba 10 15 0 0 0" @@ -161,7 +171,7 @@ minetest.register_abm({ player = players[i] - if player:get_pos():distance(pos) <= pla then + if get_distance(player:get_pos(), pos) <= pla then in_range = true