Added better solution for road modification

This commit is contained in:
Magdalena Wilczyńska 2019-05-21 21:24:57 +02:00
parent 0fdb9ce843
commit e9d7f25bbe

View File

@ -48,104 +48,63 @@ def GenerateMap():
for x in range(width): for x in range(width):
roads.append([x,y]) roads.append([x,y])
"""AND HERE WE ARE, MODIFYING ALL THE ROADS """AH SHIT HERE WE GO AGAIN"""
select half the intersections for modification #create list of path coords that can become new intersections by removing intersections and 8 adjacent tiles from roads
or at least remove half from the initial list intersections_area = []
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 = []
for x in x_roads_coordinates: for x in x_roads_coordinates:
for y in y_roads_coordinates: for y in y_roads_coordinates:
#check how many roads are joined intersection_area = []
joining_roads_count = 4 for i in range (-1,2,1):
if(x == 0 or x == width-1): for j in range (-1,2,1):
joining_roads_count -= 1 intersection_area.append([x+i,y+j])
if(y == 0 or y == height-1): intersections_area.extend(intersection_area)
joining_roads_count -= 1
if(joining_roads_count > 2):
intersections.append([[x,y],joining_roads_count])
intersections_to_modify = [j[0].copy() for j in [i for i in intersections]] possible_roads_to_modify = [i for i in roads if i not in intersections_area]
intersections_to_leave_count = (len(intersections)//2) + 1 roads_to_modify = []
for i in range(intersections_to_leave_count): for i in range(1,len(possible_roads_to_modify)//3):
intersections_to_modify.remove(random.choice(intersections_to_modify)) 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)) """CREATION TIME"""
print("To modify: "+str(intersections_to_modify)) #perform modification based on road
for r in roads_to_modify:
#modify each selected intersection #select possible directions for modification
for intersection in intersections_to_modify: x, y = r
direction = random.choice([1,-1])
#search for neighbours print(x,y)
neighbours = [] if([x+1, y] in roads or [x-1, y] in roads):
#modify y, as there is road on x coordinates
index = x_roads_coordinates.index(intersection[0]) print("y: up/down")
if(index != 0): route = []
neighbour = [x_roads_coordinates[index-1], intersection[1]] current_tile = [x,y+direction]
neighbours.append(neighbour) while(True):
if(index != len(x_roads_coordinates)-1): if(current_tile[1]<0 or current_tile[1]>=height or current_tile in roads):
neighbour = [x_roads_coordinates[index+1], intersection[1]] break
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: 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]]
print("Intersection: "+str(intersection)) #choose if the route should be complete or not
print("Neighbour: "+str(neighbour)) if(len(route)>1):
print("Route: "+str(route)) if(random.randint(1,100)<=40): #40% chance for route not to be full length
print("Intersections after modification: "+str(intersections)) route_len = random.randint(1,len(route))
route = route[1:route_len]
#EXPERIMENTAL #add new route to roads
#decide if modified route should disappear or just have some field removed roads.extend(route)
#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 #insert roads into the grid
for coord in roads: for coord in roads: