File indexing completed on 2024-05-12 05:39:27

0001 /*************************************************************************
0002  *   Copyright (C) 2009 by Renaud Guezennec                              *
0003  *                                                                       *
0004  *   https://rolisteam.org/                                           *
0005  *                                                                       *
0006  *   rolisteam is free software; you can redistribute it and/or modify   *
0007  *   it under the terms of the GNU General Public License as published   *
0008  *   by the Free Software Foundation; either version 2 of the License,   *
0009  *   or (at your option) any later version.                              *
0010  *                                                                       *
0011  *   This program is distributed in the hope that it will be useful,     *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0014  *   GNU General Public License for more details.                        *
0015  *                                                                       *
0016  *   You should have received a copy of the GNU General Public License   *
0017  *   along with this program; if not, write to the                       *
0018  *   Free Software Foundation, Inc.,                                     *
0019  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0020  *************************************************************************/
0021 #include <QList>
0022 
0023 #include "diceparser/parsingtoolbox.h"
0024 #include "keepdiceexecnode.h"
0025 
0026 KeepDiceExecNode::KeepDiceExecNode() : m_diceResult(new DiceResult())
0027 {
0028     m_result= m_diceResult;
0029 }
0030 KeepDiceExecNode::~KeepDiceExecNode() {}
0031 void KeepDiceExecNode::run(ExecutionNode* previous)
0032 {
0033     m_previousNode= previous;
0034     if(nullptr == previous || nullptr == m_numberOfDiceNode)
0035     {
0036         return;
0037     }
0038     m_numberOfDiceNode->run(previous);
0039     auto lastnode= ParsingToolBox::getLeafNode(m_numberOfDiceNode);
0040     if(nullptr == lastnode)
0041         return;
0042     auto result= lastnode->getResult();
0043     if(nullptr == result)
0044         return;
0045     if(!result->hasResultOfType(Dice::RESULT_TYPE::SCALAR))
0046         return;
0047 
0048     auto numberOfDice= result->getResult(Dice::RESULT_TYPE::SCALAR).toInt();
0049 
0050     DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(previous->getResult());
0051     m_result->setPrevious(previousDiceResult);
0052     if(nullptr != previousDiceResult)
0053     {
0054         QList<Die*> diceList= previousDiceResult->getResultList();
0055 
0056         if(numberOfDice < 0)
0057         {
0058             numberOfDice= diceList.size() + numberOfDice;
0059         }
0060 
0061         QList<Die*> diceList3= diceList.mid(0, static_cast<int>(numberOfDice));
0062         QList<Die*> diceList2;
0063 
0064         for(Die* die : qAsConst(diceList3))
0065         {
0066             Die* tmpdie= new Die(*die);
0067             diceList2.append(tmpdie);
0068             die->displayed();
0069             die->setSelected(false);
0070         }
0071 
0072         if(numberOfDice > static_cast<qint64>(diceList.size()))
0073         {
0074             m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE,
0075                             QObject::tr(" You ask to keep %1 dice but the result only has %2")
0076                                 .arg(numberOfDice)
0077                                 .arg(diceList.size()));
0078         }
0079 
0080         for(auto& tmp : diceList.mid(static_cast<int>(numberOfDice), -1))
0081         {
0082             tmp->setHighlighted(false);
0083         }
0084 
0085         m_diceResult->setResultList(diceList2);
0086         if(nullptr != m_nextNode)
0087         {
0088             m_nextNode->run(this);
0089         }
0090     }
0091 }
0092 
0093 void KeepDiceExecNode::setDiceKeepNumber(ExecutionNode* n)
0094 {
0095     m_numberOfDiceNode= n;
0096 }
0097 QString KeepDiceExecNode::toString(bool wl) const
0098 {
0099     if(wl)
0100     {
0101         // auto param= m_numberOfDiceNode->toString(wl);
0102         return QString("%1 [label=\"KeepDiceExecNode\"]").arg(m_id);
0103     }
0104     else
0105     {
0106         return m_id;
0107     }
0108 }
0109 qint64 KeepDiceExecNode::getPriority() const
0110 {
0111     qint64 priority= 0;
0112     if(nullptr != m_previousNode)
0113     {
0114         priority= m_previousNode->getPriority();
0115     }
0116     return priority;
0117 }
0118 
0119 ExecutionNode* KeepDiceExecNode::getCopy() const
0120 {
0121     KeepDiceExecNode* node= new KeepDiceExecNode();
0122     if(nullptr != m_numberOfDiceNode)
0123     {
0124         node->setDiceKeepNumber(m_numberOfDiceNode->getCopy());
0125     }
0126     if(nullptr != m_nextNode)
0127     {
0128         node->setNextNode(m_nextNode->getCopy());
0129     }
0130     return node;
0131 }