Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/triggers/MultiTrigger.h @ 6853

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

Started documenting, also changed some of the implementation to avoid possible bugs.

File size: 10.0 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 *      Benjamin Knecht
26 *
27*/
28
29#ifndef _MultiTrigger_H__
30#define _MultiTrigger_H__
31
32#include "objects/ObjectsPrereqs.h"
33
34#include "core/BaseObject.h"
35#include "core/ClassTreeMask.h"
36
37#include <set>
38#include <deque>
39
40#include "tools/interfaces/Tickable.h"
41#include "worldentities/StaticEntity.h"
42
43namespace orxonox
44{
45
46    //! The different modes the MultiTrigger can be in.
47    namespace MultiTriggerMode
48    {
49        enum Value
50        {
51            EventTriggerAND,
52            EventTriggerOR,
53            EventTriggerXOR,
54        };
55    }
56
57    //! Struct to handle MultiTrigger states internally.
58    struct MultiTriggerState
59    {
60        BaseObject* originator;
61        bool bActive;
62        bool bTriggered;
63    };
64
65    /**
66    @brief
67        The MultiTrigger class implements a trigger that has a distinct state for each object triggering it.
68        In more detail: A Trigger is an object that can either be active or inactive, whith a specified behaviour how to switch between the two. A MultiTrigger generalizes that behaviour for multiple objects trigggering the trigger. A MultiTrigger can be active or inactive for any object triggering it, with the state for each object being completely independent of the state for other objects. Each time a switch occurs an Event is fired with as the originator a MultiTriggerContainer, containig a pointer to the pointer that caused the Event and a pointer to the object that caused the trigger to change it's activity.
69
70        MultiTriggers also allow for additional complexity which can be added trough the choice of the parameters explained (briefly) below:
71        But first you must understand a small implementational detail. There is a distinction between the MultiTrigger being triggered (there is the state 'triggered' for that) and the MultiTrigger being active (for that is the state 'activity'). From the outside only the activity is visible. The state 'triggered' tells us whether the trigger is actually triggered, but it could pretend (for some reason, some of which we will see shortly) to be triggered (or to the outside, active), while it in fact isn't. The standard behaviour is, that the cativity changes, when the MultiTrigger transits from being triggered to not being triggered or the other way around.
72        The parameters are:
73            'delay':                    The delay is the time that the trigger waits until it reacts (i.e. changes it's state) to the triggering condition being fulfilled.
74            'switch':                   Switch is a bool, if true the MultiTrigger is in switch-mode, meaning, that the activity changes only when the trigger is triggered , this means, that now the activity only changes, when the trigger changes from not being triggered to being triggered but not the other way around. The default is false.
75            'stayactive':               Stay active is also a bool, if true the MultiTrigger stays active after it has been activated as many times as specified by the parameter activations. The default is -1, which is infinity.
76            'activations':              The number of activations until the trigger can't be triggered anymore. The default is -1, which is infinity.
77            'invert':                   Invert is a bool, if true the trigger is in invert-mode, meaning, that if the triggering condition is fulfilled the MultiTrigger will have the state not triggered and and if the condition is not fulfilled it will have the state triggered. In short it just inverts the behaviour of the MultiTrigger. The default is false.
78            'simultaniousTriggerers':   The number of simultanious triggerers limits the number of object that are allowed to trigger the MultiTrigger at the same time. Or a little more precisely, the number of distinct objects the MultiTrigger has 'triggered' states for, at each point in time.
79            'mode':                     The mode describes how the MultiTrigger acts in relation to all the MultiTriggers, that are appended to it. There are 3 modes: 'and', meaning that the MultiTrigger can only be triggered if all the appended MultiTriggers are active. 'or', meaning that the MultiTrigger can only triggered if at least one of the appendend MultiTriggers is active. And 'xor', meaning that the MultiTrigger can only be triggered if one and only one appended MultiTrigger is active. Notice, that I wrote 'can only be active', that implies, that there is an addtitional condition to the activity of the MultiTrigger and that is the fulfillment of the triggering condition (the MultiTrigger itself doesn't have one, but all derived classes should). Also bear in mind, that the activity of a MultiTrigger is still coupled to the object that triggered it. The default is 'and'.
80            'target':                   The target describes the kind of objects that are allowed to trigger this MultiTrigger. The default is 'ControllableEntity'.
81            Also there is the possibility of appending MultiTriggers to the MultiTrigger just by adding them as subobjects in the XML description of your MultiTrigger.
82
83    @author
84        Damian 'Mozork' Frick
85    */
86    class _ObjectsExport MultiTrigger : public StaticEntity, public Tickable
87    {
88        public:
89            MultiTrigger(BaseObject* creator); //!< Constructor. Registers the objects and initializes default values.
90            ~MultiTrigger(); //!< Destructor.
91           
92            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a MultiTrigger object through XML.
93            virtual void tick(float dt); //!< A method that is executed each tick.
94           
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);
101            inline bool isTarget(BaseObject* target)
102                { 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_; }
140           
141        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           
149            inline ClassTreeMask& getTargetMask(void)
150                { return this->targetMask_; }
151            virtual void notifyMaskUpdate(void) {}
152           
153        private:
154            static const int INF_s;
155            static const std::string or_s;
156            static const std::string and_s;
157            static const std::string xor_s;
158           
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
165            std::set<BaseObject*>& getActive(void)
166                { return this->active_; }
167
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_;
185           
186    };
187
188}
189
190#endif // _MultiTrigger_H__
Note: See TracBrowser for help on using the repository browser.