Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fix for sigsegv?

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 *      Dominik Solenicki
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
43    CommonController::CommonController(Context* context): Controller(context)
44    {
45        RegisterObject(CommonController);
46    }
47    CommonController::~CommonController() 
48    {
49        //no member variables - nothing to destroy
50    }
51    /**
52    @brief
53      PRE: a < b.
54      returns random float between a and b.
55    */
56    float CommonController::randomInRange(float a, float b)
57    {
58        return a + rnd(1.0f) * (b - a);
59    }
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)
65    {
66        if (!entity1 || !entity2)
67            return std::numeric_limits<float>::infinity();
68        return (entity1->getPosition() - entity2->getPosition()).length();
69    }
70    /**
71    @brief
72      bad function from FormationController that returns true if both entities have same team
73    */   
74    bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
75    {
76        //uncomment following code if functions stops working due to being a hack
77        /*if (!entity1 || !entity2)
78            return false;
79        return entity1->getTeam() == entity2->getTeam();*/
80        if (!entity1 || !entity2)
81            return false;
82        if (entity1 == entity2)
83            return true;
84
85        int team1 = entity1->getTeam();
86        int team2 = entity2->getTeam();
87
88        Controller* controller = 0;
89        if (entity1->getController())
90            controller = entity1->getController();
91        else
92            controller = entity1->getXMLController();
93        if (controller)
94        {
95            CommonController* ac = orxonox_cast<CommonController*>(controller);
96            if (ac)
97                team1 = ac->getTeam();
98        }
99
100        if (entity2->getController())
101            controller = entity2->getController();
102        else
103            controller = entity2->getXMLController();
104        if (controller)
105        {
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.