aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/node/listsetrollnode.cpp
blob: 2cee64536a65d43c5538bfd2bf24168e611e1781 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*************************************************************************
 *   Copyright (C) 2009 by Renaud Guezennec                              *
 *                                                                       *
 *   https://rolisteam.org/                                           *
 *                                                                       *
 *   rolisteam is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published   *
 *   by the Free Software Foundation; either version 2 of the License,   *
 *   or (at your option) any later version.                              *
 *                                                                       *
 *   This program is distributed in the hope that it will be useful,     *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
 *   GNU General Public License for more details.                        *
 *                                                                       *
 *   You should have received a copy of the GNU General Public License   *
 *   along with this program; if not, write to the                       *
 *   Free Software Foundation, Inc.,                                     *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
 *************************************************************************/
#include "listsetrollnode.h"
#include "die.h"

#include <QDebug>

ListSetRollNode::ListSetRollNode() : m_diceResult(new DiceResult()), m_stringResult(new StringResult()), m_unique(false)
{
    m_result= m_stringResult;
}
ListSetRollNode::~ListSetRollNode()
{
    if(nullptr != m_diceResult)
    {
        delete m_diceResult;
        m_diceResult= nullptr;
    }
}

QStringList ListSetRollNode::getList() const
{
    return m_values;
}
QString ListSetRollNode::toString(bool wl) const
{
    if(wl)
    {
        return QString("%1 [label=\"ListSetRoll list:%2\"]").arg(m_id, m_values.join(","));
    }
    else
    {
        return m_id;
    }
}
qint64 ListSetRollNode::getPriority() const
{
    qint64 priority= 4;
    return priority;
}
void ListSetRollNode::run(ExecutionNode* previous)
{
    m_previousNode= previous;
    if(nullptr != previous)
    {
        Result* result= previous->getResult();
        if(nullptr != result)
        {
            quint64 diceCount= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal();
            if(diceCount > static_cast<quint64>(m_values.size()) && m_unique)
            {
                m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE,
                                QObject::tr("More unique values asked than possible values (L operator)"));
            }
            else
            {
                m_result->setPrevious(result);
                for(quint64 i= 0; i < diceCount; ++i)
                {
                    QStringList rollResult;
                    Die* die= new Die();
                    computeFacesNumber(die);
                    die->roll();
                    m_diceResult->insertResult(die);
                    getValueFromDie(die, rollResult);
                    for(auto const& str : qAsConst(rollResult))
                    {
                        m_stringResult->addText(str);
                    }
                }
                m_stringResult->finished();
            }
            if(nullptr != m_nextNode)
            {
                m_nextNode->run(this);
            }
        }
    }
}
void ListSetRollNode::setListValue(QStringList lirs)
{
    m_values= lirs;
}
void ListSetRollNode::setUnique(bool u)
{
    m_unique= u;
}
void ListSetRollNode::setNoComma(bool b)
{
    if(m_stringResult)
        m_stringResult->setNoComma(b);
}
void ListSetRollNode::setRangeList(QList<Range>& ranges)
{
    m_rangeList= ranges;
}
void ListSetRollNode::computeFacesNumber(Die* die)
{
    if(m_rangeList.isEmpty())
    {
        die->setMaxValue(m_values.size());
    }
    else
    {
        Q_ASSERT(m_values.size() == m_rangeList.size());
        qint64 max;
        int i= 0;
        for(Range& range : m_rangeList)
        {
            if(((i == 0) || (max < range.getEnd())) && (range.isFullyDefined()))
            {
                max= range.getEnd();
            }
            ++i;
        }
        die->setMaxValue(max);
    }
}
void ListSetRollNode::getValueFromDie(Die* die, QStringList& rollResult)
{
    if(m_rangeList.isEmpty())
    {
        if(die->getValue() - 1 < m_values.size())
        {
            auto str= m_values[die->getValue() - 1];
            while(m_unique && rollResult.contains(str))
            {
                die->roll(false);
                str= m_values[die->getValue() - 1];
            }
            rollResult << str;
        }
    }
    else
    {
        Q_ASSERT(m_values.size() == m_rangeList.size());
        bool found= false;
        while(!found)
        {
            int i= 0;
            for(Range& range : m_rangeList)
            {
                auto it= std::find(m_rangeIndexResult.begin(), m_rangeIndexResult.end(), i);
                auto isValid= range.hasValid(die, false);
                if((isValid && !m_unique) || (isValid && it == m_rangeIndexResult.end()))
                {
                    m_rangeIndexResult.push_back(i);
                    rollResult << m_values[i];
                    found= true;
                }
                ++i;
            }
            if(!found)
            {
                die->roll(false);
            }
        }
    }
}
ExecutionNode* ListSetRollNode::getCopy() const
{
    ListSetRollNode* node= new ListSetRollNode();
    QList<Range> dataList= m_rangeList;
    node->setRangeList(dataList);
    node->setUnique(m_unique);
    node->setListValue(m_values);
    if(nullptr != m_nextNode)
    {
        node->setNextNode(m_nextNode->getCopy());
    }
    return node;
}