init.lua 4.9 KB

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