Block.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. int mapColor;
  49. Framework::RCArray<MultiblockStructure> structures;
  50. /// <summary>
  51. /// executes block specific things
  52. /// </summary>
  53. /// <param name="zqueue">a queue to add neighbor blocks that should be
  54. /// ticked after this block</param> <param name="numTicks">the number of
  55. /// ticks passed since the last call (only for tickSources)</param> <param
  56. /// name="blocked">can be set to one to tell that this block needs to be
  57. /// tickt again later in the queue of this tick</param> <returns>true, iff
  58. /// the block needs to be ticked more often</returns>
  59. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  60. /// <summary>
  61. /// gets called after each block was tickt.
  62. /// the order of blocks called will be exactly the same as onTick
  63. /// </summary>
  64. virtual void onPostTick() = 0;
  65. virtual void onDestroy();
  66. public:
  67. Block(int typeId,
  68. const ItemType* zTool,
  69. Framework::Vec3<int> pos,
  70. bool hasInventory);
  71. virtual ~Block();
  72. void tick(TickQueue* zQueue) override;
  73. void postTick() override;
  74. void setDimensionId(int id);
  75. void addToStructure(MultiblockStructure* structure);
  76. virtual void onLoaded();
  77. virtual void onUnloaded();
  78. virtual void setNeighbour(
  79. Direction dir, Framework::Either<Block*, int> neighbor);
  80. virtual void setNeighbourBlock(Direction dir, Block* zN);
  81. virtual void setNeighbourType(Direction dir, int type);
  82. virtual Framework::Text getTargetUIML();
  83. virtual void sendModelInfo(NetworkMessage* zMessage);
  84. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  85. bool isTickSource() const;
  86. const BlockType* zBlockType() const;
  87. bool isTransparent() const;
  88. bool isPassable() const;
  89. bool isInteractable() const;
  90. float getHP() const;
  91. float getMaxHP() const;
  92. float getHardness() const;
  93. const ItemType* zEffectiveTool() const;
  94. float getSpeedModifier() const;
  95. const Framework::Vec3<int> getPos() const;
  96. int getDimensionId() const;
  97. bool isVisible() const;
  98. void setHP(float hp);
  99. bool isDeadAndRemoved() const;
  100. const unsigned char* getLightEmisionColor() const;
  101. virtual void filterPassingLight(unsigned char rgb[3]) const;
  102. Block* zNeighbor(Direction dir) const;
  103. void updateModel(ModelInfo info) const;
  104. int getMapColor() const;
  105. friend BlockType;
  106. };
  107. class BasicBlockItem : public Item
  108. {
  109. protected:
  110. bool transparent;
  111. bool passable;
  112. float hardness;
  113. int toolId;
  114. float speedModifier;
  115. bool interactable;
  116. std::function<bool(const Item*, int, Framework::Vec3<int>)> placableProof;
  117. public:
  118. BasicBlockItem(int itemTypeId, int blockTypeId, const char* name);
  119. void setPlacableProof(
  120. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  121. bool andDefault);
  122. virtual bool canBeStackedWith(const Item* zItem) const override;
  123. virtual bool canBePlacedAt(
  124. int dimensionId, Framework::Vec3<int> worldPos) const override;
  125. friend BasicBlockItemType;
  126. friend BlockType;
  127. };
  128. class BasicBlockItemType : public ItemType
  129. {
  130. protected:
  131. bool transparent;
  132. bool passable;
  133. float hardness;
  134. int toolId;
  135. float speedModifier;
  136. int blockTypeId;
  137. char placableProofState;
  138. std::function<bool(const Item*, int, Framework::Vec3<int>)> placableProof;
  139. virtual void loadSuperItem(
  140. Item* zItem, Framework::StreamReader* zReader) const override;
  141. virtual void saveSuperItem(
  142. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  143. virtual Item* createItem() const override;
  144. public:
  145. BasicBlockItemType(int id,
  146. const char* name,
  147. ItemSkillLevelUpRule* levelUpRule,
  148. int brokenTypeId,
  149. ModelInfo model,
  150. int blockTypeId);
  151. BasicBlockItemType* setHardness(float hardness);
  152. BasicBlockItemType* setPlacableProof(
  153. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  154. bool andDefault);
  155. };
  156. #include "MultiblockStructure.h"