scottbot95

Shuttle-Trains-lite patch

Jul 27th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.88 KB | None | 0 0
  1. --- control.lua 2017-05-13 11:22:24.000000000 -0700
  2. +++ control_new.lua 2017-07-26 22:04:08.318756800 -0700
  3. @@ -302,7 +302,7 @@
  4.     script.on_event(defines.events.on_robot_pre_mined, destroyed)
  5.  end
  6.  
  7. -local function hasEquipment(carriage)
  8. +local function carriageHasEquipment(carriage)
  9.     if carriage.grid and carriage.grid.equipment then
  10.         for _, eq in pairs(carriage.grid.equipment) do
  11.             if eq.name == NAME then return true end
  12. @@ -311,6 +311,53 @@
  13.     return false
  14.  end
  15.  
  16. +local function hasEquipment(train)
  17. +   local valid = false
  18. +   for _,carriage in pairs(train.carriages) do
  19. +       if carriageHasEquipment(carriage) then
  20. +           valid = true
  21. +           break
  22. +       end
  23. +   end
  24. +
  25. +   return valid
  26. +end
  27. +
  28. +local function trainCanPath(train, testStation)
  29. +   --game.players[1].print("Testing train pathing...")
  30. +   -- save the old schedule
  31. +   local old_manual = train.manual_mode
  32. +   local old_schedule = train.schedule
  33. +
  34. +   -- set the test schedule
  35. +   train.schedule = {
  36. +       current = 1,
  37. +       records = {
  38. +           {
  39. +               station = testStation,
  40. +               wait_conditions = waitConditions,
  41. +           }
  42. +       }
  43. +   }
  44. +   train.manual_mode = false
  45. +
  46. +   -- force train to calculate path
  47. +   -- this returns true if it finds one
  48. +   local canPath = train.recalculate_path(true)
  49. +   if canPath then
  50. +       --game.players[1].print("Found a valid train pathing.")
  51. +   else
  52. +       --game.players[1].print("This train has no path.")
  53. +   end
  54. +
  55. +   -- restore old schedule
  56. +   train.schedule = old_schedule
  57. +   train.manual_mode = old_manual
  58. +
  59. +
  60. +   return canPath
  61. +end
  62. +
  63.  local inShuttle = {}
  64.  do
  65.     -- Must be safe to call even if we left a different vehicle type.
  66. @@ -334,7 +381,7 @@
  67.         if not p then return end
  68.         if p.vehicle and p.vehicle.valid then
  69.             if p.vehicle.type ~= "locomotive" or not p.vehicle.train then return end
  70. -           if p.vehicle.unit_number and hasEquipment(p.vehicle.train.front_stock) then
  71. +           if p.vehicle.unit_number and hasEquipment(p.vehicle.train) then
  72.                 inShuttle[event.player_index] = p.vehicle.unit_number
  73.                 -- Show UI
  74.                 local frame = modGui.get_frame_flow(p).shuttle_lite_frame
  75. @@ -359,17 +406,17 @@
  76.  
  77.     local function trainState(event)
  78.         if not event or not event.train or not event.train.front_stock then return end
  79. -       if investigate[event.train.front_stock.unit_number] then
  80. +       if investigate[event.train.id] then
  81.             local t = event.train
  82.             if (t.state == defines.train_state.no_path) or (t.state == defines.train_state.path_lost) then
  83.                 t.manual_mode = true
  84.                 t.front_stock.force.print(noPath)
  85. -               investigate[t.front_stock.unit_number] = nil
  86. +               investigate[t.id] = nil
  87.             elseif ignore[t.state] then
  88. -               investigate[t.front_stock.unit_number] = nil
  89. +               investigate[t.id] = nil
  90.             elseif t.state == defines.train_state.wait_station and #t.schedule.records == 1 then
  91.                 t.manual_mode = true
  92. -               investigate[t.front_stock.unit_number] = nil
  93. +               investigate[t.id] = nil
  94.             end
  95.         end
  96.     end
  97. @@ -389,24 +436,53 @@
  98.         local bestTrain = nil
  99.         local lowestDistance = nil
  100.  
  101. +       if not bestStation then
  102. +           if p.opened and p.opened.type and p.opened.type == "train-stop" then
  103. +               bestStation = p.opened.backer_name
  104. +           else
  105. +               local stations = p.surface.find_entities_filtered({type = "train-stop", force=p.force})
  106. +               if stations and #stations ~= 0 then
  107. +                   for _, station in next, stations do
  108. +                       local distance = (((p.position.x - station.position.x) ^ 2) + ((p.position.y - station.position.y) ^ 2)) ^ 0.5
  109. +                       if not lowestDistance or distance < lowestDistance then
  110. +                           lowestDistance = distance
  111. +                           bestStation = station.backer_name
  112. +                       end
  113. +                   end
  114. +               end
  115. +           end
  116. +           if not bestStation then
  117. +               p.print({"shuttle-lite.no-station-found"})
  118. +               return
  119. +           end
  120. +       end
  121. +
  122.         if not inShuttle[p.index] then
  123. +           lowestDistance = nil
  124.             for _, train in next, p.force.get_trains(p.surface) do
  125.                 if available[train.state] and train.front_stock then
  126.                     local free = false
  127. +                   if hasEquipment(train) then
  128. +                       free = true
  129. +                   end
  130.                     for _, carriage in next, train.carriages do
  131.                         if carriage.passenger then
  132.                             free = false
  133.                             break
  134. -                       elseif hasEquipment(carriage) then
  135. -                           free = true
  136.                         end
  137.                     end
  138.                     if free then
  139. +                       --p.print("Found valid train id " .. train.id)
  140.                         -- this train can be used, find out how far away it is
  141.                         local distance = (((p.position.x - train.front_stock.position.x) ^ 2) + ((p.position.y - train.front_stock.position.y) ^ 2)) ^ 0.5
  142. +
  143.                         if not lowestDistance or distance < lowestDistance then
  144. -                           lowestDistance = distance
  145. -                           bestTrain = train
  146. +                           -- check to see if this train can path to station
  147. +                           if trainCanPath(train, bestStation) then
  148. +                               --p.print("Found new best train with id " .. train.id)
  149. +                               lowestDistance = distance
  150. +                               bestTrain = train
  151. +                           end
  152.                         end
  153.                     end
  154.                 end
  155. @@ -419,28 +495,6 @@
  156.             bestTrain = p.vehicle.train
  157.         end
  158.  
  159. -       if not bestStation then
  160. -           lowestDistance = nil
  161. -           if p.opened and p.opened.type and p.opened.type == "train-stop" then
  162. -               bestStation = p.opened.backer_name
  163. -           else
  164. -               local stations = p.surface.find_entities_filtered({type = "train-stop", force=p.force})
  165. -               if stations and #stations ~= 0 then
  166. -                   for _, station in next, stations do
  167. -                       local distance = (((p.position.x - station.position.x) ^ 2) + ((p.position.y - station.position.y) ^ 2)) ^ 0.5
  168. -                       if not lowestDistance or distance < lowestDistance then
  169. -                           lowestDistance = distance
  170. -                           bestStation = station.backer_name
  171. -                       end
  172. -                   end
  173. -               end
  174. -           end
  175. -           if not bestStation then
  176. -               p.print({"shuttle-lite.no-station-found"})
  177. -               return
  178. -           end
  179. -       end
  180. -
  181.         -- Is the train already at the station perhaps?
  182.         if bestTrain.state == defines.train_state.wait_station and bestTrain.schedule.records[1].station == bestStation then
  183.             p.print({"shuttle-lite.already-at-station"})
Advertisement
Add Comment
Please, Sign In to add comment