Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize by localising math functions #83

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mapgen_dungeons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ local c_lava_source = minetest.get_content_id("default:lava_source")
-- Misc math functions

-- avoid needing table lookups each time a common math function is invoked
local math_max, math_min = math.max, math.min
local math_max, math_min, math_floor = math.max, math.min, math.floor


-- Dungeon excavation functions
Expand Down Expand Up @@ -275,14 +275,14 @@ nether.mapgen.decorate_dungeons = function(data, area, rooms)

local pillar_vi = {}
local pillarHeight = 0
local wallDist = 1 + math.floor((roomWidth + roomLength) / 14)
local wallDist = 1 + math_floor((roomWidth + roomLength) / 14)

local roomHeight = room_max.y - room_min.y
if roomHeight >= 7 then
-- mezzanine floor
local mezzMax = {
x = room_min.x + math.floor(roomWidth / 7 * 4),
y = room_min.y + math.floor(roomHeight / 5 * 3),
x = room_min.x + math_floor(roomWidth / 7 * 4),
y = room_min.y + math_floor(roomHeight / 5 * 3),
z = room_max.z
}

Expand Down
16 changes: 8 additions & 8 deletions mapgen_geodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ local c_crystal = minetest.get_content_id("nether:geodelite") -- geodel
local c_netherrack = minetest.get_content_id("nether:rack")
local c_glowstone = minetest.get_content_id("nether:glowstone")

-- Math funcs
local math_max, math_min, math_abs, math_floor, math_pi = math.max, math.min, math.abs, math.floor, math.pi -- avoid needing table lookups each time a common math function is invoked

-- Math funcs (avoid needing table lookups each time a common math function is invoked)
local math_max, math_min, math_abs, math_floor, math_pi = math.max, math.min, math.abs, math.floor, math.pi
local math_cos, math_sin = math.cos, math.sin

-- Create a tiling space of close-packed spheres, using Hexagonal close packing
-- of spheres with radius 0.5.
Expand Down Expand Up @@ -141,15 +141,15 @@ mapgen.getGeodeInteriorNodeId = function(x, y, z)
lastz = z
-- Calculate structure warping
-- To avoid calculating this for each node there's no warping as you look along the x axis :(
adj_y = math.sin(math_pi / 222 * y) * 30
adj_y = math_sin(math_pi / 222 * y) * 30

if y ~= lasty then
lasty = y
warpx = math.sin(math_pi / 100 * y) * 10
warpz = math.sin(math_pi / 43 * y) * 15
warpx = math_sin(math_pi / 100 * y) * 10
warpz = math_sin(math_pi / 43 * y) * 15
end
local twistRadians = math_pi / 73 * y
local sinTwist, cosTwist = math.sin(twistRadians), math.cos(twistRadians)
local sinTwist, cosTwist = math_sin(twistRadians), math_cos(twistRadians)
adj_x = cosTwist * warpx - sinTwist * warpz
adj_z = sinTwist * warpx + cosTwist * warpz
end
Expand Down Expand Up @@ -218,4 +218,4 @@ mapgen.getGeodeInteriorNodeId = function(x, y, z)
else
return c_air
end
end
end
17 changes: 8 additions & 9 deletions mapgen_mantle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ local c_lava_crust = minetest.get_content_id("nether:lava_crust")
local c_basalt = minetest.get_content_id("nether:basalt")


-- Math funcs
local math_max, math_min, math_abs, math_floor = math.max, math.min, math.abs, math.floor -- avoid needing table lookups each time a common math function is invoked
-- Math funcs (avoid needing table lookups each time a common math function is invoked)
local math_max, math_min, math_abs, math_floor = math.max, math.min, math.abs, math.floor
local math_random, math_randomseed = math.random, math.randomseed

function random_unit_vector()
return vector.normalize({
x = math.random() - 0.5,
y = math.random() - 0.5,
z = math.random() - 0.5
x = math_random() - 0.5,
y = math_random() - 0.5,
z = math_random() - 0.5
})
end

Expand All @@ -88,7 +89,7 @@ mapgen.find_nearest_lava_sealevel = function(y)
-- todo: put oceans near the bottom of chunks to improve ability to generate tunnels to the center
-- todo: constrain y to be not near the bounds of the nether
-- todo: add some random adj at each level, seeded only by the level height
local sealevel = math.floor((y + 100) / 200) * 200
local sealevel = math_floor((y + 100) / 200) * 200
--local sealevel = math.floor((y + 80) / 160) * 160
--local sealevel = math.floor((y + 120) / 240) * 240

Expand All @@ -105,8 +106,6 @@ mapgen.find_nearest_lava_sealevel = function(y)
end




mapgen.add_basalt_columns = function(data, area, minp, maxp)
-- Basalt columns are structures found in lava oceans, and the only way to obtain
-- nether basalt.
Expand Down Expand Up @@ -230,7 +229,7 @@ function excavate_pathway(data, area, nether_pos, center_pos, minp, maxp)
local ystride = area.ystride
local zstride = area.zstride

math.randomseed(nether_pos.x + 10 * nether_pos.y + 100 * nether_pos.z) -- so each tunnel generates deterministically (this doesn't have to be a quality seed)
math_randomseed(nether_pos.x + 10 * nether_pos.y + 100 * nether_pos.z) -- so each tunnel generates deterministically (this doesn't have to be a quality seed)
local dist = math_floor(vector.distance(nether_pos, center_pos))
local waypoints = generate_waypoints(nether_pos, center_pos, minp, maxp)

Expand Down
12 changes: 7 additions & 5 deletions mapgen_nobiomes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ local yblmin = NETHER_FLOOR + BLEND * 2
local yblmax = NETHER_CEILING - BLEND * 2



-- Mapgen

dofile(nether.path .. "/mapgen_decorations.lua")
Expand Down Expand Up @@ -97,6 +96,9 @@ local c_netherbrick = minetest.get_content_id("nether:brick")
local c_netherrack = minetest.get_content_id("nether:rack")


-- Math functions
local math_min, math_max = math.min, math.max

-- On-generated function

minetest.register_on_generated(function(minp, maxp, seed)
Expand All @@ -105,10 +107,10 @@ minetest.register_on_generated(function(minp, maxp, seed)
end

local x1 = maxp.x
local y1 = math.min(maxp.y, NETHER_CEILING)
local y1 = math_min(maxp.y, NETHER_CEILING)
local z1 = maxp.z
local x0 = minp.x
local y0 = math.max(minp.y, NETHER_FLOOR)
local y0 = math_max(minp.y, NETHER_FLOOR)
local z0 = minp.z

local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
Expand Down Expand Up @@ -215,7 +217,7 @@ function nether.find_nether_ground_y(target_x, target_z, start_y, player_name)
local minp = {x = minp_schem.x, y = 0, z = minp_schem.z}
local maxp = {x = maxp_schem.x, y = 0, z = maxp_schem.z}

for y = start_y, math.max(NETHER_FLOOR + BLEND, start_y - 4096), -1 do
for y = start_y, math_max(NETHER_FLOOR + BLEND, start_y - 4096), -1 do
local nval_cave = nobj_cave_point:get_3d({x = target_x, y = y, z = target_z})

if nval_cave > TCAVE then -- Cavern
Expand All @@ -237,5 +239,5 @@ function nether.find_nether_ground_y(target_x, target_z, start_y, player_name)
end
end

return math.max(start_y, NETHER_FLOOR + BLEND) -- Fallback
return math_max(start_y, NETHER_FLOOR + BLEND) -- Fallback
end
30 changes: 17 additions & 13 deletions portal_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ nether.portal_destination_not_found_message =
S("Mysterious forces prevented you from opening that portal. Please try another location")


-- Math functions
local math_floor, math_min, math_max, math_sin, math_cos = math.floor, math.min, math.max, math.sin, math.cos
local math_random, math_hypot = math.random, math.hypot

--[[

Positions
Expand Down Expand Up @@ -691,9 +695,9 @@ local function get_timerPos_from_p1_and_p2(p1, p2)
-- and if someone want to make a circular portal then that positon will still likely be part
-- of the frame.
return {
x = math.floor((p1.x + p2.x) / 2),
x = math_floor((p1.x + p2.x) / 2),
y = p1.y,
z = math.floor((p1.z + p2.z) / 2),
z = math_floor((p1.z + p2.z) / 2),
}
end

Expand All @@ -704,7 +708,7 @@ local function get_colorfacedir_from_color_and_orientation(color, orientation, p
assert(orientation, "no orientation passed")

local axis_direction, rotation
local dir = math.floor((orientation % 360) / 90 + 0.5)
local dir = math_floor((orientation % 360) / 90 + 0.5)

-- if the portal is vertical then node axis direction will be +Y (up) and portal orientation
-- will set the node's rotation.
Expand All @@ -715,7 +719,7 @@ local function get_colorfacedir_from_color_and_orientation(color, orientation, p
if dir == 1 then axis_direction = 3 end -- East
if dir == 2 then axis_direction = 2 end -- South
if dir == 3 then axis_direction = 4 end -- West
rotation = math.floor(axis_direction / 2); -- a rotation is only needed if axis_direction is east or west
rotation = math_floor(axis_direction / 2); -- a rotation is only needed if axis_direction is east or west
else
axis_direction = 0 -- 0 is up, or +Y
rotation = dir
Expand Down Expand Up @@ -875,7 +879,7 @@ local function list_closest_portals(portal_definition, anchorPos, distance_limit
local x = anchorPos.x - found_anchorPos.x
local y = anchorPos.y - found_anchorPos.y
local z = anchorPos.z - found_anchorPos.z
local distance = math.hypot(y * y_factor, math.hypot(x, z))
local distance = math_hypot(y * y_factor, math_hypot(x, z))
if distance <= distance_limit or distance_limit < 0 then
local info = minetest.deserialize(value) or {}
debugf("found %s listed at distance %.2f (within %.2f) from dest %s, found: %s orientation %s", found_name, distance, distance_limit, anchorPos, found_anchorPos, info.orientation)
Expand Down Expand Up @@ -1176,7 +1180,7 @@ local function set_schematic_param2(schematic_table, frame_node_name, frame_node
for _, node in ipairs(schematic_table.facedirNodes) do
if isFacedir and node.facedir ~= nil then
-- frame_node_color can be nil
local colorBits = (frame_node_color or math.floor((node.param2 or 0) / 32)) * 32
local colorBits = (frame_node_color or math_floor((node.param2 or 0) / 32)) * 32
node.param2 = node.facedir + colorBits
else
node.param2 = 0
Expand Down Expand Up @@ -1465,9 +1469,9 @@ local function ensure_remote_portal_then_teleport(playerName, portal_definition,
end

-- rotate the player if the destination portal is a different orientation
local rotation_angle = math.rad(destination_orientation - local_orientation)
local rotation_angle = math_rad(destination_orientation - local_orientation)
local offset = vector.subtract(playerPos, local_wormholePos) -- preserve player's position in the portal
local rotated_offset = {x = math.cos(rotation_angle) * offset.x - math.sin(rotation_angle) * offset.z, y = offset.y, z = math.sin(rotation_angle) * offset.x + math.cos(rotation_angle) * offset.z}
local rotated_offset = {x = math.cos(rotation_angle) * offset.x - math_sin(rotation_angle) * offset.z, y = offset.y, z = math_sin(rotation_angle) * offset.x + math_cos(rotation_angle) * offset.z}
local new_playerPos = vector.add(destination_wormholePos, rotated_offset)
player:set_pos(new_playerPos)
player:set_look_horizontal(player:get_look_horizontal() + rotation_angle)
Expand Down Expand Up @@ -1528,7 +1532,7 @@ function run_wormhole(timerPos, time_elapsed)

local run_wormhole_node_func = function(pos)

if math.random(2) == 1 then -- lets run only 3 particlespawners instead of 6 per portal
if math_random(2) == 1 then -- lets run only 3 particlespawners instead of 6 per portal
minetest.add_particlespawner({
amount = 16,
time = 2,
Expand Down Expand Up @@ -1621,7 +1625,7 @@ local function create_book(item_name, inventory_description, inventory_image, ti
local display_book = function(itemstack, user, pointed_thing)
local player_name = user:get_player_name()

minetest.sound_play("nether_book_open", {to_player = player_name, gain = 0.25})
minetest.sound_play("nether_book_open", {to_player = player_name, gain = 0.25}, true)

local formspec =
"size[18,12.122]" ..
Expand All @@ -1641,7 +1645,7 @@ local function create_book(item_name, inventory_description, inventory_image, ti
local width = 7.9
local height = 12.0
local item_number = i
local items_on_page = math.floor(#chapters / 2)
local items_on_page = math_floor(#chapters / 2)
if i > items_on_page then
-- page 2
left_margin = 10.1
Expand Down Expand Up @@ -2262,8 +2266,8 @@ function nether.get_schematic_volume(anchor_pos, orientation, portal_name)
-- schematic then we will also need to check orientations 3 and 4.
-- (The currently existing portal-shapes are not affected)
return
{x = math.min(minp0.x, minp1.x), y = math.min(minp0.y, minp1.y), z = math.min(minp0.z, minp1.z)},
{x = math.max(maxp0.x, maxp1.x), y = math.max(maxp0.y, maxp1.y), z = math.max(maxp0.z, maxp1.z)}
{x = math_min(minp0.x, minp1.x), y = math_min(minp0.y, minp1.y), z = math_min(minp0.z, minp1.z)},
{x = math_max(maxp0.x, maxp1.x), y = math_max(maxp0.y, maxp1.y), z = math_max(maxp0.z, maxp1.z)}
end

-- Assume the largest possible portal shape unless we know it's a smaller one.
Expand Down
4 changes: 3 additions & 1 deletion tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

local S = nether.get_translator

local math_floor = math.floor -- localise math function

minetest.register_tool("nether:pick_nether", {
description = S("Nether Pickaxe\nWell suited for mining netherrack"),
_doc_items_longdesc = S("Uniquely suited for mining netherrack, with minimal wear when doing so. Blunts quickly on other materials."),
Expand All @@ -43,7 +45,7 @@ minetest.register_tool("nether:pick_nether", {
wearDivisor = 1 + (3 * workable) -- 10 for netherrack, 1 otherwise. Making it able to mine 350 netherrack nodes, instead of 35.
end

local wear = math.floor(digparams.wear / wearDivisor)
local wear = math_floor(digparams.wear / wearDivisor)
itemstack:add_wear(wear) -- apply the adjusted wear as usual
return itemstack
end
Expand Down