From 4c2154777cb1c402b40c6f1ded32abdbe952a4a3 Mon Sep 17 00:00:00 2001 From: Lukewh Date: Tue, 4 Jan 2022 20:02:36 +0000 Subject: [PATCH] Initial commit --- .gitignore | 0 main.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 + 3 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..ef5079f --- /dev/null +++ b/main.py @@ -0,0 +1,114 @@ +import sys +import functools +import random +from noise import pnoise2 +from termcolor import colored + +def addition(a, b): + return a + b + +seed = 0 +seed_codes = sys.argv[1].encode("utf-8") +if seed_codes: + seed = functools.reduce(addition, seed_codes) + +scale = 25.0 +world_w, world_h = 16, 16 + +water_deepest = -0.3 +water_deep = -0.2 +water = -0.1 +grass = 0.3 +grass_high = 0.6 +mountains = 1 +block = "█" +block = block + block + +def generate_world(seed): + try: + print("Seed: " + str(seed)) + random.seed(seed) + world = [] + for y in range(1, world_h + 1): + row = [] + for x in range(1, world_w + 1): + row_noise = pnoise2( + x / scale, + y / scale, + octaves=10, + persistence=0.5, + lacunarity=2.0, + base=seed + ) + row.append(row_noise) + world.append(row) + return world + except Exception as e: + print(e) + +def add_home(world): + print("Choosing home location") + home = (0, 0) + + # build only on "grass" + valid_locations = [] + for y in range(world_h): + if y > 0 and y < world_h - 1: + for x in range(world_w): + if x > 0 and x < world_w - 1: + cell = world[y][x] + if cell > water and cell <= grass: + valid_locations.append((x, y)) + + location = random.randrange(len(valid_locations)) + home = valid_locations[location] + print(home) + return home + +def add_dock(world, home): + print("Adding a dock") + # find the closest bit of water to the house? + # find the biggest body of water + + +def percentage_of_land(world): + total = world_w * world_h + above_land = 0 + for y in range(world_h): + for x in range(world_w): + cell = world[y][x] + if cell > water: + above_land = above_land + 1 + return above_land / total + +def print_map(world, home): + for y in range(world_h): + for x in range(world_w): + cell = world[y][x] + if home[0] == x and home[1] == y: + print(colored(block, "magenta"), end="", flush=True) + elif cell <= water_deepest: + print(colored(block, "grey"), end="", flush=True) + elif cell <= water_deep: + print(colored(block, "blue", attrs=["dark"]), end="", flush=True) + elif cell <= water: + print(colored(block, "blue"), end="", flush=True) + elif cell <= grass: + print(colored(block, "green"), end="", flush=True) + elif cell <= grass_high: + print(colored(block, "green", attrs=["dark"]), end="", flush=True) + else: + print(colored(block, "white"), end="", flush=True) + print("") + +world = generate_world(seed) +i = 1 +while (percentage_of_land(world) < 0.25 or 1 - percentage_of_land(world) < 0.25): + world = generate_world(seed + i) + i = i + 1 + +print(str(i - 1) + " seed iterations") + +home = add_home(world) +dock = add_dock(world, home) +print_map(world, home) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bfd4965 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +noise==1.2.2 +termcolor==1.1.0