Block.cpp 7.0 KB

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