MV [RMMV] Tweaking the Enemy Skill Rating - Only pick the Highest Rating

Hello everyone, I would to tweak how the default behaviour of enemies choosing their actions work.


As you know, skills in the Enemy Tab have a rating from 1 - 9.
The higher the rating, the more likely they are to use the skill.

However, when choosing skills, only the skill with the highest rating and skills
with a rating of 1 or 2 less will be considered.

So if the highest skill has a rating of 7, only skills of rating 7, 6 and 5 will be considered, with decreasing probabilities.



Now, I want to make the rating more a absolute priority, meaning the enemy ONLY considers the highest rated skill, or skill(s) if they share a rating.

So if you have 3 skills with ratings 3, 7 and 8, the enemy will ALWAYS choose the one with a rating of 8.

While experimenting by myself, I came across these two functions in rpg_objects.js which seem to be the only functions to reference the enemy skill ratings:


Code:
Game_Enemy.prototype.selectAction = function(actionList, ratingZero) {  // rpg_objects.js - Line 4527
    var sum = actionList.reduce(function(r, a) {
        return r + a.rating - ratingZero;
    }, 0);
    if (sum > 0) {
        var value = Math.randomInt(sum);
        for (var i = 0; i < actionList.length; i++) {
            var action = actionList[i];
            value -= action.rating - ratingZero;
            if (value < 0) {
                return action;
            }
        }
    } else {
        return null;
    }
};

Game_Enemy.prototype.selectAllActions = function(actionList) { // rpg_objects.js - Line 4545
    var ratingMax = Math.max.apply(null, actionList.map(function(a) {
        return a.rating;
    }));
    var ratingZero = ratingMax - 3;
    actionList = actionList.filter(function(a) {
        return a.rating > ratingZero;
    });
    for (var i = 0; i < this.numActions(); i++) {
        this.action(i).setEnemyAction(this.selectAction(actionList, ratingZero));
    }
};

I am not confident enough in JS yet to know if these are in fact the right functions and if so, which numbers to tweak.
After some trial and error, I could not produce the results I wanted.

Thus I am asking if anyone has a solution for me.

Any help is appreciated.
I think that's the right direction, you just need to adjust the ratingZero value that gets passed from selectAllActions. Try this?
Spoiler: plugin code
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Enemies choose highest-priority action(s) only.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/164357/
 * @help Free to use and/or modify for any project, no credit required.
 */
// Override!
Game_Enemy.prototype.selectAllActions = function(actionList) {
    const ratingMax = Math.max(...actionList.map(a => a.rating));
    const ratingZero = ratingMax - 1;  // originally -3
    actionList = actionList.filter(a => a.rating > ratingZero);
    for (let i = 0; i < this.numActions(); i++) {
        this.action(i).setEnemyAction(
            this.selectAction(actionList, ratingZero)
        );
    }
};
It has to be -1, because rating - ratingZero must be positive for the weighted random selection to work. Or, another way to look at it: it's usually -3, which allows actions up to 2 ratings lower than the max...so -1 allows actions up to 0 ratings lower. 👋
Just out of curiosity...what's the point?

If the enemy always does the skill with the highest rating, why bother putting the others into the list?
@ATT_Turan You're doing the ninja thing again. You stole my question.
Conditions?
@Sword_of_Dusk Sorry, you can have it back! I'm done with it.

@caethyril But isn't that how conditions already work? My understanding (I've never looked into the code) is that the range of numbers being considered for the rating is only for skills that meet their current condition anyway.
@ATT_Turan
Well it's kind of complicated to explain in short because I
would have to explain how I set up combat as a whole.

But the abridged version is that all of the enemy skills can only be used while under certain 'background' states.

However, many skills have overlapping states and whatnot and I basically 'ran out of space' since I could only really do ratings with 1, 4 and 7 and can only choose one state per skill as condition.

I do most of the enemy skill selection and AI within my own functions, but I still needed to incorporate the default skills tab to some degree for everything to work.

@caethyril
So I really was on the right track. Because I figured messing with that line would make
the most sense. And I did actually play around with it.

And so this really was the solution to my problem. But it seems I got a false negative during my own testing since I just found out I had to fix some other problems aswell which made it look like altering the function didnt do anything.

Anyways thank you for your time and help as always! 🙂
Fair enough.

Might it not be easier at that point to use, like, Yanfly's Enemy A.I.? Or you just prefer to make your own systems?
Fair enough.

Might it not be easier at that point to use, like, Yanfly's Enemy A.I.? Or you just prefer to make your own systems?

This may be derailing the thread a little bit, but considering this is my own thread which has essentially been solved, it should be fine.

As to your question, I definitely enjoy making my own systems and it was one of the reasons.
The main reason however were technical problems. Again, explaining that would be kind of tedious, so I apologize for being so vague.

It generally has to do with state-stacking, enemy attack previews, dot effects and other smaller aspects.

Yanfly's Enemy A.I. would suffice for creating the AI and skill selection and it would have been preferable to be honest, as I loved working with it in other projects, but it would be fairly complicated to implement alongside my other systems.

Basically, I had to make some compromises and throwing out Yanfly's Enemy A.I. was the least intrusive option. I don't really regret it either, because making a dedicated plugin just for enemy AI looks a lot tidier and comprehensible than the small notetag boxes in the database.

Original thread: https://forums.rpgmakerweb.com/threads/rmmv-tweaking-the-enemy-skill-rating-only-pick-the-highest-rating.164357/