From 6bd243ccffe7e577144df14586039da4a51916cd Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 2 Mar 2016 23:24:58 +0200 Subject: [PATCH] Hack in underlay generation --- koperkapel/generators/maps.py | 59 +++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/koperkapel/generators/maps.py b/koperkapel/generators/maps.py index dc6eb33..dc36956 100644 --- a/koperkapel/generators/maps.py +++ b/koperkapel/generators/maps.py @@ -9,11 +9,21 @@ i = random.randint(0,100) ATTRIBUTE_MAP = { - '#': {'base': 'cwall', - 'behaviour': [], + '#': {'floor': {'base': 'cwall', + 'behaviour': [], + }, }, - ' ': {'base': 'floor', - 'behaviour': ['walk', 'fly'], + ' ': {'floor': {'base': 'floor', + 'behaviour': ['walk', 'fly'], + }, + }, + 'o': {'tunnels': {'base': 'cwall', + 'behaviour': [], + } + }, + '-': {'tunnels': {'base': 'floor', + 'behaviour': ['walk',], + }, }, } @@ -54,6 +64,7 @@ class LevelGenerator: print('Regions: %s' % str(regions_selected)) for region in regions_selected: self.generate_room(region) + self.generate_underlayer() def generate_regions(self): """ Generate a random level region map @@ -91,6 +102,29 @@ class LevelGenerator: self.regions[h][w] = self.region self.region += 1 + def generate_underlayer(self): + """Generate a small mess of tunnels to have something.""" + width = len(self.map[0]) + height = len(self.map) + row = ['o' for x in range(width)] + self.underlayer = [row[:] for x in range(height)] + # we create a set of biased random walks to create the tunnel network + for walk in range(random.randint(3, 6)): + x = width // 2 + random.randint(-8, 8) + y = height // 2 + random.randint(-8, 8) + dir_x = random.randint(-1, 1) + dir_y = random.randint(-1, 1) + max_steps = random.randint(40, width * height // 4) + for step in range(20, max_steps): + if 0 < x < width - 1: + if 0 < y < height - 1: + self.underlayer[y][x] = '-' + if random.random() > 0.7: + dir_x = random.randint(-1, 1) + dir_y = random.randint(-1, 1) + x += dir_x + y += dir_y + def generate_room(self, region_selected): """ """ @@ -135,6 +169,11 @@ class LevelGenerator: print(''.join(l)) file.write(''.join(l)) file.write('\n') + print('') + for l in self.underlayer: + print(''.join(l)) + file.write(''.join(l)) + file.write('\n') file.close() self._to_json() for l in self.regions: @@ -144,14 +183,16 @@ class LevelGenerator: level = {} level['tileset'] = 'dungeon' level['tiles'] = [] - for l in self.map: + for l, lu in zip(self.map, self.underlayer): row = [] - for t in l: - row.append(ATTRIBUTE_MAP[t]) + for t1, t2 in zip(l, lu): + tile = ATTRIBUTE_MAP[t1].copy() + tile.update(ATTRIBUTE_MAP[t2]) + row.append(tile) level['tiles'].append(row) - name = os.path.join(os.path.dirname(__file__), '..', 'levels', 'map.json') - # FIXME: Do a lot better here + # FIXME: Do a lot better here # Crude hack so the level is written into the levels folder + name = os.path.join(os.path.dirname(__file__), '..', 'levels', 'map.json') f = open(name, 'w') json.dump(level, f) f.close() -- 2.34.1