Panakotta00 / FicsIt-Networks

Control, Monitor, Manage and Automate your Satisfactory.
https://ficsit.app/mod/FicsItNetworks
GNU General Public License v3.0
155 stars 50 forks source link

TimeTable:addStop() - The second parameter does not resolve #252

Closed manuelfederl closed 10 months ago

manuelfederl commented 1 year ago

Describe the bug On a train, when passing the parameters for TimeTable:addStop(), the second parameter for the RailroadStation does not resolve and always returns the following error message in the console:

bad argument #-2 to 'addStop' (Instance is invalid)

This happens when passing in a TrainStation from component.proxy() or TrackGraph:getStations()

To Reproduce Steps to reproduce the behavior:

  1. Build a Computer Case with a CPU, RAM and EEPROM
  2. Connect the Computer Case to two train stations (one as a departing station, one as a destination station)
  3. Set a new locomotive with n wagons at the departing station and set it to automatic drive and a time table with a single station: Departing station
  4. Use the script provided below
  5. The error message should appear in the console when activating the Computer Case

Expected behavior A new stop at the destination train station should be added to the train waiting at the departing station

Screenshots error

Script

-- UTILITY FUNCTIONS
function findDevicesByClass(class)
  local devices = component.proxy(component.findComponent(findClass(class)))
  return devices
end

function findDevicesById(id)
  local device = component.proxy(id)
  return device
end

function dump(o)
  if type(o) == 'table' then
    local s = '{ '
    for k, v in pairs(o) do
      if type(k) ~= 'number' then k = '"' .. k .. '"' end
      s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
    end
    return s .. '} '
  else
    return tostring(o)
  end
end

function getTrainStationById(id)
  local station = findDevicesById(id)
  return station
end

function getTrainStation()
  local station = findDevicesByClass('Build_TrainStation_C')
  return station[1]
end

function getDestinationStationById(id)
  local stations = getTrainStationById(id):getTrackGraph():getStations()

  for _, station in ipairs(stations) do
    if (station.id == id) then
      print('Returned Station:', station.name, id, station.productivity)
      return station
    end
  end

  return nil
end

-- Return an array of all available trains where a train has only 1 stop in their time table (= home station)
function getAvailableTrains()
  local trains = getTrainStation():getTrackGraph():getTrains()
  local availableTrains = { }

  for _, train in ipairs(trains) do
    if (train:getTimeTable().numStops == 1) then

      table.insert(availableTrains, train)
    end
  end

  print('Available Trains:', dump(availableTrains))
  for _, availableTrain in ipairs(availableTrains) do
    print('-->', availableTrain:getName(), 'Stops:', availableTrain:getTimeTable().numStops)
  end

  return availableTrains
end

function addStopToAvailableTrain(destination)
  local availableTrains = getAvailableTrains()
  local ruleSet = { definition = 0, duration = 1.0, isDurationAndRule = false, loadFilters = { }, unloadFilters = { } }

  print('Destination:', destination.name, destination:getType().name)
  print('Is valid Station?', not not destination)

  availableTrains[1]:getTimeTable():addStop(availableTrains[1]:getTimeTable().numStops + 1, destination, ruleSet)

end

addStopToAvailableTrain(getDestinationStationById('70940CCD4B106A6290E73AA58AB7654F'))  -- This is the ID of the destination station for testing purposes

Additional context SMM Version: 2.9.3 FicsIt-Networks Version: Latest (0.3.12) Satisfactory Version: Early Access CL#211839