int
CombatGroup::CountUnits() const
{
	int n = 0;

	CombatGroup* g = (CombatGroup*) this;

	ListIter<CombatUnit> unit = g->units;
	while (++unit)
	n += unit->Count() - unit->DeadCount();

	CombatGroup* pThis = ((CombatGroup*) this);
	pThis->live_comp.clear();
	ListIter<CombatGroup> iter = g->components;
	while (++iter) {
		CombatGroup* comp = iter.value();

		if (!comp->IsReserve()) {
			int unit_count = comp->CountUnits();
			if (unit_count > 0)
			pThis->live_comp.append(comp);

			n += unit_count;
		}
	}

	return n;
}
CombatUnit*
CombatGroup::GetNextUnit()
{
	if (units.size() > 0) {
		List<CombatUnit> live;

		ListIter<CombatUnit> unit = units;
		while (++unit) {
			if (unit->Count() - unit->DeadCount() > 0)
			live.append(unit.value());
		}

		if (live.size() > 0) {
			return live[unit_index++ % live.size()];
		}
	}

	if (components.size() > 0) {
		return components[unit_index % components.size()]->GetNextUnit();
	}

	return 0;
}
CombatUnit*
CombatGroup::GetRandomUnit()
{
	CombatUnit*       result = 0;
	List<CombatUnit>  live;

	ListIter<CombatUnit> unit = units;
	while (++unit) {
		if (unit->Count() - unit->DeadCount() > 0)
		live.append(unit.value());
	}

	if (live.size() > 0) {
		int ntries = 5;
		while (!result && ntries-- > 0) {
			int index = rand() % live.size();
			result = live[index];

			int ship_class = result->GetShipClass();
			if (ship_class >= Ship::CRUISER &&
					ship_class <= Ship::FARCASTER)
			result = 0;
		}
	}

	if (!result) {
		ListIter<CombatGroup> comp = components;
		while (++comp && !result) {
			CombatUnit* u = comp->GetRandomUnit();
			if (u)
			result = u;
		}
	}

	return result;
}