MV YEP Target Core: Targeting 3 different foes once if possible

I've been doing a bit of searching and I don't think I've found a method to achieve exactly what I want, I can just get pretty close.

I want to use a YEP Target Core <Custom Target Eval> to achieve the following in a skill:

Strike three times, at different targets if possible.

So, some use cases:

- If >= 3 Troop members are alive, target up to three to hit ONCE each.

- If 3 Troop members are alive, hit all three ONCE each.

- If 2 Troop members are alive, hit two ONCE each, and then select one of them as a random target for the third hit.

- If 1 Troop member is alive, this target receives all three hits.

The closest I can get to this, is a little script from Yanfly for Chain Lightning:


Code:
<Custom Target Eval>
// Adds the selected target to the target list.
targets.push(target);
// Grab the group of alive foes as candidates.
var members = foes.aliveMembers();
// Remove the target from the group of candidates.
members.splice(members.indexOf(target), 1);
// This is the number of extra targets to select with Chain Lightning.
var extraTargets = 2;
// Loop the extra targets.
while (extraTargets--) {
  // Grab a random foe from the alive foes.
  var member = members[Math.floor(Math.random() * members.length)];
  // Check to see if the member exists.
  if (member) {
    // Add the member to the group of targets.
    targets.push(member);
    // Remove the member as a viable candidate.
    members.splice(members.indexOf(member), 1);
  }
}
</Custom Target Eval>

^ This one will hit up to 2 other targets, but will not hit the same target more than once if there are less than three targets.

Just to reiterate - I need the skill to hit different targets if it can, but if it cannot, it will strike a previous target (chosen at random) until it has completed 3 hits.

Thanks!
Strike three times, at different targets if possible.
Code:
<Custom Target Eval>
// I presume your initial selection scope is All Enemies, so empty that out
targets=[];

while (targets.length<3)
{
// Get a random enemy
    var newTarget=$gameTroop.aliveMembers()[Math.randomInt($gameTroop.aliveMembers().length)];

// The enemy is not already chosen, or you've already chosen each enemy once
    if (!targets.includes(newTarget) || $gameTroop.aliveMembers().length<=targets.length)
        targets.push(newTarget);
}
</Custom Target Eval>
Tested in all use cases - working perfectly. Thank you @ATT_Turan !

Original thread: https://forums.rpgmakerweb.com/threads/yep-target-core-targeting-3-different-foes-once-if-possible.150489/