1
0

Builtin: localize a few functions

This commit is contained in:
Maksym H 2023-04-29 01:13:10 +03:00
parent aed73dc4ba
commit e3bb04d341
5 changed files with 26 additions and 20 deletions

View File

@ -1,7 +1,8 @@
-- Minetest: builtin/item.lua -- Minetest: builtin/item.lua
local random, pi = math.random, math.pi local abs, random, pi = math.abs, math.random, math.pi
local vnew, vround, vadd, vapply = vector.new, vector.round, vector.add, vector.apply local vnew, vround, vadd, vapply, vsubtract =
vector.new, vector.round, vector.add, vector.apply, vector.subtract
local builtin_shared = ... local builtin_shared = ...
local SCALE = 0.667 local SCALE = 0.667
@ -369,7 +370,7 @@ core.register_entity(":__builtin:falling_node", {
local failure = false local failure = false
local pos = self.object:get_pos() local pos = self.object:get_pos()
local distance = vapply(vector.subtract(pos, bcp), math.abs) local distance = vapply(vsubtract(pos, bcp), abs)
if distance.x >= 1 or distance.z >= 1 then if distance.x >= 1 or distance.z >= 1 then
-- We're colliding with some part of a node that's sticking out -- We're colliding with some part of a node that's sticking out
-- Since we don't want to visually teleport, drop as item -- Since we don't want to visually teleport, drop as item

View File

@ -12,11 +12,12 @@ local total_forceloaded = 0
local forceload_blocks_changed = false local forceload_blocks_changed = false
local BLOCKSIZE = core.MAP_BLOCKSIZE local BLOCKSIZE = core.MAP_BLOCKSIZE
local floor = math.floor
local function get_blockpos(pos) local function get_blockpos(pos)
return { return {
x = math.floor(pos.x/BLOCKSIZE), x = floor(pos.x/BLOCKSIZE),
y = math.floor(pos.y/BLOCKSIZE), y = floor(pos.y/BLOCKSIZE),
z = math.floor(pos.z/BLOCKSIZE)} z = floor(pos.z/BLOCKSIZE)}
end end
-- When we create/free a forceload, it's either transient or persistent. We want -- When we create/free a forceload, it's either transient or persistent. We want

View File

@ -1,7 +1,7 @@
-- Minetest: builtin/item_entity.lua -- Minetest: builtin/item_entity.lua
local abs, min, floor, random, pi = math.abs, math.min, math.floor, math.random, math.pi local abs, min, floor, random, pi = math.abs, math.min, math.floor, math.random, math.pi
local vnormalize = vector.normalize local vadd, vnormalize = vector.add, vector.normalize
function core.spawn_item(pos, item) function core.spawn_item(pos, item)
-- Take item in any format -- Take item in any format
@ -276,7 +276,7 @@ core.register_entity(":__builtin:item", {
-- Check which one of the 4 sides is free -- Check which one of the 4 sides is free
for o = 1, #order do for o = 1, #order do
local cnode = core.get_node(vector.add(pos, order[o])).name local cnode = core.get_node(vadd(pos, order[o])).name
local cdef = core.registered_nodes[cnode] or {} local cdef = core.registered_nodes[cnode] or {}
if cnode ~= "ignore" and cdef.walkable == false then if cnode ~= "ignore" and cdef.walkable == false then
shootdir = order[o] shootdir = order[o]
@ -286,7 +286,7 @@ core.register_entity(":__builtin:item", {
-- If none of the 4 sides is free, check upwards -- If none of the 4 sides is free, check upwards
if not shootdir then if not shootdir then
shootdir = {x=0, y=1, z=0} shootdir = {x=0, y=1, z=0}
local cnode = core.get_node(vector.add(pos, shootdir)).name local cnode = core.get_node(vadd(pos, shootdir)).name
if cnode == "ignore" then if cnode == "ignore" then
shootdir = nil -- Do not push into ignore shootdir = nil -- Do not push into ignore
end end

View File

@ -1,4 +1,5 @@
-- can be overriden by mods -- can be overriden by mods
local exp = math.exp
function core.calculate_knockback(player, hitter, time_from_last_punch, tool_capabilities, dir, distance, damage) function core.calculate_knockback(player, hitter, time_from_last_punch, tool_capabilities, dir, distance, damage)
if damage == 0 or player:get_armor_groups().immortal then if damage == 0 or player:get_armor_groups().immortal then
return 0.0 return 0.0
@ -7,7 +8,7 @@ function core.calculate_knockback(player, hitter, time_from_last_punch, tool_cap
local m = 8 local m = 8
-- solve m - m*e^(k*4) = 4 for k -- solve m - m*e^(k*4) = 4 for k
local k = -0.17328 local k = -0.17328
local res = m - m * math.exp(k * damage) local res = m - m * exp(k * damage)
if distance < 2.0 then if distance < 2.0 then
res = res * 1.1 -- more knockback when closer res = res * 1.1 -- more knockback when closer
@ -22,6 +23,7 @@ local function vector_absmax(v)
return max(max(abs(v.x), abs(v.y)), abs(v.z)) return max(max(abs(v.x), abs(v.y)), abs(v.z))
end end
local vdivide, vlength, vsubtract = vector.divide, vector.length, vector.subtract
core.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, unused_dir, damage) core.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, unused_dir, damage)
if player:get_hp() == 0 then if player:get_hp() == 0 then
return -- RIP return -- RIP
@ -29,10 +31,10 @@ core.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool
-- Server::handleCommand_Interact() adds eye offset to one but not the other -- Server::handleCommand_Interact() adds eye offset to one but not the other
-- so the direction is slightly off, calculate it ourselves -- so the direction is slightly off, calculate it ourselves
local dir = vector.subtract(player:get_pos(), hitter:get_pos()) local dir = vsubtract(player:get_pos(), hitter:get_pos())
local d = vector.length(dir) local d = vlength(dir)
if d ~= 0.0 then if d ~= 0.0 then
dir = vector.divide(dir, d) dir = vdivide(dir, d)
end end
local k = core.calculate_knockback(player, hitter, time_from_last_punch, tool_capabilities, dir, d, damage) local k = core.calculate_knockback(player, hitter, time_from_last_punch, tool_capabilities, dir, d, damage)

View File

@ -1,5 +1,7 @@
-- Minetest: builtin/misc.lua -- Minetest: builtin/misc.lua
local ceil, floor = math.ceil, math.floor
-- --
-- Misc. API functions -- Misc. API functions
-- --
@ -139,9 +141,9 @@ end
function core.get_position_from_hash(hash) function core.get_position_from_hash(hash)
local pos = {} local pos = {}
pos.x = (hash % 65536) - 32768 pos.x = (hash % 65536) - 32768
hash = math.floor(hash / 65536) hash = floor(hash / 65536)
pos.y = (hash % 65536) - 32768 pos.y = (hash % 65536) - 32768
hash = math.floor(hash / 65536) hash = floor(hash / 65536)
pos.z = (hash % 65536) - 32768 pos.z = (hash % 65536) - 32768
return pos return pos
end end
@ -173,7 +175,7 @@ end
-- See l_env.cpp for the other functions -- See l_env.cpp for the other functions
function core.get_artificial_light(param1) function core.get_artificial_light(param1)
return math.floor(param1 / 16) return floor(param1 / 16)
end end
@ -220,18 +222,18 @@ function core.is_area_protected(minp, maxp, player_name, interval)
if maxp[c] > minp[c] then if maxp[c] > minp[c] then
d[c] = (maxp[c] - minp[c]) / d[c] = (maxp[c] - minp[c]) /
math.ceil((maxp[c] - minp[c]) / interval) - 1e-4 ceil((maxp[c] - minp[c]) / interval) - 1e-4
else else
d[c] = 1 -- Any value larger than 0 to avoid division by zero d[c] = 1 -- Any value larger than 0 to avoid division by zero
end end
end end
for zf = minp.z, maxp.z, d.z do for zf = minp.z, maxp.z, d.z do
local z = math.floor(zf + 0.5) local z = floor(zf + 0.5)
for yf = minp.y, maxp.y, d.y do for yf = minp.y, maxp.y, d.y do
local y = math.floor(yf + 0.5) local y = floor(yf + 0.5)
for xf = minp.x, maxp.x, d.x do for xf = minp.x, maxp.x, d.x do
local x = math.floor(xf + 0.5) local x = floor(xf + 0.5)
local pos = {x = x, y = y, z = z} local pos = {x = x, y = y, z = z}
if core.is_protected(pos, player_name) then if core.is_protected(pos, player_name) then
return pos return pos