Draycos Goldaryn
Member
I have Near_Fantastica's Pathfinding script. The only problem is that it only moves the character in four directions. I also have my own script which allows for facing in 8 directions and have tried editing Near's script to allow 8 directional movement. My only problem is when the script is called, the character moves as it should, except it faces seemingly random directions as it moves, not always the direction in which it is moving.
Here are the two versions of the script.
Can anyone help me?
Here are the two versions of the script.
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.
#==============================================================================
# Player :: $game_player.find_path(x,y)
# Event Script Call :: self.event.find_path(x,y)
# Event Movement Script Call :: 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
Code:
#==============================================================================
# â– Path Finding
#==============================================================================
# Near Fantastica *edited by Draycos Goldaryn
# Version 1 *1.5 beta
# 29.11.05 *26.02.07
#==============================================================================
# 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.
#==============================================================================
# Player :: $game_player.find_path(x,y)
# Event Script Call :: self.event.find_path(x,y)
# Event Movement Script Call :: 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(4)
case dir
when 0
move_upper_right if @map[@x+1,@y-1] == step - 1 and step != 0
move_lower_right if @map[@x+1,@y+1] == step - 1 and step != 0
move_lower_left if @map[@x-1,@y+1] == step - 1 and step != 0
move_upper_left if @map[@x-1,@y-1] == step - 1 and step != 0
when 1
move_down if @map[@x,@y+1] == step - 1 and step != 0
move_right if @map[@x+1,@y] == 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 2
move_upper_left if @map[@x-1,@y-1] == step - 1 and step != 0
move_lower_left if @map[@x-1,@y+1] == step - 1 and step != 0
move_lower_right if @map[@x+1,@y+1] == step - 1 and step != 0
move_upper_right if @map[@x+1,@y-1] == step - 1 and step != 0
when 3
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
# lower left
return [true, map, step] if x-1 == sx and y+1 == sy
if (($game_player.passable?(@x, @y, 2) and $game_player.passable?(@x, @y + 1, 4)) or
($game_player.passable?(@x, @y, 4) and $game_player.passable?(@x - 1, @y, 2))) and map[x - 1,y + 1] == 0
map[x - 1,y + 1] = step
new_positions.push([x - 1,y + 1])
end
# down
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
# lower right
return [true, map, step] if x+1 == sx and y+1 == sy
if (($game_player.passable?(@x, @y, 2) and $game_player.passable?(@x, @y + 1, 6)) or
($game_player.passable?(@x, @y, 6) and $game_player.passable?(@x + 1, @y, 2))) and map[x + 1,y + 1] == 0
map[x + 1,y + 1] = step
new_positions.push([x + 1,y + 1])
end
# left
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
# right
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
# upper left
return [true, map, step] if x-1 == sx and y-1 == sy
if (($game_player.passable?(@x, @y, 8) and $game_player.passable?(@x, @y - 1, 4)) or
($game_player.passable?(@x, @y, 4) and $game_player.passable?(@x - 1, @y, 8))) and map[x - 1,y - 1] == 0
map[x - 1,y - 1] = step
new_positions.push([x - 1,y - 1])
end
# up
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
# upper right
return [true, map, step] if x+1 == sx and y-1 == sy
if (($game_player.passable?(@x, @y, 2) and $game_player.passable?(@x, @y - 1, 6)) or
($game_player.passable?(@x, @y, 6) and $game_player.passable?(@x + 1, @y, 2))) and map[x + 1,y - 1] == 0
map[x + 1,y - 1] = step
new_positions.push([x + 1,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.dir8 != 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
Can anyone help me?