Block.cpp 12 KB

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