File indexing completed on 2024-05-12 05:39:29
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 "diceresult.h" 0024 #include <QDebug> 0025 0026 DiceResult::DiceResult() 0027 { 0028 m_resultTypes= (static_cast<int>(Dice::RESULT_TYPE::DICE_LIST) | static_cast<int>(Dice::RESULT_TYPE::SCALAR)); 0029 m_homogeneous= true; 0030 } 0031 void DiceResult::insertResult(Die* die) 0032 { 0033 m_diceValues.append(die); 0034 } 0035 QList<Die*>& DiceResult::getResultList() 0036 { 0037 return m_diceValues; 0038 } 0039 bool DiceResult::isHomogeneous() const 0040 { 0041 return m_homogeneous; 0042 } 0043 void DiceResult::setHomogeneous(bool b) 0044 { 0045 m_homogeneous= b; 0046 } 0047 0048 void DiceResult::setResultList(QList<Die*> list) 0049 { 0050 m_diceValues.erase( 0051 std::remove_if(m_diceValues.begin(), m_diceValues.end(), [list](Die* die) { return list.contains(die); }), 0052 m_diceValues.end()); 0053 0054 qDeleteAll(m_diceValues.begin(), m_diceValues.end()); 0055 m_diceValues.clear(); 0056 m_diceValues << list; 0057 } 0058 DiceResult::~DiceResult() 0059 { 0060 if(!m_diceValues.isEmpty()) 0061 { 0062 qDeleteAll(m_diceValues.begin(), m_diceValues.end()); 0063 m_diceValues.clear(); 0064 } 0065 } 0066 QVariant DiceResult::getResult(Dice::RESULT_TYPE type) 0067 { 0068 switch(type) 0069 { 0070 case Dice::RESULT_TYPE::SCALAR: 0071 { 0072 return getScalarResult(); 0073 } 0074 case Dice::RESULT_TYPE::DICE_LIST: 0075 { 0076 return QVariant::fromValue(m_diceValues); 0077 } 0078 default: 0079 break; 0080 } 0081 return QVariant(); 0082 } 0083 bool DiceResult::contains(Die* die, const std::function<bool(const Die*, const Die*)> equal) 0084 { 0085 for(auto& value : m_diceValues) 0086 { 0087 if(equal(value, die)) 0088 { 0089 return true; 0090 } 0091 } 0092 return false; 0093 } 0094 qreal DiceResult::getScalarResult() 0095 { 0096 if(m_diceValues.size() == 1) 0097 { 0098 return m_diceValues[0]->getValue(); 0099 } 0100 else 0101 { 0102 qint64 scalar= 0; 0103 int i= 0; 0104 for(auto& tmp : m_diceValues) 0105 { 0106 if(i > 0) 0107 { 0108 switch(m_operator) 0109 { 0110 case Dice::ArithmeticOperator::PLUS: 0111 scalar+= tmp->getValue(); 0112 break; 0113 case Dice::ArithmeticOperator::MULTIPLICATION: 0114 scalar*= tmp->getValue(); 0115 break; 0116 case Dice::ArithmeticOperator::MINUS: 0117 scalar-= tmp->getValue(); 0118 break; 0119 case Dice::ArithmeticOperator::POW: 0120 scalar= static_cast<int>(pow(static_cast<double>(scalar), static_cast<double>(tmp->getValue()))); 0121 break; 0122 case Dice::ArithmeticOperator::DIVIDE: 0123 case Dice::ArithmeticOperator::INTEGER_DIVIDE: 0124 if(tmp->getValue() != 0) 0125 { 0126 scalar/= tmp->getValue(); 0127 } 0128 else 0129 { 0130 /// @todo Error cant divide by 0. Must be displayed. 0131 } 0132 break; 0133 } 0134 } 0135 else 0136 { 0137 scalar= tmp->getValue(); 0138 } 0139 ++i; 0140 } 0141 return scalar; 0142 } 0143 } 0144 0145 Dice::ArithmeticOperator DiceResult::getOperator() const 0146 { 0147 return m_operator; 0148 } 0149 0150 void DiceResult::clear() 0151 { 0152 m_diceValues.clear(); 0153 } 0154 0155 void DiceResult::setOperator(const Dice::ArithmeticOperator& dieOperator) 0156 { 0157 m_operator= dieOperator; 0158 } 0159 QString DiceResult::toString(bool wl) 0160 { 0161 QStringList scalarSum; 0162 for(auto& die : m_diceValues) 0163 { 0164 scalarSum << QString::number(die->getValue()); 0165 } 0166 if(wl) 0167 { 0168 return QStringLiteral("%3 [label=\"DiceResult Value %1 dice %2\"]") 0169 .arg(QString::number(getScalarResult()), scalarSum.join('_'), m_id); 0170 } 0171 else 0172 { 0173 return m_id; 0174 } 0175 } 0176 Result* DiceResult::getCopy() const 0177 { 0178 auto copy= new DiceResult(); 0179 copy->setHomogeneous(m_homogeneous); 0180 copy->setOperator(m_operator); 0181 copy->m_id= m_id; 0182 QList<Die*> list; 0183 for(auto die : m_diceValues) 0184 { 0185 auto newdie= new Die(*die); 0186 newdie->setDisplayed(false); 0187 // die->displayed(); 0188 list.append(newdie); 0189 } 0190 copy->setResultList(list); 0191 copy->setPrevious(getPrevious()); 0192 return copy; 0193 }