function c_doenemyai(target) { switch target.data.aitype { case AI.AFK: target.waiting = true; break; case AI.KILLER: var dude = c_findnearestunit(ARMY.US); repeat(target.data.mov.val) { var dist = c_tiledist(target.pos.x, target.pos.y, dude.pos.x, dude.pos.y); if dist.x+dist.y <= target.data.rng.val { log("started combat") c_generatecombatstack([target, dude]); break; } log(c_tiledist(target.pos.x, target.pos.y, dude.pos.x, dude.pos.y)); var guy = c_gettile(target.pos.x, target.pos.y); if target.pos.x < dude.pos.x { var guy = c_gettile(target.pos.x+1, target.pos.y); } else if target.pos.x > dude.pos.x { var guy = c_gettile(target.pos.x-1, target.pos.y); } if target.pos.y < dude.pos.y { var guy = c_gettile(target.pos.x, target.pos.y+1); } else if target.pos.y > dude.pos.y { var guy = c_gettile(target.pos.x, target.pos.y-1); } if guy != noone { if guy.passable && !c_containsunit(guy.x, guy.y) { c_moveunit(target, guy); } } c_loadmarkings(); } target.waiting = true; break; case AI.WAITER: target.waiting = true; break; case AI.ESCAPER: target.waiting = true; break; } c_loadmarkings(); } function c_findnearestunit(alignment) { var lowest = new vec2(99, 99); var lowestunit = noone; for (i=0; i guy.x + guy.y) { lowest = guy; lowestunit = global.units[alignment][i]; } } return lowestunit; }