File indexing completed on 2024-04-28 05:37:00
0001 /*************************************************************************** 0002 * Copyright (C) 2014 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * This file is part of DiceParser * 0006 * * 0007 * DiceParser is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0015 * GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License * 0018 * along with this program; if not, write to the * 0019 * Free Software Foundation, Inc., * 0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0021 ***************************************************************************/ 0022 0023 #include "die.h" 0024 0025 #include <QDateTime> 0026 #include <QDebug> 0027 #include <QUuid> 0028 #include <algorithm> 0029 #include <array> 0030 #include <chrono> 0031 0032 void Die::buildSeed() 0033 { 0034 static bool init= false; 0035 if(init) 0036 return; 0037 std::array<int, std::mt19937::state_size> seed_data; 0038 std::random_device r; 0039 std::generate_n(seed_data.data(), seed_data.size(), std::ref(r)); 0040 std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); 0041 s_rng= std::mt19937(seq); 0042 init= true; 0043 } 0044 0045 std::mt19937 Die::s_rng; 0046 0047 Die::Die() 0048 : m_uuid(QUuid::createUuid().toString(QUuid::WithoutBraces)) 0049 , m_hasValue(false) 0050 , m_displayStatus(false) 0051 , m_highlighted(true) 0052 , m_base(1) 0053 , m_color("") 0054 , m_op(Dice::ArithmeticOperator::PLUS) //,m_mt(m_randomDevice) 0055 { 0056 buildSeed(); 0057 } 0058 0059 Die::Die(const Die& die) 0060 { 0061 m_value= die.m_value; 0062 m_rollResult= die.m_rollResult; 0063 m_selected= die.m_selected; 0064 m_hasValue= die.m_hasValue; 0065 m_uuid= die.m_uuid; 0066 m_displayStatus= die.m_displayStatus; 0067 m_maxValue= die.m_maxValue; 0068 m_highlighted= die.m_highlighted; 0069 m_base= die.m_base; 0070 m_color= die.getColor(); 0071 m_op= die.getOp(); 0072 // auto seed= std::chrono::high_resolution_clock::now().time_since_epoch().count(); 0073 // m_rng= std::mt19937(quintptr(this) + static_cast<unsigned long long>(seed)); 0074 } 0075 0076 void Die::setValue(qint64 r) 0077 { 0078 m_value= r; 0079 m_hasValue= true; 0080 } 0081 0082 void Die::insertRollValue(qint64 r) 0083 { 0084 m_rollResult.append(r); 0085 } 0086 0087 void Die::setSelected(bool b) 0088 { 0089 m_selected= b; 0090 } 0091 0092 bool Die::isSelected() const 0093 { 0094 return m_selected; 0095 } 0096 qint64 Die::getValue() const 0097 { 0098 if(m_hasValue) 0099 { 0100 return m_value; 0101 } 0102 else 0103 { 0104 qint64 value= 0; 0105 int i= 0; 0106 for(qint64 tmp : m_rollResult) 0107 { 0108 if(i > 0) 0109 { 0110 switch(m_op) 0111 { 0112 case Dice::ArithmeticOperator::PLUS: 0113 value+= tmp; 0114 break; 0115 case Dice::ArithmeticOperator::MULTIPLICATION: 0116 value*= tmp; 0117 break; 0118 case Dice::ArithmeticOperator::MINUS: 0119 value-= tmp; 0120 break; 0121 case Dice::ArithmeticOperator::INTEGER_DIVIDE: 0122 case Dice::ArithmeticOperator::DIVIDE: 0123 if(tmp != 0) 0124 { 0125 value/= tmp; 0126 } 0127 else 0128 { 0129 // error(); 0130 } 0131 break; 0132 case Dice::ArithmeticOperator::POW: 0133 value= static_cast<qint64>(std::pow(value, tmp)); 0134 break; 0135 } 0136 } 0137 else 0138 { 0139 value= tmp; 0140 } 0141 ++i; 0142 } 0143 return value; 0144 } 0145 } 0146 QList<qint64> Die::getListValue() const 0147 { 0148 return m_rollResult; 0149 } 0150 bool Die::hasChildrenValue() 0151 { 0152 return m_rollResult.size() > 1 ? true : false; 0153 } 0154 void Die::replaceLastValue(qint64 value) 0155 { 0156 m_rollResult.removeLast(); 0157 insertRollValue(value); 0158 } 0159 0160 void Die::roll(bool adding) 0161 { 0162 if(m_maxValue != 0) 0163 { 0164 // quint64 value=(qrand()%m_faces)+m_base; 0165 std::uniform_int_distribution<qint64> dist(m_base, m_maxValue); 0166 qint64 value= dist(s_rng); 0167 if((adding) || (m_rollResult.isEmpty())) 0168 { 0169 insertRollValue(value); 0170 } 0171 else 0172 { 0173 replaceLastValue(value); 0174 } 0175 } 0176 } 0177 0178 quint64 Die::getFaces() const 0179 { 0180 return std::abs(m_maxValue - m_base) + 1; 0181 } 0182 qint64 Die::getLastRolledValue() 0183 { 0184 if(!m_rollResult.isEmpty()) 0185 { 0186 return m_rollResult.last(); 0187 } 0188 else 0189 return 0; 0190 } 0191 bool Die::hasBeenDisplayed() const 0192 { 0193 return m_displayStatus; 0194 } 0195 void Die::displayed() 0196 { 0197 setDisplayed(true); 0198 } 0199 void Die::setDisplayed(bool b) 0200 { 0201 m_displayStatus= b; 0202 } 0203 void Die::setHighlighted(bool a) 0204 { 0205 m_highlighted= a; 0206 } 0207 0208 bool Die::isHighlighted() const 0209 { 0210 return m_highlighted; 0211 } 0212 void Die::setBase(qint64 base) 0213 { 0214 m_base= base; 0215 } 0216 qint64 Die::getBase() 0217 { 0218 return m_base; 0219 } 0220 QString Die::getColor() const 0221 { 0222 return m_color; 0223 } 0224 0225 void Die::setColor(const QString& color) 0226 { 0227 m_color= color; 0228 } 0229 0230 qint64 Die::getMaxValue() const 0231 { 0232 return m_maxValue; 0233 } 0234 0235 void Die::setMaxValue(const qint64& maxValue) 0236 { 0237 m_maxValue= maxValue; 0238 } 0239 0240 Dice::ArithmeticOperator Die::getOp() const 0241 { 0242 return m_op; 0243 } 0244 0245 void Die::setOp(const Dice::ArithmeticOperator& op) 0246 { 0247 m_op= op; 0248 } 0249 QString Die::getUuid() const 0250 { 0251 return m_uuid; 0252 } 0253 0254 void Die::setUuid(const QString& uuid) 0255 { 0256 m_uuid= uuid; 0257 }