本文整理汇总了C++中Ship::Facing方法的典型用法代码示例。如果您正苦于以下问题:C++ Ship::Facing方法的具体用法?C++ Ship::Facing怎么用?C++ Ship::Facing使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ship
的用法示例。
在下文中一共展示了Ship::Facing方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Attack
void AI::Attack(Ship &ship, Command &command, const Ship &target)
{
Point d = target.Position() - ship.Position();
// First, figure out what your shortest-range weapon is.
double shortestRange = 4000.;
for(const Armament::Weapon &weapon : ship.Weapons())
{
const Outfit *outfit = weapon.GetOutfit();
if(outfit && !weapon.IsAntiMissile())
shortestRange = min(outfit->Range(), shortestRange);
}
// Deploy any fighters you are carrying.
if(!ship.IsYours())
command |= Command::DEPLOY;
// If this ship only has long-range weapons, it should keep its distance
// instead of trying to close with the target ship.
if(shortestRange > 1000. && d.Length() < .5 * shortestRange)
{
command.SetTurn(TurnToward(ship, -d));
if(ship.Facing().Unit().Dot(d) <= 0.)
command |= Command::FORWARD;
return;
}
// First of all, aim in the direction that will hit this target.
command.SetTurn(TurnToward(ship, TargetAim(ship)));
// Calculate this ship's "turning radius; that is, the smallest circle it
// can make while at full speed.
double stepsInFullTurn = 360. / ship.TurnRate();
double circumference = stepsInFullTurn * ship.Velocity().Length();
double diameter = max(200., circumference / PI);
// This isn't perfect, but it works well enough.
if((ship.Facing().Unit().Dot(d) >= 0. && d.Length() > diameter)
|| (ship.Velocity().Dot(d) < 0. && ship.Facing().Unit().Dot(d.Unit()) >= .9))
command |= Command::FORWARD;
}
示例2: RendezvousTime
// Fire this weapon. If it is a turret, it automatically points toward
// the given ship's target. If the weapon requires ammunition, it will
// be subtracted from the given ship.
void Armament::Weapon::Fire(Ship &ship, list<Projectile> &projectiles, list<Effect> &effects)
{
// Since this is only called internally by Armament (no one else has non-
// const access), assume Armament checked that this is a valid call.
Angle aim = ship.Facing();
// Get projectiles to start at the right position. They are drawn at an
// offset of (.5 * velocity) and that velocity includes the velocity of the
// ship that fired them.
Point start = ship.Position() + aim.Rotate(point) - .5 * ship.Velocity();
shared_ptr<const Ship> target = ship.GetTargetShip();
// If you are boarding your target, do not fire on it.
if(ship.IsBoarding() || ship.Commands().Has(Command::BOARD))
target.reset();
if(!isTurret || !target || target->GetSystem() != ship.GetSystem())
aim += angle;
else
{
Point p = target->Position() - start + ship.GetPersonality().Confusion();
Point v = target->Velocity() - ship.Velocity();
double steps = RendezvousTime(p, v, outfit->Velocity());
// Special case: RendezvousTime() may return NaN. But in that case, this
// comparison will return false.
if(!(steps < outfit->TotalLifetime()))
steps = outfit->TotalLifetime();
p += steps * v;
aim = Angle(TO_DEG * atan2(p.X(), -p.Y()));
}
projectiles.emplace_back(ship, start, aim, outfit);
if(outfit->WeaponSound())
Audio::Play(outfit->WeaponSound(), start);
double force = outfit->FiringForce();
if(force)
ship.ApplyForce(aim.Unit() * -force);
for(const auto &eit : outfit->FireEffects())
for(int i = 0; i < eit.second; ++i)
{
effects.push_back(*eit.first);
effects.back().Place(start, ship.Velocity(), aim);
}
Fire(ship);
}
示例3: Stop
bool AI::Stop(Ship &ship, Command &command, double slow)
{
const Point &velocity = ship.Velocity();
const Angle &angle = ship.Facing();
double speed = velocity.Length();
if(speed <= slow)
return true;
command.SetTurn(TurnBackward(ship));
if(velocity.Unit().Dot(angle.Unit()) < -.8)
command |= Command::FORWARD;
return false;
}
示例4: TurnToward
double AI::TurnToward(const Ship &ship, const Point &vector)
{
static const double RAD_TO_DEG = 180. / 3.14159265358979;
Point facing = ship.Facing().Unit();
double cross = vector.Cross(facing);
if(vector.Dot(facing) > 0.)
{
double angle = asin(cross / vector.Length()) * RAD_TO_DEG;
if(fabs(angle) <= ship.TurnRate())
return -angle / ship.TurnRate();
}
bool left = cross < 0.;
return left - !left;
}
示例5: TurnBackward
double AI::TurnBackward(const Ship &ship)
{
Angle angle = ship.Facing();
bool left = ship.Velocity().Cross(angle.Unit()) > 0.;
double turn = left - !left;
// Check if the ship will still be pointing to the same side of the target
// angle if it turns by this amount.
angle += ship.TurnRate() * turn;
bool stillLeft = ship.Velocity().Cross(angle.Unit()) > 0.;
if(left == stillLeft)
return turn;
// If we're within one step of the correct direction, stop turning.
return 0.;
}
示例6:
// Fire an anti-missile. Returns true if the missile should be killed.
bool Armament::Weapon::FireAntiMissile(Ship &ship, const Projectile &projectile, list<Effect> &effects)
{
int strength = outfit->AntiMissile();
if(!strength)
return false;
double range = outfit->Velocity();
// Check if the missile is in range.
Point start = ship.Position() + ship.Facing().Rotate(point);
Point offset = projectile.Position() - start;
if(offset.Length() > range)
return false;
// Firing effects are displayed at the anti-missile that just fired.
Angle aim = TO_DEG * atan2(offset.X(), -offset.Y());
for(const auto &eit : outfit->FireEffects())
for(int i = 0; i < eit.second; ++i)
{
effects.push_back(*eit.first);
effects.back().Place(start, ship.Velocity(), aim);
}
// Figure out where the effect should be placed. Anti-missiles do not create
// projectiles; they just create a blast animation.
start += (.5 * range) * offset.Unit();
for(const auto &eit : outfit->HitEffects())
for(int i = 0; i < eit.second; ++i)
{
effects.push_back(*eit.first);
effects.back().Place(start, ship.Velocity(), aim);
}
// Die effects are displayed at the projectile, whether or not it actually "dies."
for(const auto &eit : outfit->DieEffects())
for(int i = 0; i < eit.second; ++i)
{
effects.push_back(*eit.first);
effects.back().Place(projectile.Position(), projectile.Velocity(), aim);
}
Fire(ship);
return (Random::Int(strength) > Random::Int(projectile.MissileStrength()));
}
示例7: MoveTo
bool AI::MoveTo(Ship &ship, Command &command, const Point &target, double radius, double slow)
{
const Point &position = ship.Position();
const Point &velocity = ship.Velocity();
const Angle &angle = ship.Facing();
Point distance = target - position;
double speed = velocity.Length();
bool isClose = (distance.Length() < radius);
if(isClose && speed < slow)
return true;
distance = target - StoppingPoint(ship);
bool isFacing = (distance.Unit().Dot(angle.Unit()) > .8);
if(!isClose || !isFacing)
command.SetTurn(TurnToward(ship, distance));
if(isFacing)
command |= Command::FORWARD;
return false;
}
示例8: StoppingPoint
Point AI::StoppingPoint(const Ship &ship)
{
const Point &position = ship.Position();
const Point &velocity = ship.Velocity();
const Angle &angle = ship.Facing();
double acceleration = ship.Acceleration();
double turnRate = ship.TurnRate();
// If I were to turn around and stop now, where would that put me?
double v = velocity.Length();
if(!v)
return position;
// This assumes you're facing exactly the wrong way.
double degreesToTurn = TO_DEG * acos(-velocity.Unit().Dot(angle.Unit()));
double stopDistance = v * (degreesToTurn / turnRate);
// Sum of: v + (v - a) + (v - 2a) + ... + 0.
// The number of terms will be v / a.
// The average term's value will be v / 2. So:
stopDistance += .5 * v * v / acceleration;
return position + stopDistance * velocity.Unit();
}
示例9: InBlastRadius
// Check if the given ship is within this projectile's blast radius. (The
// projectile will not explode unless it is also within the trigger radius.)
bool Projectile::InBlastRadius(const Ship &ship, int step) const
{
const Mask &mask = ship.GetSprite().GetMask(step);
return mask.WithinRange(position - ship.Position(), ship.Facing(), weapon->BlastRadius());
}
示例10: MoveIndependent
void AI::MoveIndependent(Ship &ship, Command &command) const
{
if(ship.Position().Length() >= 10000.)
{
MoveTo(ship, command, Point(), 40., .8);
return;
}
shared_ptr<const Ship> target = ship.GetTargetShip();
if(target && (ship.GetGovernment()->IsEnemy(target->GetGovernment())
|| (ship.IsYours() && target == sharedTarget.lock())))
{
bool shouldBoard = ship.Cargo().Free() && ship.GetPersonality().Plunders();
bool hasBoarded = Has(ship, target, ShipEvent::BOARD);
if(shouldBoard && target->IsDisabled() && !hasBoarded)
{
if(ship.IsBoarding())
return;
MoveTo(ship, command, target->Position(), 40., .8);
command |= Command::BOARD;
}
else
Attack(ship, command, *target);
return;
}
else if(target)
{
bool cargoScan = ship.Attributes().Get("cargo scan");
bool outfitScan = ship.Attributes().Get("outfit scan");
if((!cargoScan || Has(ship, target, ShipEvent::SCAN_CARGO))
&& (!outfitScan || Has(ship, target, ShipEvent::SCAN_OUTFITS)))
target.reset();
else
{
CircleAround(ship, command, *target);
if(!ship.GetGovernment()->IsPlayer())
command |= Command::SCAN;
}
return;
}
// If this ship is moving independently because it has a target, not because
// it has no parent, don't let it make travel plans.
if(ship.GetParent() && !ship.GetPersonality().IsStaying())
return;
if(!ship.GetTargetSystem() && !ship.GetTargetPlanet() && !ship.GetPersonality().IsStaying())
{
int jumps = ship.JumpsRemaining();
// Each destination system has an average priority of 10.
// If you only have one jump left, landing should be high priority.
int planetWeight = jumps ? (1 + 40 / jumps) : 1;
vector<int> systemWeights;
int totalWeight = 0;
const vector<const System *> &links = ship.Attributes().Get("jump drive")
? ship.GetSystem()->Neighbors() : ship.GetSystem()->Links();
if(jumps)
{
for(const System *link : links)
{
// Prefer systems in the direction we're facing.
Point direction = link->Position() - ship.GetSystem()->Position();
int weight = static_cast<int>(
11. + 10. * ship.Facing().Unit().Dot(direction.Unit()));
systemWeights.push_back(weight);
totalWeight += weight;
}
}
int systemTotalWeight = totalWeight;
// Anywhere you can land that has a port has the same weight. Ships will
// not land anywhere without a port.
vector<const StellarObject *> planets;
for(const StellarObject &object : ship.GetSystem()->Objects())
if(object.GetPlanet() && object.GetPlanet()->HasSpaceport()
&& object.GetPlanet()->CanLand(ship))
{
planets.push_back(&object);
totalWeight += planetWeight;
}
if(!totalWeight)
return;
int choice = Random::Int(totalWeight);
if(choice < systemTotalWeight)
{
for(unsigned i = 0; i < systemWeights.size(); ++i)
{
choice -= systemWeights[i];
if(choice < 0)
{
ship.SetTargetSystem(links[i]);
break;
}
}
}
else
{
choice = (choice - systemTotalWeight) / planetWeight;
//.........这里部分代码省略.........
示例11: MovePlayer
//.........这里部分代码省略.........
if(distance < closest)
{
ship.SetTargetPlanet(&object);
closest = distance;
}
}
if(!ship.GetTargetPlanet())
message = "There are no planets in this system that you can land on.";
else if(!ship.GetTargetPlanet()->GetPlanet()->CanLand())
message = "The authorities on this planet refuse to clear you to land here.";
else if(count > 1)
{
message = "You can land on more than one planet in this system. Landing on ";
if(ship.GetTargetPlanet()->Name().empty())
message += "???.";
else
message += ship.GetTargetPlanet()->Name() + ".";
}
}
if(!message.empty())
Messages::Add(message);
}
else if(keyDown.Has(Command::JUMP))
{
if(!ship.GetTargetSystem())
{
double bestMatch = -2.;
const auto &links = (ship.Attributes().Get("jump drive") ?
ship.GetSystem()->Neighbors() : ship.GetSystem()->Links());
for(const System *link : links)
{
Point direction = link->Position() - ship.GetSystem()->Position();
double match = ship.Facing().Unit().Dot(direction.Unit());
if(match > bestMatch)
{
bestMatch = match;
ship.SetTargetSystem(link);
}
}
}
}
else if(keyDown.Has(Command::SCAN))
command |= Command::SCAN;
bool hasGuns = Preferences::Has("Automatic firing") && !ship.IsBoarding()
&& !(keyStuck | keyHeld).Has(Command::LAND | Command::JUMP | Command::BOARD);
if(hasGuns)
command |= AutoFire(ship, ships, false);
hasGuns |= keyHeld.Has(Command::PRIMARY);
if(keyHeld)
{
if(keyHeld.Has(Command::RIGHT | Command::LEFT))
command.SetTurn(keyHeld.Has(Command::RIGHT) - keyHeld.Has(Command::LEFT));
else if(keyHeld.Has(Command::BACK))
{
if(ship.Attributes().Get("reverse thrust"))
command |= Command::BACK;
else
command.SetTurn(TurnBackward(ship));
}
if(keyHeld.Has(Command::FORWARD))
command |= Command::FORWARD;
if(keyHeld.Has(Command::PRIMARY))
{
示例12: AutoFire
// Fire whichever of the given ship's weapons can hit a hostile target.
Command AI::AutoFire(const Ship &ship, const list<shared_ptr<Ship>> &ships, bool secondary) const
{
Command command;
int index = -1;
// Special case: your target is not your enemy. Do not fire, because you do
// not want to risk damaging that target. The only time a ship other than
// the player will target a friendly ship is if the player has asked a ship
// for assistance.
shared_ptr<Ship> currentTarget = ship.GetTargetShip();
const Government *gov = ship.GetGovernment();
bool isSharingTarget = ship.IsYours() && currentTarget == sharedTarget.lock();
bool currentIsEnemy = currentTarget
&& currentTarget->GetGovernment()->IsEnemy(gov)
&& currentTarget->GetSystem() == ship.GetSystem();
if(currentTarget && !(currentIsEnemy || isSharingTarget))
currentTarget.reset();
// Only fire on disabled targets if you don't want to plunder them.
bool spareDisabled = (ship.GetPersonality().Disables() || ship.GetPersonality().Plunders());
// Find the longest range of any of your non-homing weapons.
double maxRange = 0.;
for(const Armament::Weapon &weapon : ship.Weapons())
if(weapon.IsReady() && !weapon.IsHoming() && (secondary || !weapon.GetOutfit()->Icon()))
maxRange = max(maxRange, weapon.GetOutfit()->Range());
// Extend the weapon range slightly to account for velocity differences.
maxRange *= 1.5;
// Find all enemy ships within range of at least one weapon.
vector<shared_ptr<const Ship>> enemies;
if(currentTarget)
enemies.push_back(currentTarget);
for(auto target : ships)
if(target->IsTargetable() && gov->IsEnemy(target->GetGovernment())
&& target->Velocity().Length() < 20.
&& target->GetSystem() == ship.GetSystem()
&& target->Position().Distance(ship.Position()) < maxRange
&& target != currentTarget)
enemies.push_back(target);
for(const Armament::Weapon &weapon : ship.Weapons())
{
++index;
// Skip weapons that are not ready to fire. Also skip homing weapons if
// no target is selected, and secondary weapons if only firing primaries.
if(!weapon.IsReady() || (!currentTarget && weapon.IsHoming()))
continue;
if(!secondary && weapon.GetOutfit()->Icon())
continue;
// Special case: if the weapon uses fuel, be careful not to spend so much
// fuel that you cannot leave the system if necessary.
if(weapon.GetOutfit()->FiringFuel())
{
double fuel = ship.Fuel() * ship.Attributes().Get("fuel capacity");
fuel -= weapon.GetOutfit()->FiringFuel();
// If the ship is not ever leaving this system, it does not need to
// reserve any fuel.
bool isStaying = ship.GetPersonality().IsStaying();
if(!secondary || fuel < (isStaying ? 0. : ship.JumpFuel()))
continue;
}
// Figure out where this weapon will fire from, but add some randomness
// depending on how accurate this ship's pilot is.
Point start = ship.Position() + ship.Facing().Rotate(weapon.GetPoint());
start += ship.GetPersonality().Confusion();
const Outfit *outfit = weapon.GetOutfit();
double vp = outfit->Velocity();
double lifetime = outfit->TotalLifetime();
if(currentTarget && (weapon.IsHoming() || weapon.IsTurret()))
{
bool hasBoarded = Has(ship, currentTarget, ShipEvent::BOARD);
if(currentTarget->IsDisabled() && spareDisabled && !hasBoarded)
continue;
Point p = currentTarget->Position() - start;
Point v = currentTarget->Velocity() - ship.Velocity();
// By the time this action is performed, the ships will have moved
// forward one time step.
p += v;
if(p.Length() < outfit->BlastRadius())
continue;
double steps = Armament::RendevousTime(p, v, vp);
if(steps == steps && steps <= lifetime)
{
command.SetFire(index);
continue;
}
}
// Don't fire homing weapons with no target.
if(weapon.IsHoming())
continue;
for(const shared_ptr<const Ship> &target : enemies)
//.........这里部分代码省略.........