File indexing completed on 2024-04-21 04:03:07

0001 /***************************************************************************
0002                                cmapfilter.cpp
0003                              -------------------
0004     begin                : Thu Nov 1 2001
0005     copyright            : (C) 2001 by Kmud Developer Team
0006                            (C) 2007 by Tomas Mecir <kmuddy@kmuddy.net>
0007     email                : kmud-devel@kmud.de
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #include "cmapfilter.h"
0020 
0021 #include "cmapmanager.h"
0022 #include "cmapview.h"
0023 #include "cmaproom.h"
0024 #include "cmappath.h"
0025 
0026 #include <qregexp.h>
0027 
0028 CMapFilter::CMapFilter(CMapManager *manager)
0029 {  
0030   mapManager = manager;
0031 }
0032 
0033 CMapFilter::~CMapFilter()
0034 {
0035 }
0036 
0037 /** This method is called with when input is sent to the mud */
0038 QString CMapFilter::processCommand (const QString &command)
0039 {
0040   if (mapManager->validMoveCmd(command))
0041   {
0042     CMapView *view = mapManager->getActiveView();
0043     if (view->getFollowMode())
0044     {
0045       QString newStr;
0046       newStr += executeBeforeCommand (command);
0047       newStr += command;
0048       newStr += executeAfterCommand (command);
0049 
0050       if (mapManager->getMapData()->validRoomCheck)
0051         directionCmdQueue.append (command);
0052       else
0053         mapManager->movePlayerBy(command);
0054       return newStr;
0055     }
0056   }
0057   return command;
0058 }
0059 
0060 QString CMapFilter::executeBeforeCommand (const QString &command)
0061 {
0062   QString specialCmd;
0063   directionTyp dir = mapManager->textToDirection(command);
0064   if (dir == SPECIAL)
0065     specialCmd = command;
0066 
0067   CMapPath *path = mapManager->getCurrentRoom()->getPathDirection(dir,specialCmd);
0068 
0069   if (path)
0070   {
0071     QString roomCmd = path->getBeforeCommand();
0072 
0073     if (!roomCmd.trimmed().isEmpty())
0074       return roomCmd + "\n";
0075   }
0076   return QString();
0077 }
0078 
0079 QString CMapFilter::executeAfterCommand (const QString &command)
0080 {
0081   QString specialCmd;
0082   directionTyp dir = mapManager->textToDirection(command);
0083   if (dir == SPECIAL)
0084     specialCmd = command;
0085 
0086   CMapPath *path = mapManager->getCurrentRoom()->getPathDirection(dir,specialCmd);
0087 
0088   if (path)
0089   {
0090     QString roomCmd = path->getAfterCommand();
0091 
0092     if (!roomCmd.trimmed().isEmpty())
0093       return "\n" + roomCmd;
0094   }
0095   return QString();
0096 }
0097 
0098 /** This method is called when output is sent to the mud */
0099 void CMapFilter::processServerOutput(const QString &s)
0100 {
0101   if (!directionCmdQueue.isEmpty()) {
0102      QString dirCmd = directionCmdQueue.takeFirst();
0103 
0104     if (mapManager->getActiveView()->getFollowMode())
0105     {
0106       bool movePlayer = true;
0107 
0108       QStringList::iterator it;
0109       for (it = mapManager->getMapData()->failedMoveMsg.begin();
0110            it != mapManager->getMapData()->failedMoveMsg.end(); ++it)
0111       {
0112         QString str = *it;
0113         if (!str.isEmpty())
0114         {
0115           QRegExp r(str);
0116           
0117           if ( r.indexIn(s) != -1)
0118           {
0119             movePlayer = false;
0120             break;
0121           }
0122         }
0123 
0124       }
0125 
0126       if (movePlayer)
0127         mapManager->movePlayerBy(dirCmd);
0128     }
0129   }
0130 }
0131