forked from Rolisteam/DiceParser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdie.cpp
220 lines (200 loc) · 5.13 KB
/
die.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/***************************************************************************
* Copyright (C) 2014 by Renaud Guezennec *
* http://www.rolisteam.org/contact *
* *
* This file is part of DiceParser *
* *
* DiceParser 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 "die.h"
#include <QDateTime>
#include <QDebug>
#include <chrono>
Die::Die()
: m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_base(1),m_color(""),m_op(Die::PLUS)//,m_mt(m_randomDevice)
{
// uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch();
// qsrand(seed);
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
m_rng = std::mt19937(quintptr(this)+seed);
}
Die::Die(const Die& die)
{
m_value = die.m_value;
m_rollResult = die.m_rollResult;
m_selected = die.m_selected;
m_hasValue = die.m_hasValue;
m_displayStatus = die.m_displayStatus;
m_maxValue = die.m_maxValue;
m_highlighted = die.m_highlighted;
m_base = die.m_base;
m_color = die.getColor();
m_op = die.getOp();
}
void Die::setValue(qint64 r)
{
m_value = r;
m_hasValue = true;
}
void Die::insertRollValue(qint64 r)
{
m_rollResult.append(r);
}
void Die::setSelected(bool b)
{
m_selected = b;
}
bool Die::isSelected() const
{
return m_selected;
}
qint64 Die::getValue() const
{
if(m_hasValue)
{
return m_value;
}
else
{
qint64 value=0;
int i = 0;
for(qint64 tmp : m_rollResult)
{
if(i>0)
{
switch(m_op)
{
case PLUS:
value+=tmp;
break;
case MULTIPLICATION:
value*=tmp;
break;
case MINUS:
value-=tmp;
break;
case DIVIDE:
if(tmp!=0)
{
value/=tmp;
}
else
{
//error();
}
break;
}
}
else
{
value=tmp;
}
++i;
}
return value;
}
}
QList<qint64> Die::getListValue() const
{
return m_rollResult;
}
bool Die::hasChildrenValue()
{
return m_rollResult.size()>1?true:false;
}
void Die::replaceLastValue(qint64 value)
{
m_rollResult.removeLast();
insertRollValue(value);
}
void Die::roll(bool adding)
{
if(m_maxValue!=0)
{
//quint64 value=(qrand()%m_faces)+m_base;
std::uniform_int_distribution<qint64> dist(m_base,m_maxValue);
qint64 value = dist(m_rng);
if((adding)||(m_rollResult.isEmpty()))
{
insertRollValue(value);
}
else
{
replaceLastValue(value);
}
}
}
quint64 Die::getFaces() const
{
return abs(m_maxValue-m_base)+1;
}
qint64 Die::getLastRolledValue()
{
if(!m_rollResult.isEmpty())
{
return m_rollResult.last();
}
else
return 0;
}
bool Die::hasBeenDisplayed() const
{
return m_displayStatus;
}
void Die::displayed()
{
m_displayStatus = true;
}
void Die::setHighlighted(bool a)
{
m_highlighted = a;
}
bool Die::isHighlighted() const
{
return m_highlighted;
}
void Die::setBase(qint64 base)
{
m_base = base;
}
qint64 Die::getBase()
{
return m_base;
}
QString Die::getColor() const
{
return m_color;
}
void Die::setColor(const QString &color)
{
m_color = color;
}
qint64 Die::getMaxValue() const
{
return m_maxValue;
}
void Die::setMaxValue(const qint64 &maxValue)
{
m_maxValue = maxValue;
}
Die::ArithmeticOperator Die::getOp() const
{
return m_op;
}
void Die::setOp(const Die::ArithmeticOperator &op)
{
m_op = op;
}