Block.cpp 5.5 KB

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