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 | * Fabian 'x3n' Landau, Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | #include "controllers/FightingController.h" |
---|
29 | #include "core/XMLPort.h" |
---|
30 | #include "util/Math.h" |
---|
31 | |
---|
32 | |
---|
33 | #include "worldentities/pawns/SpaceShip.h" |
---|
34 | |
---|
35 | #include "weaponsystem/WeaponMode.h" |
---|
36 | #include "weaponsystem/WeaponPack.h" |
---|
37 | #include "weaponsystem/Weapon.h" |
---|
38 | #include "weaponsystem/WeaponSlot.h" |
---|
39 | #include "weaponsystem/WeaponSystem.h" |
---|
40 | #include "weaponsystem/Munition.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | |
---|
45 | RegisterClass (FightingController); |
---|
46 | |
---|
47 | FightingController::FightingController( Context* context ): FlyingController( context ) |
---|
48 | { |
---|
49 | this->attackRange_ = 2500; |
---|
50 | this->stopLookingAtTarget(); |
---|
51 | this->bSetupWorked = false; |
---|
52 | this->timeout_ = 0; |
---|
53 | RegisterObject( FightingController ); |
---|
54 | } |
---|
55 | FightingController::~FightingController() |
---|
56 | { |
---|
57 | |
---|
58 | } |
---|
59 | void FightingController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
---|
60 | { |
---|
61 | SUPER( FightingController, XMLPort, xmlelement, mode ); |
---|
62 | } |
---|
63 | void FightingController::lookAtTarget(float dt) |
---|
64 | { |
---|
65 | if (!this || !this->getControllableEntity()) |
---|
66 | return; |
---|
67 | ControllableEntity* entity = this->getControllableEntity(); |
---|
68 | if ( !entity ) |
---|
69 | return; |
---|
70 | Vector2 coord = get2DViewCoordinates |
---|
71 | ( entity->getPosition() , |
---|
72 | entity->getOrientation() * WorldEntity::FRONT, |
---|
73 | entity->getOrientation() * WorldEntity::UP, |
---|
74 | positionOfTarget_ ); |
---|
75 | |
---|
76 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
---|
77 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
---|
78 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
---|
79 | |
---|
80 | //Yaw and Pitch are enough to start facing the target |
---|
81 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
---|
82 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
---|
83 | } |
---|
84 | void FightingController::stopLookingAtTarget() |
---|
85 | { |
---|
86 | this->bLookAtTarget_ = false; |
---|
87 | } |
---|
88 | void FightingController::startLookingAtTarget() |
---|
89 | { |
---|
90 | this->bLookAtTarget_ = true; |
---|
91 | } |
---|
92 | bool FightingController::hasTarget() const |
---|
93 | { |
---|
94 | if ( this->target_ ) |
---|
95 | return true; |
---|
96 | return false; |
---|
97 | } |
---|
98 | |
---|
99 | void FightingController::setTarget( ControllableEntity* target ) |
---|
100 | { |
---|
101 | this->target_ = target; |
---|
102 | if ( this->target_ ) |
---|
103 | { |
---|
104 | this->setPositionOfTarget( target_->getWorldPosition() ); |
---|
105 | } |
---|
106 | } |
---|
107 | void FightingController::setPositionOfTarget( const Vector3& target ) |
---|
108 | { |
---|
109 | this->positionOfTarget_ = target; |
---|
110 | this->bHasPositionOfTarget_ = true; |
---|
111 | } |
---|
112 | void FightingController::setOrientationOfTarget( const Quaternion& orient ) |
---|
113 | { |
---|
114 | this->orientationOfTarget_=orient; |
---|
115 | this->bHasOrientationOfTarget_=true; |
---|
116 | } |
---|
117 | |
---|
118 | void FightingController::maneuver() |
---|
119 | { |
---|
120 | if ( !this->target_ || !this->getControllableEntity()) |
---|
121 | return; |
---|
122 | |
---|
123 | |
---|
124 | Vector3 thisPosition = this->getControllableEntity()->getWorldPosition(); |
---|
125 | this->setPositionOfTarget(this->target_->getWorldPosition()); |
---|
126 | //this->setOrientationOfTarget(this->target_->getOrientation()); |
---|
127 | Vector3 diffVector = this->positionOfTarget_ - thisPosition; |
---|
128 | float diffLength = diffVector.length(); |
---|
129 | Vector3 diffUnit = diffVector/diffLength; |
---|
130 | |
---|
131 | if (!this || !this->getControllableEntity()) |
---|
132 | return; |
---|
133 | |
---|
134 | //too far? well, come closer then |
---|
135 | if (diffLength > this->attackRange_) |
---|
136 | { |
---|
137 | this->spread_ = 400; |
---|
138 | this->formationMode_ = FormationMode::DIAMOND; |
---|
139 | this->bKeepFormation_ = true; |
---|
140 | |
---|
141 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 100.0f); |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | bool bTargetIsLookingAtThis = CommonController::isLooking (this->target_, this->getControllableEntity(), math::pi/20.0f) |
---|
146 | || this->deltaHp < 0; |
---|
147 | this->bKeepFormation_ = false; |
---|
148 | |
---|
149 | if (!this || !this->getControllableEntity()) |
---|
150 | return; |
---|
151 | if (!this->bDodge_) |
---|
152 | { |
---|
153 | this->bStartedDodging_ = false; |
---|
154 | |
---|
155 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 50.0f); |
---|
156 | return; |
---|
157 | } |
---|
158 | else if (bTargetIsLookingAtThis || diffLength < 700.0f) |
---|
159 | { |
---|
160 | if (!this->bStartedDodging_) |
---|
161 | { |
---|
162 | this->bStartedDodging_ = true; |
---|
163 | dodge(thisPosition, diffLength, diffUnit); |
---|
164 | } |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | if (diffLength < 1000) |
---|
169 | { |
---|
170 | this->stopMoving(); |
---|
171 | this->startLookingAtTarget(); |
---|
172 | |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 300.0f); |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | void FightingController::dodge(const Vector3& thisPosition, float diffLength, Vector3& diffUnit) |
---|
183 | { |
---|
184 | //d.x*x + d.y*y + d.z*z == 0 |
---|
185 | //z = 1/d.z * (-d.y*y - d.x * x) |
---|
186 | float x = CommonController::randomInRange (300, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1); |
---|
187 | float y = CommonController::randomInRange (300, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1); |
---|
188 | float z = diffUnit.z == 0 ? 0 : (1/diffUnit.z) * (-x * diffUnit.x - y * diffUnit.y); |
---|
189 | if (diffLength < 150.0f) |
---|
190 | { |
---|
191 | this->setTargetPosition(this->positionOfTarget_ + Vector3(z,x,y)); |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | this->setTargetPosition(thisPosition + Vector3(x,y,z) + (this->deltaHp < 0 ? -diffUnit * 450.0f : |
---|
196 | (diffLength < 700.0f ? -diffUnit*700.0f : diffUnit * 50.0f))); |
---|
197 | |
---|
198 | } |
---|
199 | this->boostControl(); |
---|
200 | |
---|
201 | } |
---|
202 | bool FightingController::canFire() |
---|
203 | { |
---|
204 | //no target? why fire? |
---|
205 | if (!this->target_ || !this->getControllableEntity()) |
---|
206 | return false; |
---|
207 | Vector3 newPositionOfTarget = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), |
---|
208 | HARDCODED_PROJECTILE_SPEED, this->target_->getWorldPosition(), |
---|
209 | this->target_->getVelocity()); |
---|
210 | if (!this->target_ || !this->getControllableEntity()) |
---|
211 | return false; |
---|
212 | //Vector3.isNaN() is what I used on my machine and it worked... |
---|
213 | if (!(std::isnan(newPositionOfTarget.x) || std::isnan(newPositionOfTarget.y) || std::isnan(newPositionOfTarget.z))) |
---|
214 | { |
---|
215 | this->setPositionOfTarget(newPositionOfTarget); |
---|
216 | } |
---|
217 | |
---|
218 | return squaredDistanceToTarget() < this->attackRange_*this->attackRange_ && this->isLookingAtTarget(math::pi / 20.0f); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | float FightingController::squaredDistanceToTarget() const |
---|
223 | { |
---|
224 | if (!this || !this->getControllableEntity()) |
---|
225 | return 0; |
---|
226 | if (!this->target_ || !this->getControllableEntity()) |
---|
227 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_)); |
---|
228 | else |
---|
229 | return (this->getControllableEntity()->getPosition().squaredDistance(this->positionOfTarget_)); |
---|
230 | } |
---|
231 | bool FightingController::isLookingAtTarget( float angle ) const |
---|
232 | { |
---|
233 | if ( !this->getControllableEntity() || !this->target_ ) |
---|
234 | return false; |
---|
235 | return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle); |
---|
236 | } |
---|
237 | void FightingController::setClosestTarget() |
---|
238 | { |
---|
239 | this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); |
---|
240 | } |
---|
241 | |
---|
242 | Pawn* FightingController::closestTarget() const |
---|
243 | { |
---|
244 | if (!this || !this->getControllableEntity()) |
---|
245 | return 0; |
---|
246 | |
---|
247 | Pawn* closestTarget = 0; |
---|
248 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
249 | Gametype* gt = this->getGametype(); |
---|
250 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
251 | { |
---|
252 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
253 | continue; |
---|
254 | |
---|
255 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
---|
256 | if (distance < minDistance) |
---|
257 | { |
---|
258 | closestTarget = *itP; |
---|
259 | minDistance = distance; |
---|
260 | } |
---|
261 | } |
---|
262 | if (closestTarget) |
---|
263 | { |
---|
264 | return closestTarget; |
---|
265 | } |
---|
266 | return 0; |
---|
267 | } |
---|
268 | //I checked it out, rockets DO NOT cause any problems! this->getControllableEntity() is always a SpaceShip |
---|
269 | void FightingController::doFire() |
---|
270 | { |
---|
271 | if (!this->bSetupWorked) |
---|
272 | { |
---|
273 | this->setupWeapons(); |
---|
274 | } |
---|
275 | if (!this->target_ || !this->getControllableEntity()) |
---|
276 | { |
---|
277 | return; |
---|
278 | } |
---|
279 | |
---|
280 | Pawn* pawn = orxonox_cast<Pawn*> (this->getControllableEntity()); |
---|
281 | if (pawn) |
---|
282 | pawn->setAimPosition (this->positionOfTarget_); |
---|
283 | |
---|
284 | int firemode; |
---|
285 | float distance = CommonController::distance (this->getControllableEntity(), this->target_); |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | if (distance < 1500) |
---|
290 | { |
---|
291 | if (this->rocketsLeft_ > 0 && !this->bFiredRocket_) |
---|
292 | { |
---|
293 | firemode = getFiremode ("RocketFire"); |
---|
294 | } |
---|
295 | else |
---|
296 | { |
---|
297 | if (distance > 800) |
---|
298 | firemode = getFiremode ("HsW01"); |
---|
299 | else |
---|
300 | firemode = getFiremode ("LightningGun"); |
---|
301 | } |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | else if (distance < 2000) |
---|
307 | { |
---|
308 | firemode = getFiremode ("HsW01"); |
---|
309 | } |
---|
310 | else |
---|
311 | { |
---|
312 | firemode = getFiremode ("LightningGun"); |
---|
313 | } |
---|
314 | if (firemode < 0) |
---|
315 | { |
---|
316 | //assuming there is always some weapon with index 0 |
---|
317 | firemode = 0; |
---|
318 | } |
---|
319 | if (firemode == getFiremode("RocketFire")) |
---|
320 | { |
---|
321 | this->timeout_ = 5; |
---|
322 | this->rocketsLeft_--; |
---|
323 | this->bFiredRocket_ = true; |
---|
324 | } |
---|
325 | if (firemode == getFiremode("SimpleRocketFire")) |
---|
326 | { |
---|
327 | this->rocketsLeft_--; |
---|
328 | } |
---|
329 | |
---|
330 | this->getControllableEntity()->fire(firemode); |
---|
331 | |
---|
332 | } |
---|
333 | void FightingController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions) |
---|
334 | { |
---|
335 | this->bSetupWorked = false; |
---|
336 | if(this->getControllableEntity()) |
---|
337 | { |
---|
338 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
339 | if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip |
---|
340 | { |
---|
341 | this->weaponModes_.clear(); // reset previous weapon information |
---|
342 | WeaponSlot* wSlot = 0; |
---|
343 | for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++) |
---|
344 | { |
---|
345 | WeaponMode* wMode = 0; |
---|
346 | for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++) |
---|
347 | { |
---|
348 | std::string wName = wMode->getIdentifier()->getName(); |
---|
349 | // SubclassIdentifier<Munition> munition = ClassByString(wName); |
---|
350 | if (wName == "RocketFire") |
---|
351 | this->rocketsLeft_ = 10; |
---|
352 | if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new" |
---|
353 | weaponModes_[wName] = wMode->getMode(); |
---|
354 | } |
---|
355 | } |
---|
356 | if(weaponModes_.size())//at least one weapon detected |
---|
357 | this->bSetupWorked = true; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | } |
---|
362 | |
---|
363 | int FightingController::getFiremode(std::string name) |
---|
364 | { |
---|
365 | for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it) |
---|
366 | { |
---|
367 | if (it->first == name) |
---|
368 | return it->second; |
---|
369 | } |
---|
370 | return -1; |
---|
371 | } |
---|
372 | } |
---|