Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/Quest.cc @ 12226

Last change on this file since 12226 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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