Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestManager.cc @ 5739

Last change on this file since 5739 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 10.0 KB
RevLine 
[1996]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[2261]29/**
[3196]30    @file
[2662]31    @brief Implementation of the QuestManager class.
[2261]32*/
33
[2105]34#include "QuestManager.h"
35
[3196]36#include "util/Exception.h"
[1996]37#include "core/CoreIncludes.h"
[5693]38#include "core/GUIManager.h"
[2105]39
[5735]40#include "infos/PlayerInfo.h"
[2095]41#include "Quest.h"
42#include "QuestHint.h"
[3196]43#include "QuestItem.h"
[1996]44
[2662]45namespace orxonox
46{
[2911]47    //! Pointer to the current (and single) instance of this class.
[3370]48    /*static*/ QuestManager* QuestManager::singletonPtr_s = NULL;
[1996]49
[2261]50    /**
51    @brief
52        Constructor. Registers the object.
[2911]53    @todo
54        Is inheriting from BaseObject proper?
[2261]55    */
[2911]56    QuestManager::QuestManager()
[1996]57    {
[2911]58        RegisterRootObject(QuestManager);
[1996]59    }
[2092]60
[2261]61    /**
62    @brief
63        Destructor.
64    */
[1996]65    QuestManager::~QuestManager()
66    {
67    }
68
69    /**
70    @brief
[2261]71        Registers a Quest with the QuestManager to make it globally accessable.
72        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]73    @param quest
[2261]74        The Quest that is to be registered.
[1996]75    @return
76        Returns true if successful, false if not.
77    */
[2911]78    bool QuestManager::registerQuest(Quest* quest)
[1996]79    {
[2261]80        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
[2068]81        {
82            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
83            return false;
[2093]84        }
[2092]85
[2261]86        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[2911]87        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
[2092]88
[2261]89        if(result.second) //!< If inserting was a success.
[2068]90        {
91            COUT(3) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
92            return true;
[2093]93        }
94        else
95        {
96           COUT(2) << "Quest with the same id was already present." << std::endl;
97           return false;
98        }
[1996]99    }
[2092]100
[1996]101    /**
102    @brief
103        Registers a QuestHint with the QuestManager to make it globally accessable.
[2261]104        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]105    @param hint
106        The QuestHint to be registered.
107    @return
108        Returns true if successful, false if not.
109    */
[2911]110    bool QuestManager::registerHint(QuestHint* hint)
[1996]111    {
[2261]112        if(hint == NULL) //!< Still not liking NULL-pointers.
[2068]113        {
114            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
115            return false;
116        }
[2092]117
[2261]118        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[2911]119        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
[2092]120
[2261]121        if(result.second) //!< If inserting was a success.
[2068]122        {
123            COUT(3) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
124            return true;
[2093]125        }
126        else
127        {
128           COUT(2) << "QuestHint with the same id was already present." << std::endl;
129           return false;
130        }
[1996]131    }
[2092]132
[1996]133    /**
134    @brief
[2261]135        Finds a Quest with the given id.
[1996]136    @param questId
[2261]137        The id of the Quest sought for.
[1996]138    @return
[2261]139        Returns a pointer to the Quest with the input id.
140        Returns NULL if there is no Quest with the given questId.
[2068]141    @throws
142        Throws an exception if the given questId is invalid.
[1996]143    */
[2911]144    Quest* QuestManager::findQuest(const std::string & questId)
[1996]145    {
[2261]146        if(!QuestItem::isId(questId)) //!< Check vor validity of the given id.
[2093]147        {
[2068]148            ThrowException(Argument, "Invalid questId.");
[2093]149        }
[2092]150
[1996]151        Quest* quest;
[2911]152        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
153        if (it != this->questMap_.end()) //!< If the Quest is registered.
[2093]154        {
155            quest = it->second;
156        }
157        else
158        {
159           quest = NULL;
160           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
161        }
[2092]162
[2093]163        return quest;
[1996]164
165    }
[2092]166
[1996]167    /**
168    @brief
[2261]169        Finds a QuestHint with the given id.
[1996]170    @param hintId
[2261]171        The id of the QuestHint sought for.
[1996]172    @return
[2261]173        Returns a pointer to the QuestHint with the input id.
174        Returns NULL if there is no QuestHint with the given hintId.
[2068]175    @throws
176        Throws an exception if the given hintId is invalid.
[1996]177    */
[2911]178    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]179    {
[2261]180        if(!QuestItem::isId(hintId)) //!< Check vor validity of the given id.
[2093]181        {
[2068]182            ThrowException(Argument, "Invalid hintId.");
[2093]183        }
[2092]184
[1996]185        QuestHint* hint;
[2911]186        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
187        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
[2093]188        {
189            hint = it->second;
190        }
191        else
192        {
193           hint = NULL;
194           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
195        }
[2092]196
[2093]197        return hint;
[1996]198
199    }
200
[2993]201    /**
202    @brief
[5693]203
[2993]204    @param name
205    @return
206    */
[2963]207    QuestContainer* QuestManager::getQuestTree(std::string & name)
208    {
[5693]209        PlayerInfo* player = GUIManager::getInstance().getPlayer(name);
210        if(player == NULL)
[2963]211        {
[5693]212            COUT(1) << "Error: GUIOverlay with name '" << name << "' has no player." << std::endl;
[2963]213            return NULL;
214        }
[5693]215
[2963]216        QuestContainer* root = NULL;
217        QuestContainer* current = NULL;
[5693]218
[2993]219        std::list<Quest*>* rootQuests = new std::list<Quest*>();
220        getRootQuests(player, *rootQuests);
[5693]221
[2993]222        for(std::list<Quest*>::iterator it = rootQuests->begin(); it != rootQuests->end(); it++)
[2963]223        {
[2993]224            QuestContainer* container = addSubQuest(*it, player);
[2963]225
226            if(root == NULL)
227            {
228                root = container;
229            }
230            else
231            {
232                current->next = container;
233            }
[5693]234
[2963]235            current = container;
236
237        }
238        if(current != NULL)
239            current->next = NULL;
240
[2993]241        delete rootQuests;
[2963]242
243        return root;
244    }
245
[2993]246    /**
247    @brief
[5693]248
[2993]249    @param player
250    @param list
251    @return
252    */
[2963]253    void QuestManager::getRootQuests(const PlayerInfo* player, std::list<Quest*> & list)
254    {
255        for(std::map<std::string, Quest*>::iterator it=this->questMap_.begin(); it!=this->questMap_.end(); it++)
256        {
257            Quest* quest = (*it).second;
258            if(quest->getParentQuest() == NULL && !quest->isInactive(player))
259            {
260                list.push_back(quest);
261            }
262        }
263    }
264
[2993]265    /**
266    @brief
[5693]267
[2993]268    @param quest
269    @param player
270    @return
271    */
272    QuestContainer* QuestManager::addSubQuest(Quest* quest, const PlayerInfo* player)
[2963]273    {
[2993]274        if(quest == NULL)
275            return NULL;
276
277        QuestContainer* container = new QuestContainer;
278        container->description = quest->getDescription();
279        container->hint = addHints(quest, player);
280
281        if(quest->isActive(player))
282        {
283            container->status = "active";
284        }
285        else if(quest->isCompleted(player))
286        {
287            container->status = "completed";
288        }
289        else if(quest->isFailed(player))
290        {
291            container->status = "failed";
292        }
293        else
294        {
295            container->status = "";
[3370]296            COUT(1) << "An error occurred. A Quest of un-specified status wanted to be displayed." << std::endl;
[2993]297        }
[5693]298
[2993]299        std::list<Quest*> quests = quest->getSubQuestList();
[2963]300        QuestContainer* current = NULL;
301        QuestContainer* first = NULL;
302        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
303        {
304            Quest* subQuest = *it;
305            if(!subQuest->isInactive(player))
306            {
[2993]307                QuestContainer* subContainer = addSubQuest(subQuest, player);
[2963]308
309                if(first == NULL)
310                {
[2993]311                    first = subContainer;
[2963]312                }
313                else
314                {
[2993]315                    current->next = subContainer;
[2963]316                }
[5693]317
[2993]318                current = subContainer;
[2963]319            }
320        }
321        if(current != NULL)
322            current->next = NULL;
323        container->subQuests = first;
[5693]324
[2993]325        return container;
[2963]326    }
327
[2993]328    /**
329    @brief
[5693]330
[2993]331    @param quest
332    @param player
333    @return
334    */
335    HintContainer* QuestManager::addHints(Quest* quest, const PlayerInfo* player)
[2963]336    {
337        HintContainer* current = NULL;
338        HintContainer* first = NULL;
339
340        std::list<QuestHint*> hints = quest->getHintsList();
341        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
342        {
343            if((*it)->isActive(player))
344            {
345                HintContainer* hint = new HintContainer;
346                hint->description = (*it)->getDescription();
347
348                if(first == NULL)
349                {
350                    first = hint;
351                }
352                else
353                {
354                    current->next = hint;
355                }
[5693]356
[2963]357                current = hint;
358            }
359        }
360
361        if(current != NULL)
362            current->next = NULL;
[2993]363        return first;
[2963]364    }
365
366
[1996]367}
Note: See TracBrowser for help on using the repository browser.