BlockType.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include <Array.h>
  3. #include <Either.h>
  4. #include <Vec3.h>
  5. #include <Writer.h>
  6. #include <XML.h>
  7. #include "DropConfig.h"
  8. #include "InteractionConfig.h"
  9. #include "ModelInfo.h"
  10. class Item;
  11. class Block;
  12. class Game;
  13. class ItemType;
  14. class BlockTypeEnum
  15. {
  16. public:
  17. static const int NO_BLOCK = 0;
  18. static const int AIR = 1;
  19. };
  20. class BlockType : public virtual Framework::ReferenceCounter
  21. {
  22. private:
  23. int id;
  24. ModelInfo* model;
  25. int initialMaxHP;
  26. bool needsClientInstance;
  27. bool lightSource;
  28. Framework::Text name;
  29. bool needModelSubscription;
  30. int initialMapColor;
  31. Framework::RCArray<Framework::Text> groupNames;
  32. float hardness;
  33. Framework::RCArray<DropConfig> dropConfigs;
  34. bool damagableByHand;
  35. Framework::RCArray<InteractionConfig> interactionConfigs;
  36. const ItemType* itemType;
  37. protected:
  38. Block* defaultBlock;
  39. BlockType();
  40. virtual ~BlockType();
  41. virtual void loadSuperBlock(
  42. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const;
  43. virtual void saveSuperBlock(
  44. Block* zBlock, Framework::StreamWriter* zWriter) const;
  45. virtual void createSuperBlock(Block* zBlock, Item* zItem) const;
  46. virtual void createSuperItem(Block* zBlock, Item* zItem) const;
  47. virtual Block* createBlock(
  48. Framework::Vec3<int> position, int dimensionId) const
  49. = 0;
  50. virtual Item* createItem() const = 0;
  51. virtual ItemType* createItemTypeInternal() const = 0;
  52. public:
  53. virtual bool initialize(Game* zGame);
  54. virtual BlockType* initializeDefault();
  55. void addDropConfig(DropConfig* config);
  56. const Framework::RCArray<DropConfig>& getDropConfigs() const;
  57. const Block* zDefault() const;
  58. ItemType* createItemType();
  59. virtual int getItemTypeId() const;
  60. void writeTypeInfo(Framework::StreamWriter* zWriter) const;
  61. virtual Framework::XML::Element* getTargetUIML() const;
  62. virtual Block* loadBlock(Framework::Vec3<int> position,
  63. Framework::StreamReader* zReader,
  64. int dimensionId) const;
  65. virtual void saveBlock(
  66. Block* zBlock, Framework::StreamWriter* zWriter) const;
  67. virtual Item* getItemFromBlock(Block* zBlock) const;
  68. virtual Block* createBlockAt(
  69. Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const;
  70. int getId() const;
  71. virtual bool isFluid() const;
  72. virtual unsigned char getFlowDistance() const;
  73. void setTypeId(int id);
  74. void setModel(ModelInfo* model);
  75. ModelInfo* zModel() const;
  76. void setInitialMaxHP(int initialMaxHP);
  77. int getInitialMaxHP() const;
  78. void setNeedsClientInstance(bool needsClientInstance);
  79. bool doesNeedClientInstance() const;
  80. void setLightSource(bool lightSource);
  81. bool isLightSource() const;
  82. void setName(Framework::Text name);
  83. const char* getName() const;
  84. void setNeedModelSubscription(bool needModelSubscription);
  85. const bool doesNeedModelSubscription() const;
  86. void setMapColor(int mapColor);
  87. int getMapColor() const;
  88. void setGroupNames(Framework::RCArray<Framework::Text> groupNames);
  89. const Framework::RCArray<Framework::Text>& getGroupNames() const;
  90. void setHardness(float hardness);
  91. float getHardness() const;
  92. void setDamagableByHand(bool damagableByHand);
  93. bool isDamagableByHand() const;
  94. void addInteractionConfig(InteractionConfig* config);
  95. const Framework::RCArray<InteractionConfig>& getInteractionConfigs() const;
  96. static int getTypeId(const char* name);
  97. static Framework::Text getTypeName(int id);
  98. };
  99. const Block* getDefaultBlock(Framework::Either<Block*, int> b);
  100. template<typename S> class BlockTypeFactoryBase
  101. : public SubTypeFactory<BlockType, S>
  102. {
  103. public:
  104. BlockTypeFactoryBase()
  105. : SubTypeFactory<BlockType, S>()
  106. {}
  107. virtual S* fromJson(Framework::JSON::JSONObject* zJson) const override
  108. {
  109. S* result = createValue(zJson);
  110. BlockType* zType = dynamic_cast<BlockType*>(result);
  111. zType->setModel(Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  112. zJson->zValue("model")->asObject()));
  113. zType->setInitialMaxHP(
  114. (int)zJson->zValue("maxHp")->asNumber()->getNumber());
  115. zType->setNeedsClientInstance(
  116. zJson->zValue("needsClientInstance")->asBool()->getBool());
  117. zType->setLightSource(
  118. zJson->zValue("lightSource")->asBool()->getBool());
  119. zType->setName(zJson->zValue("name")->asString()->getString());
  120. zType->setNeedModelSubscription(
  121. zJson->zValue("needModelSubscription")->asBool()->getBool());
  122. zType->setMapColor(
  123. (int)zJson->zValue("mapColor")->asString()->getString());
  124. Framework::RCArray<Framework::Text> groupNames;
  125. for (Framework::JSON::JSONValue* value :
  126. *zJson->zValue("groupNames")->asArray())
  127. {
  128. groupNames.add(new Framework::Text(value->asString()->getString()));
  129. }
  130. zType->setGroupNames(groupNames);
  131. zType->setHardness(
  132. (float)zJson->zValue("hardness")->asNumber()->getNumber());
  133. for (Framework::JSON::JSONValue* value :
  134. *zJson->zValue("drops")->asArray())
  135. {
  136. zType->addDropConfig(
  137. Game::INSTANCE->zTypeRegistry()->fromJson<DropConfig>(value));
  138. }
  139. zType->setDamagableByHand(
  140. zJson->zValue("damagableByHand")->asBool()->getBool());
  141. for (Framework::JSON::JSONValue* value :
  142. *zJson->zValue("interactions")->asArray())
  143. {
  144. zType->addInteractionConfig(
  145. Game::INSTANCE->zTypeRegistry()->fromJson<InteractionConfig>(
  146. value));
  147. }
  148. return result;
  149. }
  150. virtual Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  151. {
  152. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  153. BlockType* zType = dynamic_cast<BlockType*>(zObject);
  154. result->addValue("model",
  155. Game::INSTANCE->zTypeRegistry()->toJson<ModelInfo>(
  156. zType->zModel()));
  157. result->addValue("maxHp",
  158. new Framework::JSON::JSONNumber((double)zType->getInitialMaxHP()));
  159. result->addValue("needsClientInstance",
  160. new Framework::JSON::JSONBool(zType->doesNeedClientInstance()));
  161. result->addValue("lightSource",
  162. new Framework::JSON::JSONBool(zType->isLightSource()));
  163. result->addValue(
  164. "name", new Framework::JSON::JSONString(zType->getName()));
  165. result->addValue("needModelSubscription",
  166. new Framework::JSON::JSONBool(zType->doesNeedModelSubscription()));
  167. result->addValue("mapColor",
  168. new Framework::JSON::JSONString(
  169. Framework::Text(zType->getMapColor())));
  170. Framework::JSON::JSONArray* groupNames
  171. = new Framework::JSON::JSONArray();
  172. for (Framework::Text* groupName : zType->getGroupNames())
  173. {
  174. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  175. }
  176. result->addValue("groupNames", groupNames);
  177. Framework::JSON::JSONArray* drops = new Framework::JSON::JSONArray();
  178. for (DropConfig* drop : zType->getDropConfigs())
  179. {
  180. drops->addValue(
  181. Game::INSTANCE->zTypeRegistry()->toJson<DropConfig>(drop));
  182. }
  183. result->addValue("drops", drops);
  184. result->addValue(
  185. "hardness", new Framework::JSON::JSONNumber(zType->getHardness()));
  186. result->addValue("damagableByHand",
  187. new Framework::JSON::JSONBool(zType->isDamagableByHand()));
  188. Framework::JSON::JSONArray* interactions
  189. = new Framework::JSON::JSONArray();
  190. for (InteractionConfig* interaction : zType->getInteractionConfigs())
  191. {
  192. interactions->addValue(
  193. Game::INSTANCE->zTypeRegistry()->toJson<InteractionConfig>(
  194. interaction));
  195. }
  196. result->addValue("interactions", interactions);
  197. return result;
  198. }
  199. virtual JSONObjectValidationBuilder* addToValidator(
  200. JSONObjectValidationBuilder* builder) const override
  201. {
  202. Framework::JSON::JSONArray* defaultDrops
  203. = new Framework::JSON::JSONArray();
  204. Framework::JSON::JSONObject* defaultBlockItemDrop
  205. = new Framework::JSON::JSONObject();
  206. defaultBlockItemDrop->addValue(
  207. "type", new Framework::JSON::JSONString("blockItem"));
  208. Framework::JSON::JSONObject* defaultDropCondition
  209. = new Framework::JSON::JSONObject();
  210. defaultDropCondition->addValue(
  211. "type", new Framework::JSON::JSONString("allways"));
  212. defaultBlockItemDrop->addValue("condition", defaultDropCondition);
  213. defaultDrops->addValue(defaultBlockItemDrop);
  214. return builder
  215. ->withRequiredAttribute("model",
  216. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  217. ->withRequiredNumber("maxHp")
  218. ->withDefault(100.0)
  219. ->finishNumber()
  220. ->withRequiredBool("needsClientInstance")
  221. ->withDefault(true)
  222. ->finishBool()
  223. ->withRequiredBool("lightSource")
  224. ->withDefault(false)
  225. ->finishBool()
  226. ->withRequiredString("name")
  227. ->finishString()
  228. ->withRequiredBool("needModelSubscription")
  229. ->withDefault(true)
  230. ->finishBool()
  231. ->withRequiredString("mapColor")
  232. ->finishString()
  233. ->withRequiredArray("groupNames")
  234. ->withDefault(new Framework::JSON::JSONArray())
  235. ->addAcceptedStringInArray()
  236. ->finishString()
  237. ->finishArray()
  238. ->withRequiredNumber("hardness")
  239. ->withDefault(1.0)
  240. ->finishNumber()
  241. ->withRequiredAttribute("drops",
  242. Framework::Validator::DataValidator::buildForArray()
  243. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  244. ->getValidator<DropConfig>())
  245. ->withDefault(defaultDrops)
  246. ->finishArray())
  247. ->withRequiredBool("damagableByHand")
  248. ->withDefault(false)
  249. ->finishBool()
  250. ->withRequiredArray("interactions")
  251. ->withDefault(new Framework::JSON::JSONArray())
  252. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  253. ->getValidator<InteractionConfig>())
  254. ->finishArray();
  255. }
  256. protected:
  257. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  258. };