<Solved> Giving a random enemy a state

Hello all!

Here's the short version: I have a state that I want to give to one enemy in battle once every couple of turns via a common event. I want the game to chose which enemy this is randomly. This code will be used for battles with as many as three enemies and as few as one, and I want to avoid giving the state to a dead enemy, as this would be pointless.

In MV by default, there exists a system for giving enemies states in battle with events. The trouble is, the existing system gives two options; either give the state to all enemies or to one enemy specifically based on ID's.

My current thinking is that I could create a variable to hold a random number between one and three, and then apply the state to whichever enemy's ID is in that variable, and then afterwards I could check to make sure that at least one enemy has the state to ensure it didn't hit a corpse or try to give a state to an enemy that isn't there, and if not, run the event again until it works.

My problems are thus: I'm not sure how I would perform such a check, I'm not keen on running a random event on a loop, as this could cause excessive run times as the event gets wrong answers over and over, and I can't help but wonder whether there's a far more elegant and effective way to do what I'm looking to do, possibly with a plugin, possibly not.

Any help you can provide would be greatly appreciated. Thank you!
Could you describe what the state does and how you are applying it? Is the common event being called through a skill or is this for all battle troops throughout the game?
The state on it's own does nothing, It's the enemies that do something. Typically the enemies follow a set of variables controlled by a common event to determine what actions they perform in battle, but when an enemy has this state, they ignore those instructions and perform any action they can, chosen at random. It's to keep them unpredictable.

It's being applied by the previously mentioned common event. It uses another variable to keep track of the last time the state was applied, in order to apply it every few turns. The common event is run at the end of every turn in every battle, and the first thing it does is remove the state from all enemies, to reset the clock.

If you need anymore info, let me know.
Why use states and variables for that? Do battles usually have a set way of performing moves that make it more puzzle-like? Hope I'm not coming off as aggressive, just trying to see what your thought process is so we can streamline it.
Okay, so, here's basically how my combat system works in as small a nutshell as I can shrink it to. A-HE-HE-HEM:


TURN 1:
On the first turn of every combat, two things will happen: The variable "Last recent player action" (Which I will henchforth refer to as LRPA) is set to 0, and each enemy performs one specific move.

Which move is performed is based on which enemy is performing it, for example, a goblin would simply attack, whereas a priest would give a buff to one of his allies. Every single time, they do the same thing on turn one.

When the player performs an action, it will trigger one of 5 common events, based on what the action does. Each of these events does one simple thing; which is to set the value of the variable "Most recent player action" (MRPA) to either 1 if the action was a physical attack, 2 for a magical attack, 3 for defencive manuvers such as dodging or guarding, 4 for any kind of buff, and 5 for any healing. There are no other types of actions players can peform in battle.

END OF TURN 1:
Turn one ends and the main common event, referred to as the AI Conductor, activates, as it will at the end of every turn in every battle. It does the following things in the following order:

Firstly, it removes the "Misconduct" status effect from all enemies, even if none of them have it, just incase one of them does. (More on that later.)

Next, it checks if LRPA is equal to 0. If it is, (and it will be at the end of every first turn of battle) then it moves onto checking all five possible values of MRPA. For each possible outcome, it will insert a different value into a variable called "AI Orders". If it isn't, it will use a more complicated set of if statements to determine the values of both LRPA and MRPA, and for each, it will assign a different value to "AI Orders".
Some values will be used for multiple combinations.

Next, it sets the value of LRPA to be equal to whatever value is currently held in MRPA.

Lastly, it checks if variable number four, "Misconduct Counter" (MC) is 0. If it is, it will give the statues effect "Misconduct" to one enemy at random, then set MC to be equal to either 1 or 2, chosen at random. If it isn't, it decreases the value of MC by 1.

TURN 2 ONWARDS:
The enemies will use the value held in AI Orders to determine what action they should undertake. Each possible value that can be assigned to AI Orders represents a different battle strategy. For example, suppose the player chose to use a magical attack both last turn and the turn before:

Now, magic is very powerful in my game, and MP is a scarce resource, so if the player is using magical attacks with such reckless abandon, then it's safe to say that they either need to survive due to having low HP and no way to heal, or they are trying to get through this pack of mooks as fast as possible.

Either way, the smart thing for the enemies to do is to drop fine strategy and attack like madmen using whatever heavy hitting attacks they know, either because it's they best chance they have to win or because it's the only way to slow the player down. And so, if the value in AI Orders is the one for LRPA = MRPA = 2, then every enemy will attack like crazy.

Except the one with the Misconduct status effect. They'll do whatever the hell they want to.

The reason for the inclusion of this status effect is twofold: if the AI has a little brain-fart and heals itself when it has full HP, or tries to put a player to sleep when they are already asleep, then the game becomes easier for players who haven't cracked the code of AI actions, and for those that have, having an enemy behave unexpectedly makes the game harder, because you can never truly know what you're up against inside and out. The sweetest part is that an enemy with the Misconduct status effect could do what it was going to do anyway with it's free turn, so you can't even be sure you're in the clear, random- action-wise, until it's too late to change your plans.

The problem? If the player knows which foe has the effect, then the whole scheme goes belly-up. It's not mysterious and unpredictable if you can see it with your own two eyes. A lot of Yanfly plugins have been used to remove the messages relaying statues effect application to the player, but if I can't give the status effect randomly and without the player knowing it, then it's time to go back to the drawing board.


...okay yeah that really wasn't that short after all. Super duper sorry 🙁
Also sorry for all of the spelling errors, I kinda typed this in a hurry.
Here's a plugin you can use. Make sure to save it as "AddStateRandomly.js":

Code:
/*:
* @plugindesc Allows the user to add a state by ID to a random living enemy in a troop.
* @author Zevia
*
* @help In a script command, call "$gameTroop.addStateRandomly(x)", where x is the ID of
* the state you want to add.
*
* @param shouldFilter
* @type boolean
* @desc Whether enemies already afflicted should be ignored. If true and all living enemies are afflicted, nothing happens.
* @default true
*/

(function() {
    'use strict';

    var shouldFilter = !!PluginManager.parameters('AddStateRandomly').shouldFilter.match(/true/i);

    Game_Troop.prototype.addStateRandomly = function(stateId) {
        var aliveEnemies = this.aliveMembers().filter(function(enemy) {
            return !shouldFilter || !enemy.isStateAffected(stateId);
        });

        if (!aliveEnemies.length) { return; }
        aliveEnemies[Math.floor(Math.random() * aliveEnemies.length)].addState(stateId);
    };
})();

In your event, do a script call of
Code:
$gameTroop.addStateRandomly(x)
where x is the ID of the state you want to apply. As an example, to apply poison to a random living enemy using a default new project, you could do
Code:
$gameTroop.addStateRandomly(4);

By default, the plugin will first filter out enemies that already have the state before trying to apply it to a random enemy. If you change the parameter "shouldFilter" to false, then if it targets an enemy that already has the state, it will refresh its duration.
Thanks a bundle Zevia! It works like a charm!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-giving-a-random-enemy-a-state.104050/