void FighterTacticalAI::FindThreat() { // pick the closest contact on Threat Warning System: Ship* threat_ship = 0; Shot* threat_missile = 0; double threat_dist = 1e9; ListIter<Contact> c_iter = ship->ContactList(); while (++c_iter) { Contact* contact = c_iter.value(); if (contact->Threat(ship) && (Game::GameTime() - contact->AcquisitionTime()) > THREAT_REACTION_TIME) { double rng = contact->Range(ship); if (contact->GetShot()) { threat_missile = contact->GetShot(); } else if (rng < threat_dist && contact->GetShip()) { Ship* candidate = contact->GetShip(); if (candidate->InTransition()) continue; if (candidate->IsStarship() && rng < 50e3) { threat_ship = candidate; threat_dist = rng; } else if (candidate->IsDropship() && rng < 25e3) { threat_ship = candidate; threat_dist = rng; } // static and ground units: else if (rng < 30e3) { threat_ship = candidate; threat_dist = rng; } } } } ship_ai->SetThreat(threat_ship); ship_ai->SetThreatMissile(threat_missile); }
void GroundAI::SelectTarget() { SimObject* potential_target = 0; // pick the closest combatant ship with a different IFF code: double target_dist = 1.0e15; Ship* current_ship_target = 0; ListIter<Contact> c_iter = ship->ContactList(); while (++c_iter) { Contact* contact = c_iter.value(); int c_iff = contact->GetIFF(ship); Ship* c_ship = contact->GetShip(); Shot* c_shot = contact->GetShot(); bool rogue = false; if (c_ship) rogue = c_ship->IsRogue(); if (rogue || c_iff > 0 && c_iff != ship->GetIFF() && c_iff < 1000) { if (c_ship && !c_ship->InTransition()) { // found an enemy, check distance: double dist = (ship->Location() - c_ship->Location()).length(); if (!current_ship_target || (c_ship->Class() <= current_ship_target->Class() && dist < target_dist)) { current_ship_target = c_ship; target_dist = dist; } } } potential_target = current_ship_target; } SetTarget(potential_target); }
void TacticalAI::FindThreat() { // pick the closest contact on Threat Warning System: Ship* threat = 0; Shot* threat_missile = 0; Ship* rumor = 0; double threat_dist = 1e9; const DWORD THREAT_REACTION_TIME = 1000; // 1 second ListIter<Contact> iter = ship->ContactList(); while (++iter) { Contact* contact = iter.value(); if (contact->Threat(ship) && (Game::GameTime() - contact->AcquisitionTime()) > THREAT_REACTION_TIME) { if (contact->GetShot()) { threat_missile = contact->GetShot(); rumor = (Ship*) threat_missile->Owner(); } else { double rng = contact->Range(ship); Ship* c_ship = contact->GetShip(); if (c_ship && !c_ship->InTransition() && c_ship->Class() != Ship::FREIGHTER && c_ship->Class() != Ship::FARCASTER) { if (c_ship->GetTarget() == ship) { if (!threat || c_ship->Class() > threat->Class()) { threat = c_ship; threat_dist = 0; } } else if (rng < threat_dist) { threat = c_ship; threat_dist = rng; } } } } } if (rumor && !rumor->InTransition()) { iter.reset(); while (++iter) { if (iter->GetShip() == rumor) { rumor = 0; ship_ai->ClearRumor(); break; } } } else { rumor = 0; ship_ai->ClearRumor(); } ship_ai->SetRumor(rumor); ship_ai->SetThreat(threat); ship_ai->SetThreatMissile(threat_missile); }
void FighterTacticalAI::SelectTargetOpportunity() { // NON-COMBATANTS do not pick targets of opportunity: if (ship->GetIFF() == 0) return; Ship* potential_target = 0; Shot* current_shot_target = 0; // pick the closest combatant ship with a different IFF code: double target_dist = 1.0e15; double min_dist = 5.0e3; // FIGHTERS are primarily anti-air platforms, but may // also attack smaller starships: Ship* ward = 0; if (element_index > 1) ward = ship->GetLeader(); // commit range for patrol/sweep is 80 Km // (about 2 minutes for a fighter at max speed) if (roe == FLEXIBLE || roe == AGRESSIVE) target_dist = ship->Design()->commit_range; if (roe < FLEXIBLE) target_dist = 0.5 * ship->Design()->commit_range; int class_limit = Ship::LCA; if (ship->Class() == Ship::ATTACK) class_limit = Ship::DESTROYER; ListIter<Contact> c_iter = ship->ContactList(); while (++c_iter) { Contact* contact = c_iter.value(); Ship* c_ship = contact->GetShip(); Shot* c_shot = contact->GetShot(); int c_iff = contact->GetIFF(ship); bool rogue = false; if (c_ship) rogue = c_ship->IsRogue(); if (!rogue && (c_iff <= 0 || c_iff == ship->GetIFF() || c_iff == 1000)) continue; // reasonable target? if (c_ship && c_ship->Class() <= class_limit && !c_ship->InTransition()) { if (!rogue) { SimObject* ttgt = c_ship->GetTarget(); // if we are self-defensive, is this contact engaging us? if (roe == SELF_DEFENSIVE && ttgt != ship) continue; // if we are defending, is this contact engaging us or our ward? if (roe == DEFENSIVE && ttgt != ship && ttgt != ward) continue; } // found an enemy, check distance: double dist = (ship->Location() - c_ship->Location()).length(); if (dist < 0.75 * target_dist) { // if on patrol, check target distance from navpoint: if (roe == FLEXIBLE && navpt) { double ndist = (navpt->Location().OtherHand() - c_ship->Location()).length(); if (ndist > 80e3) continue; } potential_target = c_ship; target_dist = dist; } } else if (c_shot && c_shot->IsDrone()) { // found an enemy shot, do we have enough time to engage? if (c_shot->GetEta() < 10) continue; // found an enemy shot, check distance: double dist = (ship->Location() - c_shot->Location()).length(); if (!current_shot_target) { current_shot_target = c_shot; target_dist = dist; } // is this shot a better target than the one we've found? else { Ship* ward = ship_ai->GetWard(); if ((c_shot->IsTracking(ward) && !current_shot_target->IsTracking(ward)) || (dist < target_dist)) { current_shot_target = c_shot; target_dist = dist; } } } } if (current_shot_target) { ship_ai->SetTarget(current_shot_target); } else { ship_ai->SetTarget(potential_target); SelectSecondaryForTarget(potential_target); } }