ItemFilter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #include "ItemFilter.h"
  2. #include "Game.h"
  3. #include "Item.h"
  4. #include "ItemSlot.h"
  5. #include "ItemStack.h"
  6. #include "ItemType.h"
  7. #include "UIMLBuilder.h"
  8. ItemFilter::ItemFilter()
  9. : ReferenceCounter()
  10. {}
  11. bool ItemFilter::matchItem(const Item* zItem) const
  12. {
  13. return 1;
  14. }
  15. bool ItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  16. {
  17. return zSlot->zStack() ? matchItem(zSlot->zStack()->zItem()) : 0;
  18. }
  19. bool ItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  20. {
  21. return 1;
  22. }
  23. CombinedItemFilter::CombinedItemFilter()
  24. : ItemFilter(),
  25. op([](bool a, bool b) { return false; })
  26. {}
  27. CombinedItemFilter::CombinedItemFilter(
  28. Framework::RCArray<ItemFilter> filters, std::function<bool(bool, bool)> op)
  29. : ItemFilter(),
  30. filters(filters),
  31. op(op)
  32. {}
  33. bool CombinedItemFilter::matchItem(const Item* zItem) const
  34. {
  35. bool result = false;
  36. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  37. {
  38. if (i == 0)
  39. result = filters.z(i)->matchItem(zItem);
  40. else
  41. result = op(result, filters.z(i)->matchItem(zItem));
  42. }
  43. return result;
  44. }
  45. bool CombinedItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  46. {
  47. bool result = false;
  48. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  49. {
  50. if (i == 0)
  51. result = filters.z(i)->matchSourceSlot(zSlot);
  52. else
  53. result = op(result, filters.z(i)->matchSourceSlot(zSlot));
  54. }
  55. return result;
  56. }
  57. bool CombinedItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  58. {
  59. bool result = false;
  60. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  61. {
  62. if (i == 0)
  63. result = filters.z(i)->matchTargetSlot(zSlot);
  64. else
  65. result = op(result, filters.z(i)->matchTargetSlot(zSlot));
  66. }
  67. return result;
  68. }
  69. UIMLItemFilterBuilder* CombinedItemFilter::getUIML() const
  70. {
  71. if (filters.getEintragAnzahl() == 0)
  72. {
  73. throw std::runtime_error(
  74. "Cannot build UIML for CombinedItemFilter with no filters");
  75. }
  76. if (filters.getEintragAnzahl() == 1)
  77. {
  78. return filters.z(0)->getUIML();
  79. }
  80. else
  81. {
  82. UIMLItemFilterBuilder* builder = 0;
  83. for (int i = 0; i < filters.getEintragAnzahl(); i++)
  84. {
  85. if (builder == 0)
  86. {
  87. builder = filters.z(i)->getUIML();
  88. }
  89. else
  90. {
  91. builder = UIMLBuilder::createOperatorItemFilter()
  92. ->setOperator(op)
  93. ->setOperands(builder, filters.z(i)->getUIML())
  94. ->build();
  95. }
  96. }
  97. return builder;
  98. }
  99. }
  100. void CombinedItemFilter::addFilter(ItemFilter* filter)
  101. {
  102. filters.add(filter);
  103. }
  104. const Framework::RCArray<ItemFilter>& CombinedItemFilter::getFilters() const
  105. {
  106. return filters;
  107. }
  108. void CombinedItemFilter::setOp(std::function<bool(bool, bool)> op)
  109. {
  110. this->op = op;
  111. }
  112. std::function<bool(bool, bool)> CombinedItemFilter::getOp() const
  113. {
  114. return op;
  115. }
  116. CombinedItemFilterFactory::CombinedItemFilterFactory()
  117. : SubTypeFactory()
  118. {}
  119. CombinedItemFilter* CombinedItemFilterFactory::fromJson(
  120. Framework::JSON::JSONObject* zJson) const
  121. {
  122. CombinedItemFilter* result = new CombinedItemFilter();
  123. std::function<bool(bool, bool)> func;
  124. Framework::Text op = zJson->zValue("operator")->asString()->getString();
  125. if (op.istGleich("or"))
  126. {
  127. func = [](bool a, bool b) { return a || b; };
  128. }
  129. else if (op.istGleich("and"))
  130. {
  131. func = [](bool a, bool b) { return a && b; };
  132. }
  133. else if (op.istGleich("xor"))
  134. {
  135. func = [](bool a, bool b) { return (!a || !b) && (a || b); };
  136. }
  137. else if (op.istGleich("nor"))
  138. {
  139. func = [](bool a, bool b) { return !(a || b); };
  140. }
  141. else if (op.istGleich("nand"))
  142. {
  143. func = [](bool a, bool b) { return !(a && b); };
  144. }
  145. else if (op.istGleich("left"))
  146. {
  147. func = [](bool a, bool b) { return a; };
  148. }
  149. else if (op.istGleich("right"))
  150. {
  151. func = [](bool a, bool b) { return b; };
  152. }
  153. else if (op.istGleich("onlyLeft"))
  154. {
  155. func = [](bool a, bool b) { return a && !b; };
  156. }
  157. else if (op.istGleich("onlyRight"))
  158. {
  159. func = [](bool a, bool b) { return !a && b; };
  160. }
  161. else if (op.istGleich("notLeft"))
  162. {
  163. func = [](bool a, bool b) { return !a; };
  164. }
  165. else if (op.istGleich("notRight"))
  166. {
  167. func = [](bool a, bool b) { return !b; };
  168. }
  169. else if (op.istGleich("eq"))
  170. {
  171. func = [](bool a, bool b) { return a == b; };
  172. }
  173. else if (op.istGleich("leftOrEq"))
  174. {
  175. func = [](bool a, bool b) { return a || (a == b); };
  176. }
  177. else if (op.istGleich("rightOrEq"))
  178. {
  179. func = [](bool a, bool b) { return b || (a == b); };
  180. }
  181. else if (op.istGleich("true"))
  182. {
  183. func = [](bool a, bool b) { return true; };
  184. }
  185. else
  186. {
  187. func = [](bool a, bool b) { return false; };
  188. }
  189. result->setOp(func);
  190. Framework::RCArray<ItemFilter> filters;
  191. for (Framework::JSON::JSONValue* value :
  192. *zJson->zValue("filters")->asArray())
  193. {
  194. result->addFilter(
  195. Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(value));
  196. }
  197. return result;
  198. }
  199. Framework::JSON::JSONObject* CombinedItemFilterFactory::toJsonObject(
  200. CombinedItemFilter* zObject) const
  201. {
  202. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  203. Framework::Text op;
  204. std::function<bool(bool, bool)> func = zObject->getOp();
  205. if (func(0, 0))
  206. {
  207. if (func(0, 1))
  208. {
  209. if (func(1, 0))
  210. {
  211. if (func(1, 1))
  212. {
  213. op = "true";
  214. }
  215. else
  216. {
  217. op = "nand";
  218. }
  219. }
  220. else
  221. {
  222. if (func(1, 1))
  223. {
  224. op = "rightOrEq";
  225. }
  226. else
  227. {
  228. op = "notLeft";
  229. }
  230. }
  231. }
  232. else
  233. {
  234. if (func(1, 0))
  235. {
  236. if (func(1, 1))
  237. {
  238. op = "leftOrEq";
  239. }
  240. else
  241. {
  242. op = "notRight";
  243. }
  244. }
  245. else
  246. {
  247. if (func(1, 1))
  248. {
  249. op = "eq";
  250. }
  251. else
  252. {
  253. op = "nor";
  254. }
  255. }
  256. }
  257. }
  258. else
  259. {
  260. if (func(0, 1))
  261. {
  262. if (func(1, 0))
  263. {
  264. if (func(1, 1))
  265. {
  266. op = "or";
  267. }
  268. else
  269. {
  270. op = "xor";
  271. }
  272. }
  273. else
  274. {
  275. if (func(1, 1))
  276. {
  277. op = "right";
  278. }
  279. else
  280. {
  281. op = "onlyRight";
  282. }
  283. }
  284. }
  285. else
  286. {
  287. if (func(1, 0))
  288. {
  289. if (func(1, 1))
  290. {
  291. op = "left";
  292. }
  293. else
  294. {
  295. op = "onlyLeft";
  296. }
  297. }
  298. else
  299. {
  300. if (func(1, 1))
  301. {
  302. op = "and";
  303. }
  304. else
  305. {
  306. op = "false";
  307. }
  308. }
  309. }
  310. }
  311. result->addValue("operator", new Framework::JSON::JSONString(op));
  312. Framework::JSON::JSONArray* filters = new Framework::JSON::JSONArray();
  313. for (ItemFilter* filter : zObject->getFilters())
  314. {
  315. filters->addValue(Game::INSTANCE->zTypeRegistry()->toJson(filter));
  316. }
  317. result->addValue("filters", filters);
  318. return result;
  319. }
  320. JSONObjectValidationBuilder* CombinedItemFilterFactory::addToValidator(
  321. JSONObjectValidationBuilder* builder) const
  322. {
  323. return builder->withRequiredString("operator")
  324. ->whichIsOneOf({"or",
  325. "and",
  326. "xor",
  327. "nor",
  328. "nand",
  329. "left",
  330. "right",
  331. "onlyLeft",
  332. "onlyRight",
  333. "notLeft",
  334. "notRight",
  335. "eq",
  336. "leftOrEq",
  337. "rightOrEq",
  338. "true",
  339. "false"})
  340. ->finishString()
  341. ->withRequiredArray("filters")
  342. ->addAcceptedTypeInArray(
  343. Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>())
  344. ->finishArray();
  345. }
  346. const char* CombinedItemFilterFactory::getTypeToken() const
  347. {
  348. return "operator";
  349. }
  350. AnyItemFilter::AnyItemFilter()
  351. : ItemFilter()
  352. {}
  353. bool AnyItemFilter::matchItem(const Item* zItem) const
  354. {
  355. return true;
  356. }
  357. UIMLItemFilterBuilder* AnyItemFilter::getUIML() const
  358. {
  359. return UIMLBuilder::createAnyItemFilter()->build();
  360. }
  361. AnyItemFilterFactory::AnyItemFilterFactory()
  362. : SubTypeFactory()
  363. {}
  364. AnyItemFilter* AnyItemFilterFactory::fromJson(
  365. Framework::JSON::JSONObject* zJson) const
  366. {
  367. return new AnyItemFilter();
  368. }
  369. Framework::JSON::JSONObject* AnyItemFilterFactory::toJsonObject(
  370. AnyItemFilter* zObject) const
  371. {
  372. return new Framework::JSON::JSONObject();
  373. }
  374. JSONObjectValidationBuilder* AnyItemFilterFactory::addToValidator(
  375. JSONObjectValidationBuilder* builder) const
  376. {
  377. return builder;
  378. }
  379. const char* AnyItemFilterFactory::getTypeToken() const
  380. {
  381. return "anyItem";
  382. }
  383. TypeItemFilter::TypeItemFilter()
  384. : ItemFilter(),
  385. type(0)
  386. {}
  387. bool TypeItemFilter::matchItem(const Item* zItem) const
  388. {
  389. return zItem->zItemType() == type;
  390. }
  391. UIMLItemFilterBuilder* TypeItemFilter::getUIML() const
  392. {
  393. return UIMLBuilder::createItemAttributeFilter()
  394. ->requireAttributeEquals("Type", Framework::Text() += type->getId())
  395. ->build();
  396. }
  397. void TypeItemFilter::setType(const ItemType* type)
  398. {
  399. this->type = type;
  400. }
  401. const ItemType* TypeItemFilter::zType() const
  402. {
  403. return type;
  404. }
  405. TypeItemFilterFactory::TypeItemFilterFactory()
  406. : SubTypeFactory()
  407. {}
  408. TypeItemFilter* TypeItemFilterFactory::fromJson(
  409. Framework::JSON::JSONObject* zJson) const
  410. {
  411. TypeItemFilter* result = new TypeItemFilter();
  412. result->setType(Game::INSTANCE->zItemType(Game::INSTANCE->getItemTypeId(
  413. zJson->zValue("itemType")->asString()->getString())));
  414. return result;
  415. }
  416. Framework::JSON::JSONObject* TypeItemFilterFactory::toJsonObject(
  417. TypeItemFilter* zObject) const
  418. {
  419. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  420. result->addValue("itemType",
  421. new Framework::JSON::JSONString(zObject->zType()->getName()));
  422. return result;
  423. }
  424. JSONObjectValidationBuilder* TypeItemFilterFactory::addToValidator(
  425. JSONObjectValidationBuilder* builder) const
  426. {
  427. return builder->withRequiredAttribute("itemType",
  428. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  429. ItemTypeNameFactory::TYPE_ID));
  430. }
  431. const char* TypeItemFilterFactory::getTypeToken() const
  432. {
  433. return "type";
  434. }
  435. SpecificSlotFilter::SpecificSlotFilter(int sourceSlotId, int targetSlotId)
  436. : ItemFilter(),
  437. sourceSlotId(sourceSlotId),
  438. targetSlotId(targetSlotId)
  439. {}
  440. bool SpecificSlotFilter::matchSourceSlot(ItemSlot* zSlot) const
  441. {
  442. return sourceSlotId == zSlot->getId();
  443. }
  444. bool SpecificSlotFilter::matchTargetSlot(ItemSlot* zSlot) const
  445. {
  446. return targetSlotId == zSlot->getId();
  447. }
  448. UIMLItemFilterBuilder* SpecificSlotFilter::getUIML() const
  449. {
  450. return UIMLBuilder::createAnyItemFilter()->build();
  451. }
  452. SourceSlotBlacklistFilter::SourceSlotBlacklistFilter()
  453. : ItemFilter()
  454. {}
  455. void SourceSlotBlacklistFilter::addBlackListSlotId(int id)
  456. {
  457. blackList.add(id);
  458. }
  459. bool SourceSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  460. {
  461. for (int black : blackList)
  462. {
  463. if (black == zSlot->getId()) return 0;
  464. }
  465. return 1;
  466. }
  467. bool SourceSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  468. {
  469. return 1;
  470. }
  471. UIMLItemFilterBuilder* SourceSlotBlacklistFilter::getUIML() const
  472. {
  473. return UIMLBuilder::createAnyItemFilter()->build();
  474. }
  475. TargetSlotBlacklistFilter::TargetSlotBlacklistFilter()
  476. : ItemFilter()
  477. {}
  478. void TargetSlotBlacklistFilter::addBlackListSlotId(int id)
  479. {
  480. blackList.add(id);
  481. }
  482. bool TargetSlotBlacklistFilter::matchSourceSlot(ItemSlot* zSlot) const
  483. {
  484. return 1;
  485. }
  486. bool TargetSlotBlacklistFilter::matchTargetSlot(ItemSlot* zSlot) const
  487. {
  488. for (int black : blackList)
  489. {
  490. if (black == zSlot->getId()) return 0;
  491. }
  492. return 1;
  493. }
  494. UIMLItemFilterBuilder* TargetSlotBlacklistFilter::getUIML() const
  495. {
  496. return UIMLBuilder::createAnyItemFilter()->build();
  497. }
  498. GroupItemFilter::GroupItemFilter()
  499. : ItemFilter()
  500. {}
  501. bool GroupItemFilter::matchItem(const Item* zItem) const
  502. {
  503. for (Framework::Text* group : groups)
  504. {
  505. for (Framework::Text* typeGroup : zItem->zItemType()->getGroups())
  506. {
  507. if (typeGroup->istGleich(*group)) return true;
  508. }
  509. }
  510. return false;
  511. }
  512. UIMLItemFilterBuilder* GroupItemFilter::getUIML() const
  513. {
  514. CombinedItemFilter filter;
  515. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  516. {
  517. if (Game::INSTANCE->zItemType(i))
  518. {
  519. bool found = false;
  520. for (Framework::Text* group : groups)
  521. {
  522. for (Framework::Text* typeGroup :
  523. Game::INSTANCE->zItemType(i)->getGroups())
  524. {
  525. if (typeGroup->istGleich(*group))
  526. {
  527. found = true;
  528. break;
  529. }
  530. }
  531. if (found) break;
  532. }
  533. if (found)
  534. {
  535. TypeItemFilter* f = new TypeItemFilter();
  536. f->setType(Game::INSTANCE->zItemType(i));
  537. filter.addFilter(f);
  538. }
  539. }
  540. }
  541. filter.setOp([](bool a, bool b) { return a || b; });
  542. return filter.getUIML();
  543. }
  544. void GroupItemFilter::addGroup(const Framework::Text group)
  545. {
  546. groups.add(new Framework::Text(group));
  547. }
  548. const Framework::RCArray<Framework::Text>& GroupItemFilter::getGroups() const
  549. {
  550. return groups;
  551. }
  552. GroupItemFilterFactory::GroupItemFilterFactory()
  553. : SubTypeFactory()
  554. {}
  555. GroupItemFilter* GroupItemFilterFactory::fromJson(
  556. Framework::JSON::JSONObject* zJson) const
  557. {
  558. GroupItemFilter* result = new GroupItemFilter();
  559. for (Framework::JSON::JSONValue* group :
  560. *zJson->zValue("groupNames")->asArray())
  561. {
  562. result->addGroup(group->asString()->getString());
  563. }
  564. return result;
  565. }
  566. Framework::JSON::JSONObject* GroupItemFilterFactory::toJsonObject(
  567. GroupItemFilter* zObject) const
  568. {
  569. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  570. Framework::JSON::JSONArray* groups = new Framework::JSON::JSONArray();
  571. for (Framework::Text* group : zObject->getGroups())
  572. {
  573. groups->addValue(new Framework::JSON::JSONString(group->getText()));
  574. }
  575. result->addValue("groupNames", groups);
  576. return result;
  577. }
  578. JSONObjectValidationBuilder* GroupItemFilterFactory::addToValidator(
  579. JSONObjectValidationBuilder* builder) const
  580. {
  581. return builder->withRequiredArray("groupNames")
  582. ->addAcceptedStringInArray()
  583. ->finishString()
  584. ->finishArray();
  585. }
  586. const char* GroupItemFilterFactory::getTypeToken() const
  587. {
  588. return "groups";
  589. }
  590. TargetSlotNameItemFilter::TargetSlotNameItemFilter(
  591. const Framework::Text& slotName)
  592. : ItemFilter(),
  593. slotName(slotName)
  594. {}
  595. bool TargetSlotNameItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  596. {
  597. return 1;
  598. }
  599. bool TargetSlotNameItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  600. {
  601. return zSlot->getName().istGleich(slotName);
  602. }
  603. UIMLItemFilterBuilder* TargetSlotNameItemFilter::getUIML() const
  604. {
  605. return UIMLBuilder::createAnyItemFilter()->build();
  606. }
  607. SourceSlotNameItemFilter::SourceSlotNameItemFilter(
  608. const Framework::Text& slotName)
  609. : ItemFilter(),
  610. slotName(slotName)
  611. {}
  612. bool SourceSlotNameItemFilter::matchSourceSlot(ItemSlot* zSlot) const
  613. {
  614. return zSlot->getName().istGleich(slotName);
  615. }
  616. bool SourceSlotNameItemFilter::matchTargetSlot(ItemSlot* zSlot) const
  617. {
  618. return 1;
  619. }
  620. UIMLItemFilterBuilder* SourceSlotNameItemFilter::getUIML() const
  621. {
  622. return UIMLBuilder::createAnyItemFilter()->build();
  623. }