Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12076 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
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    RegisterClass(DroneController);
44
45    const float DroneController::ACTION_INTERVAL = 1.0f;
46
47    DroneController::DroneController(Context* context) : ArtificialController(context)
48    {
49        RegisterObject(DroneController);
50
51        this->setTeam(0);
52        this->owner_ = nullptr;
53        this->drone_ = nullptr;
54        this->isShooting_ = false;
55        this->setAccuracy(10);
56
57        this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this)));
58
59        this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this));
60    }
61
62    DroneController::~DroneController()
63    {
64    }
65
66    void DroneController::setOwner(Pawn* owner){
67        this->owner_ = owner;
68    }
69
70    void DroneController::setDrone(Drone* drone)
71    {
72        this->drone_ = drone;
73        this->setControllableEntity(drone);
74    }
75
76    void DroneController::action()
77    {
78        float random;
79        float maxrand = 100.0f / ACTION_INTERVAL;
80        float distanceToTargetSquared = 0;
81
82        if (this->target_)
83            distanceToTargetSquared = (getDrone()->getWorldPosition() - this->target_->getWorldPosition()).squaredLength();
84
85        random = rnd(maxrand);
86        if ( random < 30 || (!this->target_) || distanceToTargetSquared > (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange()))
87            this->searchNewTarget();
88    }
89
90    /**
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    */
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    */
102    void DroneController::tick(float dt)
103    {
104        if (this->getDrone() && this->getOwner())
105        {
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)
110                {
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
119                }
120                else
121                    this->waypoints_.pop_back(); // remove invalid waypoints
122            }
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               }
136
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;
165            }
166        }
167       
168        SUPER(AIController, tick, dt);
169    }
170
171    void DroneController::ownerDied()
172    {
173//         if (this->target_) {            //Drone has some kind of Stockholm-Syndrom --> gets attached to Owners Killer
174//             this->setOwner(target_);
175//             this->searchNewTarget();
176//         }
177        if (this->drone_)
178            this->drone_->destroy();
179        else
180            this->destroy();
181    }
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    }
201}
Note: See TracBrowser for help on using the repository browser.