Advertisement
Double_X

DoubleX RMMV State Triggers v102a

Oct 30th, 2015 (edited)
553
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 State Triggers                                          
  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. Decent Javascript coding proficiency to fully utilize this plugin  
  16.  *----------------------------------------------------------------------------
  17.  *    # Links                                                                
  18.  *      This plugin:                                                          
  19.  *      1. http://pastebin.com/EatwNufM                                      
  20.  *      Mentioned Patreon Supporters:
  21.  *      https://www.patreon.com/posts/71738797
  22.  *----------------------------------------------------------------------------
  23.  *    # Author                                                                
  24.  *      DoubleX                                                              
  25.  *----------------------------------------------------------------------------
  26.  *    # Changelog                                                            
  27.  *      v1.02a(GMT 0200 14-5-2016):                                          
  28.  *      1. Added the timing while to <timing state trigger: STCX, STAX>      
  29.  *      v1.01a(GMT 1300 26-2-2016):                                          
  30.  *      1. STCX and STAX take the state using them as an argument as well    
  31.  *      v1.00g(GMT 0400 25-12-2015):                                          
  32.  *      1. The aliased functions can be accessed by other custom plugins now  
  33.  *      2. Exposed the state plugin calls that can access the notetag values  
  34.  *      3. Increased this plugin's compactness, compatibility and readability
  35.  *      v1.00f(GMT 1500 6-11-2015):                                          
  36.  *      1. Fixed undefined this in forEach bug                                
  37.  *      v1.00e(GMT 1400 6-11-2015):                                          
  38.  *      1. Simplified the notetag reading mechanisms                          
  39.  *      2. Fixed some typos                                                
  40.  *      v1.00d(GMT 1100 5-11-2015):                                          
  41.  *      1. Fixed undefined this under DoubleX_RMMV.State_Triggers bug        
  42.  *      v1.00c(GMT 0000 5-11-2015):                                          
  43.  *      1. Fixed failing to load notetags due to nil $dataStates bug          
  44.  *      v1.00b(GMT 1000 4-11-2015):                                          
  45.  *      1. Fixed several logic and syntax errors                              
  46.  *      2. Increased this plugin's maintainability                            
  47.  *      v1.00a(GMT 1500 30-10-2015):                                          
  48.  *      1. 1st version of this plugin finished                                
  49.  *============================================================================*/
  50. /*:
  51.  * @plugindesc Sets some states to trigger some actions when conditions are met
  52.  * @author DoubleX
  53.  *
  54.  * @help
  55.  * You're supposed to edit the plugin js file directly to set notetag values
  56.  *============================================================================
  57.  *    ## Notetag Info                                                        
  58.  *----------------------------------------------------------------------------
  59.  *    # State Notetags:                                                      
  60.  *      1. <timing state trigger: STCX, STAX>                                
  61.  *         - Sets a state to trigger STAX when timing and STCX are met        
  62.  *         - timing can be add, turn, remove or custom timings set by you    
  63.  *         - add means the state's just added                                
  64.  *         - turn means the state's remaining turn's just reduced by 1        
  65.  *         - remove means the state's just removed                            
  66.  *         - (v1.02a+)while means the STAX effects are active as long as the  
  67.  *           state's active                                                  
  68.  *         - timing must only consist of alphanumeric characters              
  69.  *         - STCX can be set in State Trigger Condition Functions            
  70.  *         - STAX can be set in State Trigger Action Functions                
  71.  *============================================================================
  72.  *    ## Plugin Call Info                                                    
  73.  *----------------------------------------------------------------------------
  74.  *    # Configuration manipulations                                          
  75.  *      1. DoubleX_RMMV.State_Triggers.prop                                  
  76.  *         - Returns the property prop under DoubleX_RMMV.State_Triggers      
  77.  *      2. DoubleX_RMMV.State_Triggers.prop = val                            
  78.  *         - Sets the property prop under DoubleX_RMMV.State_Triggers as a    
  79.  *           function which will be bound to the battler upon use            
  80.  *    # State manipulations                                                  
  81.  *      All meta.stateTriggers changes can be saved if                        
  82.  *      DoubleX RMMV Dynamic Data is used                                    
  83.  *      1. meta.stateTriggers[timing]                                        
  84.  *         - Returns the array of all STCX-STAX pairs of timing timing        
  85.  *      2. meta.stateTriggers[timing] = [[STCX, STAX], [STCX, STAX], ...]    
  86.  *         - Adds a new timing with some STCX-STAX pairs or overwrites all the
  87.  *           existing ones with those pairs if timing is an existing timing  
  88.  *      3. meta.stateTriggers[timing][i] = [STCX, STAX]                      
  89.  *         - Set the ith STCX-STAX pair as the new STCX-STAX pair            
  90.  *    # Battler manipulations                                                
  91.  *      (v1.02a+)1. GBB.stateTriggerTraits.call(battler, state, timing)      
  92.  *         - Collects all traits from all STAX meeting their corresponding    
  93.  *           STCX with timing timing of state applied to battler              
  94.  *         - GBB is DoubleX_RMMV.State_Triggers.Game_BattlerBase              
  95.  *      2. GBB.execStateTriggers.call(battler, stateId, timing)              
  96.  *         - Executes all state triggers with timing timing of state with id  
  97.  *           stateId applied to battler                                      
  98.  *         - GBB is DoubleX_RMMV.State_Triggers.Game_BattlerBase              
  99.  *============================================================================
  100.  */
  101.  
  102. 'use strict';
  103. var DoubleX_RMMV = DoubleX_RMMV || {};
  104. DoubleX_RMMV["State Triggers"] = 'v1.02a';
  105.  
  106. /*============================================================================
  107.  *    ## Plugin Configurations                                                
  108.  *       You only need to edit this part as it's about what this plugin does  
  109.  *----------------------------------------------------------------------------*/
  110.  
  111. DoubleX_RMMV.State_Triggers = {
  112.  
  113.     /*------------------------------------------------------------------------
  114.      *    State Trigger Condition Functions                                  
  115.      *    - Setups STCX used by this plugin's notetags                        
  116.      *------------------------------------------------------------------------*/
  117.     /* STCX are used at:
  118.      * 1. DoubleX_RMMV.State_Triggers.Game_BattlerBase
  119.      *    - return ST[trigger[0]].call(battler, state); in stateTriggerTraits
  120.      *    - if (ST[trigger[0]].call(this)) { ST[trigger[1]].call(this); } in
  121.      *      execStateTriggers
  122.      * STCX are Javascript functions which will be bound to the battler upon use
  123.      * STCX names can only use alphanumeric characters
  124.      * state is the state using the STCX
  125.      * The below STCX are examples added to help you set your STCX
  126.      * You can freely use, rewrite and/or delete these examples
  127.      */
  128.  
  129.     // Sets the state trigger condition as always true
  130.     STC1: function(state) { return true; },
  131.  
  132.     // Sets the state trigger condition as needing switch with id x to be on
  133.     STC2: function(state) { return $gameSwitches.value(x); },
  134.  
  135.     // Sets the state trigger condition as always true
  136.     STC3: function(state) { return false; },
  137.  
  138.     // Adds new STCX here
  139.    
  140.  
  141.     /*------------------------------------------------------------------------
  142.      *    State Trigger Action Values                                        
  143.      *    - Setups STAX used by this plugin's notetags                        
  144.      *------------------------------------------------------------------------*/
  145.     /* STAX are used at:
  146.      * 1. DoubleX_RMMV.State_Triggers.Game_BattlerBase
  147.      *    - return r.concat(ST[trigger[1]].call(battler, state)); in
  148.      *      stateTriggerTraits
  149.      *    - if (ST[trigger[0]].call(this)) { ST[trigger[1]].call(this); } in
  150.      *      execStateTriggers
  151.      * STAX are Javascript functions which will be bound to the battler upon use
  152.      * STAX names can only use alphanumeric characters
  153.      * state is the state using the STAX
  154.      * If the timing using the STAX is while, the STAX must return an array of
  155.      * Trait Class
  156.      * You can refer to Game_BattlerBase in rpg_objects.js and Trait Class in
  157.      * the RMMV help html
  158.      * The below STAX are examples added to help you set your STAX
  159.      * You can freely use, rewrite and/or delete these examples
  160.      */
  161.  
  162.     /* Sets the state trigger action as what Special Effect Escape does
  163.      * This STAX's not supposed to work with the timing while as it doesn't
  164.      * return an array of Trait Class
  165.      */
  166.     STA1: function(state) { this.hide(); },
  167.  
  168.     /* Sets the state trigger action as setting the battler's hp to full
  169.      * This STAX's not supposed to work with the timing while as it doesn't
  170.      * return an array of Trait Class
  171.      */
  172.     STA2: function(state) { this._hp = this.mhp; },
  173.  
  174.     /* Sets the state trigger action as ultiplying the battler's atk by x * 100%
  175.      * This STAX's supposed to work with the timing while as it returns an array
  176.      * of Trait Class
  177.      */
  178.     STA3: function(state) { return [{"code":21,"dataId":2,"value":x}]; }
  179.  
  180.     // Adds new STAX here
  181.    
  182.  
  183. }; // DoubleX_RMMV.State_Triggers
  184.  
  185. /*============================================================================
  186.  *    ## Plugin Implementations                                              
  187.  *       You need not edit this part as it's about how this plugin works      
  188.  *----------------------------------------------------------------------------
  189.  *    # Plugin Support Info:                                                  
  190.  *      1. Prerequisites                                                      
  191.  *         - Some Javascript coding proficiency to fully comprehend this      
  192.  *           plugin                                                          
  193.  *      2. Function documentation                                            
  194.  *         - The 1st part describes why this function's rewritten/extended for
  195.  *           rewritten/extended functions or what the function does for new  
  196.  *           functions                                                        
  197.  *         - The 2nd part describes what the arguments of the function are    
  198.  *         - The 3rd part informs which version rewritten, extended or created
  199.  *           this function                                                    
  200.  *         - The 4th part informs whether the function's rewritten or new    
  201.  *         - The 5th part informs whether the function's a real or potential  
  202.  *           hotspot                                                          
  203.  *         - The 6th part describes how this function works for new functions
  204.  *           only, and describes the parts added, removed or rewritten for    
  205.  *           rewritten or extended functions only                            
  206.  *         Example:                                                          
  207.  * /*----------------------------------------------------------------------
  208.  *  *    Why rewrite/extended/What this function does                      
  209.  *  *----------------------------------------------------------------------*/
  210. /* // arguments: What these arguments are                                    
  211.  * functionName = function(arguments) { // Version X+; Hotspot                
  212.  *     // Added/Removed/Rewritten to do something/How this function works    
  213.  *     functionContents                                                      
  214.  *     //                                                                    
  215.  * } // functionName                                                          
  216.  *----------------------------------------------------------------------------*/
  217.  
  218. (function(ST) {
  219.  
  220.     ST.DataManager = {};
  221.     var DM = ST.DataManager;
  222.  
  223.     DM.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  224.     DataManager.isDatabaseLoaded = function() {
  225.         // Rewritten
  226.         return DM.isDatabaseLoaded.apply(this, arguments) && DM.loadAllNotes();
  227.         //
  228.     }; // DataManager.isDatabaseLoaded
  229.  
  230.     DM.loadAllNotes = function() {
  231.         $dataStates.forEach(function(data) {
  232.             if (data) { DM.loadStateNotes(data); }
  233.         });
  234.         return true;
  235.     }; // DM.loadAllNotes
  236.  
  237.     // data: The data to have its notetags read
  238.     DM.loadStateNotes = function(data) {
  239.         data.meta.stateTriggers = {};
  240.         var timing, triggers = data.meta.stateTriggers;
  241.         var regExp = /< *(\w+) +state +trigger *: *(\w+) *, *(\w+) *>/i;
  242.         data.note.split(/[\r\n]+/).forEach(function(line) {
  243.             if (!line.match(regExp)) { return; }
  244.             timing = RegExp.$1;
  245.             triggers[timing] = triggers[timing] || [];
  246.             triggers[timing].push([RegExp.$2, RegExp.$3]);
  247.         });
  248.     }; // DM.loadStateNotes
  249.  
  250.     ST.Game_BattlerBase = {};
  251.     var GBB = ST.Game_BattlerBase;
  252.  
  253.     GBB.clearStates = Game_BattlerBase.prototype.clearStates;
  254.     Game_BattlerBase.prototype.clearStates = function() {
  255.         // Added to store the state array right before it's cleared
  256.         var lastStates = this._states;
  257.         //
  258.         GBB.clearStates.apply(this, arguments);
  259.         // Added to trigger the remove actions if the remove conditions are met
  260.         if (!lastStates) { return; }
  261.         lastStates.forEach(function(stateId) {
  262.             GBB.execStateTriggers.call(this, stateId, 'remove');
  263.         }, this);
  264.         //
  265.     }; // Game_BattlerBase.prototype.clearStates
  266.  
  267.     GBB.eraseState = Game_BattlerBase.prototype.eraseState;
  268.     Game_BattlerBase.prototype.eraseState = function(stateId) {
  269.         // Added to store the state existence flag right before it's erased
  270.         var trigger = this._states.indexOf(stateId) >= 0;
  271.         //
  272.         GBB.eraseState.apply(this, arguments);
  273.         // Added to trigger the remove actions if the remove conditions are met
  274.         if (trigger) { GBB.execStateTriggers.call(this, stateId, 'remove'); }
  275.         //
  276.     }; // Game_BattlerBase.prototype.eraseState
  277.  
  278.     GBB.resetStateCounts = Game_BattlerBase.prototype.resetStateCounts;
  279.     Game_BattlerBase.prototype.resetStateCounts = function(stateId) {
  280.         GBB.resetStateCounts.apply(this, arguments);
  281.         // Added to trigger the add actions if the add conditions are met
  282.         if (this._states.indexOf(stateId) < 0) { return; }
  283.         GBB.execStateTriggers.call(this, stateId, 'add');
  284.         //
  285.     }; // Game_BattlerBase.prototype.resetStateCounts
  286.  
  287.     GBB.updateStateTurns = Game_BattlerBase.prototype.updateStateTurns;
  288.     Game_BattlerBase.prototype.updateStateTurns = function() {
  289.         GBB.updateStateTurns.apply(this, arguments);
  290.         // Added to trigger the turn actions if the turn conditions are met
  291.         this._states.forEach(function(stateId) {
  292.             GBB.execStateTriggers.call(this, stateId, 'turn');
  293.         }, this);
  294.         //
  295.     }; // Game_BattlerBase.prototype.updateStateTurns
  296.  
  297.     GBB.allTraits = Game_BattlerBase.prototype.allTraits;
  298.     Game_BattlerBase.prototype.allTraits = function() { // v1.02a+
  299.         // Rewritten
  300.         var stateTriggerTraits = GBB.allStateTriggerTraits.call(this);
  301.         return stateTriggerTraits.concat(GBB.allTraits.apply(this, arguments));
  302.         //
  303.     }; // Game_BattlerBase.prototype.allTraits
  304.  
  305.     GBB.allStateTriggerTraits = function() { // v1.02a+
  306.         var battler = this;
  307.         return this.states().reduce(function(r, obj) {
  308.             return r.concat(GBB.stateTriggerTraits.call(battler, obj, 'while'));
  309.         }, []);
  310.     }; // GBB.allStateTriggerTraits
  311.    
  312.     /* state: The state triggering its actions
  313.      * timing: The timing of the state triggering its actions
  314.      */
  315.     GBB.stateTriggerTraits = function(state, timing) { // v1.02a+
  316.         var triggers = state.meta.stateTriggers[timing];
  317.         if (!triggers) { return []; }
  318.         // Collects all traits returned by STAX meeting STCX with timing timing
  319.         var battler = this;
  320.         return triggers.filter(function(trigger) {
  321.             return ST[trigger[0]].call(battler, state);
  322.         }).reduce(function(r, trigger) {
  323.             return r.concat(ST[trigger[1]].call(battler, state));
  324.         }, []);
  325.         //
  326.     }; // GBB.stateTriggerTraits
  327.  
  328.     /*------------------------------------------------------------------------
  329.      *    Triggers each state action when each respective condition's met    
  330.      *------------------------------------------------------------------------*/
  331.     /* stateId: The id of the state triggering its actions
  332.      * timing: The timing of the state triggering its actions
  333.      */
  334.     GBB.execStateTriggers = function(stateId, timing) {
  335.         var state = $dataStates[stateId];
  336.         var triggers = state.meta.stateTriggers[timing];
  337.         if (!triggers) { return; }
  338.         // Calls each STCX to see if its paired STAX should be called as well
  339.         triggers.forEach(function(trigger) {
  340.             if (!ST[trigger[0]].call(this, state)) { return; }
  341.             ST[trigger[1]].call(this, state);
  342.         }, this);
  343.         //
  344.     }; // GBB.execStateTriggers
  345.  
  346. })(DoubleX_RMMV.State_Triggers);
  347.  
  348. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement