-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_generate.py
146 lines (103 loc) · 3.77 KB
/
world_generate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gzip
import threading
import struct
import random
def generateFlatWorld(fileName,worldX, worldY, worldZ):
print("Generating new flat world at", fileName)
volume = worldX*worldY*worldZ
blocksArray = bytearray(volume)
f = gzip.open(fileName,"wb+")
f.write(struct.pack('h',1874))
f.write(struct.pack('h',worldX))
f.write(struct.pack('h',worldZ))
f.write(struct.pack('h',worldY))
spawnX = worldX >> 1
spawnY = (worldY >> 1) + 1
spawnZ = worldZ >> 1
f.write(struct.pack('h',spawnX))
f.write(struct.pack('h',spawnZ))
f.write(struct.pack('h',spawnY))
f.write(struct.pack('B',0))
f.write(struct.pack('B',0))
f.write(struct.pack('B',0))
f.write(struct.pack('B',0))
maxY = worldY >> 1
print("Calculating")
for y in range(maxY):
for x in range(worldX):
for z in range(worldZ):
calculatedIndex = x + worldX*(z+worldZ*y)
if (y == maxY-1):
blocksArray[calculatedIndex] = 2
else:
blocksArray[calculatedIndex] = 3
print("Writing to file")
f.write(blocksArray)
f.close()
print("Finished")
# EXPERIMENTAL
import opensimplex
def noise2D(x,z):
return 0.5*(opensimplex.noise2(x,z) + 1.0)
def generateSimplexWorld(fileName,worldX, worldY, worldZ):
print("Generating new procedural world at", fileName)
volume = worldX*worldY*worldZ
blocksArray = bytearray(volume)
print("Size: ",worldX, worldY, worldZ)
f = gzip.open(fileName,"wb+")
f.write(struct.pack('h',1874))
f.write(struct.pack('h',worldX))
f.write(struct.pack('h',worldZ))
f.write(struct.pack('h',worldY))
spawnX = worldX >> 1
spawnY = (worldY) - 1
spawnZ = worldZ >> 1
print(spawnX, spawnY, spawnZ)
f.write(struct.pack('h',spawnX))
f.write(struct.pack('h',spawnZ))
f.write(struct.pack('h',spawnY))
f.write(struct.pack('B',0))
f.write(struct.pack('B',0))
f.write(struct.pack('B',0))
f.write(struct.pack('B',0))
maxY = worldY >> 1
print("Calculating")
opensimplex.seed(random.randint(0,2**64))
for x in range(worldX):
for z in range(worldZ):
amp = 0.0
octaveCount = 20
currentAmp = 1.0
freq = 10
n = 0
for o in range(octaveCount):
n += currentAmp*noise2D(freq*x/worldX,freq*z/worldZ)
amp += currentAmp
freq *= 2
currentAmp *= 0.5
n = n / amp
#n = n ** 2
maxY = (worldY >> 1) + int(n*( (worldY>>2) ))
for y in range(maxY):
calculatedIndex = x + worldX*(z+worldZ*y)
if (y == maxY-1):
blocksArray[calculatedIndex] = 2
elif y < maxY - 5:
blocksArray[calculatedIndex] = 1
else:
blocksArray[calculatedIndex] = 3
print("Writing to file")
f.write(blocksArray)
f.close()
print("Finished")
if __name__ == "__main__":
world_type = input("Enter type (flat/terrain): ")
worldName = "world"
print("Enter sizes: ")
x = int(input("Enter x: "))
y = int(input("Enter y: "))
z = int(input("Enter z: "))
if world_type == "flat":
generateFlatWorld("maps/"+worldName+".lvl",x,y,z)
elif world_type == "terrain":
generateSimplexWorld("maps/"+worldName+".lvl",x,y,z)