How to make all enemies attack?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
AlyxDeLunar
Fast Inserter
Fast Inserter
Posts: 105
Joined: Mon Dec 22, 2014 7:32 pm
Contact:

How to make all enemies attack?

Post by AlyxDeLunar »

Hey folks, hopefully someone can help or point me in the right direction.

I'm trying to get all existing enemy units to attack. I don't care about where they attack, simply that they begin attacking the player or buildings. So far I'm trying this by creating a unit group and adding all units to the unit group and setting their behavior to autonomous, but I've either had errors using the functions or just outright crashing the game.

So far what I've got is this:
EDIT: Word of warning: DON'T DO THIS :lol: (Will likely crash the game or your computer)

Code: Select all

local surface = game.surfaces[1]

global.werebiterGroup = surface.create_unit_group({position={0, 0}}) -- Placeholder position, don't think it's important
global.werebiterGroup.set_autonomous()

local enemies = surface.find_enemy_units({0, 0}, 1000) -- Grab all nearby enemies, keep it simple
for _, enemy in pairs(enemies) do
     global.werebiterGroup.add_member(enemy)
end
Worth noting, this code currently resides in a function that runs every 60 game ticks, so it potentially runs a bit too often. I figured I'd optimize that later, but I don't know if it's causing a problem now.
Last edited by AlyxDeLunar on Thu Sep 08, 2016 7:02 am, edited 1 time in total.
Sometimes humorous, usually congenial. Always Alyx.

My Stuff:
Lunar's Factorio Mod Manager

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: How to make all enemies attack?

Post by aubergine18 »

You're going to be battling the Ai that controls the biters, as it will be issuing them orders too? So your mod tells them to attack, but a few ticks later the biter AI tells them to "go wander around by that tree over there"...

To get them to attack, could something be done with pollution - just find them and set tile pollution at their location, so they get mad and attack the player?

Here's some mods that affect biter behaviour that might be worth examining to see how they do it:

* https://mods.factorio.com/mods/Afforess/Misanthrope
* https://mods.factorio.com/mods/Supercheese/Swarm
* https://mods.factorio.com/mods/Klonan/Biter_Seasons
* https://mods.factorio.com/mods/Veden/Rampant
Last edited by aubergine18 on Wed Sep 07, 2016 11:04 pm, edited 1 time in total.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: How to make all enemies attack?

Post by Nexela »

try this from the command line.

/c for _, unit in pairs(game.player.surface.find_enemy_units(game.player.position, 10000)) do; unit.set_command({type=defines.command.attack, target=game.player.character}) end


on target="" you could do a reverse lookup to find the closest entity belonging to your force and have them attack that.

target=game.player.surface.find_entities_filtered{position=unit.position, force=game.player.force.name, limit=1}[1] --might need to expand to an area not sure exactly

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: How to make all enemies attack?

Post by Mooncat »

I need to bookmark this thread as I will need the same thing too in Creative Mode. :P

User avatar
AlyxDeLunar
Fast Inserter
Fast Inserter
Posts: 105
Joined: Mon Dec 22, 2014 7:32 pm
Contact:

Re: How to make all enemies attack?

Post by AlyxDeLunar »

aubergine18 wrote:You're going to be battling the Ai that controls the biters, as it will be issuing them orders too? So your mod tells them to attack, but a few ticks later the biter AI tells them to "go wander around by that tree over there"...

To get them to attack, could something be done with pollution - just find them and set tile pollution at their location, so they get mad and attack the player?
My thought was that the method set_autonomous() would make them attack polluted areas. That's what the docs say, though now I think it might not trigger an attack, just make it possible. I'll test it as soon as I get things to stop crashing.
Nexela wrote:try this from the command line.

/c for _, unit in pairs(game.player.surface.find_enemy_units(game.player.position, 10000)) do; unit.set_command({type=defines.command.attack, target=game.player.character}) end
Just tried this out. Word of warning: DON'T DO THIS :lol:
After locking up my computer and requiring a hard reboot, I now think my problem (and that section of code you gave) is one of two things:
- Looping through so many entities is simply too demanding, and should not be done
- Having such a high number for the radius is causing it to go out of generated chunks, causing a crash

I think the safest option is just to try another method entirely.
Sometimes humorous, usually congenial. Always Alyx.

My Stuff:
Lunar's Factorio Mod Manager

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: How to make all enemies attack?

Post by Mooncat »

I think your game didn't crash, it just takes time to find all enemies in such a big area. If some delay is allowed before the enemies start attacking, I would try separating the search into several ticks.

And how about LuaSurface::build_enemy_base?
Doc says "Send a group to build a new base.", but does the group "moves" to the position and build a base or just instantly spawns there?
Last edited by Mooncat on Thu Sep 08, 2016 4:09 pm, edited 1 time in total.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: How to make all enemies attack?

Post by Nexela »

Well I tested it and it ran fine :) granted I tested it on a new world. but I was promptly killed by hundreds of biters

User avatar
AlyxDeLunar
Fast Inserter
Fast Inserter
Posts: 105
Joined: Mon Dec 22, 2014 7:32 pm
Contact:

Re: How to make all enemies attack?

Post by AlyxDeLunar »

Nexela wrote:Well I tested it and it ran fine :) granted I tested it on a new world. but I was promptly killed by hundreds of biters
Hehe, that's what I was hoping would happen to me! I started in a new world too, but my computer isn't a real workhorse so maybe it just couldn't keep it (I did get impatient after a minute and just try to force everything closed).

Ah well, I'll give it a go when I have time to mod next. The planned mod will need to touch all entities on a semi-regular basis, so I should just figure out a more optimized way of doing it first.
Sometimes humorous, usually congenial. Always Alyx.

My Stuff:
Lunar's Factorio Mod Manager

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

Re: How to make all enemies attack?

Post by Adil »

Autonomous doesn't mean they'll go and attack. They can also go and settle somewhere, or if they're particularly distant from anything of interest, they might simply get garbage-collected.
Selecting all of them and setting their order to attack something in the middle of player's base is what I've done earlier in that judgement day mod for 0.11 (or was it 0.10).
Unless you implement a complex multitarget designation, like other AI mods do, this will yield disappointing results on a map of any notable size or complexity.
Really, I still believe that the dynamic expansion mod was the most elegant way of achieving the desired result.
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.

Post Reply

Return to “Modding help”