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 "Inventory.h"
  7. #include "Item.h"
  8. #include "NetworkMessage.h"
  9. #include "ReferenceCounter.h"
  10. #include "Tickable.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. public Tickable
  22. {
  23. private:
  24. int ticksLeftCounter;
  25. int currentTickTimeout;
  26. bool wasTicked;
  27. bool onTickCalled;
  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. int mapColor;
  47. Framework::RCArray<MultiblockStructure> structures;
  48. /// <summary>
  49. /// executes block specific things
  50. /// </summary>
  51. /// <param name="zqueue">a queue to add neighbor blocks that should be
  52. /// ticked after this block</param> <param name="numTicks">the number of
  53. /// ticks passed since the last call (only for tickSources)</param> <param
  54. /// name="blocked">can be set to one to tell that this block needs to be
  55. /// tickt again later in the queue of this tick</param> <returns>true, iff
  56. /// the block needs to be ticked more often</returns>
  57. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  58. /// <summary>
  59. /// gets called after each block was tickt.
  60. /// the order of blocks called will be exactly the same as onTick
  61. /// </summary>
  62. virtual void onPostTick() = 0;
  63. virtual void onDestroy();
  64. virtual void onDialogClosed(Framework::Text dialogId);
  65. public:
  66. Block(int typeId,
  67. const ItemType* zTool,
  68. Framework::Vec3<int> pos,
  69. int dimensionId,
  70. bool hasInventory);
  71. virtual ~Block();
  72. void tick(TickQueue* zQueue) override;
  73. void postTick() override;
  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. virtual void interact(Item* zItem, Entity *zActor);
  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. virtual bool isInteractable(const Item *zItem) 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. 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. int getMapColor() const;
  104. friend BlockType;
  105. };
  106. class BasicBlockItem : public Item
  107. {
  108. protected:
  109. bool transparent;
  110. bool passable;
  111. float hardness;
  112. int toolId;
  113. float speedModifier;
  114. bool interactable;
  115. std::function<bool(const Item*, int, Framework::Vec3<int>)> placableProof;
  116. public:
  117. BasicBlockItem(int itemTypeId, int blockTypeId, const char* name);
  118. void setPlacableProof(
  119. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  120. bool andDefault);
  121. virtual bool canBeStackedWith(const Item* zItem) const override;
  122. virtual bool canBePlacedAt(
  123. int dimensionId, Framework::Vec3<int> worldPos) const override;
  124. friend BasicBlockItemType;
  125. friend BlockType;
  126. };
  127. class BasicBlockItemType : public ItemType
  128. {
  129. protected:
  130. bool transparent;
  131. bool passable;
  132. float hardness;
  133. int toolId;
  134. float speedModifier;
  135. int blockTypeId;
  136. char placableProofState;
  137. std::function<bool(const Item*, int, Framework::Vec3<int>)> placableProof;
  138. virtual void loadSuperItem(
  139. Item* zItem, Framework::StreamReader* zReader) const override;
  140. virtual void saveSuperItem(
  141. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  142. virtual Item* createItem() const override;
  143. public:
  144. BasicBlockItemType(int id,
  145. const char* name,
  146. ItemSkillLevelUpRule* levelUpRule,
  147. int brokenTypeId,
  148. ModelInfo model,
  149. int blockTypeId);
  150. BasicBlockItemType* setHardness(float hardness);
  151. BasicBlockItemType* setPlacableProof(
  152. std::function<bool(const Item*, int, Framework::Vec3<int>)> condition,
  153. bool andDefault);
  154. };
  155. #include "MultiblockStructure.h"