Use hypertext for wagon information

This commit is contained in:
Y. Wang 2023-09-27 15:53:13 +02:00
parent 5731f414c3
commit f5ffacd12b

View File

@ -14,6 +14,10 @@ local function Ss(x)
return minetest.get_translated_string("en", x)
end
local function htescape(str)
return string.gsub(tostring(str), "([<>])", [[\1]])
end
local function latex_escape(str)
return (string.gsub(str, ".", {
["&"] = [[\&]],
@ -45,6 +49,10 @@ local function quotestring(str)
return string.format("%q", str)
end
local function htmono(str)
return string.format("<mono>%s</mono>", htescape(str))
end
local function describe_conns(conns)
local connsdesc = {[0] = "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"}
if type(conns) == "table" then
@ -122,20 +130,31 @@ if doc then
end
local function describe_length(x)
local inch = math.floor(x/0.0254)
local inch = x/0.0254
local infdenom = 32
local infnum = math.floor(inch*infdenom)%infdenom
local ft = math.floor(inch/12)
inch = inch%12
inch = math.floor(inch)%12
local st = {}
if ft > 0 then
table.insert(st, ft .. "'")
end
local infstr = ""
if infnum > 0 then
while infnum%2 == 0 do
infnum, infdenom = infnum/2, infdenom/2
end
infstr = string.format(" %d/%d", infnum, infdenom)
end
if inch > 0 then
table.insert(st, inch .. '"')
table.insert(st, string.format(' %s%s"', inch, infstr))
elseif infnum > 0 then
table.insert(st, infstr .. '"')
end
if not next(st) then
st = '0"'
end
return string.format("%.2f m (%s)", x, table.concat(st))
return string.format("%d mm (%s)", 1000*x, table.concat(st))
end
local function describe_speed(x)
@ -157,7 +176,7 @@ local function addlist(lst, tbl, title, fallback1, fallback2, mapf)
if mapf then
k = mapf(k, v)
end
table.insert(lst, "* " .. k)
table.insert(lst, " " .. k)
end
elseif fallback1 then
table.insert(lst, fallback1)
@ -168,6 +187,15 @@ local function get_coupler_name(n)
return advtrains.coupler_types[n] or n
end
local function ht_coupler_name(n)
local s = advtrains.coupler_types[n]
if s then
return htescape(s)
else
return htmono(n)
end
end
local function dlxtrains_livery_information(prototype)
if not dlxtrains then
return nil
@ -270,33 +298,33 @@ local function doc_register_wagon(itemname)
table.insert(desctext, tostring(prototype._doc_wagon_longdesc))
blankline(desctext)
end
table.insert(desctext, S("Itemstring: @1", quotestring(itemname)))
table.insert(desctext, S("Itemstring: @1", htmono(itemname)))
addlist(desctext, prototype.drops, S("Drops:"), S("Drops nothing"), false, function(_, v) return list_itemstring(v) end)
blankline(desctext)
addlist(desctext, prototype.drives_on, S("Drives on:"))
addlist(desctext, prototype.coupler_types_front, S("Compatible front couplers:"), S2("Front coupler: @1", "Absent"), S2("Front coupler: @1", "Universal"), get_coupler_name)
addlist(desctext, prototype.coupler_types_back, S("Compatible rear couplers:"), S2("Rear coupler: @1", "Absent"), S2("Rear coupler: @1", "Universal"), get_coupler_name)
addlist(desctext, prototype.drives_on, S("Drives on:"), nil, nil, htmono)
addlist(desctext, prototype.coupler_types_front, S("Compatible front couplers:"), S2("Front coupler: @1", "Absent"), S2("Front coupler: @1", "Universal"), ht_coupler_name)
addlist(desctext, prototype.coupler_types_back, S("Compatible rear couplers:"), S2("Rear coupler: @1", "Absent"), S2("Rear coupler: @1", "Universal"), ht_coupler_name)
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
table.insert(desctext, S("Horn sound: @1", hornsound and quotestring(hornsound.name) or S("Undefined")))
table.insert(desctext, S("Horn sound: @1", hornsound and string.format("<action name=playhorn><style color=cyan>%s</style></action>", htmono(hornsound.name)) or S("Undefined")))
blankline(desctext)
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)
return string.format("%s: %d", htmono(k), v)
end)
else
table.insert(desctext, S2("Cargo inventory: @1", "Absent"))
end
blankline(desctext)
table.insert(desctext, S("Mesh: @1", quotestring(prototype.mesh)))
addlist(desctext, prototype.textures, S("Textures:"), S("No textures"), false, function(_, v) return quotestring(v) end)
table.insert(desctext, S("Mesh: @1", prototype.mesh and htmono(prototype.mesh) or "None"))
addlist(desctext, prototype.textures, S("Textures:"), S("No textures"), false, function(_, v) return htmono(v) end)
local bikeliv = S("Unsupported")
local bikelivdesc = nil
@ -304,8 +332,8 @@ local function doc_register_wagon(itemname)
if prototype.livery_definition then
bikeliv = S("Supported by the multi_component_liveries mod")
bikelivdesc = {}
addlist(bikelivdesc, prototype.livery_definition.components, S("Livery components:"), nil, nil, function(_, v) return tostring(v.description) end)
addlist(bikelivdesc, prototype.livery_definition.presets, S("Livery presets:"), nil, nil, function(_, v) return tostring(v.description) end)
addlist(bikelivdesc, prototype.livery_definition.components, S("Livery components:"), nil, nil, function(_, v) return htescape(v.description) end)
addlist(bikelivdesc, prototype.livery_definition.presets, S("Livery presets:"), nil, nil, function(_, v) return htescape(v.description) end)
bikelivdesc = table.concat(bikelivdesc, "\n")
else
bikeliv = S("Supported")
@ -320,8 +348,8 @@ local function doc_register_wagon(itemname)
local atlivdef = advtrains_livery_tools_information(itemname)
table.insert(desctext, S2("Advtrains livery tools (Marnack): @1", atlivdef and "Supported" or "Unsupported"))
if atlivdef then
addlist(desctext, atlivdef.template_names, S("Livery templates:"), nil, nil, function(_, v) return v end)
addlist(desctext, atlivdef.livery_names, S("Livery presets:"), nil, nil, function(_, v) return v end)
addlist(desctext, atlivdef.template_names, S("Livery templates:"), nil, nil, function(_, v) return htescape(v) end)
addlist(desctext, atlivdef.livery_names, S("Livery presets:"), nil, nil, function(_, v) return htescape(v) end)
end
blankline(desctext)
@ -353,7 +381,7 @@ local function doc_register_wagon(itemname)
textures = fsescape(string.format("%s_%s.png", dlxlivdef.filename_prefix, dlxlivdef[0].code))
end
local fstext = {
string.format("textarea[%f,%f;%f,%f;;;%s]", x0, y0, width-mside, height+0.875, fsescape(table.concat(desctext, "\n"))),
string.format("hypertext[%f,%f;%f,%f;entry_body;%s]", x0, y0, width-mside, height+0.875, fsescape(table.concat(desctext, "\n"))),
string.format("item_image[%f,%f;%f,%f;%s]", x1-mside, y0+0.0625, mside, mside, fsescape(itemname)),
string.format("model[%f,%f;%f,%f;%s;%s;%s;%f,%f]",
x1-mside, y1-mside, mside, mside, "wagon_model", mesh, textures, -30, 135),
@ -655,3 +683,24 @@ minetest.register_chatcommand("atdoc_write", {
advtrains_doc_integration.write_all_wagons_as_latex()
end,
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "doc:entry" or fields.entry_body ~= "action:playhorn" then
return
end
local pname = player:get_player_name()
local cat, ent = doc.get_selection(pname)
if cat ~= "advtrains_wagons" or ent == nil then
return
end
local sound = (advtrains.wagon_prototypes[ent] or {}).horn_sound
if type(sound) == "table" then
sound = table.copy(sound)
else
sound = {name = sound}
end
if type(sound.name) ~= "string" or sound.name == "" then
return
end
minetest.sound_play(sound, {to_player = pname}, true)
end)