Block.h 4.4 KB

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