Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/orxonox/controllers/DroneController.cc @ 6833

Last change on this file since 6833 was 6833, checked in by gasserlu, 14 years ago

DroneController edited

File size: 4.3 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 *      Oli Scheuss
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "DroneController.h"
30#include "worldentities/Drone.h"
31#include "util/Math.h"
32
33#include "core/CoreIncludes.h"
34#include "core/Executor.h"
35#include "worldentities/ControllableEntity.h"
36
37namespace orxonox
38{
39    /**
40    @brief
41        Constructor.
42    */
43    CreateFactory(DroneController);
44
45    static const float ACTION_INTERVAL = 1.0f;
46
47    DroneController::DroneController(BaseObject* creator) : ArtificialController(creator)
48    {
49        // - do any kind of initialisation
50
51        // this checks that our creator really is a drone
52        // and saves the pointer to the drone for the controlling commands
53        assert(dynamic_cast<Drone*>(creator)!=0);
54        this->setControllableEntity(dynamic_cast<Drone*>(creator));
55   
56        RegisterObject(DroneController);
57
58        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this)));
59    }
60
61    DroneController::~DroneController()
62    {
63    }
64
65    void DroneController::setPawn(Pawn* pawn){
66        pawnpointer_ = pawn;
67    } 
68   
69    const Pawn* DroneController::getPawn(unsigned int index) const
70    {
71    if(index == 0) return pawnpointer_;
72    return NULL;
73    }
74
75    void DroneController::action()
76    {
77        float random;
78        float maxrand = 100.0f / ACTION_INTERVAL;
79
80        // search enemy
81        random = rnd(maxrand);
82        if (random < 15 && (!this->target_))
83            this->searchNewTarget();
84
85        // forget enemy
86        random = rnd(maxrand);
87        if (random < 5 && (this->target_))
88            this->forgetTarget();
89
90        // next enemy
91        random = rnd(maxrand);
92        if (random < 10 && (this->target_))
93            this->searchNewTarget();
94
95        //fly somewhere
96        random = rnd(maxrand);
97        if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
98            this->searchRandomTargetPosition(); 
99 
100        // stop flying
101        random = rnd(maxrand);
102        if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
103            this->bHasTargetPosition_ = false;
104
105        // fly somewhere else
106        random = rnd(maxrand);
107        if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
108            this->searchRandomTargetPosition();
109
110     /*   // shoot
111        random = rnd(maxrand);
112        if (random < 75 && (this->target_ && !this->bShooting_))
113            this->bShooting_ = true;
114
115        // stop shooting
116        random = rnd(maxrand);
117        if (random < 25 && (this->bShooting_)) */
118            this->bShooting_ = false;
119    }
120
121
122    /**
123    @brief
124        The controlling happens here. This method defines what the controller has to do each tick.
125    @param dt
126        The duration of the tick.
127    */
128    void DroneController::tick(float dt)
129    {
130        // Place your code here:
131        // - steering commands
132       
133
134        Drone *myDrone = static_cast<Drone*>(this->getControllableEntity());
135
136        if(myDrone != NULL) {
137       
138        setTargetPosition(this->getControllableEntity()->getPosition());
139/*
140        myDrone->setRotationThrust(25);
141        myDrone->setAuxilaryThrust(30);
142        myDrone->rotateYaw(10*dt); */
143        }
144
145        SUPER(AIController, tick, dt);
146
147        // you can use the following commands for steering
148        // - moveFrontBack, moveRightLeft, moveUpDown
149        // - rotatePitch, rotateYaw, rotateRoll
150        // - apply the to myDrone (e.g. myDrone->rotateYaw(..) )
151
152    }
153}
Note: See TracBrowser for help on using the repository browser.