Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem3/src/orxonox/objects/quest/Quest.cc @ 2329

Last change on this file since 2329 was 2328, checked in by dafrick, 15 years ago
  • Created QuestListeners, they can be used to affect Objects due to status changes of quests.
File size: 13.0 KB
Line 
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 
29/**
30    @file Quest.cc
31    @brief
32    Implementation of the Quest class.
33*/
34
35#include "OrxonoxStableHeaders.h"
36#include "Quest.h"
37
38#include "core/CoreIncludes.h"
39
40#include "orxonox/objects/infos/PlayerInfo.h"
41#include "QuestManager.h"
42#include "QuestDescription.h"
43#include "QuestHint.h"
44#include "QuestEffect.h"
45#include "QuestListener.h"
46
47namespace orxonox {
48
49    /**
50    @brief
51        Constructor. Registers and initializes object.
52    */
53    Quest::Quest(BaseObject* creator) : QuestItem(creator)
54    {
55        RegisterObject(Quest);
56
57        this->parentQuest_ = NULL;
58    }
59
60    /**
61    @brief
62        Destructor.
63    */
64    Quest::~Quest()
65    {
66
67    }
68
69    /**
70    @brief
71        Method for creating a Quest object through XML.
72    */
73    void Quest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(Quest, XMLPort, xmlelement, mode);
76
77        XMLPortObject(Quest, Quest, "subquests", addSubQuest, getSubQuest, xmlelement, mode);
78        XMLPortObject(Quest, QuestHint, "hints", addHint, getHint, xmlelement, mode);
79        XMLPortObject(Quest, QuestEffect, "fail-effects", addFailEffect, getFailEffect, xmlelement, mode);
80        XMLPortObject(Quest, QuestEffect, "complete-effects", addCompleteEffect, getCompleteEffect, xmlelement, mode);
81
82        QuestManager::registerQuest(this); //!<Registers the Quest with the QuestManager.
83    }
84
85    /**
86    @brief
87        Sets the parentquest of the Quest.
88    @param quest
89        A pointer to the Quest to be set as parentquest.
90    @return
91        Returns true if the parentquest could be set.
92    */
93    bool Quest::setParentQuest(Quest* quest)
94    {
95        if(quest == NULL) //!< We don't want to set NULL-Pointers.
96        {
97            COUT(2) << "The parentquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
98            return false;
99        }
100
101        this->parentQuest_ = quest;
102
103        COUT(3) << "Parent Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
104        return true;
105    }
106
107    /**
108    @brief
109        Adds a subquest to the Quest.
110    @param quest
111        A pointer to the Quest to be set as subquest.
112    @return
113        Returns true if the subquest could be set.
114    */
115    bool Quest::addSubQuest(Quest* quest)
116    {
117        if(quest == NULL) //!< We don't want to set NULL-Pointers.
118        {
119            COUT(2) << "The subquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
120            return false;
121        }
122
123        quest->setParentQuest(this); //!< Sets the currentQuest (this) as parentquest for the added subquest.
124        this->subQuests_.push_back(quest); //!< Adds the Quest to the end of the list of subquests.
125
126        COUT(3) << "Sub Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
127        return true;
128    }
129
130
131    /**
132    @brief
133        Adds a QuestHint to the list of QuestHints
134    @param hint
135        The QuestHint that should be added to the list of QuestHints.
136    @return
137        Returns true if the hint was successfully added.
138    */
139    bool Quest::addHint(QuestHint* hint)
140    {
141        if(hint == NULL) //!< We don't want to set NULL-Pointers. Seriously!
142        {
143            COUT(2) << "A NULL-QuestHint was trying to be added." << std::endl;
144            return false;
145        }
146
147        hint->setQuest(this); //!< Sets the current Quest (this) as Quest for the added QuestHint.
148        this->hints_.push_back(hint); //!< Adds the QuestHint to the end of the list of QuestHints.
149
150        COUT(3) << "QuestHint {" << hint->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
151        return true;
152    }
153
154    /**
155    @brief
156        Adds an QuestEffect to the list of fail QuestEffects.
157    @param effect
158        The QuestEffect to be added.
159    @return
160        Returns true if successful.
161    */
162    bool Quest::addFailEffect(QuestEffect* effect)
163    {
164        if(effect == NULL) //!< We don't want to set NULL-Pointers.
165        {
166            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
167            return false;
168        }
169
170        this->failEffects_.push_back(effect); //!< Adds the QuestEffect to the end of the list of fail QuestEffects.
171
172        COUT(3) << "A FailEffect was added to Quest {" << this->getId() << "}." << std::endl;
173        return true;
174    }
175
176    /**
177    @brief
178        Adds an QuestEffect to the list of complete QuestEffects.
179    @param effect
180        The QuestEffect to be added.
181    @return
182        Returns true if successful.
183    */
184    bool Quest::addCompleteEffect(QuestEffect* effect)
185    {
186        if(effect == NULL) //!< We don't want to set NULL-Pointers.
187        {
188            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
189            return false;
190        }
191
192        this->completeEffects_.push_back(effect); //!< Adds the QuestEffect to the end of the list of complete QuestEffects.
193
194        COUT(3) << "A CompleteEffect was added to Quest {" << this->getId() << "}." << std::endl;
195        return true;
196    }
197
198    /**
199    @brief
200        Returns the parentquest of the Quest.
201    @return
202        Returns a pointer to the parentquest of the Quest.
203    */
204    const Quest* Quest::getParentQuest(void)
205    {
206        return this->parentQuest_;
207    }
208
209    /**
210    @brief
211        Returns the subquest at the given index.
212    @param
213        The index.
214    @return
215        Returns a pointer to the subquest at the given index. NULL if there is no element at the given index.
216    */
217    const Quest* Quest::getSubQuest(unsigned int index) const
218    {
219        int i = index;
220       
221        //! Iterate through all subquests.
222        for (std::list<Quest*>::const_iterator subQuest = this->subQuests_.begin(); subQuest != this->subQuests_.end(); ++subQuest)
223        {
224            if(i == 0) //!< We're counting down...
225            {
226               return *subQuest;
227            }
228            i--;
229        }
230       
231        return NULL; //!< If the index is greater than the number of elements in the list.
232    }
233
234    /**
235    @brief
236        Returns the QuestHint at the given index.
237    @param
238        The index.
239    @return
240        Returns a pointer to the QuestHint at the given index. NULL if there is no element at the given index.
241    */
242    const QuestHint* Quest::getHint(unsigned int index) const
243    {
244        int i = index;
245       
246        //! Iterate through all QuestHints.
247        for (std::list<QuestHint*>::const_iterator hint = this->hints_.begin(); hint != this->hints_.end(); ++hint)
248        {
249            if(i == 0) //!< We're counting down...
250            {
251               return *hint;
252            }
253            i--;
254        }
255        return NULL; //!< If the index is greater than the number of elements in the list.
256    }
257
258    /**
259    @brief
260        Returns the fail QuestEffect at the given index.
261    @param
262        The index.
263    @return
264        Returns a pointer to the fail QuestEffect at the given index. NULL if there is no element at the given index.
265    */
266    const QuestEffect* Quest::getFailEffect(unsigned int index) const
267    {
268        int i = index;
269       
270        //! Iterate through all fail QuestEffects.
271        for (std::list<QuestEffect*>::const_iterator effect = this->failEffects_.begin(); effect != this->failEffects_.end(); ++effect)
272        {
273            if(i == 0) //!< We're counting down...
274            {
275               return *effect;
276            }
277            i--;
278        }
279        return NULL; //!< If the index is greater than the number of elements in the list.
280    }
281
282    /**
283    @brief
284        Returns the complete QuestEffect at the given index.
285    @param
286        The index.
287    @return
288        Returns a pointer to the complete QuestEffect at the given index. NULL if there is no element at the given index.
289    */
290    const QuestEffect* Quest::getCompleteEffect(unsigned int index) const
291    {
292        int i = index;
293       
294        //! Iterate through all complete QuestEffects.
295        for (std::list<QuestEffect*>::const_iterator effect = this->completeEffects_.begin(); effect != this->completeEffects_.end(); ++effect)
296        {
297            if(i == 0) //!< We're counting down...
298            {
299               return *effect;
300            }
301            i--;
302        }
303        return NULL; //!< If the index is greater than the number of elements in the list.
304    }
305
306    /**
307    @brief
308        Returns true if the quest status for the specific player is 'inactive'.
309    @param player
310        The player.
311    @return
312        Returns true if the quest status for the specific player is 'inactive'.
313    @throws
314        Throws an exception if getStatus throws one.
315    */
316    bool Quest::isInactive(const PlayerInfo* player) const
317    {
318        return this->getStatus(player) == questStatus::inactive;
319    }
320
321    /**
322    @brief
323        Returns true if the quest status for the specific player is 'active'.
324    @param player
325        The player.
326    @return
327        Returns true if the quest status for the specific player is 'active'.
328    @throws
329        Throws an exception if getStatus throws one.
330    */
331    bool Quest::isActive(const PlayerInfo* player) const
332    {
333
334        return this->getStatus(player) == questStatus::active;
335    }
336
337    /**
338    @brief
339        Returns true if the quest status for the specific player is 'failed'.
340    @param player
341        The player.
342    @return
343        Returns true if the quest status for the specific player is 'failed'.
344    @throws
345        Throws an exception if getStatus throws one.
346    */
347    bool Quest::isFailed(const PlayerInfo* player) const
348    {
349        return this->getStatus(player) == questStatus::failed;
350    }
351
352    /**
353    @brief
354        Returns true if the quest status for the specific player is 'completed'.
355    @param player
356        The player.
357    @return
358        Returns true if the quest status for the specific player is 'completed'.
359    @throws
360        Throws an exception if getStatus throws one.
361    */
362    bool Quest::isCompleted(const PlayerInfo* player) const
363    {
364        return this->getStatus(player) == questStatus::completed;
365    }
366   
367    /**
368    @brief
369        Fails the Quest for an input player.
370    @param player
371        The player.
372    @return
373        Returns true if the Quest could be failed, false if not.
374    */
375    bool Quest::fail(PlayerInfo* player)
376    {
377        QuestListener::advertiseStatusChange(this->listeners_, "fail"); //!< Tells the QuestListeners, that the status has changed to failed.
378        this->setStatus(player, questStatus::failed);
379        return true;
380    }
381   
382    /**
383    @brief
384        Completes the Quest for an input player.
385    @param player
386        The player.
387    @return
388        Returns true if the Quest could be completed, false if not.
389    */
390    bool Quest::complete(PlayerInfo* player)
391    {
392        QuestListener::advertiseStatusChange(this->listeners_, "complete"); //!< Tells the QuestListeners, that the status has changed to completed.
393        this->setStatus(player, questStatus::completed);
394        return true;
395    }
396
397    /**
398    @brief
399        Starts the Quest for an input player.
400    @param player
401        The player.
402    @return
403        Returns true if the Quest could be started, false if not.
404    */
405    bool Quest::start(PlayerInfo* player)
406    {
407        if(!this->isStartable(player)) //!< Checks whether the quest can be started.
408        {
409            COUT(4) << "A non-startable quest was trying to be started." << std::endl;
410            return false;
411        }
412       
413        QuestListener::advertiseStatusChange(this->listeners_, "start"); //!< Tells the QuestListeners, that the status has changed to active.
414       
415        this->setStatus(player, questStatus::active);
416        return true;
417    }
418   
419    /**
420    @brief
421        Adds a QuestListener to the list of QuestListeners listening to this Quest.
422    @param listener
423        The QuestListener to be added.
424    @return
425        Returns true if successful, false if not.
426    */
427    bool Quest::addListener(QuestListener* listener)
428    {
429        if(listener == NULL)
430        {
431            COUT(2) << "A NULL-QuestListener was trying to be added to a Quests listeners." << std::endl;
432            return false;
433        }
434       
435        this->listeners_.push_back(listener);
436        return true;
437    }
438
439}
Note: See TracBrowser for help on using the repository browser.