Added saving to file

This commit is contained in:
Magdalena Wilczyńska 2019-05-19 17:28:46 +02:00
parent 36ff47fe3d
commit 7664888597
3 changed files with 157 additions and 151 deletions

View File

@ -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)

View File

@ -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()]

View File

@ -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()