How do you write a damage formula that makes it so you can never deal 0 dmg. You will always deal at the very least 1 dmg even if the target's defense is higher than your attack.
damage mechanic
RPG Maker MV SupportMost do it with a plug-in, and the plug-in is set up so that it sets all damage to 1 if it computes it should be 0. Otherwise you can use a javascript command to do this in the damage formula, or just by a cute damage formula where getting very low numbers is near impossible.
Moved to MV Support
You can easily do it inside the damage formula:
In English,
Does the formula equal zero?
If so, put 1 instead. Otherwise, put the formula.
Replace formula with whatever your formula is
Code:
formula === 0 ? 1 : formula
In English,
Does the formula equal zero?
If so, put 1 instead. Otherwise, put the formula.
Replace formula with whatever your formula is
So for example, if a.atk < b.def, the value will return to 0. I can't edit what the value returns to by default. How would I rewrite this?
Code:
Math.max(1, a.atk * meowever - b.def * meowever)
Thank you all for helping me. 

Can you explain how this works? Is the first number inside the brackets the result of the formula it will give minimally, instead of 0?
I believe so. It's what I use, I never messed with it to know.
So I just tested this out:
Code:
Math.max(1, a.atk * 4 - b.def * 2)
And it returned 0 damage on an enemy with maximum defense. What'd I do wrong?
I am assuming that if you have variance on the damage formula, that it could be changing the result?
Hmm.. maybe put a ; at the end?
The problem with trying to do this kind of stuff with formulas is that it's affected afterwards by variance, elemental weakpoints/resistances, damage rates, critical hits (which could give you a slightly strange 3 damage instead of 1), etc.
That's why plugins like Yanfly's Damage Core are usually the better way to go, although obviously lightweight plugins are better than ones which affect a bunch of stuff you don't want to change. Or, if you know JS, you can change a few lines of code yourself to set the minimum damage to 1 right before it's applied and displayed.
That's why plugins like Yanfly's Damage Core are usually the better way to go, although obviously lightweight plugins are better than ones which affect a bunch of stuff you don't want to change. Or, if you know JS, you can change a few lines of code yourself to set the minimum damage to 1 right before it's applied and displayed.
There is always "1 + [formula]", where 1 is a solid number of damage the weapon would do regardless of user.
For example... even in the weakest hands, a sharp blade would still to 1d4 damage, where 1 would be minimum amount possible.
After that, practiced and tried used would do "1 + a.atk - b.def".
A strong character might do "1 + a.atk * a.STRENGTH - b.def"
A not very strong but a nimble character might strike more times, but lighter. "1 + ( a.atk / 2 ) * a.AGILITY - b.def"
You can also write something that would look at the strongest stat out of the two and decide more precisely how the character uses the said weapon. =)
One of the best tool to deduct these I've used was "NPC sheets".
Mei is a made, so she is great with spells, but can still hit sometimes with a knife for 1/2 of its damage, or min = 1
George is a strong fighter - he picks up an ax and applies his strength for x3 damage
And so on.
In my works I prefer to step outside of the simple math formula and work "by character" instead. It does get complex more often than simple, but it looks kinda cool. And if you add "equip comments" ("Oh, a new axe! I can definitely cleave some ork skulls with this!" [George's strength will deal x3 the normal damage of "Battle Axe"])
For example... even in the weakest hands, a sharp blade would still to 1d4 damage, where 1 would be minimum amount possible.
After that, practiced and tried used would do "1 + a.atk - b.def".
A strong character might do "1 + a.atk * a.STRENGTH - b.def"
A not very strong but a nimble character might strike more times, but lighter. "1 + ( a.atk / 2 ) * a.AGILITY - b.def"
You can also write something that would look at the strongest stat out of the two and decide more precisely how the character uses the said weapon. =)
One of the best tool to deduct these I've used was "NPC sheets".
Mei is a made, so she is great with spells, but can still hit sometimes with a knife for 1/2 of its damage, or min = 1
George is a strong fighter - he picks up an ax and applies his strength for x3 damage
And so on.
In my works I prefer to step outside of the simple math formula and work "by character" instead. It does get complex more often than simple, but it looks kinda cool. And if you add "equip comments" ("Oh, a new axe! I can definitely cleave some ork skulls with this!" [George's strength will deal x3 the normal damage of "Battle Axe"])
@WickedWolfy : Actually, 1 + a.atk - b.def can still return 0, as it is all evaulated at once. For example, if your ATK is 5 and the DEF is 50, the math is:
1 + 5 - 50 = -46, which returns 0 in the engine.
1 + 5 - 50 = -46, which returns 0 in the engine.
My example was slightly shorthanded. Thus my mention of 1 being "solid number". Thanks for poking holes in my terrible math. =D
Yes, in perfect world a max-0-value (or ceil, or floor... depending on what is the perfect goal) should be taken of the calculation and a solid number added. There are a number of ways math can be done, based on the situation.
I wanted to focus less on the math that was already mentioned and more on assigning class/character damage instead, as per my later examples.
My example with multiplication by strength and/or agility is flawed too, if you want to pick at my terrible short-hand maths. 😉
Fine... You got me into math thinking now. This is along the lines of the formula I would use for my projects, where applicable
Code:
Math.max( Math.floor((Math.random() * 10) + 1), a.atk * Math.max( a.STRENGTH * 2/3, a.AGIL * 1/3 ) - b.def );
I get what you meant. The math.max (for MV) fixes the issue heavily. Or you can do what I do:
(a.atk + STR + 20) * (a.atk) / (b.def + 20) + a.level
The first part will always be >= 0, then the a.level makes sure something positive is added on.
(a.atk + STR + 20) * (a.atk) / (b.def + 20) + a.level
The first part will always be >= 0, then the a.level makes sure something positive is added on.
Interesting. How would you write this in English? I am a bit shy using a.atk squared, because this is what you would end up if you open up the parenthesis.
( ( a.atk^2 + STR*a.atk + 20 * a.atk ) / ( b.def + 20 ) ) + a.level
Technically, because level is never lower than zero, and there is no subtraction and only devision, the result will never be less than 1.
Although funny things might happen if the enemy defense is negative 20 =D
Gee... This got me rallied up on damage maths and I even got a plugin idea in mind =\
Original thread: https://forums.rpgmakerweb.com/threads/damage-mechanic.97050/