Figuring Out The Source of Flee AI Failures In Istrolid
Hello Istrolid Community! LB-Internal here! (IGN: LB-Internal, Discord: @209711256909643776)
I've spent an entire day on understanding this issue that has been bugging me, and I believe many other players. Istrolid has many things which are fun about it, but one of my favourite parts of the game is the ability to create smart, full-fledged working AI's for your fleets. It adds a level of intelligence and depth to the game which makes it amazingly fun to play. However, it's not without it's faults, and chief among those is the little issue of ships sort of just sitting around when they're supposed to be fleeing. This mainly happens if you have a ship set to circle enemies and then tell it if it's shield is low to flee. More often than not, it just sort of sits there, or starts to run right at a bigger, badder enemy, even when you can clearly see an easy escape path. So why does this happen? Could we be using the AI function incorrectly? I decided to take a look under the hood to see what makes it tick. (All Comments in the code are MINE and were not found there originally. All credit goes to treeform for all code shown. If he wishes this article taken down, he need but ask!)
Right away, I headed straight to the AI portion of the code and found the goAway function. It's the function that gets called pretty much anywhere it says "Flee" in the interface. So what's it made up of? Check it out:
// The goAway Function for Istrolid
goAway = function(unit, thing, range) {
var dist;
if (!thing) {
return false;
}
dist = v2.distance(unit.pos, thing.pos);
if (dist > range) {
unit.stop();
return false;
}
/// This below is the part which determine where to flee to. It subtracts the "enemy to flee from"'s current position from your unit's position.
v2.sub(unit.pos, thing.pos, unit.goto);
v2.add(unit.pos, unit.goto, unit.goto);
unit.gotoMode = "goto";
return true;
};
Interesting...Doesn't seem to be too offensive, generally simple, and likely effective. I was a bit confused as to how such a clear approach to making a flee mechanism could fail so often, so I wondered how often it is run anyway? Well, a few hundred sections of code later and deep into the main engine of Istrolid, we can find this portion of code:
//// So how often does it run? Well, repeats over and over and over. But the way it's set up is to hook into ticks...
//// As can be seen here in the tick function:
Player.prototype.tick = function() {
if (sim.step % 16 === 0) {
if (this.gainsMoney && sim.gainsMoney) {
this.earnMoney(10);
}
this.apm = this.actions / (sim.step / 16 / 60);
}
///// AI is called here:
if (this.aiRules) {
sim.timeIt("ai:" + this.name, (function(_this) {
return function() {
return doPlayerAIRules(_this);
};
})(this));
}
return this.wave();
};
//// This means that it runs the AI from SCRATCH every tick.
//// Why does this matter? Well, our go away function "goes away" from a specific enemy to flee from...
//// And how does it pick that enemy?
//// Well, going back to the AI portion, I check one of the places it's called to see how the value for 'thing' (which is the enemy) is determined
/// Case for "When shields down to #%, flee" in the "doUnitRules" switch
case "When Shields down to #%, flee":
if (unit.shield / unit.maxShield < rule[1] / 100) {
///// This below is the bit we want. It picks the enemy using closest UNIT which is NOT on our team!
enemy = closest(unit.pos, (function(t) {
return t.unit && t.side !== unit.side;
}));
if (enemy) {
goAway(unit, enemy, enemy.weaponRange * 1.5);
return true;
}
}
break;
//// That means it checks for the closest unit who is not a friend EVERY TICK
//// This value can change every single tick as a battle is being waged
//// HENCE, the issue is it picks a unit to flee from, does the math, starts to flee, and then it refreshes anddddd
//// finds another closer enemy, and prepares to run! R.I.P Thus it never runs!
//// Let's see where this "goAway" function is called...
/// Case for "Flee" in the "attackMoves" switch
if (type === "Flee") {
return goAway(unit, enemy, enemy.weaponRange + enemy.radius + enemy.maxSpeed * 16);
}
/// Case for "Stay at range" in the "attackMoves" switch
if (type === "Stay at range") {
return goAway(unit, enemy, enemy.weaponRange + enemy.radius + unit.radius);
}
/// Case for "When shields down to #%, flee" in the "doUnitRules" switch
case "When Shields down to #%, flee":
if (unit.shield / unit.maxShield < rule[1] / 100) {
enemy = closest(unit.pos, (function(t) {
return t.unit && t.side !== unit.side;
}));
if (enemy) {
goAway(unit, enemy, enemy.weaponRange * 1.5);
return true;
}
}
break;
/// Case for "Stayaway in #m range from slot # units" in the "doUnitRules" switch
case "Stayaway in #m range from slot # units":
friendly = closest(unit.pos, (function(t) {
return t.unit && t.number === (parseInt(rule[2]) - 1) && t.id !== unit.id && t.side === unit.side;
}));
if (friendly && v2.distance(unit.pos, friendly.pos) < rule[1]) {
goAway(unit, friendly);
return true;
}
break;
//// But could there be a better alternative...Well, people seem to think enemy avoidance is pretty good
//// Lets check it out:
// The "avoidEnemies" Function for Istrolid
ais.avoidEnemies = function(unit, dps) {
var dist, doWhat, enemy, minDist, minEnemy, ref, stayAwayRange;
minDist = 9000000;
minEnemy = null;
doWhat = null;
ref = sim.things;
for (_ in ref) {
enemy = ref[_];
///// Basically, here it says to check how close you are to any enemy if their DPS is 16 times yours
if (enemy.unit && enemy.side !== unit.side && enemy.weaponDPS * 16 > dps) {
dist = v2.distance(unit.pos, enemy.pos);
///// If your unit is TOO close (which it always is since 'minDist' = 9000000 meters), then figure out how far away to run using weapon range and their max speed
if (dist < minDist) {
stayAwayRange = enemy.weaponRange + enemy.radius + unit.radius + enemy.maxSpeed * 16;
///// If you're too close, then do WHAT? "Flee"...Where have we seen this before...Oh yeah...
if (dist < stayAwayRange) {
minDist = dist;
minEnemy = enemy;
doWhat = "Flee";
} else if (dist < stayAwayRange * 1.10) {
minDist = dist;
minEnemy = enemy;
doWhat = "Stop";
}
}
}
}
///// Guess what "Flee" means...It's our good friend 'goAway' again!
if (doWhat === "Flee") {
goAway(unit, minEnemy, stayAwayRange);
return true;
}
if (doWhat === "Stop") {
unit.stop();
return true;
}
};
So basically, goAway CAN work, but it needs to only be applied after some form of filter. Currently it gets applied directly, which doesn't work out too well in crowded situations.When making AI, it's best to use "Stay at Range" or another mechanic for short range ships rather than flee for the time being.
Thanks for your time and hope you learned something everyone!
Posted Dec 15, 2016, 2:17 AM UTC
Available in comments for any more info people want
One of the bigger issues with flee command, is that Ai only has 3ms to think per step... Some AIs are so overloaded with commands that it does not have time to do updates for all units. Some units get skipped. The more commands*units have you the more units get skipped.
I was thinking of maybe making it more obvious by highlighting units red or some thing when they are skipped...
I would like the return of the ai grid option I loved it. But maybe it was too hard to keep functional in this case I understand its deletion.
Was the ai grid the thing that showed which action the selected ai was doing?
It did more it did indicate a grid of dps danger areas and other stuff of this kind.
I was wondering...why would ticks have to be a set time? Why doesn't it calculate everything it has to each tick before going to the next? Most games do that. When it can't handle standard tick rate, it simply slows down rather than dropping data. Is this a possibility? Could it at least be tried to see the side-effects? Is it even viable?
Thats what it does for the rest of the simulations.
But the AI is special because it is user input. Trolls will just troll by spamming the slowest command and slowing down the game for every one. Maybe even forcing a win if the other players get bored and leave.
How do I rob a bank without getting caught?
You rob a bank the day before it closes for bankruptcy. They'll still have money on hand because all debts tend to be digital. People won't stay late, since there's nothing to work towards. Finally, they won't try very hard to get the money back since they already filed for bankruptcy and what an extra million or so in debt when you don't have to pay any of it anyway under Chapter 11. Hope this helps!
But banks are too big to fail?
Not if you make one up yourself.
When I was 5 years old, I made a bank with the other children of my neighbourhood.
A parental cash bust broke the 2-dollar firm apart.