Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/shaders/src/orxonox/controllers/DroneController.cc @ 10409

Last change on this file since 10409 was 8891, checked in by jo, 13 years ago

Ai and tutorial improvements merged back to the trunk. AI features: all weapons are used, the ai-firestrength is configurable, bots are able to collect pickups . I've set the tutorial level as default level.

  • Property svn:eol-style set to native
File size: 8.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/command/Executor.h"
35#include "worldentities/ControllableEntity.h"
36
37namespace orxonox
38{
39    /**
40    @brief
41        Constructor.
42    */
43    CreateFactory(DroneController);
44
45    const float DroneController::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        this->isShooting_ = false;
54        this->setAccuracy(10);
55
56        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this)));
57
58        this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this));
59    }
60
61    DroneController::~DroneController()
62    {
63    }
64
65    void DroneController::setOwner(Pawn* owner){
66        this->owner_ = owner;
67    }
68
69    void DroneController::setDrone(Drone* drone)
70    {
71        this->drone_ = drone;
72        this->setControllableEntity(drone);
73    }
74
75    void DroneController::action()
76    {
77        float random;
78        float maxrand = 100.0f / ACTION_INTERVAL;
79        float distanceToTargetSquared = 0;
80
81        if (this->target_)
82            distanceToTargetSquared = (getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength();
83
84        random = rnd(maxrand);
85        if ( random < 30 || (!this->target_) || distanceToTargetSquared > (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange()))
86            this->searchNewTarget();
87    }
88
89    /**
90    @brief
91        The controlling happens here. This method defines what the controller has to do each tick.
92    @param dt
93        The duration of the tick.
94    */
95    /* PORTALS workaround:
96    if the owner uses a portal -> distance between owner and drone is huge -> is detected by drone
97    -> drone searches for portal -> drone adds portal as waypoint -> drone flies towards portal //ignores owner
98    -> if the drone used a portal, then the distance to the owner is small -> remove waypoint // back to normal mode
99
100    */
101    void DroneController::tick(float dt)
102    {
103        if (this->getDrone() && this->getOwner())
104        {
105            if (this->waypoints_.size() > 0 ) //Waypoint functionality: Drone should follow it's master through portals
106            {// Idea: after using the the portal, the master is far away.
107                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
108                if(wPoint)
109                {
110                    float distanceToOwnerSquared = (this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength();
111                    this->absoluteMoveToPosition(wPoint->getWorldPosition()); //simplified function - needs WORKAROUND
112                    if (distanceToOwnerSquared <= 90000.0f) //WORKAROUND: if the Drone is again near its owner, the portal has been used correctly.
113                    {
114                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
115                        this->positionReached(); //needed??
116                    }
117
118                }
119                else
120                    this->waypoints_.pop_back(); // remove invalid waypoints
121            }
122            else
123            {
124                if (this->target_)
125                {
126                    float distanceToTargetSquared = (this->getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength();
127                    if (distanceToTargetSquared < (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange()))
128                    {
129                       this->isShooting_ = true;
130                       this->aimAtTarget();
131                       if(!this->friendlyFire())
132                           this->getDrone()->fire(0);
133                    }
134               }
135
136
137                float maxDistanceSquared = this->getDrone()->getMaxDistanceToOwner()*this->getDrone()->getMaxDistanceToOwner();
138                float minDistanceSquared = this->getDrone()->getMinDistanceToOwner()*this->getDrone()->getMinDistanceToOwner();
139                if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength()  > 20.0f*maxDistanceSquared)
140                {//FIX: if the drone's owner uses portal, the drone searches for the portal & adds it as a waypoint.
141                    this->updatePointsOfInterest("PortalEndPoint", 500.0f); //possible conflict: speed-pickup
142                }
143                if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength()  > maxDistanceSquared)
144                {
145                    this->moveToPosition(this->getOwner()->getWorldPosition()); //fly towards owner
146                }
147                else if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() < minDistanceSquared)
148                {
149                    this->moveToPosition(-this->getOwner()->getWorldPosition()); //fly away from owner
150                }
151                else if (!this->isShooting_)
152                {
153                    float random = rnd(2.0f);
154                    float randomSelection = rnd(6.0f);
155                    if((int)randomSelection==0) drone_->moveUpDown(random);
156                    else if((int)randomSelection==1) drone_->moveRightLeft(random);
157                    else if((int)randomSelection==2) drone_->moveFrontBack(random);
158                    else if((int)randomSelection==3) drone_->rotateYaw(random);
159                    else if((int)randomSelection==4) drone_->rotatePitch(random);
160                    else if((int)randomSelection==5) drone_->rotateRoll(random);
161                }
162
163                this->isShooting_ = false;
164            }
165        }
166        SUPER(AIController, tick, dt);
167    }
168
169    void DroneController::ownerDied()
170    {
171//         if (this->target_) {            //Drone has some kind of Stockholm-Syndrom --> gets attached to Owners Killer
172//             this->setOwner(target_);
173//             this->searchNewTarget();
174//         }
175        if (this->drone_)
176            this->drone_->destroy();
177        else
178            this->destroy();
179    }
180
181    bool DroneController::friendlyFire()
182    {   ControllableEntity* droneEntity_ = this->getControllableEntity();
183        if (!droneEntity_) return false;
184        if(!owner_) return false;
185        if(this->bHasTargetPosition_)
186        {
187            Vector3 ownerPosition_ = owner_->getPosition();
188            Vector3 toOwner_ = owner_->getPosition() - droneEntity_->getPosition();
189            Vector3 toTarget_ = targetPosition_ - droneEntity_->getPosition();
190            if(toTarget_.length() < toOwner_.length()) return false; //owner is far away = in safty
191            float angleToOwner = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, ownerPosition_);
192            float angleToTarget = getAngle(droneEntity_->getPosition(), droneEntity_->getOrientation() * WorldEntity::FRONT, targetPosition_);
193            float angle = angleToOwner - angleToTarget;//angle between target and owner, observed by the drone
194            if(std::sin(angle)*toOwner_.length() < 5.0f)//calculate owner's distance to shooting line
195            return true;
196        }
197        return false;//Default return value: Usually there is no friendlyFire
198    }
199}
Note: See TracBrowser for help on using the repository browser.