Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS09/src/modules/questsystem/Quest.cc @ 12373

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

merged libraries2 back to trunk

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