Block.cpp 17 KB

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