def estimate(self, xDest, yDest): #convert to Hex Coordinate for easier calculation a = HexMath.convertArrayToHex(self.xPos,self.yPos) b = HexMath.convertArrayToHex(xDest,yDest) #calculate differenc beteween x and y values, and between does two dX = b[0]-a[0] dY = b[1]-a[1] dZ = dY-dX return max((dX,dY,dZ))
def InterpolateOpacity(self,Start,End,Tile1,Tile2): start = self.getTile(Start).center end = self.getTile(End).center point = self.getTile(Tile1).center d1 = HexMath.distancefromLine(start[0],start[1],end[0],end[1], point[0], point[1]) point = self.getTile(Tile2).center d2 = HexMath.distancefromLine(start[0],start[1],end[0],end[1], point[0], point[1]) d1 = round((d1/float((d1+d2))),2) #percentage distance for d1 d2 = round(1.0 - d1,2) # precentage distance for d2 #weight view difficulty return round((self.getTile(Tile1).opacity * d1 + self.getTile(Tile2).opacity * d2),2)
def nextHexes(next1,next2,cur,cur2): h = HexMath.getNeighbours(cur.x,cur.y,self.x,self.y) for pos in h: #check if hex is intersected by line,still some bugs tile = self.getTile(pos) n = HexMath.hexintersectsline(tile,startTile,goalTile) if n == 1 and tile != last1 and tile != last2 and tile != cur and tile!= cur2 and tile != next1 and tile!=next2: if next1 == None: next1 = tile elif next2 == None: next2 = tile return(next1,next2)
def HighlightMutipleHex(self,HexList): self.pathfind.fill((0,0,0,0)) color = pygame.Color(250,250,0, 250) for hex in HexList: if HexMath.checkInGrid(hex[0],hex[1],self.mMap.x,self.mMap.y): radius = self.mMap.radius height = radius * math.sqrt(3) side = radius * 3/2 width = radius * 2 cornerX = [height/2,height,height,height/2,0,0] cornerY = [0,radius/2,side,width,side,radius/2] if hex[1] % 2: #ungrade PixelX = (hex[0]+1)* height -height/2 else: #grade PixelX = hex[0] * height PixelY = hex[1] * side pointlist = [] i = 0 while i <6: pointlist.append((cornerX[i] + PixelX,cornerY[i] + PixelY)) i += 1 pygame.draw.aalines(self.highlight, color,False, pointlist,1)
def HighlightHex(self,ArrayCord): #rebuild this, hex cords are now stored in tile #highlight the selected hex #clear old highlight self.highlight.fill((0,0,0,0)) color = pygame.Color(0,0,250, 250) #check if cord is in grid if HexMath.checkInGrid(ArrayCord[0],ArrayCord[1],self.mMap.x,self.mMap.y): radius = self.mMap.radius height = radius * math.sqrt(3) side = radius * 3/2 width = radius * 2 cornerX = [height/2,height,height,height/2,0,0] cornerY = [0,radius/2,side,width,side,radius/2] if ArrayCord[1] % 2: #ungrade PixelX = (ArrayCord[0]+1)* height -height/2 else: #grade PixelX = ArrayCord[0] * height PixelY = ArrayCord[1] * side pointlist = [] i = 0 while i <6: pointlist.append((cornerX[i] + PixelX,cornerY[i] + PixelY)) i += 1 pygame.draw.aalines(self.highlight, color,False, pointlist,1)
def getTilesbyDistance(self,Pos,maxDistance,minDistance = 0): # returns a list of tiles rtrList =[] for row in self.tiles: for tile in row: if HexMath.getDistance(Pos[0],Pos[1],tile.x,tile.y) <= maxDistance and HexMath.getDistance(Pos[0],Pos[1],tile.x,tile.y) >= minDistance: rtrList.append((tile.x,tile.y)) return rtrList
def getFov(self,Pos,Range): #rather slow lot of overhead,needs to work with 120 tiles List = self.getTilesbyDistance(Pos,Range,minDistance=1) for element in List: rtrOpacity = 0 intersected = self.intersectingline(self.getTile(Pos),self.getTile(element)) for tile in intersected: if tile[0] == 1: #line did just intercept one tile so add seeing difficulty rtrOpacity += self.getTile(tile[1]).opacity else: #two hexes intercepted, calculate mean difficulty weighted by distance start = self.getTile(Pos).center end = self.getTile(element).center point = self.getTile(tile[1]).center d1 = HexMath.distancefromLine(start[0],start[1],end[0],end[1], point[0], point[1]) point = self.getTile(tile[2]).center d2 = HexMath.distancefromLine(start[0],start[1],end[0],end[1], point[0], point[1]) d1 = round((d1/float((d1+d2))),2) #percentage distance for d1 d2 = round(1.0 - d1,2) # precentage distance for d2 #weight view difficulty opacity = round((self.getTile(tile[1]).opacity * d1 + self.getTile(tile[2]).opacity * d2),2) rtrOpacity += opacity
def __init__(self,x,y,typ,map): self.map = map #array position self.x = x self.y = y #hex position hPos = HexMath.convertArrayToHex(self.x,self.y) self.hX = hPos[0] self.hY = hPos[1] #typ,for terrain self.typ = typ self.opacity = self.getOpacity() #calculate dimensions #calculate points self.center = self.setCenter() self.pointlist = self.setPoints() self.getCenterInt= (int(self.center[0]),int(self.center[1])) #test for caching intersection self.IntersectionCache = []
def getTilebyHex(self,Pos):#get a Tile using hex coords arrayPos = HexMath.convertHexToArray(Pos[0], Pos[1]) return self.getTile((arrayPos[0],arrayPos[1]))
def getCachedLineIntersections(self,startPos,endPos): ReturnList = [] #move on to right for offset startPos = (startPos[0]+1,startPos[1]) endPos = (endPos[0]+1,endPos[1]) #convert to hex coord for easier calculation startPos = HexMath.convertArrayToHex(startPos[0],startPos[1]) endPos = HexMath.convertArrayToHex(endPos[0],endPos[1]) #get direction of line #since the hex tile from where the cached lines are casted is considerd to be the top left hex #there are some cases that need special threadment direction = self.getDirecetion(startPos, endPos) dx = 0 dy = 0 #if direction == 2,swap start and endPos if direction == 2 or direction ==4 or direction ==6: startPos,endPos = endPos,startPos if direction == 3 or direction == 4: dx = endPos[0] - startPos[0] dy = endPos[1] - startPos[1] endPos = (startPos[0] + dy,startPos[1] +dx)#swap dx and dy if direction == 5 or direction == 6: dx = endPos[0] - startPos[0] endPos = (startPos[0],startPos[1]+dx) #get end pos adjusted for offset (startPos) mEndPos = (endPos[0]-startPos[0],endPos[1]-startPos[1]) #convert to array mEndPosarray = HexMath.convertHexToArray(mEndPos[0], mEndPos[1]) #get path Path = self.nodes[mEndPosarray[1]][mEndPosarray[0]+1]#+1 for offset #adjust for offset for i,Node in enumerate(Path): my = dy * i if Node[0] == 1: mNode = HexMath.convertArrayToHex(Node[1][0],Node[1][1]) mNode = (mNode[0]+ (startPos[0]-1),mNode[1] + (startPos[1] -1)) if direction ==5 or direction ==6: #cant happen with two nodes mNode = (mNode[0]+i,mNode[1]-i) mNode = HexMath.convertHexToArray(mNode[0],mNode[1]) mNode = (mNode[0]-1,mNode[1]-my) ReturnList.append((1,mNode)) if Node[0] == 2: mNode = HexMath.convertArrayToHex(Node[1][0],Node[1][1]) mNode = (mNode[0]+ (-1),mNode[1]+ (startPos[1]-1)) mNode = HexMath.convertHexToArray(mNode[0],mNode[1]) mNode = (mNode[0]-1,mNode[1]-my) #second mNode2 = HexMath.convertArrayToHex(Node[2][0],Node[2][1]) mNode2 = (mNode2[0]+ (startPos[0]-1),mNode2[1]+ (startPos[1] -1)) mNode2 = HexMath.convertHexToArray(mNode2[0],mNode2[1]) mNode2 = (mNode2[0]-1,mNode2[1]-my) ReturnList.append((2,mNode,mNode2)) if direction == 2 or direction == 4 or direction ==6: ReturnList.reverse() print ReturnList