Block.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. public:
  67. Block(int typeId,
  68. Framework::Vec3<int> pos,
  69. int dimensionId,
  70. bool hasInventory);
  71. virtual ~Block();
  72. void tick(TickQueue* zQueue) override;
  73. void postTick() override;
  74. void addToStructure(MultiblockStructure* structure);
  75. virtual void onLoaded();
  76. virtual void onUnloaded();
  77. virtual void setNeighbour(
  78. Direction dir, Framework::Either<Block*, int> neighbor);
  79. virtual void setNeighbourBlock(Direction dir, Block* zN);
  80. virtual void setNeighbourType(Direction dir, int type);
  81. virtual Framework::Text getTargetUIML();
  82. virtual void sendModelInfo(NetworkMessage* zMessage);
  83. virtual bool interact(Item* zItem, Entity* zActor);
  84. void api(Framework::StreamReader* zRequest, NetworkMessage* zResponse);
  85. bool isTickSource() 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. bool isVisible() const;
  96. void setHP(float hp);
  97. bool isDeadAndRemoved() const;
  98. const unsigned char* getLightEmisionColor() const;
  99. virtual void filterPassingLight(unsigned char rgb[3]) const;
  100. Block* zNeighbor(Direction dir) const;
  101. void updateModel(ModelInfo* zInfo) const;
  102. int getMapColor() const;
  103. friend BlockType;
  104. };
  105. class BasicBlockItem : public Item
  106. {
  107. protected:
  108. bool transparent;
  109. bool passable;
  110. float hardness;
  111. float speedModifier;
  112. bool interactable;
  113. PlaceableProof* placeableProof;
  114. public:
  115. BasicBlockItem(int itemTypeId,
  116. int blockTypeId,
  117. Framework::Text name,
  118. PlaceableProof* placeableProof);
  119. ~BasicBlockItem();
  120. virtual bool canBeStackedWith(const Item* zItem) const override;
  121. virtual bool canBePlacedAt(
  122. int dimensionId, Framework::Vec3<int> worldPos) const override;
  123. friend BasicBlockItemType;
  124. friend BlockType;
  125. };
  126. class BasicBlockItemType : public ItemType
  127. {
  128. private:
  129. bool transparent;
  130. bool passable;
  131. float hardness;
  132. float speedModifier;
  133. Framework::Text blockTypeName;
  134. int blockTypeId;
  135. PlaceableProof* placeableProof;
  136. protected:
  137. virtual void loadSuperItem(
  138. Item* zItem, Framework::StreamReader* zReader) const override;
  139. virtual void saveSuperItem(
  140. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  141. virtual Item* createItem() const override;
  142. public:
  143. BasicBlockItemType(Framework::Text name,
  144. ModelInfo* model,
  145. bool transparent,
  146. bool passable,
  147. float hardness,
  148. float speedModifier,
  149. Framework::Text blockTypeName,
  150. PlaceableProof* placeableProof,
  151. int maxStackSize,
  152. Framework::RCArray<Framework::Text> groups);
  153. ~BasicBlockItemType();
  154. virtual bool initialize(Game* zGame) override;
  155. int getBlockTypeId() const;
  156. bool isTransparent() const;
  157. bool isPassable() const;
  158. float getHardness() const;
  159. float getSpeedModifier() const;
  160. Framework::Text getBlockTypeName() const;
  161. PlaceableProof* zPlaceableProof() const;
  162. };
  163. class BasicBlockItemTypeFactory
  164. : public SubTypeFactory<ItemType, BasicBlockItemType>
  165. {
  166. public:
  167. BasicBlockItemTypeFactory();
  168. BasicBlockItemType* fromJson(
  169. Framework::JSON::JSONObject* zJson) const override;
  170. Framework::JSON::JSONObject* toJson(
  171. BasicBlockItemType* zObject) const override;
  172. Framework::JSON::Validator::JSONValidator* getValidator(
  173. Framework::JSON::Validator::ObjectValidationBuilder<
  174. Framework::JSON::Validator::JSONValidator>* builder) const override;
  175. Framework::Text getTypeToken() const override;
  176. };