Damage by percentage
Moderator: Forum Moderators
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.
Damage by percentage
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:
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:
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?
Code: Select all
{VARIABLE_OP <unit>.hitpoints add $damage_inflicted}
{VARIABLE_OP <unit>.hitpoints sub "$($($<unit>.max_hitpoints*$damage_inflicted)/100)"}
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}
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?
Re: Damage by percentage
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}
Re: Damage by percentage
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 .
Re: Damage by percentage
Ugh. Those dollar signs slay me. I found a few others missing elsewhere.... Thanks.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}
Well, I had thought I stored a value in that variable. It turns out I had tried, not that I succeeded. After adding some dollar signs, it now actually does hold the value of the hp the unit had when the fight started >.>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 .
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)"}