Block.h 5.1 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. virtual void sendModelInfo(NetworkMessage* zMessage);
  83. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  84. bool isTickSource() const;
  85. const BlockType* zBlockType() const;
  86. bool isTransparent() const;
  87. bool isPassable() const;
  88. bool isInteractable() const;
  89. float getHP() const;
  90. float getMaxHP() const;
  91. float getHardness() const;
  92. const ItemType* zEffectiveTool() const;
  93. float getSpeedModifier() const;
  94. const Framework::Vec3<int> getPos() const;
  95. int getDimensionId() const;
  96. bool isVisible() const;
  97. void setHP(float hp);
  98. bool isDeadAndRemoved() const;
  99. const unsigned char* getLightEmisionColor() const;
  100. virtual void filterPassingLight(unsigned char rgb[3]) const;
  101. Block* zNeighbor(Direction dir) const;
  102. void updateModel(ModelInfo info) const;
  103. friend BlockType;
  104. };
  105. class BasicBlockItem : public Item
  106. {
  107. protected:
  108. bool transparent;
  109. bool passable;
  110. float hardness;
  111. int toolId;
  112. float speedModifier;
  113. bool interactable;
  114. std::function<bool(const Item*, int, Framework::Vec3<int>)> placableProof;
  115. public:
  116. BasicBlockItem(int itemTypeId, int blockTypeId, const char* name);
  117. void setPlacableProof(
  118. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  119. bool andDefault);
  120. virtual bool canBeStackedWith(const Item* zItem) const override;
  121. virtual bool canBePlacedAt(int dimensionId, Framework::Vec3<int> worldPos) const override;
  122. friend BasicBlockItemType;
  123. friend BlockType;
  124. };
  125. class BasicBlockItemType : public ItemType
  126. {
  127. protected:
  128. bool transparent;
  129. bool passable;
  130. float hardness;
  131. int toolId;
  132. float speedModifier;
  133. int blockTypeId;
  134. char placableProofState;
  135. std::function<bool(const Item*, int, Framework::Vec3<int>)> placableProof;
  136. virtual void loadSuperItem(
  137. Item* zItem, Framework::StreamReader* zReader) const override;
  138. virtual void saveSuperItem(
  139. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  140. virtual Item* createItem() const override;
  141. public:
  142. BasicBlockItemType(int id,
  143. const char* name,
  144. ItemSkillLevelUpRule* levelUpRule,
  145. int brokenTypeId,
  146. ModelInfo model,
  147. int blockTypeId);
  148. BasicBlockItemType* setHardness(float hardness);
  149. BasicBlockItemType* setPlacableProof(
  150. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  151. bool andDefault);
  152. };