Find closest enemy? Friendly AI

Place to get help with not working mods / modding interface.
User avatar
darkshadow1809
Filter Inserter
Filter Inserter
Posts: 306
Joined: Thu Jan 15, 2015 10:13 pm
Contact:

Find closest enemy? Friendly AI

Post by darkshadow1809 »

Hi there!

I've been working on my mod Evolution https://forums.factorio.com/forum/vie ... 93&t=10256

And lately ive been running into trouble with my AI not being able to reach the opponent base and my biters just clump up and stay there until they get attacked. What I would like for them to do is actually move out and seek out the enemy AI however.. How would i go about doing this into the coding? I have a slight idea but since my lua isn't to great I am not sure how to implement this..

Basically what I would like to happen is > I put down a hive > Aliens (friendly) start spawning > Aliens group up > Aliens attack the enemy no matter the distance solong it is in range of ofcourse the map.
Without creating a massive lagspike / lag overall. Since changing the aggro_rad was a very bad idea. it created a huge amount of lag

https://forums.factorio.com/wiki/inde ... ua/Surface Ive found something here
More specific this section: http://prntscr.com/8343mp But still I've no clue how to implement it into the biter control so it only affects my biters and not that of the enemy AI.

If someone could help i'd be very happy :)

The control.lua here > http://pastebin.com/v08pCwJx
Has all definitions of the friendly AI.

My question is if someone could come up with some code or Idea to how this might be able to be implemented and hopefully that my biters do attack the actual opponents? instead of clumping up and standing there until the enemy comes close because of their aggro_rad




Thanks in advance!
ShadowsModpackDevelopment

jorgenRe
Filter Inserter
Filter Inserter
Posts: 535
Joined: Wed Apr 09, 2014 3:32 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by jorgenRe »

The option you might choose to have them attack like if you dont find a better option. THen i guess the hacky thing you could do would be to use the find_entities_filtered and check for force enemy or something.
And then assign the biters the first target that comes up in that find entities thing ;)!
Then when a target is assigned you can get the biters to go to that target, but now the hackiest thing is that you may have to use the function to set health and lower it by using lua :(
Atleast this is on your lowest part of the list if there comes any better option ;)!
Logo
Noticed the told change in FFF #111 so il continue to use my signature ^_^
Thanks for listening to our suggestions, devs :D!
I would jump of joy if we could specify which tiles spawned in a surfaces

User avatar
vampiricdust
Filter Inserter
Filter Inserter
Posts: 317
Joined: Wed Jan 14, 2015 1:31 am
Contact:

Re: Find closest enemy? Friendly AI

Post by vampiricdust »

I don't know about finding targets, but you could try making them move around? Have the group when ready move away from the nest a fair distance. This way they will "patrol" and run into the enemy ones. I'd personally try to use the pollution level of a chunk as a indicator for where to avoid, moving from high pollution to equal or less.

User avatar
darkshadow1809
Filter Inserter
Filter Inserter
Posts: 306
Joined: Thu Jan 15, 2015 10:13 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by darkshadow1809 »

jorgenRe wrote:The option you might choose to have them attack like if you dont find a better option. THen i guess the hacky thing you could do would be to use the find_entities_filtered and check for force enemy or something.
And then assign the biters the first target that comes up in that find entities thing ;)!
Then when a target is assigned you can get the biters to go to that target, but now the hackiest thing is that you may have to use the function to set health and lower it by using lua :(
Atleast this is on your lowest part of the list if there comes any better option ;)!
True this causes issues with other mods though. And could cause trouble with lag. :( ill consider it though!
ShadowsModpackDevelopment

User avatar
darkshadow1809
Filter Inserter
Filter Inserter
Posts: 306
Joined: Thu Jan 15, 2015 10:13 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by darkshadow1809 »

vampiricdust wrote:I don't know about finding targets, but you could try making them move around? Have the group when ready move away from the nest a fair distance. This way they will "patrol" and run into the enemy ones. I'd personally try to use the pollution level of a chunk as a indicator for where to avoid, moving from high pollution to equal or less.

Mmh... Interesting. very.. yes :d that could work. However how would I go about doing this? Since the friendly AI listens to the original code of factorio's enemy biters aswell. Could you provide me with some code examples? would be great!
ShadowsModpackDevelopment

User avatar
vampiricdust
Filter Inserter
Filter Inserter
Posts: 317
Joined: Wed Jan 14, 2015 1:31 am
Contact:

Re: Find closest enemy? Friendly AI

Post by vampiricdust »

Ok, so I was digging around in the demo level's 2 control.lua to see if I can find how they trigger attack only after you have a gun and they have the following code:

Code: Select all

{
      condition = function() return game.player.character.get_inventory(defines.inventory.player_ammo).get_item_count("basic-bullet-magazine") > 0 end,
      action = function()
        game.player.set_goal_description("")
        game.player.character.clear_gui_arrow()
        game.player.surface.set_multi_command({type=defines.command.attack,
                                            target=game.player.character,
                                            distraction=defines.distraction.none},
                                            2)
      end
    },
It seems game.player.set_multi_command might be useful or the trick. I do not have time tonight to experiment with your mod & see if it'll work for your biters, but it might be possible to command the biters from there.

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

Re: Find closest enemy? Friendly AI

Post by Adil »

Nah, set_multi_command is hardcoded for ordering around enemy biters.
For having your own units, creating unit_groups and ordering them around is pretty much the only way. (Or individual orders.) Since enemy bases aren't spawned that often, it should be trivial to implement sending attacks there. As for patrols, maybe compound commands would help here.

By the way the code on pastebin looks like functional already, what's the question?
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.

User avatar
vampiricdust
Filter Inserter
Filter Inserter
Posts: 317
Joined: Wed Jan 14, 2015 1:31 am
Contact:

Re: Find closest enemy? Friendly AI

Post by vampiricdust »

Adil wrote:Nah, set_multi_command is hardcoded for ordering around enemy biters.
For having your own units, creating unit_groups and ordering them around is pretty much the only way. (Or individual orders.) Since enemy bases aren't spawned that often, it should be trivial to implement sending attacks there. As for patrols, maybe compound commands would help here.

By the way the code on pastebin looks like functional already, what's the question?
Yeah, that was my thought. I forgot to add the unit_groups link to it. Once I saw it in that command list, I figured it would be the only real choice.

So my idea basically would be use the chunk generation to get a target list. You can use these as basically movement nodes. Program your AI to move to nodes based on whatever until they unit group is dead. It would be cool if they you added another research for like Biter Training to make us better AI for the biters. Otherwise, you could use the nest as a focal point and player entities as vectors having them path away from the closest player created entity. This would cool as you could "point" nests in certain directions and just head out in a zig-zag search pattern. Another pattern to have would be a spiral where the groups run laps around the rest in increasing intervals.

One of the biggest problems will be going around obstacles like water or the player's base. As I think on it so far I am more in favor of using chunk generation as a way to add in movement nodes as they ensure that there is land there at least. Would have to be mindful that they will get stuck.

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

Re: Find closest enemy? Friendly AI

Post by Adil »

Or just let the player to set the attack target and let the unit_group AI handle the rest.
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.

User avatar
darkshadow1809
Filter Inserter
Filter Inserter
Posts: 306
Joined: Thu Jan 15, 2015 10:13 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by darkshadow1809 »

Adil wrote:Nah, set_multi_command is hardcoded for ordering around enemy biters.
For having your own units, creating unit_groups and ordering them around is pretty much the only way. (Or individual orders.) Since enemy bases aren't spawned that often, it should be trivial to implement sending attacks there. As for patrols, maybe compound commands would help here.

By the way the code on pastebin looks like functional already, what's the question?
Not so much a question and yes it does work to a certain extent. But what happens 9/10th of the time is when theres no pollution your biters will "patrol" in the middle of your base and clump up .. basically stand there and never go out to kill enemy biters. Which is quite a pain :P So thats why i want them to be attacking 24/7 even when theres no pollution factor playing :)
ShadowsModpackDevelopment

User avatar
darkshadow1809
Filter Inserter
Filter Inserter
Posts: 306
Joined: Thu Jan 15, 2015 10:13 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by darkshadow1809 »

vampiricdust wrote:
Adil wrote:Nah, set_multi_command is hardcoded for ordering around enemy biters.
For having your own units, creating unit_groups and ordering them around is pretty much the only way. (Or individual orders.) Since enemy bases aren't spawned that often, it should be trivial to implement sending attacks there. As for patrols, maybe compound commands would help here.

By the way the code on pastebin looks like functional already, what's the question?
Yeah, that was my thought. I forgot to add the unit_groups link to it. Once I saw it in that command list, I figured it would be the only real choice.

So my idea basically would be use the chunk generation to get a target list. You can use these as basically movement nodes. Program your AI to move to nodes based on whatever until they unit group is dead. It would be cool if they you added another research for like Biter Training to make us better AI for the biters. Otherwise, you could use the nest as a focal point and player entities as vectors having them path away from the closest player created entity. This would cool as you could "point" nests in certain directions and just head out in a zig-zag search pattern. Another pattern to have would be a spiral where the groups run laps around the rest in increasing intervals.

One of the biggest problems will be going around obstacles like water or the player's base. As I think on it so far I am more in favor of using chunk generation as a way to add in movement nodes as they ensure that there is land there at least. Would have to be mindful that they will get stuck.
I am quite new at lua o.o... This seems complicated to me :P Is there like a tutorial or some code to atleast see how i make this? Or maybe help me through it :P It all sounds very promising what your telling :)
ShadowsModpackDevelopment

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

Re: Find closest enemy? Friendly AI

Post by Adil »

darkshadow1809 wrote: Not so much a question and yes it does work to a certain extent. But what happens 9/10th of the time is when theres no pollution your biters will "patrol" in the middle of your base and clump up .. basically stand there and never go out to kill enemy biters. Which is quite a pain :P So thats why i want them to be attacking 24/7 even when theres no pollution factor playing :)
To be honest, I've only briefly gazed over the commands at first, and everything related to making units behave seemed in place.
Now after more careful reading, I don't understand what are you expecting. The code there once per 1.333 second looks for enemies in range 40 (twice the range of laser turret) around the player , performs some obscure manipulation with player items, searches friendly biters in range 55 (no more than 20 of them regardless of enemy numbers) and then either send them to the area 10 tiles around the player, or issues the attack order if there are enemy spawners there. What is that there to expect about pollution I don't even.
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.

User avatar
darkshadow1809
Filter Inserter
Filter Inserter
Posts: 306
Joined: Thu Jan 15, 2015 10:13 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by darkshadow1809 »

Adil wrote:
darkshadow1809 wrote: Not so much a question and yes it does work to a certain extent. But what happens 9/10th of the time is when theres no pollution your biters will "patrol" in the middle of your base and clump up .. basically stand there and never go out to kill enemy biters. Which is quite a pain :P So thats why i want them to be attacking 24/7 even when theres no pollution factor playing :)
To be honest, I've only briefly gazed over the commands at first, and everything related to making units behave seemed in place.
Now after more careful reading, I don't understand what are you expecting. The code there once per 1.333 second looks for enemies in range 40 (twice the range of laser turret) around the player , performs some obscure manipulation with player items, searches friendly biters in range 55 (no more than 20 of them regardless of enemy numbers) and then either send them to the area 10 tiles around the player, or issues the attack order if there are enemy spawners there. What is that there to expect about pollution I don't even.
Basically looking in the control.lua only wont provide you with that answer.

In the biter.lua it states that atleast 200 pollution in a chunk is needed to launch an attack. or well "join the attack" so pollution does play a role. no pollution = no attack. On that chunk that is. unless the enemy is very very close.

putting this to 0 however creates massive lag. since they recieve a lot of commands in all chunks at once.
ShadowsModpackDevelopment

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

Re: Find closest enemy? Friendly AI

Post by Adil »

So does that biter.lua also redefine player.position or something? Since the order given is just to follow player around.
And yes, pathing of large hordes is bad, and most of the time game will just stop moving half of them since it can't find the path for them quickly enough.
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.

yeganer
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Mon Jul 06, 2015 12:07 pm
Contact:

Re: Find closest enemy? Friendly AI

Post by yeganer »

As i already told you, i think creating your own kind of pollution would be the best.

You define it as per chunk. you update it once every 60 ticks or whatever. your biters are gathered as unit_groups. each group checks the level of your pollution in the current chunk and adjacent chunks, if the level is higher there, move them there.

I don't know if there is find_idle_unitgroup command or something similiar, if there is you can send the unitgroup to the next chunk and forget about them. the only thing you have to do is check each tick if there are idle groups and then send them to the next chunk.

that way your biters will always run towards the maximum of the pollution which comes from enemy spawners. If that is done properly you won't have any lag.

Post Reply

Return to “Modding help”