Block.cpp 12 KB

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