1
0
Fork 0

Builtin: localize a few functions

merge-requests/1/head
Maksym H 2023-04-29 01:13:10 +03:00 committed by mckaygerhard
parent 4237754178
commit 6156f4b0a8
5 changed files with 26 additions and 20 deletions

View File

@ -1,7 +1,8 @@
-- Minetest: builtin/item.lua
local random, pi = math.random, math.pi
local vnew, vround, vadd, vapply = vector.new, vector.round, vector.add, vector.apply
local abs, random, pi = math.abs, math.random, math.pi
local vnew, vround, vadd, vapply, vsubtract =
vector.new, vector.round, vector.add, vector.apply, vector.subtract
local builtin_shared = ...
local SCALE = 0.667
@ -369,7 +370,7 @@ core.register_entity(":__builtin:falling_node", {
local failure = false
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
-- We're colliding with some part of a node that's sticking out
-- 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 BLOCKSIZE = core.MAP_BLOCKSIZE
local floor = math.floor
local function get_blockpos(pos)
return {
x = math.floor(pos.x/BLOCKSIZE),
y = math.floor(pos.y/BLOCKSIZE),
z = math.floor(pos.z/BLOCKSIZE)}
x = floor(pos.x/BLOCKSIZE),
y = floor(pos.y/BLOCKSIZE),
z = floor(pos.z/BLOCKSIZE)}
end
-- 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
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)
-- Take item in any format
@ -276,7 +276,7 @@ core.register_entity(":__builtin:item", {
-- Check which one of the 4 sides is free
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 {}
if cnode ~= "ignore" and cdef.walkable == false then
shootdir = order[o]
@ -286,7 +286,7 @@ core.register_entity(":__builtin:item", {
-- If none of the 4 sides is free, check upwards
if not shootdir then
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
shootdir = nil -- Do not push into ignore
end

View File

@ -1,4 +1,5 @@
-- can be overriden by mods
local exp = math.exp
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
return 0.0
@ -7,7 +8,7 @@ function core.calculate_knockback(player, hitter, time_from_last_punch, tool_cap
local m = 8
-- solve m - m*e^(k*4) = 4 for k
local k = -0.17328
local res = m - m * math.exp(k * damage)
local res = m - m * exp(k * damage)
if distance < 2.0 then
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))
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)
if player:get_hp() == 0 then
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
-- so the direction is slightly off, calculate it ourselves
local dir = vector.subtract(player:get_pos(), hitter:get_pos())
local d = vector.length(dir)
local dir = vsubtract(player:get_pos(), hitter:get_pos())
local d = vlength(dir)
if d ~= 0.0 then
dir = vector.divide(dir, d)
dir = vdivide(dir, d)
end
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
local ceil, floor = math.ceil, math.floor
--
-- Misc. API functions
--
@ -139,9 +141,9 @@ end
function core.get_position_from_hash(hash)
local pos = {}
pos.x = (hash % 65536) - 32768
hash = math.floor(hash / 65536)
hash = floor(hash / 65536)
pos.y = (hash % 65536) - 32768
hash = math.floor(hash / 65536)
hash = floor(hash / 65536)
pos.z = (hash % 65536) - 32768
return pos
end
@ -173,7 +175,7 @@ end
-- See l_env.cpp for the other functions
function core.get_artificial_light(param1)
return math.floor(param1 / 16)
return floor(param1 / 16)
end
@ -220,18 +222,18 @@ function core.is_area_protected(minp, maxp, player_name, interval)
if maxp[c] > minp[c] then
d[c] = (maxp[c] - minp[c]) /
math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
ceil((maxp[c] - minp[c]) / interval) - 1e-4
else
d[c] = 1 -- Any value larger than 0 to avoid division by zero
end
end
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
local y = math.floor(yf + 0.5)
local y = floor(yf + 0.5)
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}
if core.is_protected(pos, player_name) then
return pos