Advertisement
ezmash

Dynamic Traits and Effects (MV)

Nov 25th, 2015
1,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //==============================================================================
  2. // Dynamic Traits and Effects
  3. // by Shaz
  4. // Last Updated: 2015.11.26
  5. //==============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows you to modify traits and effects during the game
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  * This plugin allows you to add and delete traits for Actors, Classes, Weapons,
  13.  * Armors, Enemies and States, and to add and delete effects for Items and
  14.  * Skills, dynamically, without needing states set up only for that purpose.
  15.  *
  16.  * Plugin Commands:
  17.  *
  18.  * AddTrait class id traitCode dataId [value]
  19.  * RemoveTrait class id traitCode [dataId] [value]
  20.  * GetTrait var class id traitCode dataId
  21.  *
  22.  * AddEffect class id effectCode dataId [value1] [value2]
  23.  * RemoveEffect class id effectCode [dataId] [value1] [value2]
  24.  * GetEffect var1 var2 class id effectCode dataId
  25.  *
  26.  * class = actor, class, skill, item, weapon, armor, enemy or state
  27.  * id = database id of the actor, class, skill, item, etc.
  28.  * traitCode = one of the codes listed below
  29.  * effectCode = one of the codes listed below
  30.  * dataId = refers to the drop-down list when adding traits or effects; when
  31.  *          the list contains database items (like skills, equipment, states,
  32.  *          elements), it is the database id of that item; when the list does
  33.  *          not contain database items, it is the number of the item in the
  34.  *          list, where the top item is 0
  35.  * var, var1, var2 = variable id that will contain the results of the
  36.  *          GetTrait or GetEffect plugin calls
  37.  * value = a numeric value where traits need to have a value entered.  Optional
  38.  *          for the RemoveTrait command - if left out, ALL traits with the
  39.  *          specified trait and data codes will be removed
  40.  * value1 = a numeric value where effects need to have one or two values
  41.  *          entered.  Optional for the RemoveEffect command - if left out, ALL
  42.  *          effects with the specified effect and data codes will be removed.
  43.  * value2 = a numeric value where effects need to have one or two values
  44.  *          entered.  Optional for the RemoveEffect command - if left out, ALL
  45.  *          effects with the specified effect and data codes will be removed.
  46.  *
  47.  * id, value, value1 and value2 can be formulae but must not contain spaces
  48.  *
  49.  * Values may be numbers or percentages.  When percentages, sometimes they are
  50.  * whole numbers, and sometimes they are fractions
  51.  *
  52.  * To determine what numbers should be used, and what format, you could set the
  53.  * desired feature or effect up temporarily on a database item, then view the
  54.  * Data/*.json file after saving.  The Online Javascript Beautifier can make
  55.  * it more readable: http://jsbeautifier.org/?without-codemirror
  56.  *
  57.  * Valid Trait Codes (value in JSON file - text to use in plugin):
  58.  *  11-TRAIT_ELEMENT_RATE      41-TRAIT_STYPE_ADD       61-TRAIT_ACTION_PLUS
  59.  *  12-TRAIT_DEBUFF_RATE       42-TRAIT_STYPE_SEAL      62-TRAIT_SPECIAL_FLAG
  60.  *  13-TRAIT_STATE_RATE        43-TRAIT_SKILL_ADD       63-TRAIT_COLLAPSE_TYPE
  61.  *  14-TRAIT_STATE_RESIST      44-TRAIT_SKILL_SEAL      64-TRAIT_PARTY_ABILITY
  62.  *  21-TRAIT_PARAM             51-TRAIT_EQUIP_WTYPE
  63.  *  22-TRAIT_XPARAM            52-TRAIT_EQUIP_ATYPE
  64.  *  23-TRAIT_SPARAM            53-TRAIT_EQUIP_LOCK
  65.  *  31-TRAIT_ATTACK_ELEMENT    54-TRAIT_EQUIP_SEAL
  66.  *  32-TRAIT_ATTACK_STATE      55-TRAIT_SLOT_TYPE
  67.  *  33-TRAIT_ATTACK_SPEED
  68.  *  34-TRAIT_ATTACK_TIMES
  69.  *
  70.  * Valid Effect Codes (value in JSON file - text to use in plugin):
  71.  *  11-EFFECT_RECOVER_HP       41-EFFECT_SPECIAL
  72.  *  12-EFFECT_RECOVER_MP       42-EFFECT_GROW
  73.  *  13-EFFECT_GAIN_TP          43-EFFECT_LEARN_SKILL
  74.  *  21-EFFECT_ADD_STATE        44-EFFECT_COMMON_EVENT
  75.  *  22-EFFECT_REMOVE_STATE
  76.  *  31-EFFECT_ADD_BUFF
  77.  *  32-EFFECT_ADD_DEBUFF
  78.  *  33-EFFECT_REMOVE_BUFF
  79.  *  34-EFFECT_REMOVE_DEBUFF
  80.  */
  81.  
  82. (function() {
  83.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  84.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  85.     switch(command.toUpperCase()) {
  86.       case 'ADDTRAIT':
  87.         this.addTrait(args);
  88.         break;
  89.       case 'REMOVETRAIT':
  90.         this.removeTrait(args);
  91.         break;
  92.       case 'GETTRAIT':
  93.         this.getTrait(args);
  94.         break;
  95.       case 'ADDEFFECT':
  96.         this.addEffect(args);
  97.         break;
  98.       case 'REMOVEEFFECT':
  99.         this.removeEffect(args);
  100.         break;
  101.       case 'GETEFFECT':
  102.         this.getEffect(args);
  103.         break;
  104.       default:
  105.         _Game_Interpreter_pluginCommand.call(this, command, args);
  106.     }
  107.   };
  108.  
  109.   Game_Interpreter.prototype.addTrait = function(args) {
  110.     //  * AddTrait class id traitCode dataId [value] - add a trait for an object
  111.     var obj = DataManager.getDatabaseObject(args[0], eval(args[1]));
  112.     var newTrait = {};
  113.     newTrait.code = Game_BattlerBase[args[2].toUpperCase()];
  114.     newTrait.dataId = eval(args[3]);
  115.     newTrait.value = args[4] ? eval(args[4]) : 0;
  116.     obj.traits.push(newTrait);
  117.   };
  118.  
  119.   Game_Interpreter.prototype.removeTrait = function(args) {
  120.     //  * RemoveTrait class id traitCode dataId [value] - remove a trait for an object
  121.     var obj = DataManager.getDatabaseObject(args[0], eval(args[1]));
  122.     var traitCode = Game_BattlerBase[args[2].toUpperCase()];
  123.     var dataId = eval(args[3]);
  124.     var value = eval(args[4]); // args[4] ? eval(args[4]) : null;
  125.     for (var i = obj.traits.length - 1; i >= 0; i--) {
  126.       var trait = obj.traits[i];
  127.       if (trait.code === traitCode &&
  128.         (trait.dataId === dataId || !dataId) &&
  129.         (trait.value === value || !value)) {
  130.         obj.traits.splice(i, 1);
  131.       }
  132.     }
  133.   };
  134.  
  135.   Game_Interpreter.prototype.getTrait = function(args) {
  136.     //  * GetTrait var class id traitCode dataId - return the value of an object's trait
  137.     var varId = parseInt(args[0]);
  138.     var obj = DataManager.getDatabaseObject(args[1], eval(args[2]));
  139.     var traitCode = Game_BattlerBase[args[3].toUpperCase()];
  140.     var dataId = eval(args[4]);
  141.     $gameVariables.setValue(varId, 0);
  142.     for (var i = 0; i < obj.traits.length; i++) {
  143.       var trait = obj.traits[i];
  144.       if (trait.code === traitCode && trait.dataId === dataId) {
  145.         $gameVariables.setFloat(varId, trait.value);
  146.         i = obj.traits.length;
  147.       }
  148.     }
  149.   };
  150.  
  151.   Game_Interpreter.prototype.addEffect = function(args) {
  152.     //  * AddEffect class id effectCode dataId [value1] [value2] - add an effect for an object
  153.     var obj = DataManager.getDatabaseObject(args[0], eval(args[1]));
  154.     var newEffect = {};
  155.     newEffect.code = Game_Action[args[2].toUpperCase()];
  156.     newEffect.dataId = eval(args[3]);
  157.     newEffect.value1 = args[4] ? eval(args[4]) : 0;
  158.     newEffect.value2 = args[5] ? eval(args[5]) : 0;
  159.     obj.effects.push(newEffect);
  160.   };
  161.  
  162.   Game_Interpreter.prototype.removeEffect = function(args) {
  163.     //  * RemoveEffect class id effectCode dataId [value1] [value2] - remove an effect from an object
  164.     var obj = DataManager.getDatabaseObject(args[0], eval(args[1]));
  165.     var effectCode = Game_Action[args[2].toUpperCase()];
  166.     var dataId = eval(args[3]);
  167.     var value1 = eval(args[4]); // args[4] ? eval(args[4]) : null;
  168.     var value2 = eval(args[5]); // args[5] ? eval(args[5]) : null;
  169.     for (var i = obj.effects.length - 1; i >= 0; i--) {
  170.       var effect = obj.effects[i];
  171.       if (effect.code === effectCode &&
  172.         (effect.dataId === dataId || !dataId) &&
  173.         (effect.value1 === value1 || !value1) &&
  174.         (effect.value2 === value2 || !value2)) {
  175.         obj.effects.splice(i, 1);
  176.       }
  177.     }
  178.   };
  179.  
  180.   Game_Interpreter.prototype.getEffect = function(args) {
  181.     //  * GetEffect var1 var2 class id effectCode dataId - return the value of an object's effect
  182.     var var1Id = parseInt(args[0]);
  183.     var var2Id = parseInt(args[1]);
  184.     var obj = DataManager.getDatabaseObject(args[2], eval(args[3]));
  185.     var effectCode = Game_Action[args[4].toUpperCase()];
  186.     var dataId = eval(args[5]);
  187.     $gameVariables.setValue(var1Id, 0);
  188.     $gameVariables.setValue(var2Id, 0);
  189.     for (var i = 0; i < obj.effects.length; i++) {
  190.       var effect = obj.effects[i];
  191.       if (effect.code === effectCode && effect.dataId === dataId) {
  192.         $gameVariables.setFloat(var1Id, effect.value1);
  193.         $gameVariables.setFloat(var2Id, effect.value2);
  194.         i = obj.effects.length;
  195.       }
  196.     }
  197.   };
  198.  
  199.   // Allow non-integer values to be saved in Game_Variables
  200.   Game_Variables.prototype.setFloat = function(variableId, value) {
  201.     if (variableId > 0 && variableId < $dataSystem.variables.length) {
  202.       this._data[variableId] = value;
  203.       this.onChange();
  204.     }
  205.   };
  206.  
  207.  
  208.   // In the DataManager, we need to copy away the original list of
  209.   // traits and effects on launching the game, then restoring them
  210.   // whenever a new game is started or a game is loaded (to clear
  211.   // any changes that may have been made during gameplay).
  212.   // When saving a game we also need to save the current set of traits
  213.   // and effects, and when loading we need to repopulate the saved
  214.   // info back into the database
  215.   var _DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
  216.   DataManager.isDatabaseLoaded = function() {
  217.     var isLoaded = _DataManager_isDatabaseLoaded.call(this);
  218.     if (isLoaded && !this._TraitEffectsSet) {
  219.       this._TraitEffectsSet = true;
  220.       this.setTraitsAndEffects();
  221.     }
  222.     return isLoaded;
  223.   };
  224.  
  225.   var _DataManager_createGameObjects = DataManager.createGameObjects;
  226.   DataManager.createGameObjects = function() {
  227.     _DataManager_createGameObjects.call(this);
  228.     this.resetTraitsAndEffects();
  229.   };
  230.  
  231.   var _DataManager_makeSaveContents = DataManager.makeSaveContents;
  232.   DataManager.makeSaveContents = function() {
  233.     var contents = _DataManager_makeSaveContents.call(this);
  234.     contents.traits = this.gatherTraits();
  235.     contents.effects = this.gatherEffects();
  236.     return contents;
  237.   };
  238.  
  239.   var _DataManager_extractSaveContents = DataManager.extractSaveContents;
  240.   DataManager.extractSaveContents = function(contents) {
  241.     _DataManager_extractSaveContents.call(this, contents);
  242.     this.loadTraits(contents.traits || {});
  243.     this.loadEffects(contents.effects || {});
  244.   };
  245.  
  246.   DataManager.setTraitsAndEffects = function() {
  247.     $dataActors.forEach(function(obj) {this.setTraits(obj)}.bind(this));
  248.     $dataClasses.forEach(function(obj) {this.setTraits(obj)}.bind(this));
  249.     $dataSkills.forEach(function(obj) {this.setEffects(obj)}.bind(this));
  250.     $dataItems.forEach(function(obj) {this.setEffects(obj)}.bind(this));
  251.     $dataWeapons.forEach(function(obj) {this.setTraits(obj)}.bind(this));
  252.     $dataArmors.forEach(function(obj) {this.setTraits(obj)}.bind(this));
  253.     $dataEnemies.forEach(function(obj) {this.setTraits(obj)}.bind(this));
  254.     $dataStates.forEach(function(obj) {this.setTraits(obj)}.bind(this));
  255.   };
  256.  
  257.   DataManager.setTraits = function(obj) {
  258.     if (obj) {
  259.       obj.savedTraits = JsonEx.makeDeepCopy(obj.traits);
  260.     }
  261.   };
  262.  
  263.   DataManager.setEffects = function(obj) {
  264.     if (obj) {
  265.       obj.savedEffects = JsonEx.makeDeepCopy(obj.effects);
  266.     }
  267.   };
  268.  
  269.   DataManager.resetTraitsAndEffects = function() {
  270.     $dataActors.forEach(function(obj) {this.resetTraits(obj)}.bind(this));
  271.     $dataClasses.forEach(function(obj) {this.resetTraits(obj)}.bind(this));
  272.     $dataSkills.forEach(function(obj) {this.resetEffects(obj)}.bind(this));
  273.     $dataItems.forEach(function(obj) {this.resetEffects(obj)}.bind(this));
  274.     $dataWeapons.forEach(function(obj) {this.resetTraits(obj)}.bind(this));
  275.     $dataArmors.forEach(function(obj) {this.resetTraits(obj)}.bind(this));
  276.     $dataEnemies.forEach(function(obj) {this.resetTraits(obj)}.bind(this));
  277.     $dataStates.forEach(function(obj) {this.resetTraits(obj)}.bind(this));
  278.   };
  279.  
  280.   DataManager.resetTraits = function(obj) {
  281.     if (obj && obj.savedTraits) {
  282.       obj.traits = JsonEx.makeDeepCopy(obj.savedTraits);
  283.     }
  284.   };
  285.  
  286.   DataManager.resetEffects = function(obj) {
  287.     if (obj && obj.savedEffects) {
  288.       obj.effects = JsonEx.makeDeepCopy(obj.savedEffects);
  289.     }
  290.   };
  291.  
  292.   DataManager.gatherTraits = function() {
  293.     var allTraits = {};
  294.     allTraits['actors'] = this.pullTraits($dataActors);
  295.     allTraits['classes'] = this.pullTraits($dataClasses);
  296.     allTraits['weapons'] = this.pullTraits($dataWeapons);
  297.     allTraits['armors'] = this.pullTraits($dataArmors);
  298.     allTraits['enemies'] = this.pullTraits($dataEnemies);
  299.     allTraits['states'] = this.pullTraits($dataStates);
  300.     return allTraits;
  301.   };
  302.  
  303.   DataManager.gatherEffects = function() {
  304.     var allEffects = {};
  305.     allEffects['skills'] = this.pullEffects($dataSkills);
  306.     allEffects['items'] = this.pullEffects($dataItems);
  307.     return allEffects;
  308.   };
  309.  
  310.   DataManager.pullTraits = function(container) {
  311.     var traits = [];
  312.     container.forEach(function(obj) {
  313.       if (obj) {
  314.         traits.push(JsonEx.makeDeepCopy(obj.traits));
  315.       } else {
  316.         traits.push(null);
  317.       }
  318.     })
  319.     return traits;
  320.   };
  321.  
  322.   DataManager.pullEffects = function(container) {
  323.     var effects = [];
  324.     container.forEach(function(obj) {
  325.       if (obj) {
  326.         effects.push(JsonEx.makeDeepCopy(obj.effects));
  327.       } else {
  328.         effects.push(null);
  329.       }
  330.     })
  331.     return effects;
  332.   };
  333.  
  334.   DataManager.loadTraits = function(allTraits) {
  335.     this.pushTraits($dataActors, allTraits['actors'] || []);
  336.     this.pushTraits($dataClasses, allTraits['classes'] || []);
  337.     this.pushTraits($dataWeapons, allTraits['weapons'] || []);
  338.     this.pushTraits($dataArmors, allTraits['armors'] || []);
  339.     this.pushTraits($dataEnemies, allTraits['enemies'] || []);
  340.     this.pushTraits($dataStates, allTraits['states'] || []);
  341.   };
  342.  
  343.   DataManager.loadEffects = function(allEffects) {
  344.     this.pushEffects($dataSkills, allEffects['skills'] || []);
  345.     this.pushEffects($dataItems, allEffects['items'] || []);
  346.   };
  347.  
  348.   DataManager.pushTraits = function(container, traits) {
  349.     traits.forEach(function(obj, index) {
  350.       if (obj) {
  351.         container[index].traits = JsonEx.makeDeepCopy(obj);
  352.       }
  353.     })
  354.   };
  355.  
  356.   DataManager.pushEffects = function(container, effects) {
  357.     effects.forEach(function(obj, index) {
  358.       if (obj) {
  359.         container[index].effects = JsonEx.makeDeepCopy(obj);
  360.       }
  361.     })
  362.   };
  363.  
  364.   DataManager.getDatabaseObject = function(container, objId) {
  365.     switch (container.toUpperCase()) {
  366.       case 'ACTOR':   return $dataActors[objId];  break;
  367.       case 'CLASS':   return $dataClasses[objId]; break;
  368.       case 'SKILL':   return $dataSkillls[objId]; break;
  369.       case 'ITEM':    return $dataItems[objId];   break;
  370.       case 'WEAPON':  return $dataWeapons[objId]; break;
  371.       case 'ARMOR':   return $dataArmors[objId];  break;
  372.       case 'ENEMY':   return $dataEnemies[objId]; break;
  373.       case 'STATE':   return $dataStates[objId];  break;
  374.     }
  375.   };
  376.  
  377. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement