updates
This commit is contained in:
75
main.py
75
main.py
@ -49,9 +49,34 @@ def generate_world(seed):
|
|||||||
def terrain_matches(cell1, cell2):
|
def terrain_matches(cell1, cell2):
|
||||||
return cell1["is_land"] == cell2["is_land"] or cell1["is_water"] == cell2["is_water"]
|
return cell1["is_land"] == cell2["is_land"] or cell1["is_water"] == cell2["is_water"]
|
||||||
|
|
||||||
|
def get_from_coords(world, x, y):
|
||||||
|
if y >= 0 and x>= 0 and y < len(world) and x < len(world[y]):
|
||||||
|
return world[y][x]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_adjacent(world, x, y):
|
||||||
|
north = None
|
||||||
|
east = None
|
||||||
|
south = None
|
||||||
|
west = None
|
||||||
|
|
||||||
|
if y - 1 >= 0:
|
||||||
|
north = get_from_coords(world, x, y - 1)
|
||||||
|
|
||||||
|
if x + 1 < world_w:
|
||||||
|
east = get_from_coords(world, x + 1, y)
|
||||||
|
|
||||||
|
if y + 1 < world_h:
|
||||||
|
south = get_from_coords(world, x, y + 1)
|
||||||
|
|
||||||
|
if x - 1 >= 0:
|
||||||
|
west = get_from_coords(world, x - 1, y)
|
||||||
|
|
||||||
|
return north, east, south, west
|
||||||
|
|
||||||
|
|
||||||
def group_terrain(world):
|
def group_terrain(world):
|
||||||
world_terrain_without_group = []
|
groups = [[], []]
|
||||||
group_id = 0
|
|
||||||
for y in range(world_h):
|
for y in range(world_h):
|
||||||
row = []
|
row = []
|
||||||
for x in range(world_w):
|
for x in range(world_w):
|
||||||
@ -59,40 +84,21 @@ def group_terrain(world):
|
|||||||
terrain = {
|
terrain = {
|
||||||
"v": cell,
|
"v": cell,
|
||||||
"x": x,
|
"x": x,
|
||||||
"y": y,
|
"y": y
|
||||||
"is_land": cell > water,
|
|
||||||
"is_water": cell <= water
|
|
||||||
}
|
}
|
||||||
row.append(terrain)
|
if cell > water:
|
||||||
world_terrain_without_group.append(row)
|
groups[0].append(terrain)
|
||||||
|
else:
|
||||||
|
groups[1].append(terrain)
|
||||||
|
for group in groups:
|
||||||
|
sub_group_id = 1
|
||||||
|
for i, cell in enumerate(group):
|
||||||
|
if i == 0:
|
||||||
|
cell["group_id"] = sub_group_id
|
||||||
|
elif
|
||||||
|
|
||||||
world_terrain = []
|
print(groups)
|
||||||
for y in range(world_h):
|
return groups
|
||||||
row = []
|
|
||||||
for x in range(world_w):
|
|
||||||
cell = world_terrain_without_group[y][x]
|
|
||||||
north = world_terrain_without_group[y - 1][x] if y > 0 else None
|
|
||||||
south = world_terrain_without_group[y][x] if y < world_h else None
|
|
||||||
east = world_terrain_without_group[y][x] if x < world_w else None
|
|
||||||
west = world_terrain_without_group[y][x - 1] if x > 0 else None
|
|
||||||
if north and terrain_matches(cell, north):
|
|
||||||
print(f"{x}:{y} - north")
|
|
||||||
elif east and terrain_matches(cell, east):
|
|
||||||
print(f"{x}:{y} - east")
|
|
||||||
elif south and terrain_matches(cell, south):
|
|
||||||
print(f"{x}:{y} - south")
|
|
||||||
elif west and terrain_matches(cell, west):
|
|
||||||
print(f"{x}:{y} - west")
|
|
||||||
# else:
|
|
||||||
# print("new group")
|
|
||||||
# cell["group_id"] = group_id
|
|
||||||
# group_id += 1
|
|
||||||
#print(cell)
|
|
||||||
row.append(cell)
|
|
||||||
world_terrain.append(row)
|
|
||||||
|
|
||||||
print(f"Number of groups: {group_id}")
|
|
||||||
return world_terrain
|
|
||||||
|
|
||||||
def add_home(world):
|
def add_home(world):
|
||||||
print("Choosing home location")
|
print("Choosing home location")
|
||||||
@ -159,6 +165,7 @@ def print_map(world, home):
|
|||||||
|
|
||||||
def print_terrain(world):
|
def print_terrain(world):
|
||||||
colours = ["grey", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]
|
colours = ["grey", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]
|
||||||
|
colours = colours + colours + colours + colours
|
||||||
print(" ", end="", flush=True)
|
print(" ", end="", flush=True)
|
||||||
for x in range(world_w):
|
for x in range(world_w):
|
||||||
to_print = str(x) + " " if x < 10 else x
|
to_print = str(x) + " " if x < 10 else x
|
||||||
|
Reference in New Issue
Block a user