Functions for faster table creation in guide

This commit is contained in:
2025-11-23 22:16:52 +01:00
parent 37b6d2b8ff
commit 69c8355ecb

262
guide.lua
View File

@@ -16,25 +16,94 @@
local S=minetest.get_translator("industrialtest")
-- \brief Creates hypertext table with rows
-- \param rows table
-- \param withTitleRow bool
-- \param description (optional) string
-- \returns string
local function createTable(rows,withTitleRow,description)
local columnWidths={}
for _,row in ipairs(rows) do
for i,cell in ipairs(row) do
-- Add 3 here because of cell padding (2 spaces at each side of column text) and right cell separator
columnWidths[i]=math.max(columnWidths[i] or 0,string.len(cell)+3)
end
end
-- Begin with width of 1 because of left edge
local tableWidth=1
for _,width in ipairs(columnWidths) do
tableWidth=tableWidth+width
end
local horizontalEdge=string.rep("-",tableWidth).."\n"
local hypertextRows={horizontalEdge}
for i,row in ipairs(rows) do
local hypertextRow={"|"}
for o,cell in ipairs(row) do
local cellWidth=string.len(cell)+3
local rightPadding=string.rep(" ",columnWidths[o]-cellWidth)
table.insert(hypertextRow,string.format(" %s%s |",cell,rightPadding))
end
table.insert(hypertextRow,"\n")
table.insert(hypertextRows,table.concat(hypertextRow,""))
-- Insert horizontal edge if title row is present
if withTitleRow and i==1 then
table.insert(hypertextRows,horizontalEdge)
end
end
table.insert(hypertextRows,horizontalEdge)
return "<mono>"..table.concat(hypertextRows,"")..(description or "").."</mono>\n"
end
-- \brief Creates table with machine information while providing few shortcuts for common fields
-- \param fields table
-- \param machineName string
-- \returns string
local function createMachineInformationTable(fields,machineName)
local fieldLabels={
inputVoltage="Input voltage level",
recipe="Recipe type",
powerCapacity="Power capacity",
opPower="Power per operation",
efficiency="Default efficiency"
}
local rows={}
for _,field in ipairs(fields) do
local label=fieldLabels[field.name] or field.name
table.insert(rows,{
label,
field.value
})
end
return createTable(rows,false,string.format("Figure 1. Machine information for %s",machineName))
end
local pages={
{
name="introduction",
title=S("Introduction"),
icon=industrialtest.elementKeys.paper,
content=[[
content={
[[
<big>Introduction</big>
<left>
IndustrialTest implements finite power functionality which can be used to perform various tasks using generators and consumers. Such features are not a new thing in Luanti, as there are older mods which do that already. This mod, however, proves to be more flexible.
</left>
<img name="industrialtest_guide_introduction.png" width="{{IMAGE_WIDTH}}">
]]
}
},
{
name="electricalNetwork",
title=S("Electrical network"),
icon="industrialtest:insulated_copper_cable",
content=[[
content={
[[
<big>Electrical network</big>
<left>
In order to attach electricity consumers to power sources (like generators, batteries, further just called generators) electrical network is used. Consumers can get connected either directly to generators or through various cables. Each generator can output certain voltage (this is actually amperage, however since this mod doesn't simulate electricity accurately, it's called voltage), the same situation is for consumers as they can only handle up to certain voltage.
@@ -42,114 +111,197 @@ local pages={
If voltage is too high for cable it will melt without dropping anything while consumer will explode also without dropping anything and possibly destroying terrain around. Power capacity is measured in EU (Energy Units) and the voltage is EU/update.
Picture 1 shows environment after consumer explosion.
</left>
<mono>
--------------------------------------------
| Voltage level | EU/update | Cable |
--------------------------------------------
| LV | 600 | Tin |
| MV | 2400 | Copper |
| HV | 10200 | Gold |
| EV | 40800 | Iron |
| IV | 163800 | Glass Fibre |
--------------------------------------------
Figure 1. Voltage levels and respective cables
</mono>
]],
createTable({
{
"Voltage level",
"EU/update",
"Cable"
},
{
"LV",
"600",
"Tin"
},
{
"MV",
"2400",
"Copper"
},
{
"HV",
"10200",
"Gold"
},
{
"EV",
"40800",
"Iron"
},
{
"IV",
"163800",
"Glass Fibre"
}
},true,"Figure 1. Voltage levels and respective cables"),
[[
<img name="industrialtest_guide_explosion.png" width="{{IMAGE_WIDTH}}">
<left>Picture 1. Environment after consumer explosion</left>
]]
}
},
{
name="cableFormer",
title=S("Cable Former"),
icon="industrialtest:cable_former",
content=[[
content={
[[
<big>Cable Former</big>
<left>
Cable Former can be used to produce cables with higher material efficiency. It can, however, only produce cables from metals so for example <item name="industrialtest:glass_fibre_cable" height="{{ITEM_HEIGHT}}"> <b>Glass Fibre Cable</b> can't be produced in it.
</left>
<mono>
---------------------------------------
| Input voltage level | LV |
| Recipe type | Cable forming |
| Power capacity | 1400 EU |
| Power per operation | 80 EU |
---------------------------------------
Figure 1. Machine information for Cable Former
</mono>
]],
createMachineInformationTable({
{
name="inputVoltage",
value="LV"
},
{
name="recipe",
value="Cable forming"
},
{
name="powerCapacity",
value="1400 EU"
},
{
name="opPower",
value="80 EU"
}
},S("Cable Former")),
[[
<img name="industrialtest_guide_cable_former.png" width="{{IMAGE_WIDTH}}">
<left>Picture 1. Cable Former running</left>
]]
}
},
{
name="canningMachine",
title=S("Canning Machine"),
icon="industrialtest:canning_machine",
content=[[
content={
[[
<big>Canning Machine</big>
<left>
Canning Machine is used to move certain fluid from one item to another. The common usage is to refill fuel in <item name="industrialtest:jetpack_v" height="{{ITEM_HEIGHT}}"> <b>Jetpack</b> or <item name="industrialtest:fuel_can" height="{{ITEM_HEIGHT}}"> <b>Fuel Can</b>.
</left>
<mono>
---------------------------------------
| Input voltage level | LV |
| Power per operation | 200 EU |
| Power capacity | 1200 EU |
| Canning completion time | 5 seconds |
---------------------------------------
Figure 1. Machine information for Canning Machine
</mono>
]],
createMachineInformationTable({
{
name="inputVoltage",
value="LV"
},
{
name="powerCapacity",
value="1200 EU"
},
{
name="opPower",
value="200 EU"
},
{
name="Canning completion time",
value="5 seconds"
}
},S("Canning Machine")),
[[
<img name="industrialtest_guide_canning_machine.png" width="{{IMAGE_WIDTH}}">
<left>Picture 1. Canning Machine refilling <item name="industrialtest:fuel_can" height="{{ITEM_HEIGHT}}"> <b>Fuel Can</b> with fuel from <item name="industrialtest:coalfuel_cell" height="{{ITEM_HEIGHT}}"> <b>Coalfuel Cell</b></left>
]]
}
},
{
name="compressor",
title=S("Compressor"),
icon="industrialtest:compressor",
content=[[
content={
[[
<big>Compressor</big>
<left>
Compressor as the name suggests is used to compress various materials. Mostly required to produce items for crafting other items.
</left>
<mono>
---------------------------------
| Input voltage level | LV |
| Power per operation | 120 EU |
| Power capacity | 1400 EU |
| Default efficiency | 1 |
---------------------------------
Figure 1. Machine information for Compressor
</mono>
]],
createMachineInformationTable({
{
name="inputVoltage",
value="LV"
},
{
name="recipe",
value="Compressing"
},
{
name="powerCapacity",
value="1400 EU"
},
{
name="opPower",
value="120 EU"
},
{
name="efficiency",
value="1"
}
},S("Compressor")),
[[
<img name="industrialtest_guide_compressor.png" width="{{IMAGE_WIDTH}}">
<left>Picture 1. Compressor compressing <item name="industrialtest:plantball" height="{{ITEM_HEIGHT}}"> <b>Plantball</b> into <item name="industrialtest:compressed_plantball" height="{{ITEM_HEIGHT}}"> <b>Compressed Plantball</b></left>
]]
}
},
{
name="electricFurnace",
title=S("Electric Furnace"),
icon="industrialtest:electric_furnace",
content=[[
content={
[[
<big>Electric Furnace</big>
<left>
Electric Furnace like regular furnace can be used to smelt items. The difference is that Electric Furnace is slightly faster even without any upgrades and, well, it's electric so it doesn't require fuel.
</left>
<mono>
--------------------------------
| Input voltage level | LV |
| Power per operation | 60 EU |
| Power capacity | 416 EU |
| Default efficiency | 0.5 |
--------------------------------
Figure 1. Machine information for Electric Furnace
</mono>
]],
createMachineInformationTable({
{
name="inputVoltage",
value="LV"
},
{
name="recipe",
value="Smelting"
},
{
name="powerCapacity",
value="416 EU"
},
{
name="opPower",
value="60 EU"
},
{
name="efficiency",
value="0.5"
}
},S("Electric Furnace")),
[[
<img name="industrialtest_guide_electric_furnace.png" width="{{IMAGE_WIDTH}}">
<left>Picture 1. Electric Furnace smelting <item name="industrialtest:iron_dust" height="{{ITEM_HEIGHT}}"> <b>Iron Dust</b> into <item name="{{elementKeyIronIngot}}" height="{{ITEM_HEIGHT}}"> <b>Iron Ingot</b></left>
]]
}
}
}
local function preprocessHypertext(content,vars)
@@ -184,7 +336,7 @@ local function getGuideFormspec(playerName,pageName)
for _,page in ipairs(pages) do
if page.name==pageName then
local content=preprocessHypertext(page.content,{
local content=preprocessHypertext(table.concat(page.content,""),{
IMAGE_WIDTH=550,
ITEM_HEIGHT=16,
elementKeyIronIngot=industrialtest.elementKeys.ironIngot