Skip to content
This repository has been archived by the owner on Nov 28, 2021. It is now read-only.

Commit

Permalink
Fix destroy segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed Apr 20, 2011
1 parent 8441db5 commit bcc3cd1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/client/socketstorage.h
Expand Up @@ -13,7 +13,7 @@ class SocketStorage : public WorldStorage
SocketStorage(Terrain *Callback, Context *context) : WorldStorage(Callback), context(context) {};

Chunk *RequestChunk(Vector3 Index);
Chunk ReadChunk(sf::Packet &Packet);
Chunk *ReadChunk(sf::Packet &Packet);

private:
//void SendRequest(Vector3 Index);
Expand Down
2 changes: 1 addition & 1 deletion include/common/terrain.h
Expand Up @@ -43,7 +43,7 @@ class Terrain
std::vector<Vector3> RequestedChunks;

Chunk *GetChunk(Vector3 index);
void LoadChunk(Chunk chunk);
void LoadChunk(Chunk *chunk);
void HandleRequests(Vector3 Pos);
void RequestChunk(Vector3 index);

Expand Down
9 changes: 5 additions & 4 deletions src/client/socketstorage.cpp
Expand Up @@ -10,6 +10,7 @@ Chunk *SocketStorage::RequestChunk(Vector3 Index) {

return NULL;
}
#include <cstdio>

// TODO: goes elsewhere?
Block *MakeBlock2(char type) {
Expand All @@ -21,12 +22,12 @@ Block *MakeBlock2(char type) {

//void LoadChunk(Vector3 index, Chunk chunk);

Chunk SocketStorage::ReadChunk(sf::Packet &Packet) {
Chunk *SocketStorage::ReadChunk(sf::Packet &Packet) {
int BlockCount;
Vector3 ChunkIndex;
Packet >> ChunkIndex >> BlockCount;

Chunk chunk(ChunkIndex, 16);
Chunk *chunk = new Chunk(ChunkIndex, 16);

sf::Uint8 type;
Block *block;
Expand All @@ -36,10 +37,10 @@ Chunk SocketStorage::ReadChunk(sf::Packet &Packet) {
for (int i = 0; i < BlockCount; i++) {
Packet >> type >> Pos;

chunk.Blocks[Vector3(Pos)] = MakeBlock2(type);
chunk->Blocks[Vector3(Pos)] = MakeBlock2(type);
}

Loaded[ChunkIndex] = &chunk;
Loaded[ChunkIndex] = chunk;
Callback->LoadChunk(chunk);

return chunk;
Expand Down
2 changes: 2 additions & 0 deletions src/common/chunk.cpp
Expand Up @@ -91,6 +91,8 @@ Block *Chunk::PlaceBlock(char type, Vector3 index) {
break;
}
}*/
printf("(%f,\t%f,\t%f):\t%i\n", index.X, index.Y, index.Z, type);
printf("%i\n", Blocks.size());

Block *block = MakeBlock(type);
Blocks[index] = block;
Expand Down
24 changes: 14 additions & 10 deletions src/common/terrain.cpp
Expand Up @@ -81,7 +81,11 @@ void Terrain::DestroyBlock(PositionedBlock *block) {
//Socket.Send(Packet);
}

#include <cstdio>

void Terrain::PlaceBlock(char type, Vector3 chunkIndex, Vector3 blockIndex) {
printf("(%f,\t%f,\t%f) \t/ (%f,\t%f,\t%f):\t%i\n", chunkIndex.X, chunkIndex.Y, chunkIndex.Z, blockIndex.X, blockIndex.Y, blockIndex.Z, type);

// TODO: check if the block is visible before doing this loop
// TODO: handle variable chunk sizes
Vector3 absolute = (chunkIndex * 16) + blockIndex;
Expand Down Expand Up @@ -161,44 +165,44 @@ Chunk *Terrain::GetChunk(Vector3 index) {
//}
}

void Terrain::LoadChunk(Chunk chunk) {
void Terrain::LoadChunk(Chunk *chunk) {
sf::Uint8 type;
Block *block;
Vector3 Pos;

for (std::map<Vector3, Block*>::iterator it = chunk.Blocks.begin(); it != chunk.Blocks.end(); ++it) {
for (std::map<Vector3, Block*>::iterator it = chunk->Blocks.begin(); it != chunk->Blocks.end(); ++it) {
if (it->second->Type == 0) continue;
Pos = it->first;

char sides = 0x3F;

if (Pos.X > 0 && chunk.GetBlock(Pos - Vector3(1, 0, 0))->Type > 0)
if (Pos.X > 0 && chunk->GetBlock(Pos - Vector3(1, 0, 0))->Type > 0)
sides &= (0xFE); // 0b00000001

if (Pos.Y > 0 && chunk.GetBlock(Pos - Vector3(0, 1, 0))->Type > 0)
if (Pos.Y > 0 && chunk->GetBlock(Pos - Vector3(0, 1, 0))->Type > 0)
sides &= (0xFD); // 0b00000010

if (Pos.Z > 0 && chunk.GetBlock(Pos - Vector3(0, 0, 1))->Type > 0)
if (Pos.Z > 0 && chunk->GetBlock(Pos - Vector3(0, 0, 1))->Type > 0)
sides &= (0xFB); // 0b00000100

// TODO: handle variable chunk sizes

if (Pos.X < 15 && chunk.GetBlock(Pos + Vector3(1, 0, 0))->Type > 0)
if (Pos.X < 15 && chunk->GetBlock(Pos + Vector3(1, 0, 0))->Type > 0)
sides &= (0xF7); // 0b00001000

if (Pos.Y < 15 && chunk.GetBlock(Pos + Vector3(0, 1, 0))->Type > 0)
if (Pos.Y < 15 && chunk->GetBlock(Pos + Vector3(0, 1, 0))->Type > 0)
sides &= (0xEF); // 0b00010000

if (Pos.Z < 15 && chunk.GetBlock(Pos + Vector3(0, 0, 1))->Type > 0)
if (Pos.Z < 15 && chunk->GetBlock(Pos + Vector3(0, 0, 1))->Type > 0)
sides &= (0xDF); // 0b00100000

it->second->faces = sides;

if (sides > 0)
VisibleBlocks.push_back(new PositionedBlock(it->second, (chunk.Offset * 16) + Pos, 1)); // TODO: use helper
VisibleBlocks.push_back(new PositionedBlock(it->second, (chunk->Offset * 16) + Pos, 1)); // TODO: use helper
}

LoadedChunks[chunk.Offset] = &chunk;
LoadedChunks[chunk->Offset] = chunk;
}

void Terrain::HandleRequests(Vector3 Pos) {
Expand Down

0 comments on commit bcc3cd1

Please sign in to comment.