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