Add Advanced hyperspanner use, showing machine counts via sneak+use

This commit is contained in:
Zenon Seth 2024-07-30 23:02:18 +01:00
parent c5ca0d8a88
commit cf30e6736a
5 changed files with 151 additions and 19 deletions

View File

@ -33,6 +33,7 @@ end
-- These have special meaning and are treated with special actions. Each node group above must
-- map to one network group, though multiple node groups can map to the same network group<br>
-- More can be registered via logistica.network_group_register
-- NOTE: For display purposes, this needs to be kept in sync with the hyperspanner_advanced_use code
logistica.NETWORK_GROUPS = {
cables = "cables",
requesters = "requesters",
@ -47,6 +48,20 @@ logistica.NETWORK_GROUPS = {
wireless_receivers = "wireless_receivers",
}
local NETWORK_GROUP_NAMES = {
cables = "Cables",
requesters = "Request Inserters",
injectors = "Network Importers",
suppliers = "Suppliers",
mass_storage = "Mass Storages",
item_storage = "Item Storages",
misc = "Misc Machines",
trashcans = "Trashcans",
reservoirs = "Reservoirs",
wireless_transmitters = "Wireless Transmitter",
wireless_receivers = "Wireless Receivers",
}
-- The default node groups of Logistica, with utility shorthand attached.<br>
-- More can be registered via logistica.group_register_type, and other related functions (see API below)
logistica.GROUPS = {
@ -180,6 +195,12 @@ function logistica.network_group_exists(group_name)
return network_groups[lname] ~= nil
end
-- returns a readible, user-friendly name, or empty string if the groupName is not valid
function logistica.get_network_group_description(groupName)
if not groupName or not type(groupName) == "string" then return "" end
return NETWORK_GROUP_NAMES[groupName] or ""
end
----------------------------------------------------------------
-- Register all the default/built-in network groups, and then all node groups
----------------------------------------------------------------

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

129
tools/hyperspanner.lua Normal file
View File

@ -0,0 +1,129 @@
local S = logistica.TRANSLATOR
local hyperspanner_desc = S("Hyperspanner\nUse on node to see which network it belongs to\nSneak + Use on node to see detailed network info")
local msg_no_connected_network = S("Node has no connected network!")
local ROW_WIDTH = 8
local ROW_HEIGHT = 0.5
local ROW_PADD = 0.3
local FORM_WIDTH = ROW_WIDTH + 2 * ROW_PADD
local FORMSPEC_NAME = "logistica_hypinf"
local NG = logistica.NETWORK_GROUPS
local GROUPS_TO_DISPLAY = {
NG.cables,
NG.wireless_transmitters,
NG.wireless_receivers,
NG.mass_storage,
NG.item_storage,
NG.suppliers,
NG.injectors,
NG.requesters,
NG.reservoirs,
NG.trashcans,
NG.misc,
}
local function hyperspanner_basic_use(pos, player)
local networkName = logistica.get_network_name_or_nil(pos)
if networkName then
networkName = S("Network")..": "..networkName
else
networkName = msg_no_connected_network
end
logistica.show_popup(
player:get_player_name(),
"("..pos.x..","..pos.y..","..pos.z..") "..networkName
)
end
-- rowIndex should start at 0 for top row
local function make_count_row(y, text, count, rowIndex, useBool)
local yPos = y + ROW_HEIGHT * rowIndex
local txt = ""
txt = txt.."image[0.1,"..(yPos + 0.5 * ROW_HEIGHT)..";"..(FORM_WIDTH - 0.2)..",0.03"..";logistica_divider.png]"
txt = txt.."label["..ROW_PADD..","..yPos..";"..text.."]"
if useBool then
if count > 0 then count = "Yes" else count = "No" end
end
txt = txt.. "label["..(ROW_PADD + 6)..","..yPos..";"..count.."]"
return txt
end
local function count_num_machines(network, groupName)
local groupMachines = network[groupName]
if not groupMachines or type(groupMachines) ~= "table" then return 0 end
local count = 0
for _, _ in pairs(groupMachines) do count = count + 1 end
return count
end
local function get_counts_rows(network, startX, startY)
local rows = {}
local total = 0
local index = 1
for i, groupName in ipairs(GROUPS_TO_DISPLAY) do
local isWirelessTransmitter = groupName == NG.wireless_transmitters
local groupText = logistica.get_network_group_description(groupName)
if isWirelessTransmitter then groupText = "Has Wireless Transmitter" end
local count = count_num_machines(network, groupName)
if groupName ~= NG.cables then total = total + count end
rows[index] = make_count_row(startY, groupText, count, i, isWirelessTransmitter)
index = index + 1
end
rows[index] = make_count_row(startY, "Total # of Machines", total, 0, false)
return table.concat(rows)
end
local function get_network_info_formspec(network, player)
return
"formspec_version[4]"..
"size["..FORM_WIDTH..",7.5]"..
logistica.ui.background..
"image_button_exit["..(FORM_WIDTH - 0.8)..",0.3;0.5,0.5;logistica_icon_cancel.png;;;false;false;]"..
"label["..ROW_PADD..",0.5;"..S("Network info for")..": "..network.name.."]"..
get_counts_rows(network, 0.3, 1.5)
-- TODO: advanced listing of machines and positions --
-- "label["..ROW_PADD..",6.3;Machine Locator And Info]"..
-- "dropdown["..ROW_PADD..",6.5;3.9,0.8;;Controller,Mass Storages,Item Storages,Injectors,Requesters,Reservoirs,Trashcans,Wireless Receivers;1;false]"..
-- "image["..ROW_PADD..",7.4;2,2;machine_img]"..
-- "button["..ROW_PADD..",9.9;1,0.8;prev_machine;<]"..
-- "button["..(ROW_PADD + 1)..",9.9;1,0.8;next_machine;>]"..
-- "label["..(2 * ROW_PADD)..",9.7;101 / 128]"..
-- "label["..(ROW_PADD + 2.1)..",7.6;Machine Name: Logistic Network Controller]"..
-- "label["..(ROW_PADD + 2.1)..",8.1;Position: 4535\\, -2535\\, 2455]"..
-- "label["..(ROW_PADD + 2.1)..",8.6;Approximate Distance: 4153]"
end
-- NOTE: For display purposes, this needs to be kept in sync with the logistica.NETWORK_GROUPS code
local function hyperspanner_advanced_use(pos, player)
local network = logistica.get_network_or_nil(pos)
if not network then
logistica.show_popup(
player:get_player_name(),
"("..pos.x..","..pos.y..","..pos.z..") "..msg_no_connected_network
)
else
minetest.show_formspec(player:get_player_name(), FORMSPEC_NAME, get_network_info_formspec(network, player))
end
end
minetest.register_tool("logistica:hyperspanner",{
description = hyperspanner_desc,
short_description = hyperspanner_desc,
inventory_image = "logistica_hyperspanner.png",
wield_image = "logistica_hyperspanner.png",
stack_max = 1,
on_place = function(itemstack, placer, pointed_thing)
local pos = pointed_thing.under
if not placer or not pos or not placer:is_player() then return end
local node = minetest.get_node_or_nil(pos)
if not node or not (logistica.is_machine(node.name) or logistica.GROUPS.cables.is(node.name)) then return end
if placer:get_player_control().sneak then
hyperspanner_advanced_use(pos, placer)
else
hyperspanner_basic_use(pos, placer)
end
end
})

View File

@ -1,24 +1,5 @@
local S = logistica.TRANSLATOR
minetest.register_tool("logistica:hyperspanner",{
description = S("Hyperspanner\nA multipurpose engineering tool\nUse on nodes for network info.\nCan also reverse poliarity."),
short_description = S("Hyperspanner"),
inventory_image = "logistica_hyperspanner.png",
wield_image = "logistica_hyperspanner.png",
stack_max = 1,
on_place = function(itemstack, placer, pointed_thing)
local pos = pointed_thing.under
if not placer or not pos then return end
local node = minetest.get_node_or_nil(pos)
if not node or not (logistica.is_machine(node.name) or logistica.GROUPS.cables.is(node.name)) then return end
local network = logistica.get_network_name_or_nil(pos) or S("<NONE>")
logistica.show_popup(
placer:get_player_name(),
"("..pos.x..","..pos.y..","..pos.z..") "..S("Network")..": "..network
)
end
})
minetest.register_tool("logistica:wand",{
description = S("Inv List Scanner"),
inventory_image = "logistica_wand.png",

View File

@ -3,4 +3,5 @@ local path = logistica.MODPATH.."/tools"
logistica.tools = {}
dofile(path.."/misc.lua")
dofile(path.."/hyperspanner.lua")
dofile(path.."/wireless_access_pad.lua")