Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/FightingController.cc @ 10875

Last change on this file since 10875 was 10875, checked in by gania, 8 years ago

fixed library dependencies

File size: 9.1 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Dominik Solenicki
26 *
27 */
28#include "controllers/FightingController.h"
29#include "core/XMLPort.h"
30#include "util/Math.h"
31
32
33namespace orxonox
34{
35
36    RegisterClass (FightingController);
37   
38    FightingController::FightingController( Context* context ): FlyingController( context )
39    {
40        this->attackRange_ = 2500;
41        this->stopLookingAtTarget();
42
43        RegisterObject( FightingController );
44    }
45    FightingController::~FightingController() 
46    {
47       
48    }
49    void FightingController::XMLPort( Element& xmlelement, XMLPort::Mode mode )
50    {
51        SUPER( FightingController, XMLPort, xmlelement, mode );
52    }
53    void FightingController::lookAtTarget(float dt)
54    {
55        ControllableEntity* entity = this->getControllableEntity();
56        if ( !entity )
57            return;
58        Vector2 coord = get2DViewCoordinates
59            ( entity->getPosition() , 
60            entity->getOrientation()  * WorldEntity::FRONT, 
61            entity->getOrientation()  * WorldEntity::UP, 
62            positionOfTarget_ );
63
64        //rotates should be in range [-1,+1], clamp cuts off all that is not
65        float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
66        float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); 
67   
68        //Yaw and Pitch are enough to start facing the target
69        this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt );
70        this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt );
71    }
72    void FightingController::stopLookingAtTarget()
73    {
74        this->bLookAtTarget_ = false;
75    }
76    void FightingController::startLookingAtTarget()
77    {
78        this->bLookAtTarget_ = true;
79    }
80    bool FightingController::hasTarget() 
81    {
82        if ( this->target_ )
83            return true;
84        return false;
85    }
86
87    void FightingController::setTarget( ControllableEntity* target )
88    {
89        this->target_ = target;       
90        if ( this->target_ )
91        {
92            this->setPositionOfTarget( target_->getWorldPosition() );
93        }
94    }
95    void FightingController::setPositionOfTarget( const Vector3& target )
96    {
97        this->positionOfTarget_ = target;
98        this->bHasPositionOfTarget_ = true;
99    }
100    void FightingController::setOrientationOfTarget( const Quaternion& orient )
101    {
102        this->orientationOfTarget_=orient;
103        this->bHasOrientationOfTarget_=true;
104    }
105   
106    void FightingController::maneuver() 
107    {
108        maneuverCounter_++;
109        if (maneuverCounter_ > 5)
110            maneuverCounter_ = 0;
111
112        if ( !this->target_ || !this->getControllableEntity())
113            return;
114       
115        Vector3 thisPosition = this->getControllableEntity()->getWorldPosition();
116        this->setPositionOfTarget( getPredictedPosition( 
117            thisPosition, 
118            hardcoded_projectile_speed, 
119            this->target_->getWorldPosition() , 
120            this->target_->getVelocity() 
121            )  );
122        this->setOrientationOfTarget( this->target_->getOrientation() );
123
124        Vector3 diffVector = this->positionOfTarget_ - thisPosition;
125        float diffLength = diffVector.length();
126        Vector3 diffUnit = diffVector/diffLength;
127
128        bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f );
129       
130        //too far? well, come closer then
131        if ( diffLength > this->attackRange_ )
132        {
133            this->setTargetPosition( this->positionOfTarget_ );
134        }
135        //too close? How do u expect to dodge anything? Just attack!
136        else if ( diffLength < 500 )
137        {   
138            //at this point, just look and shoot
139            if ( diffLength < 250 )
140            {
141                this->stopMoving();
142                this->startLookingAtTarget();
143            }
144            else
145            {
146                this->setTargetPosition( this->positionOfTarget_ );
147            }
148        }
149        //Good distance? Check if target looks at us. It doesn't? Go hunt!
150        else if ( !bTargetIsLookingAtThis )
151        {
152            this->setTargetPosition( this->positionOfTarget_ );
153        }
154        //That's unfortunate, he is looking and probably shooting... try to dodge what we can... 
155        else 
156        {   
157            if (maneuverCounter_ == 0)
158            {
159                this->setTargetPosition( this->positionOfTarget_ );   
160                return;
161            }
162            dodge( thisPosition, diffUnit );
163        }
164    }
165   
166    void FightingController::dodge(Vector3& thisPosition, Vector3& diffUnit)
167    {
168        float factorX = 0, factorY = 0, factorZ = 0;
169        float rand = randomInRange (0, 1);
170   
171        if (rand <= 0.5)
172        { factorX = 1; }
173        else
174        { factorX = -1; }
175        rand = randomInRange (0, 1);
176        if (rand <= 0.5)
177        { factorY = 1; }
178        else
179        { factorY = -1; }
180        rand = randomInRange (0, 1);
181        if (rand <= 0.5)
182        { factorZ = 1; }
183        else
184        { factorZ = -1; }
185
186        Vector3 target = ( diffUnit )* 8000.0f;
187        Vector3* randVector = new Vector3( 
188            factorX * randomInRange( 10000, 40000 ), 
189            factorY * randomInRange( 10000, 40000 ), 
190            factorZ * randomInRange( 10000, 40000 ) 
191        );
192        Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit;
193        *randVector -= projection;
194        target += *randVector;
195        this->setTargetPosition( thisPosition + target );
196    }
197    bool FightingController::canFire() 
198    {
199        //no target? why fire?
200        if ( !this->target_ )
201            return false;
202
203        Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , 
204            hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() );
205        if ( newPositionOfTarget != Vector3::ZERO )
206        {
207            this->setPositionOfTarget( newPositionOfTarget );
208        }
209
210        float squaredDistance = squaredDistanceToTarget();
211
212        if ( squaredDistance < this->attackRange_*this->attackRange_ && this->isLookingAtTarget( math::pi / 20.0f)) 
213        {
214            return true;
215        }
216        else
217        {
218            return false;
219        }
220    }
221    void FightingController::doFire() 
222    {
223        if ( !this->target_ || !this->getControllableEntity() )
224        {
225            return;
226        }
227     
228        Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
229
230        if ( pawn )
231            pawn->setAimPosition( this->positionOfTarget_ );
232        this->getControllableEntity() ->fire( 0 );
233    }
234    float FightingController::squaredDistanceToTarget()  const
235    {
236        if ( !this->getControllableEntity()  )
237            return 0;
238        if ( !this->target_ || !this->getControllableEntity() )
239            return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) );
240        else
241            return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) );
242    }
243    bool FightingController::isLookingAtTarget( float angle )
244    {
245        if ( !this->getControllableEntity()  || !this->target_ )
246            return false;
247        return this->isLooking(this->getControllableEntity(), this->getTarget(), angle);
248    }
249        void FightingController::setClosestTarget()
250    {
251        this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); 
252    }
253   
254    Pawn* FightingController::closestTarget()
255    {
256        if (!this->getControllableEntity())
257            return 0;
258
259        Pawn* closestTarget = 0;
260        float minDistance =  std::numeric_limits<float>::infinity();
261        Gametype* gt = this->getGametype();
262        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
263        {
264            if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
265                continue;
266
267            float distance = CommonController::distance (*itP, this->getControllableEntity());
268            if (distance < minDistance)
269            {
270                closestTarget = *itP;
271                minDistance = distance;
272            }
273        }
274        if (closestTarget)
275        {
276           return closestTarget;
277        } 
278        return 0; 
279    }
280}
Note: See TracBrowser for help on using the repository browser.