Block.cpp 6.3 KB

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