Block.cpp 5.9 KB

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