MV Compatibility - Multicast targeting both an enemy and an ally

Multicast plugin I am using ( Type 1 - Select only one skill, casts it multiple times, each with its own target selection.)
Target any plugin I am using
Both plugin creators seem to be inactive and unavailable for questions.

I have a polymorph ability in my game that is intended to be used on either an enemy or an ally, should the player choose. As a separate mechanic, double casting is also possible for the spell.

Both plugins separately work as intended with this skill, but when the two are used at the same time, no matter which targets are selected the spell will initially be cast on the actor at index 1, and then the target that was selected second like normal. (as long as the caster wasn't actor 1, who will not perform the second cast due to being polymorphed)

I recreated the issue in a brand new file with only unaltered* copies of the two relevant plugins installed, and it happens no matter which plugin is placed higher on the list.

It's possible there exists a combination of plugins that are compatible, but dingk's is the only multicast plugin I've found that has the one ability/multiple targets function. I know yanfly has a paid target core, but as it's a personal project, I'm avoiding paid resources. I also found some plugins that can toggle between enemy/ally selection with a keyboard key, but given the unintuitiveness of that working with a gamepad or mouse controls, I'd rather just cut that functionality from the spell and use a different workaround.

*In my main project, I made a couple edits to Multicast because of how skill cost works in my game
Gave it a look but couldn't figure what's causing it. But it was easy to figure that something is changing the target index (EDIT: or the anyWindow index or whatever) to 0 in that one method for all but the last action (the onEnemyOk() method). I thought it could be it reading index 0 from the enemyWindow, because Hime plugin is using anyWindow, but that didn't seem to be the case).

But I think you can patch it like this (copy paste to notepad, save as .js file and add as a plugin below the other plugins). This saves the target index and uses the saved one.

Scene_Battle.prototype.onAnyTargetOk = function() {

var action = BattleManager.inputtingAction();
this._anyTargetWindow.hide();
const targetId = this._anyTargetWindow.index();
this.onEnemyOk();
action.setTarget(targetId);
}

EDIT: Focusing my thoughts and putting it more clearly, so I think the problem is that something in the chain which that onEnemyOk method calls causes the anyTargetWindow index to reset to 0 (you can see it jumping to index 0 after selecting first) for all but the last selection. And that's why when the original code sets the target with the window index after that has happened, it causes them to attack target 0. But yeah, the code I posted above should fixe it, seemed to work for me at least.
Gave it a look but couldn't figure what's causing it. But it was easy to figure that something is changing the target index (EDIT: or the anyWindow index or whatever) to 0 in that one method for all but the last action (the onEnemyOk() method). I thought it could be it reading index 0 from the enemyWindow, because Hime plugin is using anyWindow, but that didn't seem to be the case).

But I think you can patch it like this (copy paste to notepad, save as .js file and add as a plugin below the other plugins). This saves the target index and uses the saved one.

Scene_Battle.prototype.onAnyTargetOk = function() {

var action = BattleManager.inputtingAction();
this._anyTargetWindow.hide();
const targetId = this._anyTargetWindow.index();
this.onEnemyOk();
action.setTarget(targetId);
}

EDIT: Focusing my thoughts and putting it more clearly, so I think the problem is that something in the chain which that onEnemyOk method calls causes the anyTargetWindow index to reset to 0 (you can see it jumping to index 0 after selecting first) for all but the last selection. And that's why when the original code sets the target with the window index after that has happened, it causes them to attack target 0. But yeah, the code I posted above should fixe it, seemed to work for me at least.

Oh wow, thanks again. I have been struggling to fully implement this ability for over a month, and that takes care of the last of it.

On a minor appearance note, is it possible to have the enemies listed above allies in the target selection window? I haven't edited code for drawing windows before, and don't know how much I can move before it starts breaking.
Cool.

For your extra question, I didn't test it, but I think change the below method:

Window_BattleAll.prototype.refresh = function() {
this._enemies = []
var battlers = BattleManager.allBattleMembers();
for (var i = 0; i < battlers.length; i++) {
var battler = battlers[i];
if (battler.isAlive()) {
this._enemies.push(battler);
}
}

Window_Selectable.prototype.refresh.call(this);
};

There it uses
BattleManager.allBattleMembers()
which gives like
return $gameParty.members().concat($gameTroop.members());
So I think just replace it with:
var battlers = $gameTroop.members().concat($gameParty.members());
Cool.

For your extra question, I didn't test it, but I think change the below method:

Window_BattleAll.prototype.refresh = function() {
this._enemies = []
var battlers = BattleManager.allBattleMembers();
for (var i = 0; i < battlers.length; i++) {
var battler = battlers[i];
if (battler.isAlive()) {
this._enemies.push(battler);
}
}

Window_Selectable.prototype.refresh.call(this);
};

There it uses
BattleManager.allBattleMembers()
which gives like
return $gameParty.members().concat($gameTroop.members());
So I think just replace it with:
var battlers = $gameTroop.members().concat($gameParty.members());
This was close! The method right above it,Game_Action.prototype.targetsForAny also calls BattleManager.allBattleMembers(), so that needs to be replaced too. I'm just testing whether editing the function itself or patching in the alternative in those two places works better, I'm guessing the latter to make sure it doesn't interfere with other code that might be using BattleManager.allBattleMembers().

Edit: The targets get mixed up when there are dead troop or party members, this seems to be an issue with the original Target Any Scope plugin, as it happens if Multicast and any adjustments are disabled as well.
e.g. Bat A is killed. Bat C is selected as target. Spell is cast on Bat B.
Yeah, the original plugin might have an oversight there.

Didn't check it too closely, but I think the problem comes from the fact that is uses:
action.setTarget(this._anyTargetWindow.index());
That window only had the alive battlers right? So it's setting different indexes depending on whose dead.
But then when making the targets it uses:
var battlers = BattleManager.allBattleMembers();
var battler = battlers[this._targetIndex];
var unit = battler.friendsUnit();
targets.push(unit.smoothTarget(battler.index()));

I assume it works if you make the var battlers array only have alive members from both party and troop, but don't really have the energy to think LOL.
Finally came back to this issue, I think I got there by adding this function:
Code:
  Scene_Battle.prototype.aliveGuys = function() {
    var targets = [];
    var battlers = $gameTroop.members().concat($gameParty.members());
    for (var i = 0; i < battlers.length; i++) {
      var battler = battlers[i];
      if (battler.isAlive()) {
        targets.push(battler);
      }
    }
    return targets;
  }

  Game_Action.prototype.targetsForAny = function() {
    var targets = [];
    var battlers = Scene_Battle.prototype.aliveGuys();
    if (this.isForAnyAll()) {
    
      // hardcodeonly alive members
      for (var i = 0; i < battlers.length; i++) {
        var battler = battlers[i];
        if (battler.isAlive()) {
          targets.push(battler);
        }
      }
      
    }
    else if (this.isForAnyOne()) {
      var battler = battlers[this._targetIndex];
      // assume we target next friend
      var unit = battler.friendsUnit();
      targets.push(unit.smoothTarget(battler.index()));
    }
    return targets;
  }
 
  Window_BattleAll.prototype.refresh = function() {
    this._enemies = []
    var battlers = Scene_Battle.prototype.aliveGuys();
    for (var i = 0; i < battlers.length; i++) {
      var battler = battlers[i];
      if (battler.isAlive()) {
        this._enemies.push(battler);
      }
    }
This probably makes some of those .isAlive() checks redundant, but the level I'm at, cleaning it up is partially blind guessing.

Original thread: https://forums.rpgmakerweb.com/threads/compatibility-multicast-targeting-both-an-enemy-and-an-ally.182157/