/* ---------------------------------------------------------
    AP Å¬·¡½º

    LAST UPDATE : 2007.07.22
    CREATER     : [HG]ErrorDa
    MAIL TO     : hespnk@hanmail.net

    IE7.0¿¡¼­ Å×½ºÆ® µÈ ÄÚµåÀÔ´Ï´Ù.
    ¼Ò½ºÄÚµå¿¡ ´ëÇÏ¿©´Â ºÒÆß±ÝÁöÀÔ´Ï´Ù.
    »ç¿ëÀ» ¿øÇÏ½Ã´Â ºÐÀº À§ÀÇ ¸ÞÀÏ·Î ¿¬¶ôÁÖ¼¼¿ä.
--------------------------------------------------------- */

var CON_TYPE_MELEE = 0;
var CON_TYPE_RANGE = 1;
var CON_TYPE_MAGIC = 2;
var CON_TYPE_LIFE  = 3;
var CON_TYPE_ALCHEMY = 4;

//---------------------------------------------
// ½ºÅ³µ¥ÀÌÅÍ¿¡ µé¾î°¥ ·©Å©º° Á¤º¸
//---------------------------------------------
clsRank = function( sRank, sMin, sMax, sBal, sAP, arg, sCP )
{
    this.sRank = sRank;
    this.sMax = sMax;
    this.sMin = sMin;
    this.sBal = sBal;
    this.sAP  = sAP;
    this.sCP  = sCP;
    this.sInc = new Array();
    for(var nLoop=0; nLoop<arg.length; nLoop++)
    {
        this.sInc[nLoop] = arg[nLoop];
    }
}

//---------------------------------------------
// ½ºÅ³ µ¥ÀÌÅÍ
//---------------------------------------------
clsSkill = function()
{
    this.Index = -1;
    this.sType = -1;
    this.sKname = "";
    this.sEname = "";
    this.sImage = "";

    this.sRank = new Array();
    this.curRank = 0;
}

clsSkill.prototype =
{
    //---------------------------------------------
    // ½ºÅ³ ±âÃÊ µ¥ÀÌÅÍ
    // sType = Á¾Á·
    // kName = ±¹¹® ÀÌ¸§
    // eName = ¿µ¹® ÀÌ¸§
    // img   = ½ºÅ³ ÀÌ¹ÌÁö ÆÄÀÏ
    //---------------------------------------------
    Init : function( sType, kName, eName, img )
    {
        this.sType = sType;
        this.sImage = img;
        this.sKname = kName;
        this.sEname = eName;
    },

    //---------------------------------------------
    // ·©Å© Ãß°¡
    //---------------------------------------------
    AddRank : function( cRank )
    {
        this.sRank[this.sRank.length] = cRank;
    },

    //---------------------------------------------
    // ·©Å© ¼±ÅÃ
    //---------------------------------------------
    SetRank : function( index )
    {
        this.curRank = index;
    },

    //---------------------------------------------
    // ¼±ÅÃµÈ ·©Å©ÀÇ ÀüÅõ·Â ¾ò±â
    //---------------------------------------------
    getCP : function()
    {
        return this.sRank[this.curRank].sCP;
    },

    //---------------------------------------------
    // ¼±ÅÃµÈ ·©Å©±îÁöÀÇ ap ¹× status ´É·ÂÄ¡
    //---------------------------------------------
    Calc : function()
    {
        //         0  1 2 3  4 5 6 7  8  9  0
        //        ap  n x b  s d i w lf st ma
        var rtn = [0, 0,0,0, 0,0,0,0, 0, 0, 0];
        for(var nLoop=0; nLoop<=this.curRank; nLoop++)
        {
            with( this.sRank[nLoop] )
            {
                rtn[0] += sAP;
                rtn[1]  = sMin;
                rtn[2]  = sMax;
                rtn[3] += sBal;

                for(var i=0; i<sInc.length; i++)
                {
                    rtn[4+i] += sInc[i];
                }
            }
        }
        return rtn;
    }
}

//---------------------------------------------
// ÀüÃ¼ ½ºÅ³ ¸ðÀ½
//---------------------------------------------
clsSkills = function()
{
    this.skills = new Array();
        //                 0   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
        //                ap  mn mx rn rx gx b1 b2 b3, c  s  d  i  w lf st ma
        this.calcResult = [0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

    this.melee = 0;
    this.range = 0;
    this.magic = 0;
    this.life  = 0;
    this.alchemy = 0;
}

clsSkills.prototype =
{
    //---------------------------------------------
    // ½ºÅ³ Ãß°¡
    //---------------------------------------------
    addSkill : function( skill )
    {
        skill.Index = this.skills.length;
        this.skills[this.skills.length] = skill;
        switch( skill.sType )
        {
            case CON_TYPE_MELEE : this.melee++; break;
            case CON_TYPE_RANGE : this.range++; break;
            case CON_TYPE_MAGIC : this.magic++; break;
            case CON_TYPE_LIFE  : this.life++;  break;
            case CON_TYPE_ALCHEMY:this.alchemy++; break;
        }
    },

    //---------------------------------------------
    // ½ºÅ³ µ¥ÀÌÅÍ ¾ò±â
    // type = Á¾Á·
    // offset = ½ºÅ³ÀÌ µî·ÏµÈ ¼ø¼­
    //---------------------------------------------
    getSkill : function( type, offset )
    {
        var nCnt=0;
        for(var i=0; i<this.skills.length; i++)
        {
            if( this.skills[i].sType == type )
            {
                nCnt++;
                if( nCnt == offset )
                {
                    return this.skills[i];
                }
            }
        }
    },

    //---------------------------------------------
    // ½ºÅ³ ÀüÅõ·Â °è»ê
    //---------------------------------------------
    calcCp : function()
    {
        var arrCp = Array();
        for(var i=0; i<this.skills.length; i++)
        {
            arrCp[i] = this.skills[i].getCP();

            for(var j=0; j<arrCp.length; j++)
            {
                if( arrCp[i] > arrCp[j] )
                {
                    var t = arrCp[i];
                    arrCp[i] = arrCp[j];
                    arrCp[j] = t;
                }
            }
        }
        return (arrCp[0] + (arrCp[1] / 2));
    },

    //---------------------------------------------
    // ÄÄ¹î ¸¶½ºÅÍ¸®·Î ¿Ã¶ó°£ »ý¸í·Â ¾ò±â
    //---------------------------------------------
    getCombatLife : function()
    {
        var life = 0;
        for(var i=0; i<this.skills.length; i++)
        {
            if( this.skills[i].sKname == "ÄÄ¹î ¸¶½ºÅÍ¸®" )
            {
                life = this.skills[i].Calc()[8];
                break;
            }
        }
        return life;
    },

    //---------------------------------------------
    // ½ºÅÈ ÀüÅõ·Â °è»ê
    //---------------------------------------------
    calcMp : function()
    {
        var Mp=0, life=this.getCombatLife();
        // ½ºÅ×ÀÌÅÍ½º ÀüÅõ·Â = Life ¡¿1 + Mana ¡¿0.5 + Stamina ¡¿0.5 + Str ¡¿1 + Int ¡¿0.2 + Dex ¡¿0.1 + Will ¡¿0.5 + Luck ¡¿0.1
        Mp = ((this.calcResult[14] - life))
           + ((this.calcResult[16]) * 0.5)
           + ((this.calcResult[15]) * 0.5)
           + ((this.calcResult[10]) * 1.0)
           + ((this.calcResult[12]) * 0.2)
           + ((this.calcResult[11]) * 0.1)
           + ((this.calcResult[13]) * 0.5)
        return Mp;
    },

    //---------------------------------------------
    // ÀüÃ¼ ½ºÅ³·Î ¿Ã¶ó°£ ap ¹× status ´É·ÂÄ¡ °è»ê
    //---------------------------------------------
    calcAp : function()
    {
        var result;
        //                 0   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
        //                ap  mn mx rn rx gx b1 b2 b3, c  s  d  i  w lf st ma
        this.calcResult = [0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

        for(var i=0; i<this.skills.length; i++)
        {
            result = this.skills[i].Calc();
            this.calcResult[0] += result[0];

            if( this.skills[i].sKname == "ÄÄ¹î ¸¶½ºÅÍ¸®" )
            {
                this.calcResult[1] = result[1];
                this.calcResult[2] = result[2];
                this.calcResult[6] = result[3];
            }
            else if( this.skills[i].sKname == "·¹ÀÎÁö ¸¶½ºÅÍ¸®" )
            {
                this.calcResult[3] = result[1];
                this.calcResult[4] = result[2];
                this.calcResult[7] = result[3];
            }
            else if( this.skills[i].sKname == "¸ÅÁ÷ ¸¶½ºÅÍ¸®" )
            {
                this.calcResult[8] = result[3];
            }

            for(var j=4; j<result.length; j++)
            {
                this.calcResult[6+j] += result[j];
            }
        }

        // ±ÙÁ¢ µ¥¹ÌÁö
        this.calcResult[1] += Math.floor(this.calcResult[10] / 3.0);
        this.calcResult[2] += Math.floor(this.calcResult[10] / 2.5);
        // ±ÙÁ¢ ¹ë·±½º
        this.calcResult[6] += Math.floor(this.calcResult[11] / 4.0);

        // ¿ø°Å¸® µ¥¹ÌÁö
        this.calcResult[3] += Math.floor(this.calcResult[11] / 3.5);
        this.calcResult[4] += Math.floor(this.calcResult[11] / 2.5);
        // ¿ø°Å¸® ¹ë·±½º
        this.calcResult[7] += Math.floor(this.calcResult[11] / 4.0);

        // ¸¶¹ý µ¥¹ÌÁö
        //MagicDamage = 2*(log10(Int)^3) -> C2/S7 º¯°æ°ø½Ä
        this.calcResult[5] += Math.round(((2 * Math.pow(Math.log(this.calcResult[12]) / Math.log(10), 3)) * 10.0)) / 10.0;
        if( this.calcResult[5] == "-Infinity") this.calcResult[5] = 0;
        // ¸¶¹ý ¹ë·±½º
        //this.calcResult[8] += Math.floor(this.calcResult[12] / ??);

        // ¹ë·±½º ÃÖ´ëÄ¡ 80% °íÁ¤ (¸¶¹ý Á¦¿Ü)
        if( this.calcResult[6] > 80 ) this.calcResult[6] = 80;
        if( this.calcResult[7] > 80 ) this.calcResult[7] = 80;

        // Å©¸®Æ¼ÄÃ
        this.calcResult[9]  = (this.calcResult[13]/10.0);
    }
}
