Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Split up CommonController, so it is easier to debug

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