Block.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. Block::Block(int typeId,
  9. const ItemType* zTool,
  10. Framework::Vec3<int> pos,
  11. bool hasInventory)
  12. : Inventory(pos, hasInventory)
  13. {
  14. transparent = false;
  15. passable = false;
  16. hp = 1;
  17. maxHP = 1;
  18. hardness = 1;
  19. this->typeId = typeId;
  20. this->zTool = zTool;
  21. speedModifier = 1;
  22. ticksLeftCounter = 0;
  23. wasTicked = 0;
  24. onTickCalled = 0;
  25. minTickTimeout = -1;
  26. maxTickTimeout = -1;
  27. tickSource = 0;
  28. currentTickTimeout = 0;
  29. dimensionId = 0;
  30. interactable = 0;
  31. deadAndRemoved = 0;
  32. memset(zNeighbours, 0, sizeof(Block*) * 6);
  33. memset(lightEmisionColor, 0, 3);
  34. }
  35. Block::~Block() {}
  36. void Block::onDestroy()
  37. {
  38. if (!deadAndRemoved)
  39. {
  40. for (int i = 0; i < 6; i++)
  41. {
  42. if (neighbourTypes[i] == BlockTypeEnum::NO_BLOCK)
  43. {
  44. Framework::Vec3<int> pos
  45. = getPos() + getDirection(getDirectionFromIndex(i));
  46. Game::INSTANCE->zDimension(dimensionId)
  47. ->placeBlock(pos,
  48. Game::INSTANCE->zGenerator()->generateSingleBlock(
  49. pos, dimensionId));
  50. }
  51. }
  52. Item* blockItem = zBlockType()->getItemFromBlock(this);
  53. if (blockItem)
  54. {
  55. ItemEntity* itemEntity
  56. = (ItemEntity*)StaticRegistry<EntityType>::INSTANCE
  57. .zElement(EntityTypeEnum::ITEM)
  58. ->createEntity(
  59. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  60. dimensionId,
  61. Game::INSTANCE->getNextEntityId());
  62. ItemStack* stack
  63. = new ItemStack(blockItem, 1, blockItem->getMaxStackSize());
  64. itemEntity->unsaveAddItem(stack, NO_DIRECTION);
  65. stack->release();
  66. Game::INSTANCE->requestWorldUpdate(
  67. new AddEntityUpdate(itemEntity, dimensionId));
  68. deadAndRemoved = 1;
  69. }
  70. for (MultiblockStructure* structure : structures)
  71. structure->onBlockRemoved(this);
  72. Game::INSTANCE->zDimension(dimensionId)
  73. ->placeBlock(
  74. getPos(), BlockTypeEnum::AIR); // this will be deleted here
  75. }
  76. }
  77. void Block::tick(TickQueue* zQueue)
  78. {
  79. if (wasTicked) return;
  80. wasTicked = 1;
  81. ticksLeftCounter++;
  82. if (minTickTimeout >= 0)
  83. {
  84. if (currentTickTimeout < ticksLeftCounter)
  85. {
  86. onTickCalled = 1;
  87. bool blocked = 0;
  88. bool result = onTick(zQueue, ticksLeftCounter, blocked);
  89. if (blocked)
  90. {
  91. wasTicked = 0;
  92. ticksLeftCounter--;
  93. onTickCalled = 0;
  94. zQueue->addToQueue(this);
  95. return;
  96. }
  97. if (result)
  98. currentTickTimeout
  99. = MAX(MIN(currentTickTimeout - 1, maxTickTimeout),
  100. MAX(minTickTimeout, 0));
  101. else
  102. currentTickTimeout
  103. = MAX(MIN(currentTickTimeout + 1, maxTickTimeout),
  104. MAX(minTickTimeout, 0));
  105. ticksLeftCounter = 0;
  106. }
  107. }
  108. else
  109. {
  110. onTickCalled = 1;
  111. bool blocked = 0;
  112. onTick(zQueue, 1, blocked);
  113. if (blocked)
  114. {
  115. wasTicked = 0;
  116. onTickCalled = 0;
  117. zQueue->addToQueue(this);
  118. return;
  119. }
  120. }
  121. }
  122. void Block::postTick()
  123. {
  124. wasTicked = 0;
  125. if (onTickCalled)
  126. {
  127. onPostTick();
  128. onTickCalled = 0;
  129. }
  130. }
  131. void Block::setNeighbour(
  132. Direction dir, Framework::Either<Block*, int> neighbour)
  133. {
  134. if (neighbour.isA())
  135. setNeighbourBlock(dir, neighbour);
  136. else
  137. {
  138. setNeighbourBlock(dir, 0);
  139. setNeighbourType(dir, neighbour);
  140. }
  141. }
  142. void Block::setNeighbourBlock(Direction dir, Block* zN)
  143. {
  144. if (zN) setNeighbourType(dir, zN->zBlockType()->getId());
  145. zNeighbours[getDirectionIndex(dir)] = zN;
  146. }
  147. void Block::setNeighbourType(Direction dir, int type)
  148. {
  149. neighbourTypes[getDirectionIndex(dir)] = type;
  150. }
  151. void Block::setDimensionId(int id)
  152. {
  153. dimensionId = id;
  154. }
  155. void Block::addToStructure(MultiblockStructure* structure)
  156. {
  157. if (structure->isBlockMember(this))
  158. structures.add(structure);
  159. else
  160. structure->release();
  161. }
  162. void Block::onLoaded()
  163. {
  164. for (MultiblockStructure* structure : structures)
  165. structure->onBlockLoaded(dynamic_cast<Block*>(getThis()));
  166. }
  167. void Block::onUnloaded()
  168. {
  169. for (MultiblockStructure* structure : structures)
  170. structure->onBlockUnloaded(this);
  171. }
  172. Framework::Text Block::getTargetUIML()
  173. {
  174. return StaticRegistry<BlockType>::INSTANCE.zElement(typeId)
  175. ->getTargetUIML();
  176. }
  177. void Block::sendModelInfo(NetworkMessage* zMessage) {
  178. // overwritten by some blocks
  179. }
  180. void Block::api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  181. {
  182. // TODO: answer api requests
  183. char id = 0;
  184. zRequest->lese(&id, 1);
  185. switch (id)
  186. {
  187. case 0:
  188. // request model state
  189. sendModelInfo(zResponse);
  190. }
  191. }
  192. bool Block::isTickSource() const
  193. {
  194. return tickSource;
  195. }
  196. const BlockType* Block::zBlockType() const
  197. {
  198. return StaticRegistry<BlockType>::INSTANCE.zElement(typeId);
  199. }
  200. bool Block::isTransparent() const
  201. {
  202. return transparent;
  203. }
  204. bool Block::isPassable() const
  205. {
  206. return passable;
  207. }
  208. bool Block::isInteractable() const
  209. {
  210. return interactable;
  211. }
  212. float Block::getHP() const
  213. {
  214. return hp;
  215. }
  216. float Block::getMaxHP() const
  217. {
  218. return maxHP;
  219. }
  220. float Block::getHardness() const
  221. {
  222. return hardness;
  223. }
  224. const ItemType* Block::zEffectiveTool() const
  225. {
  226. return zTool;
  227. }
  228. float Block::getSpeedModifier() const
  229. {
  230. return speedModifier;
  231. }
  232. const Framework::Vec3<int> Block::getPos() const
  233. {
  234. return (Framework::Vec3<int>)location;
  235. }
  236. int Block::getDimensionId() const
  237. {
  238. return dimensionId;
  239. }
  240. bool Block::isVisible() const
  241. {
  242. if (passable || transparent) return 1;
  243. for (int i = 0; i < 6; i++)
  244. {
  245. const Block* neighbour = CONST_BLOCK(zNeighbours[i], neighbourTypes[i]);
  246. if (neighbour->isPassable() || neighbour->isTransparent()) return 1;
  247. }
  248. return 0;
  249. }
  250. void Block::setHP(float hp)
  251. {
  252. bool isDead = this->hp == 0.f;
  253. this->hp = MAX(0.f, hp);
  254. if (!isDead && this->hp == 0.f)
  255. {
  256. onDestroy(); // this will be deleted
  257. }
  258. else
  259. {
  260. NetworkMessage* changeMsg = new NetworkMessage();
  261. changeMsg->addressBlock(this);
  262. char* msg = new char[5];
  263. msg[0] = 0; // hp changed
  264. *(float*)(msg + 1) = this->hp;
  265. changeMsg->setMessage(msg, 5);
  266. Game::INSTANCE->broadcastMessage(changeMsg);
  267. }
  268. }
  269. bool Block::isDeadAndRemoved() const
  270. {
  271. return deadAndRemoved;
  272. }
  273. const unsigned char* Block::getLightEmisionColor() const
  274. {
  275. return lightEmisionColor;
  276. }
  277. void Block::filterPassingLight(unsigned char rgb[3]) const
  278. {
  279. if (!transparent) // let no light pass intransparent blocks
  280. memset(rgb, 0, 3);
  281. }
  282. Block* Block::zNeighbor(Direction dir) const
  283. {
  284. return zNeighbours[getDirectionIndex(dir)];
  285. }
  286. void Block::updateModel(ModelInfo info) const
  287. {
  288. NetworkMessage* changeMsg = new NetworkMessage();
  289. changeMsg->addressBlock(this);
  290. InMemoryBuffer buffer;
  291. info.writeTo(&buffer);
  292. char* msg = new char[(int)buffer.getSize() + 1];
  293. msg[0] = 1; // hmodel change
  294. buffer.lese(msg + 1, (int)buffer.getSize());
  295. changeMsg->setMessage(msg, (int)buffer.getSize() + 1);
  296. Game::INSTANCE->broadcastMessage(changeMsg);
  297. }
  298. BasicBlockItem::BasicBlockItem(
  299. int itemTypeId, int blockTypeId, const char* name)
  300. : Item(itemTypeId, name),
  301. transparent(0),
  302. passable(0),
  303. hardness(1.f),
  304. toolId(0),
  305. speedModifier(1.f),
  306. interactable(1)
  307. {
  308. this->blockTypeId = blockTypeId;
  309. placeable = 1;
  310. placableProof = [this](const Item* self,
  311. int dimensionId,
  312. Framework::Vec3<int> worldPos) {
  313. return Item::canBePlacedAt(dimensionId, worldPos);
  314. };
  315. }
  316. void BasicBlockItem::setPlacableProof(
  317. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  318. bool andDefault)
  319. {
  320. if (andDefault)
  321. {
  322. placableProof = [this, condition](const Item* self,
  323. int dimensionId,
  324. Framework::Vec3<int> worldPos) {
  325. return Item::canBePlacedAt(dimensionId, worldPos)
  326. && condition(self, dimensionId, worldPos);
  327. };
  328. }
  329. else
  330. {
  331. placableProof = condition;
  332. }
  333. }
  334. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  335. {
  336. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  337. if (item)
  338. {
  339. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  340. && passable == item->passable && hardness == item->hardness
  341. && toolId == item->toolId && speedModifier == item->speedModifier
  342. && interactable == item->interactable;
  343. }
  344. return 0;
  345. }
  346. bool BasicBlockItem::canBePlacedAt(
  347. int dimensionId, Framework::Vec3<int> worldPos) const
  348. {
  349. return placableProof(this, dimensionId, worldPos);
  350. }
  351. BasicBlockItemType::BasicBlockItemType(int id,
  352. const char* name,
  353. ItemSkillLevelUpRule* levelUpRule,
  354. int brokenTypeId,
  355. ModelInfo model,
  356. int blockTypeId)
  357. : ItemType(id, name, levelUpRule, brokenTypeId, model),
  358. transparent(0),
  359. passable(0),
  360. hardness(1.f),
  361. toolId(0),
  362. speedModifier(1.f),
  363. blockTypeId(blockTypeId)
  364. {}
  365. void BasicBlockItemType::loadSuperItem(
  366. Item* zItem, Framework::StreamReader* zReader) const
  367. {
  368. ItemType::loadSuperItem(zItem, zReader);
  369. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  370. if (!item)
  371. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  372. "item";
  373. zReader->lese((char*)&item->transparent, 1);
  374. zReader->lese((char*)&item->passable, 1);
  375. zReader->lese((char*)&item->hardness, 4);
  376. zReader->lese((char*)&item->toolId, 4);
  377. zReader->lese((char*)&item->speedModifier, 4);
  378. zReader->lese((char*)&item->interactable, 1);
  379. }
  380. void BasicBlockItemType::saveSuperItem(
  381. const Item* zItem, Framework::StreamWriter* zWriter) const
  382. {
  383. ItemType::saveSuperItem(zItem, zWriter);
  384. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  385. if (!item)
  386. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  387. "item";
  388. zWriter->schreibe((char*)&item->transparent, 1);
  389. zWriter->schreibe((char*)&item->passable, 1);
  390. zWriter->schreibe((char*)&item->hardness, 4);
  391. zWriter->schreibe((char*)&item->toolId, 4);
  392. zWriter->schreibe((char*)&item->speedModifier, 4);
  393. zWriter->schreibe((char*)&item->interactable, 1);
  394. }
  395. Item* BasicBlockItemType::createItem() const
  396. {
  397. BasicBlockItem* item = new BasicBlockItem(id, blockTypeId, name);
  398. item->transparent = transparent;
  399. item->passable = passable;
  400. item->hardness = hardness;
  401. item->toolId = toolId;
  402. item->speedModifier = speedModifier;
  403. item->interactable = 1;
  404. if (placableProofState)
  405. {
  406. item->setPlacableProof(placableProof, placableProofState > 1);
  407. }
  408. return item;
  409. }
  410. BasicBlockItemType* BasicBlockItemType::setHardness(float hardness)
  411. {
  412. this->hardness = hardness;
  413. return this;
  414. }
  415. BasicBlockItemType* BasicBlockItemType::setPlacableProof(
  416. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  417. bool andDefault)
  418. {
  419. placableProofState = 1 + (andDefault ? 1 : 0);
  420. placableProof = condition;
  421. return this;
  422. }