Block.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. void Block::getLightEmisionColor(unsigned char* result) const
  349. {
  350. result[0] = lightEmisionColor[0];
  351. result[1] = lightEmisionColor[0];
  352. result[2] = lightEmisionColor[0];
  353. }
  354. void Block::filterPassingLight(unsigned char rgb[3]) const
  355. {
  356. if (!transparent) // let no light pass intransparent blocks
  357. memset(rgb, 0, 3);
  358. }
  359. Block* Block::zNeighbor(Direction dir) const
  360. {
  361. return zNeighbours[getDirectionIndex(dir)];
  362. }
  363. void Block::updateModel(ModelInfo* zInfo) const
  364. {
  365. Dimension* dim = Game::INSTANCE->zDimension(getDimensionId());
  366. if (dim)
  367. {
  368. Chunk* zChunk
  369. = dim->zChunk(Game::getChunkCenter(getPos().x, getPos().y));
  370. if (zChunk)
  371. {
  372. NetworkMessage* changeMsg = new NetworkMessage();
  373. changeMsg->addressBlock(this);
  374. Framework::InMemoryBuffer buffer;
  375. zInfo->writeTo(&buffer);
  376. char* msg = new char[(int)buffer.getSize() + 1];
  377. msg[0] = 1; // hmodel change
  378. buffer.lese(msg + 1, (int)buffer.getSize());
  379. changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
  380. zChunk->notifyObservers(changeMsg);
  381. }
  382. }
  383. }
  384. int Block::getMapColor() const
  385. {
  386. return mapColor;
  387. }
  388. BasicBlockItem::BasicBlockItem(int itemTypeId,
  389. int blockTypeId,
  390. Framework::Text name,
  391. PlaceableProof* placeableProof)
  392. : Item(itemTypeId, name),
  393. transparent(0),
  394. passable(0),
  395. hardness(1.f),
  396. speedModifier(1.f),
  397. interactable(1),
  398. placeableProof(placeableProof)
  399. {
  400. this->blockTypeId = blockTypeId;
  401. placeable = 1;
  402. }
  403. BasicBlockItem::~BasicBlockItem()
  404. {
  405. if (placeableProof) placeableProof->release();
  406. }
  407. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  408. {
  409. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  410. if (item)
  411. {
  412. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  413. && passable == item->passable && hardness == item->hardness
  414. && speedModifier == item->speedModifier
  415. && interactable == item->interactable;
  416. }
  417. return 0;
  418. }
  419. bool BasicBlockItem::canBePlacedAt(
  420. int dimensionId, Framework::Vec3<int> worldPos) const
  421. {
  422. return Item::canBePlacedAt(dimensionId, worldPos)
  423. && (!placeableProof
  424. || placeableProof->isPlacable(this, worldPos, dimensionId));
  425. }
  426. BasicBlockItemType::BasicBlockItemType()
  427. : ItemType(),
  428. transparent(0),
  429. passable(0),
  430. hardness(1.f),
  431. speedModifier(1.f),
  432. blockTypeName(""),
  433. placeableProof(0)
  434. {}
  435. BasicBlockItemType::BasicBlockItemType(Framework::Text name,
  436. ModelInfo* model,
  437. bool transparent,
  438. bool passable,
  439. float hardness,
  440. float speedModifier,
  441. Framework::Text blockTypeName,
  442. PlaceableProof* placeableProof,
  443. int maxStackSize,
  444. Framework::RCArray<Framework::Text> groups)
  445. : ItemType(),
  446. transparent(transparent),
  447. passable(passable),
  448. hardness(hardness),
  449. speedModifier(speedModifier),
  450. blockTypeName(blockTypeName),
  451. placeableProof(placeableProof)
  452. {
  453. setName(name);
  454. setModel(model);
  455. setMaxStackSize(maxStackSize);
  456. for (Framework::Text* group : groups)
  457. addGroup(*group);
  458. }
  459. BasicBlockItemType::~BasicBlockItemType()
  460. {
  461. if (placeableProof) placeableProof->release();
  462. }
  463. bool BasicBlockItemType::initialize(Game* zGame)
  464. {
  465. blockTypeId = zGame->getBlockTypeId(blockTypeName);
  466. return blockTypeId >= 0 && ItemType::initialize(zGame);
  467. }
  468. int BasicBlockItemType::getBlockTypeId() const
  469. {
  470. return blockTypeId;
  471. }
  472. void BasicBlockItemType::setTransparent(bool transparent)
  473. {
  474. this->transparent = transparent;
  475. }
  476. bool BasicBlockItemType::isTransparent() const
  477. {
  478. return transparent;
  479. }
  480. void BasicBlockItemType::setPassable(bool passable)
  481. {
  482. this->passable = passable;
  483. }
  484. bool BasicBlockItemType::isPassable() const
  485. {
  486. return passable;
  487. }
  488. void BasicBlockItemType::setHardness(float hardness)
  489. {
  490. this->hardness = hardness;
  491. }
  492. float BasicBlockItemType::getHardness() const
  493. {
  494. return hardness;
  495. }
  496. void BasicBlockItemType::setSpeedModifier(float speedModifier)
  497. {
  498. this->speedModifier = speedModifier;
  499. }
  500. float BasicBlockItemType::getSpeedModifier() const
  501. {
  502. return speedModifier;
  503. }
  504. void BasicBlockItemType::setBlockTypeName(Framework::Text blockTypeName)
  505. {
  506. this->blockTypeName = blockTypeName;
  507. }
  508. Framework::Text BasicBlockItemType::getBlockTypeName() const
  509. {
  510. return blockTypeName;
  511. }
  512. void BasicBlockItemType::setPlaceableProof(PlaceableProof* placeableProof)
  513. {
  514. if (this->placeableProof) this->placeableProof->release();
  515. this->placeableProof = placeableProof;
  516. }
  517. PlaceableProof* BasicBlockItemType::zPlaceableProof() const
  518. {
  519. return placeableProof;
  520. }
  521. void BasicBlockItemType::loadSuperItem(
  522. Item* zItem, Framework::StreamReader* zReader) const
  523. {
  524. ItemType::loadSuperItem(zItem, zReader);
  525. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  526. if (!item)
  527. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  528. "item";
  529. zReader->lese((char*)&item->transparent, 1);
  530. zReader->lese((char*)&item->passable, 1);
  531. zReader->lese((char*)&item->hardness, 4);
  532. zReader->lese((char*)&item->speedModifier, 4);
  533. zReader->lese((char*)&item->interactable, 1);
  534. }
  535. void BasicBlockItemType::saveSuperItem(
  536. const Item* zItem, Framework::StreamWriter* zWriter) const
  537. {
  538. ItemType::saveSuperItem(zItem, zWriter);
  539. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  540. if (!item)
  541. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  542. "item";
  543. zWriter->schreibe((char*)&item->transparent, 1);
  544. zWriter->schreibe((char*)&item->passable, 1);
  545. zWriter->schreibe((char*)&item->hardness, 4);
  546. zWriter->schreibe((char*)&item->speedModifier, 4);
  547. zWriter->schreibe((char*)&item->interactable, 1);
  548. }
  549. Item* BasicBlockItemType::createItem() const
  550. {
  551. BasicBlockItem* item = new BasicBlockItem(id,
  552. blockTypeId,
  553. name,
  554. placeableProof
  555. ? dynamic_cast<PlaceableProof*>(placeableProof->getThis())
  556. : 0);
  557. item->transparent = transparent;
  558. item->passable = passable;
  559. item->hardness = hardness;
  560. item->speedModifier = speedModifier;
  561. item->interactable = 1;
  562. return item;
  563. }
  564. BasicBlockItemTypeFactory::BasicBlockItemTypeFactory()
  565. : ItemTypeFactoryBase()
  566. {}
  567. BasicBlockItemType* BasicBlockItemTypeFactory::createValue(
  568. Framework::JSON::JSONObject* zJson) const
  569. {
  570. return new BasicBlockItemType();
  571. }
  572. BasicBlockItemType* BasicBlockItemTypeFactory::fromJson(
  573. Framework::JSON::JSONObject* zJson) const
  574. {
  575. BasicBlockItemType* result = ItemTypeFactoryBase::fromJson(zJson);
  576. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  577. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  578. result->setHardness(
  579. (float)zJson->zValue("hardness")->asNumber()->getNumber());
  580. result->setSpeedModifier(
  581. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  582. result->setBlockTypeName(
  583. zJson->zValue("blockType")->asString()->getString());
  584. result->setPlaceableProof(
  585. zJson->zValue("placeableProof")->getType()
  586. == Framework::AbstractType::OBJECT
  587. ? Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(
  588. zJson->zValue("placeableProof"))
  589. : 0);
  590. return result;
  591. }
  592. Framework::JSON::JSONObject* BasicBlockItemTypeFactory::toJsonObject(
  593. BasicBlockItemType* zObject) const
  594. {
  595. Framework::JSON::JSONObject* result
  596. = ItemTypeFactoryBase::toJsonObject(zObject);
  597. result->addValue(
  598. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  599. result->addValue(
  600. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  601. result->addValue(
  602. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  603. result->addValue("speedModifier",
  604. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  605. result->addValue("blockType",
  606. new Framework::JSON::JSONString(zObject->getBlockTypeName()));
  607. result->addValue("placeableProof",
  608. zObject->zPlaceableProof() ? Game::INSTANCE->zTypeRegistry()->toJson(
  609. zObject->zPlaceableProof())
  610. : new Framework::JSON::JSONValue());
  611. return result;
  612. }
  613. JSONObjectValidationBuilder* BasicBlockItemTypeFactory::addToValidator(
  614. JSONObjectValidationBuilder* builder) const
  615. {
  616. return ItemTypeFactoryBase::addToValidator(
  617. builder->withRequiredBool("transparent")
  618. ->withDefault(false)
  619. ->finishBool()
  620. ->withRequiredBool("passable")
  621. ->withDefault(false)
  622. ->finishBool()
  623. ->withRequiredNumber("hardness")
  624. ->withDefault(1.f)
  625. ->finishNumber()
  626. ->withRequiredNumber("speedModifier")
  627. ->withDefault(1.f)
  628. ->finishNumber()
  629. ->withRequiredString("blockType")
  630. ->finishString()
  631. ->withRequiredAttribute("placeableProof",
  632. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>())
  633. ->withRequiredObject("placeableProof")
  634. ->withDefaultNull()
  635. ->whichCanBeNull()
  636. ->finishObject());
  637. }
  638. const char* BasicBlockItemTypeFactory::getTypeToken() const
  639. {
  640. return "placeable";
  641. }