Example #1
0
class ChunkGen:
    def __init__(self, seed, inChunkPos):
        self.chunkPos = inChunkPos
        random.seed(seed)
        self.noiseGen = Simplex(seed)
        self.chunkSize = 16

        self.mats = {}
        self.mats[-10] = terrain.Water.id
        self.mats[-9] = terrain.Water.id
        self.mats[-8] = terrain.Water.id
        self.mats[-7] = terrain.Water.id
        self.mats[-6] = terrain.Water.id
        self.mats[-5] = terrain.Water.id
        self.mats[-4] = terrain.Water.id
        self.mats[-3] = terrain.Water.id
        self.mats[-2] = terrain.Water.id
        self.mats[-1] = terrain.Water.id
        self.mats[0] = terrain.Water.id
        self.mats[1] = terrain.Sand.id
        self.mats[2] = terrain.Grass.id
        self.mats[3] = terrain.Grass.id
        self.mats[4] = terrain.Brush.id
        self.mats[5] = terrain.Foothills.id
        self.mats[6] = terrain.Mountain.id
        self.mats[7] = terrain.Mountain.id
        self.mats[8] = terrain.Peak.id
        self.mats[9] = terrain.Peak.id
        self.mats[10] = terrain.Peak.id

        # Call this when you want a new chunk

    def generate(self):

        # create blank chunk
        newChunk = [tile.Tile() for i in range(self.chunkSize ** 2)]

        # fill in the basic terrain
        newChunk = self.genTerrain(newChunk)

        # add decorations, like trees
        newChunk = self.decorate(newChunk)

        return newChunk

    def genTerrain(self, newChunk):

        octaves = 6
        persistence = 0.75
        scale = 0.004

        for row in range(self.chunkSize):
            for col in range(self.chunkSize):

                xPos = self.chunkPos[0] * self.chunkSize + col
                yPos = self.chunkPos[1] * self.chunkSize + row

                elevation = self.noiseGen.octave_noise_2d(octaves, persistence, scale, xPos, yPos)

                elevation *= 15

                elevation = int(elevation)

                ground = self.getGroundMat(elevation)

                newChunk[row * self.chunkSize + col].setGround(ground)

        return newChunk

        # takes an integer argument (0-9)
        # returns a tile

    def getGroundMat(self, elevation):

        if elevation < -10:
            elevation = -10

        if elevation > 9:
            elevation = 9

        newMat = self.mats[elevation]

        return newMat

    def decorate(self, newChunk):

        # add some features
        for i in range(self.chunkSize ** 2):

            if newChunk[i].getGround() in [terrain.Grass, terrain.Brush]:

                # add trees and stuff
                if random.randint(0, 10) == 0:

                    if random.randint(0, 4) == 0:
                        newChunk[i].build(terrain.DeadTree.id)
                    else:
                        newChunk[i].build(terrain.Tree.id)

            if newChunk[i].getGround() == terrain.Foothills:

                if random.randint(0, 10) == 0:
                    newChunk[i].build(terrain.Rock.id)

        return newChunk
Example #2
0
class MapCache(object):
	
	def __init__(self, chunkManager, seed):
		
		self.chunkManager = chunkManager
		self.mapDir = 'save/map/'
		self.noise = Simplex(seed)
		
		if not os.path.isdir(self.mapDir):
			os.mkdir(self.mapDir)
		
		self.basePath = os.path.dirname(__file__)
		self.cache = {}
		
	def load(self, name):
		path = self.mapDir + name + '.png'
		
		if not os.path.isfile(path):
			return
			
		f = open(path, 'r')
		self.cache[name] = pygame.image.load(path).convert_alpha()
		return self.cache[name]
		
	def get(self, x, y):
		name = str(x) + '_' + str(y)
		
		if(name in self.cache):
			return self.cache[name]
		
		if(self.chunkManager.isLoaded(x, y)):
			chunk = chunkManager.getChunk(x << 4, y << 4)
			self.genMap(chunk)
		
		return self.load(name)
		
	def genMap(self, chunk):
		img = pygame.Surface((16, 16))
		
		for y in range(16):
			for x in range(16):
				t = chunk.getTile(x, y)
				groundColor = t.getGround().color()
				shadow = self.getShadow(chunk.pos[0] * 16 + x, chunk.pos[1] * 16 + y)
				color = self.blend(shadow, groundColor)
				
				img.set_at((x, y), color)
				
				
		img = pygame.transform.scale2x(img)
		x, y = chunk.pos
		name = str(x) + '_' + str(y)
		path = self.mapDir + name + '.png'
		pygame.image.save(img, path)
		self.cache[name] = img
		
	def getShadow(self, x, y):
		octaves = 6
		persistence = 0.75
		scale = 0.004
		c1 = self.noise.octave_noise_2d(octaves, persistence, scale, x, y)
		if c1 < 0.05:
			return 0.5
		c1 += 0.4
		c2 = self.noise.octave_noise_2d(octaves, persistence, scale, x - 2, y - 2)
		c1 = (c1 - c2)
		c1 *= 1.2
		
		if c1 > 1:
			c1 = 1
		if c1 < 0:
			c1 = 0
		
		return c1
		
	def blend(self, percent, map):
		
		r = int(map[0] * percent)
		g = int(map[1] * percent)
		b = int(map[2] * percent)

		return r, g, b