Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/DroneController.cc @ 12037

Last change on this file since 12037 was 11783, checked in by landauf, 7 years ago

merged Presentation_HS17_merge back to trunk

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