void
MsnEditNavDlg::OnSystemSelect(AWEvent* event)
{
	StarSystem* sys = 0;

	if (cmb_system) {
		const char* name = cmb_system->GetSelectedItem();

		Galaxy* galaxy = Galaxy::GetInstance();
		ListIter<StarSystem> iter = galaxy->GetSystemList();
		while (++iter) {
			StarSystem* s = iter.value();

			if (!strcmp(s->Name(), name)) {
				sys = s;
				break;
			}
		}
	}

	if (sys && cmb_region) {
		cmb_region->ClearItems();

		List<OrbitalRegion> regions;
		regions.append(sys->AllRegions());
		regions.sort();

		ListIter<OrbitalRegion> iter = regions;
		while (++iter) {
			OrbitalRegion* region = iter.value();
			cmb_region->AddItem(region->Name());
		}
	}

	ScrapeForm();
}
void
MsnElemDlg::Show()
{
	FormWindow::Show();

	if (!elem) return;

	int current_class = 0;

	if (cmb_class) {
		cmb_class->ClearItems();

		cmb_class->AddItem(Ship::ClassName(Ship::DRONE));
		cmb_class->AddItem(Ship::ClassName(Ship::FIGHTER));
		cmb_class->AddItem(Ship::ClassName(Ship::ATTACK));

		cmb_class->AddItem(Ship::ClassName(Ship::LCA));
		cmb_class->AddItem(Ship::ClassName(Ship::COURIER));
		cmb_class->AddItem(Ship::ClassName(Ship::CARGO));
		cmb_class->AddItem(Ship::ClassName(Ship::CORVETTE));
		cmb_class->AddItem(Ship::ClassName(Ship::FREIGHTER));
		cmb_class->AddItem(Ship::ClassName(Ship::FRIGATE));
		cmb_class->AddItem(Ship::ClassName(Ship::DESTROYER));
		cmb_class->AddItem(Ship::ClassName(Ship::CRUISER));
		cmb_class->AddItem(Ship::ClassName(Ship::BATTLESHIP));
		cmb_class->AddItem(Ship::ClassName(Ship::CARRIER));
		cmb_class->AddItem(Ship::ClassName(Ship::SWACS));
		cmb_class->AddItem(Ship::ClassName(Ship::DREADNAUGHT));
		cmb_class->AddItem(Ship::ClassName(Ship::STATION));
		cmb_class->AddItem(Ship::ClassName(Ship::FARCASTER));

		cmb_class->AddItem(Ship::ClassName(Ship::MINE));
		cmb_class->AddItem(Ship::ClassName(Ship::COMSAT));
		cmb_class->AddItem(Ship::ClassName(Ship::DEFSAT));

		cmb_class->AddItem(Ship::ClassName(Ship::BUILDING));
		cmb_class->AddItem(Ship::ClassName(Ship::FACTORY));
		cmb_class->AddItem(Ship::ClassName(Ship::SAM));
		cmb_class->AddItem(Ship::ClassName(Ship::EWR));
		cmb_class->AddItem(Ship::ClassName(Ship::C3I));
		cmb_class->AddItem(Ship::ClassName(Ship::STARBASE));

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

		for (int i = 0; i < cmb_class->NumItems(); i++) {
			const char* cname   = cmb_class->GetItem(i);
			int         classid = Ship::ClassForName(cname);

			if (design && classid == design->type) {
				cmb_class->SetSelection(i);
				current_class = classid;
				break;
			}
		}
	}

	if (cmb_design) {
		OnClassSelect(0);
		OnDesignSelect(0);
	}

	if (cmb_role) {
		cmb_role->ClearItems();

		for (int i = Mission::PATROL; i <= Mission::OTHER; i++) {
			cmb_role->AddItem(Mission::RoleName(i));

			if (i == 0)
			cmb_role->SetSelection(0);

			else if (elem->MissionRole() == i)
			cmb_role->SetSelection(cmb_role->NumItems()-1);
		}
	}

	if (cmb_region) {
		cmb_region->ClearItems();

		if (mission) {
			StarSystem* sys = mission->GetStarSystem();
			if (sys) {
				List<OrbitalRegion> regions;
				regions.append(sys->AllRegions());
				regions.sort();

				ListIter<OrbitalRegion> iter = regions;
				while (++iter) {
					OrbitalRegion* region = iter.value();
					cmb_region->AddItem(region->Name());

					if (!strcmp(elem->Region(), region->Name()))
					cmb_region->SetSelection(cmb_region->NumItems()-1);
				}
			}
		}
	}

	char buf[64];

	if (edt_name)  edt_name->SetText(elem->Name());

	sprintf_s(buf, "%d", elem->Count());
	if (edt_size)  edt_size->SetText(buf);

	sprintf_s(buf, "%d", elem->GetIFF());
	if (edt_iff)   edt_iff->SetText(buf);

	sprintf_s(buf, "%d", (int) elem->Location().x / 1000);
	if (edt_loc_x) edt_loc_x->SetText(buf);

	sprintf_s(buf, "%d", (int) elem->Location().y / 1000);
	if (edt_loc_y) edt_loc_y->SetText(buf);

	sprintf_s(buf, "%d", (int) elem->Location().z / 1000);
	if (edt_loc_z) edt_loc_z->SetText(buf);

	sprintf_s(buf, "%d", elem->RespawnCount());
	if (edt_respawns) edt_respawns->SetText(buf);

	sprintf_s(buf, "%d", elem->HoldTime());
	if (edt_hold_time) edt_hold_time->SetText(buf);

	if (btn_player)      btn_player->SetButtonState(elem->Player() > 0 ? 1 : 0);
	if (btn_playable)    btn_playable->SetButtonState(elem->IsPlayable() ? 1 : 0);
	if (btn_alert)       btn_alert->SetButtonState(elem->IsAlert() ? 1 : 0);
	if (btn_command_ai)  btn_command_ai->SetButtonState(elem->CommandAI());

	UpdateTeamInfo();

	if (cmb_intel) {
		cmb_intel->ClearItems();

		for (int i = Intel::RESERVE; i < Intel::TRACKED; i++) {
			cmb_intel->AddItem(Intel::NameFromIntel(i));

			if (i == 0)
			cmb_intel->SetSelection(0);

			else if (elem->IntelLevel() == i)
			cmb_intel->SetSelection(cmb_intel->NumItems()-1);
		}
	}

	Instruction* instr = 0;
	if (elem->Objectives().size() > 0)
	instr = elem->Objectives().at(0);

	if (cmb_objective) {
		cmb_objective->ClearItems();
		cmb_objective->AddItem("");
		cmb_objective->SetSelection(0);

		for (int i = 0; i < Instruction::NUM_ACTIONS; i++) {
			cmb_objective->AddItem(Instruction::ActionName(i));

			if (instr && instr->Action() == i)
			cmb_objective->SetSelection(i+1);
		}
	}

	if (cmb_target) {
		OnObjectiveSelect(0);
	}

	if (cmb_heading) {
		double heading = elem->Heading();

		while (heading > 2*PI)
		heading -= 2*PI;

		while (heading < 0)
		heading += 2*PI;

		if (heading >= PI/4 && heading < 3*PI/4)
		cmb_heading->SetSelection(1);

		else if (heading >= 3*PI/4 && heading < 5*PI/4)
		cmb_heading->SetSelection(2);

		else if (heading >= 5*PI/4 && heading < 7*PI/4)
		cmb_heading->SetSelection(3);

		else
		cmb_heading->SetSelection(0);
	}

	exit_latch = true;
}
void
MsnEditNavDlg::Show()
{
	bool need_tab_update = !shown;

	NavDlg::Show();

	if (txt_name && cmb_type) {
		txt_name->SetText("");

		if (cmb_system) {
			cmb_system->ClearItems();

			Galaxy* galaxy = Galaxy::GetInstance();
			ListIter<StarSystem> iter = galaxy->GetSystemList();
			while (++iter) {
				cmb_system->AddItem(iter->Name());
			}
		}

		if (mission) {
			int i;

			txt_name->SetText(mission->Name());
			cmb_type->SetSelection(mission->Type());

			StarSystem* sys = mission->GetStarSystem();
			if (sys && cmb_system && cmb_region) {
				for (i = 0; i < cmb_system->NumItems(); i++) {
					if (!strcmp(cmb_system->GetItem(i), sys->Name())) {
						cmb_system->SetSelection(i);
						break;
					}
				}

				cmb_region->ClearItems();
				int sel_rgn = 0;

				List<OrbitalRegion> regions;
				regions.append(sys->AllRegions());
				regions.sort();

				i = 0;
				ListIter<OrbitalRegion> iter = regions;
				while (++iter) {
					OrbitalRegion* region = iter.value();
					cmb_region->AddItem(region->Name());

					if (!strcmp(mission->GetRegion(), region->Name())) {
						sel_rgn = i;
					}

					i++;
				}

				cmb_region->SetSelection(sel_rgn);
			}
		}
	}

	if (need_tab_update) {
		ShowTab(2);
	}

	exit_latch = true;
}
CampaignMissionRequest*
CampaignPlanMission::PlanRandomFighterMission()
{
    CampaignMissionRequest* request  = 0;
    int                     type     = fighter_mission_types[fighter_mission_index++];
    int                     ownside  = player_group->GetIFF();
    CombatGroup*            primary  = player_group;
    CombatGroup*            obj      = 0;

    if (fighter_mission_index > 15)
    fighter_mission_index = 0;

    if (type == Mission::ESCORT_FREIGHT) {
        CombatGroup*  freight  = campaign->FindGroup(ownside, CombatGroup::FREIGHT);
        if (!freight || freight->CalcValue() < 1)
        type = Mission::PATROL;
        else
        obj  = freight;
    }

    else if (type == Mission::ESCORT_SHUTTLE) {
        CombatGroup*  shuttle = campaign->FindGroup(ownside, CombatGroup::LCA_SQUADRON);
        if (!shuttle || shuttle->CalcValue() < 1)
        type = Mission::PATROL;
        else
        obj  = shuttle;
    }

    else if (primary->Type() == CombatGroup::WING) {
        if (RandomChance())
        primary = primary->FindGroup(CombatGroup::INTERCEPT_SQUADRON);
        else
        primary = primary->FindGroup(CombatGroup::FIGHTER_SQUADRON);
    }

    if (type >= Mission::AIR_PATROL && type <= Mission::AIR_INTERCEPT) {
        CombatZone* zone     = 0;
        bool        airborne = false;

        if (primary)
        zone = primary->GetAssignedZone();

        if (zone && zone->GetRegions().size() > 1) {
            Text        air_region = *zone->GetRegions().at(1);
            StarSystem* system     = campaign->GetSystem(zone->System());

            if (system) {
                OrbitalRegion* rgn = system->FindRegion(air_region);

                if (rgn && rgn->Type() == Orbital::TERRAIN)
                airborne = true;
            }
        }

        if (!airborne) {
            if (type == Mission::AIR_INTERCEPT)
            type = Mission::INTERCEPT;

            else if (type == Mission::AIR_SWEEP)
            type = Mission::SWEEP;

            else
            type = Mission::PATROL;
        }
    }

    request = new(__FILE__,__LINE__)
    CampaignMissionRequest(campaign, type, start, primary);

    if (request)
    request->SetObjective(obj);

    return request;
}