Permalink
Browse files

Made terrain look more like terrain and less like noise.

  • Loading branch information...
dfloer committed Nov 12, 2018
1 parent 33efd6f commit cc73c92889b2194b6d8b7cd786195a518676e6c7
Showing with 16 additions and 11 deletions.
  1. +5 −0 hex_math.py
  2. +5 −0 terrain test/settings.py
  3. +6 −11 terrain test/terrain_test.py
@@ -137,6 +137,11 @@ def polygon_corners(layout, h):
corners.append(Point(center.x + offset.x, center.y + offset.y))
return corners

def cube_to_offset(h):
col = h.q
row = h.r + (h.q + 1 * (h.q & 1)) // 2
return Point(col, row)

# End of code from Redblob.

def get_hex_chunk(center, radius):
@@ -3,3 +3,8 @@
minimum_core_distance = 42
# How often do we try to place a core, anyways?
core_offset = 2
# How much do we want to damp the noise to create chunks that are similar to each other next to each other?
# 1 creates basically no chunks. 0 doesn't work.
noise_damping_factor = 0.025
# This is used to select a sprite from the output of OpenSimplex noise, normalized between 0 and 1.
terrain_sprite_bins = [0, 0.2, 0.25, 0.5, 0.55, 0.6, 0.7, 0.8, 0.825, 0.875, 0.9, 0.915, 0.25, 1.0]
@@ -263,12 +263,7 @@ def generate(self, center, noise):
Returns:
A dictionary of terrain hexes, containing chunk_size * chunk_size items.
"""
# These appear magic. Maybe I'm missing something in OpenSimplex...
n_max = 0.8644
bins = 12
bin_size = (n_max * 2) / bins
n_bins = [-n_max + x * bin_size for x in range(bins + 1)]

n_bins = settings.terrain_sprite_bins
x_dim, y_dim = (self.chunk_size, self.chunk_size)
chunk_cells = {}
# Why all this futzing around with dimensions // 2? Because I wanted the start of the chunk to be centered in the middle of the chunk.
@@ -282,10 +277,10 @@ def generate(self, center, noise):
qq = center.q + q
rr = center.r + r
h = Hexagon(qq, rr, -qq - rr)
#terrain_type = str(randint(0, 12))
# Should I be normalizing the hex values to an xy grid so each hex is right next to each other, not based off of the hexagon's center coordinates?
xy = hex_math.hex_to_pixel(layout, h)
noise_val = noise.noise2d(xy.x, xy.y)
# Normalize to offset grid coordinates, because we want to sample the noise at points right next to each other.
xy = hex_math.cube_to_offset(h)
damp = settings.noise_damping_factor
noise_val = noise.noise2d(xy.x * damp, xy.y * damp) / 2.0 + 0.5 # Rescale to 0.0 to 1.0
# Find the closest value in the list to our noise value. We want to normalize to a sprite.
t = min(range(len(n_bins)), key=lambda i: abs(n_bins[i] - noise_val))
terrain_type = str(t)
@@ -341,7 +336,7 @@ def load_spritesheet(path):
path: Path to spritesheet file..
Returns:
Dictionary contraining the sprites. Key is the sprite's index extension, value is a Sprite object.
Index goes Left t right, top to bottom.
Index goes top to bottom, then left to right.
"""
images = {}
spritesheet_path = os.path.join(path, "8x8 spritesheet.png")

0 comments on commit cc73c92

Please sign in to comment.