Block.cpp 4.8 KB

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