Block.h 5.0 KB

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