Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local item_to_place = "concrete"
- local max_at_once = 20
- local current_index = 0
- local max_creepers = 0
- local tick_multiplier_calculated = 600
- local function recalc_multiplier(a)
- max_creepers = max_creepers + (a or 0)
- tick_multiplier_calculated = math.ceil(math.max(1, math.min(1000, 5 * math.max(1, 10 - max_creepers) / 10)) - .5)
- end
- local function addPort(robo)
- recalc_multiplier(1)
- global.creepers[max_creepers] = {roboport = robo, valid = true, started = false, x = 0, y = 0, dx = 0, dy = -1, t = 0, i = 0}
- end
- local function init()
- global.creepers = {}
- current_index = 1
- local roboports = game.surfaces[1].find_entities_filtered{type="roboport"}
- for index, port in pairs(roboports) do
- addPort(port)
- end
- init_done = true
- end
- local function checkRoboport(index)
- local creeper = global.creepers[index]
- if creeper and creeper.valid then
- local roboport = creeper.roboport
- if roboport and roboport.valid then
- local network = roboport.logistic_network
- if network and network.valid then
- local cell = roboport.logistic_cell
- if cell and cell.valid then
- if network.available_construction_robots > 0 then
- if not creeper.didTrees then
- 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
- v.order_deconstruction(roboport.force)
- end
- creeper.didTrees = true
- end
- if network.get_item_count(item_to_place) > 0 then
- local max_rad = cell.construction_radius * 2
- local maxI = max_rad * max_rad
- local count = 0
- for i = creeper.i, maxI do
- if network.get_item_count(item_to_place) < 1 or network.available_construction_robots < 1 then
- return
- end
- 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
- local tile = roboport.surface.get_tile(roboport.position.x + creeper.x, roboport.position.y + creeper.y)
- if not tile.name:find(item_to_place) then
- 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
- 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}
- creeper.i = i
- count = count + 1
- if count >= max_at_once then
- return
- end
- end
- end
- end
- 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
- creeper.t = creeper.dx
- creeper.dx = -creeper.dy
- creeper.dy = creeper.t
- end
- creeper.x = creeper.x + creeper.dx
- creeper.y = creeper.y + creeper.dy
- creeper.i = creeper.i + 1
- end
- end
- end
- end
- end
- else
- table.remove(global.creepers, index)
- recalc_multiplier(-1)
- end
- else
- current_index = 1
- end
- end
- script.on_event(defines.events.on_built_entity, function(event)
- if global.creepers then
- if event.created_entity and event.created_entity.valid and event.created_entity.type == "roboport" then
- addPort(event.created_entity)
- end
- else
- init()
- end
- end)
- script.on_event(defines.events.on_tick, function(event)
- if global.creepers then
- if event.tick % tick_multiplier_calculated == 0 then
- if max_creepers == 0 then init() end
- checkRoboport(current_index)
- current_index = current_index + 1
- end
- else
- init()
- end
- end)
- script.on_configuration_changed(function()
- init()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement