Skip to content

Instantly share code, notes, and snippets.

@mulark
Last active September 2, 2018 01:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mulark/bf2b8f63bd64269d891988773ec2dd85 to your computer and use it in GitHub Desktop.
/c
local surface=game.player.surface
local tile_paste_length = 234
local start_tile=0
local times_to_paste=1
local start_tick=game.tick
local start_tick_plus=start_tick+60
local start_tick_plus_plus=start_tick+120
if ((tile_paste_length % 2) ~= 0) then
tile_paste_length = tile_paste_length + 1
end
script.on_event(defines.events.on_tick, function(event)
if(game.tick == start_tick) then
for key, ent in pairs(surface.find_entities_filtered({area={{-1000, (start_tile-(tile_paste_length*(times_to_paste+1)))}, {1000, (start_tile-tile_paste_length)}}, force="player"})) do
if (ent.type ~= "player") then
ent.destroy()
end
end
end
if (game.tick == start_tick_plus) then
for key, ent in pairs(surface.find_entities_filtered({area={{-1000, (start_tile-tile_paste_length)}, {1000, start_tile}}, force="player", type={"straight-rail", "curved-rail"}})) do
local current_name=ent.name
local current_x=ent.position.x
local current_y=ent.position.y
local current_direction=ent.direction
for current_iteration=1,times_to_paste do
surface.create_entity{name=current_name, position={current_x+0, current_y-(tile_paste_length*current_iteration)}, direction=current_direction, force="player"}
local newent = surface.find_entity(current_name, {current_x, current_y-(tile_paste_length*current_iteration)})
newent.copy_settings(ent)
newent.direction = ent.direction
newent.orientation = ent.orientation
newent.update_connections()
end
end;
for key, ent in pairs(surface.find_entities_filtered({area={{-1000, (start_tile-tile_paste_length)}, {1000, start_tile}}, force="player", type="locomotive"})) do
local current_name=ent.name
local current_x=ent.position.x
local current_y=ent.position.y
local current_direction=ent.direction
for current_iteration=1,times_to_paste do
surface.create_entity{name=current_name, position={current_x+0, current_y-(tile_paste_length*current_iteration)}, force="player"}
local newent = surface.find_entity(current_name, {current_x, current_y-(tile_paste_length*current_iteration)})
newent.disconnect_rolling_stock(defines.rail_direction.front)
newent.disconnect_rolling_stock(defines.rail_direction.back)
if (newent.get_fuel_inventory()) then
if (newent.get_fuel_inventory().is_empty()) then
newent.get_fuel_inventory().insert("nuclear-fuel")
end
end
if (ent.orientation <= 0.5) then
if (ent.orientation ~= 0) then
newent.rotate()
end
end
newent.connect_rolling_stock(defines.rail_direction.front)
newent.connect_rolling_stock(defines.rail_direction.back)
newent.copy_settings(ent)
end
end;
local function has_value (val, tab)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
for key, ent in pairs(surface.find_entities_filtered({area={{-1000, (start_tile-tile_paste_length)}, {1000, start_tile}}, force="player"})) do
if not has_value(ent.type, {"beacon", "straight-rail", "curved-rail", "player", "locomotive"}) then
local current_name=ent.name
local current_x=ent.position.x
local current_y=ent.position.y
local current_direction=ent.direction
for current_iteration=1,times_to_paste do
if (ent.type == "underground-belt") then
surface.create_entity{name=current_name, position={current_x+0, current_y-(tile_paste_length*current_iteration)}, direction=current_direction, force="player", type=ent.belt_to_ground_type}
else
surface.create_entity{name=current_name, position={current_x+0, current_y-(tile_paste_length*current_iteration)}, direction=current_direction, force="player"}
end
local newent = surface.find_entity(current_name, {current_x, current_y-(tile_paste_length*current_iteration)})
newent.copy_settings(ent)
newent.direction = ent.direction
newent.orientation = ent.orientation
if (ent.get_module_inventory()) then
for x=1,#ent.get_module_inventory() do
if (ent.get_module_inventory()[x].valid_for_read) then
newent.insert(ent.get_module_inventory()[x])
end
end
end
if (newent.type == "resource") then
newent.amount=ent.amount
end
if (newent.get_fuel_inventory()) then
if (newent.get_fuel_inventory().is_empty()) then
newent.get_fuel_inventory().insert("nuclear-fuel")
end
end
if (ent.get_inventory(defines.inventory.chest)) then
for k=1,#ent.get_inventory(defines.inventory.chest) do
if (ent.get_inventory(defines.inventory.chest)[k].valid_for_read) then
local inventory = ent.get_inventory(defines.inventory.chest)
local itemname = inventory[k].name
local itemamount = inventory[k].count
if (itemamount > 1) then
itemamount = itemamount - 1
end
newent.insert({name=itemname, count=itemamount})
end
end
end
if (ent.get_inventory(defines.inventory.car_trunk)) then
for k=1,#ent.get_inventory(defines.inventory.car_trunk) do
if (ent.get_inventory(defines.inventory.car_trunk)[k].valid_for_read) then
local inventory = ent.get_inventory(defines.inventory.car_trunk)
local itemname = inventory[k].name
local itemamount = inventory[k].count
if (itemamount > 1) then
itemamount = itemamount - 1
end
newent.insert({name=itemname, count=itemamount})
end
end
end
if (ent.circuit_connection_definitions) then
for x=1, #ent.circuit_connection_definitions do
local targetent = ent.circuit_connection_definitions[x].target_entity
offset_x = (ent.position.x - targetent.position.x)
offset_y = (ent.position.y - targetent.position.y)
if (surface.find_entity(targetent.name, {(newent.position.x - offset_x), (newent.position.y - offset_y)})) then
local targetnewent = surface.find_entity(targetent.name, {(newent.position.x - offset_x), (newent.position.y - offset_y)})
newent.connect_neighbour({target_entity = targetnewent, wire=ent.circuit_connection_definitions[x].wire, source_circuit_id=ent.circuit_connection_definitions[x].source_circuit_id, target_circuit_id=ent.circuit_connection_definitions[x].target_circuit_id})
end
end
end
if (newent.train) then
newent.train.manual_mode = ent.train.manual_mode
newent.train.schedule = ent.train.schedule
end
end
end
end
end
if(game.tick == start_tick_plus_plus) then
for key, ent in pairs(surface.find_entities_filtered({area={{-1000, (start_tile-tile_paste_length)}, {1000, start_tile}}, force="player", type="beacon"})) do
local current_name=ent.name
local current_x=ent.position.x
local current_y=ent.position.y
local current_direction=ent.direction
for current_iteration=1,times_to_paste do
surface.create_entity{name=current_name, position={current_x+0, current_y-(tile_paste_length*current_iteration)}, direction=current_direction, force="player"}
local beaconent = surface.find_entity(current_name, {current_x, current_y-(tile_paste_length*current_iteration)})
beaconent.copy_settings(ent)
beaconent.direction = ent.direction
beaconent.orientation = ent.orientation
if (beaconent.get_module_inventory()) then
beaconent.get_module_inventory().insert("speed-module-3")
end
beaconent.update_connections()
end
end
game.forces.player.chart_all()
end
end);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment