diff options
| -rw-r--r-- | diceparser.cpp | 9 | ||||
| -rw-r--r-- | node/listsetrollnode.cpp | 74 | ||||
| -rw-r--r-- | node/listsetrollnode.h | 11 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 94 | ||||
| -rw-r--r-- | parsingtoolbox.h | 12 | ||||
| -rw-r--r-- | range.cpp | 27 | ||||
| -rw-r--r-- | range.h | 9 |
7 files changed, 215 insertions, 21 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index 570c96b..4f0645e 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -555,9 +555,16 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) else if(currentOperator ==L) { QStringList list; - if(m_parsingToolbox->readList(str,list)) + QList<Range> listRange; + ParsingToolBox::LIST_OPERATOR op = m_parsingToolbox->readListOperator(str); + if(m_parsingToolbox->readList(str,list,listRange)) { ListSetRollNode* lsrNode = new ListSetRollNode(); + lsrNode->setRangeList(listRange); + if(op == ParsingToolBox::UNIQUE) + { + lsrNode->setUnique(true); + } lsrNode->setListValue(list); node = lsrNode; return true; diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index fdc17a5..c7f2c45 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -22,7 +22,7 @@ #include "die.h" ListSetRollNode::ListSetRollNode() - :m_diceResult(new DiceResult()),m_stringResult(new StringResult()) + :m_diceResult(new DiceResult()),m_stringResult(new StringResult()),m_unique(false) { m_result = m_stringResult; } @@ -46,12 +46,6 @@ QString ListSetRollNode::toString() const qint64 ListSetRollNode::getPriority() const { qint64 priority=4; -// if(NULL!=m_nextNode) -// { -// priority = m_nextNode->getPriority(); -// } - - return priority; } void ListSetRollNode::run(ExecutionNode* previous) @@ -68,13 +62,11 @@ void ListSetRollNode::run(ExecutionNode* previous) for(quint64 i=0; i < diceCount ; ++i) { Die* die = new Die(); - die->setFaces(m_values.size()); + //die->setFaces(m_values.size()); + computeFacesNumber(die); die->roll(); m_diceResult->insertResult(die); - if(die->getValue()-1<m_values.size()) - { - rollResult << m_values[die->getValue()-1]; - } + getValueFromDie(die,rollResult); } m_stringResult->setText(rollResult.join(",")); if(NULL!=m_nextNode) @@ -83,11 +75,63 @@ void ListSetRollNode::run(ExecutionNode* previous) } } } - - - } void ListSetRollNode::setListValue(QStringList lirs) { m_values = lirs; } +void ListSetRollNode::setUnique(bool u) +{ + m_unique = u; +} +void ListSetRollNode::setRangeList(QList<Range>& ranges) +{ + m_rangeList = ranges; +} +void ListSetRollNode::computeFacesNumber(Die* die) +{ + if(m_rangeList.isEmpty()) + { + die->setFaces(m_values.size()); + } + else + { + Q_ASSERT(m_values.size() == m_rangeList.size()); + qint64 max; + int i=0; + foreach(Range range, m_rangeList) + { + if((i==0)||(max<range.getEnd())) + { + max= range.getEnd(); + } + ++i; + } + die->setFaces(max); + } + +} +void ListSetRollNode::getValueFromDie(Die* die,QStringList& rollResult) +{ + if(m_rangeList.isEmpty()) + { + if(die->getValue()-1<m_values.size()) + { + rollResult << m_values[die->getValue()-1]; + } + } + else + { + Q_ASSERT(m_values.size() == m_rangeList.size()); + int i=0; + foreach (Range range, m_rangeList) + { + qDebug() << range.toString()<< die->getValue(); + if(range.hasValid(die,false)) + { + rollResult << m_values[i]; + } + ++i; + } + } +} diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 3102ade..4a11da2 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -27,6 +27,7 @@ #include "executionnode.h" #include "result/diceresult.h" #include "result/stringresult.h" +#include "range.h" /** * @brief The ListSetRollNode class is dedicated to pick up item from list. */ @@ -41,12 +42,20 @@ public: QStringList getList(); void setListValue(QStringList); + void setUnique(bool ); + void setRangeList(QList<Range>&); + + +private: + void getValueFromDie(Die* die,QStringList& rollResult); + void computeFacesNumber(Die* die); private: QStringList m_values; DiceResult* m_diceResult; StringResult* m_stringResult; - + bool m_unique; + QList<Range> m_rangeList; }; #endif // LISTSETROLLNODE_H diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index f6b1f12..696d330 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -179,17 +179,20 @@ bool ParsingToolBox::readCloseParentheses(QString& str) else return false; } -bool ParsingToolBox::readList(QString& str,QStringList& list) +bool ParsingToolBox::readList(QString& str,QStringList& list,QList<Range>& ranges) { if(str.startsWith("[")) { str=str.remove(0,1); - int pos = str.indexOf("]"); + int pos = str.lastIndexOf("]"); if(-1!=pos) { QString liststr = str.left(pos); list = liststr.split(","); str=str.remove(0,pos+1); + readProbability(list,ranges); + + return true; } } @@ -271,3 +274,90 @@ bool ParsingToolBox::readDiceRange(QString& str,int& start, int& end) } } +ParsingToolBox::LIST_OPERATOR ParsingToolBox::readListOperator(QString& str) +{ + + if(str.startsWith('u')) + { + return UNIQUE; + } + return NONE; +} +void ParsingToolBox::readProbability(QStringList& str,QList<Range>& ranges) +{ + quint64 totalDistance=0; + quint64 undefDistance = 0; + int undefCount=0; + int maxValue = 0; + int i=0; + int j=0; + //range + foreach(QString line,str) + { + int pos = line.indexOf('['); + if(-1!=pos) + { + QString range = line.right(line.length()-pos); + line = line.left(pos); + str[j]=line; + int start; + int end; + if(readDiceRange(range,start,end)) + { + Range range; + range.setValue(start,end); + ranges.append(range); + totalDistance += end-start+1; + ++i; + } + else + { + Range range; + range.setStart(start); + ranges.append(range); + ++undefCount; + undefDistance +=start; + } + if((end>maxValue)||(i==1)) + { + maxValue = end; + } + } + ++j; + + } + + + + + ///Normalize list +// qDebug() << 100 - undefDistance; +// qDebug() << totalDistance; + + qint64 totalDistPourcent = totalDistance * undefDistance / (100-undefDistance); + + if(totalDistPourcent<undefCount) + { + totalDistPourcent = undefCount; + } + + for(int i = 0; i< ranges.size(); ++i) + { + Range tmp = ranges.at(i); + if(!tmp.isFullyDefined()) + { + int dist = tmp.getStart(); + tmp.setStart(maxValue+1); + maxValue+=1; + double truc = undefDistance*1.0/dist; + + tmp.setEnd(maxValue+(truc*totalDistPourcent)); + maxValue = maxValue+(truc*totalDistPourcent); + qDebug() << truc << totalDistPourcent << undefDistance << totalDistance << maxValue << dist << totalDistPourcent << tmp.toString(); + ranges[i]=tmp; + } + } + + + +} diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 5d38919..cb97708 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -36,6 +36,7 @@ class ParsingToolBox { public: + enum LIST_OPERATOR {NONE,UNIQUE}; /** * @brief ParsingToolBox */ @@ -100,7 +101,7 @@ public: * @param list * @return */ - bool readList(QString& str,QStringList& list); + bool readList(QString& str,QStringList& list, QList<Range>& ranges); /** * @brief isValidValidator * @param previous @@ -123,6 +124,15 @@ public: * @return */ bool readDiceRange(QString& str,int& start, int& end); + /** + * @brief readListOperator + * @param str + * @return + */ + LIST_OPERATOR readListOperator(QString& str); + + void readProbability(QStringList& str,QList<Range>& ranges); + private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; @@ -22,6 +22,7 @@ #include "range.h" Range::Range() + : m_hasEnd(false),m_hasStart(false) { @@ -30,6 +31,9 @@ void Range::setValue(qint64 s,qint64 e) { m_start = s; m_end=e; + + m_hasEnd = true; + m_hasStart = true; } qint64 Range::hasValid(Die* m,bool recursive, bool unhighlight) const @@ -64,3 +68,26 @@ quint8 Range::getValidRangeSize(quint64 faces) const Q_UNUSED(faces); return m_end-m_start; } +void Range::setStart(qint64 start) +{ + m_start = start; + m_hasStart = true; +} +void Range::setEnd(qint64 end) +{ + m_end = end; + m_hasEnd = true; +} + +bool Range::isFullyDefined() +{ + return (m_hasEnd & m_hasStart); +} +qint64 Range::getStart() const +{ + return m_start; +} +qint64 Range::getEnd() const +{ + return m_end; +} @@ -33,15 +33,22 @@ class Range : public Validator public: Range(); void setValue(qint64,qint64); - + void setStart(qint64); + void setEnd(qint64); virtual qint64 hasValid(Die* b,bool recursive,bool unlight = false) const; virtual QString toString(); virtual quint8 getValidRangeSize(quint64 faces) const; + bool isFullyDefined(); + qint64 getStart() const; + qint64 getEnd() const; + private: qint64 m_start; qint64 m_end; + bool m_hasEnd; + bool m_hasStart; }; #endif // RANGE_H |