Block.cpp 9.4 KB

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