/**
 * 天赋模型对象。
 * @create   2004-9-29 source0
 * @author   source0 source0@hotmail.com
 * @copyright 版权所有（C） 2004  source0
 *                这一程序是自由软件，你可以遵照自由软件基金会出版的GNU通用
 *            公共许可证条款来修改和重新发布这一程序。或者用许可证的第二版，
 *            或者（根据你的选择）用任何更新的版本。
 *                发布这一程序的目的是希望它有用，但没有任何担保。甚至没有
 *            适合特定目的的隐含的担保。更详细的情况请参阅GNU通用公共许可证。
 *                你应该已经和程序一起收到一份GNU通用公共许可证的副本。如果
 *            还没有，写信给：
 *                The Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
 *                MA02139, USA
 */


/**
 * 天赋模型对象。
 * @param id 天赋的ID。
 * @param name 天赋的名称。
 * @param tier 天赋的层次（0～6）。
 * @param type 天赋的类型：强化(TALENT_TYPE_IMPROVE)、技能(TALENT_TYPE_SKILL)。
 * @param data 天赋的效果。CTalentImprove 或者 CTalentSkill 实例。
 * @param req CTalentRequirement 实例数组。本天赋所需天赋。
 * @create   2004-9-29 source0
 */
function CTalentModel(id, name, tier, type, data, req){
    this.id = id;
    this.name = name;
    this.tier = tier;
    this.type = type;
    this.data = data;
    this.requirements = null;

    if ( req ){
        this.requirements = req;
    }
    this.toString = function(){
        return "CTalentModel["
            + "ID=\"" + this.id + "\", "
            + "名称=\"" + this.name + "\", "
            + "层次=" + this.tier + ", "
            + "效果=\"" + this.data.description + "\", "
            + "所需天赋=" + this.requirements
            + "]";
    }
}
/**
 * 强化型天赋效果对象。
 * @param desc 天赋的效果说明。
 * @param talentMaxRank 最高级别。
 * @param effectArray CTalentImproveEffect 数组。
 * @create   2004-9-29 source0
 */
function CTalentImprove(desc, talentMaxRank, effectArray){
    this.description = desc;
    this.maxRank = 5;
    this.effects = new Array();

    if ( talentMaxRank ){
        this.maxRank = talentMaxRank;
    }
    if ( effectArray ){
        this.effects = effectArray;
    }
    /**
     * 增加一个天赋效果。
     * @return 增加的天赋效果。
     * @create   2004-10-16 source0
     */
    this.addEffect = function(){
        this.effects[this.effects.length] = new CTalentImproveEffect(this.effects.length, "%", 0, 0);
        return this.effects[this.effects.length - 1];
    }
    /**
     * 删除最后一个天赋效果。
     * @return 删掉的天赋效果。如果没找到，返回 null。
     * @create   2004-10-16 source0
     */
    this.removeLastEffect = function(){
        return this.removeEffect(this.effects[this.effects.length - 1].id);
    }
    /**
     * 删除一个天赋效果。
     * @param id 需要删除的效果ID。
     * @return 删掉的天赋效果。如果没找到，返回 null。
     * @create   2004-10-16 source0
     */
    this.removeEffect = function (id){
        var newEffects = new Array();
        var rt = null;
        for ( var i = 0; i < this.effects.length; i++ ){
            if ( this.effects[i].id != id ){
                newEffects[newEffects.length] = this.effects[i];
            } else {
                rt = this.effects[i];
            }
        }
        this.effects = newEffects;
        return rt;
    }
    /**
     * 根据ID取得效果对象。
     * @param id 效果的ID。
     * @return 如果找到，返回其实例。如果没找到，返回 null。
     * @create   2004-10-17 source0
     */
    this.getEffectById = function (id){
        for ( var key in this.effects ){
            if ( this.effects[key].id == id ){
                return this.effects[key];
            }
        }
        return null;
    }

    this.toString = function(){
        var rt = "CTalentImprove["
            + "效果=\"" + this.description + "\", "
            + "最高级别=" + this.maxRank
        for ( var key in this.effects ){
            rt += this.effects[key];
       }
        rt += "]";
    }
}
/**
 * 强化型天赋的效果对象中各个效果的数据。
 * @param id 效果的ID。
 * @param effectUnit 天赋效果的单位，例如“分钟”、“%”等等。
 * @param effectInitValue 初始值。如果天赋的数值不是等差变化，那么此参数为数值数组，增量参数被忽略。
 * @param effectIncrement 每级的增量。
 * @create   2004-10-16 source0
 */
function CTalentImproveEffect(id, effectUnit, effectInitValue, effectIncrement){
    this.id = id;
    this.unit = "%";
    this.initValue = 0;
    this.increment = 0;

    this.unit = effectUnit;

    this.initValue = effectInitValue;

    if ( !isNaN(effectInitValue) ){
        if ( effectIncrement ){
            this.increment = effectIncrement;
        }
    }

    /**
     * 得到指定级别下的效果数据。包含单位。
     * @param rank 级别。
     * @create   2004-10-16 source0
     */
    this.getValue = function(rank){
        var rt = new String();

        if ( 0 >= rank ){
            return 0;
        }
        /* 如果数据不是等差数列，需要从字符串中提取数值。 */
        if ( !isNaN(this.initValue) ){
            rt += "" + (parseFloat(this.initValue) + parseFloat(this.increment * (rank - 1)));
        } else {
            var values = this.initValue.split(/[,\s]/);
            if ( rank > values.length ){
                rt += "0";
            } else {
                rt += values[rank - 1];
            }
        }
        if ( rt.lastIndexOf('.') >= 0 ){
            return rt.substring(0, rt.lastIndexOf('.') + 2)
        } else {
            return rt;
        }
    }
    this.toString = function(){
        return "CTalentImproveEffect["
            + "ID=" + this.id + ", "
            + "初始值=" + this.initValue + ", "
            + "每级增量=" + this.increment + ", "
            + "单位=\"" + this.unit + "\""
            + "]";
    }

}
/**
 * 技能型天赋效果对象。
 * @param desc 效果说明。
 * @param con 释放技能的消耗。
 * @param conType 技能消耗的类型。
 * @param time 技能释放时间。
 * @param cooldownTime 释放间隔，单位：秒。
 * @param maxRan 最大有效距离。
 * @param minRan 最小有效距离。
 * @param mark 备注。
 * @create   2004-9-29 source0
 */
function CTalentSkill(desc, con, conType, time, cooldownTime, maxRan, minRan, mark){
    this.maxRank = 1;
    this.description = desc;
    this.consume = 0;
    this.consumeType = CONSUME_TYPE_MANA;
    this.castTime = 0;
    this.cooldown = 0;
    this.maxRange = 0;
    this.minRange = 0;
    this.remark = null;
    if ( con ){
        this.consume = con;
    }
    if ( conType ){
        this.consumeType = conType;
    }
    if ( time ){
        this.castTime = time;
    }
    if ( cooldownTime ){
        this.cooldown = cooldownTime;
    }
    if ( maxRan ){
        this.maxRange = maxRan;
    }
    if ( minRan ){
        this.minRange = minRan;
    }
    if ( mark ){
        this.remark = mark;
    }

    var range = this.maxRange;

    if ( this.minRange > 0){
        range = this.minRange + "-" + range;
    }
    this.toString = function(){
        return "CTalentSkill["
            + "技能消耗=" + this.consume + ", "
            + "消耗类型=" + getConsumeTypeName(this.consumeType) + ", "
            + "释放时间=" + this.castTime + ","
            + "释放间隔=" + this.cooldown + ", "
            + "有效距离=" + range + "\", "
            + "效果=\"" + this.description + "\","
            + "备注=\"" + this.remark + "\""
            + "]";
    }
}

/**
 * 天赋需求对象。
 * @param talent 所需天赋的ID。
 * @param rank 所需天赋的级别。
 */
function CTalentRequirement(talent, rank){
    this.talent = talent;
    this.rank = rank;
    this.toString = function(){
        return "CTalentRequirement[天赋=\"" + this.talent +"\", 级别=" + this.rank + "]";
    }
}
