1
0
mirror of https://github.com/mt-mods/pipeworks.git synced 2025-07-10 12:10:34 +02:00
This commit is contained in:
OgelGames
2024-02-28 23:53:38 +11:00
parent 7e4b735a44
commit 6b06dbf32b
7 changed files with 146 additions and 154 deletions

View File

@ -809,45 +809,6 @@ local function go_back(velocity)
return pipeworks.notvel(adjlist, vel)
end
local function get_item_table(stack)
local item = stack:to_table()
if pipeworks.enable_item_tags then
item.tag = pipeworks.get_item_tag(stack)
end
return item
end
local function parse_returned_msg_v1(msg, _, velocity)
if type(msg) ~= "string" then
return false
end
local r = rules[msg]
return true, r and { r } or go_back(velocity)
end
local function parse_returned_msg_v2(msg, stack, velocity)
if type(msg) ~= "table" or type(msg.side) ~= "string" then
return false
end
local r = rules[msg.side]
if pipeworks.enable_item_tags and r and type(msg.tag) == "string" then
pipeworks.set_item_tag(stack, msg.tag)
end
return true, r and { r } or go_back(velocity)
end
local function parse_returned_msg(msg, stack, velocity)
local parsers = {
parse_returned_msg_v2,
parse_returned_msg_v1,
}
for _, parser in ipairs(parsers) do
local is_processed, result = parser(msg, stack, velocity)
if is_processed then return result end
end
return go_back(velocity)
end
local tiles_base = {
"pipeworks_mese_tube_plain_4.png", "pipeworks_mese_tube_plain_3.png",
"pipeworks_mese_tube_plain_2.png", "pipeworks_mese_tube_plain_1.png",
@ -984,7 +945,7 @@ for white = 0, 1 do
tube = {
connect_sides = {front = 1, back = 1, left = 1, right = 1, top = 1, bottom = 1},
priority = 50,
can_go = function(pos, node, velocity, stack)
can_go = function(pos, node, velocity, stack, tags)
local src = {name = nil}
-- add color of the incoming tube explicitly; referring to rules, in case they change later
for _, rule in pairs(rules) do
@ -997,13 +958,35 @@ for white = 0, 1 do
type = "item",
pin = src,
itemstring = stack:to_string(),
item = get_item_table(stack),
item = stack:to_table(),
velocity = velocity,
tags = table.copy(tags),
side = src.name,
})
if not succ then
return go_back(velocity)
end
return parse_returned_msg(msg, stack, velocity)
if type(msg) == "string" then
local side = rules[msg]
return side and {side} or go_back(velocity)
elseif type(msg) == "table" then
if pipeworks.enable_item_tags then
local new_tags
if type(msg.tags) == "table" then
new_tags = msg.tags
elseif type(msg.tag) == "string" then
new_tags = {msg.tag}
end
if new_tags then
for i=1, math.max(#tags, #new_tags) do
tags[i] = new_tags[i]
end
end
end
local side = rules[msg.side]
return side and {side} or go_back(velocity)
end
return go_back(velocity)
end,
},
after_place_node = pipeworks.after_place,

View File

@ -1,61 +1,51 @@
local S = minetest.get_translator("pipeworks")
local fs_helpers = pipeworks.fs_helpers
if not pipeworks.enable_tags_tube then return end
if not pipeworks.enable_item_tags or not pipeworks.enable_tag_tube then return end
local notag_name = "<<notag>>"
local tube_name = "pipeworks:tags_tube"
local tube_description = S("Tags Sorting Pneumatic Tube Segment")
local safe_tags = function(tags)
local length_limit = tonumber(minetest.settings:get("pipeworks_item_tag_name_limit") or "30")
return tags:sub(1, length_limit * 6)
end
local function escape(s)
return (s:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]', '%%%1'))
end
local help_text = minetest.formspec_escape(
S("Separate multiple tags using commas.").."\n"..
S("Use \"<none>\" to match items without tags.")
)
local update_formspec = function(pos)
local meta = minetest.get_meta(pos)
local buttons_formspec = ""
for i = 0, 5 do
buttons_formspec = buttons_formspec .. fs_helpers.cycling_button(meta,
"image_button[9," .. (i + (i * 0.25) + 1.5) .. ";1,0.6", "l" .. (i + 1) .. "s",
"image_button[9," .. (i + (i * 0.25) + 0.5) .. ";1,0.6", "l" .. (i + 1) .. "s",
{
pipeworks.button_off,
pipeworks.button_on
}
)
end
local size = "10.2,10"
local size = "10.2,9"
meta:set_string("formspec",
"formspec_version[2]" ..
"size[" .. size .. "]" ..
"item_image[0.2,0.2;1,1; " .. tube_name .. "]" ..
"label[1.5,0.75;" .. minetest.formspec_escape(tube_description) .. "]" ..
pipeworks.fs_helpers.get_prepends(size) ..
"field[1.5,1.25;7.25,1;tags1;;${tags1}]" ..
"field[1.5,2.5;7.25,1;tags2;;${tags2}]" ..
"field[1.5,3.75;7.25,1;tags3;;${tags3}]" ..
"field[1.5,5.0;7.25,1;tags4;;${tags4}]" ..
"field[1.5,6.25;7.25,1;tags5;;${tags5}]" ..
"field[1.5,7.5;7.25,1;tags6;;${tags6}]" ..
"field[1.5,0.25;7.25,1;tags1;;${tags1}]" ..
"field[1.5,1.5;7.25,1;tags2;;${tags2}]" ..
"field[1.5,2.75;7.25,1;tags3;;${tags3}]" ..
"field[1.5,4.0;7.25,1;tags4;;${tags4}]" ..
"field[1.5,5.25;7.25,1;tags5;;${tags5}]" ..
"field[1.5,6.5;7.25,1;tags6;;${tags6}]" ..
"image[0.22,1.25;1,1;pipeworks_white.png]" ..
"image[0.22,2.50;1,1;pipeworks_black.png]" ..
"image[0.22,3.75;1,1;pipeworks_green.png]" ..
"image[0.22,5.00;1,1;pipeworks_yellow.png]" ..
"image[0.22,6.25;1,1;pipeworks_blue.png]" ..
"image[0.22,7.50;1,1;pipeworks_red.png]" ..
"image[0.22,0.25;1,1;pipeworks_white.png]" ..
"image[0.22,1.50;1,1;pipeworks_black.png]" ..
"image[0.22,2.75;1,1;pipeworks_green.png]" ..
"image[0.22,4.00;1,1;pipeworks_yellow.png]" ..
"image[0.22,5.25;1,1;pipeworks_blue.png]" ..
"image[0.22,6.50;1,1;pipeworks_red.png]" ..
buttons_formspec ..
"button[6,8.75;1.5,1;set_items_tags;" .. S("Set") .. "]" ..
"button_exit[7.75,8.75;2,1;close;" .. S("Close") .. "]"
"label[0.22,7.9;"..help_text.."]"..
"button[7.25,7.8;1.5,0.8;set_item_tags;" .. S("Set") .. "]"
)
end
pipeworks.register_tube(tube_name, {
description = tube_description,
pipeworks.register_tube("pipeworks:tag_tube", {
description = S("Tag Sorting Pneumatic Tube Segment"),
inventory_image = "pipeworks_tag_tube_inv.png",
noctr = { "pipeworks_tag_tube_noctr_1.png", "pipeworks_tag_tube_noctr_2.png", "pipeworks_tag_tube_noctr_3.png",
"pipeworks_tag_tube_noctr_4.png", "pipeworks_tag_tube_noctr_5.png", "pipeworks_tag_tube_noctr_6.png" },
@ -66,15 +56,18 @@ pipeworks.register_tube(tube_name, {
no_facedir = true, -- Must use old tubes, since the textures are rotated with 6d ones
node_def = {
tube = {
can_go = function(pos, node, velocity, stack)
can_go = function(pos, node, velocity, stack, tags)
local tbl, tbln = {}, 0
local found, foundn = {}, 0
local meta = minetest.get_meta(pos)
local stack_tag = pipeworks.get_item_tag(stack)
if not stack_tag or stack_tag == "" then
stack_tag = notag_name
local tag_hash = {}
if #tags > 0 then
for _,tag in ipairs(tags) do
tag_hash[tag] = true
end
else
tag_hash["<none>"] = true -- Matches items without tags
end
stack_tag = pipeworks.safe_tag(stack_tag)
for i, vect in ipairs(pipeworks.meseadjlist) do
local npos = vector.add(pos, vect)
local node = minetest.get_node(npos)
@ -83,19 +76,17 @@ pipeworks.register_tube(tube_name, {
local tube_def = reg_node.tube
if not tube_def or not tube_def.can_insert or
tube_def.can_insert(npos, node, stack, vect) then
local tags_name = "tags" .. i
local tags = meta:get_string(tags_name)
local is_empty = tags == nil or tags == ""
if not is_empty then
for tag in string.gmatch(tags, "[^,]+") do
tag = pipeworks.safe_tag(tag)
if tag and (tag == stack_tag or stack_tag:find(("^%s%%."):format(escape(tag)))) then
local side_tags = meta:get_string("tags" .. i)
if side_tags ~= "" then
side_tags = pipeworks.sanitize_tags(side_tags)
for _,tag in ipairs(side_tags) do
if tag_hash[tag] then
foundn = foundn + 1
found[foundn] = vect
break
end
end
end
if is_empty then
else
tbln = tbln + 1
tbl[tbln] = vect
end
@ -107,13 +98,11 @@ pipeworks.register_tube(tube_name, {
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
for i = 1, 6 do
meta:set_int("l" .. tostring(i) .. "s", 1)
inv:set_size("line" .. tostring(i), 6 * 1)
end
update_formspec(pos)
meta:set_string("infotext", S("Tags Sorting pneumatic tube"))
meta:set_string("infotext", S("Tag sorting pneumatic tube"))
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
if placer and placer:is_player() and placer:get_player_control().aux1 then
@ -135,8 +124,8 @@ pipeworks.register_tube(tube_name, {
for i = 1, 6 do
local field_name = "tags" .. tostring(i)
if fields[field_name] then
local safe_tags = safe_tags(fields[field_name])
meta:set_string(field_name, safe_tags)
local tags = pipeworks.sanitize_tags(fields[field_name])
meta:set_string(field_name, table.concat(tags, ","))
end
end