void SocketStorage::PlaceBlock(char type, Vector3 chunkIndex, Vector3 blockIndex) { Chunk *chunk = Loaded[chunkIndex]; if (chunk == NULL) return; // TODO: this *will* segfault on unloaded chunks! Vector3 absolute = chunk->GetWorldPos(blockIndex); Block *block = chunk->GetBlock(blockIndex); chunk->PlaceBlock(type, blockIndex); if (block->Type != type) context->socket->SendBlock(type, chunkIndex, blockIndex); }
void Terrain::PlaceBlock(char type, Vector3 chunkIndex, Vector3 blockIndex) { // chunk isn't loaded, don't bother. TODO: doublecheck if (!contains(RequestedChunks, chunkIndex)) return; Chunk *chunk = LoadedChunks[chunkIndex]; Vector3 absolute = chunk->GetWorldPos(blockIndex); Block *block = chunk->GetBlock(blockIndex); if (block->Type > 0 && block->faces != 0) { for (std::list<PositionedBlock*>::iterator it = VisibleBlocks.begin(); it != VisibleBlocks.end(); ++it) { if ((*it)->pos == absolute) { VisibleBlocks.remove(*it); break; } } } Storage->PlaceBlock(type, chunkIndex, blockIndex); block = chunk->PlaceBlock(type, blockIndex); // TODO: handle placing blocks without just counting on a [reliable] glitch! Block *blk = NULL; if (blockIndex.X > 0 && chunk->GetBlock(blockIndex - Vector3(1, 0, 0))->Type > 0) { blk = chunk->GetBlock(blockIndex - Vector3(1, 0, 0)); if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute - Vector3(1, 0, 0), 1)); blk->faces |= 0x08; } if (blockIndex.Y > 0 && chunk->GetBlock(blockIndex - Vector3(0, 1, 0))->Type > 0) { blk = chunk->GetBlock(blockIndex - Vector3(0, 1, 0)); if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute - Vector3(0, 1, 0), 1)); blk->faces |= 0x10; } if (blockIndex.Z > 0 && chunk->GetBlock(blockIndex - Vector3(0, 0, 1))->Type > 0) { blk = chunk->GetBlock(blockIndex - Vector3(0, 0, 1)); if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute - Vector3(0, 0, 1), 1)); blk->faces |= 0x20; } int Upper = chunk->SideLength - 1; if (blockIndex.X < Upper && chunk->GetBlock(blockIndex + Vector3(1, 0, 0))->Type > 0) { blk = chunk->GetBlock(blockIndex + Vector3(1, 0, 0)); if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute + Vector3(1, 0, 0), 1)); blk->faces |= 0x01; } if (blockIndex.Y < Upper && chunk->GetBlock(blockIndex + Vector3(0, 1, 0))->Type > 0) { blk = chunk->GetBlock(blockIndex + Vector3(0, 1, 0)); if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute + Vector3(0, 1, 0), 1)); blk->faces |= 0x02; } if (blockIndex.Z < Upper && chunk->GetBlock(blockIndex + Vector3(0, 0, 1))->Type > 0) { blk = chunk->GetBlock(blockIndex + Vector3(0, 0, 1)); if (blk->faces == 0) VisibleBlocks.push_back(new PositionedBlock(blk, absolute + Vector3(0, 0, 1), 1)); blk->faces |= 0x04; } // TODO: do faces calc on new block if (type > 0) // && blk->faces != 0) VisibleBlocks.push_back(new PositionedBlock(block, absolute, 1)); }