Block.cpp 12 KB

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