Kolja Strohm před 1 rokem
rodič
revize
851941d5f9

+ 13 - 1
FactoryCraft/Block.cpp

@@ -189,9 +189,21 @@ Framework::Text Block::getTargetUIML()
         ->getTargetUIML();
 }
 
-void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
+void Block::sendModelInfo(NetworkMessage* zMessage) {
+    // overwritten by some blocks
+}
+
+void Block::api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
 {
     // TODO: answer api requests
+    char id = 0;
+    zRequest->lese(&id, 1);
+    switch (id)
+    {
+    case 0:
+        // request model state
+        sendModelInfo(zResponse);
+    }
 }
 
 bool Block::isTickSource() const

+ 1 - 1
FactoryCraft/Block.h

@@ -91,7 +91,7 @@ public:
     virtual void setNeighbourBlock(Direction dir, Block* zN);
     virtual void setNeighbourType(Direction dir, int type);
     virtual Framework::Text getTargetUIML();
-
+    virtual void sendModelInfo(NetworkMessage* zMessage);
     void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
 
     bool isTickSource() const;

+ 12 - 0
FactoryCraft/Dimension.cpp

@@ -70,7 +70,19 @@ void Dimension::api(Framework::InMemoryBuffer* zRequest,
             cs.unlock();
             break;
         }
+    case 1: // block message
+        Vec3<int> location;
+        zRequest->lese((char*)&location.x, 4);
+        zRequest->lese((char*)&location.y, 4);
+        zRequest->lese((char*)&location.z, 4);
+        Framework::Either<Block*, int> block = zBlock(location);
+        if (block.isA())
+        {
+            block.getA()->api(zRequest, zResponse);
+        }
+        break;
     }
+    
 }
 
 void Dimension::tickEntities()

+ 80 - 76
FactoryCraft/FluidBlock.cpp

@@ -23,36 +23,35 @@ bool FluidBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
 {
     nextFlow = 0;
     int bottom = neighbourTypes[getDirectionIndex(Direction::BOTTOM)];
-    if (bottom != zBlockType()->getId())
+    if (bottom == BlockTypeEnum::AIR)
     {
-        if (bottom == BlockTypeEnum::AIR)
-        {
-            nextFlow |= 1 << getDirectionIndex(Direction::BOTTOM);
-        }
-        else
+        nextFlow |= 1 << getDirectionIndex(Direction::BOTTOM);
+    }
+    else
+    {
+        if (zNeighbours[getDirectionIndex(Direction::BOTTOM)]
+            && bottom == zBlockType()->getId())
         {
-            if (zNeighbours[getDirectionIndex(Direction::BOTTOM)]
-                && zNeighbours[getDirectionIndex(Direction::BOTTOM)]
-                           ->zBlockType()
-                           ->getId()
-                       == typeId)
+            Inventory* array[2]
+                = {zNeighbours[getDirectionIndex(Direction::BOTTOM)], this};
+            MultipleInventoryLock lock(array, 2);
+            FluidBlock* below = dynamic_cast<FluidBlock*>(
+                zNeighbours[getDirectionIndex(Direction::BOTTOM)]);
+            if (below->fluidAmount < 1000)
             {
-                Inventory* array[2]
-                    = {zNeighbours[getDirectionIndex(Direction::BOTTOM)], this};
-                MultipleInventoryLock lock(array, 2);
-                FluidBlock* below = dynamic_cast<FluidBlock*>(
-                    zNeighbours[getDirectionIndex(Direction::BOTTOM)]);
-                if (below->fluidAmount < 1000)
-                {
-                    short transferAmount
-                        = (short)MIN(this->fluidAmount, (short)1000 - below->fluidAmount);
-                    below->fluidAmount = (short)(below->fluidAmount + transferAmount);
-                    this->fluidAmount = (short)(this->fluidAmount - transferAmount);
-                }
+                short transferAmount = (short)MIN(
+                    this->fluidAmount, (short)1000 - below->fluidAmount);
+                below->fluidAmount
+                    = (short)(below->fluidAmount + transferAmount);
+                this->fluidAmount = (short)(this->fluidAmount - transferAmount);
             }
-            short neighborCount = 0;
-            Inventory* others[4];
-            for (int i = 1; i < 5; i++)
+        }
+        short neighborCount = 0;
+        Inventory* others[4];
+        for (int i = 0; i < 6; i++)
+        {
+            if (getDirectionFromIndex(i) != Direction::BOTTOM
+                && getDirectionFromIndex(i) != Direction::TOP)
             {
                 if (neighbourTypes[i] == typeId && zNeighbours[i])
                 {
@@ -60,60 +59,60 @@ bool FluidBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
                     neighborCount++;
                 }
             }
-            if (neighborCount > 0)
+        }
+        if (neighborCount > 0)
+        {
+            Inventory* array[5]
+                = {this, others[0], others[1], others[2], others[3]};
+            MultipleInventoryLock lock(array, neighborCount + 1);
+            // all neighbot fluid blocks are locked now
+            FluidBlock* otherFluids[4];
+            for (int i = 0; i < neighborCount; i++)
+            {
+                otherFluids[i] = dynamic_cast<FluidBlock*>(others[i]);
+            }
+            // order other fluids increasing by fluid amount
+            sortByAmound(otherFluids, neighborCount);
+            short distCount = 0;
+            short targetAmount = 0;
+            for (int i = 1; i <= neighborCount; i++)
             {
-                Inventory* array[5]
-                    = {this, others[0], others[1], others[2], others[3]};
-                MultipleInventoryLock lock(array, neighborCount + 1);
-                // all neighbot fluid blocks are locked now
-                FluidBlock* otherFluids[4];
-                for (int i = 0; i < neighborCount; i++)
+                int fluidAmount = this->fluidAmount;
+                for (int j = 0; j < i; j++)
                 {
-                    otherFluids[i] = dynamic_cast<FluidBlock*>(others[i]);
+                    fluidAmount += otherFluids[j]->fluidAmount;
                 }
-                // order other fluids increasing by fluid amount
-                sortByAmound(otherFluids, neighborCount);
-                short distCount = 0;
-                short targetAmount = 0;
-                for (int i = 1; i <= neighborCount; i++)
+                if (fluidAmount / (i + 1) < this->fluidAmount)
                 {
-                    int fluidAmount = this->fluidAmount;
-                    for (int j = 0; j < i; j++)
-                    {
-                        fluidAmount += otherFluids[j]->fluidAmount;
-                    }
-                    if (fluidAmount / (i + 1) > this->fluidAmount)
-                    {
-                        targetAmount = (short)(fluidAmount / (i + 1));
-                        distCount = (short)i;
-                    }
-                    else
-                    {
-                        break;
-                    }
+                    targetAmount = (short)(fluidAmount / (i + 1));
+                    distCount = (short)i;
                 }
-                for (int i = 0; i < distCount; i++)
+                else
                 {
-                    short transferAmount
-                        = (short)(targetAmount - otherFluids[i]->fluidAmount);
-                    otherFluids[i]->fluidAmount
-                        = (short)(otherFluids[i]->fluidAmount + transferAmount);
-                    this->fluidAmount = (short)(this->fluidAmount - transferAmount);
+                    break;
                 }
-            } // unlock
-            neighborCount = 0;
-            for (int i = 0; i < 6; i++)
+            }
+            for (int i = 0; i < distCount; i++)
+            {
+                short transferAmount
+                    = (short)(targetAmount - otherFluids[i]->fluidAmount);
+                otherFluids[i]->fluidAmount
+                    = (short)(otherFluids[i]->fluidAmount + transferAmount);
+                this->fluidAmount = (short)(this->fluidAmount - transferAmount);
+            }
+        } // unlock
+        neighborCount = 0;
+        for (int i = 0; i < 6; i++)
+        {
+            if (getDirectionFromIndex(i) != Direction::BOTTOM
+                && getDirectionFromIndex(i) != Direction::TOP)
             {
-                if (getDirectionFromIndex(i) != Direction::BOTTOM
-                    && getDirectionFromIndex(i) != Direction::TOP)
+                int neighbor = neighbourTypes[i];
+                if (neighbor == BlockTypeEnum::AIR
+                    && fluidAmount > neighborCount + 1)
                 {
-                    int neighbor = neighbourTypes[i];
-                    if (neighbor == BlockTypeEnum::AIR
-                        && fluidAmount > neighborCount + 1)
-                    {
-                        nextFlow |= 1 << i;
-                        neighborCount++;
-                    }
+                    nextFlow |= 1 << i;
+                    neighborCount++;
                 }
             }
         }
@@ -121,17 +120,22 @@ bool FluidBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
     return 1;
 }
 
+void FluidBlock::sendModelInfo(NetworkMessage* zMessage)
+{
+    zMessage->addressBlock(this);
+    char* msg = new char[3];
+    msg[0] = 2; // fluid amount change
+    *(short*)(msg + 1) = fluidAmount;
+    zMessage->setMessage(msg, 3);
+}
+
 void FluidBlock::broadcastAmount()
 {
     if (lastBroadcastedAmount != fluidAmount)
     {
         lastBroadcastedAmount = fluidAmount;
         NetworkMessage* changeMsg = new NetworkMessage();
-        changeMsg->addressBlock(this);
-        char* msg = new char[3];
-        msg[0] = 2; // fluid amount change
-        *(short*)(msg + 1) = fluidAmount;
-        changeMsg->setMessage(msg, 3);
+        sendModelInfo(changeMsg);
         Game::INSTANCE->broadcastMessage(changeMsg);
     }
 }

+ 1 - 0
FactoryCraft/FluidBlock.h

@@ -23,6 +23,7 @@ public:
     FluidBlock(int typeId, Framework::Vec3<int> pos);
     virtual ~FluidBlock();
 
+    virtual void sendModelInfo(NetworkMessage* zMessage) override;
     static void sortByAmound(FluidBlock** array, int count);
 
     friend FluidBlockType;

+ 24 - 4
FactoryCraft/GrowingPlant.cpp

@@ -36,8 +36,7 @@ GrowingPlantBlock::GrowingPlantBlock(int typeId,
       name(name),
       states(),
       blockTypeAfterGrowth(blockTypeAfterGrowth),
-      plantSpawned(0),
-      modelUpdated(0)
+      plantSpawned(0)
 {
     tickSource = 1;
 }
@@ -53,11 +52,10 @@ bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
     }
     for (const GrowthState& state : states)
     {
-        if ((state.percentage > beforePercentage || !modelUpdated)
+        if ((state.percentage > beforePercentage)
             && state.percentage <= seblingTicks / (float)seblingTicksMax)
         {
             updateModel(state.model);
-            modelUpdated = 1;
         }
     }
     return 1;
@@ -75,6 +73,28 @@ void GrowingPlantBlock::onPostTick()
     }
 }
 
+void GrowingPlantBlock::sendModelInfo(NetworkMessage* zMessage)
+{
+    const GrowthState* current = 0;
+    for (const GrowthState& state : states)
+    {
+        if (state.percentage <= seblingTicks / (float)seblingTicksMax)
+        {
+            current = &state;
+        }
+    }
+    if (current)
+    {
+        zMessage->addressBlock(this);
+        InMemoryBuffer buffer;
+        current->model.writeTo(&buffer);
+        char* msg = new char[(int)buffer.getSize() + 1];
+        msg[0] = 1; // hmodel change
+        buffer.lese(msg + 1, (int)buffer.getSize());
+        zMessage->setMessage(msg, (int)buffer.getSize() + 1);
+    }
+}
+
 Framework::Text GrowingPlantBlock::getTargetUIML()
 {
     return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name

+ 1 - 1
FactoryCraft/GrowingPlant.h

@@ -26,7 +26,6 @@ private:
     Framework::Array<GrowthState> states;
     int blockTypeAfterGrowth;
     bool plantSpawned;
-    bool modelUpdated;
 
 public:
     GrowingPlantBlock(
@@ -39,6 +38,7 @@ public:
     virtual bool onTick(
         TickQueue* zQueue, int numTicks, bool& blocked) override;
     virtual void onPostTick() override;
+    virtual void sendModelInfo(NetworkMessage* zMessage) override;
     virtual Framework::Text getTargetUIML();
     GrowingPlantBlock* addGrowthState(GrowthState state);
 

+ 4 - 1
FactoryCraft/StaticInitializerOrder.cpp

@@ -255,7 +255,10 @@ void initializeBlockTypes()
          },
          "Wheat"))
         ->initializeDefault();
-    new FluidBlockType(BlockTypeEnum::WATER, ModelInfo("fluid", "fluids.ltdb/water.png", 6), "Water");
+    (new FluidBlockType(BlockTypeEnum::WATER,
+        ModelInfo("fluid", "fluids.ltdb/water.png", 6),
+        "Water"))
+        ->initializeDefault();
 }
 
 void initializeItemTypes()