Block.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. protected:
  30. bool transparent;
  31. bool passable;
  32. float hp;
  33. float maxHP;
  34. float hardness;
  35. int typeId;
  36. const ItemType* zTool;
  37. float speedModifier;
  38. Block* zNeighbours[6];
  39. int neighbourTypes[6];
  40. int minTickTimeout;
  41. int maxTickTimeout;
  42. bool tickSource;
  43. bool interactable;
  44. bool transmissionRequested;
  45. bool deadAndRemoved;
  46. unsigned char lightEmisionColor[3];
  47. int mapColor;
  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. virtual void onDialogClosed(Framework::Text dialogId);
  66. public:
  67. Block(int typeId,
  68. const ItemType* zTool,
  69. Framework::Vec3<int> pos,
  70. int dimensionId,
  71. bool hasInventory);
  72. virtual ~Block();
  73. void tick(TickQueue* zQueue) override;
  74. void postTick() override;
  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. virtual void interact(Item* zItem, Entity *zActor);
  85. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  86. bool isTickSource() const;
  87. const BlockType* zBlockType() const;
  88. bool isTransparent() const;
  89. bool isPassable() const;
  90. virtual bool isInteractable(const Item *zItem) const;
  91. float getHP() const;
  92. float getMaxHP() const;
  93. float getHardness() const;
  94. const ItemType* zEffectiveTool() const;
  95. float getSpeedModifier() const;
  96. const Framework::Vec3<int> getPos() 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"