Saturday, 10 August 2013

I found new path finding type

I found new path finding type

I researched for 6 hours and I found new pathfinding type. The question is
is it useful? Or should I back to a* pathfinding? Copy the code below and
save it at whatever.py then go to parent folder/data/map/ and create
test.map like this:
[map]
name=test map
type=multiplayer
width=12
height=10
0=gggGgggggggg
1=gggGggGgGggg
2=GGGGggGggggg
3=AggggggGgggg
4=GGGGGggGgggg
5=GGgggGGGGGgg
6=gggGggBgGggG
7=GgGGGGGGGggG
8=gggggggggggg
9=GGGGGGGGGGGG
Here is the code: (A* pathfinding and this pathfinding is very different)
import time
import struct
from tkinter import*
from configparser import SafeConfigParser
parser = SafeConfigParser()
parser.read('../data/map/test.map')
def rgb2h(rgbc):
assert(len(rgbc) == 3)
return '#%02x%02x%02x' % rgbc
mw = parser.get('map','width')
mh = parser.get('map','height')
map = [[0 for x in range(int(mh)+1)] for x in range(int(mw)+1)]
pathA = [[99999 for x in range(int(mh)+1)] for x in range(int(mw)+1)]
pathAS = [[[-1,-1] for x in range(int(mh)+1)] for x in range(int(mw)+1)]
wd = Tk()
cv = Canvas(wd, width=int(mw)*16, height=int(mh)*16,bg="white")
cv.pack(side=TOP)
PB = [00,00]
PA = [00,00]
for xx in range(int(mw)):
for yy in range(int(mh)):
map[xx][yy] = parser.get('map',str(yy))[xx]
if map[xx][yy] == "A":
PA = [xx,yy]
pathA[xx+1][yy] = 1
pathA[xx-1][yy] = 1
pathA[xx][yy-1] = 1
pathA[xx][yy+1] = 1
if map[xx][yy] == "B":
PB = [xx,yy]
chance = 1
while chance == 1:
for xx in range(int(mw)):
for yy in range(int(mh)):
if map[xx][yy] in ['A','P']:
if map[xx-1][yy] not in ['G','A']:
if not map[xx-1][yy] == "P":
map[xx-1][yy] = 'P'
chance = 2
if pathA[xx-1][yy] > pathA[xx][yy]:
pathA[xx-1][yy] = pathA[xx][yy] + 1
pathAS[xx-1][yy] = [xx,yy]
if map[xx+1][yy] not in ['G','A']:
if not map[xx+1][yy] == "P":
map[xx+1][yy] = 'P'
chance = 2
if pathA[xx+1][yy] > pathA[xx][yy]:
pathA[xx+1][yy] = pathA[xx][yy] + 1
pathAS[xx+1][yy] = [xx,yy]
if map[xx][yy-1] not in ['G','A']:
if not map[xx][yy-1] == "P":
map[xx][yy-1] = 'P'
chance = 2
if pathA[xx][yy-1] > pathA[xx][yy]:
pathA[xx][yy-1] = pathA[xx][yy] + 1
pathAS[xx][yy-1] = [xx,yy]
if map[xx][yy+1] not in ['G','A']:
if not map[xx][yy+1] == "P":
map[xx][yy+1] = 'P'
chance = 2
if pathA[xx][yy+1] > pathA[xx][yy]:
pathA[xx][yy+1] = pathA[xx][yy] + 1
pathAS[xx][yy+1] = [xx,yy]
chance -= 1
CP = PB
PATHLIST = []
iii = 0
while not CP == PA:
cxx = CP[0]
cyy = CP[1]
CP = [pathAS[cxx][cyy][0],pathAS[cxx][cyy][1]]
PATHLIST.append(CP)
if ([cxx-1,cyy] == PA) or ([cxx+1,cyy] == PA) or ([cxx,cyy-1] == PA)
or (([cxx,cyy+1] == PA)):
CP = PA
iii += 1
for xx in range(int(mw)):
for yy in range(int(mh)):
if map[xx][yy] == "G":
cv.create_rectangle(xx*16,yy*16,xx*16+16,yy*16+16,fill=rgb2h((255,0,0)))
if map[xx][yy] == "g":
cv.create_rectangle(xx*16,yy*16,xx*16+16,yy*16+16,fill="yellow")
if map[xx][yy] == "P":
cv.create_rectangle(xx*16,yy*16,xx*16+16,yy*16+16,fill="blue")
for xx in range(iii):
cv.create_rectangle(PATHLIST[xx][0]*16,PATHLIST[xx][1]*16,PATHLIST[xx][0]*16+16,PATHLIST[xx][1]*16+16,fill="purple")
wd.wm_title("Pathfinder")
wd.mainloop()

No comments:

Post a Comment