RecipieIngredient.cpp 31 KB

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