Block.cpp 18 KB

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