Block.cpp 17 KB

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