local Yuyicast = Class(function(self, inst)
    self.inst = inst
    self.canuseskill = false
end)

function Yuyicast:areaskill(caster)
    local old = caster.bufferedaction
    if caster:HasTag("huoqiang") then
        self:huoqiang_attack(caster)
	elseif caster:HasTag("baofengxue") then
        self:baofengxue_attack(caster)
    end
    caster.bufferedaction = old
    return true
end

function Yuyicast:checkskillscd(caster)
	local weapon = caster.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if weapon and weapon.weapon_skillscd then
	caster.yuyiskillcd = (100-weapon.weapon_skillscd)/100
	else
	caster.yuyiskillcd = 1
	end
	return caster.yuyiskillcd
end

function Yuyicast:yuyi_skilldmg(caster)
	local skilldmg = (10+caster.yuyilevel)*caster.yuyidmgpercent
	local weapon = caster.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if weapon and weapon.components.weapon then
	local weapondmg = weapon.components.weapon.damage
		if weapon.components.weapon.damage > 30 then
			skilldmg = (weapondmg+caster.yuyilevel)*caster.yuyidmgpercent
		end
	end
	return skilldmg
end
	
function Yuyicast:yuyi_skilldist(caster)
	local skilldist = 3.25
	local weapon = caster.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if weapon and weapon.components.weapon then
		if weapon and weapon.components.weapon and weapon.components.weapon.attackrange then
			skilldist = weapon.components.weapon.attackrange+2.25
		end
	end
	return skilldist
end

function Yuyicast:yuyitarget()
	local pos = Vector3(GetPlayer().Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 30)
	for k,v in pairs(ents) do
		if v and v.components.follower and v.components.follower.leader == GetPlayer() and not v:HasTag("myfriend") then
		v:AddTag("myfriend")
		end
		if v.components.combat and v.components.health and not v.components.health:IsDead() and not v:HasTag("yuyitarget") and not v:HasTag("yuyi") and not v:HasTag("myfriend") and not v:HasTag("wall") and not (v.components.inventoryitem and v.components.inventoryitem.owner) then
		v:AddTag("yuyitarget")
--		elseif v.components.combat and v.components.health and not v.components.health:IsDead() and not v:HasTag("yuyitarget") and not v:HasTag("myfriend") and v:HasTag("monster") and GetPlayer():HasTag("mononly") and not v:HasTag("wall") and not v:HasTag("yuyi") then
--		v:AddTag("yuyitarget")
		end
	end
end

function Yuyicast:get_yuyitarget(caster)
	self:yuyitarget()
    self.canuseskill = false
	local x1, y1, z1 = caster.Transform:GetWorldPosition()
	local target = GetClosestInstWithTag("yuyitarget", caster or GetPlayer(), 15)
	if caster.components.combat.targetfn then
		local target = caster.components.combat.targetfn(caster)
		if not caster.components.combat.target and target ~= nil then
			caster.components.combat:SetTarget(target)
		end
	end
	if target and target~=nil then
		local x2, y2, z2 = target.Transform:GetWorldPosition()
		local dist = math.sqrt((x1 - x2)*(x1 - x2) + (z1 - z2)*(z1 - z2))
		local dist_Instructions = self:yuyi_skilldist(caster)
		local pos = Point(caster.Transform:GetWorldPosition())
		local range_indicators = TheSim:FindEntities(pos.x,pos.y,pos.z, 10, {"yuyi_range_indicator"} )
		if dist > self:yuyi_skilldist(caster) then
			caster.components.combat:SetTarget(target)
			caster:RemoveTag("xinjineng")
			caster:RemoveTag("xinjineng2")
			caster.components.talker:Say(TUNING.TALK19)
			if range_indicators then
				for i,v in ipairs(range_indicators) do if v:IsValid() then v:Remove() end end
				local s  = 0.6
				if dist_Instructions == 0.75 then s =0.75 elseif dist_Instructions >= 5 then s = 1.1+(dist_Instructions-5)*0.074 end
				local range = SpawnPrefab("yuyi_range_indicator")
				range.entity:SetParent(caster.entity)
				range.Transform:SetPosition(0, 0, 0)
				range.Transform:SetScale(s,s,s)
			else
				local s  = 0.675
				if dist_Instructions == 0.75 then s =0.75 elseif dist_Instructions >= 5 then s = 1.1+(dist_Instructions-5)*0.074 end
				local range = SpawnPrefab("yuyi_range_indicator")
				range.entity:SetParent(caster.entity)
				range.Transform:SetPosition(0, 0, 0)
				range.Transform:SetScale(s,s,s)
			end
		elseif dist <= self:yuyi_skilldist(caster) then
			self.canuseskill = true
		end
	end
	if not target or target==nil or (target and target.components.health and target.components.health:IsDead()) then
	caster.components.talker:Say(TUNING.TALKS15)
	caster:RemoveTag("xinjineng")
	caster:RemoveTag("xinjineng2")
	end
	if target and target~=nil and target.components.combat and target.components.health and not target.components.health:IsDead() then
	return target
	end

end

function Yuyicast:skill_pt(caster)
	if caster:HasTag("huoqiang") then
		local pt=Vector3(TheInput:GetWorldPosition():Get())
		return pt
	elseif caster:HasTag("baofengxue") then
		local pt=Vector3(TheInput:GetWorldPosition():Get())
		return pt
	elseif caster:HasTag("yuyiskill8") and not caster:HasTag("yuyiskill8_target") then
		local pt=Vector3(TheInput:GetWorldPosition():Get())
		return pt
	elseif caster:HasTag("yuyiskill7") and caster:HasTag("yuyiskill7_target") then
		self:yuyitarget()
		local target = GetClosestInstWithTag("yuyitarget", caster or GetPlayer(), 15)
			if not (target and target~=nil and target.components.combat and target.components.health and not target.components.health:IsDead()) then
				local pt=Vector3(TheInput:GetWorldPosition():Get())
				return pt
			elseif target and target~=nil and target.components.combat and target.components.health and not target.components.health:IsDead() and caster:HasTag("xinjineng2") then
				local pt = Vector3(target.Transform:GetWorldPosition())
				return pt
		end
	elseif caster:HasTag("yuyiskill8") and caster:HasTag("yuyiskill8_target") then
		self:yuyitarget()
		local target = GetClosestInstWithTag("yuyitarget", caster or GetPlayer(), 15)
			if not (target and target~=nil and target.components.combat and target.components.health and not target.components.health:IsDead()) then
				local pt=Vector3(TheInput:GetWorldPosition():Get())
				return pt
			elseif target and target~=nil and target.components.combat and target.components.health and not target.components.health:IsDead() and caster:HasTag("xinjineng2") then
				local pt = Vector3(target.Transform:GetWorldPosition())
				return pt
		end

	end
end


function Yuyicast:huoqiang_attack(caster)
    caster:ClearBufferedAction()
    caster.components.playercontroller:Enable(false)
    caster.components.locomotor:Stop()
    if caster.components.driver and caster.components.driver.vehicle then caster.components.driver.vehicle:Show() end

	local pt = self:skill_pt(caster)
    caster:DoTaskInTime(0.05, function()
    caster:ForceFacePoint(pt.x,pt.y,pt.z)
    caster.AnimState:PlayAnimation("atk")

	local huoqiang = SpawnPrefab("huoqiang")
	huoqiang.Transform:SetPosition(pt.x, pt.y, pt.z)
	caster.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
        if caster.components.health then
            caster.components.health:SetInvincible(true)
        end
    end)
    caster:DoTaskInTime(0.5, function()
        caster.components.locomotor:Stop()
        caster.AnimState:PlayAnimation("idle")
        if caster.components.health then
            caster.components.health:SetInvincible(false)
        end
        caster.components.playercontroller:Enable(true)
        caster:ClearBufferedAction()
        if caster:HasTag("huoqiang") then
            caster:RemoveTag("huoqiang")
        end
    end)
end

function Yuyicast:baofengxue_attack(caster)
    caster:ClearBufferedAction()
    caster.components.playercontroller:Enable(false)
    caster.components.locomotor:Stop()

    if caster.components.driver and caster.components.driver.vehicle then caster.components.driver.vehicle:Show() end

	local pt = self:skill_pt(caster)
    caster:DoTaskInTime(0.05, function()
    caster:ForceFacePoint(pt.x,pt.y,pt.z)
    caster.AnimState:PlayAnimation("atk")

	local longjuan = SpawnPrefab("baofengxue")
	longjuan.Transform:SetPosition(pt.x, pt.y, pt.z)
	self:baofengxue_area(pt, caster)
	caster:DoTaskInTime(0.33, function()
		self:baofengxue_area(pt, caster)
	end)
	caster:DoTaskInTime(0.66, function()
		self:baofengxue_area(pt, caster)
	end)
	caster:DoTaskInTime(0.99, function()
		self:baofengxue_area(pt, caster)
	end)
        caster.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
        if caster.components.health then
            caster.components.health:SetInvincible(true)
        end
    end)
    caster:DoTaskInTime(0.5, function()
		caster.SoundEmitter:PlaySound("dontstarve/rain/thunder_close")
        caster.components.locomotor:Stop()
        caster.AnimState:PlayAnimation("idle")

        if caster.components.health then
            caster.components.health:SetInvincible(false)
        end
        caster.components.playercontroller:Enable(true)
        caster:ClearBufferedAction()
        if caster:HasTag("baofengxue") then
            caster:RemoveTag("baofengxue")
        end
    end)
end

function Yuyicast:zhuri_attack(other, caster)
	if caster:HasTag("aquatic") and other and not other:HasTag("aquatic") and not other.prefab == "knightboat" then
		caster:RemoveTag("zhurijianfa")
	elseif not caster:HasTag("aquatic") and other and other:HasTag("aquatic") and not other.prefab == "knightboat" then
		caster:RemoveTag("zhurijianfa")
	elseif caster:HasTag("aquatic") and other and not other:HasTag("aquatic") and other.prefab == "knightboat" and caster:HasTag("zhurijianfa") then
		caster.components.locomotor:Stop()
		caster.components.locomotor:EnableGroundSpeedMultiplier(false)
		caster.AnimState:PlayAnimation("jump")
--		caster.SoundEmitter:PlaySound("dontstarve_DLC002/common/boatjump_whoosh")
		caster.sg.statemem.startpos = caster:GetPosition()
		caster.sg.statemem.targetpos = other and other:GetPosition()
		RemovePhysicsColliders(caster)
		caster.components.health:SetInvincible(true)
		caster.components.playercontroller:Enable(false)
		caster:ForceFacePoint(caster.sg.statemem.targetpos:Get())
		local dist = caster:GetPosition():Dist(caster.sg.statemem.targetpos)
		local speed = dist*4
		caster:DoTaskInTime(0, function()
        caster.AnimState:PlayAnimation("jump")
		caster.Physics:SetMotorVelOverride(0.3 * speed, 4, 0)
		end)
		caster:DoTaskInTime(0.3, function()
		caster.Physics:SetMotorVelOverride(1 * speed, -4, 0)
        caster.AnimState:PlayAnimation("shovel_pre")
		end)

		caster:DoTaskInTime(0.5, function()
		caster.components.health:SetInvincible(false)
		caster.components.playercontroller:Enable(true)
		caster.Transform:SetPosition(other.Transform:GetWorldPosition())
		caster.Physics:Stop()
		ChangeToCharacterPhysics(caster)
		caster.components.locomotor:Stop()
		caster.components.locomotor:EnableGroundSpeedMultiplier(true)
		caster:PerformBufferedAction()
		caster:RemoveTag("zhurijianfa")
		caster.AnimState:PlayAnimation("atk")
		if other then
		other.components.combat:SetTarget(nil)
		caster.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/zhurijianfa2")
		SpawnPrefab("explode_small").Transform:SetPosition(other.Transform:GetWorldPosition())
		other.components.combat:GetAttacked(caster, caster.skill_LLv1*10+30)
		other.components.combat:BlankOutAttacks(2)
--		other.brain
		end
		end)

	elseif caster:HasTag("zhurijianfa") then
		caster.components.locomotor:Stop()
		caster.components.locomotor:EnableGroundSpeedMultiplier(false)
		caster.AnimState:PlayAnimation("jump")
--		caster.SoundEmitter:PlaySound("dontstarve_DLC002/common/boatjump_whoosh")
		caster.sg.statemem.startpos = caster:GetPosition()
		caster.sg.statemem.targetpos = other and other:GetPosition()
		RemovePhysicsColliders(caster)
		caster.components.health:SetInvincible(true)
		caster.components.playercontroller:Enable(false)
		caster:ForceFacePoint(caster.sg.statemem.targetpos:Get())
		local dist = caster:GetPosition():Dist(caster.sg.statemem.targetpos)
		local speed = dist*4
		caster:DoTaskInTime(0, function()
        caster.AnimState:PlayAnimation("jump")
		caster.Physics:SetMotorVelOverride(0.3 * speed, 4, 0)
		end)
		caster:DoTaskInTime(0.3, function()
		caster.Physics:SetMotorVelOverride(1 * speed, -4, 0)
        caster.AnimState:PlayAnimation("shovel_pre")
		end)

		caster:DoTaskInTime(0.5, function()
		caster.components.health:SetInvincible(false)
		caster.components.playercontroller:Enable(true)
		caster.Transform:SetPosition(other.Transform:GetWorldPosition())
		caster.Physics:Stop()
		ChangeToCharacterPhysics(caster)
		caster.components.locomotor:Stop()
		caster.components.locomotor:EnableGroundSpeedMultiplier(true)
		caster:PerformBufferedAction()
		caster:RemoveTag("zhurijianfa")
		caster.AnimState:PlayAnimation("atk")
		if other then
		other.components.combat:SetTarget(nil)
		SpawnPrefab("explode_small").Transform:SetPosition(other.Transform:GetWorldPosition())
		other.components.combat:GetAttacked(caster, caster.skill_LLv1*10+30)
		other.components.combat:BlankOutAttacks(2)
--		other.brain
		end
		end)
	end
end


function Yuyicast:baofengxue_attacks(other, caster)
	if caster:HasTag("baofengxue") then
	caster:RemoveTag("baofengxue")

    caster:ClearBufferedAction()
    caster.components.playercontroller:Enable(false)
    caster.components.locomotor:Stop()
	caster.components.combat:SetRange(2, 2)
    if caster.components.driver and caster.components.driver.vehicle then caster.components.driver.vehicle:Show() end

	local pt = Vector3(other.Transform:GetWorldPosition())
    caster:DoTaskInTime(0.05, function()
    caster:ForceFacePoint(pt.x,pt.y,pt.z)
    caster.AnimState:PlayAnimation("atk")

	local longjuan = SpawnPrefab("baofengxue")
	longjuan.Transform:SetPosition(pt.x, pt.y, pt.z)
	self:baofengxue_area(pt, caster)
	caster:DoTaskInTime(0.33, function()
		self:baofengxue_area(pt, caster)
	end)
	caster:DoTaskInTime(0.66, function()
		self:baofengxue_area(pt, caster)
	end)
	caster:DoTaskInTime(0.99, function()
		self:baofengxue_area(pt, caster)
	end)
        caster.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
        if caster.components.health then
            caster.components.health:SetInvincible(true)
        end
    end)
    caster:DoTaskInTime(0.5, function()
		caster.SoundEmitter:PlaySound("dontstarve/rain/thunder_close")
        caster.components.locomotor:Stop()
        caster.AnimState:PlayAnimation("idle")

        if caster.components.health then
            caster.components.health:SetInvincible(false)
        end
        caster.components.playercontroller:Enable(true)
        caster:ClearBufferedAction()
    end)
end
end

function Yuyicast:baofengxue_area(pt, caster)
	local weapon =  caster.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	local weapondmg = 20
	if weapon and weapon.components.weapon and weapon.components.weapon.damage then
		weapondmg = weapon.components.weapon.damage
	end
	local longjuandmg = caster.skill_ILv0*5+weapondmg*0.20
    local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, 5)
    for k,v in pairs(ents) do
	if v.components.combat and v.components.health and not v.components.health:IsDead() and not v:HasTag("yuyi") and not v:HasTag("myfriend") and not (v.components.inventoryitem and v.components.inventoryitem.owner) and not v:HasTag("wall") then
		if v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, longjuandmg) end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end
end
end
end


function Yuyicast:huoyu_attack(caster)
	caster:RemoveTag("shadow")
	caster:RemoveTag("yuyiskill2")
	caster.components.colourtweener:StartTween({1,1,1,1}, 0)

    caster:ClearBufferedAction()
    caster.components.playercontroller:Enable(false)
    caster.components.locomotor:Stop()

    if caster.components.driver and caster.components.driver.vehicle then caster.components.driver.vehicle:Show() end

	local pt = self:skill_pt(caster)
    caster:DoTaskInTime(0.05, function()

    caster:ForceFacePoint(pt.x,pt.y,pt.z)
    caster.AnimState:PlayAnimation("atk")
	local chance = math.random()
	if chance <= 0.33 then
	local huoyu = SpawnPrefab("huoyu")
	huoyu.Transform:SetScale(5, 5, 5)
	huoyu.Transform:SetPosition(pt.x, pt.y, pt.z)
	elseif chance <= 0.66 then
	local huoyu = SpawnPrefab("liehuo")
	huoyu.Transform:SetScale(4, 4, 4)
	huoyu.Transform:SetPosition(pt.x, pt.y, pt.z)
	elseif chance <= 1 then
	local huoyu = SpawnPrefab("huoquan")
	huoyu.Transform:SetScale(6, 6, 6)
	huoyu.Transform:SetPosition(pt.x, pt.y, pt.z)
	end

		self:huoyu_area(pt, caster)
	caster:DoTaskInTime(0.5, function()
		self:huoyu_area(pt, caster)
	end)
	caster:DoTaskInTime(1.00, function()
		self:huoyu_area(pt, caster)
	end)
	caster:DoTaskInTime(1.50, function()
		self:huoyu_area(pt, caster)
	end)
	caster:DoTaskInTime(2.00, function()
		self:huoyu_area(pt, caster)
	end)

        caster.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
        if caster.components.health then
            caster.components.health:SetInvincible(true)
        end
    end)
    caster:DoTaskInTime(0.5, function()
		caster.SoundEmitter:PlaySound("dontstarve/rain/thunder_close")
        caster.components.locomotor:Stop()
        caster.AnimState:PlayAnimation("idle")

        if caster.components.health then
            caster.components.health:SetInvincible(false)
        end
        caster.components.playercontroller:Enable(true)
        caster:ClearBufferedAction()

        if caster:HasTag("yuyiskill8") then
            caster:RemoveTag("yuyiskill8")
        end
        if caster:HasTag("xinjineng2") then
            caster:RemoveTag("xinjineng2")
        end
        if caster:HasTag("yuyiskill8_target") then
            caster:RemoveTag("yuyiskill8_target")
        end
    end)
end

function Yuyicast:huoyu_area(pt, caster)
	local huoyudmg = self:yuyi_skilldmg(caster)/2
    local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, caster.yuyilevel/10+5)
	if caster.yuyidmgcomb and caster.yuyidmgcomb < 20 then caster.yuyidmgcomb = caster.yuyidmgcomb + 1 end
    for k,v in pairs(ents) do
		if v.components.combat and v.components.health and not v.components.health:IsDead() and not v:HasTag("yuyi") and not v:HasTag("myfriend") and not (v.components.inventoryitem and v.components.inventoryitem.owner) then
        if v and v.components.burnable and not v.components.burnable:IsBurning() then v.components.burnable:Ignite() end
		if v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, huoyudmg) end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end
		end
	end
end


function Yuyicast:SpawnIceFx(caster, target)
    if not caster or not target then return end
    local numFX = math.random(15,20)
    local pos = caster:GetPosition()
    local targetPos = target:GetPosition()
    local vec = targetPos - pos
    vec = vec:Normalize()
    local dist = pos:Dist(targetPos)
    local angle = caster:GetAngleToPoint(targetPos:Get())

    for i = 1, numFX do
        caster:DoTaskInTime(math.random() * 0.25, function(caster)
            local prefab = "icespike_fx_"..math.random(1,4)
            local fx = SpawnPrefab(prefab)
            if fx then
                local x = GetRandomWithVariance(0, 3)
                local z = GetRandomWithVariance(0, 3)
                local offset = (vec * math.random(dist * 0.25, dist)) + Vector3(x,0,z)
                fx.Transform:SetPosition((offset+pos):Get())
            end
        end)
    end
end

function Yuyicast:IceAge(caster)
	if caster:HasTag("xinjineng") and caster:HasTag("yuyiskill1") then
	local range = caster.yuyilevel/5+8
	local pos = Vector3(caster.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, range)
	caster.yuyiskill1 = 10*caster.yuyiskillcd
	if caster.HUD.binghuan then
	caster.HUD.binghuan:Show()
	caster.HUD.binghuan:SetTooltip(TUNING.BINGHUAN.."CD : " .. caster.yuyiskill1)
	end

	caster.sg:GoToState("idle")
    caster:ClearBufferedAction()
    caster.components.locomotor:Stop()
    caster.components.playercontroller:Enable(false)
	caster.components.health:SetInvincible(true)
    caster.AnimState:PlayAnimation("jump")
	caster.Physics:SetMotorVelOverride(0,2,0)
	caster:DoTaskInTime(0.35, function()
	caster.Physics:ClearMotorVelOverride()
	caster.Physics:SetMotorVelOverride(0,-2,0)
	end)
    caster:DoTaskInTime(0.5, function()
	caster.Physics:ClearMotorVelOverride()
	caster.Physics:SetMotorVelOverride(0, 0, 0)
	caster.AnimState:PlayAnimation("pickup")
	caster.SoundEmitter:PlaySound("yuyiattack/yuyiattack/saolai")
	caster.SoundEmitter:PlaySound("yuyiattack/yuyiattack/jinengb")
	local fx = SpawnPrefab("groundpoundring_fx")
	fx.Transform:SetScale(caster.yuyilevel+5/50,caster.yuyilevel+5/50,caster.yuyilevel+5/50)
	fx.Transform:SetPosition(caster:GetPosition():Get())
	end)
	caster:DoTaskInTime(0.55, function()
	caster.components.health:SetInvincible(false)
		for k,v in pairs(ents) do
		if v.components.freezable and not v:HasTag("yuyi") and not v:HasTag("myfriend") then
			v.components.freezable:AddColdness(2)
		end
		end
	end)
	caster:DoTaskInTime(0.60, function()
	caster.components.locomotor:Stop()
	caster.sg:GoToState("idle")
	caster.components.playercontroller:Enable(true)
	caster:RemoveTag("yuyiskill1")
	caster:RemoveTag("xinjineng")
	end)
end
end

function Yuyicast:bingdong_attack(caster)
	local v = self:get_yuyitarget(caster)
	if v and v~=nil and self.canuseskill and caster:HasTag("bingdong") then
	local bingdongdmg = self:yuyi_skilldmg(caster)*0.6
--		caster.components.combat:SetTarget(nil)
		caster:ClearBufferedAction()
		caster.components.playercontroller:Enable(false)
		caster.components.locomotor:Stop()
		caster:ForceFacePoint(v:GetPosition():Get())
		caster.components.health:SetInvincible(true)

		self:checkskillscd(caster)
		caster.yuyiskill3 = 40*caster.yuyiskillcd
		caster.HUD.bingdong:Show()
		caster.HUD.bingdong:SetOnClick(nil)
		caster.HUD.bingdong:SetTooltip(TUNING.BINGLING.."CD : " .. caster.yuyiskill3)
		caster:RemoveTag("bingdong")
--		caster.components.talker:Say("Skills [J]",nil,true)

		caster:RemoveTag("yuyiskill2")
--		caster.yuyidmgcomb = caster.yuyidmgcomb + 1
		caster.components.colourtweener:StartTween({1,1,1,1}, 0)

		caster.AnimState:PlayAnimation("chop_loop")
		caster.SoundEmitter:PlaySound("dontstarve_DLC001/common/iceboulder_smash")


	if caster:HasTag("Ragnarok") then

		local pos = Vector3(v.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, caster.yuyilevel/15+2.5)
		for k,v in pairs(ents) do
			if v and v:HasTag("yuyitarget") and not v:HasTag("yuyi") and not v:HasTag("myfriend") and not (v.components.inventoryitem and v.components.inventoryitem.owner) then
		self:SpawnIceFx(caster, v)
		if v then v.AnimState:SetMultColour(150/255,150/255,150/255,1) end
		if v and v.components.combat then v.components.combat:BlankOutAttacks(1.5) v.components.combat:SetTarget(nil) end
		if v and v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, bingdongdmg) end
		if v and v.brain then v.brain:Stop() end
		if v and v.components.locomotor then v.components.locomotor:Stop() end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end
		if v.components.freezable then--and v.prefab ~= "yuyi_deerclops" 
		v.components.freezable:AddColdness(10, 0.5)
		v.components.freezable:SpawnShatterFX()
		end

	if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
	v:DoTaskInTime(1.5, function()
	if v.brain then v.brain:Start() end
	if v.components.freezable and v.components.freezable:IsFrozen() then v:PushEvent("unfreeze") end
	if v then v.components.combat:SetTarget(caster) end
	if v then v.AnimState:SetMultColour(255/255,255/255,255/255,1) end
	end)
	end

			end
		end


	elseif not caster:HasTag("Ragnarok") then
		self:SpawnIceFx(caster, v)
		if v then v.AnimState:SetMultColour(150/255,150/255,150/255,1) end
		if v and v.components.combat then v.components.combat:BlankOutAttacks(1.5) v.components.combat:SetTarget(nil) end
		if v and v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, bingdongdmg) end
		if v and v.brain then v.brain:Stop() end
		if v and v.components.locomotor then v.components.locomotor:Stop() end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end
		if v.components.freezable then--and v.prefab ~= "yuyi_deerclops" 
		v.components.freezable:AddColdness(10, 0.5)
		v.components.freezable:SpawnShatterFX()
		end


	if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
	v:DoTaskInTime(1.5, function()
	if v.brain then v.brain:Start() end
	if v.components.freezable and v.components.freezable:IsFrozen() then v:PushEvent("unfreeze") end
	if v then v.components.combat:SetTarget(caster) end
	if v then v.AnimState:SetMultColour(255/255,255/255,255/255,1) end
	end)
	end

end

		caster:DoTaskInTime(0.6, function()
		caster:RemoveTag("xinjineng")
		caster.sg:GoToState("idle")
		caster.components.health:SetInvincible(false)
		caster.components.playercontroller:Enable(true)
		end)

	end
end

function Yuyicast:dilie_attack(caster)
	local v = self:get_yuyitarget(caster)
	if v and v~=nil and self.canuseskill and caster:HasTag("dilie") then
	local diliedmg = self:yuyi_skilldmg(caster)*0.8
--		caster.components.combat:SetTarget(nil)
		caster:ClearBufferedAction()
		caster.components.playercontroller:Enable(false)
		caster.components.locomotor:Stop()
		caster:ForceFacePoint(v:GetPosition():Get())
		caster.components.health:SetInvincible(true)

		self:checkskillscd(caster)
		caster.yuyiskill4 = 40*caster.yuyiskillcd
		caster.HUD.dilie:Show()
		caster.HUD.dilie:SetOnClick(nil)
		caster.HUD.dilie:SetTooltip(TUNING.DILIE.."CD : " .. caster.yuyiskill4)
		caster:RemoveTag("dilie")
--		caster.components.talker:Say("Skills [K]! ",nil,true)

		caster:RemoveTag("yuyiskill2")
--		caster.yuyidmgcomb = caster.yuyidmgcomb + 1
		caster.components.colourtweener:StartTween({1,1,1,1}, 0)

		caster.AnimState:PlayAnimation("shovel_loop")
		caster.SoundEmitter:PlaySound("dontstarve/creatures/deerclops/ice_large")
		local dilie = SpawnPrefab("dilie")
		local x2, y2, z2 = v:GetPosition():Get()
		dilie.Transform:SetPosition(x2, y2, z2)
		dilie.Transform:SetScale(3.5, 3.5, 3.5)


	if caster:HasTag("Ragnarok") then

		local pos = Vector3(v.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, caster.yuyilevel/15+2.5)
		for k,v in pairs(ents) do
			if v and v:HasTag("yuyitarget") and not v:HasTag("yuyi") and not v:HasTag("myfriend") and not (v.components.inventoryitem and v.components.inventoryitem.owner) then

		if v then v.AnimState:SetMultColour(255/255,255/255,0/255,1) end
		if v and v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, diliedmg) end
		if v and v.components.combat then v.components.combat:BlankOutAttacks(1.5) v.components.combat:SetTarget(nil) end
		if v.brain then v.brain:Stop() end
		if v.components.locomotor then v.components.locomotor:Stop() end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end


				local x2, y2, z2 = v:GetPosition():Get()
				local sign=1
				v.task1 = v:DoPeriodicTask(0.05,  function()
					local x3, y3, z3 = v:GetPosition():Get()
					v:AddTag("fukong")
					v.Transform:SetPosition(x2, y3+0.35*sign, z2)
				end)
				v:DoTaskInTime(0.5, function() 
					sign=-1
				end)
				v:DoTaskInTime(1, function()
					if v and v.task1 then
					v.task1:Cancel()
					v.task1 = nil
					end
					v:RemoveTag("fukong")
					v.Transform:SetPosition(x2, y2, z2)
				end)

	if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
	v:DoTaskInTime(1.5, function()
	if v.brain then v.brain:Start() end
	if v.components.freezable and v.components.freezable:IsFrozen() then v:PushEvent("unfreeze") end
	if v then v.components.combat:SetTarget(caster) end
	if v then v.AnimState:SetMultColour(255/255,255/255,255/255,1) end
	end)
	end

	end
	end

	elseif not caster:HasTag("Ragnarok") then
		if v then v.AnimState:SetMultColour(255/255,255/255,0/255,1) end
		if v and v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, diliedmg) end
		if v and v.components.combat then v.components.combat:BlankOutAttacks(1.5) v.components.combat:SetTarget(nil) end
		if v.brain then v.brain:Stop() end
		if v.components.locomotor then v.components.locomotor:Stop() end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end


				local x2, y2, z2 = v:GetPosition():Get()
				local sign=1
				v.task1 = v:DoPeriodicTask(0.05,  function()
					local x3, y3, z3 = v:GetPosition():Get()
					v:AddTag("fukong")
					v.Transform:SetPosition(x2, y3+0.35*sign, z2)
				end)
				v:DoTaskInTime(0.5, function() 
					sign=-1
				end)
				v:DoTaskInTime(1, function()
					if v and v.task1 then
					v.task1:Cancel()
					v.task1 = nil
					end
					v:RemoveTag("fukong")
					v.Transform:SetPosition(x2, y2, z2)
				end)

	if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
	v:DoTaskInTime(1.5, function()
	if v.brain then v.brain:Start() end
	if v.components.freezable and v.components.freezable:IsFrozen() then v:PushEvent("unfreeze") end
	if v then v.components.combat:SetTarget(caster) end
	if v then v.AnimState:SetMultColour(255/255,255/255,255/255,1) end
	end)
	end

	end

		caster:DoTaskInTime(1.00, function()
		caster.sg:GoToState("idle")
		caster.components.health:SetInvincible(false)
		caster.components.playercontroller:Enable(true)
		caster:RemoveTag("xinjineng")
		end)

	end
end


function Yuyicast:luoyan_attack(caster)
	local v = self:get_yuyitarget(caster)
	if v and v~=nil and self.canuseskill and caster:HasTag("luoyan") then
	local luoyandmg = self:yuyi_skilldmg(caster)
--		caster.components.combat:SetTarget(nil)
		caster:ClearBufferedAction()
		caster.components.playercontroller:Enable(false)
		caster.components.locomotor:Stop()
		caster:ForceFacePoint(v:GetPosition():Get())
		caster.components.health:SetInvincible(true)

		self:checkskillscd(caster)
		caster.yuyiskill5 = 40*caster.yuyiskillcd
		caster.HUD.luoyan:Show()
		caster.HUD.luoyan:SetOnClick(nil)
		caster.HUD.luoyan:SetTooltip(TUNING.LUOYAN.."CD : " .. caster.yuyiskill5)
		caster:RemoveTag("luoyan")
--		caster.components.talker:Say("Skills [L]! ",nil,true)

		caster:RemoveTag("yuyiskill2")
--		caster.yuyidmgcomb = caster.yuyidmgcomb + 1
		caster.components.colourtweener:StartTween({1,1,1,1}, 0)

		caster.AnimState:PlayAnimation("pickaxe_loop")
		caster.SoundEmitter:PlaySound("dontstarve_DLC001/creatures/glommer/foot_ground")
		caster.components.playercontroller:ShakeCamera(caster, "FULL", 0.7, 0.05, .5, 40)

		local boulder = SpawnPrefab("thulecite")
		RemovePhysicsColliders(boulder)
		boulder.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
		boulder.Transform:SetScale(3,3,3)
		boulder:RemoveComponent("inventoryitem")
		boulder.Transform:SetPosition(v.Transform:GetWorldPosition())
		boulder:DoTaskInTime(0.5, function()
			SpawnPrefab("ground_chunks_breaking").Transform:SetPosition(boulder.Transform:GetWorldPosition())
			boulder:Remove()
		end )
		local blood = SpawnPrefab("splash")
		blood.Transform:SetPosition(v.Transform:GetWorldPosition())
		blood.AnimState:SetMultColour(255/255,0/255,0/255,1)
		blood.Transform:SetScale(2, 2, 2)

	if caster:HasTag("Ragnarok") then

		local pos = Vector3(v.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, caster.yuyilevel/15+2.5)
		for k,v in pairs(ents) do
			if v and v:HasTag("yuyitarget") and not v:HasTag("yuyi") and not v:HasTag("myfriend") and not (v.components.inventoryitem and v.components.inventoryitem.owner) then

		if v then v.AnimState:SetMultColour(255/255,0/255,0/255,1) end
		if v and v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, luoyandmg) end
		if v and v.components.combat then v.components.combat:BlankOutAttacks(1.5) v.components.combat:SetTarget(nil) end
		if v.brain then v.brain:Stop() end
		if v.components.locomotor then v.components.locomotor:Stop() end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end

	if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
	v:DoTaskInTime(1.5, function()
	if v.brain then v.brain:Start() end
	if v.components.freezable and v.components.freezable:IsFrozen() then v:PushEvent("unfreeze") end
	if v then v.components.combat:SetTarget(caster) end
	if v then v.AnimState:SetMultColour(255/255,255/255,255/255,1) end
	end)
	end
end
end
	elseif not caster:HasTag("Ragnarok") then
		if v then v.AnimState:SetMultColour(255/255,0/255,0/255,1) end
		if v and v.components.health and not (v.components.inventoryitem and v.components.inventoryitem.owner) then v.components.combat:GetAttacked(caster, luoyandmg) end
		if v and v.components.combat then v.components.combat:BlankOutAttacks(1.5) v.components.combat:SetTarget(nil) end
		if v.brain then v.brain:Stop() end
		if v.components.locomotor then v.components.locomotor:Stop() end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.hit and not v.components.health:IsDead() then v.sg:GoToState("hit") end
		if v.sg and v.sg.sg and v.sg.sg.states and v.sg.sg.states.idle and not v.components.health:IsDead() then v.sg:GoToState("idle") end

	if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
	v:DoTaskInTime(1.5, function()
	if v.brain then v.brain:Start() end
	if v.components.freezable and v.components.freezable:IsFrozen() then v:PushEvent("unfreeze") end
	if v then v.components.combat:SetTarget(caster) end
	if v then v.AnimState:SetMultColour(255/255,255/255,255/255,1) end
	end)
	end
end
		caster:DoTaskInTime(0.75, function()
		caster.sg:GoToState("idle")
		caster.components.health:SetInvincible(false)
		caster.components.playercontroller:Enable(true)
		caster:RemoveTag("xinjineng")
		end)

	end
end



--[[

SpawnPrefab("shock_fx")

		local xuanfeng = SpawnPrefab("xuanfeng")
		local x2, y2, z2 = v:GetPosition():Get()
		xuanfeng.Transform:SetPosition(x2, y2+1, z2)
		xuanfeng.Transform:SetScale(5, 5, 5)
]]


return Yuyicast