/**
  * qty does not actually buy that qty, but checks that the AI has enough cash to buy that qty.
  *
  * @param type
  * @param location
  * @param qty
  * @param keep_reserve_cash
  * @return
  */
 private boolean buyUnit(int type, Point location, int qty, boolean keep_reserve_cash) {
   int cost = UnitStats.GetCost(type) * qty;
   if (keep_reserve_cash) {
     cost += this.player_attack_units_threat * 2;
   }
   if (main.game_data.pdata[side].getCash() >= cost) {
     if (main.game_data.map_data.map[location.x][location.y].owner == side) {
       if (main.isAreaClearForNewUnit(
           location.x * MapData.SQUARE_SIZE, location.y * MapData.SQUARE_SIZE, type)) {
         UnitStats.CreateUnit(main, type, location.x, location.y, side);
         main.game_data.pdata[side].addCash(-UnitStats.GetCost(type));
         if (Main.DEBUG_AI) {
           main.addLogEntry("AI bought " + UnitStats.GetName(type));
         }
         this.updateIntel();
         return true;
       } else {
         if (Main.DEBUG_AI) {
           main.addLogEntry("Area " + top_threat_location + " not clear");
         }
       }
     }
   }
   return false;
 }
  public void process() {
    if (ai_interval.hitInterval()) {

      if (update_intel_interval.hitInterval()) {
        this.updateIntel();
      }

      if (attack_type_to_build == -1) {
        SelectRandomUnitTypeAndQty(true);
      }
      if (defence_type_to_build == -1) {
        SelectRandomUnitTypeAndQty(false);
      }

      if (max_map_threat <= 1) {
        if (build_mine || max_map_target == 0) {
          if (best_mine_location.x >= 0) {
            if (buyUnit(UnitStats.MINE, best_mine_location, 1, true)) {
              this.build_mine = Functions.rnd(1, BUY_MINE_CHANCE) > 1;
            }
          } else {
            if (Main.DEBUG_AI) {
              main.addLogEntry("No potential mine location found.");
            }
          }
        } else if (max_map_target > 0) {
          if (buyUnit(
              attack_type_to_build, top_target_location, this.num_attackers_to_build, false)) {
            this.num_attackers_to_build--;
            if (this.num_attackers_to_build == 0) {
              this.build_mine = Functions.rnd(1, BUY_MINE_CHANCE) > 1;
              SelectRandomUnitTypeAndQty(true);
            } else {
              ai_interval.fireInterval(); // SO we buy again quickly!
            }
          }
        }
      } else { // Defend!
        if (buyUnit(defence_type_to_build, top_threat_location, num_defenders_to_build, false)) {
          num_defenders_to_build--;
          if (num_defenders_to_build == 0) {
            SelectRandomUnitTypeAndQty(false);
          } else {
            ai_interval.fireInterval(); // SO we buy again quickly!
          }
        }
      }
    }
  }