local MakePlayerCharacter = require "prefabs/player_common"
local ImageButton = require "widgets/imagebutton"
local YuyiScreen = require "screens/yuyiscreen"
local Text = require "widgets/text"
local ModsScreen = require "screens/modsscreen"

local assets = {
        Asset( "ANIM", "anim/player_basic.zip" ),
        Asset( "ANIM", "anim/player_idles_shiver.zip" ),
        Asset( "ANIM", "anim/player_actions.zip" ),
        Asset( "ANIM", "anim/player_actions_axe.zip" ),
        Asset( "ANIM", "anim/player_actions_pickaxe.zip" ),
        Asset( "ANIM", "anim/player_actions_shovel.zip" ),
        Asset( "ANIM", "anim/player_actions_blowdart.zip" ),
        Asset( "ANIM", "anim/player_actions_eat.zip" ),
        Asset( "ANIM", "anim/player_actions_item.zip" ),
        Asset( "ANIM", "anim/player_actions_uniqueitem.zip" ),
        Asset( "ANIM", "anim/player_actions_bugnet.zip" ),
        Asset( "ANIM", "anim/player_actions_fishing.zip" ),
        Asset( "ANIM", "anim/player_actions_boomerang.zip" ),
        Asset( "ANIM", "anim/player_bush_hat.zip" ),
        Asset( "ANIM", "anim/player_attacks.zip" ),
        Asset( "ANIM", "anim/player_idles.zip" ),
        Asset( "ANIM", "anim/player_rebirth.zip" ),
        Asset( "ANIM", "anim/player_jump.zip" ),
        Asset( "ANIM", "anim/player_amulet_resurrect.zip" ),
        Asset( "ANIM", "anim/player_teleport.zip" ),
        Asset( "ANIM", "anim/wilson_fx.zip" ),
        Asset( "ANIM", "anim/player_one_man_band.zip" ),
        Asset( "ANIM", "anim/shadow_hands.zip" ),
        Asset( "SOUND", "sound/sfx.fsb" ),
        Asset( "SOUND", "sound/wilson.fsb" ),
        Asset( "ANIM", "anim/beard.zip" ),

		Asset( "IMAGE", "images/yuyitabs/yuyitab.tex"),
		Asset( "ATLAS", "images/yuyitabs/yuyitab.xml"),
		Asset("SOUNDPACKAGE", 	"sound/yuyi_attack.fev"),
		Asset("SOUND", 			"sound/yuyi_attack.fsb"),
        Asset( "ANIM", "anim/yuyi.zip" ),
        Asset( "ANIM", "anim/yuyi_dark.zip" ),
		Asset( "ANIM", "anim/cat_yuyi.zip" ),
--		Asset( "ANIM", "anim/dragon_yuyi.zip" ),
}
local prefabs = {}

local start_inv = {}

local function spawnoffscreen(pt)
	local theta = math.random() * 2 * PI
	local radius = 15
	local offset = FindWalkableOffset(pt, theta, radius, 12, true)
	if offset then return pt+offset end
end

local function SetHUDState(inst, data)
	if inst.HUD then
		local nandusize = 25
		for _, moddir in ipairs(KnownModIndex:GetModsToLoad()) do
		if KnownModIndex:GetModInfo(moddir).name == "Chinese" then nandusize = 25 end
		if KnownModIndex:GetModInfo(moddir).name == "Character yiyu" then
			TUNING.YIYUISOPEN = 1
			if TUNING.DIFFICULTY then TUNING.DIFFICULTY_YUYI = TUNING.DIFFICULTY end
			if TUNING.YIYU_MONSTERBUFF then TUNING.YIYU_MONSTERBUFF_YUYI = TUNING.YIYU_MONSTERBUFF end
			if TUNING.ATTACKDAY then TUNING.ATTACKDAY_YUYI = TUNING.ATTACKDAY end
		end
		end

		local hehe = inst.HUD.controls.bottomright_root:AddChild(ImageButton("images/selectscreen_portraits/yuyi.xml", "yuyi.tex"))
		hehe:SetPosition(-60,160,0)
		hehe:SetScale(0.4)
		hehe:SetOnClick(function ()
		TheFrontEnd:PushScreen(YuyiScreen())
		end)
		hehe:SetTooltip(STRINGS.YUYI_PLAYER1)

        inst.HUD.dengji = inst.HUD.controls.bottom_root:AddChild(Text(BODYTEXTFONT, nandusize))
		inst.HUD.dengji:SetPosition(-600,50,0)
        inst.HUD.dengji:SetColour(0, 0.6, 1, 1)
        inst.HUD.dengji:SetString(STRINGS.YUYI_PLAYER2..tostring(inst.level))
        inst.HUD.jingyan = inst.HUD.controls.bottom_root:AddChild(Text(BODYTEXTFONT, nandusize))
		inst.HUD.jingyan:SetPosition(-600,25,0)
        inst.HUD.jingyan:SetString(STRINGS.YUYI_PLAYER3..tostring(inst.exp).."/"..tostring(inst.level*TUNING.YUYI_NEEDEXP))
        inst.HUD.jingyan:SetColour(0, 0.6, 1, 1)

		GetPlayer().HUD.lianji = GetPlayer().HUD.controls.bottom_root:AddChild(ImageButton("images/lianji.xml", "lianji.tex"))
		GetPlayer().HUD.lianji:SetPosition(-450,150,0)
		GetPlayer().HUD.lianji:SetScale(0.6)
		GetPlayer().HUD.bleeding = GetPlayer().HUD.controls.topright_root:AddChild(Image("images/bleeding.xml", "bleeding.tex"))
		GetPlayer().HUD.bleeding:SetPosition(-300,-250,0)
		GetPlayer().HUD.bleeding:SetScale(0.6)
		GetPlayer().HUD.slowdown = GetPlayer().HUD.controls.topright_root:AddChild(Image("images/slowdown.xml", "slowdown.tex"))
		GetPlayer().HUD.slowdown:SetPosition(-350,-250,0)
		GetPlayer().HUD.slowdown:SetScale(0.6)
		GetPlayer().HUD.poison = GetPlayer().HUD.controls.topright_root:AddChild(Image("images/poison.xml", "poison.tex"))
		GetPlayer().HUD.poison:SetPosition(-400,-250,0)
		GetPlayer().HUD.poison:SetScale(0.6)
		GetPlayer().HUD.fear = GetPlayer().HUD.controls.topright_root:AddChild(Image("images/fear.xml", "fear.tex"))
		GetPlayer().HUD.fear:SetPosition(-450,-250,0)
		GetPlayer().HUD.fear:SetScale(0.6)
		GetPlayer().HUD.lianji:Hide()
		GetPlayer().HUD.bleeding:Hide()
		GetPlayer().HUD.slowdown:Hide()
		GetPlayer().HUD.poison:Hide()
		GetPlayer().HUD.fear:Hide()

        inst.HUD.tishi = inst.HUD.controls.bottom_root:AddChild(Text(BODYTEXTFONT, nandusize))
		inst.HUD.tishi:SetPosition(0,100,0)
        inst.HUD.tishi:SetColour(1, 0.7, 0, 1)
        inst.HUD.tishi:SetString(string.format("[enemy:%d]",inst.enemy))
		GetPlayer().HUD.tishi:Hide()

        inst.HUD.yuyi_nandu = inst.HUD.controls.topright_root:AddChild(Text(BODYTEXTFONT, nandusize))
		inst.HUD.yuyi_nandu:SetPosition(-350,-50,0)
        inst.HUD.yuyi_nandu:SetColour(0, 0.6, 1, 1)
        inst.HUD.yuyi_nandu:SetString(STRINGS.YUYI_PLAYER4..tostring(TUNING.DIFFICULTY_YUYI))
		inst.HUD.yuyi_nandu:Hide()

        inst.HUD.yuyi_buff = inst.HUD.controls.topright_root:AddChild(Text(BODYTEXTFONT, nandusize))
		inst.HUD.yuyi_buff:SetPosition(-350,-75,0)
        inst.HUD.yuyi_buff:SetColour(0, 0.6, 1, 1)
        inst.HUD.yuyi_buff:SetString(STRINGS.YUYI_PLAYER5..tostring(TUNING.YIYU_MONSTERBUFF_YUYI))
		inst.HUD.yuyi_buff:Hide()

        inst.HUD.yuyi_laixi = inst.HUD.controls.topright_root:AddChild(Text(BODYTEXTFONT, nandusize))
		inst.HUD.yuyi_laixi:SetPosition(-350,-100,0)
        inst.HUD.yuyi_laixi:SetColour(0, 0.6, 1, 1)
        inst.HUD.yuyi_laixi:SetString(STRINGS.YUYI_PLAYER6..tostring(TUNING.ATTACKDAY))
		inst.HUD.yuyi_laixi:Hide()

		inst.HUD.tiaozheng = inst.HUD.controls.bottom_root:AddChild(ImageButton("images/tiaozheng.xml", "tiaozheng.tex"))
		inst.HUD.tiaozheng:SetPosition(-500,35,0)
		inst.HUD.tiaozheng:SetScale(0.6)
		inst.HUD.tiaozheng:SetTooltip(STRINGS.YUYI_PLAYER7)
        inst.HUD.tiaozheng:SetOnClick(function()
		inst.components.talker.colour = Vector3(1, 0.75, 1, 1)
		inst.components.talker.fontsize = 26
		inst.components.talker:Say(STRINGS.YUYI_PLAYER8)
		inst.HUD.tiaozheng:Hide()
		end)

		if TUNING.YIYUISOPEN == 1 then
		inst.HUD.yuyi_nandu:Show()
		inst.HUD.yuyi_buff:Show()
		inst.HUD.tiaozheng:Hide()
--		inst.HUD.tiaozheng:Show()
		end
	end
end

local function applyupgrades(inst)
	local health_yiyuweapon = 0
	local health_yiyuarmor = 0
	local health_yiyuhat = 0
	local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	local armor = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
	local yiyuhat = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
	if weapon and weapon.weapon_health then health_yiyuweapon = weapon.weapon_health end
	if armor and armor.armor_health then health_yiyuarmor = armor.armor_health end
	if yiyuhat and yiyuhat.armor_health then health_yiyuhat = yiyuhat.armor_health end

    local hunger_percent = inst.components.hunger:GetPercent()
    local health_percent = inst.components.health:GetPercent()
    local sanity_percent = inst.components.sanity:GetPercent()

	if inst.exp >= inst.level*TUNING.YUYI_NEEDEXP and inst.level < 25 then
	inst.exp = math.floor(inst.exp - inst.level*TUNING.YUYI_NEEDEXP)
	inst.level = inst.level + 1
	inst.yiyulevel = inst.level
	end

	inst.components.hunger.max = math.ceil(TUNING.YUYI_STARTHP+inst.level*5-5)
	inst.components.health.maxhealth = math.ceil(TUNING.YUYI_STARTHP+inst.level*5+health_yiyuweapon+health_yiyuarmor+health_yiyuhat-5)
	inst.components.sanity.max = math.ceil(TUNING.YUYI_STARTHP+inst.level*5-5)

	if inst.HUD then
	inst.HUD.dengji:SetString(STRINGS.YUYI_PLAYER2..tostring(inst.level))
	inst.HUD.jingyan:SetString(STRINGS.YUYI_PLAYER3..tostring(inst.exp).."/"..tostring(inst.level*TUNING.YUYI_NEEDEXP))
	end

	inst.components.health:SetPercent(health_percent)
	inst.components.sanity:SetPercent(sanity_percent)
	inst.components.hunger:SetPercent(hunger_percent)
	inst.components.health:DoDelta(0)
	inst.components.hunger:DoDelta(0)
	inst.components.sanity:DoDelta(0)

end

local function oneat(inst, food)
		inst.components.talker.colour = Vector3(1, 0.75, 1, 1)
		inst.components.talker.fontsize = 26
		inst.HUD.controls.status.heart:PulseGreen()
		inst.HUD.controls.status.stomach:PulseGreen()
		inst.HUD.controls.status.brain:PulseGreen()
		inst.HUD.controls.status.brain:ScaleTo(1.3,1,.7)
		inst.HUD.controls.status.heart:ScaleTo(1.3,1,.7)
		inst.HUD.controls.status.stomach:ScaleTo(1.3,1,.7)
	 if food and food.prefab == "strawberry_a" then
		inst.exp = inst.exp + 50
		applyupgrades(inst)
		inst.components.talker:Say(STRINGS.YUYI_PLAYER9)
		if not inst:HasTag("treat") then
			inst:DoTaskInTime(15, function() inst:RemoveTag("treat") end)
			inst:AddTag("treat")
		end
	elseif food and food.prefab == "strawberry_b" then
		inst.exp = inst.exp + 50
		applyupgrades(inst)
		inst.components.talker:Say(STRINGS.YUYI_PLAYER9)
	elseif food and food.prefab == "strawberry_c" then
		inst.exp = inst.exp + 50
		applyupgrades(inst)
		inst.components.talker:Say(STRINGS.YUYI_PLAYER9)
	elseif food and food.prefab == "strawberry_d" then
		inst.exp = inst.exp + 50
		applyupgrades(inst)
		inst.components.talker:Say(STRINGS.YUYI_PLAYER9)
	end
end

local function onkill(inst, data)
    if inst and data and data.cause == inst.prefab 
        and not data.inst:HasTag("prey")
        and not data.inst:HasTag("veggie") 
        and not data.inst:HasTag("structure")
		and not data.inst:HasTag("wall")
		and data.inst.components.health
		and data.inst.components.health.maxhealth
		and data.inst.components.combat.defaultdamage then
		local expnum = data.inst.components.health.maxhealth/10+data.inst.components.combat.defaultdamage
	if data.inst.components.health.maxhealth <= 100 then
		expnum = (data.inst.components.health.maxhealth/10+data.inst.components.combat.defaultdamage)/2
	elseif data.inst.components.health.maxhealth >= 1000 then
		expnum = (data.inst.components.health.maxhealth/10+data.inst.components.combat.defaultdamage)*2
	end
		local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		local x, y, z = inst.Transform:GetWorldPosition()
		local pooplist ={"lightbulb","goldnugget","ice","ice","ice","poop","poop","poop","guano","goatmilk"}
		local poops = pooplist[math.random(#pooplist)]
		if weapon and (weapon.prefab == "haihun" or weapon.prefab == "longya" or weapon.prefab == "xueyin") then
		inst.components.sanity:DoDelta(2)
		end
		if math.random() < 0.25 and weapon and (weapon.prefab == "longya" or weapon.prefab == "xueyin") and not data.inst.prefab == "butterfly" then
		local poop = SpawnPrefab(poops)
		poop.Transform:SetPosition(x-math.random(2), y, z-math.random(2))
		end
--蜜蜂100/10+10   =20 *2=40 = 10
--蜘蛛100/10+20   =30 *2=60 = 15
--猎犬150/10+20   =35 *2=70
--猪人250/10+33   =58 *2=116
--影怪400/10+50   =90 *2=180
--高鸟400/10+50   =90 *2=180
--毛牛500/10+34   =84 *2=168
--触手500/10+34   =84 *2=168
--女王1250/10+80  =205*2=410
--树精2000/10+150 =350*2=700
--巨鹿2000/10+150 =350*2=700
--鸭子3000/10+150 =450*2=900
--龙蝇2750/10+150 =425*2=850
--熊大3000/10+200 =500*2=1000
		inst.exp = math.floor(inst.exp + expnum)
		applyupgrades(inst)
	end
end

local function onload(inst, data)
	if data then
		if data.level then
			inst.exp = data.exp
			inst.enemy = data.enemy
			inst.level = data.level
			inst.leiting = data.leiting
			inst.guangmang = data.guangmang
			inst.yiyulevel = data.yiyulevel
			inst.yuyi_profession = data.yuyi_profession
			inst.skill_RLv = data.skill_RLv
			inst.skill_JLv0 = data.skill_JLv0
			inst.skill_KLv0 = data.skill_KLv0
			inst.skill_LLv0 = data.skill_LLv0
			inst.skill_ULv0 = data.skill_ULv0
			inst.skill_ILv0 = data.skill_ILv0
			inst.skill_OLv0 = data.skill_OLv0
			inst.skill_JLv1 = data.skill_JLv1
			inst.skill_KLv1 = data.skill_KLv1
			inst.skill_LLv1 = data.skill_LLv1
			inst.skill_ULv1 = data.skill_ULv1
			inst.skill_ILv1 = data.skill_ILv1
			inst.skill_OLv1 = data.skill_OLv1
			applyupgrades(inst)
	if inst.yuyi_profession == 0 then
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("yuyi")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)
		inst.components.combat.damagemultiplier = 0.35
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
		inst.components.sanity.night_drain_mult = 1
		inst.components.sanity.neg_aura_mult = 1
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED+inst.skill_RLv*0.5--4
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED+inst.skill_RLv*0.5--6
	elseif inst.yuyi_profession == 1 then
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("yuyi_dark")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)
		inst.components.combat.damagemultiplier = 0.80+inst.skill_RLv*0.05
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
		inst.components.sanity.night_drain_mult = 1.25
		inst.components.sanity.neg_aura_mult = 1.25
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED--4
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED--6
	end
		end
	end
end

local function onsave(inst, data)
	data.exp = inst.exp
	data.level = inst.level
	data.enemy = inst.enemy
	data.leiting = inst.leiting
	data.guangmang = inst.guangmang
	data.yiyulevel = inst.yiyulevel
	data.yuyi_profession = inst.yuyi_profession
	data.skill_RLv = inst.skill_RLv
	data.skill_JLv0 = inst.skill_JLv0
	data.skill_KLv0 = inst.skill_KLv0
	data.skill_LLv0 = inst.skill_LLv0
	data.skill_ULv0 = inst.skill_ULv0
	data.skill_ILv0 = inst.skill_ILv0
	data.skill_OLv0 = inst.skill_OLv0
	data.skill_JLv1 = inst.skill_JLv1
	data.skill_KLv1 = inst.skill_KLv1
	data.skill_LLv1 = inst.skill_LLv1
	data.skill_ULv1 = inst.skill_ULv1
	data.skill_ILv1 = inst.skill_ILv1
	data.skill_OLv1 = inst.skill_OLv1
end


local function WeaponDroppeds(inst)
	inst.components.weapon:SetProjectile(nil)
	GetPlayer():RemoveTag("banyuewandao")
	GetPlayer().SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/banyue")
	GetPlayer().components.talker:Say(STRINGS.YUYI_PLAYER10)
end

local function WeaponDropped(inst)
    inst:Remove()
end

local function OnAttacked(inst, data)
    local attacker = data and data.attacker
	local yiyuhat = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)

	if inst.HUD.lianji then
	inst.yiyudmglast = 0
	inst.yiyudmgcomb = 0
	inst.HUD.lianji:Hide()
	inst.yiyudmgpercent = 1.0
	if inst.yuyi_profession == 1 then
		inst.components.combat.damagemultiplier = 0.80+inst.skill_RLv*0.05
	elseif inst.yuyi_profession == 0 then
		inst.components.combat.damagemultiplier = 0.35
	end
	if SaveGameIndex:IsModeShipwrecked() then
		inst.components.combat:RemoveDamageModifier("yiyu")
		inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
	end
	end


	inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/hit")
	inst.thorns_yiyu = inst.helmet_yiyu_thorns+inst.corselet_yiyu_thorns
	if attacker and attacker.components.health and attacker.components.combat and not attacker.components.health:IsDead() and inst.thorns_yiyu > 0 then
		attacker.components.combat:GetAttacked(inst, inst.thorns_yiyu)
	end
	if yiyuhat and yiyuhat.yiyu_hatstate then
    local pt = Vector3(inst.Transform:GetWorldPosition())
    local spawn_pt = spawnoffscreen(pt)
    if spawn_pt then
		inst.Transform:SetPosition(spawn_pt:Get())
		SpawnPrefab("statue_transition").Transform:SetPosition(spawn_pt:Get())
    end
	end
end

local yuyifn = function(inst)
	inst.exp = 0
	inst.level = 1
	inst.enemy = 0
	inst.leiting = 0
	inst.guangmang = 0
	inst.yiyulevel = 1
	inst.yuyi_profession = 0
	inst.yuyi_states = 0
	inst.skill_RLv = 1
	inst.skill_JLv0 = 0
	inst.skill_KLv0 = 0
	inst.skill_LLv0 = 0
	inst.skill_ULv0 = 0
	inst.skill_ILv0 = 0
	inst.skill_OLv0 = 0
	inst.skill_JLv1 = 0
	inst.skill_KLv1 = 0
	inst.skill_LLv1 = 0
	inst.skill_ULv1 = 0
	inst.skill_ILv1 = 0
	inst.skill_OLv1 = 0

--[[
	inst.skill_JLv0 = 1
	inst.skill_KLv0 = 1
	inst.skill_LLv0 = 1
	inst.skill_ULv0 = 1
	inst.skill_ILv0 = 1
	inst.skill_OLv0 = 1
	inst.skill_JLv1 = 1
	inst.skill_KLv1 = 1
	inst.skill_LLv1 = 1
	inst.skill_ULv1 = 1
	inst.skill_ILv1 = 1
	inst.skill_OLv1 = 1

]]

	inst.skill_RCD = 0
	inst.skill_JCD0 = 0
	inst.skill_KCD0 = 0
	inst.skill_LCD0 = 0
	inst.skill_UCD0 = 0
	inst.skill_ICD0 = 0
	inst.skill_OCD0 = 0
	inst.skill_JCD1 = 0
	inst.skill_KCD1 = 0
	inst.skill_LCD1 = 0
	inst.skill_UCD1 = 0
	inst.skill_ICD1 = 0
	inst.skill_OCD1 = 0

	inst.leidianshu_last = 0
	inst.baofengxue_last = 0
	inst.zhurijianfa_last = 0
	inst.huoqiang_last = 0
	inst.liehuojianfa_last = 0
	inst.duanjin_last = 0
	inst.mofadun_last = 0
	inst.yiyudmglast = 0
	inst.yiyudmgcomb = 0
	inst.yiyudmgpercent = 0
	inst.yiyuswdmgpercent = 0

	inst.god_bless = 0-- 神佑
	inst.drop_yiyu = 0--卸甲
	inst.dodge_yiyu = 0 --总闪避
	inst.thorns_yiyu = 0 --总反伤
	inst.corselet_yiyu = 0--吸收
	inst.corselet_yiyu_fire = 0--火免
	inst.corselet_yiyu_cold = 0--冰免
	inst.corselet_yiyu_dodge = 0--闪避
	inst.corselet_yiyu_winter = 0--保温
	inst.corselet_yiyu_summer = 0--隔热
	inst.corselet_yiyu_drop = 0--卸甲
	inst.corselet_yiyu_thorns = 0--反伤

	inst.helmet_yiyu = 0
	inst.helmet_yiyu_fire = 0
	inst.helmet_yiyu_cold = 0
	inst.helmet_yiyu_dodge = 0--闪避
	inst.helmet_yiyu_winter = 0
	inst.helmet_yiyu_summer = 0
	inst.helmet_yiyu_drop = 0--卸甲
	inst.helmet_yiyu_thorns = 0--反伤

	inst.soundsname = "willow"
	inst.MiniMapEntity:SetIcon( "yuyi.tex" )
	inst.components.health:SetMaxHealth(TUNING.YUYI_STARTHP)
	inst.components.hunger:SetMax(TUNING.YUYI_STARTHP)
	inst.components.sanity:SetMax(TUNING.YUYI_STARTHP)
	inst.components.combat.damagemultiplier = 0.35
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end

	inst.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE*TUNING.YUYI_HUNGERRATE
	inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED+inst.skill_RLv*0.5--4
	inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED+inst.skill_RLv*0.5--6
	inst:AddTag("yuyi")
	inst:AddTag("player")
	inst:AddComponent("reader")
	inst:AddComponent("yuyicast")
	inst:AddComponent("yiyucast")
	inst:AddComponent("lootdropper")
	inst:AddComponent("colourtweener")
	inst.components.eater:SetOnEatFn(oneat)
--	inst.components.combat.onhitotherfn = onhit
	if TUNING.CREEPINTHEDEEPS then
	inst:AddComponent("tidalmanager")
	inst:AddComponent("oxygen")
	inst.components.oxygen.max = 100
	end

TheInput:AddKeyUpHandler(KEY_P, function()
		inst.components.talker:Say(
		"\n总神佑["..inst.god_bless.."]"..
		"\n总吸收["..inst.components.health.absorb.."%]"..
		"\n总闪避["..inst.corselet_yiyu_dodge+inst.helmet_yiyu_dodge.."%]"..
		"\n总卸甲["..inst.drop_yiyu .."%]"..
		"\n总火免[".. (1-inst.components.health.fire_damage_scale)*100 .."%]"..
		"\n总冰免["..inst.components.freezable.resistance.."]"..
		"\n总保温["..inst.components.temperature.inherentinsulation.."]"..
		"\n总隔热["..inst.components.temperature.inherentsummerinsulation.."]")
end)

TheInput:AddKeyUpHandler(TUNING.YUYI_KEY1, function()
	if inst.yuyi_states == 1 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER11)
	elseif inst.skill_RCD > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER12..inst.skill_RCD)
	elseif inst.yuyi_profession == 0 and not IsPaused() then
		inst.yuyi_profession = 1
		inst.skill_RCD = 5
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("yuyi_dark")
--		inst.AnimState:PlayAnimation("powerup")
		inst.sg:GoToState("hide")
		inst.Transform:SetScale(1,1,1)
		inst.components.combat.damagemultiplier = 0.80+inst.skill_RLv*0.05
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
		inst.components.sanity.night_drain_mult = 1.25
		inst.components.sanity.neg_aura_mult = 1.25
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED--4
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED--6
		inst.components.talker:Say(STRINGS.YUYI_PLAYER13)
		inst.SoundEmitter:KillSound("talk")
--		GetPlayer().SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/huxiaoshanlin2","huxiaoshanlin2")
--		GetPlayer().SoundEmitter:SetVolume("huxiaoshanlin2", 10)
		SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
	elseif inst.yuyi_profession == 1 and not IsPaused() then
		inst.skill_RCD = 5
		inst.yuyi_profession = 0
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("yuyi")
--		inst.AnimState:PlayAnimation("hide")
		inst.sg:GoToState("hide")
		inst.Transform:SetScale(1,1,1)
		inst.components.combat.damagemultiplier = 0.35
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
		inst.components.sanity.night_drain_mult = 1
		inst.components.sanity.neg_aura_mult = 1
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED+inst.skill_RLv*0.25+0.75--4
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED+inst.skill_RLv*0.25+0.75--6
		inst.components.talker:Say(STRINGS.YUYI_PLAYER14)
		inst.SoundEmitter:KillSound("talk")
--		GetPlayer().SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/huxiaoshanlin2","huxiaoshanlin2")
--		GetPlayer().SoundEmitter:SetVolume("huxiaoshanlin2", 1)
		SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
	end
end)

TheInput:AddKeyUpHandler(TUNING.YUYI_KEY2, function()
if not IsPaused() and not TheInput:IsKeyDown(KEY_CTRL) and not TheInput:IsKeyDown(KEY_SHIFT) then
	inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
	inst.components.talker.fontsize = 26
	if inst.yuyi_profession == 0 and inst.skill_JCD0 > 0 and inst.skill_JLv0 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER15..inst.skill_JCD0)
	elseif inst.yuyi_profession == 1 and inst.skill_JCD1 > 0 and inst.skill_JLv1 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER16..inst.skill_JCD1)
	elseif inst.yuyi_profession == 0 and not inst:HasTag("leidianshu") and inst.skill_JLv0 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER17)
	elseif inst.yuyi_profession == 1 and not inst:HasTag("liehuojianfa") and inst.skill_JLv1 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER18)
	elseif inst.yuyi_profession == 0 and inst.skill_JCD0 == 0 and inst.skill_JLv0 > 0 and inst.components.sanity.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER19)
	elseif inst.yuyi_profession == 1 and inst.skill_JCD1 == 0 and inst.skill_JLv1 > 0 and inst.components.hunger.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER20)
	elseif 	inst.yuyi_profession == 0 and inst.skill_JCD0 == 0 and not inst:HasTag("leidianshu") and inst.skill_JLv0 > 0 and inst.components.sanity.current >= 8 then
		inst:AddTag("leidianshu")
		inst.skill_JCD0 = 1
		inst.leidianshu_last = 6
		inst.components.sanity:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst.components.combat:SetRange(20, 20)
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/leidianshuqianzou")
		inst.components.talker:Say(STRINGS.YUYI_PLAYER21)
		if inst.light then inst.light:Remove() inst.light = nil end
		inst.light = SpawnPrefab("lightning_rod_fx")
        local follower = inst.light.entity:AddFollower()
        follower:FollowSymbol( inst.GUID, "swap_object", 35, -180, 0 )
--雷电术
	elseif inst.yuyi_profession == 1 and inst.skill_JCD1 == 0 and not inst:HasTag("liehuojianfa") and inst.skill_JLv1 > 0 and inst.components.hunger.current >= 8 then
		inst:AddTag("liehuojianfa")
		inst.components.talker:Say(STRINGS.YUYI_PLAYER22)
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/liehuoqianzou")
		inst.skill_JCD1 = 8
		inst.liehuojianfa_last = 6
		inst.components.hunger:DoDelta(-TUNING.YUYI_SKILLSCOST)
		if inst.fire then inst.fire:Remove() inst.fire = nil end
		inst.fire = SpawnPrefab( "yuyi_fire" )
        local follower = inst.fire.entity:AddFollower()
        follower:FollowSymbol( inst.GUID, "swap_object", 0, 0, 0 )
--烈火剑法
	end
end
end)

TheInput:AddKeyUpHandler(TUNING.YUYI_KEY3, function()
if not IsPaused() and not TheInput:IsKeyDown(KEY_CTRL) and not TheInput:IsKeyDown(KEY_SHIFT) then
	inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
	inst.components.talker.fontsize = 26
	if inst.yuyi_profession == 0 and inst.skill_KCD0 > 0 and inst.skill_KLv0 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER23..inst.skill_KCD0)
	elseif inst.yuyi_profession == 1 and inst.skill_KCD1 > 0 and inst.skill_KLv1 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER24..inst.skill_KCD1)
	elseif inst.yuyi_profession == 0 and not inst:HasTag("mofadun") and inst.skill_KLv0 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER25)
	elseif inst.yuyi_profession == 1 and not inst:HasTag("banyuewandao") and inst.skill_KLv1 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER26)
	elseif inst.yuyi_profession == 0 and inst.skill_KCD0 == 0 and inst.skill_KLv0 > 0 and inst.components.sanity.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER27)
	elseif 	inst.yuyi_profession == 0 and inst.skill_KCD0 == 0 and not inst:HasTag("mofadun") and inst.skill_KLv0 > 0 and inst.components.sanity.current >= 8 then
		inst:AddTag("mofadun")
		inst.skill_KCD0 = 25
		inst.mofadun_last = inst.skill_KLv0*5+5
		inst.components.sanity:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/mofadun")
		if inst.mofadunfx then inst.mofadunfx:Remove() inst.mofadunfx = nil end
		inst.mofadunfx = SpawnPrefab( "forcefieldfx" )
		inst.mofadunfx.entity:SetParent(inst.entity)
		inst.mofadunfx.Transform:SetPosition(0, 0.2, 0)
		inst.mofadunfx.AnimState:PlayAnimation("hit")
		inst.mofadunfx.AnimState:PushAnimation("idle_loop")
		inst.components.talker:Say(STRINGS.YUYI_PLAYER28)--246 369 4812
	elseif inst.yuyi_profession == 1 and not inst:HasTag("banyuewandao") and inst.skill_KLv1 > 0 then
		local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		if weapon and weapon.components.weapon then
			weapon.components.weapon:SetProjectile("banyue")
			weapon.components.inventoryitem:SetOnDroppedFn(WeaponDroppeds)
			inst:AddTag("banyuewandao")
			inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/banyue")
			inst.components.combat.areahitrange = inst.skill_KLv1/2+2.5
			inst.components.combat.areahitdamagepercent = inst.skill_KLv1*0.20
			inst.components.talker:Say(STRINGS.YUYI_PLAYER29)
			inst.skill_KCD1 = 1
		elseif weapon and not weapon.components.weapon then
			inst.components.talker:Say(STRINGS.YUYI_PLAYER30)
		elseif not weapon then
			inst.components.talker:Say(STRINGS.YUYI_PLAYER31)
		end
--特效
--半月弯刀开启
	elseif inst.yuyi_profession == 1 and inst:HasTag("banyuewandao") and inst.skill_KLv1 > 0 then
		local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		inst:RemoveTag("banyuewandao")
		inst.skill_KCD1 = 1
		inst.components.combat.areahitrange = 0
		inst.components.combat.areahitdamagepercent = 0
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/banyue")
		if weapon then weapon.components.weapon:SetProjectile(nil) end
		inst.components.talker:Say(STRINGS.YUYI_PLAYER10)
--半月弯刀关闭
	end
end
end)

TheInput:AddKeyUpHandler(TUNING.YUYI_KEY4, function()
	if not IsPaused() then
--	and not TheInput:IsKeyDown(KEY_CTRL) 
	inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
	inst.components.talker.fontsize = 26
		inst.components.talker:Say(STRINGS.YUYI_PLAYER32)
	if inst.yuyi_profession == 0 and inst.skill_LCD0 > 0 and inst.skill_LLv0 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER33..inst.skill_LCD0)
	elseif inst.yuyi_profession == 1 and inst.skill_LCD1 > 0 and inst.skill_LLv1 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER34..inst.skill_LCD1)
	elseif inst.yuyi_profession == 0 and not inst:HasTag("huoqiang") and inst.skill_LLv0 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER35)
	elseif inst.yuyi_profession == 1 and not inst:HasTag("zhurijianfa") and inst.skill_LLv1 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER36)

	elseif inst.yuyi_profession == 0 and inst.skill_LCD0 == 0 and inst.skill_LLv0 > 0 and inst.components.sanity.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER37)
	elseif inst.yuyi_profession == 1 and inst.skill_LCD1 == 0 and inst.skill_LLv1 > 0 and inst.components.hunger.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER38)
	elseif 	inst.yuyi_profession == 0 and inst.skill_LCD0 == 0 and not inst:HasTag("huoqiang") and inst.skill_LLv0 > 0 and inst.components.sanity.current >= 8 and not IsPaused() then
		inst:AddTag("huoqiang")
		inst.components.sanity:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_firestaff")
		inst.components.yuyicast:areaskill(inst)
		inst.skill_LCD0 = 1
--火墙
	elseif inst.yuyi_profession == 1 and inst.skill_LCD1 == 0 and not inst:HasTag("zhurijianfa") and inst.skill_LLv1 > 0 and inst.components.hunger.current >= 8 and not IsPaused() then
		inst:AddTag("zhurijianfa")
		inst.skill_LCD1 = 8
		inst.zhurijianfa_last = 6
		inst.components.hunger:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/zhurijianfa1")
		inst.components.combat:SetRange(20, 20)
--特效
--逐日剑法
	end
end
end)

TheInput:AddKeyUpHandler(TUNING.YUYI_KEY5, function()
if not IsPaused() and not TheInput:IsKeyDown(KEY_CTRL) and not TheInput:IsKeyDown(KEY_SHIFT) then
	inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
	inst.components.talker.fontsize = 26
	local weapon = inst.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

	if inst.yuyi_profession == 0 and inst.skill_UCD0 > 0 and inst.skill_ULv0 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER39..inst.skill_UCD0)
	elseif inst.yuyi_profession == 1 and inst.skill_UCD1 > 0 and inst.skill_ULv1 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER40..inst.skill_UCD1)
	elseif inst.yuyi_profession == 0 and not inst:HasTag("diyuleiguang") and inst.skill_ULv0 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER41)
	elseif inst.yuyi_profession == 1 and not inst:HasTag("huxiaoshanlin") and inst.skill_ULv1 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER42)

	elseif inst.yuyi_profession == 0 and inst.skill_UCD0 == 0 and inst.skill_ULv0 > 0 and inst.components.sanity.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER43)
	elseif inst.yuyi_profession == 1 and inst.skill_UCD1 == 0 and inst.skill_ULv1 > 0 and inst.components.hunger.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER44)
	elseif 	inst.yuyi_profession == 0 and inst.skill_UCD0 == 0 and not inst:HasTag("diyuleiguang") and inst.skill_ULv0 > 0 and inst.components.sanity.current >= 8 then
		inst:AddTag("diyuleiguang")
		inst.components.sanity:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/diyuleiguang")
	inst.skill_UCD0 = 5
	local range = 6
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, range)
	inst.sg:GoToState("idle")
    inst:ClearBufferedAction()
    inst.components.locomotor:Stop()
    inst.components.playercontroller:Enable(false)
	inst.components.health:SetInvincible(true)
    inst.AnimState:PlayAnimation("jump")
	inst.Physics:SetMotorVelOverride(0,2,0)
	inst:DoTaskInTime(0.35, function()
	inst.Physics:ClearMotorVelOverride()
	inst.Physics:SetMotorVelOverride(0,-2,0)
	end)
    inst:DoTaskInTime(0.5, function()
	inst.Physics:ClearMotorVelOverride()
	inst.Physics:SetMotorVelOverride(0, 0, 0)
	inst.AnimState:PlayAnimation("atk")
	local fx = SpawnPrefab("diyuleiguang")
	fx.Transform:SetScale(3.5,3.5,3.5)
	fx.Transform:SetPosition(inst:GetPosition():Get())
	end)

	inst:DoTaskInTime(0.55, function()
	inst.components.health:SetInvincible(false)
		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("wall") and not (v.components.inventoryitem 
		and v.components.inventoryitem.owner) and not (v.components.follower 
		and v.components.follower.leader == GetPlayer()) then
			v.components.combat:GetAttacked(inst, inst.skill_ULv0*15+weapondmg*0.5)
		end
		end
	end)
	inst:DoTaskInTime(0.60, function()
	inst.components.locomotor:Stop()
	inst.sg:GoToState("idle")
	inst.components.playercontroller:Enable(true)
	inst:RemoveTag("diyuleiguang")
	end)

--地狱雷光

	elseif inst.yuyi_profession == 1 and inst.skill_UCD1 == 0 and not inst:HasTag("huxiaoshanlin") and inst.skill_ULv1 > 0 and inst.components.hunger.current >= 10 then
		inst:AddTag("huxiaoshanlin")
	if math.random() < 0.2 then
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/huxiaoshanlin2")
	else
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/huxiaoshanlin")
	end
	inst.skill_UCD1 = 5
	inst.components.hunger:DoDelta(-TUNING.YUYI_SKILLSCOST)
	local range = 6
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, range)
	inst.sg:GoToState("idle")
    inst:ClearBufferedAction()
    inst.components.locomotor:Stop()
    inst.components.playercontroller:Enable(false)
	inst.components.health:SetInvincible(true)
    inst.AnimState:PlayAnimation("jump")--play_onemanband
	local fx = SpawnPrefab("huxiaoshanlin")
	fx.Transform:SetScale(3.5,3.5,3.5)
	fx.Transform:SetPosition(inst:GetPosition():Get())
	inst.Physics:SetMotorVelOverride(0,2,0)
	inst:DoTaskInTime(0.35, function()
	inst.Physics:ClearMotorVelOverride()
	inst.Physics:SetMotorVelOverride(0,-2,0)
	end)
    inst:DoTaskInTime(0.5, function()
	inst.Physics:ClearMotorVelOverride()
	inst.Physics:SetMotorVelOverride(0, 0, 0)
	inst.AnimState:PlayAnimation("atk")
	end)

	inst:DoTaskInTime(0.55, function()
	inst.components.health:SetInvincible(false)
		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("wall") and not (v.components.inventoryitem 
		and v.components.inventoryitem.owner) and not (v.components.follower 
		and v.components.follower.leader == GetPlayer()) then
		v.components.combat:GetAttacked(inst, inst.skill_ULv1*10+weapondmg*0.25)
		v.components.combat:BlankOutAttacks(3)
		v.components.combat:SetTarget(nil)
		if v and v.brain then v.brain:Stop() end
		if v and v.components.locomotor then v.components.locomotor:Stop() end

		if v and v.components.combat and v.components.health and not v.components.health:IsDead() then
			v:DoTaskInTime(3, function()
				if v.brain then v.brain:Start() end
				if v then v.components.combat:SetTarget(inst) end
			end)
		end

		end
		end
	end)
	inst:DoTaskInTime(0.60, function()
	inst.components.locomotor:Stop()
	inst.sg:GoToState("idle")
	inst.components.playercontroller:Enable(true)
	inst:RemoveTag("huxiaoshanlin")
	end)
--虎啸山林
	end
end
end)


TheInput:AddKeyUpHandler(TUNING.YUYI_KEY6, function()
if not IsPaused() and not TheInput:IsKeyDown(KEY_CTRL) and not TheInput:IsKeyDown(KEY_SHIFT) then
	inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
	inst.components.talker.fontsize = 26
	if inst.yuyi_profession == 0 and inst.skill_ICD0 > 0 and inst.skill_ILv0 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER45..inst.skill_ICD0)
	elseif inst.yuyi_profession == 1 and inst.skill_ICD1 > 0 and inst.skill_ILv1 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER46..inst.skill_ICD1)
	elseif inst.yuyi_profession == 0 and not inst:HasTag("baofengxue") and inst.skill_ILv0 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER47)
	elseif inst.yuyi_profession == 1 and not inst:HasTag("duanjin") and inst.skill_ILv1 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER48)
	elseif inst.yuyi_profession == 0 and inst.skill_ICD0 == 0 and inst.skill_ILv0 > 0 and inst.components.sanity.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER49)
	elseif inst.yuyi_profession == 1 and inst.skill_ICD1 == 0 and inst.skill_ILv1 > 0 and inst.components.hunger.current < 8 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER50)

	elseif 	inst.yuyi_profession == 0 and inst.skill_ICD0 == 0 and not inst:HasTag("baofengxue") and inst.skill_ILv0 > 0 and inst.components.sanity.current >= 8 then
		inst.components.sanity:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst:AddTag("baofengxue")
		inst.skill_ICD0 = 5
		inst.baofengxue_last = 6
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/baofengxue")
		inst.components.combat:SetRange(20, 20)
		inst.components.talker:Say(STRINGS.YUYI_PLAYER51)
--暴风雪
	elseif inst.yuyi_profession == 1 and inst.skill_ICD1 == 0 and not inst:HasTag("duanjin") and inst.skill_ILv1 > 0 and inst.components.hunger.current >= 8 then
		inst:AddTag("duanjin")
		inst.skill_ICD1 = 8
		inst.duanjin_last = 6
		inst.components.hunger:DoDelta(-TUNING.YUYI_SKILLSCOST)
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/duanjin1")
		if inst.duanjinfx then inst.duanjinfx:Remove() inst.duanjinfx = nil end
		inst.duanjinfx = SpawnPrefab( "duanjin" )
		inst.duanjinfx.entity:SetParent(inst.entity)
		inst.duanjinfx.Transform:SetPosition(0, 0, 0)
		inst.duanjinfx.AnimState:PlayAnimation("idle")
		inst.components.talker:Say(STRINGS.YUYI_PLAYER52)--246 369 4812
--断筋-流血伤害5S 每秒10血
	end
end
end)

TheInput:AddKeyUpHandler(TUNING.YUYI_KEY7, function()
if not IsPaused() and not TheInput:IsKeyDown(KEY_CTRL) and not TheInput:IsKeyDown(KEY_SHIFT) then
	inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
	inst.components.talker.fontsize = 26
	if inst.yuyi_profession == 0 and inst.skill_OCD0 > 0 and inst.skill_OLv0 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER53..inst.skill_OCD0)
	elseif inst.yuyi_profession == 1 and inst.skill_OCD1 > 0 and inst.skill_OLv1 > 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER54..inst.skill_OCD1)
	elseif inst.yuyi_profession == 0 and not inst:HasTag("zhihuihuanxing") and inst.skill_OLv0 > 0 then
		inst:AddTag("zhihuihuanxing")
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/zhihuihuanxing")
		if inst.huanxingchixu then inst.huanxingchixu:Cancel() inst.huanxingchixu = nil end
		inst.huanxingchixu = inst:DoPeriodicTask(1, function()
		if inst:HasTag("zhihuihuanxing") and inst.components.hunger.current > 0 then
			inst.components.hunger:DoDelta(-inst.skill_OLv0*0.5+0.5)--0.5-2.5
			inst.components.sanity:DoDelta(inst.skill_OLv0*0.5+0.5)--0.5-3
		end
		end)
		inst.skill_OCD0 = 2
		inst.components.talker:Say(STRINGS.YUYI_PLAYER55)
		if inst.huanxingfx then inst.huanxingfx:Remove() inst.huanxingfx = nil end
		inst.huanxingfx = SpawnPrefab( "zhihuihuanxing" )
		inst.huanxingfx.entity:SetParent(inst.entity)
		inst.huanxingfx.Transform:SetPosition(0, 0, 0)
		inst.huanxingfx.AnimState:PlayAnimation("idle")
--智慧唤醒--消耗饱食度 恢复理智
	elseif inst.yuyi_profession == 0 and inst:HasTag("zhihuihuanxing") and inst.skill_OLv0 > 0 then
		inst:RemoveTag("zhihuihuanxing")
		inst.skill_OCD0 = 2
		if inst.huanxingfx then inst.huanxingfx:Remove() inst.huanxingfx = nil end
		if inst.huanxingchixu then inst.huanxingchixu:Cancel() inst.huanxingchixu = nil end
		inst.components.talker:Say(STRINGS.YUYI_PLAYER56)
--智慧唤醒--消耗饱食度 恢复理智
	elseif inst.yuyi_profession == 0 and not inst:HasTag("zhihuihuanxing") and inst.skill_OLv0 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER57)
	elseif inst.yuyi_profession == 1 and inst.yuyi_states == 0 and inst.skill_OLv1 == 0 then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER58)
	elseif inst.yuyi_profession == 1 and inst.yuyi_states == 0 and inst.skill_OLv1 > 0 and inst:HasTag("aquatic") then
		inst.components.talker:Say(STRINGS.YUYI_PLAYER59)
	elseif inst.yuyi_states == 0 and inst.yuyi_profession == 1 and inst.skill_OLv1 > 0 then
		inst.yuyi_states = 1
		inst.AnimState:SetBank("cat_yuyi")
		inst.AnimState:SetBuild("cat_yuyi")
		inst:SetStateGraph("SGcat_yuyi")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(3.5,3.5,3.5)
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED-2+inst.skill_OLv1*0.5
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED-4+inst.skill_OLv1*0.5
		inst.components.combat:SetAttackPeriod(0.5)
		inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
		inst.Physics:ClearCollisionMask()
		inst.Physics:CollidesWith(COLLISION.GROUND)
		inst.Physics:CollidesWith(COLLISION.OBSTACLES)
		inst.Physics:CollidesWith(COLLISION.CHARACTERS)
		inst:AddTag("amphibious")
		inst:AddTag("yiyu_pitchfork")
		inst.skill_OCD1 = 2
		if not inst.components.inventory then inst:AddComponent("inventory") end
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/duanjin2")
		SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst.SoundEmitter:KillSound("talk")
--幻化术开启
	elseif inst.yuyi_states == 1 and inst.yuyi_profession == 1 and inst.skill_OLv1 > 0 then
		inst.yuyi_states = 0
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("yuyi_dark")
		inst:SetStateGraph("SGwilson")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)
		inst.components.locomotor.walkspeed = TUNING.WILSON_WALK_SPEED
		inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED
		ChangeToCharacterPhysics(inst)
		inst.skill_OCD1 = 2
		inst:RemoveTag("yiyu_pitchfork")
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/duanjin2")
		SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst.SoundEmitter:KillSound("talk")
--幻化术关闭
	end
	end
end)


--[[
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY2, inst.yiyuskill_2)
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY3, inst.yiyuskill_3)
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY4, inst.yiyuskill_4)
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY5, inst.yiyuskill_5)
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY6, inst.yiyuskill_6)
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY7, inst.yiyuskill_7)
TheInput:AddKeyUpHandler(TUNING.YIYU_KEY8, inst.yiyuskill_8)
]]


	inst:DoTaskInTime(0, function() SetHUDState(inst) end)
	inst:ListenForEvent("attacked", OnAttacked)
	inst:ListenForEvent("entity_death", function(wrld, data) onkill(inst, data) end, GetWorld())


    local yuyimouse=inst.components.playeractionpicker.DoGetMouseActions
    inst.components.playeractionpicker.DoGetMouseActions = function ( force_target )
		local action, second_action = yuyimouse( force_target )
			if inst:HasTag("huoqiang") or inst:HasTag("baofengxue") then
				action =BufferedAction(inst, nil, ACTIONS.YUYICAST)
			end
	return action, second_action
	end

	inst.components.combat.onhitotherfn = function(inst, other, damage)
--	inst.components.talker:Say("攻击距离"..inst.components.combat.hitrange.."和"..inst.components.combat.attackrange)

	if inst.components.hunger.current == 0 then
		local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		if inst:HasTag("banyuewandao") then
		inst:RemoveTag("banyuewandao")
		inst.skill_KCD1 = 1
		inst.components.combat.areahitrange = 0
		inst.components.combat.areahitdamagepercent = 0
		inst.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/banyue")
		if weapon then weapon.components.weapon:SetProjectile(nil) end
		inst.components.talker:Say(STRINGS.YUYI_PLAYER10)
		end
	end

	local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if weapon and weapon.weapon_combo_multiplier and weapon.weapon_combo_multiplier > 0 then
	local combo_multiplier = weapon.weapon_combo_multiplier
	inst.yiyudmglast = 4
	inst.HUD.lianji:Show()
	if inst.yiyudmgcomb and inst.components.combat.damagemultiplier < 1.5 then
		inst.yiyudmgcomb = inst.yiyudmgcomb + 1
		inst.components.combat.damagemultiplier = inst.components.combat.damagemultiplier+inst.yiyudmgcomb*combo_multiplier
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
		inst.yiyudmgpercent = inst.components.combat.damagemultiplier
	end
	end

	if inst:HasTag("zhurijianfa") and other then
		SpawnPrefab("explode_small").Transform:SetPosition(other.Transform:GetWorldPosition())
		inst.components.yuyicast:zhuri_attack(other,inst)
		inst.components.combat:SetRange(2, 2)
	elseif inst:HasTag("baofengxue") and other then
		inst.components.yuyicast:baofengxue_attacks(other,inst)
	elseif inst:HasTag("duanjin") and other and other.components.health and not other.components.health:IsDead() then
		inst:RemoveTag("duanjin")
		if other and other.task2 then other.task2:Cancel() other.task2 = nil end
		other.task2 = other:DoPeriodicTask(0.5, function()
		if other and other.components.health and other.components.health:IsDead() then
		if other and other.task2 then other.task2:Cancel() other.task2 = nil end
		end
		local blood = SpawnPrefab("splash")
		blood.Transform:SetPosition(other.Transform:GetWorldPosition())
		blood.AnimState:SetMultColour(255/255,0/255,255/255,1)
		blood.Transform:SetScale(0.75, 0.75, 0.75)
		if other.components.health and not (other.components.inventoryitem and other.components.inventoryitem.owner) then other.components.combat:GetAttacked(inst, inst.skill_ILv1*5+5) end
		end)
		other:DoTaskInTime(5, function()
		if other and other.task2 then other.task2:Cancel() other.task2 = nil end
		end)
	end
	end

	inst.task_yuyi = inst:DoPeriodicTask(0.5, function()
	if inst.skill_RCD > 0 then inst.skill_RCD = inst.skill_RCD - 0.5 end

	if inst.skill_JCD0 > 0 then inst.skill_JCD0 = inst.skill_JCD0 - 0.5 end
	if inst.skill_KCD0 > 0 then inst.skill_KCD0 = inst.skill_KCD0 - 0.5 end
	if inst.skill_LCD0 > 0 then inst.skill_LCD0 = inst.skill_LCD0 - 0.5 end
	if inst.skill_UCD0 > 0 then inst.skill_UCD0 = inst.skill_UCD0 - 0.5 end
	if inst.skill_ICD0 > 0 then inst.skill_ICD0 = inst.skill_ICD0 - 0.5 end
	if inst.skill_OCD0 > 0 then inst.skill_OCD0 = inst.skill_OCD0 - 0.5 end

	if inst.skill_JCD1 > 0 then inst.skill_JCD1 = inst.skill_JCD1 - 0.5 end
	if inst.skill_KCD1 > 0 then inst.skill_KCD1 = inst.skill_KCD1 - 0.5 end
	if inst.skill_LCD1 > 0 then inst.skill_LCD1 = inst.skill_LCD1 - 0.5 end
	if inst.skill_UCD1 > 0 then inst.skill_UCD1 = inst.skill_UCD1 - 0.5 end
	if inst.skill_ICD1 > 0 then inst.skill_ICD1 = inst.skill_ICD1 - 0.5 end
	if inst.skill_OCD1 > 0 then inst.skill_OCD1 = inst.skill_OCD1 - 0.5 end

	if inst.leidianshu_last == 0.5 then
		inst.components.combat:SetRange(2, 2)
		inst:RemoveTag("leidianshu")
		inst.leidianshu_last = 0
	elseif inst.leidianshu_last > 0.5 then
		inst.leidianshu_last = inst.leidianshu_last - 0.5
	end
	if inst.baofengxue_last == 0.5 then
		inst.components.combat:SetRange(2, 2)
		inst:RemoveTag("baofengxue")
		inst.baofengxue_last = 0
	elseif inst.baofengxue_last > 0.5 then
		inst.baofengxue_last = inst.baofengxue_last - 0.5
	end
	if inst.zhurijianfa_last == 0.5 then
		inst.components.combat:SetRange(2, 2)
		inst:RemoveTag("zhurijianfa")
		inst.zhurijianfa_last = 0
	elseif inst.zhurijianfa_last > 0.5 then
		inst.zhurijianfa_last = inst.zhurijianfa_last - 0.5
	end
	if inst.huoqiang_last == 0.5 then
		inst.components.combat:SetRange(2, 2)
		inst:RemoveTag("huoqiang")
		inst.huoqiang_last = 0
	elseif inst.huoqiang_last > 0.5 then
		inst.huoqiang_last = inst.huoqiang_last - 0.5
	end
	if inst.liehuojianfa_last == 0.5 then
		if inst.fire then inst.fire:Remove() inst.fire = nil end
		inst:RemoveTag("liehuojianfa")
		inst.liehuojianfa_last = 0
	elseif inst.liehuojianfa_last > 0.5 then
		inst.liehuojianfa_last = inst.liehuojianfa_last - 0.5
	end
	if inst.duanjin_last == 0.5 then
		if inst.duanjinfx then inst.duanjinfx:Remove() inst.duanjinfx = nil end
		inst:RemoveTag("duanjin")
		inst.duanjin_last = 0
	elseif inst.duanjin_last > 0.5 then
		inst.duanjin_last = inst.duanjin_last - 0.5
	end
	if inst.mofadun_last == 0.5 then
		if inst.mofadunfx then inst.mofadunfx:Remove() inst.mofadunfx = nil end
		inst:RemoveTag("mofadun")
		inst.mofadun_last = 0
	elseif inst.mofadun_last > 0.5 then
		inst.mofadun_last = inst.mofadun_last - 0.5
	end

	local player = GetPlayer()
	local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	local pos = Vector3(inst.Transform:GetWorldPosition())
	local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 15)
    local leiting = TheSim:FindFirstEntityWithTag("leiting")
    local guangmang = TheSim:FindFirstEntityWithTag("guangmang")
	if leiting then inst.leiting = 1 end
	if guangmang then inst.guangmang = 1 end

    local pigking = TheSim:FindFirstEntityWithTag("king")
	local cathouse = TheSim:FindFirstEntityWithTag("yiyu_cathouse")
	if SaveGameIndex:IsModeShipwrecked() then pigking = TheSim:FindFirstEntityWithTag("sharkhome") end
	if pigking and TUNING.YIYUISOPEN == 1 then
		local pt = Vector3(pigking.Transform:GetWorldPosition())
		local catspawn_pt = spawnoffscreen(pt)
		if catspawn_pt and not cathouse and GetClock().numcycles <= 1 then
		SpawnPrefab("yiyu_cathouse").Transform:SetPosition(catspawn_pt.x, catspawn_pt.y, catspawn_pt.z)
		end
	end

    local yuyileif = TheSim:FindFirstEntityWithTag("leif")
	local yuyi_leifnum = c_countprefabs("leif", true) or 0
	if yuyileif and yuyi_leifnum > 3 then yuyileif:Remove() end

	if inst.yiyudmglast == 0.5 and inst.HUD.lianji then
	inst.HUD.lianji:Hide()
	inst.yiyudmglast = 0
	inst.yiyudmgcomb = 0
	if inst.yuyi_profession == 0 then
		inst.yiyudmgpercent = 0.55
		inst.components.combat.damagemultiplier = 0.35
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
	elseif inst.yuyi_profession == 1 then
		inst.yiyudmgpercent = 0.80+inst.skill_RLv*0.05
		inst.components.combat.damagemultiplier = 0.80+inst.skill_RLv*0.05
		if SaveGameIndex:IsModeShipwrecked() then
			inst.components.combat:RemoveDamageModifier("yiyu")
			inst.components.combat:AddDamageModifier("yiyu", inst.components.combat.damagemultiplier-1)
		end
	end

	inst.HUD.lianji:SetTooltip(STRINGS.YUYI_PLAYER60 .. inst.yiyudmgcomb.."\n" .. STRINGS.YUYI_PLAYER61 .. inst.yiyudmgpercent)
	elseif inst.yiyudmglast > 0 and inst.HUD.lianji then
		inst.yiyudmglast = inst.yiyudmglast - 0.5
		inst.HUD.lianji:SetTooltip(STRINGS.YUYI_PLAYER60 .. inst.yiyudmgcomb.."\n" .. STRINGS.YUYI_PLAYER61 .. inst.yiyudmgpercent)
	end

	if inst:HasTag("treat") then inst.components.health:DoDelta(1) inst.components.sanity:DoDelta(1) end
	end)

	RECIPETABS['YUYITAB'] = {str = STRINGS.TABS.YUYITAB, sort=99, icon = "yuyitab.tex", icon_atlas = "images/yuyitabs/yuyitab.xml"}

	applyupgrades(inst)
	inst.OnSave = onsave
	inst.OnLoad = onload
end

return MakePlayerCharacter("yuyi", prefabs, assets, yuyifn, start_inv)
