|
@@ -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);
|
|
|
}
|
|
|
}
|