1, 4, 3, 2,
}
function core.facedir_to_dir(facedir)
- return facedir_to_dir[facedir_to_dir_map[facedir]]
+ return facedir_to_dir[facedir_to_dir_map[facedir % 32]]
end
function core.dir_to_wallmounted(dir)
{x = 0, y = 0, z = -1},
}
function core.wallmounted_to_dir(wallmounted)
- return wallmounted_to_dir[wallmounted]
+ return wallmounted_to_dir[wallmounted % 8]
end
-function core.get_node_drops(nodename, toolname)
- local drop = ItemStack({name=nodename}):get_definition().drop
+function core.dir_to_yaw(dir)
+ return -math.atan2(dir.x, dir.z)
+end
+
+function core.yaw_to_dir(yaw)
+ return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)}
+end
+
+function core.is_colored_paramtype(ptype)
+ return (ptype == "color") or (ptype == "colorfacedir") or
+ (ptype == "colorwallmounted")
+end
+
+function core.strip_param2_color(param2, paramtype2)
+ if not core.is_colored_paramtype(paramtype2) then
+ return nil
+ end
+ if paramtype2 == "colorfacedir" then
+ param2 = math.floor(param2 / 32) * 32
+ elseif paramtype2 == "colorwallmounted" then
+ param2 = math.floor(param2 / 8) * 8
+ end
+ -- paramtype2 == "color" requires no modification.
+ return param2
+end
+
+function core.get_node_drops(node, toolname)
+ -- Compatibility, if node is string
+ local nodename = node
+ local param2 = 0
+ -- New format, if node is table
+ if (type(node) == "table") then
+ nodename = node.name
+ param2 = node.param2
+ end
+ local def = core.registered_nodes[nodename]
+ local drop = def and def.drop
+ local ptype = def and def.paramtype2
+ -- get color, if there is color (otherwise nil)
+ local palette_index = core.strip_param2_color(param2, ptype)
if drop == nil then
-- default drop
+ if palette_index then
+ local stack = ItemStack(nodename)
+ stack:get_meta():set_int("palette_index", palette_index)
+ return {stack:to_string()}
+ end
return {nodename}
elseif type(drop) == "string" then
-- itemstring drop
if good_rarity and good_tool then
got_count = got_count + 1
for _, add_item in ipairs(item.items) do
+ -- add color, if necessary
+ if item.inherit_color and palette_index then
+ local stack = ItemStack(add_item)
+ stack:get_meta():set_int("palette_index", palette_index)
+ add_item = stack:to_string()
+ end
got_items[#got_items+1] = add_item
end
if drop.max_items ~= nil and got_count == drop.max_items then
end
function core.item_place_node(itemstack, placer, pointed_thing, param2)
- local item = itemstack:peek_item()
local def = itemstack:get_definition()
if def.type ~= "node" or pointed_thing.type ~= "node" then
return itemstack, false
local oldnode_under = core.get_node_or_nil(under)
local above = pointed_thing.above
local oldnode_above = core.get_node_or_nil(above)
+ local playername = placer:get_player_name()
if not oldnode_under or not oldnode_above then
- core.log("info", placer:get_player_name() .. " tried to place"
+ core.log("info", playername .. " tried to place"
.. " node in unloaded position " .. core.pos_to_string(above))
return itemstack, false
end
- local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
+ local olddef_under = core.registered_nodes[oldnode_under.name]
olddef_under = olddef_under or core.nodedef_default
- local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
+ local olddef_above = core.registered_nodes[oldnode_above.name]
olddef_above = olddef_above or core.nodedef_default
if not olddef_above.buildable_to and not olddef_under.buildable_to then
- core.log("info", placer:get_player_name() .. " tried to place"
+ core.log("info", playername .. " tried to place"
.. " node in invalid position " .. core.pos_to_string(above)
.. ", replacing " .. oldnode_above.name)
return itemstack, false
place_to = {x = under.x, y = under.y, z = under.z}
end
- if core.is_protected(place_to, placer:get_player_name()) and
+ if core.is_protected(place_to, playername) and
not minetest.check_player_privs(placer, "protection_bypass") then
- core.log("action", placer:get_player_name()
+ core.log("action", playername
.. " tried to place " .. def.name
.. " at protected position "
.. core.pos_to_string(place_to))
- core.record_protection_violation(place_to, placer:get_player_name())
+ core.record_protection_violation(place_to, playername)
return itemstack
end
- core.log("action", placer:get_player_name() .. " places node "
+ core.log("action", playername .. " places node "
.. def.name .. " at " .. core.pos_to_string(place_to))
local oldnode = core.get_node(place_to)
- local newnode = {name = def.name, param1 = 0, param2 = param2}
+ local newnode = {name = def.name, param1 = 0, param2 = param2 or 0}
-- Calculate direction for wall mounted stuff like torches and signs
if def.place_param2 ~= nil then
newnode.param2 = def.place_param2
- elseif def.paramtype2 == 'wallmounted' and not param2 then
+ elseif (def.paramtype2 == "wallmounted" or
+ def.paramtype2 == "colorwallmounted") and not param2 then
local dir = {
x = under.x - above.x,
y = under.y - above.y,
}
newnode.param2 = core.dir_to_wallmounted(dir)
-- Calculate the direction for furnaces and chests and stuff
- elseif def.paramtype2 == 'facedir' and not param2 then
+ elseif (def.paramtype2 == "facedir" or
+ def.paramtype2 == "colorfacedir") and not param2 then
local placer_pos = placer:getpos()
if placer_pos then
local dir = {
end
end
+ local metatable = itemstack:get_meta():to_table().fields
+
+ -- Transfer color information
+ if metatable.palette_index and not def.place_param2 then
+ local color_divisor = nil
+ if def.paramtype2 == "color" then
+ color_divisor = 1
+ elseif def.paramtype2 == "colorwallmounted" then
+ color_divisor = 8
+ elseif def.paramtype2 == "colorfacedir" then
+ color_divisor = 32
+ end
+ if color_divisor then
+ local color = math.floor(metatable.palette_index / color_divisor)
+ local other = newnode.param2 % color_divisor
+ newnode.param2 = color * color_divisor + other
+ end
+ end
+
-- Check if the node is attached and if it can be placed there
if core.get_item_group(def.name, "attached_node") ~= 0 and
not builtin_shared.check_attached_node(place_to, newnode) then
end
-- Run script hook
- local _, callback
for _, callback in ipairs(core.registered_on_placenodes) do
-- Deepcopy pos, node and pointed_thing because callback can modify them
local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
function core.item_drop(itemstack, dropper, pos)
if dropper and dropper:is_player() then
- local v = dropper:get_look_dir()
- local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
- local cs = itemstack:get_count()
- if dropper:get_player_control().sneak then
- cs = 1
- end
- local item = itemstack:take_item(cs)
+ local dir = dropper:get_look_dir()
+ local p = {x = pos.x, y = pos.y + 1.2, z = pos.z}
+ local cnt = itemstack:get_count()
+ local item = itemstack:take_item(cnt)
local obj = core.add_item(p, item)
if obj then
- v.x = v.x*2
- v.y = v.y*2 + 2
- v.z = v.z*2
- obj:setvelocity(v)
+ dir.x = dir.x * 2
+ dir.y = dir.y * 2 + 2
+ dir.z = dir.z * 2
+ obj:set_velocity(dir)
obj:get_luaentity().dropped_by = dropper:get_player_name()
return itemstack
end
end
function core.node_dig(pos, node, digger)
- local def = ItemStack({name=node.name}):get_definition()
- if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
+ local def = core.registered_nodes[node.name]
+ if def and (not def.diggable or
+ (def.can_dig and not def.can_dig(pos, digger))) then
core.log("info", digger:get_player_name() .. " tried to dig "
.. node.name .. " which is not diggable "
.. core.pos_to_string(pos))
.. node.name .. " at " .. core.pos_to_string(pos))
local wielded = digger:get_wielded_item()
- local drops = core.get_node_drops(node.name, wielded:get_name())
+ local drops = core.get_node_drops(node, wielded:get_name())
local wdef = wielded:get_definition()
local tp = wielded:get_tool_capabilities()
- local dp = core.get_dig_params(def.groups, tp)
+ local dp = core.get_dig_params(def and def.groups, tp)
if wdef and wdef.after_use then
wielded = wdef.after_use(wielded, digger, node, dp) or wielded
else
-- Wear out tool
- if not core.setting_getbool("creative_mode") then
+ if not core.settings:get_bool("creative_mode") then
wielded:add_wear(dp.wear)
+ if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
+ core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
+ end
end
end
digger:set_wielded_item(wielded)
core.handle_node_drops(pos, drops, digger)
local oldmetadata = nil
- if def.after_dig_node then
+ if def and def.after_dig_node then
oldmetadata = core.get_meta(pos):to_table()
end
core.remove_node(pos)
-- Run callback
- if def.after_dig_node then
+ if def and def.after_dig_node then
-- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
end
end
+function core.itemstring_with_palette(item, palette_index)
+ local stack = ItemStack(item) -- convert to ItemStack
+ stack:get_meta():set_int("palette_index", palette_index)
+ return stack:to_string()
+end
+
+function core.itemstring_with_color(item, colorstring)
+ local stack = ItemStack(item) -- convert to ItemStack
+ stack:get_meta():set_string("color", colorstring)
+ return stack:to_string()
+end
+
-- This is used to allow mods to redefine core.item_place and so on
-- NOTE: This is not the preferred way. Preferred way is to provide enough
-- callbacks to not require redefining global functions. -celeron55