204 lines
7.4 KiB
Python
204 lines
7.4 KiB
Python
import random, datetime, itertools
|
|
|
|
def GenerateMap():
|
|
#generate random empty map
|
|
width = random.randint(4,15) #up to 15
|
|
height = random.randint(4,10) #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//3,2))
|
|
y_roads_count = random.randint(2, max(height//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))
|
|
|
|
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))
|
|
|
|
#EXPERIMENTAL
|
|
#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]
|
|
|
|
#insert roads into the grid
|
|
for coord in roads:
|
|
grid[coord[1]][coord[0]] = "R"
|
|
|
|
for g in grid: print(g)
|
|
|
|
"""OBJECTS BE HERE"""
|
|
#Select area that possibly could hold objects
|
|
objects_area = []
|
|
for r in roads:
|
|
objects_area.extend(([r[0]+1,r[1]],[r[0]-1,r[1]],[r[0],r[1]+1],[r[0],r[1]-1]))
|
|
objects_area = [c for c in objects_area if c not in roads] #remove coords that contain roads
|
|
objects_area.sort()
|
|
objects_area = [objects_area[i] for i in range(len(objects_area)) if i == 0 or objects_area[i] != objects_area[i-1]] #remove duplicates
|
|
|
|
houses_area = [i.copy() for i in objects_area]
|
|
for o in objects_area:
|
|
if(o[0] < 0 or o[1] < 0 or o[0] >= width or o[1] >= height):
|
|
houses_area.remove(o) #remove coords outside borders
|
|
|
|
#place dumps
|
|
dumps_to_place = ["B","Y","G"]
|
|
while(len(dumps_to_place) > 0):
|
|
dump_coords = random.choice(houses_area)
|
|
houses_area.remove(dump_coords)
|
|
grid[dump_coords[1]][dump_coords[0]] = dumps_to_place[0]
|
|
dumps_to_place.remove(dumps_to_place[0])
|
|
|
|
|
|
#leave random coordinates
|
|
houses_to_leave_count = len(houses_area)//4
|
|
while(len(houses_area) > houses_to_leave_count):
|
|
houses_area.remove(random.choice(houses_area))
|
|
|
|
|
|
print("================================")
|
|
|
|
#insert houses into the grid
|
|
for coord in houses_area:
|
|
print(coord)
|
|
grid[coord[1]][coord[0]] = "H"
|
|
|
|
#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)
|
|
|
|
return(name) |