Double_X

DoubleX RMMV Item Cooldown v100a

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