local Util = require("common.Util")
local CombatBuff = require("combat.CombatBuff")
local CombatDefine = require("combat.CombatDefine")
local CombatImpl = require("combat.CombatImpl")
local Skill = require("combat.Skill")
local CombatObj = require("combat.CombatObj")
local RoleAttr = require("role.RoleAttr")
local RoleDefine = require("role.RoleDefine")
local HeroExcel = require("excel.hero").hero
local SkillExcel = require("excel.skill").skill
-- local BufferExcel = require("excel.buffer").buffer
local FuwenExcel = require("excel.fuwen")
local SkinSkillExcel = require("excel.skin").skill
local BingshuLogic = require("fuwen.BingshuLogic")
local ProjectLogic = require("platform.ProjectLogic")
local HeroDefine = require("hero.HeroDefine")
local WinnerRelicLogic = require("winnerRelic.WinnerRelicLogic")
local TargetMode = require("combat.TargetMode")
local HeroExclusiveWeapon = require("hero.HeroExclusiveWeapon")


local tbFuwenSkillGroupID = {}
local posSortBySpeed = posSortBySpeed or {}

function initBeSkill(obj)
	obj.beSkillList = {}
	obj.beSkillListTemp = {}
end

function setBeSkillID(obj, skillID, isFuWenSkill, isRelicSkill)
	local skillConfig = Skill.GetFinalSkillConfig(obj, skillID)
	skillID = skillConfig.id
	if obj.beSkillListTemp[skillID] then return end
    --local skillConfig = SkillExcel[skillID]

	if skillConfig.need > obj.quality then return end
	if skillConfig.beSkillType == 0 then return end

	if skillConfig.beSkillType == BESKILL_TYPE24 then
        obj.beSkillListTemp[BESKILL_TYPE24] = obj.beSkillListTemp[BESKILL_TYPE24] or {}
		obj.beSkillListTemp[BESKILL_TYPE24][#obj.beSkillListTemp[BESKILL_TYPE24] + 1] =  {
           skillID,
           skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,
           skillConfig.cd[2] or 0, 
           isFuWenSkill or 0, isRelicSkill or 0}

		for _,skillID in ipairs(skillConfig.args) do
			--local skillConfig = SkillExcel[skillID]
			local skillConfig = Skill.GetFinalSkillConfig(obj, skillID)
			if not obj.beSkillListTemp[skillID] and skillConfig.need <= obj.quality then
				obj.beSkillList[skillConfig.beSkillType] = obj.beSkillList[skillConfig.beSkillType] or {}
				obj.beSkillList[skillConfig.beSkillType][#obj.beSkillList[skillConfig.beSkillType] + 1] = {
                skillID,
                skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,
                skillConfig.cd[2] or 0, 
                isFuWenSkill or 0, isRelicSkill or 0}

				obj.beSkillListTemp[skillID] = 1
			end
		end
	else
		obj.beSkillList[skillConfig.beSkillType] = obj.beSkillList[skillConfig.beSkillType] or {}
		obj.beSkillList[skillConfig.beSkillType][#obj.beSkillList[skillConfig.beSkillType] + 1] = {
        skillID,
        skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,
        skillConfig.cd[2] or 0, 
        isFuWenSkill or 0, isRelicSkill or 0}
		obj.beSkillListTemp[skillID] = 1
	end
end

function setBeSkill(obj, config, skinConf, monsterOutConfig)
	local temp = {}
	local confBeskillID = skinConf and skinConf.beSkillList or config.beSkillList
	
	for _,skillID in ipairs(confBeskillID) do
		setBeSkillID(obj,skillID)
	end

    -- 加载 赋能技能 这个策划说放到最后
	if config.bePvPSkillList and CombatImpl.isPVP == 1 then
	    for _,skillID in ipairs(config.bePvPSkillList) do
	        setBeSkillID(obj, skillID)
	    end
	end

    -- 怪物组附加的被动技能
    if monsterOutConfig then
       for _, skillID in ipairs(monsterOutConfig) do
       	  setBeSkillID(obj,skillID)
       end
    end
	
	-- 符文被动技能
	-- 符文技能效果这里 如果有多个同类型的技能 效果不叠加 只取最高等级的那个技能的效果
	if obj.fuwen or obj.bingshu then
		local hasFuwenBeSkill = nil
		Util.initTable(tbFuwenSkillGroupID)
		if obj.fuwen then
			for i = 1, 2 do
				local fuwenGrid = obj.fuwen[i]
				if fuwenGrid and fuwenGrid.skill then
	                for j = 1, #fuwenGrid.skill do
						local fuwenSkillID = fuwenGrid.skill[j]
	                    local fuwenSkillConfig = FuwenExcel.skill[fuwenSkillID]
	                    local fuwenGroupID = fuwenSkillConfig.groupID
						if fuwenSkillConfig.fuwenBeSkill ~= 0 then
							hasFuwenBeSkill = true
							if tbFuwenSkillGroupID[fuwenGroupID] then
								if tbFuwenSkillGroupID[fuwenGroupID] < fuwenSkillID then
									tbFuwenSkillGroupID[fuwenGroupID] = fuwenSkillID
								end
							else
								tbFuwenSkillGroupID[fuwenGroupID] = fuwenSkillID
							end
						end
	                end
				end
			end
		end
		if obj.bingshu then
	        for i = 1, BingshuLogic.BINGSHU_MAXCNT do
	            local bingshuGrid = obj.bingshu[i]
	            if bingshuGrid then
	                local skillID = bingshuGrid.skillID
	                local skillConfig = FuwenExcel.skill[skillID]
	                local groupID = skillConfig.groupID
	                hasFuwenBeSkill = true
	                if (tbFuwenSkillGroupID[groupID] or 0) < skillID then
	                    tbFuwenSkillGroupID[groupID] = skillID
	                end             
	            end
	        end
		end

		if hasFuwenBeSkill == true then
			for groupID, skillIDTemp in pairs(tbFuwenSkillGroupID) do
				local fuwenSkillConfig = FuwenExcel.skill[skillIDTemp]
				local fuwenBeSkillID = fuwenSkillConfig.fuwenBeSkill
				fuwenBeSkillID = obj.fuwenStrengthenSkillList and obj.fuwenStrengthenSkillList[fuwenBeSkillID] or fuwenBeSkillID
				setBeSkillID(obj,fuwenBeSkillID, 1)
			end
		end
	end


	--遗物被动技能
	if obj.relic and nil ~= _G.next(obj.relic) then
		local nRelicID = obj.relic.relicID
		local star = obj.relic.star
		local nSkillID = WinnerRelicLogic.WinnerRelic_GetSkillID(nRelicID, star)
		if 0 < nSkillID then
			setBeSkillID(obj, nSkillID, 0, 1)
			
			local tSkilConfig = SkillExcel and SkillExcel[nSkillID]
			if tSkilConfig and tSkilConfig.otherArgs and tSkilConfig.otherArgs.skillList then
				for k,v in ipairs(tSkilConfig.otherArgs.skillList) do
					local config = SkillExcel[v]
					if config and config.id then
						setBeSkillID(obj, config.id, 0, 1)
					end
				end
			end
		end

	end

	-- 神威灵装被动技能
	if obj.artifactsBeSkillList then
		for _, skillId in ipairs(obj.artifactsBeSkillList) do
			setBeSkillID(obj, skillId)
		end
	end
end

function setPetBeSkill(obj)
	--if obj
	--local skillConfig = SkillExcel[1999]
	local skillConfig = Skill.GetSkillConfig(1999)

	if skillConfig then
		obj.beSkillList = obj.beSkillList or {}
		obj.beSkillList[skillConfig.beSkillType] = obj.beSkillList[skillConfig.beSkillType] or {}
		obj.beSkillList[skillConfig.beSkillType][#obj.beSkillList[skillConfig.beSkillType] + 1] = {1999,skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,skillConfig.cd[2] or 0}
	end

end

--被动技能类型
BESKILL_TYPE1 = 1 --普攻附带
BESKILL_TYPE2 = 2 --被击触发
BESKILL_TYPE3 = 3 --自身生命低于参数
BESKILL_TYPE4 = 4 --自己暴击触发
BESKILL_TYPE5 = 5 --受到技能伤害
BESKILL_TYPE6 = 6 --自己攻击被格挡
BESKILL_TYPE7 = 7 --受到暴击触发
BESKILL_TYPE8 = 8 --自己格挡触发(其他单位对自己发动攻击，自己触发格挡)
BESKILL_TYPE9 = 9 --敌方英雄触发格挡
BESKILL_TYPE10 = 10 --自己受持续状态伤害
BESKILL_TYPE11 = 11 --自己受控制类状态
BESKILL_TYPE12 = 12 --场上有英雄被添加燃烧状态
BESKILL_TYPE13 = 13	-- 我方英雄施放主动技能
BESKILL_TYPE14 = 14	-- 我方英雄暴击
BESKILL_TYPE15 = 15	-- 自己死亡
BESKILL_TYPE16 = 16 --我方英雄死亡
BESKILL_TYPE17 = 17 --敌方英雄死亡
BESKILL_TYPE18 = 18 --有英雄死亡
BESKILL_TYPE19 = 19 --回合开始
BESKILL_TYPE20 = 20 --回合结束
BESKILL_TYPE23 = 23 --激活就生效(计入面板属性)
BESKILL_TYPE24 = 24 --技能包
BESKILL_TYPE25 = 25 --替换初始普攻
BESKILL_TYPE26 = 26 --激活就生效(进入战斗前算。不计入面板属性)
BESKILL_TYPE27 = 27 --魔兽回能量被动
BESKILL_TYPE28 = 28 --自身触发格挡次数
BESKILL_TYPE29 = 29 --有人受到缠绕时
BESKILL_TYPE30 = 30 --使生命低于x时
BESKILL_TYPE31 = 31 --任意英雄释放主动技能
BESKILL_TYPE32 = 32 --战斗开始时触发
BESKILL_TYPE33 = 33 --复活时触发
BESKILL_TYPE34 = 34 --使用主动技能时
BESKILL_TYPE35 = 35 --自身击杀敌人时
BESKILL_TYPE36 = 36 --攻击回合结束时触发
BESKILL_TYPE37 = 37 --物攻后触发
BESKILL_TYPE38 = 38 --法攻后触发
BESKILL_TYPE39 = 39 --攻击附带   (普攻 主动 都算) 
BESKILL_TYPE40 = 40 --攻击前根据条件判断是否触发（小战斗回合，针对每个对象单独判断）
BESKILL_TYPE41 = 41	--实际扣血前触发
BESKILL_TYPE42 = 42	--由其他英雄的buff触发
BESKILL_TYPE43 = 43 --被某个技能打死
BESKILL_TYPE44 = 44 --自己暴击+反击触发
BESKILL_TYPE45 = 45 --计算伤害时触发
BESKILL_TYPE46 = 46 --攻击附带   (普攻 主动 连击 反击都算) 
BESKILL_TYPE47 = 47 --每回合叠加属性
BESKILL_TYPE48 = 48 --死亡英雄伤害率
BESKILL_TYPE49 = 49 --加血变成减血
BESKILL_TYPE50 = 50 --每回合触发 降低对方首次攻击目标伤害率
BESKILL_TYPE51 = 51 --场上有英雄被添加诅咒状态
BESKILL_TYPE52 = 52 --场上有英雄被攻击
BESKILL_TYPE53 = 53 --场上有英雄有特定被动技能 友军普攻触发
BESKILL_TYPE54 = 54 --场上有英雄特定buff 伤害加成
BESKILL_TYPE55 = 55 --场上有英雄指定buff层数 额外伤害加成
BESKILL_TYPE56 = 56 --场上有英雄特定bufer且概率产生
BESKILL_TYPE57 = 57 --特定buff被引爆消除时 触发的效果
BESKILL_TYPE58 = 58 --指定技能暴击
BESKILL_TYPE59 = 59 --没有暴击触发一个效果
BESKILL_TYPE60 = 60 --暴击加成
BESKILL_TYPE61 = 61 --技能未击杀目标时触发
BESKILL_TYPE62 = 62 --回合开始生命值触发
BESKILL_TYPE63 = 63 --回合开始生命值触发
BESKILL_TYPE64 = 64 --冰冻buffer被击碎
BESKILL_TYPE65 = 65 --场上有英雄有特定被动技能 友军普攻暴击触发
BESKILL_TYPE66 = 66 --敌方死亡特定buff触发
BESKILL_TYPE67 = 67 --血量低于多少跟受控制触发
BESKILL_TYPE68 = 68 --血族英雄特殊处理
BESKILL_TYPE69 = 69 --血族英雄特殊处理
BESKILL_TYPE70 = 70 --增加的属性激活就生效(计入面板属性)并且概率连击
BESKILL_TYPE71 = 71	--回合开始 在其他buff添加结束
BESKILL_TYPE72 = 72	-- 不施放技能，只用来做其他效果的判断条件
BESKILL_TYPE73 = 73 --使用技能(包括普攻技能)命中敌方且造成伤害


NO_CHECK_FANJI_LIST = {
	[BESKILL_TYPE3] = 1,
	[BESKILL_TYPE15] = 1,
	[BESKILL_TYPE16] = 1,
	[BESKILL_TYPE17] = 1,
	[BESKILL_TYPE18] = 1,
	[BESKILL_TYPE35] = 1,
	[BESKILL_TYPE36] = 1,
	[BESKILL_TYPE37] = 1,
	[BESKILL_TYPE38] = 1,
	[BESKILL_TYPE44] = 1,
	[BESKILL_TYPE46] = 1,
	[BESKILL_TYPE73] = 1,
}

NO_CHECK_COMBO_LIST = {
	[BESKILL_TYPE1] = 1,
	[BESKILL_TYPE3] = 1,
    [BESKILL_TYPE14] = 1,
	[BESKILL_TYPE16] = 1,
	[BESKILL_TYPE17] = 1,
	[BESKILL_TYPE18] = 1,
	[BESKILL_TYPE35] = 1,
	[BESKILL_TYPE34] = 1,
	[BESKILL_TYPE25] = 1,
	[BESKILL_TYPE46] = 1,
    [BESKILL_TYPE37] = 1,
	[BESKILL_TYPE38] = 1,
	[BESKILL_TYPE43] = 1,
	[BESKILL_TYPE15] = 1,
	[BESKILL_TYPE73] = 1,
}

local function checkLimit(data)
   if data[2] and data[2] <= 0 then
	  return
   end
            
   if data[4] > 0 and data[3] > CombatImpl.round then
      return
   end
   return true
end

local function canUse(obj,data)
	if obj.isPet then
		return true
	end

	--被动技能次数限制判断
    if not checkLimit(data) then
       return
    end
	
	--local skillConfig = SkillExcel[data[1]]
	local skillConfig = Skill.GetSkillConfig(data[1])

	if not NO_CHECK_FANJI_LIST[skillConfig.beSkillType] and CombatImpl.isFanji then
		return
	end
	
	if not NO_CHECK_COMBO_LIST[skillConfig.beSkillType] and CombatImpl.comboType > 0 then
		return
	end 

    local bNotFeng = nil

    -- boss 被动不能被封印
    -- 世界boss模式下 boss不受feng状态影响
    if CombatImpl.fightMode[1] == CombatDefine.FIGHT_MODE3 then
       if obj.type == CombatDefine.COMBAT_OBJ_TYPE3 then
          bNotFeng = true
       end
    end

    if not bNotFeng and CombatBuff.isStatus(obj,{"banBeSkill","feng"}) then
	   return
	end

    data[3] = data[3] + data[4]

	local beSkillArgs = skillConfig.beSkillArgs
	local ret = true
	--被动技能触发条件判断
	if beSkillArgs[1] then
		if beSkillArgs[1] == "hp" then
			local hp = obj.hp
			local hpMax = CombatObj.getHpMax(obj)
			if hp > hpMax * beSkillArgs[2]/10000 then
				ret = false
			end
        elseif beSkillArgs[1] == "hpOrControl" then
            ret = false
			local hp = obj.hp
			local hpMax = CombatObj.getHpMax(obj)
			if hp <= hpMax * beSkillArgs[2]/10000 then
				ret = true
			end
            if CombatBuff.isAtControl(obj) then
                ret = true
            end
		elseif beSkillArgs[1] == "fanji" then
			local r = math.random(0,10000)
			if r > beSkillArgs[2] then
				return
			end
		elseif beSkillArgs[1] == "pos" then
			local pos = obj.pos
			ret = false
			if beSkillArgs[2] then
				for i = 1,#beSkillArgs[2] do
					if pos == beSkillArgs[2][i] then
						ret  = true
						break
					end
				end
			end
		elseif beSkillArgs[1] == "noBuffCmd" then
			return not CombatBuff.isStatus(obj,beSkillArgs[2])
		elseif beSkillArgs[1] == "attackerBuffCmd"  then
			local isStatus,cmd = CombatBuff.isStatus(obj,beSkillArgs[2])
			return isStatus
		elseif  beSkillArgs[1] == "myjob"  then
			ret = false
			for k,v in ipairs(beSkillArgs[2]) do
				if v == obj.job then
					ret = true
					break
				end
			end
		elseif beSkillArgs[1] == "sideLimit"  then
			for k,v in ipairs(beSkillArgs[2]) do
				local useCnt = CombatImpl.getBeSkillSideUse(obj.pos,v[1])
				if useCnt >= v[2] then
					return
				end
			end
		elseif beSkillArgs[1] == "firstAttackOrder" then
			local side = obj.side
			if side then
				local isFirstAttack = CombatImpl.firstAttack[side]
				if beSkillArgs[2] == 1 then
					return isFirstAttack
				else
					return not isFirstAttack
				end 
			end
		end
	end
	if ret and data[2] then
		data[2] = data[2] - 1
	end
	
	if skillConfig.cmd == "hp" then
		local hp = obj.hp
		local targetMode = skillConfig.args[3]
		--英雄血量为0时不能加血
		if (hp <= 0 or CombatBuff.isStatus(obj,{"revive"})) and targetMode and targetMode[1] == 10 then
			ret = false
		end
	end
	return ret
end

--被攻击触发，需要判断攻击者状态的被动技能
local function canUseOnHit(obj,data)
	if not obj then
		return true
	end

	--local skillConfig = SkillExcel[data[1]]
	local skillConfig = Skill.GetSkillConfig(data[1])

	local beSkillArgs = skillConfig.beSkillArgs
	local condCmd = beSkillArgs[1]
	if condCmd == "job" then
		for _,job in ipairs(beSkillArgs[2]) do
			if obj.job == job then
				return true
			end
		end
		return false	
	elseif condCmd == "buffCmd" then
		return CombatBuff.isStatus(obj,beSkillArgs[2])
	elseif condCmd == "targetHp" then
		local hp = obj.hp
		local checkHp = CombatObj.getHpMax(obj) * beSkillArgs[3] / 10000
		if beSkillArgs[2] == 1 and hp > checkHp then
			return true
		elseif  beSkillArgs[2] == 2 and hp < checkHp then
			return true
		end
	else
		return true
	end
end


local function isCanRevive(obj, attacker , data)
	-- local skillConfig = SkillExcel[data[1]]
	local skillConfig = Skill.GetSkillConfig(data[1])
	if skillConfig.cmd == "shengling" or 
       skillConfig.cmd == "revive" then
		if skillConfig.cmd == "shengling" and CombatImpl.checkSideResult(obj.pos) == true then        -- 如果是队伍最后一个死亡的不释放 圣灵复活
			return false
		end

		if CombatBuff.canRevive(obj) ~= true then
			return false
		end

		if skillConfig.cmd == "shengling" then
			if CombatBuff.isStatus(obj,{"revive"}) then
				return false
			end
		end
		if attacker and attacker.beSkillList then
			for k,skillList in pairs(attacker.beSkillList)  do
				for j,v in pairs(skillList)  do
					--local config = SkillExcel[v[1]]
					local config = Skill.GetSkillConfig(data[1])

					if config and skillConfig.cmd == "shengling" and  config.cmd == "noShengling" then  --击杀方有 不准圣灵复活被动
						return false
					elseif config and config.cmd == "noRevive" then   -- 击杀方 有不准复活的  
						if #config.args > 0 then
							local canNum = config.args[1]
							local random = math.random(1, 10000)
							if random > canNum then          -- noRevive 概率不生效
								return true
							else
                                return false
                            end
						end
						return true
					end
				end
			end		
				
		end						
	end
	return true
end

-- 连击检测
local function comboCheck(targetObj, side)
	if Skill.GroupSkillCnt ~= 1 then return end--技能包首次技能攻击才能触发
	if CombatImpl.isFanji or CombatImpl.comboType > 0 then return end

	for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
		local obj = CombatImpl.objList[pos]
		if obj and obj.beSkillList then
			for _, skillInfo in ipairs(obj.beSkillList[BESKILL_TYPE72] or {}) do
				local skillCfg = Skill.GetSkillConfig(skillInfo[1])
				if skillCfg and skillCfg.cmd == "comboProb" then
					local targetType = skillCfg.beSkillArgs[2] or 0
					local targetList = skillCfg.beSkillArgs[3]
					if targetType <= 0 or not targetList then
						return
					end

					local bl = false
					if targetType == 1 then -- 职业
						if table.find(targetList, targetObj.job) then
							bl = true
						end
					elseif targetType == 2 then -- 种族
						if table.find(targetList, targetObj.camp) then
							bl = true
						end
					end

					if not bl then return end

					local comboProbVal = skillCfg.beSkillArgs[1] or 0
					local randval = math.random(0, 10000)
					if comboProbVal >= randval then
						CombatImpl.comboList[#CombatImpl.comboList + 1] = {targetObj, 1}
					end
				end
			end
		end
    end
end



function onSkillID(obj,skillID)
	--local skillConfig = SkillExcel[skillID]
	local skillConfig = Skill.GetSkillConfig(skillID)

	if skillConfig.type == CombatDefine.SKILL_TYPE1 and obj.beSkillList[BESKILL_TYPE25] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE25]) do
			if canUse(obj,v) then
				--local conf = SkillExcel[v[1]]
				local conf = Skill.GetSkillConfig(v[1])
				return conf.args[1]
			end
		end	
	end
	return skillID
end


local function use(attacker,targets,data,newFrame, packBuff, hitObj)
	if canUse(attacker,data) and canUseOnHit(targets and targets[1],data) then
		--print("beskill realuse:",attacker.pos,data[1])
		--local skillConfig = SkillExcel[data[1]]
		local skillConfig = Skill.GetSkillConfig(data[1])
		local ret, arg2
        -- 二次元连击触发帧不新加帧
        if ProjectLogic.isSsecy() and skillConfig.cmd == "combo" then
           newFrame = false
        end

        if packBuff then
           CombatImpl.afterBuff()
        end

		if Skill.CMD[skillConfig.cmd] then
			if newFrame or CombatImpl.isSkillFrameEmpty() then
				--进入战斗时，未加入任何战斗帧触发被动技能时，先添加被动技能的战斗帧，可以在这个战斗帧上挂被动技能引发的额外作用
                CombatImpl.addFrame()
				CombatImpl.setSkillID(attacker, data[1])

			end
			ret, arg2 = Skill.CMD[skillConfig.cmd](attacker,skillConfig,targets, hitObj)
		end	

		--判断是否加喊话
		local isAddSkillFrame = CombatBuff.onUseBeSkill(attacker, skillConfig, targets, ret, nil, arg2)

		if skillConfig.contentType > 0 then
			if isAddSkillFrame then
				CombatImpl.addSaySkill(attacker,skillConfig)
			elseif ret or skillConfig.beSkillType == BESKILL_TYPE26 or 
                          skillConfig.beSkillType == BESKILL_TYPE23 or 
                          skillConfig.beSkillType == BESKILL_TYPE4 or 
                          skillConfig.beSkillType == BESKILL_TYPE44 then

				if CombatImpl.isSkillFrameEmpty() then
	                CombatImpl.addFrame()
					CombatImpl.setSkillID(attacker, data[1]) 
				end
				CombatImpl.setSaySkill(attacker.pos,skillConfig.id,skillConfig.contentType)
            end
		end
		CombatImpl.addBeSkillPosUse(attacker.pos,data[1])
	else 
		-- print("[beskill - use] 直接返回了1  id =  "..data[1])
	end

end

function onHit(attacker,targets,skillID)
	if attacker.isPet or attacker.isElf then
		return
	end

	--local useSkillConfig = SkillExcel[skillID]
	local useSkillConfig = Skill.GetSkillConfig(skillID)
	if useSkillConfig.type == CombatDefine.SKILL_TYPE2 then	
		--[[for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
			local obj = CombatImpl.objList[pos]
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE13] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE13]) do
					use(obj,targets,v)
				end
			end
		end]]
		if attacker.beSkillList[BESKILL_TYPE34] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE34]) do
				--local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])

				local beSkillArgs = skillConfig.beSkillArgs
				if beSkillArgs[1] == "attackerBuffCmd" and beSkillArgs[2][1] == "shayi" then
					local shayiCnt = CombatBuff.getBuffCnt(attacker,"shayi")
					if shayiCnt > 0 then
						for i = 1,shayiCnt do
							use(attacker,targets,v,true)
							CombatImpl.instantFrameBuffer(attacker,v[1])
						end
					end
					CombatBuff.delShayi(attacker)
				elseif beSkillArgs[1] == "qusanNeedCmd" then
					local canUse = false
					for h,need in ipairs(beSkillArgs[2]) do
						if need == useSkillConfig.cmd then
							canUse = true
							break
						end
					end
					if canUse == true then
						use(attacker,targets,v,true)
					end
				elseif beSkillArgs[1] == "jiekongNeedCmd" then
					local canUse = false
					for h,need in ipairs(beSkillArgs[2]) do
						if need == useSkillConfig.cmd then
							canUse = true
							break
						end
					end
					if canUse == true then
						use(attacker,targets,v,true)
					end
				elseif beSkillArgs[1] == "skillID" then
					local canUse = false
					for _, targetSkill in ipairs(beSkillArgs[2]) do
						if skillID == targetSkill then
							canUse = true
							break
						end
					end
					if canUse == true then
						use(attacker,targets,v,true)
					end
				else
					use(attacker,targets,v)
				end
			end
		end
	end
	--非攻击性主动技能不处理被击被动技能
	if CombatObj.getValue(attacker,RoleDefine.HURT_COMBAT_NOW) <= 0 then
		if (useSkillConfig.type == CombatDefine.SKILL_TYPE1 or useSkillConfig.type == CombatDefine.SKILL_TYPE2) then
			if  attacker.beSkillList[BESKILL_TYPE39] then
				for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE39]) do
					use(attacker,targets,v)
				end
			end

			if attacker.beSkillList[BESKILL_TYPE70] then
				for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE70]) do
					use(attacker,targets,v)
				end
			end
		end	
		return
	end

	if useSkillConfig.type == CombatDefine.SKILL_TYPE1 and attacker.beSkillList[BESKILL_TYPE1] then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE1]) do
			use(attacker,targets,v)
		end
	end

    if useSkillConfig.type == CombatDefine.SKILL_TYPE2 and attacker.beSkillList[BESKILL_TYPE61] then
        local canUseSkill = true
    	for index = 1, #targets do
		    local target = targets[index]
            if target.hp <=0 then
               canUseSkill = false
            end
        end

        if canUseSkill then
		   for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE61]) do
                --local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])

				local beSkillArgs = skillConfig.beSkillArgs
                if beSkillArgs[1] == "skillID" then
                   local canUse = false
				   for _,v in ipairs(beSkillArgs[2]) do
				   	  if skillID == v then
				   	  	 canUse = true
				   	  	 break
				   	  end
				   end
                   if canUse then
                      use(attacker,{},v)
                   end
                end
		   end
        end
	end

    

	if (useSkillConfig.type == CombatDefine.SKILL_TYPE1 or useSkillConfig.type == CombatDefine.SKILL_TYPE2 or useSkillConfig.type == CombatDefine.SKILL_TYPE3) then
		if  attacker.beSkillList[BESKILL_TYPE39] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE39]) do
				use(attacker,targets,v)
			end
		end

		if attacker.beSkillList[BESKILL_TYPE46] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE46]) do
				use(attacker,targets,v)
			end
		end

		if attacker.beSkillList[BESKILL_TYPE70] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE70]) do
				use(attacker,targets,v)
			end
		end
	end	

	local isDefendSideGedang = false
	local baojiDefender = {}
	
	for _,target in ipairs(targets) do
		if target.beSkillList[BESKILL_TYPE2] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE2]) do
				use(target,{attacker},v)
				-- print("當前受到攻擊,觸發被動技能11111111111111ssssssssss1111111111111111",skillID)
				--table.print_lua_table(v)
			end
		end

        -- 友军受击
        for _,pos in ipairs(CombatDefine.SIDE2POS[target.side]) do
           local obj = CombatImpl.objList[pos]
           if obj and obj.pos ~= target.pos and obj.beSkillList[BESKILL_TYPE52] then
			  for _,v in ipairs(obj.beSkillList[BESKILL_TYPE52]) do
			  	 use(target,{attacker},v)	
			  end
		   end
        end
         
		if useSkillConfig.type == CombatDefine.SKILL_TYPE2 and target.beSkillList[BESKILL_TYPE5] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE5]) do
				use(target,{attacker},v)	
			end
		end
		
		if target.isGedang then
			isDefendSideGedang = true
			if target.beSkillList[BESKILL_TYPE8] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE8]) do
					use(target,{attacker},v)	
				end
			end
			
			if attacker.beSkillList[BESKILL_TYPE6] then
				for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE6]) do
					use(attacker,{target},v)	
				end
			end
			
			--目标触发格挡次数			
			if target.beSkillList[BESKILL_TYPE28] then
				for _, v in ipairs(target.beSkillList[BESKILL_TYPE28]) do
					--local skillConfig = SkillExcel[v[1]]
					local skillConfig = Skill.GetSkillConfig(v[1])

					local beSkillArgs = skillConfig.beSkillArgs
					if beSkillArgs[1] == "gedang" then
                        target.isGedangCnt = target.isGedangCnt or 0
						local geDangLimit = beSkillArgs[2]
						if target.isGedangCnt >= geDangLimit then
						   target.isGedangCnt = 0
						   use(target,{attacker},v)
						end	
					end
				end
            end
		end
		
		local chanRao = CombatBuff.isChanRao(target)
		if  chanRao then
			if target.beSkillList[BESKILL_TYPE29]  then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE29]) do
					use(target,{attacker},v)	
				end
			end
		end
		
		if target.isBaoji then
			if target.beSkillList[BESKILL_TYPE7]  then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE7]) do
					use(target,{attacker},v)	
				end
			end
			baojiDefender[#baojiDefender+1] = target
		end
		
		-- if target.buffCmd["winner_nuhuo"] then
		-- 	local nIndex = target.buffCmd["winner_nuhuo"][1]
		-- 	local tBuffData = target.buffer[nIndex]
		-- 	if tBuffData.cnt < 10 then
		-- 		tBuffData.cnt = tBuffData.cnt + 1
		-- 	end
		-- end
	end

	local side = attacker.side
	local defendSide = side == CombatDefine.ATTACK_SIDE  and CombatDefine.DEFEND_SIDE  or CombatDefine.ATTACK_SIDE


	local target = targets[1]
	if target and target.side == defendSide and CombatObj.getValue(attacker,RoleDefine.HURT_COMBAT_NOW) > 0 then
		if attacker.beSkillList[BESKILL_TYPE73] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE73]) do
				use(attacker,targets,v)
			end
		end
	end

	
    --普通技能触发
	if useSkillConfig.type == CombatDefine.SKILL_TYPE1 then
        for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
           local obj = CombatImpl.objList[pos]
           if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE53] then
			  for _,v in ipairs(obj.beSkillList[BESKILL_TYPE53]) do
			  	 use(attacker,targets,v)	
			  end
		   end
        end
	end
	--主动技能触发
	if useSkillConfig.type == CombatDefine.SKILL_TYPE2 then	
		for _,pos in ipairs(CombatDefine.SIDE2POS[0]) do
			local obj = CombatImpl.objList[pos]
			local objSide = pos <= 6 and CombatDefine.ATTACK_SIDE or CombatDefine.DEFEND_SIDE
			--触发自己这方的
			if side == objSide then
				if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE13] then
					for _,v in ipairs(obj.beSkillList[BESKILL_TYPE13]) do
						use(obj,targets,v)
					end
				end
            end 
		
			--触发任意一方
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE31] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE31]) do
					use(obj,targets,v)
				end
			end
		end	
	end

    if not attacker.isBaoji then
       --指定技能没有暴击
        if attacker.beSkillList[BESKILL_TYPE59] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE59]) do
                --local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])

				local beSkillArgs = skillConfig.beSkillArgs
                if beSkillArgs[1] == "skillID" then
                   local canUse = false
				   for _,v in ipairs(beSkillArgs[2]) do
				   	  if skillID == v then
				   	  	 canUse = true
				   	  	 break
				   	  end
				   end
                   if canUse then
                      use(attacker,{},v)
                   end
                end
			end
		end
    end

	if attacker.isBaoji then
        --普通暴击技能触发
	    if useSkillConfig.type == CombatDefine.SKILL_TYPE1 then
            for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
               local obj = CombatImpl.objList[pos]
               if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE65] then
	    		  for _,v in ipairs(obj.beSkillList[BESKILL_TYPE65]) do
	    		  	 use(attacker,targets,v)	
	    		  end
	    	   end
            end
	    end

		if attacker.beSkillList[BESKILL_TYPE4] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE4]) do
				use(attacker,baojiDefender,v)
			end
		end

		if attacker.beSkillList[BESKILL_TYPE44] and CombatImpl.isFanji then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE44]) do
				use(attacker,baojiDefender,v)
			end
		end

        --指定技能暴击
        if attacker.beSkillList[BESKILL_TYPE58] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE58]) do
                --local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])

				local beSkillArgs = skillConfig.beSkillArgs
                if beSkillArgs[1] == "skillID" then
                   local canUse = false
				   for _,v in ipairs(beSkillArgs[2]) do
				   	  if skillID == v then
				   	  	 canUse = true
				   	  	 break
				   	  end
				   end
                   if canUse then
                      use(attacker,baojiDefender,v)
                   end
                end
			end
		end

		for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
			local obj = CombatImpl.objList[pos]
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE14] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE14]) do
					use(obj,targets,v)
				end
			end
		end
	end

	if isDefendSideGedang then	
		for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
			local obj = CombatImpl.objList[pos]
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE9] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE9]) do
					use(obj,targets,v)
				end
			end
		end
	end

	comboCheck(attacker, side)
end



function onHp(obj, hurt, attackPos)
	if obj.beSkillList[BESKILL_TYPE3] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE3]) do
			use(obj,{},v)
		end
	end

    if obj.beSkillList[BESKILL_TYPE67] then
	   for _,v in ipairs(obj.beSkillList[BESKILL_TYPE67]) do
	   	   use(obj,{},v)
	   end
	end

	local attacker = CombatImpl.objList[attackPos]
	if attacker and attacker.beSkillList[BESKILL_TYPE30] and obj.pos ~= attackPos then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE30]) do
			--local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])

			local beSkillArgs = skillConfig.beSkillArgs
            obj.paramData.isbeSkillByHp =  obj.paramData.isbeSkillByHp or 0		
			if beSkillArgs[1] then
				if beSkillArgs[1] == "tohp" then
					local hp = obj.hp
					local hpMax = CombatObj.getHpMax(obj)
					if hp > hpMax * beSkillArgs[2]/10000 then
						obj.paramData.isbeSkillByHp = 0	--恢复到上限						
					else
                        if obj.isbeSkillByHp == 0 then						
						    obj.paramData.isbeSkillByHp = 1 --达到下限
						end
					end
				elseif beSkillArgs[1] == "hurthpper" then
					local hpMax = CombatObj.getHpMax(obj)
					local percent = -hurt/hpMax
					if percent > beSkillArgs[2]/10000  then -- 伤害超过目标生命值上限X%时			
						use(attacker,{obj},v)	
					end
				elseif beSkillArgs[1] == "speed" then
					local atkSpeed = CombatImpl.getValue(attacker,RoleDefine.SPEED)
					local defSpeed = CombatImpl.getValue(obj,RoleDefine.SPEED)
					if atkSpeed > defSpeed then
						use(attacker,{obj},v)
					end
				end
			end
			
            --使用技能			
            if obj.paramData.isbeSkillByHp == 1 then
                use(attacker,{obj},v)	
				obj.paramData.isbeSkillByHp = 2
			end
		end
	end
end


-- 选择合适的复活技能
function selectReviceSkill(obj, attacker)
   local reviveSkillList = {}
   local len = 0
   for k, v in ipairs(obj.beSkillList[BESKILL_TYPE15]) do
      local skillID = v[1]
      local isFuwenSkill = v[5]
      --local skillConfig = SkillExcel[skillID]
	  local skillConfig = Skill.GetSkillConfig(skillID)
      if skillConfig.cmd == "revive" then
         --有可使用的主动复活技能
      	 local state = isCanRevive(obj, attacker, v)
		 if state and isFuwenSkill == 0 then
            if checkLimit(v) then
                len = len + 1
                reviveSkillList[k] = 1
                break
            end
         end 
      end
   end

   -- 没有再取符文技能的复活
   if len == 0 then
      for k, v in ipairs(obj.beSkillList[BESKILL_TYPE15]) do
         local skillID = v[1]
         local isFuwenSkill = v[5]
         --local skillConfig = SkillExcel[skillID]
		 local skillConfig = Skill.GetSkillConfig(skillID)
         if skillConfig.cmd == "revive" then
         	local state = isCanRevive(obj, attacker, v)
	   	    if state and isFuwenSkill == 1 then
               --被动技能次数限制判断
               if checkLimit(v) then
                  reviveSkillList[k] = 1
                  break
               end
            end 
         end
      end
   end

   return reviveSkillList
end



function onDie(obj, hurt, attackPos)
	if obj.hp > 0 then 
		return
	end

	local side = obj.side
	local attacker = CombatImpl.objList[attackPos]
	if obj.dieSkill then
		if attacker and attacker.beSkillList[BESKILL_TYPE43] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE43]) do
				--local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])
				for _,v1 in ipairs(skillConfig.beSkillArgs) do
					if  v1 == obj.dieSkill then
						use(attacker,{obj},v)
						break
					end					
				end

			end
		end
	end
	

	local defendSide = side == CombatDefine.ATTACK_SIDE and CombatDefine.DEFEND_SIDE or CombatDefine.ATTACK_SIDE
	for _,posData in ipairs(posSortBySpeed[side]) do
		local pos = posData[1]
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 then
			if target.beSkillList[BESKILL_TYPE16] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE16]) do
					use(target,{obj},v)
				end
			end
			if target.beSkillList[BESKILL_TYPE18] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE18]) do
					use(target,{obj},v)
				end
			end
		end
	end

	for _,posData in ipairs(posSortBySpeed[defendSide]) do
		local pos = posData[1]	
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 then
			if target.beSkillList[BESKILL_TYPE17] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE17]) do
					use(target,{obj},v)
				end
			end
			if target.beSkillList[BESKILL_TYPE18] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE18]) do
					use(target,{obj},v)
				end
			end
		end
	end	
	
	if attacker and attacker.beSkillList and attacker.beSkillList[BESKILL_TYPE30] and obj.pos ~= attackPos then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE30]) do
			if obj.isbeSkillByHp == nil or 
			   obj.isbeSkillByHp == 0 then
				use(attacker,{},v)
			end
		end
	end

	if attacker and attacker.beSkillList and attacker.beSkillList[BESKILL_TYPE35] then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE35]) do
			use(attacker,{obj},v)
		end
	end

	if obj.beSkillList[BESKILL_TYPE15] then
        -- 选择其中一个使用
        local reviceSkillList = selectReviceSkill(obj, attacker)
		for k, v in ipairs(obj.beSkillList[BESKILL_TYPE15]) do
            local skillID = v[1]
            --local skillConfig = SkillExcel[skillID]
			local skillConfig = Skill.GetSkillConfig(skillID)
            if skillConfig.cmd == "revive" then
                if reviceSkillList[k] then
                   use(obj,{},v)
                --    break
                end
            else
                use(obj,{},v)
            end		
		end	
	end
	
    	   
    for _,pos in ipairs(CombatDefine.SIDE2POS[defendSide]) do
	   local defendobj = CombatImpl.objList[pos]
       if defendobj and defendobj.hp > 0 and defendobj.beSkillList[BESKILL_TYPE66] then
          for _,v in ipairs(defendobj.beSkillList[BESKILL_TYPE66]) do
             --local skillConfig = SkillExcel[v[1]]
			 local skillConfig = Skill.GetSkillConfig(v[1])
             if skillConfig and skillConfig.otherArgs.tdBuffCmd then
                if obj.bufferCmd[skillConfig.otherArgs.tdBuffCmd] then
                   use(defendobj,{},v)
                end
             end
          end
       end
    end
end

function onAddBuffer(obj,buffer)
	--local conf = BufferExcel[buffer.id]
	local conf = CombatBuff.GetBuffConfig(buffer.id)
	local attacker = CombatImpl.objList[buffer.attackPos]

	if not attacker then -- 非英雄
		return
	end

	local cmd = conf.cmd
	if cmd == "hurt" or cmd == "liuxue"or cmd == "liuxue2" or cmd == "zhongdu" or cmd == "ranshao" then
		if obj.beSkillList[BESKILL_TYPE10] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE10]) do
				use(obj,{attacker},v)
			end
		end	
	end 

    if cmd == "bingdong" or cmd == "jiansu" then
        for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE63] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE63]) do
				   use(target,{obj},v)
				end		
			end	
		end
	end
	
	if CombatBuff.isControlCmd(cmd) then
		if obj.beSkillList[BESKILL_TYPE11] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE11]) do
				use(obj,{attacker},v)
			end
		end
        
        if obj.beSkillList[BESKILL_TYPE67] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE67]) do
				use(obj,{attacker},v, true, true)
			end
		end		
	end
	
	if cmd == "ranshao" then
		for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE12] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE12]) do
					local beSkillUseCnt = CombatImpl.getBeSkillUse(target,obj.pos,v[1])--特殊条件特殊处理:同一英雄在同一时间被施加多次燃烧状态时只触发一次
					if beSkillUseCnt == 0 then
						use(target,{obj},v)
						CombatImpl.addBeSkillUse(target,obj.pos,v[1])
					end
				end		
			end	
		end
	end

    if cmd == "zuzhou" then
        for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE51] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE51]) do
				   use(target,{obj},v)
				end		
			end	
		end
	end

	if cmd == "revive" then
		if obj.beSkillList[BESKILL_TYPE33] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE33]) do
				use(obj,{attacker},v)
			end
		end	
	end	

end


function onDelBingDongBuffer(obj, buffer)
	--local conf = BufferExcel[buffer.id]
	local conf = CombatBuff.GetBuffConfig(buffer.id)
	local attacker = CombatImpl.objList[buffer.attackPos]
	if not attacker then
		return
	end
    if conf.cmd == "bingdong" then
        for _, pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE64] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE64]) do
				   use(target,{obj},v)
				end		
			end	
		end
	end
end

--援军上阵触发
function onBackupBegin(obj)
    if not obj then return end
    if CombatDefine.isBackup(obj) ~= CombatDefine.BACKUP_TYPE2 then return end

    -- 备份的被动技能上阵
    if obj.backUpSkillList then
       obj.beSkillList = obj.backUpSkillList
       obj.backUpSkillList = nil
    end

    -- 出站计算属性
   	if obj.beSkillList[BESKILL_TYPE26] then
	    for _,v in ipairs(obj.beSkillList[BESKILL_TYPE26]) do
	    	 if canUse(obj,v) then
	    	 	--local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])
	    	 	if skillConfig.cmd == "attr" then
	    	 		for _,v1 in ipairs(skillConfig.args) do
	    	 			obj.sysAttr[v1[1]] = obj.sysAttr[v1[1]] +v1[2]
	    	 		end
	    	 		obj.isSysAttrChange = true
	    	 	elseif skillConfig.cmd == "normalTarget" then
	    	 		use(obj,{},v)
	    	 	end
	    	 end
	    end
	end
    table.sort(posSortBySpeed[CombatDefine.ATTACK_SIDE],posSpeedCmp)
	table.sort(posSortBySpeed[CombatDefine.DEFEND_SIDE],posSpeedCmp)

    --援军出战触发
	if obj and obj.beSkillList[BESKILL_TYPE32] then
		for k,v in ipairs(obj.beSkillList[BESKILL_TYPE32]) do
			use(obj,{},v)
		end
	end

    --上场技能
    CombatDefine.BACKUP_BUFF_SKILL[2] = 1
    use(obj,{}, CombatDefine.BACKUP_BUFF_SKILL)	
end


function onRoundBegin(round)
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE19] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE19]) do
				--table.print_lua_table(v)
				-- print("beskill = onRoundBegin 开始使用技能 ID = "..v[1])
				use(target,{},v)
			end
		end
        
        -- 生命值触发
        if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE62] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE62]) do
            	--local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])
			    local beSkillArgs = skillConfig.beSkillArgs

                if beSkillArgs[1] == 1 then
                   local hpMax = CombatObj.getHpMax(target)
                   local percent = target.hp/hpMax
                   if percent > beSkillArgs[2]/10000 then
                      use(target,{},v)
                   end
                end

                if beSkillArgs[1] == 2 then
                   local hpMax = CombatObj.getHpMax(target)
                   local percent = target.hp/hpMax
                   if percent < beSkillArgs[2]/10000 then
                      use(target,{},v)
                   end
                end
			end
		end
	end

 
    for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE47] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE47]) do
                --local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])
				if skillConfig.cmd == "attr" then
					for _,v1 in ipairs(skillConfig.args) do
						target.sysAttr[v1[1]] = target.sysAttr[v1[1]] +v1[2]
					end
                    CombatObj.calcAttr(target)
	                CombatImpl.addFrame()
					CombatImpl.setSkillID(target, v[1])
                    CombatImpl.setSaySkill(target.pos,skillConfig.id,skillConfig.contentType)
                end
			end
		end		
	end
	
	if round and round == 1 then
		for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
			local obj = CombatImpl.objList[pos]
			if obj and obj.beSkillList[BESKILL_TYPE32] then
				for k,v in ipairs(obj.beSkillList[BESKILL_TYPE32]) do
					use(obj,{},v)
				end
			end
		end
	end

	-- 在最后加
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE71] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE71]) do
				-- print("[onRoundBegin] 开始加类型71的被动技能 pos = "..pos)
				local skillConfig = Skill.GetSkillConfig(v[1])
				if skillConfig and skillConfig.args then
					-- local nLen = skillConfig.args[1]
					-- for i = 1, nLen, 1 do
						use(target,{},v)
					-- end
				end
			end
		end
	end
end

function beforeFanji(obj, hitObj, isEnd)
	if not obj or obj.isPet or obj.isElf then
		return 
	end

	if obj and obj.hurtType == CombatDefine.PHY_HURT_TYPE then
		if obj and obj.beSkillList and obj.beSkillList[BESKILL_TYPE37] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE37]) do
				-- use(obj,{},v)
				use(obj, {}, v, nil, nil, hitObj)
			end
		end
	else
		if obj and obj.beSkillList and obj.beSkillList[BESKILL_TYPE38] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE38]) do
				use(obj,{},v)
			end
		end
	end
end

function onRoundEnd(isRoundEnd)
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj and obj.beSkillList[BESKILL_TYPE36] then
			if pos == 1 then
			end
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE36]) do
				use(obj,{},v)
			end
		end
	end	

	if isRoundEnd then
		for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE20] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE20]) do
					use(target,{},v)
				end
			end
		end
	end
end

function posSpeedCmp(a,b)
	if a[2] > b[2] then
		return true
	elseif a[2] < b[2] then
		return false
	else
		return a[1] < b[1]
	end
end

-- 计算遗物特殊属性部分
local function BeSkill_RelicAttribute(ReleaserObj, tSkillInfo)
	local nSkillID = tSkillInfo[1]
	local tSkilConfig = Skill.GetSkillConfig(nSkillID)
	if not tSkilConfig then
		-- print("[BeSkill_RelicAttribute] 不存在技能配置 nSkillID = "..nSkillID)
		return
	end

	local winnerID = ReleaserObj and ReleaserObj.relic and ReleaserObj.relic.relicID or 0
	local winnerStar = ReleaserObj and ReleaserObj.relic and ReleaserObj.relic.star or 0

	if 0 >= winnerID or 0 >= winnerStar then
		return
	end

	-- 特殊处理防御为0, 加标识
	if tSkilConfig.otherArgs.selefDefense then
		ReleaserObj.relicAttr.bSpecialDef = true
	end

	-- 是否增加args属性
	local bAddArgs = true
	if tSkilConfig.otherArgs.winnerfiveraces then
		bAddArgs = false
	end

	local targets = TargetMode.getTargets(ReleaserObj, tSkilConfig.targetMode)
	if #targets > 0 and true == bAddArgs then
		for _, obj in ipairs(targets) do
			-- 加固定值属性
			for _, v in ipairs(tSkilConfig.args) do
				if not obj.relicAttr.FixedAttr[v[1]] then
					obj.relicAttr.FixedAttr[v[1]] = 0
				end

				obj.relicAttr.FixedAttr[v[1]] = obj.relicAttr.FixedAttr[v[1]] + v[2]
			end
		end
	else
		-- print("[BeSkill_RelicAttribute] 获取不到对应的技能目标对象")
	end

	local nSide = ReleaserObj.side
	local tCamp = {}

	-- 检查
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj and obj.side and nSide == obj.side then
			local nCamp = obj.camp
			if nCamp then
				tCamp[nCamp] = 1
			end
		end
	end

	local bAllCamp = true
	for i = CombatDefine.CAMP_TYPE1, CombatDefine.CAMP_TYPE5, 1 do
		if not tCamp[i] then
			bAllCamp = false
			break
		end
	end

	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj and obj.side and nSide == obj.side then
			-- 处理百分比值
			-- 治疗
			if tSkilConfig.otherArgs.jobZhiliao then
				--table.print_lua_table(tSkilConfig.otherArgs.jobZhiliao)
				if CombatBuff.isJob(obj, {CombatDefine.JOB_TYPE4}) then
					-- local extraRate = WinnerRelicLogic.WinnerRelic_GetRateAttack(winnerID, winnerStar)
					if not obj.relicAttr.ProAttr[RoleDefine.ZAOCHENG_HP_ADD_RATE] then
						obj.relicAttr.ProAttr[RoleDefine.ZAOCHENG_HP_ADD_RATE] = 0
					end
					local nValue = tSkilConfig.otherArgs.jobZhiliao[2]
					-- print("BeSkill_RelicAttribute 治療 nValue = ".. nValue)

					obj.relicAttr.ProAttr[RoleDefine.ZAOCHENG_HP_ADD_RATE] = obj.relicAttr.ProAttr[RoleDefine.ZAOCHENG_HP_ADD_RATE] + nValue
					if obj.relicAttr.ProAttr[RoleDefine.ZAOCHENG_HP_ADD_RATE]  then
						-- print("[BeSkill_RelicAttribute] 治療加成 為 nNewValue  = "..obj.relicAttr.ProAttr[RoleDefine.ZAOCHENG_HP_ADD_RATE] )
					else
						-- print("[BeSkill_RelicAttribute] 不存在 nNewValue" )
					end
				end
			end

			-- 免伤 百分比
			if tSkilConfig.otherArgs.jobMianshang then
				if CombatBuff.isJob(obj, {CombatDefine.JOB_TYPE1}) then
					--local extraRate = WinnerRelicLogic.WinnerRelic_GetRateDef(winnerID, winnerStar)
					if not obj.relicAttr.ProAttr[RoleDefine.JIANSHANG_RATE] then
						obj.relicAttr.ProAttr[RoleDefine.JIANSHANG_RATE] = 0
					end
					local nValue = tSkilConfig.otherArgs.jobMianshang[2]
					-- print("BeSkill_RelicAttribute 免伤 nValue = ".. nValue)

					obj.relicAttr.ProAttr[RoleDefine.JIANSHANG_RATE] = obj.relicAttr.ProAttr[RoleDefine.JIANSHANG_RATE] + nValue
					-- print("[BeSkill_RelicAttribute] 免傷加成 為 nValue = "..nValue.." nNewValue = "..obj.relicAttr.ProAttr[RoleDefine.JIANSHANG_RATE])
				end
			end

			-- 防御增加固定值
			if tSkilConfig.otherArgs.jobDefense then
				if CombatBuff.isJob(obj, {CombatDefine.JOB_TYPE4, CombatDefine.JOB_TYPE1}) then
					if winnerID > 0 and  winnerStar > 0 then
						local extraRate = WinnerRelicLogic.WinnerRelic_GetRateAttack(winnerID, winnerStar)
						if not obj.relicAttr.FixedAttr[RoleDefine.DEF] then
							obj.relicAttr.FixedAttr[RoleDefine.DEF] = 0
						end

						obj.relicAttr.FixedAttr[RoleDefine.DEF] = obj.relicAttr.FixedAttr[RoleDefine.DEF] + extraRate
					end
				end
			end

			-- 攻击 百分比
			if tSkilConfig.otherArgs.racialAtk then
				if CombatBuff.isCamp(obj, {CombatDefine.COMBAT_TYPE27}) then
					local extraRate = WinnerRelicLogic.WinnerRelic_GetRateAttack(winnerID, winnerStar)
					if not obj.relicAttr.ProAttr[RoleDefine.ATK] then
						obj.relicAttr.ProAttr[RoleDefine.ATK] = 0
					end

					obj.relicAttr.ProAttr[RoleDefine.ATK] = obj.relicAttr.ProAttr[RoleDefine.ATK] + extraRate
				end
			end

			-- 上阵阵容为全职业，加固定值
			if tSkilConfig.otherArgs.winnerfiveraces and bAllCamp == true then
				for _, v in ipairs(tSkilConfig.args) do
					if not obj.relicAttr.FixedAttr[v[1]] then
						obj.relicAttr.FixedAttr[v[1]] = 0
					end
	
					obj.relicAttr.FixedAttr[v[1]] = obj.relicAttr.FixedAttr[v[1]] + v[2]
				end
			end
		end
	end
end

function onFightBegin()
	posSortBySpeed[CombatDefine.ATTACK_SIDE] = {}
	posSortBySpeed[CombatDefine.DEFEND_SIDE] = {}
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj then
			obj.hp = obj.hp and obj.hp or 1
		end
	end
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj then
			if obj.beSkillList[BESKILL_TYPE26] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE26]) do
					if canUse(obj,v) then
						--local skillConfig = SkillExcel[v[1]]
						local skillConfig = Skill.GetSkillConfig(v[1])
						if skillConfig.cmd == "attr" then
							for _,v1 in ipairs(skillConfig.args) do
								obj.sysAttr[v1[1]] = obj.sysAttr[v1[1]] +v1[2]
							end
							obj.isSysAttrChange = true
						elseif skillConfig.cmd == "normalTarget"  then
							use(obj,{},v)
						elseif skillConfig.cmd == "winnerRelicSkill"  then
							use(obj,{},v)

							BeSkill_RelicAttribute(obj, v)
						end
					end
				end
			end
			if pos <= CombatDefine.COMBAT_HERO_CNT then
				posSortBySpeed[CombatDefine.ATTACK_SIDE][#posSortBySpeed[CombatDefine.ATTACK_SIDE]+1] = {pos,obj.attr and obj.attr[RoleDefine.SPEED] or 0}
			elseif pos <= CombatDefine.COMBAT_HERO_CNT * 2 then
				posSortBySpeed[CombatDefine.DEFEND_SIDE][#posSortBySpeed[CombatDefine.DEFEND_SIDE]+1] = {pos,obj.attr and obj.attr[RoleDefine.SPEED] or 0}
			end
		end
	end
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj then
			obj.hp = nil
		end
	end
	table.sort(posSortBySpeed[CombatDefine.ATTACK_SIDE],posSpeedCmp)
	table.sort(posSortBySpeed[CombatDefine.DEFEND_SIDE],posSpeedCmp)
end

function onCombatPetBe(round)
	if round <= 1 then
		return
	end
	for side = 1,CombatDefine.DEFEND_SIDE do
		local pet = CombatImpl.petList[side] 
		if pet and pet.petID and pet.beSkillList[BESKILL_TYPE27] then
			for _,v in ipairs(pet.beSkillList[BESKILL_TYPE27]) do
				--local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])
				if skillConfig.cmd == "mp" then
					use(pet,{},v)
				end
			end
		end
	end
end

-- 计算被动技能属性
local beSkillIDList = {}
function doCalcHero(obj, attrs, skinSkillID, heroConf)
	for i = 1, #beSkillIDList do
		beSkillIDList[i] = nil
	end		
	heroConf = heroConf or HeroDefine.getAttrConfig(obj.id, obj.star)
	local beSkillIDs = heroConf.beSkillList
	local skinSkillConf = skinSkillID and SkinSkillExcel[skinSkillID] 
	if skinSkillConf then
		beSkillIDs = skinSkillConf.beSkillList
	end

	local weapSkillList = HeroExclusiveWeapon.GetweaponEffectIdList(obj)

	for _, skillID in ipairs(beSkillIDs) do
		--local skillConfig = SkillExcel[skillID]
		skillID = weapSkillList and weapSkillList[skillID] or skillID
		local skillConfig = Skill.GetSkillConfig(skillID)
		if skillConfig.beSkillType == BESKILL_TYPE24 and 
			((not obj) or (obj.quality >= skillConfig.need)) then
			for _,v in ipairs(skillConfig.args) do
				beSkillIDList[#beSkillIDList + 1] = v
			end
		else
			beSkillIDList[#beSkillIDList + 1] = skillID
		end
	end
	for _, skillID in ipairs(beSkillIDList) do
		--local skillConfig = SkillExcel[skillID]
		local skillConfig = Skill.GetSkillConfig(skillID)
		if skillConfig and skillConfig.cmd == "attr" and
			skillConfig.beSkillType == BESKILL_TYPE23 and 
			((not obj) or (obj.quality >= skillConfig.need)) then
			for _, v in ipairs(skillConfig.args) do
				RoleAttr.updateValue(v[1], v[2], attrs)
			end
		elseif skillConfig and skillConfig.cmd == "attrcomboAgain" and
		skillConfig.beSkillType == BESKILL_TYPE70 and 
		((not obj) or (obj.quality >= skillConfig.need))  then
			for i, v in ipairs(skillConfig.args) do
				if v[1] == 1 then
					RoleAttr.updateValue(v[2], v[3], attrs)
				end
			end
		end
	end	
end

function beforeHit(attacker,defender)
	attacker.beforeHitCheck = false
	if attacker.beSkillList[BESKILL_TYPE40] then
		local hpMax = CombatObj.getHpMax(defender)
		local hp = defender.hp
		local defendHpRate = hp*10000/hpMax
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE40]) do
			--local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "lowerLimit" then
				local args = skillConfig.args
				if defendHpRate < args[1] then
					attacker.beforeHitCheck = true
					attacker.beskill40 = attacker.beskill40 or {}
					attacker.beskill40[defender.pos] = 1
				end
			end
		end	
	end
end

function afterHit(attacker,target, isEnd)
	if attacker.beforeHitCheck then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE40]) do
			use(attacker,{target},v)
		end	
	end
	-- beforeFanji(attacker, isEnd)
	beforeFanji(attacker, target, isEnd)
end


function getOutAtkRate(attacker, defender)
    local outAtkRate = 0
    -- 首次攻击
    local isFirstAttack = CombatImpl.firstAttack[attacker.side]
    if isFirstAttack then
       for _,pos in ipairs(CombatDefine.SIDE2POS[defender.side]) do
          local objM = CombatImpl.objList[pos]
          if objM and objM.beSkillList[BESKILL_TYPE50] then
             for _,v in ipairs(objM.beSkillList[BESKILL_TYPE50]) do
                --local skillConfig = SkillExcel[v[1]]
				local skillConfig = Skill.GetSkillConfig(v[1])
                if skillConfig and skillConfig.otherArgs.outAtkRate then
                   if skillConfig.otherArgs.outAtkRate >= outAtkRate then
                      outAtkRate = skillConfig.otherArgs.outAtkRate
                   end
                end
             end
          end
       end 
    end

    return outAtkRate
end

function getOutAtkRate2(attacker, skillConfig)
	local outAtkRate = 0
	for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE72] or {})  do
		local config = Skill.GetSkillConfig(v[1])
		if config.cmd == "addHurtRateByRand" then
			local addHurtRate = 0
			for _, skillInfo in ipairs(config.beSkillArgs[2]) do
				if table.find(skillInfo[1], skillConfig.id) then
					addHurtRate = skillInfo[2]
					break
				end
			end

			local randVal = math.random(0, 10000)
			if randVal <= config.beSkillArgs[1] then
				outAtkRate = outAtkRate + addHurtRate
			end
		end
	end
	return outAtkRate
end



function getExtraHurtRate(attacker,defender,skillConfig)
	if not attacker or attacker.isPet then
		return 0
	end

	local zhanyiRet = 0	
	local bodongHurtRet = 0
	local zhenduiHurtRate = 0
	local zhenduiLen = 0
	local zhenduiList = {}
	local bezhenduiList = {}
	local hpHurtRate = 0
	local extraSpeedHurt = 0
	local firstHurt = 0
    local sexHurt = 0
    local campHurt = 0
    local reJobHurt = 0
	local buffAndHPSubHurt = 0

	for k,skillList in pairs(attacker.beSkillList)  do
		for j,v in pairs(skillList)  do
			--local config = SkillExcel[v[1]]
			local config = Skill.GetSkillConfig(v[1])
			if config.cmd == "zhanyi" then  --战意 伤害加成
				local nowHp = attacker.hp
				local hpMax = CombatObj.getHpMax(attacker) 
				local percent = math.floor( (hpMax - nowHp )/hpMax * 100)
				if percent >= 1 then				
					zhanyiRet = zhanyiRet + (percent * config.args[2])
				end	
			elseif config.cmd == "fuwenAKH" then
				for i = 1,#config.otherArgs do
					zhenduiLen = zhenduiLen + 1
					zhenduiList[config.otherArgs[i][1]] = zhenduiList[config.otherArgs[i][1]] or 0
					zhenduiList[config.otherArgs[i][1]] = zhenduiList[config.otherArgs[i][1]] + config.otherArgs[i][2]
				end			
			elseif config.cmd == "hurtBodong" then
				if canUse(attacker, v) then
					local minRet = config.args[1]
					local maxRet = config.args[2]
					local randomRet = math.random(minRet,  maxRet)
					bodongHurtRet = bodongHurtRet + randomRet - 10000					
				end
			elseif config.cmd == "hpExtraHurt" then
				local hp = defender.hp
				local beSkillArgs = config.beSkillArgs
				local checkHp = CombatObj.getHpMax(defender) * beSkillArgs[3] / 10000
				if beSkillArgs[2] == 1 and hp > checkHp then
					hpHurtRate = beSkillArgs[4]
				elseif  beSkillArgs[2] == 2 and hp < checkHp then
					hpHurtRate = beSkillArgs[4]
				end
			elseif config.cmd == "extraSpeedHurt" then
				extraSpeedHurt = config.args[1]
			elseif config.cmd == "firstAttackHurt" then
				if canUse(attacker, v) then
					firstHurt = config.args[1]
				end
			elseif config.cmd == "buffAndHPSub" then
				local nowHp = defender.hp
				local hpMax = CombatObj.getHpMax(defender)
				local percent = math.floor((hpMax - nowHp )/hpMax * 100) --已损失血量百分比

				local condCfg = config.args
				local condRate = condCfg[1]

				if percent >= condRate then
					local condStatusList = condCfg[2] or {}
					local objNowSatus = CombatBuff.getStatus(defender)

					for _, buffCmd in pairs(condStatusList) do
						if objNowSatus[buffCmd] then
							buffAndHPSubHurt = condCfg[3] or 0
							break
						end
					end
				end
			elseif config.cmd == "hpSubHurtAdd" then
				local nowHp = attacker.hp
				local hpMax = CombatObj.getHpMax(attacker)
				local percent = math.floor((hpMax - nowHp )/hpMax * 100) --已损失血量百分比
				local condCfg = config.beSkillArgs
				local condRate = condCfg[1]
				if percent >= condRate then
					hpHurtRate = hpHurtRate + condCfg[2]
				end
			end
		end
	end

	for k,skillList in pairs(defender.beSkillList)  do
		for j,v in pairs(skillList)  do
			--local conf = SkillExcel[v[1]]
			local conf = Skill.GetSkillConfig(v[1])
			if conf then
                if conf.cmd == "befuwenAKH" then
				   for i = 1,#conf.otherArgs do
				   	   zhenduiLen = zhenduiLen + 1
				   	   bezhenduiList[conf.otherArgs[i][1]] = bezhenduiList[conf.otherArgs[i][1]] or 0
				   	   bezhenduiList[conf.otherArgs[i][1]] = bezhenduiList[conf.otherArgs[i][1]] + conf.otherArgs[i][2]
				   end
                -- 受性别增伤
                elseif conf.cmd == "sexHurt" then
                   if attacker.sex == conf.args[1] then
                      sexHurt = sexHurt + conf.args[2]
                   end
                -- 受阵营增伤
                elseif conf.cmd == "campHurt" then
                   if attacker.camp == conf.args[1] then
                      campHurt = campHurt + conf.args[2]
                   end
                -- 受职业增伤
                elseif conf.cmd == "reJobHurt" then
                   if attacker.job == conf.args[1] then
                      reJobHurt = reJobHurt + conf.args[2]
                   end
                end
			end
		end
	end


	if zhenduiLen > 0 then
        local bAdd = 0
		for k,skillList in pairs(defender.beSkillList) do
			for j,v in pairs(skillList)  do
				--local conf = SkillExcel[v[1]]
				local conf = Skill.GetSkillConfig(v[1])
                local isFuWenSkill = v[5]	
				if conf.cmd and conf.cmd ~= "" and zhenduiList[conf.cmd] then
                    -- 符文技能只生效一个
                    if isFuWenSkill == 1 then
                       if bAdd == 0 then
                          zhenduiHurtRate = zhenduiHurtRate + zhenduiList[conf.cmd]
                          bAdd = 1
                       end
                    else
                       zhenduiHurtRate = zhenduiHurtRate + zhenduiList[conf.cmd]
                    end
				end
			end
		end

        bAdd = 0
		for k,skillList in pairs(attacker.beSkillList)  do
			for j,v in pairs(skillList)  do
				--local conf = SkillExcel[v[1]]
				local conf = Skill.GetSkillConfig(v[1])
                local isFuWenSkill = v[5]	
				if conf.cmd and conf.cmd ~= "" and bezhenduiList[conf.cmd] then
                    if isFuWenSkill == 1 then
                       if bAdd == 0 then
					      zhenduiHurtRate = zhenduiHurtRate + bezhenduiList[conf.cmd]
                          bAdd = 1
                       end	
                    else
                       zhenduiHurtRate = zhenduiHurtRate + bezhenduiList[conf.cmd]
                    end
				end	
			end	
		end
	end
    
    --特殊buff 对血族的加成
    local campHurtRate = 0
    local campRate = nil
    local buffCmd = nil
    local camp = nil
    for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
       obj = CombatImpl.objList[pos]
       if obj and obj.hp > 0 then
          if obj.beSkillList[BESKILL_TYPE68] then
             local beSkillList = obj.beSkillList[BESKILL_TYPE68][1]
             --local skillConfig = SkillExcel[beSkillList[1]]
			 local skillConfig = Skill.GetSkillConfig(beSkillList[1])
             if skillConfig then
                camp = skillConfig.otherArgs.calcCamp[1]
                buffCmd = skillConfig.otherArgs.calcCamp[2]
                campRate = skillConfig.otherArgs.calcCamp[3]
                break
             end
	      end
       end
    end
    
    if buffCmd and campRate and attacker.camp == camp then
       local campHurtCnt = 0
       for _,pos in ipairs(CombatDefine.SIDE2POS[defender.side]) do
          obj = CombatImpl.objList[pos]
          if obj and obj.hp > 0 then
	    	   local isStatus1, cmd1, ind1 = CombatBuff.isStatus(obj, buffCmd)
              if isStatus1 then
                 campHurtCnt = campHurtCnt + 1
              end
	       end
       end
       if campHurtCnt > 0 then
          campHurtRate = campHurtCnt * campRate   
       end
    end

	local extraHurtRate = zhanyiRet + zhenduiHurtRate + bodongHurtRet + hpHurtRate + extraSpeedHurt + firstHurt + sexHurt + campHurt + reJobHurt + campHurtRate + buffAndHPSubHurt
	if attacker.beSkillList[BESKILL_TYPE40] and attacker.beforeHitCheck then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE40]) do
			--local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "extraHurt" then
				local args = skillConfig.args
				extraHurtRate = extraHurtRate + args[1]
			end
		end	
	end

	return extraHurtRate
end

--
function getExtraHurt(attacker, target, tUseskillConfig)
    local extraHurt = 0
    if attacker.beSkillList[BESKILL_TYPE55] then
       local beSkillList = attacker.beSkillList[BESKILL_TYPE55][1]
       --local skillConfig = SkillExcel[beSkillList[1]]
	   local skillConfig = Skill.GetSkillConfig(beSkillList[1])
       local buffCmd = target.bufferCmd[skillConfig.otherArgs.buffCmdHurtRate[1]]
       local rate = skillConfig.otherArgs.buffCmdHurtRate[2] / 10000
       if buffCmd then
          local buffCnt = #buffCmd
          local buffIndex = buffCmd[buffCnt]
          local buffer = target.buffer[buffIndex]
          --local conf = BufferExcel[buffer.id]
		  local conf = CombatBuff.GetBuffConfig(buffer.id)
          local hurt = buffer.arg[1]
          extraHurt = extraHurt + hurt * rate * buffCnt
       end
    end

    return extraHurt
end

-- 根据杀气buff获得额外属性加成
function GetExtraAttrByShaQi(attacker, targetAttrId)
	local attrVal = 0
	if not attacker then
		return attrVal
	end

	-- for _, skillList in pairs(attacker.beSkillList) do
	-- 	for _, v in pairs(skillList)  do
	-- 		local config = Skill.GetSkillConfig(v[1])
	-- 		if config.cmd == "shaqi" then
	-- 			local buffNum = CombatBuff.getBuffCnt(attacker, config.cmd)
	-- 			if buffNum <= 0 then
	-- 				return attrVal
	-- 			end
			
	-- 			for _, attrTb in ipairs(config.args) do
	-- 				if attrTb[1] == targetAttrId then
	-- 					attrVal = (attrTb[2] or 0) * buffNum
	-- 					break
	-- 				end
	-- 			end
	-- 		end
	-- 	end
	-- end

	local buffNum = CombatBuff.getBuffCnt(attacker, "shaqi")
	if buffNum <= 0 then
		return attrVal
	end

	local attrArr
	for i=1, attacker.buffer[0] do
		local buffer = attacker.buffer[i]
		local conf = CombatBuff.GetBuffConfig(buffer.id)
		if conf.cmd == "shaqi" then
			attrArr = conf.args
			break
		end
	end

	if not attrArr then
		return attrVal
	end

	for _, attrTb in ipairs(attrArr) do
		if attrTb[1] == targetAttrId then
			attrVal = (attrTb[2] or 0) * buffNum
			break
		end
	end

	return attrVal
end


--
function getRandomBuffCmd(attacker, target, cmd)
    local side = attacker.side
    local mathRandom = math.random
    for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
	   local obj = CombatImpl.objList[pos]
       if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE56] then
          local beSkillList = obj.beSkillList[BESKILL_TYPE56][1]
          --local skillConfig = SkillExcel[beSkillList[1]]
		  local skillConfig = Skill.GetSkillConfig(beSkillList[1])
          local buffCmdRand = skillConfig.otherArgs.buffCmdRand
          if buffCmdRand and cmd == buffCmdRand[1] then 
             local random = mathRandom(1, 10000)
             if random <= buffCmdRand[2] then
                return true
             end
          end
       end
    end
end

function getHurtPercentRate(attacker, defender, hurt, tUseskillConfig)
	local rate = 0

	for k,skillList in pairs(defender.beSkillList)  do
		for j,v in pairs(skillList)  do
			--local conf = SkillExcel[v[1]]
			local conf = Skill.GetSkillConfig(v[1])
			if conf and conf.cmd == "behurtRet" then
				local canUse = true 
				if conf.beSkillArgs and #conf.beSkillArgs > 0 then
					if  conf.beSkillArgs[1] ==  "hurthpper" then
						local hpMax = CombatObj.getHpMax(defender)
						local percent = hurt/hpMax
						if percent < conf.beSkillArgs[2]/10000  then -- 伤害超过目标生命值上限X%时			
							canUse = false
						end	
					end
				end
				if canUse == true then
					local random = math.random(1, 10000)
					if random <= conf.args[2] then
						rate = rate + conf.args[1] 						
					end					
				end					
			end

		end
	end

	if tUseskillConfig.type == CombatDefine.SKILL_TYPE1 then
		if attacker.beSkillList[BESKILL_TYPE1] then
			for _, v in pairs(attacker.beSkillList[BESKILL_TYPE1]) do
				local nSkillID = v[1]
				--local skillConfig = SkillExcel[nSkillID]
				local skillConfig = Skill.GetSkillConfig(nSkillID)
				if skillConfig and skillConfig.hurtRate and skillConfig.hurtRate[1] then
					local fRate = skillConfig.hurtRate[1]
					rate = rate + fRate
					--print("[getExtraHurt] 获取到的普工伤害加成概率 fRate = "..fRate.." 总加成 rate = "..rate)
				end
			end

		end
	end
	
	return rate
end

function getBufferExtraHurt(attacker,bufferID,cmd)
	local rate = 0

	for _,skillList in pairs(attacker.beSkillList)  do
		for k,v in pairs(skillList)  do
			--local conf = SkillExcel[v[1]]
			local conf = Skill.GetSkillConfig(v[1])
			local otherArgs = conf.otherArgs
			if bufferID and otherArgs.bufferExtraHurt then
				for k1,v1 in ipairs(otherArgs.bufferExtraHurt[1]) do
					if bufferID == v1 then
						rate = rate + otherArgs.bufferExtraHurt[2]
					end
				end
			end
			if cmd and otherArgs.bufferCmdExtraHurt then
				for k1,v1 in ipairs(otherArgs.bufferCmdExtraHurt[1]) do
					if cmd == v1 then
						rate = rate + otherArgs.bufferCmdExtraHurt[2]
					end
				end
			end
		end
	end
	return rate
end

function getExtraHurtRateEx(attacker, defender)
	local rate = 0
    -- 死亡数伤害率
    if attacker.beSkillList[BESKILL_TYPE48] then
       for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE48]) do
          --local skillConfig = SkillExcel[v[1]]
		  local skillConfig = Skill.GetSkillConfig(v[1])
          if skillConfig and skillConfig.otherArgs.deathHurt then
             local side = defender.side
             for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
		         local obj = CombatImpl.objList[pos]
		         if obj and obj.hp <=0 then
                    if rate == 0 then
                       rate = skillConfig.otherArgs.deathHurt
                    else
                       rate = rate + 10000
                    end
		         end
	         end
          end
       end
    end

	return rate
end

--
function getBuffChixuHurt(attacker, cmd)
   if not attacker then return 0 end
   local hurt = 0
   local side = attacker.side
   for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
	   local objM = CombatImpl.objList[pos]
       if objM and objM.hp > 0 and objM.beSkillList[BESKILL_TYPE54] then
          for _,v in ipairs(objM.beSkillList[BESKILL_TYPE54]) do
             --local skillConfig = SkillExcel[v[1]]
			 local skillConfig = Skill.GetSkillConfig(v[1])
             if skillConfig and skillConfig.otherArgs[cmd] then
                hurt = hurt + skillConfig.otherArgs[cmd]
             end
          end
       end
   end
   return hurt / 10000
end

--
function handlerBuff(attacker, target, arg, cmd)
   if not attacker then return end

   for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
	   local obj = CombatImpl.objList[pos]
       if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE57] then
           for _,v in ipairs(obj.beSkillList[BESKILL_TYPE57]) do

              attacker.args = arg
              use(attacker, {target}, v, false)
           end
       end
   end
end


function beforeMinusHp(obj, value)
    if value <= 0 then return end
    --敌对阵营 有对象有 49号被动技能 加血变成减血
    local side = obj.side == CombatDefine.ATTACK_SIDE and CombatDefine.DEFEND_SIDE or CombatDefine.ATTACK_SIDE
    for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
	    local objM = CombatImpl.objList[pos]
        if objM and objM.hp > 0 and objM.beSkillList[BESKILL_TYPE49] then
	       value = value * (-1)
           break
        end
    end

    return value
end

function beforeUpdateHp(obj,sum)
	if sum <= 0 and obj.beSkillList[BESKILL_TYPE41] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE41]) do
			--local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "miansi" and canUse(obj,v) then
				use(obj,{},v)
				return true
			end
		end	
	end
end

function onCheckBuffer(obj,attacker,value)
	local ret = value
	if obj.beSkillList[BESKILL_TYPE42] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE42]) do
			--local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])
			if canUse(obj,v) then
                local newFrame = true
				if skillConfig.cmd == "baohu" then
					obj.baohuHurt = value * skillConfig.args[1] / 10000
					if obj.hp < -obj.baohuHurt then
						obj.baohuHurt = -obj.hp
					end
					ret = value - obj.baohuHurt
					newFrame = false
                end

                use(obj,{attacker},v, newFrame)
				break
			end
		end	
	end
	return ret
end

function getStatusBaoji(attacker, defender)
    local baoji = 0
    local firstBaoji = 0
    for k, skillList in pairs(attacker.beSkillList) do
        for j, v in pairs(skillList) do
            --local config = SkillExcel[v[1]]
			local config = Skill.GetSkillConfig(v[1])
            if config.cmd == "firstAttackHurt" then
                if canUse(attacker, v) then
                    firstBaoji = config.args[2] or 0
                end
            end
        end
    end

    if attacker.beSkillList[BESKILL_TYPE60] then
        for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE60]) do
            --local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])
            if skillConfig and skillConfig.otherArgs.addStatusBaoji then
                local status = CombatBuff.getStatus(defender)
                local isStatus = false
                for k, v in ipairs(skillConfig.otherArgs.addStatusBaoji[1]) do
                    if status[v] then
                        isStatus = true
                        break
                    end
                end
                if isStatus then
                    baoji = baoji + skillConfig.otherArgs.addStatusBaoji[2]
                end
            end
        end
    end

    if attacker.beSkillList[BESKILL_TYPE69] and attacker.notBaoJi then
        for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE69]) do
            --local skillConfig = SkillExcel[v[1]]
			local skillConfig = Skill.GetSkillConfig(v[1])
            if skillConfig and skillConfig.otherArgs.addNotBaoji then
                baoji = baoji + skillConfig.otherArgs.addNotBaoji
            end
        end
    end

    return baoji + firstBaoji
end

function getStatusPojia(attacker, defender)
   local poJia = 0
      -- 对指定状态破甲加成  走的技能状态属性
   if attacker.beSkillList[BESKILL_TYPE60] then
      for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE60]) do
          --local skillConfig = SkillExcel[v[1]]
		  local skillConfig = Skill.GetSkillConfig(v[1])
          if skillConfig.otherArgs.addStatusPoJia then
              local status = CombatBuff.getStatus(defender)
              local isStatus = false
              for k, v in ipairs(skillConfig.otherArgs.addStatusPoJia[1]) do
                  if status[v] then
                      isStatus = true
                      break
                  end
              end
              if isStatus then
                  poJia = poJia + skillConfig.otherArgs.addStatusPoJia[2]
              end
          end
      end
   end
   return poJia
end

-- 是否不会被反击
function isNoFanji(obj)
	if obj.beSkillList[BESKILL_TYPE72] then
		for _, v in ipairs(obj.beSkillList[BESKILL_TYPE72]) do
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "noFanji" then
				return true
			end
        end
	end
	return false
end

-- 获取与神威灵装"真伤附加"效果相对的"真伤减免"
function GetTrueDamegeOffset(obj)
	if obj.beSkillList[BESKILL_TYPE72] then
		for _, v in ipairs(obj.beSkillList[BESKILL_TYPE72]) do
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "trueDamegeOffset" then
				local offsetInfo = {}
				offsetInfo[1] = skillConfig.beSkillArgs[1]
				offsetInfo[2] = skillConfig.beSkillArgs[2]
				return offsetInfo
			end
        end
	end
end

-- 触发格挡后额外减伤
function GetGeDangExtraDef(obj)
	if obj.beSkillList[BESKILL_TYPE72] then
		for _, v in ipairs(obj.beSkillList[BESKILL_TYPE72]) do
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "gedangExtraDef" then
				return skillConfig.beSkillArgs[1]
			end
        end
	end

	return 0
end

-- 伤害翻倍
function GetBeSkillHurtMul(obj)
	if obj.beSkillList[BESKILL_TYPE72] then
		for _, v in ipairs(obj.beSkillList[BESKILL_TYPE72]) do
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "physicalDamageMul" and obj.hurtType == CombatDefine.PHY_HURT_TYPE then
				return math.random(skillConfig.beSkillArgs[1], skillConfig.beSkillArgs[2])
			end

			if skillConfig.cmd == "magicDamageMul" and obj.hurtType == CombatDefine.MAGIC_HURT_TYPE then
				return math.random(skillConfig.beSkillArgs[1], skillConfig.beSkillArgs[2])
			end
        end
	end
end

-- 回血翻倍
function GetBeSkillRestoretHPMul(obj)
	local mul = 1
	if obj.beSkillList[BESKILL_TYPE72] then
		for _, v in ipairs(obj.beSkillList[BESKILL_TYPE72]) do
			local skillConfig = Skill.GetSkillConfig(v[1])
			if skillConfig.cmd == "restoreHPMul" then
				local r = math.random(1,10000)
				if r <= (skillConfig.beSkillArgs[2] or 0) then
					mul =  skillConfig.beSkillArgs[1] or 1
				end
				return mul
			end
        end
	end

	return mul
end