File indexing completed on 2024-04-28 05:37:01
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 #include "range.h" 0023 0024 Range::Range() : m_hasEnd(false), m_hasStart(false), m_emptyRange(false) {} 0025 void Range::setValue(qint64 s, qint64 e) 0026 { 0027 m_start= s; 0028 m_end= e; 0029 0030 m_hasEnd= true; 0031 m_hasStart= true; 0032 } 0033 0034 qint64 Range::hasValid(Die* m, bool recursive, bool unhighlight) const 0035 { 0036 qint64 result= 0; 0037 if(recursive) 0038 { 0039 for(qint64& value : m->getListValue()) 0040 { 0041 if((value >= m_start) && (value <= m_end)) 0042 { 0043 ++result; 0044 } 0045 } 0046 } 0047 else if((m->getLastRolledValue() >= m_start) && (m->getLastRolledValue() <= m_end)) 0048 { 0049 ++result; 0050 } 0051 if((unhighlight) && (result == 0)) 0052 { 0053 m->setHighlighted(false); 0054 } 0055 return result; 0056 } 0057 QString Range::toString() 0058 { 0059 return QStringLiteral("[%1-%2]").arg(m_start).arg(m_end); 0060 } 0061 Dice::CONDITION_STATE Range::isValidRangeSize(const std::pair<qint64, qint64>& range) const 0062 { 0063 auto minRange= std::min(m_start, m_end); 0064 auto minPossibleValue= std::min(range.first, range.second); 0065 0066 auto maxRange= std::max(m_start, m_end); 0067 auto maxPossibleValue= std::max(range.first, range.second); 0068 0069 if(minRange == minPossibleValue && maxRange == maxPossibleValue) 0070 return Dice::CONDITION_STATE::ALWAYSTRUE; 0071 else if(maxRange < minPossibleValue || minRange > maxPossibleValue) 0072 return Dice::CONDITION_STATE::UNREACHABLE; 0073 else 0074 return Dice::CONDITION_STATE::UNREACHABLE; 0075 } 0076 void Range::setStart(qint64 start) 0077 { 0078 m_start= start; 0079 m_hasStart= true; 0080 } 0081 void Range::setEnd(qint64 end) 0082 { 0083 m_end= end; 0084 m_hasEnd= true; 0085 } 0086 0087 bool Range::isFullyDefined() const 0088 { 0089 return (m_hasEnd && m_hasStart); 0090 } 0091 qint64 Range::getStart() const 0092 { 0093 return m_start; 0094 } 0095 qint64 Range::getEnd() const 0096 { 0097 return m_end; 0098 } 0099 void Range::setEmptyRange(bool b) 0100 { 0101 m_emptyRange= b; 0102 } 0103 0104 bool Range::isEmptyRange() 0105 { 0106 return m_emptyRange; 0107 } 0108 Validator* Range::getCopy() const 0109 { 0110 Range* val= new Range(); 0111 val->setEmptyRange(m_emptyRange); 0112 if(m_hasEnd) 0113 { 0114 val->setEnd(m_end); 0115 } 0116 if(m_hasStart) 0117 { 0118 val->setStart(m_start); 0119 } 0120 return val; 0121 }