-
Notifications
You must be signed in to change notification settings - Fork 0
/
worldgen.py
386 lines (322 loc) · 13.6 KB
/
worldgen.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from __future__ import print_function
import mcpi
from mcpi import minecraft
from mcpi.vec3 import Vec3
from threading import Thread
import time
import numpy as np
import queue
import threading
from connection import Connection
import random
import os
# Run this code in the terminal to improve performance
# !!!!!!!!!!!!!!!!!!!!!!!!!!!
# GENERATION CODE STARTS HERE
# !!!!!!!!!!!!!!!!!!!!!!!!!!!
# Requirements:
# 1.generator():
# Input: chunk pos
# each chunk is 64x128x64 in xyz order
# Output: must return 2 numpy arrays
# IMPORTANT:
# Chunks do not have negative coordinate values.
# If you want to place a block at y=0, write it at [x, 32, z]
# Writing at [x, 0, z] is bedrock level
# generator() should be thread safe. In this example it is not
# and perlin noise sometimes gives wrong values.
# 2.arguments:
# Must be defined, can be empty
# Used to pass global variables to the generator
# In this example, the noise is passed.
# Several values can be passed.
import perlin_noise
noise = perlin_noise.PerlinNoise(octaves = 5, seed = 4)
arguments = [noise] # Must be defined
def generator(terrain_chunk_position : Vec3, arguments):
# 1.Make a giant block array (always needed)
blocks = np.zeros([64, 128, 64], dtype=np.byte)
ids = np.zeros([64, 128, 64], dtype=np.byte)
# 2.Unpack arguments (optional, depends on the generator)
noise = arguments[0]
# 3.Fill it using perlin noise (can be changed to your algorithm)
for x_offset in range(64):
for z_offset in range(64):
offset = Vec3(x_offset,0,z_offset)
noise_position = terrain_chunk_position * 64 + offset # Get actual position
height = int(32 + 30 * noise([noise_position.x/100.21, noise_position.z/100.21]))
for y in range(height):
blocks[x_offset,y,z_offset] = 1 # Place blocks
# 4.Return arrays (always needed)
return blocks, ids
# !!!!!!!!!!!!!!!!!!!!!!!!!!!
# GENERATION CODE ENDS HERE
# !!!!!!!!!!!!!!!!!!!!!!!!!!!
# Everything beyond this point is backend code
# It is used to handle the player, build optimising and more.
# Editing it is not advised.
def threadedQuadSetup(work_queue, thread_count=20):
def threadedQuad(connection, work_queue):
while True:
if not work_queue.empty():
try:
cube = work_queue.get(False)
except:
continue
connection.send(b"world.setBlocks",
cube[0], cube[1], cube[2], cube[3])
# After testing, the queue load is distributed,
# and all calls are cubes.
# No bugs here.
elif stop_workers.is_set():
connection.close()
break
# Start threads
workers = []
for i in range(thread_count):
worker = threading.Thread(target = threadedQuad, args = (Connection("localhost", 4711), work_queue))
worker.start()
workers.append(worker)
# Stop threads
for worker in workers:
worker.join() # Wait for it to stop
print('Stopped', worker)
def toRelative(coordinates : Vec3):
global world_offset
return coordinates + world_offset
def toReal(coordinates : Vec3):
global world_offset
return coordinates - world_offset
def build(terrain_chunk_position : Vec3, world_chunk_position : Vec3, work_queue, arguments):
global generating
generating += 1
try:
chunk_top = open(f'world/t{terrain_chunk_position.x};{terrain_chunk_position.z}.quad', 'rb')
chunk_bottom = open(f'world/b{terrain_chunk_position.x};{terrain_chunk_position.z}.quad', 'rb')
data_top = chunk_top.read()
data_bottom = chunk_bottom.read()
chunk_top.close()
chunk_bottom.close()
if len(data_top) == 0 or len(data_bottom) == 0:
print(f'Empty quad chunk {terrain_chunk_position}. Deleting.')
# Corrupt file. Remove and raise an error
os.remove(f'world/t{terrain_chunk_position.x};{terrain_chunk_position.z}.quad')
os.remove(f'world/b{terrain_chunk_position.x};{terrain_chunk_position.z}.quad')
raise FileNotFoundError
except FileNotFoundError:
print(f'Generating chunk {terrain_chunk_position}')
# Make files
open(f'world/t{terrain_chunk_position.x};{terrain_chunk_position.z}.quad', 'x').close()
open(f'world/b{terrain_chunk_position.x};{terrain_chunk_position.z}.quad', 'x').close()
chunk_top = open(f'world/t{terrain_chunk_position.x};{terrain_chunk_position.z}.quad', 'wb')
chunk_bottom = open(f'world/b{terrain_chunk_position.x};{terrain_chunk_position.z}.quad', 'wb')
blocks, ids = generator(terrain_chunk_position, arguments)
data_top = bytes(saveQuad(blocks[:,:64,:], ids[:,:64,:]))
data_bottom = bytes(saveQuad(blocks[:,64:,:], ids[:,64:,:]))
chunk_top.write(data_top)
chunk_bottom.write(data_bottom)
chunk_top.close()
chunk_bottom.close()
finally:
pointer_id = threading.get_ident() # Allocate a pointer
quad_pointers[pointer_id] = 0
loadQuad(toRelative(world_chunk_position * 64), Vec3(64,64,64), data_bottom, work_queue, pointer_id)
quad_pointers[pointer_id] = 0
loadQuad(toRelative(world_chunk_position * 64) + Vec3(0,64,0), Vec3(64,64,64), data_top, work_queue, pointer_id)
quad_pointers.pop(pointer_id) # Free the memory
generating -= 1
def loadQuad(world_position : Vec3, size : Vec3, data : bytes, work_queue, pointer_id):
global quad_pointers
# Quad = 91 '['
# No edit = 93 ']'
if size.x == 0 or size.y == 0 or size.z == 0:
return
last_byte = data[quad_pointers[pointer_id]]
quad_pointers[pointer_id] += 1
half_size = size.clone()
half_size *= 0.5
half_size.ifloor()
if last_byte == 91:
for x in range(2):
for y in range(2):
for z in range(2):
loadQuad(world_position + Vec3(x * half_size.x, y * half_size.y, z * half_size.z), half_size, data, work_queue, pointer_id)
else:
if last_byte == 93: # Don't change the quad
return
debug = False
block = last_byte
if debug:
if block != 0:
# USE THIS TO DEBUG THE QUAD DECOMPOSITION
block = 35 # Wool
size_power = [1,2,4,8,16,32,64].index(size.x) # HACK: ONLY WORKS ON 2^N QUADS!!!
pos_sum = world_position.x + world_position.y + world_position.z
last_byte = size_power * 2 + ((pos_sum / size.x) % 2)
else:
last_byte = data[quad_pointers[pointer_id]]
quad_pointers[pointer_id] += 1
# Note: No bugs during unpacking found, check generator/saver
# Add to worker queue
work_queue.put((world_position, world_position + size - Vec3(1,1,1), block, last_byte))
def saveQuad(blocks, ids):
# Quad = 91 '['
# No edit = 93 ']'
if blocks.shape == (1,1,1):
return [blocks[0,0,0], ids[0,0,0]]
if 0 in blocks.shape:
return [93] # Empty because of a zero quad
min_block = blocks.argmin()
min_id = ids.argmin()
if min_block == blocks.argmax() and min_id == ids.argmax():
# All blocks are the same, quad is solved
return [blocks[0,0,0], ids[0,0,0]]
output = [91] # Start packing a quad
size_x, size_y, size_z = blocks.shape
for x in range(2):
for y in range(2):
for z in range(2):
quad = saveQuad(
blocks[
[0,size_x//2][x]:[size_x//2,size_x][x],
[0,size_y//2][y]:[size_y//2,size_y][y],
[0,size_z//2][z]:[size_z//2,size_z][z]
], ids[
[0,size_x//2][x]:[size_x//2,size_x][x],
[0,size_y//2][y]:[size_y//2,size_y][y],
[0,size_z//2][z]:[size_z//2,size_z][z]])
output += quad
return output
def getPlayerChunk(player):
position = toReal(player.getTilePos())
position.x = position.x // 64
position.z = position.z // 64
position.y = -1
return position
def getPlayerFloatChunk(player):
position = toReal(player.getTilePos())
position.x = position.x / 64
position.z = position.z / 64
position.y = -1
return position
def setChunk(pos, value, work_queue, arguments, threaded = True):
global chunk_coords
if chunk_coords[pos] == value:
return
else:
chunk_coords[pos] = value
if threaded:
t = threading.Thread(target = build, args = (value, Vec3(pos[0],0,pos[1]), work_queue, arguments))
t.start()
else:
build(value, Vec3(pos[0],0,pos[1]), work_queue, arguments)
def main(work_queue):
chunk = getPlayerChunk(mc.player)
fchunk = getPlayerFloatChunk(mc.player)
global chunk_coords
s = work_queue.qsize()
print(f'Queue size: {s} ', end = '\r')
if chunk.x == 0:
mc.postToChat('Generating new terrain. Please wait.')
print('Player on X-')
for offset_x in range(2):
for z in range(4):
setChunk((offset_x + 2, z), chunk_coords[(offset_x + 0, z)], work_queue, arguments)
while generating:
print(f'Generators: {generating} ',end = '\r')
mc.player.setPos(mc.player.getPos() + Vec3(128,0,0))
print(f'Generators: {generating} ',end = '\r')
elif 1.25 < fchunk.x < 2.00:
#mc.postToChat('Generating unseen half')
for z in range(4):
setChunk((0, z), chunk_coords[(1, z)] - Vec3(1,0,0), work_queue, arguments)
setChunk((2, z), chunk_coords[(1, z)] + Vec3(1,0,0), work_queue, arguments)
elif 2.00 < fchunk.x < 2.75:
#mc.postToChat('Generating unseen half')
for z in range(4):
setChunk((1, z), chunk_coords[(2, z)] - Vec3(1,0,0), work_queue, arguments)
setChunk((3, z), chunk_coords[(2, z)] + Vec3(1,0,0), work_queue, arguments)
elif chunk.x == 3:
mc.postToChat('Generating new terrain. Please wait.')
print('Player on X+')
for offset_x in range(2):
for z in range(4):
setChunk((offset_x + 0, z), chunk_coords[(offset_x + 2, z)], work_queue, arguments)
while generating:
print(f'Generators: {generating} ',end = '\r')
mc.player.setPos(mc.player.getPos() - Vec3(128,0,0))
print(f'Generators: {generating} ',end = '\r')
chunk = getPlayerChunk(mc.player)
fchunk = getPlayerFloatChunk(mc.player)
if chunk.z == 0:
mc.postToChat('Generating new terrain. Please wait.')
print('Player on Z-')
for offset_z in range(2):
for x in range(4):
setChunk((x, offset_z + 2), chunk_coords[(x, offset_z + 0)], work_queue, arguments)
while generating:
print(f'Generators: {generating} ',end = '\r')
mc.player.setPos(mc.player.getPos() + Vec3(0,0,128))
print(f'Generators: {generating} ',end = '\r')
elif 1.25 < fchunk.z < 2.00:
#mc.postToChat('Generating unseen half')
for x in range(4):
setChunk((x, 0), chunk_coords[(x, 1)] - Vec3(0,0,1), work_queue, arguments)
setChunk((x, 2), chunk_coords[(x, 1)] + Vec3(0,0,1), work_queue, arguments)
elif 2.00 < fchunk.z < 2.75:
#mc.postToChat('Generating unseen half')
for x in range(4):
setChunk((x, 1), chunk_coords[(x, 2)] - Vec3(0,0,1), work_queue, arguments)
setChunk((x, 3), chunk_coords[(x, 2)] + Vec3(0,0,1), work_queue, arguments)
elif chunk.z == 3:
mc.postToChat('Generating new terrain. Please wait.')
print('Player on Z+')
for offset_z in range(2):
for x in range(4):
setChunk((x, offset_z + 0), chunk_coords[(x, offset_z + 2)], work_queue, arguments)
while generating:
print(f'Generators: {generating} ',end = '\r')
mc.player.setPos(mc.player.getPos() - Vec3(0,0,128))
print(f'Generators: {generating} ',end = '\r')
mc = minecraft.Minecraft.create()
world_offset = mc.player.getTilePos()
world_offset.y = -64
mc.postToChat(f'Calculating world offset')
# Search min X:
while mc.getBlock(world_offset) != 95: # Border
world_offset.x -= 1
world_offset.x += 1
mc.postToChat(f'X offset: {world_offset.x}')
# Search min Z:
while mc.getBlock(world_offset) != 95: # Border
world_offset.z -= 1
world_offset.z += 1
mc.postToChat(f'Z offset: {world_offset.z}')
mc.player.setTilePos(world_offset + Vec3(128,128,128))
# Setting up crucial variables
work_queue = queue.Queue()
global generating
generating = 0
global quad_pointers
quad_pointers = {}
stop_workers = threading.Event()
quad_builder = threading.Thread(target = threadedQuadSetup, args = (work_queue, ))
quad_builder.start()
# Pregenerate chunk coords
mc.postToChat('Generating')
chunk_coords = {}
for x in range(4):
for z in range(4):
chunk_coords[(x, z)] = Vec3(-1, 0, -1)
setChunk((x, z), Vec3(x, 0, z), work_queue, arguments)
while generating:
print(f'Generators: {generating}',end = '\r')
print(f'Generators: {generating}',end = '\r')
mc.postToChat('Initial terrain generated. Building.')
try:
while True:
main(work_queue)
except KeyboardInterrupt:
print('Stopping all threads')
stop_workers.set()
quad_builder.join()