Block.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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)->getTargetUIML();
  175. }
  176. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse)
  177. {
  178. // TODO: answer api requests
  179. }
  180. bool Block::isTickSource() const
  181. {
  182. return tickSource;
  183. }
  184. const BlockType* Block::zBlockType() const
  185. {
  186. return StaticRegistry<BlockType>::INSTANCE.zElement(typeId);
  187. }
  188. bool Block::isTransparent() const
  189. {
  190. return transparent;
  191. }
  192. bool Block::isPassable() const
  193. {
  194. return passable;
  195. }
  196. bool Block::isInteractable() const
  197. {
  198. return interactable;
  199. }
  200. float Block::getHP() const
  201. {
  202. return hp;
  203. }
  204. float Block::getMaxHP() const
  205. {
  206. return maxHP;
  207. }
  208. float Block::getHardness() const
  209. {
  210. return hardness;
  211. }
  212. const ItemType* Block::zEffectiveTool() const
  213. {
  214. return zTool;
  215. }
  216. float Block::getSpeedModifier() const
  217. {
  218. return speedModifier;
  219. }
  220. const Framework::Vec3<int> Block::getPos() const
  221. {
  222. return (Framework::Vec3<int>)location;
  223. }
  224. int Block::getDimensionId() const
  225. {
  226. return dimensionId;
  227. }
  228. bool Block::isVisible() const
  229. {
  230. if (passable || transparent) return 1;
  231. for (int i = 0; i < 6; i++)
  232. {
  233. const Block* neighbour = CONST_BLOCK(zNeighbours[i], neighbourTypes[i]);
  234. if (neighbour->isPassable() || neighbour->isTransparent()) return 1;
  235. }
  236. return 0;
  237. }
  238. void Block::setHP(float hp)
  239. {
  240. bool isDead = this->hp == 0.f;
  241. this->hp = MAX(0.f, hp);
  242. if (!isDead && this->hp == 0.f)
  243. {
  244. onDestroy(); // this will be deleted
  245. }
  246. else
  247. {
  248. NetworkMessage* changeMsg = new NetworkMessage();
  249. changeMsg->addressBlock(this);
  250. char* msg = new char[5];
  251. msg[0] = 0; // hp changed
  252. *(float*)(msg + 1) = this->hp;
  253. changeMsg->setMessage(msg, 5);
  254. Game::INSTANCE->broadcastMessage(changeMsg);
  255. }
  256. }
  257. bool Block::isDeadAndRemoved() const
  258. {
  259. return deadAndRemoved;
  260. }
  261. const unsigned char* Block::getLightEmisionColor() const
  262. {
  263. return lightEmisionColor;
  264. }
  265. void Block::filterPassingLight(unsigned char rgb[3]) const
  266. {
  267. if (!transparent) // let no light pass intransparent blocks
  268. memset(rgb, 0, 3);
  269. }
  270. Block* Block::zNeighbor(Direction dir) const
  271. {
  272. return zNeighbours[getDirectionIndex(dir)];
  273. }
  274. BasicBlockItem::BasicBlockItem(
  275. int itemTypeId, int blockTypeId, const char* name)
  276. : Item(itemTypeId, name)
  277. {
  278. this->blockTypeId = blockTypeId;
  279. placeable = 1;
  280. }
  281. bool BasicBlockItem::canBeStackedWith(const Item* zItem) const
  282. {
  283. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  284. if (item)
  285. {
  286. return Item::canBeStackedWith(zItem) && transparent == item->transparent
  287. && passable == item->passable && hardness == item->hardness
  288. && toolId == item->toolId && speedModifier == item->speedModifier
  289. && interactable == item->interactable;
  290. }
  291. return 0;
  292. }
  293. BasicBlockItemType::BasicBlockItemType(int id,
  294. const char* name,
  295. ItemSkillLevelUpRule* levelUpRule,
  296. int brokenTypeId,
  297. ModelInfo model,
  298. int blockTypeId)
  299. : ItemType(id, name, levelUpRule, brokenTypeId, model),
  300. transparent(0),
  301. passable(0),
  302. hardness(1.f),
  303. toolId(0),
  304. speedModifier(1.f),
  305. blockTypeId(blockTypeId)
  306. {}
  307. void BasicBlockItemType::loadSuperItem(
  308. Item* zItem, Framework::StreamReader* zReader) const
  309. {
  310. ItemType::loadSuperItem(zItem, zReader);
  311. BasicBlockItem* item = dynamic_cast<BasicBlockItem*>(zItem);
  312. if (!item)
  313. throw "BasicBlockItemType::loadSuperItem was called with an invalid "
  314. "item";
  315. zReader->lese((char*)&item->transparent, 1);
  316. zReader->lese((char*)&item->passable, 1);
  317. zReader->lese((char*)&item->hardness, 4);
  318. zReader->lese((char*)&item->toolId, 4);
  319. zReader->lese((char*)&item->speedModifier, 4);
  320. zReader->lese((char*)&item->interactable, 1);
  321. }
  322. void BasicBlockItemType::saveSuperItem(
  323. const Item* zItem, Framework::StreamWriter* zWriter) const
  324. {
  325. ItemType::saveSuperItem(zItem, zWriter);
  326. const BasicBlockItem* item = dynamic_cast<const BasicBlockItem*>(zItem);
  327. if (!item)
  328. throw "BasicBlockItemType::saveSuperItem was called with an invalid "
  329. "item";
  330. zWriter->schreibe((char*)&item->transparent, 1);
  331. zWriter->schreibe((char*)&item->passable, 1);
  332. zWriter->schreibe((char*)&item->hardness, 4);
  333. zWriter->schreibe((char*)&item->toolId, 4);
  334. zWriter->schreibe((char*)&item->speedModifier, 4);
  335. zWriter->schreibe((char*)&item->interactable, 1);
  336. }
  337. Item* BasicBlockItemType::createItem() const
  338. {
  339. BasicBlockItem* item = new BasicBlockItem(id, blockTypeId, name);
  340. item->transparent = transparent;
  341. item->passable = passable;
  342. item->hardness = hardness;
  343. item->toolId = toolId;
  344. item->speedModifier = speedModifier;
  345. return item;
  346. }
  347. BasicBlockItemType* BasicBlockItemType::setHardness(float hardness)
  348. {
  349. this->hardness = hardness;
  350. return this;
  351. }