MCPcopy Create free account
hub / github.com/BZFlag-Dev/bzflag / avoidBullet

Function avoidBullet

src/bzflag/AutoPilot.cpp:231–305  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

229
230
231static bool avoidBullet(float& rotation, float& speed) {
232 LocalPlayer* myTank = LocalPlayer::getMyTank();
233 if (!myTank) {
234 return false;
235 }
236
237 const fvec3& pos = myTank->getPosition();
238
239 if ((myTank->getFlagType() == Flags::Narrow) ||
240 (myTank->getFlagType() == Flags::Burrow)) {
241 return false; // take our chances
242 }
243
244 float minDistance;
245 ShotPath* shot = findWorstBullet(minDistance);
246
247 if ((shot == NULL) || (minDistance > 100.0f)) {
248 return false;
249 }
250
251 const fvec3& shotPos = shot->getPosition();
252 const fvec3& shotVel = shot->getVelocity();
253 float shotAngle = atan2f(shotVel[1], shotVel[0]);
254 float shotUnitVec[2] = {cosf(shotAngle), sinf(shotAngle)};
255
256 float trueVec[2] = {(pos[0] - shotPos[0]) / minDistance, (pos[1] - shotPos[1]) / minDistance};
257 float dotProd = trueVec[0] * shotUnitVec[0] + trueVec[1] * shotUnitVec[1];
258
259 World* world = World::getWorld();
260 if (!world) {
261 return false;
262 }
263
264 if ((myTank->canJump())
265 && (minDistance < (std::max(dotProd, 0.5f) * BZDBCache::tankLength * 2.25f))) {
266 wantJump = true;
267 return (myTank->getFlagType() != Flags::Wings);
268 }
269 else if (dotProd > 0.96f) {
270 speed = 1.0;
271 float myAzimuth = myTank->getAngle();
272 float rotation1 = TargetingUtils::normalizeAngle((float)((shotAngle + M_PI / 2.0) - myAzimuth));
273
274 float rotation2 = TargetingUtils::normalizeAngle((float)((shotAngle - M_PI / 2.0) - myAzimuth));
275
276 float zCross = shotUnitVec[0] * trueVec[1] - shotUnitVec[1] * trueVec[0];
277
278 if (zCross > 0.0f) { //if i am to the left of the shot from shooter pov
279 rotation = rotation1;
280 if (fabs(rotation1) < fabs(rotation2)) {
281 speed = 1.0f;
282 }
283 else if (dotProd > 0.98f) {
284 speed = -0.5f;
285 }
286 else {
287 speed = 0.5f;
288 }

Callers 2

doAutoPilotFunction · 0.85
executeMethod · 0.85

Calls 6

findWorstBulletFunction · 0.85
getAngleMethod · 0.80
getPositionMethod · 0.45
getFlagTypeMethod · 0.45
getVelocityMethod · 0.45
canJumpMethod · 0.45

Tested by

no test coverage detected