wXY *HouseClass::PickIonCannonTarget(wXY *buffer) {
  HouseClass *EnemyHouse = vec_Houses[this->idxEnemyHouse];
  VectorClass<AbstractClass *> potentialTargets;
  int lenTargets = 0;
  int lastWeight = 0;
  int curWeight = 0;
  wXY curXY;

  for( int idxT = 0; idxT < vec_Technoes.Length; ++idxT) {
    TechnoClass *pTechno = vec_Technoes[idxT];
    bool eligibleTarget = 0;
    if ( pTechno->OwningPlayer != EnemyHouse ) {
      continue;
    }
    curWeight = 1;
    if ( pTechno->InWhichLayer() == LAYER_GROUND && pTechno->Alive ) {
      eligibleTarget = 1;
    } else if ( this->AIDifficulty == AI_HARD ) {
      for( int idxF = 0; idxF < vec_Factories.length; ++idxFactory) {
        if ( vec_Factories[idxF]->GetProduct() == pTechno ) {
          if ( !vec_Factories[idxF]->IsSuspended ) {
            eligibleTarget = 1;
          }
        }
      }
    }

    eAbstractID What_Am_I = pTechno->What_Am_I();
    if ( What_Am_I == IS_UNIT ) {
      UnitTypeClass *pUnit = ((UnitTypeClass *)pTechno)->UnitType;
      if ( pUnit->Harvester ) {
        curWeight = Rules->AIIonCannonHarvValue[this->AIDifficulty];
      } else if( Rules->BuildConst.Contains(pUnit->DeploysInto) ) {
        curWeight = Rules->AIIonCannonMCVValue[this->AIDifficulty];
      } else if ( pUnit->Passengers ) {
        curWeight = Rules->AIIonCannonAPCValue[this->AIDifficulty];
      } else {
        curWeight = 2;
      }
    } else if ( What_Am_I == IS_INFANTRY ) {
      InfantryTypeClass *pInf = ((InfantryClass *)pTechno)->InfantryType;
      if ( pInf->Engineer ) {
        curWeight = Rules->AIIonCannonEngineerValue[this->AIDifficulty];
      } else if ( pInf->VehicleThief ) {
        curWeight = Rules->AIIonCannonThiefValue[this->AIDifficulty];
      } else {
        curWeight = 2;
      }
    } else if ( What_Am_I == IS_BUILDING ) {
      BuildingTypeClass *pBld = ((BuildingClass *)pTechno)->BuildingType;
      if ( pBld->Factory == IS_BUILDINGTYPE ) {
        curWeight = Rules->AIIonCannonConYardValue[this->AIDifficulty];
      } else if ( FactoryOf == IS_UNITTYPE && !pBld->Naval ) {
        curWeight = Rules->AIIonCannonWarFacValue[this->AIDifficulty];
      } else if ( pBld->PowerBonus > pBld->PowerDrain ) {
        curWeight = Rules->AIIonCannonPowerValue[this->AIDifficulty];
      } else if ( pBld->IsBaseDefense ) {
        curWeight = Rules->AIIonCannonBaseDefValue[this->AIDifficulty];
      } else if ( pBld->IsPlug ) {
        curWeight = Rules->AIIonCannonPlugValue[this->AIDifficulty];
      } else if ( pBld->IsTemple ) {
        curWeight = Rules->AIIonCannonTempleValue[this->AIDifficulty];
      } else if ( pBld->HoverPad ) {
        curWeight = Rules->AIIonCannonHelipadValue[this->AIDifficulty];
      } else if ( Rules->BuildTech.Contains(pBld) ) {
        curWeight = Rules->AIIonCannonTechValue[this->AIDifficulty];
      } else {
        curWeight = 4;
      }
    }

    dwXYZ curCoords = pTechno->GetCoords();
    curXY.X = curCoords->X / 256;
    curXY.Y = curCoords->Y / 256;
    if ( !Map::IsValidCellAddress(curXY) )
      curWeight = 0;
    if ( pTechno->CloakState == STATE_CLOAKED ) {
      curWeight = Random_Ranged(0, lastWeight + 10); // ! how the hell did this ever make sense to ww... ?
    }
    if ( !eligibleTarget ) { // oh, NOW it's time to "optimize", yes
      continue;
    }
    if ( curWeight <= lastWeight ) {
      if ( curWeight == lastWeight ) {
        potentialTargets.Append(pTechno);
      }
    } else {
      potentialTargets.Clear();
      potentialTargets.Append(pTechno);
      lastWeight = curWeight;
    }
  }

  // done looping over all objects, now who will be the lucky recipient?

  lenTargets = potentialTargets.Length;
  if ( lenTargets <= 0 ) {
    return HouseClass::Blank_IonCannon_Coords;
  }
  int idxRandom = Random_Ranged(0, lenTargets - 1);
  AbstractClass * selectedTarget = potentialTargets[idxRandom];
  if ( !selectedTarget )
    return HouseClass::Blank_IonCannon_Coords;
  dwXYZ curCoords = selectedTarget->GetCoords();
  buffer->X = curCoords->X / 256;
  buffer->Y = curCoords->Y / 256;
  return buffer;
}