BlockFilter.cpp 13 KB

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