Enemy Action Selection Fix
by RPG Advocate
saved from Phylomortis.Com
Introduction
This script fixes two annoyances with the way the standard enemy action selection method determines which action an enemy will take. First, this script makes it so that actions with any rating will be chosen (action's rating / total rating of all actions x 100)% of the time, rather than completely excluding actions more than three rating units below the highest-rated action. Second, this script adds a method that excludes pointless actions, preventing monsters from doing nothing because they have, for instance, selected a skill they don't have enough SP to use, or have selected a skill that targets dead characters, but no monsters in the group are dead.
#==============================================================================
# ** Game_Enemy
#------------------------------------------------------------------------------
# This class handles enemies. It's used within the Game_Troop class
# ($game_troop).
#==============================================================================
class Game_Enemy < Game_Battler
#--------------------------------------------------------------------------
# * Make Action
#--------------------------------------------------------------------------
def make_action
# Clear current action
self.current_action.clear
# If unable to move
unless self.movable?
# End Method
return
end
# Extract current effective actions
available_actions = []
rating_max = 0
for action in self.actions
# Confirm turn conditions
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
# Confirm HP conditions
if self.hp * 100.0 / self.maxhp > action.condition_hp
next
end
# Confirm level conditions
if $game_party.max_level < action.condition_level
next
end
# Confirm switch conditions
switch_id = action.condition_switch_id
if switch_id > 0 and $game_switches[switch_id] == false
next
end
# Confirm useful condition
if exclude_pointless_actions(action)
next
end
# Add this action to applicable conditions
available_actions.push(action)
if action.rating > rating_max
rating_max = action.rating
end
end
# Calculate total
ratings_total = 0
for action in available_actions
ratings_total += action.rating
end
if ratings_total > 0
choose_action = []
# Set things that correspond to created random numbers as current action
for action in available_actions
for j in 1..action.rating
choose_action.push(action)
end
end
enemy_action = choose_action[rand(choose_action.size - 1)]
self.current_action.kind = enemy_action.kind
self.current_action.basic = enemy_action.basic
self.current_action.skill_id = enemy_action.skill_id
self.current_action.decide_random_target_for_enemy
end
end
#--------------------------------------------------------------------------
# * Exclude pointless Actions
# action : enemy action
#--------------------------------------------------------------------------
def exclude_pointless_actions(action)
if action.kind == 0
return false
end
if action.kind == 1
s = action.skill_id
if self.sp < $data_skills[s].sp_cost
return true
end
if $data_skills[s].common_event_id > 0
return false
end
if $data_skills[s].scope == 0 || $data_skills[s].scope == 1 ||
$data_skills[s].scope == 2
return false
end
if $data_skills[s].scope == 3 || $data_skills[s].scope == 4
if $game_troop != nil
fullhpflag = true
statusflag = true
for i in $game_troop.enemies
if i.hp < i.maxhp && i.exist?
fullhpflag = false
end
for j in $data_skills[s].plus_state_set
if not i.state?(j) && i.exist?
statusflag = false
end
end
for j in $data_skills[s].minus_state_set
if i.state?(j) && i.exist?
statusflag = false
end
end
end
end
if $data_skills[s].power == 0 && statusflag
return true
end
if $data_skills[s].power < 0 && statusflag && fullhpflag
return true
end
end
if $data_skills[s].scope == 5 || $data_skills[s].scope == 6
nonedeadflag = true
if $game_troop != nil
for i in $game_troop.enemies
if i.dead? and not i.hidden
nonedeadflag = false
end
end
end
if nonedeadflag
return true
end
end
if $data_skills[s].scope == 7
if $game_troop != nil
fullhpflag = true
statusflag = true
if self.hp < self.maxhp
fullhpflag = false
end
for j in $data_skills[s].plus_state_set
if not self.state?(j)
statusflag = false
end
end
for j in $data_skills[s].minus_state_set
if self.state?(j)
statusflag = false
end
end
end
if $data_skills[s].power == 0 && statusflag
return true
end
if $data_skills[s].power < 0 && statusflag && fullhpflag
return true
end
end
end
return false
end
end