Skip to content

Commit 71b02d6

Browse files
juhdanadparamat
authored andcommittedAug 14, 2017
Make dropped items colorable
1 parent 4493d47 commit 71b02d6

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed
 

Diff for: ‎builtin/game/item.lua

+32-15
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ function core.yaw_to_dir(yaw)
155155
return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)}
156156
end
157157

158+
function core.is_colored_paramtype(ptype)
159+
return (ptype == "color") or (ptype == "colorfacedir") or
160+
(ptype == "colorwallmounted")
161+
end
162+
163+
function core.strip_param2_color(param2, paramtype2)
164+
if not core.is_colored_paramtype(paramtype2) then
165+
return nil
166+
end
167+
if paramtype2 == "colorfacedir" then
168+
param2 = math.floor(param2 / 32) * 32
169+
elseif paramtype2 == "colorwallmounted" then
170+
param2 = math.floor(param2 / 8) * 8
171+
end
172+
-- paramtype2 == "color" requires no modification.
173+
return param2
174+
end
175+
158176
function core.get_node_drops(node, toolname)
159177
-- Compatibility, if node is string
160178
local nodename = node
@@ -166,24 +184,17 @@ function core.get_node_drops(node, toolname)
166184
end
167185
local def = core.registered_nodes[nodename]
168186
local drop = def and def.drop
187+
local ptype = def and def.paramtype2
188+
-- get color, if there is color (otherwise nil)
189+
local palette_index = core.strip_param2_color(param2, ptype)
169190
if drop == nil then
170191
-- default drop
171-
local stack = ItemStack(nodename)
172-
if def then
173-
local type = def.paramtype2
174-
if (type == "color") or (type == "colorfacedir") or
175-
(type == "colorwallmounted") then
176-
local meta = stack:get_meta()
177-
local color_part = param2
178-
if (type == "colorfacedir") then
179-
color_part = math.floor(color_part / 32) * 32;
180-
elseif (type == "colorwallmounted") then
181-
color_part = math.floor(color_part / 8) * 8;
182-
end
183-
meta:set_int("palette_index", color_part)
184-
end
192+
if palette_index then
193+
local stack = ItemStack(nodename)
194+
stack:get_meta():set_int("palette_index", palette_index)
195+
return {stack:to_string()}
185196
end
186-
return {stack:to_string()}
197+
return {nodename}
187198
elseif type(drop) == "string" then
188199
-- itemstring drop
189200
return {drop}
@@ -218,6 +229,12 @@ function core.get_node_drops(node, toolname)
218229
if good_rarity and good_tool then
219230
got_count = got_count + 1
220231
for _, add_item in ipairs(item.items) do
232+
-- add color, if necessary
233+
if item.inherit_color and palette_index then
234+
local stack = ItemStack(add_item)
235+
stack:get_meta():set_int("palette_index", palette_index)
236+
add_item = stack:to_string()
237+
end
221238
got_items[#got_items+1] = add_item
222239
end
223240
if drop.max_items ~= nil and got_count == drop.max_items then

Diff for: ‎doc/lua_api.txt

+23
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,21 @@ automatically transferred between node and item forms by the engine,
536536
when a player digs or places a colored node.
537537
You can disable this feature by setting the `drop` field of the node
538538
to itself (without metadata).
539+
To transfer the color to a special drop, you need a drop table.
540+
Example:
541+
542+
minetest.register_node("mod:stone", {
543+
description = "Stone",
544+
tiles = {"default_stone.png"},
545+
paramtype2 = "color",
546+
palette = "palette.png",
547+
drop = {
548+
items = {
549+
-- assume that mod:cobblestone also has the same palette
550+
{items = {"mod:cobblestone"}, inherit_color = true },
551+
}
552+
}
553+
})
539554

540555
### Colored items in craft recipes
541556
Craft recipes only support item strings, but fortunately item strings
@@ -2688,6 +2703,13 @@ and `minetest.auth_reload` call the authetification handler.
26882703
* Convert a vector into a yaw (angle)
26892704
* `minetest.yaw_to_dir(yaw)`
26902705
* Convert yaw (angle) to a vector
2706+
* `minetest.is_colored_paramtype(ptype)`
2707+
* Returns a boolean. Returns `true` if the given `paramtype2` contains color
2708+
information (`color`, `colorwallmounted` or `colorfacedir`).
2709+
* `minetest.strip_param2_color(param2, paramtype2)`
2710+
* Removes everything but the color information from the
2711+
given `param2` value.
2712+
* Returns `nil` if the given `paramtype2` does not contain color information
26912713
* `minetest.get_node_drops(nodename, toolname)`
26922714
* Returns list of item names.
26932715
* **Note**: This will be removed or modified in a future version.
@@ -4275,6 +4297,7 @@ Definition tables
42754297
{
42764298
items = {"foo:bar", "baz:frob"}, -- Items to drop.
42774299
rarity = 1, -- Probability of dropping is 1 / rarity.
4300+
inherit_color = true, -- To inherit palette color from the node
42784301
},
42794302
},
42804303
},

0 commit comments

Comments
 (0)
Please sign in to comment.