Skip to content

Instantly share code, notes, and snippets.

@morsk
morsk / factorio-restore-crash-site.lua
Created February 23, 2026 22:07
Factorio: Restore Crash Site
/silent-command
--[[
This is Factorio code from data/core/lualib/crash-site.lua, with unnecessary parts removed,
especially the fires and explosions which were many lines of code.
It is Wube code and subject to their EULA, and they can DMCA it, but they generally don't care
about their script being reused in mods and player script.
If the crash site area is cluttered, it will displace the crash parts into places that fit, which may look weird.
So it's best to clear the area before using it.
It could of course be modified to clear things itself, but I didn't try.
@morsk
morsk / logi_group_finder.lua
Last active February 17, 2025 12:33
Find entities using the named logistic group
/sc --[[ logi group finder ]]
local c = 0
local function check_for_group(e, section_container, group_name)
for _, section in pairs(section_container.sections) do
if section.group == group_name then
game.player.print({"", e.localised_name, ": ", e.gps_tag})
c = c + 1
end
end
end
@morsk
morsk / spore_absorptions_per_second.lua
Last active November 22, 2024 22:02
[Factorio] Dump spore-absorption tile data from Gleba.
/c --[[ Dump spore-absorption tile data from gleba. This will take a few seconds to run! ]]
local function tiles_on_surface(surface)
local found_tiles = {}
for c in surface.get_chunks() do
local box = c.area
for x = box.left_top.x, box.right_bottom.x do
for y = box.left_top.y, box.right_bottom.y do
local tile = surface.get_tile(x, y)
if tile.valid and not found_tiles[tile.name] then
found_tiles[tile.name] = true
@morsk
morsk / vehicle-finder.lua
Created November 13, 2024 23:27
Find Factorio Vehicles
/sc --[[ vehicle finder ]]
local found_vehicles = game.player.surface.find_entities_filtered{type = {"car", "spider-vehicle"}, force=game.player.force}
for _, vehicle in pairs(found_vehicles) do
game.player.print({"", vehicle.localised_name, ": ", vehicle.gps_tag})
end
if table_size(found_vehicles) == 0 then
game.player.print({"gui.nothing-found"})
end
/sc --[[ undo landfill 3.0, for Patch 2.0 ]]
--[[
EXPERIMENTAL!!! I took my 1.1 script and tried to make it work in 2.0 in a hurry!!
Truly undoes landfill. Regenerates the original chunks in a separate surface to see which water/
shore tiles used to be under the landfill, copies from those tiles, and cleans up afterwards.
Ends in an unterminated quote, intended to contain map pings.
Will select a box region around all pings.
Any number of pings 2+ will work, but it's most intuitive to ping 2 opposing corners, or 4 sides.
/sc --[[ blueprint change grid position ]]
local function printf(...) game.player.print(string.format(...)) end
local function handle_blueprint(bp, new_position) --[[ not recursive; does one blueprint ]]
if not (bp.blueprint_snap_to_grid and bp.blueprint_absolute_snapping) then
return 0 --[[ changed nothing ]]
end
local old_x = bp.blueprint_position_relative_to_grid.x
local old_y = bp.blueprint_position_relative_to_grid.y
if new_position.x ~= old_x or new_position.y ~= old_y then
printf("Converting %d,%d -> %d,%d \"%s\"", old_x, old_y, new_position.x, new_position.y, bp.label)
@morsk
morsk / destroy_logistic_bots.lua
Created March 26, 2024 02:22
Destroy all logistic bots on the force + surface. [Factorio]
/c --[[ Destroy all logistic bots on the force + surface. ]]
--[[ utility to count bots destroyed ]]
local counts = {}
local function count_bot(name, n)
counts[name] = (counts[name] or 0) + n
end
--[[ Find bot item names in this game. Start by searching entity prototypes. ]]
local bot_names = {}
for _, bot_prototype in pairs(game.get_filtered_entity_prototypes{{filter="type", type="logistic-robot"}}) do
for _, item_stack in pairs(bot_prototype.items_to_place_this) do
@morsk
morsk / remove_rocks_on_map.lua
Last active December 30, 2024 14:43
remove rocks on map [Factorio]
/c --[[ remove rocks ]]
local function count_in(t, s)
t[s] = (t[s] or 0) + 1
end
local counts = {}
for _,e in pairs(game.player.surface.find_entities_filtered{type = {"simple-entity"}}) do
--[[ starts rock- , has -rock- in middle, or ends -rock ]]
if e.name:match("^rock%-") or e.name:match("%-rock%-") or e.name:match("%-rock$") then
count_in(counts, e.name)
e.destroy()
@morsk
morsk / chunk-remover.lua
Created February 7, 2021 02:31
Factorio Chunk Remover. Put map pings inside the double quotes at the end to select a region.
/c
--[[ CHUNK REMOVER --]]
local SAFE_PLAYER_DISTANCE = 64 --[[ must be >= 32 --]]
local function bounding_box_from_gps_tags(s)
local x1 = math.huge
local y1,x2,y2 = x1,-x1,-x1
for x,y in s:gmatch("%[gps=([+-]?%d+),([+-]?%d+)%]") do
x1 = math.min(x1, x+0)
y1 = math.min(y1, y+0)
x2 = math.max(x2, x+0)
@morsk
morsk / gist:98a555501beb5eea00f4d09821762a30
Last active May 15, 2020 03:29
Recipe Data Dump [Factorio]
/c
--[[ file appears in Factorio's script-output folder ]]
local OUTPUT_FILE = "recipe_dump.txt"
local function print_table_to_file(t,filename)
local text_format_of_table = serpent.block(t)
game.write_file(filename, text_format_of_table)
end