Block.cpp 9.3 KB

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