I suspect both Game_Action.prototype.evalDamageFormula and Game_Action.prototype.applyGuard can be quite hard to debug:
Right now I can find 2 problems for Game_Action.prototype.evalDamageFormula and 1 problem for Game_Action.prototype.applyGuard:
The below damage flow(from making hp/mp damage to setting the final hp/mp value) shows why the return value of Game_Action.prototype.evalDamageFormula/Game_Action.prototype.applyGuard will be carried all along and unstopped by all the subsequent validation checks, leading to the final hp/mp being NaN:
I'd suggest both Game_Action.prototype.evalDamageFormula and Game_Action.prototype.applyGuard to be changed into something like these:
If throwing exceptions here can become too intrusive, RMMV users can still keep the console open and constantly check what's logged there.
Do you think the default RMMV damage formula evaluation can be hard to debug? Let's share your experiences, observations and views here 🙂
Game_Action.prototype.evalDamageFormula = function(target) { try { var item = this.item(); var a = this.subject(); var b = target; var v = $gameVariables._data; var sign = ([3, 4].contains(item.damage.type) ? -1 : 1); return Math.max(eval(item.damage.formula), 0) * sign; } catch (e) { return 0; }};
Code:
Game_Action.prototype.applyGuard = function(damage, target) { return damage / (damage > 0 && target.isGuard() ? 2 * target.grd : 1);};
Game_Action.prototype.evalDamageFormula -
1. If eval(item.damage.formula) returns NaN, Math.max(eval(item.damage.formula), 0) * sign will also return NaN, which won't be caught by catch (e) and will be carried all along as all the subsequent validation checks will fail to stop NaN from being passed, causing the final hp/mp to be NaN.
2. If an exception's thrown which is caught by catch (e), 0 will be returned instead, meaning such exception handling is effectively hiding the error, as sometimes a damage formula can legitimately return 0.
Game_Action.prototype.applyGuard -
1. If at least 1 of the targets having grd as 0% are guarding when hit, damage / (damage > 0 && target.isGuard() ? 2 * target.grd : 1) will return NaN which will be carried all along as all the subsequent validation checks will fail to stop NaN from being passed, causing the final hp/mp to be NaN.
1. If eval(item.damage.formula) returns NaN, Math.max(eval(item.damage.formula), 0) * sign will also return NaN, which won't be caught by catch (e) and will be carried all along as all the subsequent validation checks will fail to stop NaN from being passed, causing the final hp/mp to be NaN.
2. If an exception's thrown which is caught by catch (e), 0 will be returned instead, meaning such exception handling is effectively hiding the error, as sometimes a damage formula can legitimately return 0.
Game_Action.prototype.applyGuard -
1. If at least 1 of the targets having grd as 0% are guarding when hit, damage / (damage > 0 && target.isGuard() ? 2 * target.grd : 1) will return NaN which will be carried all along as all the subsequent validation checks will fail to stop NaN from being passed, causing the final hp/mp to be NaN.
Game_Action.prototype.makeDamageValue = function(target, critical) { var item = this.item(); var baseValue = this.evalDamageFormula(target); var value = baseValue * this.calcElementRate(target); if (this.isPhysical()) { value *= target.pdr; } if (this.isMagical()) { value *= target.mdr; } if (baseValue < 0) { value *= target.rec; } if (critical) { value = this.applyCritical(value); } value = this.applyVariance(value, item.damage.variance); value = this.applyGuard(value, target); value = Math.round(value); return value;};Game_Action.prototype.executeDamage = function(target, value) { var result = target.result(); if (value === 0) { result.critical = false; } if (this.isHpEffect()) { this.executeHpDamage(target, value); } if (this.isMpEffect()) { this.executeMpDamage(target, value); }};Game_Action.prototype.executeHpDamage = function(target, value) { if (this.isDrain()) { value = Math.min(target.hp, value); } this.makeSuccess(target); target.gainHp(-value); if (value > 0) { target.onDamage(value); } this.gainDrainedHp(value);};Game_Action.prototype.executeMpDamage = function(target, value) { if (!this.isMpRecover()) { value = Math.min(target.mp, value); } if (value !== 0) { this.makeSuccess(target); } target.gainMp(-value); this.gainDrainedMp(value);};Game_Action.prototype.gainDrainedHp = function(value) { if (this.isDrain()) { this.subject().gainHp(value); }};Game_Action.prototype.gainDrainedMp = function(value) { if (this.isDrain()) { this.subject().gainMp(value); }};
Code:
Game_Battler.prototype.gainHp = function(value) { this._result.hpDamage = -value; this._result.hpAffected = true; this.setHp(this.hp + value);};Game_Battler.prototype.gainMp = function(value) { this._result.mpDamage = -value; this.setMp(this.mp + value);};
Code:
Game_BattlerBase.prototype.setHp = function(hp) { this._hp = hp; this.refresh();};Game_BattlerBase.prototype.setMp = function(mp) { this._mp = mp; this.refresh();};
Code:
Game_BattlerBase.prototype.refresh = function() { this.stateResistSet().forEach(function(stateId) { this.eraseState(stateId); }, this); this._hp = this._hp.clamp(0, this.mhp); this._mp = this._mp.clamp(0, this.mmp); this._tp = this._tp.clamp(0, this.maxTp());};
Code:
/** * Returns a number whose value is limited to the given range. * * @method Number.prototype.clamp * @param {Number} min The lower boundary * @param {Number} max The upper boundary * @return {Number} A number in the range (min, max) */Number.prototype.clamp = function(min, max) { return Math.min(Math.max(this, min), max);};
Game_Action.prototype.evalDamageFormula = function(target) { try { var item = this.item(); var a = this.subject(); var b = target; var v = $gameVariables._data; var sign = ([3, 4].contains(item.damage.type) ? -1 : 1); // Rewritten var value = Math.max(eval(item.damage.formula), 0) * sign; if (isNan(value)) { console.log("The damage formula: " + item.damage.formula + " of item: " + item.name + " used by subject: " + a.name + " targeting target: " + b.name + " has return NaN."); // Exceptions can also be thrown right afterwards if it'd do more good than harm here in most cases } return value; // } catch (e) { // Added console.log("The damage formula: " + item.damage.formula + " of item: " + item.name + " used by subject: " + a.name + " targeting target: " + b.name + " has caused the below error:"); console.log(e.name, e.message); // return 0; }};
Code:
Game_Action.prototype.applyGuard = function(damage, target) { var value = damage / (damage > 0 && target.isGuard() ? 2 * target.grd : 1); if (isNan(value)) { console.log("The grd of target: " + target.name + " is 0"); // Exceptions can also be thrown right afterwards if it'd do more good than harm here in most cases }};
Do you think the default RMMV damage formula evaluation can be hard to debug? Let's share your experiences, observations and views here 🙂