Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/CommonController.cc @ 11482

Last change on this file since 11482 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 7.5 KB
RevLine 
[10719]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
[10946]11 *   of the License, or (at your option)any later version.
[10719]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:
[10885]23 *      Gani Aliguzhinov
[10719]24 *   Co-authors:
[10974]25 *      ...
[10719]26 *
27 */
28#include "controllers/CommonController.h"
[10759]29
[10946]30//here starts stuff for sameTeam function copied from FormationController
[10838]31#include "gametypes/TeamDeathmatch.h"
32#include "gametypes/Gametype.h"
33#include "controllers/DroneController.h"
[10877]34#include "gametypes/Dynamicmatch.h"
[10838]35
[10877]36#include "worldentities/pawns/TeamBaseMatchBase.h"
37
[10719]38namespace orxonox
39{
[11028]40    const float CommonController::HARDCODED_PROJECTILE_SPEED = 750;
[10719]41
[10946]42    RegisterClass(CommonController);
43    CommonController::CommonController(Context* context): Controller(context)
[10719]44    {
[10946]45        RegisterObject(CommonController);
[10719]46    }
[10799]47    CommonController::~CommonController() 
[10719]48    {
[10946]49        //no member variables - nothing to destroy
[10731]50    }
[10946]51    /**
52    @brief
53      PRE: a < b.
54      returns random float between a and b.
55    */
56    float CommonController::randomInRange(float a, float b)
[10796]57    {
[10885]58        return a + rnd(1.0f) * (b - a);
[10796]59    }
[10946]60    /**
61    @brief
62      returns distance between two entities, if either is zero pointer, returns infinity
63    */   
64    float CommonController::distance (const ControllableEntity* entity1, const ControllableEntity* entity2)
[10826]65    {
66        if (!entity1 || !entity2)
67            return std::numeric_limits<float>::infinity();
[10946]68        return (entity1->getPosition() - entity2->getPosition()).length();
[10826]69    }
[10946]70    /**
71    @brief
72      bad function from FormationController that returns true if both entities have same team
73    */   
[10838]74    bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
[10826]75    {
[10974]76       
[10946]77        if (!entity1 || !entity2)
78            return false;
[10838]79        if (entity1 == entity2)
80            return true;
81
82        int team1 = entity1->getTeam();
83        int team2 = entity2->getTeam();
84
[11071]85        Controller* controller = nullptr;
[10838]86        if (entity1->getController())
87            controller = entity1->getController();
88        else
89            controller = entity1->getXMLController();
90        if (controller)
91        {
[10953]92            if (controller->getIdentifier()->getName() == "MasterController")
93                return true;
[10838]94            CommonController* ac = orxonox_cast<CommonController*>(controller);
95            if (ac)
96                team1 = ac->getTeam();
97        }
98
99        if (entity2->getController())
100            controller = entity2->getController();
101        else
102            controller = entity2->getXMLController();
103        if (controller)
104        {
[10953]105            if (controller->getIdentifier()->getName() == "MasterController")
106                return true;
[10838]107            CommonController* ac = orxonox_cast<CommonController*>(controller);
108            if (ac)
109                team2 = ac->getTeam();
110        }
111
112        TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype);
113        if (tdm)
114        {
115            if (entity1->getPlayer())
116                team1 = tdm->getTeam(entity1->getPlayer());
117
118            if (entity2->getPlayer())
119                team2 = tdm->getTeam(entity2->getPlayer());
120        }
121
[11071]122        TeamBaseMatchBase* base = nullptr;
[10838]123        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
124        if (base)
125        {
126            switch (base->getState())
127            {
128                case BaseState::ControlTeam1:
129                    team1 = 0;
130                    break;
131                case BaseState::ControlTeam2:
132                    team1 = 1;
133                    break;
134                case BaseState::Uncontrolled:
135                default:
136                    team1 = -1;
137            }
138        }
139        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
140        if (base)
141        {
142            switch (base->getState())
143            {
144                case BaseState::ControlTeam1:
145                    team2 = 0;
146                    break;
147                case BaseState::ControlTeam2:
148                    team2 = 1;
149                    break;
150                case BaseState::Uncontrolled:
151                default:
152                    team2 = -1;
153            }
154        }
155
[11071]156        DroneController* droneController = nullptr;
[10838]157        droneController = orxonox_cast<DroneController*>(entity1->getController());
158        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
159            return true;
160        droneController = orxonox_cast<DroneController*>(entity2->getController());
161        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1)
162            return true;
163        DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController());
164        DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController());
165        if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner())
166            return true;
167
168        Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype);
169        if (dynamic)
170        {
171            if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;}
172
173            if (entity1->getPlayer())
174                team1 = dynamic->getParty(entity1->getPlayer());
175
176            if (entity2->getPlayer())
177                team2 = dynamic->getParty(entity2->getPlayer());
178
[10946]179            if (team1 ==-1 ||team2 ==-1) {return false;}
[10838]180            else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;}
181            else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;}
182            else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;}
183            else return true;
184        }
185
186        return (team1 == team2 && team1 != -1);
[10826]187    }
[10946]188    /**
189    @brief
190      returns true if entityThatLooks does look at entityBeingLookeAt with a tolerance of angle.
191    */   
192    bool CommonController::isLooking(const ControllableEntity* entityThatLooks, const ControllableEntity* entityBeingLookedAt, float angle)
[10851]193    {
[10946]194        if (!entityThatLooks || !entityBeingLookedAt)
[10851]195            return false;
[10946]196        return (getAngle(entityThatLooks ->getPosition() , 
[10851]197            entityThatLooks->getOrientation()  * WorldEntity::FRONT, 
[10946]198            entityBeingLookedAt->getWorldPosition()) < angle);
[10851]199    }
[10946]200    /**
201    @brief
202      returns a name of a Pawn entity, if no name set, returns string representing address of the Pawn.
203    */   
204    std::string CommonController::getName(const Pawn* entity)
[10851]205    {
206        std::string name = entity->getName();
207        if (name == "")
208        {
209            const void * address = static_cast<const void*>(entity);
210            std::stringstream ss;
211            ss << address; 
212            name = ss.str();           
213        }
214        return name;
215    }
[10719]216}
Note: See TracBrowser for help on using the repository browser.