123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include "Block.h"
- Block::Block( BlockType *zType, ItemType *zTool )
- {
- transparent = false;
- passable = false;
- hp = 1;
- maxHP = 1;
- hardness = 1;
- this->zType = zType;
- this->zTool = zTool;
- speedModifier = 1;
- fluidCapacity = 10;
- fluidAmount = 0;
- fluidTypeId = -1;
- fluidSpeed = 4;
- }
- void Block::tick()
- {
- if( fluidTypeId >= 0 )
- {
- int fluidLeft = MIN( fluidSpeed, fluidAmount );
- if( fluidLeft > 0 )
- {
- if( neighbours[ BOTTOM ] )
- {
- int result = neighbours[ BOTTOM ]->addFluid( fluidTypeId, fluidLeft, TOP );
- fluidLeft -= result;
- fluidAmount -= result;
- }
- int distribution = (int)ceil( (float)fluidLeft / 4.f );
- if( neighbours[ NORTH ] && neighbours[ NORTH ]->getFluidAmount() < fluidAmount - 1 )
- {
- int amount = MIN( distribution, ( fluidAmount - neighbours[ NORTH ]->getFluidAmount() ) / 2 );
- int result = neighbours[ NORTH ]->addFluid( fluidTypeId, amount, SOUTH );
- fluidLeft -= result;
- fluidAmount -= result;
- }
- if( neighbours[ EAST ] && neighbours[ EAST ]->getFluidAmount() < fluidAmount - 1 )
- {
- int amount = MIN( distribution, ( fluidAmount - neighbours[ EAST ]->getFluidAmount() ) / 2 );
- int result = neighbours[ EAST ]->addFluid( fluidTypeId, amount, WEST );
- fluidLeft -= result;
- fluidAmount -= result;
- }
- if( neighbours[ SOUTH ] && neighbours[ SOUTH ]->getFluidAmount() < fluidAmount - 1 )
- {
- int amount = MIN( distribution, ( fluidAmount - neighbours[ SOUTH ]->getFluidAmount() ) / 2 );
- int result = neighbours[ SOUTH ]->addFluid( fluidTypeId, amount, NORTH );
- fluidLeft -= result;
- fluidAmount -= result;
- }
- if( neighbours[ WEST ] && neighbours[ WEST ]->getFluidAmount() < fluidAmount - 1 )
- {
- int amount = MIN( distribution, ( fluidAmount - neighbours[ WEST ]->getFluidAmount() ) / 2 );
- int result = neighbours[ WEST ]->addFluid( fluidTypeId, amount, EAST );
- fluidLeft -= result;
- fluidAmount -= result;
- }
- if( fluidAmount == 0 )
- fluidTypeId = -1;
- }
- }
- }
- void Block::postTick()
- {
- }
- int Block::addFluid( int fluidTypeId, int amount, DIRECTION dir )
- {
- if( this->fluidTypeId != -1 && this->fluidTypeId != fluidTypeId )
- return 0;
- this->fluidTypeId = fluidTypeId;
- int result = MIN( amount, fluidCapacity - fluidAmount );
- fluidAmount += result;
- return result;
- }
- BlockType *Block::zBlockType() const
- {
- return zType;
- }
- bool Block::isTransparent() const
- {
- return transparent;
- }
- bool Block::isPassable() const
- {
- return passable;
- }
- float Block::getHP() const
- {
- return hp;
- }
- float Block::getMaxHP() const
- {
- return maxHP;
- }
- float Block::getHardness() const
- {
- return hardness;
- }
- ItemType *Block::zEffectiveTool() const
- {
- return zTool;
- }
- float Block::getSpeedModifier() const
- {
- return speedModifier;
- }
- int Block::getFluidCapacity() const
- {
- return fluidCapacity;
- }
- int Block::getFluidAmount() const
- {
- return fluidAmount;
- }
- int Block::getFluidTypeId() const
- {
- return fluidTypeId;
- }
- int Block::getFluidSpeed() const
- {
- return fluidSpeed;
- }
|