Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/modules/objects/triggers/MultiTrigger.cc @ 7579

Last change on this file since 7579 was 7297, checked in by landauf, 14 years ago

fixed lots of Doxygen warnings

Note: Doxygen prints a warning if only a part of the parameters of a function are documented.

Added documentation for missing parameters (as good as I could), removed documentation of obsolete parameters and fixed names of renamed parameters.
Some parameters are tagged with "FIXME", please replace this with an appropriate documentation if you know what it does.

  • Property svn:eol-style set to native
File size: 24.1 KB
RevLine 
[6800]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 *      Benjamin Knecht
26 *
27*/
28
[6855]29/**
30    @file MultiTrigger.cc
31    @brief Implementation of the MultiTrigger class.
32*/
33
[6800]34#include "MultiTrigger.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38
39#include "MultiTriggerContainer.h"
40
41namespace orxonox
42{
[6853]43
44    // Initialization of some static (magic) variables.
[6800]45    /*static*/ const int MultiTrigger::INF_s = -1;
[6855]46    /*static*/ const std::string MultiTrigger::and_s = "and";
[6800]47    /*static*/ const std::string MultiTrigger::or_s = "or";
48    /*static*/ const std::string MultiTrigger::xor_s = "xor";
[7163]49
[6800]50    CreateFactory(MultiTrigger);
[6853]51
52    /**
53    @brief
54        Constructor. Registers the objects and initializes default values.
55    @param creator
56        The creator.
57    */
[6800]58    MultiTrigger::MultiTrigger(BaseObject* creator) : StaticEntity(creator)
59    {
60        RegisterObject(MultiTrigger);
[6853]61
62        this->bFirstTick_ = true;
[6800]63
[6855]64        this->delay_ = 0.0f;
[6800]65        this->bSwitch_ = false;
66        this->bStayActive_ = false;
[7163]67
[6800]68        this->remainingActivations_ = INF_s;
69        this->maxNumSimultaniousTriggerers_ = INF_s;
[6855]70
71        this->bInvertMode_ = false;
72        this->mode_ = MultiTriggerMode::EventTriggerAND;
[6859]73
[6864]74        this->bBroadcast_ = false;
75
[6859]76        this->parentTrigger_ = NULL;
[7163]77
[6800]78        this->targetMask_.exclude(Class(BaseObject));
79
80        this->setSyncMode(0x0);
81    }
[7163]82
[6853]83    /**
84    @brief
85        Destructor. Cleans up the state queue.
86    */
[6800]87    MultiTrigger::~MultiTrigger()
88    {
[6864]89        COUT(4) << "Destroying MultiTrigger &" << this << ". " << this->stateQueue_.size() << " states still in queue. Deleting." << std::endl;
[6800]90        while(this->stateQueue_.size() > 0)
91        {
92            MultiTriggerState* state = this->stateQueue_.front().second;
93            this->stateQueue_.pop_front();
94            delete state;
95        }
96    }
[7163]97
[6853]98    /**
99    @brief
100        Method for creating a MultiTrigger object through XML.
101        For a detailed description of the parameters please see the class description in the header file.
102    */
[6800]103    void MultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode)
104    {
105        SUPER(MultiTrigger, XMLPort, xmlelement, mode);
106
[6855]107        XMLPortParam(MultiTrigger, "delay", setDelay, getDelay, xmlelement, mode);
108        XMLPortParam(MultiTrigger, "switch", setSwitch, getSwitch, xmlelement, mode);
109        XMLPortParam(MultiTrigger, "stayactive", setStayActive, getStayActive, xmlelement, mode);
110        XMLPortParam(MultiTrigger, "activations", setActivations, getActivations, xmlelement, mode);
111        XMLPortParam(MultiTrigger, "simultaniousTriggerers", setSimultaniousTriggerers, getSimultaniousTriggerers, xmlelement, mode);
112        XMLPortParam(MultiTrigger, "invert", setInvert, getInvert, xmlelement, mode);
113        XMLPortParamTemplate(MultiTrigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&);
[6864]114        XMLPortParam(MultiTrigger, "broadcast", setBroadcast, getBroadcast, xmlelement, mode);
[6857]115        XMLPortParamLoadOnly(MultiTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn"); //TODO: Remove load only
[6800]116
[6853]117        //TODO: Maybe nicer with explicit subgroup, e.g. triggers
[6800]118        XMLPortObject(MultiTrigger, MultiTrigger, "", addTrigger, getTrigger, xmlelement, mode);
[7163]119
[6851]120        COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") created." << std::endl;
[6800]121    }
[6853]122
[7163]123
[6853]124    /**
125    @brief
126        A method that is executed each tick.
127    @param dt
128        The duration of the last tick.
129    */
[6800]130    void MultiTrigger::tick(float dt)
131    {
[6853]132        // If this is the first tick.
[7163]133        //TODO: Determine need for this, else kick it out.
[6853]134        if(this->bFirstTick_)
[6800]135        {
136            this->bFirstTick_ = false;
[6853]137            this->fire(false);
[6800]138        }
139
[6853]140        // Check if the object is active (this is NOT MultiTrigger::isActive()!)
[6800]141        if (!this->BaseObject::isActive())
142            return;
143
144        SUPER(MultiTrigger, tick, dt);
[6853]145
146        // Let the MultiTrigger return the states that trigger and process the new states if there are any.
[6800]147        std::queue<MultiTriggerState*>* queue  = this->letTrigger();
[6805]148        if(queue != NULL)
[6800]149        {
[6805]150            while(queue->size() > 0)
151            {
152                MultiTriggerState* state = queue->front();
[6853]153                // If the state is NULL. (This really shouldn't happen)
[6805]154                if(state == NULL)
[6853]155                {
156                    COUT(1) << "In MultiTrigger '" << this->getName() << "' (&" << this << "), Error: State of new states queue was NULL." << std::endl;
157                    queue->pop();
158                    continue;
159                }
[6851]160
[6853]161                // The new triggered state dependent on the requested state, the mode and the invert-mode.
[6851]162                bool bTriggered = (state->bTriggered & this->isModeTriggered(state->originator)) ^ this->bInvertMode_;
[6853]163
164                // If the 'triggered' state has changed a new state is added to the state queue.
165                //TODO: Do something against flooding, when there is delay.
166                if(this->delay_ != 0.0f || bTriggered ^ this->isTriggered(state->originator))
[6855]167                {
168                    state->bTriggered = bTriggered;
169                    this->addState(state);
170                }
171                else
[6859]172                {
173                    COUT(1) << "BUH" << std::endl;
[6855]174                    delete state;
[6859]175                }
[7163]176
[6805]177                queue->pop();
178            }
179            delete queue;
[6853]180        }
[6800]181
[6855]182        // Go through the state queue and activate all pending states whose remaining time has expired.
[6800]183        if (this->stateQueue_.size() > 0)
184        {
185            MultiTriggerState* state;
186            float timeRemaining;
[7163]187
[6853]188            // Go through all pending states.
[6800]189            for(int size = this->stateQueue_.size(); size >= 1; size--)
190            {
191                timeRemaining = this->stateQueue_.front().first;
192                state = this->stateQueue_.front().second;
[6853]193
194                // If the remaining time has expired, the state has to be set as the state of the MultiTrigger.
[6800]195                if(timeRemaining <= dt)
196                {
[6853]197                    // If the maximum number of objects simultaniously triggering this MultiTrigger is not exceeded.
[6800]198                    if(this->maxNumSimultaniousTriggerers_ == INF_s || this->triggered_.size() < (unsigned int)this->maxNumSimultaniousTriggerers_)
199                    {
[6853]200                        bool bStateChanged = false;
201                        // If the 'triggered' state is different form what it is now, change it.
202                        if(state->bTriggered ^ this->isTriggered(state->originator))
[6800]203                        {
[6853]204                            // Add the originator to the objects triggering this MultiTrigger.
205                            if(state->bTriggered == true)
206                            {
207                                this->triggered_.insert(state->originator);
208                            }
209                            // Remove the originator from the objects triggering this MultiTrigger.
210                            else
211                            {
212                                this->triggered_.erase(state->originator);
213                            }
[7163]214
[6853]215                            bStateChanged = true;
[6800]216                        }
[6853]217
[6855]218                        // Get the activity of the new state.
219                        bool bActive;
220                        // If the MultiTrigger is in switch mode.
221                        if(this->bSwitch_ && !state->bTriggered)
222                            bActive = this->isActive(state->originator);
223                        else
224                            bActive = !this->isActive(state->originator);
225
[6853]226                        // If the activity is different from what it is now, change it and fire an Event.
[6855]227                        if(bActive ^ this->isActive(state->originator))
[6800]228                        {
[6853]229
230                            bool bFire = true;
[7163]231
[6853]232                            // Add the originator to the objects activating this MultiTrigger.
[6855]233                            if(bActive == true)
[6853]234                            {
235                                if(this->remainingActivations_ != 0)
236                                {
237                                    this->active_.insert(state->originator);
238                                    if(this->remainingActivations_ != INF_s)
239                                        this->remainingActivations_--; // Decrement the remaining activations.
240                                }
241                                else
242                                {
243                                    bFire = false;
244                                }
245                            }
246                            // Remove the originator from the objects activating this MultiTrigger.
247                            else
248                            {
249                                if(!this->bStayActive_ || this->remainingActivations_ != 0)
250                                {
251                                    this->active_.erase(state->originator);
252                                }
253                                else
254                                {
255                                    bFire = false;
256                                }
257                            }
258
259                            // Fire the Event if the activity has changed.
260                            if(bFire)
261                            {
[6864]262                                if(this->bBroadcast_ && state->originator == NULL)
263                                {
264                                    this->broadcast(bActive);
265                                }
266                                else
267                                    this->fire(bActive, state->originator);
[7163]268
[6853]269                                bStateChanged = true;
270                            }
[6800]271                        }
[6853]272
273                        // Print some debug output if the state has changed.
274                        if(bStateChanged)
[6859]275                        {
[6864]276                            if(state->originator != NULL)
277                                COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") changed state. originator: " << state->originator->getIdentifier()->getName() << " (&" << state->originator << "), active: " << bActive << ", triggered: " << state->bTriggered << "." << std::endl;
278                            else
279                                COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") changed state. originator: NULL, active: " << bActive << ", triggered: " << state->bTriggered << "." << std::endl;
[6859]280                            if(this->parentTrigger_ != NULL)
[6864]281                                this->parentTrigger_->subTrigggerActivityChanged(state->originator);
[6859]282                        }
[6853]283
[6855]284                        // If the MultiTrigger has exceeded its amount of activations and it doesn't stay active, it has to be destroyed,
285                        if(this->remainingActivations_ == 0 && !bActive)
[6800]286                        {
[6855]287                            this->BaseObject::setActive(false);
288                            COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") ran out of activations. Setting it to inactive." << std::endl;
[6800]289                        }
290                    }
[7163]291
[6800]292                    // Remove the state from the state queue.
293                    this->stateQueue_.pop_front();
294                    delete state;
295                    size -= 1;
296                }
[6853]297                // If the remaining time has not yet expired. Decrement the remainig time.
[6800]298                else
299                {
300                    this->stateQueue_.push_back(std::pair<float, MultiTriggerState*>(timeRemaining-dt, state));
301                    this->stateQueue_.pop_front();
302                }
303            }
304        }
305    }
306
[6855]307    /**
308    @brief
309        Get whether the MultiTrigger is active for a given object.
[7297]310    @param triggerer
[6855]311        A pointer to the object.
312    @return
313        Returns true if the MultiTrigger is active, false if not.
314    */
315    bool MultiTrigger::isActive(BaseObject* triggerer)
[6800]316    {
[6855]317        std::set<BaseObject*>::iterator it = this->active_.find(triggerer);
318        if(it == this->active_.end())
319            return false;
320        return true;
321    }
322
323    /**
324    @brief
325        Set the mode of the MultiTrigger.
326    @param modeName
327        The name of the mode as a string.
328    */
329    void MultiTrigger::setMode(const std::string& modeName)
330    {
331        if (modeName == MultiTrigger::and_s)
332            this->setMode(MultiTriggerMode::EventTriggerAND);
333        else if (modeName == MultiTrigger::or_s)
334            this->setMode(MultiTriggerMode::EventTriggerOR);
335        else if (modeName == MultiTrigger::xor_s)
336            this->setMode(MultiTriggerMode::EventTriggerXOR);
337    }
338
339    /**
340    @brief
341        Get the mode of the MultiTrigger.
342    @return
343        Returns the mode as a string.
344    */
345    const std::string& MultiTrigger::getModeString() const
346    {
347        if (this->mode_ == MultiTriggerMode::EventTriggerAND)
348            return MultiTrigger::and_s;
349        else if (this->mode_ == MultiTriggerMode::EventTriggerOR)
350            return MultiTrigger::or_s;
351        else if (this->mode_ == MultiTriggerMode::EventTriggerXOR)
352            return MultiTrigger::xor_s;
353        else
354            return MultiTrigger::and_s;
355    }
356
357    /**
358    @brief
359        Add some target to the MultiTrigger.
360    @param targetStr
361        The target as a string.
362    */
363    void MultiTrigger::addTargets(const std::string& targetStr)
364    {
365        Identifier* target = ClassByString(targetStr);
366
367        if (target == NULL)
[6800]368        {
[6855]369            COUT(1) << "Error: \"" << targetStr << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl;
370            return;
371        }
[6800]372
[6855]373        this->targetMask_.include(target);
[6800]374
[6855]375        // A MultiTriggers shouldn't react to itself or other MultiTriggers.
376        this->targetMask_.exclude(Class(MultiTrigger), true);
377
378        // We only want WorldEntities
379        ClassTreeMask WEMask;
380        WEMask.include(Class(WorldEntity));
381        this->targetMask_ *= WEMask;
382
383        this->notifyMaskUpdate();
[6800]384    }
[6855]385
386    /**
387    @brief
388        Remove some target from the MultiTrigger.
389    @param targetStr
390        The target to be removed as a string.
391    */
392    void MultiTrigger::removeTargets(const std::string& targetStr)
393    {
394        Identifier* target = ClassByString(targetStr);
395        this->targetMask_.exclude(target);
396    }
397
398    /**
399    @brief
400        Adds a MultiTrigger as a sub-trigger to the trigger.
401        Beware: Loops are not prevented and potentially very bad, so just don't create any loops.
402    @param trigger
403        The trigger to be added.
404    */
405    void MultiTrigger::addTrigger(MultiTrigger* trigger)
406    {
407        if (this != trigger && trigger != NULL)
408            this->subTriggers_.insert(trigger);
[6859]409        trigger->addParentTrigger(this);
[6855]410    }
411
412    /**
413    @brief
414        Get the sub-trigger of this MultiTrigger at the given index.
415    @param index
416        The index.
417    @return
418        Returns a pointer ot the MultiTrigger. NULL if no such trigger exists.
419    */
420    const MultiTrigger* MultiTrigger::getTrigger(unsigned int index) const
421    {
422        // If the index is greater than the number of sub-triggers.
423        if (this->subTriggers_.size() <= index)
424            return NULL;
425
426        std::set<MultiTrigger*>::const_iterator it;
427        it = this->subTriggers_.begin();
428
429        for (unsigned int i = 0; i != index; ++i)
430            ++it;
431
432        return (*it);
433    }
434
435    /**
436    @brief
437        This method is called by the MultiTrigger to get information about new trigger events that need to be looked at.
438        This method is the device for the behaviour (the conditions under which the MultiTrigger triggers) of any derived class from MultiTrigger.
439    @return
440        A pointer to a queue of MultiTriggerState pointers is returned, containing all the neccessary information to decide whether these states should indeed become new states of the MultiTrigger.
441        Please be aware that both the queue and the states in the queue need to be deleted one they have been used. This is already done in the tick() method of this class but would have to be done by any method calling this method.
442    */
[6800]443    std::queue<MultiTriggerState*>* MultiTrigger::letTrigger(void)
444    {
[6859]445        return NULL;
[6800]446    }
[6864]447
448    /**
449    @brief
450        This method can be called by any class inheriting from MultiTrigger to change it's triggered status for a specified originator.
451
452        Compared to the letTrigger mode, which just polls and lets the pollee send it's state changed back, the changeTriggered method lets the trigger advertise its state changes just as they happen so it's much like the interrupt version of letTrigger.
453    @param originator
454        The originator which triggered the state change.
455    */
456    void MultiTrigger::changeTriggered(BaseObject* originator)
[6859]457    {
458        MultiTriggerState* state = new MultiTriggerState;
[6864]459        state->bTriggered = (!this->isTriggered(originator) & this->isModeTriggered(originator)) ^ this->bInvertMode_;
460        state->originator = originator;
461        this->addState(state);
462    }
463
464    /**
465    @brief
466        This method is called by any sub-trigger to advertise changes in it's state to it's parent-trigger.
467    @param originator
468        The object that caused the change in activity.
469    */
470    void MultiTrigger::subTrigggerActivityChanged(BaseObject* originator)
471    {
472        MultiTriggerState* state = new MultiTriggerState;
[6859]473        state->bTriggered = (this->isTriggered(originator) & this->isModeTriggered(originator)) ^ this->bInvertMode_;
474        state->originator = originator;
475        this->addState(state);
476    }
[6855]477
478    /**
479    @brief
480        Checks whether the sub-triggers are in such a way that, according to the mode of the MultiTrigger, the MultiTrigger is triggered (only considering the sub-triggers, not the state of MultiTrigger itself), for a given object.
481        To make an example: When the mode is 'and', then this would be true or a given object if all the sub-triggers were triggered ofr the given object.
482    @param triggerer
483        The object.
484    @return
485        Returns true if the MultiTrigger is triggered concerning it's sub-triggers.
486    */
487    bool MultiTrigger::isModeTriggered(BaseObject* triggerer)
488    {
489        if (this->subTriggers_.size() != 0)
490        {
491            bool returnVal = false;
492
493            switch (this->mode_)
494            {
495                case MultiTriggerMode::EventTriggerAND:
496                    returnVal = checkAnd(triggerer);
497                    break;
498                case MultiTriggerMode::EventTriggerOR:
499                    returnVal = checkOr(triggerer);
500                    break;
501                case MultiTriggerMode::EventTriggerXOR:
502                    returnVal = checkXor(triggerer);
503                    break;
504                default:
505                    returnVal = false;
506                    break;
507            }
508
509            return returnVal;
510        }
[7163]511
[6855]512        return true;
513    }
[7163]514
[6855]515    /**
516    @brief
517        Get whether the MultiTrigger is triggered for a given object.
518    @param triggerer
519        The object.
520    @return
521        Returns true if the MultiTrigger is triggered for the given object.
522    */
[6800]523    bool MultiTrigger::isTriggered(BaseObject* triggerer)
524    {
525        std::set<BaseObject*>::iterator it = this->triggered_.find(triggerer);
526        if(it == this->triggered_.end())
527            return false;
528        return true;
529    }
530
[6855]531    /**
532    @brief
[6864]533        Helper method. Creates an Event for the given status and originator and fires it.
[6855]534        Or more precisely creates a MultiTriggerContainer to encompass all neccesary information and creates an Event from that and sends it.
535    @param status
[6864]536        The status of the Event to be fired. This is equivalent to the activity of the MultiTrigger.
[6855]537    @param originator
538        The object that triggered the MultiTrigger to fire this Event.
539    */
540    void MultiTrigger::fire(bool status, BaseObject* originator)
541    {
542        // If the originator is NULL, a normal event without MultiTriggerContainer is sent.
543        if(originator == NULL)
544        {
545            this->fireEvent(status);
[6864]546            COUT(4) << "MultiTrigger '" <<  this->getName() << "' (&" << this << "): Fired event. status: " << status << "." << std::endl;
[6855]547            return;
548        }
[7163]549
[6855]550        MultiTriggerContainer* container = new MultiTriggerContainer(this, this, originator);
551        this->fireEvent(status, container);
552        COUT(4) << "MultiTrigger '" <<  this->getName() << "' (&" << this << "): Fired event. originator: " << originator->getIdentifier()->getName() << " (&" << originator << "), status: " << status << "." << std::endl;
553        delete container;
554    }
555
556    /**
557    @brief
[6864]558        Helper method. Broadcasts an Event for every object that is a target.
559    @param status
560        The status of the Events to be fired. This is equivalent to the activity of the MultiTrigger.
561    */
562    void MultiTrigger::broadcast(bool status)
563    {
564        ClassTreeMask& targetMask = this->getTargetMask();
[7163]565
[6864]566        for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it)
567        {
568            BaseObject* object = static_cast<BaseObject*>(*it);
569            this->fire(status, object);
570        }
571    }
572
573    /**
574    @brief
[6855]575        Helper method. Adds a state to the state queue, where the state will wait to become active.
576    @param state
577        The state to be added.
578    */
579    bool MultiTrigger::addState(MultiTriggerState* state)
580    {
581        assert(state);
[7163]582
[6855]583        // If the originator is no target of this MultiTrigger.
584        if(!this->isTarget(state->originator))
585            return false;
[7163]586
[6855]587        // Add it ot the state queue.
588        this->stateQueue_.push_back(std::pair<float, MultiTriggerState*>(this->delay_, state));
589
590        return true;
591    }
592
593    /**
594    @brief
595        Checks whether the sub-triggers amount to true for the 'and' mode for a given object.
596    @param triggerer
597        The object.
598    @return
599        Returns true if they do.
600    */
[6800]601    bool MultiTrigger::checkAnd(BaseObject* triggerer)
602    {
603        std::set<MultiTrigger*>::iterator it;
[6855]604        for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); ++it)
[6800]605        {
606            if (!(*it)->isActive(triggerer))
607                return false;
608        }
609        return true;
610    }
611
[6855]612    /**
613    @brief
614        Checks whether the sub-triggers amount to true for the 'or' mode for a given object.
615    @param triggerer
616        The object.
617    @return
618        Returns true if they do.
619    */
[6800]620    bool MultiTrigger::checkOr(BaseObject* triggerer)
621    {
622        std::set<MultiTrigger*>::iterator it;
[6855]623        for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); ++it)
[6800]624        {
625            if ((*it)->isActive(triggerer))
626                return true;
627        }
628        return false;
629    }
630
[6855]631    /**
632    @brief
633        Checks whether the sub-triggers amount to true for the 'xor' mode for a given object.
634    @param triggerer
635        The object.
636    @return
637        Returns true if they do.
638    */
[6800]639    bool MultiTrigger::checkXor(BaseObject* triggerer)
640    {
641        std::set<MultiTrigger*>::iterator it;
642        bool test = false;
[6855]643        for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); ++it)
[6800]644        {
645            if (test && (*it)->isActive(triggerer))
646                return false;
647            if ((*it)->isActive(triggerer))
648                test = true;
649        }
650        return test;
651    }
652
653}
Note: See TracBrowser for help on using the repository browser.