def getShortestIntersectionPath(surface, section1, section2): points1 = section1.pathConnectionPoints points2 = section2.pathConnectionPoints if not points1: points1.append(Point(section1.xMid, section1.zMid)) if not points2: points2.append(Point(section2.xMid, section2.zMid)) length = sys.maxint point1 = None point2 = None for p1 in points1: for p2 in points2: l = getDistance(p1.x, p1.z, p2.x, p2.z) if l < length: length = l point1 = p1 point2 = p2 path = getPath(surface, point1.x, point1.z, point2.x, point2.z) for point in path[:3]: section1.exitPoints.append(point) for point in path[-1:-4:-1]: section2.exitPoints.append(point) return path
def reconstructPath(node): path = [] currentNode = node path.append(Point(currentNode.x, currentNode.z)) while currentNode.cameFrom != None: currentNode = currentNode.cameFrom path.append(Point(currentNode.x, currentNode.z)) path.reverse() return path
def getRandomPoint(surface, prop, points): x = random.randint(prop.xStart + 1, prop.xEnd - 2) z = random.randint(prop.zStart + 1, prop.zEnd - 2) p = Point(x, z) while contain(points, p): x = random.randint(prop.xStart + 1, prop.xEnd - 2) z = random.randint(prop.zStart + 1, prop.zEnd - 2) p = Point(x, z) return p
def getStraightPath(x1, z1, x2, z2): horizontalLength = abs(x2 - x1) verticalLength = abs(z2 - z1) straightPath = [] if horizontalLength > verticalLength: xStart = 0 zStart = 0 zEnd = 0 isIncreasing = True if x1 < x2: xStart = x1 zStart = z1 zEnd = z2 else: xStart = x2 zStart = z2 zEnd = z1 if zEnd < zStart: isIncreasing = False for i, x in enumerate(range(xStart, xStart + horizontalLength)): percentage = float(i + 1) / horizontalLength z = 0 if isIncreasing: z = zStart + int(verticalLength * percentage) else: z = zStart - int(verticalLength * percentage) straightPath.append(Point(x, z)) else: zStart = 0 xStart = 0 xEnd = 0 isIncreasing = True if z1 < z2: zStart = z1 xStart = x1 xEnd = x2 else: zStart = z2 xStart = x2 xEnd = x1 if xEnd < xStart: isIncreasing = False for i, z in enumerate(range(zStart, zStart + verticalLength)): percentage = float(i + 1) / verticalLength x = 0 if isIncreasing: x = xStart + int(horizontalLength * percentage) else: x = xStart - int(horizontalLength * percentage) straightPath.append(Point(x, z)) return straightPath
def getShortestIntersectionPathLength(surface, section1, section2): points1 = section1.pathConnectionPoints points2 = section2.pathConnectionPoints if not points1: points1.append(Point(section1.xMid, section1.zMid)) if not points2: points2.append(Point(section2.xMid, section2.zMid)) length = sys.maxint for p1 in points1: for p2 in points2: l = getDistance(p1.x, p1.z, p2.x, p2.z) if l < length: length = l return length
def tooCloseToOtherTowers(surface, sectionMid, towerSections): for towerSection in towerSections: towerSectionMid = Point(towerSection.xMid, towerSection.zMid) distance = getEuclideanDistance(surface, towerSectionMid, sectionMid) if distance < 100: return True return False
def findLayer(surfaceInfo, x, z, layer): queue = deque() queue.append(Point(x, z)) while queue: point = queue.popleft() if addPointToLayer(surfaceInfo, point.x, point.z, layer): addNeighborPointsToQueue(surfaceInfo, point.x, point.z, layer, queue)
def getNeighboursToCheck(point, surface, isChecked): neighboursToCheck = [] x = point.x z = point.z if (x + 1) < surface.xLength and not isChecked[x + 1][z]: isChecked[x + 1][z] = True neighboursToCheck.append(Point(x + 1, z)) if (z + 1) < surface.zLength and not isChecked[x][z + 1]: isChecked[x][z + 1] = True neighboursToCheck.append(Point(x, z + 1)) if (x - 1) >= 0 and not isChecked[x - 1][z]: isChecked[x - 1][z] = True neighboursToCheck.append(Point(x - 1, z)) if (z - 1) >= 0 and not isChecked[x][z - 1]: isChecked[x][z - 1] = True neighboursToCheck.append(Point(x, z - 1)) return neighboursToCheck
def addNeighborPointsToQueue(surfaceInfo, x, z, layer, queue): for xNeighbor in [x - 1, x, x + 1]: for zNeighbor in [z - 1, z, z + 1]: if xNeighbor == x and zNeighbor == z: continue if not surfaceInfo.surfaceMap[xNeighbor][zNeighbor].isComplete and surfaceInfo.surfaceMap[xNeighbor][zNeighbor].isCheckedByLayer != layer: surfaceInfo.surfaceMap[xNeighbor][zNeighbor].isCheckedByLayer = layer queue.append(Point(xNeighbor, zNeighbor))
def buildHouseProperties(level, surface, properties): for p in properties: biomeId = surface.surfaceMap[p.xStart][p.zStart].biomeId biome = getBiomeDict()[biomeId] point = Point(surface.xStart + p.xStart, surface.zStart + p.zStart) buildStructure(level, point, p.height, 'house', 'north', biome, prop=p) buildPathway(level, surface, p.xPathwayStart, p.zPathwayStart, p.xPathwayEnd, p.zPathwayEnd)
def buildRoads(level, surface, roads): streetLightInterval = 8 for road in roads: streetLightCounter = 0 for i, point in enumerate(road): y = surface.surfaceMap[point.x][point.z].height point = Point(point.x + surface.xStart, point.z + surface.zStart) previousPoint = None nextPoint = None if i > 0: previousPoint = Point(road[i - 1].x + surface.xStart, road[i - 1].z + surface.zStart) if i + 1 < len(road): nextPoint = Point(road[i + 1].x + surface.xStart, road[i + 1].z + surface.zStart) pointAngle = angleOfPoints(previousPoint, point, nextPoint) buildPathPoint(level, surface, point, y) if streetLightCounter % streetLightInterval == 0: placeStreetLights(level, surface, point, y, pointAngle) streetLightCounter += 1
def buildTowers(level, surface, towerSections): for section in towerSections: height = surface.surfaceMap[section.xMid][section.zMid].height biomeId = surface.surfaceMap[section.xMid][section.zMid].biomeId biome = getBiomeDict()[biomeId] buildStructure( level, Point(surface.xStart + section.xMid - 5, surface.zStart + section.zMid - 5), height, 'tower', 'north', biome)
def addDoorStep(blockRegister, doorPoint, prop, height): direction = prop.doorDirection doorStepPoint = None if direction == 'NORTH': doorStepPoint = Point(doorPoint.x, doorPoint.z - 1) prop.xPathwayStart = prop.xStart + doorPoint.x prop.zPathwayStart = prop.zStart + doorPoint.z - 2 elif direction == 'EAST': doorStepPoint = Point(doorPoint.x + 1, doorPoint.z) prop.xPathwayStart = prop.xStart + doorPoint.x + 2 prop.zPathwayStart = prop.zStart + doorPoint.z elif direction == 'SOUTH': doorStepPoint = Point(doorPoint.x, doorPoint.z + 1) prop.xPathwayStart = prop.xStart + doorPoint.x prop.zPathwayStart = prop.zStart + doorPoint.z + 2 elif direction == 'WEST': doorStepPoint = Point(doorPoint.x - 1, doorPoint.z) prop.xPathwayStart = prop.xStart + doorPoint.x - 2 prop.zPathwayStart = prop.zStart + doorPoint.z blockRegister.append({'type': 'cobblestone', 'direction': None, 'verticalAllignment': 'bottom', 'id': 5, 'data': 0, 'x': doorStepPoint.x, 'y': height, 'z': doorStepPoint.z})
def perform(level, box, options): if min([box.maxx - box.minx, box.maxz - box.minz]) < 7: return point = Point(box.minx, box.minz) property = Property(box.minx, box.minz, box.maxx, box.maxz, box.miny) property.doorDirection = options["Direction:"].upper() buildStructure(level, point, box.miny, "house", options["Direction:"], options["Biome:"], prop=property)
def placeStreetLights(level, surface, point, height, angle): if angle == 0: placeStreetLight(level, surface, Point(point.x - 2, point.z), height) placeStreetLight(level, surface, Point(point.x + 2, point.z), height) elif angle == 1: placeStreetLight(level, surface, Point( point.x + 1, point.z - 1), height) placeStreetLight(level, surface, Point( point.x - 1, point.z + 1), height) elif angle == 2: placeStreetLight(level, surface, Point(point.x, point.z - 2), height) placeStreetLight(level, surface, Point(point.x, point.z + 2), height) elif angle == 3: placeStreetLight(level, surface, Point( point.x - 1, point.z - 1), height) placeStreetLight(level, surface, Point( point.x + 1, point.z + 1), height)
def calculateSections(surface, allowedSteepness=0, minSize=1): sections = [] isChecked = getMatrix( surface.xLength, surface.zLength, False) #A matrix that keeps track of which points are checked id = 0 for x in range(surface.xLength): for z in range(surface.zLength): if isChecked[x][z]: continue if surface.surfaceMap[x][z].steepness <= allowedSteepness: section = calculateSection(id, Point(x, z), surface, isChecked, allowedSteepness) if section.size >= minSize: registerIdsOnSurface(surface, section) sections.append(section) id += 1 isChecked[x][z] = True return sections
def getTowerSections(surface, averageSurfaceHeight, bigLandSections, mediumLandSections, smallLandSections): maxAmount = (surface.xLength * surface.zLength) / 25000 + 1 sectionHeap = heapifySectionsByAverageHeight(smallLandSections) towerSections = [] for i, element in enumerate(sectionHeap): if i >= maxAmount: break section = element[1] averageSectionHeight = section.averageHeight if averageSectionHeight < averageSurfaceHeight: break sectionMid = Point(section.xMid, section.zMid) if tooCloseToOtherTowers( surface, sectionMid, towerSections) or tooCloseToBiggerHigherSections( surface, sectionMid, averageSectionHeight, bigLandSections, mediumLandSections): continue towerSections.append(section) return towerSections
def getPathsInSection(surface, section): attempts = 5 points = [Point(section.xMid, section.zMid)] pointPool = section.points poolSize = len(pointPool) expectedPoints = poolSize / 2500 - 1 if expectedPoints < 1: section.pathConnectionPoints.extend(points) return [] for _ in range(expectedPoints): greatestLength = 0 nextPoint = None for _ in range(attempts): point = getRandomPoint(pointPool, poolSize, points) length = getLengthToNearestPoint(surface, points, point) if length > greatestLength: greatestLength = length nextPoint = point points.append(nextPoint) section.pathConnectionPoints.extend(points) return getPathBetweenPoints(surface, points)
def buildPathPoint(level, surface, point, height): buildCenterPathTile(level, surface, point, height) for p in [(point.x - 1, point.z), (point.x + 1, point.z), (point.x, point.z - 1), (point.x, point.z + 1)]: buildOuterPathTile(level, surface, Point(p[0], p[1]), height)
def addWalls(point, prop, blockRegister): y = 0 isDoorPlaced = False doorPoint = None # North and South default wall segments segmentRegister = loadSegmentRegister('wall_default') for x in range(2, prop.xLength - 2, 2): for block in segmentRegister: appendAdjustedBlockToRegister(blockRegister, block, x, y, 1) appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 1 - x, y, prop.zLength - 2) if x == prop.xLength - 4: appendAdjustedBlockToRegister(blockRegister, block, x + 1, y, 1) appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 2 - x, y, prop.zLength - 2) segmentRegister = rotateRegister(segmentRegister, directions.index('EAST')) # East and West default wall segments for z in range(2, prop.zLength - 2, 2): for block in segmentRegister: appendAdjustedBlockToRegister(blockRegister, block, 1, y, z) appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 2, y, prop.zLength - 1 - z) if z == prop.zLength - 4: appendAdjustedBlockToRegister(blockRegister, block, 1, y, z + 1) appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 2, y, prop.zLength - 2 - z) segmentRegister = loadSegmentRegister('wall_window') # North and South windows and maybe door for x in range(3, prop.xLength - 3, 2): skipNorth = False skipSouth = False if not isDoorPlaced: if prop.doorDirection == 'NORTH': skipNorth = True if prop.doorDirection == 'SOUTH': skipSouth = True if skipNorth: doorPoint = Point(x, 1) isDoorPlaced = True if skipSouth: doorPoint = Point(prop.xLength - 1 - x, prop.zLength - 2) isDoorPlaced = True for block in segmentRegister: if not skipNorth: appendAdjustedBlockToRegister(blockRegister, block, x, y, 1) if not skipSouth: appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 1 - x, y, prop.zLength - 2) segmentRegister = rotateRegister(segmentRegister, directions.index('EAST')) # East and West windows and maybe door for z in range(3, prop.zLength - 3, 2): skipEast = False skipWest = False if not isDoorPlaced: if prop.doorDirection == 'EAST': skipEast = True if prop.doorDirection == 'WEST': skipWest = True if skipEast: doorPoint = Point(prop.xLength - 2, prop.zLength - 1 - z) isDoorPlaced = True if skipWest: doorPoint = Point(1, z) isDoorPlaced = True for block in segmentRegister: if not skipWest: appendAdjustedBlockToRegister(blockRegister, block, 1, y, z) if not skipEast: appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 2, y, prop.zLength - 1 - z) # Corners segmentRegister = loadSegmentRegister('wall_corner') for block in segmentRegister: appendAdjustedBlockToRegister(blockRegister, block, 1, y, 1) appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 2, y, 1) appendAdjustedBlockToRegister(blockRegister, block, 1, y, prop.zLength - 2) appendAdjustedBlockToRegister(blockRegister, block, prop.xLength - 2, y, prop.zLength - 2) # Door segmentRegister = loadSegmentRegister('wall_door') segmentRegister = rotateRegister(segmentRegister, directions.index(prop.doorDirection)) for block in segmentRegister: appendAdjustedBlockToRegister(blockRegister, block, doorPoint.x, y, doorPoint.z) addDoorStep(blockRegister, doorPoint, prop, y)