-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAStarWorker.py
More file actions
123 lines (101 loc) · 3.02 KB
/
AStarWorker.py
File metadata and controls
123 lines (101 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import socket
import Tile
import math
import time
from Node import *
def getTile(x,y):
global width,height,tiles
if 0 > x or x >= width or 0 > y or y >= height:
return Tile.void
return Tile.tiles[tiles[x + y * width]]
def getDistance(a,b):
dx = a[0] - b[0]
dy = a[1] - b[1]
return math.sqrt(dx*dx+dy*dy)
def inList(l,i):
for node in l:
if node.pos == i:
return True
return False
def lookForFastest(self,l):
currentSmallest =0
bestNode = None
if len(l)==1:
return l[0]
for i in l:
if i.costSoFar<currentSmallest or currentSmallest==0:
currentSmallest =i.costSoFar
bestNode = i
return bestNode
def findPath(self,start,goal):
openList = []
closedList = []
currentNode = Node(start,None,0,self.getDistance(start,goal))
openList.append(currentNode)
while len(openList) >0:
#sorted(openList,key=lambda i: i.totalCost)
currentNode = self.lookForFastest(openList) #only use node with lowest cost
if currentNode.pos==goal:
path = []
while currentNode.parent != None:#goes until reaches the start
path.append(currentNode)
currentNode = currentNode.parent
openList = []
closedList = []
return path
openList.remove(currentNode)
closedList.append(currentNode)
for i in range(9):
if i==4:
continue#ignore current tile
x = currentNode.pos[0]
y = currentNode.pos[1]
dx = (i % 3) -1
dy = (i / 3) -1
tile = self.getTile(x+dx,y+dy)
if tile == None or tile == Tile.void:
continue
if tile.isSolid:
#print 'solid'
continue
tilePos = (x+dx,y+dy)
costSoFar = currentNode.costSoFar + (self.getDistance(currentNode.pos,tilePos)+tile.getSpeed())
distanceToEnd = self.getDistance(tilePos,goal)
node = Node(tilePos,currentNode,costSoFar,distanceToEnd)
if self.inList(closedList,tilePos) and costSoFar >= node.costSoFar: #checks if node has already been used,or if you are going backwards
continue
if not self.inList(openList,tilePos) or costSoFar < node.costSoFar:#only add if it's not alredy there
openList.append(node)
print "No path :("
return None
print "Starting!"
HOST = '127.0.0.1'
PORT = 1337
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
print "TCP socket up!"
arraystring = ''
tiles = None
print 'Waiting for tiles...'
data = s.recv(4096)
print data
data = eval(data)
print len(data)
width = data[len(data)-2:len(data)-1][0]
height = data[len(data)-1:len(data)][0]
tiles = data[:len(data)-2]
print width,height
print tiles
print 'Tiles recieved!'
while 1:
data = s.recv(4096)
request = eval(data)
print request
firstNode = findPath(request[0],request[1])
if firstNode == None or len(firstNode)==0:
continue
firstNode = firstNode[len(firstNode)-1]
toSend = [str(firstNode.pos[0]),str(firstNode.pos[1])]
toSend = repr(toSend)
s.sendall(toSend)
s.close()