-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensimplex_cli.py
116 lines (94 loc) · 3.52 KB
/
opensimplex_cli.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from time import time
from pathlib import Path
from random import randint
from os import system as shell
from os import remove as remove_file
from json import loads as json_loads
# pylint: disable=R0801
BASE_PATH = Path(__file__).parent.resolve()
class OpenSimplexConfig:
def __init__(
self, seed: int = None, octaves: int = 10, persistence: float = 0.7, lacunarity: float = 1.5,
exponentiation: float = 0.7, height: float = 135.0, scale: float = 50.0,
):
self.seed = seed
self.octaves = octaves
self.persistence = persistence
self.lacunarity = lacunarity
self.exponentiation = exponentiation
self.height = height
self.scale = scale
class OpenSimplexCLI:
cli = f"{BASE_PATH}/noise_cli"
def __init__(self, config: OpenSimplexConfig, silent: bool = False):
self.cnf = config
self.silent = silent
if config.seed is None:
self.cnf.seed = randint(0, 100_000_000)
if not Path(self.cli).is_file():
raise FileNotFoundError(f"OpenSimplex CLI not found at: {self.cli}")
def get_2d_array(
self, size: int, pos_x: float = 0, pos_y: float = 0,
lower_by: float = None, sink_down: bool = False, rotate: str = None, mirror: str = None,
no_coords: bool = False,
) -> dict:
return self._cli(
dimensions=2, size=size, pos_x=pos_x, pos_y=pos_y,
lower_by=lower_by, sink_down=sink_down,
rotate=rotate, mirror=mirror,
no_coords=no_coords,
)
# todo: implement 3Darray
# def get_3d_array(
# self, size: int, pos_x: float = 0, pos_y: float = 0, pos_z: float = 0,
# ) -> tuple[list[float], float, float]:
# del pos_z
# return self._cli(dimensions=3, size=size, pos_x=pos_x, pos_y=pos_y)
# todo: implement 4Darray
# def get_4d_array(
# self, size: int, pos_x: float = 0, pos_y: float = 0, pos_z: float = 0, pos_w: float = 0,
# ) -> tuple[list[float], float, float]:
# del pos_z, pos_w
# return self._cli(dimensions=4, size=size, pos_x=pos_x, pos_y=pos_y)
@staticmethod
def _tmp_file() -> str:
return f'/tmp/map_{int(time())}_{randint(100, 999)}.json'
def _cli(
self, size: int, pos_x: float = 0, pos_y: float = 0, dimensions: int = 2,
lower_by: float = None, sink_down: bool = False, rotate: str = None, mirror: str = None,
no_coords: bool = False,
) -> dict:
t = self._tmp_file()
c = f"""{self.cli} \
-seed {self.cnf.seed} \
-dimensions {dimensions} \
-persistence {self.cnf.persistence} \
-scale {self.cnf.scale} \
-octaves {self.cnf.octaves} \
-lacunarity {self.cnf.lacunarity} \
-exponentiation {self.cnf.exponentiation} \
-height {self.cnf.height} \
-size {size} \
-x {pos_x} \
-y {pos_y} \
-out {t} \
"""
if self.silent:
c += ' -silent'
if sink_down:
c += ' -sink'
if lower_by:
c += f' -lower {lower_by}'
if no_coords:
c += ' -no-coords'
if mirror is not None and mirror in ['reverse', 'xy', 'x', 'y']:
c += f' -mirror {mirror}'
if rotate is not None and rotate in ['90cw', '90ccw', '180']:
c += f' -rotate {rotate}'
shell(c)
if not Path(t).is_file():
raise SystemError("OpenSimplex CLI execution failed!")
with open(t, 'r', encoding='utf-8') as f:
noise_map = json_loads(f.read())
remove_file(t)
return noise_map