Ejemplo n.º 1
0
void
TacticalAI::FindSupport()
{
    if (!ship_ai->GetThreat()) {
        ship_ai->SetSupport(0);
        return;
    }

    // pick the biggest friendly contact in the sector:
    Ship*       support        = 0;
    double      support_dist   = 1e9;

    ListIter<Contact> contact = ship->ContactList();

    while (++contact) {
        if (contact->GetShip() && contact->GetIFF(ship) == ship->GetIFF()) {
            Ship* c_ship = contact->GetShip();

            if (c_ship != ship && c_ship->Class() >= ship->Class() && !c_ship->InTransition()) {
                if (!support || c_ship->Class() > support->Class())
                support = c_ship;
            }
        }
    }

    ship_ai->SetSupport(support);
}
void
CampaignPlanStrategic::ScoreCombatant(Combatant* c)
{
    // prep lists:
    c->GetDefendList().clear();
    c->GetTargetList().clear();

    ScoreDefensible(c);

    ListIter<Combatant> iter = campaign->GetCombatants();
    while (++iter) {
        if (iter->GetIFF() > 0 && iter->GetIFF() != c->GetIFF())
        ScoreTargets(c, iter.value());
    }

    // sort lists:
    c->GetDefendList().sort();
    c->GetTargetList().sort();
}
void
MsnPkgDlg::DrawPackages()
{
	if (mission) {
		if (pkg_list) {
			pkg_list->ClearItems();

			int i = 0;
			int elem_index = 0;
			ListIter<MissionElement> elem = mission->GetElements();
			while (++elem) {
				// display this element?
				if (elem->GetIFF() == mission->Team()        && 
						!elem->IsSquadron()                      &&
						elem->Region() == mission->GetRegion()   &&
						elem->GetDesign()->type < Ship::STATION) {

					char txt[256];

					if (elem->Player() > 0) {
						sprintf_s(txt, "==>");
						if (pkg_index < 0)
						pkg_index = elem_index;
					}
					else {
						strcpy_s(txt, " ");
					}

					pkg_list->AddItemWithData(txt, elem->ElementID());
					pkg_list->SetItemText(i, 1, elem->Name());
					pkg_list->SetItemText(i, 2, elem->RoleName());

					const ShipDesign* design = elem->GetDesign();

					if (elem->Count() > 1)
					sprintf_s(txt, "%d %s", elem->Count(), design->abrv);
					else
					sprintf_s(txt, "%s %s", design->abrv, design->name);
					pkg_list->SetItemText(i, 3, txt);

					i++;
				}

				elem_index++;
			}
		}
	}
}
Ejemplo n.º 4
0
void
TacticalAI::SelectTargetOpportunity()
{
    // NON-COMBATANTS do not pick targets of opportunity:
    if (ship->GetIFF() == 0)
    return;

    SimObject* potential_target = 0;

    // pick the closest combatant ship with a different IFF code:
    double target_dist = ship->Design()->commit_range;

    SimObject* ward   = ship_ai->GetWard();

    // FRIGATES are primarily anti-air platforms, but may
    // also attack smaller starships:

    if (ship->Class() == Ship::CORVETTE || ship->Class() == Ship::FRIGATE) {
        Ship* current_ship_target = 0;
        Shot* current_shot_target = 0;

        // if we are escorting a larger warship, it is good to attack
        // the same target as our ward:

        if (ward) {
            Ship* s = (Ship*) ward;

            if (s->Class() > ship->Class()) {
                SimObject* obj = s->GetTarget();

                if (obj && obj->Type() == SimObject::SIM_SHIP) {
                    current_ship_target = (Ship*) obj;
                    target_dist = (ship->Location() - obj->Location()).length();
                }
            }
        }

        ListIter<Contact> contact = ship->ContactList();
        while (++contact) {
            Ship* c_ship = contact->GetShip();
            Shot* c_shot = contact->GetShot();

            if (!c_ship && !c_shot)
            continue;

            int   c_iff  = contact->GetIFF(ship);
            bool  rogue  = c_ship && c_ship->IsRogue();
            bool  tgt_ok = c_iff > 0               &&
            c_iff != ship->GetIFF() &&
            c_iff < 1000;

            if (rogue || tgt_ok) {
                if (c_ship && c_ship != ship && !c_ship->InTransition()) {
                    if (c_ship->Class() <  Ship::DESTROYER || 
                            (c_ship->Class() >= Ship::MINE && c_ship->Class() <= Ship::DEFSAT)) {
                        // found an enemy, check distance:
                        double dist = (ship->Location() - c_ship->Location()).length();

                        if (dist < 0.75 * target_dist &&
                                (!current_ship_target || c_ship->Class() <= current_ship_target->Class())) {
                            current_ship_target = c_ship;
                            target_dist = dist;
                        }
                    }
                }

                else if (c_shot) {
                    // found an enemy shot, is there enough time to engage?
                    if (c_shot->GetEta() < 3)
                    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) || c_shot->IsTracking(ship)) &&
                                (!current_shot_target->IsTracking(ward) || 
                                    !current_shot_target->IsTracking(ship))) {
                            current_shot_target = c_shot;
                            target_dist = dist;
                        }
                        else if (dist < target_dist) {
                            current_shot_target = c_shot;
                            target_dist = dist;
                        }
                    }
                }
            }
        }

        if (current_shot_target)
        potential_target = current_shot_target;
        else
        potential_target = current_ship_target;
    }

    // ALL OTHER SHIP CLASSES ignore fighters and only engage
    // other starships:

    else {
        List<Ship> ward_threats;

        ListIter<Contact> contact = ship->ContactList();
        while (++contact) {
            Ship* c_ship = contact->GetShip();

            if (!c_ship)
            continue;

            int   c_iff  = contact->GetIFF(ship);
            bool  rogue  = c_ship->IsRogue();
            bool  tgt_ok = c_ship != ship          && 
            c_iff > 0               && 
            c_iff != ship->GetIFF() && 
            !c_ship->InTransition();

            if (rogue || tgt_ok) {
                if (c_ship->IsStarship() || c_ship->IsStatic()) {
                    // found an enemy, check distance:
                    double dist = (ship->Location() - c_ship->Location()).length();

                    if (dist < 0.75 * target_dist) {
                        potential_target = c_ship;
                        target_dist = dist;
                    }

                    if (ward && c_ship->IsTracking(ward)) {
                        ward_threats.append(c_ship);
                    }
                }
            }
        }

        // if this ship is protecting a ward,
        // prefer targets that are threatening that ward:
        if (potential_target && ward_threats.size() && !ward_threats.contains((Ship*)potential_target)) {
            target_dist *= 2;
            
            ListIter<Ship> iter = ward_threats;
            while (++iter) {
                Ship* threat = iter.value();

                double dist = (ward->Location() - threat->Location()).length();

                if (dist < target_dist) {
                    potential_target = threat;
                    target_dist = dist;
                }
            }
        }
    }

    if (ship->Class() != Ship::CARRIER && ship->Class() != Ship::SWACS)
    ship_ai->SetTarget(potential_target);
}
Ejemplo n.º 5
0
Menu*
RadioView::GetRadioMenu(Ship* s)
{
	dst_elem = 0;

	if (s && sim) {
		if (s->IsStarship()) {
			starship_menu->ClearItems();

			int n           = 0;
			int page_offset = starship_page*PAGE_SIZE;

			ListIter<Element> elem = sim->GetElements();

			if (num_pages == 0) {
				while (++elem) {
					if (elem->IsFinished() || elem->IsSquadron() || elem->IsStatic())
					continue;

					if (ship->GetIFF() == elem->GetIFF() && ship->GetElement() != elem.value())
					n++;
				}

				num_pages = (n/PAGE_SIZE) + (n%PAGE_SIZE > 0);
				n = 0;
				elem.reset();
			}

			while (++elem) {
				if (elem->IsFinished() || elem->IsSquadron() || elem->IsStatic())
				continue;

				if (ship->GetIFF() == elem->GetIFF() && ship->GetElement() != elem.value()) {
					if (n >= page_offset && n < page_offset+PAGE_SIZE) {
						char text[64];
						sprintf_s(text, "%d. %s", n+1 - page_offset, (const char*) elem->Name());

						if (elem->IsActive()) {
							starship_menu->AddMenu(text, elem_menu, (DWORD) elem.value());
						}
						else {
							strcat_s(text, " ");
							strcat_s(text, Game::GetText("RadioView.item.not-avail").data());
							starship_menu->AddItem(text, 0, false);
						}
					}
					n++;
				}
			}

			if (num_pages > 1) {
				char text[64];
				sprintf_s(text, Game::GetText("RadioView.item.next-page").data(), starship_page + 1, num_pages);
				starship_menu->AddItem(text); 
			}

			return starship_menu;
		}
		else if (s->IsDropship()) {
			return fighter_menu;
		}
	}

	return 0;
}