Block.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "Block.h"
  2. #include "Inventory.h"
  3. Block::Block( const BlockType *zType, ItemType *zTool, Framework::Vec3<int> pos, bool hasInventory )
  4. : Inventory( pos, hasInventory )
  5. {
  6. transparent = false;
  7. passable = false;
  8. hp = 1;
  9. maxHP = 1;
  10. hardness = 1;
  11. this->zType = zType;
  12. this->zTool = zTool;
  13. speedModifier = 1;
  14. ticksLeftCounter = 0;
  15. wasTicked = 0;
  16. onTickCalled = 0;
  17. minTickTimeout = -1;
  18. maxTickTimeout = -1;
  19. tickSource = 0;
  20. currentTickTimeout = 0;
  21. dimansionId = 0;
  22. memset( zNeighbours, 0, sizeof( Block * ) * 6 );
  23. }
  24. Block::~Block()
  25. {}
  26. void Block::tick( TickQueue *zQueue )
  27. {
  28. if( wasTicked )
  29. return;
  30. wasTicked = 1;
  31. ticksLeftCounter++;
  32. if( minTickTimeout >= 0 )
  33. {
  34. if( currentTickTimeout < ticksLeftCounter )
  35. {
  36. onTickCalled = 1;
  37. bool blocked = 0;
  38. bool result = onTick( zQueue, ticksLeftCounter, blocked );
  39. if( blocked )
  40. {
  41. wasTicked = 0;
  42. ticksLeftCounter--;
  43. onTickCalled = 0;
  44. return;
  45. }
  46. if( result )
  47. currentTickTimeout = MAX( MIN( currentTickTimeout - 1, maxTickTimeout ), MAX( minTickTimeout, 0 ) );
  48. else
  49. currentTickTimeout = MAX( MIN( currentTickTimeout + 1, maxTickTimeout ), MAX( minTickTimeout, 0 ) );
  50. ticksLeftCounter = 0;
  51. }
  52. }
  53. }
  54. void Block::postTick()
  55. {
  56. wasTicked = 0;
  57. if( onTickCalled )
  58. {
  59. onPostTick();
  60. onTickCalled = 0;
  61. }
  62. }
  63. void Block::setNeighbour( Direction dir, Block *zN )
  64. {
  65. zNeighbours[ getDirectionIndex( dir ) ] = zN;
  66. }
  67. void Block::setDimensionId( int id )
  68. {
  69. dimansionId = id;
  70. }
  71. void api( Framework::StreamReader *zRequest, NetworkResponse *zResponse )
  72. {
  73. // TODO: answer api requests
  74. }
  75. bool Block::isTickSource() const
  76. {
  77. return tickSource;
  78. }
  79. const BlockType *Block::zBlockType() const
  80. {
  81. return zType;
  82. }
  83. bool Block::isTransparent() const
  84. {
  85. return transparent;
  86. }
  87. bool Block::isPassable() const
  88. {
  89. return passable;
  90. }
  91. float Block::getHP() const
  92. {
  93. return hp;
  94. }
  95. float Block::getMaxHP() const
  96. {
  97. return maxHP;
  98. }
  99. float Block::getHardness() const
  100. {
  101. return hardness;
  102. }
  103. ItemType *Block::zEffectiveTool() const
  104. {
  105. return zTool;
  106. }
  107. float Block::getSpeedModifier() const
  108. {
  109. return speedModifier;
  110. }
  111. const Framework::Vec3<int> Block::getPos() const
  112. {
  113. return location;
  114. }
  115. int Block::getDimensionId() const
  116. {
  117. return dimansionId;
  118. }
  119. bool Block::isVisible() const
  120. {
  121. for( int i = 0; i < 6; i++ )
  122. {
  123. if( zNeighbours[ i ] == (Block*)AIR_BLOCK || (IS_BLOCK( zNeighbours[ i ] ) && zNeighbours[ i ]->isTransparent()))
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. BasicBlockItem::BasicBlockItem( const ItemType *zType, const char *name )
  129. : Item( zType, name )
  130. {
  131. placeable = 1;
  132. }
  133. bool BasicBlockItem::canBeStackedWith( Item *zItem ) const
  134. {
  135. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  136. if( item )
  137. {
  138. return Item::canBeStackedWith( zItem ) &&
  139. transparent == item->transparent &&
  140. passable == item->passable &&
  141. hp == item->hp &&
  142. maxHP == item->maxHP &&
  143. hardness == item->hardness &&
  144. toolId == item->toolId &&
  145. speedModifier == item->speedModifier;
  146. }
  147. return 0;
  148. }
  149. BasicBlockItemType::BasicBlockItemType( int id, ItemSkillLevelUpRule *levelUpRule, const ItemType *zBrokenType )
  150. : ItemType( id, levelUpRule, zBrokenType )
  151. {}
  152. void BasicBlockItemType::loadSuperItem( Item *zItem, Framework::StreamReader *zReader ) const
  153. {
  154. ItemType::loadSuperItem( zItem, zReader );
  155. BasicBlockItem *item = dynamic_cast<BasicBlockItem *>( zItem );
  156. if( !item )
  157. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  158. zReader->lese( (char *)&item->transparent, 1 );
  159. zReader->lese( (char *)&item->passable, 1 );
  160. zReader->lese( (char *)&item->hp, 4 );
  161. zReader->lese( (char *)&item->maxHP, 4 );
  162. zReader->lese( (char *)&item->hardness, 4 );
  163. zReader->lese( (char *)&item->toolId, 4 );
  164. zReader->lese( (char *)&item->speedModifier, 4 );
  165. }
  166. void BasicBlockItemType::saveSuperItem( const Item *zItem, Framework::StreamWriter *zWriter ) const
  167. {
  168. ItemType::saveSuperItem( zItem, zWriter );
  169. const BasicBlockItem *item = dynamic_cast<const BasicBlockItem *>( zItem );
  170. if( !item )
  171. throw "BasicBlockItemType::saveSuperItem was called with an invalid item";
  172. zWriter->schreibe( (char *)&item->transparent, 1 );
  173. zWriter->schreibe( (char *)&item->passable, 1 );
  174. zWriter->schreibe( (char *)&item->hp, 4 );
  175. zWriter->schreibe( (char *)&item->maxHP, 4 );
  176. zWriter->schreibe( (char *)&item->hardness, 4 );
  177. zWriter->schreibe( (char *)&item->toolId, 4 );
  178. zWriter->schreibe( (char *)&item->speedModifier, 4 );
  179. }
  180. void BasicBlockItemType::initializeItem( BasicBlockItem *zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier ) const
  181. {
  182. zItem->transparent = transparent;
  183. zItem->passable = passable;
  184. zItem->hp = hp;
  185. zItem->maxHP = maxHP;
  186. zItem->hardness = hardness;
  187. zItem->toolId = toolId;
  188. zItem->speedModifier = speedModifier;
  189. }