Damage Formula Help\Review Damage Formula pls?

I'm not very skilled with coding/working with syntax, but basically I have this damage formula;

if a.hp > 101; a.hp = a.hp - 100; else; a.hp = 1; end; a.add_state(14)

The idea is to create a powerful weapon technique not based in magic, but in some super-human level of skill mastery. Like something an anime character uses when he's desperate/as a finishing move, but never spams (because of the implied exertion of the technique). The formula checks whether the user's hp is over 101 and if it is, the user loses 100 hp; In all other cases (user has less then 101 hp), it just sets the user's hp 1 to avoid KOing them. Then a regen state is added to recover the lost hp (the physical exertion incurred from using the technique) over time.

What's the best way to make the lost hp percentage-based? Like if I want the user to lose 30% of their max hp as hp everytime 'Blade Projectile' is performed.

Also, is there a good way to clean the formula up? It barely fits in the available box and the part of the formula that inflicts the skill's actual damage to the enemy is held in the formula box of a separate skill (I'm using a script to chain skills).
If you want the user to lose 30% of maximum health upon performing the skill, that's easy, you just have to multiply said percentage by the maximum hp.

Ruby:
a.hp -= a.mhp * 0.3

Also, your code can easily be cleaned up in any of the following ways (each line is a possible solution).

Ruby:
a.hp = [a.hp - 100, 1].max; a.add_state(14)

a.hp = a.hp > 100 ? a.hp - 100 : 1; a.add_state(14)

v = a.hp - 100; a.hp = v > 0 ? v : 1; a.add_state(14)

All of them are much shorter than your previous formula and will allow you to achieve the same result.

There are also other ways of putting bigger code chunks in your formula, such as creating your own snippet of pre-set formulae and invoking them in your formula box, but I am pretty confident that you will be able to figure that out as you keep learning how the engine works.
there is an additional problem with your code:
the damage is always the last number processed, so the damage formula should never end with a command like that addstate. please move that to the front of the formula.
What you put in the damage formula could also be put in a common event called by the skill (it seems easier to me, you don't have to know all the script calls then and the length wouldn't be a problem either). Of course, you might have your own reasons not to use that route, just thought I'd mention it :LZSsmile:
If you want the user to lose 30% of maximum health upon performing the skill, that's easy, you just have to multiply said percentage by the maximum hp.

Ruby:
a.hp -= a.mhp * 0.3

Also, your code can easily be cleaned up in any of the following ways (each line is a possible solution).

Ruby:
a.hp = [a.hp - 100, 1].max; a.add_state(14)

a.hp = a.hp > 100 ? a.hp - 100 : 1; a.add_state(14)

v = a.hp - 100; a.hp = v > 0 ? v : 1; a.add_state(14)

All of them are much shorter than your previous formula and will allow you to achieve the same result.

There are also other ways of putting bigger code chunks in your formula, such as creating your own snippet of pre-set formulae and invoking them in your formula box, but I am pretty confident that you will be able to figure that out as you keep learning how the engine works.
Thank you so much for your help, @Heirukichi and breaking that down for me! I had to look up what the -= operator did to make sure I was using it correctly.

Also thank you, @Andar for the heads up! I corrected the code. But I assumed any if-then statements should always be added first in the damage formula. You see my test version of the formula was
"a.atk * 4 - b.def; if a.hp > 100; a.hp = a.hp - 100; else; a.hp = 1; end"
and it would do this weird thing where the damage the skill inflicted on the enemy got exponentially lower/higher as the user's hp got lower/higher. I assumed it was from some weird interaction/poor syntax involving the self-damage portion of the skill so I changed it to
"if a.hp > 100; a.hp = a.hp - 100; else; a.hp = 1; end; a.atk * 4 - b.def"
putting the skill damage portion as the last part and it didn't happen anymore.

@NoPatience One of the mission-critical battle scripts I'm using seems to disable the function of calling certain types of common events during a battle, but thanks though.

Anyway, this is what I've got so far;

"a.add_state(14); a.hp = a.hp > a.mhp * 0.3 ? a.hp -= a.mhp * 0.3: 1" It works like magic 🙂

There's no need for the formula to be cleaned up more than this, but it could probably be shortened even further. Thanks again @Heirukichi !
Oh, one last thing, could I have some help with this damage formula?

"if a.state?(13) or a.state?(14); a.atk * 4 - b.def; else; a.atk - b.def; end"

It's for a skill that changes the damage inflicted depending on whether the user is afflicted by one or more specific states.

The formula works fine so far, but I also want it to remove the state(s) after the damage is performed.

Where do I put "a.remove_state(13, 14)" in the formula? I've tried a few different things, but while they don't crash the game, they cause the formula to spit out zeros as inflicted damage.

As @Andar said, I'm not supposed to end formulas with commands. Also don't worry about the character limit of the damage formula, I'm using a method to use bigger code chunks like @Heirukichi said. Thank you 🙂
Where do I put "a.remove_state(13, 14)" in the formula?
nowhere, because that is not a valid command.
but you can put
a.remove_state(13); a.remove_state(14);
in front of the formula.

Additionally, you need to use { and } as well as ( and ) to tell the computer which part of your conditional sequences belong to which part of the functions
nowhere, because that is not a valid command.
but you can put
a.remove_state(13); a.remove_state(14);
in front of the formula.

Additionally, you need to use { and } as well as ( and ) to tell the computer which part of your conditional sequences belong to which part of the functions
I'm still a bit confused;

"a.remove_state(13); a.remove_state(14); if a.state?(14) or a.state?(13); a.atk * 4 - b.def; else; a.atk - b.def; end" I've got this, but won't putting those commands in front and removing both states before the check can be made always result in the else formula? Also, I'm not really sure how to use {} and () correctly to define conditional sequences
The following should apply "a.atk * 4 - b.def" if #13 and/or #14 was present and "a.atk - b.def" if not:

Ruby:
n = [13, 14].any? {|i| a.state?(i) }; [13, 14].each {|i| a.remove_state(i) }; n ? a.atk * 4 - b.def : a.atk - b.def

Explanation of what's going on there:
"#any?" returns a "true" as soon as one element from the given array ("[13, 14]") produces a non-false non-nil result inside "{}". #each is for batching multiple parameters for repeating the same process (which is "a.remove_state(i)" here, again with 13 and 14).

When the states are removed, we can check again what the temporary variable ("n") had stored before, based on if state #13 and/or #14 was present before we removed them, and since if-else-end is not so good for use in long one-liners, I replaced them with ternary operators (STATEMENT ? TRUECASE : FALSECASE).

~炬燵あ
The following should apply "a.atk * 4 - b.def" if #13 and/or #14 was present and "a.atk - b.def" if not:

Ruby:
n = [13, 14].any? {|i| a.state?(i) }; [13, 14].each {|i| a.remove_state(i) }; n ? a.atk * 4 - b.def : a.atk - b.def

Explanation of what's going on there:
"#any?" returns a "true" as soon as one element from the given array ("[13, 14]") produces a non-false non-nil result inside "{}". #each is for batching multiple parameters for repeating the same process (which is "a.remove_state(i)" here, again with 13 and 14).

When the states are removed, we can check again what the temporary variable ("n") had stored before, based on if state #13 and/or #14 was present before we removed them, and since if-else-end is not so good for use in long one-liners, I replaced them with ternary operators (STATEMENT ? TRUECASE : FALSECASE).

~炬燵あ
It works! I read your post a few times, I understand your explanation of arrays and 'each', but when I read after that and look back at the damage formula to try to interpret the syntax, it just looks really intimidating and my brain starts to melt.


face-with-spiral-eyes_1f635-200d-1f4ab.png

Thank you so much @AkiraKotatsuhime ! Programming was always my lowest scoring class. I wish I was able to understand what's going on better, so I can appreciate your work on a deeper level.
Spoiler: Shortening the formula even further
Ruby:
n = [13, 14].any? {|i| a.state?(i) }; [13, 14].each {|i| a.remove_state(i) }; n ? a.atk * 4 - b.def : a.atk - b.def
I think this might be shortened even further by removing the ternary operator at the end and using it only on the attack multiplier. Of course, it has no impact at all on the overall behaviour, but it is more compliant to the DRY paradigm, since the two formulae are pretty similar.

Since the formula box is small, in case anything needs to be added later, I usually try to keep things as short as possible.

Ruby:
n = ([13, 14].any? { |i| a.state?(i) } ? 4 : 1); [13, 14].each { |i| a.remove_state(i) }; a.atk * n - b.def;

@Solr please, keep in mind that formulae are only evaluated when the attack hits. If you want your skill to consume either of those states whenever cast, then you have to think of a different approach.
For some more clarification:
Though Heirukichi's formula will work, I wanted to offer a solution that is suitable to be easier adapted to potentially a lot more use-cases where the two damage-formulas possible might be completely different from each other, even for people not so familiar with programming once they understand the rough structure of it (which I tried to explain, but I'm not so good at). This other variant is a bit shorter, but also more aligned to just fit that one specific skill.

~炬燵あ
Spoiler: Shortening the formula even further
I think this might be shortened even further by removing the ternary operator at the end and using it only on the attack multiplier. Of course, it has no impact at all on the overall behaviour, but it is more compliant to the DRY paradigm, since the two formulae are pretty similar.

Since the formula box is small, in case anything needs to be added later, I usually try to keep things as short as possible.

Ruby:
n = ([13, 14].any? { |i| a.state?(i) } ? 4 : 1); [13, 14].each { |i| a.remove_state(i) }; a.atk * n - b.def;

@Solr please, keep in mind that formulae are only evaluated when the attack hits. If you want your skill to consume either of those states whenever cast, then you have to think of a different approach.
Thanks for the alternative option. I can't describe it very well, but I think I understand how you shortened the formula (by working the if/else part into the variable itself) at the cost of making the damage the formula calculates much less modifiable. I tested it, and it also works without issue. Also thanks for the heads up about the removal only applying when the skill actually hits. It shouldn't cause any issues but thank you for informing me incase this could cause some wierd oversight/mysterious interaction.

@AkiraKotatsuhime Thank you again! 🙂 I appreciate you going the extra step to provide a more adaptable framework. I'm sure you're doing a great job of explaining it, I'm just lagging behind and I'm having trouble understanding it lol. But I think I get the fundamental differences between your solution and @Heirukichi 's.

Original thread: https://forums.rpgmakerweb.com/threads/damage-formula-help-review-damage-formula-pls.150035/