Improve the ability to manipulate unit AI
Posted: Thu Sep 17, 2015 1:16 am
I've been attempting to write a mod that alters the way biters interact with the players (attacking in waves, rather than the small packs, more advanced grouping behaviors) and manipulating larger groups of biters (100+), and the factorio API is too coarse for this sort of mechanics. I've found the Surface.set_multi_command to have serious limitations, it appears to have a maximum range of around 200 (requesting an attack on a location farther away than that has no effect, and silently fails w/o indicating any sort of problem). When I did use set_multi_command in shorter distances, where it did work, it only seemed to cause a small number of biters to follow the command, biters already in a unit group (swarming to build a new base or attack pollution) ignored commands. Also, I did discover set_multi_command has a 3rd optional argument (command, number, and force), which was not documented in the wiki - however this is not particularly useful.
Giving commands to individual units seems to work without the same distance limitation, but it is a lot slower in terms of performance. More problematic still, when the command finishes (like a go-to-location command), the biters immediately return to whatever they were doing before, which makes advanced groupings hard to maintain, without repeatedly giving them the command each tick - a massive performance drain. I've fiddled around with creating unit groups in the hopes that this might be a solution, but I haven't seen positive results. Creating a unit group from the nauvis surface, and using add_member seems to have no effect on existing biters. Giving biters the command to group with the new unit group also does not seem to work. I would suspect that biters have some sort of pre-existing unit group and so the game is rejecting adding them to a second group, but there is no way to get the unit group a biter is currently a member of, nor remove them from it.
I think the easiest improvements here would be the following:
Giving commands to individual units seems to work without the same distance limitation, but it is a lot slower in terms of performance. More problematic still, when the command finishes (like a go-to-location command), the biters immediately return to whatever they were doing before, which makes advanced groupings hard to maintain, without repeatedly giving them the command each tick - a massive performance drain. I've fiddled around with creating unit groups in the hopes that this might be a solution, but I haven't seen positive results. Creating a unit group from the nauvis surface, and using add_member seems to have no effect on existing biters. Giving biters the command to group with the new unit group also does not seem to work. I would suspect that biters have some sort of pre-existing unit group and so the game is rejecting adding them to a second group, but there is no way to get the unit group a biter is currently a member of, nor remove them from it.
I think the easiest improvements here would be the following:
- Give set_multi_command an additional optional parameter to specify the maximum range to search for units to fulfill the request, and allow set_multi_command to interrupt existing unit grouping behavior
- Expose a field to access a biters current unit group
- Fix adding members to unit groups (or clarify the documentation on how it is supposed to work)
- Add a method to disable/enable the standard biter AI behavior (wandering, attacking pollution, etc) so modders can have complete control if desired
- Add events for when a unit is given a new command, a biter base is built, etc. It would be nice to script modifications to this behavior, or move the location of the biter base, override the command, etc