[SOLVED] Divide damage among enemies present

I know this was easy in MV with one of Yanfly's plugins but struggling on this in MZ. I want an aoe attack that does say, a base amount of 100 to one enemy alone, but when there are two enemies present it will do 50 to each, and to three enemies 33.3 etc. Is there a good way to achieve this in the damage formula, or a plugin that can accomplish this?
my approach would be to make the skill hit all enemies, and divide the damage by the number of enemies alive - no plugin needed at all.

two problems:
1) if an early enemy is killed, the damage suddenly increases for the following enemies
2) I don't know the syntax for the number of enemies for the formula in MZ, you'll have to wait for someone to fill that in.
2) I don't know the syntax for the number of enemies for the formula in MZ, you'll have to wait for someone to fill that in.
That's what I'm hoping for, unfortunately there are no glossaries with all of the formula commands for MZ that I could find, just the 101 page for MV with some examples.
use $gameTroop.members().length to get current number of enemies present.
use $gameTroop.members().length to get current number of enemies present.
So, correct me if I'm wrong, but this might be the damage formula:

"100 / $gameTroop.members().length"

Correct?
There are problems with using the approaches already mentioned.
As Andar has also pointed out.



"$gameTroop.members().length" will return the amount of enemies in the enemy troop.
This CAN work, but keep in mind that any enemy that has not appeared yet ("appear halfway" function) or has already been defeated will also be counted. So the damage will always be the same.

For example, if you have 5 enemies and 4 of them are already defeated, the skill will only deal 20 damage to the one remaining enemy instead of 100.



"$gameTroop.aliveMembers().length" will return the amount of enemies that are ACTUALLY present.
This will exclude defeated enemies and those that have not appeared yet.
The problem with this however is that if during the attack any enemies are defeated, the next enemy will take more damage because the amount of present enemies has been reduced.

Example: the skill deals 100 damage and there are 5 enemies with 20 HP each.
Keep in mind, that the formula is evaluated for each enemy individually.

First enemy will take 20 damage and die. ( 100 / 5 )
Second enemy will take 25 damage and die. ( 100 / 4 )
Third enemy will take 33 damage and die. ( 100 / 3 )
Fourth enemy will take 50 damage and die. ( 100 / 2 )
Fifth enemy will take 100 damage and die. ( 100 / 1 )


By default a skill targets All, One, or X Random members of a given unit. I'm guessing you're asking about the "All" scope (e.g. "All Enemies"). If you're actually using a plugin that offers its own custom scope, then I recommend you post a link to that plugin and give details about exactly how you're using it, since it is likely it implements its own way of counting targets. Otherwise...

I suggest this formula:
  • 100 / b.friendsUnit().aliveMembers().length
I.e. "100 divided by the number of non-dead members in the target's friendly unit".

As already mentioned, though, this does not solve the "early deaths" problem. One possible solution might be to set aside a game variable (e.g. variable ID 123) specifically for storing the number of living targets. Then you could try something like this (untested):
  • v[123] = v[123] || b.friendsUnit().aliveMembers().length; 100 / v[123]
I.e. "set game variable 123 equal to itself if it is non-zero, or the number of living opponents if it is; then deal damage equal to 100 divided by the value of game variable 123".

Finally, make a Common Event that sets the variable to 0, and link that to the skill using its Effects list. The event should run once, after all targets have received damage.

If using this approach, make sure not to change the value of that variable anywhere else (or at least not without resetting it to 0).
Thanks for the quick replies! I don't mind the early deaths pumping the numbers for subsequent hits in the same attack, seems logical as long as the main number gets distributed as evenly as possible. Trying to avoid where the aoe special move is less powerful against a single boss enemy.

Edit: Nevermind, I see what you mean about the multiplicatives that don't just stay at one value

Caethyril's solution worked, so do the enemies not all get hit at the exact same time with target all? What it sounds like is that even if you have target all, each hit gets processed individually in order.
Caethyril's solution worked, so do the enemies not all get hit at the exact same time with target all? What it sounds like is that even if you have target all, each hit gets processed individually in order.
Good to hear you got it working! 😂

Without plugins, targets are processed one after another: the damage formula gets evaluated once per target. Most effects (e.g. Add State 50%) also get checked/applied once per target; the Common Event effect is an exception to that rule since it always applies once, after processing all targets.

Plugins are capable of changing any aspect of the game, including this stuff.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-divide-damage-among-enemies-present.138906/