Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Rounding numbers in RMXP? RESOLVED

Hey,

So I've been getting real adventurous messing around with scripts, and I have something going. All I need for it to fully work is some way to round a number to -7 places. I found a term "round_to(x)" which RPG Maker didn't seem to recognize. Please help and remember that I don't actually know how to script when replying.
 
Code:
class Float

  def round

    return (self+0.5).floor if self > 0.0

    return (self-0.5).ceil  if self < 0.0

    return 0

  end

  

  def round_to(digits = 0)

    mult = 10 ** digits

    return (self * mult).round / mult.to_f

  end

end
 
"round" is a default method in Ruby, whereas "round_to" is not. You'll have to use Neo-Bahamut's script if you want the latter feature (plug it in anywhere above Main should work).
 
Code:
#==============================================================================

# ** Game_Battler (part 3)

#------------------------------------------------------------------------------

#  This class deals with battlers. It's used as a superclass for the Game_Actor

#  and Game_Enemy classes.

#==============================================================================

 

class Game_Battler

  #--------------------------------------------------------------------------

  # * Determine Usable Skills

  #     skill_id : skill ID

  #--------------------------------------------------------------------------

  

 

 

  

  def skill_can_use?(skill_id)

    # If there's not enough SP, the skill cannot be used.

    if $data_skills[skill_id].sp_cost > self.sp

      return false

    end

    # Unusable if incapacitated

    if dead?

      return false

    end

    # If silent, only physical skills can be used

    if $data_skills[skill_id].atk_f == 0 and self.restriction == 1

      return false

    end

    # Get usable time

    occasion = $data_skills[skill_id].occasion

    # If in battle

    if $game_temp.in_battle

      # Usable with [Normal] and [Only Battle]

      return (occasion == 0 or occasion == 1)

    # If not in battle

    else

      # Usable with [Normal] and [Only Menu]

      return (occasion == 0 or occasion == 2)

    end

  end

  #--------------------------------------------------------------------------

  # * Applying Normal Attack Effects

  #     attacker : battler

  #--------------------------------------------------------------------------

  def attack_effect(attacker)

    # Clear critical flag

    self.critical = false

    # First hit detection  

    # Calculate basic damage

    atk = [attacker.atk - self.pdef / 2, 0].max

    self.damage = atk * (20 + attacker.str) / 20

    # Element correction

    self.damage *= elements_correct(attacker.element_set)

    self.damage /= 100

    # If damage value is strictly positive

    if self.damage > 0

      # Critical correction

      if rand(100) < 1

        self.damage *= 2

        self.critical = true

      end

      # Guard correction

      if self.guarding?

        self.damage /= 2

      end

    end

    # Dispersion

    if self.damage.abs > 0

      amp = [self.damage.abs * 15 / 100, 1].max

      self.damage += rand(amp+1) + rand(amp+1) - amp

    end

    # Second hit detection

    hit = self.damage < 0 ? attacker.hit : attacker.hit - self.eva

    hit = self.cant_evade? ? attacker.hit : hit

    hit_result = (rand(100) < hit)

    # If hit occurs

    if hit_result == true

      # State Removed by Shock

      remove_states_shock

      # Substract damage from HP

      self.hp -= self.damage

      # State change

      @state_changed = false

      states_plus(attacker.plus_state_set)

      states_minus(attacker.minus_state_set)

    # When missing

    else

      # Set damage to "Miss"

      self.damage = "Miss"

      # Clear critical flag

      self.critical = false

    end

    # End Method

    return true

  end

  #--------------------------------------------------------------------------

  # * Apply Skill Effects

  #     user  : the one using skills (battler)

  #     skill : skill

  #--------------------------------------------------------------------------

  def skill_effect(user, skill)

    # Clear critical flag

    self.critical = false

    # If skill scope is for ally with 1 or more HP, and your own HP = 0,

    # or skill scope is for ally with 0, and your own HP = 1 or more

    if ((skill.scope == 3 or skill.scope == 4) and self.hp == 0) or

       ((skill.scope == 5 or skill.scope == 6) and self.hp >= 1)

      # End Method

      return false

    end

    # Clear effective flag

    effective = false

    # Set effective flag if common ID is effective

    effective |= skill.common_event_id > 0

    # Calculate power

    power = user.atk * skill.atk_f + skill.power

    if power > 0

      power -= self.pdef * skill.pdef_f / 200

      power -= self.mdef * skill.mdef_f / 200

      power = [power, 0].max

    end

    # Calculate rate

    rate = 20

    rate += (user.str * skill.str_f / 50)

    rate += (user.agi * skill.agi_f / 100)

    rate += (user.int * skill.int_f / 50)

    # Calculate basic damage

    self.damage = power * rate

    self.damage *= skill.power / 10

    # Element correction

    self.damage *= elements_correct(skill.element_set)

    self.damage *= 100

      # Critical correction

      if rand(100) < skill.dex_f

        self.damage *= 2

        self.critical = true

      end

      # If damage value is strictly positive

      if self.damage > 0

      # Guard correction

      if self.guarding?

        self.damage /= 3

      end

      end

    # Dispersion

    self.damage.round_to(-7)

    self.damage /= 100000000

    self.damage += 1

    if skill.variance > 0 and self.damage.abs > 0

      amp = [self.damage.abs * skill.variance / 100, 1].max

      self.damage += rand(amp+1) + rand(amp+1) - amp

 

    end

    # Second hit detection

    acc = skill.hit

    if skill.atk_f > 0

      acc += user.hit - 100

    end

    eva = self.eva * skill.eva_f / 100

    hit = self.damage < 0 ? acc : acc - eva

    hit = self.cant_evade? ? acc : hit

    hit_result = (rand(100) < hit)

    # Set effective flag if skill is uncertain

    effective |= hit < 100

    # If hit occurs

    if hit_result == true

      # If physical attack has power other than 0

      if skill.power != 0 and skill.atk_f > 0

        # State Removed by Shock

        remove_states_shock

        # Set to effective flag

        effective = true

      end

      # Substract damage from HP

      last_hp = self.hp

      self.hp -= self.damage

      effective |= self.hp != last_hp

      # State change

      @state_changed = false

      effective |= states_plus(skill.plus_state_set)

      effective |= states_minus(skill.minus_state_set)

      # If power is 0

      if skill.power == 0

        # Set damage to an empty string

        self.damage = ""

        # If state is unchanged

        unless @state_changed

          # Set damage to "Miss"

          self.damage = "Miss"

        end

      end

    # If miss occurs

    else

      # Set damage to "Miss"

      self.damage = "Miss"

          # Clear critical flag

    self.critical = false

    end

    # If not in battle

    unless $game_temp.in_battle

      # Set damage to nil

      self.damage = nil

    end

    # End Method

    return effective

  end

  #--------------------------------------------------------------------------

  # * Application of Item Effects

  #     item : item

  #--------------------------------------------------------------------------

  def item_effect(item)

    # Clear critical flag

    self.critical = false

    # If item scope is for ally with 1 or more HP, and your own HP = 0,

    # or item scope is for ally with 0 HP, and your own HP = 1 or more

    if ((item.scope == 3 or item.scope == 4) and self.hp == 0) or

       ((item.scope == 5 or item.scope == 6) and self.hp >= 1)

      # End Method

      return false

    end

    # Clear effective flag

    effective = false

    # Set effective flag if common ID is effective

    effective |= item.common_event_id > 0

    # Determine hit

    hit_result = (rand(100) < item.hit)

    # Set effective flag is skill is uncertain

    effective |= item.hit < 100

    # If hit occurs

    if hit_result == true

      # Calculate amount of recovery

      recover_hp = maxhp * item.recover_hp_rate / 100 + item.recover_hp

      recover_sp = maxsp * item.recover_sp_rate / 100 + item.recover_sp

      if recover_hp < 0

        recover_hp += self.pdef * item.pdef_f / 20

        recover_hp += self.mdef * item.mdef_f / 20

        recover_hp = [recover_hp, 0].min

      end

      # Element correction

      recover_hp *= elements_correct(item.element_set)

      recover_hp /= 100

      recover_sp *= elements_correct(item.element_set)

      recover_sp /= 100

      # Dispersion

      if item.variance > 0 and recover_hp.abs > 0

        amp = [recover_hp.abs * item.variance / 100, 1].max

        recover_hp += rand(amp+1) + rand(amp+1) - amp

      end

      if item.variance > 0 and recover_sp.abs > 0

        amp = [recover_sp.abs * item.variance / 100, 1].max

        recover_sp += rand(amp+1) + rand(amp+1) - amp

      end

      # If recovery code is negative

      if recover_hp < 0

        # Guard correction

        if self.guarding?

          recover_hp /= 2

        end

      end

      # Set damage value and reverse HP recovery amount

      self.damage = -recover_hp

      # HP and SP recovery

      last_hp = self.hp

      last_sp = self.sp

      self.hp += recover_hp

      self.sp += recover_sp

      effective |= self.hp != last_hp

      effective |= self.sp != last_sp

      # State change

      @state_changed = false

      effective |= states_plus(item.plus_state_set)

      effective |= states_minus(item.minus_state_set)

      # If parameter value increase is effective

      if item.parameter_type > 0 and item.parameter_points != 0

        # Branch by parameter

        case item.parameter_type

        when 1  # Max HP

          @maxhp_plus += item.parameter_points

        when 2  # Max SP

          @maxsp_plus += item.parameter_points

        when 3  # Strength

          @str_plus += item.parameter_points

        when 4  # Dexterity

          @dex_plus += item.parameter_points

        when 5  # Agility

          @agi_plus += item.parameter_points

        when 6  # Intelligence

          @int_plus += item.parameter_points

        end

        # Set to effective flag

        effective = true

      end

      # If HP recovery rate and recovery amount are 0

      if item.recover_hp_rate == 0 and item.recover_hp == 0

        # Set damage to empty string

        self.damage = ""

        # If SP recovery rate / recovery amount are 0, and parameter increase

        # value is ineffective.

        if item.recover_sp_rate == 0 and item.recover_sp == 0 and

           (item.parameter_type == 0 or item.parameter_points == 0)

          # If state is unchanged

          unless @state_changed

            # Set damage to "Miss"

            self.damage = "Miss"

          end

        end

      end

    # If miss occurs

    else

      # Set damage to "Miss"

      self.damage = "Miss"

    end

    # If not in battle

    unless $game_temp.in_battle

      # Set damage to nil

      self.damage = nil

    end

    # End Method

    return effective

  end

  #--------------------------------------------------------------------------

  # * Application of Slip Damage Effects

  #--------------------------------------------------------------------------

  def slip_damage_effect

    # Set damage

    self.damage = self.maxhp / 10

    # Dispersion

    if self.damage.abs > 0

      amp = [self.damage.abs * 15 / 100, 1].max

      self.damage += rand(amp+1) + rand(amp+1) - amp

    end

    # Subtract damage from HP

    self.hp -= self.damage

    # End Method

    return true

  end

  #--------------------------------------------------------------------------

  # * Calculating Element Correction

  #     element_set : element

  #--------------------------------------------------------------------------

  def elements_correct(element_set)

    # If not an element

    if element_set == []

      # Return 100

      return 100

    end

    # Return the weakest object among the elements given

    # * "element_rate" method is defined by Game_Actor and Game_Enemy classes,

    #    which inherit from this class.

    weakest = -100

    for i in element_set

      weakest = [weakest, self.element_rate(i)].max

    end

    return weakest

  end

end

 

 
Uh well obviously it's not my code, but I'm basically editing it to turn up more reasonable results. As you can see I've kind of made a mess of it since I'm just seeing what I can and can't do atm. Rounding is at line 147.
 
Oh, gotcha. In that case, try replacing the script with this:
Code:
class Float

  def round

    return (self+0.5).floor if self > 0.0

    return (self-0.5).ceil  if self < 0.0

    return 0

  end

 

  def round_to(digits = 0)

    mult = 10 ** digits.abs

    if digits >= 0

      return (self * mult).round / mult.to_f

    else

      return (self / mult.to_f).round * mult

    end

  end

end

Now negative values should round big numbers while positive round down decimal places.
 
Hmm, it may be because damage is an integer rather than a float (decimal value). Try adding this under class Float:
Code:
class Integer

  def round_to(digits = 0)

    mult = 10 ** digits.abs

    return (self / mult) * mult

  end

end
(In this case, both positive and negative yield the same results (as there's no decimals in integers).
 
Hooray! thank you everyone for helping me out here!

Just one last thing... For whatever reason, now healing seems to do damage instead of taking away damage. Is that to do with rounding or did I screw something else up?
 
Negative numbers seem to work when I test them, so it might be somewhere in your script. I'm not entirely sure where, though. Did you make sure to set healing power as negative?
 
Nevermind, I think I see the problem. I changed the formulas and it just so happens to include a bit where it's like power * skill power or something, which is a negative times a negative. I've got it all under control(for once).

Thanks again guys! Show's over, move along~
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top