Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h @ 10955

Last change on this file since 10955 was 10955, checked in by gania, 9 years ago

cleaned up a bit

File size: 12.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 *      Gani Aliguzhinov
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _ActionpointController_H__
30#define _ActionpointController_H__
31
32#include "controllers/FightingController.h"
33#include "tools/Timer.h"
34#include "tools/interfaces/Tickable.h"
35#include "../modules/pickup/PickupSpawner.h"
36#include <map>
37
38#include <boost/shared_ptr.hpp>
39
40
41namespace orxonox
42{
43    /**
44    @brief
45        ActionpointController is a state machine with states:
46            1) NONE
47            2) FLY: fly towards a point
48            3) FIGHT: fight enemies that are in attackRange_ (see FightingController)
49            4) PROTECT: follow this->protect_
50            5) FIGHTALL: fight all enemies on the map
51            6) ATTACK: fight a specific spaceship
52        This controller always executes an action that is in the back of the vector being used.
53        After current this->action_ is completed, next action becomes the top action (one that will
54        be returned by someVector.back()), and current action either will be removed (if not looping),
55        or moved to the top (if looping).
56
57        Every second action(), which is once in two seconds, this searches the area for enemies that are in attack range, if finds anyone,
58        pushes Action::FIGHT to the stack. That makes spaceship fight enemies inside of a sphere, and when all enemies in range are dead,
59        Action::FIGHT is removed from the stack, and spaceship resumes doing whatever action was being executed before.
60
61        In XML one has to attack Actionpoints in order to achieve any complex behaviour, but in Controller all actionpoints are effectively
62        being stored in an array of type Point::Value.
63    @note
64        ActionpointController will not work, if there is no MasterController in the level!
65    */
66    namespace Action
67    { 
68        enum Value
69        {
70            NONE, FLY, FIGHT, PROTECT, FIGHTALL, ATTACK
71        };
72       
73    }
74   
75    struct Point {
76        Action::Value action;
77        std::string name;
78        Vector3 position;
79        bool inLoop;
80    } ;
81    namespace PickupType
82    {
83        enum Value
84        { 
85            NONE, DAMAGE, HEALTH, SPEED, PORTAL
86        };
87    }
88
89    class _OrxonoxExport ActionpointController : public FightingController, public Tickable
90    {
91        public:
92           
93            ActionpointController(Context* context);
94            virtual ~ActionpointController();
95            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);         
96               
97            /**
98            @brief
99                tick is called every tick by Game (?).
100                In tick ship flies and fires.
101            */
102            virtual void tick(float dt);   
103            /**
104            @brief
105                XML method, example XML usage:
106                <SpaceShip position="-2000, 1500, -1000" lookat="0,0,0" team=0 name="ss2">
107                  <templates>
108                    <Template link=spaceshipassff />
109                  </templates>
110                  <controller>
111                    <DivisionController team=0 formationMode="finger4">
112                      <actionpoints>
113                        <Actionpoint position="0,0,0" action="FLY" />
114                        <Actionpoint position="-1000,750,-500" action="ATTACK" attack="attack" />
115                        <Actionpoint position="-1000,750,-500" action="PROTECt" protectMe=true />
116                        <Actionpoint position="-1000,750,-500" action="PROTECt" protect="protect" />
117                        <Actionpoint position="-1000,750,-500" action="FIGHTALL" />
118                       </actionpoints>
119                    </DivisionController>
120                  </controller>
121                </SpaceShip>
122               
123                Full description:
124                Adds an Actionpoint to this->actionpoints_. Actionpoint can take arguments like action="attack" attack="name".
125                For documentation on Actionpoint XML arguments, check out Actionpoint.h class
126                If any WorldEntity that is not Actionpoint or its child being sent to actionpoints through XML,
127                action would be assumed to be Action::FLY and target position to be position of the entity. Also, if not Actionpoint
128                is passed, it is assumed to be in a loop. How it works is: in <actionpoints> first all Actionpoints between
129                first Actionpoint with loopStart=true and first following Actionpoint with loopEnd=true are included in a single loop.
130                If they are adjacent (in the input array) with WorldEntity, then WorldEntity is also in a loop.
131                All the Worldentities are assumed to be in loop.
132               
133                Loop example:
134                <SpaceShip position="-1500, 1500, -1000" lookat="0,0,0" team=0 name="ss1">
135                  <templates>
136                    <Template link=spaceshipassff />
137                  </templates>
138                  <controller>
139                    <DivisionController team=0 formationMode="wall">
140                      <actionpoints>
141                        <Actionpoint position="  0,2000,-600" action="FLY" loopStart=true/>
142                        <Actionpoint position="  0,2000,-1000" action="FLY"  />
143                        <Actionpoint position="400,2000,-1000" action="FLY" />
144                        <Actionpoint position="400,2000,-600" action="FLY" loopEnd=true />
145                      </actionpoints>
146                    </DivisionController>
147                  </controller>
148                </SpaceShip>
149               
150                other loop example:
151                <SpaceShip position="-1500, -1500, -1500" lookat="0,0,0" team=0 name="ss1">
152                  <templates>
153                    <Template link=spaceshipassff />
154                  </templates>
155                  <controller>
156                    <DivisionController team=0 formationMode="diamond">
157                      <actionpoints>
158                        <Model mesh="cube.mesh" scale=8 position="  0,2000,-600" />
159                        <Model mesh="cube.mesh" scale=8 position="  0,2000,-1000" />
160                        <Model mesh="cube.mesh" scale=8 position="400,2000,-1000" />
161                        <Model mesh="cube.mesh" scale=8 position="400,2000,-600" />
162                      </actionpoints>
163                    </DivisionController>
164                  </controller>
165                </SpaceShip>
166
167            @note
168                Don't use several loops, and don't use WorldEntities as input to <actionpoints> as I didn't test it well, but you
169                can try if feeling lucky. 
170            */
171            void addActionpoint(WorldEntity* actionpoint); 
172            WorldEntity* getActionpoint(unsigned int index) const;   
173            void setDefaultFightAll(bool value)
174                { this->bDefaultFightAll_ = value; }
175            bool getDefaultFightAll ()
176                { return this->bDefaultFightAll_; }
177            void setDefaultPatrol(bool value)
178                { this->bDefaultPatrol_ = value; }
179            bool getDefaultPatrol ()
180                { return this->bDefaultPatrol_; }
181               
182
183            virtual void stayNearProtect();
184            virtual void action(); //<! action() is called in regular intervals managing the bot's behaviour. Only gets called by MasterController
185            virtual void takeActionpoints (const std::vector<Point>& vector, const std::vector<Point>& loop, bool b);
186
187            virtual Action::Value getAction ();
188            virtual std::string getActionName();
189
190            void setAction (Action::Value action);
191            void setAction (Action::Value action, ControllableEntity* target);
192            void setAction (Action::Value action, const Vector3& target);
193            void setAction (Action::Value action, const Vector3& target,  const Quaternion& orient );
194
195            virtual bool setWingman(ActionpointController* wingman)
196                { return false; }
197            virtual bool hasWingman()
198                { return true; }
199            virtual bool setFollower(ActionpointController* myFollower)
200                { return false; }
201            virtual bool hasFollower()
202                { return true; }
203
204
205        protected:
206                void startAttackingEnemiesThatAreClose();
207                WeakPtr<ActionpointController> myWingman_;
208                WeakPtr<ActionpointController> myFollower_;
209                WeakPtr<ActionpointController> myDivisionLeader_;
210            //----[Actionpoint information]----
211                Action::Value action_;
212                std::string protectName_;
213                std::string targetName_;
214                std::vector<WeakPtr<WorldEntity> > actionpoints_;
215                float squaredaccuracy_;
216                std::vector<Point > parsedActionpoints_;//<! actionpoints as they are stored here after being parsed from XML
217                std::vector<Point > loopActionpoints_;  //<! actionpoints that are to be looped
218                bool bInLoop_;
219                bool bLoop_;                            //<! is state machine looping?
220                bool bEndLoop_;                   
221                bool bTakenOver_;                       //<! are actionpoints taken over from the leader when he died? if yes, top actionpoint
222                                                        //<! is to be executed for the state machine to start working
223            //----[/Actionpoint information]----
224                void setProtect (ControllableEntity* protect);
225                ControllableEntity* getProtect (); 
226                WeakPtr<ControllableEntity> protect_;   //<! entity that is to be protected if this->action_ == Action::PROTECT
227                void fillLoop();                        //<! moves actionpoints that are should be in loop from parsedActionpoints_ to loopActionpoints_
228                void fillLoopReversed();
229                void moveBackToTop();                   //<! analog of removing back actionpoint for loopActionpoints_: instead of removing it,
230                                                        //<! move it to the top, so that it will be executed later on.
231                void setClosestTarget();
232                Pawn* closestTarget();
233            //----[Actionpoint methods]----
234                /**
235                @brief
236                    Sets this->target_, this->targetPosition_, this->protect_ and this->action_ depending
237                    on the current actionpoint in the vector parsedActionpoints_ if not looping or
238                    loopActionpoints_ if looping.
239                @note
240                */
241                void executeActionpoint(); 
242                /**
243                @brief
244                    If this->bLoop_, move back action to top (back is the current one, top is the last),
245                    otherwise remove back actionpoint.
246                @note
247                    actionpoints_ is only used for XML, real state stacks are parsedActionpoints_ and loopActionpoints_
248                */           
249                void nextActionpoint();                 
250            //----[Actionpoint methods]----         
251
252                bool bDefaultFightAll_;
253
254                bool bPatrolling_;
255                bool bDefaultPatrol_;
256                bool stop_;
257                unsigned int ticks_;     //<! local tick counter           
258
259    };
260}
261
262#endif /* _ActionpointController_H__ */
Note: See TracBrowser for help on using the repository browser.