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 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "ArtificialController.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "core/XMLPort.h" |
---|
33 | #include "worldentities/ControllableEntity.h" |
---|
34 | #include "worldentities/pawns/Pawn.h" |
---|
35 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
36 | #include "gametypes/TeamDeathmatch.h" |
---|
37 | #include "controllers/WaypointPatrolController.h" |
---|
38 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) |
---|
42 | { |
---|
43 | RegisterObject(ArtificialController); |
---|
44 | |
---|
45 | this->target_ = 0; |
---|
46 | this->myMaster_ = 0; |
---|
47 | //this->slaveNumber_ = -1; |
---|
48 | this->team_ = -1;//new |
---|
49 | this->state_ = FREE;//new |
---|
50 | this->bShooting_ = false; |
---|
51 | this->bHasTargetPosition_ = false; |
---|
52 | this->targetPosition_ = Vector3::ZERO; |
---|
53 | |
---|
54 | this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); |
---|
55 | } |
---|
56 | |
---|
57 | ArtificialController::~ArtificialController() |
---|
58 | { |
---|
59 | } |
---|
60 | |
---|
61 | void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
62 | { |
---|
63 | SUPER(ArtificialController, XMLPort, xmlelement, mode); |
---|
64 | |
---|
65 | XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(0); |
---|
66 | } |
---|
67 | |
---|
68 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
69 | { |
---|
70 | if (!this->getControllableEntity()) |
---|
71 | return; |
---|
72 | |
---|
73 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
74 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
75 | |
---|
76 | if (this->target_ || distance > 10) |
---|
77 | { |
---|
78 | // Multiply with 0.8 to make them a bit slower |
---|
79 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
80 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
81 | } |
---|
82 | |
---|
83 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
84 | this->getControllableEntity()->moveFrontBack(-0.5f); // They don't brake with full power to give the player a chance |
---|
85 | else |
---|
86 | this->getControllableEntity()->moveFrontBack(0.8f); |
---|
87 | } |
---|
88 | |
---|
89 | void ArtificialController::moveToTargetPosition() |
---|
90 | { |
---|
91 | this->moveToPosition(this->targetPosition_); |
---|
92 | } |
---|
93 | |
---|
94 | int ArtificialController::getState() |
---|
95 | { |
---|
96 | return this->state_; |
---|
97 | } |
---|
98 | |
---|
99 | void ArtificialController::unregisterSlave() { |
---|
100 | if(myMaster_) |
---|
101 | { |
---|
102 | myMaster_->slaves.remove(this); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | void ArtificialController::searchNewMaster() |
---|
107 | { |
---|
108 | |
---|
109 | if (!this->getControllableEntity()) |
---|
110 | return; |
---|
111 | |
---|
112 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
113 | this->forgetTarget(); |
---|
114 | |
---|
115 | //go through all pawns |
---|
116 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
117 | { |
---|
118 | |
---|
119 | //same team? |
---|
120 | if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
121 | continue; |
---|
122 | |
---|
123 | //has it an ArtificialController and is it a master? |
---|
124 | if (!it->getController()) |
---|
125 | continue; |
---|
126 | |
---|
127 | ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); |
---|
128 | if (!controller || controller->getState() != MASTER) |
---|
129 | continue; |
---|
130 | |
---|
131 | //is pawn oneself? && is pawn in range? |
---|
132 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 |
---|
133 | { |
---|
134 | if(controller->slaves.size() > 9) continue; |
---|
135 | |
---|
136 | this->freeAllSlaves(); |
---|
137 | this->slaves.clear(); |
---|
138 | this->state_ = SLAVE; |
---|
139 | |
---|
140 | this->myMaster_ = controller; |
---|
141 | controller->slaves.push_back(this); |
---|
142 | |
---|
143 | break; |
---|
144 | } |
---|
145 | }//for |
---|
146 | |
---|
147 | //hasn't encountered any masters in range? -> become a master |
---|
148 | if (state_!=SLAVE) state_ = MASTER; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop within master mode in AIcontroller... |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | void ArtificialController::commandSlaves() { |
---|
153 | |
---|
154 | for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++) |
---|
155 | { |
---|
156 | (*it)->setTargetPosition(this->getControllableEntity()->getPosition()); |
---|
157 | } |
---|
158 | /* |
---|
159 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
160 | { |
---|
161 | |
---|
162 | if (!it->getController()) |
---|
163 | continue; |
---|
164 | |
---|
165 | ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); |
---|
166 | if (!controller || controller->getState() != SLAVE) |
---|
167 | continue; |
---|
168 | //controller->setTargetPosition(this->getControllableEntity()->getPosition()); |
---|
169 | controller->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
170 | controller->bHasTargetPosition_ = true; |
---|
171 | } |
---|
172 | */ |
---|
173 | } |
---|
174 | |
---|
175 | void ArtificialController::freeAllSlaves() |
---|
176 | { |
---|
177 | |
---|
178 | |
---|
179 | for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++) |
---|
180 | { |
---|
181 | (*it)->state_ = FREE; |
---|
182 | } |
---|
183 | /* |
---|
184 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
185 | { |
---|
186 | ArtificialController *controller = static_cast<ArtificialController*>(it->getController()); |
---|
187 | if (controller && controller->getState() != SLAVE) |
---|
188 | continue; |
---|
189 | |
---|
190 | controller->state_ = FREE; |
---|
191 | } |
---|
192 | */ |
---|
193 | } |
---|
194 | |
---|
195 | void ArtificialController::loseMasterState() |
---|
196 | { |
---|
197 | this->freeAllSlaves(); |
---|
198 | this->state_ = FREE; |
---|
199 | } |
---|
200 | |
---|
201 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
202 | { |
---|
203 | this->targetPosition_ = target; |
---|
204 | this->bHasTargetPosition_ = true; |
---|
205 | } |
---|
206 | |
---|
207 | void ArtificialController::searchRandomTargetPosition() |
---|
208 | { |
---|
209 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
210 | this->bHasTargetPosition_ = true; |
---|
211 | } |
---|
212 | |
---|
213 | void ArtificialController::setTarget(Pawn* target) |
---|
214 | { |
---|
215 | this->target_ = target; |
---|
216 | |
---|
217 | if (target) |
---|
218 | this->targetPosition_ = target->getPosition(); |
---|
219 | } |
---|
220 | |
---|
221 | void ArtificialController::searchNewTarget() |
---|
222 | { |
---|
223 | if (!this->getControllableEntity()) |
---|
224 | return; |
---|
225 | |
---|
226 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
227 | this->forgetTarget(); |
---|
228 | |
---|
229 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
230 | { |
---|
231 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
232 | continue; |
---|
233 | |
---|
234 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
235 | { |
---|
236 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
237 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
238 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
239 | if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / (2 * Ogre::Math::PI)) |
---|
240 | < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / (2 * Ogre::Math::PI)) + rnd(-250, 250)) |
---|
241 | { |
---|
242 | this->target_ = (*it); |
---|
243 | this->targetPosition_ = it->getPosition(); |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | void ArtificialController::forgetTarget() |
---|
250 | { |
---|
251 | this->target_ = 0; |
---|
252 | this->bShooting_ = false; |
---|
253 | } |
---|
254 | |
---|
255 | void ArtificialController::aimAtTarget() |
---|
256 | { |
---|
257 | if (!this->target_ || !this->getControllableEntity()) |
---|
258 | return; |
---|
259 | |
---|
260 | static const float hardcoded_projectile_speed = 1250; |
---|
261 | |
---|
262 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
263 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
264 | |
---|
265 | Pawn* pawn = dynamic_cast<Pawn*>(this->getControllableEntity()); |
---|
266 | if (pawn) |
---|
267 | pawn->setAimPosition(this->targetPosition_); |
---|
268 | } |
---|
269 | |
---|
270 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
271 | { |
---|
272 | if (!this->getControllableEntity()) |
---|
273 | return false; |
---|
274 | |
---|
275 | if (!this->target_) |
---|
276 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
277 | else |
---|
278 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
279 | } |
---|
280 | |
---|
281 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
282 | { |
---|
283 | if (!this->getControllableEntity()) |
---|
284 | return false; |
---|
285 | |
---|
286 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
287 | } |
---|
288 | |
---|
289 | void ArtificialController::abandonTarget(Pawn* target) |
---|
290 | { |
---|
291 | if (target == this->target_) |
---|
292 | this->targetDied(); |
---|
293 | } |
---|
294 | |
---|
295 | void ArtificialController::targetDied() |
---|
296 | { |
---|
297 | this->forgetTarget(); |
---|
298 | this->searchRandomTargetPosition(); |
---|
299 | } |
---|
300 | |
---|
301 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
302 | { |
---|
303 | if (entity1 == entity2) |
---|
304 | return true; |
---|
305 | |
---|
306 | int team1 = -1; |
---|
307 | int team2 = -1; |
---|
308 | |
---|
309 | Controller* controller = 0; |
---|
310 | if (entity1->getController()) |
---|
311 | controller = entity1->getController(); |
---|
312 | else |
---|
313 | controller = entity1->getXMLController(); |
---|
314 | if (controller) |
---|
315 | { |
---|
316 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
317 | if (ac) |
---|
318 | team1 = ac->getTeam(); |
---|
319 | } |
---|
320 | |
---|
321 | if (entity1->getController()) |
---|
322 | controller = entity1->getController(); |
---|
323 | else |
---|
324 | controller = entity1->getXMLController(); |
---|
325 | if (controller) |
---|
326 | { |
---|
327 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
328 | if (ac) |
---|
329 | team2 = ac->getTeam(); |
---|
330 | } |
---|
331 | |
---|
332 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
333 | if (tdm) |
---|
334 | { |
---|
335 | if (entity1->getPlayer()) |
---|
336 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
337 | |
---|
338 | if (entity2->getPlayer()) |
---|
339 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
340 | } |
---|
341 | |
---|
342 | TeamBaseMatchBase* base = 0; |
---|
343 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
344 | if (base) |
---|
345 | { |
---|
346 | switch (base->getState()) |
---|
347 | { |
---|
348 | case BaseState::ControlTeam1: |
---|
349 | team1 = 0; |
---|
350 | break; |
---|
351 | case BaseState::ControlTeam2: |
---|
352 | team1 = 1; |
---|
353 | break; |
---|
354 | case BaseState::Uncontrolled: |
---|
355 | default: |
---|
356 | team1 = -1; |
---|
357 | } |
---|
358 | } |
---|
359 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
360 | if (base) |
---|
361 | { |
---|
362 | switch (base->getState()) |
---|
363 | { |
---|
364 | case BaseState::ControlTeam1: |
---|
365 | team2 = 0; |
---|
366 | break; |
---|
367 | case BaseState::ControlTeam2: |
---|
368 | team2 = 1; |
---|
369 | break; |
---|
370 | case BaseState::Uncontrolled: |
---|
371 | default: |
---|
372 | team2 = -1; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | return (team1 == team2 && team1 != -1); |
---|
377 | } |
---|
378 | } |
---|