Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10882 was 10880, checked in by gania, 10 years ago

cut smoothness, gained performance

File size: 9.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 *      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 = CommonController::isLooking ( this->target_, getControllableEntity(), math::pi/20.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 = CommonController::randomInRange (0, 1);
170   
171        if (rand <= 0.5)
172        { factorX = 1; }
173        else
174        { factorX = -1; }
175        rand = CommonController::randomInRange (0, 1);
176        if (rand <= 0.5)
177        { factorY = 1; }
178        else
179        { factorY = -1; }
180        rand = CommonController::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 * CommonController::randomInRange( 10000, 40000 ), 
189            factorY * CommonController::randomInRange( 10000, 40000 ), 
190            factorZ * CommonController::randomInRange( 10000, 40000 ) 
191        );
192        Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit;
193        Vector3 randV = *randVector;
194        delete randVector;
195        randV -= projection;
196        target += randV;
197        this->setTargetPosition( thisPosition + target );
198    }
199    bool FightingController::canFire() 
200    {
201        //no target? why fire?
202        if ( !this->target_ )
203            return false;
204
205        Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , 
206            hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() );
207        if ( newPositionOfTarget != Vector3::ZERO )
208        {
209            this->setPositionOfTarget( newPositionOfTarget );
210        }
211
212        float squaredDistance = squaredDistanceToTarget();
213
214        if ( squaredDistance < this->attackRange_*this->attackRange_ && this->isLookingAtTarget( math::pi / 20.0f)) 
215        {
216            return true;
217        }
218        else
219        {
220            return false;
221        }
222    }
223    void FightingController::doFire() 
224    {
225        if ( !this->target_ || !this->getControllableEntity() )
226        {
227            return;
228        }
229     
230        Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
231
232        if ( pawn )
233            pawn->setAimPosition( this->positionOfTarget_ );
234        this->getControllableEntity() ->fire( 0 );
235    }
236    float FightingController::squaredDistanceToTarget()  const
237    {
238        if ( !this->getControllableEntity()  )
239            return 0;
240        if ( !this->target_ || !this->getControllableEntity() )
241            return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) );
242        else
243            return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) );
244    }
245    bool FightingController::isLookingAtTarget( float angle )
246    {
247        if ( !this->getControllableEntity()  || !this->target_ )
248            return false;
249        return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle);
250    }
251        void FightingController::setClosestTarget()
252    {
253        this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); 
254    }
255   
256    Pawn* FightingController::closestTarget()
257    {
258        if (!this->getControllableEntity())
259            return 0;
260
261        Pawn* closestTarget = 0;
262        float minDistance =  std::numeric_limits<float>::infinity();
263        Gametype* gt = this->getGametype();
264        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
265        {
266            if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
267                continue;
268
269            float distance = CommonController::distance (*itP, this->getControllableEntity());
270            if (distance < minDistance)
271            {
272                closestTarget = *itP;
273                minDistance = distance;
274            }
275        }
276        if (closestTarget)
277        {
278           return closestTarget;
279        } 
280        return 0; 
281    }
282}
Note: See TracBrowser for help on using the repository browser.