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 "jumpbackwardnode.h"
0022 #include <QDebug>
0023 
0024 JumpBackwardNode::JumpBackwardNode()
0025 {
0026     m_previousNode= nullptr;
0027     m_backwardNode= nullptr;
0028     m_diceResult= new DiceResult();
0029     m_result= m_diceResult;
0030 }
0031 
0032 qint64 JumpBackwardNode::getPriority() const
0033 {
0034     return 4;
0035 }
0036 QString JumpBackwardNode::toString(bool wl) const
0037 {
0038     if(wl)
0039     {
0040         return QString("%1 [label=\"JumpBackwardNode\"]").arg(m_id);
0041     }
0042     else
0043     {
0044         return m_id;
0045     }
0046 }
0047 void JumpBackwardNode::generateDotTree(QString& s)
0048 {
0049     s.append(toString(true));
0050     s.append(";\n");
0051 
0052     if(nullptr != m_backwardNode)
0053     {
0054         s.append(toString(false));
0055         s.append(" -> ");
0056         s.append(m_backwardNode->toString(false));
0057         s.append("[label=\"backward\"];\n");
0058         // m_backwardNode->generateDotTree(s);
0059     }
0060 
0061     if(nullptr != m_nextNode)
0062     {
0063         s.append(toString(false));
0064         s.append(" -> ");
0065         s.append(m_nextNode->toString(false));
0066         s.append("[label=\"next\"];\n");
0067         m_nextNode->generateDotTree(s);
0068     }
0069     else
0070     {
0071         s.append(toString(false));
0072         s.append(" -> ");
0073         s.append("nullptr;\n");
0074 
0075         if(nullptr != m_result)
0076         {
0077             s.append(toString(false));
0078             s.append(" ->");
0079             s.append(m_result->toString(false));
0080             s.append(" [label=\"Result\"];\n");
0081             m_result->generateDotTree(s);
0082         }
0083     }
0084 }
0085 
0086 void JumpBackwardNode::run(ExecutionNode* previous)
0087 {
0088     m_previousNode= previous;
0089     ExecutionNode* parent= previous;
0090     bool found= false;
0091     // int i = 3;
0092     Result* result= nullptr;
0093     while((nullptr != parent) && (!found))
0094     {
0095         result= parent->getResult();
0096         if(nullptr != result)
0097         {
0098             //--i;
0099             if(/*(i==0)&&*/ (result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)))
0100             {
0101                 found= true;
0102                 m_backwardNode= parent;
0103             }
0104             else
0105             {
0106                 JumpBackwardNode* jpNode= dynamic_cast<JumpBackwardNode*>(parent);
0107                 if(nullptr != jpNode)
0108                 {
0109                     found= true;
0110                     m_backwardNode= parent;
0111                 }
0112             }
0113         }
0114         if(!found)
0115         {
0116             parent= parent->getPreviousNode();
0117         }
0118     }
0119     if(nullptr == result)
0120     {
0121         m_errors.insert(
0122             Dice::ERROR_CODE::DIE_RESULT_EXPECTED,
0123             QObject::tr(" The @ operator expects dice result. Please check the documentation to fix your command."));
0124     }
0125     else
0126     {
0127         DiceResult* diceResult= dynamic_cast<DiceResult*>(result);
0128         if(nullptr != diceResult)
0129         {
0130             for(auto& die : diceResult->getResultList())
0131             {
0132                 Die* tmpdie= new Die(*die);
0133                 //*tmpdie= *die;
0134                 m_diceResult->insertResult(tmpdie);
0135                 die->displayed();
0136             }
0137         }
0138 
0139         m_result->setPrevious(previous->getResult());
0140 
0141         if(nullptr != m_nextNode)
0142         {
0143             m_nextNode->run(this);
0144         }
0145         if(nullptr != diceResult)
0146         {
0147             for(int i= 0; i < diceResult->getResultList().size(); ++i)
0148             {
0149                 Die* tmp= diceResult->getResultList().at(i);
0150                 Die* tmp2= m_diceResult->getResultList().at(i);
0151                 if(tmp->isHighlighted())
0152                 {
0153                     tmp2->setHighlighted(true);
0154                 }
0155             }
0156         }
0157     }
0158 }
0159 
0160 ExecutionNode* JumpBackwardNode::getCopy() const
0161 {
0162     JumpBackwardNode* node= new JumpBackwardNode();
0163     if(nullptr != m_nextNode)
0164     {
0165         node->setNextNode(m_nextNode->getCopy());
0166     }
0167     return node;
0168 }