Block.cpp 13 KB

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