Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

started working on pickups

File size: 14.8 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    void FightingController::dodgeTowards (Vector3& position)
175    {
176        Vector3 thisPosition = this->getControllableEntity()->getPosition();
177        Vector3 diff = (position - thisPosition);
178        float diffLength = diff.length();
179        Vector3 diffUnit = diff/diffLength;
180        float factor = 300.0f;
181        if (diffLength < 300)
182        {
183            this->setTargetPosition(position);
184            return;   
185        }
186        else if (diffLength < 600)
187            factor = 400.0f;
188        else if (diffLength < 1000)
189            factor = 700.0f;
190        else
191            factor = 1000.0f;
192        float x = CommonController::randomInRange (400, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1);
193        float y = CommonController::randomInRange (400, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1);
194        float z = diffUnit.z == 0 ? 0 : (1/diffUnit.z) * (-x * diffUnit.x - y * diffUnit.y);
195        this->setTargetPosition(thisPosition + Vector3(x,y,z) + diffUnit * factor);
196        // orxout(internal_error) << "Dodging towards " << Vector3(x,y,z)  << endl;
197        this->boostControl();
198    }
199    void FightingController::dodge(const Vector3& thisPosition, Vector3& diffUnit)
200    {
201        //d.x*x + d.y*y + d.z*z == 0
202        //z = 1/d.z * (-d.y*y - d.x * x)
203        float x = CommonController::randomInRange (100, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1);
204        float y = CommonController::randomInRange (100, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1);
205        float z = diffUnit.z == 0 ? 0 : (1/diffUnit.z) * (-x * diffUnit.x - y * diffUnit.y);
206        this->setTargetPosition(thisPosition + Vector3(x,y,z) + (this->deltaHp < 0 ? -diffUnit * 450.0f : diffUnit * 100.0f));
207        this->boostControl();
208
209    }
210    bool FightingController::canFire() 
211    {
212        //no target? why fire?
213        if (!this->target_)
214            return false;
215        Vector3 newPositionOfTarget = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), 
216                                                           hardcoded_projectile_speed, this->target_->getWorldPosition(), 
217                                                           this->target_->getVelocity());
218        if (!newPositionOfTarget.isNaN())
219        {
220            this->setPositionOfTarget(newPositionOfTarget);
221        }
222
223        return squaredDistanceToTarget() < this->attackRange_*this->attackRange_ && this->isLookingAtTarget(math::pi / 20.0f);
224    }
225    // void FightingController::doFire()
226    // {
227    //     if ( !this->target_ || !this->getControllableEntity() )
228    //     {
229    //         return;
230    //     }
231     
232    //     Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
233
234    //     if ( pawn )
235    //         pawn->setAimPosition( this->positionOfTarget_ );
236    //     float distance = CommonController::distance (this->getControllableEntity(), this->target_);
237    //     this->getControllableEntity() ->fire(distance < 1500 ? (distance < 1000 && distance > 700 ? 3 : 0) : (1));
238    // }
239
240
241    float FightingController::squaredDistanceToTarget()  const
242    {
243        if (!this->getControllableEntity())
244            return 0;
245        if (!this->target_ || !this->getControllableEntity())
246            return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_));
247        else
248            return (this->getControllableEntity()->getPosition().squaredDistance(this->positionOfTarget_));
249    }
250    bool FightingController::isLookingAtTarget( float angle ) const
251    {
252        if ( !this->getControllableEntity()  || !this->target_ )
253            return false;
254        return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle);
255    }
256    void FightingController::setClosestTarget()
257    {
258        this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); 
259    }
260   
261    Pawn* FightingController::closestTarget() const
262    {
263        if (!this->getControllableEntity())
264            return 0;
265
266        Pawn* closestTarget = 0;
267        float minDistance =  std::numeric_limits<float>::infinity();
268        Gametype* gt = this->getGametype();
269        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
270        {
271            if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) )
272                continue;
273
274            float distance = CommonController::distance (*itP, this->getControllableEntity());
275            if (distance < minDistance)
276            {
277                closestTarget = *itP;
278                minDistance = distance;
279            }
280        }
281        if (closestTarget)
282        {
283           return closestTarget;
284        } 
285        return 0; 
286    }
287    //I checked it out, rockets DO NOT cause any problems! this->getControllableEntity() is always a SpaceShip
288    void FightingController::doFire()
289    {
290        if (!this->bSetupWorked)
291        {
292            this->setupWeapons();
293        }
294        if (!this->target_ || !this->getControllableEntity())
295        {
296            return;
297        }
298
299        Pawn* pawn = orxonox_cast<Pawn*> (this->getControllableEntity());
300        if (pawn)
301            pawn->setAimPosition (this->positionOfTarget_);
302        // if (pawn->getHealth() < 100)
303        //     orxout(internal_error) << "not full, hp = " << pawn->getHealth() << endl;
304        int firemode;
305        float distance = CommonController::distance (this->getControllableEntity(), this->target_);
306
307        // firemode = distance < 1500 ? (distance > 800 && distance < 1200 ?
308        //                             (this->rocketsLeft_ > 0 && !this->bFiredRocket_ ? getFiremode("RocketFire") : getFiremode ("HsW01"))
309        //                                                      : getFiremode("HsW01")) :
310        //                                                      (distance < 2500 ? getFiremode("LightningGun") : getFiremode("HsW01"));
311        if (distance < 1500)
312        {
313            if (this->rocketsLeft_ > 0 && !this->bFiredRocket_)
314            {
315                firemode = getFiremode ("RocketFire");
316            }
317            else
318            {
319                if (distance > 800)
320                    firemode = getFiremode ("HsW01");
321                else
322                    firemode = getFiremode ("LightningGun");
323            }
324
325        } 
326        // else if (distance < 1000)
327        // {
328        //     if (this->rocketsLeft_ > 0 && !this->bFiredRocket_)
329        //     {
330        //         firemode = getFiremode ("RocketFire");
331        //     }
332        //     else
333        //     {
334        //         firemode = getFiremode ("HsW01");
335        //     }
336        // }
337        else if (distance < 2000)
338        {
339            firemode = getFiremode ("HsW01");
340        }
341        else
342        {
343            firemode = getFiremode ("LightningGun");
344        }
345        if (firemode < 0)
346        {
347            //assuming there is always some weapon with index 0
348            firemode = 0;
349        }
350        if (firemode == getFiremode("RocketFire"))
351        {
352            this->timeout_ = 0.5f;
353            this->rocketsLeft_--;
354            this->bFiredRocket_ = true;
355        }
356        if (firemode == getFiremode("SimpleRocketFire"))
357        {
358            this->rocketsLeft_--;
359        }
360             
361        this->getControllableEntity()->fire(firemode);
362       
363    }
364    void FightingController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
365    {
366        this->bSetupWorked = false;
367        if(this->getControllableEntity())
368        {
369            Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
370            if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip
371            {
372                this->weaponModes_.clear(); // reset previous weapon information
373                WeaponSlot* wSlot = 0;
374                for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++)
375                {
376                    WeaponMode* wMode = 0;
377                    for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++)
378                    {
379                        std::string wName = wMode->getIdentifier()->getName();
380                        if (wName == "RocketFire")
381                            this->rocketsLeft_ = 10;
382                        if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new"
383                            weaponModes_[wName] = wMode->getMode();
384                    }
385                }
386                if(weaponModes_.size())//at least one weapon detected
387                    this->bSetupWorked = true;
388            }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
389        }
390    }
391
392    int FightingController::getFiremode(std::string name)
393    {
394        for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it)
395        {
396            if (it->first == name)
397                return it->second;
398        }
399        return -1;
400    }
401}
Note: See TracBrowser for help on using the repository browser.