BlockFilter.cpp 13 KB

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