describe.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --- Describing things.
  2. -- This module includes functions used for describing things.
  3. -- @module advtrains_doc_integration.describe
  4. -- @alias D
  5. local M = advtrains_doc_integration.mathutils
  6. local utils = advtrains_doc_integration.utils
  7. local D = {}
  8. --- Describe a conns object
  9. -- @tparam advtrains.conns|{advtrains.conns,number,...} conns The conns object to describe.
  10. -- @treturn string|nil The description of the conns object.
  11. function D.conns(conns)
  12. local connsdesc = {[0] = "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"}
  13. if type(conns) == "table" then
  14. if conns.c then
  15. if conns.y and conns.y ~= 0 then
  16. return ("%s%+d%%"):format(D.conns(conns.c), conns.y*100)
  17. else
  18. return D.conns(conns.c)
  19. end
  20. else
  21. local cst = utils.map(conns, D.conns)
  22. local cstl = #cst
  23. if cstl == 2 then
  24. return ("%s - %s"):format(unpack(cst))
  25. elseif cstl == 3 then
  26. return ("[%s <-] %s -> %s"):format(cst[3], cst[1], cst[2])
  27. elseif cstl == 4 then
  28. return ("%s - %s; %s - %s"):format(unpack(cst))
  29. elseif cstl == 5 then
  30. return ("[%s,%s <-] %s -> %s"):format(cst[3], cst[4], cst[1], cst[2])
  31. end
  32. end
  33. else
  34. return connsdesc[tonumber(conns)]
  35. end
  36. end
  37. --- Describe a (mixed) fraction
  38. -- @tparam integer int The integer part of the number.
  39. -- @tparam integer num The numerator.
  40. -- @tparam integer denom The denominator.
  41. -- @treturn string The description of the mixed fraction.
  42. function D.mixed_fraction(int, num, denom)
  43. local str = tostring(int)
  44. if num > 0 then
  45. local fs = ("%d/%d"):format(num, denom)
  46. if int ~= 0 then
  47. str = ("%d %s"):format(int, fs)
  48. else
  49. str = fs
  50. end
  51. end
  52. return str
  53. end
  54. --- Describe a short length (using mm and in).
  55. -- @tparam number x The length in meters.
  56. -- @treturn string The description of the length.
  57. function D.length(x)
  58. local inch, inchfrac = math.modf(x/0.0254)
  59. local ft = math.floor(inch/12)
  60. inch, inchfrac = inch%12, math.floor(inchfrac*32)
  61. local imst = {}
  62. if ft > 0 then
  63. table.insert(imst, ft .. "'")
  64. end
  65. if inch > 0 or inchfrac > 0 or ft == 0 then
  66. table.insert(imst, D.mixed_fraction(inch, M.reduce_fraction(inchfrac, 32)) .. '"')
  67. end
  68. return ("%d mm (%s)"):format(1000*x, table.concat(imst, " "))
  69. end
  70. --- Describe a speed value (using m/s, km/h and mph).
  71. -- @tparam number x The speed in m/s.
  72. -- @treturn string The description of the speed.
  73. function D.speed(x)
  74. local kmph = x*3.6
  75. local mph = kmph/1.609344
  76. return string.format("%.1f m/s (%.1f km/h; %.1f mph)", x, kmph, mph)
  77. end
  78. return D