Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6815 was 6800, checked in by dafrick, 15 years ago

Created a new class of triggers called Multitriggers.
MultiTriggers are triggers which (as opposed to normal triggers) have a state for each object triggering the MultiTrigger, that means, that a MultiTrigger can be triggered for two different Players, while not being triggered for a third.
To go with this MultiTrigger I created a data structure (MultitriggerContainer), which helps relaying important information to the objects, that receive the Events of the trigger.
Also there is a MultiDistanceTrigger, which is just the DistanceTrigger as a MultiTrigger.

To make this work I had to make some adjustements to the eventsystem, namely an EventState can now be either an EventState (as it was before) or an EventSink, which means that every efent arriving at the EventState is processed as opposed to just the ones which change the state.
There is a new makro (XMLPortEventSink) to create an EventState with sink behaviour. It can be used exacly as the XMLPortEventState makro.

  • 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
[6800]78        XMLPortEventState(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
[5929]79
[2226]80        COUT(3) << "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.
[2221]94    @param trigger
[6800]95        A pointer to the PlayerTrigger that threw the Event.
[2221]96    @return
97        Returns true if successfully executed, false if not.
98    */
[6800]99    bool QuestEffectBeacon::execute(bool b, BaseObject* trigger)
[2146]100    {
[6800]101        //TODO: Remove debug output.
102        COUT(1) << "Debug: Calling execute on QuestEffectBeacon." << std::endl;
103       
[2209]104        if(!b)
[2146]105        {
[2226]106            return false;
[2209]107        }
[2221]108        if(!(this->isActive())) //!< If the QuestEffectBeacon is inactive it cannot be executed.
[2209]109        {
[2208]110            COUT(3) << "The QuestEffectBeacon is inactive." << std::endl;
[2146]111            return false;
112        }
[5720]113
[6800]114        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
115        MultiTriggerContainer* mTrigger = orxonox_cast<MultiTriggerContainer*>(trigger);
116        Pawn* pawn = NULL;
117       
118        //! If the trigger is neither a Playertrigger nor a MultiTrigger (i.e. a MultitriggerContainer) we can do anything with it.
119        if(pTrigger == NULL && mTrigger == NULL)
120            return false;
121       
122        // If the trigger is a PlayerTrigger.       
123        if(pTrigger != NULL)
[2221]124        {
[6800]125            if(!pTrigger->isForPlayer())  //!< The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
126                return false;
127            else
128                pawn = pTrigger->getTriggeringPlayer();
[2221]129        }
[6800]130       
131        // If the trigger is a MultiTrigger (i.e. a MultiTriggerContainer)
132        if(mTrigger != NULL)
133        {
134            pawn = orxonox_cast<Pawn*>(mTrigger->getData());
135        }
[2209]136
[3033]137        if(pawn == NULL)
[2146]138        {
[3033]139            COUT(2) << "The QuestEffectBeacon was triggered by an entity other than a Pawn." << std::endl;
[2146]140            return false;
141        }
[5720]142
[5938]143        //! Extract the PlayerInfo from the Pawn.
[3033]144        PlayerInfo* player = pawn->getPlayer();
[5720]145
[2208]146        if(player == NULL)
147        {
148            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
149            return false;
150        }
[2209]151
[2221]152        COUT(3) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;
[2209]153
[2221]154        bool check = QuestEffect::invokeEffects(player, this->effects_); //!< Invoke the QuestEffects on the PlayerInfo.
[2146]155        if(check)
156        {
[2221]157            this->decrementTimes(); //!< Decrement the number of times the beacon can be used.
[2146]158            return true;
[2662]159        }
[2221]160
[2662]161        return false;
[2146]162    }
[5720]163
[2221]164    /**
165    @brief
166        Set the status of the QuestEffectBeacon.
167    @param activate
168        If true the QuestEffectBeacon is activated, if false it is deactivated.
169    @return
170        Returns whether the activation/deactivation was successful.
171    */
172    bool QuestEffectBeacon::setActive(bool activate)
[2146]173    {
[2221]174        if(this->getTimes() == 0 && activate) //!< A QuestEffectBeacon that can be executed only 0 times is always inactive.
175        {
176            return false;
177        }
[5720]178
[2221]179        if(activate)
180        {
[3280]181        this->status_ = QuestEffectBeaconStatus::Active;
[2251]182        return true;
[2221]183        }
[5720]184
[3280]185        this->status_ = QuestEffectBeaconStatus::Inactive;
[2221]186        return true;
[2146]187    }
[5720]188
[2221]189    /**
190    @brief
191        Decrement the number of times the QuestEffectBeacon can be executed.
192    @return
193        Returns true if successful.
194    */
[2146]195    bool QuestEffectBeacon::decrementTimes(void)
196    {
[2221]197        if(!(this->isActive())) //!< The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
[2146]198        {
199            return false;
200        }
[2662]201        if(this->getTimes() == INFINITE_TIME) //!< If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
[2146]202        {
203            return true;
204        }
[5720]205
[2221]206        this->times_ = this->times_ - 1; //!< Decrement number of times the QuestEffectBeacon can be executed.
[2662]207        if(this->getTimes() == 0) //!< Set the QuestEffectBeacon to inactive when the number of times it can be executed is reduced to 0.
208        {
[3280]209            this->status_ = QuestEffectBeaconStatus::Inactive;
[2662]210        }
[5720]211
[2146]212        return true;
213    }
[5720]214
[2221]215    /**
216    @brief
217        Set the number of times the QuestEffectBeacon can be executed.
218        The number must be eighter <= 0, or INFINITY which is '-1'.
219    @param n
220        The number of times the QuestEffectBeacon can be executed.
221        The number must be eighter <= 0, or INFINITY which is '-1'.
222    @return
223        Returns true if successful.
224    */
[2146]225    bool QuestEffectBeacon::setTimes(const int & n)
226    {
[2662]227        if(n < 0 && n != INFINITE_TIME)
[2146]228        {
229            return false;
230        }
[5720]231
[2146]232        this->times_ = n;
233        return true;
234    }
[5720]235
[2146]236    /**
237    @brief
[2221]238        Adds a QuestEffect to the QuestEffectBeacon.
239    @param effect
240        A pointer to the QuestEffect to be added.
241    @return
242        Returns true if successful.
[2146]243    */
244    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
245    {
[2221]246        if(effect == NULL) //!< NULL-pointers are not well liked here...
[2146]247        {
248            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
249            return false;
250        }
251
252        this->effects_.push_back(effect);
253
[2191]254        COUT(3) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
[2146]255        return true;
256    }
[5720]257
[2221]258    /**
[2146]259    @brief
[2221]260        Returns the QuestEffect at the given index.
261    @param index
262        The index.
263    @return
264        Returns a pointer to the QuestEffect at the given index.
[2146]265    */
[2191]266    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
[2146]267    {
268        int i = index;
269        for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
270        {
271            if(i == 0)
272            {
273               return *effect;
274            }
275            i--;
276        }
277        return NULL;
278    }
279
280}
Note: See TracBrowser for help on using the repository browser.