Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

converted hack to a legal class

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