Block.cpp 17 KB

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