speed.lua 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. -- auxiliary functions for the reworked speed restriction system
  2. local function s_lessp(a, b)
  3. if not a or a == -1 then
  4. return false
  5. elseif not b or b == -1 then
  6. return true
  7. else
  8. return a < b
  9. end
  10. end
  11. local function s_greaterp(a, b)
  12. return s_lessp(b, a)
  13. end
  14. local function s_not_lessp(a, b)
  15. return not s_lessp(a, b)
  16. end
  17. local function s_not_greaterp(a, b)
  18. return not s_greaterp(a, b)
  19. end
  20. local function s_equalp(a, b)
  21. return (a or -1) == (b or -1)
  22. end
  23. local function s_not_equalp(a, b)
  24. return (a or -1) ~= (b or -1)
  25. end
  26. local function s_max(a, b)
  27. if s_lessp(a, b) then
  28. return b
  29. else
  30. return a
  31. end
  32. end
  33. local function s_min(a, b)
  34. if s_lessp(a, b) then
  35. return a
  36. else
  37. return b
  38. end
  39. end
  40. local function get_speed_restriction_from_table (tbl)
  41. local strictest = -1
  42. for _, v in pairs(tbl) do
  43. strictest = s_min(strictest, v)
  44. end
  45. if strictest == -1 then
  46. return nil
  47. end
  48. return strictest
  49. end
  50. local function set_speed_restriction (tbl, rtype, rval)
  51. if rval then
  52. tbl[rtype or "main"] = rval
  53. end
  54. return tbl
  55. end
  56. local function set_speed_restriction_for_train (train, rtype, rval)
  57. local t = train.speed_restrictions_t or {main = train.speed_restriction}
  58. train.speed_restrictions_t = set_speed_restriction(t, rtype, rval)
  59. train.speed_restriction = get_speed_restriction_from_table(t)
  60. end
  61. local function merge_speed_restriction_from_aspect_to_train (train, asp)
  62. return set_speed_restriction_for_train(train, asp.type, asp.main)
  63. end
  64. return {
  65. lessp = s_lessp,
  66. greaterp = s_greaterp,
  67. not_lessp = s_not_lessp,
  68. not_greaterp = s_not_greaterp,
  69. equalp = s_equalp,
  70. not_equalp = s_not_equalp,
  71. max = s_max,
  72. min = s_min,
  73. set_restriction = set_speed_restriction_for_train,
  74. merge_aspect = merge_speed_restriction_from_aspect_to_train,
  75. }