Page 1 of 1

Find closest enemy? Friendly AI

Posted: Mon Aug 10, 2015 8:26 pm
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!

Re: Find closest enemy? Friendly AI

Posted: Mon Aug 10, 2015 9:08 pm
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 ;)!

Re: Find closest enemy? Friendly AI

Posted: Mon Aug 10, 2015 9:37 pm
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.

Re: Find closest enemy? Friendly AI

Posted: Mon Aug 10, 2015 11:42 pm
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!

Re: Find closest enemy? Friendly AI

Posted: Mon Aug 10, 2015 11:45 pm
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!

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 8:13 am
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.

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 9:27 am
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?

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 9:40 am
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.

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 10:42 am
by Adil
Or just let the player to set the attack target and let the unit_group AI handle the rest.

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 3:32 pm
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 :)

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 3:33 pm
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 :)

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 4:36 pm
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.

Re: Find closest enemy? Friendly AI

Posted: Tue Aug 11, 2015 11:06 pm
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.

Re: Find closest enemy? Friendly AI

Posted: Wed Aug 12, 2015 2:20 am
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.

Re: Find closest enemy? Friendly AI

Posted: Wed Aug 12, 2015 6:11 am
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.