Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed library dependencies

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