void AI::DoCloak(Ship &ship, Command &command, const list<shared_ptr<Ship>> &ships) { if(ship.Attributes().Get("cloak")) { // Never cloak if it will cause you to be stranded. if(ship.Attributes().Get("cloaking fuel") && !ship.Attributes().Get("ramscoop")) { double fuel = ship.Fuel() * ship.Attributes().Get("fuel capacity"); fuel -= ship.Attributes().Get("cloaking fuel"); if(fuel < ship.JumpFuel()) return; } // Otherwise, always cloak if you are in imminent danger. static const double MAX_RANGE = 10000.; double nearestEnemy = MAX_RANGE; for(const auto &other : ships) if(other->GetSystem() == ship.GetSystem() && other->IsTargetable() && other->GetGovernment()->IsEnemy(ship.GetGovernment())) nearestEnemy = min(nearestEnemy, ship.Position().Distance(other->Position())); if(ship.Hull() + ship.Shields() < 1. && nearestEnemy < 2000.) command |= Command::CLOAK; // Also cloak if there are no enemies nearby and cloaking does // not cost you fuel. if(nearestEnemy == MAX_RANGE && !ship.Attributes().Get("cloaking fuel")) command |= Command::CLOAK; } }
EscortDisplay::Icon::Icon(const Ship &ship, bool isHere) : sprite(ship.GetSprite().GetSprite()), isHere(isHere && !ship.IsDisabled()), stackSize(1), cost(ship.Cost()), system((!isHere && ship.GetSystem()) ? ship.GetSystem()->Name() : ""), low{ship.Shields(), ship.Hull(), ship.Energy(), ship.Heat(), ship.Fuel()}, high(low) { }
void ShipInfoDisplay::UpdateAttributes(const Ship &ship) { bool isGeneric = ship.Name().empty() || ship.GetPlanet(); attributeLabels.clear(); attributeValues.clear(); attributesHeight = 20; const Outfit &attributes = ship.Attributes(); attributeLabels.push_back("cost:"); attributeValues.push_back(Format::Number(ship.Cost())); attributesHeight += 20; attributeLabels.push_back(string()); attributeValues.push_back(string()); attributesHeight += 10; if(attributes.Get("shield generation")) { attributeLabels.push_back("shields charge / max:"); attributeValues.push_back(Format::Number(60. * attributes.Get("shield generation")) + " / " + Format::Number(attributes.Get("shields"))); } else { attributeLabels.push_back("shields:"); attributeValues.push_back(Format::Number(attributes.Get("shields"))); } attributesHeight += 20; if(attributes.Get("hull repair rate")) { attributeLabels.push_back("hull repair / max:"); attributeValues.push_back(Format::Number(60. * attributes.Get("hull repair rate")) + " / " + Format::Number(attributes.Get("hull"))); } else { attributeLabels.push_back("hull:"); attributeValues.push_back(Format::Number(attributes.Get("hull"))); } attributesHeight += 20; double emptyMass = ship.Mass(); attributeLabels.push_back(isGeneric ? "mass with no cargo:" : "mass:"); attributeValues.push_back(Format::Number(emptyMass)); attributesHeight += 20; attributeLabels.push_back(isGeneric ? "cargo space:" : "cargo:"); if(isGeneric) attributeValues.push_back(Format::Number(attributes.Get("cargo space"))); else attributeValues.push_back(Format::Number(ship.Cargo().Used()) + " / " + Format::Number(attributes.Get("cargo space"))); attributesHeight += 20; attributeLabels.push_back("required crew / bunks:"); attributeValues.push_back(Format::Number(ship.RequiredCrew()) + " / " + Format::Number(attributes.Get("bunks"))); attributesHeight += 20; attributeLabels.push_back(isGeneric ? "fuel capacity:" : "fuel:"); double fuelCapacity = attributes.Get("fuel capacity"); if(isGeneric) attributeValues.push_back(Format::Number(fuelCapacity)); else attributeValues.push_back(Format::Number(ship.Fuel() * fuelCapacity) + " / " + Format::Number(fuelCapacity)); attributesHeight += 20; double fullMass = emptyMass + (isGeneric ? attributes.Get("cargo space") : ship.Cargo().Used()); isGeneric &= (fullMass != emptyMass); attributeLabels.push_back(string()); attributeValues.push_back(string()); attributesHeight += 10; attributeLabels.push_back(isGeneric ? "movement, full / no cargo:" : "movement:"); attributeValues.push_back(string()); attributesHeight += 20; attributeLabels.push_back("max speed:"); attributeValues.push_back(Format::Number(60. * attributes.Get("thrust") / attributes.Get("drag"))); attributesHeight += 20; attributeLabels.push_back("acceleration:"); if(!isGeneric) attributeValues.push_back(Format::Number(3600. * attributes.Get("thrust") / fullMass)); else attributeValues.push_back(Format::Number(3600. * attributes.Get("thrust") / fullMass) + " / " + Format::Number(3600. * attributes.Get("thrust") / emptyMass)); attributesHeight += 20; attributeLabels.push_back("turning:"); if(!isGeneric) attributeValues.push_back(Format::Number(60. * attributes.Get("turn") / fullMass)); else attributeValues.push_back(Format::Number(60. * attributes.Get("turn") / fullMass) + " / " + Format::Number(60. * attributes.Get("turn") / emptyMass)); attributesHeight += 20; // Find out how much outfit, engine, and weapon space the chassis has. map<string, double> chassis; static const string names[] = { "outfit space free:", "outfit space", " weapon capacity:", "weapon capacity", " engine capacity:", "engine capacity", "gun ports free:", "gun ports", "turret mounts free:", "turret mounts" }; static const int NAMES = sizeof(names) / sizeof(names[0]); for(int i = 1; i < NAMES; i += 2) chassis[names[i]] = attributes.Get(names[i]); for(const auto &it : ship.Outfits()) for(auto &cit : chassis) cit.second -= it.second * it.first->Get(cit.first); attributeLabels.push_back(string()); attributeValues.push_back(string()); attributesHeight += 10; for(int i = 0; i < NAMES; i += 2) { attributeLabels.push_back(names[i]); attributeValues.push_back(Format::Number(attributes.Get(names[i + 1])) + " / " + Format::Number(chassis[names[i + 1]])); attributesHeight += 20; } if(ship.BaysFree(false)) { attributeLabels.push_back("drone bays:"); attributeValues.push_back(to_string(ship.BaysFree(false))); attributesHeight += 20; } if(ship.BaysFree(true)) { attributeLabels.push_back("fighter bays:"); attributeValues.push_back(to_string(ship.BaysFree(true))); attributesHeight += 20; } tableLabels.clear(); energyTable.clear(); heatTable.clear(); // Skip a spacer and the table header. attributesHeight += 30; tableLabels.push_back("idle:"); energyTable.push_back(Format::Number( 60. * (attributes.Get("energy generation") + attributes.Get("solar collection")))); heatTable.push_back(Format::Number( 60. * (attributes.Get("heat generation") - attributes.Get("cooling")))); attributesHeight += 20; tableLabels.push_back("moving:"); energyTable.push_back(Format::Number( -60. * (attributes.Get("thrusting energy") + attributes.Get("reverse thrusting energy") + attributes.Get("turning energy")))); heatTable.push_back(Format::Number( 60. * (attributes.Get("thrusting heat") + attributes.Get("reverse thrusting heat") + attributes.Get("turning heat")))); attributesHeight += 20; double firingEnergy = 0.; double firingHeat = 0.; for(const auto &it : ship.Outfits()) if(it.first->IsWeapon() && it.first->Reload()) { firingEnergy += it.second * it.first->FiringEnergy() / it.first->Reload(); firingHeat += it.second * it.first->FiringHeat() / it.first->Reload(); } tableLabels.push_back("firing:"); energyTable.push_back(Format::Number(-60. * firingEnergy)); heatTable.push_back(Format::Number(60. * firingHeat)); attributesHeight += 20; double shieldEnergy = attributes.Get("shield energy"); double hullEnergy = attributes.Get("hull energy"); tableLabels.push_back((shieldEnergy && hullEnergy) ? "shields / hull:" : hullEnergy ? "repairing hull:" : "charging shields:"); energyTable.push_back(Format::Number(-60. * (shieldEnergy + hullEnergy))); double shieldHeat = attributes.Get("shield heat"); double hullHeat = attributes.Get("hull heat"); heatTable.push_back(Format::Number(60. * (shieldHeat + hullHeat))); attributesHeight += 20; tableLabels.push_back("max:"); energyTable.push_back(Format::Number(attributes.Get("energy capacity"))); heatTable.push_back(Format::Number(60. * emptyMass * .1 * attributes.Get("heat dissipation"))); // Pad by 10 pixels on the top and bottom. attributesHeight += 30; }
void MapPanel::DrawTravelPlan() const { Color defaultColor(.5, .4, 0., 0.); Color outOfFlagshipFuelRangeColor(.55, .1, .0, 0.); Color withinFleetFuelRangeColor(.2, .5, .0, 0.); Color wormholeColor(0.5, 0.2, 0.9, 1.); Ship *ship = player.Flagship(); bool hasHyper = ship ? ship->Attributes().Get("hyperdrive") : false; bool hasJump = ship ? ship->Attributes().Get("jump drive") : false; // Find out how much fuel your ship and your escorts use per jump. double flagshipCapacity = 0.; if(ship) flagshipCapacity = ship->Attributes().Get("fuel capacity") * ship->Fuel(); double flagshipJumpFuel = 0.; if(ship) flagshipJumpFuel = hasHyper ? ship->Attributes().Get("scram drive") ? 150. : 100. : 200.; double escortCapacity = 0.; double escortJumpFuel = 1.; bool escortHasJump = false; // Skip your flagship, parked ships, and fighters. for(const shared_ptr<Ship> &it : player.Ships()) if(it.get() != ship && !it->IsParked() && !it->CanBeCarried()) { double capacity = it->Attributes().Get("fuel capacity") * it->Fuel(); double jumpFuel = it->Attributes().Get("hyperdrive") ? it->Attributes().Get("scram drive") ? 150. : 100. : 200.; if(escortJumpFuel < 100. || capacity / jumpFuel < escortCapacity / escortJumpFuel) { escortCapacity = capacity; escortJumpFuel = jumpFuel; escortHasJump = it->Attributes().Get("jump drive"); } } // Draw your current travel plan. if(!playerSystem) return; const System *previous = playerSystem; for(int i = player.TravelPlan().size() - 1; i >= 0; --i) { const System *next = player.TravelPlan()[i]; // Figure out what kind of jump this is, and check if the player is able // to make jumps of that kind. bool isHyper = (find(previous->Links().begin(), previous->Links().end(), next) != previous->Links().end()); bool isJump = isHyper || (find(previous->Neighbors().begin(), previous->Neighbors().end(), next) != previous->Neighbors().end()); bool isWormhole = false; if(!((isHyper && hasHyper) || (isJump && hasJump))) { for(const StellarObject &object : previous->Objects()) isWormhole |= (object.GetPlanet() && object.GetPlanet()->WormholeDestination(previous) == next); if(!isWormhole) break; } Point from = Zoom() * (next->Position() + center); Point to = Zoom() * (previous->Position() + center); Point unit = (from - to).Unit() * 7.; from -= unit; to += unit; if(isWormhole) { // Wormholes cost no fuel to travel through. } else if(!isHyper) { if(!escortHasJump) escortCapacity = 0.; flagshipCapacity -= 200.; escortCapacity -= 200.; } else { flagshipCapacity -= flagshipJumpFuel; escortCapacity -= escortJumpFuel; } Color drawColor = outOfFlagshipFuelRangeColor; if(isWormhole) drawColor = wormholeColor; else if(flagshipCapacity >= 0. && escortCapacity >= 0.) drawColor = withinFleetFuelRangeColor; else if(flagshipCapacity >= 0. || escortCapacity >= 0.) drawColor = defaultColor; LineShader::Draw(from, to, 3., drawColor); previous = next; } }
// 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) { if(!target->IsTargetable() || target->Velocity().Length() > 20. || target->GetSystem() != ship.GetSystem()) continue; // Don't shoot ships we want to plunder. bool hasBoarded = Has(ship, target, ShipEvent::BOARD); if(target->IsDisabled() && spareDisabled && !hasBoarded) continue; Point p = target->Position() - start; Point v = target->Velocity() - ship.Velocity(); // By the time this action is performed, the ships will have moved // forward one time step. p += v; // Get the vector the weapon will travel along. v = (ship.Facing() + weapon.GetAngle()).Unit() * vp - v; // Extrapolate over the lifetime of the projectile. v *= lifetime; const Mask &mask = target->GetSprite().GetMask(step); if(mask.Collide(-p, v, target->Facing()) < 1.) { command.SetFire(index); break; } } } return command; }