// generates units for one house only
// fires in all games where mpmodes exist (Skirmish, Online, Coop, etc)
// AmountToSpend is GameSettings->StartingUnitCount * (totalCost + totalCount / 2) / totalCount
// here totalCost and totalCount include only those which are allowed to this house, unlike in Generate_B
// and BaseUnit is spawned in another function, also unlike _B

bool ScenarioClass::GenerateStartingUnits_A(HouseClass *OwnerHouse, int &AmountToSpend) {

  if(AmountToSpend <= 0) {
    return 1;
  }

  DynamicVectorClass<InfantryTypeClass*> vec_Inf;
  DynamicVectorClass<UnitTypeClass*> vec_Veh;

  int houseBitfield = 1 << OwnerHouse->CountryIndex;

  foreach(vec_UnitTypes as Veh) {
    if(Veh->AllowedToStartInMultiplayer
      && (houseBitfield & Veh->Owners)
      && Veh->TechLevel <= House->TechLevel
      && !Rules->BaseUnit.Contains(Veh)
    ) {
      vec_Veh.Append(Veh);
    }
  }

  foreach(vec_InfantryTypes as Inf) {
    if(Inf->AllowedToStartInMultiplayer
      && (houseBitfield & Inf->Owners)
      && Inf->TechLevel <= House->TechLevel
    ) {
      vec_Inf.Append(Inf);
    }
  }

  if(!vec_Veh.Length && !vec_Inf.Length) {
    return 0;
  }

  int RemainingCost = AmountToSpend;
  int SpentCost = 0;
  int RemainingUnits = 20;

  TechnoTypeClass *randUnit;

  while(RemainingUnits) {
    if(SpentCost < AmountToSpend/3 || !vec_Veh.Length) {
      randUnit = vec_InfantryTypes[Random_Ranged(0, vec_Inf.Length - 1)];
    } else {
      randUnit = vec_UnitTypes[Random_Ranged(0, vec_Veh.Length - 1)];
    }

    TechnoClass * Instance = randUnit->CreateInstance(OwnerHouse);

    if(Instance->PlaceNearby(OwnerHouse->GetHomeCell, 4)) {
      RemainingCost -= randUnit->GetCost();
      if(this->Flags & FLAG_INITIALVETERAN) {
        Instance->SetVeteran();
      }
      Instance->ForceMission(OwnerHouse->IsHumanControlled ? MISSION_GUARD : MISSION_AREA_GUARD);
    } else {
      RemainingCost -= randUnit->GetCost(); // not refunded!
      delete Instance;
    }

    if(RemainingCost <= 0) {
      break;
    }
  }

  return 1;
}