Added better solution for road modification
This commit is contained in:
parent
0fdb9ce843
commit
e9d7f25bbe
143
MapGenerator.py
143
MapGenerator.py
@ -48,104 +48,63 @@ def GenerateMap():
|
||||
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
|
||||
THIS MIGHT NOT WORK AS INTENDED"""
|
||||
|
||||
"""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 = []
|
||||
"""AH SHIT HERE WE GO AGAIN"""
|
||||
#create list of path coords that can become new intersections by removing intersections and 8 adjacent tiles from roads
|
||||
intersections_area = []
|
||||
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])
|
||||
intersection_area = []
|
||||
for i in range (-1,2,1):
|
||||
for j in range (-1,2,1):
|
||||
intersection_area.append([x+i,y+j])
|
||||
intersections_area.extend(intersection_area)
|
||||
|
||||
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))
|
||||
possible_roads_to_modify = [i for i in roads if i not in intersections_area]
|
||||
roads_to_modify = []
|
||||
for i in range(1,len(possible_roads_to_modify)//3):
|
||||
choice = random.choice(possible_roads_to_modify)
|
||||
possible_roads_to_modify.remove(choice)
|
||||
roads_to_modify.append(choice)
|
||||
print(roads_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)
|
||||
"""CREATION TIME"""
|
||||
#perform modification based on road
|
||||
for r in roads_to_modify:
|
||||
#select possible directions for modification
|
||||
x, y = r
|
||||
direction = random.choice([1,-1])
|
||||
print(x,y)
|
||||
if([x+1, y] in roads or [x-1, y] in roads):
|
||||
#modify y, as there is road on x coordinates
|
||||
print("y: up/down")
|
||||
route = []
|
||||
current_tile = [x,y+direction]
|
||||
while(True):
|
||||
if(current_tile[1]<0 or current_tile[1]>=height or current_tile in roads):
|
||||
break
|
||||
else:
|
||||
intersections[index][1] = joined_routes
|
||||
route.append(current_tile)
|
||||
current_tile = [current_tile[0],current_tile[1]+direction]
|
||||
else:
|
||||
#modify x, as there is road on y coordinates
|
||||
print("x: left/right")
|
||||
route = []
|
||||
current_tile = [x+direction,y]
|
||||
while(True):
|
||||
if(current_tile[0]<0 or current_tile[0]>=width or current_tile in roads):
|
||||
break
|
||||
else:
|
||||
route.append(current_tile)
|
||||
current_tile = [current_tile[0]+direction,current_tile[1]]
|
||||
|
||||
#choose if the route should be complete or not
|
||||
if(len(route)>1):
|
||||
if(random.randint(1,100)<=40): #40% chance for route not to be full length
|
||||
route_len = random.randint(1,len(route))
|
||||
route = route[1:route_len]
|
||||
|
||||
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]"""
|
||||
#add new route to roads
|
||||
roads.extend(route)
|
||||
|
||||
#insert roads into the grid
|
||||
for coord in roads:
|
||||
|
Loading…
Reference in New Issue
Block a user