本文整理汇总了C++中Ship::GetTargetShip方法的典型用法代码示例。如果您正苦于以下问题:C++ Ship::GetTargetShip方法的具体用法?C++ Ship::GetTargetShip怎么用?C++ Ship::GetTargetShip使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ship
的用法示例。
在下文中一共展示了Ship::GetTargetShip方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TargetAim
// Get a vector giving the direction this ship should aim in in order to do
// maximum damaged to a target at the given position with its non-turret,
// non-homing weapons. If the ship has no non-homing weapons, this just
// returns the direction to the target.
Point AI::TargetAim(const Ship &ship)
{
Point result;
shared_ptr<const Ship> target = ship.GetTargetShip();
if(!target)
return result;
for(const Armament::Weapon &weapon : ship.Weapons())
{
const Outfit *outfit = weapon.GetOutfit();
if(!outfit || weapon.IsHoming() || weapon.IsTurret())
continue;
Point start = ship.Position() + ship.Facing().Rotate(weapon.GetPoint());
Point p = target->Position() - start + ship.GetPersonality().Confusion();
Point v = target->Velocity() - ship.Velocity();
double steps = Armament::RendevousTime(p, v, outfit->Velocity());
if(!(steps == steps))
continue;
steps = min(steps, outfit->TotalLifetime());
p += steps * v;
double damage = outfit->ShieldDamage() + outfit->HullDamage();
result += p.Unit() * damage;
}
if(!result)
return target->Position() - ship.Position();
return result;
}
示例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: weapon
Projectile::Projectile(const Ship &parent, Point position, Angle angle, const Outfit *weapon)
: weapon(weapon), animation(weapon->WeaponSprite()),
position(position), velocity(parent.Velocity()), angle(angle),
targetShip(parent.GetTargetShip()), government(parent.GetGovernment()),
lifetime(weapon->Lifetime())
{
// If you are boarding your target, do not fire on it.
if(parent.IsBoarding() || parent.Commands().Has(Command::BOARD))
targetShip.reset();
cachedTarget = targetShip.lock().get();
double inaccuracy = weapon->Inaccuracy();
if(inaccuracy)
this->angle += Angle::Random(inaccuracy) - Angle::Random(inaccuracy);
velocity += this->angle.Unit() * weapon->Velocity();
}
示例4: Body
Projectile::Projectile(const Ship &parent, Point position, Angle angle, const Outfit *weapon)
: Body(weapon->WeaponSprite(), position, parent.Velocity(), angle),
weapon(weapon), targetShip(parent.GetTargetShip()), lifetime(weapon->Lifetime())
{
government = parent.GetGovernment();
// If you are boarding your target, do not fire on it.
if(parent.IsBoarding() || parent.Commands().Has(Command::BOARD))
targetShip.reset();
cachedTarget = targetShip.lock().get();
if(cachedTarget)
targetGovernment = cachedTarget->GetGovernment();
double inaccuracy = weapon->Inaccuracy();
if(inaccuracy)
this->angle += Angle::Random(inaccuracy) - Angle::Random(inaccuracy);
velocity += this->angle.Unit() * (weapon->Velocity() + Random::Real() * weapon->RandomVelocity());
// If a random lifetime is specified, add a random amount up to that amount.
if(weapon->RandomLifetime())
lifetime += Random::Int(weapon->RandomLifetime() + 1);
}
示例5: DoSurveillance
void AI::DoSurveillance(Ship &ship, Command &command, const list<shared_ptr<Ship>> &ships) const
{
const shared_ptr<Ship> &target = ship.GetTargetShip();
if(target && (!target->IsTargetable() || target->GetSystem() != ship.GetSystem()))
ship.SetTargetShip(shared_ptr<Ship>());
if(target && ship.GetGovernment()->IsEnemy(target->GetGovernment()))
{
MoveIndependent(ship, command);
command |= AutoFire(ship, ships);
return;
}
bool cargoScan = ship.Attributes().Get("cargo scan");
bool outfitScan = ship.Attributes().Get("outfit scan");
double atmosphereScan = ship.Attributes().Get("atmosphere scan");
bool jumpDrive = ship.Attributes().Get("jump drive");
bool hyperdrive = ship.Attributes().Get("hyperdrive");
// This function is only called for ships that are in the player's system.
if(ship.GetTargetSystem())
{
PrepareForHyperspace(ship, command);
command |= Command::JUMP;
command |= Command::DEPLOY;
}
else if(ship.GetTargetPlanet())
{
MoveToPlanet(ship, command);
double distance = ship.Position().Distance(ship.GetTargetPlanet()->Position());
if(distance < atmosphereScan && !Random::Int(100))
ship.SetTargetPlanet(nullptr);
else
command |= Command::LAND;
}
else if(ship.GetTargetShip() && ship.GetTargetShip()->IsTargetable()
&& ship.GetTargetShip()->GetSystem() == ship.GetSystem())
{
bool mustScanCargo = cargoScan && !Has(ship, target, ShipEvent::SCAN_CARGO);
bool mustScanOutfits = outfitScan && !Has(ship, target, ShipEvent::SCAN_OUTFITS);
bool isInSystem = (ship.GetSystem() == target->GetSystem() && !target->IsEnteringHyperspace());
if(!isInSystem || (!mustScanCargo && !mustScanOutfits))
ship.SetTargetShip(shared_ptr<Ship>());
else
{
CircleAround(ship, command, *target);
command |= Command::SCAN;
}
}
else
{
shared_ptr<Ship> newTarget = FindTarget(ship, ships);
if(newTarget && ship.GetGovernment()->IsEnemy(newTarget->GetGovernment()))
{
ship.SetTargetShip(newTarget);
return;
}
vector<shared_ptr<Ship>> targetShips;
vector<const StellarObject *> targetPlanets;
vector<const System *> targetSystems;
if(cargoScan || outfitScan)
for(const auto &it : ships)
if(it->GetGovernment() != ship.GetGovernment() && it->IsTargetable()
&& it->GetSystem() == ship.GetSystem())
{
if(Has(ship, it, ShipEvent::SCAN_CARGO) && Has(ship, it, ShipEvent::SCAN_OUTFITS))
continue;
targetShips.push_back(it);
}
if(atmosphereScan)
for(const StellarObject &object : ship.GetSystem()->Objects())
if(!object.IsStar() && object.Radius() < 130.)
targetPlanets.push_back(&object);
if(jumpDrive)
for(const System *link : ship.GetSystem()->Neighbors())
targetSystems.push_back(link);
else if(hyperdrive)
for(const System *link : ship.GetSystem()->Links())
targetSystems.push_back(link);
unsigned total = targetShips.size() + targetPlanets.size() + targetSystems.size();
if(!total)
return;
unsigned index = Random::Int(total);
if(index < targetShips.size())
ship.SetTargetShip(targetShips[index]);
else
{
index -= targetShips.size();
if(index < targetPlanets.size())
ship.SetTargetPlanet(targetPlanets[index]);
else
ship.SetTargetSystem(targetSystems[index - targetPlanets.size()]);
}
}
//.........这里部分代码省略.........
示例6: 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;
//.........这里部分代码省略.........
示例7: min
// Pick a new target for the given ship.
shared_ptr<Ship> AI::FindTarget(const Ship &ship, const list<shared_ptr<Ship>> &ships) const
{
// If this ship has no government, it has no enemies.
shared_ptr<Ship> target;
const Government *gov = ship.GetGovernment();
if(!gov)
return target;
bool isPlayerEscort = ship.IsYours();
if(isPlayerEscort)
{
shared_ptr<Ship> locked = sharedTarget.lock();
if(locked && locked->GetSystem() == ship.GetSystem() && !locked->IsDisabled())
return locked;
}
// If this ship is not armed, do not make it fight.
double minRange = numeric_limits<double>::infinity();
double maxRange = 0.;
for(const Armament::Weapon &weapon : ship.Weapons())
if(weapon.GetOutfit() && !weapon.IsAntiMissile())
{
minRange = min(minRange, weapon.GetOutfit()->Range());
maxRange = max(maxRange, weapon.GetOutfit()->Range());
}
if(!maxRange)
return target;
shared_ptr<Ship> oldTarget = ship.GetTargetShip();
if(oldTarget && !oldTarget->IsTargetable())
oldTarget.reset();
shared_ptr<Ship> parentTarget;
if(ship.GetParent())
parentTarget = ship.GetParent()->GetTargetShip();
if(parentTarget && !parentTarget->IsTargetable())
parentTarget.reset();
// Find the closest enemy ship (if there is one). If this ship is "heroic,"
// it will attack any ship in system. Otherwise, if all its weapons have a
// range higher than 2000, it will engage ships up to 50% beyond its range.
// If a ship has short range weapons and is not heroic, it will engage any
// ship that is within 3000 of it.
const Personality &person = ship.GetPersonality();
double closest = person.IsHeroic() ? numeric_limits<double>::infinity() :
(minRange > 1000.) ? maxRange * 1.5 : 4000.;
const System *system = ship.GetSystem();
bool isDisabled = false;
for(const auto &it : ships)
if(it->GetSystem() == system && it->IsTargetable() && gov->IsEnemy(it->GetGovernment()))
{
if(person.IsNemesis() && !it->GetGovernment()->IsPlayer())
continue;
double range = it->Position().Distance(ship.Position());
// Preferentially focus on your previous target or your parent ship's
// target if they are nearby.
if(it == oldTarget || it == parentTarget)
range -= 500.;
// If your personality it to disable ships rather than destroy them,
// never target disabled ships.
if(it->IsDisabled() && !person.Plunders()
&& (person.Disables() || (!person.IsNemesis() && it != oldTarget)))
continue;
if(!person.Plunders())
range += 5000. * it->IsDisabled();
else
{
bool hasBoarded = Has(ship, it, ShipEvent::BOARD);
// Don't plunder unless there are no "live" enemies nearby.
range += 2000. * (2 * it->IsDisabled() - !hasBoarded);
}
// Focus on nearly dead ships.
range += 500. * (it->Shields() + it->Hull());
if(range < closest)
{
closest = range;
target = it;
isDisabled = it->IsDisabled();
}
}
bool cargoScan = ship.Attributes().Get("cargo scan");
bool outfitScan = ship.Attributes().Get("outfit scan");
if(!target && (cargoScan || outfitScan) && !isPlayerEscort)
{
closest = numeric_limits<double>::infinity();
for(const auto &it : ships)
if(it->GetSystem() == system && it->GetGovernment() != gov && it->IsTargetable())
{
if((cargoScan && !Has(ship, it, ShipEvent::SCAN_CARGO))
|| (outfitScan && !Has(ship, it, ShipEvent::SCAN_OUTFITS)))
{
double range = it->Position().Distance(ship.Position());
if(range < closest)
{
closest = range;
target = it;
//.........这里部分代码省略.........
示例8: MovePlayer
void AI::MovePlayer(Ship &ship, const PlayerInfo &player, const list<shared_ptr<Ship>> &ships)
{
Command command;
if(player.HasTravelPlan())
{
const System *system = player.TravelPlan().back();
ship.SetTargetSystem(system);
// Check if there's a particular planet there we want to visit.
for(const Mission &mission : player.Missions())
if(mission.Destination() && mission.Destination()->GetSystem() == system)
{
ship.SetDestination(mission.Destination());
break;
}
}
if(keyDown.Has(Command::NEAREST))
{
double closest = numeric_limits<double>::infinity();
int closeState = 0;
for(const shared_ptr<Ship> &other : ships)
if(other.get() != &ship && other->IsTargetable())
{
// Sort ships into one of three priority states:
// 0 = friendly, 1 = disabled enemy, 2 = active enemy.
int state = other->GetGovernment()->IsEnemy(ship.GetGovernment());
// Do not let "target nearest" select a friendly ship, so that
// if the player is repeatedly targeting nearest to, say, target
// a bunch of fighters, they won't start firing on friendly
// ships as soon as the last one is gone.
if((!state && !shift) || other->GetGovernment()->IsPlayer())
continue;
state += state * !other->IsDisabled();
double d = other->Position().Distance(ship.Position());
if(state > closeState || (state == closeState && d < closest))
{
ship.SetTargetShip(other);
closest = d;
closeState = state;
}
}
}
else if(keyDown.Has(Command::TARGET))
{
shared_ptr<const Ship> target = ship.GetTargetShip();
bool selectNext = !target || !target->IsTargetable();
for(const shared_ptr<Ship> &other : ships)
{
bool isPlayer = other->GetGovernment()->IsPlayer() || other->GetPersonality().IsEscort();
if(other == target)
selectNext = true;
else if(other.get() != &ship && selectNext && other->IsTargetable() && isPlayer == shift)
{
ship.SetTargetShip(other);
selectNext = false;
break;
}
}
if(selectNext)
ship.SetTargetShip(shared_ptr<Ship>());
}
else if(keyDown.Has(Command::BOARD))
{
shared_ptr<const Ship> target = ship.GetTargetShip();
if(!target || !target->IsDisabled() || target->IsDestroyed() || target->GetSystem() != ship.GetSystem())
{
double closest = numeric_limits<double>::infinity();
bool foundEnemy = false;
bool foundAnything = false;
for(const shared_ptr<Ship> &other : ships)
if(other->IsTargetable() && other->IsDisabled() && !other->IsDestroyed())
{
bool isEnemy = other->GetGovernment()->IsEnemy(ship.GetGovernment());
double d = other->Position().Distance(ship.Position());
if((isEnemy && !foundEnemy) || d < closest)
{
closest = d;
foundEnemy = isEnemy;
foundAnything = true;
ship.SetTargetShip(other);
}
}
if(!foundAnything)
keyDown.Clear(Command::BOARD);
}
}
else if(keyDown.Has(Command::LAND))
{
// If the player is right over an uninhabited planet, display a message
// explaining why they cannot land there.
string message;
for(const StellarObject &object : ship.GetSystem()->Objects())
if(!object.GetPlanet() && !object.GetSprite().IsEmpty())
{
double distance = ship.Position().Distance(object.Position());
if(distance < object.Radius())
//.........这里部分代码省略.........
示例9: 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)
//.........这里部分代码省略.........