public int getAvailableLevel(L2Summon cha, int skillId) {
    int lvl = 0;
    if (!_skillTrees.containsKey(cha.getId())) {
      LOGGER.warning(
          getClass().getSimpleName()
              + ": Pet id "
              + cha.getId()
              + " does not have any skills assigned.");
      return lvl;
    }
    Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
    for (L2PetSkillLearn temp : skills) {
      if (temp.getId() != skillId) {
        continue;
      }
      if (temp.getLevel() == 0) {
        if (cha.getLevel() < 70) {
          lvl = (cha.getLevel() / 10);
          if (lvl <= 0) {
            lvl = 1;
          }
        } else {
          lvl = (7 + ((cha.getLevel() - 70) / 5));
        }

        // formula usable for skill that have 10 or more skill levels
        int maxLvl = SkillData.getInstance().getMaxLevel(temp.getId());
        if (lvl > maxLvl) {
          lvl = maxLvl;
        }
        break;
      } else if (temp.getMinLevel() <= cha.getLevel()) {
        if (temp.getLevel() > lvl) {
          lvl = temp.getLevel();
        }
      }
    }
    return lvl;
  }
  private boolean canControl(L2Summon summon) {
    if (summon instanceof L2BabyPetInstance) {
      if (!((L2BabyPetInstance) summon).isInSupportMode()) {
        sendPacket(SystemMessageId.A_PET_ON_AUXILIARY_MODE_CANNOT_USE_SKILLS);
        return false;
      }
    }

    if (summon.isPet()) {
      if ((summon.getLevel() - getActiveChar().getLevel()) > 20) {
        sendPacket(SystemMessageId.YOUR_PET_IS_TOO_HIGH_LEVEL_TO_CONTROL);
        return false;
      }
    }
    return true;
  }
  /**
   * Cast a skill for active summon.<br>
   * Target is specified as a parameter but can be overwrited or ignored depending on skill type.
   *
   * @param skillId the skill Id to be casted by the summon
   * @param target the target to cast the skill on, overwritten or ignored depending on skill type
   * @param pet if {@code true} it'll validate a pet, if {@code false} it will validate a servitor
   */
  private void useSkill(int skillId, L2Object target, boolean pet) {
    final L2PcInstance activeChar = getActiveChar();
    if (activeChar == null) {
      return;
    }

    if (pet) {
      final L2Summon summon = activeChar.getPet();
      if (!validateSummon(summon, pet)) {
        return;
      }

      if (!canControl(summon)) {
        return;
      }

      final int lvl =
          PetDataTable.getInstance()
              .getPetData(summon.getId())
              .getAvailableLevel(skillId, summon.getLevel());

      if (lvl > 0) {
        summon.setTarget(target);
        summon.useMagic(
            SkillData.getInstance().getSkill(skillId, lvl), _ctrlPressed, _shiftPressed);
      }

      if (skillId == SWITCH_STANCE_ID) {
        summon.switchMode();
      }
    } else {
      final L2Summon servitor = activeChar.getAnyServitor();
      if (!validateSummon(servitor, pet)) {
        return;
      }

      final int lvl = SummonSkillsTable.getInstance().getAvailableLevel(servitor, skillId);

      if (lvl > 0) {
        servitor.setTarget(target);
        servitor.useMagic(
            SkillData.getInstance().getSkill(skillId, lvl), _ctrlPressed, _shiftPressed);
      }
    }
  }
 @Override
 protected final void writeImpl() {
   writeC(0xB6);
   writeD(_summon.getSummonType());
   writeD(_summon.getObjectId());
   writeD(_summon.getX());
   writeD(_summon.getY());
   writeD(_summon.getZ());
   writeS(_summon.getTitle());
   writeD(_curFed);
   writeD(_maxFed);
   writeD((int) _summon.getCurrentHp());
   writeD(_summon.getMaxHp());
   writeD((int) _summon.getCurrentMp());
   writeD(_summon.getMaxMp());
   writeD(_summon.getLevel());
   writeQ(_summon.getStat().getExp());
   writeQ(_summon.getExpForThisLevel()); // 0% absolute value
   writeQ(_summon.getExpForNextLevel()); // 100% absolute value
 }
  @Override
  protected final void writeImpl() {
    writeC(0xb2);
    writeD(_summon.getSummonType());
    writeD(_summon.getObjectId());
    writeD(_summon.getTemplate().idTemplate + 1000000);
    writeD(0); // 1=attackable

    writeD(_x);
    writeD(_y);
    writeD(_z);
    writeD(_heading);
    writeD(0);
    writeD(_mAtkSpd);
    writeD(_pAtkSpd);
    writeD(_runSpd);
    writeD(_walkSpd);
    writeD(_swimRunSpd);
    writeD(_swimWalkSpd);
    writeD(_flRunSpd);
    writeD(_flWalkSpd);
    writeD(_flyRunSpd);
    writeD(_flyWalkSpd);

    writeF(_multiplier); // movement multiplier
    writeF(1); // attack speed multiplier
    writeF(_summon.getTemplate().fCollisionRadius);
    writeF(_summon.getTemplate().fCollisionHeight);
    writeD(_summon.getWeapon()); // right hand weapon
    writeD(_summon.getArmor()); // body armor
    writeD(0); // left hand weapon
    writeC(
        _summon.getOwner() != null
            ? 1
            : 0); // when pet is dead and player exit game, pet doesn't show master name
    writeC(1); // running=1 (it is always 1, walking mode is calculated from multiplier)
    writeC(_summon.isInCombat() ? 1 : 0); // attacking 1=true
    writeC(_summon.isAlikeDead() ? 1 : 0); // dead 1=true
    writeC(_isSummoned ? 2 : _val); //  0=teleported  1=default   2=summoned
    writeD(-1); // High Five NPCString ID
    writeS(_summon.getName()); // summon name
    writeD(-1); // High Five NPCString ID
    writeS(_summon.getTitle()); // owner name
    writeD(1);
    writeD(
        _summon.getOwner() != null
            ? _summon.getOwner().getPvpFlag()
            : 0); // 0 = white,2= purpleblink, if its greater then karma = purple
    writeD(_summon.getOwner() != null ? _summon.getOwner().getKarma() : 0); // karma
    writeD(_curFed); // how fed it is
    writeD(_maxFed); // max fed it can be
    writeD((int) _summon.getCurrentHp()); // current hp
    writeD(_maxHp); // max hp
    writeD((int) _summon.getCurrentMp()); // current mp
    writeD(_maxMp); // max mp
    writeD(_summon.getStat().getSp()); // sp
    writeD(_summon.getLevel()); // lvl
    writeQ(_summon.getStat().getExp());

    if (_summon.getExpForThisLevel() > _summon.getStat().getExp())
      writeQ(_summon.getStat().getExp()); // 0%  absolute value
    else writeQ(_summon.getExpForThisLevel()); // 0%  absolute value

    writeQ(_summon.getExpForNextLevel()); // 100% absoulte value
    writeD(
        _summon instanceof L2PetInstance ? _summon.getInventory().getTotalWeight() : 0); // weight
    writeD(_summon.getMaxLoad()); // max weight it can carry
    writeD(_summon.getPAtk(null)); // patk
    writeD(_summon.getPDef(null)); // pdef
    writeD(_summon.getMAtk(null, null)); // matk
    writeD(_summon.getMDef(null, null)); // mdef
    writeD(_summon.getAccuracy()); // accuracy
    writeD(_summon.getEvasionRate(null)); // evasion
    writeD(_summon.getCriticalHit(null, null)); // critical
    writeD((int) _summon.getStat().getMoveSpeed()); // speed
    writeD(_summon.getPAtkSpd()); // atkspeed
    writeD(_summon.getMAtkSpd()); // casting speed

    writeD(
        _summon.getAbnormalEffect()); // c2  abnormal visual effect... bleed=1; poison=2; poison &
    // bleed=3; flame=4;
    int npcId = _summon.getTemplate().npcId;
    writeH(_summon.isMountable() ? 1 : 0); // c2    ride button

    writeC(0); // c2

    // Following all added in C4.
    writeH(0); // ??
    writeC(
        _summon.getOwner() != null
            ? _summon.getOwner().getTeam()
            : 0); // team aura (1 = blue, 2 = red)
    writeD(_summon.getSoulShotsPerHit()); // How many soulshots this servitor uses per hit
    writeD(_summon.getSpiritShotsPerHit()); // How many spiritshots this servitor uses per hit

    int form = 0;
    if (npcId == 16041 || npcId == 16042) {
      if (_summon.getLevel() > 84) form = 3;
      else if (_summon.getLevel() > 79) form = 2;
      else if (_summon.getLevel() > 74) form = 1;
    } else if (npcId == 16025 || npcId == 16037) {
      if (_summon.getLevel() > 69) form = 3;
      else if (_summon.getLevel() > 64) form = 2;
      else if (_summon.getLevel() > 59) form = 1;
    }
    writeD(form); // CT1.5 Pet form and skills
    writeD(_summon.getSpecialEffect());
  }