Block.cpp 5.6 KB

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