Double_X

DoubleX RMMV Item Cooldown v100a

Aug 28th, 2016
357
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 Item Cooldown                                            
  6.  *----------------------------------------------------------------------------
  7.  *    # Introduction                                                          
  8.  *      Suppose a battler has n action slots(that battler can input n actions
  9.  *      in a single turn), and suppose that battler has inputted n actions,  
  10.  *      each needing xi turns to be cooled down, where 1 <= i <= n, then that
  11.  *      battler will behave as follows:                                      
  12.  *      1. At turn y, that battler has inputted the aforementioned n actions  
  13.  *      2. At turn y, that battler will execute the 1st inputted action      
  14.  *      3. At turn y + x1, that battler will execute the 2nd inputted action  
  15.  *      4. At turn y + x1 + x2, that battler will execute the 3rd inputted    
  16.  *         action                                                            
  17.  *      5. At turn y + x1 + x2 + x3, that battler will execute the 4th        
  18.  *         inputted action                                                    
  19.  *      6. At turn y + x1 + x2 + x3 + ... + xi, where 1 <= i <= n, that      
  20.  *         battler will execute the (i + 1)th inputted action                
  21.  *      7. At turn y + x1 + x2 + x3 + ... + x(n - 1), that battler will      
  22.  *         execute the nth inputted action                                    
  23.  *      8. At turn y + x1 + x2 + x3 + ... + xn, that battler will become able
  24.  *         to input actions again                                            
  25.  *      If the ith action that's not executed yet is the 1st one needing      
  26.  *      cooling down, the battler speed will only take the first ith actions'
  27.  *      speeds into account                                                  
  28.  *      Forced actions themselves don't cause their users to cooldown        
  29.  *----------------------------------------------------------------------------
  30.  *    # Terms Of Use                                                          
  31.  *      1. Commercial use's always allowed and crediting me's always optional.
  32.  *      2. You shall keep this plugin's Plugin Info part's contents intact.  
  33.  *      3. You shalln't claim that this plugin's written by anyone other than
  34.  *         DoubleX or my aliases. I always reserve the right to deny you from
  35.  *         using any of my plugins anymore if you've violated this.          
  36.  *      4. CC BY 4.0, except those conflicting with any of the above, applies
  37.  *         to this plugin, unless you've my permissions not needing follow so.
  38.  *      5. I always reserve the right to deny you from using this plugin      
  39.  *         anymore if you've violated any of the above.                      
  40.  *----------------------------------------------------------------------------
  41.  *    # Prerequisites                                                        
  42.  *      Abilities:                                                            
  43.  *      1. Nothing special for most ordinary use cases                        
  44.  *      2. Little RMMV plugin development proficiency to fully utilize this  
  45.  *----------------------------------------------------------------------------
  46.  *    # Links                                                                
  47.  *      This plugin:                                                          
  48.  *      1. http://pastebin.com/Pvn5Vpfe                                      
  49.  *      Video:                                                                
  50.  *      1. https://www.youtube.com/watch?v=t7LSw2qfkcs                        
  51.  *----------------------------------------------------------------------------
  52.  *    # Author                                                                
  53.  *      DoubleX                                                              
  54.  *----------------------------------------------------------------------------
  55.  *    # Changelog                                                            
  56.  *      v1.00a(GMT 1400 28-8-2016):                                          
  57.  *      1. 1st version of this plugin finished                                
  58.  *============================================================================*/
  59. /*:
  60.  * @plugindesc Lets you set skills/items causing users to cooldown after using
  61.  * @author DoubleX
  62.  *
  63.  * @param isEnabled
  64.  * @desc Sets whether this plugin will be enabled
  65.  *       It'll be stored as a boolean, and will be regarded as true if and only
  66.  *       if it's true
  67.  *       Don't change this during the same battle unless you really know what
  68.  *       you're truly foing
  69.  *       E.g.: Setting isEnabled as false will disable this plugin
  70.  * @default true
  71.  *
  72.  * @param textColor
  73.  * @desc Sets the text color of the text showing the number of turns needed to
  74.  *       cooldown the skill/item user on the skill/item window
  75.  *       It'll be stored as a Number
  76.  *       Don't change this when it's shown to ensure proper text displays
  77.  *       E.g.: Setting textColor as 26 will set the text color of the text
  78.  *             showing the number of turns needed to cooldown the skill/item
  79.  *             user on the skill/item window as 26
  80.  * @default 19
  81.  *
  82.  * @help
  83.  * The skill/item window cooldown turn display can be problematic if the
  84.  * number of turn's 1000 or above
  85.  * The default plugin file name is DoubleX RMMV Item Cooldown v100a
  86.  * If you want to change that, you must edit the value of
  87.  * DoubleX_RMMV.Item_Cooldown_File, which must be done via opening this plugin
  88.  * js file directly
  89.  *============================================================================
  90.  *    ## Notetag Info                                                        
  91.  *----------------------------------------------------------------------------
  92.  *    # Skill/Item Notetags:                                                  
  93.  *      1. <item cooldown: turns>                                            
  94.  *         - Sets the number of turns needed to cooldown the skill/item user  
  95.  *           after using it as turns                                          
  96.  *         - E.g.:                                                            
  97.  *           <item cooldown: 1> will set the number of turns needed to        
  98.  *           cooldown the skill/item user after using it as 1                
  99.  *         - Only the 1st notetag will be used                                
  100.  *============================================================================
  101.  *    ## Plugin Call Info                                                    
  102.  *----------------------------------------------------------------------------
  103.  *    # Configuration manipulations                                          
  104.  *      1. $gameSystem.itemCooldown.param                                    
  105.  *         - Returns the stored value of param listed in the plugin manager  
  106.  *         - E.g.:                                                            
  107.  *           $gameSystem.itemCooldown.textColor will return the stored value  
  108.  *           of parameter textColor shown on the plugin manager              
  109.  *      2. $gameSystem.itemCooldown.param = val                              
  110.  *         - Sets the stored value of param listed in plugin manager as val  
  111.  *         - E.g.:                                                            
  112.  *           $gameSystem.itemCooldown.isEnabled = false will set the stored  
  113.  *           value of parameter isEnabled shown on the plugin manager as false
  114.  *         - All $gameSystem.itemCooldown.param changes will be saved        
  115.  *    # Skill/Item notetag manipulations                                      
  116.  *      1. meta.itemCooldown                                                  
  117.  *         - Returns the <item cooldown: turns> notetag value turns as a      
  118.  *           Number                                                          
  119.  *         - E.g.:                                                            
  120.  *           $dataSkills[1].meta.itemCooldown will return the                
  121.  *           <item cooldown: turns> notetag value of skill with id 1          
  122.  *      2. meta.itemCooldown = turns                                          
  123.  *         - Sets the <item cooldown: turns> notetag value turns as a Number  
  124.  *         - E.g.:                                                            
  125.  *           $dataItems[2].meta.itemCooldown = 0 will set the                
  126.  *           <item cooldown: turns> notetag value of item with id 2 as 0      
  127.  *         - All meta.itemCooldown changes can be saved if                    
  128.  *           DoubleX RMMV Dynamic Data is used                                
  129.  *============================================================================
  130.  */
  131.  
  132. var DoubleX_RMMV = DoubleX_RMMV || {};
  133. DoubleX_RMMV['Item Cooldown'] = 'v1.00a';
  134.  
  135. // The plugin file name must be the same as DoubleX_RMMV.Item_Cooldown_File
  136. DoubleX_RMMV.Item_Cooldown_File = 'DoubleX RMMV Item Cooldown v100a';
  137.  
  138. /*============================================================================
  139.  *    ## Plugin Implementations                                              
  140.  *       You need not edit this part as it's about how this plugin works      
  141.  *----------------------------------------------------------------------------
  142.  *    # Plugin Support Info:                                                  
  143.  *      1. Prerequisites                                                      
  144.  *         - Basic knowledge on the default RMMV battle flow implementations  
  145.  *         - Some RMMV plugin development proficiency to fully comprehend this
  146.  *----------------------------------------------------------------------------*/
  147.  
  148. DoubleX_RMMV.Is_Item_Cooldown_Notes_Loaded = false; // v1.00a - v1.00a
  149.  
  150. DoubleX_RMMV.Item_Cooldown_Params = { // v1.00a - v1.00a
  151.  
  152.     isEnabled: 'Boolean', // Marks that isEnabled is a Boolean
  153.  
  154.     textColor: 'Number', // Marks that textColor is a Number
  155.  
  156.     Boolean: function(param) { return param === 'true'; }, // Boolean
  157.  
  158.     Number: function(param) { return +param; } // Number
  159.  
  160. }; // DoubleX_RMMV.Item_Cooldown_Params
  161.  
  162. (function(IC) {
  163.  
  164.     'use strict';
  165.  
  166.     IC.DataManager = {};
  167.     var DM = IC.DataManager;
  168.  
  169.     DM.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  170.     DataManager.isDatabaseLoaded = function() { // Extended; v1.00a - v1.00a
  171.         // Rewritten to read all notetags of this plugin as well
  172.         return DM.isDatabaseLoaded.apply(this, arguments) && DM.loadAllNotes();
  173.         //
  174.     }; // DataManager.isDatabaseLoaded
  175.  
  176.     /* Reads all notetags of this plugin from the database
  177.      * Return: True
  178.      * Functional cohesion/Message coupling/Idempotent
  179.      */
  180.     DM.loadAllNotes = function() { // New; v1.00a - v1.00a
  181.         // Ensures the notetags will only be read exactly once upon game start
  182.         if (DoubleX_RMMV.Is_Item_Cooldown_Notes_Loaded) return true;
  183.         [$dataSkills, $dataItems].forEach(function(type) {
  184.             type.forEach(function(data) { if (data) DM.loadNotes(data); });
  185.         });
  186.         DoubleX_RMMV.Is_Item_Cooldown_Notes_Loaded = true;
  187.         //
  188.         return true;
  189.     }; // DM.loadAllNotes
  190.  
  191.     /* Reads all notetags of this plugin from a dataum of the database
  192.      * (Object)datum: The datum to have its notetags of this plugin read
  193.      * Functional cohesion/Data coupling/Idempotent
  194.      */
  195.     DM.loadNotes = function(datum) { // New; v1.00a - v1.00a
  196.         var regExp = /< *item +cooldown *: *(\d+) *>/i;
  197.         var lines = datum.note.split(/[\r\n]+/);
  198.         for (var index = 0, length = lines.length; index < length; index++) {
  199.             if (!lines[index].match(regExp)) continue;
  200.             return datum.meta.itemCooldown = +RegExp.$1;
  201.         }
  202.         datum.meta.itemCooldown = 0; // The default's not needing charging
  203.     }; // DM.loadNotes
  204.  
  205.     IC.BattleManager = {};
  206.     var BM = IC.BattleManager;
  207.  
  208.     BM.makeActionOrders = BattleManager.makeActionOrders;
  209.     BattleManager.makeActionOrders = function() { // Rewritten; v1.00a - v1.00a
  210.         var battlers = [];
  211.         // Rewritten to exclude battlers still cooling down
  212.         if (!this._surprise) {
  213.             battlers = battlers.concat(GU.nonItemCooldownMems.call($gameParty));
  214.         }
  215.         if (!this._preemptive) {
  216.             battlers = battlers.concat(GU.nonItemCooldownMems.call($gameTroop));
  217.         }
  218.         //
  219.         battlers.forEach(function(battler) {
  220.             battler.makeSpeed();
  221.         });
  222.         battlers.sort(function(a, b) {
  223.             return b.speed() - a.speed();
  224.         });
  225.         this._actionBattlers = battlers;
  226.     }; // BattleManager.makeActionOrders
  227.  
  228.     BM.startTurn = BattleManager.startTurn;
  229.     BattleManager.startTurn = function() { // Extended; v1.00a - v1.00a
  230.         BM.updateItemCooldown(); // Added
  231.         BM.startTurn.apply(this, arguments);
  232.     }; // BattleManager.startTurn
  233.  
  234.     BM.endAction = BattleManager.endAction;
  235.     BattleManager.endAction = function() { // Extended; v1.00a - v1.00a
  236.         BM.endAction.apply(this, arguments);
  237.         // Added to mark the number of turns for the battler to cooldown
  238.         if (!$gameSystem.itemCooldown.isEnabled) return;
  239.         this._subject.itemCooldown = this._action.item().meta.itemCooldown;
  240.         //
  241.     }; // BattleManager.endAction
  242.  
  243.     /* Updates the remaining cooldown turns of all battlers
  244.      * Functional cohesion/Message coupling
  245.      */
  246.     BM.updateItemCooldown = function() { // New; v1.00a - v1.00a
  247.         GU.updateItemCooldown.call($gameParty);
  248.         GU.updateItemCooldown.call($gameTroop);
  249.     }; // BM.updateItemCooldown
  250.  
  251.     IC.Game_System = {};
  252.     var GS = IC.Game_System;
  253.  
  254.     /*------------------------------------------------------------------------
  255.      *    New public instance variable                                        
  256.      *------------------------------------------------------------------------*/
  257.     // itemCooldown: The container of all parameters shown on the plugin manger
  258.  
  259.     GS.initialize = Game_System.prototype.initialize;
  260.     Game_System.prototype.initialize = function() { // Extended; v1.00a - v1.00a
  261.         GS.initialize.apply(this, arguments);
  262.         GS.initializeItemCooldown.call(this); // Added
  263.     }; // Game_System.prototype.initialize
  264.  
  265.     /* Initializes all parameters of this plugin shown on the plugin manager
  266.      * Functional cohesion/Message coupling/Idempotent
  267.      */
  268.     GS.initializeItemCooldown = function() { // New; v1.00a - v1.00a
  269.         this.itemCooldown = {};
  270.         var params = PluginManager.parameters(DoubleX_RMMV.Item_Cooldown_File);
  271.         var ICP = DoubleX_RMMV.Item_Cooldown_Params;
  272.         Object.keys(params).forEach(function(param) {
  273.             this.itemCooldown[param] = ICP[ICP[param]](params[param]);
  274.         }, this);
  275.     }; // GS.initializeItemCooldown
  276.  
  277.     IC.Game_Battler = {};
  278.     var GB = IC.Game_Battler;
  279.  
  280.     /*------------------------------------------------------------------------
  281.      *    New public instance variable                                        
  282.      *------------------------------------------------------------------------*/
  283.     // itemCooldown: The number of turns for this battler to be cooled down
  284.  
  285.     GB.initMembers = Game_Battler.prototype.initMembers;
  286.     Game_Battler.prototype.initMembers = function() {
  287.     // Extended; v1.00a - v1.00a
  288.         GB.initMembers.apply(this, arguments);
  289.         this.itemCooldown = 0; // Added
  290.     };
  291.     // Game_Battler.prototype.initMembers
  292.  
  293.     GB.makeSpeed = Game_Battler.prototype.makeSpeed;
  294.     Game_Battler.prototype.makeSpeed = function() {
  295.     // Extended; v1.00a - v1.00a
  296.         // Rewritten
  297.         if (!$gameSystem.itemCooldown.isEnabled) {
  298.             return GB.makeSpeed.apply(this, arguments);
  299.         }
  300.         this._speed = Math.min.apply(null, GB.makeItemCooldownSpeed.call(this));
  301.         this._speed = this._speed || 0;
  302.         //
  303.     };
  304.     // Game_Battler.prototype.makeSpeed
  305.  
  306.     /* Collects speed from 1st act to the 1st one needing cooldown
  307.      * Return: An array of Numbers each being the speed of a collected action
  308.      * Functional cohesion/Message coupling
  309.      */
  310.     GB.makeItemCooldownSpeed = function() { // New; v1.00a - v1.00a
  311.         var act, item, length = this._actions.length, speeds = [];
  312.         for (var index = 0; index < length; index++) {
  313.             act = this._actions[index];
  314.             if (!act) continue;
  315.             speeds.push(act.speed());
  316.             item = act.item();
  317.             if (item && item.meta.itemCooldown > 0) return speeds;
  318.         }
  319.         return speeds;
  320.     }; // GB.makeItemCooldownSpeed
  321.  
  322.     IC.Game_Actor = {};
  323.     var GActor = IC.Game_Actor;
  324.  
  325.     GActor.makeActions = Game_Actor.prototype.makeActions;
  326.     Game_Actor.prototype.makeActions = function() { // Extended; v1.00a - 1.00a
  327.         // Added to stop making new actions when there are still charging ones
  328.         if ($gameSystem.itemCooldown.isEnabled && this.itemCooldown > 0) return;
  329.         //
  330.         GActor.makeActions.apply(this, arguments);
  331.     }; // Game_Actor.prototype.makeActions
  332.  
  333.     /* Checks whether this actor isn't also charging skills/items
  334.      * Functional cohesion/Message coupling/Referentially transparent
  335.      */
  336.     Game_Actor.prototype.canInput = function() { // New; v1.00a - v1.00a
  337.         // Ensures this plugin works with those having state changes in canInput
  338.         if (!Game_BattlerBase.prototype.canInput.call(this)) return false;
  339.         return !$gameSystem.itemCooldown.isEnabled || this.itemCooldown <= 0;
  340.         //
  341.     }; // Game_Actor.prototype.canInput
  342.  
  343.     IC.Game_Enemy = {};
  344.     var GE = IC.Game_Enemy;
  345.  
  346.     GE.makeActions = Game_Enemy.prototype.makeActions;
  347.     Game_Enemy.prototype.makeActions = function() { // Extended; v1.00a - 1.00a
  348.         // Added to stop making new actions when there are still charging ones
  349.         if ($gameSystem.itemCooldown.isEnabled && this.itemCooldown > 0) return;
  350.         //
  351.         GE.makeActions.apply(this, arguments);
  352.     }; // Game_Enemy.prototype.makeActions
  353.  
  354.     IC.Game_Unit = {};
  355.     var GU = IC.Game_Unit;
  356.  
  357.     /* Gets all alive members not cooling down
  358.      * Return: An array of alive members not cooling down
  359.      * Functional cohesion/Message coupling/Referentially Transparent
  360.      */
  361.     GU.nonItemCooldownMems = function() { // New; v1.00a - v1.00a
  362.         return this.aliveMembers().filter(function(mem) {
  363.             return mem.itemCooldown <= 0;
  364.         });
  365.     }; // GU.nonItemCooldownMems
  366.  
  367.     /* Updates the remaining cooldown turn of all alive members
  368.      * Functional cohesion/Message coupling
  369.      */
  370.     GU.updateItemCooldown = function() { // New; v1.00a - v1.00a
  371.         this.aliveMembers().forEach(function(mem) {
  372.             if (mem.itemCooldown > 0) mem.itemCooldown -= 1;
  373.         });
  374.     }; // GU.updateItemCooldown
  375.  
  376.     IC.Window_ItemList = {};
  377.     var WIL = IC.Window_ItemList;
  378.  
  379.     WIL.drawItem = Window_ItemList.prototype.drawItem;
  380.     Window_ItemList.prototype.drawItem = function(index) {
  381.     // Extended; v1.00a - v1.00a
  382.         WIL.drawItem.apply(this, arguments);
  383.         // Added to draw the turns need to cooldown the item as well
  384.         if (!$gameSystem.itemCooldown.isEnabled) return;
  385.         var item = this._data[index];
  386.         if (!item || item.meta.itemCooldown <= 0) return;
  387.         var rect = this.itemRect(index);
  388.         rect.x -= WSL.costWidth.apply(this, arguments);
  389.         rect.width -= this.textPadding();
  390.         var turns = item.meta.itemCooldown;
  391.         WIL.drawItemCooldown.call(this, turns, rect.x, rect.y, rect.width);
  392.         //
  393.     }; // Window_ItemList.prototype.drawItem
  394.  
  395.     WIL.numberWidth = Window_ItemList.prototype.numberWidth;
  396.     Window_ItemList.prototype.numberWidth = function() {
  397.     // Rewritten; v1.00a - v1.00a
  398.         // Added
  399.         if ($gameSystem.itemCooldown.isEnabled) {
  400.             return WIL.numberWidth.apply(this, arguments) * 2;
  401.         }
  402.         //
  403.         return WIL.numberWidth.apply(this, arguments);
  404.     }; // Window_ItemList.prototype.numberWidth
  405.  
  406.     /* Draws the number of turns needed to cooldown the item on the item window
  407.      * (Number)turns: The number of turns needed to cooldown the item
  408.      * (Number)x; The x position of the text drawn
  409.      * (Number)y; The y position of the text drawn
  410.      * (Number)width: The max width of the text drawn
  411.      * Functional cohesion/Data coupling/Idempotent
  412.      */
  413.     WIL.drawItemCooldown = function(turns, x, y, width) {
  414.     // New; v1.00a - v1.00a
  415.         var textColor = $gameSystem.itemCooldown.textColor;
  416.         this.changeTextColor(this.textColor(textColor));
  417.         this.drawText(turns, x, y, width, 'right');
  418.         this.resetTextColor();
  419.     }; // WIL.drawItemCooldown
  420.  
  421.     IC.Window_SkillList = {};
  422.     var WSL = IC.Window_SkillList;
  423.  
  424.     WSL.drawItem = Window_SkillList.prototype.drawItem;
  425.     Window_SkillList.prototype.drawItem = function(index) {
  426.     // Extended; v1.00a - v1.00a
  427.         WSL.drawItem.apply(this, arguments);
  428.         // Added to draw the number of turns to cooldown the skill user as well
  429.         if (!$gameSystem.itemCooldown.isEnabled) return;
  430.         var skill = this._data[index];
  431.         if (!skill || skill.meta.itemCooldown <= 0) return;
  432.         var rect = this.itemRect(index);
  433.         rect.x -= WSL.costWidth.apply(this, arguments);
  434.         rect.width -= this.textPadding();
  435.         var turns = skill.meta.itemCooldown;
  436.         WSL.drawItemCooldown.call(this, turns, rect.x, rect.y, rect.width);
  437.         //
  438.     }; // Window_SkillList.prototype.drawItem
  439.  
  440.     WSL.costWidth = Window_SkillList.prototype.costWidth;
  441.     Window_SkillList.prototype.costWidth = function() {
  442.     // Rewritten; v1.00a - v1.00a
  443.         // Added
  444.         if ($gameSystem.itemCooldown.isEnabled) {
  445.             return WSL.costWidth.apply(this, arguments) + this.textWidth('000');
  446.         }
  447.         //
  448.         return WSL.costWidth.apply(this, arguments);
  449.     }; // Window_SkillList.prototype.costWidth
  450.  
  451.     /* Draws number of turns needed to cooldown skill user on skill window
  452.      * (Number)turns: The number of turns needed to cooldown the item
  453.      * (Number)x; The x position of the text drawn
  454.      * (Number)y; The y position of the text drawn
  455.      * (Number)width: The max width of the text drawn
  456.      * Functional cohesion/Data coupling/Idempotent
  457.      */
  458.     WSL.drawItemCooldown = function(turns, x, y, width) {
  459.     // New; v1.00a - v1.00a
  460.         var textColor = $gameSystem.itemCooldown.textColor;
  461.         this.changeTextColor(this.textColor(textColor));
  462.         this.drawText(turns, x, y, width, 'right');
  463.     }; // WSL.drawItemCooldown
  464.  
  465. })(DoubleX_RMMV.Item_Cooldown = {});
  466.  
  467. /*============================================================================*/
RAW Paste Data