Block.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. void broadcastPassableSpeedModifierChange();
  67. public:
  68. Block(int typeId,
  69. Framework::Vec3<int> pos,
  70. int dimensionId,
  71. bool hasInventory);
  72. virtual ~Block();
  73. void tick(TickQueue* zQueue) override;
  74. void postTick() override;
  75. void addToStructure(MultiblockStructure* structure);
  76. virtual void onLoaded();
  77. virtual void onUnloaded();
  78. virtual void setNeighbour(
  79. Direction dir, Framework::Either<Block*, int> neighbor);
  80. virtual void setNeighbourBlock(Direction dir, Block* zN);
  81. virtual void setNeighbourType(Direction dir, int type);
  82. virtual Framework::Text getTargetUIML();
  83. virtual void sendModelInfo(NetworkMessage* zMessage);
  84. virtual bool interact(Item* zItem, Entity* zActor);
  85. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  86. bool isTickSource() const;
  87. const BlockType* zBlockType() const;
  88. bool isTransparent() const;
  89. bool isPassable() const;
  90. virtual bool isInteractable(const Item* zItem) const;
  91. float getHP() const;
  92. float getMaxHP() const;
  93. float getHardness() 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* zInfo) 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. float speedModifier;
  113. bool interactable;
  114. PlaceableProof* placeableProof;
  115. public:
  116. BasicBlockItem(int itemTypeId,
  117. int blockTypeId,
  118. Framework::Text name,
  119. PlaceableProof* placeableProof);
  120. ~BasicBlockItem();
  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. private:
  130. bool transparent;
  131. bool passable;
  132. float hardness;
  133. float speedModifier;
  134. Framework::Text blockTypeName;
  135. int blockTypeId;
  136. PlaceableProof* placeableProof;
  137. public:
  138. BasicBlockItemType();
  139. BasicBlockItemType(Framework::Text name,
  140. ModelInfo* model,
  141. bool transparent,
  142. bool passable,
  143. float hardness,
  144. float speedModifier,
  145. Framework::Text blockTypeName,
  146. PlaceableProof* placeableProof,
  147. int maxStackSize,
  148. Framework::RCArray<Framework::Text> groups);
  149. ~BasicBlockItemType();
  150. protected:
  151. virtual void loadSuperItem(
  152. Item* zItem, Framework::StreamReader* zReader) const override;
  153. virtual void saveSuperItem(
  154. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  155. virtual Item* createItem() const override;
  156. public:
  157. virtual bool initialize(Game* zGame) override;
  158. int getBlockTypeId() const;
  159. void setTransparent(bool transparent);
  160. bool isTransparent() const;
  161. void setPassable(bool passable);
  162. bool isPassable() const;
  163. void setHardness(float hardness);
  164. float getHardness() const;
  165. void setSpeedModifier(float speedModifier);
  166. float getSpeedModifier() const;
  167. void setBlockTypeName(Framework::Text blockTypeName);
  168. Framework::Text getBlockTypeName() const;
  169. void setPlaceableProof(PlaceableProof* placeableProof);
  170. PlaceableProof* zPlaceableProof() const;
  171. };
  172. class BasicBlockItemTypeFactory : public ItemTypeFactoryBase<BasicBlockItemType>
  173. {
  174. public:
  175. BasicBlockItemTypeFactory();
  176. BasicBlockItemType* createValue(
  177. Framework::JSON::JSONObject* zJson) const override;
  178. void fromJson(BasicBlockItemType* zResult,
  179. Framework::JSON::JSONObject* zJson) const override;
  180. void toJson(BasicBlockItemType* zObject,
  181. Framework::JSON::JSONObject* zResult) const override;
  182. JSONObjectValidationBuilder* addToValidator(
  183. JSONObjectValidationBuilder* builder) const override;
  184. Framework::Text getTypeToken() const override;
  185. };