Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17/src/modules/questsystem/Quest.cc @ 11690

Last change on this file since 11690 was 11690, checked in by patricwi, 6 years ago

rolled back to functional without 2 branches

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