Block.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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, int maxStackSize)
  386. : ItemType(name, model, maxStackSize),
  387. transparent(transparent),
  388. passable(passable),
  389. hardness(hardness),
  390. speedModifier(speedModifier),
  391. blockTypeName(blockTypeName),
  392. placeableProof(placeableProof)
  393. {}
  394. BasicBlockItemType::~BasicBlockItemType()
  395. {
  396. if (placeableProof) placeableProof->release();
  397. }
  398. bool BasicBlockItemType::initialize(Game* zGame)
  399. {
  400. blockTypeId = zGame->getBlockTypeId(blockTypeName);
  401. return blockTypeId >= 0 && ItemType::initialize(zGame);
  402. }
  403. int BasicBlockItemType::getBlockTypeId() const
  404. {
  405. return blockTypeId;
  406. }
  407. bool BasicBlockItemType::isTransparent() const
  408. {
  409. return transparent;
  410. }
  411. bool BasicBlockItemType::isPassable() const
  412. {
  413. return passable;
  414. }
  415. float BasicBlockItemType::getHardness() const
  416. {
  417. return hardness;
  418. }
  419. float BasicBlockItemType::getSpeedModifier() const
  420. {
  421. return speedModifier;
  422. }
  423. Framework::Text BasicBlockItemType::getBlockTypeName() const
  424. {
  425. return blockTypeName;
  426. }
  427. PlaceableProof* BasicBlockItemType::zPlaceableProof() const
  428. {
  429. return placeableProof;
  430. }
  431. void BasicBlockItemType::loadSuperItem(
  432. Item* zItem, Framework::StreamReader* zReader) const
  433. {
  434. ItemType::loadSuperItem(zItem, zReader);
  435. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  436. if (!item)
  437. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  438. "item";
  439. zReader->lese((char*)&item->transparent, 1);
  440. zReader->lese((char*)&item->passable, 1);
  441. zReader->lese((char*)&item->hardness, 4);
  442. zReader->lese((char*)&item->speedModifier, 4);
  443. zReader->lese((char*)&item->interactable, 1);
  444. }
  445. void BasicBlockItemType::saveSuperItem(
  446. const Item* zItem, Framework::StreamWriter* zWriter) const
  447. {
  448. ItemType::saveSuperItem(zItem, zWriter);
  449. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  450. if (!item)
  451. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  452. "item";
  453. zWriter->schreibe((char*)&item->transparent, 1);
  454. zWriter->schreibe((char*)&item->passable, 1);
  455. zWriter->schreibe((char*)&item->hardness, 4);
  456. zWriter->schreibe((char*)&item->speedModifier, 4);
  457. zWriter->schreibe((char*)&item->interactable, 1);
  458. }
  459. Item* BasicBlockItemType::createItem() const
  460. {
  461. BasicBlockItem* item = new BasicBlockItem(id,
  462. blockTypeId,
  463. name,
  464. placeableProof
  465. ? dynamic_cast<PlaceableProof*>(placeableProof->getThis())
  466. : 0);
  467. item->transparent = transparent;
  468. item->passable = passable;
  469. item->hardness = hardness;
  470. item->speedModifier = speedModifier;
  471. item->interactable = 1;
  472. return item;
  473. }
  474. BasicBlockItemTypeFactory::BasicBlockItemTypeFactory()
  475. : SubTypeFactory()
  476. {}
  477. BasicBlockItemType* BasicBlockItemTypeFactory::fromJson(
  478. Framework::JSON::JSONObject* zJson) const
  479. {
  480. return new BasicBlockItemType(
  481. zJson->zValue("name")->asString()->getString(),
  482. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  483. zJson->zValue("model")),
  484. zJson->zValue("transparent")->asBool()->getBool(),
  485. zJson->zValue("passable")->asBool()->getBool(),
  486. (float)zJson->zValue("hardness")->asNumber()->getNumber(),
  487. (float)zJson->zValue("speedModifier")->asNumber()->getNumber(),
  488. zJson->zValue("blockType")->asString()->getString(),
  489. zJson->zValue("placeableProof")->getType()
  490. == Framework::JSON::JSONType::OBJECT
  491. ? Game::INSTANCE->zTypeRegistry()->fromJson<PlaceableProof>(
  492. zJson->zValue("placeableProof"))
  493. : 0,
  494. (int)zJson->zValue("maxStack")->asNumber()->getNumber());
  495. }
  496. Framework::JSON::JSONObject* BasicBlockItemTypeFactory::toJson(
  497. BasicBlockItemType* zObject) const
  498. {
  499. Framework::JSON::JSONObject* zJson = new Framework::JSON::JSONObject();
  500. zJson->addValue(
  501. "name", new Framework::JSON::JSONString(zObject->getName()));
  502. zJson->addValue(
  503. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  504. zJson->addValue(
  505. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  506. zJson->addValue(
  507. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  508. zJson->addValue(
  509. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  510. zJson->addValue("speedModifier",
  511. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  512. zJson->addValue("blockType",
  513. new Framework::JSON::JSONString(zObject->getBlockTypeName()));
  514. zJson->addValue("placeableProof",
  515. zObject->zPlaceableProof() ? Game::INSTANCE->zTypeRegistry()->toJson(
  516. zObject->zPlaceableProof())
  517. : new Framework::JSON::JSONValue());
  518. zJson->addValue("maxStack",
  519. new Framework::JSON::JSONNumber(zObject->getMaxStackSize()));
  520. return zJson;
  521. }
  522. Framework::JSON::Validator::JSONValidator*
  523. BasicBlockItemTypeFactory::getValidator(
  524. Framework::JSON::Validator::ObjectValidationBuilder<
  525. Framework::JSON::Validator::JSONValidator>* builder) const
  526. {
  527. return builder->withRequiredString("name")
  528. ->finishString()
  529. ->withRequiredAttribute(
  530. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  531. ->withRequiredBool("transparent")
  532. ->withDefault(false)
  533. ->finishBool()
  534. ->withRequiredBool("passable")
  535. ->withDefault(false)
  536. ->finishBool()
  537. ->withRequiredNumber("hardness")
  538. ->withDefault(1.f)
  539. ->finishNumber()
  540. ->withRequiredNumber("speedModifier")
  541. ->withDefault(1.f)
  542. ->finishNumber()
  543. ->withRequiredString("blockType")
  544. ->finishString()
  545. ->withRequiredAttribute("placeableProof",
  546. Game::INSTANCE->zTypeRegistry()->getValidator<PlaceableProof>())
  547. ->withRequiredObject("placeableProof")
  548. ->withDefaultNull()
  549. ->whichCanBeNull()
  550. ->finishObject()
  551. ->withRequiredNumber("maxStack")
  552. ->withDefault(50.0)
  553. ->whichIsGreaterOrEqual(1.0)
  554. ->finishNumber()
  555. ->finishObject();
  556. }
  557. Framework::Text BasicBlockItemTypeFactory::getTypeToken() const
  558. {
  559. return "placeable";
  560. }