Refactor default mod and get rid of many globals

master
Wuzzy 2017-01-27 13:45:21 +01:00
parent 8ee0184a1d
commit fd1845029e
5 changed files with 35 additions and 82 deletions

View File

@ -151,7 +151,7 @@ minetest.register_on_dignode(function(pos, node)
local i=1 local i=1
while timber_nodenames[i]~=nil do while timber_nodenames[i]~=nil do
if node.name==timber_nodenames[i] then if node.name==timber_nodenames[i] then
np={x=pos.x, y=pos.y+1, z=pos.z} local np={x=pos.x, y=pos.y+1, z=pos.z}
while minetest.get_node(np).name==timber_nodenames[i] do while minetest.get_node(np).name==timber_nodenames[i] do
minetest.remove_node(np) minetest.remove_node(np)
minetest.add_item(np, timber_nodenames[i]) minetest.add_item(np, timber_nodenames[i])
@ -166,14 +166,7 @@ end)
-- Flint and Steel -- Flint and Steel
-- --
function get_nodedef_field(nodename, fieldname) function default.set_fire(pointed_thing)
if not minetest.registered_nodes[nodename] then
return nil
end
return minetest.registered_nodes[nodename][fieldname]
end
function set_fire(pointed_thing)
local n = minetest.get_node(pointed_thing.above) local n = minetest.get_node(pointed_thing.above)
if n.name ~= "" and n.name == "air" and not minetest.is_protected(pointed_thing.above, "fire") then if n.name ~= "" and n.name == "air" and not minetest.is_protected(pointed_thing.above, "fire") then
minetest.add_node(pointed_thing.above, {name="fire:basic_flame"}) minetest.add_node(pointed_thing.above, {name="fire:basic_flame"})
@ -184,7 +177,7 @@ end
-- Fire Particles -- Fire Particles
-- --
function add_fire(pos) function default.add_fire(pos)
local null = {x=0, y=0, z=0} local null = {x=0, y=0, z=0}
pos.y = pos.y+0.19 pos.y = pos.y+0.19
minetest.add_particle(pos, null, null, 1.1, minetest.add_particle(pos, null, null, 1.1,
@ -202,7 +195,7 @@ local n
local n2 local n2
local pos local pos
function apple_leave() local function apple_leave()
if math.random(0, 10) == 3 then if math.random(0, 10) == 3 then
return {name = "default:apple"} return {name = "default:apple"}
else else
@ -210,7 +203,7 @@ function apple_leave()
end end
end end
function air_leave() local function air_leave()
if math.random(0, 50) == 3 then if math.random(0, 50) == 3 then
return {name = "air"} return {name = "air"}
else else
@ -218,7 +211,7 @@ function air_leave()
end end
end end
function generate_tree(pos, trunk, leaves, typearbre) local function generate_tree(pos, trunk, leaves, typearbre)
pos.y = pos.y-1 pos.y = pos.y-1
local nodename = minetest.get_node(pos).name local nodename = minetest.get_node(pos).name
@ -226,6 +219,7 @@ function generate_tree(pos, trunk, leaves, typearbre)
if not minetest.get_node_light(pos) then if not minetest.get_node_light(pos) then
return return
end end
local node
if typearbre == nil or typearbre == 1 then if typearbre == nil or typearbre == 1 then
node = {name = ""} node = {name = ""}
for dy=1,4 do for dy=1,4 do
@ -460,7 +454,7 @@ end
end) end)
function duengen(pointed_thing) function default.duengen(pointed_thing)
pos = pointed_thing.under pos = pointed_thing.under
n = minetest.get_node(pos) n = minetest.get_node(pos)
if n.name == "" then return false end if n.name == "" then return false end
@ -630,54 +624,13 @@ minetest.register_abm({
local newpos = {x=pos.x, y=pos.y-1, z=pos.z} local newpos = {x=pos.x, y=pos.y-1, z=pos.z}
local n = minetest.get_node(newpos) local n = minetest.get_node(newpos)
if n.name == "air" then if n.name == "air" then
walldir = node.param2 local walldir = node.param2
minetest.add_node(newpos, {name = "default:vine", param2 = walldir}) minetest.add_node(newpos, {name = "default:vine", param2 = walldir})
end end
end end
}) })
-- Global environment step function
function on_step(dtime)
-- print("on_step")
end
minetest.register_globalstep(on_step)
function on_placenode(p, node)
--print("on_placenode")
end
minetest.register_on_placenode(on_placenode)
function on_dignode(p, node)
--print("on_dignode")
end
minetest.register_on_dignode(on_dignode)
function on_punchnode(p, node)
end
minetest.register_on_punchnode(on_punchnode)
-- END
-- Support old code
function default.spawn_falling_node(p, nodename)
spawn_falling_node(p, nodename)
end
-- Horrible crap to support old code
-- Don't use this and never do what this does, it's completely wrong!
-- (More specifically, the client and the C++ code doesn't get the group)
function default.register_falling_node(nodename, texture)
minetest.log("error", debug.traceback())
minetest.log('error', "WARNING: default.register_falling_node is deprecated")
if minetest.registered_nodes[nodename] then
minetest.registered_nodes[nodename].groups.falling_node = 1
end
end
--Sounds
-- --
-- Sounds -- Sounds
-- --
@ -864,7 +817,7 @@ minetest.register_abm({
------------------------ ------------------------
-- Create Color Glass -- -- Create Color Glass --
------------------------ ------------------------
function AddGlass(desc, recipeitem, color) function default.add_glass(desc, recipeitem, color)
minetest.register_node("default:glass_"..color, { minetest.register_node("default:glass_"..color, {
description = desc, description = desc,

View File

@ -2,10 +2,6 @@
-- See README.txt for licensing and other information. -- See README.txt for licensing and other information.
-- The API documentation in here was moved into doc/lua_api.txt -- The API documentation in here was moved into doc/lua_api.txt
WATER_ALPHA = 160
WATER_VISC = 1
LAVA_VISC = 7
-- Definitions made by this mod that other mods can use too -- Definitions made by this mod that other mods can use too
default = {} default = {}
default.gui_slots = "listcolors[#9990;#FFF7;#FFF0;#000;#FFF]" default.gui_slots = "listcolors[#9990;#FFF7;#FFF0;#000;#FFF]"

View File

@ -1,5 +1,9 @@
-- mods/default/nodes.lua -- mods/default/nodes.lua
local WATER_ALPHA = 160
local WATER_VISC = 1
local LAVA_VISC = 7
-- --
-- Node definitions -- Node definitions
-- --
@ -1025,22 +1029,22 @@ minetest.register_node("default:glass", {
}) })
---- colored glass ---- colored glass
AddGlass( "Red Stained Glass", "basecolor_red", "red") default.add_glass( "Red Stained Glass", "basecolor_red", "red")
AddGlass( "Green Stained Glass", "unicolor_dark_green", "green") default.add_glass( "Green Stained Glass", "unicolor_dark_green", "green")
AddGlass( "Blue Stained Glass", "basecolor_blue", "blue") default.add_glass( "Blue Stained Glass", "basecolor_blue", "blue")
AddGlass( "Light Blue Stained Glass", "unicolor_light_blue", "light_blue") default.add_glass( "Light Blue Stained Glass", "unicolor_light_blue", "light_blue")
AddGlass( "Black Stained Glass", "basecolor_black", "black") default.add_glass( "Black Stained Glass", "basecolor_black", "black")
AddGlass( "White Stained Glass", "basecolor_white", "white") default.add_glass( "White Stained Glass", "basecolor_white", "white")
AddGlass( "Yellow Stained Glass", "basecolor_yellow", "yellow") default.add_glass( "Yellow Stained Glass", "basecolor_yellow", "yellow")
AddGlass( "Brown Stained Glass", "unicolor_dark_orange", "brown") default.add_glass( "Brown Stained Glass", "unicolor_dark_orange", "brown")
AddGlass( "Orange Stained Glass", "excolor_orange", "orange") default.add_glass( "Orange Stained Glass", "excolor_orange", "orange")
AddGlass( "Pink Stained Glass", "unicolor_light_red", "pink") default.add_glass( "Pink Stained Glass", "unicolor_light_red", "pink")
AddGlass( "Gray Stained Glass", "unicolor_darkgrey", "gray") default.add_glass( "Gray Stained Glass", "unicolor_darkgrey", "gray")
AddGlass( "Lime Stained Glass", "basecolor_green", "lime") default.add_glass( "Lime Stained Glass", "basecolor_green", "lime")
AddGlass( "Light Gray Stained Glass", "basecolor_grey", "silver") default.add_glass( "Light Gray Stained Glass", "basecolor_grey", "silver")
AddGlass( "Magenta Stained Glass", "basecolor_magenta", "magenta") default.add_glass( "Magenta Stained Glass", "basecolor_magenta", "magenta")
AddGlass( "Purple Stained Glass", "excolor_violet", "purple") default.add_glass( "Purple Stained Glass", "excolor_violet", "purple")
AddGlass( "Cyan Stained Glass", "basecolor_cyan", "cyan") default.add_glass( "Cyan Stained Glass", "basecolor_cyan", "cyan")
minetest.register_node("default:rail", { minetest.register_node("default:rail", {
description = "Rail", description = "Rail",
@ -1498,7 +1502,7 @@ minetest.register_node("default:sponge", {
on_water = true on_water = true
end end
end end
local p, n
if on_water == true then if on_water == true then
for i=-3,3 do for i=-3,3 do
for j=-3,3 do for j=-3,3 do
@ -1514,8 +1518,8 @@ minetest.register_node("default:sponge", {
end end
end end
end end
local p = {x=pos.x, y=pos.y, z=pos.z} p = {x=pos.x, y=pos.y, z=pos.z}
local n = minetest.get_node(p) n = minetest.get_node(p)
if change == true then if change == true then
minetest.add_node(pointed_thing.above, {name = "default:sponge_wet"}) minetest.add_node(pointed_thing.above, {name = "default:sponge_wet"})
else else

View File

@ -331,7 +331,7 @@ minetest.register_tool("default:flint_and_steel", {
if minetest.get_node(pointed_thing.under).name == "mcl_tnt:tnt" then if minetest.get_node(pointed_thing.under).name == "mcl_tnt:tnt" then
tnt.ignite(pointed_thing.under) tnt.ignite(pointed_thing.under)
else else
set_fire(pointed_thing) default.set_fire(pointed_thing)
itemstack:add_wear(66000/65) -- 65 uses itemstack:add_wear(66000/65) -- 65 uses
return itemstack return itemstack
end end

View File

@ -102,7 +102,7 @@ minetest.register_craftitem("dye:white", {
stack_max = 64, stack_max = 64,
groups = {dye=1, craftitem=1, basecolor_white=1, excolor_white=1, unicolor_white=1}, groups = {dye=1, craftitem=1, basecolor_white=1, excolor_white=1, unicolor_white=1},
on_place = function(itemstack, user, pointed_thing) on_place = function(itemstack, user, pointed_thing)
if(duengen(pointed_thing)) then if(default.duengen(pointed_thing)) then
itemstack:take_item() itemstack:take_item()
end end
return itemstack return itemstack