Block.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #include <Vec3.h>
  3. #include "Inventory.h"
  4. #include "Item.h"
  5. #include "ItemType.h"
  6. #include "MultiblockStructure.h"
  7. #include "NetworkMessage.h"
  8. #include "Tickable.h"
  9. #include "TickSourceType.h"
  10. #define CONST_BLOCK(maybeBlock, type) \
  11. ((maybeBlock) ? (maybeBlock) \
  12. : Game::INSTANCE->zBlockType((int)(type))->zDefault())
  13. class ItemType;
  14. class Chunk;
  15. class BasicBlockItemType;
  16. class PlaceableProof;
  17. class TickQueue;
  18. class Block : public Inventory,
  19. public Tickable
  20. {
  21. private:
  22. int ticksLeftCounter;
  23. int currentTickTimeout;
  24. bool wasTicked;
  25. bool onTickCalled;
  26. protected:
  27. bool transparent;
  28. bool passable;
  29. float hp;
  30. float maxHP;
  31. float hardness;
  32. int typeId;
  33. float speedModifier;
  34. int minTickTimeout;
  35. int maxTickTimeout;
  36. bool interactable;
  37. bool transmissionRequested;
  38. bool deadAndRemoved;
  39. unsigned char lightEmisionColor[3];
  40. int mapColor;
  41. Framework::RCArray<MultiblockStructure> structures;
  42. /// <summary>
  43. /// executes block specific things
  44. /// </summary>
  45. /// <param name="zqueue">a queue to add neighbor blocks that should be
  46. /// ticked after this block</param> <param name="numTicks">the number of
  47. /// ticks passed since the last call (only for tickSources)</param> <param
  48. /// name="blocked">can be set to one to tell that this block needs to be
  49. /// tickt again later in the queue of this tick</param> <returns>true, iff
  50. /// 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. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill);
  59. virtual void onDialogClosed(Framework::Text dialogId);
  60. void broadcastModelInfoChange();
  61. void broadcastMessage(NetworkMessage* message);
  62. void broadcastPassableSpeedModifierChange();
  63. virtual void onApiCall(char messageType,
  64. Framework::StreamReader* zRequest,
  65. NetworkMessage* zResponse,
  66. Entity* zSource);
  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 Framework::XML::Element* getTargetUIML() const;
  79. virtual void sendModelInfo(NetworkMessage* zMessage);
  80. virtual bool interact(Item* zItem, Entity* zActor, bool& itemChanged);
  81. void api(Framework::StreamReader* zRequest,
  82. NetworkMessage* zResponse,
  83. Entity* zSource);
  84. virtual TickSourceType isTickSource() const;
  85. virtual bool needsTick() 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. void setHP(
  96. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp);
  97. bool isDeadAndRemoved() const;
  98. virtual void getLightEmisionColor(unsigned char* result) const;
  99. virtual void filterPassingLight(unsigned char rgb[3]) const;
  100. void updateModel(ModelInfo* zInfo) const;
  101. int getMapColor() const;
  102. friend BlockType;
  103. };
  104. class BasicBlockItem : public Item
  105. {
  106. protected:
  107. bool transparent;
  108. bool passable;
  109. float hardness;
  110. float speedModifier;
  111. bool interactable;
  112. PlaceableProof* placeableProof;
  113. public:
  114. BasicBlockItem(int itemTypeId,
  115. int blockTypeId,
  116. Framework::Text name,
  117. PlaceableProof* placeableProof);
  118. ~BasicBlockItem();
  119. virtual bool canBeStackedWith(const Item* zItem) const override;
  120. virtual bool canBePlacedAt(
  121. int dimensionId, Framework::Vec3<int> worldPos) const override;
  122. friend BasicBlockItemType;
  123. friend BlockType;
  124. };
  125. class BasicBlockItemType : public ItemType
  126. {
  127. private:
  128. bool transparent;
  129. bool passable;
  130. float hardness;
  131. float speedModifier;
  132. Framework::Text blockTypeName;
  133. int blockTypeId;
  134. PlaceableProof* placeableProof;
  135. public:
  136. BasicBlockItemType();
  137. BasicBlockItemType(Framework::Text name,
  138. ModelInfo* model,
  139. bool transparent,
  140. bool passable,
  141. float hardness,
  142. float speedModifier,
  143. Framework::Text blockTypeName,
  144. PlaceableProof* placeableProof,
  145. int maxStackSize,
  146. Framework::RCArray<Framework::Text> groups);
  147. ~BasicBlockItemType();
  148. protected:
  149. virtual void loadSuperItem(
  150. Item* zItem, Framework::StreamReader* zReader) const override;
  151. virtual void saveSuperItem(
  152. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  153. virtual Item* createItem() const override;
  154. public:
  155. virtual bool initialize(Game* zGame) override;
  156. int getBlockTypeId() const;
  157. void setTransparent(bool transparent);
  158. bool isTransparent() const;
  159. void setPassable(bool passable);
  160. bool isPassable() const;
  161. void setHardness(float hardness);
  162. float getHardness() const;
  163. void setSpeedModifier(float speedModifier);
  164. float getSpeedModifier() const;
  165. void setBlockTypeName(Framework::Text blockTypeName);
  166. Framework::Text getBlockTypeName() const;
  167. void setPlaceableProof(PlaceableProof* placeableProof);
  168. PlaceableProof* zPlaceableProof() const;
  169. };
  170. class BasicBlockItemTypeFactory : public ItemTypeFactoryBase<BasicBlockItemType>
  171. {
  172. public:
  173. BasicBlockItemTypeFactory();
  174. BasicBlockItemType* createValue(
  175. Framework::JSON::JSONObject* zJson) const override;
  176. BasicBlockItemType* fromJson(
  177. Framework::JSON::JSONObject* zJson) const override;
  178. Framework::JSON::JSONObject* toJsonObject(
  179. BasicBlockItemType* zObject) const override;
  180. JSONObjectValidationBuilder* addToValidator(
  181. JSONObjectValidationBuilder* builder) const override;
  182. const char* getTypeToken() const override;
  183. };