Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/orxonox/controllers/AIController.cc @ 6795

Last change on this file since 6795 was 6795, checked in by solex, 15 years ago

death of master results in forming a new formation. Correct response to master death still to solve.

  • Property svn:eol-style set to native
File size: 4.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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "AIController.h"
30
31#include "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/Executor.h"
34#include "worldentities/ControllableEntity.h"
35
36namespace orxonox
37{
38    static const float ACTION_INTERVAL = 1.0f;
39
40    CreateFactory(AIController);
41
42    AIController::AIController(BaseObject* creator) : ArtificialController(creator)
43    {
44        RegisterObject(AIController);
45
46        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this)));
47    }
48
49    AIController::~AIController()
50    {
51COUT(0) << "~AIController 1" << std::endl;
52        if (this->state_ == MASTER) setNewMasterWithinFormation();
53COUT(0) << "~AIController 2" << std::endl;
54        if (this->state_ == SLAVE) unregisterSlave();
55COUT(0) << "~AIController 3" << std::endl;
56        this->slaves_.clear();
57COUT(0) << "~AIController 4" << std::endl;
58    }
59
60    void AIController::action()
61    {
62        float random;
63        float maxrand = 100.0f / ACTION_INTERVAL;
64
65        if (this->state_ == FREE)//FREE
66        {
67
68            //this->state_ = MASTER;
69            // search master
70            //random = rnd(maxrand);
71            //if (random < 101 && (!this->target_))
72                this->searchNewMaster();
73
74        }
75
76        if (this->state_ == SLAVE)//SLAVE
77        {
78               // this->bShooting_ = true;
79        }
80
81        if (this->state_ == MASTER)//MASTER
82        {
83
84            // command slaves
85            this->commandSlaves();
86
87
88            // lose master status (only if less than 4 slaves in formation)
89            random = rnd(maxrand);
90            if(random < 5/(this->slaves_.size()+1) && this->slaves_.size() < 5 ) 
91                this->loseMasterState();
92
93            // look out for outher masters if formation is small
94            if(this->slaves_.size() < 3)
95                this->searchNewMaster();
96
97            // search enemy
98            random = rnd(maxrand);
99            if (random < 15 && (!this->target_))
100                this->searchNewTarget();
101
102            // forget enemy
103            random = rnd(maxrand);
104            if (random < 5 && (this->target_))
105                this->forgetTarget();
106
107            // next enemy
108            random = rnd(maxrand);
109            if (random < 10 && (this->target_))
110                this->searchNewTarget();
111
112            // fly somewhere
113            random = rnd(maxrand);
114            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
115                this->searchRandomTargetPosition();
116
117            // stop flying
118            random = rnd(maxrand);
119            if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
120                this->bHasTargetPosition_ = false;
121
122            // fly somewhere else
123            random = rnd(maxrand);
124            if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
125                this->searchRandomTargetPosition();
126
127            // shoot
128            /*random = rnd(maxrand);
129            if (random < 75 && (this->target_ && !this->bShooting_))
130                this->bShooting_ = true;
131*/
132            // stop shooting
133            random = rnd(maxrand);
134            if (random < 25 && (this->bShooting_))
135                this->bShooting_ = false;
136        }
137    }
138
139    void AIController::tick(float dt)
140    {
141        if (!this->isActive())
142            return;
143
144        if (this->state_ == MASTER)
145        {
146            if (this->target_)
147                this->aimAtTarget();
148
149            if (this->bHasTargetPosition_)
150                this->moveToTargetPosition();
151
152            if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
153                this->getControllableEntity()->fire(0);
154        }
155
156        if (this->state_ == SLAVE)
157        {
158
159            if (this->bHasTargetPosition_)
160                this->moveToTargetPosition();
161
162        //this->getControllableEntity()->fire(0);
163
164        }
165
166        SUPER(AIController, tick, dt);
167    }
168
169}
Note: See TracBrowser for help on using the repository browser.