Advertisement
Double_X

DoubleX RMMV Equip Prerequisites v101g

Oct 31st, 2015 (edited)
386
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 Equip Prerequisites                                      
  6.  *----------------------------------------------------------------------------
  7.  *    # Terms Of Use                                                          
  8.  *      You shall keep this plugin's Plugin Info part's contents intact      
  9.  *      You shalln't claim that this plugin's written by anyone other than    
  10.  *      DoubleX or his aliases                                                
  11.  *      None of the above applies to DoubleX or his aliases                  
  12.  *----------------------------------------------------------------------------
  13.  *    # Prerequisites                                                        
  14.  *      Abilities:                                                            
  15.  *      1. Some Javascript coding proficiency to fully utilize this plugin    
  16.  *----------------------------------------------------------------------------
  17.  *    # Links                                                                
  18.  *      This plugin:                                                          
  19.  *      1. http://pastebin.com/6TetJ6dH                                      
  20.  *      Mentioned Patreon Supporters:
  21.  *      https://www.patreon.com/posts/71738797
  22.  *----------------------------------------------------------------------------
  23.  *    # Author                                                                
  24.  *      DoubleX                                                              
  25.  *----------------------------------------------------------------------------
  26.  *    # Changelog                                                            
  27.  *      v1.01g(GMT 0100 18-1-2016):                                          
  28.  *      1. <stat req: stat, operator, val> only support battler getters      
  29.  *      2. Fixed Number is not a function bug                                
  30.  *      v1.01f(GMT 0300 17-1-2016):                                          
  31.  *      1. Fixed undefined variable req bug                                  
  32.  *      2. Exposed this plugin's equip manipulation plugin calls              
  33.  *      3. Increased this plugin's compactness, compatibility and readability
  34.  *      v1.01e(GMT 1200 24-11-2015):                                          
  35.  *      1. Fixed unsupported arrow function in ES5 bug                        
  36.  *      2. The extended functions can be accessed by other custom plugins now
  37.  *      3. Notetags with invalid operators will log message on console instead
  38.  *      4. Increased this plugin's readability and robustness                
  39.  *      v1.01d(GMT 1600 6-11-2015):                                          
  40.  *      1. Simplified the notetag reading mechanisms                          
  41.  *      2. Fixed some typos                                                  
  42.  *      v1.01c(GMT 1200 5-11-2015):                                          
  43.  *      1. Fixed undefined this under DoubleX_RMMV.Equip_Prerequisites bug    
  44.  *      v1.01b(GMT 0000 5-11-2015):                                          
  45.  *      1. Fixed failing to load notetags due to nil data bug                
  46.  *      v1.01a(GMT 1000 4-11-2015):                                          
  47.  *      1. Added <no weapon req: id>, <no armor req: id> and <weapon req: id>
  48.  *         notetags                                                          
  49.  *      2. Fixed several logic and syntax errors                              
  50.  *      3. Increased this plugin's maintainability                            
  51.  *      v1.00a(GMT 1300 31-10-2015):                                          
  52.  *      1. 1st version of this plugin finished                                
  53.  *============================================================================*/
  54. /*:
  55.  * @plugindesc Lets users add various prerequisites for equipments to actors
  56.  * @author DoubleX
  57.  *
  58.  * @help
  59.  *============================================================================
  60.  *    ## Notetag Info                                                        
  61.  *----------------------------------------------------------------------------
  62.  *    # Equip Notetags:                                                      
  63.  *      (v1.01a+)1. <no weapon req: id>                                      
  64.  *         - Sets that equip to be unequippable with an equipped weapon with  
  65.  *           id id                                                            
  66.  *      (v1.01a+)2. <no armor req: id>                                        
  67.  *         - Sets that equip to be unequippable with an equipped armor with id
  68.  *           id                                                              
  69.  *      (v1.01a+)3. <weapon req: id>                                          
  70.  *         - Sets that equip to need an equipped weapon with id id to be      
  71.  *           equipped                                                        
  72.  *      4. <armor req: id>                                                    
  73.  *         - Sets that equip to need an equipped armor with id id to be      
  74.  *           equipped                                                        
  75.  *      5. <stat req: stat, operator, val>                                    
  76.  *         - Sets that equip to need the owner's stat stat to meet the        
  77.  *           operator operator and value val to be equipped                  
  78.  *         - stat can be any Parameter, Ex-Parameter and Sp-Parameter, hp, mp,
  79.  *           tp and level                                                    
  80.  *         - (Advanced)stat can also be any other battler getter name that can
  81.  *           use operator                                                    
  82.  *         - operator can be either l, le, e, ge, g or ne, meaning <, <=. ===,
  83.  *           >=, > and !== respectively                                      
  84.  *         - If val is a number, it'll be treated as a Number rather than a  
  85.  *           String                                                          
  86.  *      6. <var req: id, operator, val>                                      
  87.  *         - Sets that equip to need the value of variable with id id to meet
  88.  *           the operator operator and value val to be equipped              
  89.  *         - operator can be either l, le, e, ge, g or ne, meaning <, <=. ===,
  90.  *           >=, > and !== respectively                                      
  91.  *         - If val is a number, it'll be treated as a Number rather than a  
  92.  *           String                                                          
  93.  *============================================================================
  94.  *    ## Plugin Call Info                                                    
  95.  *----------------------------------------------------------------------------
  96.  *    # Equip manipulations                                                  
  97.  *      1. meta.noWeaponReq                                                  
  98.  *         - Returns the Array of ids of all equipped weapons making this    
  99.  *           equip unequippable                                              
  100.  *      2. meta.noWeaponReq = [id, id, id, ..., id]                          
  101.  *         - Sets the Array of ids of all equipped weapons making this equip  
  102.  *           unequippable                                                    
  103.  *         - All meta.noWeaponReq changes can be saved if                    
  104.  *           DoubleX RMMV Dynamic Data is used                                
  105.  *      3. meta.noArmorReq                                                    
  106.  *         - Returns the Array of ids of all equipped armors making this equip
  107.  *           unequippable                                                    
  108.  *      4. meta.noArmorReq = [id, id, id, ..., id]                            
  109.  *         - Sets the Array of ids of all equipped armors making this equip  
  110.  *           unequippable                                                    
  111.  *         - All meta.noArmorReq changes can be saved if                      
  112.  *           DoubleX RMMV Dynamic Data is used                                
  113.  *      5. meta.weaponReq                                                    
  114.  *         - Returns the Array of ids of all equipped weapons needed for this
  115.  *           equip to be equippable                                          
  116.  *      6. meta.weaponReq = id                                                
  117.  *         - Sets the Array of ids of all equipped weapons needed for this    
  118.  *           equip to be equippable                                          
  119.  *         - All meta.weaponReq changes can be saved if                      
  120.  *           DoubleX RMMV Dynamic Data is used                                
  121.  *      7. meta.armorReq                                                      
  122.  *         - Returns the Array of ids of all equipped armors needed for this  
  123.  *           equip to be equippable                                          
  124.  *      8. meta.armorReq = id                                                
  125.  *         - Sets the Array of ids of all equipped armors needed for this    
  126.  *           equip to be equippable                                          
  127.  *         - All meta.armorReq changes can be saved if                        
  128.  *           DoubleX RMMV Dynamic Data is used                                
  129.  *      9. meta.statReq                                                      
  130.  *         - Returns the Array of all stat-operator-value triples, each in the
  131.  *           form of [stat, operator, value], as the equip prerequisites      
  132.  *      10. meta.statReq = [[stat, operator, value], [stat, operator, value]]
  133.  *          - Sets the Array of all stat-operator-value triples, each in the  
  134.  *            form of [stat, operator, value], as the equip prerequisites    
  135.  *          - All meta.statReq changes can be saved if                        
  136.  *            DoubleX RMMV Dynamic Data is used                              
  137.  *      11. meta.varReq                                                      
  138.  *          - Returns the Array of all variable-operator-value triples, each  
  139.  *            in the form of [variable, operator, value], as the equip        
  140.  *            prerequisites                                                  
  141.  *      12. meta.varReq = [[variable, operator, value]]                      
  142.  *          - Sets the Array of all variable-operator-value triples, each in  
  143.  *            the form of [variable, operator, value], as the equip          
  144.  *            prerequisites                                                  
  145.  *          - All meta.varReq changes can be saved if                        
  146.  *            DoubleX RMMV Dynamic Data is used                              
  147.  *============================================================================
  148.  */
  149.  
  150. "use strict";
  151. var DoubleX_RMMV = DoubleX_RMMV || {};
  152. DoubleX_RMMV["Equip Prerequisites"] = "v1.01g";
  153.  
  154. /*============================================================================
  155.  *    ## Plugin Implementations                                              
  156.  *       You need not edit this part as it's about how this plugin works      
  157.  *----------------------------------------------------------------------------
  158.  *    # Plugin Support Info:                                                  
  159.  *      1. Prerequisites                                                      
  160.  *         - Some Javascript coding proficiency to fully comprehend this      
  161.  *           plugin                                                          
  162.  *      2. Function documentation                                            
  163.  *         - The 1st part describes why this function's rewritten/extended for
  164.  *           rewritten/extended functions or what the function does for new  
  165.  *           functions                                                        
  166.  *         - The 2nd part describes what the arguments of the function are    
  167.  *         - The 3rd part informs which version rewritten, extended or created
  168.  *           this function                                                    
  169.  *         - The 4th part informs whether the function's rewritten or new    
  170.  *         - The 5th part informs whether the function's a real or potential  
  171.  *           hotspot                                                          
  172.  *         - The 6th part describes how this function works for new functions
  173.  *           only, and describes the parts added, removed or rewritten for    
  174.  *           rewritten or extended functions only                            
  175.  *         Example:                                                          
  176.  * /*----------------------------------------------------------------------
  177.  *  *    Why rewrite/extended/What this function does                      
  178.  *  *----------------------------------------------------------------------*/
  179. /* // arguments: What these arguments are                                    
  180.  * functionName = function(arguments) { // Version X+; Hotspot                
  181.  *     // Added/Removed/Rewritten to do something/How this function works    
  182.  *     functionContents                                                      
  183.  *     //                                                                    
  184.  * } // functionName                                                          
  185.  *----------------------------------------------------------------------------*/
  186.  
  187. DoubleX_RMMV.Equip_Req = {};
  188. (function(ER) {
  189.  
  190.     ER.DataManager = {};
  191.     var DM = ER.DataManager;
  192.  
  193.     DM.isDatabaseLoaded = DataManager.isDatabaseLoaded;
  194.     DataManager.isDatabaseLoaded = function() {
  195.         // Rewritten
  196.         return DM.isDatabaseLoaded.apply(this, arguments) && DM.loadAllNotes();
  197.         //
  198.     }; // DataManager.isDatabaseLoaded
  199.  
  200.     DM.loadAllNotes = function() {
  201.         [$dataWeapons, $dataArmors].forEach(function(type) {
  202.             type.forEach(function(data) {
  203.                 if (data) { DM.loadItemNotes(data); }
  204.             });
  205.         });
  206.         return true;
  207.     }; // DM.loadAllNotes
  208.  
  209.     // data: The data to have its notetags read
  210.     DM.loadItemNotes = function(data) { // v1.01e+; New
  211.         var noWeaponReq = /< *no +weapon +req *: *(\d+) *>/i;
  212.         var noArmorReq = /< *no +armor +req *: *(\d+) *>/i;
  213.         var weaponReq = /< *weapon +req *: *(\d+) *>/i;
  214.         var armorReq = /< *armor +req *: *(\d+) *>/i;
  215.         var statReq = /< *stat +req *: *(.+) *, *(\w+) *, *(\w+) *>/i;
  216.         var varReq = /< *var +req *: *(\d+) *, *(\w+) *, *(\w+) *>/i;
  217.         var meta = data.meta, num;
  218.         meta.noWeaponReq = [];
  219.         meta.noArmorReq = [];
  220.         meta.weaponReq = [];
  221.         meta.armorReq = [];
  222.         meta.statReq = [];
  223.         meta.varReq = [];
  224.         data.note.split(/[\r\n]+/).forEach(function(line) {
  225.             if (line.match(noWeaponReq)) {
  226.                 meta.noWeaponReq.push(+RegExp.$1);
  227.             } else if (line.match(noArmorReq)) {
  228.                 meta.noArmorReq.push(+RegExp.$1);
  229.             } else if (line.match(weaponReq)) {
  230.                 meta.weaponReq.push(+RegExp.$1);
  231.             } else if (line.match(armorReq)) {
  232.                 meta.armorReq.push(+RegExp.$1);
  233.             } else if (line.match(statReq)) {
  234.                 num = +RegExp.$3;
  235.                 if (isNaN(num)) { num = RegExp.$3; }
  236.                 meta.statReq.push([RegExp.$1, RegExp.$2, num]);
  237.             } else if (line.match(varReq)) {
  238.                 num = +RegExp.$3;
  239.                 if (isNaN(num)) { num = RegExp.$3; }
  240.                 meta.varReq.push([+RegExp.$1, RegExp.$2, num]);
  241.             }
  242.         });
  243.     }; // DM.loadItemNotes
  244.  
  245.     ER.Game_BattlerBase = {};
  246.     var GBB = ER.Game_BattlerBase;
  247.  
  248.     GBB.canEquip = Game_BattlerBase.prototype.canEquip;
  249.     Game_BattlerBase.prototype.canEquip = function(item) {
  250.         // Rewritten
  251.         if (!GBB.canEquip.apply(this, arguments)) { return false; }
  252.         return GBB.equipReq.call(this, item);
  253.         //
  254.     }; // Game_BattlerBase.prototype.canEquip
  255.  
  256.     // item: The equip to be checked
  257.     GBB.equipReq = function(item) { // New
  258.         if (!GBB.equipReqWeapon.call(this, item.meta.noWeaponReq, false)) {
  259.             return false;
  260.         } else if (!GBB.equipReqArmor.call(this, item.meta.noArmorReq, false)) {
  261.             return false;
  262.         } else if (!GBB.equipReqWeapon.call(this, item.meta.weaponReq, true)) {
  263.             return false;
  264.         } else if (!GBB.equipReqArmor.call(this, item.meta.armorReq, true)) {
  265.             return false;
  266.         }
  267.         if (!GBB.equipReqOperator.call(this, item.meta.statReq, "statReq")) {
  268.             return false;
  269.         }
  270.         return GBB.equipReqOperator.call(this, item.meta.varReq, "varReq");
  271.     }; // GBB.equipReq
  272.  
  273.     /* req: The item equip prerequisites
  274.      * flag: The required to be equipped/unequippable flag
  275.      */
  276.     GBB.equipReqWeapon = function(req, flag) { // New
  277.         return req.length <= 0 || flag === req.some(function(id) {
  278.             return this.hasWeapon($dataWeapons[id]);
  279.         }, this);
  280.     }; // GBB.equipReqWeapon
  281.  
  282.     /* req: The item equip prerequisites
  283.      * flag: The required to be equipped/unequippable flag
  284.      */
  285.     GBB.equipReqArmor = function(req, flag) { // New
  286.         return req.length <= 0 || flag === req.some(function(id) {
  287.             return this.hasArmor($dataArmors[id]);
  288.         }, this);
  289.     }; // GBB.equipReqArmor
  290.  
  291.     /* item: The equip to be checked
  292.      * note: The notetag name
  293.      */
  294.     GBB.equipReqOperator = function(req, note) { // New
  295.         if (req.length <= 0) { return true; };
  296.         var varVal = note === "varReq";
  297.         return req.every(function(r) {
  298.             return GBB.equipReqEval.call(this, r, varVal);
  299.         }, this);
  300.     }; // GBB.equipReqOperator
  301.  
  302.     /* req: The equip prerequisite notetag value array
  303.      * varVal: The variable id flag
  304.      */
  305.     GBB.equipReqEval = function(req, varVal) { // New
  306.         // req[0] must be a battler getter name in case of stat req
  307.         var val = varVal ? $gameVariables.value(req[0]) : this[req[0]];
  308.         //
  309.         switch (req[1]) {
  310.         case "l": return val < req[2];
  311.         case "le": return val <= req[2];
  312.         case "e": return val === req[2];
  313.         case "ge": return val >= req[2];
  314.         case "g": return val > req[2];
  315.         case "n": return val !== req[2];
  316.         default:
  317.             console.log("Invalid operator " + req[1] + " as notetag values");
  318.             return true;
  319.         }
  320.     }; // GBB.equipReqEval
  321.  
  322. })(DoubleX_RMMV.Equip_Req);
  323.  
  324. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement