found the masses
This commit is contained in:
56
main.py
56
main.py
@ -23,6 +23,8 @@ grass_high = 0.6
|
|||||||
mountains = 1
|
mountains = 1
|
||||||
block = "█"
|
block = "█"
|
||||||
block = block + block
|
block = block + block
|
||||||
|
LAND = "land"
|
||||||
|
WATER = "water"
|
||||||
|
|
||||||
def generate_world(seed):
|
def generate_world(seed):
|
||||||
try:
|
try:
|
||||||
@ -46,8 +48,8 @@ def generate_world(seed):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
def terrain_matches(cell1, cell2):
|
def terrain_matches(cell1, cell2, key):
|
||||||
return cell1["is_land"] == cell2["is_land"] or cell1["is_water"] == cell2["is_water"]
|
return cell1[key] == cell2[key]
|
||||||
|
|
||||||
def get_from_coords(world, x, y):
|
def get_from_coords(world, x, y):
|
||||||
if y >= 0 and x>= 0 and y < len(world) and x < len(world[y]):
|
if y >= 0 and x>= 0 and y < len(world) and x < len(world[y]):
|
||||||
@ -76,29 +78,47 @@ def get_adjacent(world, x, y):
|
|||||||
|
|
||||||
|
|
||||||
def group_terrain(world):
|
def group_terrain(world):
|
||||||
groups = [[], []]
|
terrain = []
|
||||||
|
to_visit = []
|
||||||
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):
|
||||||
cell = world[y][x]
|
cell = world[y][x]
|
||||||
terrain = {
|
cell_with_details = {
|
||||||
"v": cell,
|
"v": cell,
|
||||||
"x": x,
|
"x": x,
|
||||||
"y": y
|
"y": y,
|
||||||
}
|
"type": LAND if cell > water else WATER
|
||||||
if cell > water:
|
}
|
||||||
groups[0].append(terrain)
|
row.append(cell_with_details)
|
||||||
else:
|
to_visit.append(cell_with_details)
|
||||||
groups[1].append(terrain)
|
terrain.append(row)
|
||||||
for group in groups:
|
|
||||||
sub_group_id = 1
|
|
||||||
for i, cell in enumerate(group):
|
|
||||||
if i == 0:
|
|
||||||
cell["group_id"] = sub_group_id
|
|
||||||
elif
|
|
||||||
|
|
||||||
print(groups)
|
visited = []
|
||||||
return groups
|
def lets_walk(w, x, y, visited, group_id):
|
||||||
|
cell = get_from_coords(w, x, y)
|
||||||
|
cell["group_id"] = group_id
|
||||||
|
visited.append(cell)
|
||||||
|
to_visit.remove(cell)
|
||||||
|
|
||||||
|
north, east, south, west = get_adjacent(w, x, y)
|
||||||
|
if north and not north in visited and terrain_matches(cell, north, "type"):
|
||||||
|
lets_walk(w, north["x"], north["y"], visited, group_id)
|
||||||
|
if east and not east in visited and terrain_matches(cell, east, "type"):
|
||||||
|
lets_walk(w, east["x"], east["y"], visited, group_id)
|
||||||
|
if south and not south in visited and terrain_matches(cell, south, "type"):
|
||||||
|
lets_walk(w, south["x"], south["y"], visited, group_id)
|
||||||
|
if west and not west in visited and terrain_matches(cell, west, "type"):
|
||||||
|
lets_walk(w, west["x"], west["y"], visited, group_id)
|
||||||
|
|
||||||
|
group_id = 0
|
||||||
|
while len(to_visit) > 0:
|
||||||
|
lets_walk(terrain, to_visit[0]["x"], to_visit[0]["y"], visited, group_id)
|
||||||
|
group_id += 1
|
||||||
|
|
||||||
|
print(group_id)
|
||||||
|
|
||||||
|
return terrain
|
||||||
|
|
||||||
def add_home(world):
|
def add_home(world):
|
||||||
print("Choosing home location")
|
print("Choosing home location")
|
||||||
|
Reference in New Issue
Block a user