from opensimplex import OpenSimplex import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from matplotlib.patches import Patch import random size = 100 levels = 5 colors = ["#0000FF", "#008000", "#A52A2A", "#808080", "#FFFFFF"] labels = ["Water", "Plains", "Hills", "Mountains", "High Mountains"] min_adjacent = [5, 7, 7, 7, 4] # Generate OpenSimplex noise scale = 0.1 # Increase this to make the noise more "smooth" seed = 0 # Change this to any integer to get a different noise pattern gen = OpenSimplex(seed) world = np.zeros((size, size)) for i in range(size): for j in range(size): world[i][j] = gen.noise2(i*scale, j*scale) # Normalize to [0, levels - 1.5] world = np.interp(world, (world.min(), world.max()), (0, levels - 1.5)) world = world.astype(int) # Enforce constraints # This is a simple implementation and may not work perfectly for all inputs for level in range(levels): while (world == level).sum() < min_adjacent[level]: world[np.where(world == level - 1)[0][0], np.where(world == level - 1)[1][0]] = level # Post-processing: Add level 4 in the middle of every level 3 cluster mountain_level = 3 high_mountain_level = 4 for i in range(1, size - 1): for j in range(1, size - 1): if world[i, j] == mountain_level: # Check if the cell is surrounded by mountains if all(world[i + di, j + dj] == mountain_level for di in [-1, 0, 1] for dj in [-1, 0, 1]): # Check if there are less than 3 high mountains in the neighborhood if sum(world[i + di, j + dj] == high_mountain_level for di in [-1, 0, 1] for dj in [-1, 0, 1]) < 3: world[i, j] = high_mountain_level # Convert the world array to a list of x, y, z tuples xyz = [(i, j, world[i, j]) for i in range(size) for j in range(size)] # Save to a CSV file filename = f"map_{seed}.csv" np.savetxt(filename, xyz, delimiter=",", fmt="%d") print(f"Saved to {filename}") # Visualize fig, ax = plt.subplots(figsize=(11, 7)) # Adjust as needed cmap = ListedColormap(colors) plt.imshow(world, cmap=cmap) # Create a custom legend legend_elements = [Patch(facecolor=colors[i], edgecolor=colors[i], label=f"{labels[i]}: {(world == i).sum()}") for i in range(levels)] plt.legend(handles=legend_elements, bbox_to_anchor=(1.05, 1), loc='upper left') # Save the figure to a .jpg file fig_filename = filename.replace(".csv", ".jpg") plt.savefig(fig_filename) print(f"Saved figure to {fig_filename}") plt.show()