From 322e5aaf9285e9686101393967f1a3c1e7db986c Mon Sep 17 00:00:00 2001 From: Dániel Juhász Date: Tue, 20 Jun 2017 09:19:56 +0000 Subject: Automatic item and node colorization (#5640) * Automatic item and node colorization Now nodes with a palette yield colored item stacks, and colored items place colored nodes by default. The client predicts the colorization. * Backwards compatibility * Use nil * Style fixes * Fix code style * Document changes --- builtin/game/falling.lua | 6 +++--- builtin/game/item.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'builtin/game') diff --git a/builtin/game/falling.lua b/builtin/game/falling.lua index b1beb1ab0..1ac4f7081 100644 --- a/builtin/game/falling.lua +++ b/builtin/game/falling.lua @@ -93,7 +93,7 @@ core.register_entity(":__builtin:falling_node", { core.remove_node(np) if nd and nd.buildable_to == false then -- Add dropped items - local drops = core.get_node_drops(n2.name, "") + local drops = core.get_node_drops(n2, "") for _, dropped_item in pairs(drops) do core.add_item(np, dropped_item) end @@ -145,9 +145,9 @@ function core.spawn_falling_node(pos) end local function drop_attached_node(p) - local nn = core.get_node(p).name + local n = core.get_node(p) core.remove_node(p) - for _, item in pairs(core.get_node_drops(nn, "")) do + for _, item in pairs(core.get_node_drops(n, "")) do local pos = { x = p.x + math.random()/2 - 0.25, y = p.y + math.random()/2 - 0.25, diff --git a/builtin/game/item.lua b/builtin/game/item.lua index e36745f93..f6de2c339 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -155,12 +155,35 @@ function core.yaw_to_dir(yaw) return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)} end -function core.get_node_drops(nodename, toolname) +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 if drop == nil then -- default drop - return {nodename} + local stack = ItemStack(nodename) + if def then + local type = def.paramtype2 + if (type == "color") or (type == "colorfacedir") or + (type == "colorwallmounted") then + local meta = stack:get_meta() + local color_part = param2 + if (type == "colorfacedir") then + color_part = math.floor(color_part / 32) * 32; + elseif (type == "colorwallmounted") then + color_part = math.floor(color_part / 8) * 8; + end + meta:set_int("palette_index", color_part) + end + end + return {stack:to_string()} elseif type(drop) == "string" then -- itemstring drop return {drop} @@ -258,7 +281,7 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2) .. 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 @@ -286,6 +309,25 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2) 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 @@ -474,7 +516,7 @@ function core.node_dig(pos, node, digger) .. 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() -- cgit v1.2.3