Block.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. friend BlockType;
  102. };
  103. class BasicBlockItem : public Item
  104. {
  105. protected:
  106. bool transparent;
  107. bool passable;
  108. float hardness;
  109. int toolId;
  110. float speedModifier;
  111. bool interactable;
  112. public:
  113. BasicBlockItem(int itemTypeId, int blockTypeId, const char* name);
  114. virtual bool canBeStackedWith(const Item* zItem) const override;
  115. friend BasicBlockItemType;
  116. friend BlockType;
  117. };
  118. class BasicBlockItemType : public ItemType
  119. {
  120. protected:
  121. bool transparent;
  122. bool passable;
  123. float hardness;
  124. int toolId;
  125. float speedModifier;
  126. int blockTypeId;
  127. virtual void loadSuperItem(
  128. Item* zItem, Framework::StreamReader* zReader) const override;
  129. virtual void saveSuperItem(
  130. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  131. virtual Item* createItem() const override;
  132. public:
  133. BasicBlockItemType(int id,
  134. const char* name,
  135. ItemSkillLevelUpRule* levelUpRule,
  136. int brokenTypeId,
  137. ModelInfo model,
  138. int blockTypeId);
  139. BasicBlockItemType* setHardness(float hardness);
  140. };