diff --git a/control.lua b/control.lua index b871aed..c85d47a 100644 --- a/control.lua +++ b/control.lua @@ -2,11 +2,11 @@ local movingstate = { [defines.train_state.on_the_path] = true, - [defines.train_state.path_lost] = true, + [defines.train_state.path_lost] = false, [defines.train_state.no_schedule] = false, [defines.train_state.no_path] = false, [defines.train_state.arrive_signal] = true, - [defines.train_state.wait_signal] = false, + [defines.train_state.wait_signal] = true, [defines.train_state.arrive_station] = true, [defines.train_state.wait_station] = false, [defines.train_state.manual_control_stop] = false, @@ -14,11 +14,12 @@ local movingstate = { } local function rotate(loco) - loco.disconnect_rolling_stock(defines.rail_direction.back) - loco.disconnect_rolling_stock(defines.rail_direction.front) + local back = loco.disconnect_rolling_stock(defines.rail_direction.back) + local front = loco.disconnect_rolling_stock(defines.rail_direction.front) local ret = loco.rotate() - loco.connect_rolling_stock(defines.rail_direction.back) - loco.connect_rolling_stock(defines.rail_direction.front) + -- @todo: Check if this needs to be inversed due to rotate or not? + if back then loco.connect_rolling_stock(defines.rail_direction.back) end + if front then loco.connect_rolling_stock(defines.rail_direction.front) end return ret end @@ -28,9 +29,7 @@ script.on_init(function() end) script.on_event(defines.events.on_train_created, function (event) - global.movingstate = global.movingstate or {} - global.lastmode = global.lastmode or {} - if event.old_train_id_1 then -- We can ignore the second one since the first old id is the one that gets its stuff copied. + if event.old_train_id_1 then -- We can ignore old_train_id_2 since the first old id is the one that gets its stuff copied. -- Train changed global.movingstate[event.train.id] = global.movingstate[event.old_train_id_1] global.movingstate[event.old_train_id_1] = nil @@ -44,6 +43,50 @@ script.on_event(defines.events.on_train_created, function (event) end end) +script.on_nth_tick(10, function() + local trains = game.surfaces[1].get_trains() + for _,train in pairs(trains) do + local moving = train.speed ~= 0 --@todo: Maybe see slow moving as stopped as well? + if moving ~= global.movingstate[train.id] then + global.movingstate[train.id] = moving + global.lastmode[train.id] = train.manual_mode + if moving then + -- figure out which locos are facing the wrong way + -- I currently know of no way to reliably tell which locomotives are moving forward and visa versa. + game.print("f: " .. train.front_stock.unit_number .. " b: " .. train.back_stock.unit_number) + for _,w in pairs(train.locomotives) do + for k,v in pairs(w) do + game.print(k .. ":" .. v.unit_number .. ": " .. v.orientation) + end + end + for _,v in pairs(train.carriages) do + if v.type == "locomotive" then + game.print(v.unit_number .. ": " .. v.orientation) + end + end + else + for _, loco in pairs(train.locomotives.back_movers) do + if global[loco.unit_number] then + rotate(loco) + global[loco.unit_number] = nil + train = loco.train + end + end + for _, loco in pairs(train.locomotives.front_movers) do + if global[loco.unit_number] then + rotate(loco) + global[loco.unit_number] = nil + train = loco.train + end + end + train.manual_mode = global.lastmode[train.id] + end + end + end +end) + +-- @todo: I may have to do this stuff in on-tick and just check train speed/direction then +--[[ script.on_event(defines.events.on_train_changed_state, function(event) local train = event.train if train.state == defines.train_state.manual_control or train.state == defines.train_state.manual_control_stop then @@ -52,11 +95,14 @@ script.on_event(defines.events.on_train_changed_state, function(event) if event.old_state == defines.train_state.manual_control or event.old_state == defines.train_state.manual_control_stop then return end + game.print("f:" .. train.front_stock.unit_number) + game.print("b:" .. train.back_stock.unit_number) if movingstate[train.state] ~= global.movingstate[train.id] then global.movingstate[train.id] = movingstate[train.state] global.lastmode[train.id] = train.manual_mode if movingstate[train.state] then for _, loco in pairs(train.locomotives.back_movers) do + game.print("l" .. loco.unit_number .. ":" .. serpent.line(loco.train.riding_state)) global[loco.unit_number] = rotate(loco) train = loco.train end @@ -79,4 +125,5 @@ script.on_event(defines.events.on_train_changed_state, function(event) train.manual_mode = global.lastmode[train.id] end end -end) \ No newline at end of file +end) +--]] \ No newline at end of file