Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7290 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

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