Block.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. void Block::interact(Item* zItem, Entity* zActor) {}
  175. void Block::api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  176. {
  177. char id = 0;
  178. zRequest->lese(&id, 1);
  179. switch (id)
  180. {
  181. case 0:
  182. // request model state
  183. sendModelInfo(zResponse);
  184. break;
  185. case 1: // dialog closed
  186. short nameLen;
  187. zRequest->lese((char*)&nameLen, 2);
  188. char* name = new char[nameLen + 1];
  189. zRequest->lese(name, nameLen);
  190. name[nameLen] = 0;
  191. onDialogClosed(name);
  192. delete[] name;
  193. break;
  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 Item* zItem) 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. bool Block::isVisible() const
  241. {
  242. if (passable || transparent) return 1;
  243. for (int i = 0; i < 6; i++)
  244. {
  245. const Block* neighbour = CONST_BLOCK(zNeighbours[i], neighbourTypes[i]);
  246. if (neighbour->isPassable() || neighbour->isTransparent()) return 1;
  247. }
  248. return 0;
  249. }
  250. void Block::setHP(float hp)
  251. {
  252. bool isDead = this->hp == 0.f;
  253. this->hp = MAX(0.f, hp);
  254. if (!isDead && this->hp == 0.f)
  255. {
  256. onDestroy(); // this will be deleted
  257. }
  258. else
  259. {
  260. NetworkMessage* changeMsg = new NetworkMessage();
  261. changeMsg->addressBlock(this);
  262. char* msg = new char[5];
  263. msg[0] = 0; // hp changed
  264. *(float*)(msg + 1) = this->hp;
  265. changeMsg->setMessage(msg, 5);
  266. Game::INSTANCE->broadcastMessage(changeMsg);
  267. }
  268. }
  269. bool Block::isDeadAndRemoved() const
  270. {
  271. return deadAndRemoved;
  272. }
  273. const unsigned char* Block::getLightEmisionColor() const
  274. {
  275. return lightEmisionColor;
  276. }
  277. void Block::filterPassingLight(unsigned char rgb[3]) const
  278. {
  279. if (!transparent) // let no light pass intransparent blocks
  280. memset(rgb, 0, 3);
  281. }
  282. Block* Block::zNeighbor(Direction dir) const
  283. {
  284. return zNeighbours[getDirectionIndex(dir)];
  285. }
  286. void Block::updateModel(ModelInfo info) const
  287. {
  288. NetworkMessage* changeMsg = new NetworkMessage();
  289. changeMsg->addressBlock(this);
  290. InMemoryBuffer buffer;
  291. info.writeTo(&buffer);
  292. char* msg = new char[(int)buffer.getSize() + 1];
  293. msg[0] = 1; // hmodel change
  294. buffer.lese(msg + 1, (int)buffer.getSize());
  295. changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
  296. Game::INSTANCE->broadcastMessage(changeMsg);
  297. }
  298. int Block::getMapColor() const
  299. {
  300. return mapColor;
  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. placableProofState(0)
  369. {}
  370. void BasicBlockItemType::loadSuperItem(
  371. Item* zItem, Framework::StreamReader* zReader) const
  372. {
  373. ItemType::loadSuperItem(zItem, zReader);
  374. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  375. if (!item)
  376. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  377. "item";
  378. zReader->lese((char*)&item->transparent, 1);
  379. zReader->lese((char*)&item->passable, 1);
  380. zReader->lese((char*)&item->hardness, 4);
  381. zReader->lese((char*)&item->toolId, 4);
  382. zReader->lese((char*)&item->speedModifier, 4);
  383. zReader->lese((char*)&item->interactable, 1);
  384. }
  385. void BasicBlockItemType::saveSuperItem(
  386. const Item* zItem, Framework::StreamWriter* zWriter) const
  387. {
  388. ItemType::saveSuperItem(zItem, zWriter);
  389. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  390. if (!item)
  391. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  392. "item";
  393. zWriter->schreibe((char*)&item->transparent, 1);
  394. zWriter->schreibe((char*)&item->passable, 1);
  395. zWriter->schreibe((char*)&item->hardness, 4);
  396. zWriter->schreibe((char*)&item->toolId, 4);
  397. zWriter->schreibe((char*)&item->speedModifier, 4);
  398. zWriter->schreibe((char*)&item->interactable, 1);
  399. }
  400. Item* BasicBlockItemType::createItem() const
  401. {
  402. BasicBlockItem* item = new BasicBlockItem(id, blockTypeId, name);
  403. item->transparent = transparent;
  404. item->passable = passable;
  405. item->hardness = hardness;
  406. item->toolId = toolId;
  407. item->speedModifier = speedModifier;
  408. item->interactable = 1;
  409. if (placableProofState)
  410. {
  411. item->setPlacableProof(placableProof, placableProofState > 1);
  412. }
  413. return item;
  414. }
  415. BasicBlockItemType* BasicBlockItemType::setHardness(float hardness)
  416. {
  417. this->hardness = hardness;
  418. return this;
  419. }
  420. BasicBlockItemType* BasicBlockItemType::setPlacableProof(
  421. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  422. bool andDefault)
  423. {
  424. placableProofState = 1 + (andDefault ? 1 : 0);
  425. placableProof = condition;
  426. return this;
  427. }