static HouseClass::FindEligibleAITeams(VectorClass<AITeamTypeClass *> buffer, HouseClass *OwnerHouse, bool unused) {
  HouseClass *EnemyHouse = NULL;
  if( OwnerHouse->idxEnemyHouse != -1 ) {
    HouseClass *EnemyHouse = vec_Houses[OwnerHouse->idxEnemyHouse];
  }

  VectorClass<AITeamTypeClass *> TeamTypes;
  int chance = Random_Ranged(1, 100);
  int countTeams = 0;
  int countBaseDef = 0;
  bool enoughBaseDefense = 0;
  if( chance <= OwnerHouse->RatioAITriggerTeams ) {
    if( OwnerHouse->AITriggersActive ) {
      for( int idxTeam = 0; idxTeam < vec_Teams.Length; ++idxTeam ) {
        AITeamClass *curTeam = vec_Teams[idxTeam];
        if( OwnerHouse == curTeam->OwnerHouse ) {
          ++countTeams;
          if( curTeam->TeamType->IsBaseDefense ) {
            ++countBaseDef;
          }
        }
      }
      if( Rules->TotalAITeamCap[OwnerHouse->AIDifficulty] < countTeams || countBaseDef < countTeams / 2 ) {
        if( Rules->MaximumAIDefensiveTeams[OwnerHouse->AIDifficulty] > countBaseDef  ) {
          enoughBaseDefense = 1;
        }
      } else { 
        int lastCreationTime = MAX_INT;
        AITeamClass *chosenTeam = NULL;

        for( int idxTeam = 0; idxTeam < vec_Teams.Length; ++idxTeam ) {
          AITeamClass *curTeam = vec_Teams[idxTeam];
          if( OwnerHouse == curTeam->OwnerHouse ) {
            if( !curTeam->TeamType->IsBaseDefense ) {
              if( curTeam->CreationFrame <= lastCreationTime ) {
                chosenTeam = curTeam;
                lastCreationTime = curTeam->CreationFrame;
              }
            }
          }
        }

        if( chosenTeam ) {
          --countTeams;
          chosenTeam->~chosenTeam();
          enoughBaseDefense = 1;
        }
      }

      if( Rules->TotalAITeamCap[OwnerHouse->AIDifficulty] < countTeams ) {
        VectorClass<DiscreteDistribution<AITriggerTypeClass *>> vec_Distribution;
        DiscreteDistribution<AITriggerTypeClass *> obj_DistributionNode;
        bool foundSuperTrigger = 0;
        
        int curWeight = FloatToInt(curTrig->curWeight);
        int sumWeight = 0;
        obj_DistributionNode.Object = NULL;
        obj_DistributionNode.Weight = 0;

        for( int idxTrig = 0; idxTrig < vec_AITriggerTypes.Length; ++idxTrig ) {
          AITriggerTypeClass *curTrig = vec_AITriggerTypes[idxTrig];
          if( !curTrig ) {
            continue;
          }
          
          if( !curTrig->ConditionMet(OwnerHouse, TargetHouse, enoughBaseDefense) ) {
            continue;
          }
                    
          if( curWeight == 5000 ) {
            if( foundSuperTrigger ) {
              continue;
            }
            foundSuperTrigger = 1;
            sumWeight = 0;
            vec_Distribution.Clear();
          } else {
            if( foundSuperTrigger ) {
              continue;
            }
          }

          vec_Distribution.Append(&obj_DistributionNode);
          obj_DistributionNode.Object = curTrig;
          obj_DistributionNode.Weight = curWeight;
          
          sumWeight += curWeight;
        }
        if( sumWeight >= 1 ) {
          if( vec_Distribution.Length > 0 ) {
            int rndWeight = Random_Ranged(1, sumWeight);
            if( vec_Distribution.Length > 0 ) {
              VectorClass<DiscreteDistribution<AITriggerTypeClass *>>::const_iterator iter;
              int loopWeight = 0;
              for( iter = vec_Distribution.begin(); iter != vec_Distribution.end(); ++iter ) {
                loopWeight += iter.Weight;
                if( loopWeight >= rndWeight ) {
                  AITriggerTypeClass *selTrig = iter.Object;
                  
                  if( selTrig->Team1 ) {
                    TeamTypes.Append(selTrig->Team1);
                  }
                  if( selTrig->Team2 ) {
                    TeamTypes.Append(selTrig->Team2);
                  }
                  return TeamTypes;
                }
              }              
            }
          }
        }
      }
    }
  }
  return TeamTypes;
}