Sign in to follow this  
Followers 0
JosySasuke

Script de Pathfinding

5 posts in this topic

Muy bien, este es un script de pathfinding (buscador de caminos).

Script

[spoiler]
CODE
#==============================================================================
#  ** Path Finding
#==============================================================================
# Near Fantastica
# Version 1
# 29.11.05
#==============================================================================
# Lets the Player or Event draw a path from an desonation to the source. This
# method is very fast and because the palthfinding is imbeded into the Game
# Character the pathfinding can be inturputed or redrawn at any time.
#==============================================================================
# Jugador :: $game_player.find_path(x,y)
# Llamar evento :: self.event.find_path(x,y)
# Movimiento de llamar evento :: self.find_path(x,y)
#==============================================================================

#--------------------------------------------------------------------------
# * SDK Log Script
#--------------------------------------------------------------------------
SDK.log("Path Finding", "Near Fantastica", 1, "29.11.05")

#--------------------------------------------------------------------------
# * Begin SDK Enable Test
#--------------------------------------------------------------------------
if SDK.state("Path Finding") == true
 class Game_Character
   #--------------------------------------------------------------------------
   alias nf_pf_game_character_initialize initialize
   alias nf_pf_game_character_update update
   #--------------------------------------------------------------------------
   attr_accessor :map
   attr_accessor :runpath
   #--------------------------------------------------------------------------
   def initialize
     nf_pf_game_character_initialize
     @map = nil
     @runpath = false
   end
   #--------------------------------------------------------------------------
   def update
     run_path if @runpath == true
     nf_pf_game_character_update
   end
   #--------------------------------------------------------------------------
   def run_path
     return if moving?
     step = @map[@x,@y]
     if step == 1
       @map = nil
       @runpath = false
       return
     end
     dir = rand(2)
     case dir
     when 0
       move_right if @map[@x+1,@y] == step - 1 and step != 0
       move_down if @map[@x,@y+1] == step - 1 and step != 0
       move_left if @map[@x-1,@y] == step -1 and step != 0
       move_up if @map[@x,@y-1] == step - 1 and step != 0
     when 1
       move_up if @map[@x,@y-1] == step - 1 and step != 0
       move_left if @map[@x-1,@y] == step -1 and step != 0
       move_down if @map[@x,@y+1] == step - 1 and step != 0
       move_right if @map[@x+1,@y] == step - 1 and step != 0
     end
   end
   #--------------------------------------------------------------------------
   def find_path(x,y)
     sx, sy = @x, @y
     result = setup_map(sx,sy,x,y)
     @runpath = result[0]
     @map = result[1]
     @map[sx,sy] = result[2] if result[2] != nil
   end
   #--------------------------------------------------------------------------
   def clear_path
     @map = nil
     @runpath = false
   end
   #--------------------------------------------------------------------------
   def setup_map(sx,sy,ex,ey)
     map = Table.new($game_map.width, $game_map.height)
     map[ex,ey] = 1
     old_positions = []
     new_positions = []
     old_positions.push([ex, ey])
     depth = 2
     depth.upto(100){|step|
       loop do
         break if old_positions[0] == nil
         x,y = old_positions.shift
         return [true, map, step] if x == sx and y+1 == sy
         if $game_player.passable?(x, y, 2) and map[x,y + 1] == 0
           map[x,y + 1] = step
           new_positions.push([x,y + 1])
         end
         return [true, map, step] if x-1 == sx and y == sy
         if $game_player.passable?(x, y, 4) and map[x - 1,y] == 0
           map[x - 1,y] = step
           new_positions.push([x - 1,y])
         end
         return [true, map, step] if x+1 == sx and y == sy
         if $game_player.passable?(x, y, 6) and map[x + 1,y] == 0
           map[x + 1,y] = step
           new_positions.push([x + 1,y])
         end
         return [true, map, step] if x == sx and y-1 == sy
         if $game_player.passable?(x, y, 8) and map[x,y - 1] == 0
           map[x,y - 1] = step
           new_positions.push([x,y - 1])
         end
       end
       old_positions = new_positions
       new_positions = []
     }
     return [false, nil, nil]
   end
 end
 
 class Game_Map
   #--------------------------------------------------------------------------
   alias pf_game_map_setup setup
   #--------------------------------------------------------------------------
   def setup(map_id)
     pf_game_map_setup(map_id)
     $game_player.clear_path
   end
 end
 
 class Game_Player
   #--------------------------------------------------------------------------
   alias pf_game_player_update_player_movement update_player_movement
   #--------------------------------------------------------------------------
   def update_player_movement
     $game_player.clear_path if Input.dir4 != 0
     pf_game_player_update_player_movement
   end
 end
 
 class Interpreter
   #--------------------------------------------------------------------------
   def event
     return $game_map.events[@event_id]
   end
 end
 
#--------------------------------------------------------------------------
# * End SDK Enable Test
#--------------------------------------------------------------------------
end
[/spoiler]

Requiere SDK
Script creado por Near Fantastica

Instrucciones

Coloca el script arriba de Main y debajo del SDK.

Este script sirve para mover al personaje o a un evento en una escena, sin deducir nada en Mover evento, y ese rollo:
Pon en llamar script:

CODE
$game_player.find_path(Coordenada X,Coordenada Y)


Y si se trata de un evento lo que quieres mover, usa:

CODE
self.event.find_path(Coordenada X,Coordenada Y )


ATENCION: Para que se mueva, dicho evento, el evento de la escena, debe ser el de el evento a mover. Puedes hacer una nueva página en el evento a mover, accionada con un interruptor, que en esa pagina, llame al script, y le pones un Esperar 20 frames, luego activar otro interruptor, que activa una nueva página del evento de la escena (el evento que se mueve no, el que tiene los dialogos, los llamar scripts, en fin, el de la escena.) para que continue la escena, y un desactivar interruptor (el que activo que se moviera dicho evento).

Salu2 Edited by JosySasuke
0

Share this post


Link to post
Share on other sites
Por lo que entiendo en estas líneas:

Jugador :: $game_player.find_path(x,y)
Llamar evento :: self.event.find_path(x,y)
Movimiento de llamar evento :: self.find_path(x,y)

Para que el jugador vaya de un lugar a otro sin poner mucha mierda en Mover Evento, debes poner (me imagino yo):

$game_player.find_path(Coordenada X del punto a Ir,Coordenada Y del punto a Ir)

O sea, si el jugador anda en las coordenadas 2 (x) y 16 (y) y yo pongo:

$game_player.find_path(22,63)

El jugador irá a esas coordenadas especificadas desde su posición actual.

self.event.find_path(Coordenada X del punto a Ir,Coordenada Y del punto a Ir)

Me imagino que es igual al del jugador excepcuanto que este mueve al evento en donde pusiste este comando.

O sea que si pusiste ese comando en el EV008 moverá el evento 8 a las coordenasas puestas.

El otro comando no tengo idea.

========

NOTA: Eso es lo que deduzco a simple vista. Tu prueba a ver si funciona.
0

Share this post


Link to post
Share on other sites
Vale, vale, ya lo cambie xD
Bueno, este script lo querra mucha gente. Edited by JosySasuke
0

Share this post


Link to post
Share on other sites
Es exactamente como ha dicho Drako. Y la verdad, cuando intenté usar el otro comando me da error, así que supongo que servirá para mover desde un evento otro evento distinto, pero habrá que señalarlo de alguna manera.
0

Share this post


Link to post
Share on other sites
¿Hay alguna manera de combinar este script con variables?

Seria muy util... si alguien sabe como hacerlo q lo postee porfavor.

S a l u d o s P o l l a 3
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0