Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/environment/src/modules/questsystem/QuestEffectBeacon.cc @ 8703

Last change on this file since 8703 was 7564, checked in by rgrieder, 14 years ago

Renamed all symbols called "check" because of macro collisions on OS X.
Inserted something like an assert in btGjkPairDetector.cpp with the hope that I don't have to modify that 'check' there.

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