Skip to content

Instantly share code, notes, and snippets.

@morsk
Created March 26, 2024 02:22
Show Gist options
  • Save morsk/407e0f9b45a536b81883dfd915418b56 to your computer and use it in GitHub Desktop.
Save morsk/407e0f9b45a536b81883dfd915418b56 to your computer and use it in GitHub Desktop.
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
bot_names[item_stack.name] = true
end
end
--[[ Loop on logistic networks. ]]
local surface_networks = game.player.force.logistic_networks[game.player.surface.name]
for _, network in pairs(surface_networks) do
--[[ Destroy bot entities. ]]
for _, bot in pairs(network.logistic_robots) do
count_bot(bot.name, 1)
bot.destroy()
end
--[[ Destroy bot items in roboports. ]]
for _, logi_cell in pairs(network.cells) do
local roboport = logi_cell.owner
local inv = roboport.get_inventory(defines.inventory.roboport_robot)
if inv then
for i = 1, #inv do
if inv[i].valid_for_read and bot_names[inv[i].name] then
count_bot(inv[i].name, inv[i].count)
inv[i].clear()
end
end
end
end
end
local print = game.player.force.print
if next(counts) == nil then
print("No bots to destroy.")
else
print("Destroyed bots:")
for name, count in pairs(counts) do
print(("%s: %d"):format(name, count))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment