Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6855


Ignore:
Timestamp:
May 4, 2010, 9:54:59 PM (15 years ago)
Author:
dafrick
Message:

Done documenting MultiTrigger. Also some simplification and resilience improvement on the code done.

Location:
code/trunk/src/modules/objects/triggers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/objects/triggers/MultiTrigger.cc

    r6853 r6855  
    2727*/
    2828
     29/**
     30    @file MultiTrigger.cc
     31    @brief Implementation of the MultiTrigger class.
     32*/
     33
    2934#include "MultiTrigger.h"
    3035
     
    3944    // Initialization of some static (magic) variables.
    4045    /*static*/ const int MultiTrigger::INF_s = -1;
     46    /*static*/ const std::string MultiTrigger::and_s = "and";
    4147    /*static*/ const std::string MultiTrigger::or_s = "or";
    42     /*static*/ const std::string MultiTrigger::and_s = "and";
    4348    /*static*/ const std::string MultiTrigger::xor_s = "xor";
    4449   
     
    5156        The creator.
    5257    */
    53     //TODO: Clean up.
    5458    MultiTrigger::MultiTrigger(BaseObject* creator) : StaticEntity(creator)
    5559    {
     
    5761
    5862        this->bFirstTick_ = true;
    59        
    60         this->mode_ = MultiTriggerMode::EventTriggerAND;
    61 
    62         this->bInvertMode_ = false;
     63
     64        this->delay_ = 0.0f;
    6365        this->bSwitch_ = false;
    6466        this->bStayActive_ = false;
    65         this->delay_ = 0.0f;
     67       
    6668        this->remainingActivations_ = INF_s;
    6769        this->maxNumSimultaniousTriggerers_ = INF_s;
     70
     71        this->bInvertMode_ = false;
     72        this->mode_ = MultiTriggerMode::EventTriggerAND;
    6873       
    6974        this->targetMask_.exclude(Class(BaseObject));
    7075
    7176        this->setSyncMode(0x0);
    72        
    7377    }
    7478   
     
    8690            delete state;
    8791        }
    88 
    89         // Destroying the appended triggers
    90         for(std::set<MultiTrigger*>::iterator it = this->children_.begin(); it != this->children_.end(); it++)
    91         {
    92             (*it)->destroy();
    93         }
    94         this->children_.clear();
    95 
    96         // Telling everyone, that this trigger is gone.
    97         for(std::set<BaseObject*>::iterator it = this->active_.begin(); it != this->active_.end(); it++)
    98         {
    99             this->fire(false, *it);
    100         }
    101         this->active_.clear();
    10292    }
    10393   
     
    111101        SUPER(MultiTrigger, XMLPort, xmlelement, mode);
    112102
    113         XMLPortParam(MultiTrigger, "delay", setDelay, getDelay, xmlelement, mode).defaultValues(0.0f);
    114         XMLPortParam(MultiTrigger, "switch", setSwitch, getSwitch, xmlelement, mode).defaultValues(false);
    115         XMLPortParam(MultiTrigger, "stayactive", setStayActive, getStayActive, xmlelement, mode).defaultValues(false);
    116         XMLPortParam(MultiTrigger, "activations", setActivations, getActivations, xmlelement, mode).defaultValues(INF_s);
    117         XMLPortParam(MultiTrigger, "invert", setInvert, getInvert, xmlelement, mode).defaultValues(false);
    118         XMLPortParam(MultiTrigger, "simultaniousTriggerers", setSimultaniousTriggerers, getSimultaniousTriggerers, xmlelement, mode).defaultValues(INF_s);
    119         XMLPortParamTemplate(MultiTrigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&).defaultValues(MultiTrigger::and_s);
     103        XMLPortParam(MultiTrigger, "delay", setDelay, getDelay, xmlelement, mode);
     104        XMLPortParam(MultiTrigger, "switch", setSwitch, getSwitch, xmlelement, mode);
     105        XMLPortParam(MultiTrigger, "stayactive", setStayActive, getStayActive, xmlelement, mode);
     106        XMLPortParam(MultiTrigger, "activations", setActivations, getActivations, xmlelement, mode);
     107        XMLPortParam(MultiTrigger, "simultaniousTriggerers", setSimultaniousTriggerers, getSimultaniousTriggerers, xmlelement, mode);
     108        XMLPortParam(MultiTrigger, "invert", setInvert, getInvert, xmlelement, mode);
     109        XMLPortParamTemplate(MultiTrigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&);
    120110        XMLPortParamLoadOnly(MultiTrigger, "target", addTargets, xmlelement, mode).defaultValues("ControllableEntity"); //TODO: Remove load only
    121111
     
    155145            while(queue->size() > 0)
    156146            {
    157                 //TODO: Be more efficient, Don't delete a state and create a new one immediately after that. Reuse!
    158147                MultiTriggerState* state = queue->front();
    159148                // If the state is NULL. (This really shouldn't happen)
     
    171160                //TODO: Do something against flooding, when there is delay.
    172161                if(this->delay_ != 0.0f || bTriggered ^ this->isTriggered(state->originator))
    173                     this->addState(bTriggered, state->originator);
     162                {
     163                    state->bTriggered = bTriggered;
     164                    this->addState(state);
     165                }
     166                else
     167                    delete state;
    174168               
    175169                queue->pop();
    176                 delete state;
    177170            }
    178171            delete queue;
    179172        }
    180173
    181         // Go through the state queue.
     174        // Go through the state queue and activate all pending states whose remaining time has expired.
    182175        if (this->stateQueue_.size() > 0)
    183176        {
     
    215208                        }
    216209
     210                        // Get the activity of the new state.
     211                        bool bActive;
     212                        // If the MultiTrigger is in switch mode.
     213                        if(this->bSwitch_ && !state->bTriggered)
     214                            bActive = this->isActive(state->originator);
     215                        else
     216                            bActive = !this->isActive(state->originator);
     217
    217218                        // If the activity is different from what it is now, change it and fire an Event.
    218                         if(state->bActive ^ this->isActive(state->originator))
     219                        if(bActive ^ this->isActive(state->originator))
    219220                        {
    220221
     
    222223                           
    223224                            // Add the originator to the objects activating this MultiTrigger.
    224                             if(state->bActive == true)
     225                            if(bActive == true)
    225226                            {
    226227                                if(this->remainingActivations_ != 0)
     
    251252                            if(bFire)
    252253                            {
    253                                 this->fire(state->bActive, state->originator);
     254                                this->fire(bActive, state->originator);
    254255                                bStateChanged = true;
    255256                            }
     
    258259                        // Print some debug output if the state has changed.
    259260                        if(bStateChanged)
    260                             COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") changed state. originator: " << state->originator->getIdentifier()->getName() << " (&" << state->originator << "), active: " << state->bActive << ", triggered: " << state->bTriggered << "." << std::endl;
    261 
    262                         // If the MultiTrigger has exceeded its amount of activations and it desn't stay active, it has to be destroyed,
    263                         if(this->remainingActivations_ == 0 && bStateChanged && !state->bActive && !this->bStayActive_)
     261                            COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") changed state. originator: " << state->originator->getIdentifier()->getName() << " (&" << state->originator << "), active: " << bActive << ", triggered: " << state->bTriggered << "." << std::endl;
     262
     263                        // If the MultiTrigger has exceeded its amount of activations and it doesn't stay active, it has to be destroyed,
     264                        if(this->remainingActivations_ == 0 && !bActive)
    264265                        {
    265                             this->destroy();
     266                            this->BaseObject::setActive(false);
     267                            COUT(4) << "MultiTrigger '" << this->getName() << "' (&" << this << ") ran out of activations. Setting it to inactive." << std::endl;
    266268                        }
    267269                    }
     
    282284    }
    283285
    284     //TODO: Document
     286    /**
     287    @brief
     288        Get whether the MultiTrigger is active for a given object.
     289    @param triggerers
     290        A pointer to the object.
     291    @return
     292        Returns true if the MultiTrigger is active, false if not.
     293    */
     294    bool MultiTrigger::isActive(BaseObject* triggerer)
     295    {
     296        std::set<BaseObject*>::iterator it = this->active_.find(triggerer);
     297        if(it == this->active_.end())
     298            return false;
     299        return true;
     300    }
     301
     302    /**
     303    @brief
     304        Set the mode of the MultiTrigger.
     305    @param modeName
     306        The name of the mode as a string.
     307    */
     308    void MultiTrigger::setMode(const std::string& modeName)
     309    {
     310        if (modeName == MultiTrigger::and_s)
     311            this->setMode(MultiTriggerMode::EventTriggerAND);
     312        else if (modeName == MultiTrigger::or_s)
     313            this->setMode(MultiTriggerMode::EventTriggerOR);
     314        else if (modeName == MultiTrigger::xor_s)
     315            this->setMode(MultiTriggerMode::EventTriggerXOR);
     316    }
     317
     318    /**
     319    @brief
     320        Get the mode of the MultiTrigger.
     321    @return
     322        Returns the mode as a string.
     323    */
     324    const std::string& MultiTrigger::getModeString() const
     325    {
     326        if (this->mode_ == MultiTriggerMode::EventTriggerAND)
     327            return MultiTrigger::and_s;
     328        else if (this->mode_ == MultiTriggerMode::EventTriggerOR)
     329            return MultiTrigger::or_s;
     330        else if (this->mode_ == MultiTriggerMode::EventTriggerXOR)
     331            return MultiTrigger::xor_s;
     332        else
     333            return MultiTrigger::and_s;
     334    }
     335
     336    /**
     337    @brief
     338        Add some target to the MultiTrigger.
     339    @param targetStr
     340        The target as a string.
     341    */
     342    void MultiTrigger::addTargets(const std::string& targetStr)
     343    {
     344        Identifier* target = ClassByString(targetStr);
     345
     346        if (target == NULL)
     347        {
     348            COUT(1) << "Error: \"" << targetStr << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl;
     349            return;
     350        }
     351
     352        this->targetMask_.include(target);
     353
     354        // A MultiTriggers shouldn't react to itself or other MultiTriggers.
     355        this->targetMask_.exclude(Class(MultiTrigger), true);
     356
     357        // We only want WorldEntities
     358        ClassTreeMask WEMask;
     359        WEMask.include(Class(WorldEntity));
     360        this->targetMask_ *= WEMask;
     361
     362        this->notifyMaskUpdate();
     363    }
     364
     365    /**
     366    @brief
     367        Remove some target from the MultiTrigger.
     368    @param targetStr
     369        The target to be removed as a string.
     370    */
     371    void MultiTrigger::removeTargets(const std::string& targetStr)
     372    {
     373        Identifier* target = ClassByString(targetStr);
     374        this->targetMask_.exclude(target);
     375    }
     376
     377    /**
     378    @brief
     379        Adds a MultiTrigger as a sub-trigger to the trigger.
     380        Beware: Loops are not prevented and potentially very bad, so just don't create any loops.
     381    @param trigger
     382        The trigger to be added.
     383    */
     384    void MultiTrigger::addTrigger(MultiTrigger* trigger)
     385    {
     386        if (this != trigger && trigger != NULL)
     387            this->subTriggers_.insert(trigger);
     388    }
     389
     390    /**
     391    @brief
     392        Get the sub-trigger of this MultiTrigger at the given index.
     393    @param index
     394        The index.
     395    @return
     396        Returns a pointer ot the MultiTrigger. NULL if no such trigger exists.
     397    */
     398    const MultiTrigger* MultiTrigger::getTrigger(unsigned int index) const
     399    {
     400        // If the index is greater than the number of sub-triggers.
     401        if (this->subTriggers_.size() <= index)
     402            return NULL;
     403
     404        std::set<MultiTrigger*>::const_iterator it;
     405        it = this->subTriggers_.begin();
     406
     407        for (unsigned int i = 0; i != index; ++i)
     408            ++it;
     409
     410        return (*it);
     411    }
     412
     413    /**
     414    @brief
     415        This method is called by the MultiTrigger to get information about new trigger events that need to be looked at.
     416        This method is the device for the behaviour (the conditions under which the MultiTrigger triggers) of any derived class from MultiTrigger.
     417
     418        In this implementation it collects all objects triggering all sub-triggers nd the MultiTrigger itself and creates a state for each of them.
     419    @return
     420        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.
     421        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.
     422    */
     423    std::queue<MultiTriggerState*>* MultiTrigger::letTrigger(void)
     424    {
     425        // Goes through all sub-triggers and gets the objects triggering them.
     426        std::set<BaseObject*>* triggerers = new std::set<BaseObject*>();
     427        std::set<BaseObject*>::iterator objIt;
     428        for(std::set<MultiTrigger*>::iterator it = this->subTriggers_.begin(); it != this->subTriggers_.end(); it ++)
     429        {
     430            std::set<BaseObject*> set = (*it)->getActive();
     431            for(objIt = set.begin(); objIt != set.end(); objIt++)
     432            {
     433                triggerers->insert(*objIt);
     434            }
     435        }
     436
     437        // Goes through all the triggerers of this trigger.
     438        for(objIt = this->active_.begin(); objIt != this->active_.end(); objIt++)
     439        {
     440            triggerers->insert(*objIt);
     441        }
     442
     443        // If no objects are triggering this MultiTrigger or the sub-triggers.
     444        if(triggerers->size() == 0)
     445            return NULL;
     446
     447        // Create a state for each object triggering this MultiTrigger or any of the sub-triggers and append it to the queue.
     448        std::queue<MultiTriggerState*>* queue = new std::queue<MultiTriggerState*>();
     449        MultiTriggerState* state = NULL;
     450        for(std::set<BaseObject*>::iterator it = triggerers->begin(); it != triggerers->end(); it++)
     451        {
     452            state = new MultiTriggerState;
     453            state->bTriggered = true;
     454            state->originator = *it;
     455            queue->push(state);
     456        }
     457        delete triggerers;
     458
     459        return queue;
     460    }
     461
     462    /**
     463    @brief
     464        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.
     465        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.
     466    @param triggerer
     467        The object.
     468    @return
     469        Returns true if the MultiTrigger is triggered concerning it's sub-triggers.
     470    */
    285471    bool MultiTrigger::isModeTriggered(BaseObject* triggerer)
    286472    {
    287         if (this->children_.size() != 0)
     473        if (this->subTriggers_.size() != 0)
    288474        {
    289475            bool returnVal = false;
     
    311497    }
    312498   
    313     //TODO: Document
    314     std::queue<MultiTriggerState*>* MultiTrigger::letTrigger(void)
    315     {
    316         // Goes through all trigger children and gets the objects triggering them.
    317         std::set<BaseObject*>* triggerers = new std::set<BaseObject*>();
    318         std::set<BaseObject*>::iterator objIt;
    319         for(std::set<MultiTrigger*>::iterator it = this->children_.begin(); it != this->children_.end(); it ++)
    320         {
    321             std::set<BaseObject*> set = (*it)->getActive();
    322             for(objIt = set.begin(); objIt != set.end(); objIt++)
    323             {
    324                 triggerers->insert(*objIt);
    325             }
    326         }
    327 
    328         // Goes through all the triggerers of this trigger.
    329         for(objIt = this->active_.begin(); objIt != this->active_.end(); objIt++)
    330         {
    331             triggerers->insert(*objIt);
    332         }
    333 
    334         if(triggerers->size() == 0)
    335             return NULL;
    336        
    337         std::queue<MultiTriggerState*>* queue = new std::queue<MultiTriggerState*>();
    338         MultiTriggerState* state = NULL;
    339         for(std::set<BaseObject*>::iterator it = triggerers->begin(); it != triggerers->end(); it++)
    340         {
    341             state = new MultiTriggerState;
    342             state->bTriggered = true;
    343             state->originator = *it;
    344             queue->push(state);
    345         }
    346         delete triggerers;
    347        
    348         return queue;
    349     }
    350    
    351     //TODO: Document
     499    /**
     500    @brief
     501        Get whether the MultiTrigger is triggered for a given object.
     502    @param triggerer
     503        The object.
     504    @return
     505        Returns true if the MultiTrigger is triggered for the given object.
     506    */
    352507    bool MultiTrigger::isTriggered(BaseObject* triggerer)
    353508    {
     
    358513    }
    359514
    360     //TODO: Document
     515    /**
     516    @brief
     517        Helper method. Creates an event for the given status and originator and fires it.
     518        Or more precisely creates a MultiTriggerContainer to encompass all neccesary information and creates an Event from that and sends it.
     519    @param status
     520        The status of the event to be fired. This is equivalent to the activity of the MultiTrigger.
     521    @param originator
     522        The object that triggered the MultiTrigger to fire this Event.
     523    */
     524    void MultiTrigger::fire(bool status, BaseObject* originator)
     525    {
     526        // If the originator is NULL, a normal event without MultiTriggerContainer is sent.
     527        if(originator == NULL)
     528        {
     529            this->fireEvent(status);
     530            return;
     531        }
     532       
     533        MultiTriggerContainer* container = new MultiTriggerContainer(this, this, originator);
     534        this->fireEvent(status, container);
     535        COUT(4) << "MultiTrigger '" <<  this->getName() << "' (&" << this << "): Fired event. originator: " << originator->getIdentifier()->getName() << " (&" << originator << "), status: " << status << "." << std::endl;
     536        delete container;
     537    }
     538
     539    /**
     540    @brief
     541        Helper method. Adds a state to the state queue, where the state will wait to become active.
     542    @param state
     543        The state to be added.
     544    */
     545    bool MultiTrigger::addState(MultiTriggerState* state)
     546    {
     547        assert(state);
     548       
     549        // If the originator is no target of this MultiTrigger.
     550        if(!this->isTarget(state->originator))
     551            return false;
     552       
     553        // Add it ot the state queue.
     554        this->stateQueue_.push_back(std::pair<float, MultiTriggerState*>(this->delay_, state));
     555
     556        return true;
     557    }
     558
     559    /**
     560    @brief
     561        Checks whether the sub-triggers amount to true for the 'and' mode for a given object.
     562    @param triggerer
     563        The object.
     564    @return
     565        Returns true if they do.
     566    */
    361567    bool MultiTrigger::checkAnd(BaseObject* triggerer)
    362568    {
    363569        std::set<MultiTrigger*>::iterator it;
    364         for(it = this->children_.begin(); it != this->children_.end(); ++it)
     570        for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); ++it)
    365571        {
    366572            if (!(*it)->isActive(triggerer))
     
    370576    }
    371577
    372     //TODO: Document
     578    /**
     579    @brief
     580        Checks whether the sub-triggers amount to true for the 'or' mode for a given object.
     581    @param triggerer
     582        The object.
     583    @return
     584        Returns true if they do.
     585    */
    373586    bool MultiTrigger::checkOr(BaseObject* triggerer)
    374587    {
    375588        std::set<MultiTrigger*>::iterator it;
    376         for(it = this->children_.begin(); it != this->children_.end(); ++it)
     589        for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); ++it)
    377590        {
    378591            if ((*it)->isActive(triggerer))
     
    382595    }
    383596
    384     //TODO: Document
     597    /**
     598    @brief
     599        Checks whether the sub-triggers amount to true for the 'xor' mode for a given object.
     600    @param triggerer
     601        The object.
     602    @return
     603        Returns true if they do.
     604    */
    385605    bool MultiTrigger::checkXor(BaseObject* triggerer)
    386606    {
    387607        std::set<MultiTrigger*>::iterator it;
    388608        bool test = false;
    389         for(it = this->children_.begin(); it != this->children_.end(); ++it)
     609        for(it = this->subTriggers_.begin(); it != this->subTriggers_.end(); ++it)
    390610        {
    391611            if (test && (*it)->isActive(triggerer))
     
    396616        return test;
    397617    }
    398    
    399     //TODO: Document
    400     bool MultiTrigger::addState(bool bTriggered, BaseObject* originator)
    401     {
    402         if(!this->isTarget(originator))
    403             return false;
    404        
    405         // If the state doesn't change.
    406         if(this->isTriggered() && bTriggered)
    407             return false;
    408        
    409         bool bActive = !this->isActive(originator);
    410        
    411         // If the MultiTrigger is in switch mode.
    412         if(this->bSwitch_ && !bTriggered)
    413         {
    414             bActive = this->isActive(originator);
    415         }
    416        
    417         // Create state.
    418         MultiTriggerState* state = new MultiTriggerState;
    419         state->bActive = bActive;
    420         state->bTriggered = bTriggered;
    421         state->originator = originator;
    422         this->stateQueue_.push_back(std::pair<float, MultiTriggerState*>(this->delay_, state));
    423        
    424         return true;
    425     }
    426 
    427     //TODO: Document
    428     void MultiTrigger::setMode(const std::string& modeName)
    429     {
    430         if (modeName == MultiTrigger::and_s)
    431             this->setMode(MultiTriggerMode::EventTriggerAND);
    432         else if (modeName == MultiTrigger::or_s)
    433             this->setMode(MultiTriggerMode::EventTriggerOR);
    434         else if (modeName == MultiTrigger::xor_s)
    435             this->setMode(MultiTriggerMode::EventTriggerXOR);
    436     }
    437 
    438     //TODO: Document
    439     const std::string& MultiTrigger::getModeString() const
    440     {
    441         if (this->mode_ == MultiTriggerMode::EventTriggerAND)
    442             return MultiTrigger::and_s;
    443         else if (this->mode_ == MultiTriggerMode::EventTriggerOR)
    444             return MultiTrigger::or_s;
    445         else if (this->mode_ == MultiTriggerMode::EventTriggerXOR)
    446             return MultiTrigger::xor_s;
    447         else
    448             return MultiTrigger::and_s;
    449     }
    450    
    451     //TODO: Document
    452     bool MultiTrigger::isActive(BaseObject* triggerer)
    453     {
    454         std::set<BaseObject*>::iterator it = this->active_.find(triggerer);
    455         if(it == this->active_.end())
    456             return false;
    457         return true;
    458     }
    459 
    460     //TODO: Document
    461     void MultiTrigger::addTrigger(MultiTrigger* trigger)
    462     {
    463         if (this != trigger && trigger != NULL)
    464             this->children_.insert(trigger);
    465     }
    466 
    467     //TODO: Document
    468     const MultiTrigger* MultiTrigger::getTrigger(unsigned int index) const
    469     {
    470         if (this->children_.size() <= index)
    471             return NULL;
    472 
    473         std::set<MultiTrigger*>::const_iterator it;
    474         it = this->children_.begin();
    475 
    476         for (unsigned int i = 0; i != index; ++i)
    477             ++it;
    478 
    479         return (*it);
    480     }
    481    
    482     //TODO: Document
    483     void MultiTrigger::fire(bool status, BaseObject* originator)
    484     {
    485         if(originator == NULL)
    486         {
    487             this->fireEvent(status);
    488             return;
    489         }
    490         MultiTriggerContainer* container = new MultiTriggerContainer(this, this, originator);
    491         this->fireEvent(status, container);
    492         COUT(4) << "MultiTrigger &" << this << ": Fired event. originator: " << originator->getIdentifier()->getName() << " (&" << originator << "), status: " << status << "." << std::endl;
    493         delete container;
    494     }
    495    
    496     //TODO: Document
    497     void MultiTrigger::addTargets(const std::string& targets)
    498     {
    499         Identifier* target = ClassByString(targets);
    500 
    501         if (target == NULL)
    502         {
    503             COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl;
    504             return;
    505         }
    506 
    507         this->targetMask_.include(target);
    508 
    509         // trigger shouldn't react on itself or other triggers
    510         this->targetMask_.exclude(Class(MultiTrigger), true);
    511 
    512         // we only want WorldEntities
    513         ClassTreeMask WEMask;
    514         WEMask.include(Class(WorldEntity));
    515         this->targetMask_ *= WEMask;
    516 
    517         this->notifyMaskUpdate();
    518     }
    519 
    520     //TODO: Document
    521     void MultiTrigger::removeTargets(const std::string& targets)
    522     {
    523         Identifier* target = ClassByString(targets);
    524         this->targetMask_.exclude(target);
    525     }
    526    
     618
    527619}
  • code/trunk/src/modules/objects/triggers/MultiTrigger.h

    r6853 r6855  
    2525 *      Benjamin Knecht
    2626 *
     27*/
     28
     29/**
     30    @file MultiTrigger.h
     31    @brief Definition of the MultiTrigger class.
    2732*/
    2833
     
    5964    {
    6065        BaseObject* originator;
    61         bool bActive;
    6266        bool bTriggered;
    6367    };
     
    9397            virtual void tick(float dt); //!< A method that is executed each tick.
    9498           
    95             bool isActive(BaseObject* triggerer = NULL);
    96             void addTrigger(MultiTrigger* trigger);
    97             const MultiTrigger* getTrigger(unsigned int index) const;
    98            
    99             void addTargets(const std::string& targets);
    100             void removeTargets(const std::string& targets);
     99            bool isActive(BaseObject* triggerer = NULL); //!< Get whether the MultiTrigger is active for a given object.
     100
     101            /**
     102            @brief Set the delay of the MultiTrigger.
     103            @param delay The delay to be set.
     104            */
     105            inline void setDelay(float delay)
     106                { if(delay > 0) this->delay_= delay; }
     107            /**
     108            @brief Get the delay of the MultiTrigger.
     109            @return The delay.
     110            */
     111            inline float getDelay() const
     112                { return this->delay_; }
     113
     114            /**
     115            @brief Set switch-mode of the MultiTrigger.
     116            @param bSwitch If true the MultiTrigger is set to switched.
     117            */
     118            inline void setSwitch(bool bSwitch)
     119                { this->bSwitch_ = bSwitch; }
     120            /**
     121            @brief Get the switch-mode of the MultiTrigger.
     122            @return Returns true if the MultiTriger is in switch-mode.
     123            */
     124            inline bool getSwitch() const
     125                { return this->bSwitch_; }
     126
     127            /**
     128            @brief Set the stay-active-mode of the MultiTrigger.
     129            @param bStayActive If true the MultiTrigger is set to stay active.
     130            */
     131            inline void setStayActive(bool bStayActive)
     132                { this->bStayActive_ = bStayActive; }
     133            /**
     134            @brief Get the stay-active-mode of the MultiTrigger.
     135            @return Returns true if the MultiTrigger stays active.
     136            */
     137            inline bool getStayActive() const
     138                { return this->bStayActive_; }
     139
     140            /**
     141            @brief Set the number of activations the MultiTrigger can go through.
     142            @param activations The number of activations. -1 denotes infinitely many activations.
     143            */
     144            inline void setActivations(int activations)
     145                { if(activations >= 0 || activations == INF_s) this->remainingActivations_ = activations; }
     146            /**
     147            @brief Get the number of remaining activations of the MultiTrigger.
     148            @return The number of activations. -1 denotes infinity.
     149            */
     150            inline int getActivations() const
     151                { return this->remainingActivations_; }
     152
     153            /**
     154            @brief Set the number of objects that are allowed to simultaniously trigger this MultiTrigger.
     155            @param triggerers The number of objects. -1 denotes infinitely many.
     156            */
     157            inline void setSimultaniousTriggerers(int triggerers)
     158                { if(triggerers >= 0 || triggerers == INF_s) this->maxNumSimultaniousTriggerers_ = triggerers; }
     159            /**
     160            @brief Get the number of objects that are allowed to simultaniously trigger this MultiTriggger.
     161            @return Returns the number of objects. -1 denotes infinity.
     162            */
     163            inline int getSimultaniousTriggerers(void)
     164                { return this->maxNumSimultaniousTriggerers_; }
     165
     166            /**
     167            @brief Set the invert-mode of the MultiTrigger.
     168            @param bInvert If true the MultiTrigger is set to invert.
     169            */
     170            inline void setInvert(bool bInvert)
     171                { this->bInvertMode_ = bInvert; }
     172            /**
     173            @brief Get the invert-mode of the MultiTrigger.
     174            @return Returns true if the MultiTrigger is set to invert.
     175            */
     176            inline bool getInvert() const
     177                { return this->bInvertMode_; }
     178
     179            void setMode(const std::string& modeName); //!< Set the mode of the MultiTrigger.
     180            /**
     181            @brief Set the mode of the MultiTrigger.
     182            @param mode The mode of the MultiTrigger.
     183            */
     184            inline void setMode(MultiTriggerMode::Value mode) //!< Get the mode of the MultiTrigger.
     185                { this->mode_ = mode; }
     186            const std::string& getModeString(void) const;
     187            /**
     188            @brief Get the mode of the MultiTrigger.
     189            @return Returns and Enum for the mode of the MultiTrigger.
     190            */
     191            inline MultiTriggerMode::Value getMode() const
     192                { return mode_; }
     193
     194            /**
     195            @brief Get whether the input object is a target of the MultiTrigger.
     196            @param target A pointer to the object.
     197            @return Returns true if the input object is a target, false if not.
     198            */
    101199            inline bool isTarget(BaseObject* target)
    102200                { return targetMask_.isIncluded(target->getIdentifier()); }
    103            
    104             void setMode(const std::string& modeName);
    105             const std::string& getModeString(void) const;
    106             inline void setMode(MultiTriggerMode::Value mode)
    107                 { this->mode_ = mode; }
    108             inline MultiTriggerMode::Value getMode() const
    109                 { return mode_; }
    110 
    111             inline void setInvert(bool bInvert)
    112                 { this->bInvertMode_ = bInvert; }
    113             inline bool getInvert() const
    114                 { return this->bInvertMode_; }
    115 
    116             inline void setSwitch(bool bSwitch)
    117                 { this->bSwitch_ = bSwitch; }
    118             inline bool getSwitch() const
    119                 { return this->bSwitch_; }
    120 
    121             inline void setStayActive(bool bStayActive)
    122                 { this->bStayActive_ = bStayActive; }
    123             inline bool getStayActive() const
    124                 { return this->bStayActive_; }
    125 
    126             inline void setActivations(int activations)
    127                 { this->remainingActivations_ = activations; }
    128             inline int getActivations() const
    129                 { return this->remainingActivations_; }
    130                
    131             inline void setSimultaniousTriggerers(int triggerers)
    132                 { this->maxNumSimultaniousTriggerers_ = triggerers; }
    133             inline int getSimultaniousTriggerers(void)
    134                 { return this->maxNumSimultaniousTriggerers_; }
    135 
    136             inline void setDelay(float delay)
    137                 { this->delay_= delay; }
    138             inline float getDelay() const
    139                 { return this->delay_; }
     201            void addTargets(const std::string& targets); //!< Add some target to the MultiTrigger.
     202            void removeTargets(const std::string& targets); //!< Remove some target from the MultiTrigger.
     203           
     204            void addTrigger(MultiTrigger* trigger); //!< Adds a MultiTrigger as a sub-trigger to the trigger.
     205            const MultiTrigger* getTrigger(unsigned int index) const; //!< Get the sub-trigger of this MultiTrigger at the given index.
    140206           
    141207        protected:
    142             void fire(bool status, BaseObject* originator = NULL);
    143            
    144             bool isModeTriggered(BaseObject* triggerer = NULL);
    145             bool isTriggered(BaseObject* triggerer = NULL);
    146            
    147             virtual std::queue<MultiTriggerState*>* letTrigger(void);
    148            
     208            virtual std::queue<MultiTriggerState*>* letTrigger(void); //!< This method is called by the MultiTrigger to get information about new trigger events that need to be looked at.
     209           
     210            bool isModeTriggered(BaseObject* triggerer = NULL); //!< Checks whetherx the MultiTrigger is triggered concerning it's sub-triggers.
     211            bool isTriggered(BaseObject* triggerer = NULL); //!< Get whether the MultiTrigger is triggered for a given object.
     212
     213            void fire(bool status, BaseObject* originator = NULL);  //!< Helper method. Creates an event for the given status and originator and fires it.
     214
     215            /**
     216            @brief Get the target mask used to identify the targets of this MultiTrigger.
     217            @return Returns the target mask.
     218            */
    149219            inline ClassTreeMask& getTargetMask(void)
    150220                { return this->targetMask_; }
     221            /**
     222            @brief Is called, when the target mask changes.
     223            */
     224            //TODO: Check if something mus be done here.
    151225            virtual void notifyMaskUpdate(void) {}
    152226           
    153227        private:
    154             static const int INF_s;
     228            static const int INF_s; //!< Magic number for infinity.
     229            //! Magic strings for the mode.
     230            static const std::string and_s;
    155231            static const std::string or_s;
    156             static const std::string and_s;
    157232            static const std::string xor_s;
    158233           
    159             bool addState(bool bTriggered, BaseObject* originator = NULL);
    160            
    161             bool checkAnd(BaseObject* triggerer);
    162             bool checkOr(BaseObject* triggerer);
    163             bool checkXor(BaseObject* triggerer);
    164 
     234            bool addState(MultiTriggerState* state); //!< Helper method. Adds a state to the state queue, where the state will wait to become active.
     235           
     236            bool checkAnd(BaseObject* triggerer); //!< Checks whether the sub-triggers amount to true for the 'and' mode for a given object.
     237            bool checkOr(BaseObject* triggerer); //!< Checks whether the sub-triggers amount to true for the 'or' mode for a given object.
     238            bool checkXor(BaseObject* triggerer); //!< Checks whether the sub-triggers amount to true for the 'xor' mode for a given object.
     239
     240            /**
     241            @brief Get the objects for which this MultiTrigger is active.
     242            @return Returns a set with all the object this MultiTrigger is active for.
     243            */
    165244            std::set<BaseObject*>& getActive(void)
    166245                { return this->active_; }
    167246
    168             bool bFirstTick_;
    169 
    170             MultiTriggerMode::Value mode_;
    171             bool bInvertMode_;
    172             bool bSwitch_;
    173             bool bStayActive_;
    174             float delay_;
    175             int remainingActivations_;
    176             int maxNumSimultaniousTriggerers_;
    177            
    178             std::set<BaseObject*> active_;
    179             std::set<BaseObject*> triggered_;
    180 
    181             std::set<MultiTrigger*> children_;
    182             std::deque<std::pair<float, MultiTriggerState*> > stateQueue_;
    183            
    184             ClassTreeMask targetMask_;
     247            bool bFirstTick_; //!< Bool to distinguish the first tick form all the following.
     248
     249            float delay_; //!< The delay that is imposed on all new trigger events.
     250            bool bSwitch_; //!< Bool for the switch-mode, if true the MultiTrigger behaves like a switch.
     251            bool bStayActive_; //!< Bool for the stay-active-mode, if true the MultiTrigger stays active after its last activation.;
     252           
     253            int remainingActivations_; //!< The remaining activations of this MultiTrigger.
     254            int maxNumSimultaniousTriggerers_; //!< The maximum number of objects simultaniously trigggering this MultiTrigger.
     255
     256            bool bInvertMode_; //!< Bool for the invert-mode, if true the MultiTrigger is inverted.
     257            MultiTriggerMode::Value mode_; //!< The mode of the MultiTrigger.
     258
     259            std::set<MultiTrigger*> subTriggers_; //!< The sub-triggers of this MultiTrigger.
     260           
     261            std::set<BaseObject*> active_; //!< The set of all objects the MultiTrigger is active for.
     262            std::set<BaseObject*> triggered_; //!< The set of all objects the MultiTrigger is triggered for.
     263
     264            std::deque< std::pair<float, MultiTriggerState*> > stateQueue_; //!< The queue of states waiting to become active.
     265           
     266            ClassTreeMask targetMask_; //!< The target mask, masking all objects that can trigger this MultiTrigger.
    185267           
    186268    };
Note: See TracChangeset for help on using the changeset viewer.