Advertisement
Double_X

DoubleX RMMV Confusion Edit v100e

Jan 3rd, 2016 (edited)
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*============================================================================
  2.  *    ## Plugin Info
  3.  *----------------------------------------------------------------------------
  4.  *    # Plugin Name
  5.  *      DoubleX RMMV Confusion Edit
  6.  *----------------------------------------------------------------------------
  7.  *    # Terms Of Use
  8.  *      You shall keep this plugin's Plugin Info part's contents intact
  9.  *      You shalln't claim that this plugin's written by anyone other than
  10.  *      DoubleX or his aliases
  11.  *      None of the above applies to DoubleX or his aliases
  12.  *----------------------------------------------------------------------------
  13.  *    # Prerequisites
  14.  *      Abilities:
  15.  *      1. Little Javascript coding proficiency to fully utilize this plugin
  16.  *----------------------------------------------------------------------------
  17.  *    # Links
  18.  *      This plugin:
  19.  *      1. http://pastebin.com/LX7qJ74P
  20.  *      Video:
  21.  *      1. https://www.youtube.com/watch?v=NCVMdR7HFls
  22.  *      Mentioned Patreon Supporters:
  23.  *      https://www.patreon.com/posts/71738797
  24.  *----------------------------------------------------------------------------
  25.  *    # Author
  26.  *      DoubleX
  27.  *----------------------------------------------------------------------------
  28.  *    # Changelog
  29.  *      v1.00e(GMT 1100 9-9-2022):
  30.  *      1. Fixed infinite loop in targetsForReversedExcludeSelf due to typo
  31.  *      v1.00d(GMT 0200 6-6-2020):
  32.  *      1. Fixed actions having no targets not executing bug
  33.  *      v1.00c(GMT 0300 9-3-2020):
  34.  *      1. Fixed undefined subject in hasValidOriginalTargets
  35.  *      v1.00b(GMT 0800 30-1-2016):
  36.  *      1. Fixed missing return in targetsForOpponents and targetsForFriends
  37.  *      2. Fixed undefined target in targetsForReversedExcludeSelf due to typo
  38.  *      3. Fixed infinite loop with excludeSelf when subject's always selected
  39.  *      v1.00a(GMT 0800 3-1-2016):
  40.  *      1. 1st version of this plugin finished
  41.  *============================================================================*/
  42. /*:
  43.  * @plugindesc Lets you set some states to reverse the ally/foe identification
  44.  * @author DoubleX
  45.  *
  46.  * @help
  47.  *============================================================================
  48.  *    ## Notetag Info
  49.  *----------------------------------------------------------------------------
  50.  *    # State Notetags:
  51.  *      1. <confusion edit: reverse, excludeSelf>
  52.  *         - Sets the confusion state to reverse the ally/foe identification
  53.  *           only instead of restricting the battler to be merely using the
  54.  *           1st skill in the database if reverse is true and the actions
  55.  *           aren't forced
  56.  *         - Reversal will never take place for state Restriction as Attack an
  57.  *           Enemy nor skill/item scope as The User
  58.  *         - Reversal will have 50% chance to take place for state Restriction
  59.  *           as Attack Anyone
  60.  *         - Reversal will always take place for state Restriction as Attack
  61.  *           an Ally
  62.  *         - Sets the confusion state to make the battler never targeting self
  63.  *           if excludeSelf is true and the actions aren't forced
  64.  *         - Only the effective notetag with the highest priority will be used
  65.  *============================================================================
  66.  *    ## Plugin Call Info
  67.  *----------------------------------------------------------------------------
  68.  *    # State manipulations
  69.  *      1. meta.confusionEdit
  70.  *         - Returns the confusion edit reverse and excludeSelf flag in the
  71.  *           form of { reverse: reverse, excludeSelf: excludeSelf }
  72.  *      2. meta.confusionEdit = { reverse: reverse, excludeSelf: excludeSelf }
  73.  *         - Sets the confusion edit reverse and excludeSelf flag in the form
  74.  *           of { reverse: reverse, excludeSelf: excludeSelf }
  75.  *         - All meta.confusionEdit changes can be saved if
  76.  *           DoubleX RMMV Dynamic Data is used
  77.  *============================================================================
  78.  */
  79.  
  80. "use strict";
  81. var DoubleX_RMMV = DoubleX_RMMV || {};
  82. DoubleX_RMMV["Confusion Edit"] = "v1.00e";
  83.  
  84. /*============================================================================
  85.  *    ## Plugin Implementations
  86.  *       You need not edit this part as it's about how this plugin works
  87.  *----------------------------------------------------------------------------
  88.  *    # Plugin Support Info:
  89.  *      1. Prerequisites
  90.  *         - Some Javascript coding proficiency to fully comprehend this
  91.  *           plugin
  92.  *      2. Function documentation
  93.  *         - The 1st part describes why this function's rewritten/extended for
  94.  *           rewritten/extended functions or what the function does for new
  95.  *           functions
  96.  *         - The 2nd part describes what the arguments of the function are
  97.  *         - The 3rd part informs which version rewritten, extended or created
  98.  *           this function
  99.  *         - The 4th part informs whether the function's rewritten or new
  100.  *         - The 5th part informs whether the function's a real or potential
  101.  *           hotspot
  102.  *         - The 6th part describes how this function works for new functions
  103.  *           only, and describes the parts added, removed or rewritten for
  104.  *           rewritten or extended functions only
  105.  *         Example:
  106.  * /*----------------------------------------------------------------------
  107.  *  *    Why rewrite/extended/What this function does
  108.  *  *----------------------------------------------------------------------*/
  109. /* // arguments: What these arguments are
  110.  * functionName = function(arguments) { // Version X+; Hotspot
  111.  *     // Added/Removed/Rewritten to do something/How this function works
  112.  *     functionContents
  113.  *     //
  114.  * } // functionName
  115.  *----------------------------------------------------------------------------*/
  116.  
  117. DoubleX_RMMV.Confusion_Edit = {};
  118. (function(CE) {
  119.  
  120.     CE.DataManager = {};
  121.     var DM = CE.DataManager;
  122.  
  123.     DM.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  124.     DataManager.isDatabaseLoaded = function() {
  125.         // Rewritten
  126.         return DM.isDatabaseLoaded.apply(this, arguments) && DM.loadAllNotes();
  127.         //
  128.     }; // DataManager.isDatabaseLoaded
  129.  
  130.     DM.loadAllNotes = function() {
  131.         $dataStates.forEach(function(data) {
  132.             if (data) { DM.loadDataNotes(data); }
  133.         });
  134.         return true;
  135.     }; // DM.loadAllNotes
  136.  
  137.     // data: The data to have its notetags read
  138.     DM.loadDataNotes = function(data) {
  139.         data.meta.confusionEdit = {};
  140.         var lines = data.note.split(/[\r\n]+/);
  141.         var regex = /< *confusion +edit *: *(\w+) *, *(\w+) *>/i;
  142.         for (var index = 0, length = lines.length; index < length; index++) {
  143.             if (!lines[index].match(regex)) { continue; }
  144.             data.meta.confusionEdit.reverse = RegExp.$1 === "true";
  145.             return data.meta.confusionEdit.excludeSelf = RegExp.$2 === "true";
  146.         }
  147.     }; // DM.loadDataNotes
  148.  
  149.     CE.Game_Action = {};
  150.     var GA = CE.Game_Action;
  151.  
  152.     GA.friendsUnit = Game_Action.prototype.friendsUnit;
  153.     Game_Action.prototype.friendsUnit = function() {
  154.         // Rewritten to reverse the ally/foe identification with reverse flag
  155.         return GA.originalReversedUnit.call(this, "friends", "opponents");
  156.         //
  157.     }; // Game_Action.prototype.friendsUnit
  158.  
  159.     GA.opponentsUnit = Game_Action.prototype.opponentsUnit;
  160.     Game_Action.prototype.opponentsUnit = function() {
  161.         // Rewritten to reverse the ally/foe identification with reverse flag
  162.         return GA.originalReversedUnit.call(this, "opponents", "friends");
  163.         //
  164.     }; // Game_Action.prototype.opponentsUnit
  165.  
  166.     GA.setConfusion = Game_Action.prototype.setConfusion;
  167.     Game_Action.prototype.setConfusion = function() {
  168.         // Added to let battlers with reverse flag to use all available skills
  169.         if (GBB.isConfusionEdit.call(this.subject(), "reverse")) { return; }
  170.         //
  171.         GA.setConfusion.apply(this, arguments);
  172.     }; // Game_Action.prototype.setConfusion
  173.  
  174.     GA.isValid = Game_Action.prototype.isValid;
  175.     Game_Action.prototype.isValid = function() {
  176.         // Rewritten to ensure the target list won't be empty due to excludeSelf
  177.         if (!GA.isValid.apply(this, arguments)) { return false; }
  178.         if (!this.isForOpponent() && !this.isForFriend()) return true;
  179.         return this.makeTargets().length > 0;
  180.         //
  181.     }; // Game_Action.prototype.isValid
  182.  
  183.     GA.makeTargets = Game_Action.prototype.makeTargets;
  184.     Game_Action.prototype.makeTargets = function() {
  185.         // Rewritten
  186.         if (GBB.isConfusionEdit.call(this.subject(), "reverse")) {
  187.             return this.repeatTargets(GA.makeReversedTargets.call(this));
  188.         } else if (GA.hasValidOriginalTargets.call(this)) {
  189.             return GA.makeTargets.apply(this, arguments);
  190.         }
  191.         return [];
  192.         //
  193.     }; // Game_Action.prototype.makeTargets
  194.  
  195.     GA.confusionTarget = Game_Action.prototype.confusionTarget;
  196.     Game_Action.prototype.confusionTarget = function() {
  197.         // Rewritten to exclude the subject itself with the excludeSelf flag
  198.         var subject = this.subject();
  199.         var target = GA.confusionTarget.apply(this, arguments);
  200.         if (GBB.isConfusionEdit.call(subject, "excludeSelf")) {
  201.             while (subject === target) {
  202.                 target = GA.confusionTarget.apply(this, arguments);
  203.             }
  204.         }
  205.         return target;
  206.         //
  207.     }; // Game_Action.prototype.confusionTarget
  208.  
  209.     GA.targetsForOpponents = Game_Action.prototype.targetsForOpponents;
  210.     Game_Action.prototype.targetsForOpponents = function() {
  211.         // Rewritten
  212.         return GA.confusionEditTargets.call(this, "targetsForOpponents");
  213.         //
  214.     }; // Game_Action.prototype.targetsForOpponents
  215.  
  216.     GA.targetsForFriends = Game_Action.prototype.targetsForFriends;
  217.     Game_Action.prototype.targetsForFriends = function() {
  218.         // Rewritten
  219.         return GA.confusionEditTargets.call(this, "targetsForFriends");
  220.         //
  221.     }; // Game_Action.prototype.targetsForFriends
  222.     /*------------------------------------------------------------------------
  223.      *    Returns the reversed allies/foes for reverse flag
  224.      *------------------------------------------------------------------------*/
  225.     /* original: The original units to be returned
  226.      * reversed: The units with the reverse flag to the returned
  227.      */
  228.     GA.originalReversedUnit = function(original, reversed) {
  229.         var subject = this.subject();
  230.         if (!this._forcing && GBB.isConfusionEdit.call(subject, "reverse")) {
  231.             switch (subject.confusionLevel()) {
  232.             case 1: return GA[original + "Unit"].apply(this, arguments);
  233.             case 2:
  234.                 var side = Math.random() < 0.5 ? original : reversed;
  235.                 return GA[side + "Unit"].apply(this, arguments);
  236.             default: return GA[reversed + "Unit"].apply(this, arguments);
  237.             }
  238.         }
  239.         return GA[original + "Unit"].apply(this, arguments);
  240.     }; // GA.originalReversedUnit
  241.  
  242.     /*------------------------------------------------------------------------
  243.      *    Uses Autobattle with reversed ally/foe definition to mimic confusion
  244.      *------------------------------------------------------------------------*/
  245.     GA.makeReversedTargets = function() {
  246.         if (this.isForOpponent()) { return this.targetsForOpponents(); }
  247.         if (this.isForFriend()) { return this.targetsForFriends(); }
  248.         return [];
  249.     }; // GA.makeReversedTargets
  250.  
  251.     /*------------------------------------------------------------------------
  252.      *    Returns if excludeSelf is false or self isn't the only valid target
  253.      *------------------------------------------------------------------------*/
  254.     GA.hasValidOriginalTargets = function() {
  255.         if (this._forcing) { return true; }
  256.         var subject = this.subject();
  257.         if (!GBB.isConfusionEdit.call(subject, "excludeSelf")) { return true; }
  258.         if (this.isForUser()) { return false; }
  259.         if (subject.confusionLevel() !== 3) { return true; }
  260.         return this.friendsUnit().aliveMembers().length > 1;
  261.     }; // GA.hasValidOriginalTargets
  262.  
  263.     // functionName: The targetsForOpponents/targetsForFriends function name
  264.     GA.confusionEditTargets = function(functionName) {
  265.         if (!GBB.isConfusionEdit.call(this.subject(), "excludeSelf")) {
  266.             return GA[functionName].apply(this, arguments);
  267.         }
  268.         // Prevents inifinite loops by checking for cases having no valid target
  269.         if (!GA.canTargetReversed.call(this)) { return []; }
  270.         //
  271.         return GA.targetsForReversedExcludeSelf.call(this, functionName);
  272.     }; // GA.confusionEditTargets
  273.  
  274.     /*------------------------------------------------------------------------
  275.      *    Checks if there are targets other than self with the reverse flag
  276.      *------------------------------------------------------------------------*/
  277.     GA.canTargetReversed = function() {
  278.         // User's always equal to self for both original and reversed cases
  279.         if (this.isForUser()) { return false; }
  280.         //
  281.         var level = this.subject().confusionLevel();
  282.         if (level === 1 && this.isForFriend()) {
  283.             return this.friendsUnit().aliveMembers().length > 1;
  284.         } else if (level === 3 && this.isForOpponent()) {
  285.             return this.opponentsUnit().aliveMembers().length > 1;
  286.         }
  287.         return true;
  288.     }; // GA.canTargetReversed
  289.  
  290.     /*------------------------------------------------------------------------
  291.      *    Returns the target list with both reverse and excludeSelf flags
  292.      *------------------------------------------------------------------------*/
  293.     // functionName: The targetsForOpponents/targetsForFriends function name
  294.     GA.targetsForReversedExcludeSelf = function(functionName) {
  295.         var subject = this.subject();
  296.         var index, num = this.isForRandom() ? this.numTargets() : 1, targets;
  297.         do {
  298.             // Prevents infinite loop by ensuring the subject won't be selected
  299.             if (this._targetIndex === subject.index() && this.isForOne()) {
  300.                 this._targetIndex -= 1;
  301.             }
  302.             //
  303.             targets = GA[functionName].apply(this, arguments);
  304.             index = targets.indexOf(subject);
  305.             if (index >= 0) { targets.splice(index, 1); }
  306.         } while (targets.length < num);
  307.         return targets;
  308.     }; // GA.targetsForReversedExcludeSelf
  309.  
  310.     CE.Game_BattlerBase = {};
  311.     var GBB = CE.Game_BattlerBase;
  312.  
  313.     /*------------------------------------------------------------------------
  314.      *    Returns the queried notetag value of the 1st found effective notetag
  315.      *------------------------------------------------------------------------*/
  316.     // flag: The reverse/excludeSelf notetag value
  317.     GBB.isConfusionEdit = function(flag) {
  318.         if (!this.isConfused() || !this._states) { return false; }
  319.         var state = this.states().filter(function(state) {
  320.             return state.meta.confusionEdit !== undefined;
  321.         })[0];
  322.         return state && state.meta.confusionEdit[flag];
  323.     }; // GBB.isConfusionEdit
  324.  
  325.     CE.Game_Actor = {};
  326.  
  327.     CE.Game_Actor.makeActions = Game_Actor.prototype.makeActions;
  328.     Game_Actor.prototype.makeActions = function() {
  329.         // Added to let actors with the reverse flag to use all available skills
  330.         if (GBB.isConfusionEdit.call(this, "reverse")) {
  331.             Game_Battler.prototype.makeActions.call(this);
  332.             return this.makeAutoBattleActions();
  333.         }
  334.         //
  335.         CE.Game_Actor.makeActions.apply(this, arguments);
  336.     }; // Game_Actor.prototype.makeActions
  337.  
  338. })(DoubleX_RMMV.Confusion_Edit);
  339.  
  340. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement