RecipieIngredient.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. #include "RecipieIngredient.h"
  2. #include <FileSystem.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().isEqual("anyItem"))
  12. {
  13. return new LogicTree(LogicalOperator::OR, new RequirementSet());
  14. }
  15. else if (zElement->getName().isEqual("attribute"))
  16. {
  17. AttributeOperator op = AttributeOperator::Equals;
  18. if (zElement->getAttributeValue("operator").isEqual("!="))
  19. {
  20. op = AttributeOperator::NotEquals;
  21. }
  22. else if (zElement->getAttributeValue("operator").isEqual(">"))
  23. {
  24. op = AttributeOperator::GreaterThan;
  25. }
  26. else if (zElement->getAttributeValue("operator").isEqual(">="))
  27. {
  28. op = AttributeOperator::GreaterThanOrEquals;
  29. }
  30. else if (zElement->getAttributeValue("operator").isEqual("<"))
  31. {
  32. op = AttributeOperator::LessThan;
  33. }
  34. else if (zElement->getAttributeValue("operator").isEqual("<="))
  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().isEqual("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().isEqual("ingredient");
  199. }
  200. //! erstellt eine neue Drawable zu einem gegebenen xml Element
  201. Framework::Drawable* 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::Drawable& z,
  228. Framework::UIMLContainer& generalFactory)
  229. {
  230. return false;
  231. }
  232. //! wendet die layout parameter zu einer Drawable an
  233. void RecipieIngredientElement::layout(Framework::XML::Element& element,
  234. Framework::Drawable& 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::DrawableBackground(),
  245. currentIconIndex(0),
  246. timtUntilNextIcon(2.0),
  247. amount(amount)
  248. {
  249. setStyle(Framework::Drawable::Style::Allowed
  250. | Framework::Drawable::Style::Visible);
  251. Framework::ToolTip* tip = new Framework::ToolTip(window->zScreen());
  252. tip->addStyle(Framework::DrawableBackground::Style::Background
  253. | Framework::DrawableBackground::Style::BAlpha
  254. | Framework::DrawableBackground::Style::Border
  255. | Framework::DrawableBackground::Style::Visible);
  256. tip->setBackgroundColor(0xA0000000);
  257. tip->setBorderColor(0xFFFFFFFF);
  258. tip->setBorderWidth(1);
  259. toolTip = uiFactory.createTextField(uiFactory.initParam);
  260. toolTip->setText("");
  261. toolTip->setSize(0, 0);
  262. toolTip->addStyle(Framework::TextField::Style::Multiline);
  263. tip->addMember(toolTip);
  264. tip->setWarten(0.5);
  265. setToolTipZ(tip);
  266. }
  267. void RecipieIngredient::addPossibleItem(
  268. Framework::Image* icon, Framework::Text* toolTip, int typeId)
  269. {
  270. icons.add(icon);
  271. toolTips.add(toolTip);
  272. itemTypes.add(typeId);
  273. if (toolTips.getEntryCount() == 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.getEntryCount())
  290. {
  291. currentIconIndex = 0;
  292. }
  293. if (toolTips.getEntryCount() > 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 DrawableBackground::tick(tickVal);
  303. }
  304. void RecipieIngredient::render(Framework::Image& rObj)
  305. {
  306. DrawableBackground::render(rObj);
  307. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
  308. TextRenderer tr;
  309. tr.setFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
  310. tr.setFontSize(12);
  311. rObj.fillRegion(0, 0, 50, 50, 0xFF222222);
  312. if (icons.getEntryCount() > 0)
  313. rObj.alphaImage(0, 0, 50, 50, *icons.z(currentIconIndex));
  314. const char* units[] = {"", "K", "M", "G", "T", "P"};
  315. int i = 0;
  316. int tmpCount = amount;
  317. for (; i < 6 && tmpCount > 1024; i++)
  318. tmpCount = tmpCount / 1024;
  319. Text count = tmpCount;
  320. count += units[i];
  321. tr.renderText(45 - tr.getTextWidth(count),
  322. 45 - tr.getTextHeight(count),
  323. count,
  324. rObj,
  325. 0xFFFFFFFF);
  326. rObj.releaseDrawOptions();
  327. }
  328. void RecipieIngredient::doMouseEvent(Framework::MouseEvent& me, bool userRet)
  329. {
  330. if (me.id == ME_RLeft)
  331. {
  332. if (itemTypes.getEntryCount() > currentIconIndex)
  333. {
  334. World::INSTANCE->zClient()->craftingUIMLRequest(
  335. itemTypes.get(currentIconIndex));
  336. }
  337. }
  338. DrawableBackground::doMouseEvent(me, userRet);
  339. }
  340. Requirement::Requirement(
  341. Framework::Text attribute, Framework::Text value, AttributeOperator op)
  342. : Framework::ReferenceCounter(),
  343. attribute(attribute),
  344. value(value),
  345. op(op)
  346. {}
  347. void Requirement::negate()
  348. {
  349. switch (op)
  350. {
  351. case AttributeOperator::Equals:
  352. op = AttributeOperator::NotEquals;
  353. break;
  354. case AttributeOperator::NotEquals:
  355. op = AttributeOperator::Equals;
  356. break;
  357. case AttributeOperator::GreaterThan:
  358. op = AttributeOperator::LessThanOrEquals;
  359. break;
  360. case AttributeOperator::GreaterThanOrEquals:
  361. op = AttributeOperator::LessThan;
  362. break;
  363. case AttributeOperator::LessThan:
  364. op = AttributeOperator::GreaterThanOrEquals;
  365. break;
  366. case AttributeOperator::LessThanOrEquals:
  367. op = AttributeOperator::GreaterThan;
  368. break;
  369. }
  370. }
  371. Requirement* Requirement::clone()
  372. {
  373. return new Requirement(attribute, value, op);
  374. }
  375. bool Requirement::contradicts(Requirement* zOther)
  376. {
  377. if (zOther->attribute.isEqual(attribute.getText()))
  378. {
  379. if (zOther->value.isEqual(value))
  380. {
  381. if (op == AttributeOperator::Equals
  382. && zOther->op == AttributeOperator::NotEquals)
  383. {
  384. return 1;
  385. }
  386. if (op == AttributeOperator::NotEquals
  387. && zOther->op == AttributeOperator::Equals)
  388. {
  389. return 1;
  390. }
  391. if (op == AttributeOperator::GreaterThan
  392. && zOther->op == AttributeOperator::LessThan)
  393. {
  394. return 1;
  395. }
  396. if (op == AttributeOperator::LessThan
  397. && zOther->op == AttributeOperator::GreaterThan)
  398. {
  399. return 1;
  400. }
  401. }
  402. if (op == AttributeOperator::LessThan
  403. || op == AttributeOperator::LessThanOrEquals
  404. || op == AttributeOperator::GreaterThan
  405. || op == AttributeOperator::GreaterThanOrEquals)
  406. {
  407. double v1 = (double)value;
  408. double v2 = (double)zOther->value;
  409. if (op == AttributeOperator::LessThan
  410. || op == AttributeOperator::LessThanOrEquals)
  411. {
  412. if (v1 < v2)
  413. {
  414. if (zOther->op == AttributeOperator::GreaterThan
  415. || zOther->op == AttributeOperator::GreaterThanOrEquals
  416. || zOther->op == AttributeOperator::Equals)
  417. {
  418. return 1;
  419. }
  420. }
  421. }
  422. else if (op == AttributeOperator::GreaterThan
  423. || op == AttributeOperator::GreaterThanOrEquals)
  424. {
  425. if (v1 > v2)
  426. {
  427. if (zOther->op == AttributeOperator::LessThan
  428. || zOther->op == AttributeOperator::LessThanOrEquals
  429. || zOther->op == AttributeOperator::Equals)
  430. {
  431. return 1;
  432. }
  433. }
  434. }
  435. }
  436. if (zOther->op == AttributeOperator::LessThan
  437. || zOther->op == AttributeOperator::LessThanOrEquals
  438. || zOther->op == AttributeOperator::GreaterThan
  439. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  440. {
  441. double v1 = (double)zOther->value;
  442. double v2 = (double)value;
  443. if (zOther->op == AttributeOperator::LessThan
  444. || zOther->op == AttributeOperator::LessThanOrEquals)
  445. {
  446. if (v1 < v2)
  447. {
  448. if (op == AttributeOperator::GreaterThan
  449. || op == AttributeOperator::GreaterThanOrEquals
  450. || op == AttributeOperator::Equals)
  451. {
  452. return 1;
  453. }
  454. }
  455. }
  456. else if (zOther->op == AttributeOperator::GreaterThan
  457. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  458. {
  459. if (v1 > v2)
  460. {
  461. if (op == AttributeOperator::LessThan
  462. || op == AttributeOperator::LessThanOrEquals
  463. || op == AttributeOperator::Equals)
  464. {
  465. return 1;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. return 0;
  472. }
  473. bool Requirement::merge(Requirement* zOther)
  474. {
  475. if (zOther->attribute.isEqual(attribute.getText()))
  476. {
  477. if (zOther->value.isEqual(value))
  478. {
  479. if (op == zOther->op)
  480. {
  481. return 1;
  482. }
  483. }
  484. else
  485. {
  486. if (op == AttributeOperator::Equals
  487. && zOther->op == AttributeOperator::NotEquals)
  488. {
  489. return 1;
  490. }
  491. if (op == AttributeOperator::NotEquals
  492. && zOther->op == AttributeOperator::Equals)
  493. {
  494. op = AttributeOperator::Equals;
  495. value = zOther->value;
  496. return 1;
  497. }
  498. }
  499. if (op == AttributeOperator::LessThan
  500. || op == AttributeOperator::LessThanOrEquals
  501. || op == AttributeOperator::GreaterThan
  502. || op == AttributeOperator::GreaterThanOrEquals)
  503. {
  504. double v1 = (double)value;
  505. double v2 = (double)zOther->value;
  506. if (op == AttributeOperator::LessThan
  507. || op == AttributeOperator::LessThanOrEquals)
  508. {
  509. if (zOther->op == AttributeOperator::LessThan
  510. || zOther->op == AttributeOperator::LessThanOrEquals)
  511. {
  512. if (v1 > v2
  513. || (v1 == v2
  514. && op == AttributeOperator::LessThanOrEquals))
  515. {
  516. op = zOther->op;
  517. value = zOther->value;
  518. }
  519. return 1;
  520. }
  521. if (zOther->op == AttributeOperator::Equals)
  522. {
  523. if (v2 < v1
  524. || (v2 == v1
  525. && op == AttributeOperator::LessThanOrEquals))
  526. {
  527. op = zOther->op;
  528. value = zOther->value;
  529. return 1;
  530. }
  531. }
  532. if (zOther->op == AttributeOperator::NotEquals)
  533. {
  534. if (v2 > v1
  535. || (v2 == v1 && op == AttributeOperator::LessThan))
  536. {
  537. return 1;
  538. }
  539. }
  540. }
  541. else if (op == AttributeOperator::GreaterThan
  542. || op == AttributeOperator::GreaterThanOrEquals)
  543. {
  544. if (zOther->op == AttributeOperator::GreaterThan
  545. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  546. {
  547. if (v1 < v2
  548. || (v1 == v2
  549. && op == AttributeOperator::GreaterThanOrEquals))
  550. {
  551. op = zOther->op;
  552. value = zOther->value;
  553. }
  554. return 1;
  555. }
  556. if (zOther->op == AttributeOperator::Equals)
  557. {
  558. if (v2 > v1
  559. || (v2 == v1
  560. && op == AttributeOperator::GreaterThanOrEquals))
  561. {
  562. op = zOther->op;
  563. value = zOther->value;
  564. return 1;
  565. }
  566. }
  567. if (zOther->op == AttributeOperator::NotEquals)
  568. {
  569. if (v2 < v1
  570. || (v2 == v1 && op == AttributeOperator::GreaterThan))
  571. {
  572. return 1;
  573. }
  574. }
  575. }
  576. }
  577. if (zOther->op == AttributeOperator::LessThan
  578. || zOther->op == AttributeOperator::LessThanOrEquals
  579. || zOther->op == AttributeOperator::GreaterThan
  580. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  581. {
  582. double v1 = (double)zOther->value;
  583. double v2 = (double)value;
  584. if (zOther->op == AttributeOperator::LessThan
  585. || zOther->op == AttributeOperator::LessThanOrEquals)
  586. {
  587. if (op == AttributeOperator::Equals)
  588. {
  589. if (v2 < v1
  590. || (v2 == v1
  591. && zOther->op
  592. == AttributeOperator::LessThanOrEquals))
  593. {
  594. return 1;
  595. }
  596. }
  597. if (op == AttributeOperator::NotEquals)
  598. {
  599. if (v2 > v1
  600. || (v2 == v1
  601. && zOther->op == AttributeOperator::LessThan))
  602. {
  603. op = zOther->op;
  604. value = zOther->value;
  605. return 1;
  606. }
  607. }
  608. }
  609. else if (zOther->op == AttributeOperator::GreaterThan
  610. || zOther->op == AttributeOperator::GreaterThanOrEquals)
  611. {
  612. if (op == AttributeOperator::Equals)
  613. {
  614. if (v2 > v1
  615. || (v2 == v1
  616. && zOther->op
  617. == AttributeOperator::GreaterThanOrEquals))
  618. {
  619. return 1;
  620. }
  621. }
  622. if (op == AttributeOperator::NotEquals)
  623. {
  624. if (v2 < v1
  625. || (v2 == v1
  626. && zOther->op == AttributeOperator::GreaterThan))
  627. {
  628. op = zOther->op;
  629. value = zOther->value;
  630. return 1;
  631. }
  632. }
  633. }
  634. }
  635. }
  636. return 0;
  637. }
  638. int Requirement::getItemType()
  639. {
  640. if (attribute.isEqual("Type") && op == AttributeOperator::Equals)
  641. {
  642. return (int)value;
  643. }
  644. return -1;
  645. }
  646. Framework::Text Requirement::getDescription()
  647. {
  648. Framework::Text result = attribute;
  649. result += " ";
  650. switch (op)
  651. {
  652. case AttributeOperator::Equals:
  653. result += "is";
  654. break;
  655. case AttributeOperator::NotEquals:
  656. result += "is not";
  657. break;
  658. case AttributeOperator::LessThan:
  659. result += "is less than";
  660. break;
  661. case AttributeOperator::LessThanOrEquals:
  662. result += "is less than or equal to";
  663. break;
  664. case AttributeOperator::GreaterThan:
  665. result += "is greater than";
  666. break;
  667. case AttributeOperator::GreaterThanOrEquals:
  668. result += "is greater than or equal to";
  669. break;
  670. }
  671. result += " ";
  672. result += value;
  673. return result;
  674. }
  675. RequirementSet::RequirementSet()
  676. : Framework::ReferenceCounter()
  677. {}
  678. void RequirementSet::addRequirement(Requirement* zReq)
  679. {
  680. for (Requirement* req : requirements)
  681. {
  682. if (req->merge(zReq)) return;
  683. }
  684. requirements.add(zReq->clone());
  685. }
  686. RequirementSet* RequirementSet::clone()
  687. {
  688. RequirementSet* result = new RequirementSet();
  689. for (Requirement* req : requirements)
  690. {
  691. result->requirements.add(req->clone());
  692. }
  693. return result;
  694. }
  695. void RequirementSet::addRequirements(RequirementSet* zOther)
  696. {
  697. for (Requirement* req : zOther->requirements)
  698. {
  699. addRequirement(req);
  700. }
  701. }
  702. void RequirementSet::negate(LogicTree* zNode)
  703. {
  704. for (Requirement* req : requirements)
  705. {
  706. Requirement* negate = req->clone();
  707. negate->negate();
  708. RequirementSet* set = new RequirementSet();
  709. set->addRequirement(negate);
  710. negate->release();
  711. zNode->addChildren(new LogicTree(LogicalOperator::OR, set));
  712. }
  713. }
  714. bool RequirementSet::isNoneMatch() const
  715. {
  716. for (Requirement* req : requirements)
  717. {
  718. for (Requirement* req2 : requirements)
  719. {
  720. if (req != req2)
  721. {
  722. if (req->contradicts(req2))
  723. {
  724. return 1;
  725. }
  726. }
  727. }
  728. }
  729. return 0;
  730. }
  731. bool RequirementSet::isAnyMatch() const
  732. {
  733. return requirements.getEntryCount() == 0;
  734. }
  735. Framework::Text RequirementSet::renderToTooltip() const
  736. {
  737. int itemType = -1;
  738. for (Requirement* req : requirements)
  739. {
  740. int rT = req->getItemType();
  741. if (rT != -1)
  742. {
  743. if (itemType == -1)
  744. {
  745. itemType = rT;
  746. }
  747. else if (itemType != rT)
  748. {
  749. itemType = -2;
  750. }
  751. }
  752. }
  753. Framework::Text result = "";
  754. if (itemType == -2)
  755. {
  756. return "No Item matches this filter";
  757. }
  758. else if (itemType == -1)
  759. {
  760. result += "Any Item";
  761. }
  762. else
  763. {
  764. result += zItemType(itemType)->getName();
  765. }
  766. if (requirements.getEntryCount() > 0)
  767. {
  768. bool first = 1;
  769. for (Requirement* req : requirements)
  770. {
  771. if (req->getItemType() == -1)
  772. {
  773. if (first)
  774. {
  775. result += ":\n";
  776. first = 0;
  777. }
  778. result += " ";
  779. result += req->getDescription();
  780. result += "\n";
  781. }
  782. }
  783. }
  784. return result;
  785. }
  786. Framework::Image* RequirementSet::getIcon() const
  787. {
  788. int itemType = -1;
  789. for (Requirement* req : requirements)
  790. {
  791. int rT = req->getItemType();
  792. if (rT != -1)
  793. {
  794. if (itemType == -1)
  795. {
  796. itemType = rT;
  797. }
  798. else if (itemType != rT)
  799. {
  800. itemType = -2;
  801. }
  802. }
  803. }
  804. if (itemType == -2)
  805. {
  806. LTDBFile dat;
  807. dat.setFile(new Text("data/images/gui_icons.ltdb"));
  808. dat.readData(0);
  809. return dat.load(0, new Text("noitem.png"));
  810. }
  811. else if (itemType == -1)
  812. {
  813. LTDBFile dat;
  814. dat.setFile(new Text("data/images/gui_icons.ltdb"));
  815. dat.readData(0);
  816. return dat.load(0, new Text("anyitem.png"));
  817. }
  818. else
  819. {
  820. return dynamic_cast<Framework::Image*>(
  821. zItemType(itemType)->zIcon()->getThis());
  822. }
  823. }
  824. int RequirementSet::getItemType() const
  825. {
  826. int itemType = -1;
  827. for (Requirement* req : requirements)
  828. {
  829. int rT = req->getItemType();
  830. if (rT != -1)
  831. {
  832. if (itemType == -1)
  833. {
  834. itemType = rT;
  835. }
  836. else if (itemType != rT)
  837. {
  838. itemType = -2;
  839. }
  840. }
  841. }
  842. return itemType;
  843. }
  844. LogicTree::LogicTree(LogicalOperator op, RequirementSet* set)
  845. : Framework::ReferenceCounter(),
  846. op(op),
  847. requirementSet(set)
  848. {}
  849. LogicTree::~LogicTree()
  850. {
  851. if (requirementSet) requirementSet->release();
  852. }
  853. Framework::RCArray<RequirementSet>* LogicTree::multiply(
  854. Framework::RCArray<RequirementSet>* a,
  855. Framework::RCArray<RequirementSet>* b)
  856. {
  857. Framework::RCArray<RequirementSet>* result
  858. = new Framework::RCArray<RequirementSet>();
  859. for (RequirementSet* aSet : *a)
  860. {
  861. for (RequirementSet* bSet : *b)
  862. {
  863. RequirementSet* set = aSet->clone();
  864. set->addRequirements(bSet);
  865. if (!set->isNoneMatch())
  866. {
  867. if (set->isAnyMatch())
  868. {
  869. result->clear();
  870. result->add(set);
  871. a->release();
  872. b->release();
  873. return result;
  874. }
  875. else
  876. {
  877. result->add(set);
  878. }
  879. }
  880. else
  881. {
  882. set->release();
  883. }
  884. }
  885. }
  886. return result;
  887. }
  888. void LogicTree::addChildren(LogicTree* tree)
  889. {
  890. children.add(tree);
  891. }
  892. LogicTree* LogicTree::clone()
  893. {
  894. LogicTree* result = new LogicTree(op, requirementSet->clone());
  895. for (LogicTree* child : children)
  896. {
  897. result->children.add(child->clone());
  898. }
  899. return result;
  900. }
  901. void LogicTree::negate()
  902. {
  903. if (requirementSet != 0)
  904. {
  905. requirementSet->negate(this);
  906. requirementSet->release();
  907. requirementSet = 0;
  908. }
  909. else
  910. {
  911. if (op == LogicalOperator::AND)
  912. {
  913. op = LogicalOperator::OR;
  914. }
  915. else if (op == LogicalOperator::OR)
  916. {
  917. op = LogicalOperator::AND;
  918. }
  919. for (LogicTree* child : children)
  920. {
  921. child->negate();
  922. }
  923. }
  924. }
  925. Framework::RCArray<RequirementSet>* LogicTree::resolve()
  926. {
  927. if (requirementSet != 0)
  928. {
  929. Framework::RCArray<RequirementSet>* result
  930. = new Framework::RCArray<RequirementSet>();
  931. result->add(dynamic_cast<RequirementSet*>(requirementSet->getThis()));
  932. return result;
  933. }
  934. else
  935. {
  936. Framework::RCArray<RequirementSet>* result = 0;
  937. if (op == LogicalOperator::OR)
  938. {
  939. result = new Framework::RCArray<RequirementSet>();
  940. for (LogicTree* child : children)
  941. {
  942. Framework::RCArray<RequirementSet>* childSet = child->resolve();
  943. for (RequirementSet* set : *childSet)
  944. {
  945. result->add(dynamic_cast<RequirementSet*>(set->getThis()));
  946. }
  947. childSet->release();
  948. }
  949. }
  950. else if (op == LogicalOperator::AND)
  951. {
  952. result = children.z(0)->resolve();
  953. for (int i = 1; i < children.getEntryCount(); i++)
  954. {
  955. Framework::RCArray<RequirementSet>* childSet
  956. = children.z(i)->resolve();
  957. result = multiply(result, childSet);
  958. }
  959. }
  960. if (result != 0)
  961. {
  962. for (int i = 0; i < result->getEntryCount(); i++)
  963. {
  964. if (result->z(i)->isNoneMatch())
  965. {
  966. result->remove(i);
  967. i--;
  968. }
  969. else if (result->z(i)->isAnyMatch())
  970. {
  971. Framework::RCArray<RequirementSet>* anyMatch
  972. = new Framework::RCArray<RequirementSet>();
  973. anyMatch->add(result->get(i));
  974. result->release();
  975. return anyMatch;
  976. }
  977. }
  978. }
  979. return result;
  980. }
  981. }