How to remove enemies via console?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
mophydeen
Filter Inserter
Filter Inserter
Posts: 529
Joined: Sun Nov 22, 2015 5:02 pm
Contact:

Re: How to remove enemies via console?

Post by mophydeen »

sarcolopter wrote:
mophydeen wrote:
sarcolopter wrote:Sorry for necroing. I used this, and it appears to have removed all enemies from the map, instead of ~50%. Is there any way to respawn them?

Go back one save ?
That would defeat the purpose, since I'd be back right where I started.
Go back and retry command
Which command did you enter?

sarcolopter
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Sun Jul 26, 2015 5:08 pm
Contact:

Re: How to remove enemies via console?

Post by sarcolopter »

mophydeen wrote:
sarcolopter wrote:
mophydeen wrote:
sarcolopter wrote:Sorry for necroing. I used this, and it appears to have removed all enemies from the map, instead of ~50%. Is there any way to respawn them?

Go back one save ?
That would defeat the purpose, since I'd be back right where I started.
Go back and retry command
Which command did you enter?

Code: Select all

   local c = 0, 0
    local s = game.local_player.surface
    for coord in s.get_chunks() do
        local X,Y = coord.x, coord.y
        find = {"biter-spawner", "spitter-spawner", "small-worm-turret", "medium-worm-turret", "big-worm-turret"}
        if s.is_chunk_generated{X,Y} then
            local area = {{X*32, Y*32}, {X*32 + 32, Y*32 + 32}}
            for _,enemy in pairs(find) do
                for _, entity in ipairs(s.find_entities_filtered{area = area, name = enemy}) do
                    c = c+1
                    entity.destroy()
                end
            end
        end
    end
    game.local_player.print("Terminated "..c.." enemy structures")
I don't see how repeating an entity.destroy() command would spawn additional entities. Is there an entity.create() command or similar?

Koub
Global Moderator
Global Moderator
Posts: 7217
Joined: Fri May 30, 2014 8:54 am
Contact:

Re: How to remove enemies via console?

Post by Koub »

Well next time, if you want to remove, say, half of the spawners, in the innermost loop, add a test like :

Code: Select all

                for _, entity in ipairs(s.find_entities_filtered{area = area, name = enemy}) do
                  if math.random(0,1) > 0.5 then
                     c = c+1
                     entity.destroy()
                     end
                end
What you did there is basically remove all spawners one by one.

Well I'm not 100% sure of the syntax, but the idea is there :)
Koub - Please consider English is not my native language.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: How to remove enemies via console?

Post by Klonan »

Here is a script that will kill all worms, biters, spitters and spawners everywhere on the map:

Code: Select all

/c local surface = game.local_player.surface
for c in surface.get_chunks() do
for key, entity in pairs(surface.find_entities_filtered({area={{c.x * 32, c.y * 32}, {c.x * 32 + 32, c.y * 32 + 32}}, force= "enemy"})) do
entity.destroy()
end
end
This is the same, but will only kill half of them:

Code: Select all

/c local surface = game.local_player.surface
for c in surface.get_chunks() do
for key, entity in pairs(surface.find_entities_filtered({area={{c.x * 32, c.y * 32}, {c.x * 32 + 32, c.y * 32 + 32}}, force= "enemy"})) do
if key % 2 == 0 then
entity.destroy() 
end
end
end

sarcolopter
Long Handed Inserter
Long Handed Inserter
Posts: 58
Joined: Sun Jul 26, 2015 5:08 pm
Contact:

Re: How to remove enemies via console?

Post by sarcolopter »

well sheeeeeeit. guess I'll figure out how to mod in a crafting recipe for artifacts

Iggmynir
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Sep 11, 2016 11:54 am
Contact:

Re: How to remove enemies via console?

Post by Iggmynir »

Klonan wrote:Here is a script that will kill all worms, biters, spitters and spawners everywhere on the map:

Code: Select all

/c local surface = game.local_player.surface
for c in surface.get_chunks() do
for key, entity in pairs(surface.find_entities_filtered({area={{c.x * 32, c.y * 32}, {c.x * 32 + 32, c.y * 32 + 32}}, force= "enemy"})) do
entity.destroy()
end
end
Hello Klonan, is this command still supposed to work? I used it in a map of mine a few months ago. After one of the recent experimental updates enemys started showing up at my borders again. When I tried to use the command to wreck all of thier stuff I got an error message:

Code: Select all

Cannot execute command. Error: LuaGameScript doesn't contain key local_player.stack traceback: [string "local surface = game.local_player.surface fo.."]:1: in main chunk

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: How to remove enemies via console?

Post by Adil »

These days it's `player` instead of `local_player`.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Iggmynir
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Sep 11, 2016 11:54 am
Contact:

Re: How to remove enemies via console?

Post by Iggmynir »

Adil wrote:These days it's `player` instead of `local_player`.
Thank you :)

Post Reply

Return to “Gameplay Help”