Block.cpp 5.0 KB

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