Efficiently calculate every enemy's remaining-HP:MHP ratio and target the lowest one for a skill?

FirestormNeos

Veteran
Veteran
Joined
Jul 23, 2019
Messages
178
Reaction score
128
First Language
English
Primarily Uses
RMMV
I have a skill called "Natural Selection" that automatically targets the enemy who has taken the most damage proportionate to their health bar. For example, say I had an enemy with 10,000/40,000HP (25%) remaining and another with 2,000/2,500HP (80%) remaining in the same troop when the player uses this skill.

How do I set up a-- I presume a common event triggered by casting the skill? --common event/damage formula that accomplishes this in a manner that's efficient to event?
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
I can see many ways to do this.
  1. Using a troop event to force an action at the end of each turn.
    If you do it this way, you have to use a script call and store the target into a variable

  2. Targeting all enemies at once and calculating the right target using the damage formula.
    When doing it this way, you have to keep in mind that, as any other skill that relies on the damage formula, the formula is only applied when the skill hits. Be sure to set it in the right way.
That said, when we talk about efficiency, we usually consider efficiency related to the time spent to perform a certain action. However, it is also possible to consider efficiency based on the time spent to write the code itself. In this case, there is no big difference in terms of time spent coding the action itself, however, there is a big difference when it comes to how the action is performed and how much the engine need to perform that action.

The code to store your target is always the same:
Code:
$game_variables[var_id_for_hp_rate] = 1
$game_variables[var_id_for_target] = 0
$game_switches[switch_id_for_flag] = false
$game_troop.members.each.with_index do |e, i|
  next unless e.alive?
  if (e.hp_rate < $game_variables[var_id_for_hp_rate])
    $game_variables[var_id_for_hp_rate]
    $game_variables[var_id_for_target] = i
    $game_switches[switch_id_for_flag] = true
  end
end
This allows you to force an action based on the target id (stored in $game_variables[var_id_for_target]), to access the target using $game_troop.members[$game_variables[var_id_for_target]] and only cast the skill if a valid target was found ($game_switches[switch_id_for_flag] is true). When the switch is false, enemies are dead or they did not take damage at all. If the former is true, unless you are using a script to prevent that, BattleManager.judge_win_loss returns true and the battle ends (so the event is not going to be executed and you can discard this option at all, it is not worth considering), if the latter is true, you can simply force the action against a random target.
 

Rinobi

Veteran
Veteran
Joined
Mar 24, 2014
Messages
579
Reaction score
219
First Language
English
Primarily Uses
RMVXA
Just to simplify things. The below code will give you the enemy index with the lowest HP.
Code:
$game_troop.alive_members.min_by {|e| e.hp_rate}.index
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
The below code will give you the enemy index with the lowest HP.
@FirestormNeos just be aware of the fact that it is not the most "efficient" way to do it if you put it in a damage formula that targets all enemies. Not if you mean "efficient" as in "efficient algorithm". However, it definitely is the most efficient if you mean "efficient" as "the fastest solution to write".
 

FirestormNeos

Veteran
Veteran
Joined
Jul 23, 2019
Messages
178
Reaction score
128
First Language
English
Primarily Uses
RMMV
I meant efficient as in "fastest solution to write" when I wrote the but I appreciate the information, @Heirukichi. I learned something new today about algorithm efficiency! :D
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,035
Messages
1,018,455
Members
137,821
Latest member
Capterson
Top