| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022 |
- #include "RecipieIngredient.h"
- #include <FileSystem.h>
- #include <Image.h>
- #include <ToolTip.h>
- #include <XML.h>
- #include "FactoryClient.h"
- #include "Globals.h"
- #include "ItemType.h"
- #include "World.h"
- RecipieIngredientElement::RecipieIngredientElement()
- : Framework::UIMLElement()
- {}
- LogicTree* RecipieIngredientElement::parse(Framework::XML::Element* zElement)
- {
- if (zElement->getName().isEqual("anyItem"))
- {
- return new LogicTree(LogicalOperator::OR, new RequirementSet());
- }
- else if (zElement->getName().isEqual("attribute"))
- {
- AttributeOperator op = AttributeOperator::Equals;
- if (zElement->getAttributeValue("operator").isEqual("!="))
- {
- op = AttributeOperator::NotEquals;
- }
- else if (zElement->getAttributeValue("operator").isEqual(">"))
- {
- op = AttributeOperator::GreaterThan;
- }
- else if (zElement->getAttributeValue("operator").isEqual(">="))
- {
- op = AttributeOperator::GreaterThanOrEquals;
- }
- else if (zElement->getAttributeValue("operator").isEqual("<"))
- {
- op = AttributeOperator::LessThan;
- }
- else if (zElement->getAttributeValue("operator").isEqual("<="))
- {
- op = AttributeOperator::LessThanOrEquals;
- }
- Requirement* req = new Requirement(zElement->getAttributeValue("name"),
- zElement->getAttributeValue("value"),
- op);
- RequirementSet* set = new RequirementSet();
- set->addRequirement(req);
- req->release();
- return new LogicTree(LogicalOperator::OR, set);
- }
- else if (zElement->getName().isEqual("operator"))
- {
- bool result0_0 = (bool)(int)zElement->getAttributeValue("result_0_0");
- bool result0_1 = (bool)(int)zElement->getAttributeValue("result_0_1");
- bool result1_0 = (bool)(int)zElement->getAttributeValue("result_1_0");
- bool result1_1 = (bool)(int)zElement->getAttributeValue("result_1_1");
- if (!result0_0 && !result0_1 && !result1_0 && !result1_1)
- { // none match
- RequirementSet* set = new RequirementSet();
- Requirement* req
- = new Requirement("_x", "0", AttributeOperator::Equals);
- set->addRequirement(req);
- req->release();
- req = new Requirement("_x", "1", AttributeOperator::Equals);
- set->addRequirement(req);
- req->release();
- return new LogicTree(LogicalOperator::OR, set);
- }
- if (result0_0 && result0_1 && result1_0 && result1_1)
- { // any match
- return new LogicTree(LogicalOperator::OR, new RequirementSet());
- }
- auto iterator = zElement->getChilds();
- LogicTree* left = parse(iterator.val());
- if (!result0_0 && !result0_1 && result1_0 && result1_1)
- {
- return left;
- }
- if (result0_0 && result0_1 && !result1_0 && !result1_1)
- {
- left->negate();
- return left;
- }
- LogicTree* right = 0;
- iterator++;
- right = parse(iterator.val());
- if (!result0_0 && result0_1 && !result1_0 && result1_1)
- {
- left->release();
- return right;
- }
- if (result0_0 && !result0_1 && result1_0 && !result1_1)
- {
- left->release();
- right->negate();
- return right;
- }
- if (!result0_0 && !result0_1 && !result1_0 && result1_1)
- {
- LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (!result0_0 && result0_1 && result1_0 && result1_1)
- {
- LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (result0_0 && result0_1 && result1_0 && !result1_1)
- {
- left->negate();
- right->negate();
- LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (result0_0 && !result0_1 && !result1_0 && !result1_1)
- {
- left->negate();
- right->negate();
- LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (!result0_0 && !result0_1 && result1_0 && !result1_1)
- {
- right->negate();
- LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (!result0_0 && result0_1 && !result1_0 && !result1_1)
- {
- left->negate();
- LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (!result0_0 && result0_1 && result1_0 && !result1_1)
- {
- LogicTree* orT = new LogicTree(LogicalOperator::OR, 0);
- orT->addChildren(left);
- orT->addChildren(right);
- LogicTree* notLeft = left->clone();
- notLeft->negate();
- LogicTree* notRight = right->clone();
- notRight->negate();
- LogicTree* notOr = new LogicTree(LogicalOperator::OR, 0);
- notOr->addChildren(notLeft);
- notOr->addChildren(notRight);
- LogicTree* result = new LogicTree(LogicalOperator::AND, 0);
- result->addChildren(orT);
- result->addChildren(notOr);
- return result;
- }
- if (result0_0 && !result0_1 && !result1_0 && result1_1)
- {
- LogicTree* andT = new LogicTree(LogicalOperator::AND, 0);
- andT->addChildren(left);
- andT->addChildren(right);
- LogicTree* notLeft = left->clone();
- notLeft->negate();
- LogicTree* notRight = right->clone();
- notRight->negate();
- LogicTree* notAnd = new LogicTree(LogicalOperator::AND, 0);
- notAnd->addChildren(notLeft);
- notAnd->addChildren(notRight);
- LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
- result->addChildren(andT);
- result->addChildren(notAnd);
- return result;
- }
- if (result0_0 && !result0_1 && result1_0 && result1_1)
- {
- right->negate();
- LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- if (result0_0 && result0_1 && !result1_0 && result1_1)
- {
- left->negate();
- LogicTree* result = new LogicTree(LogicalOperator::OR, 0);
- result->addChildren(left);
- result->addChildren(right);
- return result;
- }
- }
- return 0;
- }
- //! prüft, ob dieses UIML Element für ein bestimmtes xml Element zuständig
- //! ist
- bool RecipieIngredientElement::isApplicableFor(Framework::XML::Element& element)
- {
- return element.getName().isEqual("ingredient");
- }
- //! erstellt eine neue Drawable zu einem gegebenen xml Element
- Framework::Drawable* RecipieIngredientElement::parseElement(
- Framework::XML::Element& element, Framework::UIMLContainer& generalFactory)
- {
- int amount = (int)element.getAttributeValue("amount");
- RecipieIngredient* result = new RecipieIngredient(amount);
- Framework::XML::Editor logicSelector = element.selectChildsByName("logic");
- if (logicSelector.exists())
- {
- LogicTree* logic = new LogicTree(LogicalOperator::OR, 0);
- logicSelector.selectChildren().forEach(
- [this, logic](Framework::XML::Element* zElement) {
- logic->addChildren(parse(zElement));
- });
- Framework::RCArray<RequirementSet>* requirements = logic->resolve();
- logic->release();
- for (RequirementSet* set : *requirements)
- {
- result->addPossibleItem(set->getIcon(),
- new Text(set->renderToTooltip()),
- set->getItemType());
- }
- requirements->release();
- }
- return result;
- }
- bool RecipieIngredientElement::updateElement(Framework::XML::Element& element,
- Framework::Drawable& z,
- Framework::UIMLContainer& generalFactory)
- {
- return false;
- }
- //! wendet die layout parameter zu einer Drawable an
- void RecipieIngredientElement::layout(Framework::XML::Element& element,
- Framework::Drawable& z,
- int pWidth,
- int pHeight,
- Framework::UIMLContainer& generalLayouter)
- {
- UIMLElement::layout(element, z, pWidth, pHeight, generalLayouter);
- z.setWidth(50);
- z.setHeight(50);
- }
- RecipieIngredient::RecipieIngredient(int amount)
- : Framework::DrawableBackground(),
- currentIconIndex(0),
- timtUntilNextIcon(2.0),
- amount(amount)
- {
- setStyle(Framework::Drawable::Style::Allowed
- | Framework::Drawable::Style::Visible);
- Framework::ToolTip* tip = new Framework::ToolTip(window->zScreen());
- tip->addStyle(Framework::DrawableBackground::Style::Background
- | Framework::DrawableBackground::Style::BAlpha
- | Framework::DrawableBackground::Style::Border
- | Framework::DrawableBackground::Style::Visible);
- tip->setBackgroundColor(0xA0000000);
- tip->setBorderColor(0xFFFFFFFF);
- tip->setBorderWidth(1);
- toolTip = uiFactory.createTextField(uiFactory.initParam);
- toolTip->setText("");
- toolTip->setSize(0, 0);
- toolTip->addStyle(Framework::TextField::Style::Multiline);
- tip->addMember(toolTip);
- tip->setWarten(0.5);
- setToolTipZ(tip);
- }
- void RecipieIngredient::addPossibleItem(
- Framework::Image* icon, Framework::Text* toolTip, int typeId)
- {
- icons.add(icon);
- toolTips.add(toolTip);
- itemTypes.add(typeId);
- if (toolTips.getEntryCount() == 1)
- {
- this->toolTip->setText(toolTips.z(0)->getText());
- this->toolTip->setSize(
- this->toolTip->getNeededWidth(), this->toolTip->getNeededHeight());
- }
- }
- bool RecipieIngredient::tick(double tickVal)
- {
- if (!zToolTip()->isVisible())
- {
- timtUntilNextIcon -= tickVal;
- if (timtUntilNextIcon <= 0)
- {
- timtUntilNextIcon = 2.0;
- currentIconIndex++;
- if (currentIconIndex >= icons.getEntryCount())
- {
- currentIconIndex = 0;
- }
- if (toolTips.getEntryCount() > 0)
- {
- toolTip->setText(toolTips.z(currentIconIndex)->getText());
- toolTip->setSize(
- toolTip->getNeededWidth(), toolTip->getNeededHeight());
- rend = 1;
- }
- }
- }
- return DrawableBackground::tick(tickVal);
- }
- void RecipieIngredient::render(Framework::Image& rObj)
- {
- DrawableBackground::render(rObj);
- if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
- TextRenderer tr;
- tr.setFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
- tr.setFontSize(12);
- rObj.fillRegion(0, 0, 50, 50, 0xFF222222);
- if (icons.getEntryCount() > 0)
- rObj.alphaImage(0, 0, 50, 50, *icons.z(currentIconIndex));
- const char* units[] = {"", "K", "M", "G", "T", "P"};
- int i = 0;
- int tmpCount = amount;
- for (; i < 6 && tmpCount > 1024; i++)
- tmpCount = tmpCount / 1024;
- Text count = tmpCount;
- count += units[i];
- tr.renderText(45 - tr.getTextWidth(count),
- 45 - tr.getTextHeight(count),
- count,
- rObj,
- 0xFFFFFFFF);
- rObj.releaseDrawOptions();
- }
- void RecipieIngredient::doMouseEvent(Framework::MouseEvent& me, bool userRet)
- {
- if (me.id == ME_RLeft)
- {
- if (itemTypes.getEntryCount() > currentIconIndex)
- {
- World::INSTANCE->zClient()->craftingUIMLRequest(
- itemTypes.get(currentIconIndex));
- }
- }
- DrawableBackground::doMouseEvent(me, userRet);
- }
- Requirement::Requirement(
- Framework::Text attribute, Framework::Text value, AttributeOperator op)
- : Framework::ReferenceCounter(),
- attribute(attribute),
- value(value),
- op(op)
- {}
- void Requirement::negate()
- {
- switch (op)
- {
- case AttributeOperator::Equals:
- op = AttributeOperator::NotEquals;
- break;
- case AttributeOperator::NotEquals:
- op = AttributeOperator::Equals;
- break;
- case AttributeOperator::GreaterThan:
- op = AttributeOperator::LessThanOrEquals;
- break;
- case AttributeOperator::GreaterThanOrEquals:
- op = AttributeOperator::LessThan;
- break;
- case AttributeOperator::LessThan:
- op = AttributeOperator::GreaterThanOrEquals;
- break;
- case AttributeOperator::LessThanOrEquals:
- op = AttributeOperator::GreaterThan;
- break;
- }
- }
- Requirement* Requirement::clone()
- {
- return new Requirement(attribute, value, op);
- }
- bool Requirement::contradicts(Requirement* zOther)
- {
- if (zOther->attribute.isEqual(attribute.getText()))
- {
- if (zOther->value.isEqual(value))
- {
- if (op == AttributeOperator::Equals
- && zOther->op == AttributeOperator::NotEquals)
- {
- return 1;
- }
- if (op == AttributeOperator::NotEquals
- && zOther->op == AttributeOperator::Equals)
- {
- return 1;
- }
- if (op == AttributeOperator::GreaterThan
- && zOther->op == AttributeOperator::LessThan)
- {
- return 1;
- }
- if (op == AttributeOperator::LessThan
- && zOther->op == AttributeOperator::GreaterThan)
- {
- return 1;
- }
- }
- if (op == AttributeOperator::LessThan
- || op == AttributeOperator::LessThanOrEquals
- || op == AttributeOperator::GreaterThan
- || op == AttributeOperator::GreaterThanOrEquals)
- {
- double v1 = (double)value;
- double v2 = (double)zOther->value;
- if (op == AttributeOperator::LessThan
- || op == AttributeOperator::LessThanOrEquals)
- {
- if (v1 < v2)
- {
- if (zOther->op == AttributeOperator::GreaterThan
- || zOther->op == AttributeOperator::GreaterThanOrEquals
- || zOther->op == AttributeOperator::Equals)
- {
- return 1;
- }
- }
- }
- else if (op == AttributeOperator::GreaterThan
- || op == AttributeOperator::GreaterThanOrEquals)
- {
- if (v1 > v2)
- {
- if (zOther->op == AttributeOperator::LessThan
- || zOther->op == AttributeOperator::LessThanOrEquals
- || zOther->op == AttributeOperator::Equals)
- {
- return 1;
- }
- }
- }
- }
- if (zOther->op == AttributeOperator::LessThan
- || zOther->op == AttributeOperator::LessThanOrEquals
- || zOther->op == AttributeOperator::GreaterThan
- || zOther->op == AttributeOperator::GreaterThanOrEquals)
- {
- double v1 = (double)zOther->value;
- double v2 = (double)value;
- if (zOther->op == AttributeOperator::LessThan
- || zOther->op == AttributeOperator::LessThanOrEquals)
- {
- if (v1 < v2)
- {
- if (op == AttributeOperator::GreaterThan
- || op == AttributeOperator::GreaterThanOrEquals
- || op == AttributeOperator::Equals)
- {
- return 1;
- }
- }
- }
- else if (zOther->op == AttributeOperator::GreaterThan
- || zOther->op == AttributeOperator::GreaterThanOrEquals)
- {
- if (v1 > v2)
- {
- if (op == AttributeOperator::LessThan
- || op == AttributeOperator::LessThanOrEquals
- || op == AttributeOperator::Equals)
- {
- return 1;
- }
- }
- }
- }
- }
- return 0;
- }
- bool Requirement::merge(Requirement* zOther)
- {
- if (zOther->attribute.isEqual(attribute.getText()))
- {
- if (zOther->value.isEqual(value))
- {
- if (op == zOther->op)
- {
- return 1;
- }
- }
- else
- {
- if (op == AttributeOperator::Equals
- && zOther->op == AttributeOperator::NotEquals)
- {
- return 1;
- }
- if (op == AttributeOperator::NotEquals
- && zOther->op == AttributeOperator::Equals)
- {
- op = AttributeOperator::Equals;
- value = zOther->value;
- return 1;
- }
- }
- if (op == AttributeOperator::LessThan
- || op == AttributeOperator::LessThanOrEquals
- || op == AttributeOperator::GreaterThan
- || op == AttributeOperator::GreaterThanOrEquals)
- {
- double v1 = (double)value;
- double v2 = (double)zOther->value;
- if (op == AttributeOperator::LessThan
- || op == AttributeOperator::LessThanOrEquals)
- {
- if (zOther->op == AttributeOperator::LessThan
- || zOther->op == AttributeOperator::LessThanOrEquals)
- {
- if (v1 > v2
- || (v1 == v2
- && op == AttributeOperator::LessThanOrEquals))
- {
- op = zOther->op;
- value = zOther->value;
- }
- return 1;
- }
- if (zOther->op == AttributeOperator::Equals)
- {
- if (v2 < v1
- || (v2 == v1
- && op == AttributeOperator::LessThanOrEquals))
- {
- op = zOther->op;
- value = zOther->value;
- return 1;
- }
- }
- if (zOther->op == AttributeOperator::NotEquals)
- {
- if (v2 > v1
- || (v2 == v1 && op == AttributeOperator::LessThan))
- {
- return 1;
- }
- }
- }
- else if (op == AttributeOperator::GreaterThan
- || op == AttributeOperator::GreaterThanOrEquals)
- {
- if (zOther->op == AttributeOperator::GreaterThan
- || zOther->op == AttributeOperator::GreaterThanOrEquals)
- {
- if (v1 < v2
- || (v1 == v2
- && op == AttributeOperator::GreaterThanOrEquals))
- {
- op = zOther->op;
- value = zOther->value;
- }
- return 1;
- }
- if (zOther->op == AttributeOperator::Equals)
- {
- if (v2 > v1
- || (v2 == v1
- && op == AttributeOperator::GreaterThanOrEquals))
- {
- op = zOther->op;
- value = zOther->value;
- return 1;
- }
- }
- if (zOther->op == AttributeOperator::NotEquals)
- {
- if (v2 < v1
- || (v2 == v1 && op == AttributeOperator::GreaterThan))
- {
- return 1;
- }
- }
- }
- }
- if (zOther->op == AttributeOperator::LessThan
- || zOther->op == AttributeOperator::LessThanOrEquals
- || zOther->op == AttributeOperator::GreaterThan
- || zOther->op == AttributeOperator::GreaterThanOrEquals)
- {
- double v1 = (double)zOther->value;
- double v2 = (double)value;
- if (zOther->op == AttributeOperator::LessThan
- || zOther->op == AttributeOperator::LessThanOrEquals)
- {
- if (op == AttributeOperator::Equals)
- {
- if (v2 < v1
- || (v2 == v1
- && zOther->op
- == AttributeOperator::LessThanOrEquals))
- {
- return 1;
- }
- }
- if (op == AttributeOperator::NotEquals)
- {
- if (v2 > v1
- || (v2 == v1
- && zOther->op == AttributeOperator::LessThan))
- {
- op = zOther->op;
- value = zOther->value;
- return 1;
- }
- }
- }
- else if (zOther->op == AttributeOperator::GreaterThan
- || zOther->op == AttributeOperator::GreaterThanOrEquals)
- {
- if (op == AttributeOperator::Equals)
- {
- if (v2 > v1
- || (v2 == v1
- && zOther->op
- == AttributeOperator::GreaterThanOrEquals))
- {
- return 1;
- }
- }
- if (op == AttributeOperator::NotEquals)
- {
- if (v2 < v1
- || (v2 == v1
- && zOther->op == AttributeOperator::GreaterThan))
- {
- op = zOther->op;
- value = zOther->value;
- return 1;
- }
- }
- }
- }
- }
- return 0;
- }
- int Requirement::getItemType()
- {
- if (attribute.isEqual("Type") && op == AttributeOperator::Equals)
- {
- return (int)value;
- }
- return -1;
- }
- Framework::Text Requirement::getDescription()
- {
- Framework::Text result = attribute;
- result += " ";
- switch (op)
- {
- case AttributeOperator::Equals:
- result += "is";
- break;
- case AttributeOperator::NotEquals:
- result += "is not";
- break;
- case AttributeOperator::LessThan:
- result += "is less than";
- break;
- case AttributeOperator::LessThanOrEquals:
- result += "is less than or equal to";
- break;
- case AttributeOperator::GreaterThan:
- result += "is greater than";
- break;
- case AttributeOperator::GreaterThanOrEquals:
- result += "is greater than or equal to";
- break;
- }
- result += " ";
- result += value;
- return result;
- }
- RequirementSet::RequirementSet()
- : Framework::ReferenceCounter()
- {}
- void RequirementSet::addRequirement(Requirement* zReq)
- {
- for (Requirement* req : requirements)
- {
- if (req->merge(zReq)) return;
- }
- requirements.add(zReq->clone());
- }
- RequirementSet* RequirementSet::clone()
- {
- RequirementSet* result = new RequirementSet();
- for (Requirement* req : requirements)
- {
- result->requirements.add(req->clone());
- }
- return result;
- }
- void RequirementSet::addRequirements(RequirementSet* zOther)
- {
- for (Requirement* req : zOther->requirements)
- {
- addRequirement(req);
- }
- }
- void RequirementSet::negate(LogicTree* zNode)
- {
- for (Requirement* req : requirements)
- {
- Requirement* negate = req->clone();
- negate->negate();
- RequirementSet* set = new RequirementSet();
- set->addRequirement(negate);
- negate->release();
- zNode->addChildren(new LogicTree(LogicalOperator::OR, set));
- }
- }
- bool RequirementSet::isNoneMatch() const
- {
- for (Requirement* req : requirements)
- {
- for (Requirement* req2 : requirements)
- {
- if (req != req2)
- {
- if (req->contradicts(req2))
- {
- return 1;
- }
- }
- }
- }
- return 0;
- }
- bool RequirementSet::isAnyMatch() const
- {
- return requirements.getEntryCount() == 0;
- }
- Framework::Text RequirementSet::renderToTooltip() const
- {
- int itemType = -1;
- for (Requirement* req : requirements)
- {
- int rT = req->getItemType();
- if (rT != -1)
- {
- if (itemType == -1)
- {
- itemType = rT;
- }
- else if (itemType != rT)
- {
- itemType = -2;
- }
- }
- }
- Framework::Text result = "";
- if (itemType == -2)
- {
- return "No Item matches this filter";
- }
- else if (itemType == -1)
- {
- result += "Any Item";
- }
- else
- {
- result += zItemType(itemType)->getName();
- }
- if (requirements.getEntryCount() > 0)
- {
- bool first = 1;
- for (Requirement* req : requirements)
- {
- if (req->getItemType() == -1)
- {
- if (first)
- {
- result += ":\n";
- first = 0;
- }
- result += " ";
- result += req->getDescription();
- result += "\n";
- }
- }
- }
- return result;
- }
- Framework::Image* RequirementSet::getIcon() const
- {
- int itemType = -1;
- for (Requirement* req : requirements)
- {
- int rT = req->getItemType();
- if (rT != -1)
- {
- if (itemType == -1)
- {
- itemType = rT;
- }
- else if (itemType != rT)
- {
- itemType = -2;
- }
- }
- }
- if (itemType == -2)
- {
- LTDBFile dat;
- dat.setFile(new Text("data/images/gui_icons.ltdb"));
- dat.readData(0);
- return dat.load(0, new Text("noitem.png"));
- }
- else if (itemType == -1)
- {
- LTDBFile dat;
- dat.setFile(new Text("data/images/gui_icons.ltdb"));
- dat.readData(0);
- return dat.load(0, new Text("anyitem.png"));
- }
- else
- {
- return dynamic_cast<Framework::Image*>(
- zItemType(itemType)->zIcon()->getThis());
- }
- }
- int RequirementSet::getItemType() const
- {
- int itemType = -1;
- for (Requirement* req : requirements)
- {
- int rT = req->getItemType();
- if (rT != -1)
- {
- if (itemType == -1)
- {
- itemType = rT;
- }
- else if (itemType != rT)
- {
- itemType = -2;
- }
- }
- }
- return itemType;
- }
- LogicTree::LogicTree(LogicalOperator op, RequirementSet* set)
- : Framework::ReferenceCounter(),
- op(op),
- requirementSet(set)
- {}
- LogicTree::~LogicTree()
- {
- if (requirementSet) requirementSet->release();
- }
- Framework::RCArray<RequirementSet>* LogicTree::multiply(
- Framework::RCArray<RequirementSet>* a,
- Framework::RCArray<RequirementSet>* b)
- {
- Framework::RCArray<RequirementSet>* result
- = new Framework::RCArray<RequirementSet>();
- for (RequirementSet* aSet : *a)
- {
- for (RequirementSet* bSet : *b)
- {
- RequirementSet* set = aSet->clone();
- set->addRequirements(bSet);
- if (!set->isNoneMatch())
- {
- if (set->isAnyMatch())
- {
- result->clear();
- result->add(set);
- a->release();
- b->release();
- return result;
- }
- else
- {
- result->add(set);
- }
- }
- else
- {
- set->release();
- }
- }
- }
- return result;
- }
- void LogicTree::addChildren(LogicTree* tree)
- {
- children.add(tree);
- }
- LogicTree* LogicTree::clone()
- {
- LogicTree* result = new LogicTree(op, requirementSet->clone());
- for (LogicTree* child : children)
- {
- result->children.add(child->clone());
- }
- return result;
- }
- void LogicTree::negate()
- {
- if (requirementSet != 0)
- {
- requirementSet->negate(this);
- requirementSet->release();
- requirementSet = 0;
- }
- else
- {
- if (op == LogicalOperator::AND)
- {
- op = LogicalOperator::OR;
- }
- else if (op == LogicalOperator::OR)
- {
- op = LogicalOperator::AND;
- }
- for (LogicTree* child : children)
- {
- child->negate();
- }
- }
- }
- Framework::RCArray<RequirementSet>* LogicTree::resolve()
- {
- if (requirementSet != 0)
- {
- Framework::RCArray<RequirementSet>* result
- = new Framework::RCArray<RequirementSet>();
- result->add(dynamic_cast<RequirementSet*>(requirementSet->getThis()));
- return result;
- }
- else
- {
- Framework::RCArray<RequirementSet>* result = 0;
- if (op == LogicalOperator::OR)
- {
- result = new Framework::RCArray<RequirementSet>();
- for (LogicTree* child : children)
- {
- Framework::RCArray<RequirementSet>* childSet = child->resolve();
- for (RequirementSet* set : *childSet)
- {
- result->add(dynamic_cast<RequirementSet*>(set->getThis()));
- }
- childSet->release();
- }
- }
- else if (op == LogicalOperator::AND)
- {
- result = children.z(0)->resolve();
- for (int i = 1; i < children.getEntryCount(); i++)
- {
- Framework::RCArray<RequirementSet>* childSet
- = children.z(i)->resolve();
- result = multiply(result, childSet);
- }
- }
- if (result != 0)
- {
- for (int i = 0; i < result->getEntryCount(); i++)
- {
- if (result->z(i)->isNoneMatch())
- {
- result->remove(i);
- i--;
- }
- else if (result->z(i)->isAnyMatch())
- {
- Framework::RCArray<RequirementSet>* anyMatch
- = new Framework::RCArray<RequirementSet>();
- anyMatch->add(result->get(i));
- result->release();
- return anyMatch;
- }
- }
- }
- return result;
- }
- }
|