Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/modules/questsystem/QuestEffectBeacon.cc @ 8016

Last change on this file since 8016 was 7552, checked in by dafrick, 14 years ago

Resolving some TODOs and doing some additional cleanup. Almost done now…

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