From 4c9b2e95c51ca420caa8752d250ead7a534dbd11 Mon Sep 17 00:00:00 2001 From: Magdalena Wilczynska Date: Sun, 19 May 2019 14:42:12 +0200 Subject: [PATCH 1/7] Started road generator --- MapGenerator.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 MapGenerator.py diff --git a/MapGenerator.py b/MapGenerator.py new file mode 100644 index 0000000..69a271a --- /dev/null +++ b/MapGenerator.py @@ -0,0 +1,44 @@ +import random + +#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('G') +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) + +print(width, height) +print(x_roads_count, y_roads_count) +for g in grid: print(g) + +print("++++++++++++++++++++++++") From 36ff47fe3d0d9885409c9c7e9946c982db240ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Sun, 19 May 2019 17:11:08 +0200 Subject: [PATCH 2/7] Added route generating --- MapGenerator.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 3 deletions(-) diff --git a/MapGenerator.py b/MapGenerator.py index 69a271a..1654524 100644 --- a/MapGenerator.py +++ b/MapGenerator.py @@ -1,4 +1,4 @@ -import random +import random, datetime #generate random empty map width = random.randint(4,7) #up to 15 @@ -7,7 +7,7 @@ grid = [] row = [] for i in range(width): - row.append('G') + row.append('E') for i in range(height): grid.append(row.copy()) @@ -37,8 +37,140 @@ for i in range(y_roads_count): 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) -print("++++++++++++++++++++++++") +#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) From 76648885975d91aa64446e6282091c36ee38233d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Sun, 19 May 2019 17:28:46 +0200 Subject: [PATCH 3/7] Added saving to file --- MapGenerator.py | 295 ++++++++++++++++++++++++------------------------ config.py | 9 +- main.py | 4 +- 3 files changed, 157 insertions(+), 151 deletions(-) diff --git a/MapGenerator.py b/MapGenerator.py index 1654524..430240f 100644 --- a/MapGenerator.py +++ b/MapGenerator.py @@ -1,176 +1,175 @@ import random, datetime -#generate random empty map -width = random.randint(4,7) #up to 15 -height = random.randint(4,7) #up to 10 -grid = [] +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()) + 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)) + #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 + #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) + 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 + #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) + 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)""" + """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]) -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]) + 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: -#create list of road coordinates -roads = [] -for x in x_roads_coordinates: - for y in range(height): - roads.append([x,y]) + #search for neighbours + neighbours = [] -for y in y_roads_coordinates: - for x in range(width): - roads.append([x,y]) + 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) -"""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""" + 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) -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 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) -#remove joined roads number, we need coords -#for i in intersections_to_modify: -# i.pop() + #neighbour to modify + neighbour = random.choice(neighbours) -print("Intersections: "+str(intersections)) -print("To modify: "+str(intersections_to_modify)) + #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]]) -#modify each selected intersection -for intersection in intersections_to_modify: - - #search for neighbours - neighbours = [] + #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 - 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) + print("Intersection: "+str(intersection)) + print("Neighbour: "+str(neighbour)) + print("Route: "+str(route)) + print("Intersections after modification: "+str(intersections)) - 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)) + #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("----------------------------") - + #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) + 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" + #insert roads into the grid + for coord in roads: + #print(coord) + grid[coord[1]][coord[0]] = "R" -for g in grid: print(g) + for g in grid: print(g) -#Select position for GC -GC_position = random.choice(roads) + #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) + #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) \ No newline at end of file diff --git a/config.py b/config.py index f41f6c1..68f80b1 100644 --- a/config.py +++ b/config.py @@ -1,10 +1,17 @@ import sys, random +from MapGenerator import GenerateMap CELL_SIZE = 64 FPS = 60 DELAY = 500 -map = open( sys.argv[1], 'r' ) +try: + MAP_NAME = sys.argv[1] +except: + MAP_NAME = GenerateMap() + + +map = open( MAP_NAME, 'r' ) GRID_WIDTH, GRID_HEIGHT = [int(x) for x in map.readline().split()] GC_X, GC_Y = [int(x) for x in map.readline().split()] diff --git a/main.py b/main.py index fcf5c4d..4439689 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import pygame import sys from random import randint -from config import WINDOW_HEIGHT, WINDOW_WIDTH, GRID_HEIGHT, GRID_WIDTH, HOUSE_CAPACITY, FPS, GC_X, GC_Y +from config import WINDOW_HEIGHT, WINDOW_WIDTH, GRID_HEIGHT, GRID_WIDTH, HOUSE_CAPACITY, FPS, GC_X, GC_Y, MAP_NAME from PIL import Image,ImageDraw @@ -21,7 +21,7 @@ dump_count=0 FPS_CLOCK = pygame.time.Clock() GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) -map = open(sys.argv[1], 'r') +map = open(MAP_NAME, 'r') map.readline() map.readline() From b567eec536e94f24aa8f2154c563ee4b6131cedb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Sun, 19 May 2019 18:15:25 +0200 Subject: [PATCH 4/7] Added placing objects --- MapGenerator.py | 51 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/MapGenerator.py b/MapGenerator.py index 430240f..a958c5f 100644 --- a/MapGenerator.py +++ b/MapGenerator.py @@ -1,4 +1,4 @@ -import random, datetime +import random, datetime, itertools def GenerateMap(): #generate random empty map @@ -137,28 +137,57 @@ def GenerateMap(): 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) + """f = random.randint(0,100) if(not(f % 2 or f % 5)): - route.remove(random.choice(route)) - + 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) + """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) From 0fdb9ce843f15dfa02f2d9c61385a477d8e1e41f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Sun, 19 May 2019 18:22:46 +0200 Subject: [PATCH 5/7] Disabled road modification for time being --- MapGenerator.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/MapGenerator.py b/MapGenerator.py index a958c5f..8f47112 100644 --- a/MapGenerator.py +++ b/MapGenerator.py @@ -38,22 +38,6 @@ def GenerateMap(): 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: @@ -67,7 +51,24 @@ def GenerateMap(): """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""" + 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 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]) intersections_to_modify = [j[0].copy() for j in [i for i in intersections]] intersections_to_leave_count = (len(intersections)//2) + 1 @@ -139,12 +140,12 @@ def GenerateMap(): #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))""" + #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] + roads = [c for c in roads if c not in route]""" #insert roads into the grid for coord in roads: From e9d7f25bbea170ebb2aacab35dc6e8eb93f10adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Tue, 21 May 2019 21:24:57 +0200 Subject: [PATCH 6/7] Added better solution for road modification --- MapGenerator.py | 143 +++++++++++++++++------------------------------- 1 file changed, 51 insertions(+), 92 deletions(-) diff --git a/MapGenerator.py b/MapGenerator.py index 8f47112..506037d 100644 --- a/MapGenerator.py +++ b/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: From 06de0e681f5b8099893ccefd4f35b6dcba9b347e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Wilczy=C5=84ska?= Date: Tue, 21 May 2019 21:51:10 +0200 Subject: [PATCH 7/7] Cleaned up a bit --- DataModels/GC.py | 1 - MapGenerator.py | 23 ++++++----------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/DataModels/GC.py b/DataModels/GC.py index 6e60278..aa26734 100644 --- a/DataModels/GC.py +++ b/DataModels/GC.py @@ -79,7 +79,6 @@ class GC(Cell): for row in environment: b = 0 for col in row: - print(col) if (type(col) is House): houses_list.append([col,[a,b]]) if (type(col) is Dump): diff --git a/MapGenerator.py b/MapGenerator.py index 506037d..4cb4d01 100644 --- a/MapGenerator.py +++ b/MapGenerator.py @@ -2,8 +2,8 @@ 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 + width = random.randint(5,15) #up to 15 + height = random.randint(5,10) #up to 10 grid = [] row = [] @@ -25,7 +25,6 @@ def GenerateMap(): 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 @@ -36,7 +35,6 @@ def GenerateMap(): 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) #create list of road coordinates roads = [] @@ -65,7 +63,6 @@ def GenerateMap(): choice = random.choice(possible_roads_to_modify) possible_roads_to_modify.remove(choice) roads_to_modify.append(choice) - print(roads_to_modify) """CREATION TIME""" #perform modification based on road @@ -73,10 +70,8 @@ def GenerateMap(): #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): @@ -87,7 +82,6 @@ def GenerateMap(): 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): @@ -99,9 +93,10 @@ def GenerateMap(): #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] + if(random.randint(1,100)<=65): #40% chance for route not to be full length + #route.reverse() + route_len = random.randint(1,len(route)-1) + route = route[0:route_len] #add new route to roads roads.extend(route) @@ -110,8 +105,6 @@ def GenerateMap(): 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 = [] @@ -140,12 +133,8 @@ def GenerateMap(): 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