Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestEffectBeacon.cc @ 7460

Last change on this file since 7460 was 7456, checked in by dafrick, 14 years ago

Reviewing documentation fo Questsystem, moving documentation fully into doxygen.
Added some files to modules they belong to.

  • Property svn:eol-style set to native
File size: 8.7 KB
RevLine 
[2146]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
[2221]29/**
[7456]30    @file QuestEffectBeacon.cc
[2662]31    @brief Implementation of the QuestEffectBeacon class.
[2221]32*/
33
[2146]34#include "QuestEffectBeacon.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
[2193]38#include "core/EventIncludes.h"
[7456]39
40#include "interfaces/PlayerTrigger.h"
[5735]41#include "worldentities/pawns/Pawn.h"
[7456]42
[6800]43#include "objects/triggers/MultiTriggerContainer.h"
[2146]44#include "QuestEffect.h"
45
[2662]46namespace orxonox
47{
[2146]48    CreateFactory(QuestEffectBeacon);
49
[2221]50    /**
51    @brief
52        Constructor. Registers the object and initializes defaults.
53    */
[7456]54    //TODO: Make just BaseObject?
[2662]55    QuestEffectBeacon::QuestEffectBeacon(BaseObject* creator) : StaticEntity(creator)
[2146]56    {
57        RegisterObject(QuestEffectBeacon);
[5720]58
[3280]59        this->status_ = QuestEffectBeaconStatus::Active;
[2662]60        this->times_ = INFINITE_TIME;
[2146]61    }
62
[2221]63    /**
64        Destructor.
65    */
[2146]66    QuestEffectBeacon::~QuestEffectBeacon()
67    {
68    }
[5720]69
[2221]70    /**
71    @brief
72        Method for creating a QuestEffectBeacon object through XML.
73    */
[2146]74    void QuestEffectBeacon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
75    {
76        SUPER(QuestEffectBeacon, XMLPort, xmlelement, mode);
77
78        XMLPortParam(QuestEffectBeacon, "times", setTimes, getTimes, xmlelement, mode);
[2221]79        XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode);
[5720]80
[6859]81        XMLPortEventSink(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
[5929]82
[7163]83        COUT(4) << "New QuestEffectBeacon created." << std::endl;
[2146]84    }
[5720]85
[5929]86    void QuestEffectBeacon::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[2191]87    {
[5929]88        SUPER(QuestEffectBeacon, XMLEventPort, xmlelement, mode);
[5720]89
[6800]90        XMLPortEventSink(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode);
[2191]91    }
[5720]92
[2221]93    /**
94    @brief
95        Executes the QuestEffectBeacon.
[5938]96        This means extracting the Pawn from the PlayerTrigger, provided by the Event causing the execution, and the extracting the PlayerInfo from the received Pawn and invoking the QuestEffectbeacon's QuestEffects on the received PlayerInfo.
[7401]97    @param bTriggered
98        true means the trigger was activated while false means it was deactivated
[2221]99    @param trigger
[6800]100        A pointer to the PlayerTrigger that threw the Event.
[2221]101    @return
102        Returns true if successfully executed, false if not.
103    */
[7456]104    //TODO: Eliminate MultiTriggerContainer stuff, since they are now PlayerTriggers as well.
[7401]105    bool QuestEffectBeacon::execute(bool bTriggered, BaseObject* trigger)
[2146]106    {
[7401]107        if(!bTriggered)
[2146]108        {
[2226]109            return false;
[2209]110        }
[7456]111        if(!(this->isActive())) // If the QuestEffectBeacon is inactive it cannot be executed.
[2209]112        {
[6906]113            COUT(4) << "The QuestEffectBeacon is inactive." << std::endl;
[2146]114            return false;
115        }
[5720]116
[6800]117        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
118        MultiTriggerContainer* mTrigger = orxonox_cast<MultiTriggerContainer*>(trigger);
119        Pawn* pawn = NULL;
[7163]120
[7456]121        // If the trigger is neither a Playertrigger nor a MultiTrigger (i.e. a MultitriggerContainer) we can do anything with it.
[6800]122        if(pTrigger == NULL && mTrigger == NULL)
123            return false;
[7163]124
125        // If the trigger is a PlayerTrigger.
[6800]126        if(pTrigger != NULL)
[2221]127        {
[7456]128            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[6800]129                return false;
130            else
131                pawn = pTrigger->getTriggeringPlayer();
[2221]132        }
[7163]133
[6800]134        // If the trigger is a MultiTrigger (i.e. a MultiTriggerContainer)
135        if(mTrigger != NULL)
136        {
137            pawn = orxonox_cast<Pawn*>(mTrigger->getData());
138        }
[2209]139
[3033]140        if(pawn == NULL)
[2146]141        {
[7163]142            COUT(4) << "The QuestEffectBeacon was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
[2146]143            return false;
144        }
[5720]145
[7456]146        // Extract the PlayerInfo from the Pawn.
[3033]147        PlayerInfo* player = pawn->getPlayer();
[5720]148
[2208]149        if(player == NULL)
150        {
151            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
152            return false;
153        }
[2209]154
[7163]155        COUT(4) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;
[2209]156
[7456]157        bool check = QuestEffect::invokeEffects(player, this->effects_); // Invoke the QuestEffects on the PlayerInfo.
[2146]158        if(check)
159        {
[7456]160            this->decrementTimes(); // Decrement the number of times the beacon can be used.
[2146]161            return true;
[2662]162        }
[2221]163
[2662]164        return false;
[2146]165    }
[5720]166
[2221]167    /**
168    @brief
169        Set the status of the QuestEffectBeacon.
170    @param activate
171        If true the QuestEffectBeacon is activated, if false it is deactivated.
172    @return
173        Returns whether the activation/deactivation was successful.
174    */
175    bool QuestEffectBeacon::setActive(bool activate)
[2146]176    {
[7456]177        if(this->getTimes() == 0 && activate) // A QuestEffectBeacon that can be executed only 0 times is always inactive.
[2221]178            return false;
[5720]179
[2221]180        if(activate)
181        {
[7456]182            this->status_ = QuestEffectBeaconStatus::Active;
183            return true;
[2221]184        }
[5720]185
[3280]186        this->status_ = QuestEffectBeaconStatus::Inactive;
[2221]187        return true;
[2146]188    }
[5720]189
[2221]190    /**
191    @brief
192        Decrement the number of times the QuestEffectBeacon can be executed.
193    @return
194        Returns true if successful.
195    */
[2146]196    bool QuestEffectBeacon::decrementTimes(void)
197    {
[7456]198        if(!(this->isActive())) // The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
[2146]199            return false;
[7456]200
201        if(this->getTimes() == INFINITE_TIME) // If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
[2146]202            return true;
[5720]203
[7456]204        this->times_ = this->times_ - 1; // Decrement number of times the QuestEffectBeacon can be executed.
205        if(this->getTimes() == 0) // Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0.
[3280]206            this->status_ = QuestEffectBeaconStatus::Inactive;
[5720]207
[2146]208        return true;
209    }
[5720]210
[2221]211    /**
212    @brief
213        Set the number of times the QuestEffectBeacon can be executed.
214        The number must be eighter <= 0, or INFINITY which is '-1'.
215    @param n
216        The number of times the QuestEffectBeacon can be executed.
217        The number must be eighter <= 0, or INFINITY which is '-1'.
218    @return
219        Returns true if successful.
220    */
[2146]221    bool QuestEffectBeacon::setTimes(const int & n)
222    {
[2662]223        if(n < 0 && n != INFINITE_TIME)
[2146]224            return false;
[5720]225
[2146]226        this->times_ = n;
227        return true;
228    }
[5720]229
[2146]230    /**
231    @brief
[2221]232        Adds a QuestEffect to the QuestEffectBeacon.
233    @param effect
234        A pointer to the QuestEffect to be added.
235    @return
236        Returns true if successful.
[2146]237    */
238    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
239    {
[7456]240        //TODO: Replace with assert.
241        if(effect == NULL) // NULL-pointers are not well liked here...
[2146]242        {
243            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
244            return false;
245        }
246
247        this->effects_.push_back(effect);
248
[7163]249        COUT(4) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
[2146]250        return true;
251    }
[5720]252
[2221]253    /**
[2146]254    @brief
[2221]255        Returns the QuestEffect at the given index.
256    @param index
257        The index.
258    @return
259        Returns a pointer to the QuestEffect at the given index.
[2146]260    */
[2191]261    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
[2146]262    {
263        int i = index;
264        for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
265        {
266            if(i == 0)
267               return *effect;
[7456]268
[2146]269            i--;
270        }
271        return NULL;
272    }
273
274}
Note: See TracBrowser for help on using the repository browser.