local thtd_combo_table = 
{	
	["cirno"] = 
	{
		["letty_cirno"] = 
		{		
			["abilityName"] = "thtd_cirno_01",				
		}
	},
	["letty"] = 
	{
		["letty_cirno"] = 
		{		
			["abilityName"] = "thtd_letty_01",				
		}
	},
	["lyrica"] = 
	{
		["lyrica_lunasa_merlin"] = 
		{		
			["delay"] = 3,
			["duration"] = 3,	
			["abilityName"] = "thtd_lyrica_01",		
		}
	},
	["lunasa"] = 
	{
		["lyrica_lunasa_merlin"] = 
		{			
			["delay"] = 3,
			["duration"] = 3,			
		}
	},
	["merlin"] = 
	{	
		["lyrica_lunasa_merlin"] = 
		{			
			["delay"] = 3,
			["duration"] = 3,			
		}
	},
	["youmu"] = 
	{	
		["yuyuko_youmu"] = 
		{			
			["delay"] = 9,
			["duration"] = 5,	
			["abilityName"] = "thtd_youmu_01",				
		},
		["youmu_reisen"] = 
		{	
			["abilityName"] = "thtd_youmu_02",				
		}
	},
	["yuyuko"] = 
	{	
		["yuyuko_youmu"] = 
		{		
			["delay"] = 13,
			["duration"] = 5,	
			["abilityName"] = "thtd_yuyuko_02",	
		},
		["yuyuko_yukari"] = 
		{	
			["abilityName"] = "thtd_yuyuko_01",		
		}
	},
	["marisa"] = 
	{	
		["reimu_marisa"] = 
		{		
			["delay"] = 3,
			["duration"] = 2,	
			["abilityName"] = "thtd_marisa_01",		
			["abilityName2"] = "thtd_marisa_03",		
		},
		["marisa_alice"] = 
		{			
			["delay"] = 2,
			["duration"] = 3, 
			["abilityName"] = "thtd_marisa_02",	
		}
	},
	["alice"] = 
	{			
		["marisa_alice"] = 
		{
			["delay"] = 3,
			["duration"] = 3, 
			["abilityName"] = "thtd_alice_03",
		},
		["reimu_alice"] = 
		{			
			["delay"] = 3,
			["duration"] = 3, 	
			["abilityName"] = "thtd_alice_02",		
		}
	},
	["koishi"] = 
	{	
		["koishi_satori"] = 
		{			
			["delay"] = 5,
			["duration"] = 2,		
			["abilityName"] = "thtd_koishi_01",	
		}
	},
	["satori"] = 
	{	
		["koishi_satori"] = 
		{	
			["abilityName"] = "thtd_satori_02",	
		}
	},
	["sakuya"] = 
	{	
		["remilia_sakuya"] = 
		{			
			["delay"] = 3,
			["duration"] = 3,
			["abilityName"] = "thtd_sakuya_02",		
		},
		["meirin_sakuya"] = 
		{		
			["abilityName"] = "thtd_sakuya_03",	
		}
	},
	["koakuma"] = 
	{	
		["koakuma_patchouli"] = 
		{			
			["delay"] = 7,
			["duration"] = 3,		
			["abilityName"] = "thtd_koakuma_01",		
		}
	},
	["patchouli"] = 
	{	
		["koakuma_patchouli"] = 
		{			
			["delay"] = 5,
			["duration"] = 2,		
			["abilityName"] = "thtd_patchouli_04",				
		}
	},
	["eirin"] = 
	{	
		["eirin_kaguya"] = 
		{			
			["delay"] = 2,
			["duration"] = 2,		
			["abilityName"] = "thtd_eirin_01",	
		}
	},
	["kaguya"] = 
	{	
		["eirin_kaguya"] = 
		{			
			["delay"] = 2,
			["duration"] = 3,	
			["abilityName"] = "thtd_kaguya_01",			
		}
	},
	["yukari"] = 
	{	
		["reimu_yukari"] = 
		{		
			["abilityName"] = "thtd_yukari_01",	
		},
		["chen_yukari_ran"] = 
		{		
			["abilityName"] = "thtd_yukari_04",	
		}
	},
	["remilia"] = 
	{	
		["remilia_sakuya"] = 
		{		
			["abilityName"] = "thtd_remilia_04",	
		},
		["remilia_flandre"] = 
		{		
			["abilityName"] = "thtd_remilia_02",	
		}
	},
	["reimu"] = 
	{	
		["rumia_reimu"] = 
		{		
			["abilityName"] = "thtd_reimu_01",	
		},
		["reimu_marisa"] = 
		{		
			["abilityName"] = "thtd_reimu_02",	
		},
		["reimu_yukari"] = 
		{		
			["abilityName"] = "thtd_reimu_04",	
		},
	},
	["flandre"] = 
	{	
		["remilia_flandre"] = 
		{			
			["abilityName"] = "thtd_flandre_02",			
		}
	},
	["soga"] = 
	{	
		["futo_soga"] = 
		{			
			["abilityName"] = "thtd_soga_03",			
		}
	},
	["minamitsu"] = 
	{	
		["nue_minamitsu"] = 
		{			
			["abilityName"] = "thtd_minamitsu_02",			
		}
	},
	["futo"] = 
	{	
		["futo_soga"] = 
		{			
			["abilityName"] = "thtd_futo_02",			
		}
	},
	["sanae"] = 
	{	
		["suwako_kanako_sanae"] = 
		{			
			["abilityName"] = "thtd_sanae_04",			
		}
	},
	["tenshi"] = 
	{	
		["tenshi_iku"] = 
		{			
			["abilityName"] = "thtd_tenshi_03",			
		}
	},
	["mokou"] = 
	{	
		["mokou_keine"] = 
		{			
			["abilityName"] = "thtd_mokou_01",			
		}
	},
	["reisen"] = 
	{	
		["youmu_reisen"] = 
		{			
			["abilityName"] = "thtd_reisen_02",			
		}
	},
	["utsuho"] = 
	{	
		["utsuho_rin"] = 
		{			
			["abilityName"] = "thtd_utsuho_01",			
		}
	},
	["keine"] = 
	{	
		["mokou_keine"] = 
		{			
			["abilityName"] = "thtd_keine_03",			
		}
	},
	["ran"] = 
	{	
		["chen_yukari_ran"] = 
		{			
			["abilityName"] = "thtd_ran_03",			
		}
	},
	["yoshika"] = 
	{	
		["yoshika_seiga"] = 
		{			
			["abilityName"] = "thtd_yoshika_01",			
		}
	},
	["rumia"] = 
	{	
		["rumia_reimu"] = 
		{			
			["abilityName"] = "thtd_rumia_04",			
		}
	},
	["meirin"] = 
	{	
		["meirin_sakuya"] = 
		{			
			["abilityName"] = "thtd_meirin_02",			
		}
	},
	["iku"] = 
	{	
		["tenshi_iku"] = 
		{			
			["abilityName"] = "thtd_iku_01",			
		}
	},	
	["junko"] = 
	{	
		["junko_hecatia"] = 
		{			
			["abilityName"] = "thtd_junko_01",			
		}
	},	
	["hecatia"] = 
	{	
		["junko_hecatia"] = 
		{			
			["abilityName"] = "thtd_hecatia_03",			
		}
	},	
	["daiyousei"] = 
	{	
		["daiyousei_cirno"] = 
		{			
			["abilityName"] = "thtd_daiyousei_03",			
		}
	},	
}

function HasCombo(combo,name)
	for k,v in pairs(combo) do
		if k == name then
			return true
		end
	end
	return false
end

function CDOTA_BaseNPC:THTD_GetComboVoice(comboName)
	local data = thtd_combo_table[self:GetUnitName()][comboName]
	if data ~= nil and data["delay"] ~= nil then 
		data["comboName"] = comboName
	else
		data = nil		
	end
	return data
end

-- 设置组合技能的生效或关闭，返回组合语音数据组，组合影响：升级技能为2级（可设置此等级下光环开启）、增加buff、返回语音。（和或）
function CDOTA_BaseNPC:THTD_Set_Combo(combo)
	local comboVoiceTable = {}

	local unitName = self:GetUnitName()
	local comboList = thtd_combo_table[unitName]
	if comboList == nil then		
		combo = {}
		return comboVoiceTable
	end

	for k,v in pairs(comboList) do
		local isChange = false

		if v["abilityName"] ~= nil then 			
			local ability = self:FindAbilityByName(v["abilityName"])
			if ability == nil then
				print("----------- "..unitName..": combo error, combo: "..k..", ability: "..v["abilityName"]..", ability is nil")
				combo = {}
				return comboVoiceTable
			end
		
			if ability:IsActivated() then 
				if HasCombo(combo, k) then
					-- buff等级，如果为1，则仅开启buff不升级，否则升级，名称格式固定
					local buffOrLevel = ability:GetSpecialValueFor("has_combo_buff")
					if buffOrLevel > 0 and self:HasModifier("modifier_"..v["abilityName"].."_combo_buff") == false then			
						isChange = true				
						ability:ApplyDataDrivenModifier(self, self, "modifier_"..v["abilityName"].."_combo_buff", nil)							
					end	

					if ability:GetLevel() ~= 2 and buffOrLevel ~= 1 then
						isChange = true
						ability:SetLevel(2)
						self[v["abilityName"].."bonus_level"] = nil	

						-- 升级后是否存在光环技能，名称格式固定
						local auraName = "modifier_"..v["abilityName"].."_aura"
						local auraLevel = ability:GetSpecialValueFor("open_aura_level")	 --开启等级要求
						if auraLevel == 2 and self:HasModifier(auraName) == false then
							ability:ApplyDataDrivenModifier(self, self, auraName, nil)
						end
					end	
				else
					-- buff等级，如果为1，则仅开启buff不升级，否则升级
					local buffOrLevel = ability:GetSpecialValueFor("has_combo_buff")
					if buffOrLevel > 0 and self:HasModifier("modifier_"..v["abilityName"].."_combo_buff") then	
						isChange = true					
						self:RemoveModifierByName("modifier_"..v["abilityName"].."_combo_buff")					
					end	

					if ability:GetLevel() > 1 and buffOrLevel ~= 1 then	
						isChange = true
						ability:SetLevel(1)
						self[v["abilityName"].."bonus_level"] = nil	
						local auraName = "modifier_"..v["abilityName"].."_aura"
						local auraLevel = ability:GetSpecialValueFor("open_aura_level")	
						if auraLevel > 1 and self:HasModifier(auraName) then
							self:RemoveModifierByName(auraName)
						end						
					end					
				end
			end
		end

		if v["abilityName2"] ~= nil then 
			local ability = self:FindAbilityByName(v["abilityName2"])
			if ability == nil then
				print("----------- "..unitName..": combo error, combo: "..k..", ability: "..v["abilityName2"]..", ability is nil")
				combo = {}
				return comboVoiceTable
			end
		
			if ability:IsActivated() then 
				if HasCombo(combo, k) then
					-- buff等级，如果为1，则仅开启buff不升级，否则升级，名称格式固定
					local buffOrLevel = ability:GetSpecialValueFor("has_combo_buff")
					if buffOrLevel > 0 and self:HasModifier("modifier_"..v["abilityName2"].."_combo_buff") == false then	
						isChange = true						
						ability:ApplyDataDrivenModifier(self, self, "modifier_"..v["abilityName2"].."_combo_buff", nil)							
					end	

					if ability:GetLevel() ~= 2 and buffOrLevel ~= 1 then
						isChange = true		
						ability:SetLevel(2)
						self[v["abilityName2"].."bonus_level"] = nil	

						-- 升级后是否存在光环技能，名称格式固定
						local auraName = "modifier_"..v["abilityName2"].."_aura"
						local auraLevel = ability:GetSpecialValueFor("open_aura_level")	
						if auraLevel == 2 and self:HasModifier(auraName) == false then
							ability:ApplyDataDrivenModifier(self, self, auraName, nil)
						end
					end						
				else
					-- buff等级，如果为1，则仅开启buff不升级，否则升级
					local buffOrLevel = ability:GetSpecialValueFor("has_combo_buff")
					if buffOrLevel > 0 and self:HasModifier("modifier_"..v["abilityName2"].."_combo_buff") then		
						isChange = true							
						self:RemoveModifierByName("modifier_"..v["abilityName2"].."_combo_buff")							
					end

					if ability:GetLevel() > 1 and buffOrLevel ~= 1 then
						isChange = true		
						ability:SetLevel(1)
						self[v["abilityName2"].."bonus_level"] = nil	
						local auraName = "modifier_"..v["abilityName2"].."_aura"
						local auraLevel = ability:GetSpecialValueFor("open_aura_level")	
						if auraLevel > 1 and self:HasModifier(auraName) then
							self:RemoveModifierByName(auraName)
						end						
					end					
				end
			end
		end

		if isChange then 
			local voiceData = self:THTD_GetComboVoice(k)
			if voiceData ~= nil then
				table.insert(comboVoiceTable, voiceData)
			end
		end
	end	

	combo = {}
	return comboVoiceTable
end


function CDOTA_BaseNPC:THTD_chen_thtd_combo(combo)
	local comboVoiceTable = {}
	if HasCombo(combo,"chen_yukari_ran") then
		if self.thtd_chen_01_distance_increase ~= 75 then
			self.thtd_chen_01_distance_increase = 75
		end		
	else
		if self.thtd_chen_01_distance_increase ~= 100 then
			self.thtd_chen_01_distance_increase = 100
		end		
	end
	combo = {}
	return comboVoiceTable
end

function CDOTA_BaseNPC:THTD_sunny_thtd_combo(combo)
	local comboVoiceTable = {}

	if HasCombo(combo,"luna_star_sunny") and self.thtd_combo_fairyList == nil then		
		local hero = self:GetOwner()
		local sunny = nil
		local luna = nil
		local star = nil		
		local sunnyList = {}
		local lunaList = {}
		local starList = {}		
		for k,v in pairs(hero.thtd_hero_tower_list) do
			if v:GetUnitName() == "sunny" and v.thtd_is_in_fairy_combo~=true then
				table.insert(sunnyList,v)
			elseif v:GetUnitName() == "luna" and v.thtd_is_in_fairy_combo~=true then
				table.insert(lunaList,v)
			elseif v:GetUnitName() == "star" and v.thtd_is_in_fairy_combo~=true then
				table.insert(starList,v)
			end
		end		
		if #sunnyList > 0 and #lunaList > 0 and #starList > 0 then
			--最大连线距离
			local maxDistance = 2000
			for sunnyK,sunnyV in pairs(sunnyList) do
				for lunaK,lunaV in pairs(lunaList) do
					--计算连线距离，如果小于
					if math.floor(GetDistanceBetweenTwoVec2D(sunnyV:GetAbsOrigin(), lunaV:GetAbsOrigin()) + 0.5) <= maxDistance then 
						for starK,starV in pairs(starList) do
							--计算连线距离，如果小于
							if math.floor(GetDistanceBetweenTwoVec2D(starV:GetAbsOrigin(), lunaV:GetAbsOrigin()) + 0.5) <= maxDistance and math.floor(GetDistanceBetweenTwoVec2D(starV:GetAbsOrigin(), sunnyV:GetAbsOrigin()) + 0.5) <= maxDistance then 
								sunny = sunnyV
								luna = lunaV
								star = starV
								break
							end						
						end					
					end
					if sunny ~= nil then break end
				end
				if sunny ~= nil then break end
			end
		end

		if sunny~=nil and luna~=nil and star~=nil then	
			local sunny_effectIndex = ParticleManager:CreateParticle("particles/heroes/daiyousei/ability_daiyousei_03.vpcf", PATTACH_CUSTOMORIGIN, sunny)
			ParticleManager:SetParticleControlEnt(sunny_effectIndex , 0, sunny, 5, "attach_hitloc", Vector(0,0,0), true)
			ParticleManager:SetParticleControlEnt(sunny_effectIndex , 1, luna, 5, "attach_hitloc", Vector(0,0,0), true)

			local luna_effectIndex = ParticleManager:CreateParticle("particles/heroes/daiyousei/ability_daiyousei_03.vpcf", PATTACH_CUSTOMORIGIN, luna)
			ParticleManager:SetParticleControlEnt(luna_effectIndex , 0, luna, 5, "attach_hitloc", Vector(0,0,0), true)
			ParticleManager:SetParticleControlEnt(luna_effectIndex , 1, star, 5, "attach_hitloc", Vector(0,0,0), true)

			local star_effectIndex = ParticleManager:CreateParticle("particles/heroes/daiyousei/ability_daiyousei_03.vpcf", PATTACH_CUSTOMORIGIN, star)
			ParticleManager:SetParticleControlEnt(star_effectIndex , 0, star, 5, "attach_hitloc", Vector(0,0,0), true)
			ParticleManager:SetParticleControlEnt(star_effectIndex , 1, sunny, 5, "attach_hitloc", Vector(0,0,0), true)

			sunny.thtd_is_in_fairy_combo = true
			star.thtd_is_in_fairy_combo = true
			luna.thtd_is_in_fairy_combo = true	

			self.thtd_combo_fairyList = 
			{				
				sunny = sunny,
				star = star,
				luna = luna,				
			}
			self:SetContextThink(DoUniqueString("thtd_fairy_combo"), 
				function()
					if GameRules:IsGamePaused() then return 0.03 end
					if THTD_IsValid(self) == false or THTD_IsValid(star) == false or THTD_IsValid(luna) == false then 					
						ParticleManager:DestroyParticleSystem(luna_effectIndex,true)
						luna.thtd_is_in_fairy_combo = nil					
						
						ParticleManager:DestroyParticleSystem(star_effectIndex,true)
						star.thtd_is_in_fairy_combo = nil					
						
						ParticleManager:DestroyParticleSystem(sunny_effectIndex,true)
						sunny.thtd_is_in_fairy_combo = nil	
						
						sunny.thtd_combo_fairyList = nil
					
						self:GetOwner():THTD_HeroComboRefresh()
						return nil 
					end						
					return 0.03
				end, 
			0)
		end	
	end
	combo = {}
	return comboVoiceTable
end

function CDOTA_BaseNPC:THTD_wriggle_thtd_combo(combo)
	local comboVoiceTable = {}
	if HasCombo(combo,"wriggle_rumia") then
		if self.thtd_wriggle_rumia_combo ~= true then
			self.thtd_wriggle_rumia_combo = true
		end
	else	
		if self.thtd_wriggle_rumia_combo ~= false then
			self.thtd_wriggle_rumia_combo = false
		end
	end
	combo = {}
	return comboVoiceTable
end


function UpdatePrismriverComboName(caster,target)
	if target.thtd_prismriver_comboName == nil then
		target.thtd_prismriver_comboName = {[1]="",[2]="",[3]=""}		
	end
	target.thtd_prismriver_comboName[1] = target.thtd_prismriver_comboName[2]
	target.thtd_prismriver_comboName[2] = target.thtd_prismriver_comboName[3]
	target.thtd_prismriver_comboName[3] = caster:GetUnitName()	
end

function GetPrismriverComboName(target)
	if target.thtd_prismriver_comboName == nil then
		target.thtd_prismriver_comboName = {[1]="",[2]="",[3]=""}		
	end
	return target.thtd_prismriver_comboName[1]..target.thtd_prismriver_comboName[2]..target.thtd_prismriver_comboName[3]
end

function ResetPrismriverComboName(target)
	target.thtd_prismriver_comboName = {[1]="",[2]="",[3]=""}
end

function GetCountPrismriver(target)
	if target.thtd_prismriver_comboName == nil then return 0 end
	local count = 0
	if target.thtd_prismriver_comboName[1] ~= "" then
		count = count + 1
	end 
	if target.thtd_prismriver_comboName[2] ~= "" then
		count = count + 1
	end 
	if target.thtd_prismriver_comboName[3] ~= "" then
		count = count + 1
	end 
	return count
end
