This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
/
map_generation.py
48 lines (41 loc) · 1.86 KB
/
map_generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from opensimplex import OpenSimplex
import noise
import numpy as np
class GenerationConfig:
def __init__(self):
self.shape = (30, 30)
self.scale = 100.0
self.octaves = 6
self.persistence = 0.5
self.lacunarity = 2.0
self.amplitude_scale = 1.0
self.simplex_step = 1.0
class GenerationResult:
def __init__(self, data):
self.data = data
def generate_map(map_config: GenerationConfig, x_offset: float, map_type: str) -> GenerationResult:
if map_type == 'perlin':
data = np.zeros(map_config.shape, dtype=np.float32)
for i in range(map_config.shape[0]):
for j in range(map_config.shape[1]):
data[i][j] = noise.pnoise2(i / map_config.scale,
(j + x_offset) / map_config.scale,
octaves=map_config.octaves,
persistence=map_config.persistence,
lacunarity=map_config.lacunarity,
repeatx=10,
repeaty=10,
base=0) * map_config.amplitude_scale
result = GenerationResult(data.flatten())
elif map_type == 'simplex':
gen = OpenSimplex(seed=1)
data = np.zeros(map_config.shape, dtype=np.float32)
for i in range(map_config.shape[0]):
for j in range(map_config.shape[1]):
data[i][j] = gen.noise2d(i / map_config.scale * map_config.simplex_step,
(j + x_offset) / map_config.scale * map_config.simplex_step) \
* map_config.amplitude_scale
result = GenerationResult(data.flatten())
else:
raise Exception('Invalid type:' + map_type)
return result