Guest User

Untitled

a guest
Jun 13th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.26 KB | None | 0 0
  1. local timetableHelper = {}
  2.  
  3. -------------------------------------------------------------
  4. ---------------------- Vehicle related ----------------------
  5. -------------------------------------------------------------
  6.  
  7. -- returns [lineID] indext by VehicleID : String
  8. function timetableHelper.getAllRailVehicles()
  9.     local res = {}
  10.     local vehicleMap = api.engine.system.transportVehicleSystem.getLine2VehicleMap()
  11.     for k,v in pairs(vehicleMap) do
  12.         for _,v2 in pairs(v) do
  13.             res[tostring(v2)] = k
  14.         end
  15.     end
  16.     return res
  17. end
  18.  
  19. ---@param line number
  20. -- returns [{stopIndex: Number, vehicle: Number, atTerminal: Bool, countStr: "SINGLE"| "MANY" }]
  21. --         indext by StopIndes : string
  22. function timetableHelper.getTrainLocations(line)
  23.     local res = {}
  24.     local vehicles = api.engine.system.transportVehicleSystem.getLineVehicles(line)
  25.     for _,v in pairs(vehicles) do
  26.         local vehicle = api.engine.getComponent(v, api.type.ComponentType.TRANSPORT_VEHICLE)
  27.         local atTerminal = vehicle.state == 2
  28.         if res[tostring(vehicle.stopIndex)] then
  29.             local prevAtTerminal = res[tostring(vehicle.stopIndex)].atTerminal
  30.             res[tostring(vehicle.stopIndex)] = {
  31.                 stopIndex = vehicle.stopIndex,
  32.                 vehicle = v,
  33.                 atTerminal = (atTerminal or prevAtTerminal),
  34.                 countStr = "MANY"
  35.             }
  36.         else
  37.             res[tostring(vehicle.stopIndex)] = {
  38.                 stopIndex = vehicle.stopIndex,
  39.                 vehicle = v,
  40.                 atTerminal = atTerminal,
  41.                 countStr = "SINGLE"
  42.             }
  43.         end
  44.     end
  45.     return res
  46. end
  47.  
  48. ---@param line number
  49. -- returns [ vehicle:Number ]
  50. function timetableHelper.getVehiclesOnLine(line)
  51.     return api.engine.system.transportVehicleSystem.getLineVehicles(line)
  52. end
  53.  
  54. ---@param vehicle number | string
  55. -- returns stationIndex : Number
  56. function timetableHelper.getCurrentStation(vehicle)
  57.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  58.     if not(type(vehicle) == "number") then print("Expected String or Number") return -1 end
  59.     if not vehicle then return -1 end
  60.     local res = api.engine.getComponent(vehicle, api.type.ComponentType.TRANSPORT_VEHICLE)
  61.     return res.stopIndex + 1
  62.  
  63. end
  64.  
  65. ---@param vehicle number | string
  66. -- returns lineID : Number
  67. function timetableHelper.getCurrentLine(vehicle)
  68.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  69.     if not(type(vehicle) == "number") then print("Expected String or Number") return -1 end
  70.     if not vehicle then return -1 end
  71.  
  72.     local res = api.engine.getComponent(vehicle, api.type.ComponentType.TRANSPORT_VEHICLE)
  73.     if res and res.line then return res.line else return -1 end
  74. end
  75.  
  76.  
  77. ---@param vehicle number | string
  78. -- returns Bool
  79. function timetableHelper.isInStation(vehicle)
  80.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  81.     if not(type(vehicle) == "number") then print("Expected String or Number") return false end
  82.  
  83.     local v = api.engine.getComponent(tonumber(vehicle), api.type.ComponentType.TRANSPORT_VEHICLE)
  84.     return v and v.state == 2
  85. end
  86.  
  87. ---@param vehicle number | string
  88. -- returns Null
  89. function timetableHelper.startVehicle(vehicle)
  90.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  91.     if not(type(vehicle) == "number") then print("Expected String or Number") return false end
  92.  
  93.     api.cmd.sendCommand(api.cmd.make.setUserStopped(vehicle,false))
  94.     return nil
  95. end
  96.  
  97. ---@param vehicle number | string
  98. -- returns Null
  99. function timetableHelper.stopVehicle(vehicle)
  100.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  101.     if not(type(vehicle) == "number") then print("Expected String or Number") return false end
  102.  
  103.     api.cmd.sendCommand(api.cmd.make.setUserStopped(vehicle,true))
  104.  
  105.     return nil
  106. end
  107.  
  108. ---@param vehicle number | string
  109. -- returns time in seconds from game start
  110. function timetableHelper.getPreviousDepartureTime(vehicle)
  111.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  112.     if not(type(vehicle) == "number") then print("Expected String or Number") return false end
  113.  
  114.     local vehicleLineMap = api.engine.system.transportVehicleSystem.getLine2VehicleMap()
  115.     local line = timetableHelper.getCurrentLine(vehicle)
  116.     local departureTimes = {}
  117.     local currentStopIndex = api.engine.getComponent(vehicle, 70).stopIndex
  118.     for _,v in pairs(vehicleLineMap[line]) do
  119.         departureTimes[#departureTimes + 1] = api.engine.getComponent(v, 70).lineStopDepartures[currentStopIndex + 1]
  120.  
  121.     end
  122.     return (timetableHelper.maximumArray(departureTimes))/1000
  123. end
  124.  
  125. ---@param vehicle number | string
  126. -- returns Time in seconds and -1 in case of an error
  127. function timetableHelper.getTimeUntilDeparture(vehicle)
  128.     if type(vehicle) == "string" then vehicle = tonumber(vehicle) end
  129.     if not(type(vehicle) == "number") then print("Expected String or Number") return -1 end
  130.  
  131.     local v = api.engine.getComponent(vehicle, 70)
  132.     if v and v.timeUntilCloseDoors then return v.timeUntilCloseDoors else return -1 end
  133. end
  134.  
  135. -------------------------------------------------------------
  136. ---------------------- Line related -------------------------
  137. -------------------------------------------------------------
  138.  
  139. ---@param lineType string, eg "RAIL", "ROAD", "TRAM", "WATER", "AIR"
  140. -- returns Bool
  141. function timetableHelper.isLineOfType(lineType)
  142.     local lines = api.engine.system.lineSystem.getLines()
  143.     local res = {}
  144.     for k,l in pairs(lines) do
  145.         res[k] = timetableHelper.lineHasType(l, lineType)
  146.     end
  147.     return res
  148. end
  149.  
  150. ---@param line  number | string
  151. ---@param lineType string, eg "RAIL", "ROAD", "TRAM", "WATER", "AIR"
  152. -- returns Bool
  153. function timetableHelper.lineHasType(line, lineType)
  154.     if type(line) == "string" then line = tonumber(line) end
  155.     if not(type(line) == "number") then print("Expected String or Number") return -1 end
  156.  
  157.     local vehicles = api.engine.system.transportVehicleSystem.getLineVehicles(line)
  158.     if vehicles and vehicles[1] and api.engine.getComponent(vehicles[1], api.type.ComponentType.TRANSPORT_VEHICLE).carrier then
  159.         return api.engine.getComponent(vehicles[1], api.type.ComponentType.TRANSPORT_VEHICLE).carrier == api.type.enum.Carrier[lineType]
  160.     end
  161.     return false
  162. end
  163.  
  164. ---@param line number | string
  165. -- returns String, RGB value string eg: "204060" with Red 20, Green 40, Blue 60
  166. function timetableHelper.getLineColour(line)
  167.     if type(line) == "string" then line = tonumber(line) end
  168.     if not(type(line) == "number") then return "default" end
  169.     local colour = api.engine.getComponent(line, api.type.ComponentType.COLOR)
  170.     if (colour and  colour.color) then
  171.         local a = string.format("%02d", (colour.color.x * 100))
  172.         local b = string.format("%02d", (colour.color.y * 100))
  173.         local c = string.format("%02d", (colour.color.z * 100))
  174.         return a .. b .. c
  175.     else
  176.         return "default"
  177.     end
  178. end
  179.  
  180. ---@param line number | string
  181. -- returns lineName : String
  182. function timetableHelper.getLineName(line)
  183.     if type(line) == "string" then line = tonumber(line) end
  184.     if not(type(line) == "number") then return "ERROR" end
  185.     return api.engine.getComponent(tonumber(line), api.type.ComponentType.NAME).name
  186. end
  187.  
  188. ---@param line number | string
  189. -- returns lineFrequency : String, formatted '%M:%S'
  190. function timetableHelper.getFrequency(line)
  191.     if type(line) == "string" then line = tonumber(line) end
  192.     if not(type(line) == "number") then return "ERROR" end
  193.  
  194.     local lineEntity = game.interface.getEntity(line)
  195.     if lineEntity and lineEntity.frequency then
  196.         if lineEntity.frequency == 0 then return "--" end
  197.         local x = 1 / lineEntity.frequency
  198.         return math.floor(x / 60) .. ":" .. os.date('%S', x)
  199.     else
  200.         return "--"
  201.     end
  202. end
  203.  
  204. -- returns [{id : number, name : String}]
  205. function timetableHelper.getAllRailLines()
  206.     local res = {}
  207.     local ls = api.engine.system.lineSystem.getLines()
  208.     for k,l in pairs(ls) do
  209.         local lineName = api.engine.getComponent(l, 63)
  210.         if lineName and lineName.name then
  211.             res[k] = {id = l, name = lineName.name}
  212.         else
  213.             res[k] = {id = l, name = "ERROR"}
  214.         end
  215.     end
  216.     return res
  217. end
  218.  
  219. ---@param line number | string
  220. -- returns [time: Number] Array indexed by station index in sec starting with index 1
  221. function timetableHelper.getLegTimes(line)
  222.     if type(line) == "string" then line = tonumber(line) end
  223.     if not(type(line) == "number") then return "ERROR" end
  224.     local vehicleLineMap = api.engine.system.transportVehicleSystem.getLine2VehicleMap()
  225.     if vehicleLineMap[line] == nil or vehicleLineMap[line][1] == nil then return {}end
  226.     local vehicle = vehicleLineMap[line][1]
  227.     local vehicleObject = api.engine.getComponent(vehicle, 70)
  228.     if vehicleObject and vehicleObject.sectionTimes then
  229.         return vehicleObject.sectionTimes
  230.     else
  231.         return {}
  232.     end
  233. end
  234.  
  235. -------------------------------------------------------------
  236. ---------------------- Station related ----------------------
  237. -------------------------------------------------------------
  238.  
  239. ---@param station number | string
  240. -- returns {name : String}
  241. function timetableHelper.getStation(station)
  242.     if type(station) == "string" then station = tonumber(station) end
  243.     if not(type(station) == "number") then return "ERROR" end
  244.  
  245.     local stationObject = api.engine.getComponent(station, api.type.ComponentType.NAME)
  246.     if stationObject and stationObject.name then
  247.         return { name = stationObject.name }
  248.     else
  249.         return {name = "ERROR"}
  250.     end
  251. end
  252.  
  253.  
  254. ---@param line number | string
  255. -- returns [id : Number] Array of stationIds
  256. function timetableHelper.getAllStations(line)
  257.     if type(line) == "string" then line = tonumber(line) end
  258.     if not(type(line) == "number") then return "ERROR" end
  259.  
  260.     local lineObject = api.engine.getComponent(line, api.type.ComponentType.LINE)
  261.     if lineObject and lineObject.stops then
  262.         local res = {}
  263.         for k, v in pairs(lineObject.stops) do
  264.             res[k] = v.stationGroup
  265.         end
  266.         return res
  267.     else
  268.         return {}
  269.     end
  270. end
  271.  
  272. ---@param station number | string
  273. -- returns stationName : String
  274. function timetableHelper.getStationName(station)
  275.     if type(station) == "string" then station = tonumber(station) end
  276.     if not(type(station) == "number") then return "ERROR" end
  277.  
  278.     local err, res = pcall(function()
  279.         return api.engine.getComponent(station, api.type.ComponentType.NAME)
  280.     end)
  281.     if err and res then return res.name else return "ERROR" end
  282. end
  283.  
  284.  
  285. ---@param line number | string
  286. ---@param stationNumber number
  287. -- returns stationID : Number and -1 in Error Case
  288. function timetableHelper.getStationID(line, stationNumber)
  289.     if type(line) == "string" then line = tonumber(line) end
  290.     if not(type(line) == "number") then return -1 end
  291.  
  292.     local lineObject = api.engine.getComponent(line, api.type.ComponentType.LINE)
  293.     if lineObject and lineObject.stops and lineObject.stops[stationNumber] then
  294.         return lineObject.stops[stationNumber].stationGroup
  295.     else
  296.         return -1
  297.     end
  298. end
  299.  
  300. -------------------------------------------------------------
  301. ---------------------- Array Functions ----------------------
  302. -------------------------------------------------------------
  303.  
  304. ---@param arr table
  305. -- returns [Number], an Array where the index it the source element and the number is the target position
  306. function timetableHelper.getOrderOfArray(arr)
  307.     local toSort = {}
  308.     for k,v in pairs(arr) do
  309.         toSort[k] = {key =  k, value = v}
  310.     end
  311.     table.sort(toSort, function(a,b)
  312.         return string.lower(a.value) < string.lower(b.value)
  313.     end)
  314.     local res = {}
  315.     for k,v in pairs(toSort) do
  316.         res[k-1] = v.key-1
  317.     end
  318.     return res
  319. end
  320.  
  321. ---@param a table
  322. ---@param b table
  323. -- returns Array, the merged arrays a,b
  324. function timetableHelper.mergeArray(a,b)
  325.     if a == nil then return b end
  326.     if b == nil then return a end
  327.     local ab = {}
  328.     for _, v in pairs(a) do
  329.         table.insert(ab, v)
  330.     end
  331.     for _, v in pairs(b) do
  332.         table.insert(ab, v)
  333.     end
  334.     return ab
  335. end
  336.  
  337. ---@param hasTimetable function lineId -> boolean
  338. -- returns [{vehicleID: lineID}]
  339. function timetableHelper.getAllTimetableRailVehicles(hasTimetable)
  340.     local res = {}
  341.     local vehicleMap = api.engine.system.transportVehicleSystem.getLine2VehicleMap()
  342.     for k,v in pairs(vehicleMap) do
  343.         if (hasTimetable(k)) then
  344.             for _,v2 in pairs(v) do
  345.                 res[tostring(v2)] = k
  346.             end
  347.         end
  348.     end
  349.     return res
  350. end
  351.  
  352.  
  353. -- returns Number, current GameTime in seconds
  354. function timetableHelper.getTime()
  355.     local time = math.floor(api.engine.getComponent(0,16).gameTime/ 1000)
  356.     return time
  357. end
  358.  
  359. ---@param tab table
  360. ---@param val any
  361. -- returns Bool,
  362. function timetableHelper.hasValue(tab, val)
  363.     for _, v in pairs(tab) do
  364.         if v == val then
  365.             return true
  366.         end
  367.     end
  368.  
  369.     return false
  370. end
  371.  
  372. ---@param arr table
  373. -- returns a, the maximum element of the array
  374. function timetableHelper.maximumArray(arr)
  375.     local max = arr[1]
  376.     for k,_ in pairs(arr) do
  377.         if max < arr[k] then
  378.             max = arr[k]
  379.         end
  380.     end
  381.     return max
  382. end
  383.  
  384.  
  385. -------------------------------------------------------------
  386. ---------------------- Other --------------------------------
  387. -------------------------------------------------------------
  388.  
  389. ---@param cond table : TimetableCondition,
  390. ---@param type string, "ArrDep" |"debounce"
  391. -- returns String, ready to display in the UI
  392. function timetableHelper.conditionToString(cond, type)
  393.     if (not cond) or (not type) then return "" end
  394.     if type =="ArrDep" then
  395.         local arr = _("arr_i18n")
  396.         local dep = _("dep_i18n")
  397.         for _,v in pairs(cond) do
  398.             arr = arr .. string.format("%02d", v[1]) .. ":" .. string.format("%02d", v[2])  .. "|"
  399.             dep = dep .. string.format("%02d", v[3]) .. ":" .. string.format("%02d", v[4])  .. "|"
  400.         end
  401.         local res = arr .. "\n"  .. dep
  402.         return res
  403.     elseif type == "debounce" then
  404.         if not cond[1] then cond[1] = 0 end
  405.         if not cond[2] then cond[2] = 0 end
  406.         return _("unbunch_time_i18n").. ": " .. string.format("%02d", cond[1]) .. ":" .. string.format("%02d", cond[2])
  407.     else
  408.         return type
  409.     end
  410. end
  411.  
  412. ---@param i number Index of Combobox,
  413. -- returns String, ready to display in the UI
  414. function timetableHelper.constraintIntToString(i)
  415.     if i == 0 then return "None"
  416.     elseif i == 1 then return "ArrDep"
  417.     --elseif i == 2 then return "minWait"
  418.     elseif i == 2 then return "debounce"
  419.     --elseif i == 4 then return "moreFancey"
  420.     else return "ERROR"
  421.     end
  422. end
  423.  
  424. ---@param i string, "ArrDep" |"debounce"
  425. -- returns Number, index of combobox
  426. function timetableHelper.constraintStringToInt(i)
  427.     if i == "None" then return 0
  428.     elseif i == "ArrDep" then return 1
  429.     --elseif i == "minWait" then return 2
  430.     elseif i == "debounce" then return 2
  431.     --elseif i == "moreFancey" then return 4
  432.     else return 0
  433.     end
  434.  
  435. end
  436.  
  437.  
  438. return timetableHelper
  439.  
Advertisement
Add Comment
Please, Sign In to add comment