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. 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. 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. virtual TickSourceType isTickSource() const;
  87. virtual bool needsTick() const;
  88. const BlockType* zBlockType() const;
  89. bool isTransparent() const;
  90. bool isPassable() const;
  91. virtual bool isInteractable(const Item* zItem) const;
  92. float getHP() const;
  93. float getMaxHP() const;
  94. float getHardness() const;
  95. float getSpeedModifier() const;
  96. const Framework::Vec3<int> getPos() const;
  97. bool isVisible() const;
  98. void setHP(float hp);
  99. bool isDeadAndRemoved() const;
  100. const unsigned char* getLightEmisionColor() const;
  101. virtual void filterPassingLight(unsigned char rgb[3]) const;
  102. Block* zNeighbor(Direction dir) 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. void fromJson(BasicBlockItemType* zResult,
  180. Framework::JSON::JSONObject* zJson) const override;
  181. void toJson(BasicBlockItemType* zObject,
  182. Framework::JSON::JSONObject* zResult) const override;
  183. JSONObjectValidationBuilder* addToValidator(
  184. JSONObjectValidationBuilder* builder) const override;
  185. Framework::Text getTypeToken() const override;
  186. };