Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

a little clean up

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