Iavra

Iavra Sets

Dec 29th, 2015 (edited)
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Adds equipment sets to the game, that provide bonuses depending on the number of items equipped.
  3.  * <Iavra Sets>
  4.  * @author Iavra
  5.  *
  6.  * @param Configuration
  7.  * @desc File path to the configuration file, that's used to load set bonuses.
  8.  * @default sets.json
  9.  *
  10.  * @param Stackable Bonuses
  11.  * @desc If set to true, all set bonuses stack. If false, only the highest applicable one gets applied.
  12.  * @default true
  13.  *
  14.  * @help
  15.  * To register one or more equipment sets, put a JSON file in your project folder and set the parameter "Configuration"
  16.  * to its file path. Following is a sample file containing 1 set:
  17.  *
  18.  * [
  19.  *     {
  20.  *         "name": "Testset",
  21.  *         "description": "This is a test set.",
  22.  *         "icon": 1,
  23.  *         "items": [
  24.  *             ["weapon", 1],
  25.  *             ["armor", 2]
  26.  *         ],
  27.  *         "traits": {
  28.  *             "2": [
  29.  *                 {"code": 21, "dataId": 2, "value": 2.0}
  30.  *             ]
  31.  *         }
  32.  *     }
  33.  * ]
  34.  *
  35.  * The sample set contains of the weapon #1 and armor #2 and doubles the "atk" parameter of an actor, if he has both
  36.  * equipped (equipping the weapon twice does NOT count). Set name, description and icon are not directly used by this
  37.  * plugin, but can be utilized by others.
  38.  *
  39.  * You can add all traits, that are available in the database (actors/classes/weapons/armors -> traits). Following is
  40.  * a table containing all codes:
  41.  *
  42.  * 11   Element Rate
  43.  * 12   Debuff Rate
  44.  * 13   State Rate
  45.  * 14   State Resist
  46.  * 20   Param Plus
  47.  * 21   Param Rate
  48.  * 22   XParam Plus
  49.  * 23   SParam Rate
  50.  * 31   Attack Element
  51.  * 32   Attack State
  52.  * 33   Attack Speed
  53.  * 34   Attack Times
  54.  * 41   Add Skill Type
  55.  * 42   Seal Skill Type
  56.  * 43   Add Skill
  57.  * 44   Seal Skill
  58.  * 62   Special Flag
  59.  *
  60.  * There are more trait codes, but i'm not sure, if and how they are working, but feel free to tinker around with them.
  61.  *
  62.  * Following is a table containing the dataIds for all params:
  63.  *
  64.  * 0    Max HP
  65.  * 1    Max MP
  66.  * 2    Attack Power
  67.  * 3    Defense Power
  68.  * 4    Magic Attack Power
  69.  * 5    Magic Defense Power
  70.  * 6    Agility
  71.  * 7    Luck
  72.  *
  73.  * Following is a table containing the dataIds for all xparams:
  74.  *
  75.  * 0    Hit Rate
  76.  * 1    Evasion Rate
  77.  * 2    Critical Rate
  78.  * 3    Critical Evasion Rate
  79.  * 4    Magic Evasion Rate
  80.  * 5    Magic Reflection Rate
  81.  * 6    Counter Attack Rate
  82.  * 7    HP Regeneration Rate
  83.  * 8    MP Regeneration Rate
  84.  * 9    TP Regeneration Rate
  85.  *
  86.  * Following is a table containing the dataIds for all sparams:
  87.  *
  88.  * 0    Target Rate
  89.  * 1    Guard Effect Rate
  90.  * 2    Recovery Effect Rate
  91.  * 3    Pharmacology
  92.  * 4    MP Cost Rate
  93.  * 5    TP Charge Rate
  94.  * 6    Physical Damage Rate
  95.  * 7    Magical Damage Rate
  96.  * 8    Floor Damage Rate
  97.  * 9    Experience Rate
  98.  *
  99.  * Following is a table containing the dataIds for special flags:
  100.  *
  101.  * 0    Auto Battle
  102.  * 1    Guard
  103.  * 2    Substitute
  104.  * 3    Preserve TP
  105.  *
  106.  * The plugin provides some script calls to interact with sets:
  107.  *
  108.  * IAVRA.SETS.sets();               Returns all registered sets.
  109.  * IAVRA.SETS.setsForItem(item);    Returns all sets containing the given armor or weapon.
  110.  * IAVRA.SETS.setsForActor(actor);  Returns all sets containing at least 1 item currently equipped to the given actor.
  111.  *
  112.  * Furthermore, each set has the following functions to interact with:
  113.  *
  114.  * set.numItemsEquipped(actor);     Returns the number of items belonging to this set currently equipped to the actor.
  115.  * set.applicableTraits(actor);     Returns all traits of the set, that are currently applied to the actor.
  116.  * set.name;                        The name of the set.
  117.  * set.description;                 The description of the set.
  118.  * set.icon;                        The icon index of the set.
  119.  * set.items;                       All items belonging to the set.
  120.  */
  121.  
  122. var IAVRA = IAVRA || {};
  123.  
  124. (function($) {
  125.     "use strict";
  126.  
  127.     /**
  128.      * Read plugin parameters independent from the actual filename.
  129.      */
  130.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Sets>'); })[0].parameters;
  131.     var _param_config = _params['Configuration'];
  132.     var _param_stackable = _params['Stackable Bonuses'].toLowerCase() === 'true';
  133.  
  134.     /**
  135.      * New trait code, that defines value to be added to "paramPlus" to increase the base param values.
  136.      */
  137.     var _trait_paramPlus = 20;
  138.  
  139.     /**
  140.      * Holds the set data, before create set objects, since we have to wait for the database being loaded, first.
  141.      */
  142.     var _data;
  143.  
  144.     /**
  145.      * Holds all registered sets.
  146.      */
  147.     var _sets = [];
  148.  
  149.     /**
  150.      * Load the configuration file and store the data for later usage.
  151.      */
  152.     var _loadData = function() {
  153.         var request = new XMLHttpRequest();
  154.         request.open('GET', _param_config);
  155.         request.overrideMimeType('application/json');
  156.         request.onload = function() { _data = JSON.parse(request.responseText); };
  157.         request.onerror = function() { throw new Error('There was an error loading the file.'); };
  158.         request.send();
  159.     };
  160.  
  161.     /**
  162.      * Create set objects from the loaded data. If part of the data is invalid, log the error, but continue. Makes it
  163.      * easier to test configurations. Afterwards, clear the cached data object to free some memory, because it's not
  164.      * needed anymore.
  165.      */
  166.     var _initialize = function() {
  167.         _data.forEach(function(data) {
  168.             try { _sets.push(new IAVRA.SETS.Set(data)); } catch(error) { console.warn('Invalid set data: ', error); }
  169.         });
  170.         _data = null;
  171.     };
  172.  
  173.     /**
  174.      * Return the actual weapon/armor entries for the given item data.
  175.      */
  176.     var _parseItems = function(data) {
  177.         return data.map(function(item) { switch(item[0]) {
  178.             case 'weapon': return $dataWeapons[item[1]];
  179.             case 'armor': return $dataArmors[item[1]];
  180.         }})
  181.     };
  182.  
  183.     /**
  184.      * Returns an array containing all traits of a given set data, ordered by the number of equipped items needed.
  185.      */
  186.     var _parseTraits = function(data) {
  187.         return Object.keys(data).reduce(function(array, key) { array[key|0] = data[key]; return array; }, [])
  188.     };
  189.  
  190.     /**
  191.      * On refresh, store the actor sets and set bonuses inside the actor, so they don't have to be calculated every
  192.      * time a parameter or trait is accessed.
  193.      */
  194.     var _refreshActor = function(actor) {
  195.         var sets = $.SETS.setsForActor(actor);
  196.         var traits = Array.prototype.concat.apply([], sets.map(function(s) { return s.applicableTraits(actor); }));
  197.         actor._iavra_sets = { sets: sets, traits: traits };
  198.     };
  199.  
  200.     //=============================================================================
  201.     // IAVRA.SETS
  202.     //=============================================================================
  203.  
  204.     $.SETS = {
  205.         /**
  206.          * Returns all registered sets.
  207.          */
  208.         sets: function() { return _sets; },
  209.         /**
  210.          * Returns an array of all sets containing the given item.
  211.          */
  212.         setsForItem: function(item) { return _sets.filter(function(s) { return s.items.contains(item); }); },
  213.         /**
  214.          * Returns an array of all sets, that are currently present on the given actor.
  215.          */
  216.         setsForActor: function(actor) { return _sets.filter(function(s) { return s.numItemsEquipped(actor) > 0; }); },
  217.         /**
  218.          * Holds all information regarding the registered equipment sets.
  219.          */
  220.         Set: function() { this.initialize.apply(this, arguments); }
  221.     };
  222.  
  223.     //=============================================================================
  224.     // IAVRA.SETS.Set
  225.     //=============================================================================
  226.  
  227.     (function($) {
  228.  
  229.         /**
  230.          * Initialize a bunch of stuff from the given data.
  231.          */
  232.         $.prototype.initialize = function(data) {
  233.             this._name = data.name;
  234.             this._description = data.description;
  235.             this._icon = parseInt(data.icon);
  236.             this._items = _parseItems(data.items);
  237.             this._traits = _parseTraits(data.traits);
  238.         };
  239.  
  240.         /**
  241.          * Returns the number of items a given actor has equipped, that belong to this set.
  242.          */
  243.         $.prototype.numItemsEquipped = function(actor) {
  244.             var e = actor.equips(); return this.items.filter(function(i) { return e.contains(i); }).length;
  245.         };
  246.  
  247.         /**
  248.          * Returns the traits belonging to this set, that can be applied to the given actor depending on the number of
  249.          * set items they have equipped.
  250.          */
  251.         $.prototype.applicableTraits = function(actor) {
  252.             var num = this.numItemsEquipped(actor), result = [];
  253.             for(var t = this._traits, i = t.length; i--; ) { if(t[i] && i <= num) {
  254.                 if(_param_stackable) { result = result.concat(t[i]); } else { return t[i]; }
  255.             } }
  256.             return result;
  257.         };
  258.  
  259.         /**
  260.          * Defining some properties for encapsulation.
  261.          */
  262.         Object.defineProperties($.prototype, {
  263.             name: { get: function() { return this._name; } },
  264.             description: { get: function() { return this._description; } },
  265.             icon: { get: function() { return this._icon; } },
  266.             items: { get: function() { return this._items; } }
  267.         });
  268.  
  269.     })($.SETS.Set);
  270.  
  271.     //=============================================================================
  272.     // Game_Actor
  273.     //=============================================================================
  274.  
  275.     (function($) {
  276.  
  277.         /**
  278.          * On refresh, store all sets and their traits applicable to this actor inside the object, so we don't need to
  279.          * calculate them every time we need to access a trait.
  280.          */
  281.         var alias_refresh = $.prototype.refresh;
  282.         $.prototype.refresh = function() {
  283.             _refreshActor(this);
  284.             alias_refresh.call(this);
  285.         };
  286.  
  287.         /**
  288.          * Also add all traits supplied by equipment sets.
  289.          */
  290.         var alias_traitObjects = $.prototype.traitObjects;
  291.         $.prototype.traitObjects = function() {
  292.             return alias_traitObjects.call(this).concat(this._iavra_sets);
  293.         };
  294.  
  295.         /**
  296.          * We have added a new trait id (20), that gets used to add constant values to the param base.
  297.          */
  298.         var alias_paramPlus = $.prototype.paramPlus;
  299.         $.prototype.paramPlus = function(paramId) {
  300.             return alias_paramPlus.call(this, paramId) + this.traitsSum(_trait_paramPlus, paramId);
  301.         };
  302.  
  303.         /**
  304.          * Contains all sets and set bonuses, that are currently applied to this actor. Gets updated during refresh.
  305.          */
  306.         $.prototype._iavra_sets = { sets: [], traits: [] };
  307.  
  308.     })(Game_Actor);
  309.  
  310.     //=============================================================================
  311.     // Scene_Boot
  312.     //=============================================================================
  313.  
  314.     (function($) {
  315.  
  316.         /**
  317.          * On create, start loading our configuration file.
  318.          */
  319.         var alias_create = $.prototype.create;
  320.         $.prototype.create = function() { alias_create.call(this); _loadData(); };
  321.  
  322.         /**
  323.          * Wait, until the configuration has been loaded.
  324.          */
  325.         var alias_isReady = $.prototype.isReady;
  326.         $.prototype.isReady = function() { return (_data !== undefined) && alias_isReady.call(this); };
  327.  
  328.         /**
  329.          * On start, parse the loaded data, since we need access to $dataWeapons and $dataArmors.
  330.          */
  331.         var alias_start = $.prototype.start;
  332.         $.prototype.start = function() { _initialize(); alias_start.call(this); };
  333.  
  334.     })(Scene_Boot);
  335.  
  336.     //=============================================================================
  337.     // DataManager
  338.     //=============================================================================
  339.  
  340.     (function($) {
  341.  
  342.         /**
  343.          * When loading a game, refresh all set bonuses. This makes the plugin plug-and-play and allows to edit the
  344.          * configuration file without invalidating all savegames.
  345.          */
  346.         var alias_loadGame = $.loadGame;
  347.         $.loadGame = function(savefileId) {
  348.             var result = alias_loadGame.call(this, savefileId);
  349.             $gameActors._data.forEach(function(actor) { if(actor) { _refreshActor(actor); } });
  350.             return result;
  351.         };
  352.  
  353.     })(DataManager);
  354.  
  355. })(IAVRA);
Add Comment
Please, Sign In to add comment