Add new flow direction calculating algorithm (#4160)
This commit is contained in:
parent
cdd8e42587
commit
90d552a702
@ -1047,51 +1047,23 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
||||
}
|
||||
|
||||
// Get water direction
|
||||
Direction WaterDir = m_World->GetWaterSimulator()->GetFlowingDirection(BlockX, BlockY, BlockZ);
|
||||
Vector3f WaterDir = m_World->GetWaterSimulator()->GetFlowingDirection(BlockX, BlockY, BlockZ);
|
||||
|
||||
m_WaterSpeed *= 0.9; // Reduce speed each tick
|
||||
|
||||
switch (WaterDir)
|
||||
{
|
||||
case X_PLUS:
|
||||
auto AdjustSpeed = [](double & a_WaterSpeed, float a_WaterDir)
|
||||
{
|
||||
m_WaterSpeed.x = 0.2f;
|
||||
m_bOnGround = false;
|
||||
break;
|
||||
}
|
||||
case X_MINUS:
|
||||
{
|
||||
m_WaterSpeed.x = -0.2f;
|
||||
m_bOnGround = false;
|
||||
break;
|
||||
}
|
||||
case Z_PLUS:
|
||||
{
|
||||
m_WaterSpeed.z = 0.2f;
|
||||
m_bOnGround = false;
|
||||
break;
|
||||
}
|
||||
case Z_MINUS:
|
||||
{
|
||||
m_WaterSpeed.z = -0.2f;
|
||||
m_bOnGround = false;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fabs(m_WaterSpeed.x) < 0.05)
|
||||
{
|
||||
m_WaterSpeed.x = 0;
|
||||
}
|
||||
|
||||
if (fabs(m_WaterSpeed.z) < 0.05)
|
||||
{
|
||||
m_WaterSpeed.z = 0;
|
||||
}
|
||||
if (std::abs(a_WaterDir) > (0.05f / 0.4f))
|
||||
{
|
||||
a_WaterSpeed = 0.4 * a_WaterDir;
|
||||
}
|
||||
else if (std::abs(a_WaterSpeed) < 0.05)
|
||||
{
|
||||
a_WaterSpeed = 0.0;
|
||||
}
|
||||
};
|
||||
AdjustSpeed(m_WaterSpeed.x, WaterDir.x);
|
||||
AdjustSpeed(m_WaterSpeed.z, WaterDir.z);
|
||||
|
||||
NextSpeed += m_WaterSpeed;
|
||||
|
||||
@ -1104,35 +1076,49 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
||||
auto isHit = cLineBlockTracer::FirstSolidHitTrace(*GetWorld(), NextPos, wantNextPos, HitCoords, HitBlockCoords, HitBlockFace);
|
||||
if (isHit)
|
||||
{
|
||||
// Set our position to where the block was hit, minus a bit:
|
||||
// TODO: The real entity's m_Width should be taken into account here
|
||||
NextPos = HitCoords - NextSpeed.NormalizeCopy() * 0.1;
|
||||
if (HitBlockFace == BLOCK_FACE_YP)
|
||||
{
|
||||
// We hit the ground, adjust the position to the top of the block:
|
||||
m_bOnGround = true;
|
||||
NextPos.y = HitBlockCoords.y + 1;
|
||||
}
|
||||
// Set our position to where the block was hit:
|
||||
NextPos = HitCoords;
|
||||
|
||||
// Avoid movement in the direction of the blockface that has been hit:
|
||||
// Avoid movement in the direction of the blockface that has been hit and correct for collision box:
|
||||
double HalfWidth = GetWidth() / 2.0;
|
||||
switch (HitBlockFace)
|
||||
{
|
||||
case BLOCK_FACE_XM:
|
||||
{
|
||||
NextSpeed.x = 0;
|
||||
NextPos.x -= HalfWidth;
|
||||
break;
|
||||
}
|
||||
case BLOCK_FACE_XP:
|
||||
{
|
||||
NextSpeed.x = 0;
|
||||
NextPos.x += HalfWidth;
|
||||
break;
|
||||
}
|
||||
case BLOCK_FACE_YM:
|
||||
{
|
||||
NextSpeed.y = 0;
|
||||
NextPos.y -= GetHeight();
|
||||
break;
|
||||
}
|
||||
case BLOCK_FACE_YP:
|
||||
{
|
||||
NextSpeed.y = 0;
|
||||
// We hit the ground, adjust the position to the top of the block:
|
||||
m_bOnGround = true;
|
||||
NextPos.y = HitBlockCoords.y + 1;
|
||||
break;
|
||||
}
|
||||
case BLOCK_FACE_ZM:
|
||||
{
|
||||
NextSpeed.z = 0;
|
||||
NextPos.z -= HalfWidth;
|
||||
break;
|
||||
}
|
||||
case BLOCK_FACE_ZP:
|
||||
{
|
||||
NextSpeed.z = 0;
|
||||
NextPos.z += HalfWidth;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
@ -128,100 +128,61 @@ bool cFluidSimulator::IsHigherMeta(NIBBLETYPE a_Meta1, NIBBLETYPE a_Meta2)
|
||||
|
||||
|
||||
|
||||
// TODO Not working very well yet :s
|
||||
Direction cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over)
|
||||
Vector3f cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z)
|
||||
{
|
||||
if (!cChunkDef::IsValidHeight(a_Y))
|
||||
{
|
||||
return NONE;
|
||||
}
|
||||
BLOCKTYPE BlockID = m_World.GetBlock(a_X, a_Y, a_Z);
|
||||
if (!IsAllowedBlock(BlockID)) // No Fluid -> No Flowing direction :D
|
||||
{
|
||||
return NONE;
|
||||
return {};
|
||||
}
|
||||
|
||||
/*
|
||||
Disabled because of causing problems and being useless atm
|
||||
char BlockBelow = m_World.GetBlock(a_X, a_Y - 1, a_Z); // If there is nothing or fluid below it -> dominating flow is down :D
|
||||
if ((BlockBelow == E_BLOCK_AIR) || IsAllowedBlock(BlockBelow))
|
||||
if (!IsAllowedBlock(m_World.GetBlock(a_X, a_Y, a_Z))) // No Fluid -> No Flowing direction :D
|
||||
{
|
||||
return Y_MINUS;
|
||||
}
|
||||
*/
|
||||
|
||||
NIBBLETYPE LowestPoint = m_World.GetBlockMeta(a_X, a_Y, a_Z); // Current Block Meta so only lower points will be counted
|
||||
int X = 0, Z = 0; // Lowest Pos will be stored here
|
||||
|
||||
if (IsAllowedBlock(m_World.GetBlock(a_X, a_Y + 1, a_Z)) && a_Over) // check for upper block to flow because this also affects the flowing direction
|
||||
{
|
||||
return GetFlowingDirection(a_X, a_Y + 1, a_Z, false);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector< Vector3i * > Points;
|
||||
|
||||
Points.reserve(4); // Already allocate 4 places :D
|
||||
|
||||
// add blocks around the checking pos
|
||||
Points.push_back(new Vector3i(a_X - 1, a_Y, a_Z));
|
||||
Points.push_back(new Vector3i(a_X + 1, a_Y, a_Z));
|
||||
Points.push_back(new Vector3i(a_X, a_Y, a_Z + 1));
|
||||
Points.push_back(new Vector3i(a_X, a_Y, a_Z - 1));
|
||||
|
||||
for (auto itr = Points.cbegin(), end = Points.cend(); itr != end; ++itr)
|
||||
{
|
||||
Vector3i * Pos = (*itr);
|
||||
auto PosBlockID = m_World.GetBlock(Pos->x, Pos->y, Pos->z);
|
||||
if (IsAllowedBlock(PosBlockID))
|
||||
const auto HeightFromMeta = [](NIBBLETYPE a_BlockMeta) -> NIBBLETYPE
|
||||
{
|
||||
NIBBLETYPE Meta = m_World.GetBlockMeta(Pos->x, Pos->y, Pos->z);
|
||||
// Falling water blocks are always full height (0)
|
||||
return ((a_BlockMeta & 0x08) != 0) ? 0 : a_BlockMeta;
|
||||
};
|
||||
|
||||
if (Meta > LowestPoint)
|
||||
{
|
||||
LowestPoint = Meta;
|
||||
X = Pos->x;
|
||||
Z = Pos->z;
|
||||
}
|
||||
}
|
||||
else if (PosBlockID == E_BLOCK_AIR)
|
||||
auto BlockMeta = m_World.GetBlockMeta(a_X, a_Y, a_Z);
|
||||
NIBBLETYPE CentralPoint = HeightFromMeta(BlockMeta);
|
||||
NIBBLETYPE LevelPoint[4];
|
||||
|
||||
// blocks around the checking pos
|
||||
Vector3i Points[]
|
||||
{
|
||||
{ a_X + 1, a_Y, a_Z },
|
||||
{ a_X, a_Y, a_Z + 1 },
|
||||
{ a_X - 1, a_Y, a_Z },
|
||||
{ a_X, a_Y, a_Z - 1 }
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < ARRAYCOUNT(LevelPoint); i++)
|
||||
{
|
||||
if (IsAllowedBlock(m_World.GetBlock(Points[i])))
|
||||
{
|
||||
LowestPoint = 9; // This always dominates
|
||||
X = Pos->x;
|
||||
Z = Pos->z;
|
||||
|
||||
LevelPoint[i] = HeightFromMeta(m_World.GetBlockMeta(Points[i]));
|
||||
}
|
||||
else
|
||||
{
|
||||
LevelPoint[i] = CentralPoint;
|
||||
}
|
||||
delete Pos;
|
||||
Pos = nullptr;
|
||||
}
|
||||
|
||||
if (LowestPoint == m_World.GetBlockMeta(a_X, a_Y, a_Z))
|
||||
Vector3f Direction;
|
||||
|
||||
// Calculate the flow direction
|
||||
|
||||
Direction.x = (LevelPoint[0] - LevelPoint[2]) / 2.0f;
|
||||
Direction.z = (LevelPoint[1] - LevelPoint[3]) / 2.0f;
|
||||
|
||||
if ((BlockMeta & 0x08) != 0) // Test falling bit
|
||||
{
|
||||
return NONE;
|
||||
Direction.y = -1.0f;
|
||||
}
|
||||
|
||||
if (a_X - X > 0)
|
||||
{
|
||||
return X_MINUS;
|
||||
}
|
||||
|
||||
if (a_X - X < 0)
|
||||
{
|
||||
return X_PLUS;
|
||||
}
|
||||
|
||||
if (a_Z - Z > 0)
|
||||
{
|
||||
return Z_MINUS;
|
||||
}
|
||||
|
||||
if (a_Z - Z < 0)
|
||||
{
|
||||
return Z_PLUS;
|
||||
}
|
||||
|
||||
return NONE;
|
||||
return Direction;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -28,7 +28,7 @@ class cFluidSimulatorData
|
||||
{
|
||||
public:
|
||||
virtual ~cFluidSimulatorData() {}
|
||||
} ;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -45,8 +45,8 @@ public:
|
||||
// cSimulator overrides:
|
||||
virtual bool IsAllowedBlock(BLOCKTYPE a_BlockType) override;
|
||||
|
||||
/** Gets the flowing direction. If a_Over is true also the block over the current block affects the direction (standard) */
|
||||
virtual Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true);
|
||||
/** Returns a unit vector in the direction the fluid is flowing or a zero-vector if not flowing. */
|
||||
virtual Vector3f GetFlowingDirection(int a_X, int a_Y, int a_Z);
|
||||
|
||||
/** Creates a ChunkData object for the simulator to use. The simulator returns the correct object type. */
|
||||
virtual cFluidSimulatorData * CreateChunkData(void) { return nullptr; }
|
||||
@ -66,7 +66,7 @@ public:
|
||||
protected:
|
||||
BLOCKTYPE m_FluidBlock; // The fluid block type that needs simulating
|
||||
BLOCKTYPE m_StationaryFluidBlock; // The fluid block type that indicates no simulation is needed
|
||||
} ;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user