Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

improved dodging

File size: 13.6 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 *      Gani Aliguzhinov
24 *   Co-authors:
25 *      Fabian 'x3n' Landau, Dominik Solenicki
26 *
27 */
28#include "controllers/FightingController.h"
29#include "core/XMLPort.h"
30#include "util/Math.h"
31
32
33#include "worldentities/pawns/SpaceShip.h"
34
35#include "weaponsystem/WeaponMode.h"
36#include "weaponsystem/WeaponPack.h"
37#include "weaponsystem/Weapon.h"
38#include "weaponsystem/WeaponSlot.h"
39#include "weaponsystem/WeaponSlot.h"
40namespace orxonox
41{
42
43    RegisterClass (FightingController);
44   
45    FightingController::FightingController( Context* context ): FlyingController( context )
46    {
47        this->attackRange_ = 2500;
48        this->stopLookingAtTarget();
49        this->bSetupWorked = false;
50        this->timeout_ = 0;
51        RegisterObject( FightingController );
52    }
53    FightingController::~FightingController() 
54    {
55       
56    }
57    void FightingController::XMLPort( Element& xmlelement, XMLPort::Mode mode )
58    {
59        SUPER( FightingController, XMLPort, xmlelement, mode );
60    }
61    void FightingController::lookAtTarget(float dt)
62    {
63        ControllableEntity* entity = this->getControllableEntity();
64        if ( !entity )
65            return;
66        Vector2 coord = get2DViewCoordinates
67            ( entity->getPosition() , 
68            entity->getOrientation()  * WorldEntity::FRONT, 
69            entity->getOrientation()  * WorldEntity::UP, 
70            positionOfTarget_ );
71
72        //rotates should be in range [-1,+1], clamp cuts off all that is not
73        float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
74        float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); 
75   
76        //Yaw and Pitch are enough to start facing the target
77        this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt );
78        this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt );
79    }
80    void FightingController::stopLookingAtTarget()
81    {
82        this->bLookAtTarget_ = false;
83    }
84    void FightingController::startLookingAtTarget()
85    {
86        this->bLookAtTarget_ = true;
87    }
88    bool FightingController::hasTarget() const
89    {
90        if ( this->target_ )
91            return true;
92        return false;
93    }
94
95    void FightingController::setTarget( ControllableEntity* target )
96    {
97        this->target_ = target;       
98        if ( this->target_ )
99        {
100            this->setPositionOfTarget( target_->getWorldPosition() );
101        }
102    }
103    void FightingController::setPositionOfTarget( const Vector3& target )
104    {
105        this->positionOfTarget_ = target;
106        this->bHasPositionOfTarget_ = true;
107    }
108    void FightingController::setOrientationOfTarget( const Quaternion& orient )
109    {
110        this->orientationOfTarget_=orient;
111        this->bHasOrientationOfTarget_=true;
112    }
113   
114    void FightingController::maneuver() 
115    {
116        if ( !this->target_ || !this->getControllableEntity())
117            return;
118        maneuverCounter_++;
119        if (maneuverCounter_ > 5)
120            maneuverCounter_ = 0;
121
122        Vector3 thisPosition = this->getControllableEntity()->getWorldPosition();
123        this->setPositionOfTarget(this->target_->getWorldPosition());
124        //this->setOrientationOfTarget(this->target_->getOrientation());
125        Vector3 diffVector = this->positionOfTarget_ - thisPosition;
126        float diffLength = diffVector.length();
127        Vector3 diffUnit = diffVector/diffLength;
128        bool bTargetIsLookingAtThis = CommonController::isLooking (this->target_, this->getControllableEntity(), math::pi/15.0f)
129            || this->deltaHp < 0;
130
131        //too far? well, come closer then
132        if (diffLength > this->attackRange_)
133        {
134            this->spread_ = 400;
135            this->formationMode_ = FormationMode::DIAMOND;
136            this->bKeepFormation_ = true;
137           
138            this->setTargetPosition(this->positionOfTarget_ - diffUnit * 100.0f);
139        }
140        //too close? How do u expect to dodge anything? Just attack!
141        else if (diffLength < 400)
142        {   
143            this->bKeepFormation_ = false;
144
145            //at this point, just look and shoot
146            if (diffLength < 200)
147            {
148                this->stopMoving();
149                this->startLookingAtTarget();
150            }
151            else
152            {
153                this->setTargetPosition(this->positionOfTarget_ - diffUnit * 100.0f);
154            }
155        }
156        //Good distance? Check if target looks at us. It doesn't? Go hunt!
157        else if (!bTargetIsLookingAtThis)
158        {
159            this->bKeepFormation_ = false;
160            this->setTargetPosition(this->positionOfTarget_ - diffUnit * 100.0f);
161        }
162        //That's unfortunate, he is looking and probably shooting... try to dodge what we can... 
163        else 
164        {   
165            this->bKeepFormation_ = false;
166            if (maneuverCounter_ == 0)
167            {
168                this->setTargetPosition(this->positionOfTarget_ - diffUnit * 50.0f);   
169                return;
170            }
171            dodge(thisPosition, diffUnit);
172        }
173    }
174   
175    void FightingController::dodge(const Vector3& thisPosition, Vector3& diffUnit)
176    {
177        //d.x*x + d.y*y + d.z*z == 0
178        //z = 1/d.z * (-d.y*y - d.x * x)
179        float x = CommonController::randomInRange (100, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1);
180        float y = CommonController::randomInRange (100, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1);
181        float z = (1/diffUnit.z) * (-x * diffUnit.x - y * diffUnit.y);
182        this->setTargetPosition(thisPosition + Vector3(x,y,z) + (this->deltaHp < 0 ? -diffUnit * 450.0f : diffUnit * 100.0f));
183        this->boostControl();
184
185    }
186    bool FightingController::canFire() 
187    {
188        //no target? why fire?
189        if (!this->target_)
190            return false;
191        Vector3 newPositionOfTarget = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), 
192                                                           hardcoded_projectile_speed, this->target_->getWorldPosition(), 
193                                                           this->target_->getVelocity());
194        if (!newPositionOfTarget.isNaN())
195        {
196            this->setPositionOfTarget(newPositionOfTarget);
197        }
198
199        return squaredDistanceToTarget() < this->attackRange_*this->attackRange_ && this->isLookingAtTarget(math::pi / 20.0f);
200    }
201    // void FightingController::doFire()
202    // {
203    //     if ( !this->target_ || !this->getControllableEntity() )
204    //     {
205    //         return;
206    //     }
207     
208    //     Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
209
210    //     if ( pawn )
211    //         pawn->setAimPosition( this->positionOfTarget_ );
212    //     float distance = CommonController::distance (this->getControllableEntity(), this->target_);
213    //     this->getControllableEntity() ->fire(distance < 1500 ? (distance < 1000 && distance > 700 ? 3 : 0) : (1));
214    // }
215
216
217    float FightingController::squaredDistanceToTarget()  const
218    {
219        if (!this->getControllableEntity())
220            return 0;
221        if (!this->target_ || !this->getControllableEntity())
222            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_));
223        else
224            return (this->getControllableEntity()->getPosition().squaredDistance(this->positionOfTarget_));
225    }
226    bool FightingController::isLookingAtTarget( float angle ) const
227    {
228        if ( !this->getControllableEntity()  || !this->target_ )
229            return false;
230        return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle);
231    }
232    void FightingController::setClosestTarget()
233    {
234        this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); 
235    }
236   
237    Pawn* FightingController::closestTarget() const
238    {
239        if (!this->getControllableEntity())
240            return 0;
241
242        Pawn* closestTarget = 0;
243        float minDistance =  std::numeric_limits<float>::infinity();
244        Gametype* gt = this->getGametype();
245        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
246        {
247            if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
248                continue;
249
250            float distance = CommonController::distance (*itP, this->getControllableEntity());
251            if (distance < minDistance)
252            {
253                closestTarget = *itP;
254                minDistance = distance;
255            }
256        }
257        if (closestTarget)
258        {
259           return closestTarget;
260        } 
261        return 0; 
262    }
263    //I checked it out, rockets DO NOT cause any problems! this->getControllableEntity() is always a SpaceShip
264    void FightingController::doFire()
265    {
266        if (!this->bSetupWorked)
267        {
268            this->setupWeapons();
269        }
270        if (!this->target_ || !this->getControllableEntity())
271        {
272            return;
273        }
274
275        Pawn* pawn = orxonox_cast<Pawn*> (this->getControllableEntity());
276        if (pawn)
277            pawn->setAimPosition (this->positionOfTarget_);
278        // if (pawn->getHealth() < 100)
279        //     orxout(internal_error) << "not full, hp = " << pawn->getHealth() << endl;
280        int firemode;
281        float distance = CommonController::distance (this->getControllableEntity(), this->target_);
282
283        // firemode = distance < 1500 ? (distance > 800 && distance < 1200 ?
284        //                             (this->rocketsLeft_ > 0 && !this->bFiredRocket_ ? getFiremode("RocketFire") : getFiremode ("HsW01"))
285        //                                                      : getFiremode("HsW01")) :
286        //                                                      (distance < 2500 ? getFiremode("LightningGun") : getFiremode("HsW01"));
287        if (distance < 1500)
288        {
289            if (this->rocketsLeft_ > 0 && !this->bFiredRocket_)
290            {
291                firemode = getFiremode ("RocketFire");
292            }
293            else
294            {
295                if (distance > 800)
296                    firemode = getFiremode ("HsW01");
297                else
298                    firemode = getFiremode ("LightningGun");
299            }
300
301        } 
302        // else if (distance < 1000)
303        // {
304        //     if (this->rocketsLeft_ > 0 && !this->bFiredRocket_)
305        //     {
306        //         firemode = getFiremode ("RocketFire");
307        //     }
308        //     else
309        //     {
310        //         firemode = getFiremode ("HsW01");
311        //     }
312        // }
313        else if (distance < 2000)
314        {
315            firemode = getFiremode ("HsW01");
316        }
317        else
318        {
319            firemode = getFiremode ("LightningGun");
320        }
321        if (firemode < 0)
322        {
323            //assuming there is always some weapon with index 0
324            firemode = 0;
325        }
326        if (firemode == getFiremode("RocketFire"))
327        {
328            this->timeout_ = 0.5f;
329            this->rocketsLeft_--;
330            this->bFiredRocket_ = true;
331        }
332        if (firemode == getFiremode("SimpleRocketFire"))
333        {
334            this->rocketsLeft_--;
335        }
336             
337        this->getControllableEntity()->fire(firemode);
338       
339    }
340    void FightingController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
341    {
342        this->bSetupWorked = false;
343        if(this->getControllableEntity())
344        {
345            Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
346            if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip
347            {
348                this->weaponModes_.clear(); // reset previous weapon information
349                WeaponSlot* wSlot = 0;
350                for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++)
351                {
352                    WeaponMode* wMode = 0;
353                    for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++)
354                    {
355                        std::string wName = wMode->getIdentifier()->getName();
356                        if (wName == "RocketFire")
357                            this->rocketsLeft_ = 10;
358                        if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new"
359                            weaponModes_[wName] = wMode->getMode();
360                    }
361                }
362                if(weaponModes_.size())//at least one weapon detected
363                    this->bSetupWorked = true;
364            }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
365        }
366    }
367
368    int FightingController::getFiremode(std::string name)
369    {
370        for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it)
371        {
372            if (it->first == name)
373                return it->second;
374        }
375        return -1;
376    }
377}
Note: See TracBrowser for help on using the repository browser.