Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

drone follows in realistic matter

File size: 5.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        RegisterObject(DroneController);
50
51        this->owner_ = 0;
52        this->drone_ = 0;
53
54        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this)));
55
56        this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this));
57    }
58
59    DroneController::~DroneController()
60    {
61    }
62
63    void DroneController::setOwner(Pawn* owner){
64        this->owner_ = owner;
65    } 
66
67    void DroneController::setDrone(Drone* drone)
68    {
69        this->drone_ = drone;
70        this->setControllableEntity(drone);
71    }
72   
73    void DroneController::action()
74    {
75        float random;
76        float maxrand = 100.0f / ACTION_INTERVAL;
77
78        const Vector3& ownerPosition = getOwner()->getWorldPosition();
79        const Vector3& dronePosition = getDrone()->getWorldPosition();
80
81        const Vector3& locOwnerDir = getDrone()->getOrientation().UnitInverse()*(ownerPosition-dronePosition); //Vector from Drone To Owner out of drones local coordinate system
82/*
83        int distance_square  = (ownerPosition.x-dronePosition.x)*(ownerPosition.x-dronePosition.x)
84                             + (ownerPosition.y-dronePosition.y)*(ownerPosition.y-dronePosition.y)
85                             + (ownerPosition.z-dronePosition.z)*(ownerPosition.z-dronePosition.z); //distance to Owner squared
86*/
87        random = rnd(maxrand);
88        if ( random < 30 && (!this->target_))
89            this->searchNewTarget();
90/*
91        //face target
92        drone_->rotateYaw(-targetPosition_.x);
93        drone_->rotatePitch(targetPosition_.y);
94  */     
95        if (this->target_)
96        {
97            this->aimAtTarget();
98            drone_->fire(0);
99        }
100         
101
102
103/*
104        COUT(0) << "Drone: " << dronePosition << endl;
105        COUT(0) << "Distance: " << distance << endl;
106        COUT(0) << "locDrone: " << locOwnerDir << endl;
107        COUT(0) << "target: " << targetPosition_ << endl;
108        COUT(0) << "Owner: " << ownerPosition << endl;
109        COUT(0) << "Rand: " << random << endl;
110*/
111/*
112        // search enemy
113        random = rnd(maxrand);
114        if (random < 15 && (!this->target_))
115            this->searchNewTarget();
116
117        // forget enemy
118        random = rnd(maxrand);
119        if (random < 5 && (this->target_))
120            this->forgetTarget();
121
122        // next enemy
123        random = rnd(maxrand);
124        if (random < 10 && (this->target_))
125            this->searchNewTarget();
126
127        //fly somewhere
128        random = rnd(maxrand);
129        if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
130            this->searchRandomTargetPosition();
131 
132        // stop flying
133        random = rnd(maxrand);
134        if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
135            this->bHasTargetPosition_ = false;
136
137        // fly somewhere else
138        random = rnd(maxrand);
139        if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
140            this->searchRandomTargetPosition();
141
142        // shoot
143        random = rnd(maxrand);
144        if (random < 75 && (this->target_ && !this->bShooting_))
145            this->bShooting_ = true;
146
147        // stop shooting
148        random = rnd(maxrand);
149        if (random < 25 && (this->bShooting_))
150            this->bShooting_ = false; */
151    }
152
153
154    /**
155    @brief
156        The controlling happens here. This method defines what the controller has to do each tick.
157    @param dt
158        The duration of the tick.
159    */
160    void DroneController::tick(float dt)
161    {
162       
163
164        Drone *myDrone = static_cast<Drone*>(this->getControllableEntity());
165
166        if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength()  > 150*150) { //TODO: variable implementation of maxdistance
167            this->moveToPosition(this->getOwner()->getWorldPosition());
168
169        }
170
171        SUPER(AIController, tick, dt);
172
173    }
174
175    void DroneController::ownerDied()
176    {
177        if (this->drone_)
178            this->drone_->destroy();
179        else
180            this->destroy();
181    }
182}
Note: See TracBrowser for help on using the repository browser.