Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- control.lua 2017-05-13 11:22:24.000000000 -0700
- +++ control_new.lua 2017-07-26 22:04:08.318756800 -0700
- @@ -302,7 +302,7 @@
- script.on_event(defines.events.on_robot_pre_mined, destroyed)
- end
- -local function hasEquipment(carriage)
- +local function carriageHasEquipment(carriage)
- if carriage.grid and carriage.grid.equipment then
- for _, eq in pairs(carriage.grid.equipment) do
- if eq.name == NAME then return true end
- @@ -311,6 +311,53 @@
- return false
- end
- +local function hasEquipment(train)
- + local valid = false
- + for _,carriage in pairs(train.carriages) do
- + if carriageHasEquipment(carriage) then
- + valid = true
- + break
- + end
- + end
- +
- + return valid
- +end
- +
- +local function trainCanPath(train, testStation)
- + --game.players[1].print("Testing train pathing...")
- + -- save the old schedule
- + local old_manual = train.manual_mode
- + local old_schedule = train.schedule
- +
- + -- set the test schedule
- + train.schedule = {
- + current = 1,
- + records = {
- + {
- + station = testStation,
- + wait_conditions = waitConditions,
- + }
- + }
- + }
- + train.manual_mode = false
- +
- + -- force train to calculate path
- + -- this returns true if it finds one
- + local canPath = train.recalculate_path(true)
- + if canPath then
- + --game.players[1].print("Found a valid train pathing.")
- + else
- + --game.players[1].print("This train has no path.")
- + end
- +
- + -- restore old schedule
- + train.schedule = old_schedule
- + train.manual_mode = old_manual
- +
- +
- + return canPath
- +end
- +
- local inShuttle = {}
- do
- -- Must be safe to call even if we left a different vehicle type.
- @@ -334,7 +381,7 @@
- if not p then return end
- if p.vehicle and p.vehicle.valid then
- if p.vehicle.type ~= "locomotive" or not p.vehicle.train then return end
- - if p.vehicle.unit_number and hasEquipment(p.vehicle.train.front_stock) then
- + if p.vehicle.unit_number and hasEquipment(p.vehicle.train) then
- inShuttle[event.player_index] = p.vehicle.unit_number
- -- Show UI
- local frame = modGui.get_frame_flow(p).shuttle_lite_frame
- @@ -359,17 +406,17 @@
- local function trainState(event)
- if not event or not event.train or not event.train.front_stock then return end
- - if investigate[event.train.front_stock.unit_number] then
- + if investigate[event.train.id] then
- local t = event.train
- if (t.state == defines.train_state.no_path) or (t.state == defines.train_state.path_lost) then
- t.manual_mode = true
- t.front_stock.force.print(noPath)
- - investigate[t.front_stock.unit_number] = nil
- + investigate[t.id] = nil
- elseif ignore[t.state] then
- - investigate[t.front_stock.unit_number] = nil
- + investigate[t.id] = nil
- elseif t.state == defines.train_state.wait_station and #t.schedule.records == 1 then
- t.manual_mode = true
- - investigate[t.front_stock.unit_number] = nil
- + investigate[t.id] = nil
- end
- end
- end
- @@ -389,24 +436,53 @@
- local bestTrain = nil
- local lowestDistance = nil
- + if not bestStation then
- + if p.opened and p.opened.type and p.opened.type == "train-stop" then
- + bestStation = p.opened.backer_name
- + else
- + local stations = p.surface.find_entities_filtered({type = "train-stop", force=p.force})
- + if stations and #stations ~= 0 then
- + for _, station in next, stations do
- + local distance = (((p.position.x - station.position.x) ^ 2) + ((p.position.y - station.position.y) ^ 2)) ^ 0.5
- + if not lowestDistance or distance < lowestDistance then
- + lowestDistance = distance
- + bestStation = station.backer_name
- + end
- + end
- + end
- + end
- + if not bestStation then
- + p.print({"shuttle-lite.no-station-found"})
- + return
- + end
- + end
- +
- if not inShuttle[p.index] then
- + lowestDistance = nil
- for _, train in next, p.force.get_trains(p.surface) do
- if available[train.state] and train.front_stock then
- local free = false
- + if hasEquipment(train) then
- + free = true
- + end
- for _, carriage in next, train.carriages do
- if carriage.passenger then
- free = false
- break
- - elseif hasEquipment(carriage) then
- - free = true
- end
- end
- if free then
- + --p.print("Found valid train id " .. train.id)
- -- this train can be used, find out how far away it is
- local distance = (((p.position.x - train.front_stock.position.x) ^ 2) + ((p.position.y - train.front_stock.position.y) ^ 2)) ^ 0.5
- +
- if not lowestDistance or distance < lowestDistance then
- - lowestDistance = distance
- - bestTrain = train
- + -- check to see if this train can path to station
- + if trainCanPath(train, bestStation) then
- + --p.print("Found new best train with id " .. train.id)
- + lowestDistance = distance
- + bestTrain = train
- + end
- end
- end
- end
- @@ -419,28 +495,6 @@
- bestTrain = p.vehicle.train
- end
- - if not bestStation then
- - lowestDistance = nil
- - if p.opened and p.opened.type and p.opened.type == "train-stop" then
- - bestStation = p.opened.backer_name
- - else
- - local stations = p.surface.find_entities_filtered({type = "train-stop", force=p.force})
- - if stations and #stations ~= 0 then
- - for _, station in next, stations do
- - local distance = (((p.position.x - station.position.x) ^ 2) + ((p.position.y - station.position.y) ^ 2)) ^ 0.5
- - if not lowestDistance or distance < lowestDistance then
- - lowestDistance = distance
- - bestStation = station.backer_name
- - end
- - end
- - end
- - end
- - if not bestStation then
- - p.print({"shuttle-lite.no-station-found"})
- - return
- - end
- - end
- -
- -- Is the train already at the station perhaps?
- if bestTrain.state == defines.train_state.wait_station and bestTrain.schedule.records[1].station == bestStation then
- p.print({"shuttle-lite.already-at-station"})
Advertisement
Add Comment
Please, Sign In to add comment