Block.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "TickSourceType.h"
  13. #define CONST_BLOCK(maybeBlock, type) \
  14. (maybeBlock ? maybeBlock \
  15. : Game::INSTANCE->zBlockType((int)type)->zDefault())
  16. class ItemType;
  17. class Chunk;
  18. class BasicBlockItemType;
  19. class PlaceableProof;
  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. protected:
  30. bool transparent;
  31. bool passable;
  32. float hp;
  33. float maxHP;
  34. float hardness;
  35. int typeId;
  36. float speedModifier;
  37. Block* zNeighbours[6];
  38. int neighbourTypes[6];
  39. int minTickTimeout;
  40. int maxTickTimeout;
  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. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill);
  64. virtual void onDialogClosed(Framework::Text dialogId);
  65. void broadcastModelInfoChange();
  66. void broadcastMessage(NetworkMessage* message);
  67. void broadcastPassableSpeedModifierChange();
  68. virtual void onApiCall(char messageType,
  69. Framework::StreamReader* zRequest,
  70. NetworkMessage* zResponse,
  71. Entity* zSource);
  72. public:
  73. Block(int typeId,
  74. Framework::Vec3<int> pos,
  75. int dimensionId,
  76. bool hasInventory);
  77. virtual ~Block();
  78. void tick(TickQueue* zQueue) override;
  79. void postTick() override;
  80. void addToStructure(MultiblockStructure* structure);
  81. virtual void onLoaded();
  82. virtual void onUnloaded();
  83. virtual void setNeighbour(
  84. Direction dir, Framework::Either<Block*, int> neighbor);
  85. virtual void setNeighbourBlock(Direction dir, Block* zN);
  86. virtual void setNeighbourType(Direction dir, int type);
  87. virtual Framework::XML::Element* getTargetUIML() const;
  88. virtual void sendModelInfo(NetworkMessage* zMessage);
  89. virtual bool interact(Item* zItem, Entity* zActor, bool& itemChanged);
  90. void api(Framework::StreamReader* zRequest,
  91. NetworkMessage* zResponse,
  92. Entity* zSource);
  93. virtual TickSourceType isTickSource() const;
  94. virtual bool needsTick() const;
  95. const BlockType* zBlockType() const;
  96. bool isTransparent() const;
  97. bool isPassable() const;
  98. virtual bool isInteractable(const Item* zItem) const;
  99. float getHP() const;
  100. float getMaxHP() const;
  101. float getHardness() const;
  102. float getSpeedModifier() const;
  103. const Framework::Vec3<int> getPos() const;
  104. bool isVisible() const;
  105. void setHP(
  106. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp);
  107. bool isDeadAndRemoved() const;
  108. virtual void getLightEmisionColor(unsigned char* result) const;
  109. virtual void filterPassingLight(unsigned char rgb[3]) const;
  110. Block* zNeighbor(Direction dir) const;
  111. void updateModel(ModelInfo* zInfo) const;
  112. int getMapColor() const;
  113. friend BlockType;
  114. };
  115. class BasicBlockItem : public Item
  116. {
  117. protected:
  118. bool transparent;
  119. bool passable;
  120. float hardness;
  121. float speedModifier;
  122. bool interactable;
  123. PlaceableProof* placeableProof;
  124. public:
  125. BasicBlockItem(int itemTypeId,
  126. int blockTypeId,
  127. Framework::Text name,
  128. PlaceableProof* placeableProof);
  129. ~BasicBlockItem();
  130. virtual bool canBeStackedWith(const Item* zItem) const override;
  131. virtual bool canBePlacedAt(
  132. int dimensionId, Framework::Vec3<int> worldPos) const override;
  133. friend BasicBlockItemType;
  134. friend BlockType;
  135. };
  136. class BasicBlockItemType : public ItemType
  137. {
  138. private:
  139. bool transparent;
  140. bool passable;
  141. float hardness;
  142. float speedModifier;
  143. Framework::Text blockTypeName;
  144. int blockTypeId;
  145. PlaceableProof* placeableProof;
  146. public:
  147. BasicBlockItemType();
  148. BasicBlockItemType(Framework::Text name,
  149. ModelInfo* model,
  150. bool transparent,
  151. bool passable,
  152. float hardness,
  153. float speedModifier,
  154. Framework::Text blockTypeName,
  155. PlaceableProof* placeableProof,
  156. int maxStackSize,
  157. Framework::RCArray<Framework::Text> groups);
  158. ~BasicBlockItemType();
  159. protected:
  160. virtual void loadSuperItem(
  161. Item* zItem, Framework::StreamReader* zReader) const override;
  162. virtual void saveSuperItem(
  163. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  164. virtual Item* createItem() const override;
  165. public:
  166. virtual bool initialize(Game* zGame) override;
  167. int getBlockTypeId() const;
  168. void setTransparent(bool transparent);
  169. bool isTransparent() const;
  170. void setPassable(bool passable);
  171. bool isPassable() const;
  172. void setHardness(float hardness);
  173. float getHardness() const;
  174. void setSpeedModifier(float speedModifier);
  175. float getSpeedModifier() const;
  176. void setBlockTypeName(Framework::Text blockTypeName);
  177. Framework::Text getBlockTypeName() const;
  178. void setPlaceableProof(PlaceableProof* placeableProof);
  179. PlaceableProof* zPlaceableProof() const;
  180. };
  181. class BasicBlockItemTypeFactory : public ItemTypeFactoryBase<BasicBlockItemType>
  182. {
  183. public:
  184. BasicBlockItemTypeFactory();
  185. BasicBlockItemType* createValue(
  186. Framework::JSON::JSONObject* zJson) const override;
  187. BasicBlockItemType* fromJson(
  188. Framework::JSON::JSONObject* zJson) const override;
  189. Framework::JSON::JSONObject* toJsonObject(
  190. BasicBlockItemType* zObject) const override;
  191. JSONObjectValidationBuilder* addToValidator(
  192. JSONObjectValidationBuilder* builder) const override;
  193. const char* getTypeToken() const override;
  194. };