Advertisement
Double_X

DoubleX RMMV Permanent States v102c

Oct 31st, 2015 (edited)
540
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 Permanent States                                        
  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/avCcChi6                                      
  20.  *      Mentioned Patreon Supporters:
  21.  *      https://www.patreon.com/posts/71738797
  22.  *----------------------------------------------------------------------------
  23.  *    # Author                                                                
  24.  *      DoubleX                                                              
  25.  *----------------------------------------------------------------------------
  26.  *    # Changelog                                                            
  27.  *      v1.02c(GMT 0100 4-12-2015):                                          
  28.  *      1. Fixed undefined all array function which should be empty instead  
  29.  *      2. Increased this plugin's compactness, efficiency and robustness    
  30.  *      v1.02b(GMT 0800 2-12-2015):                                          
  31.  *      1. Fixed reading repetitive notetag values due to typo bug            
  32.  *      2. Fixed permanent state type persist not working due to typo bug    
  33.  *      3. Fixed null permanent state container due to wrong init timing bug  
  34.  *      4. Fixed undefined $gameParty upon actor initializations bug          
  35.  *      v1.02a(GMT 1400 1-12-2015):                                          
  36.  *      1. <permanent state: type> has changed to                            
  37.  *         <permanent state: scene, type>                                    
  38.  *      2. Added new permanent state type recover restored upon recover all  
  39.  *      3. The same state can now have more than 1 permanent state type      
  40.  *      4. Removed param allStatesPermanent in the plugin manager            
  41.  *      5. Fixed undefined permanent state containers upon loading savefiles  
  42.  *      v1.01h(GMT 0600 29-11-2015):                                          
  43.  *      1. Fixed clearing all states upon calling Recover All event command  
  44.  *      2. Fixed notetags not reading at all due to treating "null" as falsey
  45.  *      v1.01g(GMT 0300 29-11-2015):                                          
  46.  *      1. Fixed undefined error due to non persist nor revive poermanent type
  47.  *      v1.01f(GMT 1200 27-11-2015):                                          
  48.  *      1. Fixed undefined state_id in store_permanent_states due to typo bug
  49.  *      2. Increased this plugin's efficiency, readability and robustness    
  50.  *      v1.01e(GMT 0900 25-11-2015):                                          
  51.  *      1. Fixed wrong number of arguments when loading state notetags bug    
  52.  *      2. The aliased functions can be accessed by other custom plugins now  
  53.  *      3. Exposed the state plugin calls that can access the notetag values  
  54.  *      v1.01d(GMT 1600 6-11-2015):                                          
  55.  *      1. Simplified the notetag reading mechanisms                          
  56.  *      2. Fixed undefined this under forEach bug                            
  57.  *      v1.01c(GMT 1300 5-11-2015):                                          
  58.  *      1. Fixed undefined this under DoubleX_RMMV.Permanent_States bug      
  59.  *      v1.01b(GMT 0000 5-11-2015):                                          
  60.  *      1. Fixed failing to load notetags due to nil $dataStates bug          
  61.  *      v1.01a(GMT 1300 4-11-2015):                                          
  62.  *      1. The notetag <permanent state> has changed to <permanent state: type>
  63.  *      2. Fixed several logic and syntax errors                              
  64.  *      3. Increased this plugin's maintainability                            
  65.  *      v1.00a(GMT 0800 31-10-2015):                                          
  66.  *      1. 1st version of this plugin finished                                
  67.  *============================================================================*/
  68. /*:
  69.  * @plugindesc Lets you sets some states to be kept/restored in some set cases
  70.  * @author DoubleX
  71.  *
  72.  * @help
  73.  *============================================================================
  74.  *    ## Notetag Info                                                        
  75.  *----------------------------------------------------------------------------
  76.  *    # State Notetags:                                                      
  77.  *      1. <permanent state: scene, type>                                    
  78.  *         - scene can be either battle or map, meaning the state can be      
  79.  *           restored in battle or map respectively                          
  80.  *         - type can be either persist, recover or revive, meaning the state
  81.  *           will be kept upon death or recover all, or added back upon revive
  82.  *           respectively                                                    
  83.  *         - The same state can have more than 1 permanent state scene/type  
  84.  *============================================================================
  85.  *    ## Plugin Call Info                                                    
  86.  *----------------------------------------------------------------------------
  87.  *    # State manipulations                                                  
  88.  *      1. meta.permanentStates                                              
  89.  *         - Returns the permanent state scenes and types in                  
  90.  *           <permanent state: scene, type> in the form of                    
  91.  *           { battle: types, map: types } with types being an Array storing  
  92.  *           all unique String type or an empty Array if scene matches and    
  93.  *           doesn't match the property name storing types respectively      
  94.  *      2. meta.permanentStates = { battle: types, map: types }              
  95.  *         - Sets the permanent state scene and types in                      
  96.  *           <permanent state: type> as { battle: types, map: types } with    
  97.  *           types being an Array storing all unique String type which will be
  98.  *           used in the scene matching the property name storing types      
  99.  *         - types can only include persist, recover and revive              
  100.  *         - All meta.permanentStates changes can be saved if                
  101.  *           DoubleX RMMV Dynamic Data is used                                
  102.  *============================================================================
  103.  */
  104.  
  105. "use strict";
  106. var DoubleX_RMMV = DoubleX_RMMV || {};
  107. DoubleX_RMMV["Permanent States"] = "v1.02c";
  108.  
  109. /*============================================================================
  110.  *    ## Plugin Implementations                                              
  111.  *       You need not edit this part as it's about how this plugin works      
  112.  *----------------------------------------------------------------------------
  113.  *    # Plugin Support Info:                                                  
  114.  *      1. Prerequisites                                                      
  115.  *         - Some Javascript coding proficiency to fully comprehend this      
  116.  *           plugin                                                          
  117.  *      2. Function documentation                                            
  118.  *         - The 1st part describes why this function's rewritten/extended for
  119.  *           rewritten/extended functions or what the function does for new  
  120.  *           functions                                                        
  121.  *         - The 2nd part describes what the arguments of the function are    
  122.  *         - The 3rd part informs which version rewritten, extended or created
  123.  *           this function                                                    
  124.  *         - The 4th part informs whether the function's rewritten or new    
  125.  *         - The 5th part informs whether the function's a real or potential  
  126.  *           hotspot                                                          
  127.  *         - The 6th part describes how this function works for new functions
  128.  *           only, and describes the parts added, removed or rewritten for    
  129.  *           rewritten or extended functions only                            
  130.  *         Example:                                                          
  131.  * /*----------------------------------------------------------------------
  132.  *  *    Why rewrite/extended/What this function does                      
  133.  *  *----------------------------------------------------------------------*/
  134. /* // arguments: What these arguments are                                    
  135.  * function_name = function(arguments) { // Version X+; Hotspot              
  136.  *     // Added/Removed/Rewritten to do something/How this function works    
  137.  *     function_name_code                                                    
  138.  *     //                                                                    
  139.  * } // function_name                                                        
  140.  *----------------------------------------------------------------------------*/
  141.  
  142. DoubleX_RMMV.Permanent_States = {
  143.  
  144.     scene: function() { // v1.02a+
  145.         // Returns map upon actor initializations as well
  146.         return $gameParty && $gameParty.inBattle() ? "battle" : "map";
  147.         //
  148.     },
  149.  
  150.     // (v1.02a+)Stores all permanent state scenes
  151.     scenes: ["battle", "map"],
  152.  
  153.     // (v1.02a+)Stores all permanent state types
  154.     types: ["persist", "recover", "revive"]
  155.  
  156. }; // DoubleX_RMMV.Permanent_States
  157.  
  158. (function(PS) {
  159.  
  160.     PS.DataManager = {};
  161.  
  162.     PS.DataManager.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  163.     DataManager.isDatabaseLoaded = function() {
  164.         // Rewritten
  165.         if (!PS.DataManager.isDatabaseLoaded.apply(this, arguments)) {
  166.             return false;
  167.         }
  168.         PS.DataManager.loadAllPermanentStateNotes();
  169.         return true;
  170.         //
  171.     }; // DataManager.isDatabaseLoaded
  172.  
  173.     PS.DataManager.loadAllPermanentStateNotes = function() {
  174.         $dataStates.forEach(function(data) {
  175.             if (data) { PS.DataManager.loadPermanentStateNotes(data); }
  176.         });
  177.     }; // loadAllPermanentStateNotes
  178.  
  179.     // data: The data to have its notetags read
  180.     PS.DataManager.loadPermanentStateNotes = function(data) {
  181.         var ps, scene, type, lines = data.note.split(/[\r\n]+/);
  182.         var regex = /< *permanent +state *: *(\w+) *, *(\w+) *>/i;
  183.         var scenes = PS.scenes, types = PS.types;
  184.         data.meta.permanentStates = {};
  185.         ps = data.meta.permanentStates;
  186.         scenes.forEach(function(s) { ps[s] = []; });
  187.         // Reads all valid unique combinations until all of them are read
  188.         for (var index = 0, length = lines.length; index < length; index++) {
  189.             if (!lines[index].match(regex)) { continue; }
  190.             scene = RegExp.$1;
  191.             if (scenes.indexOf(scene) < 0) { continue; }
  192.             type = RegExp.$2;
  193.             if (types.indexOf(type) < 0 || ps[scene].indexOf(type) >= 0) {
  194.                 continue;
  195.             }
  196.             ps[scene].push(type);
  197.             if (scenes.every(function(s) {
  198.                 return ps[s].length >= types.length;
  199.             })) { return; }
  200.         }
  201.         //
  202.     }; // loadPermanentStateNotes
  203.  
  204.     PS.Game_BattlerBase = {};
  205.  
  206.     /*------------------------------------------------------------------------
  207.      *    New private instance variables                                      
  208.      *------------------------------------------------------------------------*/
  209.     /* _permanentStates: The id of all permanent states of all scene/type
  210.      * _permanentStateTurns: The turns of all permanent states of all scene/type
  211.      */
  212.  
  213.     PS.Game_BattlerBase.initMembers = Game_BattlerBase.prototype.initMembers;
  214.     Game_BattlerBase.prototype.initMembers = function() { // v1.01h+
  215.         PS.Game_BattlerBase.initPermanentStates.call(this); // Added
  216.         PS.Game_BattlerBase.initMembers.apply(this, arguments);
  217.     }; // Game_BattlerBase.prototype.initMembers
  218.  
  219.     PS.Game_BattlerBase.clearStates = Game_BattlerBase.prototype.clearStates;
  220.     Game_BattlerBase.prototype.clearStates = function() {
  221.         PS.Game_BattlerBase.storePermanentStates.call(this); // Added
  222.         PS.Game_BattlerBase.clearStates.apply(this, arguments);
  223.     }; // Game_BattlerBase.prototype.clearStates
  224.  
  225.     PS.Game_BattlerBase.die = Game_BattlerBase.prototype.die;
  226.     Game_BattlerBase.prototype.die = function() { // v1.01a+
  227.         PS.Game_BattlerBase.die.apply(this, arguments);
  228.         // Added
  229.         PS.Game_BattlerBase.restorePermanentStates.call(this, "persist");
  230.         //
  231.     }; // Game_BattlerBase.prototype.die
  232.  
  233.     PS.Game_BattlerBase.revive = Game_BattlerBase.prototype.revive;
  234.     Game_BattlerBase.prototype.revive = function() {
  235.         PS.Game_BattlerBase.revive.apply(this, arguments);
  236.         // Added
  237.         PS.Game_BattlerBase.restorePermanentStates.call(this, "revive");
  238.         //
  239.     }; // Game_BattlerBase.prototype.revive
  240.  
  241.     PS.Game_BattlerBase.recoverAll = Game_BattlerBase.prototype.recoverAll;
  242.     Game_BattlerBase.prototype.recoverAll = function() { // v1.02a+
  243.         PS.Game_BattlerBase.recoverAll.apply(this, arguments);
  244.         // Added
  245.         PS.Game_BattlerBase.restorePermanentStates.call(this, "recover");
  246.         //
  247.     }; // Game_BattlerBase.prototype.recoverAll
  248.  
  249.     PS.Game_BattlerBase.initPermanentStates = function() { // v1.01h+
  250.         var types = PS.types;
  251.         this._permanentStates = {};
  252.         this._permanentStateTurns = {};
  253.         PS.scenes.forEach(function(scene) {
  254.             this._permanentStates[scene] = {};
  255.             this._permanentStateTurns[scene] = {};
  256.             types.forEach(function(type) {
  257.                 this._permanentStates[scene][type] = [];
  258.                 this._permanentStateTurns[scene][type] = {};
  259.             }, this);
  260.         }, this);
  261.     }; // initPermanentStates
  262.  
  263.     PS.Game_BattlerBase.storePermanentStates = function() {
  264.         var sc;
  265.         if (!this._states || !this._stateTurns) { return; }
  266.         sc = PS.scene();
  267.         this.states().forEach(function(s) {
  268.             s.meta.permanentStates[sc].forEach(function(t) {
  269.                 this._permanentStates[sc][t].push(s.id);
  270.                 this._permanentStateTurns[sc][t][s.id] = this._stateTurns[s.id];
  271.             }, this);
  272.         }, this);
  273.     }; // storePermanentStates
  274.  
  275.     // type: The permanent state type
  276.     PS.Game_BattlerBase.restorePermanentStates = function(type) {
  277.         var sc;
  278.         if (!this._states || !this._stateTurns) { return; }
  279.         sc = PS.scene();
  280.         this._states = this._states.concat(this._permanentStates[sc][type]);
  281.         this._permanentStates[sc][type].length = 0;
  282.         Object.keys(this._permanentStateTurns[sc][type]).forEach(function(id) {
  283.             id = +id;
  284.             this._stateTurns[id] = this._permanentStateTurns[sc][type][id];
  285.         }, this);
  286.         this._permanentStateTurns[sc][type] = {};
  287.     }; // restorePermanentStates
  288.  
  289. })(DoubleX_RMMV.Permanent_States);
  290.  
  291. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement