2011-11-30 16:52:02 -05:00
|
|
|
--
|
|
|
|
-- This file contains built-in stuff in Minetest implemented in Lua.
|
|
|
|
--
|
|
|
|
-- It is always loaded and executed after registration of the C API,
|
|
|
|
-- before loading and running any mods.
|
|
|
|
--
|
|
|
|
|
2012-03-11 09:41:53 -04:00
|
|
|
--
|
|
|
|
-- Override some Lua library functions
|
|
|
|
--
|
|
|
|
|
|
|
|
print = minetest.debug
|
|
|
|
|
|
|
|
--
|
|
|
|
--
|
|
|
|
--
|
|
|
|
|
2011-11-25 14:52:09 -05:00
|
|
|
function basic_dump2(o)
|
|
|
|
if type(o) == "number" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "string" then
|
|
|
|
return string.format("%q", o)
|
|
|
|
elseif type(o) == "boolean" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "function" then
|
|
|
|
return "<function>"
|
|
|
|
elseif type(o) == "userdata" then
|
|
|
|
return "<userdata>"
|
|
|
|
elseif type(o) == "nil" then
|
|
|
|
return "nil"
|
|
|
|
else
|
|
|
|
error("cannot dump a " .. type(o))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function dump2(o, name, dumped)
|
|
|
|
name = name or "_"
|
|
|
|
dumped = dumped or {}
|
|
|
|
io.write(name, " = ")
|
|
|
|
if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
|
|
|
|
or type(o) == "function" or type(o) == "nil"
|
|
|
|
or type(o) == "userdata" then
|
|
|
|
io.write(basic_dump2(o), "\n")
|
|
|
|
elseif type(o) == "table" then
|
|
|
|
if dumped[o] then
|
|
|
|
io.write(dumped[o], "\n")
|
|
|
|
else
|
|
|
|
dumped[o] = name
|
|
|
|
io.write("{}\n") -- new table
|
|
|
|
for k,v in pairs(o) do
|
|
|
|
local fieldname = string.format("%s[%s]", name, basic_dump2(k))
|
|
|
|
dump2(v, fieldname, dumped)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error("cannot dump a " .. type(o))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function dump(o, dumped)
|
|
|
|
dumped = dumped or {}
|
|
|
|
if type(o) == "number" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "string" then
|
|
|
|
return string.format("%q", o)
|
|
|
|
elseif type(o) == "table" then
|
|
|
|
if dumped[o] then
|
|
|
|
return "<circular reference>"
|
|
|
|
end
|
|
|
|
dumped[o] = true
|
|
|
|
local t = {}
|
|
|
|
for k,v in pairs(o) do
|
|
|
|
t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
|
|
|
|
end
|
|
|
|
return "{" .. table.concat(t, ", ") .. "}"
|
|
|
|
elseif type(o) == "boolean" then
|
|
|
|
return tostring(o)
|
|
|
|
elseif type(o) == "function" then
|
|
|
|
return "<function>"
|
|
|
|
elseif type(o) == "userdata" then
|
|
|
|
return "<userdata>"
|
|
|
|
elseif type(o) == "nil" then
|
|
|
|
return "nil"
|
|
|
|
else
|
|
|
|
error("cannot dump a " .. type(o))
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--
|
2012-01-12 00:10:39 -05:00
|
|
|
-- Item definition helpers
|
|
|
|
--
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
function minetest.inventorycube(img1, img2, img3)
|
2012-01-12 00:10:39 -05:00
|
|
|
img2 = img2 or img1
|
|
|
|
img3 = img3 or img1
|
|
|
|
return "[inventorycube"
|
|
|
|
.. "{" .. img1:gsub("%^", "&")
|
|
|
|
.. "{" .. img2:gsub("%^", "&")
|
|
|
|
.. "{" .. img3:gsub("%^", "&")
|
|
|
|
end
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
function minetest.pos_to_string(pos)
|
|
|
|
return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.get_pointed_thing_position(pointed_thing, above)
|
2012-01-12 00:10:39 -05:00
|
|
|
if pointed_thing.type == "node" then
|
|
|
|
if above then
|
|
|
|
-- The position where a node would be placed
|
|
|
|
return pointed_thing.above
|
|
|
|
else
|
|
|
|
-- The position where a node would be dug
|
|
|
|
return pointed_thing.under
|
|
|
|
end
|
|
|
|
elseif pointed_thing.type == "object" then
|
2012-02-28 12:55:24 -05:00
|
|
|
obj = pointed_thing.ref
|
2012-01-12 00:10:39 -05:00
|
|
|
if obj ~= nil then
|
|
|
|
return obj:getpos()
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
function minetest.dir_to_facedir(dir)
|
|
|
|
if math.abs(dir.x) > math.abs(dir.z) then
|
|
|
|
if dir.x < 0 then
|
|
|
|
return 3
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if dir.z < 0 then
|
|
|
|
return 2
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.dir_to_wallmounted(dir)
|
|
|
|
if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
|
|
|
|
if dir.y < 0 then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif math.abs(dir.x) > math.abs(dir.z) then
|
|
|
|
if dir.x < 0 then
|
|
|
|
return 3
|
|
|
|
else
|
|
|
|
return 2
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if dir.z < 0 then
|
|
|
|
return 5
|
|
|
|
else
|
|
|
|
return 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.get_node_drops(nodename, toolname)
|
|
|
|
local drop = ItemStack({name=nodename}):get_definition().drop
|
|
|
|
if drop == nil then
|
|
|
|
-- default drop
|
|
|
|
return {ItemStack({name=nodename})}
|
|
|
|
elseif type(drop) == "string" then
|
|
|
|
-- itemstring drop
|
|
|
|
return {ItemStack(drop)}
|
|
|
|
elseif drop.items == nil then
|
|
|
|
-- drop = {} to disable default drop
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Extended drop table
|
|
|
|
local got_items = {}
|
|
|
|
local got_count = 0
|
|
|
|
local _, item, tool
|
|
|
|
for _, item in ipairs(drop.items) do
|
|
|
|
local good_rarity = true
|
|
|
|
local good_tool = true
|
|
|
|
if item.rarity ~= nil then
|
|
|
|
good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
|
|
|
|
end
|
|
|
|
if item.tools ~= nil then
|
|
|
|
good_tool = false
|
|
|
|
for _, tool in ipairs(item.tools) do
|
|
|
|
if tool:sub(1, 1) == '~' then
|
|
|
|
good_tool = toolname:find(tool:sub(2)) ~= nil
|
|
|
|
else
|
|
|
|
good_tool = toolname == tool
|
|
|
|
end
|
|
|
|
if good_tool then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if good_rarity and good_tool then
|
|
|
|
got_count = got_count + 1
|
|
|
|
for _, add_item in ipairs(item.items) do
|
|
|
|
got_items[#got_items+1] = add_item
|
|
|
|
end
|
|
|
|
if drop.max_items ~= nil and got_count == drop.max_items then
|
|
|
|
break
|
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
end
|
|
|
|
end
|
2012-01-20 18:11:44 -05:00
|
|
|
return got_items
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.item_place_node(itemstack, placer, pointed_thing)
|
|
|
|
local item = itemstack:peek_item()
|
|
|
|
local def = itemstack:get_definition()
|
|
|
|
if def.type == "node" and pointed_thing.type == "node" then
|
|
|
|
local pos = pointed_thing.above
|
|
|
|
local oldnode = minetest.env:get_node(pos)
|
|
|
|
local olddef = ItemStack({name=oldnode.name}):get_definition()
|
|
|
|
|
|
|
|
if not olddef.buildable_to then
|
|
|
|
minetest.log("info", placer:get_player_name() .. " tried to place"
|
|
|
|
.. " node in invalid position " .. minetest.pos_to_string(pos)
|
|
|
|
.. ", replacing " .. oldnode.name)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.log("action", placer:get_player_name() .. " places node "
|
|
|
|
.. def.name .. " at " .. minetest.pos_to_string(pos))
|
|
|
|
|
|
|
|
local newnode = {name = def.name, param1 = 0, param2 = 0}
|
|
|
|
|
|
|
|
-- Calculate direction for wall mounted stuff like torches and signs
|
|
|
|
if def.paramtype2 == 'wallmounted' then
|
|
|
|
local under = pointed_thing.under
|
|
|
|
local above = pointed_thing.above
|
|
|
|
local dir = {x = under.x - above.x, y = under.y - above.y, z = under.z - above.z}
|
|
|
|
newnode.param2 = minetest.dir_to_wallmounted(dir)
|
|
|
|
-- Calculate the direction for furnaces and chests and stuff
|
|
|
|
elseif def.paramtype2 == 'facedir' then
|
|
|
|
local playerpos = placer:getpos()
|
|
|
|
local dir = {x = pos.x - playerpos.x, y = pos.y - playerpos.y, z = pos.z - playerpos.z}
|
|
|
|
newnode.param2 = minetest.dir_to_facedir(dir)
|
|
|
|
minetest.log("action", "facedir: " .. newnode.param2)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add node and update
|
|
|
|
minetest.env:add_node(pos, newnode)
|
|
|
|
|
|
|
|
-- Set metadata owner
|
|
|
|
if def.metadata_name ~= "" then
|
|
|
|
minetest.env:get_meta(pos):set_owner(placer:get_player_name())
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Run script hook
|
|
|
|
local _, callback
|
|
|
|
for _, callback in ipairs(minetest.registered_on_placenodes) do
|
|
|
|
callback(pos, newnode, placer)
|
|
|
|
end
|
|
|
|
|
|
|
|
itemstack:take_item()
|
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.item_place_object(itemstack, placer, pointed_thing)
|
|
|
|
local pos = minetest.get_pointed_thing_position(pointed_thing, true)
|
|
|
|
if pos ~= nil then
|
|
|
|
local item = itemstack:take_item()
|
|
|
|
minetest.env:add_item(pos, item)
|
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
function minetest.item_place(itemstack, placer, pointed_thing)
|
|
|
|
if itemstack:get_definition().type == "node" then
|
|
|
|
return minetest.item_place_node(itemstack, placer, pointed_thing)
|
|
|
|
else
|
|
|
|
return minetest.item_place_object(itemstack, placer, pointed_thing)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
function minetest.item_drop(itemstack, dropper, pos)
|
|
|
|
minetest.env:add_item(pos, itemstack)
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
function minetest.item_eat(hp_change, replace_with_item)
|
2012-01-12 00:10:39 -05:00
|
|
|
return function(itemstack, user, pointed_thing) -- closure
|
|
|
|
if itemstack:take_item() ~= nil then
|
|
|
|
user:set_hp(user:get_hp() + hp_change)
|
2012-01-20 18:11:44 -05:00
|
|
|
itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
|
2012-01-12 00:10:39 -05:00
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
function minetest.node_punch(pos, node, puncher)
|
|
|
|
-- Run script hook
|
|
|
|
local _, callback
|
|
|
|
for _, callback in ipairs(minetest.registered_on_punchnodes) do
|
|
|
|
callback(pos, node, puncher)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.node_dig(pos, node, digger)
|
|
|
|
minetest.debug("node_dig")
|
|
|
|
|
|
|
|
local def = ItemStack({name=node.name}):get_definition()
|
|
|
|
if not def.diggable then
|
|
|
|
minetest.debug("not diggable")
|
|
|
|
minetest.log("info", digger:get_player_name() .. " tried to dig "
|
|
|
|
.. node.name .. " which is not diggable "
|
|
|
|
.. minetest.pos_to_string(pos))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local meta = minetest.env:get_meta(pos)
|
|
|
|
if meta ~= nil and not meta:get_allow_removal() then
|
|
|
|
minetest.debug("dig prevented by metadata")
|
|
|
|
minetest.log("info", digger:get_player_name() .. " tried to dig "
|
|
|
|
.. node.name .. ", but removal is disabled by metadata "
|
|
|
|
.. minetest.pos_to_string(pos))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.log('action', digger:get_player_name() .. " digs "
|
|
|
|
.. node.name .. " at " .. minetest.pos_to_string(pos))
|
|
|
|
|
|
|
|
if not minetest.setting_getbool("creative_mode") then
|
|
|
|
local wielded = digger:get_wielded_item()
|
|
|
|
local drops = minetest.get_node_drops(node.name, wielded:get_name())
|
|
|
|
|
|
|
|
-- Wear out tool
|
2012-02-28 12:45:23 -05:00
|
|
|
tp = wielded:get_tool_capabilities()
|
|
|
|
dp = minetest.get_dig_params(def.groups, tp)
|
2012-01-20 18:11:44 -05:00
|
|
|
wielded:add_wear(dp.wear)
|
|
|
|
digger:set_wielded_item(wielded)
|
|
|
|
|
|
|
|
-- Add dropped items
|
|
|
|
local _, dropped_item
|
|
|
|
for _, dropped_item in ipairs(drops) do
|
|
|
|
digger:get_inventory():add_item("main", dropped_item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove node and update
|
|
|
|
minetest.env:remove_node(pos)
|
|
|
|
|
|
|
|
-- Run script hook
|
|
|
|
local _, callback
|
|
|
|
for _, callback in ipairs(minetest.registered_on_dignodes) do
|
|
|
|
callback(pos, node, digger)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
--
|
|
|
|
-- Item definition defaults
|
2011-11-25 14:52:09 -05:00
|
|
|
--
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
minetest.nodedef_default = {
|
|
|
|
-- Item properties
|
|
|
|
type="node",
|
2011-11-25 19:26:19 -05:00
|
|
|
-- name intentionally not defined here
|
2012-01-12 00:10:39 -05:00
|
|
|
description = "",
|
2012-02-03 19:20:05 -05:00
|
|
|
groups = {},
|
2012-01-12 00:10:39 -05:00
|
|
|
inventory_image = "",
|
|
|
|
wield_image = "",
|
|
|
|
wield_scale = {x=1,y=1,z=1},
|
|
|
|
stack_max = 99,
|
|
|
|
usable = false,
|
|
|
|
liquids_pointable = false,
|
2012-02-28 12:45:23 -05:00
|
|
|
tool_capabilities = nil,
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
-- Interaction callbacks
|
2012-01-20 18:11:44 -05:00
|
|
|
on_place = minetest.item_place,
|
2012-01-12 00:10:39 -05:00
|
|
|
on_drop = minetest.item_drop,
|
|
|
|
on_use = nil,
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
on_punch = minetest.node_punch,
|
|
|
|
on_dig = minetest.node_dig,
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
-- Node properties
|
2011-11-25 19:26:19 -05:00
|
|
|
drawtype = "normal",
|
|
|
|
visual_scale = 1.0,
|
2012-01-12 00:10:39 -05:00
|
|
|
tile_images = {""},
|
2011-11-25 19:26:19 -05:00
|
|
|
special_materials = {
|
|
|
|
{image="", backface_culling=true},
|
|
|
|
{image="", backface_culling=true},
|
|
|
|
},
|
|
|
|
alpha = 255,
|
|
|
|
post_effect_color = {a=0, r=0, g=0, b=0},
|
|
|
|
paramtype = "none",
|
2012-01-20 18:11:44 -05:00
|
|
|
paramtype2 = "none",
|
2011-11-25 19:26:19 -05:00
|
|
|
is_ground_content = false,
|
|
|
|
sunlight_propagates = false,
|
|
|
|
walkable = true,
|
|
|
|
pointable = true,
|
|
|
|
diggable = true,
|
|
|
|
climbable = false,
|
|
|
|
buildable_to = false,
|
|
|
|
metadata_name = "",
|
2011-11-25 20:19:25 -05:00
|
|
|
liquidtype = "none",
|
2011-11-25 19:26:19 -05:00
|
|
|
liquid_alternative_flowing = "",
|
|
|
|
liquid_alternative_source = "",
|
|
|
|
liquid_viscosity = 0,
|
|
|
|
light_source = 0,
|
|
|
|
damage_per_second = 0,
|
|
|
|
selection_box = {type="regular"},
|
2012-01-20 18:11:44 -05:00
|
|
|
legacy_facedir_simple = false,
|
|
|
|
legacy_wallmounted = false,
|
2012-01-12 00:10:39 -05:00
|
|
|
}
|
2011-11-25 19:26:19 -05:00
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
minetest.craftitemdef_default = {
|
|
|
|
type="craft",
|
|
|
|
-- name intentionally not defined here
|
|
|
|
description = "",
|
2012-02-03 19:20:05 -05:00
|
|
|
groups = {},
|
2012-01-12 00:10:39 -05:00
|
|
|
inventory_image = "",
|
|
|
|
wield_image = "",
|
|
|
|
wield_scale = {x=1,y=1,z=1},
|
|
|
|
stack_max = 99,
|
|
|
|
liquids_pointable = false,
|
2012-02-28 12:45:23 -05:00
|
|
|
tool_capabilities = nil,
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
-- Interaction callbacks
|
|
|
|
on_place = minetest.item_place,
|
|
|
|
on_drop = minetest.item_drop,
|
|
|
|
on_use = nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
minetest.tooldef_default = {
|
|
|
|
type="tool",
|
|
|
|
-- name intentionally not defined here
|
|
|
|
description = "",
|
2012-02-03 19:20:05 -05:00
|
|
|
groups = {},
|
2012-01-12 00:10:39 -05:00
|
|
|
inventory_image = "",
|
|
|
|
wield_image = "",
|
|
|
|
wield_scale = {x=1,y=1,z=1},
|
|
|
|
stack_max = 1,
|
|
|
|
liquids_pointable = false,
|
2012-02-28 12:45:23 -05:00
|
|
|
tool_capabilities = nil,
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
-- Interaction callbacks
|
|
|
|
on_place = minetest.item_place,
|
|
|
|
on_drop = minetest.item_drop,
|
|
|
|
on_use = nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
minetest.noneitemdef_default = { -- This is used for the hand and unknown items
|
|
|
|
type="none",
|
|
|
|
-- name intentionally not defined here
|
|
|
|
description = "",
|
2012-02-03 19:20:05 -05:00
|
|
|
groups = {},
|
2012-01-12 00:10:39 -05:00
|
|
|
inventory_image = "",
|
|
|
|
wield_image = "",
|
|
|
|
wield_scale = {x=1,y=1,z=1},
|
|
|
|
stack_max = 99,
|
|
|
|
liquids_pointable = false,
|
2012-02-28 12:45:23 -05:00
|
|
|
tool_capabilities = nil,
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
-- Interaction callbacks
|
|
|
|
on_place = nil,
|
|
|
|
on_drop = nil,
|
|
|
|
on_use = nil,
|
|
|
|
}
|
2011-11-25 14:52:09 -05:00
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
--
|
|
|
|
-- Make raw registration functions inaccessible to anyone except builtin.lua
|
|
|
|
--
|
|
|
|
|
|
|
|
local register_item_raw = minetest.register_item_raw
|
|
|
|
minetest.register_item_raw = nil
|
|
|
|
|
|
|
|
local register_alias_raw = minetest.register_alias_raw
|
|
|
|
minetest.register_item_raw = nil
|
2011-11-25 14:52:09 -05:00
|
|
|
|
2011-11-29 14:30:22 -05:00
|
|
|
--
|
2012-01-12 00:10:39 -05:00
|
|
|
-- Item / entity / ABM registration functions
|
2011-11-29 14:30:22 -05:00
|
|
|
--
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
minetest.registered_abms = {}
|
|
|
|
minetest.registered_entities = {}
|
|
|
|
minetest.registered_items = {}
|
|
|
|
minetest.registered_nodes = {}
|
|
|
|
minetest.registered_craftitems = {}
|
|
|
|
minetest.registered_tools = {}
|
|
|
|
minetest.registered_aliases = {}
|
|
|
|
|
|
|
|
-- For tables that are indexed by item name:
|
|
|
|
-- If table[X] does not exist, default to table[minetest.registered_aliases[X]]
|
|
|
|
local function set_alias_metatable(table)
|
|
|
|
setmetatable(table, {
|
|
|
|
__index = function(name)
|
|
|
|
return rawget(table, minetest.registered_aliases[name])
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
set_alias_metatable(minetest.registered_items)
|
|
|
|
set_alias_metatable(minetest.registered_nodes)
|
|
|
|
set_alias_metatable(minetest.registered_craftitems)
|
|
|
|
set_alias_metatable(minetest.registered_tools)
|
|
|
|
|
|
|
|
-- These item names may not be used because they would interfere
|
|
|
|
-- with legacy itemstrings
|
|
|
|
local forbidden_item_names = {
|
|
|
|
MaterialItem = true,
|
|
|
|
MaterialItem2 = true,
|
|
|
|
MaterialItem3 = true,
|
|
|
|
NodeItem = true,
|
|
|
|
node = true,
|
|
|
|
CraftItem = true,
|
|
|
|
craft = true,
|
|
|
|
MBOItem = true,
|
|
|
|
ToolItem = true,
|
|
|
|
tool = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
local function check_modname_prefix(name)
|
|
|
|
if name:sub(1,1) == ":" then
|
|
|
|
-- Escape the modname prefix enforcement mechanism
|
|
|
|
return name:sub(2)
|
|
|
|
else
|
|
|
|
-- Modname prefix enforcement
|
|
|
|
local expected_prefix = minetest.get_current_modname() .. ":"
|
|
|
|
if name:sub(1, #expected_prefix) ~= expected_prefix then
|
|
|
|
error("Name " .. name .. " does not follow naming conventions: " ..
|
|
|
|
"\"modname:\" or \":\" prefix required")
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
local subname = name:sub(#expected_prefix+1)
|
|
|
|
if subname:find("[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]") then
|
|
|
|
error("Name " .. name .. " does not follow naming conventions: " ..
|
|
|
|
"contains unallowed characters")
|
|
|
|
end
|
|
|
|
return name
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
function minetest.register_abm(spec)
|
|
|
|
-- Add to minetest.registered_abms
|
|
|
|
minetest.registered_abms[#minetest.registered_abms+1] = spec
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.register_entity(name, prototype)
|
|
|
|
-- Check name
|
|
|
|
if name == nil then
|
|
|
|
error("Unable to register entity: Name is nil")
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
name = check_modname_prefix(tostring(name))
|
|
|
|
|
|
|
|
prototype.name = name
|
|
|
|
prototype.__index = prototype -- so that it can be used as a metatable
|
|
|
|
|
|
|
|
-- Add to minetest.registered_entities
|
|
|
|
minetest.registered_entities[name] = prototype
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.register_item(name, itemdef)
|
|
|
|
-- Check name
|
|
|
|
if name == nil then
|
|
|
|
error("Unable to register item: Name is nil")
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
name = check_modname_prefix(tostring(name))
|
|
|
|
if forbidden_item_names[name] then
|
|
|
|
error("Unable to register item: Name is forbidden: " .. name)
|
|
|
|
end
|
|
|
|
itemdef.name = name
|
|
|
|
|
|
|
|
-- Apply defaults and add to registered_* table
|
|
|
|
if itemdef.type == "node" then
|
|
|
|
setmetatable(itemdef, {__index = minetest.nodedef_default})
|
|
|
|
minetest.registered_nodes[itemdef.name] = itemdef
|
|
|
|
elseif itemdef.type == "craft" then
|
|
|
|
setmetatable(itemdef, {__index = minetest.craftitemdef_default})
|
|
|
|
minetest.registered_craftitems[itemdef.name] = itemdef
|
|
|
|
elseif itemdef.type == "tool" then
|
|
|
|
setmetatable(itemdef, {__index = minetest.tooldef_default})
|
|
|
|
minetest.registered_tools[itemdef.name] = itemdef
|
|
|
|
elseif itemdef.type == "none" then
|
|
|
|
setmetatable(itemdef, {__index = minetest.noneitemdef_default})
|
|
|
|
else
|
|
|
|
error("Unable to register item: Type is invalid: " .. dump(itemdef))
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
-- Flowing liquid uses param2
|
|
|
|
if itemdef.type == "node" and itemdef.liquidtype == "flowing" then
|
|
|
|
itemdef.paramtype2 = "flowingliquid"
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
-- BEGIN Legacy stuff
|
2012-01-12 00:10:39 -05:00
|
|
|
if itemdef.cookresult_itemstring ~= nil and itemdef.cookresult_itemstring ~= "" then
|
|
|
|
minetest.register_craft({
|
|
|
|
type="cooking",
|
|
|
|
output=itemdef.cookresult_itemstring,
|
|
|
|
recipe=itemdef.name,
|
|
|
|
cooktime=itemdef.furnace_cooktime
|
|
|
|
})
|
|
|
|
end
|
|
|
|
if itemdef.furnace_burntime ~= nil and itemdef.furnace_burntime >= 0 then
|
|
|
|
minetest.register_craft({
|
|
|
|
type="fuel",
|
|
|
|
recipe=itemdef.name,
|
|
|
|
burntime=itemdef.furnace_burntime
|
|
|
|
})
|
|
|
|
end
|
2012-01-20 18:11:44 -05:00
|
|
|
-- END Legacy stuff
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
-- Disable all further modifications
|
|
|
|
getmetatable(itemdef).__newindex = {}
|
|
|
|
|
|
|
|
--minetest.log("Registering item: " .. itemdef.name)
|
|
|
|
minetest.registered_items[itemdef.name] = itemdef
|
|
|
|
minetest.registered_aliases[itemdef.name] = nil
|
|
|
|
register_item_raw(itemdef)
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
function minetest.register_node(name, nodedef)
|
|
|
|
nodedef.type = "node"
|
|
|
|
minetest.register_item(name, nodedef)
|
2011-11-29 14:30:22 -05:00
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
function minetest.register_craftitem(name, craftitemdef)
|
|
|
|
craftitemdef.type = "craft"
|
2011-12-03 02:40:32 -05:00
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
-- BEGIN Legacy stuff
|
2012-01-12 00:10:39 -05:00
|
|
|
if craftitemdef.inventory_image == nil and craftitemdef.image ~= nil then
|
|
|
|
craftitemdef.inventory_image = craftitemdef.image
|
|
|
|
end
|
2012-01-20 18:11:44 -05:00
|
|
|
-- END Legacy stuff
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
minetest.register_item(name, craftitemdef)
|
2011-12-03 02:40:32 -05:00
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
function minetest.register_tool(name, tooldef)
|
|
|
|
tooldef.type = "tool"
|
|
|
|
tooldef.stack_max = 1
|
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
-- BEGIN Legacy stuff
|
2012-01-12 00:10:39 -05:00
|
|
|
if tooldef.inventory_image == nil and tooldef.image ~= nil then
|
|
|
|
tooldef.inventory_image = tooldef.image
|
|
|
|
end
|
2012-02-28 12:45:23 -05:00
|
|
|
if tooldef.tool_capabilities == nil and
|
2012-01-12 00:10:39 -05:00
|
|
|
(tooldef.full_punch_interval ~= nil or
|
|
|
|
tooldef.basetime ~= nil or
|
|
|
|
tooldef.dt_weight ~= nil or
|
|
|
|
tooldef.dt_crackiness ~= nil or
|
|
|
|
tooldef.dt_crumbliness ~= nil or
|
|
|
|
tooldef.dt_cuttability ~= nil or
|
|
|
|
tooldef.basedurability ~= nil or
|
|
|
|
tooldef.dd_weight ~= nil or
|
|
|
|
tooldef.dd_crackiness ~= nil or
|
|
|
|
tooldef.dd_crumbliness ~= nil or
|
|
|
|
tooldef.dd_cuttability ~= nil) then
|
2012-02-28 12:45:23 -05:00
|
|
|
tooldef.tool_capabilities = {
|
2012-01-12 00:10:39 -05:00
|
|
|
full_punch_interval = tooldef.full_punch_interval,
|
|
|
|
basetime = tooldef.basetime,
|
|
|
|
dt_weight = tooldef.dt_weight,
|
|
|
|
dt_crackiness = tooldef.dt_crackiness,
|
|
|
|
dt_crumbliness = tooldef.dt_crumbliness,
|
|
|
|
dt_cuttability = tooldef.dt_cuttability,
|
|
|
|
basedurability = tooldef.basedurability,
|
|
|
|
dd_weight = tooldef.dd_weight,
|
|
|
|
dd_crackiness = tooldef.dd_crackiness,
|
|
|
|
dd_crumbliness = tooldef.dd_crumbliness,
|
|
|
|
dd_cuttability = tooldef.dd_cuttability,
|
|
|
|
}
|
|
|
|
end
|
2012-01-20 18:11:44 -05:00
|
|
|
-- END Legacy stuff
|
2011-11-30 16:52:02 -05:00
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
minetest.register_item(name, tooldef)
|
2011-11-30 16:52:02 -05:00
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
function minetest.register_alias(name, convert_to)
|
|
|
|
if forbidden_item_names[name] then
|
|
|
|
error("Unable to register alias: Name is forbidden: " .. name)
|
2011-11-30 16:52:02 -05:00
|
|
|
end
|
2012-01-12 00:10:39 -05:00
|
|
|
if minetest.registered_items[name] ~= nil then
|
|
|
|
minetest.log("WARNING: Not registering alias, item with same name" ..
|
|
|
|
" is already defined: " .. name .. " -> " .. convert_to)
|
|
|
|
else
|
|
|
|
--minetest.log("Registering alias: " .. name .. " -> " .. convert_to)
|
|
|
|
minetest.registered_aliases[name] = convert_to
|
|
|
|
register_alias_raw(name, convert_to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Alias the forbidden item names to "" so they can't be
|
|
|
|
-- created via itemstrings (e.g. /give)
|
|
|
|
local name
|
|
|
|
for name in pairs(forbidden_item_names) do
|
|
|
|
minetest.registered_aliases[name] = ""
|
|
|
|
register_alias_raw(name, "")
|
2011-11-30 16:52:02 -05:00
|
|
|
end
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
|
|
|
|
-- Deprecated:
|
|
|
|
-- Aliases for minetest.register_alias (how ironic...)
|
|
|
|
--minetest.alias_node = minetest.register_alias
|
|
|
|
--minetest.alias_tool = minetest.register_alias
|
|
|
|
--minetest.alias_craftitem = minetest.register_alias
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Built-in node definitions. Also defined in C.
|
|
|
|
--
|
|
|
|
|
|
|
|
minetest.register_item(":unknown", {
|
|
|
|
type = "none",
|
|
|
|
description = "Unknown Item",
|
|
|
|
inventory_image = "unknown_item.png",
|
|
|
|
on_place = minetest.item_place,
|
|
|
|
on_drop = minetest.item_drop,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_node(":air", {
|
|
|
|
description = "Air (you hacker you!)",
|
|
|
|
inventory_image = "unknown_block.png",
|
|
|
|
wield_image = "unknown_block.png",
|
|
|
|
drawtype = "airlike",
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
walkable = false,
|
|
|
|
pointable = false,
|
|
|
|
diggable = false,
|
|
|
|
buildable_to = true,
|
|
|
|
air_equivalent = true,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_node(":ignore", {
|
|
|
|
description = "Ignore (you hacker you!)",
|
|
|
|
inventory_image = "unknown_block.png",
|
|
|
|
wield_image = "unknown_block.png",
|
|
|
|
drawtype = "airlike",
|
|
|
|
paramtype = "none",
|
|
|
|
sunlight_propagates = false,
|
|
|
|
walkable = false,
|
|
|
|
pointable = false,
|
|
|
|
diggable = false,
|
|
|
|
buildable_to = true, -- A way to remove accidentally placed ignores
|
|
|
|
air_equivalent = true,
|
|
|
|
})
|
|
|
|
|
2012-03-09 16:53:25 -05:00
|
|
|
-- The hand (bare definition)
|
|
|
|
minetest.register_item(":", {
|
|
|
|
type = "none",
|
|
|
|
})
|
|
|
|
|
2011-12-03 13:25:21 -05:00
|
|
|
--
|
|
|
|
-- Default material types
|
|
|
|
--
|
|
|
|
|
|
|
|
function minetest.digprop_constanttime(time)
|
|
|
|
return {
|
|
|
|
diggability = "constant",
|
|
|
|
constant_time = time,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.digprop_stonelike(toughness)
|
|
|
|
return {
|
|
|
|
diggablity = "normal",
|
|
|
|
weight = toughness * 5,
|
|
|
|
crackiness = 1,
|
|
|
|
crumbliness = -0.1,
|
|
|
|
cuttability = -0.2,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.digprop_dirtlike(toughness)
|
|
|
|
return {
|
|
|
|
diggablity = "normal",
|
|
|
|
weight = toughness * 1.2,
|
|
|
|
crackiness = 0,
|
|
|
|
crumbliness = 1.2,
|
|
|
|
cuttability = -0.4,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.digprop_gravellike(toughness)
|
|
|
|
return {
|
|
|
|
diggablity = "normal",
|
|
|
|
weight = toughness * 2,
|
|
|
|
crackiness = 0.2,
|
|
|
|
crumbliness = 1.5,
|
|
|
|
cuttability = -1.0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.digprop_woodlike(toughness)
|
|
|
|
return {
|
|
|
|
diggablity = "normal",
|
|
|
|
weight = toughness * 1.0,
|
|
|
|
crackiness = 0.75,
|
|
|
|
crumbliness = -1.0,
|
|
|
|
cuttability = 1.5,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.digprop_leaveslike(toughness)
|
|
|
|
return {
|
|
|
|
diggablity = "normal",
|
|
|
|
weight = toughness * (-0.5),
|
|
|
|
crackiness = 0,
|
|
|
|
crumbliness = 0,
|
|
|
|
cuttability = 2.0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function minetest.digprop_glasslike(toughness)
|
|
|
|
return {
|
|
|
|
diggablity = "normal",
|
|
|
|
weight = toughness * 0.1,
|
|
|
|
crackiness = 2.0,
|
|
|
|
crumbliness = -1.0,
|
|
|
|
cuttability = -1.0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-12-02 05:43:57 -05:00
|
|
|
--
|
|
|
|
-- Creative inventory
|
|
|
|
--
|
|
|
|
|
|
|
|
minetest.creative_inventory = {}
|
|
|
|
|
|
|
|
minetest.add_to_creative_inventory = function(itemstring)
|
|
|
|
table.insert(minetest.creative_inventory, itemstring)
|
|
|
|
end
|
|
|
|
|
2011-11-27 12:39:36 -05:00
|
|
|
--
|
2011-11-27 18:17:36 -05:00
|
|
|
-- Callback registration
|
2011-11-27 12:39:36 -05:00
|
|
|
--
|
|
|
|
|
2012-01-12 00:10:39 -05:00
|
|
|
local function make_registration()
|
2011-11-27 18:11:17 -05:00
|
|
|
local t = {}
|
|
|
|
local registerfunc = function(func) table.insert(t, func) end
|
|
|
|
return t, registerfunc
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
|
|
|
|
minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
|
|
|
|
minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration()
|
|
|
|
minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration()
|
|
|
|
minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
|
|
|
|
minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
|
|
|
|
minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
|
2012-01-05 17:12:33 -05:00
|
|
|
minetest.registered_on_dieplayers, minetest.register_on_dieplayer = make_registration()
|
2011-11-27 18:11:17 -05:00
|
|
|
minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
|
2011-11-27 12:39:36 -05:00
|
|
|
|
2012-01-20 18:11:44 -05:00
|
|
|
--
|
|
|
|
-- Set random seed
|
|
|
|
--
|
|
|
|
|
|
|
|
math.randomseed(os.time())
|
|
|
|
|
2011-11-27 12:39:36 -05:00
|
|
|
-- END
|