Added saving to file
This commit is contained in:
parent
36ff47fe3d
commit
7664888597
285
MapGenerator.py
285
MapGenerator.py
@ -1,176 +1,175 @@
|
|||||||
import random, datetime
|
import random, datetime
|
||||||
|
|
||||||
#generate random empty map
|
def GenerateMap():
|
||||||
width = random.randint(4,7) #up to 15
|
#generate random empty map
|
||||||
height = random.randint(4,7) #up to 10
|
width = random.randint(4,15) #up to 15
|
||||||
grid = []
|
height = random.randint(4,10) #up to 10
|
||||||
|
grid = []
|
||||||
|
|
||||||
row = []
|
row = []
|
||||||
for i in range(width):
|
for i in range(width):
|
||||||
row.append('E')
|
row.append('E')
|
||||||
for i in range(height):
|
for i in range(height):
|
||||||
grid.append(row.copy())
|
grid.append(row.copy())
|
||||||
|
|
||||||
#define number of roads for each axis
|
#define number of roads for each axis
|
||||||
x_roads_count = random.randint(2, max(width//2.3,2))
|
x_roads_count = random.randint(2, max(width//3,2))
|
||||||
y_roads_count = random.randint(2, max(height//2.3,2))
|
y_roads_count = random.randint(2, max(height//3,2))
|
||||||
|
|
||||||
#select coords of roads for x
|
#select coords of roads for x
|
||||||
x_roads_coordinates = [] #output coords
|
x_roads_coordinates = [] #output coords
|
||||||
possible_coordiantes = [i for i in range(width)] #coords to choose from
|
possible_coordiantes = [i for i in range(width)] #coords to choose from
|
||||||
|
|
||||||
for i in range(x_roads_count):
|
for i in range(x_roads_count):
|
||||||
coordinate = random.choice(possible_coordiantes)
|
coordinate = random.choice(possible_coordiantes)
|
||||||
road_area = [coordinate-1, coordinate, coordinate+1]
|
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
|
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)
|
x_roads_coordinates.append(coordinate)
|
||||||
print(x_roads_coordinates)
|
print(x_roads_coordinates)
|
||||||
|
|
||||||
#select coords of roads for y
|
#select coords of roads for y
|
||||||
y_roads_coordinates = [] #output coords
|
y_roads_coordinates = [] #output coords
|
||||||
possible_coordiantes = [i for i in range(height)] #coords to choose from
|
possible_coordiantes = [i for i in range(height)] #coords to choose from
|
||||||
|
|
||||||
for i in range(y_roads_count):
|
for i in range(y_roads_count):
|
||||||
coordinate = random.choice(possible_coordiantes)
|
coordinate = random.choice(possible_coordiantes)
|
||||||
road_area = [coordinate-1, coordinate, coordinate+1]
|
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
|
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)
|
y_roads_coordinates.append(coordinate)
|
||||||
print(y_roads_coordinates)
|
print(y_roads_coordinates)
|
||||||
|
|
||||||
"""combine coords into list of intersection coordinates with number of roads they are joining
|
"""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)"""
|
(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:
|
for y in y_roads_coordinates:
|
||||||
#check how many roads are joined
|
for x in range(width):
|
||||||
joining_roads_count = 4
|
roads.append([x,y])
|
||||||
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
|
"""AND HERE WE ARE, MODIFYING ALL THE ROADS
|
||||||
roads = []
|
select half the intersections for modification
|
||||||
for x in x_roads_coordinates:
|
or at least remove half from the initial list
|
||||||
for y in range(height):
|
i mean we're operating on copy"""
|
||||||
roads.append([x,y])
|
|
||||||
|
|
||||||
for y in y_roads_coordinates:
|
intersections_to_modify = [j[0].copy() for j in [i for i in intersections]]
|
||||||
for x in range(width):
|
intersections_to_leave_count = (len(intersections)//2) + 1
|
||||||
roads.append([x,y])
|
for i in range(intersections_to_leave_count):
|
||||||
|
intersections_to_modify.remove(random.choice(intersections_to_modify))
|
||||||
|
|
||||||
"""AND HERE WE ARE, MODIFYING ALL THE ROADS
|
print("Intersections: "+str(intersections))
|
||||||
select half the intersections for modification
|
print("To modify: "+str(intersections_to_modify))
|
||||||
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]]
|
#modify each selected intersection
|
||||||
intersections_to_leave_count = (len(intersections)//2) + 1
|
for intersection in intersections_to_modify:
|
||||||
for i in range(intersections_to_leave_count):
|
|
||||||
intersections_to_modify.remove(random.choice(intersections_to_modify))
|
|
||||||
|
|
||||||
#remove joined roads number, we need coords
|
#search for neighbours
|
||||||
#for i in intersections_to_modify:
|
neighbours = []
|
||||||
# i.pop()
|
|
||||||
|
|
||||||
print("Intersections: "+str(intersections))
|
index = x_roads_coordinates.index(intersection[0])
|
||||||
print("To modify: "+str(intersections_to_modify))
|
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)
|
||||||
|
|
||||||
#modify each selected intersection
|
index = y_roads_coordinates.index(intersection[1])
|
||||||
for intersection in intersections_to_modify:
|
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)
|
||||||
|
|
||||||
#search for neighbours
|
#remove neighbour if not an intersection
|
||||||
neighbours = []
|
for n in neighbours:
|
||||||
|
if (n not in (item for sublist in intersections for item in sublist) ):
|
||||||
|
#print("FAKE"+str(n))
|
||||||
|
neighbours.remove(n)
|
||||||
|
|
||||||
index = x_roads_coordinates.index(intersection[0])
|
#neighbour to modify
|
||||||
if(index != 0):
|
neighbour = random.choice(neighbours)
|
||||||
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])
|
#get route between intersections
|
||||||
if(index != 0):
|
route = []
|
||||||
neighbour = [intersection[0], y_roads_coordinates[index-1]]
|
if(intersection[0] == neighbour[0]):
|
||||||
neighbours.append(neighbour)
|
a = min(intersection[1], neighbour[1])
|
||||||
if(index != len(y_roads_coordinates)-1):
|
b = max(intersection[1], neighbour[1])
|
||||||
neighbour = [intersection[0], y_roads_coordinates[index+1]]
|
for y in range(a+1,b):
|
||||||
neighbours.append(neighbour)
|
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]])
|
||||||
|
|
||||||
#remove neighbour if not an intersection
|
#decrease joined routes count for intersections
|
||||||
for n in neighbours:
|
for i in intersections:
|
||||||
if (n not in (item for sublist in intersections for item in sublist) ):
|
if(i[0] == intersection or i[0] == neighbour):
|
||||||
#print("FAKE"+str(n))
|
index = intersections.index(i)
|
||||||
neighbours.remove(n)
|
joined_routes = intersections[index][1]
|
||||||
|
joined_routes -= 1
|
||||||
|
if(joined_routes == 2):
|
||||||
|
intersections.remove(i)
|
||||||
|
else:
|
||||||
|
intersections[index][1] = joined_routes
|
||||||
|
|
||||||
#neighbour to modify
|
print("Intersection: "+str(intersection))
|
||||||
neighbour = random.choice(neighbours)
|
print("Neighbour: "+str(neighbour))
|
||||||
|
print("Route: "+str(route))
|
||||||
|
print("Intersections after modification: "+str(intersections))
|
||||||
|
|
||||||
#get route between intersections
|
#decide if modified route should disappear or just have some field removed
|
||||||
route = []
|
f = random.randint(0,100)
|
||||||
if(intersection[0] == neighbour[0]):
|
if(not(f % 2 or f % 5)):
|
||||||
a = min(intersection[1], neighbour[1])
|
route.remove(random.choice(route))
|
||||||
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
|
#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]
|
||||||
print("----------------------------")
|
print("----------------------------")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print(width, height)
|
print(width, height)
|
||||||
print(x_roads_count, y_roads_count)
|
print(x_roads_count, y_roads_count)
|
||||||
|
|
||||||
#insert roads into the grid
|
#insert roads into the grid
|
||||||
for coord in roads:
|
for coord in roads:
|
||||||
#print(coord)
|
#print(coord)
|
||||||
grid[coord[1]][coord[0]] = "R"
|
grid[coord[1]][coord[0]] = "R"
|
||||||
|
|
||||||
for g in grid: print(g)
|
for g in grid: print(g)
|
||||||
|
|
||||||
#Select position for GC
|
#Select position for GC
|
||||||
GC_position = random.choice(roads)
|
GC_position = random.choice(roads)
|
||||||
|
|
||||||
#Save map to file
|
#Save map to file
|
||||||
name = ".\\Resources\\Maps\\map"+str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))+"_auto.txt"
|
name = ".\\Resources\\Maps\\map"+str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))+"_auto.txt"
|
||||||
map_file = open(name, "w+")
|
map_file = open(name, "w+")
|
||||||
map_file.write(str(width)+" "+str(height)+"\n")
|
map_file.write(str(width)+" "+str(height)+"\n")
|
||||||
map_file.write(str(GC_position[0])+" "+str(GC_position[1])+"\n")
|
map_file.write(str(GC_position[0])+" "+str(GC_position[1])+"\n")
|
||||||
for row in grid:
|
for row in grid:
|
||||||
map_file.write(" ".join(row)+"\n")
|
map_file.write(" ".join(row)+"\n")
|
||||||
map_file.close()
|
map_file.close()
|
||||||
print(name)
|
print(name)
|
||||||
|
|
||||||
|
return(name)
|
@ -1,10 +1,17 @@
|
|||||||
import sys, random
|
import sys, random
|
||||||
|
from MapGenerator import GenerateMap
|
||||||
|
|
||||||
CELL_SIZE = 64
|
CELL_SIZE = 64
|
||||||
FPS = 60
|
FPS = 60
|
||||||
DELAY = 500
|
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()]
|
GRID_WIDTH, GRID_HEIGHT = [int(x) for x in map.readline().split()]
|
||||||
GC_X, GC_Y = [int(x) for x in map.readline().split()]
|
GC_X, GC_Y = [int(x) for x in map.readline().split()]
|
||||||
|
4
main.py
4
main.py
@ -3,7 +3,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import sys
|
import sys
|
||||||
from random import randint
|
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
|
from PIL import Image,ImageDraw
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ dump_count=0
|
|||||||
FPS_CLOCK = pygame.time.Clock()
|
FPS_CLOCK = pygame.time.Clock()
|
||||||
GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
|
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()
|
||||||
map.readline()
|
map.readline()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user