Help - Search - Members - Calendar
Full Version: The Ai is more dumb than a rock.
RPG RPG Revolution Forums > Game Engines > RPG Maker XP Discussion
Vexus
I've been doing some of the monsters with their attacks and I am finding it very annoying that a monster uses a buffing spell 3 times in a row when it will last it 4 turns, same goes for other type of skills/spells.

So I've been trying to find some script that let's me make the enemies ai less dumb with no actual success sadly :/

Anyone know of some script that let's me configure the enemies behaviours into something better?

Thanks.
Jens of Zanicuud
Yes, this is pretty annoying...
I've just a script ready that should fix this.
I employed it in Tryadine Effect too.

Paste it above main, it should work.
If not, just reply here.

CODE
#==============================================================================
# ** Game_Party
#------------------------------------------------------------------------------
#  This class handles the party. It includes information on amount of gold
#  and items. Refer to "$game_party" for the instance of this class.
#==============================================================================

HEAL_RATE = 70.0

class Game_Party
  #--------------------------------------------------------------------------
  # * good_health? (i.e. all party with heal greater than 70%)
  #--------------------------------------------------------------------------
  def good_health?
    flag = true
    for actor in self.actors
      if actor.hp*100/actor.maxhp < HEAL_RATE
        flag = false
      end
    end
    return flag
  end
  #--------------------------------------------------------------------------
  # * weakest actor (i.e. actors with lesser heal)
  #--------------------------------------------------------------------------
  def weakest_actors_roulette
    weakest = [self.actors[0]]
    for actor in self.actors
      if actor.hp*100/actor.maxhp < weakest[0].hp*100/actor.maxhp
        weakest = [actor]
      elsif actor.hp*100/actor.maxhp == weakest[0].hp*100/actor.maxhp
        weakest.push(actor)
      end
    end
    return weakest[rand(weakest.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor with a specified status
  #--------------------------------------------------------------------------
  def rand_act_status(id)
    state = []
    for actor in self.actors
      if actor.state?(id)
        state.push(actor)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor with a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_status_max(skill)
    state = []
    for id in skill.minus_state_set
    for actor in self.actors
      if actor.state?(id)
        state.push(actor)
      end
    end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor without a specified status
  #--------------------------------------------------------------------------
  def rand_act_non_status(id)
    state = []
    for actor in self.actors
      if !actor.state?(id)
        state.push(actor)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor without a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_non_status_max(skill)
    state = []
    for id in skill.plus_state_set
    for actor in self.actors
      if !actor.state?(id)
        state.push(actor)
      end
    end
    end
    return state[rand(state.size)]
  end
  
  end
#==============================================================================
# ** Game_Troop
#==============================================================================

class Game_Troop
  #--------------------------------------------------------------------------
  # * good_health? (i.e. all troop members with heal greater than HEAL_RATE)
  #--------------------------------------------------------------------------
  def good_health?
    flag = true
    for enemy in self.enemies
      if enemy.hp*100/enemy.maxhp < HEAL_RATE
        flag = false
      end
    end
    return flag
  end
  #--------------------------------------------------------------------------
  # * weakest enemy (i.e. enemies with lesser heal)
  #--------------------------------------------------------------------------
  def weakest_enemies_roulette
    weakest = [self.enemies[0]]
    for enemy in self.enemies
      if enemy.hp*100/enemy.maxhp < weakest[0].hp*100/enemy.maxhp
        weakest = [enemy]
      elsif enemy.hp*100/enemy.maxhp == weakest[0].hp*100/enemy.maxhp
        weakest.push(enemy)
      end
    end
    return weakest[rand(weakest.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy with a specified status
  #--------------------------------------------------------------------------
  def rand_act_status(id)
    state = []
    for enemy in self.enemies
      if enemy.state?(id)
        state.push(enemy)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy with a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_status_max(skill)
    state = []
    for id in skill.minus_state_set
    for enemy in self.enemies
      if enemy.state?(id)
        state.push(enemy)
      end
    end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy without a specified status
  #--------------------------------------------------------------------------
  def rand_act_non_status(id)
    state = []
    for enemy in self.enemies
      if !enemy.state?(id)
        state.push(enemy)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy without a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_non_status_max(skill)
    state = []
    for id in skill.plus_state_set
    for enemy in self.enemies
      if !enemy.state?(id)
        state.push(enemy)
      end
    end
    end
    return state[rand(state.size)]
  end
  
  end
#==============================================================================
# ** Game_BattleAction
#==============================================================================
  class Game_BattleAction
  #--------------------------------------------------------------------------
  # — for an enemy?
  #--------------------------------------------------------------------------
  def for_one_enemy?
    #skills and items that have scope 1 or 2 are listed as for_one_enemy?
    return !(for_one_friend? or for_one_friend_hp0?)
  end
  #--------------------------------------------------------------------------
  # — skill
  #--------------------------------------------------------------------------
  def skill
    return $data_skills[self.skill_id]
  end
  #--------------------------------------------------------------------------
  # — random_enemy_target
  #--------------------------------------------------------------------------
  def decide_random_target_for_enemy
    #
    #NEW HP check
    if self.kind == 1 and self.skill.power < 0
      if for_one_friend?
        battler = $game_troop.weakest_enemies_roulette
      end
    end
    
    #NEW state minus check
    if battler == nil
    if self.kind == 1 and self.skill.minus_state_set.size != 0
      #case: for a friend or for an enemy?
      if for_one_friend?
        battler = $game_troop.rand_act_status_max(skill)
      end
      if for_one_enemy?
        battler = $game_party.rand_act_status_max(skill)
      end
     end  
    end
    
    #NEW state plus check
    if battler == nil
    if self.kind == 1 and self.skill.plus_state_set.size != 0
      if for_one_friend?
        battler = $game_troop.rand_act_non_status_max(skill)
      end
      if for_one_enemy?
        battler = $game_party.rand_act_non_status_max(skill)
      end
     end  
    end
    
    if battler == nil
    if for_one_friend_hp0?
      battler = $game_troop.random_target_enemy_hp0
    elsif for_one_friend?
      battler = $game_troop.random_target_enemy
    else
      battler = $game_party.random_target_actor
    end
    end
    if battler != nil
      @target_index = battler.index
    else
      clear
    end
  end
  end

class Game_Enemy
  #--------------------------------------------------------------------------
  # — make_action
  #--------------------------------------------------------------------------
  def make_action
    self.current_action.clear
    # check mobility
    unless self.movable?
      # if unable to move, return
      return
    end
    #set available actions
    available_actions = []
    rating_max = 0
    for action in self.actions
      # actions check
      n = $game_temp.battle_turn
      a = action.condition_turn_a
      b = action.condition_turn_b
      if (b == 0 and n != a) or
         (b > 0 and (n < 1 or n < a or n % b != a % b))
        next
      end
      # HP check
      if [self.hp * 100.0 / self.maxhp,100].min > action.condition_hp
        next
      end
      #SP check
      if self.sp < $data_skills[action.skill_id].sp_cost
        next
      end
      
      #health_check
      test_skill = $data_skills[action.skill_id]
      
      if action.kind == 1 and test_skill.power < 0 and
        (test_skill.scope == 3 or test_skill.scope == 4)
        if $game_troop.good_health?
        next
        end
      end
      
      #status check
      if test_skill.minus_state_set.size != 0 and test_skill.power == 0
        
        if [3,4].include?(test_skill.scope)
        #game troop status check
        flag = false
        for id in test_skill.minus_state_set
          if $game_troop.rand_act_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end
        
      elsif [1,2].include?(test_skill.scope)
        #game party status check
        flag = false
        for id in test_skill.minus_state_set
          if $game_party.rand_act_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end

        end
      end
      
      #plus status check
      if test_skill.plus_state_set.size != 0 and test_skill.power == 0
        if [3,4].include?(test_skill.scope)
        #game troop status check
        flag = false
        for id in test_skill.plus_state_set
          if !$game_troop.rand_act_non_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end
        
      elsif [1,2].include?(test_skill.scope)
        #game party status check
        flag = false
        for id in test_skill.plus_state_set
          if !$game_party.rand_act_non_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end

        end
      end
      
      #check available target
      if !available_target(action)
        next
      end
      # level check
      if $game_party.max_level < action.condition_level
        next
      end
      # check switch conditions
      switch_id = action.condition_switch_id
      if switch_id > 0 and $game_switches[switch_id] == false
        next
      end
      # actions available
      available_actions.push(action)
      if action.rating > rating_max
        rating_max = action.rating
      end
    end
    # ratings calculation
    ratings_total = 0
    for action in available_actions
      if action.rating > rating_max - 3
        ratings_total += action.rating - (rating_max - 3)
      end
    end
    
    if ratings_total > 0
    
      value = rand(ratings_total)
      
      for action in available_actions
        if action.rating > rating_max - 3
          if value < action.rating - (rating_max - 3)
            self.current_action.kind = action.kind
            self.current_action.basic = action.basic
            self.current_action.skill_id = action.skill_id
            self.current_action.decide_random_target_for_enemy
            return
          else
            value -= action.rating - (rating_max - 3)
          end
        end
      end
    end

    end

  #--------------------------------------------------------------------------
  # — available _target
  #--------------------------------------------------------------------------
  def available_target(action)
    act = Game_BattleAction.new
    act.kind = action.kind
    act.basic = action.basic
    act.skill_id = action.skill_id
    act.decide_random_target_for_enemy
    if act.target_index == -1
      return false
    end
      return true
    end
  
  end


Jens
Vexus
I tried the script and for some reason it took way longer for the monster to start using the buff skill (It can only be used when the monster has 60% or less hp) but no it didn't work, the enemy did the buff one after another :/.

Thanks for the help.
Jens of Zanicuud
QUOTE (Vexus @ Feb 26 2012, 11:42 AM) *
I tried the script and for some reason it took way longer for the monster to start using the buff skill (It can only be used when the monster has 60% or less hp) but no it didn't work, the enemy did the buff one after another :/.

Thanks for the help.


Okay, I need an english lesson. What do you intend with "a buff skill"?

First of all, let me explain how that script works:
-If the enemy has health higher than HEAL_RATE percentage, they won't cast healing spells;
-if so, they will cast healing spell on the weakest enemies of the troop;
-to avoid this, give them self cure ability, with "Self" as target (the last one on the scope window, in database).

Maybe this should work better...

Jens
Vexus
By buff I mean a skill that increases say for example defence for a few turns then it wears off.
Jens of Zanicuud
Strange... I would have bet it should work...
I'll test it myself and let you know...

Jens
Jens of Zanicuud
Try this and set your buff skills as "Self" target.

CODE
#==============================================================================
# ** Game_Party
#------------------------------------------------------------------------------
#  This class handles the party. It includes information on amount of gold
#  and items. Refer to "$game_party" for the instance of this class.
#==============================================================================

HEAL_RATE = 70.0

class Game_Party
  #--------------------------------------------------------------------------
  # * good_health? (i.e. all party with heal greater than 70%)
  #--------------------------------------------------------------------------
  def good_health?
    flag = true
    for actor in self.actors
      if actor.hp*100/actor.maxhp < HEAL_RATE
        flag = false
      end
    end
    return flag
  end
  #--------------------------------------------------------------------------
  # * weakest actor (i.e. actors with lesser heal)
  #--------------------------------------------------------------------------
  def weakest_actors_roulette
    weakest = [self.actors[0]]
    for actor in self.actors
      if actor.hp*100/actor.maxhp < weakest[0].hp*100/actor.maxhp
        weakest = [actor]
      elsif actor.hp*100/actor.maxhp == weakest[0].hp*100/actor.maxhp
        weakest.push(actor)
      end
    end
    return weakest[rand(weakest.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor with a specified status
  #--------------------------------------------------------------------------
  def rand_act_status(id)
    state = []
    for actor in self.actors
      if actor.state?(id)
        state.push(actor)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor with a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_status_max(skill)
    state = []
    for id in skill.minus_state_set
    for actor in self.actors
      if actor.state?(id)
        state.push(actor)
      end
    end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor without a specified status
  #--------------------------------------------------------------------------
  def rand_act_non_status(id)
    state = []
    for actor in self.actors
      if !actor.state?(id)
        state.push(actor)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random actor without a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_non_status_max(skill)
    state = []
    for id in skill.plus_state_set
    for actor in self.actors
      if !actor.state?(id)
        state.push(actor)
      end
    end
    end
    return state[rand(state.size)]
  end
  
  end
#==============================================================================
# ** Game_Troop
#==============================================================================

class Game_Troop
  #--------------------------------------------------------------------------
  # * good_health? (i.e. all troop members with heal greater than HEAL_RATE)
  #--------------------------------------------------------------------------
  def good_health?
    flag = true
    for enemy in self.enemies
      if enemy.hp*100/enemy.maxhp < HEAL_RATE
        flag = false
      end
    end
    return flag
  end
  #--------------------------------------------------------------------------
  # * weakest enemy (i.e. enemies with lesser heal)
  #--------------------------------------------------------------------------
  def weakest_enemies_roulette
    weakest = [self.enemies[0]]
    for enemy in self.enemies
      if enemy.hp*100/enemy.maxhp < weakest[0].hp*100/enemy.maxhp
        weakest = [enemy]
      elsif enemy.hp*100/enemy.maxhp == weakest[0].hp*100/enemy.maxhp
        weakest.push(enemy)
      end
    end
    return weakest[rand(weakest.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy with a specified status
  #--------------------------------------------------------------------------
  def rand_act_status(id)
    state = []
    for enemy in self.enemies
      if enemy.state?(id)
        state.push(enemy)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy with a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_status_max(skill)
    state = []
    for id in skill.minus_state_set
    for enemy in self.enemies
      if enemy.state?(id)
        state.push(enemy)
      end
    end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy without a specified status
  #--------------------------------------------------------------------------
  def rand_act_non_status(id)
    state = []
    for enemy in self.enemies
      if !enemy.state?(id)
        state.push(enemy)
      end
    end
    return state[rand(state.size)]
  end
  #--------------------------------------------------------------------------
  # * random enemy without a specified status in a skill selection
  #--------------------------------------------------------------------------
  def rand_act_non_status_max(skill)
    state = []
    for id in skill.plus_state_set
    for enemy in self.enemies
      if !enemy.state?(id)
        state.push(enemy)
      end
    end
    end
    return state[rand(state.size)]
  end
  
  end
#==============================================================================
# ** Game_BattleAction
#==============================================================================
  class Game_BattleAction
  #--------------------------------------------------------------------------
  # — for an enemy?
  #--------------------------------------------------------------------------
  def for_one_enemy?
    #skills and items that have scope 1 or 2 are listed as for_one_enemy?
    return !(for_one_friend? or for_one_friend_hp0?)
  end
  #--------------------------------------------------------------------------
  # — skill
  #--------------------------------------------------------------------------
  def skill
    return $data_skills[self.skill_id]
  end
  #--------------------------------------------------------------------------
  # — random_enemy_target
  #--------------------------------------------------------------------------
  def decide_random_target_for_enemy
    #
    #NEW HP check
    if self.kind == 1 and self.skill.power < 0
      if for_one_friend?
        battler = $game_troop.weakest_enemies_roulette
      end
    end
    
    #NEW state minus check
    if battler == nil
    if self.kind == 1 and self.skill.minus_state_set.size != 0
      #case: for a friend or for an enemy?
      if for_one_friend?
        battler = $game_troop.rand_act_status_max(skill)
      end
      if for_one_enemy?
        battler = $game_party.rand_act_status_max(skill)
      end
     end  
    end
    
    #NEW state plus check
    if battler == nil
    if self.kind == 1 and self.skill.plus_state_set.size != 0
      if for_one_friend?
        battler = $game_troop.rand_act_non_status_max(skill)
      end
      if for_one_enemy?
        battler = $game_party.rand_act_non_status_max(skill)
      end
     end  
    end
    
    if battler == nil
    if for_one_friend_hp0?
      battler = $game_troop.random_target_enemy_hp0
    elsif for_one_friend?
      battler = $game_troop.random_target_enemy
    else
      battler = $game_party.random_target_actor
    end
    end
    # 対象Œ˜œ™‚‹な‚‰‚ƒƒ‡ƒƒ‚‚‚’–——€
    # 対象Œ˜œ—な„場ˆは‚‚‚ƒƒ‚’‚ƒ‚
    if battler != nil
      @target_index = battler.index
    else
      clear
    end
  end
  end

class Game_Enemy
  #--------------------------------------------------------------------------
  # — make_action
  #--------------------------------------------------------------------------
  def make_action
    # ‚ƒƒƒˆ‚‚‚ƒƒ‚’‚ƒ‚
    
    #self.guard_remove
    
    self.current_action.clear
    # ‹•‘な„場ˆ
    unless self.movable?
      # ƒ‚ƒƒƒ‰‚†
      return
    end
    # 現œœ‰Šな‚‚‚ƒƒ‚’Ї
    available_actions = []
    rating_max = 0
    for action in self.actions
      # ‚ƒƒ 条件確認
      n = $game_temp.battle_turn
      a = action.condition_turn_a
      b = action.condition_turn_b
      if (b == 0 and n != a) or
         (b > 0 and (n < 1 or n < a or n % b != a % b))
        next
      end
      # HP 条件確認
      if [self.hp * 100.0 / self.maxhp,100].min > action.condition_hp
        next
      end
      #SP check
      if self.sp < $data_skills[action.skill_id].sp_cost
        next
      end
      
      #health_check
      test_skill = $data_skills[action.skill_id]
      
      if action.kind == 1 and test_skill.power < 0 and
        (test_skill.scope == 3 or test_skill.scope == 4)
        if $game_troop.good_health?
        next
        end
      end
      
      #status check
      if test_skill.minus_state_set.size != 0 and test_skill.power == 0
        
      if [3,4].include?(test_skill.scope)
        #game troop status check
        flag = false
        for id in test_skill.minus_state_set
          if $game_troop.rand_act_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end
        
      elsif [1,2].include?(test_skill.scope)
        #game party status check
        flag = false
        for id in test_skill.minus_state_set
          if $game_party.rand_act_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end

        end
      end
      
      #plus status check
      if test_skill.plus_state_set.size != 0 and test_skill.power == 0
        if test_skill.scope == 7
          flag = true
        for id in test_skill.plus_state_set
            if self.state?(id)
              flag = false
            else
              flag = true
            end
          end
           if !flag
             next
           end
        end
        if [3,4].include?(test_skill.scope)
        #game troop status check
        flag = false
        for id in test_skill.plus_state_set
          if $game_troop.rand_act_non_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end
        
      elsif [1,2].include?(test_skill.scope)
        #game party status check
        flag = false
        for id in test_skill.plus_state_set
          if $game_party.rand_act_non_status(id) != nil
            flag = true
          end
        end
        #if flag, then next
        if !flag
          next
        end

        end
      end
      
      #check available target
      if !available_target(action)
        next
      end
      # ƒƒ™ƒ 条件確認
      if $game_party.max_level < action.condition_level
        next
      end
      # ‚‚ƒƒƒ 条件確認
      switch_id = action.condition_switch_id
      if switch_id > 0 and $game_switches[switch_id] == false
        next
      end
      # 条件に該“ : “の‚‚‚ƒƒ‚’追Š
      available_actions.push(action)
      if action.rating > rating_max
        rating_max = action.rating
      end
    end
    # œ€大のƒƒƒ†‚ƒ‚€‚’ 3 と—てˆˆ‚’ˆ— (0 以‹は™–)
    ratings_total = 0
    for action in available_actions
      if action.rating > rating_max - 3
        ratings_total += action.rating - (rating_max - 3)
      end
    end
    # ƒƒƒ†‚ƒ‚のˆˆŒ 0 ではな„場ˆ
    if ratings_total > 0
      # 乱•‚’œˆ
      value = rand(ratings_total)
      # œˆ—Ÿ乱•に対œ™‚‹‚‚の‚’‚ƒƒƒˆ‚‚‚ƒƒに設š
      for action in available_actions
        if action.rating > rating_max - 3
          if value < action.rating - (rating_max - 3)
            self.current_action.kind = action.kind
            self.current_action.basic = action.basic
            self.current_action.skill_id = action.skill_id
            self.current_action.decide_random_target_for_enemy
            return
          else
            value -= action.rating - (rating_max - 3)
          end
        end
      end
    end

    end

  #--------------------------------------------------------------------------
  # — available _target
  #--------------------------------------------------------------------------
  def available_target(action)
    act = Game_BattleAction.new
    act.kind = action.kind
    act.basic = action.basic
    act.skill_id = action.skill_id
    act.decide_random_target_for_enemy
    if act.target_index == -1
      return false
    end
      return true
    end
  
  end


Jens
Vexus
Nomethod error on line 256:

undefined method 'Guard_remove' for #<Game_Enemy:0x91d5550>

When I test played.
Jens of Zanicuud
Remove that line. I just forgot to do it myself.

Jens
Vexus
Thanks it works smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.