Block.cpp 4.9 KB

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