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 | //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 | |
---|
38 | namespace 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 | return a + rnd(1.0f) * (b - a); |
---|
55 | } |
---|
56 | float CommonController::distance (ControllableEntity* entity1, ControllableEntity* entity2) |
---|
57 | { |
---|
58 | if (!entity1 || !entity2) |
---|
59 | return std::numeric_limits<float>::infinity(); |
---|
60 | return ( entity1->getPosition() - entity2->getPosition() ).length(); |
---|
61 | } |
---|
62 | bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
63 | { |
---|
64 | /*if (!entity1 || !entity2) |
---|
65 | return false; |
---|
66 | return entity1->getTeam() == entity2->getTeam();*/ |
---|
67 | if (entity1 == entity2) |
---|
68 | return true; |
---|
69 | |
---|
70 | int team1 = entity1->getTeam(); |
---|
71 | int team2 = entity2->getTeam(); |
---|
72 | |
---|
73 | Controller* controller = 0; |
---|
74 | if (entity1->getController()) |
---|
75 | controller = entity1->getController(); |
---|
76 | else |
---|
77 | controller = entity1->getXMLController(); |
---|
78 | if (controller) |
---|
79 | { |
---|
80 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
81 | if (ac) |
---|
82 | team1 = ac->getTeam(); |
---|
83 | } |
---|
84 | |
---|
85 | if (entity2->getController()) |
---|
86 | controller = entity2->getController(); |
---|
87 | else |
---|
88 | controller = entity2->getXMLController(); |
---|
89 | if (controller) |
---|
90 | { |
---|
91 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
---|
92 | if (ac) |
---|
93 | team2 = ac->getTeam(); |
---|
94 | } |
---|
95 | |
---|
96 | TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype); |
---|
97 | if (tdm) |
---|
98 | { |
---|
99 | if (entity1->getPlayer()) |
---|
100 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
101 | |
---|
102 | if (entity2->getPlayer()) |
---|
103 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
104 | } |
---|
105 | |
---|
106 | TeamBaseMatchBase* base = 0; |
---|
107 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
108 | if (base) |
---|
109 | { |
---|
110 | switch (base->getState()) |
---|
111 | { |
---|
112 | case BaseState::ControlTeam1: |
---|
113 | team1 = 0; |
---|
114 | break; |
---|
115 | case BaseState::ControlTeam2: |
---|
116 | team1 = 1; |
---|
117 | break; |
---|
118 | case BaseState::Uncontrolled: |
---|
119 | default: |
---|
120 | team1 = -1; |
---|
121 | } |
---|
122 | } |
---|
123 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
124 | if (base) |
---|
125 | { |
---|
126 | switch (base->getState()) |
---|
127 | { |
---|
128 | case BaseState::ControlTeam1: |
---|
129 | team2 = 0; |
---|
130 | break; |
---|
131 | case BaseState::ControlTeam2: |
---|
132 | team2 = 1; |
---|
133 | break; |
---|
134 | case BaseState::Uncontrolled: |
---|
135 | default: |
---|
136 | team2 = -1; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | DroneController* droneController = 0; |
---|
141 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
142 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
143 | return true; |
---|
144 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
145 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
146 | return true; |
---|
147 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
148 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
149 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
150 | return true; |
---|
151 | |
---|
152 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
153 | if (dynamic) |
---|
154 | { |
---|
155 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
156 | |
---|
157 | if (entity1->getPlayer()) |
---|
158 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
159 | |
---|
160 | if (entity2->getPlayer()) |
---|
161 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
162 | |
---|
163 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
164 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
165 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
166 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
167 | else return true; |
---|
168 | } |
---|
169 | |
---|
170 | return (team1 == team2 && team1 != -1); |
---|
171 | } |
---|
172 | bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) |
---|
173 | { |
---|
174 | if ( !entityThatLooks || !entityBeingLookedAt ) |
---|
175 | return false; |
---|
176 | return ( getAngle( entityThatLooks ->getPosition() , |
---|
177 | entityThatLooks->getOrientation() * WorldEntity::FRONT, |
---|
178 | entityBeingLookedAt->getWorldPosition() ) < angle ); |
---|
179 | } |
---|
180 | std::string CommonController::getName(Pawn* entity) |
---|
181 | { |
---|
182 | std::string name = entity->getName(); |
---|
183 | if (name == "") |
---|
184 | { |
---|
185 | const void * address = static_cast<const void*>(entity); |
---|
186 | std::stringstream ss; |
---|
187 | ss << address; |
---|
188 | name = ss.str(); |
---|
189 | } |
---|
190 | return name; |
---|
191 | } |
---|
192 | } |
---|