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