VoxelServer/ServerCore/Chunk.py

84 lines
2.4 KiB
Python
Raw Permalink Normal View History

2018-10-17 23:13:32 +02:00
#!/usr/bin/env python3
import numpy as np
from enum import Enum
from typing import Tuple
2018-10-24 19:27:42 +02:00
import json
2018-11-24 16:19:29 +01:00
2018-10-17 23:13:32 +02:00
class Direction(Enum):
Left = 0
Down = 1
Front = 2
Right = 3
Up = 4
Back = 5
2018-11-24 16:19:29 +01:00
2018-10-17 23:13:32 +02:00
class Chunk(object):
"""
2018-11-24 16:19:29 +01:00
This is a manager for Chunks. Raw Data (provided as 3D-Tensor of integers)
is located in Chunk.block_data
2018-10-17 23:13:32 +02:00
"""
CHUNK_SIDELENGTH = 16 # 16
CHUNK_SIDELENGTH_SQRD = 256 # 16 * 16
CHUNK_BLOCKS = 4096 # 16 * 16 * 16
@staticmethod
2018-11-24 16:19:29 +01:00
def Index2Coords(i: int) -> Tuple[int, int, int]:
2018-10-17 23:13:32 +02:00
x = i % Chunk.CHUNK_SIDELENGTH
y = (i // Chunk.CHUNK_SIDELENGTH) % Chunk.CHUNK_SIDELENGTH
z = i // (Chunk.CHUNK_SIDELENGTH_SQRD)
2018-11-24 16:19:29 +01:00
return x, y, z
2018-10-17 23:13:32 +02:00
@staticmethod
2018-11-24 16:19:29 +01:00
def Coords2Index(x: int, y: int, z: int) -> int:
2018-10-17 23:13:32 +02:00
return x + y * Chunk.CHUNK_SIDELENGTH + z * Chunk.CHUNK_SIDELENGTH_SQRD
2018-11-24 16:19:29 +01:00
2018-10-24 20:59:22 +02:00
@staticmethod
2018-11-24 16:19:29 +01:00
def CreateFromJson(json_string: str) -> 'Chunk':
2018-10-24 20:59:22 +02:00
# reading data fields from json
d = json.loads(json_string)
assert d['datatype'] == 'world_chunk'
chunk_x, chunk_y, chunk_z = d['position']
c = Chunk(chunk_x, chunk_y, chunk_z)
2018-11-24 16:19:29 +01:00
c.block_data[:, :, :] = np.array(d['data']).reshape(
(Chunk.CHUNK_SIDELENGTH,
Chunk.CHUNK_SIDELENGTH,
Chunk.CHUNK_SIDELENGTH))
2018-10-24 20:59:22 +02:00
return c
2018-11-24 16:19:29 +01:00
2018-10-24 19:27:42 +02:00
def toJson(self):
2018-11-24 16:19:29 +01:00
# numpy array -> reshape to 1D -> to python list
self.json_rep['data'] = self.block_data.reshape(-1).tolist()
2018-10-24 19:27:42 +02:00
return json.dumps(self.json_rep)
2018-10-17 23:13:32 +02:00
2018-11-24 16:19:29 +01:00
def __init__(self, chunk_x: int, chunk_y: int, chunk_z: int):
self.block_data = np.zeros(shape=(
Chunk.CHUNK_SIDELENGTH,
Chunk.CHUNK_SIDELENGTH,
Chunk.CHUNK_SIDELENGTH),
dtype=int)
self.chunk_position = [chunk_x, chunk_y, chunk_z]
self.neighbor_chunks = [None, None, None, None, None, None]
2018-10-24 19:27:42 +02:00
2018-11-24 16:19:29 +01:00
self.json_rep = {} # create empty json representation
2018-10-24 19:27:42 +02:00
self.json_rep['datatype'] = 'world_chunk'
self.json_rep['position'] = [chunk_x, chunk_y, chunk_z]
self.json_rep['data'] = None
2018-11-24 16:19:29 +01:00
def LinkToChunk(self, chunk: 'Chunk', direction: Direction) -> None:
2018-10-17 23:13:32 +02:00
self.neighbor_chunks[direction.value] = chunk
chunk.neighbor_chunks[(direction.value + 3) % 6] = self
2018-10-24 19:27:42 +02:00
def __str__(self):
return self.toJson()
2018-11-24 16:19:29 +01:00
2018-10-24 19:27:42 +02:00
def __repr__(self):
return self.__str__()