Damage by percentage

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
Larceny
Posts: 9
Joined: September 23rd, 2011, 3:38 am

Damage by percentage

Post by Larceny » September 21st, 2014, 11:13 am

I want/wanted to make a weapon special that makes the attack deal damage as a percentage of the enemy's maximum health. I made a method that works:

Code: Select all

{VARIABLE_OP <unit>.hitpoints add $damage_inflicted}
{VARIABLE_OP <unit>.hitpoints sub "$($($<unit>.max_hitpoints*$damage_inflicted)/100)"}
The percentage of HP to be removed is the damage the attack does. I think that's a good solution overall, because it shows easily what percentage is removed and how much the damage is affected by resistances.

This code, when in an attacker_hits or defender_hits and properly bracketed by unit storage and unstorage, works. However, this method has an unintended side-effect that fights end when the enemy has a lower HP than the "percentage" damage the unit is dealing.
Example: Peasant is has 21 hp, and the attack does 20% damage. The first hit is just fine: the Peasant loses 4 hp, as predicted, and the fight continues. However, the second hit ends the fight. The peasant still has 13 hp when the smoke clears, but there's no chance for a third hit.

I believe this is because the game sees a unit with 17 hp take 20 damage, assumes he's dead, and ends the fight regardless of what the attacker/defender hits have to say.


I began working on a solution: at the start of the fight, the unit's current HP would be stored in a variable, then replaced with 100. After the fight, I would execute this code:

Code: Select all

{VARIABLE_OP <unit>.variables.fieldHP sub "$(($<unit>.max_hitpoints)(100-$<unit>.hitpoints)/100)"}
{VARIABLE <unit>.hitpoints <unit>.variables.fieldHP}
What this is supposed to do is subtract the max hp times the % of health lost from the hp the unit had at the start of the fight. Then replace the current health value with it.

It all seems to work beautifully, except for the fact that, for no apparent reason, the unit's health is set to 0 at the end of the fight. This happens both to Armageddon Drakes and to Peasants, so it's not rounding error. I think. Hopefully.

Any thoughts?

User avatar
zookeeper
WML Wizard
Posts: 9740
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: Damage by percentage

Post by zookeeper » September 21st, 2014, 11:17 am

Well, I don't know if you're missing it in your actual code, but you're missing the dollar sign on the last line:

Code: Select all

-{VARIABLE <unit>.hitpoints <unit>.variables.fieldHP}
+{VARIABLE <unit>.hitpoints $<unit>.variables.fieldHP}

User avatar
Ravana
Forum Moderator
Posts: 2261
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Damage by percentage

Post by Ravana » September 21st, 2014, 11:22 am

This second VARIABLE_OP means (0-[(($<unit>.max_hitpoints)(100-$<unit>.hitpoints)/100)]) so you set health to negative value. Damage as % of hp is already done with https://github.com/ProditorMagnus/Agele ... er.cfg#L69 .

Larceny
Posts: 9
Joined: September 23rd, 2011, 3:38 am

Re: Damage by percentage

Post by Larceny » September 21st, 2014, 1:28 pm

zookeeper wrote:Well, I don't know if you're missing it in your actual code, but you're missing the dollar sign on the last line:

Code: Select all

-{VARIABLE <unit>.hitpoints <unit>.variables.fieldHP}
+{VARIABLE <unit>.hitpoints $<unit>.variables.fieldHP}
Ugh. Those dollar signs slay me. I found a few others missing elsewhere.... Thanks.

Ravana wrote:This second VARIABLE_OP means (0-[(($<unit>.max_hitpoints)(100-$<unit>.hitpoints)/100)]) so you set health to negative value. Damage as % of hp is already done with https://github.com/ProditorMagnus/Agele ... er.cfg#L69 .
Well, I had thought I stored a value in that variable. It turns out I had tried, not that I succeeded. :P After adding some dollar signs, it now actually does hold the value of the hp the unit had when the fight started >.>

As for that percentage-damage-thing, thank you for the link, but it's really not what I'm looking for.


Now the fight progresses all fine and dandy, but the HP is restored to full at the end. Looks like it's time for more "fun" with dollar signs...

After more fun with dollar signs, I have discovered that removing the "/100" from the following line results in the hp being set to very large negative values. Keeping it there results in the hp being restored to its original value.

Code: Select all

{VARIABLE_OP <unit>.variables.fieldHP sub "$($<unit>.max_hitpoints*$(100-$<unit>.hitpoints)/100)"}
I just don't know anymore, guys. WML is crazy.

Post Reply