The PokéCommunity Forums Fan Games Game Development Scripts & Tutorials
Script Small Pathfinder. No more manual move-routes necessary!

Scripts & Tutorials This forum is for scripts and code, as well as all kinds of tutorials, software, tools and so forth. Remember to give credit!
The thread revival limit does not apply here.


Reply
 
Thread Tools
  #1    
Old February 5th, 2017 (8:44 AM). Edited February 5th, 2017 by khkramer.
khkramer's Avatar
khkramer khkramer is offline
     
    Join Date: Apr 2010
    Gender: Male
    Posts: 321
    I've made a modified version of pbMoveTowardPlayer, which allows an event to move towards another event instead of the player ( handy for cutscenes etc where positions are dynamic ).

    Here is the script, place it somewhere above main:

    Spoiler:
    Code:
    class Game_Character
      def move_toward_event(event)
        sx = @x - event.x
        sy = @y - event.y
        if sx == 0 and sy == 0
          return
        end
        abs_sx = sx.abs
        abs_sy = sy.abs
        if abs_sx == abs_sy
          (rand(2) == 0) ? abs_sx += 1 : abs_sy += 1
        end
        if abs_sx > abs_sy
          sx > 0 ? move_left : move_right
          if not moving? and sy != 0
            sy > 0 ? move_up : move_down
          end
        else
          sy > 0 ? move_up : move_down
          if not moving? and sx != 0
            sx > 0 ? move_left : move_right
          end
        end
      end
      
      def move_toward_position(x, y)
        sx = @x - x
        sy = @y - y
        if sx == 0 and sy == 0
          return
        end
        abs_sx = sx.abs
        abs_sy = sy.abs
        if abs_sx == abs_sy
          (rand(2) == 0) ? abs_sx += 1 : abs_sy += 1
        end
        if abs_sx > abs_sy
          sx > 0 ? move_left : move_right
          if not moving? and sy != 0
            sy > 0 ? move_up : move_down
          end
        else
          sy > 0 ? move_up : move_down
          if not moving? and sx != 0
            sx > 0 ? move_left : move_right
          end
        end
      end
    end
    
    def getEventByName(name)
      if name.is_a?(Symbol)
        name = name.to_s
      end
      
      $game_map.events.values.each do |event|
        return event if event.name.downcase == name.downcase
      end
      
      return nil
    end
    
    def _resolveEvent(event)
      if event.is_a?(Game_Event)
        return event
      end
      
      if event.is_a?(Numeric)
        return get_character(event)
      end
      
      return getEventByName(event) # Symbol or string
    end
    
    def pbMoveTowardEvent(event, otherEvent)
      event = _resolveEvent(event)
      otherEvent = _resolveEvent(otherEvent)
      
      maxsize=[$game_map.width,$game_map.height].max
      #return if !pbEventCanReachPlayer?(event,otherEvent,maxsize)
      loop do
        x=event.x
        y=event.y
        event.move_toward_event(otherEvent)
        break if event.x==x && event.y==y
        while event.moving?
          Graphics.update
          Input.update
          pbUpdateSceneMap
        end
      end
      $PokemonMap.addMovedEvent(event.id) if $PokemonMap
    end
    
    def pbMoveTowardPosition(event, x, y)
      event = _resolveEvent(event)
        
      maxsize=[$game_map.width,$game_map.height].max
      #return if !pbEventCanReachPlayer?(event,otherEvent,maxsize)
      loop do
        event.move_toward_position(x, y)
        break if event.x==x && event.y==y
        while event.moving?
          Graphics.update
          Input.update
          pbUpdateSceneMap
        end
      end
      $PokemonMap.addMovedEvent(event.id) if $PokemonMap
    end


    The script can be used in an event like this:

    Code:
    pbMoveTowardEvent(:Distraction, :Oak)
    Where :Distraction and :Redwood are the names of the respective events ( case insensitive ).

    Or to move to a specified position instead:

    Code:
    pbMoveTowardPosition(:Distraction, 24, 15)
    Where :Distraction is the event's name, 24 is the X, and 15 is the Y.


    Hope this is useful for some of you :)
    If you find any bugs or have a suggestion, please let me know.
    __________________


    Lurking for 8 years..
    Reply With Quote

    Relevant Advertising!

      #2    
    Old April 3rd, 2017 (3:47 AM).
    Lionhearted Lionhearted is offline
       
      Join Date: Mar 2017
      Posts: 3
      This script looks great! Nice idea, I'm definitely going to try it out! :3
      Reply With Quote
        #3    
      Old May 12th, 2017 (9:00 PM).
      Jazerules Jazerules is offline
         
        Join Date: Apr 2017
        Posts: 34
        Tested works okay. Some few things I found.

        1. When the destination event is too far on the same map ( which you won't see if it is reaching the target),
        it waits like forever to move as if waiting for the event to finish but the game won't freeze as all animation in the map still moving.

        2. When blocked by moving event the game freezes and restarts.

        3. When the destination has wide obstacle in front, it just stay in the obstacle and not finding its way around on the target.

        Overall it is working good. Thanks for this beautiful script.
        Reply With Quote
          #4    
        Old August 6th, 2017 (5:40 AM).
        khkramer's Avatar
        khkramer khkramer is offline
           
          Join Date: Apr 2010
          Gender: Male
          Posts: 321
          Quote:
          Originally Posted by Jazerules View Post
          Tested works okay. Some few things I found.

          1. When the destination event is too far on the same map ( which you won't see if it is reaching the target),
          it waits like forever to move as if waiting for the event to finish but the game won't freeze as all animation in the map still moving.

          2. When blocked by moving event the game freezes and restarts.

          3. When the destination has wide obstacle in front, it just stay in the obstacle and not finding its way around on the target.

          Overall it is working good. Thanks for this beautiful script.
          Thanks for the feedback.
          I know it's a late response on my end but I'll be updating the script this week with fixes for these issues.
          __________________


          Lurking for 8 years..
          Reply With Quote
            #5    
          Old August 23rd, 2017 (12:13 AM).
          The Dewitty's Avatar
          The Dewitty The Dewitty is offline
          Pixel artist, professional leo
             
            Join Date: Jul 2009
            Location: Netherlands
            Age: 22
            Gender: Male
            Nature: Bold
            Posts: 266
            This could be really useful! Have the bugs mentioned by Jazerules been fixed?
            __________________
            Reply With Quote
              #6    
            Old January 7th, 2018 (8:51 AM).
            khkramer's Avatar
            khkramer khkramer is offline
               
              Join Date: Apr 2010
              Gender: Male
              Posts: 321
              Quote:
              Originally Posted by The Dewitty View Post
              This could be really useful! Have the bugs mentioned by Jazerules been fixed?
              Few months later and I still haven't fixed them :3c
              That's mostly cause this was intended as a really simple script, and I myself use it 90% for movement in indoor cutscenes. In situations like that it works perfectly fine.

              I did at one point have another pathfinder script based on an A* pathfinder edited to work with essentials which is much more useful for the problems described by Jazerules: dealing with obstacles, long distances etc. I'm going search my PC to see if I still got the script and if so I intend to release it here since I've picked up fangame development again.
              __________________


              Lurking for 8 years..
              Reply With Quote
              Reply

              Quick Reply

              Join the conversation!

              Create an account to post a reply in this thread, participate in other discussions, and more!

              Create a PokéCommunity Account

              Sponsored Links
              Thread Tools

              Posting Rules
              You may not post new threads
              You may not post replies
              You may not post attachments
              You may not edit your posts

              BB code is On
              Smilies are On
              [IMG] code is On
              HTML code is Off

              Forum Jump


              All times are GMT -8. The time now is 1:46 PM.