Block.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #include "BlockType.h"
  3. #include "ReferenceCounter.h"
  4. #include "Item.h"
  5. #include "Inventory.h"
  6. #include "NetworkMessage.h"
  7. #include <Trie.h>
  8. #include <Vec3.h>
  9. #include <Either.h>
  10. #include <VecN.h>
  11. #define CONST_BLOCK(maybeBlock, type) (maybeBlock ? maybeBlock : StaticRegistry<BlockType>::INSTANCE.zElement((int)type)->zDefault())
  12. class ItemType;
  13. class Chunk;
  14. class BasicBlockItemType;
  15. class MultiblockStructure;
  16. class TickQueue;
  17. class Block : public Inventory
  18. {
  19. private:
  20. int ticksLeftCounter;
  21. int currentTickTimeout;
  22. bool wasTicked;
  23. bool onTickCalled;
  24. int dimensionId;
  25. protected:
  26. bool transparent;
  27. bool passable;
  28. float hp;
  29. float maxHP;
  30. float hardness;
  31. const BlockType* zType;
  32. const ItemType* zTool;
  33. float speedModifier;
  34. Block* zNeighbours[6];
  35. int neighbourTypes[6];
  36. int minTickTimeout;
  37. int maxTickTimeout;
  38. bool tickSource;
  39. bool interactable;
  40. bool transmissionRequested;
  41. bool deadAndRemoved;
  42. unsigned char lightEmisionColor[3];
  43. Framework::RCArray<MultiblockStructure> structures;
  44. /// <summary>
  45. /// executes block specific things
  46. /// </summary>
  47. /// <param name="zqueue">a queue to add neighbor blocks that should be ticked after this block</param>
  48. /// <param name="numTicks">the number of ticks passed since the last call (only for tickSources)</param>
  49. /// <param name="blocked">can be set to one to tell that this block needs to be tickt again later in the queue of this tick</param>
  50. /// <returns>true, iff the block needs to be ticked more often</returns>
  51. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  52. /// <summary>
  53. /// gets called after each block was tickt.
  54. /// the order of blocks called will be exactly the same as onTick
  55. /// </summary>
  56. virtual void onPostTick() = 0;
  57. virtual void onDestroy();
  58. public:
  59. Block(const BlockType* zType, const ItemType* zTool, Framework::Vec3<int> pos, bool hasInventory);
  60. virtual ~Block();
  61. void tick(TickQueue* zQueue);
  62. void postTick();
  63. void setDimensionId(int id);
  64. void addToStructure(MultiblockStructure* structure);
  65. virtual void onLoaded();
  66. virtual void onUnloaded();
  67. virtual void setNeighbour(Direction dir, Framework::Either<Block*, int> neighbor);
  68. virtual void setNeighbourBlock(Direction dir, Block* zN);
  69. virtual void setNeighbourType(Direction dir, int type);
  70. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  71. bool isTickSource() const;
  72. const BlockType* zBlockType() const;
  73. bool isTransparent() const;
  74. bool isPassable() const;
  75. bool isInteractable() const;
  76. float getHP() const;
  77. float getMaxHP() const;
  78. float getHardness() const;
  79. const ItemType* zEffectiveTool() const;
  80. float getSpeedModifier() const;
  81. const Framework::Vec3<int> getPos() const;
  82. int getDimensionId() const;
  83. bool isVisible() const;
  84. void setHP(float hp);
  85. bool isDeadAndRemoved() const;
  86. const unsigned char* getLightEmisionColor() const;
  87. virtual void filterPassingLight(unsigned char rgb[3]) const;
  88. Block* zNeighbor(Direction dir) const;
  89. friend BlockType;
  90. };
  91. class BasicBlockItem : public Item
  92. {
  93. protected:
  94. bool transparent;
  95. bool passable;
  96. float hardness;
  97. int toolId;
  98. float speedModifier;
  99. bool interactable;
  100. public:
  101. BasicBlockItem(const ItemType* zType, const BlockType* zPlacedBlockType, const char* name);
  102. virtual bool canBeStackedWith(const Item* zItem) const override;
  103. friend BasicBlockItemType;
  104. friend BlockType;
  105. };
  106. class BasicBlockItemType : public ItemType
  107. {
  108. protected:
  109. BasicBlockItemType(int id, const char* name, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType, ModelInfo model);
  110. virtual void loadSuperItem(Item* zItem, Framework::StreamReader* zReader) const override;
  111. virtual void saveSuperItem(const Item* zItem, Framework::StreamWriter* zWriter) const override;
  112. void initializeItem(BasicBlockItem* zItem, bool transparent, bool passable, float hardness, int toolId, float speedModifier) const;
  113. };