Ver Fonte

only send block updates to observers of the corresponding chunk

Kolja Strohm há 9 meses atrás
pai
commit
8c998fba42

+ 54 - 9
FactoryCraft/Block.cpp

@@ -77,6 +77,42 @@ void Block::onDestroy()
 
 void Block::onDialogClosed(Text dialogId) {}
 
+void Block::broadcastModelInfoChange()
+{
+    NetworkMessage* message = new NetworkMessage();
+    sendModelInfo(message);
+    broadcastMessage(message);
+}
+
+void Block::broadcastMessage(NetworkMessage* message)
+{
+    if (message->isEmpty())
+    {
+        message->release();
+    }
+    else
+    {
+        Dimension* dim = Game::INSTANCE->zDimension(getDimensionId());
+        if (dim)
+        {
+            Chunk* zChunk
+                = dim->zChunk(Game::getChunkCenter(getPos().x, getPos().y));
+            if (zChunk)
+            {
+                zChunk->notifyObservers(message);
+            }
+            else
+            {
+                message->release();
+            }
+        }
+        else
+        {
+            message->release();
+        }
+    }
+}
+
 void Block::tick(TickQueue* zQueue)
 {
     if (wasTicked) return;
@@ -323,15 +359,24 @@ Block* Block::zNeighbor(Direction dir) const
 
 void Block::updateModel(ModelInfo info) const
 {
-    NetworkMessage* changeMsg = new NetworkMessage();
-    changeMsg->addressBlock(this);
-    InMemoryBuffer buffer;
-    info.writeTo(&buffer);
-    char* msg = new char[(int)buffer.getSize() + 1];
-    msg[0] = 1; // hmodel change
-    buffer.lese(msg + 1, (int)buffer.getSize());
-    changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
-    Game::INSTANCE->broadcastMessage(changeMsg);
+    Dimension* dim = Game::INSTANCE->zDimension(getDimensionId());
+    if (dim)
+    {
+        Chunk* zChunk
+            = dim->zChunk(Game::getChunkCenter(getPos().x, getPos().y));
+        if (zChunk)
+        {
+            NetworkMessage* changeMsg = new NetworkMessage();
+            changeMsg->addressBlock(this);
+            InMemoryBuffer buffer;
+            info.writeTo(&buffer);
+            char* msg = new char[(int)buffer.getSize() + 1];
+            msg[0] = 1; // hmodel change
+            buffer.lese(msg + 1, (int)buffer.getSize());
+            changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
+            zChunk->notifyObservers(changeMsg);
+        }
+    }
 }
 
 int Block::getMapColor() const

+ 2 - 0
FactoryCraft/Block.h

@@ -73,6 +73,8 @@ protected:
     virtual void onDestroy();
 
     virtual void onDialogClosed(Framework::Text dialogId);
+    void broadcastModelInfoChange();
+    void broadcastMessage(NetworkMessage *message);
 
 public:
     Block(int typeId,

+ 2 - 2
FactoryCraft/Chest.cpp

@@ -46,7 +46,7 @@ void Chest::onDialogClosed(Framework::Text dialogId)
             1.0,
             Vec3<float>(0.5f, 0.f, 0.45f),
             Vec3<float>(0.0f, 0.f, 0.f)); // close the chest over one second
-        Game::INSTANCE->broadcastMessage(msg);
+        broadcastMessage(msg);
     }
 }
 
@@ -125,7 +125,7 @@ bool Chest::interact(Item* zItem, Entity* zActor)
             Vec3<float>(0.5f, 0.f, 0.45f),
             Vec3<float>(
                 0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
-        Game::INSTANCE->broadcastMessage(msg);
+        broadcastMessage(msg);
     }
     unlock();
     return false; // item was not changed

+ 13 - 0
FactoryCraft/Chunk.cpp

@@ -678,6 +678,19 @@ void Chunk::sendBlockInfo(Framework::Vec3<int> location)
     message->addressChunck(this);
     message->setMessage(msg, 9);
     notifyObservers(message);
+    if (blocks[index])
+    {
+        NetworkMessage* message = new NetworkMessage();
+        blocks[index]->sendModelInfo(message);
+        if (message->isEmpty())
+        {
+            message->release();
+        }
+        else
+        {
+            notifyObservers(message);
+        }
+    }
     cs.lock();
     for (int i = 0; i < 6; i++)
     {

+ 3 - 10
FactoryCraft/FluidBlock.cpp

@@ -69,23 +69,16 @@ void FluidBlock::sendModelInfo(NetworkMessage* zMessage)
     zMessage->setMessage(msg, 3);
 }
 
-void FluidBlock::broadcastFlow()
-{
-    NetworkMessage* changeMsg = new NetworkMessage();
-    sendModelInfo(changeMsg);
-    Game::INSTANCE->broadcastMessage(changeMsg);
-}
-
 void FluidBlock::doFlow()
 {
     bool doesFlowSidewards = 1;
-    if ((zNeighbours[getDirectionIndex(Direction::BOTTOM)]
+    if (((zNeighbours[getDirectionIndex(Direction::BOTTOM)]
                 && zNeighbours[getDirectionIndex(Direction::BOTTOM)]
                            ->zBlockType()
                        == zBlockType()
             || neighbourTypes[getDirectionIndex(Direction::BOTTOM)]
                    == BlockTypeEnum::AIR)
-        && distanceToSource)
+        && distanceToSource) || distanceToSource >= maxFlowDistance)
     {
         doesFlowSidewards = 0;
     }
@@ -150,7 +143,7 @@ void FluidBlock::doFlow()
     }
     if (changed)
     {
-        broadcastFlow();
+        broadcastModelInfoChange();
         neighborChanged = 1;
         for (int i = 0; i < 6; i++)
         {

+ 0 - 1
FactoryCraft/FluidBlock.h

@@ -19,7 +19,6 @@ protected:
     virtual bool onTick(
         TickQueue* zQueue, int numTicks, bool& blocked) override;
     virtual void onPostTick() override;
-    void broadcastFlow();
     void doFlow();
 
 public:

+ 16 - 4
FactoryCraft/GrowingPlant.cpp

@@ -37,7 +37,8 @@ GrowingPlantBlock::GrowingPlantBlock(int typeId,
       name(name),
       states(),
       blockTypeAfterGrowth(blockTypeAfterGrowth),
-      plantSpawned(0)
+      plantSpawned(0),
+      lastSendState(-1)
 {
     tickSource = 1;
 }
@@ -51,13 +52,24 @@ bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
     {
         Game::INSTANCE->blockTargetChanged(this);
     }
+    int index = 0;
+    int currentIndex = 0;
     for (const GrowthState& state : states)
     {
-        if ((state.percentage > beforePercentage)
-            && state.percentage <= seblingTicks / (float)seblingTicksMax)
+        if (state.percentage <= seblingTicks / (float)seblingTicksMax)
+        {
+            currentIndex = index;
+        }
+        else
         {
-            updateModel(state.model);
+            break;
         }
+        index++;
+    }
+    if (lastSendState != currentIndex)
+    {
+        updateModel(states.get(currentIndex).model);
+        lastSendState = currentIndex;
     }
     return 1;
 }

+ 1 - 0
FactoryCraft/GrowingPlant.h

@@ -25,6 +25,7 @@ private:
     Framework::Array<GrowthState> states;
     int blockTypeAfterGrowth;
     bool plantSpawned;
+    int lastSendState;
 
 public:
     GrowingPlantBlock(int typeId,