Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tetris/src/modules/questsystem/Quest.cc @ 8564

Last change on this file since 8564 was 7552, checked in by dafrick, 14 years ago

Resolving some TODOs and doing some additional cleanup. Almost done now…

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