Minor code refactor for (future) modularity

This commit is contained in:
Y. Wang 2023-02-04 16:29:37 +01:00
parent 58d34ea39b
commit 773ddb3768
2 changed files with 97 additions and 63 deletions

View File

@ -1,7 +1,22 @@
---*- mode: lua -*-
globals = {"advtrains_doc_integration"}
read_globals = {
"advtrains", "doc", "dlxtrains", "minetest",
"table" = {
"doc", "ItemStack", "minetest",
"advtrains_attachment_offset_patch", "multi_component_liveries",
advtrains = {
fields = {
coupler_types = {other_fields = true},
register_wagon = {read_only = false},
wagon_prototypes = {other_fields = true},
}
},
dlxtrains = {
fields = {
update_livery = {read_only = false},
},
},
table = {
fields = {"copy"},
},
}
ignore = {"631",}

141
init.lua
View File

@ -17,48 +17,50 @@ local function quotestring(str)
return string.format("%q", str)
end
if doc.sub.items then
local register_factoid = doc.sub.items.register_factoid
local function group_factoid(cat, gr, f)
register_factoid(cat, "groups", function(_, def)
return f(def.groups[gr] or 0) or ""
if doc then
if doc.sub.items then
local register_factoid = doc.sub.items.register_factoid
local function group_factoid(cat, gr, f)
register_factoid(cat, "groups", function(_, def)
return f(def.groups[gr] or 0) or ""
end)
end
for cat, cinfo in pairs{
nodes = {
not_blocking_trains = S("This block does not block trains."),
save_in_at_nodedb = S("This block is saved in the Advtrains node database."),
},
} do
for group, ginfo in pairs(cinfo) do
local tp = type(ginfo)
if tp == "string" then
group_factoid(cat, group, function(x)
if x > 0 then
return ginfo
end
end)
elseif tp == "function" then
group_factoid(cat, group, ginfo)
end
end
end
register_factoid("nodes", "groups", function(_, ndef)
if ndef.advtrains then
if ndef.advtrains.set_aspect then
return S("This is a signal with a variable aspect.")
elseif ndef.advtrains.get_aspect then
return S("This is a signal with a static aspect.")
end
end
return ""
end)
end
for cat, cinfo in pairs{
nodes = {
not_blocking_trains = S("This block does not block trains."),
save_in_at_nodedb = S("This block is saved in the Advtrains node database."),
},
} do
for group, ginfo in pairs(cinfo) do
local tp = type(ginfo)
if tp == "string" then
group_factoid(cat, group, function(x)
if x > 0 then
return ginfo
end
end)
elseif tp == "function" then
group_factoid(cat, group, ginfo)
end
end
end
register_factoid("nodes", "groups", function(_, ndef)
if ndef.advtrains then
if ndef.advtrains.set_aspect then
return S("This is a signal with a variable aspect.")
elseif ndef.advtrains.get_aspect then
return S("This is a signal with a static aspect.")
end
end
return ""
end)
end
doc.add_category("advtrains_wagons", {
name = S("Wagons"),
build_formspec = doc.entry_builders.formspec,
})
doc.add_category("advtrains_wagons", {
name = S("Wagons"),
build_formspec = doc.entry_builders.formspec,
})
end
local function describe_length(x)
local inch = math.floor(x/0.0254)
@ -78,7 +80,7 @@ local function describe_length(x)
end
local function describe_speed(x)
local kmph = advtrains.ms_to_kmh(x)
local kmph = x*3.6
local mph = kmph/1.609344
return string.format("%.1f m/s (%.1f km/h; %.1f mph)", x, kmph, mph)
end
@ -144,9 +146,31 @@ local function blankline(st)
return table.insert(st, "")
end
local function adjust_wagon_prototype(prototype)
local p = table.copy(prototype)
if p._doc_wagon_longdesc then
p.desc = p._long_wagon_longdesc
end
if type(p.horn_sound) == "string" then
p.horn_sound = {name = prototype.horn_sound}
end
local pax, driver = 0, 0
if p.seats and p.seat_groups then
for _, v in pairs(p.seats) do
if p.seat_groups[v.group].driving_ctrl_access then
driver = driver + 1
else
pax = pax + 1
end
end
end
p.max_passengers = pax
p.max_drivers = driver
return p
end
local function doc_register_wagon(itemname)
local prototype = advtrains.wagon_prototypes[itemname]
local itemdef = minetest.registered_items[itemname]
local prototype = adjust_wagon_prototype(advtrains.wagon_prototypes[itemname])
local desctext = {}
if prototype._doc_wagon_longdesc then
table.insert(desctext, tostring(prototype._doc_wagon_longdesc))
@ -162,25 +186,14 @@ local function doc_register_wagon(itemname)
table.insert(desctext, S("Wagon span: @1", prototype.wagon_span and describe_length(2*prototype.wagon_span) or S("Undefined")))
table.insert(desctext, S("Maximum speed: @1", prototype.max_speed and describe_speed(prototype.max_speed) or S("Undefined")))
table.insert(desctext, S2("Motive power: @1", prototype.is_locomotive and "Present" or "Absent"))
local hornsound = prototype.horn_sound
if type(hornsound) == "table" then
hornsound = hornsound.name
if prototype.horn_seound then
local hornsound = prototype.horn_sound.name
table.insert(desctext, S("Horn sound: @1", hornsound and hornsound ~= "" and quotestring(hornsound) or S("Undefined")))
end
table.insert(desctext, S("Horn sound: @1", hornsound and hornsound ~= "" and quotestring(hornsound) or S("Undefined")))
blankline(desctext)
local pax, driver = 0, 0
if prototype.seats and prototype.seat_groups then
for _, v in pairs(prototype.seats) do
if prototype.seat_groups[v.group].driving_ctrl_access then
driver = driver + 1
else
pax = pax + 1
end
end
end
table.insert(desctext, S("Passenger seats: @1", pax))
table.insert(desctext, S("Driver seats: @1", driver))
table.insert(desctext, S("Passenger seats: @1", prototype.max_passengers))
table.insert(desctext, S("Driver seats: @1", prototype.max_drivers))
if prototype.has_inventory then
addlist(desctext, prototype.inventory_list_sizes, S("Cargo inventory size:"), S2("Cargo inventory: @1", "Present"), false, function(k, v)
return string.format("%s: %s", k, v)
@ -253,8 +266,10 @@ local function doc_register_wagon(itemname)
})
end
for k in pairs(advtrains.wagon_prototypes) do
doc_register_wagon(k)
if doc then
for k in pairs(advtrains.wagon_prototypes) do
doc_register_wagon(k)
end
end
local _register_wagon = advtrains.register_wagon
@ -264,5 +279,9 @@ function advtrains.register_wagon(...)
if not string.find(name, ":") then
name = "advtrains:" .. name
end
doc_register_wagon(name)
if doc then
doc_register_wagon(name)
end
end
advtrains_doc_integration = {}