Skip to content

Commit

Permalink
fixed decoupled train missing after state change
Browse files Browse the repository at this point in the history
reset cargo output when updating train after it changed
  • Loading branch information
Choumiko committed Jul 31, 2016
1 parent 1d14b13 commit fc735e4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Train.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Train = {
local new = {
train = train,
ID = id,
locomotives = {},
line = false,
lineVersion = 0,
settings = {},
Expand Down Expand Up @@ -84,6 +85,7 @@ Train = {
update = function(self, train, id)
self.ID = id
self.train = train
self.locomotives = self:getLocomotives()
self.type = self:getType()
self.name = self:getName()
self.passengers = 0
Expand Down
117 changes: 76 additions & 41 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ end

TrainList.updateTrainInfo = function(train)
local newMainID = TrainList.getTrainID(train)
if not newMainID then
log("No locomotive in train")
return
end
if not newMainID then return end
local newLocos = TrainList.getLocomotives(train)
local found = {}
local foundMainID = false
Expand All @@ -110,20 +107,35 @@ TrainList.updateTrainInfo = function(train)
local ti = global.trains[loco.unit_number]
end
end
-- old front loco stays front loco
if foundMainID then
for _, old in pairs(found) do
global.trains[old.ID] = nil
TrainList.removeTrain(old)
end
-- check old locomotives for different train (decoupling)
local newTrain = false
for _, oldLoco in pairs(foundMainID.locomotives) do
if TrainList.getTrainID(oldLoco.train) ~= newMainID then
newTrain = oldLoco.train
end
end
if newTrain then
TrainList.addTrainInfo(newTrain)
end
foundMainID:update(train, newMainID)
foundMainID:resetWaitingStation()
global.trains[newMainID] = foundMainID
-- loco/cargo wagon added in front of old front loco
else
if #found > 0 then
found[1]:update(train, newMainID)
found[1]:resetWaitingStation()
global.trains[newMainID] = found[1]
for i=#found, 2, -1 do
global.trains[found[i].ID] = nil
TrainList.removeTrain(found[i])
end
else
--loco was added to cargo-wagon
return TrainList.addTrainInfo(train)
end
end
Expand Down Expand Up @@ -153,7 +165,43 @@ TrainList.getTrainInfoFromUI = function(playerIndex)
end

TrainList.removeTrain = function(trainInfo)
global.trains[trainInfo.ID] = nil
if trainInfo and trainInfo.ID and global.trains[trainInfo.ID] then
trainInfo:resetWaitingStation()
global.trains[trainInfo.ID] = nil
end
end

TrainList.removeInvalidTrains = function(show)
local removed = 0
show = show or debug
for id, ti in pairs(global.trains) do
if not ti.train or not ti.train.valid or (#ti.train.locomotives.front_movers == 0 and #ti.train.locomotives.back_movers == 0) then
ti:resetCircuitSignal()

TrainList.removeTrain(ti)
removed = removed + 1
-- try to detect change through pressing G/V
else
local test = ti:getType()
if test ~= ti.type then
ti:resetCircuitSignal()
TrainList.removeTrain(ti)
removed = removed + 1
end
end
end
if removed > 0 and show then
flyingText("Removed "..removed.." invalid trains", colors.RED, false, true)
end
return removed
end

TrainList.getCount = function()
local c = 0
for _, _ in pairs(global.trains) do
c = c + 1
end
return c
end

local function debugLog(var, prepend)
Expand Down Expand Up @@ -567,7 +615,7 @@ local update_from_version = {
end
end
fixSmartstopTable()
removeInvalidTrains()
TrainList.removeInvalidTrains()
findStations()

for _, l in pairs(global.trainLines) do
Expand Down Expand Up @@ -747,7 +795,7 @@ local update_from_version = {
end
end
if remove_invalid then
removeInvalidTrains()
TrainList.removeInvalidTrains()
end
return "0.3.96"
end,
Expand Down Expand Up @@ -776,19 +824,30 @@ local update_from_version = {
["0.4.3"] = function() return "0.4.4" end,
["0.4.4"] = function()
local trains = {}
local trainCount = #global.trains
local id = false
for _, trainInfo in pairs(global.trains) do
id = TrainList.getTrainID(trainInfo.train)
assert(id and not trains[id])
if id then
trainInfo.ID = id
trainInfo:update(trainInfo.train, id)
trains[id] = trainInfo
end
end
global.train = trains
return "0.4.5"
end,
["0.4.5"] = function()
local id
for _, trainInfo in pairs(global.trains) do
if trainInfo.train and trainInfo.train.valid then
id = TrainList.getTrainID(trainInfo.train)
trainInfo:update(trainInfo.train, id)
else
TrainList.removeTrain(trainInfo)
end
end
return "0.4.6"
end,
}

function on_configuration_changed(data)
Expand Down Expand Up @@ -986,31 +1045,6 @@ function findSmartTrainStopByTrain(vehicle, rail, stationName)
return false
end

function removeInvalidTrains(show)
local removed = 0
show = show or debug
for id, ti in pairs(global.trains) do
if not ti.train or not ti.train.valid or (#ti.train.locomotives.front_movers == 0 and #ti.train.locomotives.back_movers == 0) then
ti:resetCircuitSignal()

global.trains[id] = nil
removed = removed + 1
-- try to detect change through pressing G/V
else
local test = ti:getType()
if test ~= ti.type then
ti:resetCircuitSignal()
global.trains[id] = nil
removed = removed + 1
end
end
end
if removed > 0 and show then
flyingText("Removed "..removed.." invalid trains", colors.RED, false, true)
end
return removed
end

function inSchedule(station, schedule)
if type(schedule) == "table" and type(schedule.records) == "table" then
for i, rec in pairs(schedule.records) do
Expand Down Expand Up @@ -1085,7 +1119,7 @@ function addInventoryContents(invA, invB)
return res
end

function getKeyByValue(t, value)
local function getKeyByValue(t, value)
for k, v in pairs(t) do
if v == value then
return k
Expand All @@ -1100,7 +1134,7 @@ function on_train_changed_state(event)
local train = event.train
local trainKey
local t = TrainList.getTrainInfo(train)
log(serpent.line(t))
--log(serpent.line(t,{comment=false}))
--assert(t.train.valid)
-- TODO is this for de/coupling an automated train???
if not t.train.valid then
Expand Down Expand Up @@ -1190,6 +1224,7 @@ function on_train_changed_state(event)

local schedule = train.schedule
if train.state == defines.train_state.manual_control_stop or train.state == defines.train_state.manual_control then
-- TODO Why like this??
for _, train_ in pairs(global.trains) do
if train_ == t then
t:resetWaitingStation()
Expand Down Expand Up @@ -1275,7 +1310,7 @@ function on_tick(event)
remove_invalid = true
end
end
if remove_invalid then removeInvalidTrains() end
if remove_invalid then TrainList.removeInvalidTrains() end
end)
if not status then
pauseError(err, "on_tick: refueling")
Expand Down Expand Up @@ -1307,7 +1342,7 @@ function on_tick(event)
remove_invalid = true
end
end
if remove_invalid then removeInvalidTrains() end
if remove_invalid then TrainList.removeInvalidTrains() end
end)
if not status then
pauseError(err, "on_tick: update_cargo")
Expand Down Expand Up @@ -1619,7 +1654,7 @@ function on_player_mined_item(event)
end
global.tmpPos = nil
end
removeInvalidTrains()
TrainList.removeInvalidTrains()
end
end)
if not status then
Expand Down
7 changes: 2 additions & 5 deletions gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ GUI = {
GUI.addLabel(tbl, {"stg-intervals-write"}).style.left_padding = 10
GUI.addTextfield(tbl, {name="intervals_write", style="st_textfield_small", text = global.settings.intervals.write})

GUI.addLabel(tbl, {"",{"stg-tracked-trains"}, " ", #global.trains})
GUI.addLabel(tbl, {"",{"stg-tracked-trains"}, " ", TrainList.getCount()})
local noStations, uniqueStations = 0,0
local force = game.players[index].force.name
for _,station in pairs(global.stationCount[force]) do
Expand Down Expand Up @@ -643,10 +643,7 @@ end

on_gui_click = {
add_trains_to_update = function(line, newConditions)
local trains = global.trains
local train
for i=1, #trains do
train = trains[i]
for i, train in pairs(global.trains) do
if train and train.line and train.line == line and train.train.valid
and train.train.state == defines.train_state.wait_station and not train.opened then
if newConditions[train.train.schedule.current] then
Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SmartTrains",
"version": "0.4.5",
"version": "0.4.6",
"factorio_version": "0.13",
"title": "Smarter Trains",
"author": "Choumiko",
Expand Down

0 comments on commit fc735e4

Please sign in to comment.