COB Extensions - Switching unit weapons based on distance to attacker

New extension called ATTACKER_ID with a self explanatory name, allowed me to get ID of a unit that is attacking caller unit. Since knowing ID means - being able to access all info about the unit, i can calculate distance between attacked and attacker unit. Based on that i've made a simple weapon switcher (primary weapon: laser / flame thrower).

Script:

WeaponAutoSwitcher()
{
    var attackerid, distance
    while (TRUE)
    {
        attackerid = get ATTACKER_ID;
        if (attackerid != 0)
        {
            distance = get DISTANCE(get MY_ID, attackerid);
            if (distance != 0)
            {
                if (distance >= 150)
                {
                    set WEAPON_PRIMARY to 91; //laser    
                }
                if (distance < 150)
                {
                    set WEAPON_PRIMARY to 1; //flame thrower
                }
            }
        }
        sleep rand(500, 1000);
    }
}