Block.cpp 7.0 KB

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