Advertisement
Guest User

Orphan Finder mod control.lua for Factorio 0.16

a guest
Jan 18th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. SEARCH_RANGE = 100
  2. ARROW_ENTITY = "orphan-arrow"
  3.  
  4. function createArrowAt(entity, index)
  5.   global.arrows = global.arrows or {}
  6.   global.arrows[index] = global.arrows[index] or {}
  7.   global.arrows[index][entity.unit_number] = entity.surface.create_entity{name = ARROW_ENTITY, position = entity.position}
  8. end
  9.  
  10. function clearArrows(index)
  11.   global.arrows = global.arrows or {}
  12.   global.arrows[index] = global.arrows[index] or {}
  13.   local destroyed = false
  14.   for _,arrow in pairs(global.arrows[index]) do
  15.     arrow.destroy()
  16.     destroyed = true
  17.   end
  18.   global.arrows[index] = nil
  19.   return destroyed
  20. end
  21.  
  22. function deleteArrowAt(entity)
  23.   global.arrows = global.arrows or {}
  24.   for i,_ in pairs(global.arrows) do
  25.     if global.arrows[i][entity.unit_number] then
  26.       global.arrows[i][entity.unit_number].destroy()
  27.       global.arrows[i][entity.unit_number] = nil
  28.       return true
  29.     end
  30.   end
  31.   return false
  32. end
  33.  
  34. function isBeltOrphan(belt)
  35.   return not belt.neighbours
  36. end
  37.  
  38. function adoptBeltNeighbours(belt)
  39.   if belt.neighbours then
  40.     deleteArrowAt(belt.neighbours)
  41.   end
  42. end
  43.  
  44. function isPipeConnectedToNeighbourUnderground(pipe, neighbour)
  45.   if pipe.type == "pipe-to-ground" and neighbour.type == "pipe-to-ground" then
  46.     return (pipe.direction == defines.direction.north and
  47.         neighbour.direction == defines.direction.south and
  48.         neighbour.position.y > pipe.position.y) or
  49.       (pipe.direction == defines.direction.east and
  50.         neighbour.direction == defines.direction.west and
  51.         neighbour.position.x < pipe.position.x) or
  52.       (pipe.direction == defines.direction.south and
  53.         neighbour.direction == defines.direction.north and
  54.         neighbour.position.y < pipe.position.y) or
  55.       (pipe.direction == defines.direction.west and
  56.         neighbour.direction == defines.direction.east and
  57.         neighbour.position.x > pipe.position.x)
  58.   end
  59.   return false
  60. end
  61.  
  62. function isPipeOrphan(pipe)
  63.   local fluidBoxNeighbours
  64.   _,fluidBoxNeighbours = next(pipe.neighbours)
  65.   if fluidBoxNeighbours then
  66.     for _,neighbour in pairs(fluidBoxNeighbours) do
  67.       if isPipeConnectedToNeighbourUnderground(pipe, neighbour) then
  68.         return false
  69.       end
  70.     end
  71.   end
  72.   return true
  73. end
  74.  
  75. function adoptPipeNeighbours(pipe)
  76.   local fluidBoxNeighbours
  77.   _,fluidBoxNeighbours = next(pipe.neighbours)
  78.   if fluidBoxNeighbours then
  79.     for _,neighbour in pairs(fluidBoxNeighbours) do
  80.       if isPipeConnectedToNeighbourUnderground(pipe, neighbour) then
  81.         deleteArrowAt(neighbour)
  82.       end
  83.     end
  84.   end
  85. end
  86.  
  87. script.on_event(defines.events.on_built_entity, function(event)
  88.   if event.created_entity.type == "underground-belt" then
  89.     adoptBeltNeighbours(event.created_entity)
  90.   elseif event.created_entity.type == "pipe-to-ground" then
  91.     adoptPipeNeighbours(event.created_entity)
  92.   end
  93. end)
  94.  
  95. script.on_event(defines.events.on_robot_built_entity, function(event)
  96.   if event.created_entity.type == "underground-belt" then
  97.     adoptBeltNeighbours(event.created_entity)
  98.   elseif event.created_entity.type == "pipe-to-ground" then
  99.     adoptPipeNeighbours(event.created_entity)
  100.   end
  101. end)
  102.  
  103. script.on_event(defines.events.on_player_rotated_entity, function(event)
  104.   if event.entity.type == "pipe-to-ground" then
  105.     if not isPipeOrphan(event.entity) then
  106.       deleteArrowAt(event.entity)
  107.       adoptPipeNeighbours(event.entity)
  108.     end
  109.   end
  110. end)
  111.  
  112. script.on_event(defines.events.on_pre_player_mined_item, function(event)
  113.   deleteArrowAt(event.entity)
  114. end)
  115.  
  116. script.on_event(defines.events.on_robot_pre_mined, function(event)
  117.   deleteArrowAt(event.entity)
  118. end)
  119.  
  120. script.on_event(defines.events.on_entity_died, function(event)
  121.   deleteArrowAt(event.entity)
  122. end)
  123.  
  124. script.on_event(defines.events.on_player_left_game, function(event)
  125.   clearArrows(event.player_index)
  126. end)
  127.  
  128. script.on_event("find-orphans", function(event)
  129.   local next = next
  130.   local player = game.players[event.player_index]
  131.   local search_area =
  132.   {
  133.     {player.position.x - SEARCH_RANGE, player.position.y - SEARCH_RANGE},
  134.     {player.position.x + SEARCH_RANGE, player.position.y + SEARCH_RANGE}
  135.   }
  136.   if not clearArrows(event.player_index) then
  137.     local count = 0
  138.     local belts = player.surface.find_entities_filtered{
  139.       area = search_area,
  140.       type = "underground-belt"
  141.     }
  142.     local pipes = player.surface.find_entities_filtered{
  143.       area = search_area,
  144.       type = "pipe-to-ground"
  145.     }
  146.     for _,belt in pairs(belts) do
  147.       if isBeltOrphan(belt) then
  148.         createArrowAt(belt, event.player_index)
  149.         count = count + 1
  150.       end
  151.     end
  152.     for _,pipe in pairs(pipes) do
  153.       if isPipeOrphan(pipe) then
  154.         createArrowAt(pipe, event.player_index)
  155.         count = count + 1
  156.       end
  157.     end
  158.     if count == 0 then
  159.       player.print{"orphans.found-none"}
  160.     elseif count == 1 then
  161.       player.print{"orphans.found-one"}
  162.     else
  163.       player.print{"orphans.found-many", count}
  164.     end
  165.   else
  166.     player.print{"orphans.markers-cleared"}
  167.   end
  168. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement