Block.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, Framework::Either<Block*, int> neighbour )
  65. {
  66. if( neighbour.isA() )
  67. setNeighbourBlock( dir, neighbour );
  68. else
  69. {
  70. setNeighbourBlock( dir, 0 );
  71. setNeighbourType( dir, neighbour );
  72. }
  73. }
  74. void Block::setNeighbourBlock( Direction dir, Block* zN )
  75. {
  76. if( zN )
  77. setNeighbourType( dir, zN->zBlockType()->getId() );
  78. zNeighbours[ getDirectionIndex( dir ) ] = zN;
  79. }
  80. void Block::setNeighbourType( Direction dir, int type )
  81. {
  82. neighbourTypes[ getDirectionIndex( dir ) ] = type;
  83. }
  84. void Block::setDimensionId( int id )
  85. {
  86. dimansionId = id;
  87. }
  88. void api( Framework::StreamReader* zRequest, NetworkResponse* zResponse )
  89. {
  90. // TODO: answer api requests
  91. }
  92. bool Block::isTickSource() const
  93. {
  94. return tickSource;
  95. }
  96. const BlockType* Block::zBlockType() const
  97. {
  98. return zType;
  99. }
  100. bool Block::isTransparent() const
  101. {
  102. return transparent;
  103. }
  104. bool Block::isPassable() const
  105. {
  106. return passable;
  107. }
  108. float Block::getHP() const
  109. {
  110. return hp;
  111. }
  112. float Block::getMaxHP() const
  113. {
  114. return maxHP;
  115. }
  116. float Block::getHardness() const
  117. {
  118. return hardness;
  119. }
  120. ItemType* Block::zEffectiveTool() const
  121. {
  122. return zTool;
  123. }
  124. float Block::getSpeedModifier() const
  125. {
  126. return speedModifier;
  127. }
  128. const Framework::Vec3<int> Block::getPos() const
  129. {
  130. return location;
  131. }
  132. int Block::getDimensionId() const
  133. {
  134. return dimansionId;
  135. }
  136. bool Block::isVisible() const
  137. {
  138. if( passable || transparent )
  139. return 1;
  140. for( int i = 0; i < 6; i++ )
  141. {
  142. const Block* neighbour = CONST_BLOCK( zNeighbours[ i ], neighbourTypes[ i ] );
  143. if( neighbour->isPassable() || neighbour->isTransparent() )
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. BasicBlockItem::BasicBlockItem( const ItemType* zType, const char* name )
  149. : Item( zType, name )
  150. {
  151. placeable = 1;
  152. }
  153. bool BasicBlockItem::canBeStackedWith( Item* zItem ) const
  154. {
  155. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  156. if( item )
  157. {
  158. return Item::canBeStackedWith( zItem ) &&
  159. transparent == item->transparent &&
  160. passable == item->passable &&
  161. hp == item->hp &&
  162. maxHP == item->maxHP &&
  163. hardness == item->hardness &&
  164. toolId == item->toolId &&
  165. speedModifier == item->speedModifier;
  166. }
  167. return 0;
  168. }
  169. BasicBlockItemType::BasicBlockItemType( int id, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType )
  170. : ItemType( id, levelUpRule, zBrokenType )
  171. {}
  172. void BasicBlockItemType::loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const
  173. {
  174. ItemType::loadSuperItem( zItem, zReader );
  175. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  176. if( !item )
  177. throw "BasicBlockItemType::loadSuperItem was called with an invalid item";
  178. zReader->lese( (char*)&item->transparent, 1 );
  179. zReader->lese( (char*)&item->passable, 1 );
  180. zReader->lese( (char*)&item->hp, 4 );
  181. zReader->lese( (char*)&item->maxHP, 4 );
  182. zReader->lese( (char*)&item->hardness, 4 );
  183. zReader->lese( (char*)&item->toolId, 4 );
  184. zReader->lese( (char*)&item->speedModifier, 4 );
  185. }
  186. void BasicBlockItemType::saveSuperItem( const Item* zItem, Framework::StreamWriter* zWriter ) const
  187. {
  188. ItemType::saveSuperItem( zItem, zWriter );
  189. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  190. if( !item )
  191. throw "BasicBlockItemType::saveSuperItem was called with an invalid item";
  192. zWriter->schreibe( (char*)&item->transparent, 1 );
  193. zWriter->schreibe( (char*)&item->passable, 1 );
  194. zWriter->schreibe( (char*)&item->hp, 4 );
  195. zWriter->schreibe( (char*)&item->maxHP, 4 );
  196. zWriter->schreibe( (char*)&item->hardness, 4 );
  197. zWriter->schreibe( (char*)&item->toolId, 4 );
  198. zWriter->schreibe( (char*)&item->speedModifier, 4 );
  199. }
  200. void BasicBlockItemType::initializeItem( BasicBlockItem* zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier ) const
  201. {
  202. zItem->transparent = transparent;
  203. zItem->passable = passable;
  204. zItem->hp = hp;
  205. zItem->maxHP = maxHP;
  206. zItem->hardness = hardness;
  207. zItem->toolId = toolId;
  208. zItem->speedModifier = speedModifier;
  209. }