[XP] Help with a small piece of code

Started by Sase, March 18, 2010, 08:02:03 am

Previous topic - Next topic

Sase

March 18, 2010, 08:02:03 am Last Edit: March 18, 2010, 08:08:30 am by Sase
In RTAB with animated battlers you can have the players to not move when attacking with certain weapons.

At early (edit part) of the script is this:
@stationary_weapons = [89]


And it is used in:
  def setmove(destination_x, destination_y)
   unless (@battler.is_a?(Game_Enemy) and @stationary_enemies) or
          (@battler.is_a?(Game_Actor) and @stationary_actors)
     unless @stationary_weapons.include?(@battler.weapon_id)
       @original_x = @display_x
       @original_y = @display_y
       @destination_x = destination_x
       @destination_y = destination_y
     end
   end
 end


If I want to put certain enemies not moving, how should I add it into that part? Since the stationary_weapons isn't used anywhere but that script piece I wrote above, I'm guessing it's not complicated to make a new one (@stationary_enemyid).

  def setmove(destination_x, destination_y)
   unless (@battler.is_a?(Game_Enemy) and @stationary_enemies) or
          (@battler.is_a?(Game_Actor) and @stationary_actors)
     unless (@stationary_weapons.include?(@battler.weapon_id)) or
            (@stationary_enemyid.include?(@enemy.id))
              @original_x = @display_x
              @original_y = @display_y
              @destination_x = destination_x
              @destination_y = destination_y
     end
   end
 end


My guess would be this but maybe someone can confirm/help with this, since I have no idea how it works.

If you are interested the whole script is here:
Spoiler: ShowHide

#==============================================================================
# ** Sprite_Battler
#------------------------------------------------------------------------------
#  Animated Battlers by Minkoff
#  Enhancement v 1.2 by DerVVulfman                                (07-18-2006)
#==============================================================================
#
#   Now, an additional call can be made before combat begins:
#       Script:  $game_system.sideview_mirror = 1
#   Reverses the position and direction of the battlers on-screen.
#
#       Script:  $game_system.sideview_mirror = 0
#   Returns it to the normal style (enemies on left / heroes on right).


class Sprite_Battler < RPG::Sprite
 #--------------------------------------------------------------------------
 # * Initialize
 #--------------------------------------------------------------------------
 alias cbs_initialize initialize
 def initialize(viewport, battler = nil)

   # * Configuration System *
   
   # Animation Frames and Animation Speed
   @speed              = 6      # Framerate speed of the battlers
   @frames             = 4      # Number of frames in each pose
   @poses              = 11     # Number of poses (stances) in the template

   # Poses Control (Some people wanted to change their template design)
   $p1   =   0   # Sets the 'Ready Pose'  ($p1) to be pose #1 in your template
   $p2   =   1   # Sets the 'Damage Pose' ($p2) to be pose #2 in your template
   $p3   =   2   # Sets the 'Crit-HP Pose'  ($p3) to be pose #3 in your template
   $p4   =   3   # Sets the 'Defend-Pose'  ($p4) to be pose #4 in your template
   $p5   =   4   # Sets the 'Move left Pose' ($p5) to be pose #5 in your template
   $p6   =   5   # Sets the 'Move right Pose'($p6) to be pose #6 in your template
   $p7   =   6   # Sets the 'Attack Pose' ($p7) to be pose #7 in your template
   $p8   =   7   # Sets the 'Item Pose'   ($p8) to be pose #8 in your template
   $p9   =   8   # Sets the 'Skill Pose'  ($p9) to be pose #9 in your template
   $p10  =   9   # Sets the 'Victory Pose'($p10)to be pose #10 in your template
   $p11  =  10   # Sets the 'Dead Pose' ($p11)to be pose #11 in your template
   
   # Individual Battler Settings
   @mirror_enemies     = true   # Enemy battlers use reversed image
   @stationary_enemies = false  # If the enemies don't move while attacking
   @stationary_actors  = false  # If the actors don't move while attacking
   @calculate_speed    = false  # System calculates a mean/average speed
   @phasing            = true   # Characters fade in/out while attacking
   @default_collapse   = false   # If true, restores the old 'red fade' effect.
   
   # Action Settings
   $rush_offset        = 40     # How much overlap between attackers
   $rush_skill         = true   # If true, battler steps forward to use skill
   $rush_item          = true   # If true, battler steps forward to use item
   
   # Array that holds the id # of skills, items or weapons that affect movement
   @stationary_weapons = [89] # (examples are bows & guns)
   @stationary_enemies = [1]
   $moving_item_atk    = [1] # Examples poisoned... um... dunno... (didn't add)
   $moving_skill_atk   = [59,63,60,64] # Examples are martial-art attacks & sneak attack
   
   # DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING
   @frame, @pose = 0, 0
   @last_time = 0
   @last_move_time = 0
   cbs_initialize(viewport, battler)
   viewport.z = 99
 end
 #--------------------------------------------------------------------------
 # * Update
 #--------------------------------------------------------------------------
 alias cbs_update update
 def update
   return unless @battler

   # Regular Update
   cbs_update

   # Start Routine
   unless @started
     @pose = state
     @width = @width / @frames
     @height = @height / @poses
     @display_x = @battler.screen_x
     @display_y = @battler.screen_y
     @destination_x = @display_x
     @destination_y = @display_y
     self.opacity = 0
     @started = true
   end
   
   # Cut Out Frame
   self.src_rect.set(@width * @frame, @height * @pose, @width, @height)

   # Position Sprite
   self.x = @display_x
   self.y = @display_y
   self.z = @display_y
   self.ox = @width / 2
   self.oy = @height
   
   # Adjust sprite direction if facing the other way...
   if $game_system.sideview_mirror == 1
     if @battler.is_a?(Game_Actor)
       self.mirror = !!battler
     else
       if not @mirror_enemies
         self.mirror = !!battler
       end
     end
   else
     if @battler.is_a?(Game_Enemy)
       if @mirror_enemies
         self.mirror = !!battler
       end
     end
   end
   
   # Setup Animation
   time = Graphics.frame_count / (Graphics.frame_rate / @speed)
   if @last_time < time
     @frame = (@frame + 1) % @frames
     if @frame == 0
       if @freeze
         @frame = @frames - 1
         return
       end
       @pose = state
     end
   end
   @last_time = time

   # Move It
   move if moving
 end
 #--------------------------------------------------------------------------
 # * Current State
 #--------------------------------------------------------------------------
 def state
   # Damage State
   if [nil,{}].include?(@battler.damage)
     # Battler Fine
     @state = $p1
     # Battler Wounded
     @state = $p3 if @battler.hp < @battler.maxhp / 4
     # Battler Dead
     if @default_collapse
       # (Red-Out Collapse)
       if @battler.dead? and @battler.is_a?(Game_Actor)
         #@state = $p3
         @state = $p11
         # Fix Opacity
         self.opacity = 50
       end
     else
       # (Pose-Type Collapse)
       if @battler.dead?
         #@state = $p3
         @state = $p11
         # Fix Opacity
         self.opacity = 255#50
       end
     end
   end
   # Guarding State
   @state = $p4 if @battler.guarding?
   # Moving State
   if moving
   # Adjust sprite direction if facing the other way...
     if $game_system.sideview_mirror == 1
       # If enemy battler moving
       if @battler.is_a?(Game_Enemy)
         # Battler Moving Left
         @state = $p5 if moving.eql?(0)
         # Battler Moving Right
         @state = $p6 if moving.eql?(1)
       # Else actor battler moving
       else
         # Battler Moving Left
         @state = $p6 if moving.eql?(0)
         # Battler Moving Right
         @state = $p5 if moving.eql?(1)
       end
             
     else      
       # If enemy battler moving
       if @battler.is_a?(Game_Enemy)
         # Battler Moving Left
         @state = $p6 if moving.eql?(0)
         # Battler Moving Right
         @state = $p5 if moving.eql?(1)
       # Else actor battler moving
       else
         # Battler Moving Left
         @state = $p5 if moving.eql?(0)
         # Battler Moving Right
         @state = $p6 if moving.eql?(1)
       end
     end
     
   end
   # Return State
   return @state
 end
 #--------------------------------------------------------------------------
 # * Move
 #--------------------------------------------------------------------------
 def move
   time = Graphics.frame_count / (Graphics.frame_rate.to_f / (@speed * 5))
   if @last_move_time < time

     # Pause for Animation
     return if @pose != state

     # Phasing
     if @phasing
       d1 = (@display_x - @original_x).abs
       d2 = (@display_y - @original_y).abs
       d3 = (@display_x - @destination_x).abs
       d4 = (@display_y - @destination_y).abs
       self.opacity = [255 - ([d1 + d2, d3 + d4].min * 1.75).to_i, 0].max
     end

     # Calculate Difference
     difference_x = (@display_x - @destination_x).abs
     difference_y = (@display_y - @destination_y).abs

     # Done? Reset, Stop
     if [difference_x, difference_y].max.between?(0, 8)
       @display_x = @destination_x
       @display_y = @destination_y
       @pose = state
       return
     end

     # Calculate Movement Increments
     increment_x = increment_y = 1
     if difference_x < difference_y
       increment_x = 1.0 / (difference_y.to_f / difference_x)
     elsif difference_y < difference_x
       increment_y = 1.0 / (difference_x.to_f / difference_y)
     end
     
     # Calculate Movement Speed
     if @calculate_speed
       total = 0; $game_party.actors.each{ |actor| total += actor.agi }
       speed = @battler.agi.to_f / (total / $game_party.actors.size)
       increment_x *= speed
       increment_y *= speed
     end
     
     # Multiply and Move
     multiplier_x = (@destination_x - @display_x > 0 ? 8 : -8)
     multiplier_y = (@destination_y - @display_y > 0 ? 8 : -8)
     @display_x += (increment_x * multiplier_x).to_i
     @display_y += (increment_y * multiplier_y).to_i
   end
   @last_move_time = time
   
 
 end
 #--------------------------------------------------------------------------
 # * Set Movement
 #--------------------------------------------------------------------------
 def setmove(destination_x, destination_y)
   unless (@battler.is_a?(Game_Enemy) and @stationary_enemies) or
          (@battler.is_a?(Game_Actor) and @stationary_actors)
     unless @stationary_weapons.include?(@battler.weapon_id)
       @original_x = @display_x
       @original_y = @display_y
       @destination_x = destination_x
       @destination_y = destination_y
     end
   end
 end
 #--------------------------------------------------------------------------
 # * Movement Check
 #--------------------------------------------------------------------------
 def moving
   if (@display_x != @destination_x and @display_y != @destination_y and !@battler.dead?)
     return (@display_x > @destination_x ? 0 : 1)
   end
 end
 #--------------------------------------------------------------------------
 # * Set Pose
 #--------------------------------------------------------------------------
 def pose=(pose)
   @pose = pose
   @frame = 0
 end
 #--------------------------------------------------------------------------
 # * Freeze
 #--------------------------------------------------------------------------
 def freeze
   @freeze = true
 end
 #--------------------------------------------------------------------------
 # * Fallen Pose
 #--------------------------------------------------------------------------
 alias cbs_collapse collapse
 def collapse
   if @default_collapse
     cbs_collapse if @battler.is_a?(Game_Enemy)
     end
   end
 end

#==============================================================================
# ** Game_System
#==============================================================================

class Game_System
 #--------------------------------------------------------------------------
 # * Public Instance Variables
 #--------------------------------------------------------------------------
 attr_accessor   :sideview_mirror
 #--------------------------------------------------------------------------
 # * Object Initialization
 #--------------------------------------------------------------------------
 alias initialize_cbs_customize initialize
 def initialize
   # Call original initialization process
   initialize_cbs_customize
   @sideview_mirror = 0
 end
end  
 

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

class Game_Actor
 #--------------------------------------------------------------------------
 # * Actor X Coordinate
 #--------------------------------------------------------------------------
 def screen_x
   if self.index != nil
     if $game_system.sideview_mirror == 1
       return self.index * -45 + 202        
     else
       return self.index * 45 + 450
     end
   else
     return 0
   end
 end
 #--------------------------------------------------------------------------
 # * Actor Y Coordinate
 #--------------------------------------------------------------------------
 def screen_y
   return self.index * 35 + 200
 end
 #--------------------------------------------------------------------------
 # * Actor Z Coordinate
 #--------------------------------------------------------------------------
 def screen_z
   return screen_y
 end
end


#==============================================================================
# ** Game_Enemy
#------------------------------------------------------------------------------
#  This class handles enemies. It's used within the Game_Troop class
#  ($game_troop).
#==============================================================================

class Game_Enemy < Game_Battler
 def screen_x
   if self.index != nil
     if $game_system.sideview_mirror == 1
       return 640 - $data_troops[@troop_id].members[@member_index].x
     else
       return $data_troops[@troop_id].members[@member_index].x
     end
   end
 end
end  
 

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

class Scene_Battle
 
 #--------------------------------------------------------------------------
 # * Make Skill Action Results (alias used to determine skill used)
 #--------------------------------------------------------------------------
 alias make_skill_action_result_anim make_skill_action_result
 def make_skill_action_result(battler = @active_battler)
   @rtab = !@target_battlers
   @rtab ? make_skill_action_result_anim(battler) : make_skill_action_result_anim
   @skill_used = @skill.id
 end
 
 #--------------------------------------------------------------------------
 # * Make Item Action Results (alias used to determine item used)
 #--------------------------------------------------------------------------
 alias make_item_action_result_anim make_item_action_result
 def make_item_action_result(battler = @active_battler)
   @rtab = !@target_battlers
   @rtab ? make_item_action_result_anim(battler) : make_item_action_result_anim
   @item_used = @item.id
   @item_usage = @item.scope
 end

 #--------------------------------------------------------------------------
 # * Action Animation, Movement
 #--------------------------------------------------------------------------
 alias cbs_update_phase4_step3 update_phase4_step3
 def update_phase4_step3(battler = @active_battler)
   @rtab = !@target_battlers
   target = (@rtab ? battler.target : @target_battlers)[0]
   @moved = {} unless @moved
   return if @spriteset.battler(battler).moving
   case battler.current_action.kind
   
   when 0 # Attack
     
     # Get 1 battle points for using an attack
     $game_variables[1] += 1      
     if not (@moved[battler] or battler.guarding?)
       if $game_system.sideview_mirror == 1
         offset = (battler.is_a?(Game_Actor) ? -($rush_offset) : $rush_offset)
       else
         offset = (battler.is_a?(Game_Actor) ? $rush_offset : -($rush_offset))
       end
       @spriteset.battler(battler).setmove(target.screen_x + offset, target.screen_y)
       @moved[battler] = true
       return
     elsif not battler.guarding?
       @spriteset.battler(battler).pose = $p7
       @spriteset.battler(battler).setmove(battler.screen_x, battler.screen_y)
     end
     
   when 1 # Skill
     
     # Get 2 battle points for using a skill
     $game_variables[1] += 1
     unless $moving_skill_atk.include?(@skill_used)
       if $rush_skill
         if not (@moved[battler] or battler.guarding?)
           if $game_system.sideview_mirror == 1
             offset = (battler.is_a?(Game_Actor) ? -($rush_offset) : $rush_offset)
           else
             offset = (battler.is_a?(Game_Actor) ? $rush_offset : -($rush_offset))
           end
           @spriteset.battler(battler).setmove(battler.screen_x - offset, battler.screen_y + 1)
           @moved[battler] = true
           return
         elsif not battler.guarding?
           @spriteset.battler(battler).pose = $p9
           @spriteset.battler(battler).setmove(battler.screen_x, battler.screen_y)
         end
       else
         @spriteset.battler(battler).pose = $p9
       end  
     else
       if not (@moved[battler] or battler.guarding?)
         if $game_system.sideview_mirror == 1
           offset = (battler.is_a?(Game_Actor) ? -($rush_offset) : $rush_offset)
         else
           offset = (battler.is_a?(Game_Actor) ? $rush_offset : -($rush_offset))
         end
         @spriteset.battler(battler).setmove(target.screen_x + offset, target.screen_y)
         @moved[battler] = true
         return
       elsif not battler.guarding?
         @spriteset.battler(battler).pose = $p9
         @spriteset.battler(battler).setmove(battler.screen_x, battler.screen_y)
       end
     end
     
   when 2 # Item
     
     unless $moving_item_atk.include?(@item_used) or @item_scope == 1..2
       # Perform attacks as normal
       if $rush_item
         if not (@moved[battler] or battler.guarding?)
           if $game_system.sideview_mirror == 1
             offset = (battler.is_a?(Game_Actor) ? -($rush_offset) : $rush_offset)
           else
             offset = (battler.is_a?(Game_Actor) ? $rush_offset : -($rush_offset))
           end
           @spriteset.battler(battler).setmove(battler.screen_x - offset, battler.screen_y + 1)
           @moved[battler] = true
           return
         elsif not battler.guarding?
           @spriteset.battler(battler).pose = $p8
           @spriteset.battler(battler).setmove(battler.screen_x, battler.screen_y)
         end
       else
         @spriteset.battler(battler).pose = $p8
       end  
     else
       # Perform rushing attack styled item
       if not (@moved[battler] or battler.guarding?)
         if $game_system.sideview_mirror == 1
           offset = (battler.is_a?(Game_Actor) ? -($rush_offset) : $rush_offset)
         else
           offset = (battler.is_a?(Game_Actor) ? $rush_offset : -($rush_offset))
         end
         @spriteset.battler(battler).setmove(target.screen_x + offset, target.screen_y)
         @moved[battler] = true
         return
       elsif not battler.guarding?
         @spriteset.battler(battler).pose = $p8
         @spriteset.battler(battler).setmove(battler.screen_x, battler.screen_y)
       end
     end
   end
   @moved[battler] = false
   @rtab ? cbs_update_phase4_step3(battler) : cbs_update_phase4_step3
 end
 #--------------------------------------------------------------------------
 # * Hit Animation
 #--------------------------------------------------------------------------
 alias cbs_update_phase4_step4 update_phase4_step4
 def update_phase4_step4(battler = @active_battler)
   for target in (@rtab ? battler.target : @target_battlers)
     damage = (@rtab ? target.damage[battler] : target.damage)
     if damage.is_a?(Numeric) and damage > 0
       @spriteset.battler(target).pose = $p2
     end
   end
   @rtab ? cbs_update_phase4_step4(battler) : cbs_update_phase4_step4
 end
 #--------------------------------------------------------------------------
 # * Victory Animation
 #--------------------------------------------------------------------------
 alias cbs_start_phase5 start_phase5
 def start_phase5
   for actor in $game_party.actors
     return if @spriteset.battler(actor).moving
   end
   for actor in $game_party.actors
     unless actor.dead?
       @spriteset.battler(actor).pose = $p10
       @spriteset.battler(actor).freeze
     end
   end
   cbs_start_phase5
 end
 #--------------------------------------------------------------------------
 # * Change Arrow Viewport
 #--------------------------------------------------------------------------
 alias cbs_start_enemy_select start_enemy_select
 def start_enemy_select
   cbs_start_enemy_select
   @enemy_arrow.dispose
   @enemy_arrow = Arrow_Enemy.new(@spriteset.viewport2)
   @enemy_arrow.help_window = @help_window
 end
end

#==============================================================================
# ** Spriteset_Battle
#==============================================================================

class Spriteset_Battle
 #--------------------------------------------------------------------------
 # * Change Enemy Viewport
 #--------------------------------------------------------------------------
 alias cbs_initialize initialize
 def initialize
   cbs_initialize
   @enemy_sprites = []
   for enemy in $game_troop.enemies.reverse
     @enemy_sprites.push(Sprite_Battler.new(@viewport2, enemy))
   end
 end
 #--------------------------------------------------------------------------
 # * Find Sprite From Battler Handle
 #--------------------------------------------------------------------------
 def battler(handle)
   for sprite in @actor_sprites + @enemy_sprites
     return sprite if sprite.battler == handle
   end
 end
end

#==============================================================================
# ** Arrow_Base
#==============================================================================

class Arrow_Base < Sprite
 #--------------------------------------------------------------------------
 # * Reposition Arrows
 #--------------------------------------------------------------------------
 alias cbs_initialize initialize
 def initialize(viewport)
   cbs_initialize(viewport)
   self.ox = 14
   self.oy = 10
 end
end

Jragyn

looking at the script you provided at the bottom, it looks like there is already a built-in array that you insert enemies' IDs into to cause them not to move.

Quote@stationary_enemies = [1]


Perhaps thats where you should put in the IDs at?
Forgive me if I am not understanding this entirely.
I haven't touched cogwheels' RTAB since I discovered KGC Count Battle :D
A bright light can either illuminate or blind, but how will you know which until you open your eyes?