Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel2/src/modules/objects/triggers/MultiTrigger.cc @ 8526

Last change on this file since 8526 was 8371, checked in by dafrick, 14 years ago

Merging tutoriallevel branch into tutoriallevel2 branch.

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