Using a Skill to spread a state.

I was wondering if anyone could help me make a skill that when used on an enemy with a specific state, it inflicts the state to another random enemy(s), being able to choose how many would be great but if it just has to be one that's perfectly okay too.

This is the closest thing I've found however it spreads the state to all enemies.
https://forums.rpgmakerweb.com/index.php?threads/spreading-states.92925/#post-843631
This is the code from the link if you don't feel like going to it.
Code:
<Post-Damage Eval>
var spread = [4,5,6];
spread = target.states().filter(function(state) {
return spread.indexOf(state.id) > -1;
});
if (spread.length > 0) {
var group = target.friendsUnit().members();
group.splice(target.index(), 1);
while (spread.length > 0) {
var state = spread.shift();
group.forEach(function(targ) {
targ.addState(state.id);
});
}
}
</Post-Damage Eval>

Any help is greatly appreciated, thanks for your time.

I've moved this thread to JavaScript Support. Thank you.

The code you posted looks to work for spreading multiple states, is that what you want or is it only 1 state that gets spread?

What should happen if it picks a random enemy that already has the state? Should it just try to pick a different random enemy?
1.That code spread to ALL enemies when it does spread. I only want it to spread to one enemy at a time. For the state im using its just too strong to let it affect all enemies at once. Having the option to how many enemies it spread to rather than automatically ALL targets would be the absolute best scenario. Worst case, I want it to spread to just one other target.
2. Ideally, if it did reselect the target of the skill who already has the state it would be preferable to pick a new random enemy, it wouldn't be the end of the world if it did just reapply because of a plugin I have it just refreshes the states anyway. So this isn't like a huge setback if that's the case, obviously not preferable but it's manageable.
👋 I think this ought to do it! It should check the target's states for any with an ID listed in the spread array, then copy any matching states to count of the target's friends (excluding the original target, and limited by the unit size).
Spoiler
Code:
<After Eval>
var spread = [4,5,6];
var count = 1;
spread = target.states().filter(function(state) {
  return spread.indexOf(state.id) > -1;
});
if (spread.length > 0) {
  var group = target.friendsUnit().members();
  var targ2 = [];
  if (count >= group.length - 1) {
    targ2 = group.slice();
    targ2.splice(target.index(), 1);
  } else {
    targ2.push(target.index());
    for (var n = 1; n <= count; n++) {
      var r = Math.randomInt(group.length - n);
      while (targ2.contains(r)) r++;
      targ2.push(r);
    }
    targ2.shift();
    targ2 = targ2.map(function(r) {
      return group[r];
    });
  }
  targ2.forEach(function(t) {
    spread.forEach(function(s) {
      t.addState(s.id);
    });
  });
}
</After Eval>
You can change the notetag type to <Post-Damage Eval> if you only want it to process on hit.

Edit: this version is biased to pick lower random target indices and may glitch out after one or more of the target unit has died. See my follow-up post below for a "better" version.
It looks like that code won't reselect a new enemy if the random enemy it selected already has the state.

I was thinking something like this:
Spoiler
Code:
// The state IDs that could be spread
const STATES_TO_SPREAD = [4, 5, 6];
// How many enemies to spread to
const NUM_TO_SPREAD = 2;
// What states will actually be spread
const states = target.states().filter(state => STATES_TO_SPREAD.includes(state.id));
if (states.length > 0) {
    // Create a helper function
    const hasState = (member, id) => member.states().some(state => state.id === id);
    // Create a copy of the unit array without the target
    const unit = [...target.friendsUnit().members()];
    unit.splice(target.index(), 1);
    states.forEach(state => {
        // Repeat for as many enemies to spread to
        for (let n = 0; n < NUM_TO_SPREAD; n++) {
            // Don't look for targets if every member already has the state
            while (!unit.every(member => hasState(member, state.id))) {
                const i = Math.randomInt(unit.length - 1);
                // If the enemy doesn't have the state
                if (!hasState(unit[i], state.id)) {
                    unit[i].addState(state.id);
                    break;
                }
            }
        }
    });
}
Note: This uses 2015+ standards of Javascript so will not work on old versions of MV.
@caethyril This code does work. It will spread the state specified to the amount of targets specified without issue. It can however, spread to a target who already possesses the state. With the plugins I'm using it refreshes the state if it does connect with an enemy who already has the state. This isn't a huge problem for me, but if there was a way to pick an enemy who didn't have the state, that would be preferred.

@Aloe Guvner This code runs into a problem for me. A couple ways it has froze on me:
Against 3 enemies:
1. Apply State 1 to Enemy A.
2. Use Skill 1 to Spread State 1; State spreads to Enemy B.
3. Use Skill 1 on Enemy A or B both with State 1 active, the game freezes.

Also, changing the number of targets to spread to other than 1 also causes a freeze as the skill is landing.
@caethyril This code does work. It will spread the state specified to the amount of targets specified without issue. It can however, spread to a target who already possesses the state. With the plugins I'm using it refreshes the state if it does connect with an enemy who already has the state. This isn't a huge problem for me, but if there was a way to pick an enemy who didn't have the state, that would be preferred.
Oh, I see. With further testing it turns out that my solution had other problems, too: spreading to dead battlers (wut) and a unwarranted bias toward lower target index when selecting random secondary targets. So, here's a version that I think fixes those things, including not adding states to targets who already have them...and not selecting them in the first place if they already have all the spreading states. I also tidied things up a little~
Spoiler
Code:
<After Eval>
var spread = [4,5,6];
var count = 1;
spread = target.states().filter(function(state) {
  return spread.indexOf(state.id) > -1;
});  // These are now states, not IDs
if (spread.length > 0) {
  var group = target.friendsUnit().aliveMembers().slice();
  var targ2 = [];
  group.splice(group.indexOf(target), 1);
  group = group.filter(function(m) {
    return spread.some(function(s) {
      return !m.states().contains(s);
    });
  });
  if (count >= group.length) {
    targ2 = group;
  } else {
    for (var n = 0; n < count; n++) {
      var r = Math.randomInt(group.length - n);
      targ2.forEach(function(i) {
        if (i <= r) r++;
      });
      targ2.push(r);
    }
    targ2 = targ2.map(function(n) {
      return group[n];
    });
  }
  targ2.forEach(function(t) {
    spread.forEach(function(s) {
      if (!t.isStateAffected(s.id)) {
        t.addState(s.id);
      }
    });
  });
}
</After Eval>
@caethyril I tested it quite a bit and haven't encountered any problems. At least none that I noticed, works beautifully. Thank you very much, I appreciate your time and effort!

Original thread: https://forums.rpgmakerweb.com/threads/using-a-skill-to-spread-a-state.103517/