Block.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "MultiblockStructure.h"
  9. #include "NetworkMessage.h"
  10. #include "ReferenceCounter.h"
  11. #include "Tickable.h"
  12. #define CONST_BLOCK(maybeBlock, type) \
  13. (maybeBlock ? maybeBlock \
  14. : Game::INSTANCE->zBlockType((int)type)->zDefault())
  15. class ItemType;
  16. class Chunk;
  17. class BasicBlockItemType;
  18. class PlaceableProof;
  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. float speedModifier;
  36. Block* zNeighbours[6];
  37. int neighbourTypes[6];
  38. int minTickTimeout;
  39. int maxTickTimeout;
  40. bool tickSource;
  41. bool interactable;
  42. bool transmissionRequested;
  43. bool deadAndRemoved;
  44. unsigned char lightEmisionColor[3];
  45. int mapColor;
  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. virtual void onDialogClosed(Framework::Text dialogId);
  64. void broadcastModelInfoChange();
  65. void broadcastMessage(NetworkMessage* message);
  66. public:
  67. Block(int typeId,
  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 bool 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. float getSpeedModifier() const;
  94. const Framework::Vec3<int> getPos() 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. void updateModel(ModelInfo* zInfo) const;
  102. int getMapColor() const;
  103. friend BlockType;
  104. };
  105. class BasicBlockItem : public Item
  106. {
  107. protected:
  108. bool transparent;
  109. bool passable;
  110. float hardness;
  111. float speedModifier;
  112. bool interactable;
  113. PlaceableProof* placeableProof;
  114. public:
  115. BasicBlockItem(int itemTypeId,
  116. int blockTypeId,
  117. Framework::Text name,
  118. PlaceableProof* placeableProof);
  119. ~BasicBlockItem();
  120. virtual bool canBeStackedWith(const Item* zItem) const override;
  121. virtual bool canBePlacedAt(
  122. int dimensionId, Framework::Vec3<int> worldPos) const override;
  123. friend BasicBlockItemType;
  124. friend BlockType;
  125. };
  126. class BasicBlockItemType : public ItemType
  127. {
  128. private:
  129. bool transparent;
  130. bool passable;
  131. float hardness;
  132. float speedModifier;
  133. Framework::Text blockTypeName;
  134. int blockTypeId;
  135. PlaceableProof* placeableProof;
  136. public:
  137. BasicBlockItemType();
  138. BasicBlockItemType(Framework::Text name,
  139. ModelInfo* model,
  140. bool transparent,
  141. bool passable,
  142. float hardness,
  143. float speedModifier,
  144. Framework::Text blockTypeName,
  145. PlaceableProof* placeableProof,
  146. int maxStackSize,
  147. Framework::RCArray<Framework::Text> groups);
  148. ~BasicBlockItemType();
  149. protected:
  150. virtual void loadSuperItem(
  151. Item* zItem, Framework::StreamReader* zReader) const override;
  152. virtual void saveSuperItem(
  153. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  154. virtual Item* createItem() const override;
  155. public:
  156. virtual bool initialize(Game* zGame) override;
  157. int getBlockTypeId() const;
  158. void setTransparent(bool transparent);
  159. bool isTransparent() const;
  160. void setPassable(bool passable);
  161. bool isPassable() const;
  162. void setHardness(float hardness);
  163. float getHardness() const;
  164. void setSpeedModifier(float speedModifier);
  165. float getSpeedModifier() const;
  166. void setBlockTypeName(Framework::Text blockTypeName);
  167. Framework::Text getBlockTypeName() const;
  168. void setPlaceableProof(PlaceableProof* placeableProof);
  169. PlaceableProof* zPlaceableProof() const;
  170. };
  171. class BasicBlockItemTypeFactory : public ItemTypeFactoryBase<BasicBlockItemType>
  172. {
  173. public:
  174. BasicBlockItemTypeFactory();
  175. BasicBlockItemType* createValue(
  176. Framework::JSON::JSONObject* zJson) const override;
  177. void fromJson(BasicBlockItemType* zResult,
  178. Framework::JSON::JSONObject* zJson) const override;
  179. void toJson(BasicBlockItemType* zObject,
  180. Framework::JSON::JSONObject* zResult) const override;
  181. JSONObjectValidationBuilder* addToValidator(
  182. JSONObjectValidationBuilder* builder) const override;
  183. Framework::Text getTypeToken() const override;
  184. };