Mineclonia/mods/ITEMS/mcl_banners/init.lua

212 lines
7.4 KiB
Lua
Raw Normal View History

2017-07-27 06:14:42 +02:00
local node_sounds
if minetest.get_modpath("mcl_sounds") then
node_sounds = mcl_sounds.node_sound_wood_defaults()
end
-- Helper function
local function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
2017-07-27 19:13:41 +02:00
local colors = {
-- ID, description, wool, unified dyes color group, overlay color,
2017-07-27 20:31:58 +02:00
["unicolor_white"] = {"white", "White Banner", "mcl_wool:white", "#FFFFFFE0" },
2017-07-27 21:05:08 +02:00
["unicolor_darkgrey"] = {"grey", "Grey Banner", "mcl_wool:grey", "#303030E0" },
2017-07-27 20:31:58 +02:00
["unicolor_grey"] = {"silver", "Light Grey Banner", "mcl_wool:silver", "#5B5B5BE0" },
2017-07-27 20:01:12 +02:00
["unicolor_black"] = {"black", "Black Banner", "mcl_wool:black", "#000000E0" },
2017-07-27 21:18:24 +02:00
["unicolor_red"] = {"red", "Red Banner", "mcl_wool:red", "#BC0000E0" },
["unicolor_yellow"] = {"yellow", "Yellow Banner", "mcl_wool:yellow", "#BCA800E0" },
2017-07-27 21:05:08 +02:00
["unicolor_dark_green"] = {"green", "Green Banner", "mcl_wool:green", "#006000E0" },
["unicolor_cyan"] = {"cyan", "Cyan Banner", "mcl_wool:cyan", "#00ACACE0" },
["unicolor_blue"] = {"blue", "Blue Banner", "mcl_wool:blue", "#0000ACE0" },
["unicolor_red_violet"] = {"magenta", "Magenta Banner", "mcl_wool:magenta", "#AC007CE0" },
2017-07-27 21:18:24 +02:00
["unicolor_orange"] = {"orange", "Orange Banner", "mcl_wool:orange", "#BC6900E0" },
2017-07-27 21:05:08 +02:00
["unicolor_violet"] = {"purple", "Purple Banner", "mcl_wool:purple", "#6400ACE0" },
["unicolor_brown"] = {"brown", "Brown Banner", "mcl_wool:brown", "#402100E0" },
["unicolor_pink"] = {"pink", "Pink Banner", "mcl_wool:pink", "#DE557CE0" },
["unicolor_lime"] = {"lime", "Lime Banner", "mcl_wool:lime", "#30AC00E0"},
["unicolor_light_blue"] = {"light_blue", "Light Blue Banner", "mcl_wool:light_blue", "#4040CFE0" },
2017-07-27 19:13:41 +02:00
}
-- After destroying the standing banner node
2017-07-27 19:13:41 +02:00
local on_destruct_standing_banner = function(pos)
if not minetest.settings:get_bool("creative_mode") then
return
end
-- Find this node's banner entity and make it drop as an item
2017-07-27 19:13:41 +02:00
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, v in ipairs(objects) do
if v:get_entity_name() == "mcl_banners:standing_banner" then
v:get_luaentity():_drop()
2017-07-27 06:14:42 +02:00
end
2017-07-27 19:13:41 +02:00
end
end
2017-07-27 06:14:42 +02:00
2017-07-27 19:13:41 +02:00
local make_banner_texture = function(colorid)
local colorize
if colors[colorid] then
colorize = colors[colorid][4]
end
if colorize then
2017-07-27 20:01:12 +02:00
return { "(mcl_banners_banner_base.png^[mask:mcl_banners_base_inverted.png)^((mcl_banners_banner_base.png^[colorize:"..colorize..")^[mask:mcl_banners_base.png)" }
2017-07-27 19:13:41 +02:00
else
return { "mcl_banners_banner_base.png" }
end
end
2017-07-27 06:14:42 +02:00
-- Standing banner node.
-- This is an invisible node which is only used to destroy the banner entity.
-- All the important banner information (such as color) is stored in the entity.
-- It is used only used internally.
minetest.register_node("mcl_banners:standing_banner", {
walkable = false,
is_ground_content = false,
paramtype = "light",
sunlight_propagates = true,
drawtype = "airlike",
inventory_image = "mcl_banners_item_base.png",
wield_image = "mcl_banners_item_base.png",
tiles = { "blank.png" },
selection_box = {type = "fixed", fixed= {-0.2, -0.5, -0.2, 0.2, 0.5, 0.2} },
groups = { banner = 1, deco_block = 1, attached_node = 1, not_in_creative_inventory = 1, not_in_craft_guide = 1, },
stack_max = 16,
sounds = node_sounds,
drop = "", -- Item drops are handled in entity code
on_destruct = on_destruct_standing_banner,
_mcl_hardness = 1,
_mcl_blast_resistance = 5,
})
2017-07-27 19:13:41 +02:00
for colorid, colortab in pairs(colors) do
local itemid = colortab[1]
local desc = colortab[2]
local wool = colortab[3]
local colorize = colortab[4]
local itemstring = "mcl_banners:banner_item_"..itemid
2017-07-27 19:13:41 +02:00
local inv
if colorize then
inv = "mcl_banners_item_base.png^(mcl_banners_item_overlay.png^[colorize:"..colorize..")"
else
inv = "mcl_banners_item_base.png^mcl_banners_item_overlay.png"
end
-- Banner items.
-- This is the player-visible banner item. It comes in 16 base colors.
-- The multiple items are really only needed for the different item images.
-- TODO: Combine the items into only 1 item.
minetest.register_craftitem(itemstring, {
2017-07-27 21:18:24 +02:00
description = desc,
2017-07-27 22:09:50 +02:00
_doc_items_longdesc = "Banners are tall decorative blocks with a solid color. They can be placed on the floor. Banners can not be emblazoned (yet).",
2017-07-27 21:18:24 +02:00
inventory_image = inv,
wield_image = inv,
groups = { banner = 1, deco_block = 1, },
2017-07-27 21:18:24 +02:00
stack_max = 16,
on_place = function(itemstack, placer, pointed_thing)
local above = pointed_thing.above
local under = pointed_thing.under
-- Use pointed node's on_rightclick function first, if present
local node_under = minetest.get_node(under)
if placer and not placer:get_player_control().sneak then
if minetest.registered_nodes[node_under.name] and minetest.registered_nodes[node_under.name].on_rightclick then
return minetest.registered_nodes[node_under.name].on_rightclick(under, node_under, placer, itemstack) or itemstack
end
end
-- Place the node!
local _, success = minetest.item_place_node(ItemStack("mcl_banners:standing_banner"), placer, pointed_thing)
2017-07-27 21:18:24 +02:00
if not success then
return itemstack
end
local place_pos
if minetest.registered_nodes[node_under.name].buildable_to then
place_pos = under
else
place_pos = above
end
place_pos.y = place_pos.y - 0.5
local banner = minetest.add_entity(place_pos, "mcl_banners:standing_banner")
banner:set_properties({textures=make_banner_texture(colorid)})
banner:get_luaentity()._base_color = colorid
-- Determine the rotation based on player's yaw
local yaw = placer:get_look_horizontal()
-- Select one of 16 possible rotations (0-15)
local rotation_level = round((yaw / (math.pi*2)) * 16)
local final_yaw = (rotation_level * (math.pi/8)) + math.pi
banner:set_yaw(final_yaw)
if not minetest.settings:get_bool("creative_mode") then
itemstack:take_item()
end
minetest.sound_play({name="default_place_node_hard", gain=1.0}, {pos = place_pos})
return itemstack
end,
2017-07-27 19:13:41 +02:00
})
if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_wool") then
minetest.register_craft({
output = itemstring,
2017-07-27 19:13:41 +02:00
recipe = {
{ wool, wool, wool },
{ wool, wool, wool },
{ "", "mcl_core:stick", "" },
}
})
end
end
2017-07-27 06:14:42 +02:00
2017-07-27 06:19:47 +02:00
minetest.register_entity("mcl_banners:standing_banner", {
2017-07-27 06:14:42 +02:00
physical = false,
collide_with_objects = false,
visual = "mesh",
mesh = "amc_banner.b3d",
2017-07-27 19:13:41 +02:00
visual_size = { x=2.499, y=2.499 },
textures = make_banner_texture(),
2017-07-27 06:14:42 +02:00
collisionbox = { 0, 0, 0, 0, 0, 0 },
_base_color = nil,
get_staticdata = function(self)
local out = { _base_color = self._base_color }
return minetest.serialize(out)
end,
on_activate = function(self, staticdata)
if staticdata and staticdata ~= "" then
local inp = minetest.deserialize(staticdata)
self._base_color = inp._base_color
2017-07-27 19:13:41 +02:00
self.object:set_properties({textures = make_banner_texture(self._base_color)})
2017-07-27 06:14:42 +02:00
end
self.object:set_armor_groups({immortal=1})
end,
-- This is a custom function which causes the banner to be dropped as item and destroys the entity.
_drop = function(self)
-- Drop as item when the entity is destroyed.
if not self._base_color then
return
end
local pos = self.object:getpos()
pos.y = pos.y + 1
minetest.add_item(pos, "mcl_banners:banner_item_"..colors[self._base_color][1])
-- Destroy entity
self:remove()
end,
2017-07-27 06:14:42 +02:00
})
minetest.register_craft({
type = "fuel",
2017-07-27 19:13:41 +02:00
recipe = "group:banner",
2017-07-27 06:14:42 +02:00
burntime = 15,
})