Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/CommonController.cc @ 10877

Last change on this file since 10877 was 10877, checked in by gania, 9 years ago

CommonController now has static methods only. Replace with a namespace?

File size: 6.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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Dominik Solenicki
26 *
27 */
28#include "controllers/CommonController.h"
29
30//stuff for sameTeam function
31#include "gametypes/TeamDeathmatch.h"
32#include "gametypes/Gametype.h"
33#include "controllers/DroneController.h"
34#include "gametypes/Dynamicmatch.h"
35
36#include "worldentities/pawns/TeamBaseMatchBase.h"
37
38namespace orxonox
39{
40
41    RegisterClass( CommonController );
42
43    CommonController::CommonController( Context* context ): Controller( context )
44    {
45        RegisterObject( CommonController );
46    }
47    CommonController::~CommonController() 
48    {
49       
50    }
51
52    float CommonController::randomInRange( float a, float b )
53    {
54        float random = rnd( 1.0f );
55        float diff = b - a;
56        float r = random * diff;
57        return a + r;
58    }
59    float CommonController::distance (ControllableEntity* entity1, ControllableEntity* entity2)
60    {
61        if (!entity1 || !entity2)
62            return std::numeric_limits<float>::infinity();
63        return ( entity1->getPosition() - entity2->getPosition() ).length();
64    }
65    bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
66    {
67        /*if (!entity1 || !entity2)
68            return false;
69        return entity1->getTeam() == entity2->getTeam();*/
70        if (entity1 == entity2)
71            return true;
72
73        int team1 = entity1->getTeam();
74        int team2 = entity2->getTeam();
75
76        Controller* controller = 0;
77        if (entity1->getController())
78            controller = entity1->getController();
79        else
80            controller = entity1->getXMLController();
81        if (controller)
82        {
83            CommonController* ac = orxonox_cast<CommonController*>(controller);
84            if (ac)
85                team1 = ac->getTeam();
86        }
87
88        if (entity2->getController())
89            controller = entity2->getController();
90        else
91            controller = entity2->getXMLController();
92        if (controller)
93        {
94            CommonController* ac = orxonox_cast<CommonController*>(controller);
95            if (ac)
96                team2 = ac->getTeam();
97        }
98
99        TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype);
100        if (tdm)
101        {
102            if (entity1->getPlayer())
103                team1 = tdm->getTeam(entity1->getPlayer());
104
105            if (entity2->getPlayer())
106                team2 = tdm->getTeam(entity2->getPlayer());
107        }
108
109        TeamBaseMatchBase* base = 0;
110        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
111        if (base)
112        {
113            switch (base->getState())
114            {
115                case BaseState::ControlTeam1:
116                    team1 = 0;
117                    break;
118                case BaseState::ControlTeam2:
119                    team1 = 1;
120                    break;
121                case BaseState::Uncontrolled:
122                default:
123                    team1 = -1;
124            }
125        }
126        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
127        if (base)
128        {
129            switch (base->getState())
130            {
131                case BaseState::ControlTeam1:
132                    team2 = 0;
133                    break;
134                case BaseState::ControlTeam2:
135                    team2 = 1;
136                    break;
137                case BaseState::Uncontrolled:
138                default:
139                    team2 = -1;
140            }
141        }
142
143        DroneController* droneController = 0;
144        droneController = orxonox_cast<DroneController*>(entity1->getController());
145        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
146            return true;
147        droneController = orxonox_cast<DroneController*>(entity2->getController());
148        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1)
149            return true;
150        DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController());
151        DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController());
152        if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner())
153            return true;
154
155        Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype);
156        if (dynamic)
157        {
158            if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;}
159
160            if (entity1->getPlayer())
161                team1 = dynamic->getParty(entity1->getPlayer());
162
163            if (entity2->getPlayer())
164                team2 = dynamic->getParty(entity2->getPlayer());
165
166            if (team1 ==-1 ||team2 ==-1 ) {return false;}
167            else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;}
168            else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;}
169            else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;}
170            else return true;
171        }
172
173        return (team1 == team2 && team1 != -1);
174    }
175    bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle )
176    {
177        if ( !entityThatLooks || !entityBeingLookedAt )
178            return false;
179        return ( getAngle( entityThatLooks ->getPosition() , 
180            entityThatLooks->getOrientation()  * WorldEntity::FRONT, 
181            entityBeingLookedAt->getWorldPosition() ) < angle );
182    }
183    std::string CommonController::getName(Pawn* entity)
184    {
185        std::string name = entity->getName();
186        if (name == "")
187        {
188            const void * address = static_cast<const void*>(entity);
189            std::stringstream ss;
190            ss << address; 
191            name = ss.str();           
192        }
193        return name;
194    }
195 
196
197}
Note: See TracBrowser for help on using the repository browser.