def generate_dungeon(w, h, difficulty=1): level = Level(w, h) new_map = [[0 for _ in xrange(h)] for _ in xrange(w)] # initialize new_map to noise (0.43% filled) for x,y in range2d(w, h): if random.random() < 0.43: new_map[x][y] = 1 # apply cellular automation for i in xrange(2): temp_map = [[0 for _ in xrange(h)] for _ in xrange(w)] for x,y in range2d(w, h): wall_count = 0 for i,j in box2d(x-1, y-1, 3, 3): if 0 <= i < w and 0 <= j < h: wall_count += new_map[i][j] else: # sides = instawall wall_count += 3 if wall_count >= 5: temp_map[x][y] = 1 new_map = temp_map # apply changes to actual map for x,y in range2d(w, h): tile = level.tiles[x][y] if new_map[x][y] == 1: tile.img = spr.ROCK tile.blocking = True tile.transparent = False else: tile.img = spr.MUD_FLOOR tile.blocking = False tile.transparent = True # spawn treasures and creatures mr = level.get_main_region() treasures = random.sample(mr, difficulty) for loc in treasures: level.add_item(loc, Item(spr.GOLD_NUGGET, name="gold nugget", value=50+difficulty*25)) mobs = random.sample(mr, difficulty) for loc in mobs: c = Creature(level, *loc, hp=difficulty*10, maxhp=difficulty*10, name="malicious slime", gold=10+difficulty*5) c.min_atk = difficulty c.max_atk = difficulty*2 level.creatures.append(c) return level
# cheat items # change to true for cheating if False: town.add_item((11,11), Item(spr.WATER, name="lapis lazuli", value=90019001)) town.add_item((13,13), Item(spr.TRIFORCE, name="triforce", value=0, luminosity=25, equippable=True, attr={ 'attack_damage': (9000,9001), 'attack_haste': 0.4, 'movement_haste': 0.4, 'health': 90001, }), ) # For each shop, generate it, a position for it on the town, then put it there for loc in random.sample(town.get_main_region(), 16): x,y = loc shop_type = random.randint(0, 2) if shop_type == 0: # General store shop_items = default_items.general shop_img = spr.SHOP shop_name = "general store" elif shop_type == 1: # Weapon shop shop_items = default_items.weapons shop_img = spr.SHOP_WEAPONS shop_name = "weapon shop"