void AI::MoveEscort(Ship &ship, Command &command) { const Ship &parent = *ship.GetParent(); bool isStaying = ship.GetPersonality().IsStaying(); // If an escort is out of fuel, they should refuel without waiting for the // "parent" to land (because the parent may not be planning on landing). if(ship.Attributes().Get("fuel capacity") && !ship.JumpsRemaining() && ship.GetSystem()->IsInhabited()) Refuel(ship, command); else if(ship.GetSystem() != parent.GetSystem() && !isStaying) { DistanceMap distance(ship, parent.GetSystem()); const System *system = distance.Route(ship.GetSystem()); ship.SetTargetSystem(system); if(!system || (ship.GetSystem()->IsInhabited() && !system->IsInhabited() && ship.JumpsRemaining() == 1)) Refuel(ship, command); else { PrepareForHyperspace(ship, command); command |= Command::JUMP; } } else if(parent.Commands().Has(Command::LAND) && parent.GetTargetPlanet()) { ship.SetTargetPlanet(parent.GetTargetPlanet()); MoveToPlanet(ship, command); if(parent.IsLanding() || parent.CanLand()) command |= Command::LAND; } else if(parent.Commands().Has(Command::BOARD) && parent.GetTargetShip().get() == &ship) Stop(ship, command); else if(parent.Commands().Has(Command::JUMP) && parent.GetTargetSystem() && !isStaying) { DistanceMap distance(ship, parent.GetTargetSystem()); const System *dest = distance.Route(ship.GetSystem()); ship.SetTargetSystem(dest); if(!dest || (dest != parent.GetTargetSystem() && !dest->IsInhabited() && ship.JumpsRemaining() == 1)) Refuel(ship, command); else { PrepareForHyperspace(ship, command); if(parent.IsEnteringHyperspace() || parent.CheckHyperspace()) command |= Command::JUMP; } } else CircleAround(ship, command, parent); }
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; ship.SetTargetPlanet(planets[choice]); } } if(ship.GetTargetSystem()) { PrepareForHyperspace(ship, command); bool mustWait = false; for(const weak_ptr<const Ship> &escort : ship.GetEscorts()) { shared_ptr<const Ship> locked = escort.lock(); mustWait = locked && locked->CanBeCarried(); } if(!mustWait) command |= Command::JUMP; } else if(ship.GetTargetPlanet()) { MoveToPlanet(ship, command); if(!ship.GetPersonality().IsStaying()) command |= Command::LAND; else if(ship.Position().Distance(ship.GetTargetPlanet()->Position()) < 100.) ship.SetTargetPlanet(nullptr); } else if(ship.GetPersonality().IsStaying() && ship.GetSystem()->Objects().size()) { unsigned i = Random::Int(ship.GetSystem()->Objects().size()); ship.SetTargetPlanet(&ship.GetSystem()->Objects()[i]); } }
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()) message = object.LandingMessage(); } const StellarObject *target = ship.GetTargetPlanet(); if(target && ship.Position().Distance(target->Position()) < target->Radius()) { // Special case: if there are two planets in system and you have one // selected, then press "land" again, do not toggle to the other if // you are within landing range of the one you have selected. } else if(message.empty() && target) { bool found = false; const StellarObject *next = nullptr; for(const StellarObject &object : ship.GetSystem()->Objects()) if(object.GetPlanet()) { if(found) { next = &object; break; } else if(&object == ship.GetTargetPlanet()) found = true; } if(!next) { for(const StellarObject &object : ship.GetSystem()->Objects()) if(object.GetPlanet()) { next = &object; break; } } ship.SetTargetPlanet(next); if(next->GetPlanet() && !next->GetPlanet()->CanLand()) message = "The authorities on this planet refuse to clear you to land here."; } else if(message.empty()) { double closest = numeric_limits<double>::infinity(); int count = 0; for(const StellarObject &object : ship.GetSystem()->Objects()) if(object.GetPlanet()) { ++count; double distance = ship.Position().Distance(object.Position()); const Planet *planet = object.GetPlanet(); if(planet == ship.GetDestination()) distance = 0.; else if(!planet->HasSpaceport() && !planet->IsWormhole()) distance += 10000.; 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)) { int index = 0; for(const Armament::Weapon &weapon : ship.Weapons()) { const Outfit *outfit = weapon.GetOutfit(); if(outfit && !outfit->Icon()) { command.SetFire(index); hasGuns |= !weapon.IsTurret(); } ++index; } } if(keyHeld.Has(Command::SECONDARY)) { int index = 0; for(const Armament::Weapon &weapon : ship.Weapons()) { const Outfit *outfit = weapon.GetOutfit(); if(outfit && outfit == player.SelectedWeapon()) command.SetFire(index); ++index; } } if(keyHeld.Has(Command::AFTERBURNER)) command |= Command::AFTERBURNER; if(keyHeld.Has(AutopilotCancelKeys())) keyStuck = keyHeld; } if(hasGuns && Preferences::Has("Automatic aiming") && !command.Turn() && ship.GetTargetShip() && ship.GetTargetShip()->GetSystem() == ship.GetSystem() && !keyStuck.Has(Command::LAND | Command::JUMP | Command::BOARD)) { Point distance = ship.GetTargetShip()->Position() - ship.Position(); if(distance.Unit().Dot(ship.Facing().Unit()) >= .8) command.SetTurn(TurnToward(ship, TargetAim(ship))); } if(ship.IsBoarding()) keyStuck.Clear(); else if(keyStuck.Has(Command::LAND) && ship.GetTargetPlanet()) { if(ship.GetPlanet()) keyStuck.Clear(); else { MoveToPlanet(ship, command); command |= Command::LAND; } } else if(keyStuck.Has(Command::JUMP) && ship.GetTargetSystem()) { if(!ship.Attributes().Get("hyperdrive") && !ship.Attributes().Get("jump drive")) { Messages::Add("You do not have a hyperdrive installed."); keyStuck.Clear(); } else if(!ship.HyperspaceType()) { Messages::Add("You cannot jump to the selected system."); keyStuck.Clear(); } else if(!ship.JumpsRemaining() && !ship.IsEnteringHyperspace()) { Messages::Add("You do not have enough fuel to make a hyperspace jump."); keyStuck.Clear(); } else if(!ship.GetTargetSystem()) keyStuck.Clear(); else { PrepareForHyperspace(ship, command); command |= Command::JUMP; if(keyHeld.Has(Command::JUMP)) command |= Command::WAIT; } } else if(keyStuck.Has(Command::BOARD) && ship.GetTargetShip()) { shared_ptr<const Ship> target = ship.GetTargetShip(); if(!target || !target->IsTargetable() || !target->IsDisabled() || target->IsDestroyed()) keyStuck.Clear(Command::BOARD); else { MoveTo(ship, command, target->Position(), 40., .8); command |= Command::BOARD; } } if(isLaunching) command |= Command::DEPLOY; if(isCloaking) command |= Command::CLOAK; ship.SetCommands(command); }