Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
3,136
Reaction score
709
First Language
English
Primarily Uses
RMVXA
Display Damage Value v1.04
by Roninator2​

Introduction
A script I made for my game, but I like to share my work.

Features
- Display potential damage to the enemy for the skill selected
- Accounts for element weakness or defense
- Set text color

How to Use
Put below ▼ Materials

Images
1620920334147.png1620920360331.png1620929073460.pngskill window hidden

Script
Ruby:
# ╔═════════════════════════════════════╦════════════════════╗
# ║ Title: Display Damage Values        ║  Version: 1.04     ║
# ║ Author: Roninator2                  ║                    ║
# ╠═════════════════════════════════════╬════════════════════╣
# ║ Function:                           ║   Date Created     ║
# ║                                     ╠════════════════════╣
# ║ Show potential damage values        ║    13 May 2021     ║
# ╚═════════════════════════════════════╩════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Displays text on battler to show damage range            ║
# ║ for the skill selected.                                  ║
# ╚══════════════════════════════════════════════════════════╝
# ╔═════════════════════════════════════╗
# ║ Terms of use:                       ║
# ║ Free for all uses in RPG Maker      ║
# ╚═════════════════════════════════════╝
# ╔══════════════════════════════════════════════════════════╗
# ║ Updates:                                                 ║
# ║ - When selecting a skill, the window will now hide       ║
# ║ - Corrected damage value checking every frame            ║
# ║ - fixed error when first battler is defeated             ║
# ╚══════════════════════════════════════════════════════════╝
# ╔═════════════════════════════════════╗
# ║ Credits to:                         ║
# ║ Vlue & Tsukihime                    ║
# ╚═════════════════════════════════════╝

module R2_DAM
  #Whether to place the hp bar above or below the enemy
  ABOVE_MONSTER = true
  #The width of the hp bar
  TEXT_WIDTH = 150
  #The height of the hp bar
  TEXT_HEIGHT = 20
  #Offset the hit rate along the x-axis(left,right)
  TEXT_OFFSET_X = -50
  #Offset the hit rate along the y-axis(up,down)
  TEXT_OFFSET_Y = 2

  #The width of the hp bar
  LOC_WIDTH = 100
  #The height of the hp bar
  LOC_HEIGHT = 15

  #Size of the displayed text
  TEXT_SIZE = Font.default_size
  #Font of the displayed text
  TEXT_FONT = Font.default_name

  TEXT = "damage"
 
  DAMAGE_SWITCH = 2
end

class Sprite_Battler
  alias dam_value_update update
  def update
    dam_value_update
    return unless @battler.is_a?(Game_Enemy)
    if @battler
      update_dam_value
    end
  end
  def update_dam_value
    setup_dam_value if @dam_value.nil?
    if @dam_display.nil?
      @dam_display = Sprite_Base.new(self.viewport)
      @dam_display.bitmap = Bitmap.new(R2_DAM::TEXT_WIDTH,R2_DAM::TEXT_SIZE)
      @dam_display.bitmap.font.size = R2_DAM::TEXT_SIZE
      @dam_display.bitmap.font.name = R2_DAM::TEXT_FONT
      @dam_display.x = @dam_value.x + R2_DAM::TEXT_OFFSET_X
      @dam_display.y = @dam_value.y + R2_DAM::TEXT_OFFSET_Y
      @dam_display.z = 110
    end
    determine_dam_visible
    return unless @dam_value.visible
    if @dam_value.opacity != self.opacity
      @dam_value.opacity = self.opacity
    end
    @dam_value.bitmap.clear
    @dam_display.opacity = @dam_value.opacity if @dam_display.opacity != @dam_value.opacity
    @dam_display.bitmap.clear
    text = draw_dam_value(@dam_display.x,@dam_display.y)
    return if text.nil?
    @dam_display.bitmap.draw_text(0,0,R2_DAM::TEXT_WIDTH,@dam_display.height,text)
  end

  def setup_dam_value
    @dam_value = Sprite_Base.new(self.viewport)
    width = R2_DAM::LOC_WIDTH
    height = R2_DAM::LOC_HEIGHT
    @dam_value.bitmap = Bitmap.new(width,height)
    @dam_value.x = self.x + @dam_value.width / 2 + R2_DAM::TEXT_OFFSET_X
    @dam_value.y = self.y + R2_DAM::TEXT_OFFSET_Y - self.bitmap.height - @dam_value.height
    @dam_value.y = self.y + R2_DAM::TEXT_OFFSET_Y unless R2_DAM::ABOVE_MONSTER
    @dam_value.x = 0 if @dam_value.x < 0
    @dam_value.y = 0 if @dam_value.y < 0
    @dam_value.z = 109
  end

  def determine_dam_visible
    if !@battler.alive?
      @dam_value.visible = false
      @dam_display.visible = false
      return if !@battler.alive?
    end
    @dam_value.visible = true
    return unless SceneManager.scene.is_a?(Scene_Battle)
    return unless SceneManager.scene.enemy_window
    @dam_value.visible = SceneManager.scene.target_window_index == @battler.index
    @dam_value.visible = false if !SceneManager.scene.enemy_window.active
    @dam_display.visible = false if !@dam_value.visible
    @dam_display.visible = true if @dam_value.visible
  end

  alias dam_display_dispose dispose
  def dispose
    @dam_value.dispose if @dam_value
    @dam_display.dispose if @dam_display
    dam_display_dispose
  end

  def reveal(battler)
    return if @battler == battler
    @battler = battler
    $game_switches[R2_DAM::DAMAGE_SWITCH] = false
  end

  def draw_dam_value(dx, dy)
    return unless SceneManager.scene_is?(Scene_Battle)
    return unless SceneManager.scene.enemy_window
    reveal(SceneManager.scene.enemy_window.enemy)
    enemy = $game_troop.members[SceneManager.scene.enemy_window.enemy.index]
    user = $game_actors[BattleManager.actor.id]
    item = $data_skills[BattleManager.actor.input.item.id]
    @dam_data = [] if $game_switches[R2_DAM::DAMAGE_SWITCH] == false
    @dam_data = find_dam_value(enemy, user, item) if $game_switches[R2_DAM::DAMAGE_SWITCH] == false
    text = "#{@dam_data[0]} to #{@dam_data[1]} #{R2_DAM::TEXT}"
    $game_switches[R2_DAM::DAMAGE_SWITCH] = true
    return text
  end

  def actor; return BattleManager.actor; end
  
  def enemy; return SceneManager.scene.enemy_window.enemy; end

  def find_dam_value(enemy, user, item)
    damage = actor.find_damage_value(enemy, user, item)
    amp = [damage.abs * item.damage.variance / 100, 0].max.to_i
    low = damage * ((100.00 - item.damage.variance) / 100.00)
    low = low - amp
    low = low.to_i
    high = damage * ((100.00 + item.damage.variance) / 100.00)
    high = high + amp
    high = high.to_i
    return [low, high]
  end

end

class Game_Battler < Game_BattlerBase
  def find_damage_value(enemy, user, item)
    value = item.damage.eval(user, enemy, $game_variables)
    value *= item_element_rate(user, item)
    enemy_element = $data_enemies[enemy.enemy_id]
    item_element = item.damage.element_id
    enemy_element.features.each do |elem|
      if elem.data_id == item_element
        value *= elem.value
        value = value.to_i
      end
    end
    value *= pdr if item.physical?
    value *= mdr if item.magical?
    value *= rec if item.damage.recover?
    return value
  end
end
class Window_BattleEnemy < Window_Selectable
  alias r2_cursor_move_enemy  process_cursor_move
  def process_cursor_move
    r2_cursor_move_enemy
    $game_switches[R2_DAM::DAMAGE_SWITCH] = false
  end
end
class Scene_Battle
  attr_reader  :enemy_window
  def target_window_index
    begin
    @enemy_window.enemy.index
    rescue
      return -1
    end
  end
  def on_skill_ok
    @skill = @skill_window.item
    BattleManager.actor.input.set_skill(@skill.id)
    BattleManager.actor.last_skill.object = @skill
    if !@skill.need_selection?
      @skill_window.hide
      next_command
    elsif @skill.for_opponent?
      @skill_window.hide
      select_enemy_selection
    else
      select_actor_selection
    end
    $game_switches[R2_DAM::DAMAGE_SWITCH] = false
  end
  def on_skill_cancel
    @skill_window.hide
    @actor_command_window.activate
    $game_switches[R2_DAM::DAMAGE_SWITCH] = false
  end
  def on_enemy_cancel
    @enemy_window.hide
    case @actor_command_window.current_symbol
    when :attack
      @actor_command_window.activate
    when :skill
      @skill_window.show
      @skill_window.activate
    when :item
      @item_window.activate
    end
    $game_switches[R2_DAM::DAMAGE_SWITCH] = false
  end
end
Notes
If you want the skill window to be hidden, you'll have to find a script or code snippet for that.
This was bugging me so I added it in. Skill window hides when selecting the enemy.

If the text does not show up for you, adjust the numbers for width/height and location .
I though it wasn't working in my game until I changed the numbers.
I had to change the TEXT_OFFSET_Y from 2 to 100

Credit and Thanks
- Roninator2
- Vlue - for replicating some code
- Tsukihime - for replicating some code

Terms of use
Free for all RPG Maker VX Ace uses
 
Last edited:

Wyrelade

Wyrelade - The one and only.
Veteran
Joined
Apr 11, 2014
Messages
72
Reaction score
26
First Language
Finnish
Primarily Uses
RMVXA
That is very neat. Actually have not seen a tooltip type damage show before. First time for me.

good job!
 

Roninator2

Gamer
Veteran
Joined
May 22, 2016
Messages
3,136
Reaction score
709
First Language
English
Primarily Uses
RMVXA
good job!
Thanks. I actually modified it a bit more to be turned on or off with a switch. Then using CT BOLT's state switch script, I set it so that when the actor uses a skill to apply a state (2-5 turns), the switch is turned on and then the player can see the damage.
But I did that for my game.
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,200
Reaction score
849
First Language
Hungarian
Primarily Uses
RMVXA
Do note that due to how you get the damage value here, everything in the damage formula gets executed more than once, even before the skill/item is actually used. This means that it can mess up some things depending on what you use in your damage formulas (a variable/switch change is probably the most common things people use here for some special cases).

This issue is present in all of these damage prediction scripts, since all of them do the same thing (use the damage formula without accounting for these special cases). At least the variable/switch cases can be fixed easily by saving the old values before the damage calculation part and re-load it after.
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,200
Reaction score
849
First Language
Hungarian
Primarily Uses
RMVXA
Ohh, I didn't even notice that part. :D

I meant this line:
Code:
value = item.damage.eval(user, enemy, $game_variables)
This will execute whatever is used in the damage formula.
For example:
Code:
dmg = v[15] % 3 == 0 ? a.atk * 10 : -a.atk; $game_variables[15] += 1; dmg
This will make the skill do 10 * ATK damage if variable 15 is divisible by 3, otherwise it heals the target.
Variable 15 is increased by 1 every time this skill is used.
That's how it should work.
I'm kinda rusty with the damage formula things, so there may be some errors in the example, but I hope you get the idea behind it.

But because you execute the damage formula to calculate the possible damage, variable 15 will be increased for every single calculation without actually using the skill, effectively breaking the skill.
This is just one example, but I imagine a lot of people who actually put some effort to come up with some more unique skills will have at least some similar cases.

To fix this, you can save the switch and variable arrays before the damage calculation, and re-load it when you are done with it. Something like:
Code:
ovars = Marshal.load(Marshal.dump($game_variables))
oswitchs = $game_switches.clone
# Insert damage calculation here
$game_variables = ovars
$game_switches = oswitchs
I used marshal for the variables because people could use other things there, like strings and even other arrays/hashes, so simply cloning it may not be enough.

Other than these cases, your script should work fine. It's only an issue when something like the example I gave is used in the damage formulas.
 

Latest Threads

Latest Profile Posts

Currently trying to add items in the same style as Kyrise's excellent icons. I like having a large variety of items :3 KyriseandMe.PNG
Create MiniMaps, Sprites for MV Enemies, & Aethereal Plane Battlebacks | RPG Maker News #74

Vtubers Vs MOTHER.png

This might be my next project. You like it?
I'm off to search the master script list for my skill issue. HOORAY! FUN!
Fun fact: Digimon is an isekai :kaoswt2:

Forum statistics

Threads
112,328
Messages
1,067,413
Members
145,964
Latest member
Senkiji
Top