소스 검색

fix isste with light transmission

Kolja Strohm 2 년 전
부모
커밋
7bdd0a2bc9
6개의 변경된 파일180개의 추가작업 그리고 133개의 파일을 삭제
  1. 3 2
      FactoryCraft/Block.cpp
  2. 123 129
      FactoryCraft/Chunk.cpp
  3. 1 0
      FactoryCraft/Chunk.h
  4. 42 1
      FactoryCraft/Dimension.cpp
  5. 3 0
      FactoryCraft/Dimension.h
  6. 8 1
      FactoryCraft/World.cpp

+ 3 - 2
FactoryCraft/Block.cpp

@@ -162,8 +162,9 @@ void Block::setLightData(Direction dir, unsigned char* data)
 
 bool Block::isVisible() const
 {
-    return sideVisible[0] || sideVisible[1] || sideVisible[2] || sideVisible[3]
-        || sideVisible[4] || sideVisible[5];
+    return true;
+    //return sideVisible[0] || sideVisible[1] || sideVisible[2] || sideVisible[3]
+    //    || sideVisible[4] || sideVisible[5];
 }
 
 Vec3<int> Block::getLocation() const

+ 123 - 129
FactoryCraft/Chunk.cpp

@@ -47,93 +47,6 @@ void Chunk::api(char* message)
             }
             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 = World::INSTANCE->zBlockAt(pos);
-                        if (zB)
-                        {
-                            bool visible = zB->isVisible();
-                            zB->setLightData(
-                                getOppositeDirection(getDirectionFromIndex(i)),
-                                (unsigned char*)(message + 5));
-                            if (zB->isVisible() != visible)
-                            {
-                                Chunk* c = World::INSTANCE->zChunk(
-                                    World::INSTANCE->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();
-                            }
-                        }
-                    }
-                }
-            }
-        }
     }
 }
 
@@ -179,11 +92,13 @@ void Chunk::setBlock(Block* block)
         }
     }
     blocks.add(block);
+    cs.unlock();
+    vcs.lock();
     if (block->isVisible())
     {
         visibleBlocks.add(block);
     }
-    cs.unlock();
+    vcs.unlock();
 }
 
 void Chunk::removeBlock(Block* zBlock)
@@ -239,6 +154,12 @@ void Chunk::load(Framework::StreamReader* zReader)
             blocks.add(b);
             blockCache.set((char*)&index, 4, dynamic_cast<Block*>(b->getThis()));
             cs.unlock();
+            vcs.lock();
+            if (b->isVisible())
+            {
+                visibleBlocks.add(b);
+            }
+            vcs.unlock();
         }
         zReader->lese((char*)&id, 2);
     }
@@ -246,57 +167,109 @@ void Chunk::load(Framework::StreamReader* zReader)
     // light
     zReader->lese((char*)&index, 4);
     char lightData[6];
-    while (index >= 0)
+    while (index >= -1)
     {
-        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++)
+        if (index == -1)
+        {
+            int x = 0;
+            int y = 0;
+            int z = 0;
+            zReader->lese((char*)&x, 4);
+            zReader->lese((char*)&y, 4);
+            zReader->lese((char*)&z, 4);
+            zReader->lese(lightData, 6);
+            if (x == -1)
+            {
+                int cacheIndex
+                    = y * WORLD_HEIGHT + z;
+                Block* zB = blockCache.z((char*)&cacheIndex, 4);
+                if (zB)
+                {
+                    zB->setLightData(WEST, (unsigned char*)lightData);
+                }
+            }
+            else if (y == -1)
+            {
+                int cacheIndex = (x * CHUNK_SIZE) * WORLD_HEIGHT + z;
+                Block* zB = blockCache.z((char*)&cacheIndex, 4);
+                if (zB)
+                {
+                    zB->setLightData(NORTH, (unsigned char*)lightData);
+                }
+            }
+            else if (x == CHUNK_SIZE)
+            {
+                int cacheIndex = ((CHUNK_SIZE - 1) * CHUNK_SIZE + y) * WORLD_HEIGHT + z;
+                Block* zB = blockCache.z((char*)&cacheIndex, 4);
+                if (zB)
+                {
+                    zB->setLightData(EAST, (unsigned char*)lightData);
+                }
+            }
+            else if (y == CHUNK_SIZE)
+            {
+                int cacheIndex
+                    = (x * CHUNK_SIZE + (CHUNK_SIZE - 1)) * WORLD_HEIGHT + z;
+                Block* zB = blockCache.z((char*)&cacheIndex, 4);
+                if (zB)
+                {
+                    zB->setLightData(SOUTH, (unsigned char*)lightData);
+                }
+            }
+        }
+        else
         {
-            Framework::Vec3<int> pos
-                = location + getDirection(getDirectionFromIndex(i));
-            if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
+            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++)
             {
-                if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
-                    && pos.y < CHUNK_SIZE)
+                Framework::Vec3<int> pos
+                    = location + getDirection(getDirectionFromIndex(i));
+                if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
                 {
-                    int cacheIndex
-                        = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT + pos.z;
-                    Block* zB
-                        = blockCache.z((char*)&cacheIndex, 4);
-                    if (zB)
+                    if (pos.x >= 0 && pos.x < CHUNK_SIZE && pos.y >= 0
+                        && pos.y < CHUNK_SIZE)
                     {
-                        bool visible = zB->isVisible();
-                        zB->setLightData(
-                            getOppositeDirection(getDirectionFromIndex(i)),
-                            (unsigned char*)lightData);
-                        if (zB->isVisible() && !visible)
+                        int cacheIndex
+                            = (pos.x * CHUNK_SIZE + pos.y) * WORLD_HEIGHT
+                            + pos.z;
+                        Block* zB = blockCache.z((char*)&cacheIndex, 4);
+                        if (zB)
                         {
-                            vcs.lock();
-                            visibleBlocks.add(zB);
-                            vcs.unlock();
+                            bool visible = zB->isVisible();
+                            zB->setLightData(
+                                getOppositeDirection(getDirectionFromIndex(i)),
+                                (unsigned char*)lightData);
+                            if (zB->isVisible() && !visible)
+                            {
+                                vcs.lock();
+                                visibleBlocks.add(zB);
+                                vcs.unlock();
+                            }
                         }
                     }
-                }
-                else
-                {
-                    pos.x += this->location.x - CHUNK_SIZE / 2;
-                    pos.y += this->location.y - CHUNK_SIZE / 2;
-                    Block* zB = World::INSTANCE->zBlockAt(pos);
-                    if (zB)
+                    else
                     {
-                        bool visible = zB->isVisible();
-                        zB->setLightData(
-                            getOppositeDirection(getDirectionFromIndex(i)),
-                            (unsigned char*)lightData);
-                        if (zB->isVisible() && !visible)
+                        pos.x += this->location.x - CHUNK_SIZE / 2;
+                        pos.y += this->location.y - CHUNK_SIZE / 2;
+                        Block* zB = World::INSTANCE->zBlockAt(pos);
+                        if (zB)
                         {
-                            Chunk* c = World::INSTANCE->zChunk(
-                                World::INSTANCE->getChunkCenter(pos.x, pos.y));
-                            c->vcs.lock();
-                            c->visibleBlocks.add(zB);
-                            c->vcs.unlock();
+                            bool visible = zB->isVisible();
+                            zB->setLightData(
+                                getOppositeDirection(getDirectionFromIndex(i)),
+                                (unsigned char*)lightData);
+                            if (zB->isVisible() && !visible)
+                            {
+                                Chunk* c = World::INSTANCE->zChunk(
+                                    World::INSTANCE->getChunkCenter(
+                                        pos.x, pos.y));
+                                c->vcs.lock();
+                                c->visibleBlocks.add(zB);
+                                c->vcs.unlock();
+                            }
                         }
                     }
                 }
@@ -307,6 +280,27 @@ void Chunk::load(Framework::StreamReader* zReader)
     isLoading = 0;
 }
 
+void Chunk::blockVisibilityChanged(Block* zB)
+{
+    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();
+}
+
 Framework::Punkt Chunk::getCenter() const
 {
     return location;

+ 1 - 0
FactoryCraft/Chunk.h

@@ -30,6 +30,7 @@ public:
     void setBlock(Block* block);
     void removeBlock(Block* zBlock);
     void load(Framework::StreamReader* zReader);
+    void blockVisibilityChanged(Block* zB);
     Framework::Punkt getCenter() const;
     Framework::Vec3<int> getMin() const;
     Framework::Vec3<int> getMax() const;

+ 42 - 1
FactoryCraft/Dimension.cpp

@@ -9,7 +9,8 @@
 using namespace Framework;
 
 Dimension::Dimension()
-    : chunks(new Trie<Chunk>()),
+    : id(-1),
+      chunks(new Trie<Chunk>()),
       entities(new RCArray<Entity>())
 {}
 
@@ -19,6 +20,11 @@ Dimension::~Dimension()
     chunks->release();
 }
 
+void Dimension::setId(int id)
+{
+    this->id = id;
+}
+
 void Dimension::getAddrOf(Punkt cPos, char* addr) const
 {
     *(int*)addr = cPos.x;
@@ -90,6 +96,36 @@ void Dimension::api(char* message)
             World::INSTANCE->onChunkAdded(center);
             break;
         }
+    case 5: // light update
+        {
+            int x = *(int*)(message + 1);
+            int y = *(int*)(message + 5);
+            int z = *(int*)(message + 9);
+            int index = *(int*)(message + 1);
+            Framework::Vec3<int> location(x, y, z);
+            for (int i = 0; i < 6; i++)
+            {
+                Framework::Vec3<int> pos
+                    = location + getDirection(getDirectionFromIndex(i));
+                if (pos.z >= 0 && pos.z < WORLD_HEIGHT)
+                {
+                    Block* zB = zBlock(pos);
+                    if (zB)
+                    {
+                        bool visible = zB->isVisible();
+                        zB->setLightData(
+                            getOppositeDirection(getDirectionFromIndex(i)),
+                            (unsigned char*)(message + 5));
+                        if (zB->isVisible() != visible)
+                        {
+                            zChunk(World::INSTANCE->getChunkCenter(pos.x, pos.y))
+                                ->blockVisibilityChanged(zB);
+                        }
+                    }
+                }
+            }
+            break;
+        }
     }
 }
 
@@ -258,4 +294,9 @@ void Dimension::removeEntity(int id)
     }
     cs.unlock();
     World::INSTANCE->unlockWorld();
+}
+
+int Dimension::getId() const
+{
+    return id;
 }

+ 3 - 0
FactoryCraft/Dimension.h

@@ -12,6 +12,7 @@ class World;
 class Dimension : public virtual Framework::ReferenceCounter
 {
 private:
+    int id;
     Framework::Trie<Chunk>* chunks;
     Framework::Array<Chunk*> chunkList;
     Framework::RCArray<Entity>* entities;
@@ -23,6 +24,7 @@ public:
     Dimension();
     ~Dimension();
 
+    void setId(int id);
     void api(char* message);
     Block* zBlock(Framework::Vec3<int> location);
     Block* getBlock(Framework::Vec3<int> location);
@@ -36,4 +38,5 @@ public:
     Entity* zEntity(int id);
     Entity* getEntity(int id);
     void removeEntity(int id);
+    int getId() const;
 };

+ 8 - 1
FactoryCraft/World.cpp

@@ -70,7 +70,11 @@ void World::update(bool background)
             {
             case 1: // dimension message
                 {
-                    currentDimension->api(data + 1);
+                    int dimId = *(int*)(data + 1);
+                    if (dimId == currentDimension->getId())
+                    {
+                        currentDimension->api(data + 5);
+                    }
                     break;
                 }
             case 2: // gui message
@@ -114,6 +118,9 @@ void World::update(bool background)
         {
             int old = ownEntityId;
             serverMessageReader->lese((char*)&ownEntityId, 4);
+            int dimensionId = 0;
+            serverMessageReader->lese((char*)&dimensionId, 4);
+            currentDimension->setId(dimensionId);
             kam->setEntityId(ownEntityId);
             Entity* p = zEntity(ownEntityId);
             if (p) p->setPlayerControlled();