[XP] Help Adapting a Script

Started by CountVlad, October 17, 2010, 07:24:59 am

Previous topic - Next topic

CountVlad

Hi,

I have a script that does automatic movement for Birds and Pigeons. Basically the way it works is you name an event either Bird of Pigeon and it changes the movement accordingly (and the graphic for the pigeon). The Bird flys in a straight line in a random direction until it gets to the edge. Then it gets transfered to a random edge in a random position and repeats.
I've already adapted it slightly so instead of putting 'Bird' as the event name I can use 'NPC'. I've also changed it so that the event will only move left or right.

However, I want to adapt the script so that the event will transfer to either the left or right edge when it goes off the map and only horizontally between the coordinates Y15 and Y17 (X doesn't matter if it only transfers to the left or right edge).
That way the event will walk along the street and then walk along it again from either left or right when it gets to the end.

Anyway, the script (with adaptations):

#--------------------------------------------------------------------------
# * SDK Log
#--------------------------------------------------------------------------
SDK.log('Birds', 'tibuda', 1.0, '')

#--------------------------------------------------------------------------
# * SDK Requirement Check
#--------------------------------------------------------------------------
SDK.check_requirements(2.0, [1, 2])

#--------------------------------------------------------------------------
# Begin SDK Enabled Check
#--------------------------------------------------------------------------
if SDK.enabled?('Birds')
#==============================================================================
class Game_Event
  #--------------------------------------------------------------------------
  def name
    return @event.name
  end
  #--------------------------------------------------------------------------
  if SDK.enabled?('AntiLag')
    alias_method :tibuda_birds_antilag_gm_event_in_screen?, :in_screen?
    def in_screen?
      if self.name == 'Pigeon' || self.name == 'NPC'
        return true
      else
        return tibuda_birds_antilag_gm_event_in_screen?
      end
    end
  end
end

class Game_Character
  #--------------------------------------------------------------------------
  alias_method :tibuda_birds_gm_char_up_movement, :update_movement
  def update_movement
    if self.is_a?(Game_Event) && self.name == 'Pigeon'
      move_type_pigeon
    elsif self.is_a?(Game_Event) && self.name == 'NPC'
      move_type_bird
    else
      tibuda_birds_gm_char_up_movement
    end
  end
  #--------------------------------------------------------------------------
  def move_type_bird
    return if moving?
    @bird_dir = @direction unless @bird_dir.is_a?(Numeric)
    @move_frequency = 6
    @through = false
    @always_on_top = false
    if !moving? &&
       ((self.x == 0 && [1,4,7].include?(@bird_dir)) ||
        (self.y == 0 && [7,8,9].include?(@bird_dir)) ||
        (self.x == $game_map.width-1 && [3,6,9].include?(@bird_dir)) ||
        (self.y == $game_map.height-1 && [1,2,3].include?(@bird_dir)))
      @bird_dir = rand(8) + 1
      @bird_dir += 1 if @bird_dir == 5
      case @bird_dir
      when 1
        pos = rand($game_map.width + $game_map.height - 1)
        if pos <= $game_map.width
          self.moveto(pos, 0)
        else
          self.moveto($game_map.width-1, pos - $game_map.width + 1)
        end
      when 2
        self.moveto(rand($game_map.width), 0)
      when 3
        pos = rand($game_map.width + $game_map.height - 1)
        if pos <= $game_map.width
          self.moveto(pos, 0)
        else
          self.moveto(0, pos - $game_map.width + 1)
        end
      when 4
        self.moveto($game_map.width - 1, rand($game_map.height))
      when 6
        self.moveto(0, rand($game_map.height))
      when 7
        pos = rand($game_map.width + $game_map.height - 1)
        if pos <= $game_map.width
          self.moveto(pos, $game_map.height-1)
        else
          self.moveto($game_map.width-1, pos - $game_map.width + 1)
        end
      when 8
        self.moveto(rand($game_map.width), $game_map.height-1)
      when 9
        pos = rand($game_map.width + $game_map.height - 1)
        if pos <= $game_map.width
          self.moveto(pos, $game_map.height - 1)
        else
          self.moveto(0, pos - $game_map.width + 1)
        end
      end
    end
    case @bird_dir
    when 1
      move_left
    when 2
      move_left
    when 3
      move_right
    when 4
      move_left
    when 6
      move_right
    when 7
      move_left
    when 8
      move_right
    when 9
      move_right
    end
  end
  #--------------------------------------------------------------------------
  def move_type_pigeon
    range = ((self.x - $game_player.x)**2 + (self.y - $game_player.y)**2)**0.5
    limit = @move_frequency == 3 ? 2 : 5
    if range > limit && $game_map.passable?(self.x, self.y, 0)
      @character_name = '166-Small08'
      @move_frequency = @page.move_frequency
      @through = @page.through
      @always_on_top = @page.always_on_top
      tibuda_birds_gm_char_up_movement
    else
      @character_name = '165-Small07'
      if @move_frequency == 3
        turn_away_from_player
        @bird_dir = nil
      end
      move_type_bird
    end
  end
  #--------------------------------------------------------------------------
end

end
#--------------------------------------------------------------------------
# End SDK Enabled Test
#--------------------------------------------------------------------------

The Niche

Uhhh...pigeons are birds. [/asshole]
Level me down, I'm trying to become the anti-blizz!
Quote from: winkio on June 15, 2011, 07:30:23 pm
Ah, excellent.  You liked my amusing sideshow, yes?  I'm just a simple fool, my wit entertains the wise, and my wisdom fools the fools.



I'm like the bible, widely hated and beautifully quotable.

Dropbox is this way, not any other way!

CountVlad

Quote from: The Niche on October 17, 2010, 04:30:04 pm
Uhhh...pigeons are birds. [/asshole]

Thanks for that enlightened comment.
Has anyone worked out what I need to change yet?

ForeverZer0

That is an ass-load of code for such a simple script.
If I get time, I can re-write the script for you, remove the SDK requirement, and add the above mentioned configurables. I may get time tonight, but dont count on it till after the week-end.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

The Niche

Level me down, I'm trying to become the anti-blizz!
Quote from: winkio on June 15, 2011, 07:30:23 pm
Ah, excellent.  You liked my amusing sideshow, yes?  I'm just a simple fool, my wit entertains the wise, and my wisdom fools the fools.



I'm like the bible, widely hated and beautifully quotable.

Dropbox is this way, not any other way!

CountVlad

Quote from: ForeverZer0 on October 19, 2010, 12:15:45 pm
That is an ass-load of code for such a simple script.
If I get time, I can re-write the script for you, remove the SDK requirement, and add the above mentioned configurables. I may get time tonight, but dont count on it till after the week-end.

Thanks! I appreciate it! :D

ForeverZer0

Will post soon. Had it almost done last night, but got tired and went to bed instead of fixing one last little thing. So far, it allows you to define specific directions if you like, or keep it random. Also allows you to set the "variance", which will keep the event within a defined range when it reappears on the screen, which I believe is what you are looking for. All done with a couple comments in the event's code. Including comments, it is still roughly half the code of your current script, as well.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

CountVlad

Wow! It sound great! :)
Don't worry, there's no rush as it's one of those things that can be added later without too much trouble.

ForeverZer0

October 26, 2010, 12:22:12 pm #8 Last Edit: October 27, 2010, 12:01:53 pm by ForeverZer0
Here, try this. Instructions are included. I hope this kinda what your looking for. Let me know if you need something changed. I may even put this in Zer0 Add-Ons at some point.

Spoiler: ShowHide
#=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# Automatic Event Movement
# Author: ForeverZer0
# Version: 1.0
# Date: 10.22.10
#=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#
# Introduction:
#   This script will allow you to easily set up events to move across the game
#   map in random manner. The event will move from a point on the map's edge in
#   a line until it reaches another map edge, at which point it will transfer
#   randomly to another point and take a new direction and repeat. The user
#   also has the option to define possible directions that the event can switch
#   to and the number of tiles that event can move from its position. All of
#   the setup is a few simple comments in the event's page.
#
# Instructions:
#   Create an event, setting the graphic, move speed, and move frequency as
#   desired. Don't forget to check the "Always on Top" and "Through" boxes if
#   using this for a bird. Set the event's direction if you do not want it to be
#   chosen randomly by changing the graphic to the appropriate direction. If the
#   "Direction Fix" box is checked, it will disable the event from changing
#   directions randomly when it begins a new route across the screen.
#
#   Optional Featues:
#
#     To set the possible directions that the event can choose from randomly
#     after it exits the screen, create another comment under the first that
#     reads:
#
#             Directions = DIRECTION1 DIRECTION2 DIRECTION3 etc etc
#
#     Do not seperate the directions with commas, simply use single spaces.
#     The values are numbers that correspond to each directions. The directions
#     are releative to a calculator keypad setup:
#
#                               7  8  9
#                               4     6
#                               1  2  3
#
#     To set the variance, which is the number of tiles give or take that the
#     event can possibly shift from its original position when re-entering the
#     screen, create a comment that reads:
#
#             Variance = NUMBER_OF_TILES
#
#     Here is an example of what the comment should look like:
#
#               Comment: Auto Move
#                        Directions = 4 6
#                        Variance = 1
#  
#     In this example, the event will move randomly either left or right across
#     the screen, and stay within one tile either way from it original coords.
#
#=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

class Game_Event
 
 # Private constant. DO NOT edit.
 MOVES = [nil, :move_lower_left, :move_down, :move_lower_right, :move_left,
          nil, :move_right, :move_upper_left, :move_up, :move_upper_right]
 
 alias zer0_automove_event_init initialize
 def initialize(map_id, event)
   # Set event's original coordinates and map size in instance variables.
   @coords = [event.x, event.y]
   @map_w, @map_h = $game_map.width, $game_map.height
   zer0_automove_event_init(map_id, event)
 end

 alias zer0_automove_event_refresh refresh
 def refresh
   zer0_automove_event_refresh
   if @page != nil
     # Set flag if comment "Auto Move" is found on top line.
     if @page.list[0].code == 108 && @page.list[0].parameters[0] == 'Auto Move'
       @bird_event, @dir = true, @direction
       # Set directions and variance if defined, else it will be chosen randomly.
       @bird_variance = comment_value('Variance = ')
       directions = comment_value('Directions = ', true)
       @directions = directions == nil ? [1, 2, 3, 4, 6, 7, 8, 9] : directions
     end
   end
 end
 
 def comment_value(string, to_a = false)
   # Returns the defined integer following the string in an event comment.
   @page.list.each {|command| if [108, 408].include?(command.code) &&
   command.parameters[0] =~ string
     string = command.parameters[0].gsub(string) { '' }
     return to_a ? string.split(' ').collect {|num| num.to_i } : string.to_i
   end }
   return
 end
 
 alias zer0_automove_event_upd update
 def update
   # Check if this event is a auto-move and not moving.
   if @bird_event && !moving?
     # Check if event is on the screen.
     unless at_map_edge?
       self.send(MOVES[@dir])
     else # If event has reached edge of screen.
       # Reset the event's direction unless the Direction Fix box checked.
       @dir = @directions[rand(@directions.size)] unless @page.direction_fix
       x, y = @dir == 4 ? @map_w-1 : 0, [1, 2, 3].include?(@dir) ? 0 : @map_h-1
       if @bird_variance == nil
         # Set coordinates randomly if no variance is defined.
         [4, 6].include?(@dir) ? moveto(x, rand(@map_h)) : moveto(rand(@map_w), y)
       else
         value = rand(@bird_variance+1)
         # If variance is defined, keep within defined range, randomly.
         if [4, 6].include?(@dir)
           moveto(x, rand(2) == 0 ? (@coords[1]+value) : (@coords[1]-value))
         else
           moveto(rand(2) == 0 ? (@coords[0]+value) : (@coords[0]-value), y)
         end
       end
     end
   end
   zer0_automove_event_upd
 end

 def at_map_edge?
   # Returns true if event's next move is off the map's visible edge.
   return case @dir
   when 1 then self.x == 0 || self.y == @map_h-1
   when 2 then self.y == @map_h-1
   when 3 then self.x == @map_w-1 || self.y == @map_h-1
   when 4 then self.x == 0
   when 6 then self.x == @map_w-1
   when 7 then [self.x, self.y].include?(0)
   when 8 then self.y == 0
   when 9 then self.x == @map_w-1 || self.y == 0
   end
 end
end  
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

CountVlad

Wow! It works perfectly! :D

One thing, you may wish to edit the example in the comment.
It says that you have to put in "Bird Event" to make the event move automatically. In the coding it's set to "Auto Move".

ForeverZer0

Quote from: CountVlad on October 26, 2010, 09:39:46 pm
Wow! It works perfectly! :D

One thing, you may wish to edit the example in the comment.
It says that you have to put in "Bird Event" to make the event move automatically. In the coding it's set to "Auto Move".


Oops, you are correct, sir.
I originally went with bird event, then changed it, since it may not be used exclusively for that. I changed that. Thanks.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.