Block.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "Block.h"
  2. Block::Block( BlockType *zType, ItemType *zTool )
  3. {
  4. transparent = false;
  5. passable = false;
  6. hp = 1;
  7. maxHP = 1;
  8. hardness = 1;
  9. this->zType = zType;
  10. this->zTool = zTool;
  11. speedModifier = 1;
  12. fluidCapacity = 10;
  13. fluidAmount = 0;
  14. fluidTypeId = -1;
  15. fluidSpeed = 4;
  16. }
  17. void Block::tick()
  18. {
  19. if( fluidTypeId >= 0 )
  20. {
  21. int fluidLeft = MIN( fluidSpeed, fluidAmount );
  22. if( fluidLeft > 0 )
  23. {
  24. if( neighbours[ BOTTOM ] )
  25. {
  26. int result = neighbours[ BOTTOM ]->addFluid( fluidTypeId, fluidLeft, TOP );
  27. fluidLeft -= result;
  28. fluidAmount -= result;
  29. }
  30. int distribution = (int)ceil( (float)fluidLeft / 4.f );
  31. if( neighbours[ NORTH ] && neighbours[ NORTH ]->getFluidAmount() < fluidAmount - 1 )
  32. {
  33. int amount = MIN( distribution, ( fluidAmount - neighbours[ NORTH ]->getFluidAmount() ) / 2 );
  34. int result = neighbours[ NORTH ]->addFluid( fluidTypeId, amount, SOUTH );
  35. fluidLeft -= result;
  36. fluidAmount -= result;
  37. }
  38. if( neighbours[ EAST ] && neighbours[ EAST ]->getFluidAmount() < fluidAmount - 1 )
  39. {
  40. int amount = MIN( distribution, ( fluidAmount - neighbours[ EAST ]->getFluidAmount() ) / 2 );
  41. int result = neighbours[ EAST ]->addFluid( fluidTypeId, amount, WEST );
  42. fluidLeft -= result;
  43. fluidAmount -= result;
  44. }
  45. if( neighbours[ SOUTH ] && neighbours[ SOUTH ]->getFluidAmount() < fluidAmount - 1 )
  46. {
  47. int amount = MIN( distribution, ( fluidAmount - neighbours[ SOUTH ]->getFluidAmount() ) / 2 );
  48. int result = neighbours[ SOUTH ]->addFluid( fluidTypeId, amount, NORTH );
  49. fluidLeft -= result;
  50. fluidAmount -= result;
  51. }
  52. if( neighbours[ WEST ] && neighbours[ WEST ]->getFluidAmount() < fluidAmount - 1 )
  53. {
  54. int amount = MIN( distribution, ( fluidAmount - neighbours[ WEST ]->getFluidAmount() ) / 2 );
  55. int result = neighbours[ WEST ]->addFluid( fluidTypeId, amount, EAST );
  56. fluidLeft -= result;
  57. fluidAmount -= result;
  58. }
  59. if( fluidAmount == 0 )
  60. fluidTypeId = -1;
  61. }
  62. }
  63. }
  64. void Block::postTick()
  65. {
  66. }
  67. int Block::addFluid( int fluidTypeId, int amount, DIRECTION dir )
  68. {
  69. if( this->fluidTypeId != -1 && this->fluidTypeId != fluidTypeId )
  70. return 0;
  71. this->fluidTypeId = fluidTypeId;
  72. int result = MIN( amount, fluidCapacity - fluidAmount );
  73. fluidAmount += result;
  74. return result;
  75. }
  76. BlockType *Block::zBlockType() const
  77. {
  78. return zType;
  79. }
  80. bool Block::isTransparent() const
  81. {
  82. return transparent;
  83. }
  84. bool Block::isPassable() const
  85. {
  86. return passable;
  87. }
  88. float Block::getHP() const
  89. {
  90. return hp;
  91. }
  92. float Block::getMaxHP() const
  93. {
  94. return maxHP;
  95. }
  96. float Block::getHardness() const
  97. {
  98. return hardness;
  99. }
  100. ItemType *Block::zEffectiveTool() const
  101. {
  102. return zTool;
  103. }
  104. float Block::getSpeedModifier() const
  105. {
  106. return speedModifier;
  107. }
  108. int Block::getFluidCapacity() const
  109. {
  110. return fluidCapacity;
  111. }
  112. int Block::getFluidAmount() const
  113. {
  114. return fluidAmount;
  115. }
  116. int Block::getFluidTypeId() const
  117. {
  118. return fluidTypeId;
  119. }
  120. int Block::getFluidSpeed() const
  121. {
  122. return fluidSpeed;
  123. }