State Paranoid (Resolved)

Hello,
I need help finalizing a status effect I'm trying to create. It's a paranoia effect (much like in the darkest dungeon).
A character affected by this effect will systematically dodge all beneficial effects inflicted on him by his allies.
At the moment my code looks like this:

Code:
<Custom Selection Effect>

// Check if this action is a skill and targets an opponent.

if (this.item() && DataManager.isSkill(this.item()) && this.isForFriend()) {

  // Create a pool of blocked skill TYPES.

  var blockedtypes = [];

  // Add skill type IDs to this pool.

  blocked types.push(1, 2, 4, 17, 18);

  // Check if skill type or skill ID matches.

  if (blockedtypes.contains(this.item().stypeId) ||blockedskills.contains(this.item().id)) {

    // If so, store the skill's original success rate.

    this._formerItemSuccessRate = this.item().successRate;

    // Now reduce the skill's hit rate to 0.

    this.item().successRate = 0;



  }

}

</Custom Selection Effect>



<Custom deselection effect>

// Check if a success rate is stored.

if (this._formerItemSuccessRate !== undefined) {

  // Restore the skill's success rate.

  this.item().successRate = this._formerItemSuccessRate;

  // Start an animation to show that the spell shield has occurred.

    target.startAnimation(233);

}

</Custom deselection effect>

There are still a few changes to be made. I see 3, but don't hesitate to tell me if I forget any.

1 - The character assigned to the status must not dodge their own abilities. Currently, if he tries to heal himself, the action will be dodged.

2 - Objects must also be dodged.

3 - I don't know if it's possible. Otherwise it does not matter. But is it possible to force the character to animate in the same way as during a real dodge? Currently I'm just using an animation which has no visual effects but uses the "miss" SE. I'll settle for it if I can't do better.
you can mix with default mechanics:
just add resist state traits to the paranoid state and those will never affect the paranoid actor.
similar ways for elemental damage rates to negate the effects of certain damage types.

to make his own skills pass that, one option would be to give the actor copies of skills with a different element and different states that only look the same to the player because of identical descriptions.

however you might have to mix both options together because the selective passing would be more problematical if multiple actors can become paranoid.
Thank you for your answer but I admit I don't understand everything. I'm very bad at coding, I only know how to copy and recross pieces of code that I've already seen on other effects.
In my game, it will probably be possible to have several paranoid heroes at the same time.

For 1) I think something like "if !TargetIsUser" may exist. But I can't spell it correctly.

For 2) I found a piece of code that says:
Code:
// Check if the current action is an item for an ally.
if (action && action.isItem() && action.isForFriend()) {
This should be able to work, but I can't get it to fit properly in the code. Can be with an "or" function, or can be by including it in the "blockedtypes" variable. I don't know if it's possible, and I don't know how to do it.

These are just ideas to help you for help me ^^"
Code:
<Custom Select Effect>
// Check if this action is a skill and targets an opponent.
if (this.item() && DataManager.isSkill(this.item()) && this.isForFriend()) {
If you only want to dodge abilities from other actors, then just change this to have the addition:
Code:
if (this.item() && DataManager.isSkill(this.item()) && user!=target && this.isForFriend()) {
Code:
  // Create a pool of blocked Skill TYPES.
  var blockedtypes = [];
  // Add the skill type ID's to that pool.
  blockedtypes.push(1, 2, 4, 17, 18);
These two separate lines are quite pointless. That should simply be a part of the variable's declarataion.
Code:
var blockedtypes=[1, 2, 4, 17, 18];

It's also done really weirdly. You have it checking both the skill type and the skill ID to be in those numbers. So that means, in a default project, all beneficial skills of the magic or special types. But then you're also looking at specific skill IDs.

But then I have absolutely no idea why you're looking at either skill types or skill IDs when your very first conditional establishes that it's a beneficial effect. Are there specific skills you do want to allow? If so, I think you're doing it wrong. If not, about half of the code in this notetag is useless.
Code:
  // Check if the skill type or skill ID matches.
  if (blockedtypes.contains(this.item().stypeId) || blockedskills.contains(this.item().id)) {
    // If it does, then store the skill's original success rate.
    this._formerItemSuccessRate = this.item().successRate;
This. Very weird.
1 - The character affected for status must not dodge their own abilities. Currently, if he attempts to heal himself, the action will be dodged.
I gave that fix above.
2 - Objects must also be dodged.
Do you mean Items? You specifically check for it to not be an item in your original conditional. If you don't want that, it shouldn't be there. So the re-modified line would be:
Code:
if (this.item() && user!=target && this.isForFriend()) {

I would suggest, the next time you want to produce something special, just asking for help off the bat. You seem to have taken and made a Frankenstein construct of other code that you found laying around, and it's actively working against your purposes.
3 - I don't know if it's possible. If not, it's no big deal. But is it possible to force the character to animate in the same way as during a real dodge? Currently I'm just using an animation which has no visual effect but uses the "miss" SE. I'll settle for it if I can't do better.
You would have to rewrite the state and its code. Right now it sets the action's success rate to 0, which means it doesn't hit and therefore evade is never checked. You would need to replace that with code that guarantees a hit and an evade in some way.
Thank you for your answer, once again you are a great help. I was able to solve problems number 1 and 2 with your code:
Code:
if (this.item() && user!=target && this.isForFriend()) {

Indeed, my code comes from another status effect that I had found on the internet (a tip from Yanfly). I tried to modify it but since I don't understand everything, I may have kept unnecessary things.
To answer your questions:
No, there is no skill that will make an exception.
Yes it is indeed an item (like a potion, translation error).

Regarding point number 3, I understand what you mean. But I don't know how to do it. Can you help me ? If it's too complicated, I'll settle for the correction for points 1 and 2. That was really what was essential.
I would point out that most of the beneficial abilities to dodge are "certain hits".
Indeed, my code comes from another status effect that I had found on the internet (a tip from Yanfly). I tried to modify it but since I don't understand everything, I may have kept unnecessary things.
It's always tricky trying to modify code if you don't actually know what you're doing. Especially when what you want is very different from what the Tip & Trick is starting with.

Making the actor actually evade is fairly complex, as the evasion stat is calculated from all of the actor's traits and there isn't a timing window in Yanfly's states that allows you to directly modify whether the attack is evaded before it processes damage on the hit. You would basically need a little plugin in addition to the state.

So leaving it as saying "Miss" is the easiest thing to do. You can cut your first notetag down to this:
Code:
<Custom Select Effect>
// Check if this action targets an ally and does not come from the user
if (this.item() && this.isForFriend() && target!=user) {
    // Store the action's original success rate.
    this._formerItemSuccessRate = this.item().successRate;
    // Now drop the item's success rate to 0.
    this.item().successRate = 0;
}
</Custom Select Effect>
I understand. Thank you for your explanations. This code will work just fine =)
One make-shift solution could be to modify the result in your deselect effect to indicate that the skill was evaded even though it technically wasn't (which should not make a difference in most cases):
Code:
if (target.result().missed) {
  target.result().missed = false;
  target.result().evaded = true;
}

Alternatively you could change your evasion chance calculations in a new plugin. By default the evasion chance is calculated in rmmz_objects.js, line 1878-1886. You can copy that function into a new plugin and add your paranoid state there:
Code:
Game_Action.prototype.itemEva = function(target) {
    // Paranoia:
    let user = this.subject();
    if (this.item() && this.isForFriend() && target !== user) {
        if (target.traitObjects().some((obj) => obj.meta["paranoid"])) {
            return 1.0;
        }
    }
    // Old evasion chance if paranoia does not apply:
    if (this.isPhysical()) {
        return target.eva;
    } else if (this.isMagical()) {
        return target.mev;
    } else {
        return 0;
    }
};
Instead of user you'd have to use this.subject() here.
Since that formula is applied to all actions, you would need another way to check if a target is actually paranoid. In this example the target would need to have a state or other trait with a <paranoid> notetag, but you can change the condition to something simpler if needed.

Instead of copying the old code over you can also work with a function snapshot. This is generally preferred since it allows multiple plugins to change the same function without necessarily competing with each other. Apart from that it works the same as the above:
Spoiler
Code:
(function() {
   
    const oldVersion = Game_Action.prototype.itemEva;
    Game_Action.prototype.itemEva = function(target) {
        // Paranoia:
        let user = this.subject();
        if (this.item() && this.isForFriend() && target !== user) {
            if (target.traitObjects().some((obj) => obj.meta["paranoid"])) {
                return 1.0;
            }
        }
        // Old evasion chance if paranoia does not apply:
        return oldVersion.call(this, target);
    };

})();
One make-shift solution could be to modify the result in your deselect effect to indicate that the skill was evaded even though it technically wasn't
That's a good call. I thought the evading and such was during the damage execution, but you're absolutely correct that it happens afterward.

I would suggest making the conditional based on the change made in the Select Effect, otherwise the actor will appear to evade every attack that just misses.

Code:
<Custom Deselect Effect>
// Check if there is a success rate stored.
if (this._formerItemSuccessRate !== undefined) {
  // Restore the skill's success rate.
  this.item().successRate = this._formerItemSuccessRate;
  this._formerItemSuccessRate=undefined;
  target.result().missed=false;
  target.result().evaded=true;
}
</Custom Deselect Effect>

By default the evasion chance is calculated in rmmz_objects.js, line 1878-1886.
Note the OP is using MV 😉
I have the impression that you have the solution but I did not succeed.
Automatic google translation + my level in javascript... ^^"

From what I understood :

I replaced my deselection effect with:
Code:
<Custom Deselect Effect>
// Check if there is a success rate stored.
if (this._formerItemSuccessRate !== undefined) {
  // Restore the skill's success rate.
  this.item().successRate = this._formerItemSuccessRate;
  this._formerItemSuccessRate=undefined;
  target.result().missed=false;
  target.result().evaded=true;
}
</Custom Deselect Effect>


I created a new script that I added to the project:
Code:
Game_Action.prototype.itemEva = function(target) {
    // Paranoia:
    let user = this.subject();
    if (this.item() && this.isForFriend() && target !== user) {
        if (target.traitObjects().some((obj) => obj.meta["paranoid"])) {
            return 1.0;
        }
    }
    // Old evasion chance if paranoia does not apply:
    if (this.isPhysical()) {
        return target.eva;
    } else if (this.isMagical()) {
        return target.mev;
    } else {
        return 0;
    }
};


I also added a line to my paranoid status:
Code:
<paranoid>
For this one, I'm pretty sure it wasn't the thing to do ^^"

After making these changes, it does not work. I probably did something wrong or forgot something.
From what I understood :
[...]
The post was meant to provide two alternatives.
If you change the outcome in your deselect effect you should not need the extra plugin. If you use the extra plugin to customize evasion chances you should not need to set the items success rate to 0.

That said, unfortunately changing the result after the fact does not seem to work with YEP_BattleEngineCore.

If you are already using the plugin from the last post you could try to remove your <custom select effect> to see if it works. If you want to change what items are evaded under paranoia later, you would have to edit the plugin then.

Right now I don't see another easy way to acomplish this with a selection effect alone.
If you are already using the plugin from the last post you could try to remove your <custom select effect> to see if it works. If you want to change what items are evaded under paranoia later, you would have to edit the plugin then.
It works. I confess that I stopped trying to understand, but in any case it works.
A big thank you to you and to those who helped me achieve this state =)

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/state-paranoid-resolved.149969/