Block.cpp 17 KB

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