Block.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. return;
  95. }
  96. if (result)
  97. currentTickTimeout
  98. = MAX(MIN(currentTickTimeout - 1, maxTickTimeout),
  99. MAX(minTickTimeout, 0));
  100. else
  101. currentTickTimeout
  102. = MAX(MIN(currentTickTimeout + 1, maxTickTimeout),
  103. MAX(minTickTimeout, 0));
  104. ticksLeftCounter = 0;
  105. }
  106. }
  107. }
  108. void Block::postTick()
  109. {
  110. wasTicked = 0;
  111. if (onTickCalled)
  112. {
  113. onPostTick();
  114. onTickCalled = 0;
  115. }
  116. }
  117. void Block::setNeighbour(
  118. Direction dir, Framework::Either<Block*, int> neighbour)
  119. {
  120. if (neighbour.isA())
  121. setNeighbourBlock(dir, neighbour);
  122. else
  123. {
  124. setNeighbourBlock(dir, 0);
  125. setNeighbourType(dir, neighbour);
  126. }
  127. }
  128. void Block::setNeighbourBlock(Direction dir, Block* zN)
  129. {
  130. if (zN) setNeighbourType(dir, zN->zBlockType()->getId());
  131. zNeighbours[getDirectionIndex(dir)] = zN;
  132. }
  133. void Block::setNeighbourType(Direction dir, int type)
  134. {
  135. neighbourTypes[getDirectionIndex(dir)] = type;
  136. }
  137. void Block::setDimensionId(int id)
  138. {
  139. dimensionId = id;
  140. }
  141. void Block::addToStructure(MultiblockStructure* structure)
  142. {
  143. if (structure->isBlockMember(this))
  144. structures.add(structure);
  145. else
  146. structure->release();
  147. }
  148. void Block::onLoaded()
  149. {
  150. for (MultiblockStructure* structure : structures)
  151. structure->onBlockLoaded(dynamic_cast<Block*>(getThis()));
  152. }
  153. void Block::onUnloaded()
  154. {
  155. for (MultiblockStructure* structure : structures)
  156. structure->onBlockUnloaded(this);
  157. }
  158. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  159. {
  160. // TODO: answer api requests
  161. }
  162. bool Block::isTickSource() const
  163. {
  164. return tickSource;
  165. }
  166. const BlockType* Block::zBlockType() const
  167. {
  168. return StaticRegistry<BlockType>::INSTANCE.zElement(typeId);
  169. }
  170. bool Block::isTransparent() const
  171. {
  172. return transparent;
  173. }
  174. bool Block::isPassable() const
  175. {
  176. return passable;
  177. }
  178. bool Block::isInteractable() const
  179. {
  180. return interactable;
  181. }
  182. float Block::getHP() const
  183. {
  184. return hp;
  185. }
  186. float Block::getMaxHP() const
  187. {
  188. return maxHP;
  189. }
  190. float Block::getHardness() const
  191. {
  192. return hardness;
  193. }
  194. const ItemType* Block::zEffectiveTool() const
  195. {
  196. return zTool;
  197. }
  198. float Block::getSpeedModifier() const
  199. {
  200. return speedModifier;
  201. }
  202. const Framework::Vec3<int> Block::getPos() const
  203. {
  204. return (Framework::Vec3<int>)location;
  205. }
  206. int Block::getDimensionId() const
  207. {
  208. return dimensionId;
  209. }
  210. bool Block::isVisible() const
  211. {
  212. if (passable || transparent) return 1;
  213. for (int i = 0; i < 6; i++)
  214. {
  215. const Block* neighbour = CONST_BLOCK(zNeighbours[i], neighbourTypes[i]);
  216. if (neighbour->isPassable() || neighbour->isTransparent()) return 1;
  217. }
  218. return 0;
  219. }
  220. void Block::setHP(float hp)
  221. {
  222. bool isDead = this->hp == 0.f;
  223. this->hp = MAX(0.f, hp);
  224. if (!isDead && this->hp == 0.f)
  225. {
  226. onDestroy(); // this will be deleted
  227. }
  228. else
  229. {
  230. NetworkMessage* changeMsg = new NetworkMessage();
  231. changeMsg->addressBlock(this);
  232. char* msg = new char[5];
  233. msg[0] = 0; // hp changed
  234. *(float*)(msg + 1) = this->hp;
  235. changeMsg->setMessage(msg, 5);
  236. Game::INSTANCE->broadcastMessage(changeMsg);
  237. }
  238. }
  239. bool Block::isDeadAndRemoved() const
  240. {
  241. return deadAndRemoved;
  242. }
  243. const unsigned char* Block::getLightEmisionColor() const
  244. {
  245. return lightEmisionColor;
  246. }
  247. void Block::filterPassingLight(unsigned char rgb[3]) const
  248. {
  249. if (!transparent) // let no light pass intransparent blocks
  250. memset(rgb, 0, 3);
  251. }
  252. Block* Block::zNeighbor(Direction dir) const
  253. {
  254. return zNeighbours[getDirectionIndex(dir)];
  255. }
  256. BasicBlockItem::BasicBlockItem(
  257. int itemTypeId, int blockTypeId, const char* name)
  258. : Item(itemTypeId, name)
  259. {
  260. blockTypeId = blockTypeId;
  261. placeable = 1;
  262. }
  263. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  264. {
  265. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  266. if (item)
  267. {
  268. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  269. && passable == item->passable && hardness == item->hardness
  270. && toolId == item->toolId && speedModifier == item->speedModifier
  271. && interactable == item->interactable;
  272. }
  273. return 0;
  274. }
  275. BasicBlockItemType::BasicBlockItemType(int id,
  276. const char* name,
  277. ItemSkillLevelUpRule* levelUpRule,
  278. int brokenTypeId,
  279. ModelInfo model,
  280. int blockTypeId)
  281. : ItemType(id, name, levelUpRule, brokenTypeId, model),
  282. transparent(0),
  283. passable(0),
  284. hardness(1.f),
  285. toolId(0),
  286. speedModifier(1.f),
  287. blockTypeId(blockTypeId)
  288. {}
  289. void BasicBlockItemType::loadSuperItem(
  290. Item* zItem, Framework::StreamReader* zReader) const
  291. {
  292. ItemType::loadSuperItem(zItem, zReader);
  293. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  294. if (!item)
  295. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  296. "item";
  297. zReader->lese((char*)&item->transparent, 1);
  298. zReader->lese((char*)&item->passable, 1);
  299. zReader->lese((char*)&item->hardness, 4);
  300. zReader->lese((char*)&item->toolId, 4);
  301. zReader->lese((char*)&item->speedModifier, 4);
  302. zReader->lese((char*)&item->interactable, 1);
  303. }
  304. void BasicBlockItemType::saveSuperItem(
  305. const Item* zItem, Framework::StreamWriter* zWriter) const
  306. {
  307. ItemType::saveSuperItem(zItem, zWriter);
  308. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  309. if (!item)
  310. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  311. "item";
  312. zWriter->schreibe((char*)&item->transparent, 1);
  313. zWriter->schreibe((char*)&item->passable, 1);
  314. zWriter->schreibe((char*)&item->hardness, 4);
  315. zWriter->schreibe((char*)&item->toolId, 4);
  316. zWriter->schreibe((char*)&item->speedModifier, 4);
  317. zWriter->schreibe((char*)&item->interactable, 1);
  318. }
  319. Item* BasicBlockItemType::createItem() const
  320. {
  321. BasicBlockItem* item = new BasicBlockItem(id, blockTypeId, name);
  322. item->transparent = transparent;
  323. item->passable = passable;
  324. item->hardness = hardness;
  325. item->toolId = toolId;
  326. item->speedModifier = speedModifier;
  327. return item;
  328. }
  329. BasicBlockItemType* BasicBlockItemType::setHardness(float hardness)
  330. {
  331. this->hardness = hardness;
  332. return this;
  333. }