Self destructing enemy based on condition?

In battle, how can I make an enemy that destroys itself when certain conditions are met?

Example:
There is an invincible Turret enemy, with other soldier enemies. Once you kill all the soldiers, the Turret self destructs and you win the battle.


Now, I am sure the above can be done easily with a custom event for one troop. What I am looking for is a way to say, put a Turret enemy in any troop, and it will know to self destruct when all living/non-robotic enemies have been destroyed.
Your profile says you use MV, but you're posting in MZ support, so...I'll answer for MZ.

On state 1, put:
Code:
<JS On Add State>
if ($gameTroop.aliveMembers().filter(enemy =>$dataEnemies[enemy.enemyId()].meta.living).length==0)
{
    $gameTroop.aliveMembers().forEach(enemy => 
    {
        if ($dataEnemies[enemy.enemyId()].meta.turret) 
        {
            $gameTemp.requestAnimation([enemy], X);
            enemy.gainHp(-enemy.hp);
            enemy.performCollapse();
        }
    }
}
</JS On Add State>

You'll need to mark your enemies with the <living> and <turret> notetag as appropriate. Replace X with the ID of the animation of your choice, if you want one to play for the turret blowing up or whatever.

This requires VisuStella's Skills & States and Battle Core.
Another way would be to use base troop events (plugin) - those events are automatically processed on all troops.

Original thread: https://forums.rpgmakerweb.com/threads/self-destructing-enemy-based-on-condition.160922/