123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #include <Network.h>
- #include <Welt3D.h>
- #include <GraphicsApi.h>
- #include <iostream>
- #include "World.h"
- #include "Globals.h"
- #include "WorldUpdate.h"
- #include "Constants.h"
- #include "Registries.h"
- #include "Game.h"
- #include <AsynchronCall.h>
- using namespace Network;
- using namespace Framework;
- World::World(Bildschirm3D* zScreen)
- : Thread()
- {
- renderedWorld = new Welt3D();
- renderedWorld->addDiffuseLight(DiffuseLight{ Vec3<float>(0.5f, 0.5f, -1.f), Vec3<float>(1.f, 1.f, 1.f) });
- currentDimension = new Dimension();
- zScreenPtr = zScreen;
- kam = new PlayerKam(zScreen);
- kam->setWelt(renderedWorld);
- zScreen->addKamera(kam);
- firstMessage = 1;
- ownEntityId = -1;
- currentTarget = 0;
- start();
- }
- World::~World()
- {
- zScreenPtr->removeKamera(kam);
- currentDimension->release();
- if (currentTarget)
- currentTarget->release();
- }
- void World::update(bool background)
- {
- NetworkReader* serverMessageReader = 0;
- unsigned char type = 0;
- while (background ? serverMessageReader = network->zFactoryClient()->getNextBackgroundMessage() : serverMessageReader = network->zFactoryClient()->getNextForegroundMessage())
- {
- serverMessageReader->lese((char*)&type, 1);
- if (type == 2) // WORLD UPDATE
- {
- int id = 0;
- serverMessageReader->lese((char*)&id, 4);
- STATIC_REGISTRY(WorldUpdateType).zElement(id)->applyUpdateAndCheck(serverMessageReader);
- }
- if (type == 3) // API MESSAGE
- {
- int length;
- serverMessageReader->lese((char*)&length, 4);
- char* data = new char[length];
- serverMessageReader->lese(data, length);
- switch (data[0])
- {
- case 1: // dimension message
- {
- currentDimension->api(data + 1);
- break;
- }
- case 2: // gui message
- ((Game*)(Menu*)menuRegister->get("game"))->api(data + 1);
- break;
- case 3: // set target
- {
- switch (data[1])
- {
- case 0:
- setTarget(0);
- break;
- case 1:
- setTarget(zEntity(*(int*)(data + 2)));
- break;
- case 2:
- setTarget(zBlockAt(Vec3<int>(*(int*)(data + 2), *(int*)(data + 6), *(int*)(data + 10))));
- break;
- }
- break;
- }
- }
- delete[] data;
- // TODO: process messages
- }
- if (type == 4) // POSITION UPDATE
- {
- serverMessageReader->lese((char*)&ownEntityId, 4);
- kam->setEntityId(ownEntityId);
- Entity* p = zEntity(ownEntityId);
- if (p)
- {
- p->setPlayerControlled();
- }
- }
- network->zFactoryClient()->endMessageReading(background);
- }
- network->zFactoryClient()->endMessageReading(background);
- Entity* player = getCurrentPlayerEntity();
- if (player)
- {
- renderedWorld->lock();
- currentDimension->removeDistantChunks({ (int)player->getPos().x, (int)player->getPos().y });
- Punkt currentChunk = getChunkCenter((int)player->getX(), (int)player->getY());
- for (int x = 0; x <= CHUNK_VISIBILITY_RANGE; x++)
- {
- for (int y = 0; y <= CHUNK_VISIBILITY_RANGE; y++)
- {
- std::function<void(Punkt)> requestChunk = [this](Punkt center)
- {
- Chunk* zC = currentDimension->zChunk(center);
- if (!zC)
- {
- char msg[1];
- msg[0] = 0; // add observer and request chaunk data
- Punkt pos = center;
- subLock.lock();
- bool found = 0;
- for (Punkt p : subscriptions)
- {
- if (p == pos)
- {
- found = 1;
- break;
- }
- }
- if (!found)
- {
- network->zFactoryClient()->chunkAPIRequest(pos, msg, 1);
- subscriptions.add(pos);
- }
- subLock.unlock();
- }
- };
- requestChunk(currentChunk + Punkt(x * CHUNK_SIZE, y * CHUNK_SIZE));
- if (y > 0)
- requestChunk(currentChunk + Punkt(x * CHUNK_SIZE, -y * CHUNK_SIZE));
- if (x > 0)
- {
- requestChunk(currentChunk + Punkt(-x * CHUNK_SIZE, y * CHUNK_SIZE));
- if (y > 0)
- requestChunk(currentChunk + Punkt(-x * CHUNK_SIZE, -y * CHUNK_SIZE));
- }
- }
- }
- renderedWorld->unlock();
- }
- }
- void World::setChunk(Chunk* chunk)
- {
- zScreenPtr->lock();
- renderedWorld->lock();
- currentDimension->setChunk(chunk, chunk->getCenter());
- renderedWorld->unlock();
- zScreenPtr->unlock();
- }
- void World::thread()
- {
- new AsynchronCall("World Update", [this]()
- {
- while (true)
- {
- zScreenPtr->lock();
- if (currentGame != this)
- {
- zScreenPtr->unlock();
- return;
- }
- zScreenPtr->unlock();
- update(0);
- Sleep(10);
- }
- });
- while (true)
- {
- zScreenPtr->lock();
- if (currentGame != this)
- {
- zScreenPtr->unlock();
- return;
- }
- zScreenPtr->unlock();
- update(1);
- Sleep(10);
- }
- }
- Block* World::zBlockAt(Framework::Vec3<int> location) const
- {
- return currentDimension->zBlock(location);
- return 0;
- }
- Block* World::getBlockAt(Framework::Vec3<int> location) const
- {
- return currentDimension->getBlock(location);
- return 0;
- }
- Dimension* World::zDimension() const
- {
- return currentDimension;
- }
- void World::setVisibility(Chunk* zChunk, bool visible)
- {
- renderedWorld->lock();
- if (visible)
- renderedWorld->addCollection(dynamic_cast<Framework::Model3DCollection*>(zChunk->getThis()));
- else
- renderedWorld->removeCollection(zChunk);
- renderedWorld->unlock();
- }
- void World::setVisibility(Entity* zEntity, bool visible)
- {
- renderedWorld->lock();
- if (visible)
- renderedWorld->addZeichnung(dynamic_cast<Framework::Model3D*>(zEntity->getThis()));
- else
- renderedWorld->removeZeichnung(zEntity);
- renderedWorld->unlock();
- }
- Framework::Punkt World::getChunkCenter(int x, int y) const
- {
- return Punkt(((x < 0 ? x + 1 : x) / CHUNK_SIZE) * CHUNK_SIZE + (x < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2, ((y < 0 ? y + 1 : y) / CHUNK_SIZE) * CHUNK_SIZE + (y < 0 ? -CHUNK_SIZE : CHUNK_SIZE) / 2);
- }
- Entity* World::zEntity(int id) const
- {
- Entity* e = currentDimension->zEntity(id);
- if (e)
- return e;
- return 0;
- }
- Entity* World::getEntity(int id) const
- {
- Entity* e = currentDimension->getEntity(id);
- if (e)
- return e;
- return 0;
- }
- void World::removeEntity(int id)
- {
- currentDimension->removeEntity(id);
- }
- PlayerKam* World::zKamera() const
- {
- return kam;
- }
- int World::getCurrentPlayerId() const
- {
- return ownEntityId;
- }
- Entity* World::getCurrentPlayerEntity() const
- {
- return zEntity(ownEntityId);
- }
- void World::setTarget(Framework::Model3D* zTarget)
- {
- if (zTarget != currentTarget)
- {
- targetLock.lock();
- if (currentTarget)
- {
- currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() - 0.2f);
- currentTarget->release();
- currentTarget = 0;
- }
- if (zTarget)
- {
- currentTarget = dynamic_cast<Framework::Model3D*>(zTarget->getThis());
- if (currentTarget)
- currentTarget->setAmbientFactor(currentTarget->getAmbientFactor() + 0.2f);
- }
- targetLock.unlock();
- }
- }
- void World::lockWorld()
- {
- renderedWorld->lock();
- }
- void World::unlockWorld()
- {
- renderedWorld->unlock();
- }
- void World::onChunkAdded(Punkt pos)
- {
- subLock.lock();
- int index = 0;
- for (Punkt p : subscriptions)
- {
- if (p == pos)
- {
- subscriptions.remove(index);
- break;
- }
- index++;
- }
- subLock.unlock();
- }
- Framework::Model3D* World::getCurrentTarget() const
- {
- while (targetLock.isLocked())
- Sleep(1);
- return currentTarget ? dynamic_cast<Framework::Model3D * >(currentTarget->getThis()) : 0;
- }
|