Block.cpp 6.9 KB

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