aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libparser/node/explodedicenode.cpp
blob: 15468833b8b4542adac2ca384002abf911da3c09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "explodedicenode.h"
#include "validatorlist.h"

ExplodeDiceNode::ExplodeDiceNode() : m_diceResult(new DiceResult())
{
    m_result= m_diceResult;
}
void ExplodeDiceNode::run(ExecutionNode* previous)
{
    m_previousNode= previous;
    if((nullptr != previous) && (nullptr != previous->getResult()))
    {
        DiceResult* previous_result= dynamic_cast<DiceResult*>(previous->getResult());
        m_result->setPrevious(previous_result);
        if(nullptr != previous_result)
        {
            Die* exampleDie;
            for(auto& die : previous_result->getResultList())
            {
                Die* tmpdie= new Die(*die);
                m_diceResult->insertResult(tmpdie);
                die->displayed();
                exampleDie= tmpdie;
            }

            // QList<Die*> list= m_diceResult->getResultList();

            bool hasExploded= false;
            std::function<void(Die*, qint64)> f= [&hasExploded, this](Die* die, qint64) {
                if(Dice::CONDITION_STATE::ALWAYSTRUE
                   == m_validatorList->isValidRangeSize(
                          std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue())))
                {
                    m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR,
                                    QObject::tr("Condition (%1) cause an endless loop with this dice: %2")
                                        .arg(toString(true))
                                        .arg(QStringLiteral("d[%1,%2]")
                                                 .arg(static_cast<int>(die->getBase()))
                                                 .arg(static_cast<int>(die->getMaxValue()))));
                }
                hasExploded= true;
                die->roll(true);
            };
            do
            {
                hasExploded= false;
                m_validatorList->validResult(m_diceResult, false, false, f);
            } while(hasExploded);

            /*for(auto& die : list)
            {
                if(Dice::CONDITION_STATE::ALWAYSTRUE
                   == m_validatorList->isValidRangeSize(
                          std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue())))
                {

                    continue;
                }

                while(m_validatorList->hasValid(die, false))
                {
                    die->roll(true);
                }
            }*/

            if(nullptr != m_nextNode)
            {
                m_nextNode->run(this);
            }
        }
    }
}
ExplodeDiceNode::~ExplodeDiceNode()
{
    if(nullptr != m_validatorList)
    {
        delete m_validatorList;
    }
}
void ExplodeDiceNode::setValidatorList(ValidatorList* val)
{
    m_validatorList= val;
}
QString ExplodeDiceNode::toString(bool withlabel) const
{
    if(withlabel)
    {
        return QString("%1 [label=\"ExplodeDiceNode %2\"]").arg(m_id, m_validatorList->toString());
    }
    else
    {
        return m_id;
    }
}
qint64 ExplodeDiceNode::getPriority() const
{
    qint64 priority= 0;
    if(nullptr != m_previousNode)
    {
        priority= m_previousNode->getPriority();
    }
    return priority;
}

ExecutionNode* ExplodeDiceNode::getCopy() const
{
    ExplodeDiceNode* node= new ExplodeDiceNode();
    if(nullptr != m_validatorList)
    {
        node->setValidatorList(m_validatorList->getCopy());
    }
    if(nullptr != m_nextNode)
    {
        node->setNextNode(m_nextNode->getCopy());
    }
    return node;
}