123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- #include "Chunk.h"
- #include "Constants.h"
- #include "Globals.h"
- #include "Registries.h"
- Chunk::Chunk(Framework::Punkt location)
- : ReferenceCounter(),
- location(location),
- isLoading(0)
- {}
- Chunk::Chunk(Framework::Punkt location, Framework::StreamReader* zReader)
- : Chunk(location)
- {
- load(zReader);
- }
- Chunk::~Chunk()
- {
- char msg = 1; // remove observer
- network->zFactoryClient()->chunkAPIRequest(location, &msg, 1);
- }
- void Chunk::api(char* message)
- {
- switch (message[0])
- {
- case 0: // set block
- {
- int index = *(int*)(message + 1);
- int id = *(int*)(message + 5);
- Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE, (index / WORLD_HEIGHT) % CHUNK_SIZE, index % WORLD_HEIGHT);
- location.x += this->location.x - CHUNK_SIZE / 2;
- location.y += this->location.y - CHUNK_SIZE / 2;
- if (blockTypes[id]->doesNeedInstance())
- {
- Block* zB = blockTypes[id]->createBlock(location);
- setBlock(zB);
- }
- else
- {
- Block* zB = zBlockAt(location);
- if (zB)
- removeBlock(zB);
- }
- break;
- }
- case 1: // update light
- {
- int index = *(int*)(message + 1);
- Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE, (index / WORLD_HEIGHT) % CHUNK_SIZE, index % WORLD_HEIGHT);
- for (int i = 0; i < 6; i++)
- {
- Framework::Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
- if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
- {
- if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0 && pos.y < CHUNK_SIZE)
- {
- pos.x += this->location.x - CHUNK_SIZE / 2;
- pos.y += this->location.y - CHUNK_SIZE / 2;
- Block* zB = zBlockAt(pos);
- if (zB)
- {
- bool visible = zB->isVisible();
- zB->setLightData(getOppositeDirection(getDirectionFromIndex(i)), (unsigned char*)(message + 5));
- if (zB->isVisible() != visible)
- {
- vcs.lock();
- if (zB->isVisible())
- visibleBlocks.add(zB);
- else
- {
- for (Framework::Iterator<Block*> iterator = visibleBlocks.begin(); iterator; iterator++)
- {
- if (zB == (Block*)iterator)
- {
- iterator.remove();
- break;
- }
- }
- }
- vcs.unlock();
- }
- }
- }
- else
- {
- pos.x += this->location.x - CHUNK_SIZE / 2;
- pos.y += this->location.y - CHUNK_SIZE / 2;
- Block* zB = currentGame->zBlockAt(pos);
- if (zB)
- {
- bool visible = zB->isVisible();
- zB->setLightData(getOppositeDirection(getDirectionFromIndex(i)), (unsigned char*)(message + 5));
- if (zB->isVisible() != visible)
- {
- Chunk* c = currentGame->zChunk(currentGame->getChunkCenter(pos.x, pos.y));
- c->vcs.lock();
- if (zB->isVisible())
- c->visibleBlocks.add(zB);
- else
- {
- for (Framework::Iterator<Block*> iterator = c->visibleBlocks.begin(); iterator; iterator++)
- {
- if (zB == (Block*)iterator)
- {
- iterator.remove();
- break;
- }
- }
- }
- c->vcs.unlock();
- }
- }
- }
- }
- }
- }
- }
- }
- Block* Chunk::zBlockAt(Framework::Vec3<int> location)
- {
- cs.lock();
- for (Block* b : blocks)
- {
- if (b->getLocation() == location)
- {
- cs.unlock();
- return b;
- }
- }
- cs.unlock();
- return 0;
- }
- void Chunk::setBlock(Block* block)
- {
- cs.lock();
- Framework::Vec3<int> pos = block->getLocation();
- for (Framework::Iterator<Block*> iterator = blocks.begin(); iterator; iterator++)
- {
- if (pos == iterator->getLocation())
- {
- vcs.lock();
- for (Framework::Iterator<Block*> vi = visibleBlocks.begin(); vi; vi++)
- {
- if ((Block*)iterator == (Block*)vi)
- {
- iterator.remove();
- break;
- }
- }
- vcs.unlock();
- iterator->release();
- iterator.set(block);
- break;
- }
- }
- blocks.add(block);
- cs.unlock();
- }
- void Chunk::removeBlock(Block* zBlock)
- {
- cs.lock();
- vcs.lock();
- for (Framework::Iterator<Block*> iterator = visibleBlocks.begin(); iterator; iterator++)
- {
- if (zBlock == (Block*)iterator)
- {
- iterator.remove();
- break;
- }
- }
- vcs.unlock();
- for (Framework::Iterator<Block*> iterator = blocks.begin(); iterator; iterator++)
- {
- if (zBlock == (Block*)iterator)
- {
- iterator.remove();
- break;
- }
- }
- cs.unlock();
- }
- void Chunk::load(Framework::StreamReader* zReader)
- {
- isLoading = 1;
- Framework::Vec3<int> pos = { 0, 0, 0 };
- unsigned short id;
- zReader->lese((char*)&id, 2);
- while (id)
- {
- int index;
- zReader->lese((char*)&index, 4);
- pos = Vec3<int>((index / WORLD_HEIGHT) / CHUNK_SIZE, (index / WORLD_HEIGHT) % CHUNK_SIZE, index % WORLD_HEIGHT);
- if (blockTypes[id]->doesNeedInstance())
- setBlock(blockTypes[id]->createBlock({ pos.x + location.x - CHUNK_SIZE / 2, pos.y + location.y - CHUNK_SIZE / 2, pos.z }));
- zReader->lese((char*)&id, 2);
- }
- int index = 0;
- // light
- zReader->lese((char*)&index, 4);
- char lightData[6];
- while (index >= 0)
- {
- zReader->lese(lightData, 6);
- Framework::Vec3<int> location((index / WORLD_HEIGHT) / CHUNK_SIZE, (index / WORLD_HEIGHT) % CHUNK_SIZE, index % WORLD_HEIGHT);
- for (int i = 0; i < 6; i++)
- {
- Framework::Vec3<int> pos = location + getDirection(getDirectionFromIndex(i));
- if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
- {
- if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0 && pos.y < CHUNK_SIZE)
- {
- pos.x += this->location.x - CHUNK_SIZE / 2;
- pos.y += this->location.y - CHUNK_SIZE / 2;
- Block* zB = zBlockAt(pos);
- if (zB)
- {
- bool visible = zB->isVisible();
- zB->setLightData(getOppositeDirection(getDirectionFromIndex(i)), (unsigned char*)lightData);
- if (zB->isVisible() && !visible)
- visibleBlocks.add(zB);
- }
- }
- else
- {
- pos.x += this->location.x - CHUNK_SIZE / 2;
- pos.y += this->location.y - CHUNK_SIZE / 2;
- Block* zB = currentGame->zBlockAt(pos);
- if (zB)
- {
- bool visible = zB->isVisible();
- zB->setLightData(getOppositeDirection(getDirectionFromIndex(i)), (unsigned char*)lightData);
- if (zB->isVisible() && !visible)
- {
- Chunk* c = currentGame->zChunk(currentGame->getChunkCenter(pos.x, pos.y));
- c->vcs.lock();
- c->visibleBlocks.add(zB);
- c->vcs.unlock();
- }
- }
- }
- }
- }
- zReader->lese((char*)&index, 4);
- }
- isLoading = 0;
- }
- Framework::Punkt Chunk::getCenter() const
- {
- return location;
- }
- Framework::Vec3<int> Chunk::getMin() const
- {
- return { location.x - CHUNK_SIZE / 2, location.y - CHUNK_SIZE / 2, 0 };
- }
- Framework::Vec3<int> Chunk::getMax() const
- {
- return { location.x + CHUNK_SIZE / 2, location.y + CHUNK_SIZE / 2, WORLD_HEIGHT };
- }
- void Chunk::forAll(std::function<void(Model3D*)> f)
- {
- vcs.lock();
- for (Block* b : visibleBlocks)
- f(b);
- vcs.unlock();
- }
|