RecipieIngredient.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. #include "RecipieIngredient.h"
  2. #include <DateiSystem.h>
  3. #include <ToolTip.h>
  4. #include <XML.h>
  5. #include "Globals.h"
  6. RecipieIngredientElement::RecipieIngredientElement()
  7. : Framework::UIMLElement()
  8. {}
  9. LogicTree* RecipieIngredientElement::parse(Framework::XML::Element* zElement)
  10. {
  11. if (zElement->getName().istGleich("anyItem"))
  12. {
  13. return new LogicTree(LogicalOperator::OR, new RequirementSet());
  14. }
  15. else if (zElement->getName().istGleich("attribute"))
  16. {
  17. AttributeOperator op = AttributeOperator::Equals;
  18. if (zElement->getAttributeValue("operator").istGleich("!="))
  19. {
  20. op = AttributeOperator::NotEquals;
  21. }
  22. else if (zElement->getAttributeValue("operator").istGleich(">"))
  23. {
  24. op = AttributeOperator::GreaterThan;
  25. }
  26. else if (zElement->getAttributeValue("operator").istGleich(">="))
  27. {
  28. op = AttributeOperator::GreaterThanOrEquals;
  29. }
  30. else if (zElement->getAttributeValue("operator").istGleich("<"))
  31. {
  32. op = AttributeOperator::LessThan;
  33. }
  34. else if (zElement->getAttributeValue("operator").istGleich("<="))
  35. {
  36. op = AttributeOperator::LessThanOrEquals;
  37. }
  38. Requirement* req = new Requirement(zElement->getAttributeValue("name"),
  39. zElement->getAttributeValue("value"),
  40. op);
  41. RequirementSet* set = new RequirementSet();
  42. set->addRequirement(req);
  43. req->release();
  44. return new LogicTree(LogicalOperator::OR, set);
  45. }
  46. else if (zElement->getName().istGleich("operator"))
  47. {
  48. bool result0_0 = (bool)(int)zElement->getAttributeValue("result_0_0");
  49. bool result0_1 = (bool)(int)zElement->getAttributeValue("result_0_1");
  50. bool result1_0 = (bool)(int)zElement->getAttributeValue("result_1_0");
  51. bool result1_1 = (bool)(int)zElement->getAttributeValue("result_1_1");
  52. if (!result0_0 && !result0_1 && !result1_0 && !result1_1)
  53. { // none match
  54. RequirementSet* set = new RequirementSet();
  55. Requirement* req
  56. = new Requirement("_x", "0", AttributeOperator::Equals);
  57. set->addRequirement(req);
  58. req->release();
  59. req = new Requirement("_x", "1", AttributeOperator::Equals);
  60. set->addRequirement(req);
  61. req->release();
  62. return new LogicTree(LogicalOperator::OR, set);
  63. }
  64. if (result0_0 && result0_1 && result1_0 && result1_1)
  65. { // any match
  66. return new LogicTree(LogicalOperator::OR, new RequirementSet());
  67. }
  68. auto iterator = zElement->getChilds();
  69. LogicTree* left = parse(iterator.val());
  70. if (!result0_0 && !result0_1 && result1_0 && result1_1)
  71. {
  72. return left;
  73. }
  74. if (result0_0 && result0_1 && !result1_0 && !result1_1)
  75. {
  76. left->negate();
  77. return left;
  78. }
  79. LogicTree* right = 0;
  80. iterator++;
  81. right = parse(iterator.val());
  82. if (!result0_0 && result0_1 && !result1_0 && result1_1)
  83. {
  84. left->release();
  85. return right;
  86. }
  87. if (result0_0 && !result0_1 && result1_0 && !result1_1)
  88. {
  89. left->release();
  90. right->negate();
  91. return right;
  92. }
  93. if (!result0_0 && !result0_1 && !result1_0 && result1_1)
  94. {
  95. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  96. result->addChildren(left);
  97. result->addChildren(right);
  98. return result;
  99. }
  100. if (!result0_0 && result0_1 && result1_0 && result1_1)
  101. {
  102. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  103. result->addChildren(left);
  104. result->addChildren(right);
  105. return result;
  106. }
  107. if (result0_0 && result0_1 && result1_0 && !result1_1)
  108. {
  109. left->negate();
  110. right->negate();
  111. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  112. result->addChildren(left);
  113. result->addChildren(right);
  114. return result;
  115. }
  116. if (result0_0 && !result0_1 && !result1_0 && !result1_1)
  117. {
  118. left->negate();
  119. right->negate();
  120. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  121. result->addChildren(left);
  122. result->addChildren(right);
  123. return result;
  124. }
  125. if (!result0_0 && !result0_1 && result1_0 && !result1_1)
  126. {
  127. right->negate();
  128. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  129. result->addChildren(left);
  130. result->addChildren(right);
  131. return result;
  132. }
  133. if (!result0_0 && result0_1 && !result1_0 && !result1_1)
  134. {
  135. left->negate();
  136. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  137. result->addChildren(left);
  138. result->addChildren(right);
  139. return result;
  140. }
  141. if (!result0_0 && result0_1 && result1_0 && !result1_1)
  142. {
  143. LogicTree* orT = new LogicTree(LogicalOperator::OR, 0);
  144. orT->addChildren(left);
  145. orT->addChildren(right);
  146. LogicTree* notLeft = left->clone();
  147. notLeft->negate();
  148. LogicTree* notRight = right->clone();
  149. notRight->negate();
  150. LogicTree* notOr = new LogicTree(LogicalOperator::OR, 0);
  151. notOr->addChildren(notLeft);
  152. notOr->addChildren(notRight);
  153. LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
  154. result->addChildren(orT);
  155. result->addChildren(notOr);
  156. return result;
  157. }
  158. if (result0_0 && !result0_1 && !result1_0 && result1_1)
  159. {
  160. LogicTree* andT = new LogicTree(LogicalOperator::AND, 0);
  161. andT->addChildren(left);
  162. andT->addChildren(right);
  163. LogicTree* notLeft = left->clone();
  164. notLeft->negate();
  165. LogicTree* notRight = right->clone();
  166. notRight->negate();
  167. LogicTree* notAnd = new LogicTree(LogicalOperator::AND, 0);
  168. notAnd->addChildren(notLeft);
  169. notAnd->addChildren(notRight);
  170. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  171. result->addChildren(andT);
  172. result->addChildren(notAnd);
  173. return result;
  174. }
  175. if (result0_0 && !result0_1 && result1_0 && result1_1)
  176. {
  177. right->negate();
  178. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  179. result->addChildren(left);
  180. result->addChildren(right);
  181. return result;
  182. }
  183. if (result0_0 && result0_1 && !result1_0 && result1_1)
  184. {
  185. left->negate();
  186. LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
  187. result->addChildren(left);
  188. result->addChildren(right);
  189. return result;
  190. }
  191. }
  192. return 0;
  193. }
  194. //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig
  195. //! ist
  196. bool RecipieIngredientElement::isApplicableFor(Framework::XML::Element& element)
  197. {
  198. return element.getName().istGleich("ingredient");
  199. }
  200. //! erstellt eine neue Zeichnung zu einem gegebenen xml Element
  201. Framework::Zeichnung* RecipieIngredientElement::parseElement(
  202. Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
  203. {
  204. int amount = (int)element.getAttributeValue("amount");
  205. RecipieIngredient* result = new RecipieIngredient(amount);
  206. Framework::XML::Editor logicSelector = element.selectChildsByName("logic");
  207. if (logicSelector.exists())
  208. {
  209. LogicTree* logic = new LogicTree(LogicalOperator::OR, 0);
  210. logicSelector.selectChildren().forEach(
  211. [this, logic](Framework::XML::Element* zElement) {
  212. logic->addChildren(parse(zElement));
  213. });
  214. Framework::RCArray<RequirementSet>* requirements = logic->resolve();
  215. logic->release();
  216. for (RequirementSet* set : *requirements)
  217. {
  218. result->addPossibleItem(set->getIcon(),
  219. new Text(set->renderToTooltip()),
  220. set->getItemType());
  221. }
  222. requirements->release();
  223. }
  224. return result;
  225. }
  226. bool RecipieIngredientElement::updateElement(Framework::XML::Element& element,
  227. Framework::Zeichnung& z,
  228. Framework::UIMLContainer& generalFactory)
  229. {
  230. return false;
  231. }
  232. //! wendet die layout parameter zu einer Zeichnung an
  233. void RecipieIngredientElement::layout(Framework::XML::Element& element,
  234. Framework::Zeichnung& z,
  235. int pWidth,
  236. int pHeight,
  237. Framework::UIMLContainer& generalLayouter)
  238. {
  239. UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
  240. z.setWidth(50);
  241. z.setHeight(50);
  242. }
  243. RecipieIngredient::RecipieIngredient(int amount)
  244. : Framework::ZeichnungHintergrund(),
  245. currentIconIndex(0),
  246. timtUntilNextIcon(2.0),
  247. amount(amount)
  248. {
  249. setStyle(Framework::Zeichnung::Style::Erlaubt
  250. | Framework::Zeichnung::Style::Sichtbar);
  251. Framework::ToolTip* tip = new Framework::ToolTip(window->zBildschirm());
  252. tip->addStyle(Framework::ZeichnungHintergrund::Style::Hintergrund
  253. | Framework::ZeichnungHintergrund::Style::HAlpha
  254. | Framework::ZeichnungHintergrund::Style::Rahmen
  255. | Framework::ZeichnungHintergrund::Style::Sichtbar);
  256. tip->setHintergrundFarbe(0xA0000000);
  257. tip->setRahmenFarbe(0xFFFFFFFF);
  258. tip->setRahmenBreite(1);
  259. toolTip = uiFactory.createTextFeld(uiFactory.initParam);
  260. toolTip->setText("");
  261. toolTip->setSize(0, 0);
  262. toolTip->addStyle(Framework::TextFeld::Style::Mehrzeilig);
  263. tip->addMember(toolTip);
  264. tip->setWarten(0.5);
  265. setToolTipZ(tip);
  266. }
  267. void RecipieIngredient::addPossibleItem(
  268. Framework::Bild* icon, Framework::Text* toolTip, int typeId)
  269. {
  270. icons.add(icon);
  271. toolTips.add(toolTip);
  272. itemTypes.add(typeId);
  273. if (toolTips.getEintragAnzahl() == 1)
  274. {
  275. this->toolTip->setText(toolTips.z(0)->getText());
  276. this->toolTip->setSize(
  277. this->toolTip->getNeededWidth(), this->toolTip->getNeededHeight());
  278. }
  279. }
  280. bool RecipieIngredient::tick(double tickVal)
  281. {
  282. if (!zToolTip()->isVisible())
  283. {
  284. timtUntilNextIcon -= tickVal;
  285. if (timtUntilNextIcon <= 0)
  286. {
  287. timtUntilNextIcon = 2.0;
  288. currentIconIndex++;
  289. if (currentIconIndex >= icons.getEintragAnzahl())
  290. {
  291. currentIconIndex = 0;
  292. }
  293. if (toolTips.getEintragAnzahl() > 0)
  294. {
  295. toolTip->setText(toolTips.z(currentIconIndex)->getText());
  296. toolTip->setSize(
  297. toolTip->getNeededWidth(), toolTip->getNeededHeight());
  298. rend = 1;
  299. }
  300. }
  301. }
  302. return ZeichnungHintergrund::tick(tickVal);
  303. }
  304. void RecipieIngredient::render(Framework::Bild& rObj)
  305. {
  306. ZeichnungHintergrund::render(rObj);
  307. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  308. TextRenderer tr;
  309. tr.setSchriftZ(
  310. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  311. tr.setSchriftSize(12);
  312. rObj.fillRegion(0, 0, 50, 50, 0xFF222222);
  313. if (icons.getEintragAnzahl() > 0)
  314. rObj.alphaBild(0, 0, 50, 50, *icons.z(currentIconIndex));
  315. const char* units[] = {"", "K", "M", "G", "T", "P"};
  316. int i = 0;
  317. int tmpCount = amount;
  318. for (; i < 6 && tmpCount > 1024; i++)
  319. tmpCount = tmpCount / 1024;
  320. Text count = tmpCount;
  321. count += units[i];
  322. tr.renderText(45 - tr.getTextBreite(count),
  323. 45 - tr.getTextHeight(count),
  324. count,
  325. rObj,
  326. 0xFFFFFFFF);
  327. rObj.releaseDrawOptions();
  328. }
  329. void RecipieIngredient::doMausEreignis(
  330. Framework::MausEreignis& me, bool userRet)
  331. {
  332. if (me.id == ME_RLinks)
  333. {
  334. if (itemTypes.getEintragAnzahl() > currentIconIndex)
  335. {
  336. World::INSTANCE->zClient()->craftingUIMLRequest(
  337. itemTypes.get(currentIconIndex));
  338. }
  339. }
  340. ZeichnungHintergrund::doMausEreignis(me, userRet);
  341. }
  342. Requirement::Requirement(
  343. Framework::Text attribute, Framework::Text value, AttributeOperator op)
  344. : Framework::ReferenceCounter(),
  345. attribute(attribute),
  346. value(value),
  347. op(op)
  348. {}
  349. void Requirement::negate()
  350. {
  351. switch (op)
  352. {
  353. case AttributeOperator::Equals:
  354. op = AttributeOperator::NotEquals;
  355. break;
  356. case AttributeOperator::NotEquals:
  357. op = AttributeOperator::Equals;
  358. break;
  359. case AttributeOperator::GreaterThan:
  360. op = AttributeOperator::LessThanOrEquals;
  361. break;
  362. case AttributeOperator::GreaterThanOrEquals:
  363. op = AttributeOperator::LessThan;
  364. break;
  365. case AttributeOperator::LessThan:
  366. op = AttributeOperator::GreaterThanOrEquals;
  367. break;
  368. case AttributeOperator::LessThanOrEquals:
  369. op = AttributeOperator::GreaterThan;
  370. break;
  371. }
  372. }
  373. Requirement* Requirement::clone()
  374. {
  375. return new Requirement(attribute, value, op);
  376. }
  377. bool Requirement::contradicts(Requirement* zOther)
  378. {
  379. if (zOther->attribute.istGleich(attribute.getText()))
  380. {
  381. if (zOther->value.istGleich(value))
  382. {
  383. if (op == AttributeOperator::Equals
  384. && zOther->op == AttributeOperator::NotEquals)
  385. {
  386. return 1;
  387. }
  388. if (op == AttributeOperator::NotEquals
  389. && zOther->op == AttributeOperator::Equals)
  390. {
  391. return 1;
  392. }
  393. if (op == AttributeOperator::GreaterThan
  394. && zOther->op == AttributeOperator::LessThan)
  395. {
  396. return 1;
  397. }
  398. if (op == AttributeOperator::LessThan
  399. && zOther->op == AttributeOperator::GreaterThan)
  400. {
  401. return 1;
  402. }
  403. }
  404. if (op == AttributeOperator::LessThan
  405. || op == AttributeOperator::LessThanOrEquals
  406. || op == AttributeOperator::GreaterThan
  407. || op == AttributeOperator::GreaterThanOrEquals)
  408. {
  409. double v1 = (double)value;
  410. double v2 = (double)zOther->value;
  411. if (op == AttributeOperator::LessThan
  412. || op == AttributeOperator::LessThanOrEquals)
  413. {
  414. if (v1 < v2)
  415. {
  416. if (zOther->op == AttributeOperator::GreaterThan
  417. || zOther->op == AttributeOperator::GreaterThanOrEquals
  418. || zOther->op == AttributeOperator::Equals)
  419. {
  420. return 1;
  421. }
  422. }
  423. }
  424. else if (op == AttributeOperator::GreaterThan
  425. || op == AttributeOperator::GreaterThanOrEquals)
  426. {
  427. if (v1 > v2)
  428. {
  429. if (zOther->op == AttributeOperator::LessThan
  430. || zOther->op == AttributeOperator::LessThanOrEquals
  431. || zOther->op == AttributeOperator::Equals)
  432. {
  433. return 1;
  434. }
  435. }
  436. }
  437. }
  438. if (zOther->op == AttributeOperator::LessThan
  439. || zOther->op == AttributeOperator::LessThanOrEquals
  440. || zOther->op == AttributeOperator::GreaterThan
  441. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  442. {
  443. double v1 = (double)zOther->value;
  444. double v2 = (double)value;
  445. if (zOther->op == AttributeOperator::LessThan
  446. || zOther->op == AttributeOperator::LessThanOrEquals)
  447. {
  448. if (v1 < v2)
  449. {
  450. if (op == AttributeOperator::GreaterThan
  451. || op == AttributeOperator::GreaterThanOrEquals
  452. || op == AttributeOperator::Equals)
  453. {
  454. return 1;
  455. }
  456. }
  457. }
  458. else if (zOther->op == AttributeOperator::GreaterThan
  459. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  460. {
  461. if (v1 > v2)
  462. {
  463. if (op == AttributeOperator::LessThan
  464. || op == AttributeOperator::LessThanOrEquals
  465. || op == AttributeOperator::Equals)
  466. {
  467. return 1;
  468. }
  469. }
  470. }
  471. }
  472. }
  473. return 0;
  474. }
  475. bool Requirement::merge(Requirement* zOther)
  476. {
  477. if (zOther->attribute.istGleich(attribute.getText()))
  478. {
  479. if (zOther->value.istGleich(value))
  480. {
  481. if (op == zOther->op)
  482. {
  483. return 1;
  484. }
  485. }
  486. else
  487. {
  488. if (op == AttributeOperator::Equals
  489. && zOther->op == AttributeOperator::NotEquals)
  490. {
  491. return 1;
  492. }
  493. if (op == AttributeOperator::NotEquals
  494. && zOther->op == AttributeOperator::Equals)
  495. {
  496. op = AttributeOperator::Equals;
  497. value = zOther->value;
  498. return 1;
  499. }
  500. }
  501. if (op == AttributeOperator::LessThan
  502. || op == AttributeOperator::LessThanOrEquals
  503. || op == AttributeOperator::GreaterThan
  504. || op == AttributeOperator::GreaterThanOrEquals)
  505. {
  506. double v1 = (double)value;
  507. double v2 = (double)zOther->value;
  508. if (op == AttributeOperator::LessThan
  509. || op == AttributeOperator::LessThanOrEquals)
  510. {
  511. if (zOther->op == AttributeOperator::LessThan
  512. || zOther->op == AttributeOperator::LessThanOrEquals)
  513. {
  514. if (v1 > v2
  515. || (v1 == v2
  516. && op == AttributeOperator::LessThanOrEquals))
  517. {
  518. op = zOther->op;
  519. value = zOther->value;
  520. }
  521. return 1;
  522. }
  523. if (zOther->op == AttributeOperator::Equals)
  524. {
  525. if (v2 < v1
  526. || (v2 == v1
  527. && op == AttributeOperator::LessThanOrEquals))
  528. {
  529. op = zOther->op;
  530. value = zOther->value;
  531. return 1;
  532. }
  533. }
  534. if (zOther->op == AttributeOperator::NotEquals)
  535. {
  536. if (v2 > v1
  537. || (v2 == v1 && op == AttributeOperator::LessThan))
  538. {
  539. return 1;
  540. }
  541. }
  542. }
  543. else if (op == AttributeOperator::GreaterThan
  544. || op == AttributeOperator::GreaterThanOrEquals)
  545. {
  546. if (zOther->op == AttributeOperator::GreaterThan
  547. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  548. {
  549. if (v1 < v2
  550. || (v1 == v2
  551. && op == AttributeOperator::GreaterThanOrEquals))
  552. {
  553. op = zOther->op;
  554. value = zOther->value;
  555. }
  556. return 1;
  557. }
  558. if (zOther->op == AttributeOperator::Equals)
  559. {
  560. if (v2 > v1
  561. || (v2 == v1
  562. && op == AttributeOperator::GreaterThanOrEquals))
  563. {
  564. op = zOther->op;
  565. value = zOther->value;
  566. return 1;
  567. }
  568. }
  569. if (zOther->op == AttributeOperator::NotEquals)
  570. {
  571. if (v2 < v1
  572. || (v2 == v1 && op == AttributeOperator::GreaterThan))
  573. {
  574. return 1;
  575. }
  576. }
  577. }
  578. }
  579. if (zOther->op == AttributeOperator::LessThan
  580. || zOther->op == AttributeOperator::LessThanOrEquals
  581. || zOther->op == AttributeOperator::GreaterThan
  582. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  583. {
  584. double v1 = (double)zOther->value;
  585. double v2 = (double)value;
  586. if (zOther->op == AttributeOperator::LessThan
  587. || zOther->op == AttributeOperator::LessThanOrEquals)
  588. {
  589. if (op == AttributeOperator::Equals)
  590. {
  591. if (v2 < v1
  592. || (v2 == v1
  593. && zOther->op
  594. == AttributeOperator::LessThanOrEquals))
  595. {
  596. return 1;
  597. }
  598. }
  599. if (op == AttributeOperator::NotEquals)
  600. {
  601. if (v2 > v1
  602. || (v2 == v1
  603. && zOther->op == AttributeOperator::LessThan))
  604. {
  605. op = zOther->op;
  606. value = zOther->value;
  607. return 1;
  608. }
  609. }
  610. }
  611. else if (zOther->op == AttributeOperator::GreaterThan
  612. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  613. {
  614. if (op == AttributeOperator::Equals)
  615. {
  616. if (v2 > v1
  617. || (v2 == v1
  618. && zOther->op
  619. == AttributeOperator::GreaterThanOrEquals))
  620. {
  621. return 1;
  622. }
  623. }
  624. if (op == AttributeOperator::NotEquals)
  625. {
  626. if (v2 < v1
  627. || (v2 == v1
  628. && zOther->op == AttributeOperator::GreaterThan))
  629. {
  630. op = zOther->op;
  631. value = zOther->value;
  632. return 1;
  633. }
  634. }
  635. }
  636. }
  637. }
  638. return 0;
  639. }
  640. int Requirement::getItemType()
  641. {
  642. if (attribute.istGleich("Type") && op == AttributeOperator::Equals)
  643. {
  644. return (int)value;
  645. }
  646. return -1;
  647. }
  648. Framework::Text Requirement::getDescription()
  649. {
  650. Framework::Text result = attribute;
  651. result += " ";
  652. switch (op)
  653. {
  654. case AttributeOperator::Equals:
  655. result += "is";
  656. break;
  657. case AttributeOperator::NotEquals:
  658. result += "is not";
  659. break;
  660. case AttributeOperator::LessThan:
  661. result += "is less than";
  662. break;
  663. case AttributeOperator::LessThanOrEquals:
  664. result += "is less than or equal to";
  665. break;
  666. case AttributeOperator::GreaterThan:
  667. result += "is greater than";
  668. break;
  669. case AttributeOperator::GreaterThanOrEquals:
  670. result += "is greater than or equal to";
  671. break;
  672. }
  673. result += " ";
  674. result += value;
  675. return result;
  676. }
  677. RequirementSet::RequirementSet()
  678. : Framework::ReferenceCounter()
  679. {}
  680. void RequirementSet::addRequirement(Requirement* zReq)
  681. {
  682. for (Requirement* req : requirements)
  683. {
  684. if (req->merge(zReq)) return;
  685. }
  686. requirements.add(zReq->clone());
  687. }
  688. RequirementSet* RequirementSet::clone()
  689. {
  690. RequirementSet* result = new RequirementSet();
  691. for (Requirement* req : requirements)
  692. {
  693. result->requirements.add(req->clone());
  694. }
  695. return result;
  696. }
  697. void RequirementSet::addRequirements(RequirementSet* zOther)
  698. {
  699. for (Requirement* req : zOther->requirements)
  700. {
  701. addRequirement(req);
  702. }
  703. }
  704. void RequirementSet::negate(LogicTree* zNode)
  705. {
  706. for (Requirement* req : requirements)
  707. {
  708. Requirement* negate = req->clone();
  709. negate->negate();
  710. RequirementSet* set = new RequirementSet();
  711. set->addRequirement(negate);
  712. negate->release();
  713. zNode->addChildren(new LogicTree(LogicalOperator::OR, set));
  714. }
  715. }
  716. bool RequirementSet::isNoneMatch() const
  717. {
  718. for (Requirement* req : requirements)
  719. {
  720. for (Requirement* req2 : requirements)
  721. {
  722. if (req != req2)
  723. {
  724. if (req->contradicts(req2))
  725. {
  726. return 1;
  727. }
  728. }
  729. }
  730. }
  731. return 0;
  732. }
  733. bool RequirementSet::isAnyMatch() const
  734. {
  735. return requirements.getEintragAnzahl() == 0;
  736. }
  737. Framework::Text RequirementSet::renderToTooltip() const
  738. {
  739. int itemType = -1;
  740. for (Requirement* req : requirements)
  741. {
  742. int rT = req->getItemType();
  743. if (rT != -1)
  744. {
  745. if (itemType == -1)
  746. {
  747. itemType = rT;
  748. }
  749. else if (itemType != rT)
  750. {
  751. itemType = -2;
  752. }
  753. }
  754. }
  755. Framework::Text result = "";
  756. if (itemType == -2)
  757. {
  758. return "No Item matches this filter";
  759. }
  760. else if (itemType == -1)
  761. {
  762. result += "Any Item";
  763. }
  764. else
  765. {
  766. result += zItemType(itemType)->getName();
  767. }
  768. if (requirements.getEintragAnzahl() > 0)
  769. {
  770. bool first = 1;
  771. for (Requirement* req : requirements)
  772. {
  773. if (req->getItemType() == -1)
  774. {
  775. if (first)
  776. {
  777. result += ":\n";
  778. first = 0;
  779. }
  780. result += " ";
  781. result += req->getDescription();
  782. result += "\n";
  783. }
  784. }
  785. }
  786. return result;
  787. }
  788. Framework::Bild* RequirementSet::getIcon() const
  789. {
  790. int itemType = -1;
  791. for (Requirement* req : requirements)
  792. {
  793. int rT = req->getItemType();
  794. if (rT != -1)
  795. {
  796. if (itemType == -1)
  797. {
  798. itemType = rT;
  799. }
  800. else if (itemType != rT)
  801. {
  802. itemType = -2;
  803. }
  804. }
  805. }
  806. if (itemType == -2)
  807. {
  808. LTDBDatei dat;
  809. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  810. dat.leseDaten(0);
  811. return dat.laden(0, new Text("noitem.png"));
  812. }
  813. else if (itemType == -1)
  814. {
  815. LTDBDatei dat;
  816. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  817. dat.leseDaten(0);
  818. return dat.laden(0, new Text("anyitem.png"));
  819. }
  820. else
  821. {
  822. return dynamic_cast<Framework::Bild*>(
  823. zItemType(itemType)->zIcon()->getThis());
  824. }
  825. }
  826. int RequirementSet::getItemType() const
  827. {
  828. int itemType = -1;
  829. for (Requirement* req : requirements)
  830. {
  831. int rT = req->getItemType();
  832. if (rT != -1)
  833. {
  834. if (itemType == -1)
  835. {
  836. itemType = rT;
  837. }
  838. else if (itemType != rT)
  839. {
  840. itemType = -2;
  841. }
  842. }
  843. }
  844. return itemType;
  845. }
  846. LogicTree::LogicTree(LogicalOperator op, RequirementSet* set)
  847. : Framework::ReferenceCounter(),
  848. op(op),
  849. requirementSet(set)
  850. {}
  851. LogicTree::~LogicTree()
  852. {
  853. if (requirementSet) requirementSet->release();
  854. }
  855. Framework::RCArray<RequirementSet>* LogicTree::multiply(
  856. Framework::RCArray<RequirementSet>* a,
  857. Framework::RCArray<RequirementSet>* b)
  858. {
  859. Framework::RCArray<RequirementSet>* result
  860. = new Framework::RCArray<RequirementSet>();
  861. for (RequirementSet* aSet : *a)
  862. {
  863. for (RequirementSet* bSet : *b)
  864. {
  865. RequirementSet* set = aSet->clone();
  866. set->addRequirements(bSet);
  867. if (!set->isNoneMatch())
  868. {
  869. if (set->isAnyMatch())
  870. {
  871. result->leeren();
  872. result->add(set);
  873. a->release();
  874. b->release();
  875. return result;
  876. }
  877. else
  878. {
  879. result->add(set);
  880. }
  881. }
  882. else
  883. {
  884. set->release();
  885. }
  886. }
  887. }
  888. return result;
  889. }
  890. void LogicTree::addChildren(LogicTree* tree)
  891. {
  892. children.add(tree);
  893. }
  894. LogicTree* LogicTree::clone()
  895. {
  896. LogicTree* result = new LogicTree(op, requirementSet->clone());
  897. for (LogicTree* child : children)
  898. {
  899. result->children.add(child->clone());
  900. }
  901. return result;
  902. }
  903. void LogicTree::negate()
  904. {
  905. if (requirementSet != 0)
  906. {
  907. requirementSet->negate(this);
  908. requirementSet->release();
  909. requirementSet = 0;
  910. }
  911. else
  912. {
  913. if (op == LogicalOperator::AND)
  914. {
  915. op = LogicalOperator::OR;
  916. }
  917. else if (op == LogicalOperator::OR)
  918. {
  919. op = LogicalOperator::AND;
  920. }
  921. for (LogicTree* child : children)
  922. {
  923. child->negate();
  924. }
  925. }
  926. }
  927. Framework::RCArray<RequirementSet>* LogicTree::resolve()
  928. {
  929. if (requirementSet != 0)
  930. {
  931. Framework::RCArray<RequirementSet>* result
  932. = new Framework::RCArray<RequirementSet>();
  933. result->add(dynamic_cast<RequirementSet*>(requirementSet->getThis()));
  934. return result;
  935. }
  936. else
  937. {
  938. Framework::RCArray<RequirementSet>* result = 0;
  939. if (op == LogicalOperator::OR)
  940. {
  941. result = new Framework::RCArray<RequirementSet>();
  942. for (LogicTree* child : children)
  943. {
  944. Framework::RCArray<RequirementSet>* childSet = child->resolve();
  945. for (RequirementSet* set : *childSet)
  946. {
  947. result->add(dynamic_cast<RequirementSet*>(set->getThis()));
  948. }
  949. childSet->release();
  950. }
  951. }
  952. else if (op == LogicalOperator::AND)
  953. {
  954. result = children.z(0)->resolve();
  955. for (int i = 1; i < children.getEintragAnzahl(); i++)
  956. {
  957. Framework::RCArray<RequirementSet>* childSet
  958. = children.z(i)->resolve();
  959. result = multiply(result, childSet);
  960. }
  961. }
  962. if (result != 0)
  963. {
  964. for (int i = 0; i < result->getEintragAnzahl(); i++)
  965. {
  966. if (result->z(i)->isNoneMatch())
  967. {
  968. result->remove(i);
  969. i--;
  970. }
  971. else if (result->z(i)->isAnyMatch())
  972. {
  973. Framework::RCArray<RequirementSet>* anyMatch
  974. = new Framework::RCArray<RequirementSet>();
  975. anyMatch->add(result->get(i));
  976. result->release();
  977. return anyMatch;
  978. }
  979. }
  980. }
  981. return result;
  982. }
  983. }