Block.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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(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. for (int i = 0; i < 6; i++)
  40. {
  41. Framework::Vec3<int> pos
  42. = getPos() + getDirection(getDirectionFromIndex(i));
  43. int type = Game::INSTANCE->getBlockType(pos, getDimensionId());
  44. if (type == 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::onApiCall(char messageType,
  142. Framework::StreamReader* zRequest,
  143. NetworkMessage* zResponse,
  144. Entity* zSource)
  145. {}
  146. void Block::tick(TickQueue* zQueue)
  147. {
  148. if (wasTicked) return;
  149. wasTicked = 1;
  150. ticksLeftCounter++;
  151. if (minTickTimeout >= 0)
  152. {
  153. if (currentTickTimeout < ticksLeftCounter)
  154. {
  155. onTickCalled = 1;
  156. bool blocked = 0;
  157. bool result = onTick(zQueue, ticksLeftCounter, blocked);
  158. if (blocked)
  159. {
  160. wasTicked = 0;
  161. ticksLeftCounter--;
  162. onTickCalled = 0;
  163. zQueue->addToQueue(this);
  164. return;
  165. }
  166. if (result)
  167. currentTickTimeout
  168. = MAX(MIN(currentTickTimeout - 1, maxTickTimeout),
  169. MAX(minTickTimeout, 0));
  170. else
  171. currentTickTimeout
  172. = MAX(MIN(currentTickTimeout + 1, maxTickTimeout),
  173. MAX(minTickTimeout, 0));
  174. ticksLeftCounter = 0;
  175. }
  176. }
  177. else
  178. {
  179. onTickCalled = 1;
  180. bool blocked = 0;
  181. onTick(zQueue, 1, blocked);
  182. if (blocked)
  183. {
  184. wasTicked = 0;
  185. onTickCalled = 0;
  186. zQueue->addToQueue(this);
  187. return;
  188. }
  189. }
  190. }
  191. void Block::postTick()
  192. {
  193. wasTicked = 0;
  194. if (onTickCalled)
  195. {
  196. onPostTick();
  197. onTickCalled = 0;
  198. }
  199. }
  200. void Block::addToStructure(MultiblockStructure* structure)
  201. {
  202. if (structure->isBlockMember(this))
  203. structures.add(structure);
  204. else
  205. structure->release();
  206. }
  207. void Block::onLoaded()
  208. {
  209. for (MultiblockStructure* structure : structures)
  210. structure->onBlockLoaded(dynamic_cast<Block*>(getThis()));
  211. }
  212. void Block::onUnloaded()
  213. {
  214. for (MultiblockStructure* structure : structures)
  215. structure->onBlockUnloaded(this);
  216. }
  217. Framework::XML::Element* Block::getTargetUIML() const
  218. {
  219. return Game::INSTANCE->zBlockType(typeId)->getTargetUIML();
  220. }
  221. void Block::sendModelInfo(NetworkMessage* zMessage)
  222. {
  223. // overwritten by some blocks
  224. }
  225. bool Block::interact(Item* zItem, Entity* zActor, bool& itemChanged)
  226. {
  227. bool result = 0;
  228. for (InteractionConfig* config : zBlockType()->getInteractionConfigs())
  229. {
  230. result |= config->doInteraction(this, zItem, zActor, itemChanged);
  231. }
  232. return false;
  233. }
  234. void Block::api(Framework::StreamReader* zRequest,
  235. NetworkMessage* zResponse,
  236. Entity* zSource)
  237. {
  238. char id = 0;
  239. zRequest->lese(&id, 1);
  240. switch (id)
  241. {
  242. case 0:
  243. // request model state
  244. sendModelInfo(zResponse);
  245. break;
  246. case 1: // dialog closed
  247. {
  248. short nameLen;
  249. zRequest->lese((char*)&nameLen, 2);
  250. char* name = new char[nameLen + 1];
  251. zRequest->lese(name, nameLen);
  252. name[nameLen] = 0;
  253. onDialogClosed(name);
  254. delete[] name;
  255. break;
  256. }
  257. // 2 is handled in BasicBlock
  258. default: // component request handled in BasicBlock
  259. onApiCall(id, zRequest, zResponse, zSource);
  260. }
  261. }
  262. TickSourceType Block::isTickSource() const
  263. {
  264. return NONE;
  265. }
  266. bool Block::needsTick() const
  267. {
  268. return 1;
  269. }
  270. const BlockType* Block::zBlockType() const
  271. {
  272. return Game::INSTANCE->zBlockType(typeId);
  273. }
  274. bool Block::isTransparent() const
  275. {
  276. return transparent;
  277. }
  278. bool Block::isPassable() const
  279. {
  280. return passable;
  281. }
  282. bool Block::isInteractable(const Item* zItem) const
  283. {
  284. return interactable;
  285. }
  286. float Block::getHP() const
  287. {
  288. return hp;
  289. }
  290. float Block::getMaxHP() const
  291. {
  292. return maxHP;
  293. }
  294. float Block::getHardness() const
  295. {
  296. return hardness;
  297. }
  298. float Block::getSpeedModifier() const
  299. {
  300. return speedModifier;
  301. }
  302. const Framework::Vec3<int> Block::getPos() const
  303. {
  304. return (Framework::Vec3<int>)location;
  305. }
  306. void Block::setHP(
  307. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
  308. {
  309. bool isDead = this->hp == 0.f;
  310. this->hp = MAX(0.f, hp);
  311. if (!isDead && this->hp == 0.f)
  312. {
  313. onDestroy(zActor, zUsedItem, zUsedSkill); // this will be deleted
  314. }
  315. else
  316. {
  317. NetworkMessage* changeMsg = new NetworkMessage();
  318. changeMsg->addressBlock(this);
  319. char* msg = new char[5];
  320. msg[0] = 0; // hp changed
  321. *(float*)(msg + 1) = this->hp;
  322. changeMsg->setMessage(msg, 5);
  323. Game::INSTANCE->broadcastMessage(changeMsg);
  324. }
  325. }
  326. bool Block::isDeadAndRemoved() const
  327. {
  328. return deadAndRemoved;
  329. }
  330. void Block::getLightEmisionColor(unsigned char* result) const
  331. {
  332. result[0] = lightEmisionColor[0];
  333. result[1] = lightEmisionColor[0];
  334. result[2] = lightEmisionColor[0];
  335. }
  336. void Block::filterPassingLight(unsigned char rgb[3]) const
  337. {
  338. if (!transparent) // let no light pass intransparent blocks
  339. memset(rgb, 0, 3);
  340. }
  341. void Block::updateModel(ModelInfo* zInfo) const
  342. {
  343. Dimension* dim = Game::INSTANCE->zDimension(getDimensionId());
  344. if (dim)
  345. {
  346. Chunk* zChunk
  347. = dim->zChunk(Game::getChunkCenter(getPos().x, getPos().y));
  348. if (zChunk)
  349. {
  350. NetworkMessage* changeMsg = new NetworkMessage();
  351. changeMsg->addressBlock(this);
  352. Framework::InMemoryBuffer buffer;
  353. zInfo->writeTo(&buffer);
  354. char* msg = new char[(int)buffer.getSize() + 1];
  355. msg[0] = 1; // hmodel change
  356. buffer.lese(msg + 1, (int)buffer.getSize());
  357. changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
  358. zChunk->notifyObservers(changeMsg);
  359. }
  360. }
  361. }
  362. int Block::getMapColor() const
  363. {
  364. return mapColor;
  365. }
  366. BasicBlockItem::BasicBlockItem(int itemTypeId,
  367. int blockTypeId,
  368. Framework::Text name,
  369. PlaceableProof* placeableProof)
  370. : Item(itemTypeId, name),
  371. transparent(0),
  372. passable(0),
  373. hardness(1.f),
  374. speedModifier(1.f),
  375. interactable(1),
  376. placeableProof(placeableProof)
  377. {
  378. this->blockTypeId = blockTypeId;
  379. placeable = 1;
  380. }
  381. BasicBlockItem::~BasicBlockItem()
  382. {
  383. if (placeableProof) placeableProof->release();
  384. }
  385. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  386. {
  387. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  388. if (item)
  389. {
  390. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  391. && passable == item->passable && hardness == item->hardness
  392. && speedModifier == item->speedModifier
  393. && interactable == item->interactable;
  394. }
  395. return 0;
  396. }
  397. bool BasicBlockItem::canBePlacedAt(
  398. int dimensionId, Framework::Vec3<int> worldPos) const
  399. {
  400. return Item::canBePlacedAt(dimensionId, worldPos)
  401. && (!placeableProof
  402. || placeableProof->isPlacable(this, worldPos, dimensionId));
  403. }
  404. BasicBlockItemType::BasicBlockItemType()
  405. : ItemType(),
  406. transparent(0),
  407. passable(0),
  408. hardness(1.f),
  409. speedModifier(1.f),
  410. blockTypeName(""),
  411. placeableProof(0)
  412. {}
  413. BasicBlockItemType::BasicBlockItemType(Framework::Text name,
  414. ModelInfo* model,
  415. bool transparent,
  416. bool passable,
  417. float hardness,
  418. float speedModifier,
  419. Framework::Text blockTypeName,
  420. PlaceableProof* placeableProof,
  421. int maxStackSize,
  422. Framework::RCArray<Framework::Text> groups)
  423. : ItemType(),
  424. transparent(transparent),
  425. passable(passable),
  426. hardness(hardness),
  427. speedModifier(speedModifier),
  428. blockTypeName(blockTypeName),
  429. placeableProof(placeableProof)
  430. {
  431. setName(name);
  432. setModel(model);
  433. setMaxStackSize(maxStackSize);
  434. for (Framework::Text* group : groups)
  435. addGroup(*group);
  436. }
  437. BasicBlockItemType::~BasicBlockItemType()
  438. {
  439. if (placeableProof) placeableProof->release();
  440. }
  441. bool BasicBlockItemType::initialize(Game* zGame)
  442. {
  443. blockTypeId = zGame->getBlockTypeId(blockTypeName);
  444. return blockTypeId >= 0 && ItemType::initialize(zGame);
  445. }
  446. int BasicBlockItemType::getBlockTypeId() const
  447. {
  448. return blockTypeId;
  449. }
  450. void BasicBlockItemType::setTransparent(bool transparent)
  451. {
  452. this->transparent = transparent;
  453. }
  454. bool BasicBlockItemType::isTransparent() const
  455. {
  456. return transparent;
  457. }
  458. void BasicBlockItemType::setPassable(bool passable)
  459. {
  460. this->passable = passable;
  461. }
  462. bool BasicBlockItemType::isPassable() const
  463. {
  464. return passable;
  465. }
  466. void BasicBlockItemType::setHardness(float hardness)
  467. {
  468. this->hardness = hardness;
  469. }
  470. float BasicBlockItemType::getHardness() const
  471. {
  472. return hardness;
  473. }
  474. void BasicBlockItemType::setSpeedModifier(float speedModifier)
  475. {
  476. this->speedModifier = speedModifier;
  477. }
  478. float BasicBlockItemType::getSpeedModifier() const
  479. {
  480. return speedModifier;
  481. }
  482. void BasicBlockItemType::setBlockTypeName(Framework::Text blockTypeName)
  483. {
  484. this->blockTypeName = blockTypeName;
  485. }
  486. Framework::Text BasicBlockItemType::getBlockTypeName() const
  487. {
  488. return blockTypeName;
  489. }
  490. void BasicBlockItemType::setPlaceableProof(PlaceableProof* placeableProof)
  491. {
  492. if (this->placeableProof) this->placeableProof->release();
  493. this->placeableProof = placeableProof;
  494. }
  495. PlaceableProof* BasicBlockItemType::zPlaceableProof() const
  496. {
  497. return placeableProof;
  498. }
  499. void BasicBlockItemType::loadSuperItem(
  500. Item* zItem, Framework::StreamReader* zReader) const
  501. {
  502. ItemType::loadSuperItem(zItem, zReader);
  503. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  504. if (!item)
  505. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  506. "item";
  507. zReader->lese((char*)&item->transparent, 1);
  508. zReader->lese((char*)&item->passable, 1);
  509. zReader->lese((char*)&item->hardness, 4);
  510. zReader->lese((char*)&item->speedModifier, 4);
  511. zReader->lese((char*)&item->interactable, 1);
  512. }
  513. void BasicBlockItemType::saveSuperItem(
  514. const Item* zItem, Framework::StreamWriter* zWriter) const
  515. {
  516. ItemType::saveSuperItem(zItem, zWriter);
  517. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  518. if (!item)
  519. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  520. "item";
  521. zWriter->schreibe((char*)&item->transparent, 1);
  522. zWriter->schreibe((char*)&item->passable, 1);
  523. zWriter->schreibe((char*)&item->hardness, 4);
  524. zWriter->schreibe((char*)&item->speedModifier, 4);
  525. zWriter->schreibe((char*)&item->interactable, 1);
  526. }
  527. Item* BasicBlockItemType::createItem() const
  528. {
  529. BasicBlockItem* item = new BasicBlockItem(id,
  530. blockTypeId,
  531. name,
  532. placeableProof
  533. ? dynamic_cast<PlaceableProof*>(placeableProof->getThis())
  534. : 0);
  535. item->transparent = transparent;
  536. item->passable = passable;
  537. item->hardness = hardness;
  538. item->speedModifier = speedModifier;
  539. item->interactable = 1;
  540. return item;
  541. }
  542. BasicBlockItemTypeFactory::BasicBlockItemTypeFactory()
  543. : ItemTypeFactoryBase()
  544. {}
  545. BasicBlockItemType* BasicBlockItemTypeFactory::createValue(
  546. Framework::JSON::JSONObject* zJson) const
  547. {
  548. return new BasicBlockItemType();
  549. }
  550. BasicBlockItemType* BasicBlockItemTypeFactory::fromJson(
  551. Framework::JSON::JSONObject* zJson) const
  552. {
  553. BasicBlockItemType* result = ItemTypeFactoryBase::fromJson(zJson);
  554. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  555. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  556. result->setHardness(
  557. (float)zJson->zValue("hardness")->asNumber()->getNumber());
  558. result->setSpeedModifier(
  559. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  560. result->setBlockTypeName(
  561. zJson->zValue("blockType")->asString()->getString());
  562. result->setPlaceableProof(
  563. zJson->zValue("placeableProof")->getType()
  564. == Framework::AbstractType::OBJECT
  565. ? Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(
  566. zJson->zValue("placeableProof"))
  567. : 0);
  568. return result;
  569. }
  570. Framework::JSON::JSONObject* BasicBlockItemTypeFactory::toJsonObject(
  571. BasicBlockItemType* zObject) const
  572. {
  573. Framework::JSON::JSONObject* result
  574. = ItemTypeFactoryBase::toJsonObject(zObject);
  575. result->addValue(
  576. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  577. result->addValue(
  578. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  579. result->addValue(
  580. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  581. result->addValue("speedModifier",
  582. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  583. result->addValue("blockType",
  584. new Framework::JSON::JSONString(zObject->getBlockTypeName()));
  585. result->addValue("placeableProof",
  586. zObject->zPlaceableProof() ? Game::INSTANCE->zTypeRegistry()->toJson(
  587. zObject->zPlaceableProof())
  588. : new Framework::JSON::JSONValue());
  589. return result;
  590. }
  591. JSONObjectValidationBuilder* BasicBlockItemTypeFactory::addToValidator(
  592. JSONObjectValidationBuilder* builder) const
  593. {
  594. return ItemTypeFactoryBase::addToValidator(
  595. builder->withRequiredBool("transparent")
  596. ->withDefault(false)
  597. ->finishBool()
  598. ->withRequiredBool("passable")
  599. ->withDefault(false)
  600. ->finishBool()
  601. ->withRequiredNumber("hardness")
  602. ->withDefault(1.f)
  603. ->finishNumber()
  604. ->withRequiredNumber("speedModifier")
  605. ->withDefault(1.f)
  606. ->finishNumber()
  607. ->withRequiredString("blockType")
  608. ->finishString()
  609. ->withRequiredAttribute("placeableProof",
  610. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>())
  611. ->withRequiredObject("placeableProof")
  612. ->withDefaultNull()
  613. ->whichCanBeNull()
  614. ->finishObject());
  615. }
  616. const char* BasicBlockItemTypeFactory::getTypeToken() const
  617. {
  618. return "placeable";
  619. }