void
FltDlg::OnAlert(AWEvent* event)
{
	if (!filter_list || !hangar_list || !ship) return;

	int         squad  = filter_list->GetSelectedIndex();
	Hangar*     hangar = ship->GetHangar();
	Sim*        sim    = Sim::GetSim();
	const char* call   = sim->FindAvailCallsign(ship->GetIFF());
	Element*    elem   = sim->CreateElement(call, ship->GetIFF());
	FlightDeck* deck   = 0;
	int         queue  = 1000;
	const int*  load   = 0;

	elem->SetSquadron(hangar->SquadronName(squad));
	elem->SetCarrier(ship);

	for (int i = 0; i < ship->NumFlightDecks(); i++) {
		FlightDeck* d = ship->GetFlightDeck(i);

		if (d && d->IsLaunchDeck()) {
			int dq = hangar->PreflightQueue(d);

			if (dq < queue) {
				queue = dq;
				deck  = d;
			}
		}
	}

	int nalert = 0;
	int slots[4];

	for (int i = 0; i < 4; i++)
	slots[i] = -1;

	for (int i = 0; i < hangar_list->NumItems(); i++) {
		if (hangar_list->IsSelected(i)) {
			int nslot = hangar_list->GetItemData(i);
			slots[nalert] = nslot;

			if (!load) {
				const HangarSlot* hangar_slot = hangar->GetSlot(squad, nslot);
				if (hangar_slot) {
					load = hangar->GetLoadout(hangar_slot);
					elem->SetLoadout((int*) load);
				}
			}

			hangar->GotoAlert(squad, nslot, deck, elem);
			hangar_list->SetSelected(i, false);
			nalert++;

			if (nalert >= 4)
			break;
		}
	}

	NetUtil::SendElemCreate(elem, squad, slots, true);
}
void
NetGameClient::DoElemCreate(NetMsg* msg)
{
	if (!msg) return;

	NetElemCreate elem_create;
	elem_create.Unpack(msg->Data());

	const char* elem_name = elem_create.GetName().data();

	::Print("NetGameClient::DoElemCreate name: %s iff: %d type %s\n",
	elem_name,
	elem_create.GetIFF(),
	Mission::RoleName(elem_create.GetType()));

	Sim*     sim   = Sim::GetSim();
	Element* elem  = sim->FindElement(elem_name);
	if (elem) {
		::Print("  element '%' already exists - ignored\n", elem_name);
		return;
	}

	elem = sim->CreateElement(elem_name,
	elem_create.GetIFF(),
	elem_create.GetType());

	int*     load     = elem_create.GetLoadout();
	int*     slots    = elem_create.GetSlots();
	int      squadron = elem_create.GetSquadron();
	int      code     = elem_create.GetObjCode();
	Text     target   = elem_create.GetObjective();
	bool     alert    = elem_create.GetAlert();
	bool     active   = elem_create.GetInFlight();

	elem->SetIntelLevel(elem_create.GetIntel());
	elem->SetLoadout(load);

	if (code > Instruction::RTB || target.length() > 0) {
		Instruction* obj  = new(__FILE__,__LINE__) Instruction(code, target);
		elem->AddObjective(obj);
	}

	Ship* carrier = sim->FindShip(elem_create.GetCarrier());
	if (carrier) {
		elem->SetCarrier(carrier);

		Hangar* hangar = carrier->GetHangar();
		if (hangar) {
			Text squadron_name = hangar->SquadronName(squadron);
			elem->SetSquadron(squadron_name);

			if (active) {
				for (int i = 0; i < 4; i++) {
					int slot = slots[i];
					if (slot > -1) {
						hangar->GotoActiveFlight(squadron, slot, elem, load);
					}
				}
			}

			else {
				FlightDeck* deck   = 0;
				int         queue  = 1000;

				for (int i = 0; i < carrier->NumFlightDecks(); i++) {
					FlightDeck* d = carrier->GetFlightDeck(i);

					if (d && d->IsLaunchDeck()) {
						int dq = hangar->PreflightQueue(d);

						if (dq < queue) {
							queue = dq;
							deck  = d;
						}
					}
				}

				for (int i = 0; i < 4; i++) {
					int slot = slots[i];
					if (slot > -1) {
						hangar->GotoAlert(squadron, slot, deck, elem, load, !alert);
					}
				}
			}
		}
	}
}
void
FltDlg::OnPackage(AWEvent* event)
{
	if (!filter_list || !hangar_list || !ship) return;

	int         code   = Mission::PATROL;

	switch (mission_type) {
	case 0:  code = Mission::PATROL;       break;
	case 1:  code = Mission::INTERCEPT;    break;
	case 2:  code = Mission::ASSAULT;      break;
	case 3:  code = Mission::STRIKE;       break;
	case 4:  code = Mission::ESCORT;       break;
	case 5:  code = Mission::INTEL;        break;
	}

	int         squad  = filter_list->GetSelectedIndex();
	Hangar*     hangar = ship->GetHangar();
	Sim*        sim    = Sim::GetSim();
	const char* call   = sim->FindAvailCallsign(ship->GetIFF());
	Element*    elem   = sim->CreateElement(call, ship->GetIFF(), code);
	Element*    tgt    = 0;
	FlightDeck* deck   = 0;
	int         queue  = 1000;
	int*        load   = 0;

	elem->SetSquadron(hangar->SquadronName(squad));
	elem->SetCarrier(ship);

	if (objective_list) {
		int   index     = objective_list->GetListIndex();
		Text  target    = objective_list->GetItemText(index);

		Instruction* objective = new(__FILE__,__LINE__) Instruction(code, target.data());
		elem->AddObjective(objective);

		tgt = sim->FindElement(target.data());
	}

	if (loadout_list && design) {
		int   index    = loadout_list->GetListIndex();
		Text  loadname = loadout_list->GetItemText(index);

		ListIter<ShipLoad> sl = (List<ShipLoad>&) design->loadouts;
		while (++sl && !load) {
			if (sl->name == loadname) {
				load = sl->load;
				elem->SetLoadout(load);
			}
		}
	}

	for (int i = 0; i < ship->NumFlightDecks(); i++) {
		FlightDeck* d = ship->GetFlightDeck(i);

		if (d && d->IsLaunchDeck()) {
			int dq = hangar->PreflightQueue(d);
			
			if (dq < queue) {
				queue = dq;
				deck  = d;
			}
		}
	}

	int npackage = 0;
	int slots[6];

	for (int i = 0; i < 6; i++)
	slots[i] = -1;

	for (int i = 0; i < hangar_list->NumItems(); i++) {
		if (hangar_list->IsSelected(i)) {
			int nslot = hangar_list->GetItemData(i);
			hangar->GotoAlert(squad, nslot, deck, elem, load, true);
			slots[npackage] = nslot;
			hangar_list->SetSelected(i, false);
			npackage++;

			if (npackage >= 6)
			break;
		}
	}

	NetUtil::SendElemCreate(elem, squad, slots, false);

	if (flight_planner) {
		switch (mission_type) {
		case 0:
		default:
			flight_planner->CreatePatrolRoute(elem, patrol_pattern++);
			break;

		case 1:
		case 2:
		case 3:
			if (tgt)
			flight_planner->CreateStrikeRoute(elem, tgt);
			else
			flight_planner->CreatePatrolRoute(elem, patrol_pattern++);
			break;

		case 4:
			if (tgt)
			flight_planner->CreateEscortRoute(elem, tgt);
			else
			flight_planner->CreatePatrolRoute(elem, patrol_pattern++);
			break;
		}

		if (patrol_pattern < 0 || patrol_pattern > 3)
		patrol_pattern = 0;
	}
}