updated mesecons and signs_lib

This commit is contained in:
Vanessa Ezekowitz 2016-09-09 06:21:35 -04:00
parent 10466bea04
commit def6e46501
7 changed files with 324 additions and 279 deletions

View File

@ -87,7 +87,11 @@ minetest.register_globalstep(function (dtime)
end)
function mesecon.queue:execute(action)
mesecon.queue.funcs[action.func](action.pos, unpack(action.params))
-- ignore if action queue function name doesn't exist,
-- (e.g. in case the action queue savegame was written by an old mesecons version)
if mesecon.queue.funcs[action.func] then
mesecon.queue.funcs[action.func](action.pos, unpack(action.params))
end
end

View File

@ -70,6 +70,8 @@ dofile(minetest.get_modpath("mesecons").."/internal.lua");
-- these are the only functions you need to remember
mesecon.queue:add_function("receptor_on", function (pos, rules)
mesecon.vm_begin()
rules = rules or mesecon.rules.default
-- if area (any of the rule targets) is not loaded, keep trying and call this again later
@ -90,6 +92,8 @@ mesecon.queue:add_function("receptor_on", function (pos, rules)
mesecon.turnon(np, rulename)
end
end
mesecon.vm_commit()
end)
function mesecon.receptor_on(pos, rules)
@ -112,10 +116,16 @@ mesecon.queue:add_function("receptor_off", function (pos, rules)
local np = vector.add(pos, rule)
local rulenames = mesecon.rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do
if not mesecon.connected_to_receptor(np, mesecon.invertRule(rule)) then
mesecon.turnoff(np, rulename)
mesecon.vm_begin()
mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
-- Turnoff returns true if turnoff process was successful, no onstate receptor
-- was found along the way. Commit changes that were made in voxelmanip. If turnoff
-- returns true, an onstate receptor was found, abort voxelmanip transaction.
if (mesecon.turnoff(np, rulename)) then
mesecon.vm_commit()
else
mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
mesecon.vm_abort()
end
end
end

View File

@ -37,11 +37,6 @@
-- HIGH-LEVEL Internals
-- mesecon.is_power_on(pos) --> Returns true if pos emits power in any way
-- mesecon.is_power_off(pos) --> Returns true if pos does not emit power in any way
-- mesecon.turnon(pos, link) --> link is the input rule that caused calling turnon, turns on every connected node, iterative
-- mesecon.turnoff(pos, link) --> link is the input rule that caused calling turnoff, turns off every connected node, iterative
-- mesecon.connected_to_receptor(pos, link) --> Returns true if pos is connected to a receptor directly or via conductors, iterative
-- mesecon.rules_link(output, input, dug_outputrules) --> Returns true if outputposition + outputrules = inputposition and inputposition + inputrules = outputposition (if the two positions connect)
-- mesecon.rules_link_anydir(outp., inp., d_outpr.) --> Same as rules mesecon.rules_link but also returns true if output and input are swapped
-- mesecon.is_powered(pos) --> Returns true if pos is powered by a receptor or a conductor
-- RULES ROTATION helpers
@ -371,52 +366,32 @@ function mesecon.is_power_off(pos, rulename)
return false
end
-- Turn off an equipotential section starting at `pos`, which outputs in the direction of `link`.
-- Breadth-first search. Map is abstracted away in a voxelmanip.
-- Follow all all conductor paths replacing conductors that were already
-- looked at, activating / changing all effectors along the way.
function mesecon.turnon(pos, link)
local frontiers = {{pos = pos, link = link}}
local depth = 1
while frontiers[depth] do
local f = frontiers[depth]
while frontiers[1] do
local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos)
-- area not loaded, postpone action
if not node then
mesecon.queue:add_action(f.pos, "turnon", {f.link}, nil, true)
-- Area does not exist; do nothing
elseif mesecon.is_conductor_off(node, f.link) then
local rules = mesecon.conductor_get_rules(node)
-- Success: If false, at least one neighboring node is unloaded,
-- postpone turning on action
local success = true
local neighborlinks = {}
-- call turnon on neighbors
-- Call turnon on neighbors
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r)
-- Neighboring node not loaded, postpone turning on current node
-- since we can't even know if neighboring node has matching rules
if not mesecon.get_node_force(np) then
success = false
break
else
neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r)
for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
table.insert(frontiers, {pos = np, link = l})
end
end
if success then
minetest.swap_node(f.pos, {name = mesecon.get_conductor_on(node, f.link),
param2 = node.param2})
for npos, links in pairs(neighborlinks) do
-- links = all links to node, l = each single link
for _, l in ipairs(links) do
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
end
end
else
mesecon.queue:add_action(f.pos, "turnon", {f.link}, nil, true)
end
mesecon.swap_node_force(f.pos, mesecon.get_conductor_on(node, f.link))
elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.on, depth)
if mesecon.is_effector_off(node.name) then
@ -427,151 +402,77 @@ function mesecon.turnon(pos, link)
end
end
mesecon.queue:add_function("turnon", function(pos, rulename, recdepth)
mesecon.turnon(pos, rulename, recdepth)
end)
-- Turn on an equipotential section starting at `pos`, which outputs in the direction of `link`.
-- Breadth-first search. Map is abstracted away in a voxelmanip.
-- Follow all all conductor paths replacing conductors that were already
-- looked at, deactivating / changing all effectors along the way.
-- In case an onstate receptor is discovered, abort the process by returning false, which will
-- cause `receptor_off` to discard all changes made in the voxelmanip.
-- Contrary to turnon, turnoff has to cache all change and deactivate signals so that they will only
-- be called in the very end when we can be sure that no conductor was found along the path.
--
-- Signal table entry structure:
-- {
-- pos = position of effector,
-- node = node descriptor (name, param1 and param2),
-- link = link the effector is connected to,
-- depth = indicates order in which signals wire fired, higher is later
-- }
function mesecon.turnoff(pos, link)
local frontiers = {{pos = pos, link = link}}
local signals = {}
local depth = 1
while frontiers[depth] do
local f = frontiers[depth]
while frontiers[1] do
local f = table.remove(frontiers, 1)
local node = mesecon.get_node_force(f.pos)
-- area not loaded, postpone action
if not node then
mesecon.queue:add_action(f.pos, "turnoff", {f.link}, nil, true)
-- Area does not exist; do nothing
elseif mesecon.is_conductor_on(node, f.link) then
local rules = mesecon.conductor_get_rules(node)
-- Success: If false, at least one neighboring node is unloaded,
-- postpone turning on action
local success = true
local neighborlinks = {}
-- call turnoff on neighbors
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r)
-- Neighboring node not loaded, postpone turning off current node
-- since we can't even know if neighboring node has matching rules
if not mesecon.get_node_force(np) then
success = false
break
else
neighborlinks[minetest.hash_node_position(np)] = mesecon.rules_link_rule_all(f.pos, r)
end
end
if success then
minetest.swap_node(f.pos, {name = mesecon.get_conductor_off(node, f.link),
param2 = node.param2})
for npos, links in pairs(neighborlinks) do
-- links = all links to node, l = each single link
for _, l in ipairs(links) do
table.insert(frontiers, {pos = minetest.get_position_from_hash(npos), link = l})
-- Check if an onstate receptor is connected. If that is the case,
-- abort this turnoff process by returning false. `receptor_off` will
-- discard all the changes that we made in the voxelmanip:
for _, l in ipairs(mesecon.rules_link_rule_all_inverted(f.pos, r)) do
if mesecon.is_receptor_on(mesecon.get_node_force(np).name) then
return false
end
end
else
mesecon.queue:add_action(f.pos, "turnoff", {f.link}, nil, true)
-- Call turnoff on neighbors
for _, l in ipairs(mesecon.rules_link_rule_all(f.pos, r)) do
table.insert(frontiers, {pos = np, link = l})
end
end
mesecon.swap_node_force(f.pos, mesecon.get_conductor_off(node, f.link))
elseif mesecon.is_effector(node.name) then
mesecon.changesignal(f.pos, node, f.link, mesecon.state.off, depth)
if mesecon.is_effector_on(node.name) and not mesecon.is_powered(f.pos) then
mesecon.deactivate(f.pos, node, f.link, depth)
end
table.insert(signals, {
pos = f.pos,
node = node,
link = f.link,
depth = depth
})
end
depth = depth + 1
end
end
mesecon.queue:add_function("turnoff", function(pos, rulename, recdepth)
mesecon.turnoff(pos, rulename, recdepth)
end)
function mesecon.connected_to_receptor(pos, link)
local node = mesecon.get_node_force(pos)
if not node then return false end
-- Check if conductors around are connected
local rules = mesecon.get_any_inputrules(node)
if not rules then return false end
for _, rule in ipairs(mesecon.rule2meta(link, rules)) do
local links = mesecon.rules_link_rule_all_inverted(pos, rule)
for _, l in ipairs(links) do
local np = vector.add(pos, l)
if mesecon.find_receptor_on(np, mesecon.invertRule(l)) then
return true
end
for _, sig in ipairs(signals) do
mesecon.changesignal(sig.pos, sig.node, sig.link, mesecon.state.off, sig.depth)
if mesecon.is_effector_on(sig.node.name) and not mesecon.is_powered(sig.pos) then
mesecon.deactivate(sig.pos, sig.node, sig.link, sig.depth)
end
end
return false
end
function mesecon.find_receptor_on(pos, link)
local frontiers = {{pos = pos, link = link}}
local checked = {}
-- List of positions that have been searched for onstate receptors
local depth = 1
while frontiers[depth] do
local f = frontiers[depth]
local node = mesecon.get_node_force(f.pos)
if not node then return false end
if mesecon.is_receptor_on(node.name) then return true end
if mesecon.is_conductor_on(node, f.link) then
local rules = mesecon.conductor_get_rules(node)
-- call turnoff on neighbors: normal rules
for _, r in ipairs(mesecon.rule2meta(f.link, rules)) do
local np = vector.add(f.pos, r)
local links = mesecon.rules_link_rule_all_inverted(f.pos, r)
for _, l in ipairs(links) do
local checkedstring = np.x..np.y..np.z..l.x..l.y..l.z
if not checked[checkedstring] then
table.insert(frontiers, {pos = np, link = l})
checked[checkedstring] = true
end
end
end
end
depth = depth + 1
end
end
function mesecon.rules_link(output, input, dug_outputrules) --output/input are positions (outputrules optional, used if node has been dug), second return value: the name of the affected input rule
local outputnode = mesecon.get_node_force(output)
local inputnode = mesecon.get_node_force(input)
local outputrules = dug_outputrules or mesecon.get_any_outputrules(outputnode)
local inputrules = mesecon.get_any_inputrules(inputnode)
if not outputrules or not inputrules then
return
end
for _, outputrule in ipairs(mesecon.flattenrules(outputrules)) do
-- Check if output sends to input
if vector.equals(vector.add(output, outputrule), input) then
for _, inputrule in ipairs(mesecon.flattenrules(inputrules)) do
-- Check if input accepts from output
if vector.equals(vector.add(input, inputrule), output) then
return true, inputrule
end
end
end
end
return false
return true
end
-- Get all linking inputrules of inputnode (effector or conductor) that is connected to
-- outputnode (receptor or conductor) at position `output` and has an output in direction `rule`
function mesecon.rules_link_rule_all(output, rule)
local input = vector.add(output, rule)
local inputnode = mesecon.get_node_force(input)
@ -591,8 +492,9 @@ function mesecon.rules_link_rule_all(output, rule)
return rules
end
-- Get all linking outputnodes of outputnode (receptor or conductor) that is connected to
-- inputnode (effector or conductor) at position `input` and has an input in direction `rule`
function mesecon.rules_link_rule_all_inverted(input, rule)
--local irule = mesecon.invertRule(rule)
local output = vector.add(input, rule)
local outputnode = mesecon.get_node_force(output)
local outputrules = mesecon.get_any_outputrules(outputnode)
@ -609,10 +511,6 @@ function mesecon.rules_link_rule_all_inverted(input, rule)
return rules
end
function mesecon.rules_link_anydir(pos1, pos2)
return mesecon.rules_link(pos1, pos2) or mesecon.rules_link(pos2, pos1)
end
function mesecon.is_powered(pos, rule)
local node = mesecon.get_node_force(pos)
local rules = mesecon.get_any_inputrules(node)

View File

@ -1,30 +1,14 @@
-- Ugly hack to prevent breaking compatibility with other mods
-- Just remove the following two functions to delete the hack, to be done when other mods have updated
function mesecon.receptor_on(self, pos, rules)
if (self.receptor_on) then
print("[Mesecons] Warning: A mod with mesecon support called mesecon:receptor_on.")
print("[Mesecons] If you are the programmer of this mod, please update it ")
print("[Mesecons] to use mesecon.receptor_on instead. mesecon:* is deprecated")
print("[Mesecons] Otherwise, please make sure you're running the latest version")
print("[Mesecons] of that mod and inform the mod creator.")
else
rules = pos
pos = self
end
mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
-- Un-forceload any forceloaded mapblocks from older versions of Mesecons which
-- used forceloading instead of VoxelManipulators.
local BLOCKSIZE = 16
-- convert block hash --> node position
local function unhash_blockpos(hash)
return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE)
end
function mesecon.receptor_off(self, pos, rules)
if (self.receptor_off) then
print("[Mesecons] Warning: A mod with mesecon support called mesecon:receptor_off.")
print("[Mesecons] If you are the programmer of this mod, please update it ")
print("[Mesecons] to use mesecon.receptor_off instead. mesecon:* is deprecated")
print("[Mesecons] Otherwise, please make sure you're running the latest version")
print("[Mesecons] of that mod and inform the mod creator.")
else
rules = pos
pos = self
end
mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
local old_forceloaded_blocks = mesecon.file2table("mesecon_forceloaded")
for hash, _ in pairs(old_forceloaded_blocks) do
minetest.forceload_free_block(unhash_blockpos(hash))
end
os.remove(minetest.get_worldpath()..DIR_DELIM.."mesecon_forceloaded")

View File

@ -219,7 +219,7 @@ function mesecon.table2file(filename, table)
f:close()
end
-- Forceloading: Force server to load area if node is nil
-- Block position "hashing" (convert to integer) functions for voxelmanip cache
local BLOCKSIZE = 16
-- convert node position --> block hash
@ -236,43 +236,132 @@ local function unhash_blockpos(hash)
return vector.multiply(minetest.get_position_from_hash(hash), BLOCKSIZE)
end
mesecon.forceloaded_blocks = {}
-- Maps from a hashed mapblock position (as returned by hash_blockpos) to a
-- table.
--
-- Contents of the table are:
-- “vm” → the VoxelManipulator
-- “va” → the VoxelArea
-- “data” → the data array
-- “param1” → the param1 array
-- “param2” → the param2 array
-- “dirty” → true if data has been modified
--
-- Nil if no VM-based transaction is in progress.
local vm_cache = nil
-- get node and force-load area
function mesecon.get_node_force(pos)
local hash = hash_blockpos(pos)
if mesecon.forceloaded_blocks[hash] == nil then
-- if no more forceload spaces are available, try again next time
if minetest.forceload_block(pos) then
mesecon.forceloaded_blocks[hash] = 0
end
else
mesecon.forceloaded_blocks[hash] = 0
end
return minetest.get_node_or_nil(pos)
-- Starts a VoxelManipulator-based transaction.
--
-- During a VM transaction, calls to vm_get_node and vm_swap_node operate on a
-- cached copy of the world loaded via VoxelManipulators. That cache can later
-- be committed to the real map by means of vm_commit or discarded by means of
-- vm_abort.
function mesecon.vm_begin()
vm_cache = {}
end
minetest.register_globalstep(function (dtime)
for hash, time in pairs(mesecon.forceloaded_blocks) do
-- unload forceloaded blocks after 10 minutes without usage
if (time > mesecon.setting("forceload_timeout", 600)) then
minetest.forceload_free_block(unhash_blockpos(hash))
mesecon.forceloaded_blocks[hash] = nil
else
mesecon.forceloaded_blocks[hash] = time + dtime
-- Finishes a VoxelManipulator-based transaction, freeing the VMs and map data
-- and writing back any modified areas.
function mesecon.vm_commit()
for hash, tbl in pairs(vm_cache) do
if tbl.dirty then
local vm = tbl.vm
vm:set_data(tbl.data)
vm:write_to_map()
vm:update_map()
end
end
end)
vm_cache = nil
end
-- Store and read the forceloaded blocks to / from a file
-- so that those blocks are remembered when the game
-- is restarted
mesecon.forceloaded_blocks = mesecon.file2table("mesecon_forceloaded")
minetest.register_on_shutdown(function()
mesecon.table2file("mesecon_forceloaded", mesecon.forceloaded_blocks)
end)
-- Finishes a VoxelManipulator-based transaction, freeing the VMs and throwing
-- away any modified areas.
function mesecon.vm_abort()
vm_cache = nil
end
-- Gets the cache entry covering a position, populating it if necessary.
local function vm_get_or_create_entry(pos)
local hash = hash_blockpos(pos)
local tbl = vm_cache[hash]
if not tbl then
local vm = minetest.get_voxel_manip(pos, pos)
local min_pos, max_pos = vm:get_emerged_area()
local va = VoxelArea:new{MinEdge = min_pos, MaxEdge = max_pos}
tbl = {vm = vm, va = va, data = vm:get_data(), param1 = vm:get_light_data(), param2 = vm:get_param2_data(), dirty = false}
vm_cache[hash] = tbl
end
return tbl
end
-- Gets the node at a given position during a VoxelManipulator-based
-- transaction.
function mesecon.vm_get_node(pos)
local tbl = vm_get_or_create_entry(pos)
local index = tbl.va:indexp(pos)
local node_value = tbl.data[index]
if node_value == core.CONTENT_IGNORE then
return nil
else
local node_param1 = tbl.param1[index]
local node_param2 = tbl.param2[index]
return {name = minetest.get_name_from_content_id(node_value), param1 = node_param1, param2 = node_param2}
end
end
-- Sets a nodes name during a VoxelManipulator-based transaction.
--
-- Existing param1, param2, and metadata are left alone.
function mesecon.vm_swap_node(pos, name)
local tbl = vm_get_or_create_entry(pos)
local index = tbl.va:indexp(pos)
tbl.data[index] = minetest.get_content_id(name)
tbl.dirty = true
end
-- Gets the node at a given position, regardless of whether it is loaded or
-- not, respecting a transaction if one is in progress.
--
-- Outside a VM transaction, if the mapblock is not loaded, it is pulled into
-- the servers main map data cache and then accessed from there.
--
-- Inside a VM transaction, the transactions VM cache is used.
function mesecon.get_node_force(pos)
if vm_cache then
return mesecon.vm_get_node(pos)
else
local node = minetest.get_node_or_nil(pos)
if node == nil then
-- Node is not currently loaded; use a VoxelManipulator to prime
-- the mapblock cache and try again.
minetest.get_voxel_manip(pos, pos)
node = minetest.get_node_or_nil(pos)
end
return node
end
end
-- Swaps the node at a given position, regardless of whether it is loaded or
-- not, respecting a transaction if one is in progress.
--
-- Outside a VM transaction, if the mapblock is not loaded, it is pulled into
-- the servers main map data cache and then accessed from there.
--
-- Inside a VM transaction, the transactions VM cache is used.
--
-- This function can only be used to change the nodes name, not its parameters
-- or metadata.
function mesecon.swap_node_force(pos, name)
if vm_cache then
return mesecon.vm_swap_node(pos, name)
else
-- This serves to both ensure the mapblock is loaded and also hand us
-- the old node table so we can preserve param2.
local node = mesecon.get_node_force(pos)
node.name = name
minetest.swap_node(pos, node)
end
end
-- Autoconnect Hooks
-- Nodes like conductors may change their appearance and their connection rules

View File

@ -41,10 +41,10 @@ signs_lib.regular_wall_sign_model = {
textpos = {
nil,
nil,
{delta = {x = 0.43, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = {x = -0.43, y = 0.07, z = 0 }, yaw = math.pi / 2},
{delta = {x = 0, y = 0.07, z = 0.43 }, yaw = 0},
{delta = {x = 0, y = 0.07, z = -0.43 }, yaw = math.pi},
{delta = { x = 0.41, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = { x = -0.41, y = 0.07, z = 0 }, yaw = math.pi / 2},
{delta = { x = 0, y = 0.07, z = 0.41 }, yaw = 0},
{delta = { x = 0, y = 0.07, z = -0.41 }, yaw = math.pi},
}
}
@ -54,10 +54,10 @@ signs_lib.metal_wall_sign_model = {
fixed = {-0.4375, -0.25, 0.4375, 0.4375, 0.375, 0.5}
},
textpos = {
{delta = {x = 0, y = 0.07, z = 0.43 }, yaw = 0},
{delta = {x = 0.43, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = {x = 0, y = 0.07, z = -0.43 }, yaw = math.pi},
{delta = {x = -0.43, y = 0.07, z = 0 }, yaw = math.pi / 2},
{delta = { x = 0, y = 0.07, z = 0.41 }, yaw = 0},
{delta = { x = 0.41, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = { x = 0, y = 0.07, z = -0.41 }, yaw = math.pi},
{delta = { x = -0.41, y = 0.07, z = 0 }, yaw = math.pi / 2},
}
}
@ -70,10 +70,10 @@ signs_lib.yard_sign_model = {
}
},
textpos = {
{delta = {x = 0, y = 0.07, z = -0.068}, yaw = 0},
{delta = {x = -0.068, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = {x = 0, y = 0.07, z = 0.068}, yaw = math.pi},
{delta = {x = 0.068, y = 0.07, z = 0 }, yaw = math.pi / 2},
{delta = { x = 0, y = 0.07, z = -0.08 }, yaw = 0},
{delta = { x = -0.08, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = { x = 0, y = 0.07, z = 0.08 }, yaw = math.pi},
{delta = { x = 0.08, y = 0.07, z = 0 }, yaw = math.pi / 2},
}
}
@ -86,10 +86,10 @@ signs_lib.hanging_sign_model = {
}
},
textpos = {
{delta = {x = 0, y = -0.02, z = -0.063}, yaw = 0},
{delta = {x = -0.063, y = -0.02, z = 0 }, yaw = math.pi / -2},
{delta = {x = 0, y = -0.02, z = 0.063}, yaw = math.pi},
{delta = {x = 0.063, y = -0.02, z = 0 }, yaw = math.pi / 2},
{delta = { x = 0, y = -0.02, z = -0.08 }, yaw = 0},
{delta = { x = -0.08, y = -0.02, z = 0 }, yaw = math.pi / -2},
{delta = { x = 0, y = -0.02, z = 0.08 }, yaw = math.pi},
{delta = { x = 0.08, y = -0.02, z = 0 }, yaw = math.pi / 2},
}
}
@ -102,10 +102,10 @@ signs_lib.sign_post_model = {
}
},
textpos = {
{delta = {x = 0, y = 0.07, z = -0.188}, yaw = 0},
{delta = {x = -0.188, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = {x = 0, y = 0.07, z = 0.188 }, yaw = math.pi},
{delta = {x = 0.188, y = 0.07, z = 0 }, yaw = math.pi / 2},
{delta = { x = 0, y = 0.07, z = -0.2 }, yaw = 0},
{delta = { x = -0.2, y = 0.07, z = 0 }, yaw = math.pi / -2},
{delta = { x = 0, y = 0.07, z = 0.2 }, yaw = math.pi},
{delta = { x = 0.2, y = 0.07, z = 0 }, yaw = math.pi / 2},
}
}
@ -141,6 +141,9 @@ else
default_sign_image = "default_sign_wall.png"
end
default_sign_metal = "default:sign_wall_steel"
default_sign_metal_image = "default_sign_steel.png"
--table copy
function signs_lib.table_copy(t)
@ -301,14 +304,16 @@ local function fill_line(x, y, w, c)
return table.concat(tex)
end
local function make_line_texture(line, lineno)
local function make_line_texture(line, lineno, pos)
local width = 0
local maxw = 0
local words = { }
local n = minetest.registered_nodes[minetest.get_node(pos).name]
local defaultcolor = n.defaultcolor or 0
local cur_color = 0
local cur_color = tonumber(defaultcolor, 16)
-- We check which chars are available here.
for word_i, word in ipairs(line) do
@ -389,12 +394,12 @@ local function make_line_texture(line, lineno)
return table.concat(texture), lineno
end
local function make_sign_texture(lines)
local function make_sign_texture(lines, pos)
local texture = { ("[combine:%dx%d"):format(SIGN_WIDTH, LINE_HEIGHT * NUMBER_OF_LINES) }
local lineno = 0
for i = 1, #lines do
if lineno >= NUMBER_OF_LINES then break end
local linetex, ln = make_line_texture(lines[i], lineno)
local linetex, ln = make_line_texture(lines[i], lineno, pos)
table.insert(texture, linetex)
lineno = ln + 1
end
@ -402,10 +407,10 @@ local function make_sign_texture(lines)
return table.concat(texture, "")
end
local function set_obj_text(obj, text, new)
local function set_obj_text(obj, text, new, pos)
local split = new and split_lines_and_words or split_lines_and_words_old
obj:set_properties({
textures={make_sign_texture(split(text))},
textures={make_sign_texture(split(text), pos)},
visual_size = TEXT_SCALE,
})
end
@ -495,7 +500,7 @@ signs_lib.update_sign = function(pos, fields, owner)
if found then
v:remove()
else
set_obj_text(v, text, new)
set_obj_text(v, text, new, pos)
found = true
end
end
@ -507,12 +512,17 @@ signs_lib.update_sign = function(pos, fields, owner)
-- if there is no entity
local sign_info
local signnode = minetest.get_node(pos)
if signnode.name == "signs:sign_yard" then
local signname = signnode.name
local textpos = minetest.registered_nodes[signname].textpos
if textpos then
sign_info = textpos[minetest.get_node(pos).param2 + 1]
elseif signnode.name == "signs:sign_yard" then
sign_info = signs_lib.yard_sign_model.textpos[minetest.get_node(pos).param2 + 1]
elseif signnode.name == "signs:sign_hanging" then
sign_info = signs_lib.hanging_sign_model.textpos[minetest.get_node(pos).param2 + 1]
elseif string.find(signnode.name, "sign_wall") then
if signnode.name == default_sign
or signnode.name == default_sign_metal
or signnode.name == "locked_sign:sign_wall_locked" then
sign_info = signs_lib.regular_wall_sign_model.textpos[minetest.get_node(pos).param2 + 1]
else
@ -577,7 +587,6 @@ function signs_lib.determine_sign_type(itemstack, placer, pointed_thing, locked)
end
local fdir = minetest.dir_to_facedir(dir)
local pt_name = minetest.get_node(under).name
local signname = itemstack:get_name()
@ -587,8 +596,11 @@ function signs_lib.determine_sign_type(itemstack, placer, pointed_thing, locked)
minetest.add_node(above, {name = "signs:sign_hanging", param2 = fdir})
elseif wdir == 1 and signname == default_sign then
minetest.add_node(above, {name = "signs:sign_yard", param2 = fdir})
elseif signname == default_sign_metal then
minetest.add_node(above, {name = signname, param2 = wdir })
elseif signname ~= default_sign
and signname ~= "locked_sign:sign_wall_locked" then -- it's a metal wall sign.
and signname ~= default_sign_metal
and signname ~= "locked_sign:sign_wall_locked" then -- it's a signs_lib colored metal wall sign.
minetest.add_node(above, {name = signname, param2 = fdir})
else -- it must be a default or locked wooden wall sign
minetest.add_node(above, {name = signname, param2 = wdir }) -- note it's wallmounted here!
@ -793,11 +805,47 @@ minetest.register_node(":locked_sign:sign_wall_locked", {
on_rotate = signs_lib.wallmounted_rotate
})
-- default metal sign, if defined
if minetest.registered_nodes["default:sign_wall_steel"] then
minetest.register_node(":"..default_sign_metal, {
description = S("Sign"),
inventory_image = default_sign_metal_image,
wield_image = default_sign_metal_image,
node_placement_prediction = "",
sunlight_propagates = true,
paramtype = "light",
paramtype2 = "wallmounted",
drawtype = "nodebox",
node_box = signs_lib.regular_wall_sign_model.nodebox,
tiles = {"signs_wall_sign_metal.png"},
groups = sign_groups,
on_place = function(itemstack, placer, pointed_thing)
return signs_lib.determine_sign_type(itemstack, placer, pointed_thing)
end,
on_construct = function(pos)
signs_lib.construct_sign(pos)
end,
on_destruct = function(pos)
signs_lib.destruct_sign(pos)
end,
on_receive_fields = function(pos, formname, fields, sender)
signs_lib.receive_fields(pos, formname, fields, sender)
end,
on_punch = function(pos, node, puncher)
signs_lib.update_sign(pos)
end,
on_rotate = signs_lib.wallmounted_rotate
})
end
-- metal, colored signs
local sign_colors = { "green", "yellow", "red", "white_red", "white_black", "orange", "blue", "brown" }
local sign_default_text_colors = { "f", "0", "f", "4", "0", "0", "f", "f" }
for _, color in ipairs(sign_colors) do
for i, color in ipairs(sign_colors) do
minetest.register_node(":signs:sign_wall_"..color, {
description = S("Sign ("..color..", metal)"),
inventory_image = "signs_"..color.."_inv.png",
@ -816,6 +864,7 @@ for _, color in ipairs(sign_colors) do
"signs_metal_back.png",
"signs_"..color.."_front.png"
},
defaultcolor = sign_default_text_colors[i],
groups = sign_groups,
on_place = function(itemstack, placer, pointed_thing)
return signs_lib.determine_sign_type(itemstack, placer, pointed_thing)
@ -838,12 +887,13 @@ end
local signs_text_on_activate
signs_text_on_activate = function(self)
local meta = minetest.get_meta(self.object:getpos())
local pos = self.object:getpos()
local meta = minetest.get_meta(pos)
local text = meta:get_string("text")
local new = (meta:get_int("__signslib_new_format") ~= 0)
if text then
text = trim_input(text)
set_obj_text(self.object, text, new)
set_obj_text(self.object, text, new, pos)
end
end
@ -934,13 +984,15 @@ minetest.register_alias("sign_wall_locked", "locked_sign:sign_wall_locked")
signs_lib.register_fence_with_sign("default:fence_wood", "signs:sign_post")
-- restore signs' text after /clearobjects and the like
-- restore signs' text after /clearobjects and the like, the next time
-- a block is reloaded by the server.
minetest.register_abm({
minetest.register_lbm({
nodenames = signs_lib.sign_node_list,
interval = 15,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
name = "signs_lib:restore_sign_text",
label = "Restore sign text",
run_at_every_load = true,
action = function(pos, node)
signs_lib.update_sign(pos)
end
})
@ -956,7 +1008,7 @@ minetest.register_craft({
}
})
--Alternate recipe.
--Alternate recipes.
minetest.register_craft({
output = "locked_sign:sign_wall_locked",
@ -966,13 +1018,21 @@ minetest.register_craft({
},
})
minetest.register_craft({
output = "locked_sign:sign_wall_locked",
recipe = {
{default_sign_metal},
{"default:steel_ingot"},
},
})
-- craft recipes for the metal signs
minetest.register_craft( {
output = "signs:sign_wall_green 4",
output = "signs:sign_wall_green",
recipe = {
{ "dye:dark_green", "dye:white", "dye:dark_green" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -985,10 +1045,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_yellow 4",
output = "signs:sign_wall_yellow",
recipe = {
{ "dye:yellow", "dye:black", "dye:yellow" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -1001,10 +1061,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_red 4",
output = "signs:sign_wall_red",
recipe = {
{ "dye:red", "dye:white", "dye:red" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -1017,10 +1077,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_white_red 4",
output = "signs:sign_wall_white_red",
recipe = {
{ "dye:white", "dye:red", "dye:white" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -1033,10 +1093,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_white_black 4",
output = "signs:sign_wall_white_black",
recipe = {
{ "dye:white", "dye:black", "dye:white" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -1049,10 +1109,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_orange 4",
output = "signs:sign_wall_orange",
recipe = {
{ "dye:orange", "dye:black", "dye:orange" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -1065,10 +1125,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_blue 4",
output = "signs:sign_wall_blue",
recipe = {
{ "dye:blue", "dye:white", "dye:blue" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})
@ -1081,10 +1141,10 @@ minetest.register_craft( {
})
minetest.register_craft( {
output = "signs:sign_wall_brown 4",
output = "signs:sign_wall_brown",
recipe = {
{ "dye:brown", "dye:white", "dye:brown" },
{ "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }
{ "", default_sign_metal, "" }
},
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 553 B