#include const int ChunkSizeX = 4; const int ChunkSizeY = 6; const int ChunkSizeZ = 4; const int WorldSizeX = 11; const int WorldSizeZ = 11; const double Scale = 0.1; const int WorldSeed = random(1000,9999); const int CaveSeed = random(1000,9999); const byte JaggedNess = 2; const int MaxChangesPerChunk = 64; SimpleNoise WorldNoise; struct BlockData { byte pX,pY,pZ; byte blockType; }; struct ChunkData { BlockData Blocks[ChunkSizeX][ChunkSizeY][ChunkSizeZ]; byte PositionX; byte PositionZ; }; ChunkData CurrentChunk; ChunkData generateChunk(int pX,int pZ) { for (int x = 0; x < ChunkSizeX; x++) { for (int z = 0; z < ChunkSizeZ; z++) { for (int y = 0; y < ChunkSizeY; y++) { BlockData CurrentBlock; CurrentBlock.blockType = 0; //stone CurrentBlock.pX = (pX+x); CurrentBlock.pZ = (pZ+z); float scaledX = (pX+x)*Scale; float scaledZ = (pZ+z)*Scale; scaledX = scaledX + WorldSeed; scaledZ = scaledZ + WorldSeed; float NoiseY = WorldNoise.noise(scaledX,scaledZ); byte actualY = (floor(NoiseY) - y); byte surfaceY = floor(floor(NoiseY*JaggedNess)-y); CurrentBlock.pY = surfaceY; float depthFactor = y/ChunkSizeY; bool willPlace = true; if (depthFactor > 0.2){ byte undergroundY = floor(y*(Scale*4)); CurrentBlock.pY = undergroundY; if (y < ChunkSizeY){ float yScaled = y* Scale / 15; float CaveMap = WorldNoise.noise(scaledX+yScaled,scaledZ+yScaled+CaveSeed); if (CaveMap < -0.75){ willPlace = false; }; }; }; if (y >= ChunkSizeY-1){ CurrentBlock.pY = -ChunkSizeY-1; }; if (willPlace){ CurrentBlock.blockType = 3; //stone }; CurrentChunk.Blocks[x][y][z] = CurrentBlock; }; }; }; return CurrentChunk; }; void setup() { Serial.begin(115200); delay(2000); Serial.println("RDY"); } bool Debug = true; void SendChunkUpdate(ChunkData chunkToSend){ byte DebugBlockType = random(1,5); for (int bX = 0; bX < ChunkSizeX; bX++){ for (int bY = 0; bY < ChunkSizeY; bY++){ for (int bZ = 0; bZ < ChunkSizeZ; bZ++){ BlockData block = chunkToSend.Blocks[bX][bY][bZ]; Serial.write((uint8_t*)&block.pX, 1); Serial.write((uint8_t*)&block.pY, 1); Serial.write((uint8_t*)&block.pZ, 1); if (!Debug){ Serial.write((uint8_t*)&block.blockType, 1); } else { Serial.write((uint8_t*)&DebugBlockType, 1); }; }; }; }; }; bool WorldRendered = false; void loop() { // put your main code here, to run repeatedly: if (!WorldRendered){ for (int Wx = 0; Wx < WorldSizeX; Wx++){ for (int Wz = 0; Wz < WorldSizeZ; Wz++){ ChunkData chunk = generateChunk(Wx*ChunkSizeX, Wz*ChunkSizeZ); SendChunkUpdate(chunk); } } WorldRendered = true; }; }