2020-05-03 19:20:48 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
### Tangled Mind
|
|
|
|
### Author: Arthur 'Grizzly' Grisel-Davy
|
|
|
|
|
|
|
|
from glob import glob
|
|
|
|
import random
|
2020-05-04 04:08:10 +02:00
|
|
|
import pygame
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
def fetch_rooms(path):
|
|
|
|
"""Fetch all the basic tiles asset from the path.
|
|
|
|
Return a dictionary
|
|
|
|
"""
|
|
|
|
|
2020-05-05 01:29:22 +02:00
|
|
|
common_wildcard = '[A-Z]*-*.png'
|
|
|
|
unique_wildcard = '?-*.png'
|
|
|
|
common_filenames = glob(path+common_wildcard)
|
|
|
|
unique_filenames = glob(path+unique_wildcard)
|
|
|
|
#print(f"Found {len(common_filenames)} common rooms and {len(unique_filenames)} uniques rooms.")
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
assembly = {'T':[],
|
|
|
|
'R':[],
|
|
|
|
'D':[],
|
|
|
|
'L':[],}
|
|
|
|
|
2020-05-05 01:29:22 +02:00
|
|
|
for filename in common_filenames:
|
2020-05-03 19:20:48 +02:00
|
|
|
label = filename.split("/")[-1].split('-')[0]
|
|
|
|
if 'T' in label:
|
|
|
|
assembly['D'].append((label,filename))
|
|
|
|
if 'R' in label:
|
|
|
|
assembly['L'].append((label,filename))
|
|
|
|
if 'D' in label:
|
|
|
|
assembly['T'].append((label,filename))
|
|
|
|
if 'L' in label:
|
|
|
|
assembly['R'].append((label,filename))
|
|
|
|
|
2020-05-04 04:27:44 +02:00
|
|
|
background = glob(path+'*back*')[0]
|
|
|
|
|
2020-05-05 01:29:22 +02:00
|
|
|
return unique_filenames,assembly,background
|
2020-05-03 19:20:48 +02:00
|
|
|
|
2020-05-05 00:34:26 +02:00
|
|
|
def check_map(carte):
|
|
|
|
"""Function to check if a map is valid i.e. if no two rooms are at the same location
|
|
|
|
"""
|
|
|
|
positions = []
|
|
|
|
for room in carte:
|
|
|
|
if room[2] in positions:
|
|
|
|
return(False)
|
|
|
|
else:
|
|
|
|
positions.append(room[2])
|
|
|
|
return(True)
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
def map_generator(n):
|
|
|
|
"""Map generator generate a map with a main path of n rooms
|
|
|
|
"""
|
|
|
|
|
|
|
|
room_side = 1000
|
|
|
|
|
2020-05-05 01:29:22 +02:00
|
|
|
unique_filenames,assembly,back_path = fetch_rooms('./maps/rooms/')
|
2020-05-03 19:20:48 +02:00
|
|
|
#assembly = {'T/R/D/L':[(label1,path1),(label2,path2),...]}
|
|
|
|
|
2020-05-05 01:29:22 +02:00
|
|
|
|
|
|
|
unique_rooms = {filename.split("/")[-1][0].upper():pygame.image.load(filename).convert_alpha() for filename in unique_filenames}
|
|
|
|
|
|
|
|
current_label= random.choice(list(unique_rooms.keys()))
|
|
|
|
start_room = unique_rooms[current_label]
|
2020-05-03 19:20:48 +02:00
|
|
|
|
2020-05-04 04:08:10 +02:00
|
|
|
# Place the first room in the list
|
2020-05-05 00:34:26 +02:00
|
|
|
positions = []
|
2020-05-03 19:20:48 +02:00
|
|
|
position = [0,0]
|
2020-05-05 01:29:22 +02:00
|
|
|
mask = pygame.mask.from_surface(start_room)
|
|
|
|
carte = [(start_room,mask,position)]
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
|
2020-05-05 00:34:26 +02:00
|
|
|
counter = 0
|
|
|
|
while counter < n:
|
|
|
|
|
2020-05-03 19:20:48 +02:00
|
|
|
# select the next direction, can't be the direction of arrival
|
2020-05-05 01:29:22 +02:00
|
|
|
if len(current_label) > 1:
|
|
|
|
dir_next = random.choice(current_label.replace(dir_from,''))
|
|
|
|
else:
|
|
|
|
dir_next = current_label
|
|
|
|
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
# Select the next room
|
|
|
|
next_label,next_room = random.choice(assembly[dir_next])
|
2020-05-04 04:08:10 +02:00
|
|
|
asset = pygame.image.load(next_room).convert_alpha()
|
|
|
|
mask = pygame.mask.from_surface(asset)
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
# Compute the position of the next tile
|
|
|
|
if dir_next == 'T':
|
|
|
|
next_position = [position[0],position[1]-room_side]
|
|
|
|
dir_from = 'D'
|
|
|
|
|
|
|
|
if dir_next == 'R':
|
|
|
|
next_position = [position[0]+room_side,position[1]]
|
|
|
|
dir_from = 'L'
|
|
|
|
|
|
|
|
if dir_next == 'D':
|
|
|
|
next_position = [position[0],position[1]+room_side]
|
|
|
|
dir_from = 'T'
|
|
|
|
|
|
|
|
if dir_next == 'L':
|
|
|
|
next_position = [position[0]-room_side,position[1]]
|
|
|
|
dir_from = 'R'
|
|
|
|
|
2020-05-05 00:34:26 +02:00
|
|
|
# Check if we are not overwriting an existing room and store the current one.
|
|
|
|
if next_position not in positions:
|
|
|
|
counter += 1
|
|
|
|
# Build the map
|
|
|
|
carte.append((asset,mask,next_position))
|
|
|
|
positions.append(position)
|
2020-05-03 19:20:48 +02:00
|
|
|
|
2020-05-05 00:34:26 +02:00
|
|
|
# Update the variables for next turn
|
|
|
|
current_label = next_label
|
|
|
|
position = next_position
|
|
|
|
positions.append(position)
|
2020-05-03 19:20:48 +02:00
|
|
|
|
2020-05-04 04:27:44 +02:00
|
|
|
background = pygame.image.load(back_path).convert()
|
|
|
|
|
2020-05-05 00:34:26 +02:00
|
|
|
if not check_map(carte):
|
2020-05-05 01:29:22 +02:00
|
|
|
raise ValueError("Invalid Map.")
|
2020-05-05 00:34:26 +02:00
|
|
|
else:
|
|
|
|
print("Map checked and valid!")
|
|
|
|
|
2020-05-05 01:29:22 +02:00
|
|
|
for room in carte:
|
|
|
|
print(room)
|
|
|
|
|
|
|
|
return(carte,(int(room_side/2),int(room_side/2)),background)
|
2020-05-03 19:20:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|