BlockFilter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #include "BlockFilter.h"
  2. #include "Block.h"
  3. #include "FluidBlock.h"
  4. #include "Game.h"
  5. BlockFilter::BlockFilter()
  6. : ReferenceCounter()
  7. {}
  8. BlockFilterAnd::BlockFilterAnd(Framework::RCArray<BlockFilter> filters)
  9. : BlockFilter(),
  10. filters(filters)
  11. {}
  12. bool BlockFilterAnd::test(const Block* zBlock)
  13. {
  14. for (BlockFilter* filter : filters)
  15. {
  16. if (!filter->test(zBlock))
  17. {
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. Framework::RCArray<BlockFilter> BlockFilterAnd::getFilters() const
  24. {
  25. return filters;
  26. }
  27. BlockFilterAndFactory::BlockFilterAndFactory()
  28. : SubTypeFactory()
  29. {}
  30. BlockFilterAnd* BlockFilterAndFactory::fromJson(
  31. Framework::JSON::JSONObject* zJson) const
  32. {
  33. Framework::RCArray<BlockFilter> filters;
  34. for (Framework::JSON::JSONValue* value :
  35. *zJson->zValue("filters")->asArray())
  36. {
  37. filters.add(
  38. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(value));
  39. }
  40. return new BlockFilterAnd(filters);
  41. }
  42. Framework::JSON::JSONObject* BlockFilterAndFactory::toJson(
  43. BlockFilterAnd* zObject) const
  44. {
  45. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  46. Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
  47. for (BlockFilter* filter : zObject->getFilters())
  48. {
  49. filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
  50. }
  51. result->addValue("filters", filters);
  52. return result;
  53. }
  54. Framework::JSON::Validator::JSONValidator* BlockFilterAndFactory::getValidator(
  55. Framework::JSON::Validator::ObjectValidationBuilder<
  56. Framework::JSON::Validator::JSONValidator>* builder) const
  57. {
  58. return builder->withRequiredArray("filters")
  59. ->addAcceptedTypeInArray(
  60. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  61. ->finishArray()
  62. ->finishObject();
  63. }
  64. Framework::Text BlockFilterAndFactory::getTypeToken() const
  65. {
  66. return "and";
  67. }
  68. BlockFilterOr::BlockFilterOr(Framework::RCArray<BlockFilter> filters)
  69. : BlockFilter(),
  70. filters(filters)
  71. {}
  72. bool BlockFilterOr::test(const Block* zBlock)
  73. {
  74. for (BlockFilter* filter : filters)
  75. {
  76. if (filter->test(zBlock))
  77. {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. Framework::RCArray<BlockFilter> BlockFilterOr::getFilters() const
  84. {
  85. return filters;
  86. }
  87. BlockFilterOrFactory::BlockFilterOrFactory()
  88. : SubTypeFactory()
  89. {}
  90. BlockFilterOr* BlockFilterOrFactory::fromJson(
  91. Framework::JSON::JSONObject* zJson) const
  92. {
  93. Framework::RCArray<BlockFilter> filters;
  94. for (Framework::JSON::JSONValue* value :
  95. *zJson->zValue("filters")->asArray())
  96. {
  97. filters.add(
  98. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(value));
  99. }
  100. return new BlockFilterOr(filters);
  101. }
  102. Framework::JSON::JSONObject* BlockFilterOrFactory::toJson(
  103. BlockFilterOr* zObject) const
  104. {
  105. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  106. Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
  107. for (BlockFilter* filter : zObject->getFilters())
  108. {
  109. filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
  110. }
  111. result->addValue("filters", filters);
  112. return result;
  113. }
  114. Framework::JSON::Validator::JSONValidator* BlockFilterOrFactory::getValidator(
  115. Framework::JSON::Validator::ObjectValidationBuilder<
  116. Framework::JSON::Validator::JSONValidator>* builder) const
  117. {
  118. return builder->withRequiredArray("filters")
  119. ->addAcceptedTypeInArray(
  120. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  121. ->finishArray()
  122. ->finishObject();
  123. }
  124. Framework::Text BlockFilterOrFactory::getTypeToken() const
  125. {
  126. return "or";
  127. }
  128. BlockFilterNot::BlockFilterNot(BlockFilter* filter)
  129. : BlockFilter(),
  130. filter(filter)
  131. {}
  132. bool BlockFilterNot::test(const Block* zBlock)
  133. {
  134. return !filter->test(zBlock);
  135. }
  136. BlockFilter* BlockFilterNot::zFilter() const
  137. {
  138. return filter;
  139. }
  140. BlockFilterNotFactory::BlockFilterNotFactory()
  141. : SubTypeFactory()
  142. {}
  143. BlockFilterNot* BlockFilterNotFactory::fromJson(
  144. Framework::JSON::JSONObject* zJson) const
  145. {
  146. return new BlockFilterNot(
  147. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  148. zJson->zValue("filter")));
  149. }
  150. Framework::JSON::JSONObject* BlockFilterNotFactory::toJson(
  151. BlockFilterNot* zObject) const
  152. {
  153. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  154. result->addValue(
  155. "filter", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
  156. return result;
  157. }
  158. Framework::JSON::Validator::JSONValidator* BlockFilterNotFactory::getValidator(
  159. Framework::JSON::Validator::ObjectValidationBuilder<
  160. Framework::JSON::Validator::JSONValidator>* builder) const
  161. {
  162. return builder
  163. ->withRequiredAttribute("filter",
  164. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  165. ->finishObject();
  166. }
  167. Framework::Text BlockFilterNotFactory::getTypeToken() const
  168. {
  169. return "not";
  170. }
  171. BlockFilterBlockType::BlockFilterBlockType(Framework::Array<int> blockTypeIds)
  172. : BlockFilter(),
  173. blockTypeIds(blockTypeIds)
  174. {}
  175. bool BlockFilterBlockType::test(const Block* zBlock)
  176. {
  177. for (int blockTypeId : blockTypeIds)
  178. {
  179. if (zBlock->zBlockType()->getId() == blockTypeId)
  180. {
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. const Framework::Array<int>& BlockFilterBlockType::getBlockTypeIds() const
  187. {
  188. return blockTypeIds;
  189. }
  190. BlockFilterBlockTypeFactory::BlockFilterBlockTypeFactory()
  191. : SubTypeFactory()
  192. {}
  193. BlockFilterBlockType* BlockFilterBlockTypeFactory::fromJson(
  194. Framework::JSON::JSONObject* zJson) const
  195. {
  196. Framework::Array<int> blockTypeIds;
  197. for (Framework::JSON::JSONValue* value :
  198. *zJson->zValue("typeNames")->asArray())
  199. {
  200. blockTypeIds.add(
  201. Game::INSTANCE->getBlockTypeId(value->asString()->getString()));
  202. }
  203. return new BlockFilterBlockType(blockTypeIds);
  204. }
  205. Framework::JSON::JSONObject* BlockFilterBlockTypeFactory::toJson(
  206. BlockFilterBlockType* zObject) const
  207. {
  208. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  209. Framework::JSON::JSONArray* typeNames = new Framework::JSON::JSONArray();
  210. for (int typeId : zObject->getBlockTypeIds())
  211. {
  212. typeNames->addValue(new Framework::JSON::JSONString(
  213. Game::INSTANCE->zBlockType(typeId)->getName()));
  214. }
  215. result->addValue("typeNames", typeNames);
  216. return result;
  217. }
  218. Framework::JSON::Validator::JSONValidator*
  219. BlockFilterBlockTypeFactory::getValidator(
  220. Framework::JSON::Validator::ObjectValidationBuilder<
  221. Framework::JSON::Validator::JSONValidator>* builder) const
  222. {
  223. return builder->withRequiredArray("typeNames")
  224. ->addAcceptedStringInArray()
  225. ->finishString()
  226. ->finishArray()
  227. ->finishObject();
  228. }
  229. Framework::Text BlockFilterBlockTypeFactory::getTypeToken() const
  230. {
  231. return "types";
  232. }
  233. BlockFilterTypeGroup::BlockFilterTypeGroup(
  234. Framework::RCArray<Framework::Text> groupNames)
  235. : BlockFilter(),
  236. groupNames(groupNames)
  237. {}
  238. bool BlockFilterTypeGroup::test(const Block* zBlock)
  239. {
  240. for (Framework::Text* groupName : groupNames)
  241. {
  242. for (Framework::Text* otherGroupName :
  243. zBlock->zBlockType()->getGroupNames())
  244. {
  245. if (groupName->istGleich(*otherGroupName))
  246. {
  247. return true;
  248. }
  249. }
  250. }
  251. return false;
  252. }
  253. const Framework::RCArray<Framework::Text>&
  254. BlockFilterTypeGroup::getGroupNames() const
  255. {
  256. return groupNames;
  257. }
  258. BlockFilterTypeGroupFactory::BlockFilterTypeGroupFactory()
  259. : SubTypeFactory()
  260. {}
  261. BlockFilterTypeGroup* BlockFilterTypeGroupFactory::fromJson(
  262. Framework::JSON::JSONObject* zJson) const
  263. {
  264. Framework::RCArray<Framework::Text> groupNames;
  265. for (Framework::JSON::JSONValue* value :
  266. *zJson->zValue("groupNames")->asArray())
  267. {
  268. groupNames.add(new Framework::Text(value->asString()->getString()));
  269. }
  270. return new BlockFilterTypeGroup(groupNames);
  271. }
  272. Framework::JSON::JSONObject* BlockFilterTypeGroupFactory::toJson(
  273. BlockFilterTypeGroup* zObject) const
  274. {
  275. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  276. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  277. for (Framework::Text* groupName : zObject->getGroupNames())
  278. {
  279. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  280. }
  281. result->addValue("groupNames", groupNames);
  282. return result;
  283. }
  284. Framework::JSON::Validator::JSONValidator*
  285. BlockFilterTypeGroupFactory::getValidator(
  286. Framework::JSON::Validator::ObjectValidationBuilder<
  287. Framework::JSON::Validator::JSONValidator>* builder) const
  288. {
  289. return builder->withRequiredArray("groupNames")
  290. ->addAcceptedStringInArray()
  291. ->finishString()
  292. ->finishArray()
  293. ->finishObject();
  294. }
  295. Framework::Text BlockFilterTypeGroupFactory::getTypeToken() const
  296. {
  297. return "groups";
  298. }
  299. BlockFilterMaxHardness::BlockFilterMaxHardness(float maxHardness)
  300. : BlockFilter(),
  301. maxHardness(maxHardness)
  302. {}
  303. bool BlockFilterMaxHardness::test(const Block* zBlock)
  304. {
  305. return zBlock->zBlockType()->getHardness() <= maxHardness;
  306. }
  307. float BlockFilterMaxHardness::getMaxHardness() const
  308. {
  309. return maxHardness;
  310. }
  311. BlockFilterMaxHardnessFactory::BlockFilterMaxHardnessFactory()
  312. : SubTypeFactory()
  313. {}
  314. BlockFilterMaxHardness* BlockFilterMaxHardnessFactory::fromJson(
  315. Framework::JSON::JSONObject* zJson) const
  316. {
  317. return new BlockFilterMaxHardness(
  318. (float)zJson->zValue("maxHardness")->asNumber()->getNumber());
  319. }
  320. Framework::JSON::JSONObject* BlockFilterMaxHardnessFactory::toJson(
  321. BlockFilterMaxHardness* zObject) const
  322. {
  323. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  324. result->addValue("maxHardness",
  325. new Framework::JSON::JSONNumber(zObject->getMaxHardness()));
  326. return result;
  327. }
  328. Framework::JSON::Validator::JSONValidator*
  329. BlockFilterMaxHardnessFactory::getValidator(
  330. Framework::JSON::Validator::ObjectValidationBuilder<
  331. Framework::JSON::Validator::JSONValidator>* builder) const
  332. {
  333. return builder->withRequiredNumber("maxHardness")
  334. ->whichIsGreaterOrEqual(0.0)
  335. ->finishNumber()
  336. ->finishObject();
  337. }
  338. Framework::Text BlockFilterMaxHardnessFactory::getTypeToken() const
  339. {
  340. return "maxHardness";
  341. }
  342. BlockFilterMinHardness::BlockFilterMinHardness(float minHardness)
  343. : BlockFilter(),
  344. minHardness(minHardness)
  345. {}
  346. bool BlockFilterMinHardness::test(const Block* zBlock)
  347. {
  348. return zBlock->zBlockType()->getHardness() >= minHardness;
  349. }
  350. float BlockFilterMinHardness::getMinHardness() const
  351. {
  352. return minHardness;
  353. }
  354. BlockFilterMinHardnessFactory::BlockFilterMinHardnessFactory()
  355. : SubTypeFactory()
  356. {}
  357. BlockFilterMinHardness* BlockFilterMinHardnessFactory::fromJson(
  358. Framework::JSON::JSONObject* zJson) const
  359. {
  360. return new BlockFilterMinHardness(
  361. (float)zJson->zValue("minHardness")->asNumber()->getNumber());
  362. }
  363. Framework::JSON::JSONObject* BlockFilterMinHardnessFactory::toJson(
  364. BlockFilterMinHardness* zObject) const
  365. {
  366. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  367. result->addValue("minHardness",
  368. new Framework::JSON::JSONNumber(zObject->getMinHardness()));
  369. return result;
  370. }
  371. Framework::JSON::Validator::JSONValidator*
  372. BlockFilterMinHardnessFactory::getValidator(
  373. Framework::JSON::Validator::ObjectValidationBuilder<
  374. Framework::JSON::Validator::JSONValidator>* builder) const
  375. {
  376. return builder->withRequiredNumber("minHardness")
  377. ->whichIsGreaterOrEqual(0.0)
  378. ->finishNumber()
  379. ->finishObject();
  380. }
  381. Framework::Text BlockFilterMinHardnessFactory::getTypeToken() const
  382. {
  383. return "minHardness";
  384. }
  385. BlockFilterMaxHeat::BlockFilterMaxHeat(float maxHeat)
  386. : BlockFilter(),
  387. maxHeat(maxHeat)
  388. {}
  389. bool BlockFilterMaxHeat::test(const Block* zBlock)
  390. {
  391. const FluidBlockType* type
  392. = dynamic_cast<const FluidBlockType*>(zBlock->zBlockType());
  393. return type && type->getHeat() <= maxHeat;
  394. }
  395. float BlockFilterMaxHeat::getMaxHeat() const
  396. {
  397. return maxHeat;
  398. }
  399. BlockFilterMaxHeatFactory::BlockFilterMaxHeatFactory()
  400. : SubTypeFactory()
  401. {}
  402. BlockFilterMaxHeat* BlockFilterMaxHeatFactory::fromJson(
  403. Framework::JSON::JSONObject* zJson) const
  404. {
  405. return new BlockFilterMaxHeat(
  406. (float)zJson->zValue("heat")->asNumber()->getNumber());
  407. }
  408. Framework::JSON::JSONObject* BlockFilterMaxHeatFactory::toJson(
  409. BlockFilterMaxHeat* zObject) const
  410. {
  411. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  412. result->addValue(
  413. "heat", new Framework::JSON::JSONNumber(zObject->getMaxHeat()));
  414. return result;
  415. }
  416. Framework::JSON::Validator::JSONValidator*
  417. BlockFilterMaxHeatFactory::getValidator(
  418. Framework::JSON::Validator::ObjectValidationBuilder<
  419. Framework::JSON::Validator::JSONValidator>* builder) const
  420. {
  421. return builder->withRequiredNumber("heat")->finishNumber()->finishObject();
  422. }
  423. Framework::Text BlockFilterMaxHeatFactory::getTypeToken() const
  424. {
  425. return "maxHeat";
  426. }