Advertisement
Guest User

Untitled

a guest
Nov 19th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. local item_to_place = "concrete"
  2. local max_at_once = 20
  3.  
  4. local current_index = 0
  5. local max_creepers = 0
  6. local tick_multiplier_calculated = 600
  7.  
  8. local function recalc_multiplier(a)
  9.     max_creepers = max_creepers + (a or 0)
  10.     tick_multiplier_calculated = math.ceil(math.max(1, math.min(1000, 5 * math.max(1, 10 - max_creepers) / 10)) - .5)
  11. end
  12.  
  13. local function addPort(robo)
  14.     recalc_multiplier(1)
  15.     global.creepers[max_creepers] = {roboport = robo, valid = true, started = false, x = 0, y = 0, dx = 0, dy = -1, t = 0, i = 0}
  16. end
  17.  
  18. local function init()
  19.     global.creepers = {}
  20.     current_index = 1
  21.     local roboports = game.surfaces[1].find_entities_filtered{type="roboport"}
  22.     for index, port in pairs(roboports) do
  23.         addPort(port)
  24.     end
  25.     init_done = true
  26. end
  27.  
  28. local function checkRoboport(index)
  29.     local creeper = global.creepers[index]
  30.     if creeper and creeper.valid then
  31.         local roboport = creeper.roboport
  32.         if roboport and roboport.valid then
  33.             local network = roboport.logistic_network
  34.             if network and network.valid then
  35.                 local cell = roboport.logistic_cell
  36.                 if cell and cell.valid then
  37.                     if network.available_construction_robots > 0 then
  38.                         if not creeper.didTrees then
  39.                             for i, v in pairs(roboport.surface.find_entities_filtered{type = "tree", area={{roboport.position.x - cell.construction_radius + 1, roboport.position.y - cell.construction_radius + 1},{roboport.position.x + cell.construction_radius - 1, roboport.position.y + cell.construction_radius - 1}}}) do
  40.                                 v.order_deconstruction(roboport.force)
  41.                             end
  42.                             creeper.didTrees = true
  43.                         end
  44.                         if network.get_item_count(item_to_place) > 0 then
  45.                             local max_rad = cell.construction_radius * 2
  46.                             local maxI = max_rad * max_rad
  47.                             local count = 0
  48.                             for i = creeper.i, maxI do
  49.                                 if network.get_item_count(item_to_place) < 1 or network.available_construction_robots < 1 then
  50.                                     return
  51.                                 end
  52.                                 if ((-max_rad/2 <= creeper.x) and (creeper.x <= max_rad/2) and (-max_rad/2 <= creeper.y) and (creeper.y <= max_rad/2)) then
  53.                                     local tile = roboport.surface.get_tile(roboport.position.x + creeper.x, roboport.position.y + creeper.y)
  54.                                     if not tile.name:find(item_to_place) then
  55.                                         if roboport.surface.can_place_entity{name="tile-ghost", position={roboport.position.x + creeper.x, roboport.position.y + creeper.y}, inner_name=item_to_place, force=roboport.force} then
  56.                                             roboport.surface.create_entity{name="tile-ghost", position={roboport.position.x + creeper.x, roboport.position.y + creeper.y}, inner_name=item_to_place, force=roboport.force}
  57.                                             creeper.i = i
  58.                                             count = count + 1
  59.                                             if count >= max_at_once then
  60.                                                 return
  61.                                             end
  62.                                         end
  63.                                     end
  64.                                 end
  65.                                 if ((creeper.x == creeper.y) or ((creeper.x < 0) and (creeper.x == -creeper.y)) or ((creeper.x > 0) and (creeper.x == 1-creeper.y))) then
  66.                                     creeper.t = creeper.dx
  67.                                     creeper.dx = -creeper.dy
  68.                                     creeper.dy = creeper.t
  69.                                 end
  70.                                 creeper.x = creeper.x + creeper.dx
  71.                                 creeper.y = creeper.y + creeper.dy
  72.                                 creeper.i = creeper.i + 1
  73.                             end
  74.                         end
  75.                     end
  76.                 end
  77.             end
  78.         else
  79.             table.remove(global.creepers, index)
  80.             recalc_multiplier(-1)
  81.         end
  82.     else
  83.         current_index = 1
  84.     end
  85. end
  86.  
  87. script.on_event(defines.events.on_built_entity, function(event)
  88.     if global.creepers then
  89.         if event.created_entity and event.created_entity.valid and event.created_entity.type == "roboport" then
  90.             addPort(event.created_entity)
  91.         end
  92.     else
  93.         init()
  94.     end
  95. end)
  96.  
  97. script.on_event(defines.events.on_tick, function(event)
  98.     if global.creepers then
  99.         if event.tick % tick_multiplier_calculated == 0 then
  100.             if max_creepers == 0 then init() end
  101.             checkRoboport(current_index)
  102.             current_index = current_index + 1
  103.         end
  104.     else
  105.         init()
  106.     end
  107. end)
  108.  
  109. script.on_configuration_changed(function()
  110.     init()
  111. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement