A bug in Cozziekun's Earthbound-Ish Odometer Roll

This is a bug I noticed recently in Cozziekun's Earthbound-Ish Odometer Roll, I have been using it for a my project but it is until now I notice this bug.

So, if the battle is over or you go to another battle, the odometer will still roll down even after the battle is over or you start a new one, making an inevitable Game Over unless you heal. If there's somebody that can fix this bug, I will be very grateful and even put you in the game credits 😀.

Spoiler: Spoiler:
Code:
#==============================================================================
# ** Earthbound-Ish Odometer Roll
#------------------------------------------------------------------------------
# Version: 1.0
# Author: cozziekuns
# Date: February 17, 2013
#==============================================================================
# Description:
#------------------------------------------------------------------------------
# This script attempts to emulate the battle system of the Earthbound/Mother
# series; most notably Earthbound and Earthbound 2 (Mother 2 and 3). This
# certain addon addresses the infamous HP/MP scrolling system that made battles
# that much more intense.
#==============================================================================
# Instructions:
#------------------------------------------------------------------------------
# Paste this script into its own slot in the Script Editor, above Main but
# below Materials. Edit the modules to your liking.
#==============================================================================
# Graphics:
#------------------------------------------------------------------------------
# You must have two Odometer pictures in your Graphics/System folder. One of
# them must be named "Odometer_HP", and the other one must be named
# "Odometer_MP". Obviously, they represent the odometer for HP and MP values,
# respectively
#==============================================================================

#==============================================================================
# ** Cozziekuns
#==============================================================================

module Cozziekuns
 
  module Earthboundish
   
    Odometer_Roll_Speed = 3 # Smaller speeds are faster than larger speeds.
   
  end
 
end

include Cozziekuns

#==============================================================================
# ** Game_Actor
#==============================================================================

class Game_Actor
 
  attr_accessor :odometer_hp
  attr_accessor :odometer_mp
 
  alias coz_ebisohd_gmactr_setup setup
  def setup(actor_id, *args)
    coz_ebisohd_gmactr_setup(actor_id, *args)
    @odometer_hp = 0
    @odometer_mp = 0
  end
 
  alias coz_ebishod_gmactr_execute_damage execute_damage
  def execute_damage(user, *args)
    if $game_party.in_battle
      on_damage(@result.hp_damage) if @result.hp_damage > 0
      @odometer_hp += @result.hp_damage
      @odometer_mp += @result.mp_damage
      user.hp += @result.hp_drain
      user.mp += @result.mp_drain
    else
      coz_ebishod_gmactr_execute_damage(user, *args)
    end
  end
 
  [:hp, :mp].each { |stat|
    alias_method("coz_ebishod_gmactr_item_effect_recover_#{stat}".to_sym, "item_effect_recover_#{stat}".to_sym)
    define_method("item_effect_recover_#{stat}".to_sym) { |user, item, effect|
      if $game_party.in_battle
        value = (send("m#{stat}".to_sym) * effect.value1 + effect.value2) * rec
        value *= user.pha if item.is_a?(RPG::Item)
        value = value.to_i
        @result.send("#{stat}_damage=".to_sym, @result.send("#{stat}_damage".to_sym) - value)
        @result.success = true
        send("odometer_#{stat}=".to_sym, send("odometer_#{stat}".to_sym) - value)
      else
        send("coz_ebishod_gmactr_item_effect_recover_#{stat}".to_sym, user, item, effect)      
      end
    }
  }
 
end

#==============================================================================
# ** Game_Enemy
#==============================================================================

class Game_Enemy
 
  def execute_damage(user)
    on_damage(@result.hp_damage) if @result.hp_damage > 0
    self.hp -= @result.hp_damage
    self.mp -= @result.mp_damage
    user.odometer_hp -= @result.hp_drain
    user.odometer_mp -= @result.mp_drain
  end
 
end

#==============================================================================
# ** Window_BattleStatus
#==============================================================================

class Window_BattleStatus
 
  def refresh_hpmp(actor, index)
    rect = item_rect(index)
    if gauge_area_rect(index) == item_rect(index)
      rect.y += line_height * 2
      rect.height -= line_height * 2
      contents.clear_rect(rect)
    else
      contents.clear_rect(gauge_area_rect(index))
    end
    draw_gauge_area(gauge_area_rect(index), actor)
  end
 
  [:hp, :mp].each { |stat|
    define_method("draw_actor_#{stat}".to_sym) { |actor, x, y, width|
      change_color(system_color)
      draw_text(x, y, 30, line_height, Vocab.send("#{stat}_a"))
      od_x = x + contents.text_size(Vocab.send("#{stat}_a")).width + 4
      actor_hp = actor.send("#{stat}".to_sym)
      actor_od_hp = actor.send("odometer_#{stat}".to_sym)
      draw_odometer(od_x, y, stat, actor_hp, actor_od_hp)
    }
  }
 
  def draw_odometer(x, y, type, value, od_value)
    bitmap = Cache.system("Odometer_#{type.upcase}")
    places = [1000,  100, 10, 1]
    od_ary = value.to_s.split("").collect { |str| str.to_i }
    (3 - od_ary.size).times { od_ary.unshift(0) }
    od_ary.each_index { |i|
      src_y = (9 - od_ary[i]) * 20
      if (od_ary.join.to_i) % places[i] == 0 and od_value != 0
        src_y += 20 / Earthboundish::Odometer_Roll_Speed * (Graphics.frame_count % Earthboundish::Odometer_Roll_Speed)
      end
      contents.blt(x + i * 24, y + 2, bitmap, Rect.new(0, src_y, 24, 20))
    }
  end
 
end

#==============================================================================
# ** Scene_Battle
#==============================================================================

class Scene_Battle
 
  alias coz_ebishod_scbtl_update_basic update_basic
  def update_basic(*args)
    coz_ebishod_scbtl_update_basic(*args)
    update_odometer
  end
 
  def update_odometer
    $game_party.members.each { |actor|
      if actor.odometer_hp != 0 or actor.odometer_mp != 0
        if actor.odometer_hp != 0 and Graphics.frame_count % Earthboundish::Odometer_Roll_Speed == 0
          damage = actor.odometer_hp > 0 ? 1 : - 1
          actor.hp -= damage
          actor.odometer_hp -= damage
        end
        if actor.odometer_mp != 0 and Graphics.frame_count % Earthboundish::Odometer_Roll_Speed == 0
          damage = actor.odometer_mp > 0 ? 1 : - 1
          actor.mp -= damage
          actor.odometer_mp -= damage
        end
        @status_window.refresh_hpmp(actor, actor.index)
      end
    }
  end
 
end

Thanks to Roninator2 for solving this! I hope this works for others in case they encounter the same bug!

Spoiler: Code by Roninator2
Ruby:
module BattleManager
  def self.in_input?
    @phase == :input
  end
  def self.in_init?
    @phase == :init
  end
end

class Scene_Battle
  alias r2_stop_odometer_roll_on_battle_end_update    update
  def update
    r2_stop_odometer_roll_on_battle_end_update
    if !BattleManager.in_turn? && !BattleManager.in_input? &&
        !BattleManager.turn_end? && !BattleManager.in_init?
      stop_odometer
    end
  end
 
  def stop_odometer
    $game_party.members.each { |actor|
      if actor.odometer_hp != 0 or actor.odometer_mp != 0
        actor.odometer_hp = 0
        actor.odometer_mp = 0
      end
    }
  end
end
I currently use that script on my project, it did solve that problem but not this one, but thank you very much.
So, even without this script, the bug is still in there :/.
try this
Ruby:
module BattleManager
  def self.in_input?
    @phase == :input
  end
  def self.in_init?
    @phase == :init
  end
end

class Scene_Battle
  alias r2_stop_odometer_roll_on_battle_end_update    update
  def update
    r2_stop_odometer_roll_on_battle_end_update
    if !BattleManager.in_turn? && !BattleManager.in_input? &&
        !BattleManager.turn_end? && !BattleManager.in_init?
      stop_odometer
    end
  end
 
  def stop_odometer
    $game_party.members.each { |actor|
      if actor.odometer_hp != 0 or actor.odometer_mp != 0
        hpdam = actor.odometer_hp
        mpdam = actor.odometer_mp
        actor.hp -= hpdam
        actor.mp -= mpdam
        actor.odometer_hp = 0
        actor.odometer_mp = 0
      end
    }
  end
end
try this
Ruby:
class Scene_Battle
  alias r2_stop_odometer_roll_on_battle_end_update    update
  def update
    r2_stop_odometer_roll_on_battle_end_update
    if !BattleManager.in_turn?
      stop_odometer
    end
  end
 
  def stop_odometer
    $game_party.members.each { |actor|
      if actor.odometer_hp != 0 or actor.odometer_mp != 0
        hpdam = actor.odometer_hp
        mpdam = actor.odometer_mp
        actor.hp -= hpdam
        actor.mp -= mpdam
        actor.odometer_hp = 0
        actor.odometer_mp = 0
      end
    }
  end
end
Works perfectly! The thing is that after the turn the odometer goes directly to the result instead of scrolling all the way to the result, so after the turn does the substraction directly instead of scrolling.
Right, I had a thought that this would only apply at battle end, but it would not.
Let me work a bit more on that.

Edited. Code fixed
Right, I had a thought that this would only apply at battle end, but it would not.
Let me work a bit more on that.

Edited. Code fixed
Works very well, but the odometer refreshes after the turn, so if a battler does an excessive amount of damage, it results in an instant death. I'm not very good at coding but maybe it shouldn't check if the player is in turn to stop it? I appreaciate your patience 🙂.
Works very well, but the odometer refreshes after the turn, so if a battler does an excessive amount of damage, it results in an instant death. I'm not very good at coding but maybe it shouldn't check if the player is in turn to stop it? I appreaciate your patience 🙂.
It's made so that it stops it it the player is not inputting commands or in turn. Which should be when aborting or defeat or victory conditions.
Oh but you're right there is another. turn_end. I'll add that.
Oh and init.

Those are added now.
It's made so that it stops it it the player is not inputting commands or in turn. Which should be when aborting or defeat or victory conditions.
Oh but you're right there is another. turn_end. I'll add that.
Oh and init.

Those are added now.
It works perfectly! Thank you very much and I hope this works well for others too 😀!

Original thread: https://forums.rpgmakerweb.com/threads/a-bug-in-cozziekuns-earthbound-ish-odometer-roll.170821/