Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Documented and simplified DistanceMultiTrigger.

File size: 15.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/**
30    @file MultiTrigger.h
31    @brief Definition of the MultiTrigger class.
32*/
33
34#ifndef _MultiTrigger_H__
35#define _MultiTrigger_H__
36
37#include "objects/ObjectsPrereqs.h"
38
39#include "core/BaseObject.h"
40#include "core/ClassTreeMask.h"
41
42#include <set>
43#include <deque>
44
45#include "tools/interfaces/Tickable.h"
46#include "worldentities/StaticEntity.h"
47
48namespace orxonox
49{
50
51    //! The different modes the MultiTrigger can be in.
52    namespace MultiTriggerMode
53    {
54        enum Value
55        {
56            EventTriggerAND,
57            EventTriggerOR,
58            EventTriggerXOR,
59        };
60    }
61
62    //! Struct to handle MultiTrigger states internally.
63    struct MultiTriggerState
64    {
65        BaseObject* originator;
66        bool bTriggered;
67    };
68
69    /**
70    @brief
71        The MultiTrigger class implements a trigger that has a distinct state for each object triggering it.
72        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.
73
74        MultiTriggers also allow for additional complexity which can be added trough the choice of the parameters explained (briefly) below:
75        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.
76        The parameters are:
77            '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.
78            '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.
79            '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.
80            'activations':              The number of activations until the trigger can't be triggered anymore. The default is -1, which is infinity.
81            '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.
82            '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.
83            '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'.
84            'target':                   The target describes the kind of objects that are allowed to trigger this MultiTrigger. The default is 'Pawn'.
85            Also there is the possibility of appending MultiTriggers to the MultiTrigger just by adding them as subobjects in the XML description of your MultiTrigger.
86
87    @author
88        Damian 'Mozork' Frick
89    */
90    class _ObjectsExport MultiTrigger : public StaticEntity, public Tickable
91    {
92        public:
93            MultiTrigger(BaseObject* creator); //!< Constructor. Registers the objects and initializes default values.
94            ~MultiTrigger(); //!< Destructor.
95           
96            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a MultiTrigger object through XML.
97            virtual void tick(float dt); //!< A method that is executed each tick.
98           
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            */
199            inline bool isTarget(BaseObject* target)
200                { return targetMask_.isIncluded(target->getIdentifier()); }
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.
206           
207        protected:
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            */
219            inline ClassTreeMask& getTargetMask(void)
220                { return this->targetMask_; }
221            /**
222            @brief Is called, when the target mask changes.
223            */
224            //TODO: Check if something mus be done here.
225            virtual void notifyMaskUpdate(void) {}
226           
227        private:
228            static const int INF_s; //!< Magic number for infinity.
229            //! Magic strings for the mode.
230            static const std::string and_s;
231            static const std::string or_s;
232            static const std::string xor_s;
233           
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            */
244            std::set<BaseObject*>& getActive(void)
245                { return this->active_; }
246
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.
267           
268    };
269
270}
271
272#endif // _MultiTrigger_H__
Note: See TracBrowser for help on using the repository browser.