Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding/src/modules/questsystem/QuestEffectBeacon.cc @ 7693

Last change on this file since 7693 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
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 QuestEffectBeacon.cc
31    @brief Implementation of the QuestEffectBeacon class.
32*/
33
34#include "QuestEffectBeacon.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "core/EventIncludes.h"
39
40#include "interfaces/PlayerTrigger.h"
41#include "worldentities/pawns/Pawn.h"
42
43#include "objects/triggers/MultiTriggerContainer.h"
44#include "QuestEffect.h"
45
46namespace orxonox
47{
48    CreateFactory(QuestEffectBeacon);
49
50    /**
51    @brief
52        Constructor. Registers the object and initializes defaults.
53    */
54    //TODO: Make just BaseObject?
55    QuestEffectBeacon::QuestEffectBeacon(BaseObject* creator) : StaticEntity(creator)
56    {
57        RegisterObject(QuestEffectBeacon);
58
59        this->status_ = QuestEffectBeaconStatus::Active;
60        this->times_ = INFINITE_TIME;
61    }
62
63    /**
64        Destructor.
65    */
66    QuestEffectBeacon::~QuestEffectBeacon()
67    {
68    }
69
70    /**
71    @brief
72        Method for creating a QuestEffectBeacon object through XML.
73    */
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);
79        XMLPortObject(QuestEffectBeacon, QuestEffect, "effects", addEffect, getEffect, xmlelement, mode);
80
81        XMLPortEventSink(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode); //TODO: Change BaseObject to MultiTrigger as soon as MultiTrigger is the base of all triggers.
82
83        COUT(4) << "New QuestEffectBeacon created." << std::endl;
84    }
85
86    void QuestEffectBeacon::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
87    {
88        SUPER(QuestEffectBeacon, XMLEventPort, xmlelement, mode);
89
90        XMLPortEventSink(QuestEffectBeacon, BaseObject, "execute", execute, xmlelement, mode);
91    }
92
93    /**
94    @brief
95        Executes the QuestEffectBeacon.
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.
97    @param bTriggered
98        true means the trigger was activated while false means it was deactivated
99    @param trigger
100        A pointer to the PlayerTrigger that threw the Event.
101    @return
102        Returns true if successfully executed, false if not.
103    */
104    //TODO: Eliminate MultiTriggerContainer stuff, since they are now PlayerTriggers as well.
105    bool QuestEffectBeacon::execute(bool bTriggered, BaseObject* trigger)
106    {
107        if(!bTriggered)
108        {
109            return false;
110        }
111        if(!(this->isActive())) // If the QuestEffectBeacon is inactive it cannot be executed.
112        {
113            COUT(4) << "The QuestEffectBeacon is inactive." << std::endl;
114            return false;
115        }
116
117        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
118        MultiTriggerContainer* mTrigger = orxonox_cast<MultiTriggerContainer*>(trigger);
119        Pawn* pawn = NULL;
120
121        // If the trigger is neither a Playertrigger nor a MultiTrigger (i.e. a MultitriggerContainer) we can do anything with it.
122        if(pTrigger == NULL && mTrigger == NULL)
123            return false;
124
125        // If the trigger is a PlayerTrigger.
126        if(pTrigger != NULL)
127        {
128            if(!pTrigger->isForPlayer())  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
129                return false;
130            else
131                pawn = pTrigger->getTriggeringPlayer();
132        }
133
134        // If the trigger is a MultiTrigger (i.e. a MultiTriggerContainer)
135        if(mTrigger != NULL)
136        {
137            pawn = orxonox_cast<Pawn*>(mTrigger->getData());
138        }
139
140        if(pawn == NULL)
141        {
142            COUT(4) << "The QuestEffectBeacon was triggered by an entity other than a Pawn. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
143            return false;
144        }
145
146        // Extract the PlayerInfo from the Pawn.
147        PlayerInfo* player = pawn->getPlayer();
148
149        if(player == NULL)
150        {
151            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
152            return false;
153        }
154
155        COUT(4) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;
156
157        bool check = QuestEffect::invokeEffects(player, this->effects_); // Invoke the QuestEffects on the PlayerInfo.
158        if(check)
159        {
160            this->decrementTimes(); // Decrement the number of times the beacon can be used.
161            return true;
162        }
163
164        return false;
165    }
166
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)
176    {
177        if(this->getTimes() == 0 && activate) // A QuestEffectBeacon that can be executed only 0 times is always inactive.
178            return false;
179
180        if(activate)
181        {
182            this->status_ = QuestEffectBeaconStatus::Active;
183            return true;
184        }
185
186        this->status_ = QuestEffectBeaconStatus::Inactive;
187        return true;
188    }
189
190    /**
191    @brief
192        Decrement the number of times the QuestEffectBeacon can be executed.
193    @return
194        Returns true if successful.
195    */
196    bool QuestEffectBeacon::decrementTimes(void)
197    {
198        if(!(this->isActive())) // The QuestEffectBeacon mus be active to decrement the number of times it can be executed.
199            return false;
200
201        if(this->getTimes() == INFINITE_TIME) // If times is infinity the QuestEffectBeacon can be executed an infinite number fo times.
202            return true;
203
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.
206            this->status_ = QuestEffectBeaconStatus::Inactive;
207
208        return true;
209    }
210
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    */
221    bool QuestEffectBeacon::setTimes(const int & n)
222    {
223        if(n < 0 && n != INFINITE_TIME)
224            return false;
225
226        this->times_ = n;
227        return true;
228    }
229
230    /**
231    @brief
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.
237    */
238    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
239    {
240        //TODO: Replace with assert.
241        if(effect == NULL) // NULL-pointers are not well liked here...
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
249        COUT(4) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
250        return true;
251    }
252
253    /**
254    @brief
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.
260    */
261    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
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;
268
269            i--;
270        }
271        return NULL;
272    }
273
274}
Note: See TracBrowser for help on using the repository browser.