Skip to content

Commit

Permalink
Make all functions local
Browse files Browse the repository at this point in the history
  • Loading branch information
bdjnk committed Sep 25, 2015
1 parent 97f7f4a commit 30f9cf3
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions init.lua
Expand Up @@ -35,7 +35,40 @@ minetest.register_node("glow:cave_worms", {
on_place = minetest.rotate_node,
})

function make_worms(pos)
local function near_surface(pos)
for dx = -1, 1, 1 do
for dy = -1, 1, 1 do
for dz = -1, 1, 1 do
local dpos = { x=pos.x+dx, y=pos.y+dy, z=pos.z+dz }
local light = minetest.get_node_light(dpos, 0.5) -- 0.5 means noon
if light and light > 5 then
return true
end
end
end
end
return false
end

local function place_worms(pos)
local axes = {
{ x=pos.x, y=pos.y-1, z=pos.z },
{ x=pos.x, y=pos.y, z=pos.z-1 },
{ x=pos.x, y=pos.y, z=pos.z+1 },
{ x=pos.x-1, y=pos.y, z=pos.z },
{ x=pos.x+1, y=pos.y, z=pos.z },
{ x=pos.x, y=pos.y+1, z=pos.z },
}
for i, cpos in ipairs(axes) do
if minetest.get_node(cpos).name == "default:stone" then
local facedir = (i-1) * 4 + math.random(0, 3) -- see 6d facedir info
minetest.set_node(pos, { name = "glow:cave_worms", param2 = facedir })
return
end
end
end

local function make_worms(pos)
local spot = minetest.find_node_near(pos, 1, "air")
if not spot or near_surface(spot) then
return
Expand Down Expand Up @@ -86,39 +119,6 @@ minetest.register_abm({
end,
})

function place_worms(pos)
local axes = {
{ x=pos.x, y=pos.y-1, z=pos.z },
{ x=pos.x, y=pos.y, z=pos.z-1 },
{ x=pos.x, y=pos.y, z=pos.z+1 },
{ x=pos.x-1, y=pos.y, z=pos.z },
{ x=pos.x+1, y=pos.y, z=pos.z },
{ x=pos.x, y=pos.y+1, z=pos.z },
}
for i, cpos in ipairs(axes) do
if minetest.get_node(cpos).name == "default:stone" then
local facedir = (i-1) * 4 + math.random(0, 3) -- see 6d facedir info
minetest.set_node(pos, { name = "glow:cave_worms", param2 = facedir })
return
end
end
end

function near_surface(pos)
for dx = -1, 1, 1 do
for dy = -1, 1, 1 do
for dz = -1, 1, 1 do
local dpos = { x=pos.x+dx, y=pos.y+dy, z=pos.z+dz }
local light = minetest.get_node_light(dpos, 0.5) -- 0.5 means noon
if light and light > 5 then
return true
end
end
end
end
return false
end

--[[
function is_facing(pos, nodename)
for d = -1, 1, 2 do
Expand Down Expand Up @@ -177,6 +177,28 @@ minetest.register_node("glow:shrooms", {
},
})

local function add_shrooms(pos)
if minetest.find_node_near(pos, 2, "glow:shrooms") then
return
end
for nx = -1, 1, 2 do
for nz = -1, 1, 2 do
for ny = 1, -1, -1 do
if math.random() < 0.2 then
local p = { x=pos.x+nx, y=pos.y-1+ny, z=pos.z+nz }
if minetest.get_item_group(minetest.get_node(p).name, "soil") ~= 0 then
p.y = p.y+1
if minetest.get_node(p).name == "air" then
minetest.set_node(p, { name = "glow:shrooms" })
end
break
end
end
end
end
end
end

minetest.register_on_generated(function(minp, maxp, seed)
for _,pos in pairs(minetest.find_nodes_in_area(minp, maxp, "default:tree")) do
if math.random() < 0.2
Expand Down Expand Up @@ -221,28 +243,6 @@ minetest.register_abm({
end,
})

function add_shrooms(pos)
if minetest.find_node_near(pos, 2, "glow:shrooms") then
return
end
for nx = -1, 1, 2 do
for nz = -1, 1, 2 do
for ny = 1, -1, -1 do
if math.random() < 0.2 then
local p = { x=pos.x+nx, y=pos.y-1+ny, z=pos.z+nz }
if minetest.get_item_group(minetest.get_node(p).name, "soil") ~= 0 then
p.y = p.y+1
if minetest.get_node(p).name == "air" then
minetest.set_node(p, { name = "glow:shrooms" })
end
break
end
end
end
end
end
end


-- FIREFLIES ----------------------------------------------

Expand Down

3 comments on commit 30f9cf3

@bdjnk
Copy link
Owner Author

@bdjnk bdjnk commented on 30f9cf3 Sep 25, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Evidently, Lua doesn't do hoisting, so I've rearranged the functions.

@HybridDog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to rearrange the functions, just write
local near_surface, add_shrooms, make_worms
on the beginning of the file and these are accessible and localized everywhere under this line.

function i() does the same as i = function()
local function i() does the same as local i = function()

@bdjnk
Copy link
Owner Author

@bdjnk bdjnk commented on 30f9cf3 Sep 25, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The reason I rearranged was so the function variables are declared and defined in a single location. This seems cleaner and clearer to me (and my OCD).

Please sign in to comment.