Advertisement
Ellye

EllyeStatesDamage

Oct 30th, 2015
3,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Place file inside /js/plugins
  2. // Remember to save after adding plugins or changing parameters.
  3. //=============================================================================
  4. // Ellye's States Damage
  5. //=============================================================================
  6. /*:
  7.  * Version: 2015-11-20-2232
  8.  *
  9.  * CHANGE LOG:
  10.  * 2015-11-20-2232 - Normal attack states should also get the caster paramaters properly now.
  11.  * 2015-11-19-2359 - Fixed decimal damage.
  12.  * 2015-11-19-0021 - Release of Version 2 Beta - changed how the damage is applied, needs a lot of testing.
  13.  * 2015-11-08-0156 - Changed the _stateCaster array to no longer store direct object references, that could easily result in infinite data loops. Thanks, Bobstah.
  14.  * 2015-11-05-1029 - Added tags <drain>, <mp_drain>, <mp_heal>, <mp_damage>, <can_critical>.
  15.  * 2015-11-04-2327 - Made this compatible with Yanfly's Cooldowns (that plugin wasn't expecting the possibility of its attributes being undefined in a skill)
  16.  * 2015-11-03-3442 - Fixed _stateCaster information being unintentionaly shared between battlers.
  17.  * 2015-11-03-0948 - Fixed an issue that could cause compatibility problems.
  18.  * 2015-11-01-1141 - Added collapse animation for enemies that die due to status effects.
  19.  * 2015-10-31-2112 - Some combinations of plugins would cause the damage popup to appear twice. So I added a compatibility parameter.
  20.  * 2015-10-31-1802 - Fixed a bug that made the plugin not work properly in many cases.
  21.  * 2015-10-31-1434 - Fixed a crash at turn end.
  22.  * 2015-10-31-1225 - Fixed a few potential crashes.
  23.  * 2015-10-31-0221 - Beta Release.
  24.  *
  25.  *
  26.  *
  27.  * @plugindesc Ver.2015-11-20-2232. Customized state damage/healing. See help.
  28.  * <Ellye SD>
  29.  * @author https://ellyeblog.wordpress.com/ || http://steamcommunity.com/id/Ellye
  30.  *
  31.  * @param Damage Popup Compatibility
  32.  * @desc Change this to "1" if you're having an issue that causes damage numbers to appear twice per turn (compatibility).
  33.  * @default 0
  34.  *
  35.  *
  36.  * @help This plugin allows you to set up custom damage or healing for States.
  37.  * Use the notes field, and swap FORMULA_HERE for the formula you want to use (exactly like you'd do for a Skill).
  38.  * <formula:FORMULA_HERE>
  39.  * <element:ELEMENT_ID>
  40.  * <variance:VARIANCE_PERCENT>
  41.  *
  42.  *
  43.  * You can also set the following flags (mutually exclusive):
  44.  * <heal>
  45.  * <drain>
  46.  * <mp_damage>
  47.  * <mp_heal>
  48.  * <mp_drain>
  49.  *
  50.  * For example, let's say we want to create a "Burning" state:
  51.  * <formula:a.mat/(1+(b.mdf/100))>
  52.  * <element:2>
  53.  * <variance:20>
  54.  *
  55.  * Or a "Regeneration" state:
  56.  * <formula:a.mat>
  57.  * <element:6>
  58.  * <variance:10>
  59.  *
  60.  * Keep in mind, "a" is always the person who have casted the state, and "b" is always the afflicted person.
  61.  * Sometimes they might both be the same (if a person casts Regeneration on himself, for an easy example).
  62.  */
  63. (function() {
  64.  
  65.     var parameters = $plugins.filter(function(p) {
  66.         return p.description.contains('<Ellye SD>');
  67.     })[0].parameters; //Thanks to Iavra
  68.     var damage_popup_compatibility = Number(parameters['Damage Popup Compatibility'] || 0);
  69.  
  70.     //Let's add a new property for battlers, called "_stateCaster". It will be array holding the caster of each state we have.
  71.     Object.defineProperties(Game_Battler.prototype, {
  72.         _stateCasterIndex: {
  73.             writable: true,
  74.             value: [],
  75.             configurable: true,
  76.             enumerable: true
  77.         }
  78.     });
  79.  
  80.     Game_Battler.prototype.stateCasters = function() {
  81.         var stateCasters = [];
  82.         this._stateCasterIndex.forEach(function(index, state) {
  83.             //Actors receive a positive id on the array
  84.             if (index > 0 && $gameActors._data.length >= index - 1 && typeof $gameActors._data[index] !== 'undefined')
  85.             {
  86.                 stateCasters[state] = $gameActors._data[index];
  87.             }
  88.             else
  89.             {
  90.                 index *= -1;
  91.                 if ($gameTroop._enemies.length >= index - 1 && typeof $gameTroop._enemies[index] !== 'undefined')
  92.                 {
  93.                     stateCasters[state] = $gameTroop._enemies[index];
  94.                 }
  95.             }
  96.         }, this);
  97.         return stateCasters;
  98.     };
  99.  
  100.     //We need to initialize _stateCaster for our Game Battlers:
  101.     _alis_gb_init = Game_Battler.prototype.initMembers;
  102.     Game_Battler.prototype.initMembers = function() {
  103.         _alis_gb_init.call(this);
  104.         this._stateCasterIndex = [];
  105.     };
  106.  
  107.  
  108.     //We modify the function that adds states, so that it shall pass the caster as a parameter:
  109.     Game_Action.prototype.itemEffectAddNormalState = function(target, effect) {
  110.         var caster = this.subject();
  111.         var chance = effect.value1;
  112.         if (!this.isCertainHit()) {
  113.             chance *= target.stateRate(effect.dataId);
  114.             chance *= this.lukEffectRate(target);
  115.         }
  116.         if (Math.random() < chance) {
  117.             target.addState(effect.dataId, caster);
  118.             this.makeSuccess(target);
  119.         }
  120.     };
  121.  
  122.     //And also this one:
  123.     Game_Action.prototype.itemEffectAddAttackState = function(target, effect) {
  124.         this.subject().attackStates().forEach(function(stateId) {
  125.             var chance = effect.value1;
  126.             chance *= target.stateRate(stateId);
  127.             chance *= this.subject().attackStatesRate(stateId);
  128.             chance *= this.lukEffectRate(target);
  129.             if (Math.random() < chance) {
  130.                 target.addState(stateId, this.subject());
  131.                 this.makeSuccess(target);
  132.             }
  133.         }.bind(this), target);
  134.     };
  135.  
  136.     //We start receiving a parameter:
  137.     Game_Battler.prototype.addState = function(stateId, caster) {
  138.         if (typeof caster === 'undefined') {
  139.             caster = this;
  140.         }
  141.         if (this.isStateAddable(stateId)) {
  142.             if (!this.isStateAffected(stateId)) {
  143.                 this.addNewState(stateId);
  144.                 this.refresh();
  145.             }
  146.             this.resetStateCounts(stateId);
  147.             this._result.pushAddedState(stateId);
  148.         }
  149.         //We could also override AddNewState() to make it receive the parameter and assign values, but we might be compromising compatibility a bit too much.
  150.         //So we are going to add those values here. This also means that receiving a second Poison will override the values from the first one.
  151.         if (caster.isActor())
  152.         {
  153.             this._stateCasterIndex[stateId] = $gameActors._data.indexOf(caster);
  154.         }
  155.         else
  156.         {
  157.             this._stateCasterIndex[stateId] = -$gameTroop._enemies.indexOf(caster);
  158.         }
  159.     };
  160.  
  161.     //At end of turn, we need to call our method that will deal with those parameters:
  162.     _game_battler_onTurnEnd_alias = Game_Battler.prototype.onTurnEnd;
  163.     Game_Battler.prototype.onTurnEnd = function() {
  164.         this.ProcessEllyeStatesDamage();
  165.         _game_battler_onTurnEnd_alias.call(this);
  166.     };
  167.  
  168.     //And our method:
  169.     Game_Battler.prototype.ProcessEllyeStatesDamage = function() {
  170.         this.states().forEach(function(state) {
  171.             if (typeof state.meta !== 'undefined' && state.meta !== null && typeof state.meta.formula !== 'undefined')
  172.             {
  173.                 var caster = this;
  174.                 var element = 1;
  175.                 var variance = 0;
  176.                 var formula = state.meta.formula;
  177.                 var stateCasters = this.stateCasters();
  178.                 var damageOrHeal = -1;
  179.                 if (typeof stateCasters[state.id] !== 'undefined') {
  180.                     caster = stateCasters[state.id];
  181.                 }
  182.                 if (typeof state.meta.element !== 'undefined')
  183.                 {
  184.                     element = Number(state.meta.element || 1);
  185.                 }
  186.                 if (typeof state.meta.variance !== 'undefined')
  187.                 {
  188.                     variance = Number(state.meta.variance || 0);
  189.                 }
  190.                 if (state.meta.heal || state.meta.mp_recover || state.meta.mp_heal)
  191.                 {
  192.                     damageOrHeal = 1;
  193.                 }
  194.                 var value = EllyeSD_EvalFormula(formula, caster, this, damageOrHeal, element, variance);
  195.                 if (state.meta.mp_damage || state.meta.mp_drain)
  196.                 {
  197.                     this.gainMp(value);
  198.                     if (state.meta.mp_drain && caster.isAlive())
  199.                     {
  200.                         caster.gainMp(-value);
  201.                     }
  202.                 }
  203.                 else
  204.                 {
  205.                     this.gainHp(value);
  206.                     if (state.meta.drain && caster.isAlive())
  207.                     {
  208.                         caster.gainHp(value);
  209.                     }
  210.                 }
  211.                 BattleManager.refreshStatus();
  212.                 if (damage_popup_compatibility !== 1)
  213.                 {
  214.                     this.startDamagePopup();
  215.                 }
  216.                 if (this.isActor())
  217.                 {
  218.                     BattleManager._spriteset._actorSprites.forEach(function(sprite)
  219.                     {
  220.                         if (typeof sprite._battler !== 'undefined' && sprite._battler === this)
  221.                         {
  222.                             sprite.update();
  223.                         }
  224.                     }, this);
  225.                 }
  226.                 else
  227.                 {
  228.                     BattleManager._spriteset._enemySprites.forEach(function(sprite)
  229.                     {
  230.                         if (typeof sprite._battler !== 'undefined' && sprite._battler === this)
  231.                         {
  232.                             sprite.update();
  233.                         }
  234.                     }, this);
  235.                 }
  236.                 if (this.isDead())
  237.                 {
  238.                     this.performCollapse();
  239.                 }
  240.             }
  241.         }, this);
  242.     };
  243.  
  244.     //-1 for damage, 1 for heal
  245.     EllyeSD_EvalFormula = function(formula, caster, target, damageOrHeal, elementId, variance) {
  246.         try {
  247.             var a = caster;
  248.             var b = target;
  249.             var v = $gameVariables._data;
  250.             var elementRate = target.elementRate(elementId);
  251.             var damage = Math.floor(Math.max(eval(formula), 0) * damageOrHeal * elementRate);
  252.             var amp = Math.floor(Math.max(Math.abs(damage) * variance / 100, 0));
  253.             var v = Math.floor(Math.randomInt(amp + 1) + Math.randomInt(amp + 1) - amp);
  254.             return damage >= 0 ? damage + v : damage - v;
  255.         } catch (e) {
  256.             return 0;
  257.         }
  258.     };
  259.  
  260.     //Clear everything on battle end:
  261.     _alias_gb_obe = Game_Battler.prototype.onBattleEnd;
  262.     Game_Battler.prototype.onBattleEnd = function() {
  263.         _alias_gb_obe.call(this);
  264.         this._stateCasterIndex = [];
  265.     };
  266. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement