Advertisement
Double_X

DoubleX RMMV Linked Battlers v100d

Dec 23rd, 2015 (edited)
526
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 Linked Battlers                                          
  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. Some Javascript coding proficiency to fully utilize this plugin    
  16.  *----------------------------------------------------------------------------
  17.  *    # Links                                                                
  18.  *      This plugin:                                                          
  19.  *      1. http://pastebin.com/wemL2xFW                                      
  20.  *      Mentioned Patreon Supporters:
  21.  *      https://www.patreon.com/posts/71738797
  22.  *----------------------------------------------------------------------------
  23.  *    # Author                                                                
  24.  *      DoubleX                                                              
  25.  *----------------------------------------------------------------------------
  26.  *    # Changelog                                                            
  27.  *      v1.00d(GMT 1500 29-1-2016):                                          
  28.  *      1. Fixed undefined g in Proto[func[0]] bug                            
  29.  *      2. Fixed passing getter instead of its name to linkedBattlersStateId  
  30.  *      3. Fixed calling getter via this[getter] instead of this.getter      
  31.  *      4. Fixed state.linkedBattlers instead of state.meta.linkedBattlers bug
  32.  *      5. Fixed adding 2 Arrays instead of concatenating them bug            
  33.  *      6. Fixed undefined battler isStateAdded bug                          
  34.  *      7. Fixed linked battlers' stats being possible to be non-integer bug  
  35.  *      8. Only the target will collapse when some others die at the same time
  36.  *      v1.00c(GMT 1400 31-12-2015):                                          
  37.  *      1. Fixed writing getter contents directly instead of using String bug
  38.  *      v1.00b(GMT 1100 28-12-2015):                                          
  39.  *      1. Fixed unintentionally declaring global variable in strict mode bug
  40.  *      2. Fixed using new Function and class instead of eval and prototype  
  41.  *      v1.00a(GMT 1400 23-12-2015):                                          
  42.  *      1. 1st version of this plugin finished                                
  43.  *============================================================================*/
  44. /*:
  45.  * @plugindesc Lets users set some states to let their owners share some stats
  46.  * @author DoubleX
  47.  *
  48.  * @help
  49.  * You're supposed to edit the plugin js file directly to set notetag values
  50.  *============================================================================
  51.  *    ## Notetag Info                                                        
  52.  *----------------------------------------------------------------------------
  53.  *    # State Notetags:                                                      
  54.  *      1. <linked battlers: LBCX, LBSX, LBWX>                                
  55.  *         - Sets all owners of this state meeting LBCX to share stats        
  56.  *           included in LBSX with weight LBWX applied to each of them when  
  57.  *           any included stat of any included battler changes                
  58.  *         - Only the 1st applicable notetag of the state with the highest    
  59.  *           priority will be applied to the stat change of the linked battler
  60.  *         - If a linked battler can't take all of that battler's share of the
  61.  *           original stat change due to hitting the min/max stat value, thost
  62.  *           not being taken by that battler will be shared by the rest of the
  63.  *           linked battlers also                                            
  64.  *         - The battler originally having a stat to be changed is the last  
  65.  *           linked battler taking the change not shared by any other ones    
  66.  *         - LBCX can be set in Linked Battler Condition Functions            
  67.  *         - LBSX can be set in Linked Battler Stat Functions                
  68.  *         - LBWX can be set in Linked Battler Weight Functions              
  69.  *============================================================================
  70.  *    ## Plugin Call Info                                                    
  71.  *----------------------------------------------------------------------------
  72.  *    # Configuration manipulations                                          
  73.  *      1. DoubleX_RMMV.Linked_Battlers.prop                                  
  74.  *         - Returns the property prop under DoubleX_RMMV.Linked_Battlers    
  75.  *      2. DoubleX_RMMV.Linked_Battlers.prop = function                      
  76.  *         - Sets the property prop under DoubleX_RMMV.Linked_Battlers as    
  77.  *           function which will be bound to the battler upon use            
  78.  *         - No DoubleX_RMMV.Linked_Battlers.prop change will be saved        
  79.  *    # State manipulations                                                  
  80.  *      All meta.linkedBattlers changes can be saved if                      
  81.  *      DoubleX RMMV Dynamic Data is used                                    
  82.  *      1. meta.linkedBattlers                                                
  83.  *         - Returns an Array of all LBCX-LBSX-LBWX triples, each in the form
  84.  *           of [LBCX, LBSX, LBWX]                                            
  85.  *      2. meta.linkedBattlers = [[LBCX, LBSX, LBWX], [LBCX, LBSX, LBWX], ...]
  86.  *         - Resets the Array of all LBCX-LBSX-LBWX triples, each in the form
  87.  *           of [LBCX, LBSX, LBWX], stored sequentially in that Array        
  88.  *      3. meta.linkedBattlers[i] = [LBCX, LBSX, LBWX]                        
  89.  *         - Sets the (i + 1)th LBCX-LBSX-LBWX triple as LBCX-LBSX-LBWX      
  90.  *============================================================================
  91.  */
  92.  
  93. "use strict";
  94. var DoubleX_RMMV = DoubleX_RMMV || {};
  95. DoubleX_RMMV["Linked Battlers"] = "v1.00d";
  96.  
  97. /*============================================================================
  98.  *    ## Plugin Configurations                                                
  99.  *       You only need to edit this part as it's about what this plugin does  
  100.  *----------------------------------------------------------------------------*/
  101.  
  102. DoubleX_RMMV.Linked_Battlers = {
  103.  
  104.     /*------------------------------------------------------------------------
  105.      *    Linked Battler Condition Functions                                  
  106.      *    - Setups LBCX used by <linked battlers: LBCX, LBSX, LBWX>          
  107.      *------------------------------------------------------------------------*/
  108.     /* LBCX are used in functions included in LINKED_STATS
  109.      * LBCX are functions that will be bound to battler calling them upon use
  110.      * LBCX names can only use alphanumeric characters
  111.      * Each linked battler besides the caller can be referenced by battler
  112.      * The caller will always pass LBCX even when it's supposed to fail
  113.      * The below LBCX are examples added to help you set your LBCX
  114.      * You can freely use, rewrite and/or delete these examples
  115.      */
  116.  
  117.     // Sets the linked battler condition to include all linked battlers
  118.     LBC1: function(battler) { return true; },
  119.  
  120.     /* Sets the linked battler condition to include all and no linked battlers
  121.      * if switch with id x is on and off respectively
  122.      */
  123.     LBC2: function(battler) { return $gameSwitches.value(x); },
  124.  
  125.     // Adds new LBCX here
  126.    
  127.  
  128.     /*------------------------------------------------------------------------
  129.      *    Linked Battler Stat Functions                                      
  130.      *    - Setups LBSX used by <linked battlers: LBCX, LBSX, LBWX>          
  131.      *------------------------------------------------------------------------*/
  132.     /* LBSX are used in functions included in LINKED_STATS
  133.      * LBSX are functions that will be bound to battlers upon use
  134.      * LBSX names can only use alphanumeric characters
  135.      * It must return an Array, which should include all strings of getter
  136.      * functions of each stat to be included
  137.      * The below LBSX are examples added to help you set your LBSX
  138.      * You can freely use, rewrite and/or delete these examples
  139.      */
  140.  
  141.     // Sets the linked battler stat to include hp, mp and tp
  142.     LBS1: function() { return ["hp", "mp", "tp"]; },
  143.  
  144.     // Sets the linked battler stat to include nothing
  145.     LBS2: function() { return []; },
  146.  
  147.     // Adds new LBSX here
  148.    
  149.  
  150.     /*------------------------------------------------------------------------
  151.      *    Linked Battler Weight Functions                                    
  152.      *    - Setups LBWX used by <linked battlers: LBCX, LBSX, LBWX>          
  153.      *------------------------------------------------------------------------*/
  154.     /* LBWX are used in functions included in LINKED_STATS
  155.      * LBWX are functions that will be bound to battlers upon use
  156.      * LBWX names can only use alphanumeric characters
  157.      * It must return a Number
  158.      * No stat change will take place for any linked battler if the sum of all
  159.      * weights of all linked battlers is 0
  160.      * Each linked battler besides the caller can be referenced by battler
  161.      * The below LBWX are examples added to help you set your LBWX
  162.      * You can freely use, rewrite and/or delete these examples
  163.      */
  164.  
  165.     // Sets the linked battler weight to be the same for all linked battlers
  166.     LBW1: function(battler) { return 1; },
  167.  
  168.     /* Sets the linked battler weight to be multiplied by x if the linked
  169.      * battler is the one having a stat to be changed
  170.      */
  171.     LBW2:  function(battler) { return battler === this ? x : 1; },
  172.  
  173.     // Adds new LBWX here
  174.    
  175.  
  176.     /* Sets the battler functions to be used by linked battlers
  177.      * Its property names must be the battler stat getter function names
  178.      * Its values must be Arrays, each containing the battler stat setter
  179.      * function name, the index of the argument as the original new stat value
  180.      * in the stat setter function argument list, and each linked battler's
  181.      * min/max stat value
  182.      * All battler functions as min/max stat value must be referenced by this
  183.      * All the included battler stat getter functions will be extended
  184.      */
  185.     LINKED_STATS: {
  186.  
  187.       /* General form:
  188.        * FunctionClass: {
  189.        *     getter: ["setter", statArgIndex, "statMin", "statMax"]
  190.        * }
  191.        */
  192.  
  193.       Game_BattlerBase: {
  194.         /* General form:
  195.          * getter: ["setter", statArgIndex, "statMin", "statMax"]
  196.          */
  197.         hp: ["setHp", 0, "0", "this.mhp"],
  198.         mp: ["setMp", 0, "0", "this.mmp"],
  199.         tp: ["setTp", 0, "0", "this.maxTp()"]
  200.         // Adds new functions here
  201.        
  202.       }
  203.  
  204.       // Adds new classes here
  205.      
  206.  
  207.     }
  208.  
  209. }; // DoubleX_RMMV.Linked_Battlers
  210.  
  211. /*============================================================================
  212.  *    ## Plugin Implementations                                              
  213.  *       You need not edit this part as it's about how this plugin works      
  214.  *----------------------------------------------------------------------------
  215.  *    # Plugin Support Info:                                                  
  216.  *      1. Prerequisites                                                      
  217.  *         - Decent Javascript coding proficiency to fully comprehend this    
  218.  *           plugin                                                          
  219.  *      2. Function documentation                                            
  220.  *         - The 1st part describes why this function's rewritten/extended for
  221.  *           rewritten/extended functions or what the function does for new  
  222.  *           functions                                                        
  223.  *         - The 2nd part describes what the arguments of the function are    
  224.  *         - The 3rd part informs which version rewritten, extended or created
  225.  *           this function                                                    
  226.  *         - The 4th part informs whether the function's rewritten or new    
  227.  *         - The 5th part informs whether the function's a real or potential  
  228.  *           hotspot                                                          
  229.  *         - The 6th part describes how this function works for new functions
  230.  *           only, and describes the parts added, removed or rewritten for    
  231.  *           rewritten or extended functions only                            
  232.  *         Example:                                                          
  233.  * /*----------------------------------------------------------------------
  234.  *  *    Why rewrite/extended/What this function does                      
  235.  *  *----------------------------------------------------------------------*/
  236. /* // arguments: What these arguments are                                    
  237.  * functionName = function(arguments) { // Version X+; Hotspot                
  238.  *     // Added/Removed/Rewritten to do something/How this function works    
  239.  *     functionContents                                                      
  240.  *     //                                                                    
  241.  * } // functionName                                                          
  242.  *----------------------------------------------------------------------------*/
  243.  
  244. (function(LB) {
  245.  
  246.     LB.DataManager = {};
  247.     var DM = LB.DataManager;
  248.  
  249.     DM.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  250.     DataManager.isDatabaseLoaded = function() {
  251.         // Rewritten
  252.         return DM.isDatabaseLoaded.apply(this, arguments) && DM.loadAllNotes();
  253.         //
  254.     }; // DataManager.isDatabaseLoaded
  255.  
  256.     DM.loadAllNotes = function() {
  257.         $dataStates.forEach(function(data) {
  258.             if (data) { DM.loadStateNotes(data); }
  259.         });
  260.         return true;
  261.     }; // DM.loadAllNotes
  262.  
  263.     // data: The data to have its notetags read
  264.     DM.loadStateNotes = function(data) {
  265.         var regExp = /< *linked +battlers *: *(\w+) *, *(\w+) *, *(\w+) *>/i;
  266.         data.meta.linkedBattlers = [];
  267.         var linkedBattlers = data.meta.linkedBattlers;
  268.         // Stores all LBCX-LBSX-LBWX triples in 1 single Array sequentially
  269.         data.note.split(/[\r\n]+/).forEach(function(line) {
  270.             if (!line.match(regExp)) { return; }
  271.             linkedBattlers.push([RegExp.$1, RegExp.$2, RegExp.$3]);
  272.         });
  273.         //
  274.     }; // DM.loadStateNotes
  275.  
  276.     LB.Game_BattlerBase = {};
  277.     var GBB = LB.Game_BattlerBase, gf = "getLinkedBattlers";
  278.  
  279.     GBB[gf] = function(stateId, cond) {
  280.         var aliveMems = $gameParty.aliveMembers();
  281.         return aliveMems.concat($gameTroop.aliveMembers()).filter(function(mem) {
  282.             return mem.isStateAffected(stateId) && LB[cond].call(this, mem);
  283.         }, this);
  284.     }; // GBB[gf]
  285.  
  286.     GBB.linkedBattlersStateId = function(stat) {
  287.         if (!this._states) { return null; }
  288.         var linkedBattlers, states = this.states();
  289.         // Returns the (1st applicable LBCX-LBSX-LBWX triple and state id) pair
  290.         for (var index = 0, length = states.length; index < length; index++) {
  291.             linkedBattlers = states[index].meta.linkedBattlers;
  292.             for (var i = 0, l = linkedBattlers.length; i < l; i++) {
  293.                 if (LB[linkedBattlers[i][1]].call(this).indexOf(stat) >= 0) {
  294.                     return [linkedBattlers[i], states[index].id];
  295.                 }
  296.             }
  297.         }
  298.         //
  299.         return null;
  300.     }; // GBB.linkedBattlersStateId
  301.  
  302.     var Proto, Klass, func;
  303.     for (var k in LB.LINKED_STATS) {
  304.         if (!LB.LINKED_STATS.hasOwnProperty(k)) { continue; }
  305.         LB[k] = LB[k] || {}; // Ensures container GBB won't be rewritten
  306.         Proto = eval(k + ".prototype"); // Actual class prototype
  307.         Klass = LB.LINKED_STATS[k]; // Class name as string
  308.         for (var g in Klass) {
  309.             if (!Klass.hasOwnProperty(g)) { continue; }
  310.             func = Klass[g]; // ["setter", statArgIndex, "statMin", "statMax"]
  311.  
  312.             /*----------------------------------------------------------------
  313.              *    Extends all stat setter functions to have shared stat change
  314.              *----------------------------------------------------------------*/
  315.             LB[k][func[0]] = Proto[func[0]];
  316.             Proto[func[0]] = new Function ([
  317.                 "var LB = DoubleX_RMMV.Linked_Battlers;",
  318.                 "var lBSI = LB.Game_BattlerBase.linkedBattlersStateId;",
  319.                 "this._linkedBattler = lBSI.call(this, '" + g + "');",
  320.                 "if (this._linkedBattler) {",
  321.                 "    return LB." + k + "." + gf + g + ".apply(this, arguments);",
  322.                 "}",
  323.                 "LB." + k + "." + func[0] + ".apply(this, arguments);",
  324.             ].join("\n")); // Proto[func[0]]
  325.  
  326.             /*----------------------------------------------------------------
  327.              *    Redistributes the stat change to all found linked battlers  
  328.              *----------------------------------------------------------------*/
  329.             LB[k][gf + g] = new Function ([
  330.                 "var LB = DoubleX_RMMV.Linked_Battlers;",
  331.                 "var GBB = LB.Game_BattlerBase;",
  332.                 "var lB = this._linkedBattler[0];",
  333.                 "var stateId = this._linkedBattler[1];",
  334.                 "var mems = GBB." + gf + ".call(this, stateId, lB[0]);",
  335.                 "var index = mems.indexOf(this);",
  336.                 "if (index >= 0) { mems.splice(index, 1); }",
  337.                 "mems.push(this);",
  338.                 "var weights = mems.map(function(mem) {",
  339.                 "    return LB[lB[2]].call(this, mem);",
  340.                 "}, this);",
  341.                 "var weightSum = weights.reduce(function(a, b) {",
  342.                 "    return a + b;",
  343.                 "}, 0);",
  344.                 "if (weightSum === 0) { return; }",
  345.                 "var statDiff = arguments[" + func[1] + "] - this." + g + ";",
  346.                 "var statChange, newStat, minStat, maxStat;",
  347.                 "for (var i = 0, l = mems.length; i < l; i++) {",
  348.                 "    statChange = statDiff * weights[i] / weightSum;",
  349.                 "    statChange = Math.trunc(statChange);",
  350.                 "    newStat = mems[i]." + g + " + statChange;",
  351.                 "    minStat = LB." + k + ".min" + g + ".call(mems[i]);",
  352.                 "    maxStat = LB." + k + ".max" + g + ".call(mems[i]);",
  353.                 "    if (newStat < minStat) {",
  354.                 "        statChange = mems[i]." + g + " - minStat;",
  355.                 "        arguments[" + func[1] + "] = minStat;",
  356.                 "    } else if (newStat > maxStat) {",
  357.                 "        statChange = maxStat - mems[i]." + g + ";",
  358.                 "        arguments[" + func[1] + "] = maxStat;",
  359.                 "    } else {",
  360.                 "        arguments[" + func[1] + "] = newStat;",
  361.                 "    }",
  362.                 "    LB." + k + "." + func[0] + ".apply(mems[i], arguments);",
  363.                 "    statDiff -= statChange;",
  364.                 "    weightSum -= weights[i];",
  365.                 "}",
  366.             ].join("\n")); // LB[k][gf + g]
  367.  
  368.             // Returns the minimum stat value
  369.             LB[k]["min" + g] = new Function("return " + func[2] + ";");
  370.  
  371.             // Returns the maximum stat value
  372.             LB[k]["max" + g] = new Function("return " + func[3] + ";");
  373.  
  374.         }
  375.     }
  376.  
  377. })(DoubleX_RMMV.Linked_Battlers);
  378.  
  379. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement