-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProceduralGeneration.py
117 lines (103 loc) · 4.95 KB
/
ProceduralGeneration.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
117
from helpers import *
#import matplotlib.pyplot as plt
def GenerateFlatland (self, n, s, y):
for x in xrange(-n, n + 1, s):
for z in xrange(-n, n + 1, s):
# create a layer stone an grass everywhere.
self.add_block((x, y - 2, z), GRASS, immediate=False)
self.add_block((x, y - 3, z), STONE, immediate=False)
if x in (-n, n) or z in (-n, n):
# create outer walls.
for dy in xrange(-2, 3):
self.add_block((x, y + dy, z), STONE, immediate=False)
def GenerateHills (self, n):
o = n - 10
for _ in xrange(120):
a = random.randint(-o, o) # x position of the hill
b = random.randint(-o, o) # z position of the hill
c = -1 # base of the hill
h = random.randint(1, 6) # height of the hill
s = random.randint(4, 8) # 2 * s is the side length of the hill
d = 1 # how quickly to taper off the hills
# t = random.choice([GRASS, SAND, BRICK])
t = GRASS
for y in xrange(c, c + h):
for x in xrange(a - s, a + s + 1):
for z in xrange(b - s, b + s + 1):
if (x - a) ** 2 + (z - b) ** 2 > (s + 1) ** 2:
continue
if (x - 0) ** 2 + (z - 0) ** 2 < 5 ** 2:
continue
self.add_block((x, y, z), t, immediate=False)
s -= d # decrement side lenth so hills taper off
def GenerateFlatLandBlockList (self, n, s):
blockList = []
for x in xrange(-n, n + 1, s):
for z in xrange(-n, n + 1, s):
blockList.append (GRASS)
return blockList
def GenerateLandFromBlockList (self, noise, n, s, y):
biomeBlockTypeList = GenerateBiomeNoise (self, n, s, y)
for x in xrange(-n, n + 1, s):
for z in xrange(-n, n + 1, s):
self.add_block((x, int (noise[x, z]) - 3, z), biomeBlockTypeList[x * 2 * n + z], immediate=False)
for block_height in xrange(-WORLD_HEIGHT, int (noise[x, z]) - 3, s):
self.add_block((x, block_height, z), DIRT, immediate=False)
self.add_block((x, -WORLD_HEIGHT - 1, z), STONE, immediate=False)
def GeneratePerlinNoise (self, n, s, y):
blockList = []
lin = np.linspace (0, 20, n*2, endpoint=False)
x, z = np.meshgrid (lin,lin) # FIX3: I thought I had to invert x and y here but it was a mistake
current_time = time.time()
r = int (random.random () * int(current_time) * 1000) % (2**32 - 1)
noise = perlin(x, z, r)
for x in xrange (0, 2*n, s):
for z in xrange (0, 2*n, s):
noise[x, z] = - math.floor (noise[x,z] * 4.5)
for y in xrange(-y, y + 1, s):
if (noise[x, z] == y):
blockList.append (DIRT)
# print(noise[x, z])
# self.add_block ((x, noise[x,z] - 2.0, z), GRASS, immediate = False)
# print(len(blockList))
return noise
def get_gradient_3d (width, height, start_list, stop_list, is_horizontal_list):
result = np.zeros ((height, width, len (start_list)), dtype = np.float)
for i, (start, stop, is_horizontal) in enumerate (zip (start_list, stop_list, is_horizontal_list)):
result[:, :, i] = get_gradient_2d (start, stop, width, height, is_horizontal)
return result
def GenerateBiomeNoise (self, n, s, y):
array = get_gradient_2d (0, 255, n * 2, n * 2, True)
# print (array)
blockTypeList = []
for i in range(0, n*2):
for j in range(0, n*2):
blockTypeList.append (GRASS)
lin = np.linspace (0, 2, n*2, endpoint=False)
x, z = np.meshgrid (lin,lin) # FIX3: I thought I had to invert x and y here but it was a mistake
current_time = time.time()
r = int (random.random () * int (current_time) * 1000) % (2**32 - 1)
noise = perlin (x, z, r)
for x in xrange (0, 2*n, s):
for z in xrange (0, 2*n, s):
# array[x, z] = - math.floor (array[x, z] * 4.5)
if (array[x, z] < (255/6.)):
blockTypeList[int(x * n * 2 + z)] = GRASS
elif (array[x, z] < (255/6. * 2.)):
blockTypeList[int(x * n * 2 + z)] = SAND
elif (array[x, z] < (255/6. * 3.)):
blockTypeList[int(x * n * 2 + z)] = MUD
elif (array[x, z] < (255/6. * 4.)):
blockTypeList[int(x * n * 2 + z)] = GLASS
elif (array[x, z] < (255/6. * 5.)):
blockTypeList[int(x * n * 2 + z)] = JAIL
elif (array[x, z] < (255/6. * 6.)):
blockTypeList[int(x * n * 2 + z)] = ICE
# print (noise[x, z])
# self.add_block ((x, noise[x,z] - 2.0, z), GRASS, immediate = False)
return blockTypeList
def get_gradient_2d (start, stop, width, height, is_horizontal):
if is_horizontal:
return np.tile (np.linspace (start, stop, width), (height, 1))
else:
return np.tile (np.linspace (start, stop, height), (width, 1)).T