Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7429 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

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