RecipieIngredient.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. World::INSTANCE->zClient()->craftingUIMLRequest(
  335. itemTypes.get(currentIconIndex));
  336. }
  337. ZeichnungHintergrund::doMausEreignis(me, userRet);
  338. }
  339. Requirement::Requirement(
  340. Framework::Text attribute, Framework::Text value, AttributeOperator op)
  341. : Framework::ReferenceCounter(),
  342. attribute(attribute),
  343. value(value),
  344. op(op)
  345. {}
  346. void Requirement::negate()
  347. {
  348. switch (op)
  349. {
  350. case AttributeOperator::Equals:
  351. op = AttributeOperator::NotEquals;
  352. break;
  353. case AttributeOperator::NotEquals:
  354. op = AttributeOperator::Equals;
  355. break;
  356. case AttributeOperator::GreaterThan:
  357. op = AttributeOperator::LessThanOrEquals;
  358. break;
  359. case AttributeOperator::GreaterThanOrEquals:
  360. op = AttributeOperator::LessThan;
  361. break;
  362. case AttributeOperator::LessThan:
  363. op = AttributeOperator::GreaterThanOrEquals;
  364. break;
  365. case AttributeOperator::LessThanOrEquals:
  366. op = AttributeOperator::GreaterThan;
  367. break;
  368. }
  369. }
  370. Requirement* Requirement::clone()
  371. {
  372. return new Requirement(attribute, value, op);
  373. }
  374. bool Requirement::contradicts(Requirement* zOther)
  375. {
  376. if (zOther->attribute.istGleich(attribute.getText()))
  377. {
  378. if (zOther->value.istGleich(value))
  379. {
  380. if (op == AttributeOperator::Equals
  381. && zOther->op == AttributeOperator::NotEquals)
  382. {
  383. return 1;
  384. }
  385. if (op == AttributeOperator::NotEquals
  386. && zOther->op == AttributeOperator::Equals)
  387. {
  388. return 1;
  389. }
  390. if (op == AttributeOperator::GreaterThan
  391. && zOther->op == AttributeOperator::LessThan)
  392. {
  393. return 1;
  394. }
  395. if (op == AttributeOperator::LessThan
  396. && zOther->op == AttributeOperator::GreaterThan)
  397. {
  398. return 1;
  399. }
  400. }
  401. if (op == AttributeOperator::LessThan
  402. || op == AttributeOperator::LessThanOrEquals
  403. || op == AttributeOperator::GreaterThan
  404. || op == AttributeOperator::GreaterThanOrEquals)
  405. {
  406. double v1 = (double)value;
  407. double v2 = (double)zOther->value;
  408. if (op == AttributeOperator::LessThan
  409. || op == AttributeOperator::LessThanOrEquals)
  410. {
  411. if (v1 < v2)
  412. {
  413. if (zOther->op == AttributeOperator::GreaterThan
  414. || zOther->op == AttributeOperator::GreaterThanOrEquals
  415. || zOther->op == AttributeOperator::Equals)
  416. {
  417. return 1;
  418. }
  419. }
  420. }
  421. else if (op == AttributeOperator::GreaterThan
  422. || op == AttributeOperator::GreaterThanOrEquals)
  423. {
  424. if (v1 > v2)
  425. {
  426. if (zOther->op == AttributeOperator::LessThan
  427. || zOther->op == AttributeOperator::LessThanOrEquals
  428. || zOther->op == AttributeOperator::Equals)
  429. {
  430. return 1;
  431. }
  432. }
  433. }
  434. }
  435. if (zOther->op == AttributeOperator::LessThan
  436. || zOther->op == AttributeOperator::LessThanOrEquals
  437. || zOther->op == AttributeOperator::GreaterThan
  438. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  439. {
  440. double v1 = (double)zOther->value;
  441. double v2 = (double)value;
  442. if (zOther->op == AttributeOperator::LessThan
  443. || zOther->op == AttributeOperator::LessThanOrEquals)
  444. {
  445. if (v1 < v2)
  446. {
  447. if (op == AttributeOperator::GreaterThan
  448. || op == AttributeOperator::GreaterThanOrEquals
  449. || op == AttributeOperator::Equals)
  450. {
  451. return 1;
  452. }
  453. }
  454. }
  455. else if (zOther->op == AttributeOperator::GreaterThan
  456. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  457. {
  458. if (v1 > v2)
  459. {
  460. if (op == AttributeOperator::LessThan
  461. || op == AttributeOperator::LessThanOrEquals
  462. || op == AttributeOperator::Equals)
  463. {
  464. return 1;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. return 0;
  471. }
  472. bool Requirement::merge(Requirement* zOther)
  473. {
  474. if (zOther->attribute.istGleich(attribute.getText()))
  475. {
  476. if (zOther->value.istGleich(value))
  477. {
  478. if (op == zOther->op)
  479. {
  480. return 1;
  481. }
  482. }
  483. else
  484. {
  485. if (op == AttributeOperator::Equals
  486. && zOther->op == AttributeOperator::NotEquals)
  487. {
  488. return 1;
  489. }
  490. if (op == AttributeOperator::NotEquals
  491. && zOther->op == AttributeOperator::Equals)
  492. {
  493. op = AttributeOperator::Equals;
  494. value = zOther->value;
  495. return 1;
  496. }
  497. }
  498. if (op == AttributeOperator::LessThan
  499. || op == AttributeOperator::LessThanOrEquals
  500. || op == AttributeOperator::GreaterThan
  501. || op == AttributeOperator::GreaterThanOrEquals)
  502. {
  503. double v1 = (double)value;
  504. double v2 = (double)zOther->value;
  505. if (op == AttributeOperator::LessThan
  506. || op == AttributeOperator::LessThanOrEquals)
  507. {
  508. if (zOther->op == AttributeOperator::LessThan
  509. || zOther->op == AttributeOperator::LessThanOrEquals)
  510. {
  511. if (v1 > v2
  512. || (v1 == v2
  513. && op == AttributeOperator::LessThanOrEquals))
  514. {
  515. op = zOther->op;
  516. value = zOther->value;
  517. }
  518. return 1;
  519. }
  520. if (zOther->op == AttributeOperator::Equals)
  521. {
  522. if (v2 < v1
  523. || (v2 == v1
  524. && op == AttributeOperator::LessThanOrEquals))
  525. {
  526. op = zOther->op;
  527. value = zOther->value;
  528. return 1;
  529. }
  530. }
  531. if (zOther->op == AttributeOperator::NotEquals)
  532. {
  533. if (v2 > v1
  534. || (v2 == v1 && op == AttributeOperator::LessThan))
  535. {
  536. return 1;
  537. }
  538. }
  539. }
  540. else if (op == AttributeOperator::GreaterThan
  541. || op == AttributeOperator::GreaterThanOrEquals)
  542. {
  543. if (zOther->op == AttributeOperator::GreaterThan
  544. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  545. {
  546. if (v1 < v2
  547. || (v1 == v2
  548. && op == AttributeOperator::GreaterThanOrEquals))
  549. {
  550. op = zOther->op;
  551. value = zOther->value;
  552. }
  553. return 1;
  554. }
  555. if (zOther->op == AttributeOperator::Equals)
  556. {
  557. if (v2 > v1
  558. || (v2 == v1
  559. && op == AttributeOperator::GreaterThanOrEquals))
  560. {
  561. op = zOther->op;
  562. value = zOther->value;
  563. return 1;
  564. }
  565. }
  566. if (zOther->op == AttributeOperator::NotEquals)
  567. {
  568. if (v2 < v1
  569. || (v2 == v1 && op == AttributeOperator::GreaterThan))
  570. {
  571. return 1;
  572. }
  573. }
  574. }
  575. }
  576. if (zOther->op == AttributeOperator::LessThan
  577. || zOther->op == AttributeOperator::LessThanOrEquals
  578. || zOther->op == AttributeOperator::GreaterThan
  579. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  580. {
  581. double v1 = (double)zOther->value;
  582. double v2 = (double)value;
  583. if (zOther->op == AttributeOperator::LessThan
  584. || zOther->op == AttributeOperator::LessThanOrEquals)
  585. {
  586. if (op == AttributeOperator::Equals)
  587. {
  588. if (v2 < v1
  589. || (v2 == v1
  590. && zOther->op
  591. == AttributeOperator::LessThanOrEquals))
  592. {
  593. return 1;
  594. }
  595. }
  596. if (op == AttributeOperator::NotEquals)
  597. {
  598. if (v2 > v1
  599. || (v2 == v1
  600. && zOther->op == AttributeOperator::LessThan))
  601. {
  602. op = zOther->op;
  603. value = zOther->value;
  604. return 1;
  605. }
  606. }
  607. }
  608. else if (zOther->op == AttributeOperator::GreaterThan
  609. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  610. {
  611. if (op == AttributeOperator::Equals)
  612. {
  613. if (v2 > v1
  614. || (v2 == v1
  615. && zOther->op
  616. == AttributeOperator::GreaterThanOrEquals))
  617. {
  618. return 1;
  619. }
  620. }
  621. if (op == AttributeOperator::NotEquals)
  622. {
  623. if (v2 < v1
  624. || (v2 == v1
  625. && zOther->op == AttributeOperator::GreaterThan))
  626. {
  627. op = zOther->op;
  628. value = zOther->value;
  629. return 1;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. return 0;
  636. }
  637. int Requirement::getItemType()
  638. {
  639. if (attribute.istGleich("Type") && op == AttributeOperator::Equals)
  640. {
  641. return (int)value;
  642. }
  643. return -1;
  644. }
  645. Framework::Text Requirement::getDescription()
  646. {
  647. Framework::Text result = attribute;
  648. result += " ";
  649. switch (op)
  650. {
  651. case AttributeOperator::Equals:
  652. result += "is";
  653. break;
  654. case AttributeOperator::NotEquals:
  655. result += "is not";
  656. break;
  657. case AttributeOperator::LessThan:
  658. result += "is less than";
  659. break;
  660. case AttributeOperator::LessThanOrEquals:
  661. result += "is less than or equal to";
  662. break;
  663. case AttributeOperator::GreaterThan:
  664. result += "is greater than";
  665. break;
  666. case AttributeOperator::GreaterThanOrEquals:
  667. result += "is greater than or equal to";
  668. break;
  669. }
  670. result += " ";
  671. result += value;
  672. return result;
  673. }
  674. RequirementSet::RequirementSet()
  675. : Framework::ReferenceCounter()
  676. {}
  677. void RequirementSet::addRequirement(Requirement* zReq)
  678. {
  679. for (Requirement* req : requirements)
  680. {
  681. if (req->merge(zReq)) return;
  682. }
  683. requirements.add(zReq->clone());
  684. }
  685. RequirementSet* RequirementSet::clone()
  686. {
  687. RequirementSet* result = new RequirementSet();
  688. for (Requirement* req : requirements)
  689. {
  690. result->requirements.add(req->clone());
  691. }
  692. return result;
  693. }
  694. void RequirementSet::addRequirements(RequirementSet* zOther)
  695. {
  696. for (Requirement* req : zOther->requirements)
  697. {
  698. addRequirement(req);
  699. }
  700. }
  701. void RequirementSet::negate(LogicTree* zNode)
  702. {
  703. for (Requirement* req : requirements)
  704. {
  705. Requirement* negate = req->clone();
  706. negate->negate();
  707. RequirementSet* set = new RequirementSet();
  708. set->addRequirement(negate);
  709. negate->release();
  710. zNode->addChildren(new LogicTree(LogicalOperator::OR, set));
  711. }
  712. }
  713. bool RequirementSet::isNoneMatch() const
  714. {
  715. for (Requirement* req : requirements)
  716. {
  717. for (Requirement* req2 : requirements)
  718. {
  719. if (req != req2)
  720. {
  721. if (req->contradicts(req2))
  722. {
  723. return 1;
  724. }
  725. }
  726. }
  727. }
  728. return 0;
  729. }
  730. bool RequirementSet::isAnyMatch() const
  731. {
  732. return requirements.getEintragAnzahl() == 0;
  733. }
  734. Framework::Text RequirementSet::renderToTooltip() const
  735. {
  736. int itemType = -1;
  737. for (Requirement* req : requirements)
  738. {
  739. int rT = req->getItemType();
  740. if (rT != -1)
  741. {
  742. if (itemType == -1)
  743. {
  744. itemType = rT;
  745. }
  746. else if (itemType != rT)
  747. {
  748. itemType = -2;
  749. }
  750. }
  751. }
  752. Framework::Text result = "";
  753. if (itemType == -2)
  754. {
  755. return "No Item matches this filter";
  756. }
  757. else if (itemType == -1)
  758. {
  759. result += "Any Item";
  760. }
  761. else
  762. {
  763. result += zItemType(itemType)->getName();
  764. }
  765. if (requirements.getEintragAnzahl() > 0)
  766. {
  767. bool first = 1;
  768. for (Requirement* req : requirements)
  769. {
  770. if (req->getItemType() == -1)
  771. {
  772. if (first)
  773. {
  774. result += ":\n";
  775. first = 0;
  776. }
  777. result += " ";
  778. result += req->getDescription();
  779. result += "\n";
  780. }
  781. }
  782. }
  783. return result;
  784. }
  785. Framework::Bild* RequirementSet::getIcon() const
  786. {
  787. int itemType = -1;
  788. for (Requirement* req : requirements)
  789. {
  790. int rT = req->getItemType();
  791. if (rT != -1)
  792. {
  793. if (itemType == -1)
  794. {
  795. itemType = rT;
  796. }
  797. else if (itemType != rT)
  798. {
  799. itemType = -2;
  800. }
  801. }
  802. }
  803. if (itemType == -2)
  804. {
  805. LTDBDatei dat;
  806. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  807. dat.leseDaten(0);
  808. return dat.laden(0, new Text("noitem.png"));
  809. }
  810. else if (itemType == -1)
  811. {
  812. LTDBDatei dat;
  813. dat.setDatei(new Text("data/images/gui_icons.ltdb"));
  814. dat.leseDaten(0);
  815. return dat.laden(0, new Text("anyitem.png"));
  816. }
  817. else
  818. {
  819. return dynamic_cast<Framework::Bild*>(
  820. zItemType(itemType)->zIcon()->getThis());
  821. }
  822. }
  823. int RequirementSet::getItemType() const
  824. {
  825. int itemType = -1;
  826. for (Requirement* req : requirements)
  827. {
  828. int rT = req->getItemType();
  829. if (rT != -1)
  830. {
  831. if (itemType == -1)
  832. {
  833. itemType = rT;
  834. }
  835. else if (itemType != rT)
  836. {
  837. itemType = -2;
  838. }
  839. }
  840. }
  841. return itemType;
  842. }
  843. LogicTree::LogicTree(LogicalOperator op, RequirementSet* set)
  844. : Framework::ReferenceCounter(),
  845. op(op),
  846. requirementSet(set)
  847. {}
  848. LogicTree::~LogicTree()
  849. {
  850. if (requirementSet) requirementSet->release();
  851. }
  852. Framework::RCArray<RequirementSet>* LogicTree::multiply(
  853. Framework::RCArray<RequirementSet>* a,
  854. Framework::RCArray<RequirementSet>* b)
  855. {
  856. Framework::RCArray<RequirementSet>* result
  857. = new Framework::RCArray<RequirementSet>();
  858. for (RequirementSet* aSet : *a)
  859. {
  860. for (RequirementSet* bSet : *b)
  861. {
  862. RequirementSet* set = aSet->clone();
  863. set->addRequirements(bSet);
  864. if (!set->isNoneMatch())
  865. {
  866. if (set->isAnyMatch())
  867. {
  868. result->leeren();
  869. result->add(set);
  870. a->release();
  871. b->release();
  872. return result;
  873. }
  874. else
  875. {
  876. result->add(set);
  877. }
  878. }
  879. else
  880. {
  881. set->release();
  882. }
  883. }
  884. }
  885. return result;
  886. }
  887. void LogicTree::addChildren(LogicTree* tree)
  888. {
  889. children.add(tree);
  890. }
  891. LogicTree* LogicTree::clone()
  892. {
  893. LogicTree* result = new LogicTree(op, requirementSet->clone());
  894. for (LogicTree* child : children)
  895. {
  896. result->children.add(child->clone());
  897. }
  898. return result;
  899. }
  900. void LogicTree::negate()
  901. {
  902. if (requirementSet != 0)
  903. {
  904. requirementSet->negate(this);
  905. requirementSet->release();
  906. requirementSet = 0;
  907. }
  908. else
  909. {
  910. if (op == LogicalOperator::AND)
  911. {
  912. op = LogicalOperator::OR;
  913. }
  914. else if (op == LogicalOperator::OR)
  915. {
  916. op = LogicalOperator::AND;
  917. }
  918. for (LogicTree* child : children)
  919. {
  920. child->negate();
  921. }
  922. }
  923. }
  924. Framework::RCArray<RequirementSet>* LogicTree::resolve()
  925. {
  926. if (requirementSet != 0)
  927. {
  928. Framework::RCArray<RequirementSet>* result
  929. = new Framework::RCArray<RequirementSet>();
  930. result->add(dynamic_cast<RequirementSet*>(requirementSet->getThis()));
  931. return result;
  932. }
  933. else
  934. {
  935. Framework::RCArray<RequirementSet>* result = 0;
  936. if (op == LogicalOperator::OR)
  937. {
  938. result = new Framework::RCArray<RequirementSet>();
  939. for (LogicTree* child : children)
  940. {
  941. Framework::RCArray<RequirementSet>* childSet = child->resolve();
  942. for (RequirementSet* set : *childSet)
  943. {
  944. result->add(dynamic_cast<RequirementSet*>(set->getThis()));
  945. }
  946. childSet->release();
  947. }
  948. }
  949. else if (op == LogicalOperator::AND)
  950. {
  951. result = children.z(0)->resolve();
  952. for (int i = 1; i < children.getEintragAnzahl(); i++)
  953. {
  954. Framework::RCArray<RequirementSet>* childSet
  955. = children.z(i)->resolve();
  956. result = multiply(result, childSet);
  957. }
  958. }
  959. if (result != 0)
  960. {
  961. for (int i = 0; i < result->getEintragAnzahl(); i++)
  962. {
  963. if (result->z(i)->isNoneMatch())
  964. {
  965. result->remove(i);
  966. i--;
  967. }
  968. else if (result->z(i)->isAnyMatch())
  969. {
  970. Framework::RCArray<RequirementSet>* anyMatch
  971. = new Framework::RCArray<RequirementSet>();
  972. anyMatch->add(result->get(i));
  973. result->release();
  974. return anyMatch;
  975. }
  976. }
  977. }
  978. return result;
  979. }
  980. }