init.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. local S = minetest.get_translator("advtrains_doc_integration")
  2. local fsescape = minetest.formspec_escape
  3. local function map(tbl, func)
  4. local t = {}
  5. for k, v in pairs(tbl or {}) do
  6. t[k] = func(v)
  7. end
  8. return t
  9. end
  10. if doc.sub.items then
  11. local register_factoid = doc.sub.items.register_factoid
  12. local function group_factoid(cat, gr, f)
  13. register_factoid(cat, "groups", function(_, def)
  14. return f(def.groups[gr] or 0) or ""
  15. end)
  16. end
  17. for cat, cinfo in pairs{
  18. nodes = {
  19. not_blocking_trains = "This block does not block trains.",
  20. save_in_at_nodedb = "This block is saved in the Advtrains node database.",
  21. },
  22. } do
  23. for group, ginfo in pairs(cinfo) do
  24. local tp = type(ginfo)
  25. if tp == "string" then
  26. group_factoid(cat, group, function(x)
  27. if x > 0 then
  28. return ginfo
  29. end
  30. end)
  31. elseif tp == "function" then
  32. group_factoid(cat, group, ginfo)
  33. end
  34. end
  35. end
  36. register_factoid("nodes", "groups", function(_, ndef)
  37. if ndef.advtrains then
  38. if ndef.advtrains.set_aspect then
  39. return "This is a signal with a variable aspect."
  40. elseif ndef.advtrains.get_aspect then
  41. return "This is a signal with a static aspect."
  42. end
  43. end
  44. return ""
  45. end)
  46. end
  47. doc.add_category("advtrains_wagons", {
  48. name = S("Wagons"),
  49. build_formspec = doc.entry_builders.formspec,
  50. })
  51. local function addlist(lst, tbl, title, fallback1, fallback2, mapf)
  52. if not tbl then
  53. if fallback2 then
  54. table.insert(lst, fallback2)
  55. elseif fallback2 == false and fallback1 then
  56. table.insert(lst, fallback1)
  57. end
  58. elseif next(tbl) ~= nil then
  59. table.insert(lst, title)
  60. for k, v in pairs(tbl) do
  61. if mapf then
  62. k = mapf(k, v)
  63. end
  64. table.insert(lst, "* " .. k)
  65. end
  66. elseif fallback1 then
  67. table.insert(lst, fallback1)
  68. end
  69. end
  70. local function get_coupler_name(n)
  71. return advtrains.coupler_types[n] or n
  72. end
  73. local function doc_register_wagon(itemname)
  74. local prototype = advtrains.wagon_prototypes[itemname]
  75. local itemdef = minetest.registered_items[itemname]
  76. local desctext = {}
  77. addlist(desctext, prototype.drives_on, S("Drives on:"))
  78. addlist(desctext, prototype.coupler_types_front, S("Compatible couplers (front):"), S("No front couplers"), S("Universal front coupler"), get_coupler_name)
  79. addlist(desctext, prototype.coupler_types_back, S("Compatible couplers (back):"), S("No back couplers"), S("Universal back coupler"), get_coupler_name)
  80. if prototype.wagon_span then
  81. table.insert(desctext, S("Wagon span: @1", 2*prototype.wagon_span))
  82. end
  83. table.insert(desctext, S("Max speed: @1 m/s", prototype.max_speed or "not defined"))
  84. table.insert(desctext, S("Motive power: " .. (prototype.is_locomotive and "yes" or "no")))
  85. if prototype.has_inventory then
  86. addlist(desctext, prototype.inventory_list_sizes, S("Cargo inventory size:"), S("Cargo inventory: yes"), false, function(k, v)
  87. return string.format("%s: %s", k, v)
  88. end)
  89. else
  90. table.insert(desctext, S("Cargo inventory: no"))
  91. end
  92. local pax, driver = 0, 0
  93. if prototype.seats and prototype.seat_groups then
  94. for _, v in pairs(prototype.seats) do
  95. if prototype.seat_groups[v.group].driving_ctrl_access then
  96. driver = driver + 1
  97. else
  98. pax = pax + 1
  99. end
  100. end
  101. end
  102. table.insert(desctext, S("Passenger seats: @1", pax))
  103. table.insert(desctext, S("Driver seats: @1", driver))
  104. addlist(desctext, prototype.drops, S("Drops:"), S("Drops nothing"), false, function(_, v) return v end)
  105. local x0, y0 = doc.FORMSPEC.ENTRY_START_X, doc.FORMSPEC.ENTRY_START_Y
  106. local x1, y1 = doc.FORMSPEC.ENTRY_END_X+0.75, doc.FORMSPEC.ENTRY_END_Y
  107. local width, height = x1-x0, y1-y0
  108. local mside = height/2
  109. local mesh = fsescape(prototype.mesh or "")
  110. local textures = table.concat(map(prototype.textures, fsescape), ",")
  111. local fstext = {
  112. doc.widgets.text(table.concat(desctext, "\n"), x0, y0, width-mside, height),
  113. string.format("item_image[%f,%f;%f,%f;%s]", x1-mside, y0, mside, mside, fsescape(itemname)),
  114. "style[wagon_model;bgcolor=#000]",
  115. string.format("model[%f,%f;%f,%f;%s;%s;%s]", x1-mside, y1-mside+0.625, mside, mside, "wagon_model", mesh, textures),
  116. }
  117. minetest.override_item(itemname, {_doc_items_create_entry = false})
  118. doc.add_entry("advtrains_wagons", itemname, {
  119. name = string.split(itemdef.description, "\n", true)[1],
  120. data = table.concat(fstext),
  121. })
  122. end
  123. for k in pairs(advtrains.wagon_prototypes) do
  124. doc_register_wagon(k)
  125. end