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',],
+ },
},
}
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
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):
"""
"""
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:
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()