Block.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #pragma once
  2. #include <Either.h>
  3. #include <Trie.h>
  4. #include <Vec3.h>
  5. #include <VecN.h>
  6. #include "BlockType.h"
  7. #include "Inventory.h"
  8. #include "Item.h"
  9. #include "NetworkMessage.h"
  10. #include "ReferenceCounter.h"
  11. #define CONST_BLOCK(maybeBlock, type) \
  12. (maybeBlock ? maybeBlock \
  13. : StaticRegistry<BlockType>::INSTANCE.zElement((int)type) \
  14. ->zDefault())
  15. class ItemType;
  16. class Chunk;
  17. class BasicBlockItemType;
  18. class MultiblockStructure;
  19. class TickQueue;
  20. class Block : public Inventory
  21. {
  22. private:
  23. int ticksLeftCounter;
  24. int currentTickTimeout;
  25. bool wasTicked;
  26. bool onTickCalled;
  27. int dimensionId;
  28. protected:
  29. bool transparent;
  30. bool passable;
  31. float hp;
  32. float maxHP;
  33. float hardness;
  34. int typeId;
  35. const ItemType* zTool;
  36. float speedModifier;
  37. Block* zNeighbours[6];
  38. int neighbourTypes[6];
  39. int minTickTimeout;
  40. int maxTickTimeout;
  41. bool tickSource;
  42. bool interactable;
  43. bool transmissionRequested;
  44. bool deadAndRemoved;
  45. unsigned char lightEmisionColor[3];
  46. Framework::RCArray<MultiblockStructure> structures;
  47. /// <summary>
  48. /// executes block specific things
  49. /// </summary>
  50. /// <param name="zqueue">a queue to add neighbor blocks that should be
  51. /// ticked after this block</param> <param name="numTicks">the number of
  52. /// ticks passed since the last call (only for tickSources)</param> <param
  53. /// name="blocked">can be set to one to tell that this block needs to be
  54. /// tickt again later in the queue of this tick</param> <returns>true, iff
  55. /// the block needs to be ticked more often</returns>
  56. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  57. /// <summary>
  58. /// gets called after each block was tickt.
  59. /// the order of blocks called will be exactly the same as onTick
  60. /// </summary>
  61. virtual void onPostTick() = 0;
  62. virtual void onDestroy();
  63. public:
  64. Block(int typeId,
  65. const ItemType* zTool,
  66. Framework::Vec3<int> pos,
  67. bool hasInventory);
  68. virtual ~Block();
  69. void tick(TickQueue* zQueue);
  70. void postTick();
  71. void setDimensionId(int id);
  72. void addToStructure(MultiblockStructure* structure);
  73. virtual void onLoaded();
  74. virtual void onUnloaded();
  75. virtual void setNeighbour(
  76. Direction dir, Framework::Either<Block*, int> neighbor);
  77. virtual void setNeighbourBlock(Direction dir, Block* zN);
  78. virtual void setNeighbourType(Direction dir, int type);
  79. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  80. bool isTickSource() const;
  81. const BlockType* zBlockType() const;
  82. bool isTransparent() const;
  83. bool isPassable() const;
  84. bool isInteractable() const;
  85. float getHP() const;
  86. float getMaxHP() const;
  87. float getHardness() const;
  88. const ItemType* zEffectiveTool() const;
  89. float getSpeedModifier() const;
  90. const Framework::Vec3<int> getPos() const;
  91. int getDimensionId() const;
  92. bool isVisible() const;
  93. void setHP(float hp);
  94. bool isDeadAndRemoved() const;
  95. const unsigned char* getLightEmisionColor() const;
  96. virtual void filterPassingLight(unsigned char rgb[3]) const;
  97. Block* zNeighbor(Direction dir) const;
  98. friend BlockType;
  99. };
  100. class BasicBlockItem : public Item
  101. {
  102. protected:
  103. bool transparent;
  104. bool passable;
  105. float hardness;
  106. int toolId;
  107. float speedModifier;
  108. bool interactable;
  109. public:
  110. BasicBlockItem(int itemTypeId, int blockTypeId, const char* name);
  111. virtual bool canBeStackedWith(const Item* zItem) const override;
  112. friend BasicBlockItemType;
  113. friend BlockType;
  114. };
  115. class BasicBlockItemType : public ItemType
  116. {
  117. protected:
  118. bool transparent;
  119. bool passable;
  120. float hardness;
  121. int toolId;
  122. float speedModifier;
  123. int blockTypeId;
  124. virtual void loadSuperItem(
  125. Item* zItem, Framework::StreamReader* zReader) const override;
  126. virtual void saveSuperItem(
  127. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  128. virtual Item* createItem() const override;
  129. public:
  130. BasicBlockItemType(int id,
  131. const char* name,
  132. ItemSkillLevelUpRule* levelUpRule,
  133. int brokenTypeId,
  134. ModelInfo model,
  135. int blockTypeId);
  136. BasicBlockItemType* setHardness(float hardness);
  137. };