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. #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. int minTickTimeout;
  38. int maxTickTimeout;
  39. bool interactable;
  40. bool transmissionRequested;
  41. bool deadAndRemoved;
  42. unsigned char lightEmisionColor[3];
  43. int mapColor;
  44. Framework::RCArray<MultiblockStructure> structures;
  45. /// <summary>
  46. /// executes block specific things
  47. /// </summary>
  48. /// <param name="zqueue">a queue to add neighbor blocks that should be
  49. /// ticked after this block</param> <param name="numTicks">the number of
  50. /// ticks passed since the last call (only for tickSources)</param> <param
  51. /// name="blocked">can be set to one to tell that this block needs to be
  52. /// tickt again later in the queue of this tick</param> <returns>true, iff
  53. /// the block needs to be ticked more often</returns>
  54. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  55. /// <summary>
  56. /// gets called after each block was tickt.
  57. /// the order of blocks called will be exactly the same as onTick
  58. /// </summary>
  59. virtual void onPostTick() = 0;
  60. virtual void onDestroy(
  61. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill);
  62. virtual void onDialogClosed(Framework::Text dialogId);
  63. void broadcastModelInfoChange();
  64. void broadcastMessage(NetworkMessage* message);
  65. void broadcastPassableSpeedModifierChange();
  66. virtual void onApiCall(char messageType,
  67. Framework::StreamReader* zRequest,
  68. NetworkMessage* zResponse,
  69. Entity* zSource);
  70. public:
  71. Block(int typeId,
  72. Framework::Vec3<int> pos,
  73. int dimensionId,
  74. bool hasInventory);
  75. virtual ~Block();
  76. void tick(TickQueue* zQueue) override;
  77. void postTick() override;
  78. void addToStructure(MultiblockStructure* structure);
  79. virtual void onLoaded();
  80. virtual void onUnloaded();
  81. virtual Framework::XML::Element* getTargetUIML() const;
  82. virtual void sendModelInfo(NetworkMessage* zMessage);
  83. virtual bool interact(Item* zItem, Entity* zActor, bool& itemChanged);
  84. void api(Framework::StreamReader* zRequest,
  85. NetworkMessage* zResponse,
  86. Entity* zSource);
  87. virtual TickSourceType isTickSource() const;
  88. virtual bool needsTick() const;
  89. const BlockType* zBlockType() const;
  90. bool isTransparent() const;
  91. bool isPassable() const;
  92. virtual bool isInteractable(const Item* zItem) const;
  93. float getHP() const;
  94. float getMaxHP() const;
  95. float getHardness() const;
  96. float getSpeedModifier() const;
  97. const Framework::Vec3<int> getPos() const;
  98. void setHP(
  99. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp);
  100. bool isDeadAndRemoved() const;
  101. virtual void getLightEmisionColor(unsigned char* result) const;
  102. virtual void filterPassingLight(unsigned char rgb[3]) const;
  103. void updateModel(ModelInfo* zInfo) const;
  104. int getMapColor() const;
  105. friend BlockType;
  106. };
  107. class BasicBlockItem : public Item
  108. {
  109. protected:
  110. bool transparent;
  111. bool passable;
  112. float hardness;
  113. float speedModifier;
  114. bool interactable;
  115. PlaceableProof* placeableProof;
  116. public:
  117. BasicBlockItem(int itemTypeId,
  118. int blockTypeId,
  119. Framework::Text name,
  120. PlaceableProof* placeableProof);
  121. ~BasicBlockItem();
  122. virtual bool canBeStackedWith(const Item* zItem) const override;
  123. virtual bool canBePlacedAt(
  124. int dimensionId, Framework::Vec3<int> worldPos) const override;
  125. friend BasicBlockItemType;
  126. friend BlockType;
  127. };
  128. class BasicBlockItemType : public ItemType
  129. {
  130. private:
  131. bool transparent;
  132. bool passable;
  133. float hardness;
  134. float speedModifier;
  135. Framework::Text blockTypeName;
  136. int blockTypeId;
  137. PlaceableProof* placeableProof;
  138. public:
  139. BasicBlockItemType();
  140. BasicBlockItemType(Framework::Text name,
  141. ModelInfo* model,
  142. bool transparent,
  143. bool passable,
  144. float hardness,
  145. float speedModifier,
  146. Framework::Text blockTypeName,
  147. PlaceableProof* placeableProof,
  148. int maxStackSize,
  149. Framework::RCArray<Framework::Text> groups);
  150. ~BasicBlockItemType();
  151. protected:
  152. virtual void loadSuperItem(
  153. Item* zItem, Framework::StreamReader* zReader) const override;
  154. virtual void saveSuperItem(
  155. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  156. virtual Item* createItem() const override;
  157. public:
  158. virtual bool initialize(Game* zGame) override;
  159. int getBlockTypeId() const;
  160. void setTransparent(bool transparent);
  161. bool isTransparent() const;
  162. void setPassable(bool passable);
  163. bool isPassable() const;
  164. void setHardness(float hardness);
  165. float getHardness() const;
  166. void setSpeedModifier(float speedModifier);
  167. float getSpeedModifier() const;
  168. void setBlockTypeName(Framework::Text blockTypeName);
  169. Framework::Text getBlockTypeName() const;
  170. void setPlaceableProof(PlaceableProof* placeableProof);
  171. PlaceableProof* zPlaceableProof() const;
  172. };
  173. class BasicBlockItemTypeFactory : public ItemTypeFactoryBase<BasicBlockItemType>
  174. {
  175. public:
  176. BasicBlockItemTypeFactory();
  177. BasicBlockItemType* createValue(
  178. Framework::JSON::JSONObject* zJson) const override;
  179. BasicBlockItemType* fromJson(
  180. Framework::JSON::JSONObject* zJson) const override;
  181. Framework::JSON::JSONObject* toJsonObject(
  182. BasicBlockItemType* zObject) const override;
  183. JSONObjectValidationBuilder* addToValidator(
  184. JSONObjectValidationBuilder* builder) const override;
  185. const char* getTypeToken() const override;
  186. };