1
0
Fork 0
MineClone2/mods/ITEMS/mcl_signs/init.lua

484 lines
14 KiB
Lua
Raw Normal View History

2015-06-29 17:55:56 +00:00
-- Font: 04.jp.org
-- load characters map
2017-07-25 00:20:37 +00:00
local chars_file = io.open(minetest.get_modpath("mcl_signs").."/characters", "r")
2017-07-25 01:33:54 +00:00
-- FIXME: Support more characters (many characters are missing)
2015-06-29 17:55:56 +00:00
local charmap = {}
if not chars_file then
2017-07-25 00:20:37 +00:00
minetest.log("error", "[mcl_signs] : character map file not found")
2015-06-29 17:55:56 +00:00
else
2017-07-24 21:59:49 +00:00
while true do
local char = chars_file:read("*l")
if char == nil then
break
end
local img = chars_file:read("*l")
chars_file:read("*l")
charmap[char] = img
end
2015-06-29 17:55:56 +00:00
end
2017-01-24 01:31:49 +00:00
-- CONSTANTS
2017-07-24 23:33:06 +00:00
local SIGN_WIDTH = 115
2017-01-24 01:31:49 +00:00
2017-07-24 23:57:05 +00:00
local LINE_LENGTH = 15
2017-01-24 01:31:49 +00:00
local NUMBER_OF_LINES = 4
local LINE_HEIGHT = 14
local CHAR_WIDTH = 5
2017-07-26 01:45:22 +00:00
-- Helper functions
local function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
2017-01-24 01:31:49 +00:00
local string_to_array = function(str)
local tab = {}
for i=1,string.len(str) do
table.insert(tab, string.sub(str, i,i))
end
return tab
end
2017-07-24 22:38:10 +00:00
local string_to_line_array = function(str)
2017-01-24 01:31:49 +00:00
local tab = {}
local current = 1
2017-07-24 22:38:10 +00:00
local linechar = 1
2017-01-24 01:31:49 +00:00
tab[1] = ""
for _,char in ipairs(string_to_array(str)) do
2017-07-24 22:38:10 +00:00
-- New line
if char == "\n" then
current = current + 1
2017-01-24 01:31:49 +00:00
tab[current] = ""
2017-07-24 22:38:10 +00:00
linechar = 1
-- This check cuts off overlong lines
elseif linechar <= LINE_LENGTH then
tab[current] = tab[current]..char
linechar = linechar + 1
2017-01-24 01:31:49 +00:00
end
end
return tab
end
local create_lines = function(text)
local line_num = 1
local tab = {}
2017-07-24 22:38:10 +00:00
for _, line in ipairs(string_to_line_array(text)) do
if line_num > NUMBER_OF_LINES then
break
2017-01-24 01:31:49 +00:00
end
2017-07-24 22:38:10 +00:00
table.insert(tab, line)
line_num = line_num + 1
2017-01-24 01:31:49 +00:00
end
return tab
end
local generate_line = function(s, ypos)
2017-07-24 21:59:49 +00:00
local i = 1
local parsed = {}
local width = 0
local chars = 0
2017-07-25 01:31:23 +00:00
local printed_char_width = CHAR_WIDTH + 1
2017-07-24 22:38:10 +00:00
while chars <= LINE_LENGTH and i <= #s do
2017-07-24 21:59:49 +00:00
local file = nil
if charmap[s:sub(i, i)] ~= nil then
file = charmap[s:sub(i, i)]
i = i + 1
elseif i < #s and charmap[s:sub(i, i + 1)] ~= nil then
file = charmap[s:sub(i, i + 1)]
i = i + 2
else
2017-07-25 00:20:37 +00:00
minetest.log("warning", "[mcl_signs] Unknown symbol in '"..s.."' at "..i.." (probably "..s:sub(i, i)..")")
2017-07-24 21:59:49 +00:00
i = i + 1
end
if file ~= nil then
2017-07-25 01:31:23 +00:00
width = width + printed_char_width
2017-07-24 21:59:49 +00:00
table.insert(parsed, file)
chars = chars + 1
end
end
width = width - 1
local texture = ""
2017-07-25 01:31:23 +00:00
local xpos = math.floor((SIGN_WIDTH - width) / 2)
2017-07-24 21:59:49 +00:00
for i = 1, #parsed do
texture = texture..":"..xpos..","..ypos.."="..parsed[i]..".png"
2017-07-25 01:31:23 +00:00
xpos = xpos + printed_char_width
2017-07-24 21:59:49 +00:00
end
return texture
2017-01-24 01:31:49 +00:00
end
2017-07-26 13:11:52 +00:00
local generate_texture = function(lines, signnodename)
2017-07-24 22:06:06 +00:00
local texture = "[combine:"..SIGN_WIDTH.."x"..SIGN_WIDTH
2017-07-26 13:11:52 +00:00
local ypos
if signnodename == "mcl_signs:wall_sign" then
2017-07-26 13:34:09 +00:00
ypos = 29
2017-07-26 13:11:52 +00:00
else
2017-07-26 13:34:09 +00:00
ypos = -2
2017-07-26 13:11:52 +00:00
end
2017-07-24 21:59:49 +00:00
for i = 1, #lines do
texture = texture..generate_line(lines[i], ypos)
ypos = ypos + LINE_HEIGHT
end
return texture
2017-01-24 01:31:49 +00:00
end
2017-07-26 13:11:52 +00:00
local n = 23/56 - 1/128
2017-07-24 21:36:18 +00:00
2017-07-26 02:40:44 +00:00
local signtext_info_wall = {
2017-07-24 21:59:49 +00:00
{delta = {x = 0, y = 0, z = n}, yaw = 0},
{delta = {x = n, y = 0, z = 0}, yaw = math.pi / -2},
{delta = {x = 0, y = 0, z = -n}, yaw = math.pi},
{delta = {x = -n, y = 0, z = 0}, yaw = math.pi / 2},
2015-06-29 17:55:56 +00:00
}
2017-07-26 02:40:44 +00:00
local signtext_info_standing = {}
2017-07-24 21:36:18 +00:00
2017-07-26 02:40:44 +00:00
local m = -1/16 + 1/64
for rot=0, 15 do
local yaw = math.pi*2 - (((math.pi*2) / 16) * rot)
local delta = vector.multiply(minetest.yaw_to_dir(yaw), m)
2017-07-26 14:50:26 +00:00
-- Offset because sign is a bit above node boundaries
delta.y = delta.y + 2/28
2017-07-26 02:40:44 +00:00
table.insert(signtext_info_standing, { delta = delta, yaw = yaw })
end
local function get_rotation_level(facedir, nodename)
local rl = facedir * 4
if nodename == "mcl_signs:standing_sign22_5" then
rl = rl + 1
elseif nodename == "mcl_signs:standing_sign45" then
rl = rl + 2
elseif nodename == "mcl_signs:standing_sign67_5" then
rl = rl + 3
end
return rl
end
2015-06-29 17:55:56 +00:00
2017-07-25 01:06:25 +00:00
local sign_groups = {handy=1,axey=1, flammable=1, deco_block=1, material_wood=1, attached_node=1}
2015-06-29 17:55:56 +00:00
local destruct_sign = function(pos)
2017-07-24 21:59:49 +00:00
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, v in ipairs(objects) do
2017-08-18 19:58:20 +00:00
local ent = v:get_luaentity()
if ent and ent.name == "mcl_signs:text" then
2017-07-24 21:59:49 +00:00
v:remove()
end
end
2015-06-29 17:55:56 +00:00
end
local update_sign = function(pos, fields, sender)
2017-07-24 21:59:49 +00:00
local meta = minetest.get_meta(pos)
if not meta then
return
end
2015-06-29 17:55:56 +00:00
local text = meta:get_string("text")
2017-07-24 23:35:29 +00:00
if fields and (text == "" and fields.text) then
2015-06-29 17:55:56 +00:00
meta:set_string("text", fields.text)
2017-07-24 22:38:10 +00:00
text = fields.text
2015-06-29 17:55:56 +00:00
end
2017-07-24 23:25:54 +00:00
if text == nil then
text = ""
end
2017-07-24 21:59:49 +00:00
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, v in ipairs(objects) do
2017-08-18 19:58:20 +00:00
local ent = v:get_luaentity()
if ent and ent.name == "mcl_signs:text" then
2017-07-26 13:11:52 +00:00
v:set_properties({textures={generate_texture(create_lines(text), v:get_luaentity()._signnodename)}})
2015-06-29 17:55:56 +00:00
return
2017-07-24 21:59:49 +00:00
end
end
2015-06-29 17:55:56 +00:00
-- if there is no entity
local sign_info
2017-07-26 02:40:44 +00:00
local n = minetest.get_node(pos)
local nn = n.name
2017-07-26 01:45:22 +00:00
if nn == "mcl_signs:standing_sign" or nn == "mcl_signs:standing_sign22_5" or nn == "mcl_signs:standing_sign45" or nn == "mcl_signs:standing_sign67_5" then
2017-07-26 02:40:44 +00:00
sign_info = signtext_info_standing[get_rotation_level(n.param2, nn) + 1]
2017-07-26 01:45:22 +00:00
elseif nn == "mcl_signs:wall_sign" then
2017-07-26 02:40:44 +00:00
sign_info = signtext_info_wall[n.param2 + 1]
2015-06-29 17:55:56 +00:00
end
if sign_info == nil then
return
end
2017-07-26 02:40:44 +00:00
local text_entity = minetest.add_entity({
x = pos.x + sign_info.delta.x,
y = pos.y + sign_info.delta.y,
z = pos.z + sign_info.delta.z}, "mcl_signs:text")
2017-07-26 01:45:22 +00:00
if nn == "mcl_signs:standing_sign22_5" then
sign_info.yaw = sign_info.yaw + math.pi / 8
elseif nn == "mcl_signs:standing_sign45" then
sign_info.yaw = sign_info.yaw + 2 * (math.pi / 8)
elseif nn == "mcl_signs:standing_sign67_5" then
sign_info.yaw = sign_info.yaw + 3 * (math.pi / 8)
end
2017-07-26 13:11:52 +00:00
text_entity:get_luaentity()._signnodename = nn
2017-07-26 01:45:22 +00:00
2017-07-24 22:38:10 +00:00
text_entity:setyaw(sign_info.yaw)
2015-06-29 17:55:56 +00:00
end
local show_formspec = function(player, pos)
2017-07-25 00:00:55 +00:00
minetest.show_formspec(
player:get_player_name(),
2017-07-25 00:20:37 +00:00
"mcl_signs:set_text_"..pos.x.."_"..pos.y.."_"..pos.z,
2017-07-25 00:00:55 +00:00
"size[6,3]textarea[0.25,0.25;6,1.5;text;Edit sign text:;]label[0,1.5;Maximum line length: 15\nMaximum lines: 4]button_exit[0,2.5;6,1;submit;Done]"
)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
2017-07-25 00:20:37 +00:00
if formname:find("mcl_signs:set_text_") == 1 then
local x, y, z = formname:match("mcl_signs:set_text_(.-)_(.-)_(.*)")
local pos = {x=tonumber(x), y=tonumber(y), z=tonumber(z)}
if not pos or not pos.x or not pos.y or not pos.z then return end
update_sign(pos, fields, player)
end
end)
2017-07-26 14:13:19 +00:00
local node_sounds
if minetest.get_modpath("mcl_sounds") then
node_sounds = mcl_sounds.node_sound_wood_defaults()
end
2017-07-25 00:20:37 +00:00
minetest.register_node("mcl_signs:wall_sign", {
2017-07-24 21:59:49 +00:00
description = "Sign",
2017-03-11 18:33:03 +00:00
_doc_items_longdesc = "Signs can be written and come in two variants: Wall sign and sign on a sign post. Signs can be placed on the top and the sides of other blocks, but not below them.",
2017-07-25 00:09:30 +00:00
_doc_items_usagehelp = "Place the sign at the side to build a wall sign, place it on top of another block to build a sign with a sign post.\nAfter placing the sign, you can write something on it. You have 4 lines of text with up to 15 characters for each line; anything beyond these limits is lost. The text can not be changed once it has been written; you have to break and place the sign again.",
2017-07-24 21:59:49 +00:00
inventory_image = "default_sign.png",
2015-06-29 17:55:56 +00:00
walkable = false,
is_ground_content = false,
2017-07-24 21:59:49 +00:00
wield_image = "default_sign.png",
node_placement_prediction = "",
paramtype = "light",
2015-06-29 17:55:56 +00:00
sunlight_propagates = true,
2017-07-25 01:06:25 +00:00
paramtype2 = "wallmounted",
2017-07-26 13:11:52 +00:00
drawtype = "mesh",
mesh = "mcl_signs_signonwallmount.obj",
selection_box = {type = "wallmounted", wall_side = {-0.5, -7/28, -0.5, -23/56, 7/28, 0.5}},
tiles = {"mcl_signs_sign.png"},
2017-07-24 21:59:49 +00:00
groups = sign_groups,
2017-01-16 22:34:40 +00:00
stack_max = 16,
2017-07-26 14:13:19 +00:00
sounds = node_sounds,
2015-06-29 17:55:56 +00:00
2017-07-24 21:59:49 +00:00
on_place = function(itemstack, placer, pointed_thing)
local above = pointed_thing.above
local under = pointed_thing.under
2017-07-24 21:59:49 +00:00
-- Use pointed node's on_rightclick function first, if present
2017-07-26 14:02:55 +00:00
local node_under = minetest.get_node(under)
2017-07-24 21:59:49 +00:00
if placer and not placer:get_player_control().sneak then
2017-07-26 14:02:55 +00:00
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
2017-07-24 21:59:49 +00:00
end
end
2017-07-26 03:02:29 +00:00
local dir = vector.subtract(under, above)
2015-06-29 17:55:56 +00:00
2017-07-24 21:59:49 +00:00
-- Only build when it's legal
local abovenodedef = minetest.registered_nodes[minetest.get_node(above).name]
if not abovenodedef or abovenodedef.buildable_to == false then
return itemstack
end
2017-07-24 21:59:49 +00:00
local wdir = minetest.dir_to_wallmounted(dir)
2015-06-29 17:55:56 +00:00
2017-07-24 21:59:49 +00:00
local placer_pos = placer:getpos()
2015-06-29 17:55:56 +00:00
2017-07-24 21:59:49 +00:00
local fdir = minetest.dir_to_facedir(dir)
2015-06-29 17:55:56 +00:00
2017-07-24 21:59:49 +00:00
local sign_info
2017-07-26 13:11:52 +00:00
local nodeitem = ItemStack(itemstack)
2017-07-26 01:45:22 +00:00
-- Ceiling
2017-07-24 21:59:49 +00:00
if wdir == 0 then
--how would you add sign to ceiling?
return itemstack
2017-07-26 01:45:22 +00:00
-- Floor
2017-07-24 21:59:49 +00:00
elseif wdir == 1 then
2017-07-26 01:45:22 +00:00
-- Standing sign
-- Determine the sign rotation based on player's yaw
local yaw = math.pi*2 - placer:get_look_horizontal()
-- Select one of 16 possible rotations (0-15)
local rotation_level = round((yaw / (math.pi*2)) * 16)
if rotation_level > 15 then
rotation_level = 0
elseif rotation_level < 0 then
rotation_level = 15
end
-- The actual rotation is a combination of predefined mesh and facedir (see node definition)
if rotation_level % 4 == 0 then
2017-07-26 13:11:52 +00:00
nodeitem:set_name("mcl_signs:standing_sign")
2017-07-26 01:45:22 +00:00
elseif rotation_level % 4 == 1 then
2017-07-26 13:11:52 +00:00
nodeitem:set_name("mcl_signs:standing_sign22_5")
2017-07-26 01:45:22 +00:00
elseif rotation_level % 4 == 2 then
2017-07-26 13:11:52 +00:00
nodeitem:set_name("mcl_signs:standing_sign45")
2017-07-26 01:45:22 +00:00
elseif rotation_level % 4 == 3 then
2017-07-26 13:11:52 +00:00
nodeitem:set_name("mcl_signs:standing_sign67_5")
2017-07-26 01:45:22 +00:00
end
fdir = math.floor(rotation_level / 4)
-- Place the node!
2017-07-26 13:11:52 +00:00
local _, success = minetest.item_place_node(nodeitem, placer, pointed_thing, fdir)
2017-07-25 00:44:40 +00:00
if not success then
return itemstack
end
2017-07-26 02:40:44 +00:00
sign_info = signtext_info_standing[rotation_level + 1]
2017-07-26 01:45:22 +00:00
-- Side
2017-07-24 21:59:49 +00:00
else
2017-07-26 01:45:22 +00:00
-- Wall sign
2017-07-25 01:06:25 +00:00
local _, success = minetest.item_place_node(itemstack, placer, pointed_thing, wdir)
2017-07-25 00:44:40 +00:00
if not success then
return itemstack
end
2017-07-26 02:40:44 +00:00
sign_info = signtext_info_wall[fdir + 1]
2017-07-24 21:59:49 +00:00
end
2017-07-26 14:02:55 +00:00
-- Determine spawn position of entity
local place_pos
if minetest.registered_nodes[node_under.name].buildable_to then
place_pos = under
else
place_pos = above
end
2017-07-26 13:11:52 +00:00
local text_entity = minetest.add_entity({
x = place_pos.x + sign_info.delta.x,
y = place_pos.y + sign_info.delta.y,
2017-07-25 00:20:37 +00:00
z = place_pos.z + sign_info.delta.z}, "mcl_signs:text")
2017-07-26 13:11:52 +00:00
text_entity:setyaw(sign_info.yaw)
text_entity:get_luaentity()._signnodename = nodeitem:get_name()
2015-06-29 17:55:56 +00:00
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})
show_formspec(placer, place_pos)
2017-07-24 21:59:49 +00:00
return itemstack
end,
on_destruct = destruct_sign,
2017-07-24 21:59:49 +00:00
on_receive_fields = function(pos, formname, fields, sender)
update_sign(pos, fields, sender)
end,
2015-06-29 17:55:56 +00:00
on_punch = function(pos, node, puncher)
update_sign(pos)
end,
_mcl_hardness = 1,
_mcl_blast_resistance = 5,
2015-06-29 17:55:56 +00:00
})
2017-07-26 01:45:22 +00:00
-- Standing sign nodes.
-- 4 rotations at 0°, 22.5°, 45° and 67.5°.
-- These are 4 out of 16 possible rotations.
-- With facedir the remaining 12 rotations are constructed.
-- 0°
local ssign = {
2017-07-24 21:59:49 +00:00
paramtype = "light",
2015-06-29 17:55:56 +00:00
sunlight_propagates = true,
walkable = false,
is_ground_content = false,
2017-07-24 21:59:49 +00:00
paramtype2 = "facedir",
2017-07-26 01:45:22 +00:00
drawtype = "mesh",
2017-07-26 14:50:26 +00:00
mesh = "mcl_signs_sign.obj",
2017-07-26 02:50:34 +00:00
selection_box = {type = "fixed", fixed = {-0.2, -0.5, -0.2, 0.2, 0.5, 0.2}},
2017-07-26 01:45:22 +00:00
tiles = {"mcl_signs_sign.png"},
2017-07-24 21:59:49 +00:00
groups = sign_groups,
2017-07-25 00:20:37 +00:00
drop = "mcl_signs:wall_sign",
2017-01-16 22:34:40 +00:00
stack_max = 16,
2017-07-26 14:13:19 +00:00
sounds = node_sounds,
2015-06-29 17:55:56 +00:00
on_destruct = destruct_sign,
2017-07-24 21:59:49 +00:00
on_receive_fields = function(pos, formname, fields, sender)
update_sign(pos, fields, sender)
end,
2015-06-29 17:55:56 +00:00
on_punch = function(pos, node, puncher)
update_sign(pos)
end,
_mcl_hardness = 1,
_mcl_blast_resistance = 5,
2017-07-26 01:45:22 +00:00
}
-- 22.5°
minetest.register_node("mcl_signs:standing_sign", ssign)
local ssign22_5 = table.copy(ssign)
2017-07-26 14:50:26 +00:00
ssign22_5.mesh = "mcl_signs_sign22.5.obj"
2017-07-26 01:45:22 +00:00
-- 45°
minetest.register_node("mcl_signs:standing_sign22_5", ssign22_5)
local ssign45 = table.copy(ssign)
2017-07-26 14:50:26 +00:00
ssign45.mesh = "mcl_signs_sign45.obj"
2017-07-26 01:45:22 +00:00
minetest.register_node("mcl_signs:standing_sign45", ssign45)
-- 67.5°
local ssign67 = table.copy(ssign)
2017-07-26 14:50:26 +00:00
ssign67.mesh = "mcl_signs_sign67.5.obj"
2017-07-26 01:45:22 +00:00
minetest.register_node("mcl_signs:standing_sign67_5", ssign67)
2015-06-29 17:55:56 +00:00
2017-07-25 00:20:37 +00:00
minetest.register_entity("mcl_signs:text", {
2017-07-24 21:59:49 +00:00
collisionbox = { 0, 0, 0, 0, 0, 0 },
visual = "upright_sprite",
textures = {},
2017-07-24 23:01:50 +00:00
physical = false,
collide_with_objects = false,
2017-07-24 21:59:49 +00:00
2017-07-26 13:11:52 +00:00
_signnodename = nil, -- node name of sign node to which the text belongs
on_activate = function(self, staticdata)
2017-07-26 13:34:09 +00:00
if staticdata ~= nil and staticdata ~= "" then
local des = minetest.deserialize(staticdata)
if des then
self._signnodename = des._signnodename
end
2017-07-26 13:11:52 +00:00
end
2017-07-24 21:59:49 +00:00
local meta = minetest.get_meta(self.object:getpos())
local text = meta:get_string("text")
2017-07-24 23:01:50 +00:00
self.object:set_properties({
2017-07-26 13:11:52 +00:00
textures={generate_texture(create_lines(text), self._signnodename)},
2017-07-24 23:01:50 +00:00
})
self.object:set_armor_groups({ immortal = 1 })
2017-07-26 13:11:52 +00:00
end,
get_staticdata = function(self)
local out = { _signnodename = self._signnodename }
2017-07-26 13:11:52 +00:00
return minetest.serialize(out)
end,
2015-06-29 17:55:56 +00:00
})
2017-01-10 05:46:56 +00:00
minetest.register_craft({
type = "fuel",
2017-07-25 00:20:37 +00:00
recipe = "mcl_signs:wall_sign",
2017-01-10 05:46:56 +00:00
burntime = 10,
})
2017-02-16 20:47:47 +00:00
2017-07-26 14:13:19 +00:00
if minetest.get_modpath("mcl_core") then
minetest.register_craft({
output = 'mcl_signs:wall_sign 3',
recipe = {
{'group:wood', 'group:wood', 'group:wood'},
{'group:wood', 'group:wood', 'group:wood'},
{'', 'mcl_core:stick', ''},
}
})
end
2017-02-16 20:47:47 +00:00
if minetest.get_modpath("doc") then
2017-07-25 00:20:37 +00:00
doc.add_entry_alias("nodes", "mcl_signs:wall_sign", "nodes", "mcl_signs:standing_sign")
2017-07-26 14:13:19 +00:00
doc.add_entry_alias("nodes", "mcl_signs:wall_sign", "nodes", "mcl_signs:standing_sign22_5")
doc.add_entry_alias("nodes", "mcl_signs:wall_sign", "nodes", "mcl_signs:standing_sign45")
doc.add_entry_alias("nodes", "mcl_signs:wall_sign", "nodes", "mcl_signs:standing_sign67_5")
end
2017-07-25 00:20:37 +00:00
minetest.register_alias("signs:sign_wall", "mcl_signs:wall_sign")
minetest.register_alias("signs:sign_yard", "mcl_signs:standing_sign")
2017-07-26 02:48:31 +00:00
if minetest.settings:get_bool("log_mods") then
2017-07-26 02:48:31 +00:00
minetest.log("action", "[mcl_signs] loaded")
end