def spawnItems(self, items): for i in range(len(items)): spawned = False while not spawned: x = self.map.shroom.x + cfg.randint(12, -12) y = self.map.shroom.y + cfg.randint(12, -12) if not x and not y: continue if self.map.addItem(items[i], x, y): self.mapElement.setDirty() spawned = True
def spawnShroom(self): maxX = config.world['mapWidth'] / 6 minX = config.world['mapWidth'] / 2 - maxX / 2 maxY = config.world['mapHeight'] / 6 minY = config.world['mapHeight'] / 2 - maxY / 2 x = minX + config.randint(maxX) y = minY + config.randint(maxY) if self.map.getCell(x, y).type == 'grass': shroom = Shroom('Shroom', chars.shroom, Colors.white, cfg.shroom) shroom.spawn(self.map, x, y, cfg.shroom['hp']) self.map.shroom = shroom self.focusX, self.focusY = self.map.shroom.x, self.map.shroom.y else: self.spawnShroom()
def spawnEnemies(self, waveEnemies): side = cfg.randint(3) passable = lambda x1, y1, x2, y2, blech: int( self.map.getCell(x2, y2).passable) enemies = [] for e in range(len(waveEnemies)): attempts = 0 while attempts < 10: enemy = waveEnemies[e] (x, y) = self.findSuitableSpawnPoint(side) path = libtcod.path_new_using_function(self.map.width, self.map.height, passable) libtcod.path_compute(path, x, y, self.map.shroom.x, self.map.shroom.y) s = libtcod.path_size(path) if s: enemy.spawn(self.map, x, y, enemy.hp) enemies.append(enemy) self.messageList.message("%s spawned" % enemy.name) libtcod.path_delete(path) break else: attempts += 1 self.waves[0].enemies = enemies
def generateWorld(self): while True: w = config.world['mapWidth'] h = config.world['mapHeight'] hm = libtcod.heightmap_new(w, h) rand = config.rand hills = 4096 hillHeight = 15 hillRad = 50 libtcod.heightmap_clear(hm) for i in range(hills): height = config.randint(hillHeight) rad = config.randint(hillRad) hillX1 = config.randint(config.world['mapWidth']) hillY1 = config.randint(config.world['mapHeight']) hillX2 = config.randint(config.world['mapWidth']) hillY2 = config.randint(config.world['mapHeight']) if config.randint(10) < 3: libtcod.heightmap_dig_hill(hm, hillX1, hillY1, height, rad) libtcod.heightmap_dig_hill(hm, hillX2, hillY2, height, rad) else: libtcod.heightmap_add_hill(hm, hillX1, hillY1, height, rad) libtcod.heightmap_add_hill(hm, hillX2, hillY2, height, rad) libtcod.heightmap_rain_erosion(hm, 10000, 0.3, 0.2) libtcod.heightmap_normalize(hm, 0.0, 1024.0) thresholds = [{ 'type': 'water', 'range': 0.1 }, { 'type': 'grass', 'range': 0.666 }, { 'type': 'mountain', 'range': 0.8 }, { 'type': 'grass', 'range': 1.0 }] self.map = Map.FromHeightmap(hm, thresholds) libtcod.heightmap_delete(hm) self.generateTrees() self.spawnShroom() if self.validMap(): self.setupMapView() self.removeHandler('gen') return True else: print "invalid map. Retrying"
def findSuitableSpawnPoint(self, side): while True: if side == 0: y = 0 x = cfg.randint(self.map.width-1) elif side == 1: x = self.map.width - 1 y = cfg.randint(self.map.height-1) elif side == 2: y = self.map.height - 1 x = cfg.randint(self.map.width-1) else: x = 0 y = cfg.randint(self.map.height-1) c = self.map.getCell(x, y) if not c.passable: continue if c.entity: continue return x, y
def findSuitableSpawnPoint(self, side): while True: if side == 0: y = 0 x = cfg.randint(self.map.width - 1) elif side == 1: x = self.map.width - 1 y = cfg.randint(self.map.height - 1) elif side == 2: y = self.map.height - 1 x = cfg.randint(self.map.width - 1) else: x = 0 y = cfg.randint(self.map.height - 1) c = self.map.getCell(x, y) if not c.passable: continue if c.entity: continue return x, y
def entityAttack(self, src, target): if target.isDead: return dmg = (src.damage * 0.5) + (0.5 * cfg.randint(src.damage)) msg = "%s hit %s for [%d] damage" if target.takeDamage(dmg): msg = "%s killed %s! [%d] damage" target.die() if isinstance(target, Enemy): self.purgeEnemies() self.messageList.message(msg % (src.name, target.name, dmg))
def generateTrees(self): caTreeDensity = 0.666 caNeighboursSpawn = 7 caNeighboursStarve = 4 caIterations = 6 w = config.world['mapWidth'] h = config.world['mapHeight'] treeCount = int(w * h * caTreeDensity) treeMap = [False for c in range(w + h * w)] while treeCount > 0: x = config.randint(w - 1) y = random.randrange(h - 1) try: tree = treeMap[x + y * w] except IndexError: continue if not tree: treeCount -= 1 treeMap[x + y * w] = True for i in range(caIterations): neighbours = [None for _i in range(w + h * w)] for y in range(h): for x in range(w): neighbours[x + y * w] = self.countTreeNeighbours( x, y, treeMap) for y in range(h): for x in range(w): hasTree = treeMap[x + y * w] n = neighbours[x + y * w] if not hasTree: if n >= caNeighboursSpawn: treeMap[x + y * w] = True else: if n <= caNeighboursStarve: treeMap[x + y * w] = False self.setTrees(treeMap)
def spawnEnemies(self, waveEnemies): side = cfg.randint(3) passable = lambda x1, y1, x2, y2, blech: int(self.map.getCell(x2, y2).passable) enemies = [] for e in range(len(waveEnemies)): attempts = 0 while attempts < 10: enemy = waveEnemies[e] (x, y) = self.findSuitableSpawnPoint(side) path = libtcod.path_new_using_function(self.map.width, self.map.height, passable) libtcod.path_compute(path, x, y, self.map.shroom.x, self.map.shroom.y) s = libtcod.path_size(path) if s: enemy.spawn(self.map, x, y, enemy.hp) enemies.append(enemy) self.messageList.message("%s spawned" % enemy.name) libtcod.path_delete(path) break else: attempts += 1 self.waves[0].enemies = enemies