Block.cpp 12 KB

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