SZI2019SmieciarzWmi/MapGenerator.py
Magdalena Wilczyńska 36ff47fe3d Added route generating
2019-05-19 17:11:08 +02:00

177 lines
5.7 KiB
Python

import random, datetime
#generate random empty map
width = random.randint(4,7) #up to 15
height = random.randint(4,7) #up to 10
grid = []
row = []
for i in range(width):
row.append('E')
for i in range(height):
grid.append(row.copy())
#define number of roads for each axis
x_roads_count = random.randint(2, max(width//2.3,2))
y_roads_count = random.randint(2, max(height//2.3,2))
#select coords of roads for x
x_roads_coordinates = [] #output coords
possible_coordiantes = [i for i in range(width)] #coords to choose from
for i in range(x_roads_count):
coordinate = random.choice(possible_coordiantes)
road_area = [coordinate-1, coordinate, coordinate+1]
possible_coordiantes = [i for i in possible_coordiantes if i not in road_area] #removes road and surrounding coords (total 3 coords) from possible coords
x_roads_coordinates.append(coordinate)
print(x_roads_coordinates)
#select coords of roads for y
y_roads_coordinates = [] #output coords
possible_coordiantes = [i for i in range(height)] #coords to choose from
for i in range(y_roads_count):
coordinate = random.choice(possible_coordiantes)
road_area = [coordinate-1, coordinate, coordinate+1]
possible_coordiantes = [i for i in possible_coordiantes if i not in road_area] #removes road and surrounding coords (total 3 coords) from possible coords
y_roads_coordinates.append(coordinate)
print(y_roads_coordinates)
"""combine coords into list of intersection coordinates with number of roads they are joining
(if less than 2, then removed as it is no longer an intersection)"""
intersections = []
roads = []
for x in x_roads_coordinates:
for y in y_roads_coordinates:
#check how many roads are joined
joining_roads_count = 4
if(x == 0 or x == width-1):
joining_roads_count -= 1
if(y == 0 or y == height-1):
joining_roads_count -= 1
if(joining_roads_count > 2):
intersections.append([[x,y],joining_roads_count])
#create list of road coordinates
roads = []
for x in x_roads_coordinates:
for y in range(height):
roads.append([x,y])
for y in y_roads_coordinates:
for x in range(width):
roads.append([x,y])
"""AND HERE WE ARE, MODIFYING ALL THE ROADS
select half the intersections for modification
or at least remove half from the initial list
i mean we're operating on copy"""
intersections_to_modify = [j[0].copy() for j in [i for i in intersections]]
intersections_to_leave_count = (len(intersections)//2) + 1
for i in range(intersections_to_leave_count):
intersections_to_modify.remove(random.choice(intersections_to_modify))
#remove joined roads number, we need coords
#for i in intersections_to_modify:
# i.pop()
print("Intersections: "+str(intersections))
print("To modify: "+str(intersections_to_modify))
#modify each selected intersection
for intersection in intersections_to_modify:
#search for neighbours
neighbours = []
index = x_roads_coordinates.index(intersection[0])
if(index != 0):
neighbour = [x_roads_coordinates[index-1], intersection[1]]
neighbours.append(neighbour)
if(index != len(x_roads_coordinates)-1):
neighbour = [x_roads_coordinates[index+1], intersection[1]]
neighbours.append(neighbour)
index = y_roads_coordinates.index(intersection[1])
if(index != 0):
neighbour = [intersection[0], y_roads_coordinates[index-1]]
neighbours.append(neighbour)
if(index != len(y_roads_coordinates)-1):
neighbour = [intersection[0], y_roads_coordinates[index+1]]
neighbours.append(neighbour)
#remove neighbour if not an intersection
for n in neighbours:
if (n not in (item for sublist in intersections for item in sublist) ):
#print("FAKE"+str(n))
neighbours.remove(n)
#neighbour to modify
neighbour = random.choice(neighbours)
#get route between intersections
route = []
if(intersection[0] == neighbour[0]):
a = min(intersection[1], neighbour[1])
b = max(intersection[1], neighbour[1])
for y in range(a+1,b):
route.append([intersection[0],y])
else:
a = min(intersection[0], neighbour[0])
b = max(intersection[0], neighbour[0])
for x in range(a+1,b):
route.append([x, intersection[1]])
#decrease joined routes count for intersections
for i in intersections:
if(i[0] == intersection or i[0] == neighbour):
index = intersections.index(i)
joined_routes = intersections[index][1]
joined_routes -= 1
if(joined_routes == 2):
intersections.remove(i)
else:
intersections[index][1] = joined_routes
print("Intersection: "+str(intersection))
print("Neighbour: "+str(neighbour))
print("Route: "+str(route))
print("Intersections after modification: "+str(intersections))
#decide if modified route should disappear or just have some field removed
f = random.randint(0,100)
if(not(f % 2 or f % 5)):
route.remove(random.choice(route))
#remove modified route from roads
roads = [c for c in roads if c not in route]
print("----------------------------")
print(width, height)
print(x_roads_count, y_roads_count)
#insert roads into the grid
for coord in roads:
#print(coord)
grid[coord[1]][coord[0]] = "R"
for g in grid: print(g)
#Select position for GC
GC_position = random.choice(roads)
#Save map to file
name = ".\\Resources\\Maps\\map"+str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))+"_auto.txt"
map_file = open(name, "w+")
map_file.write(str(width)+" "+str(height)+"\n")
map_file.write(str(GC_position[0])+" "+str(GC_position[1])+"\n")
for row in grid:
map_file.write(" ".join(row)+"\n")
map_file.close()
print(name)