Block.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #include "Block.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. #include "InteractionConfig.h"
  5. #include "Inventory.h"
  6. #include "ItemEntity.h"
  7. #include "MultiblockStructure.h"
  8. #include "NoBlock.h"
  9. #include "PlaceableProof.h"
  10. #include "TickQueue.h"
  11. #include "WorldGenerator.h"
  12. Block::Block(
  13. int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
  14. : Inventory(pos, dimensionId, hasInventory)
  15. {
  16. transparent = false;
  17. passable = false;
  18. hp = 1;
  19. maxHP = 1;
  20. hardness = 1;
  21. this->typeId = typeId;
  22. speedModifier = 1;
  23. ticksLeftCounter = 0;
  24. wasTicked = 0;
  25. onTickCalled = 0;
  26. minTickTimeout = -1;
  27. maxTickTimeout = -1;
  28. currentTickTimeout = 0;
  29. interactable = 0;
  30. deadAndRemoved = 0;
  31. memset(zNeighbours, 0, sizeof(Block*) * 6);
  32. memset(lightEmisionColor, 0, 3);
  33. mapColor = 0;
  34. }
  35. Block::~Block() {}
  36. void Block::onDestroy(Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill)
  37. {
  38. if (!deadAndRemoved)
  39. {
  40. for (int i = 0; i < 6; i++)
  41. {
  42. Framework::Vec3<int> pos
  43. = getPos() + getDirection(getDirectionFromIndex(i));
  44. if (neighbourTypes[i] == BlockTypeEnum::NO_BLOCK)
  45. {
  46. Game::INSTANCE->zDimension(dimensionId)
  47. ->placeBlock(pos,
  48. Game::INSTANCE->zGenerator()->generateSingleBlock(
  49. pos, dimensionId));
  50. }
  51. else
  52. {
  53. Game::INSTANCE->zDimension(dimensionId)->sendBlockInfo(pos);
  54. }
  55. }
  56. getThis();
  57. if (zActor)
  58. {
  59. for (const DropConfig* config : zBlockType()->getDropConfigs())
  60. {
  61. config->onObjectDestroyed(zActor,
  62. zUsedItem,
  63. zUsedSkill,
  64. this); // a nother block might replace this block during the
  65. // drops
  66. }
  67. }
  68. Framework::Either<Block*, int> block
  69. = Game::INSTANCE->zBlockAt(getPos(), dimensionId, 0);
  70. deadAndRemoved = 1;
  71. if (block.isA()
  72. && block.getA() == this) // no other block has replaced this block
  73. {
  74. for (MultiblockStructure* structure : structures)
  75. structure->onBlockRemoved(zActor, zUsedItem, zUsedSkill, this);
  76. Game::INSTANCE->zDimension(dimensionId)
  77. ->placeBlock(getPos(), BlockTypeEnum::AIR);
  78. }
  79. else
  80. {
  81. if (structures.getEintragAnzahl() > 0)
  82. { // replace this block in the structures
  83. Block* zReplacement = block.isA()
  84. ? block.getA()
  85. : Game::INSTANCE->zRealBlockInstance(
  86. getPos(), dimensionId);
  87. for (MultiblockStructure* structure : structures)
  88. structure->onBlockReplaced(
  89. zActor, zUsedItem, zUsedSkill, this, zReplacement);
  90. }
  91. }
  92. release();
  93. }
  94. }
  95. void Block::onDialogClosed(Framework::Text dialogId) {}
  96. void Block::broadcastModelInfoChange()
  97. {
  98. NetworkMessage* message = new NetworkMessage();
  99. sendModelInfo(message);
  100. broadcastMessage(message);
  101. }
  102. void Block::broadcastMessage(NetworkMessage* message)
  103. {
  104. if (message->isEmpty())
  105. {
  106. message->release();
  107. }
  108. else
  109. {
  110. Dimension* dim = Game::INSTANCE->zDimension(getDimensionId());
  111. if (dim)
  112. {
  113. Chunk* zChunk
  114. = dim->zChunk(Game::getChunkCenter(getPos().x, getPos().y));
  115. if (zChunk)
  116. {
  117. zChunk->notifyObservers(message);
  118. }
  119. else
  120. {
  121. message->release();
  122. }
  123. }
  124. else
  125. {
  126. message->release();
  127. }
  128. }
  129. }
  130. void Block::broadcastPassableSpeedModifierChange()
  131. {
  132. NetworkMessage* message = new NetworkMessage();
  133. message->addressBlock(this);
  134. char* msg = new char[6];
  135. msg[0] = 3;
  136. msg[1] = passable;
  137. *(float*)(msg + 2) = speedModifier;
  138. message->setMessage(msg, 6);
  139. broadcastMessage(message);
  140. }
  141. void Block::tick(TickQueue* zQueue)
  142. {
  143. if (wasTicked) return;
  144. wasTicked = 1;
  145. ticksLeftCounter++;
  146. if (minTickTimeout >= 0)
  147. {
  148. if (currentTickTimeout < ticksLeftCounter)
  149. {
  150. onTickCalled = 1;
  151. bool blocked = 0;
  152. bool result = onTick(zQueue, ticksLeftCounter, blocked);
  153. if (blocked)
  154. {
  155. wasTicked = 0;
  156. ticksLeftCounter--;
  157. onTickCalled = 0;
  158. zQueue->addToQueue(this);
  159. return;
  160. }
  161. if (result)
  162. currentTickTimeout
  163. = MAX(MIN(currentTickTimeout - 1, maxTickTimeout),
  164. MAX(minTickTimeout, 0));
  165. else
  166. currentTickTimeout
  167. = MAX(MIN(currentTickTimeout + 1, maxTickTimeout),
  168. MAX(minTickTimeout, 0));
  169. ticksLeftCounter = 0;
  170. }
  171. }
  172. else
  173. {
  174. onTickCalled = 1;
  175. bool blocked = 0;
  176. onTick(zQueue, 1, blocked);
  177. if (blocked)
  178. {
  179. wasTicked = 0;
  180. onTickCalled = 0;
  181. zQueue->addToQueue(this);
  182. return;
  183. }
  184. }
  185. }
  186. void Block::postTick()
  187. {
  188. wasTicked = 0;
  189. if (onTickCalled)
  190. {
  191. onPostTick();
  192. onTickCalled = 0;
  193. }
  194. }
  195. void Block::setNeighbour(
  196. Direction dir, Framework::Either<Block*, int> neighbour)
  197. {
  198. if (neighbour.isA())
  199. setNeighbourBlock(dir, neighbour);
  200. else
  201. {
  202. setNeighbourBlock(dir, 0);
  203. setNeighbourType(dir, neighbour);
  204. }
  205. }
  206. void Block::setNeighbourBlock(Direction dir, Block* zN)
  207. {
  208. if (zN) setNeighbourType(dir, zN->zBlockType()->getId());
  209. zNeighbours[getDirectionIndex(dir)] = zN;
  210. }
  211. void Block::setNeighbourType(Direction dir, int type)
  212. {
  213. neighbourTypes[getDirectionIndex(dir)] = type;
  214. }
  215. void Block::addToStructure(MultiblockStructure* structure)
  216. {
  217. if (structure->isBlockMember(this))
  218. structures.add(structure);
  219. else
  220. structure->release();
  221. }
  222. void Block::onLoaded()
  223. {
  224. for (MultiblockStructure* structure : structures)
  225. structure->onBlockLoaded(dynamic_cast<Block*>(getThis()));
  226. }
  227. void Block::onUnloaded()
  228. {
  229. for (MultiblockStructure* structure : structures)
  230. structure->onBlockUnloaded(this);
  231. }
  232. Framework::Text Block::getTargetUIML()
  233. {
  234. return Game::INSTANCE->zBlockType(typeId)->getTargetUIML();
  235. }
  236. void Block::sendModelInfo(NetworkMessage* zMessage)
  237. {
  238. // overwritten by some blocks
  239. }
  240. bool Block::interact(Item* zItem, Entity* zActor, bool& itemChanged)
  241. {
  242. bool result = 0;
  243. for (InteractionConfig* config : zBlockType()->getInteractionConfigs())
  244. {
  245. result |= config->doInteraction(this, zItem, zActor, itemChanged);
  246. }
  247. return false;
  248. }
  249. void Block::api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  250. {
  251. char id = 0;
  252. zRequest->lese(&id, 1);
  253. switch (id)
  254. {
  255. case 0:
  256. // request model state
  257. sendModelInfo(zResponse);
  258. break;
  259. case 1: // dialog closed
  260. short nameLen;
  261. zRequest->lese((char*)&nameLen, 2);
  262. char* name = new char[nameLen + 1];
  263. zRequest->lese(name, nameLen);
  264. name[nameLen] = 0;
  265. onDialogClosed(name);
  266. delete[] name;
  267. break;
  268. }
  269. }
  270. TickSourceType Block::isTickSource() const
  271. {
  272. return NONE;
  273. }
  274. bool Block::needsTick() const
  275. {
  276. return 1;
  277. }
  278. const BlockType* Block::zBlockType() const
  279. {
  280. return Game::INSTANCE->zBlockType(typeId);
  281. }
  282. bool Block::isTransparent() const
  283. {
  284. return transparent;
  285. }
  286. bool Block::isPassable() const
  287. {
  288. return passable;
  289. }
  290. bool Block::isInteractable(const Item* zItem) const
  291. {
  292. return interactable;
  293. }
  294. float Block::getHP() const
  295. {
  296. return hp;
  297. }
  298. float Block::getMaxHP() const
  299. {
  300. return maxHP;
  301. }
  302. float Block::getHardness() const
  303. {
  304. return hardness;
  305. }
  306. float Block::getSpeedModifier() const
  307. {
  308. return speedModifier;
  309. }
  310. const Framework::Vec3<int> Block::getPos() const
  311. {
  312. return (Framework::Vec3<int>)location;
  313. }
  314. bool Block::isVisible() const
  315. {
  316. if (passable || transparent) return 1;
  317. for (int i = 0; i < 6; i++)
  318. {
  319. const Block* neighbour = CONST_BLOCK(zNeighbours[i], neighbourTypes[i]);
  320. if (neighbour->isPassable() || neighbour->isTransparent()) return 1;
  321. }
  322. return 0;
  323. }
  324. void Block::setHP(
  325. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  326. {
  327. bool isDead = this->hp == 0.f;
  328. this->hp = MAX(0.f, hp);
  329. if (!isDead && this->hp == 0.f)
  330. {
  331. onDestroy(zActor, zUsedItem, zUsedSkill); // this will be deleted
  332. }
  333. else
  334. {
  335. NetworkMessage* changeMsg = new NetworkMessage();
  336. changeMsg->addressBlock(this);
  337. char* msg = new char[5];
  338. msg[0] = 0; // hp changed
  339. *(float*)(msg + 1) = this->hp;
  340. changeMsg->setMessage(msg, 5);
  341. Game::INSTANCE->broadcastMessage(changeMsg);
  342. }
  343. }
  344. bool Block::isDeadAndRemoved() const
  345. {
  346. return deadAndRemoved;
  347. }
  348. const unsigned char* Block::getLightEmisionColor() const
  349. {
  350. return lightEmisionColor;
  351. }
  352. void Block::filterPassingLight(unsigned char rgb[3]) const
  353. {
  354. if (!transparent) // let no light pass intransparent blocks
  355. memset(rgb, 0, 3);
  356. }
  357. Block* Block::zNeighbor(Direction dir) const
  358. {
  359. return zNeighbours[getDirectionIndex(dir)];
  360. }
  361. void Block::updateModel(ModelInfo* zInfo) const
  362. {
  363. Dimension* dim = Game::INSTANCE->zDimension(getDimensionId());
  364. if (dim)
  365. {
  366. Chunk* zChunk
  367. = dim->zChunk(Game::getChunkCenter(getPos().x, getPos().y));
  368. if (zChunk)
  369. {
  370. NetworkMessage* changeMsg = new NetworkMessage();
  371. changeMsg->addressBlock(this);
  372. Framework::InMemoryBuffer buffer;
  373. zInfo->writeTo(&buffer);
  374. char* msg = new char[(int)buffer.getSize() + 1];
  375. msg[0] = 1; // hmodel change
  376. buffer.lese(msg + 1, (int)buffer.getSize());
  377. changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
  378. zChunk->notifyObservers(changeMsg);
  379. }
  380. }
  381. }
  382. int Block::getMapColor() const
  383. {
  384. return mapColor;
  385. }
  386. BasicBlockItem::BasicBlockItem(int itemTypeId,
  387. int blockTypeId,
  388. Framework::Text name,
  389. PlaceableProof* placeableProof)
  390. : Item(itemTypeId, name),
  391. transparent(0),
  392. passable(0),
  393. hardness(1.f),
  394. speedModifier(1.f),
  395. interactable(1),
  396. placeableProof(placeableProof)
  397. {
  398. this->blockTypeId = blockTypeId;
  399. placeable = 1;
  400. }
  401. BasicBlockItem::~BasicBlockItem()
  402. {
  403. if (placeableProof) placeableProof->release();
  404. }
  405. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  406. {
  407. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  408. if (item)
  409. {
  410. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  411. && passable == item->passable && hardness == item->hardness
  412. && speedModifier == item->speedModifier
  413. && interactable == item->interactable;
  414. }
  415. return 0;
  416. }
  417. bool BasicBlockItem::canBePlacedAt(
  418. int dimensionId, Framework::Vec3<int> worldPos) const
  419. {
  420. return Item::canBePlacedAt(dimensionId, worldPos)
  421. && (!placeableProof
  422. || placeableProof->isPlacable(this, worldPos, dimensionId));
  423. }
  424. BasicBlockItemType::BasicBlockItemType()
  425. : ItemType(),
  426. transparent(0),
  427. passable(0),
  428. hardness(1.f),
  429. speedModifier(1.f),
  430. blockTypeName(""),
  431. placeableProof(0)
  432. {}
  433. BasicBlockItemType::BasicBlockItemType(Framework::Text name,
  434. ModelInfo* model,
  435. bool transparent,
  436. bool passable,
  437. float hardness,
  438. float speedModifier,
  439. Framework::Text blockTypeName,
  440. PlaceableProof* placeableProof,
  441. int maxStackSize,
  442. Framework::RCArray<Framework::Text> groups)
  443. : ItemType(),
  444. transparent(transparent),
  445. passable(passable),
  446. hardness(hardness),
  447. speedModifier(speedModifier),
  448. blockTypeName(blockTypeName),
  449. placeableProof(placeableProof)
  450. {
  451. setName(name);
  452. setModel(model);
  453. setMaxStackSize(maxStackSize);
  454. for (Framework::Text* group : groups)
  455. addGroup(*group);
  456. }
  457. BasicBlockItemType::~BasicBlockItemType()
  458. {
  459. if (placeableProof) placeableProof->release();
  460. }
  461. bool BasicBlockItemType::initialize(Game* zGame)
  462. {
  463. blockTypeId = zGame->getBlockTypeId(blockTypeName);
  464. return blockTypeId >= 0 && ItemType::initialize(zGame);
  465. }
  466. int BasicBlockItemType::getBlockTypeId() const
  467. {
  468. return blockTypeId;
  469. }
  470. void BasicBlockItemType::setTransparent(bool transparent)
  471. {
  472. this->transparent = transparent;
  473. }
  474. bool BasicBlockItemType::isTransparent() const
  475. {
  476. return transparent;
  477. }
  478. void BasicBlockItemType::setPassable(bool passable)
  479. {
  480. this->passable = passable;
  481. }
  482. bool BasicBlockItemType::isPassable() const
  483. {
  484. return passable;
  485. }
  486. void BasicBlockItemType::setHardness(float hardness)
  487. {
  488. this->hardness = hardness;
  489. }
  490. float BasicBlockItemType::getHardness() const
  491. {
  492. return hardness;
  493. }
  494. void BasicBlockItemType::setSpeedModifier(float speedModifier)
  495. {
  496. this->speedModifier = speedModifier;
  497. }
  498. float BasicBlockItemType::getSpeedModifier() const
  499. {
  500. return speedModifier;
  501. }
  502. void BasicBlockItemType::setBlockTypeName(Framework::Text blockTypeName)
  503. {
  504. this->blockTypeName = blockTypeName;
  505. }
  506. Framework::Text BasicBlockItemType::getBlockTypeName() const
  507. {
  508. return blockTypeName;
  509. }
  510. void BasicBlockItemType::setPlaceableProof(PlaceableProof* placeableProof)
  511. {
  512. if (this->placeableProof) this->placeableProof->release();
  513. this->placeableProof = placeableProof;
  514. }
  515. PlaceableProof* BasicBlockItemType::zPlaceableProof() const
  516. {
  517. return placeableProof;
  518. }
  519. void BasicBlockItemType::loadSuperItem(
  520. Item* zItem, Framework::StreamReader* zReader) const
  521. {
  522. ItemType::loadSuperItem(zItem, zReader);
  523. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  524. if (!item)
  525. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  526. "item";
  527. zReader->lese((char*)&item->transparent, 1);
  528. zReader->lese((char*)&item->passable, 1);
  529. zReader->lese((char*)&item->hardness, 4);
  530. zReader->lese((char*)&item->speedModifier, 4);
  531. zReader->lese((char*)&item->interactable, 1);
  532. }
  533. void BasicBlockItemType::saveSuperItem(
  534. const Item* zItem, Framework::StreamWriter* zWriter) const
  535. {
  536. ItemType::saveSuperItem(zItem, zWriter);
  537. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  538. if (!item)
  539. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  540. "item";
  541. zWriter->schreibe((char*)&item->transparent, 1);
  542. zWriter->schreibe((char*)&item->passable, 1);
  543. zWriter->schreibe((char*)&item->hardness, 4);
  544. zWriter->schreibe((char*)&item->speedModifier, 4);
  545. zWriter->schreibe((char*)&item->interactable, 1);
  546. }
  547. Item* BasicBlockItemType::createItem() const
  548. {
  549. BasicBlockItem* item = new BasicBlockItem(id,
  550. blockTypeId,
  551. name,
  552. placeableProof
  553. ? dynamic_cast<PlaceableProof*>(placeableProof->getThis())
  554. : 0);
  555. item->transparent = transparent;
  556. item->passable = passable;
  557. item->hardness = hardness;
  558. item->speedModifier = speedModifier;
  559. item->interactable = 1;
  560. return item;
  561. }
  562. BasicBlockItemTypeFactory::BasicBlockItemTypeFactory()
  563. : ItemTypeFactoryBase()
  564. {}
  565. BasicBlockItemType* BasicBlockItemTypeFactory::createValue(
  566. Framework::JSON::JSONObject* zJson) const
  567. {
  568. return new BasicBlockItemType();
  569. }
  570. BasicBlockItemType* BasicBlockItemTypeFactory::fromJson(
  571. Framework::JSON::JSONObject* zJson) const
  572. {
  573. BasicBlockItemType* result = ItemTypeFactoryBase::fromJson(zJson);
  574. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  575. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  576. result->setHardness(
  577. (float)zJson->zValue("hardness")->asNumber()->getNumber());
  578. result->setSpeedModifier(
  579. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  580. result->setBlockTypeName(
  581. zJson->zValue("blockType")->asString()->getString());
  582. result->setPlaceableProof(
  583. zJson->zValue("placeableProof")->getType()
  584. == Framework::AbstractType::OBJECT
  585. ? Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(
  586. zJson->zValue("placeableProof"))
  587. : 0);
  588. return result;
  589. }
  590. Framework::JSON::JSONObject* BasicBlockItemTypeFactory::toJsonObject(
  591. BasicBlockItemType* zObject) const
  592. {
  593. Framework::JSON::JSONObject* result
  594. = ItemTypeFactoryBase::toJsonObject(zObject);
  595. result->addValue(
  596. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  597. result->addValue(
  598. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  599. result->addValue(
  600. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  601. result->addValue("speedModifier",
  602. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  603. result->addValue("blockType",
  604. new Framework::JSON::JSONString(zObject->getBlockTypeName()));
  605. result->addValue("placeableProof",
  606. zObject->zPlaceableProof() ? Game::INSTANCE->zTypeRegistry()->toJson(
  607. zObject->zPlaceableProof())
  608. : new Framework::JSON::JSONValue());
  609. return result;
  610. }
  611. JSONObjectValidationBuilder* BasicBlockItemTypeFactory::addToValidator(
  612. JSONObjectValidationBuilder* builder) const
  613. {
  614. return ItemTypeFactoryBase::addToValidator(
  615. builder->withRequiredBool("transparent")
  616. ->withDefault(false)
  617. ->finishBool()
  618. ->withRequiredBool("passable")
  619. ->withDefault(false)
  620. ->finishBool()
  621. ->withRequiredNumber("hardness")
  622. ->withDefault(1.f)
  623. ->finishNumber()
  624. ->withRequiredNumber("speedModifier")
  625. ->withDefault(1.f)
  626. ->finishNumber()
  627. ->withRequiredString("blockType")
  628. ->finishString()
  629. ->withRequiredAttribute("placeableProof",
  630. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>())
  631. ->withRequiredObject("placeableProof")
  632. ->withDefaultNull()
  633. ->whichCanBeNull()
  634. ->finishObject());
  635. }
  636. const char* BasicBlockItemTypeFactory::getTypeToken() const
  637. {
  638. return "placeable";
  639. }