Block.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "Block.h"
  2. #include "AddEntityUpdate.h"
  3. #include "Game.h"
  4. #include "Inventory.h"
  5. #include "ItemEntity.h"
  6. #include "MultiblockStructure.h"
  7. #include "NoBlock.h"
  8. Block::Block(int typeId,
  9. const ItemType* zTool,
  10. Framework::Vec3<int> pos,
  11. bool hasInventory)
  12. : Inventory(pos, hasInventory)
  13. {
  14. transparent = false;
  15. passable = false;
  16. hp = 1;
  17. maxHP = 1;
  18. hardness = 1;
  19. this->typeId = typeId;
  20. this->zTool = zTool;
  21. speedModifier = 1;
  22. ticksLeftCounter = 0;
  23. wasTicked = 0;
  24. onTickCalled = 0;
  25. minTickTimeout = -1;
  26. maxTickTimeout = -1;
  27. tickSource = 0;
  28. currentTickTimeout = 0;
  29. dimensionId = 0;
  30. interactable = 0;
  31. deadAndRemoved = 0;
  32. memset(zNeighbours, 0, sizeof(Block*) * 6);
  33. memset(lightEmisionColor, 0, 3);
  34. }
  35. Block::~Block() {}
  36. void Block::onDestroy()
  37. {
  38. if (!deadAndRemoved)
  39. {
  40. for (int i = 0; i < 6; i++)
  41. {
  42. if (neighbourTypes[i] == BlockTypeEnum::NO_BLOCK)
  43. {
  44. Framework::Vec3<int> pos
  45. = getPos() + getDirection(getDirectionFromIndex(i));
  46. Game::INSTANCE->zDimension(dimensionId)
  47. ->placeBlock(pos,
  48. Game::INSTANCE->zGenerator()->generateSingleBlock(
  49. pos, dimensionId));
  50. }
  51. }
  52. Item* blockItem = zBlockType()->getItemFromBlock(this);
  53. if (blockItem)
  54. {
  55. ItemEntity* itemEntity
  56. = (ItemEntity*)StaticRegistry<EntityType>::INSTANCE
  57. .zElement(EntityTypeEnum::ITEM)
  58. ->createEntity(
  59. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  60. dimensionId,
  61. Game::INSTANCE->getNextEntityId());
  62. ItemStack* stack
  63. = new ItemStack(blockItem, 1, blockItem->getMaxStackSize());
  64. itemEntity->unsaveAddItem(stack, NO_DIRECTION);
  65. stack->release();
  66. Game::INSTANCE->requestWorldUpdate(
  67. new AddEntityUpdate(itemEntity, dimensionId));
  68. deadAndRemoved = 1;
  69. }
  70. for (MultiblockStructure* structure : structures)
  71. structure->onBlockRemoved(this);
  72. Game::INSTANCE->zDimension(dimensionId)
  73. ->placeBlock(
  74. getPos(), BlockTypeEnum::AIR); // this will be deleted here
  75. }
  76. }
  77. void Block::tick(TickQueue* zQueue)
  78. {
  79. if (wasTicked) return;
  80. wasTicked = 1;
  81. ticksLeftCounter++;
  82. if (minTickTimeout >= 0)
  83. {
  84. if (currentTickTimeout < ticksLeftCounter)
  85. {
  86. onTickCalled = 1;
  87. bool blocked = 0;
  88. bool result = onTick(zQueue, ticksLeftCounter, blocked);
  89. if (blocked)
  90. {
  91. wasTicked = 0;
  92. ticksLeftCounter--;
  93. onTickCalled = 0;
  94. zQueue->addToQueue(this);
  95. return;
  96. }
  97. if (result)
  98. currentTickTimeout
  99. = MAX(MIN(currentTickTimeout - 1, maxTickTimeout),
  100. MAX(minTickTimeout, 0));
  101. else
  102. currentTickTimeout
  103. = MAX(MIN(currentTickTimeout + 1, maxTickTimeout),
  104. MAX(minTickTimeout, 0));
  105. ticksLeftCounter = 0;
  106. }
  107. }
  108. else
  109. {
  110. onTickCalled = 1;
  111. bool blocked = 0;
  112. onTick(zQueue, 1, blocked);
  113. if (blocked)
  114. {
  115. wasTicked = 0;
  116. onTickCalled = 0;
  117. zQueue->addToQueue(this);
  118. return;
  119. }
  120. }
  121. }
  122. void Block::postTick()
  123. {
  124. wasTicked = 0;
  125. if (onTickCalled)
  126. {
  127. onPostTick();
  128. onTickCalled = 0;
  129. }
  130. }
  131. void Block::setNeighbour(
  132. Direction dir, Framework::Either<Block*, int> neighbour)
  133. {
  134. if (neighbour.isA())
  135. setNeighbourBlock(dir, neighbour);
  136. else
  137. {
  138. setNeighbourBlock(dir, 0);
  139. setNeighbourType(dir, neighbour);
  140. }
  141. }
  142. void Block::setNeighbourBlock(Direction dir, Block* zN)
  143. {
  144. if (zN) setNeighbourType(dir, zN->zBlockType()->getId());
  145. zNeighbours[getDirectionIndex(dir)] = zN;
  146. }
  147. void Block::setNeighbourType(Direction dir, int type)
  148. {
  149. neighbourTypes[getDirectionIndex(dir)] = type;
  150. }
  151. void Block::setDimensionId(int id)
  152. {
  153. dimensionId = id;
  154. }
  155. void Block::addToStructure(MultiblockStructure* structure)
  156. {
  157. if (structure->isBlockMember(this))
  158. structures.add(structure);
  159. else
  160. structure->release();
  161. }
  162. void Block::onLoaded()
  163. {
  164. for (MultiblockStructure* structure : structures)
  165. structure->onBlockLoaded(dynamic_cast<Block*>(getThis()));
  166. }
  167. void Block::onUnloaded()
  168. {
  169. for (MultiblockStructure* structure : structures)
  170. structure->onBlockUnloaded(this);
  171. }
  172. Framework::Text Block::getTargetUIML()
  173. {
  174. return StaticRegistry<BlockType>::INSTANCE.zElement(typeId)
  175. ->getTargetUIML();
  176. }
  177. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  178. {
  179. // TODO: answer api requests
  180. }
  181. bool Block::isTickSource() const
  182. {
  183. return tickSource;
  184. }
  185. const BlockType* Block::zBlockType() const
  186. {
  187. return StaticRegistry<BlockType>::INSTANCE.zElement(typeId);
  188. }
  189. bool Block::isTransparent() const
  190. {
  191. return transparent;
  192. }
  193. bool Block::isPassable() const
  194. {
  195. return passable;
  196. }
  197. bool Block::isInteractable() const
  198. {
  199. return interactable;
  200. }
  201. float Block::getHP() const
  202. {
  203. return hp;
  204. }
  205. float Block::getMaxHP() const
  206. {
  207. return maxHP;
  208. }
  209. float Block::getHardness() const
  210. {
  211. return hardness;
  212. }
  213. const ItemType* Block::zEffectiveTool() const
  214. {
  215. return zTool;
  216. }
  217. float Block::getSpeedModifier() const
  218. {
  219. return speedModifier;
  220. }
  221. const Framework::Vec3<int> Block::getPos() const
  222. {
  223. return (Framework::Vec3<int>)location;
  224. }
  225. int Block::getDimensionId() const
  226. {
  227. return dimensionId;
  228. }
  229. bool Block::isVisible() const
  230. {
  231. if (passable || transparent) return 1;
  232. for (int i = 0; i < 6; i++)
  233. {
  234. const Block* neighbour = CONST_BLOCK(zNeighbours[i], neighbourTypes[i]);
  235. if (neighbour->isPassable() || neighbour->isTransparent()) return 1;
  236. }
  237. return 0;
  238. }
  239. void Block::setHP(float hp)
  240. {
  241. bool isDead = this->hp == 0.f;
  242. this->hp = MAX(0.f, hp);
  243. if (!isDead && this->hp == 0.f)
  244. {
  245. onDestroy(); // this will be deleted
  246. }
  247. else
  248. {
  249. NetworkMessage* changeMsg = new NetworkMessage();
  250. changeMsg->addressBlock(this);
  251. char* msg = new char[5];
  252. msg[0] = 0; // hp changed
  253. *(float*)(msg + 1) = this->hp;
  254. changeMsg->setMessage(msg, 5);
  255. Game::INSTANCE->broadcastMessage(changeMsg);
  256. }
  257. }
  258. bool Block::isDeadAndRemoved() const
  259. {
  260. return deadAndRemoved;
  261. }
  262. const unsigned char* Block::getLightEmisionColor() const
  263. {
  264. return lightEmisionColor;
  265. }
  266. void Block::filterPassingLight(unsigned char rgb[3]) const
  267. {
  268. if (!transparent) // let no light pass intransparent blocks
  269. memset(rgb, 0, 3);
  270. }
  271. Block* Block::zNeighbor(Direction dir) const
  272. {
  273. return zNeighbours[getDirectionIndex(dir)];
  274. }
  275. void Block::updateModel(ModelInfo info) const
  276. {
  277. NetworkMessage* changeMsg = new NetworkMessage();
  278. changeMsg->addressBlock(this);
  279. InMemoryBuffer buffer;
  280. info.writeTo(&buffer);
  281. char* msg = new char[(int)buffer.getSize() + 1];
  282. msg[0] = 1; // hmodel change
  283. buffer.lese(msg + 1, (int)buffer.getSize());
  284. changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
  285. Game::INSTANCE->broadcastMessage(changeMsg);
  286. }
  287. BasicBlockItem::BasicBlockItem(
  288. int itemTypeId, int blockTypeId, const char* name)
  289. : Item(itemTypeId, name),
  290. transparent(0),
  291. passable(0),
  292. hardness(1.f),
  293. toolId(0),
  294. speedModifier(1.f),
  295. interactable(1)
  296. {
  297. this->blockTypeId = blockTypeId;
  298. placeable = 1;
  299. placableProof = [this](const Item* self,
  300. int dimensionId,
  301. Framework::Vec3<int> worldPos) {
  302. return Item::canBePlacedAt(dimensionId, worldPos);
  303. };
  304. }
  305. void BasicBlockItem::setPlacableProof(
  306. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  307. bool andDefault)
  308. {
  309. if (andDefault)
  310. {
  311. placableProof = [this, condition](const Item* self,
  312. int dimensionId,
  313. Framework::Vec3<int> worldPos) {
  314. return Item::canBePlacedAt(dimensionId, worldPos)
  315. && condition(self, dimensionId, worldPos);
  316. };
  317. }
  318. else
  319. {
  320. placableProof = condition;
  321. }
  322. }
  323. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  324. {
  325. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  326. if (item)
  327. {
  328. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  329. && passable == item->passable && hardness == item->hardness
  330. && toolId == item->toolId && speedModifier == item->speedModifier
  331. && interactable == item->interactable;
  332. }
  333. return 0;
  334. }
  335. bool BasicBlockItem::canBePlacedAt(
  336. int dimensionId, Framework::Vec3<int> worldPos) const
  337. {
  338. return placableProof(this, dimensionId, worldPos);
  339. }
  340. BasicBlockItemType::BasicBlockItemType(int id,
  341. const char* name,
  342. ItemSkillLevelUpRule* levelUpRule,
  343. int brokenTypeId,
  344. ModelInfo model,
  345. int blockTypeId)
  346. : ItemType(id, name, levelUpRule, brokenTypeId, model),
  347. transparent(0),
  348. passable(0),
  349. hardness(1.f),
  350. toolId(0),
  351. speedModifier(1.f),
  352. blockTypeId(blockTypeId)
  353. {}
  354. void BasicBlockItemType::loadSuperItem(
  355. Item* zItem, Framework::StreamReader* zReader) const
  356. {
  357. ItemType::loadSuperItem(zItem, zReader);
  358. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  359. if (!item)
  360. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  361. "item";
  362. zReader->lese((char*)&item->transparent, 1);
  363. zReader->lese((char*)&item->passable, 1);
  364. zReader->lese((char*)&item->hardness, 4);
  365. zReader->lese((char*)&item->toolId, 4);
  366. zReader->lese((char*)&item->speedModifier, 4);
  367. zReader->lese((char*)&item->interactable, 1);
  368. }
  369. void BasicBlockItemType::saveSuperItem(
  370. const Item* zItem, Framework::StreamWriter* zWriter) const
  371. {
  372. ItemType::saveSuperItem(zItem, zWriter);
  373. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  374. if (!item)
  375. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  376. "item";
  377. zWriter->schreibe((char*)&item->transparent, 1);
  378. zWriter->schreibe((char*)&item->passable, 1);
  379. zWriter->schreibe((char*)&item->hardness, 4);
  380. zWriter->schreibe((char*)&item->toolId, 4);
  381. zWriter->schreibe((char*)&item->speedModifier, 4);
  382. zWriter->schreibe((char*)&item->interactable, 1);
  383. }
  384. Item* BasicBlockItemType::createItem() const
  385. {
  386. BasicBlockItem* item = new BasicBlockItem(id, blockTypeId, name);
  387. item->transparent = transparent;
  388. item->passable = passable;
  389. item->hardness = hardness;
  390. item->toolId = toolId;
  391. item->speedModifier = speedModifier;
  392. item->interactable = 1;
  393. if (placableProofState)
  394. {
  395. item->setPlacableProof(placableProof, placableProofState > 1);
  396. }
  397. return item;
  398. }
  399. BasicBlockItemType* BasicBlockItemType::setHardness(float hardness)
  400. {
  401. this->hardness = hardness;
  402. return this;
  403. }
  404. BasicBlockItemType* BasicBlockItemType::setPlacableProof(
  405. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  406. bool andDefault)
  407. {
  408. placableProofState = 1 + (andDefault ? 1 : 0);
  409. placableProof = condition;
  410. return this;
  411. }