#include "prc_racial_const"
#include "inc_2dacache"
#include "inc_persist_loca"

int GetTotalLA(object oTarget)
{
    int nLA;
    int nRace = GetRacialType(oTarget);
    if(GetPRCSwitch(PRC_XP_USE_SIMPLE_LA))
	{
        nLA += StringToInt(Get2DACache("ECL", "LA", nRace));
	}
    if(GetPRCSwitch(PRC_XP_INCLUDE_RACIAL_HIT_DIE_IN_LA))
	{
        nLA += StringToInt(Get2DACache("ECL", "RaceHD", nRace));
	}
    nLA += GetPersistantLocalInt(oTarget, "template_LA");
    nLA -= GetPersistantLocalInt(oTarget, "LA_Buyoff");
    return nLA;
}

int GetECL(object oTarget)
{
    int nLevel;
    // we need to use a derivation of the base xp formular to compute the
    // pc level based on total XP.
    //
    // base XP formula (x = pc level, t = total xp):
    //
    //   t = x * (x-1) * 500
    //
    // need to use some base math..
    // transform for pq formula use (remove brackets with x inside and zero right side)
    //
    //   x^2 - x - (t / 500) = 0
    //
    // use pq formula to solve it [ x^2 + px + q = 0, p = -1, q = -(t/500) ]...
    //
    // that's our new formula to get the level based on total xp:
    //   level = 0.5 + sqrt(0.25 + (t/500))
    //
    if(GetPRCSwitch(PRC_ECL_USES_XP_NOT_HD) && GetIsPC(oTarget))
        nLevel = FloatToInt(0.5 + sqrt(0.25 + ( IntToFloat(GetXP(oTarget)) / 500 )));
    else
        nLevel = GetHitDice(oTarget);
    nLevel += GetTotalLA(oTarget);
    return nLevel;
}


/* int GetECL(object oPC)
{
    int nECL = 0;
    int nRace = GetRacialType(oPC);
    switch(nRace)
    {
        case RACIAL_TYPE_DUERGAR:
        case RACIAL_TYPE_AASIMAR:
        case RACIAL_TYPE_TIEFLING:
        case RACIAL_TYPE_HOBGOBLIN:
        case RACIAL_TYPE_HALFOGRE:
        case RACIAL_TYPE_HALFDROW:
        case RACIAL_TYPE_GRAYORC:
        case RACIAL_TYPE_AIR_GEN:
        case RACIAL_TYPE_EARTH_GEN:
        case RACIAL_TYPE_FIRE_GEN:
        case RACIAL_TYPE_WATER_GEN:
        case RACIAL_TYPE_SHADOWSWYFT:
        case RACIAL_TYPE_LIZARDFOLK:
        case RACIAL_TYPE_GNOLL:
            nECL = 1;
            break;
        case RACIAL_TYPE_DROW_FEMALE:
        case RACIAL_TYPE_DROW_MALE:
        case RACIAL_TYPE_GITHYANKI:
        case RACIAL_TYPE_GITHZERAI:
        case RACIAL_TYPE_OROG:
        case RACIAL_TYPE_OGRE:
        case RACIAL_TYPE_PURE_YUAN:
        case RACIAL_TYPE_THRIKREEN:
            nECL = 2;
            break;
        case RACIAL_TYPE_AVARIEL:
        case RACIAL_TYPE_DEEP_GNOME:
        case RACIAL_TYPE_FORMIAN:
            nECL = 3;
            break;
        case RACIAL_TYPE_AZER:
        case RACIAL_TYPE_PIXIE:
            nECL = 4;
            break;
        case RACIAL_TYPE_TROLL:
            nECL = 5;
            break;
        case RACIAL_TYPE_RAKSHASA:
        case RACIAL_TYPE_ABOM_YUAN:
        case RACIAL_TYPE_ILLITHID:
            nECL = 7;
            break;
    }
    nECL += GetHitDice(oPC);
    return nECL;
} */