Browse Source

[cavestuff] Optimize math.* call functions

master
sys4 2 weeks ago
parent
commit
f661500cb3
2 changed files with 15 additions and 10 deletions
  1. +10
    -7
      cavestuff/mapgen.lua
  2. +5
    -3
      cavestuff/nodes.lua

+ 10
- 7
cavestuff/mapgen.lua View File

@@ -1,5 +1,8 @@
--Map Generation Stuff

local random = math.random
local floor = math.floor

minetest.register_on_generated(function(minp, maxp, seed)
if maxp.y >= 2 and minp.y <= 0 then
-- Generate pebbles
@@ -9,12 +12,12 @@ minetest.register_on_generated(function(minp, maxp, seed)
local divs = (maxp.x-minp.x)/divlen+1;
for divx=0,divs-1 do
for divz=0,divs-1 do
local x0 = minp.x + math.floor((divx+0)*divlen)
local z0 = minp.z + math.floor((divz+0)*divlen)
local x1 = minp.x + math.floor((divx+1)*divlen)
local z1 = minp.z + math.floor((divz+1)*divlen)
local x0 = minp.x + floor((divx+0)*divlen)
local z0 = minp.z + floor((divz+0)*divlen)
local x1 = minp.x + floor((divx+1)*divlen)
local z1 = minp.z + floor((divz+1)*divlen)
-- Determine pebble amount from perlin noise
local pebble_amount = math.floor(perlin1:get2d({x=x0, y=z0}) ^ 2 * 2)
local pebble_amount = floor(perlin1:get2d({x=x0, y=z0}) ^ 2 * 2)
-- Find random positions for pebbles based on this random
local pr = PseudoRandom(seed+1)
for i=0,pebble_amount do
@@ -38,9 +41,9 @@ minetest.register_on_generated(function(minp, maxp, seed)
nn = minetest.get_node({x=x,y=ground_y,z=z}).name
-- If desert sand, add dry shrub
if nn == "default:dirt_with_grass" then
minetest.swap_node(p,{name="cavestuff:pebble_"..pr:next(1,2), param2=math.random(0,3)})
minetest.swap_node(p,{name="cavestuff:pebble_"..pr:next(1,2), param2=random(0,3)})
elseif nn == "default:desert_sand" then
minetest.swap_node(p,{name="cavestuff:desert_pebble_"..pr:next(1,2), param2=math.random(0,3)})
minetest.swap_node(p,{name="cavestuff:desert_pebble_"..pr:next(1,2), param2=random(0,3)})
end
end
end


+ 5
- 3
cavestuff/nodes.lua View File

@@ -1,6 +1,8 @@
-- support for i18n
local S = minetest.get_translator("cavestuff")

local random = math.random

--Rocks

local cbox = {
@@ -20,7 +22,7 @@ minetest.register_node("cavestuff:pebble_1",{
collision_box = cbox,
on_place = function(itemstack, placer, pointed_thing)
-- place a random pebble node
local stack = ItemStack("cavestuff:pebble_"..math.random(1,2))
local stack = ItemStack("cavestuff:pebble_"..random(1,2))
local ret = minetest.item_place(stack, placer, pointed_thing)
return ItemStack("cavestuff:pebble_1 "..itemstack:get_count()-(1-ret:get_count()))
end,
@@ -53,7 +55,7 @@ minetest.register_node("cavestuff:desert_pebble_1",{
collision_box = cbox,
on_place = function(itemstack, placer, pointed_thing)
-- place a random pebble node
local stack = ItemStack("cavestuff:desert_pebble_"..math.random(1,2))
local stack = ItemStack("cavestuff:desert_pebble_"..random(1,2))
local ret = minetest.item_place(stack, placer, pointed_thing)
return ItemStack("cavestuff:desert_pebble_1 "..itemstack:get_count()-(1-ret:get_count()))
end,
@@ -97,7 +99,7 @@ minetest.register_node("cavestuff:stalactite_1",{
if minetest.get_node(pt.under).name=="default:stone"
and minetest.get_node({x=pt.under.x, y=pt.under.y-1, z=pt.under.z}).name=="air"
and minetest.get_node({x=pt.under.x, y=pt.under.y-2, z=pt.under.z}).name=="air" then
minetest.swap_node({x=pt.under.x, y=pt.under.y-1, z=pt.under.z}, {name="cavestuff:stalactite_"..math.random(1,3)})
minetest.swap_node({x=pt.under.x, y=pt.under.y-1, z=pt.under.z}, {name="cavestuff:stalactite_"..random(1,3)})
if not minetest.setting_getbool("creative_mode") then
itemstack:take_item()
end


Loading…
Cancel
Save