1
0
Fork 0

Refactored both simulators (water and lava) to make them more flexible and easier to modify

git-svn-id: http://mc-server.googlecode.com/svn/trunk@108 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
lapayo94@gmail.com 2011-12-25 16:20:06 +00:00
parent 62737fd163
commit d6953a7ea2
8 changed files with 409 additions and 565 deletions

View File

@ -229,6 +229,7 @@
<ClCompile Include="..\source\cEnderman.cpp" />
<ClCompile Include="..\source\cEvent.cpp" />
<ClCompile Include="..\source\cFileFormatUpdater.cpp" />
<ClCompile Include="..\source\cFluidSimulator.cpp" />
<ClCompile Include="..\source\cFurnaceEntity.cpp" />
<ClCompile Include="..\Source\cFurnaceRecipe.cpp" />
<ClCompile Include="..\source\cFurnaceWindow.cpp" />
@ -366,6 +367,7 @@
<ClInclude Include="..\source\cEnderman.h" />
<ClInclude Include="..\source\cEvent.h" />
<ClInclude Include="..\source\cFileFormatUpdater.h" />
<ClInclude Include="..\source\cFluidSimulator.h" />
<ClInclude Include="..\source\cFurnaceEntity.h" />
<ClInclude Include="..\Source\cFurnaceRecipe.h" />
<ClInclude Include="..\source\cFurnaceWindow.h" />

View File

@ -322,9 +322,6 @@
<Filter Include="Packets\cPacket_EntityStatus">
<UniqueIdentifier>{dee92edf-fe18-479a-9108-b11353e14913}</UniqueIdentifier>
</Filter>
<Filter Include="cWaterSimulator">
<UniqueIdentifier>{02fd457d-3735-4f4b-aaf2-f7701a7ee7bc}</UniqueIdentifier>
</Filter>
<Filter Include="Threading\cSocket">
<UniqueIdentifier>{82fda897-4c61-4ada-9160-8ae3982a72f8}</UniqueIdentifier>
</Filter>
@ -382,9 +379,6 @@
<Filter Include="cPiston">
<UniqueIdentifier>{4f551776-009b-4f05-8ab8-748b82279a67}</UniqueIdentifier>
</Filter>
<Filter Include="cLavaSimulator">
<UniqueIdentifier>{b0401fd9-4021-4ab7-bf61-c8de112b4196}</UniqueIdentifier>
</Filter>
<Filter Include="LuaBindings\SquirrelBindings">
<UniqueIdentifier>{a71ee043-df55-4d42-a9aa-1643b40e1c22}</UniqueIdentifier>
</Filter>
@ -403,6 +397,15 @@
<Filter Include="cChunkGenerator">
<UniqueIdentifier>{0d6f822b-71eb-406f-b17a-d188c4924283}</UniqueIdentifier>
</Filter>
<Filter Include="cFluidSimulator">
<UniqueIdentifier>{6f92474b-e2a5-4685-abf1-053b09dbc4f1}</UniqueIdentifier>
</Filter>
<Filter Include="cFluidSimulator\cWaterSimulator">
<UniqueIdentifier>{02fd457d-3735-4f4b-aaf2-f7701a7ee7bc}</UniqueIdentifier>
</Filter>
<Filter Include="cFluidSimulator\cLavaSimulator">
<UniqueIdentifier>{b0401fd9-4021-4ab7-bf61-c8de112b4196}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\source\cServer.cpp">
@ -725,7 +728,7 @@
<Filter>Packets\cPacket_EntityStatus</Filter>
</ClCompile>
<ClCompile Include="..\source\cWaterSimulator.cpp">
<Filter>cWaterSimulator</Filter>
<Filter>cFluidSimulator\cWaterSimulator</Filter>
</ClCompile>
<ClCompile Include="..\source\cSocket.cpp">
<Filter>Threading\cSocket</Filter>
@ -788,7 +791,7 @@
<Filter>cPiston</Filter>
</ClCompile>
<ClCompile Include="..\source\cLavaSimulator.cpp">
<Filter>cLavaSimulator</Filter>
<Filter>cFluidSimulator\cLavaSimulator</Filter>
</ClCompile>
<ClCompile Include="..\source\SquirrelBindings.cpp">
<Filter>LuaBindings\SquirrelBindings</Filter>
@ -808,6 +811,9 @@
<ClCompile Include="..\source\cChunkGenerator.cpp">
<Filter>cChunkGenerator</Filter>
</ClCompile>
<ClCompile Include="..\source\cFluidSimulator.cpp">
<Filter>cFluidSimulator</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\source\cServer.h">
@ -1162,7 +1168,7 @@
<Filter>Packets\cPacket_EntityStatus</Filter>
</ClInclude>
<ClInclude Include="..\source\cWaterSimulator.h">
<Filter>cWaterSimulator</Filter>
<Filter>cFluidSimulator\cWaterSimulator</Filter>
</ClInclude>
<ClInclude Include="..\source\cSocket.h">
<Filter>Threading\cSocket</Filter>
@ -1228,7 +1234,7 @@
<Filter>cPiston</Filter>
</ClInclude>
<ClInclude Include="..\source\cLavaSimulator.h">
<Filter>cLavaSimulator</Filter>
<Filter>cFluidSimulator\cLavaSimulator</Filter>
</ClInclude>
<ClInclude Include="..\source\SquirrelBindings.h">
<Filter>LuaBindings\SquirrelBindings</Filter>
@ -1248,6 +1254,9 @@
<ClInclude Include="..\source\cChunkGenerator.h">
<Filter>cChunkGenerator</Filter>
</ClInclude>
<ClInclude Include="..\source\cFluidSimulator.h">
<Filter>cFluidSimulator</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\source\AllToLua.pkg">

304
source/cFluidSimulator.cpp Normal file
View File

@ -0,0 +1,304 @@
#include "cFluidSimulator.h"
#include "cWorld.h"
#include "Vector3i.h"
#include "BlockID.h"
#include "Defines.h"
#include <vector>
class cFluidSimulator::FluidData
{
public:
FluidData( cWorld* a_World, cFluidSimulator *a_Simulator )
: m_ActiveFluid( new std::vector< Vector3i >() )
, m_Simulator (a_Simulator)
, m_Buffer( new std::vector< Vector3i >() )
, m_World( a_World )
{}
std::vector< Vector3i > GetLowestPoints( int a_X, int a_Y, int a_Z )
{
std::vector< Vector3i > Points;
if( m_World->GetBlock(a_X, a_Y-1, a_Z) == E_BLOCK_AIR )
{
Points.push_back( Vector3i( a_X, a_Y-1, a_Z ) );
return Points;
}
Vector3i LowerPoints [] = {
Vector3i( a_X-1, a_Y-1, a_Z ),
Vector3i( a_X+1, a_Y-1, a_Z ),
Vector3i( a_X, a_Y-1, a_Z-1 ),
Vector3i( a_X, a_Y-1, a_Z+1 ),
};
bool bFluidFound = false;
for( int i = 0; i < 4; ++i )
{
char Block1 = m_World->GetBlock( LowerPoints[i].x, LowerPoints[i].y, LowerPoints[i].z );
char Block2 = m_World->GetBlock( LowerPoints[i].x, a_Y, LowerPoints[i].z );
if( Block1 == E_BLOCK_AIR && Block2 == E_BLOCK_AIR )
{
Points.push_back( LowerPoints[i] );
LowerPoints[i].y = a_Y;
Points.push_back( LowerPoints[i] );
}
else if( m_Simulator->IsAllowedBlock(Block2) && m_Simulator->IsPassableForFluid(Block1) )
{
bFluidFound = true;
}
}
if( Points.size() == 0 && !bFluidFound )
{
Vector3i LevelPoints [] = {
Vector3i( a_X-1, a_Y, a_Z ),
Vector3i( a_X+1, a_Y, a_Z ),
Vector3i( a_X, a_Y, a_Z-1 ),
Vector3i( a_X, a_Y, a_Z+1 ),
};
for( int i = 0; i < 4; ++i )
{
char Block = m_World->GetBlock( LevelPoints[i].x, a_Y, LevelPoints[i].z );
if( m_Simulator->IsPassableForFluid(Block) )
Points.push_back( LevelPoints[i] );
}
}
return Points;
}
std::vector< Vector3i >* m_ActiveFluid;
std::vector< Vector3i >* m_Buffer;
cWorld* m_World;
cFluidSimulator *m_Simulator;
};
cFluidSimulator::cFluidSimulator( cWorld* a_World )
: m_World(a_World)
, m_Data(new FluidData(a_World, this))
{
}
cFluidSimulator::~cFluidSimulator()
{
}
void cFluidSimulator::WakeUp( int a_X, int a_Y, int a_Z )
{
AddBlock( a_X, a_Y, a_Z );
AddBlock( a_X-1, a_Y, a_Z );
AddBlock( a_X+1, a_Y, a_Z );
AddBlock( a_X, a_Y-1, a_Z );
AddBlock( a_X, a_Y+1, a_Z );
AddBlock( a_X, a_Y, a_Z-1 );
AddBlock( a_X, a_Y, a_Z+1 );
}
void cFluidSimulator::AddBlock( int a_X, int a_Y, int a_Z )
{
// Check for duplicates
std::vector< Vector3i > & ActiveFluid = *m_Data->m_ActiveFluid;
for( std::vector< Vector3i >::iterator itr = ActiveFluid.begin(); itr != ActiveFluid.end(); ++itr )
{
Vector3i & pos = *itr;
if( pos.x == a_X && pos.y == a_Y && pos.z == a_Z )
return;
}
ActiveFluid.push_back( Vector3i( a_X, a_Y, a_Z ) );
}
char cFluidSimulator::GetHighestLevelAround( int a_X, int a_Y, int a_Z )
{
char Max = m_MaxHeight + 1;
//TODO Remove define function
#define __HIGHLEVEL_CHECK__( x, y, z ) \
if( IsAllowedBlock( m_World->GetBlock( x, y, z ) ) ) \
{ \
char Meta; \
if( (Meta = m_World->GetBlockMeta( x, y, z ) ) < Max ) Max = Meta; \
else if( Meta == m_MaxHeight + 1 ) Max = 0; \
if( Max == 0 ) return 0; \
}
__HIGHLEVEL_CHECK__( a_X-1, a_Y, a_Z );
__HIGHLEVEL_CHECK__( a_X+1, a_Y, a_Z );
__HIGHLEVEL_CHECK__( a_X, a_Y, a_Z-1 );
__HIGHLEVEL_CHECK__( a_X, a_Y, a_Z+1 );
return Max;
}
void cFluidSimulator::Simulate( float a_Dt )
{
m_Timer += a_Dt;
if(m_Data->m_ActiveFluid->empty()) //Nothing to do if there is no active fluid ;) saves very little time ;D
return;
std::swap( m_Data->m_ActiveFluid, m_Data->m_Buffer ); // Swap so blocks can be added to empty ActiveFluid array
m_Data->m_ActiveFluid->clear();
std::vector< Vector3i > & FluidBlocks = *m_Data->m_Buffer;
for( std::vector< Vector3i >::iterator itr = FluidBlocks.begin(); itr != FluidBlocks.end(); ++itr )
{
Vector3i & pos = *itr;
char BlockID = m_World->GetBlock( pos.x, pos.y, pos.z );
if( IsAllowedBlock( BlockID ) ) // only care about own fluid
{
bool bIsFed = false;
char Meta = m_World->GetBlockMeta( pos.x, pos.y, pos.z );
char Feed = Meta;
if( Meta & 8 ) // Falling fluid
{
if( IsAllowedBlock( m_World->GetBlock(pos.x, pos.y+1, pos.z) ) ) // Block above is fluid
{
bIsFed = true;
Meta = 0; // Make it a full block
}
}
else if( Meta < m_FlowReduction ) // It's a full block, so it's always fed
{
bIsFed = true;
}
else
{
if( (Feed = GetHighestLevelAround( pos.x, pos.y, pos.z )) < Meta )
bIsFed = true;
}
if( bIsFed )
{
char DownID = m_World->GetBlock( pos.x, pos.y-1, pos.z );
if( IsPassableForFluid(DownID) ) // free for fluid
{
m_World->FastSetBlock( pos.x, pos.y-1, pos.z, m_FluidBlock, 8 ); // falling
AddBlock( pos.x, pos.y-1, pos.z );
}
else // Not falling
{
if( Feed + m_FlowReduction < Meta )
{
m_World->FastSetBlock( pos.x, pos.y, pos.z, m_FluidBlock, Feed + m_FlowReduction );
AddBlock( pos.x, pos.y, pos.z );
}
else if( Meta < m_MaxHeight ) // max is the lowest, so it cannot spread
{
std::vector< Vector3i > Points = m_Data->GetLowestPoints( pos.x, pos.y, pos.z );
for( std::vector< Vector3i >::iterator itr = Points.begin(); itr != Points.end(); ++itr )
{
Vector3i & p = *itr;
char BlockID = m_World->GetBlock( p.x, p.y, p.z );
if( !IsAllowedBlock( BlockID ) )
{
if( p.y == pos.y )
m_World->FastSetBlock(p.x, p.y, p.z, m_FluidBlock, Meta + m_FlowReduction);
else
m_World->FastSetBlock(p.x, p.y, p.z, m_FluidBlock, 8);
AddBlock( p.x, p.y, p.z );
}
else // it's fluid
{
char PointMeta = m_World->GetBlockMeta( p.x, p.y, p.z );
if( PointMeta > Meta + m_FlowReduction )
{
AddBlock( p.x, p.y, p.z );
}
}
}
}
}
}
else // not fed
{
m_World->FastSetBlock( pos.x, pos.y, pos.z, E_BLOCK_AIR, 0 );
WakeUp( pos.x, pos.y, pos.z );
}
}
}
}
//TODO Not working very well yet :s
Direction cFluidSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over)
{
char BlockID = m_World->GetBlock(a_X, a_Y, a_Z);
if(!IsAllowedBlock(BlockID)) //No Fluid -> No Flowing direction :D
return NONE;
/*
Disabled because of causing problems and beeing 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))
return Y_MINUS;
*/
char LowestPoint = m_World->GetBlockMeta(a_X, a_Y, a_Z); //Current Block Meta so only lower points will be counted
int X, Y, Z; //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);
}
std::vector< Vector3i * > Points;
//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(std::vector<Vector3i *>::iterator it = Points.begin(); it < Points.end(); it++)
{
Vector3i *Pos = (*it);
char BlockID = m_World->GetBlock(Pos->x, Pos->y, Pos->z);
if(IsAllowedBlock(BlockID))
{
char Meta = m_World->GetBlockMeta(Pos->x, Pos->y, Pos->z);
if(Meta > LowestPoint)
{
LowestPoint = Meta;
X = Pos->x;
Y = Pos->y;
Z = Pos->z;
}
}else if(BlockID == E_BLOCK_AIR)
{
LowestPoint = 9; //This always dominates
X = Pos->x;
Y = Pos->y;
Z = Pos->z;
}
delete Pos;
}
if(LowestPoint == m_World->GetBlockMeta(a_X, a_Y, a_Z))
return NONE;
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;
}

49
source/cFluidSimulator.h Normal file
View File

@ -0,0 +1,49 @@
#pragma once
//TODO This definitly needs a better naming :D but how?
enum Direction
{
X_PLUS,
X_MINUS,
Y_PLUS,
Y_MINUS,
Z_PLUS,
Z_MINUS,
NONE
};
class Vector3i;
class cWorld;
class cFluidSimulator
{
public:
cFluidSimulator( cWorld* a_World );
~cFluidSimulator();
virtual void Simulate( float a_Dt );
void WakeUp( int a_X, int a_Y, int a_Z );
//Gets the flowing direction. if a_Over is true also the block over the current block affects the direction (standard)
Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true);
virtual inline bool IsAllowedBlock( char a_BlockID ) = 0;
virtual inline bool IsPassableForFluid( char a_BlockID ) = 0;
protected:
void AddBlock( int a_X, int a_Y, int a_Z);
char GetHighestLevelAround( int a_X, int a_Y, int a_Z );
float m_Timer;
cWorld* m_World;
class FluidData;
FluidData* m_Data;
//Customize
char m_FluidBlock;
char m_MaxHeight;
char m_FlowReduction;
};

View File

@ -1,216 +1,23 @@
#include "cLavaSimulator.h"
#include "cWorld.h"
#include "Vector3i.h"
#include "BlockID.h"
#include <vector>
#include "Defines.h"
class cLavaSimulator::LavaData
{
public:
LavaData( cWorld* a_World )
: m_ActiveLava( new std::vector< Vector3i >() )
, m_Buffer( new std::vector< Vector3i >() )
, m_World( a_World )
{}
std::vector< Vector3i > GetLowestPoints( int a_X, int a_Y, int a_Z )
{
std::vector< Vector3i > Points;
if( m_World->GetBlock(a_X, a_Y-1, a_Z) == E_BLOCK_AIR )
{
Points.push_back( Vector3i( a_X, a_Y-1, a_Z ) );
return Points;
}
Vector3i LowerPoints [] = {
Vector3i( a_X-1, a_Y-1, a_Z ),
Vector3i( a_X+1, a_Y-1, a_Z ),
Vector3i( a_X, a_Y-1, a_Z-1 ),
Vector3i( a_X, a_Y-1, a_Z+1 ),
};
bool bLavaFound = false;
for( int i = 0; i < 4; ++i )
{
char Block1 = m_World->GetBlock( LowerPoints[i].x, LowerPoints[i].y, LowerPoints[i].z );
char Block2 = m_World->GetBlock( LowerPoints[i].x, a_Y, LowerPoints[i].z );
if( Block1 == E_BLOCK_AIR && Block2 == E_BLOCK_AIR )
{
Points.push_back( LowerPoints[i] );
LowerPoints[i].y = a_Y;
Points.push_back( LowerPoints[i] );
}
else if( (Block2 == E_BLOCK_LAVA || Block2 == E_BLOCK_STATIONARY_LAVA ) && ( Block1 == E_BLOCK_AIR || Block1 == E_BLOCK_LAVA || Block1 == E_BLOCK_STATIONARY_LAVA ) )
{
bLavaFound = true;
}
}
if( Points.size() == 0 && !bLavaFound )
{
Vector3i LevelPoints [] = {
Vector3i( a_X-1, a_Y, a_Z ),
Vector3i( a_X+1, a_Y, a_Z ),
Vector3i( a_X, a_Y, a_Z-1 ),
Vector3i( a_X, a_Y, a_Z+1 ),
};
for( int i = 0; i < 4; ++i )
{
char Block = m_World->GetBlock( LevelPoints[i].x, a_Y, LevelPoints[i].z );
if( Block == E_BLOCK_AIR || Block == E_BLOCK_LAVA || Block == E_BLOCK_STATIONARY_LAVA )
Points.push_back( LevelPoints[i] );
}
}
return Points;
}
std::vector< Vector3i >* m_ActiveLava;
std::vector< Vector3i >* m_Buffer;
cWorld* m_World;
};
cLavaSimulator::cLavaSimulator( cWorld* a_World )
: m_World( a_World )
, m_Data( new LavaData( a_World ) )
cLavaSimulator::cLavaSimulator(cWorld *a_World)
: cFluidSimulator(a_World)
{
m_FluidBlock = E_BLOCK_LAVA;
m_MaxHeight = 6;
m_FlowReduction = 2;
}
cLavaSimulator::~cLavaSimulator()
bool cLavaSimulator::IsAllowedBlock(char a_BlockID)
{
return IsBlockLava(a_BlockID);
}
void cLavaSimulator::WakeUp( int a_X, int a_Y, int a_Z )
bool cLavaSimulator::IsPassableForFluid(char a_BlockID)
{
AddBlock( a_X, a_Y, a_Z );
AddBlock( a_X-1, a_Y, a_Z );
AddBlock( a_X+1, a_Y, a_Z );
AddBlock( a_X, a_Y-1, a_Z );
AddBlock( a_X, a_Y+1, a_Z );
AddBlock( a_X, a_Y, a_Z-1 );
AddBlock( a_X, a_Y, a_Z+1 );
}
void cLavaSimulator::AddBlock( int a_X, int a_Y, int a_Z )
{
// Check for duplicates
std::vector< Vector3i > & ActiveLava = *m_Data->m_ActiveLava;
for( std::vector< Vector3i >::iterator itr = ActiveLava.begin(); itr != ActiveLava.end(); ++itr )
{
Vector3i & pos = *itr;
if( pos.x == a_X && pos.y == a_Y && pos.z == a_Z )
return;
}
ActiveLava.push_back( Vector3i( a_X, a_Y, a_Z ) );
}
char cLavaSimulator::GetHighestLevelAround( int a_X, int a_Y, int a_Z )
{
char Max = 8;
#define __HIGHLEVEL_CHECK__( x, y, z ) \
if( IsLavaBlock( m_World->GetBlock( x, y, z ) ) ) \
{ \
char Meta; \
if( (Meta = m_World->GetBlockMeta( x, y, z ) ) < Max ) Max = Meta; \
else if( Meta == 8 ) Max = 0; \
if( Max == 0 ) return 0; \
}
__HIGHLEVEL_CHECK__( a_X-1, a_Y, a_Z );
__HIGHLEVEL_CHECK__( a_X+1, a_Y, a_Z );
__HIGHLEVEL_CHECK__( a_X, a_Y, a_Z-1 );
__HIGHLEVEL_CHECK__( a_X, a_Y, a_Z+1 );
return Max;
}
void cLavaSimulator::Simulate( float a_Dt )
{
m_Timer += a_Dt;
std::swap( m_Data->m_ActiveLava, m_Data->m_Buffer ); // Swap so blocks can be added to empty ActiveLava array
m_Data->m_ActiveLava->clear();
std::vector< Vector3i > & LavaBlocks = *m_Data->m_Buffer;
for( std::vector< Vector3i >::iterator itr = LavaBlocks.begin(); itr != LavaBlocks.end(); ++itr )
{
Vector3i & pos = *itr;
char BlockID = m_World->GetBlock( pos.x, pos.y, pos.z );
if( IsLavaBlock( BlockID ) ) // only care about lava
{
bool bIsFed = false;
char Meta = m_World->GetBlockMeta( pos.x, pos.y, pos.z );
char Feed = Meta;
if( Meta == 8 ) // Falling lava
{
if( IsLavaBlock( m_World->GetBlock(pos.x, pos.y+1, pos.z) ) ) // Block above is lava
{
bIsFed = true;
Meta = 0; // Make it a full block
}
}
else if( Meta <= 2 )
{
bIsFed = true;
}
else
{
if( (Feed = GetHighestLevelAround( pos.x, pos.y, pos.z )) < Meta )
bIsFed = true;
}
if( bIsFed )
{
char DownID = m_World->GetBlock( pos.x, pos.y-1, pos.z );
if( DownID == E_BLOCK_AIR || IsLavaBlock( DownID ) ) // free for Lava
{
m_World->FastSetBlock( pos.x, pos.y-1, pos.z, E_BLOCK_LAVA, 8 ); // falling
AddBlock( pos.x, pos.y-1, pos.z );
}
else // Not falling Lava
{
if( Feed+2 < Meta )
{
m_World->FastSetBlock( pos.x, pos.y, pos.z, E_BLOCK_LAVA, Feed+2 );
AddBlock( pos.x, pos.y, pos.z );
}
else if( Meta < 6 ) // 7 is only 1 unit high, so it cannot spread, lower than 7 can though.
{
std::vector< Vector3i > Points = m_Data->GetLowestPoints( pos.x, pos.y, pos.z );
for( std::vector< Vector3i >::iterator itr = Points.begin(); itr != Points.end(); ++itr )
{
Vector3i & p = *itr;
char BlockID = m_World->GetBlock( p.x, p.y, p.z );
if( !IsLavaBlock( BlockID ) )
{
if( p.y == pos.y )
m_World->FastSetBlock(p.x, p.y, p.z, E_BLOCK_LAVA, Meta+2);
else
m_World->FastSetBlock(p.x, p.y, p.z, E_BLOCK_LAVA, 8);
AddBlock( p.x, p.y, p.z );
}
else // it's Lava
{
char PointMeta = m_World->GetBlockMeta( p.x, p.y, p.z );
if( PointMeta > Meta+2 )
{
AddBlock( p.x, p.y, p.z );
}
}
}
}
}
}
else // not fed
{
m_World->FastSetBlock( pos.x, pos.y, pos.z, E_BLOCK_AIR, 0 );
WakeUp( pos.x, pos.y, pos.z );
}
}
}
}
bool cLavaSimulator::IsLavaBlock( char a_BlockID )
{
return a_BlockID == E_BLOCK_LAVA || a_BlockID == E_BLOCK_STATIONARY_LAVA;
}
return ( a_BlockID == E_BLOCK_AIR || IsAllowedBlock(a_BlockID) );
}

View File

@ -1,25 +1,12 @@
#pragma once
#include "cFluidSimulator.h"
class Vector3i;
class cWorld;
class cLavaSimulator
class cLavaSimulator : public cFluidSimulator
{
public:
cLavaSimulator( cWorld* a_World );
~cLavaSimulator();
void Simulate( float a_Dt );
void WakeUp( int a_X, int a_Y, int a_Z );
virtual inline bool IsAllowedBlock( char a_BlockID );
virtual inline bool IsPassableForFluid( char a_BlockID );
private:
void AddBlock( int a_X, int a_Y, int a_Z);
char GetHighestLevelAround( int a_X, int a_Y, int a_Z );
bool IsLavaBlock( char a_BlockID );
float m_Timer;
cWorld* m_World;
class LavaData;
LavaData* m_Data;
};
};

View File

@ -1,306 +1,23 @@
#include "cWaterSimulator.h"
#include "cWorld.h"
#include "Vector3i.h"
#include "BlockID.h"
#include "Defines.h"
#include <vector>
class cWaterSimulator::WaterData
{
public:
WaterData( cWorld* a_World )
: m_ActiveWater( new std::vector< Vector3i >() )
, m_Buffer( new std::vector< Vector3i >() )
, m_World( a_World )
{}
std::vector< Vector3i > GetLowestPoints( int a_X, int a_Y, int a_Z )
{
std::vector< Vector3i > Points;
if( m_World->GetBlock(a_X, a_Y-1, a_Z) == E_BLOCK_AIR )
{
Points.push_back( Vector3i( a_X, a_Y-1, a_Z ) );
return Points;
}
Vector3i LowerPoints [] = {
Vector3i( a_X-1, a_Y-1, a_Z ),
Vector3i( a_X+1, a_Y-1, a_Z ),
Vector3i( a_X, a_Y-1, a_Z-1 ),
Vector3i( a_X, a_Y-1, a_Z+1 ),
};
bool bWaterFound = false;
for( int i = 0; i < 4; ++i )
{
char Block1 = m_World->GetBlock( LowerPoints[i].x, LowerPoints[i].y, LowerPoints[i].z );
char Block2 = m_World->GetBlock( LowerPoints[i].x, a_Y, LowerPoints[i].z );
if( Block1 == E_BLOCK_AIR && Block2 == E_BLOCK_AIR )
{
Points.push_back( LowerPoints[i] );
LowerPoints[i].y = a_Y;
Points.push_back( LowerPoints[i] );
}
else if( IsBlockWater(Block2) && ( Block1 == E_BLOCK_AIR || IsBlockWater(Block1) ) )
{
bWaterFound = true;
}
}
if( Points.size() == 0 && !bWaterFound )
{
Vector3i LevelPoints [] = {
Vector3i( a_X-1, a_Y, a_Z ),
Vector3i( a_X+1, a_Y, a_Z ),
Vector3i( a_X, a_Y, a_Z-1 ),
Vector3i( a_X, a_Y, a_Z+1 ),
};
for( int i = 0; i < 4; ++i )
{
char Block = m_World->GetBlock( LevelPoints[i].x, a_Y, LevelPoints[i].z );
if( Block == E_BLOCK_AIR || Block == E_BLOCK_WATER || Block == E_BLOCK_STATIONARY_WATER )
Points.push_back( LevelPoints[i] );
}
}
return Points;
}
std::vector< Vector3i >* m_ActiveWater;
std::vector< Vector3i >* m_Buffer;
cWorld* m_World;
};
cWaterSimulator::cWaterSimulator( cWorld* a_World )
: m_World( a_World )
, m_Data( new WaterData( a_World ) )
cWaterSimulator::cWaterSimulator(cWorld *a_World)
: cFluidSimulator(a_World)
{
m_FluidBlock = E_BLOCK_WATER;
m_MaxHeight = 7;
m_FlowReduction = 1;
}
cWaterSimulator::~cWaterSimulator()
bool cWaterSimulator::IsAllowedBlock(char a_BlockID)
{
return IsBlockWater(a_BlockID);
}
void cWaterSimulator::WakeUp( int a_X, int a_Y, int a_Z )
bool cWaterSimulator::IsPassableForFluid(char a_BlockID)
{
AddBlock( a_X, a_Y, a_Z );
AddBlock( a_X-1, a_Y, a_Z );
AddBlock( a_X+1, a_Y, a_Z );
AddBlock( a_X, a_Y-1, a_Z );
AddBlock( a_X, a_Y+1, a_Z );
AddBlock( a_X, a_Y, a_Z-1 );
AddBlock( a_X, a_Y, a_Z+1 );
}
void cWaterSimulator::AddBlock( int a_X, int a_Y, int a_Z )
{
// Check for duplicates
std::vector< Vector3i > & ActiveWater = *m_Data->m_ActiveWater;
for( std::vector< Vector3i >::iterator itr = ActiveWater.begin(); itr != ActiveWater.end(); ++itr )
{
Vector3i & pos = *itr;
if( pos.x == a_X && pos.y == a_Y && pos.z == a_Z )
return;
}
ActiveWater.push_back( Vector3i( a_X, a_Y, a_Z ) );
}
char cWaterSimulator::GetHighestLevelAround( int a_X, int a_Y, int a_Z )
{
char Max = 8;
#define __HIGHLEVEL_CHECK__( x, y, z ) \
if( IsWaterBlock( m_World->GetBlock( x, y, z ) ) ) \
{ \
char Meta; \
if( (Meta = m_World->GetBlockMeta( x, y, z ) ) < Max ) Max = Meta; \
else if( Meta == 8 ) Max = 0; \
if( Max == 0 ) return 0; \
}
__HIGHLEVEL_CHECK__( a_X-1, a_Y, a_Z );
__HIGHLEVEL_CHECK__( a_X+1, a_Y, a_Z );
__HIGHLEVEL_CHECK__( a_X, a_Y, a_Z-1 );
__HIGHLEVEL_CHECK__( a_X, a_Y, a_Z+1 );
return Max;
}
void cWaterSimulator::Simulate( float a_Dt )
{
m_Timer += a_Dt;
if(m_Data->m_ActiveWater->empty()) //Nothing to do if there is no ActiveWater ;) saves very little time ;D
return;
std::swap( m_Data->m_ActiveWater, m_Data->m_Buffer ); // Swap so blocks can be added to empty ActiveWater array
m_Data->m_ActiveWater->clear();
std::vector< Vector3i > & WaterBlocks = *m_Data->m_Buffer;
for( std::vector< Vector3i >::iterator itr = WaterBlocks.begin(); itr != WaterBlocks.end(); ++itr )
{
Vector3i & pos = *itr;
char BlockID = m_World->GetBlock( pos.x, pos.y, pos.z );
if( IsWaterBlock( BlockID ) ) // only care about water
{
bool bIsFed = false;
char Meta = m_World->GetBlockMeta( pos.x, pos.y, pos.z );
char Feed = Meta;
if( Meta == 8 ) // Falling water
{
if( IsWaterBlock( m_World->GetBlock(pos.x, pos.y+1, pos.z) ) ) // Block above is water
{
bIsFed = true;
Meta = 0; // Make it a full block
}
}
else if( Meta == 0 ) // It's a full block, so it's always fed
{
bIsFed = true;
}
else
{
if( (Feed = GetHighestLevelAround( pos.x, pos.y, pos.z )) < Meta )
bIsFed = true;
}
if( bIsFed )
{
char DownID = m_World->GetBlock( pos.x, pos.y-1, pos.z );
if( DownID == E_BLOCK_AIR || IsWaterBlock( DownID ) ) // free for water
{
m_World->FastSetBlock( pos.x, pos.y-1, pos.z, E_BLOCK_WATER, 8 ); // falling
AddBlock( pos.x, pos.y-1, pos.z );
}
else // Not falling water
{
if( Feed+1 < Meta )
{
m_World->FastSetBlock( pos.x, pos.y, pos.z, E_BLOCK_WATER, Feed+1 );
AddBlock( pos.x, pos.y, pos.z );
}
else if( Meta < 7 ) // 7 is only 1 unit high, so it cannot spread, lower than 7 can though.
{
std::vector< Vector3i > Points = m_Data->GetLowestPoints( pos.x, pos.y, pos.z );
for( std::vector< Vector3i >::iterator itr = Points.begin(); itr != Points.end(); ++itr )
{
Vector3i & p = *itr;
char BlockID = m_World->GetBlock( p.x, p.y, p.z );
if( !IsWaterBlock( BlockID ) )
{
if( p.y == pos.y )
m_World->FastSetBlock(p.x, p.y, p.z, E_BLOCK_WATER, Meta+1);
else
m_World->FastSetBlock(p.x, p.y, p.z, E_BLOCK_WATER, 8);
AddBlock( p.x, p.y, p.z );
}
else // it's water
{
char PointMeta = m_World->GetBlockMeta( p.x, p.y, p.z );
if( PointMeta > Meta+1 )
{
AddBlock( p.x, p.y, p.z );
}
}
}
}
}
}
else // not fed
{
m_World->FastSetBlock( pos.x, pos.y, pos.z, E_BLOCK_AIR, 0 );
WakeUp( pos.x, pos.y, pos.z );
}
}
}
}
bool cWaterSimulator::IsWaterBlock( char a_BlockID )
{
return a_BlockID == E_BLOCK_WATER || a_BlockID == E_BLOCK_STATIONARY_WATER;
}
//TODO Not working very well yet :s
Direction cWaterSimulator::GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over)
{
char BlockID = m_World->GetBlock(a_X, a_Y, a_Z);
if(!IsBlockWater(BlockID)) //No Water -> No Flowing direction :D
return NONE;
/*
Disabled because of causing problems and beeing useless atm
char BlockBelow = m_World->GetBlock(a_X, a_Y - 1, a_Z); //If there is nothing or water below it -> dominating flow is down :D
if(BlockBelow == E_BLOCK_AIR || IsBlockWater(BlockBelow))
return Y_MINUS;
*/
char LowestPoint = m_World->GetBlockMeta(a_X, a_Y, a_Z); //Current Block Meta so only lower points will be counted
int X, Y, Z; //Lowest Pos will be stored here
if(IsBlockWater(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);
}
std::vector< Vector3i * > Points;
//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(std::vector<Vector3i *>::iterator it = Points.begin(); it < Points.end(); it++)
{
Vector3i *Pos = (*it);
char BlockID = m_World->GetBlock(Pos->x, Pos->y, Pos->z);
if(IsBlockWater(BlockID))
{
char Meta = m_World->GetBlockMeta(Pos->x, Pos->y, Pos->z);
if(Meta > LowestPoint)
{
LowestPoint = Meta;
X = Pos->x;
Y = Pos->y;
Z = Pos->z;
}
}else if(BlockID == E_BLOCK_AIR)
{
LowestPoint = 9; //This always dominates
X = Pos->x;
Y = Pos->y;
Z = Pos->z;
}
delete Pos;
}
if(LowestPoint == m_World->GetBlockMeta(a_X, a_Y, a_Z))
return NONE;
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 ( a_BlockID == E_BLOCK_AIR || IsAllowedBlock(a_BlockID) );
}

View File

@ -1,43 +1,12 @@
#pragma once
#include "cFluidSimulator.h"
//TODO This definitly needs a better naming :D but how?
enum Direction
{
X_PLUS,
X_MINUS,
Y_PLUS,
Y_MINUS,
Z_PLUS,
Z_MINUS,
NONE
};
class Vector3i;
class cWorld;
class cWaterSimulator
class cWaterSimulator : public cFluidSimulator
{
public:
cWaterSimulator( cWorld* a_World );
~cWaterSimulator();
void Simulate( float a_Dt );
void WakeUp( int a_X, int a_Y, int a_Z );
virtual inline bool IsAllowedBlock( char a_BlockID );
virtual inline bool IsPassableForFluid( char a_BlockID );
//Gets the flowing direction of a Waterblock if a_Over is true also the block over the current block affects the direction (standard)
Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true);
private:
void AddBlock( int a_X, int a_Y, int a_Z);
char GetHighestLevelAround( int a_X, int a_Y, int a_Z );
bool IsWaterBlock( char a_BlockID );
float m_Timer;
cWorld* m_World;
class WaterData;
WaterData* m_Data;
};