for ln in fnlines:
l = ln.split(" ")
if l[0] == self._tag and l[1] == self._value:
+ l[3] = l[3].replace("\n", "")
touch = l[3]
return touch
return [ (tv+1, th), (tv, th+1), (tv-1, th), (tv, th-1) ]
- # Walk through the now existing data files and make sure we always pick the correct
- # sources for every tile, thus evading previous edge detection errors
- def _placeTileSources(self, mlat, mlng):
+ # Scan connections of a specific tile
+ def _scanConnections(self, tile):
+ # The array of visited tiles
+ visited = []
# None of the edges have reached their end yet
edge_end = [0,0,0,0] # top, right, bottom, left
-
- # Array with info we need
- resinfo = ["", ""] # Touch, Resource numbers
-
- # Go through everything until the end is reached (no more options left)
end_reached = False
while end_reached == False:
-
# Go north
- tv = self._tile_v
- th = self._tile_h
+ tv = tile[0]
+ th = tile[1]
while edge_end[0] == 0:
- resinfo[0] = self._getResourceTouch(tv, th)
- resinfo[1] = self._getResourceInfo(tv, th)
-
- # Only do the following steps if we have nothing stored
- if resinfo[1] == "0":
- neighbors = self._findNeighbors(tv, th)
- neighborinfo = ["",""]
- for n in range(0, len(neighbors)):
- neighborinfo[0] = self._getResourceTouch(neighbors[n][0], neighbors[n][1])
- neighborinfo[1] = self._getResourceInfo(neighbors[n][0], neighbors[n][1])
- if "b" in neighborinfo[0] and neighborinfo[1] != "0":
- resinfo[1] = neighborinfo[1]
- break
-
- # Store info. At this point we either have neighboring data, or
- # we need to select something
- if resinfo[1] == "0": resinfo[1] = self._selectResources()
- self._storeResourceInfo(tv, th, resinfo[1])
-
- # Move forward
- if "t" in resinfo[0]:
+ owntouch = self._getResourceTouch(tv, th)
+ if "t" in owntouch:
+ if self._checkVisited(visited, (tv, th)) == False: visited.append((tv, th))
+ if self._checkVisited(visited, (tv+1, th)) == False: visited.append((tv+1, th))
tv=tv+1
else:
edge_end[0] = 1
-
# Go east
- tv = self._tile_v
- th = self._tile_h
+ tv = tile[0]
+ th = tile[1]
while edge_end[1] == 0:
- resinfo[0] = self._getResourceTouch(tv, th)
- resinfo[1] = self._getResourceInfo(tv, th)
-
- # Only do the following steps if we have nothing stored
- if resinfo[1] == "0":
- neighbors = self._findNeighbors(tv, th)
- neighborinfo = ["",""]
- for n in range(0, len(neighbors)):
- neighborinfo[0] = self._getResourceTouch(neighbors[n][0], neighbors[n][1])
- neighborinfo[1] = self._getResourceInfo(neighbors[n][0], neighbors[n][1])
- if "l" in neighborinfo[0] and neighborinfo[1] != "0":
- resinfo[1] = neighborinfo[1]
- break
-
- # Store info. At this point we either have neighboring data, or
- # we need to select something
- if resinfo[1] == "0": resinfo[1] = self._selectResources()
- self._storeResourceInfo(tv, th, resinfo[1])
-
- # Move forward
- if "r" in resinfo[0]:
+ owntouch = self._getResourceTouch(tv, th)
+ if "r" in owntouch:
+ if self._checkVisited(visited, (tv, th)) == False: visited.append((tv, th))
+ if self._checkVisited(visited, (tv, th+1)) == False: visited.append((tv, th+1))
th=th+1
else:
edge_end[1] = 1
-
# Go south
- tv = self._tile_v
- th = self._tile_h
+ tv = tile[0]
+ th = tile[1]
while edge_end[2] == 0:
- resinfo[0] = self._getResourceTouch(tv, th)
- resinfo[1] = self._getResourceInfo(tv, th)
-
- # Only do the following steps if we have nothing stored
- if resinfo[1] == "0":
- neighbors = self._findNeighbors(tv, th)
- neighborinfo = ["",""]
- for n in range(0, len(neighbors)):
- neighborinfo[0] = self._getResourceTouch(neighbors[n][0], neighbors[n][1])
- neighborinfo[1] = self._getResourceInfo(neighbors[n][0], neighbors[n][1])
- if "t" in neighborinfo[0] and neighborinfo[1] != "0":
- resinfo[1] = neighborinfo[1]
- break
-
- # Store info. At this point we either have neighboring data, or
- # we need to select something
- if resinfo[1] == "0": resinfo[1] = self._selectResources()
- self._storeResourceInfo(tv, th, resinfo[1])
-
- # Move forward
- if "b" in resinfo[0]:
+ owntouch = self._getResourceTouch(tv, th)
+ if "b" in owntouch:
+ if self._checkVisited(visited, (tv, th)) == False: visited.append((tv, th))
+ if self._checkVisited(visited, (tv-1, th)) == False: visited.append((tv-1, th))
tv=tv-1
else:
edge_end[2] = 1
-
# Go west
- tv = self._tile_v
- th = self._tile_h
+ tv = tile[0]
+ th = tile[1]
while edge_end[3] == 0:
- resinfo[0] = self._getResourceTouch(tv, th)
- resinfo[1] = self._getResourceInfo(tv, th)
-
- # Only do the following steps if we have nothing stored
- if resinfo[1] == "0":
- neighbors = self._findNeighbors(tv, th)
- neighborinfo = ["",""]
- for n in range(0, len(neighbors)):
- neighborinfo[0] = self._getResourceTouch(neighbors[n][0], neighbors[n][1])
- neighborinfo[1] = self._getResourceInfo(neighbors[n][0], neighbors[n][1])
- if "r" in neighborinfo[0] and neighborinfo[1] != "0":
- resinfo[1] = neighborinfo[1]
- break
-
- # Store info. At this point we either have neighboring data, or
- # we need to select something
- if resinfo[1] == "0": resinfo[1] = self._selectResources()
- self._storeResourceInfo(tv, th, resinfo[1])
-
- # Move forward
- if "l" in resinfo[0]:
+ owntouch = self._getResourceTouch(tv, th)
+ if "b" in owntouch:
+ if self._checkVisited(visited, (tv, th)) == False: visited.append((tv, th))
+ if self._checkVisited(visited, (tv, th-1)) == False: visited.append((tv, th-1))
th=th-1
else:
edge_end[3] = 1
if edge_end[0] == 1 and edge_end[1] == 1 and edge_end[2] == 1 and edge_end[3] == 1:
- end_reached = True # <- Break loop
\ No newline at end of file
+ end_reached = True # <- Break loop
+
+ return visited
+
+
+ # Walk through the now existing data files and make sure we always pick the correct
+ # sources for every tile, thus evading previous edge detection errors
+ def _placeTileSources(self, mlat, mlng):
+
+ # Initial scan "cross"
+ visited = self._scanConnections((self._tile_v, self._tile_h))
+
+ # Recursive scan of initial cross
+ for v in range(len(visited)):
+ vst = self._scanConnections((visited[v][0], visited[v][1]))
+ for l in range(len(vst)):
+ if self._checkVisited(visited, (vst[l][0], vst[l][1])) == False: visited.append((vst[l][0], vst[l][1]))
+
+ # We will take the resource of the original tile we have started
+ # our scan from. If there is nothing, we can choose.
+ # This is then used for all visited tiles.
+ tv = self._tile_v
+ th = self._tile_h
+ resstring = self._getResourceInfo(tv, th)
+ if resstring == "0" or resstring == "": resstring = self._selectResources()
+ for v in range(0, len(visited)):
+ self._storeResourceInfo(visited[v][0], visited[v][1], resstring)
+
+
+
+ # Check if a tile was visited
+ def _checkVisited(self, visited, tile):
+ v = False
+ for t in range(0, len(visited)):
+ if visited[t][0] == tile[0] and visited[t][1] == tile[1]:
+ v = True
+ break
+ return v
+
+
+ # Show result of path tracing - DEBUG ONLY
+ def _debugRender(self, visited, mlat, mlng):
+ rmap = Image.new("RGBA", (mlng*10, mlat*10), color="#FFFFFF")
+ draw = ImageDraw.Draw(rmap)
+ for v in range(len(visited)):
+ sx, sy = (visited[v][1]-1)*10, (rmap.height-(visited[v][0]-1)*10)-10
+ dx, dy = ((visited[v][1]-1)*10)+10, (rmap.height-(visited[v][0]-1)*10)+10
+ shape = [ (sx, sy), (dx, dy) ]
+ fill = (0,0,0,255)
+ draw.rectangle(shape, fill=fill)
+ rmap.show()