local MakePlayerCharacter = require "prefabs/player_common"
local ImageButton = require "widgets/imagebutton"
--local YiyuScreen = require "screens/yiyuscreen"
local Text = require "widgets/text"
local ModsScreen = require "screens/modsscreen"
--local player = GetPlayer()
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" ),
		--tianjia
        Asset( "ANIM", "anim/toso_prey.zip" ),
        Asset( "ANIM", "anim/toso_blue.zip" ),
        Asset( "ANIM", "anim/toso_red.zip" ),
        Asset( "ANIM", "anim/toso_baqi.zip" ),
        Asset( "ANIM", "anim/toso_blue2.zip" ),
	    Asset("ANIM", "anim/walani_paddle.zip"),
        Asset("ANIM", "anim/player_wolfgang.zip"),
	    --其他动作
        --Asset("ANIM", "anim/ds_pig_attacks.zip"),		
		--煞气鬼化
		Asset("ANIM", "anim/stalker_basic.zip"),
        Asset("ANIM", "anim/stalker_action.zip"),
        Asset("ANIM", "anim/stalker_shadow_build.zip"),
        Asset("ANIM", "anim/stalker_cave_build.zip"),
		
		Asset("ANIM", "anim/stalker_forest.zip"),
        Asset("ANIM", "anim/stalker_shadow_build.zip"),
        Asset("ANIM", "anim/stalker_forest_build.zip"),
		
		Asset("ANIM", "anim/stalker_basic.zip"),
        Asset("ANIM", "anim/stalker_action.zip"),
        Asset("ANIM", "anim/stalker_atrium.zip"),
        Asset("ANIM", "anim/stalker_shadow_build.zip"),
        Asset("ANIM", "anim/stalker_atrium_build.zip"),

--技能图标
    Asset("IMAGE", "images/shuangman.tex"),
    Asset("ATLAS", "images/shuangman.xml"),
    Asset("IMAGE", "images/taqianzhan.tex"),
    Asset("ATLAS", "images/taqianzhan.xml"),
    Asset("IMAGE", "images/linghuazhuangtai.tex"),
    Asset("ATLAS", "images/linghuazhuangtai.xml"),
    Asset("IMAGE", "images/fenjijianyu.tex"),
    Asset("ATLAS", "images/fenjijianyu.xml"),
    Asset("IMAGE", "images/liehuodaofa.tex"),
    Asset("ATLAS", "images/liehuodaofa.xml"),
	
		--这个是给制作栏图标用的
	Asset( "ATLAS", "images/tabs/toso_tab.xml" ),
	Asset( "IMAGE", "images/tabs/toso_tab.tex" ),	

}
local prefabs = 
{
	"surfboard_item_toso",
}
local start_inv = {
                 --"fenji",
				 --"eye",
				 --"jiandai",
	             --"bcape", 
				 --"san",
				 --"light1",
				 --"sword_1",
				 --"sword_2",
				 --"sword_3",
				 "sword_4",
	             --"sword_5",
	             --"sword_6",
				 --"shouzhang",
				 "jianqiao",
				 --"jianqiaoxiu",
				 --"jianqiao2",
				 --"sheath",
				 --"sheath2",
				 --"futou",
				 --"surfboard_item_toso",
				 "hulu",
				 --"musha_egg",
				 --"toso_dog",
				 "toso_xiang",
				 --"sword_7",
				 --"surfboard_item_toso",
}

--技能状态和经验等级显示均参考翼语
local function SetHUDState_UI(inst, data)
	if inst.HUD then
		GetPlayer().HUD.taqianzhan = GetPlayer().HUD.controls.bottom_root:AddChild(ImageButton("images/taqianzhan.xml", "taqianzhan.tex"))
		GetPlayer().HUD.taqianzhan:SetPosition(-400,100,0)
		GetPlayer().HUD.taqianzhan:SetScale(0.6)
		
		GetPlayer().HUD.shuangman = GetPlayer().HUD.controls.bottom_root:AddChild(ImageButton("images/shuangman.xml", "shuangman.tex"))
		GetPlayer().HUD.shuangman:SetPosition(-400,100,0)
		GetPlayer().HUD.shuangman:SetScale(0.6)		

		GetPlayer().HUD.linghuazhuangtai = GetPlayer().HUD.controls.bottom_root:AddChild(ImageButton("images/linghuazhuangtai.xml", "linghuazhuangtai.tex"))
		GetPlayer().HUD.linghuazhuangtai:SetPosition(-400,100,0)
		GetPlayer().HUD.linghuazhuangtai:SetScale(0.6)
	
		GetPlayer().HUD.liehuodaofa = GetPlayer().HUD.controls.bottom_root:AddChild(ImageButton("images/liehuodaofa.xml", "liehuodaofa.tex"))
		GetPlayer().HUD.liehuodaofa:SetPosition(-400,100,0)
		GetPlayer().HUD.liehuodaofa:SetScale(0.6)	
	
		GetPlayer().HUD.fenjijianyu = GetPlayer().HUD.controls.bottom_root:AddChild(ImageButton("images/fenjijianyu.xml", "fenjijianyu.tex"))
		GetPlayer().HUD.fenjijianyu:SetPosition(-400,100,0)
		GetPlayer().HUD.fenjijianyu:SetScale(0.6)

		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()





		inst.components.talker.colour = Vector3(1, 0.75, 1, 1)
		inst.components.talker.fontsize = 26

	end
end
--显示经验和等级
local function SetHUDState(inst, data)
	if inst.HUD then
		local nandusize = 28
        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.9, 1, 1)
        inst.HUD.dengji:SetString("等级:"..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("经验:"..tostring(inst.exp).."/"..tostring(inst.level*TUNING.TOSO_NEEDEXP))
        inst.HUD.jingyan:SetColour(0, 0.9, 1, 1)

	end
end


local function applyupgrades(inst)

    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.TOSO_NEEDEXP and inst.level < 25 then
	inst.exp = math.floor(inst.exp - inst.level*TUNING.TOSO_NEEDEXP)
	inst.level = inst.level + 1
	inst.tosolevel = inst.level
	end

	inst.components.hunger.max = math.ceil(TUNING.TOSO_STARTHP+inst.level*5-5)
	inst.components.health.maxhealth = math.ceil(TUNING.TOSO_STARTHP+inst.level*5-5)
	inst.components.sanity.max = math.ceil(TUNING.TOSO_STARTHP+inst.level*5-5)
	inst.components.talker:Say("Level Up! : ".. (inst.level))
--升级提示
    inst.components.talker:Say("等级: "..(inst.level))
	if inst.HUD then
	inst.HUD.dengji:SetString("等级:"..tostring(inst.level))
	inst.HUD.jingyan:SetString("经验:"..tostring(inst.exp).."/"..tostring(inst.level*TUNING.TOSO_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 onpreload(inst, data)
	if data then
		if data.level then
			inst.exp = data.exp
			inst.level = data.level
			inst.tosolevel = data.tosolevel
			inst.toso_state = data.toso_state
			inst.damagemult_accident = data.damagemult_accident
	        inst.speed_accident = data.speed_accident
	        inst.hpabsorb_accident = data.hpabsorb_accident
	        inst.hp_reversion = data.hp_reversion
	        inst.sanity_reversion = data.sanity_reversion
			--煞气初始化
	        inst.shaqibaocun = data.shaqibaocun
            inst.change_data = data.change_data
			
	        --inst.toso_skill_chance = data.toso_skill_chance	
	        --inst.toso_san_drain = data.toso_san_drain
            --inst.toso_absorb = data.toso_absorb 	
            --inst.toso_fire_absorb = data.toso_fire_absorb
            --inst.toso_cold_resistance = data.toso_cold_resistance
            --inst.toso_inherentinsulation = data.toso_inherentinsulation
            --inst.toso_summerinsulation = data.toso_summerinsulation
	        --inst.toso_cure_time = data.toso_cure_time
			applyupgrades(inst)
		end
	end
end

local function onload(inst, data)
	if data then
		if data.level then
			inst.exp = data.exp
			inst.level = data.level
			inst.tosolevel = data.tosolevel
			inst.toso_state = data.toso_state
			inst.damagemult_accident = data.damagemult_accident
	        inst.speed_accident = data.speed_accident
	        inst.hpabsorb_accident = data.hpabsorb_accident
	        inst.hp_reversion = data.hp_reversion
	        inst.sanity_reversion = data.sanity_reversion
			--煞气初始化
	        inst.shaqibaocun = data.shaqibaocun
            inst.change_data = data.change_data
		
	        --inst.toso_skill_chance = data.toso_skill_chance	
	        --inst.toso_san_drain = data.toso_san_drain
            --inst.toso_absorb = data.toso_absorb 	
            --inst.toso_fire_absorb = data.toso_fire_absorb
            --inst.toso_cold_resistance = data.toso_cold_resistance
            --inst.toso_inherentinsulation = data.toso_inherentinsulation
            --inst.toso_summerinsulation = data.toso_summerinsulation
	        --inst.toso_cure_time = data.toso_cure_time
			applyupgrades(inst)
    if inst.toso_state == 0 then
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("toso_blue2")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)

	elseif inst.toso_state == 1 then
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("toso_baqi")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)

	elseif inst.toso_state == 2 then
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("toso_blue")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)

	end
		end
	end
end

local function onsave(inst, data)
	data.exp = inst.exp
	data.tosolevel = inst.tosolevel
	data.level = inst.level
	data.toso_state = inst.toso_state
	data.damagemult_accident = inst.damagemult_accident
	data.speed_accident = inst.speed_accident
	data.hpabsorb_accident = inst.hpabsorb_accident
	data.hp_reversion = inst.hp_reversion
	data.sanity_reversion = inst.sanity_reversion
	--煞气初始化
    data.shaqibaocun = inst.shaqibaocun
    data.change_data = inst.change_data         
	
	--data.toso_skill_chance = inst.toso_skill_chance
	--data.toso_san_drain = inst.toso_san_drain
	--data.toso_absorb = inst.toso_absorb
    --data.toso_fire_absorb = inst.toso_fire_absorb
	--data.toso_cold_resistance = inst.toso_cold_resistance
	--data.toso_inherentinsulation = inst.toso_inherentinsulation
	--data.toso_summerinsulation = inst.toso_summerinsulation
	--data.toso_cure_time = inst.toso_cure_time

end
--木属性
local function OnHittree(inst, data)
    local treechance1 = 0.00 + inst.level*0.01
    local other = data.target
    if math.random() < treechance1 and inst:HasTag("nature_tree") then
        local pt = other:GetPosition()
        local st_pt =  FindWalkableOffset(pt or owner:GetPosition(), math.random()*2*PI, 2, 3)
        if st_pt then
            inst.SoundEmitter:PlaySound("dontstarve/common/shadowTentacleAttack_1")
            inst.SoundEmitter:PlaySound("dontstarve/common/shadowTentacleAttack_2")            
            st_pt = st_pt + pt
            local st = SpawnPrefab("shadowcane")
            --print(st_pt.x, st_pt.y, st_pt.z)
            st.Transform:SetPosition(st_pt.x, st_pt.y, st_pt.z)
            st.components.combat:SetTarget(other)
        end
	    other.components.health:DoDelta(-5)

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHittree) 
end 
--火属性
local function OnHitfire(inst, data)
    local firechance1 = 0.00 + inst.level*0.01
    local other = data.target
    if math.random() < firechance1 and inst:HasTag("nature_fire") then
	
	    other.components.health:DoDelta(-16)	
		if other.components.burnable and not other:HasTag("structure") then
			other.components.burnable:Ignite() 
		end

        if other.components.burnable and other.components.burnable:IsBurning() then
			other.components.burnable:Extinguish() 
		end

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHitfire) 
end 
--[[
--水属性暂时换为持续掉血
local function OnHitwater(inst, data)
    local waterchance1 = 0.0 + inst.level*0.01
    local other = data.target
    if math.random() < waterchance1 and inst:HasTag("nature_water") then
    --SpawnPrefab("jianyu").Transform:SetPosition(other:GetPosition():Get())
	--inst.SoundEmitter:PlaySound("dontstarve/tentacle/tentacle_emerge")
	other.components.health:DoDelta(-10)
	other:DoTaskInTime(10, function() other.components.health:DoDelta(-2) end)

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHitwater) 
end 
]]

--冰属性
local function OnHitfreeze(inst, data)
    local freezechance1 = 0.05 + inst.level*0.01
    local other = data.target	
	
    if math.random() < freezechance1 and inst:HasTag("nature_freeze") then
--------冰冻代码
	if other.components.sleeper and other.components.sleeper:IsAsleep() then
        other.components.sleeper:WakeUp()
    end
    if other.components.burnable then
        if other.components.burnable:IsBurning() then
            other.components.burnable:Extinguish()
        elseif other.components.burnable:IsSmoldering() then
            other.components.burnable:SmotherSmolder()
        end
    end

    --if other.components.combat then
        --other.components.combat:SuggestTarget(attacker)
    --end

    --if other.sg and not other.sg:HasStateTag("frozen") then
        --other:PushEvent("attacked", {attacker = attacker, damage = 0})
    --end
	--冰		 
	if not inst or not other then 
	return end
	
    local numFX = math.random(5,10)
    local pos = inst:GetPosition()
    local targetPos = other:GetPosition()
    local vec = targetPos - pos
    vec = vec:Normalize()
    local dist = pos:Dist(targetPos)
    local angle = inst:GetAngleToPoint(targetPos:Get())

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

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHitfreeze) 
end 

--雷属性
local function OnHitlightning(inst, data)
    local lightningchance1 = 0 + inst.level*0.005
    local other = data.target
    if math.random() < lightningchance1 and inst:HasTag("nature_lightning") then
    SpawnPrefab("lightning").Transform:SetPosition(other:GetPosition():Get())
	--inst.SoundEmitter:PlaySound("dontstarve/tentacle/tentacle_emerge")
	other.components.health:DoDelta(-15)

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHitlightning) 
end 
--电属性
local function OnHitelectric(inst, data)
    local electricchance1 = 0.00 + inst.level*0.01
    local other = data.target
    if math.random() < electricchance1 and inst:HasTag("nature_electric") then
    SpawnPrefab("lightning_rod_fx").Transform:SetPosition(other:GetPosition():Get())
    --SpawnPrefab("toso_fenjijianyu").Transform:SetPosition(other:GetPosition():Get())
	--inst.SoundEmitter:PlaySound("dontstarve/tentacle/tentacle_emerge")
	other.components.health:DoDelta(-15)

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHitelectric) 
end 
--闪避，用于生命值过低时
local function OnHitleap(inst, data)
    --local leapchance1 = 1
    local other = data.target
    if inst:HasTag("dongzuo_leap") then
	  if inst.components.health:GetPercent() < 0.25 then
		local pos1 = inst:GetPosition()
		local pos2 = other:GetPosition()
		--local dist = pos1:Dist(pos2)
		local dist = 2.5
		local angle = inst:GetAngleToPoint(pos2)
		local x,y,z = pos1:Get()
		SpawnPrefab("collapse_small").Transform:SetPosition(x,y,z)

		local x2,y2,z2 = x+math.cos(angle)*dist, 0, z-math.sin(angle)*dist
		SpawnPrefab("collapse_small").Transform:SetPosition(x2,y2,z2)
		
		inst.Transform:SetPosition(x2,y2,z2)
		if not inst:IsOnValidGround() then
			inst:DoTaskInTime(0, function() inst.Transform:SetPosition(x,y,z) end)
		end
			 		inst:RemoveEventCallback("onhitother", OnHitleap) 
	--other.components.health:DoDelta(-20)
	  end
    end 

end 

--反击，生命值过低时触发，共用闪避tag
local function OnHitfanji(inst, data)
    local fanjichance1 = 0.5
    local other = data.target
    if inst:HasTag("dongzuo_leap") then
	  if inst.components.health:GetPercent() < 0.5 then
	    if math.random() < fanjichance1 then
           inst.components.tosocast:siwangfanzhao_attack(inst)
			 		inst:RemoveEventCallback("onhitother", OnHitleap) 
		end
	--other.components.health:DoDelta(-20)
	  end
    end 

end 


local function dorgecheck(inst, data)
	inst:ListenForEvent("onhitother",OnHitleap)
end
--吸血属性
local function OnHitbloodabsorb(inst, data)
    local bloodabsorbchance1 = 0.05 + inst.level*0.02
    local bloodabsorbchance2 = 1
    local other = data.target
  if not inst:HasTag("bloodabsorbforever") then
    if math.random() < bloodabsorbchance1 and inst:HasTag("character_bloodabsorb") then
	--换为治疗特效
    SpawnPrefab("zhiliaofx").Transform:SetPosition(inst:GetPosition():Get())
	--inst.SoundEmitter:PlaySound("dontstarve/tentacle/tentacle_emerge")
	inst.components.health:DoDelta(10)
	end
  else
    --if math.random() < bloodabsorbchance2 and inst:HasTag("character_bloodabsorb") then
	--换为治疗特效
    --SpawnPrefab("explode_small").Transform:SetPosition(other:GetPosition():Get())
	--inst.SoundEmitter:PlaySound("dontstarve/tentacle/tentacle_emerge")
	inst.components.health:DoDelta(inst.level*0.2)  
    --end
  end 		
--inst:RemoveEventCallback("onhitother", OnHitbloodabsorb) 
end 
------------------------------意外剑法---------------------------------

--蓝剑持续掉血
local function OnHitwater(inst, data)
    local waterchance1 = 0.0 + inst.level*0.01
    local other = data.target
    if math.random() < waterchance1 and inst:HasTag("nature_water") then
    SpawnPrefab("baoliehuoyan_toso").Transform:SetPosition(other:GetPosition():Get())
	--inst.SoundEmitter:PlaySound("dontstarve/tentacle/tentacle_emerge")
		--if other.components.health and not (other.components.inventoryitem and other.components.inventoryitem.owner) then other.components.combat:GetAttacked(inst, 9) end
		--other:DoTaskInTime(5, function()
			--other.components.health:DoDelta(-7) 
		--end)
	other.components.health:DoDelta(-10)
	--other:DoTaskInTime(10, function() other.components.health:DoDelta(-2) end)
		if other and other.task_diaoxue then other.task_diaoxue:Cancel() other.task_diaoxue = nil end
		other.task_diaoxue = other:DoPeriodicTask(0.5, function()
        other.components.health:DoDelta(-2)
		end)
		other:DoTaskInTime(5, function()
		if other and other.task_diaoxue then other.task_diaoxue:Cancel() other.task_diaoxue = nil end
		end)

    end 
	 		--inst:RemoveEventCallback("onhitother", OnHitwater) 
end 
--暴击
local function OnHitdouble(inst, data)
    local doublechance1 = 0.0 + inst.level*0.01 
    local other = data.target 
    if math.random() < doublechance1 then
        SpawnPrefab("baojitexiao").Transform:SetPosition(other:GetPosition():Get())
        other.components.health:DoDelta(-50 - inst.level*10)	
    end	
 
end
------------------------------意外剑法。---------------------------------


--在妖怪的旁边会加煞气，参考荷花仙子，未生效
local function shaqifn(inst)
    local x, y, z = inst.Transform:GetWorldPosition()  
	local x,y,z = inst.Transform:GetWorldPosition()	
	local delta = 0
	local max_rad = 10
	local ents = TheSim:FindEntities(x,y,z, max_rad, {"monster"})
    for k,v in pairs(ents) do 
    	if v.components.burnable and v.components.burnable.burning then
    		local sz = TUNING.SANITYAURA_TINY
    		local rad = v.components.burnable:GetLargestLightRadius() or 1
    		sz = sz * ( math.min(max_rad, rad) / max_rad )
			local distsq = inst:GetDistanceSqToInst(v)
			delta = delta + sz/math.max(1, distsq)
    	end
    end
    return delta
end

--移除技能
local function removeskill(inst, data)
	if not inst:HasTag("frameon1") and not inst:HasTag("frameon2") and not inst:HasTag("frameon3") and not inst:HasTag("frameon4") then
	     inst:RemoveEventCallback("attacked", Onframeshield1)
	     inst:RemoveEventCallback("attacked", Onframeshield2)
	     inst:RemoveEventCallback("attacked", Onframeshield3)
	     inst:RemoveEventCallback("attacked", Onframeshield4)
end
end

--煞气爆发
local function shaqi_state(inst)

  local shaqi_percent = inst.components.shaqi:GetPercent()  
  local lingli_percent = inst.components.lingli:GetPercent() 
  --local fxkaishi = SpawnPrefab("shaqi_start")
  --local fxjieshu = SpawnPrefab("shaqi_over")
--通过存档进入
if inst.shaqibaocun then
if not inst:HasTag("Shaqihua") then
inst:AddTag("Shaqihua")

   if lingli_percent >= 0.6 then

        --inst.components.locomotor:Stop()
        --inst.components.playercontroller:Enable(false)
        inst:DoTaskInTime(0.3, function()
        --GetPlayer().components.talker:Say("我好晕~~!")
		    inst.components.shaqi:StopShadc()	
			--fxjieshu.Transform:SetPosition( inst.Transform:GetWorldPosition() )
		end) 



 

   elseif lingli_percent < 0.6 then
          inst:DoTaskInTime(0.3, function()
          inst.components.shaqi:StartShadc()
		  --fxkaishi.Transform:SetPosition( inst.Transform:GetWorldPosition() )
		  end) 
		--打开夜视
		  if GetClock() and GetWorld() and GetWorld().components.colourcubemanager then
            GetClock():SetNightVision(true)
            --if not GetClock():IsDay() and not GetWorld():IsCave() then
            if GetClock() and GetWorld() and not GetClock():IsDay() and not GetWorld():IsCave() then
               GetWorld().components.colourcubemanager:SetOverrideColourCube(nil, .25)
            else -- Dusk and Night
                GetWorld().components.colourcubemanager:SetOverrideColourCube("images/colour_cubes/mole_vision_on_cc.tex", .25)
            end
          end

   end
elseif inst:HasTag("Shaqihua") then

   if lingli_percent >= 0.6 then

        inst:DoTaskInTime(0.3, function()
		    inst.components.shaqi:StopShadc()
			
		end) 

   elseif lingli_percent < 0.6 then
          inst:DoTaskInTime(0.3, function()
          inst.components.shaqi:StartShadc()
		  --fxkaishi.Transform:SetPosition( inst.Transform:GetWorldPosition() )
		  end)
   end

end
--通过第一次进入
elseif not inst.shaqibaocun then

    if shaqi_percent >= 0.8 and lingli_percent < 0.6 then
       inst:AddTag("Shaqihua")
	   inst.shaqibaocun = true

--if inst:HasTag("Shaqihua") then return end       
        inst:DoTaskInTime(0.3, function() 
        --GetPlayer().components.talker:Say("我控几不住我自己啊!")

           --inst.AnimState:SetBuild("hinatamonster")
		   --模型加手杖皮肤？
		   	inst.components.shaqi:StartShadc()
			--fxkaishi.Transform:SetPosition( inst.Transform:GetWorldPosition() )
           --负增益效果
           --local dehp = 1 + math.random()
           --inst.components.health:StartRegen(-dehp, 10)	
		end)
		
	end 

end


  if inst:HasTag("Shaqihua") then 	
        if not inst.shaqi_fx then 
  			  inst.shaqi_fx = SpawnPrefab("shaistate")
			  inst.shaqi_fx.Transform:SetPosition(-0.1, 0, 0)
			--self.inst.shaqi_fx = SpawnPrefab("baguadun")
			  inst.shaqi_fx.entity:SetParent(inst.entity)
			  --inst.shaqi_fx.Transform:SetPosition(0, -1, 0)
			  inst.shaqi_fx.AnimState:PushAnimation("idle", true)
		end

		--打开夜视
		  if GetClock() and GetWorld() and GetWorld().components.colourcubemanager then
            GetClock():SetNightVision(true)
            -- Dusk and Night
            if GetClock() and GetWorld() and not GetClock():IsDay() and not GetWorld():IsCave() then
			           GetWorld().components.colourcubemanager:SetOverrideColourCube("images/colour_cubes/mole_vision_on_cc.tex", .25)
               
            elseif GetWorld() and GetWorld().components.colourcubemanager and GetClock():IsDay() then --白天关闭
                       GetClock():SetNightVision(false)
                       GetWorld().components.colourcubemanager:SetOverrideColourCube(nil, .25)
			--else
                       --GetClock():SetNightVision(false)
                       --GetWorld().components.colourcubemanager:SetOverrideColourCube(nil, .25)
            end
          end  
  else
       if inst.shaqi_fx then inst.shaqi_fx:Remove() inst.shaqi_fx = nil end	
        --关闭夜视
          if  GetClock() then
            GetClock():SetNightVision(false)
          end
          if  GetWorld() and GetWorld().components.colourcubemanager then
            GetWorld().components.colourcubemanager:SetOverrideColourCube(nil, .25)
          end	 

  end 


end

local function panduanshaqifx(inst, data)
  local other = data.target
  if inst:HasTag("Shaqihua") then 

     SpawnPrefab("statue_transition_2").Transform:SetPosition(other:GetPosition():Get())

  end

end

--吃某些食物加经验
local function oneatexp_up(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 == "tallbirdegg" then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(10)
		end
		inst.exp = inst.exp + 40
		applyupgrades(inst)
		inst.components.talker:Say("经验[+40]")
		if not inst:HasTag("treat") then
			inst:DoTaskInTime(15, function() inst:RemoveTag("treat") end)
			inst:AddTag("treat")
		end
	elseif food and food.prefab == "tallbirdegg_cooked" then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(20)
		end
		inst.exp = inst.exp + 50
		applyupgrades(inst)
		inst.components.talker:Say("经验[+50]")
	elseif food and food.prefab == "batwing" then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(10)
		end
		inst.exp = inst.exp + 100
		applyupgrades(inst)
		inst.components.talker:Say("经验[+100]")
	elseif food and food.prefab == "batwing_cooked" then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(20)
		end
		inst.exp = inst.exp + 120
		applyupgrades(inst)
		inst.components.talker:Say("经验[+120]")
	elseif food and food.prefab == "plantmeat" then
		inst.exp = inst.exp + 500
		applyupgrades(inst)
		inst.components.talker:Say("经验[+500]")
	elseif food and food.prefab == "plantmeat_cooked" then
		inst.exp = inst.exp + 1000
		applyupgrades(inst)
		inst.components.talker:Say("经验[+1000]")
	elseif food and food.prefab == "plantmeat" then
		inst.exp = inst.exp + 120
		applyupgrades(inst)
		inst.components.talker:Say("经验[+500]")
	elseif food and food.components.edible then
	    inst.exp = inst.exp + 18
		applyupgrades(inst)
		inst.components.talker:Say("经验[+18]")
	 

	end
	--吃花升级
	  if food and food.prefab == "flower_hua" then
	     if inst.components.lingli then
		    inst.components.lingli:DoDelta(10)
		 end
		--inst.level = inst.level + 1
		--applyupgrades(inst)
	--elseif food and food.prefab == "strawberry_b" then

	  end
	  --吃灵力补充剂
	  if food and food.prefab == "linglibuchong" then
	     if inst.components.lingli then
		    inst.components.lingli:DoDelta(50)
		 end
		--inst.level = inst.level + 1
		--applyupgrades(inst)
	--elseif food and food.prefab == "strawberry_b" then

	  end
	--吃怪物肉加煞气值
	  if food and food.prefab == "monstermeat" then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(12)
		end
	  end 
	  if food and food.prefab == "cookedmonstermeat" then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(8)
		end
	  end 
	  if food and food.prefab == "monstermeat_dried" then
		if inst.components.shaqi then
			--inst.components.shaqi:DoDelta(4)
			inst.components.shaqi:DoDelta(20)
		end
	  end 
		--[[
		--丹药属性tag
		if food:HasTag("pellet_item") then
			if food.prefab == "pellet_seed" then
				inst.components.health:DoDelta(5*lv)
			elseif food.prefab == "lotus_leaf" then
				inst.components.sanity:DoDelta(10*lv)
			end
		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

--蜜蜂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
	--击杀小动物增加煞气
	if data and data.victim and data.victim:HasTag("smallcreature")then
		if inst.components.shaqi then
			inst.components.shaqi:DoDelta(2)
		end
	end
	
end
--击杀怪物会得到各种炼药材料
local function pelletspawner(inst,data)
	if data and data.victim and data.victim:HasTag("monster")then
		if math.random() <= 0.2 then
			local victim = data.victim
			local x,y,z = victim.Transform:GetWorldPosition()
			local stuff1 = SpawnPrefab("linglibuchong")
			stuff1.Transform:SetPosition(x,y,z)
		end
	end
end

--被攻击召唤
local function zhaohuan_kikyou(inst, data)
		if math.random() <= 0.1 then
local x,y,z = inst.Transform:GetWorldPosition()	
local assasin1 = SpawnPrefab("assasin_kikyou")
local popo = SpawnPrefab("explode_small")
local drakeangle = math.random(1, 360)
local offset = FindWalkableOffset(inst:GetPosition(), drakeangle*DEGREES, math.random(2,8), 30, false, false)
popo.Transform:SetScale(1.25, 1.25, 1.25)
assasin1.Transform:SetPosition(x + offset.x, y + offset.y, z + offset.z)
popo.Transform:SetPosition(assasin1:GetPosition():Get())
SpawnPrefab("statue_transition_2").Transform:SetPosition(assasin1:GetPosition():Get()) 


        end
	  inst:RemoveEventCallback("attack", zhaohuan_kikyou)

end

--回灵力回血
local function applylingli_up(inst)
  if inst.components.hunger:GetPercent() > 0.6 then
	if math.random() < 0.3 then
	          local xyzq = SpawnPrefab("shaqi_start")
	    --SpawnPrefab("shaqi_start").Transform:SetPosition(inst:GetPosition():Get())	  
			  xyzq.entity:SetParent(inst.entity)
			  xyzq.Transform:SetPosition(inst:GetPosition():Get())
			  --xyzq.AnimState:PushAnimation("idle", true)		
	    inst.components.talker:Say("我感觉到灵力正在恢复！")
	    inst.components.hunger:DoDelta(-20)
		if inst and inst.task_lingliup then inst.task_lingliup:Cancel() inst.task_lingliup = nil end
		inst.task_lingliup = inst:DoPeriodicTask(0.5, function()
	    inst.components.lingli:DoDelta(5)
		end)
		inst:DoTaskInTime(10, function()
		if inst and inst.task_lingliup then inst.task_lingliup:Cancel() inst.task_lingliup = nil end
		end)		  
	end
  elseif inst.components.hunger:GetPercent() >= 0.8 and inst.components.shaqi:GetPercent() < 0.5 then
        SpawnPrefab("zhiliao").Transform:SetPosition(inst:GetPosition():Get())	 
        inst.components.talker:Say("伤口。。正在复原！！")
	    inst.components.hunger:DoDelta(-20)
		if inst and inst.task_huixue then inst.task_huixue:Cancel() inst.task_huixue = nil end
		inst.task_huixue = inst:DoPeriodicTask(0.5, function()
	    inst.components.lingli:DoDelta(5)
		end)
		inst:DoTaskInTime(10, function()
		if inst and inst.task_huixue then inst.task_huixue:Cancel() inst.task_huixue = nil end
		end) 
  
  end

end
--服装
local function applycloth(inst)
		if math.random() < 0.3 then
    SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("toso_blue2")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)
		inst.components.talker:Say("今天又是元气满满的一天!")		
		elseif math.random() >= 0.3 and math.random() < 0.6 then
    SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("toso_baqi")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)
		inst.components.talker:Say("一日之计在于晨!")	
		elseif math.random() >= 0.6 then
    SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
		inst.AnimState:SetBank("wilson")
		inst.AnimState:SetBuild("toso_blue")
		inst.AnimState:PlayAnimation("idle")
		inst.Transform:SetScale(1,1,1)
		inst.components.talker:Say("上天可曾眷念我!")		
		end


end

--废弃
local function changgebuild(inst)
    inst.AnimState:SetBank("stalker")
    inst.AnimState:SetBuild("stalker_atrium_build")
    --inst.AnimState:AddOverrideBuild(build)
    inst.AnimState:PlayAnimation("idle")
	inst.Transform:SetScale(1,1,1)
	inst.components.talker:Say("谁也无法逃过这劫!")
    inst:RemoveEventCallback("onhitother", changgebuild)
end



local function checkshaqitag(inst)

if inst:HasTag("Shaqihua") then
   inst:ListenForEvent("onhitother", changgebuild)


else
   inst:ListenForEvent("onhitother", applycloth)
   inst:RemoveEventCallback("onhitother", changgebuild)
end
end



local fn = function(inst)

	inst.exp = 0
	inst.level = 1
	inst.tosolevel = 1
	inst.toso_state = 0
	inst.damagemult_accident = 0
	inst.speed_accident = 0
	inst.hpabsorb_accident = 0
	inst.hp_reversion = 0
	inst.sanity_reversion = 0
    inst.shaqibaocun = false
    inst.change_data = 1	
	--inst.toso_skill_chance = 0	
	--inst.toso_san_drain = 0
    --inst.toso_absorb = 0 	
    --inst.toso_fire_absorb = 0
    --inst.toso_cold_resistance = 0
    --inst.toso_inherentinsulation = 0
    --inst.toso_summerinsulation = 0
	--inst.toso_cure_time = 0

--技能表--
--inst.components.health:GetPercent() < 0.5

local rpg_jineng = 
{

	[1] = function()
	local skillconsumemax1 = 20
    local skillconsume1 = skillconsumemax1*(1-0.03*inst.level)
    if inst.components.hunger.current >  skillconsume1 then
	inst.components.hunger:DoDelta(-skillconsume1)
    inst.components.tosocast:taqianzhan_attack(inst)
    else
	inst.components.talker:Say("体力不足")
	end
   
	end,
	
	[2] = function()
	local skillconsumemax2 = 20
    local skillconsume2 = skillconsumemax2*(1-0.03*inst.level)
    if inst.components.lingli.currentlingli >  skillconsume2 then
	inst.components.lingli:DoDelta(-skillconsume2)
    inst.components.tosocast:bingdongsanchi_attack(inst)
    else
	inst.components.talker:Say("灵力不足")
	end
	
	end,
	
	[3] = function()

    inst.components.tosocast:linglishifang_attack(inst)	

	end,
	
	[4] = function()
	local skillconsumemax4 = 20
    local skillconsume4 = skillconsumemax4*(1-0.03*inst.level)
    if inst.components.lingli.currentlingli >  skillconsume4 then
	inst.components.lingli:DoDelta(-skillconsume4)
	inst.components.tosocast:liehuodaofa_attack(inst)
    else
	inst.components.talker:Say("灵力不足")
	end
	
	end,	
	
	[5] = function()
	local skillconsumemax5 = 20
    local skillconsume5 = skillconsumemax5*(1-0.03*inst.level)
    if inst.components.lingli.currentlingli >  skillconsume5 then
	inst.components.lingli:DoDelta(-skillconsume5)	
    inst.components.tosocast:jianyu_attack(inst)
    else
	inst.components.talker:Say("灵力不足")
	end
	
	end,	
}
	



TheInput:AddKeyUpHandler(TUNING.TOSO_KEY2, function()
    if not IsPaused() then
if inst.change_data < 5 then
inst.change_data = inst.change_data + 1
else 
inst.change_data = 1
end

--判断
if inst.change_data == 1 then
    inst.components.talker:Say("踏前斩")
		GetPlayer().HUD.taqianzhan:Show()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
elseif inst.change_data == 2 then
    inst.components.talker:Say("零度")
		GetPlayer().HUD.shuangman:Show()
		GetPlayer().HUD.linghuazhuangtai:Hide()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
elseif inst.change_data == 3 then
    inst.components.talker:Say("灵力状态")
		GetPlayer().HUD.linghuazhuangtai:Show()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
elseif inst.change_data == 4 then
    inst.components.talker:Say("烈火刀法")
		GetPlayer().HUD.liehuodaofa:Show()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()
elseif inst.change_data == 5 then
    inst.components.talker:Say("焚寂剑雨")
		GetPlayer().HUD.fenjijianyu:Show()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
end

    end
end)

TheInput:AddKeyUpHandler(TUNING.TOSO_KEY3, function()
    if not IsPaused() then
if inst.change_data > 0 then
inst.change_data = inst.change_data - 1
else 
inst.change_data = 5
end

--判断
if inst.change_data == 1 then
    inst.components.talker:Say("踏前斩")
		GetPlayer().HUD.taqianzhan:Show()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
elseif inst.change_data == 2 then
    inst.components.talker:Say("零度")
		GetPlayer().HUD.shuangman:Show()
		GetPlayer().HUD.linghuazhuangtai:Hide()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
elseif inst.change_data == 3 then
    inst.components.talker:Say("灵力状态")
		GetPlayer().HUD.linghuazhuangtai:Show()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
elseif inst.change_data == 4 then
    inst.components.talker:Say("烈火刀法")
		GetPlayer().HUD.liehuodaofa:Show()
		GetPlayer().HUD.fenjijianyu:Hide()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()
elseif inst.change_data == 5 then
    inst.components.talker:Say("焚寂剑雨")
		GetPlayer().HUD.fenjijianyu:Show()
		GetPlayer().HUD.taqianzhan:Hide()
		GetPlayer().HUD.shuangman:Hide()
		GetPlayer().HUD.linghuazhuangtai:Hide()
		GetPlayer().HUD.liehuodaofa:Hide()
end

    end
end)

--R	
TheInput:AddKeyUpHandler(TUNING.TOSO_KEY1, function()
    if not IsPaused() then
	--local skillconsumemax = 20
	--local skillconsume = skillconsumemax*(1-inst.level*0.03)
rpg_jineng[inst.change_data]()

    end
end)



TheInput:AddKeyUpHandler(KEY_T, function()
		inst.components.talker:Say(
		"\n人物的等级["..(inst.level).."]"..
		"\n总精神抗性["..(inst.level*0.04)*100 .."%]"..
		"\n总物理吸收["..(inst.level*0.02)*100 .."%]"..
		"\n总火焰抗性["..(inst.level*0.04)*100 .."%]"..
		"\n总冰冻抗性["..(inst.level*0.04)*100 .."%]"..
		"\n总保温时间[".. (inst.level*20) .."]"..
		"\n总隔热时间["..(inst.level*12).."]"..
		"\n走路速度["..(inst.components.locomotor.walkspeed).."]"..
		"\n跑步速度["..(inst.components.locomotor.runspeed).."]"..
		"\n回血量["..(inst.level*0.2).."]"..
		"\n总回血间隔["..(110 - inst.level*4).."]")
		--"\n总隔热["..inst.components.temperature.inherentsummerinsulation.."]")
end)

	
	
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "toso.tex" )
	inst:AddTag("toso")
	inst:AddTag("tososhaqi")
	--测试添加
	inst:AddTag("nature_fire")
	inst:AddTag("nature_freeze")
	inst:AddTag("nature_lightning")
	inst:AddTag("nature_tree")
	inst:AddTag("dongzuo_leap")
	inst:AddTag("nature_water")
	inst:AddTag("nature_electric")
	inst:AddTag("character_bloodabsorb")
	--添加SG
    inst:SetStateGraph("SGwilson")	

    inst:AddComponent("shaqi")
    inst:AddComponent("lingli")
	inst:AddComponent("tosocast")
	-- 选择人物的声音
	inst.soundsname = "willow"
	--设定外表
	inst.AnimState:SetBuild("toso_blue2")
-------------------继承等级逐渐提升----------------
	--精神抗性
    inst.components.sanity.night_drain_mult = 1.00 - inst.level*0.04
	inst.components.sanity.neg_aura_mult = 1.00 - inst.level*0.04
	--初始伤害
    inst.components.combat.damagemultiplier = 0.75 + inst.level*0.01 + inst.damagemult_accident
	--火抗
	inst.components.health.fire_damage_scale = 1 - inst.level*0.04
	--冰抗
	inst.components.freezable.resistance = 1 - inst.level*0.04
	--正常的移动速度
	inst.components.locomotor.walkspeed = 4 + inst.level*0.1 + inst.speed_accident
	inst.components.locomotor.runspeed = 6 + inst.level*0.1 + inst.speed_accident


	--物抗
	inst.components.health.absorb = inst.level*0.02


	--保温
	inst.components.temperature.inherentinsulation = inst.level*20
	--隔热
	inst.components.temperature.inherentsummerinsulation = inst.level*12
	--回血
	inst.components.health:StartRegen(inst.level*0.2, 110 - inst.level*4)
	--回精神
	inst.components.sanity.dapperness = (inst.sanity_reversion)
	
	--基本特性设定，涉及部分与人物介绍相同
	-- 三围设定
	inst.components.health:SetMaxHealth(TUNING.TOSO_STARTHP)
	inst.components.hunger:SetMax(TUNING.TOSO_STARTHP)
	inst.components.sanity:SetMax(TUNING.TOSO_STARTHP)
	---击杀第一只怪物后生成春鹿
	--inst:ListenForEvent("killed",pelletspawner)
	--击杀小动物加煞气
	--inst:ListenForEvent("killed",killprey)
	--击杀加经验
	inst:ListenForEvent("killed",onkill)
	--击杀获得灵力补充剂
	inst:ListenForEvent("killed",pelletspawner)	
	--饥饿速率，标准
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	--设置被动，一定条件定时不可被冻住
    --inst:RemoveComponent("freezable")

	--吃怪物肉，加煞气/升级
	--inst.components.eater:SetOnEatFn(oneat)
	--inst.components.eater:SetOnEatFn(oneat111)
	inst.components.eater:SetOnEatFn(oneatexp_up)
	
	--各种小动物不害怕
	--inst:RemoveTag("scarytoprey")
	--添加树燃烧掉精神的特性,利用精神的组件完成，参考薇洛在火边加精神的特性，反向实现
	inst.components.shaqi.custom_rate_fn = shaqifn
    --被攻击
	--inst:ListenForEvent( "attacked", zhaohuan_kikyou)
	
	--用于触发人物的被动，利用hungerdelta随时检测
	--inst:ListenForEvent("hungerdelta",passivetest)
	--HUD经验等级
	inst:DoTaskInTime(0, function() SetHUDState(inst) end)	
	inst:DoTaskInTime(0, function() SetHUDState_UI(inst) end)	
	
	inst:ListenForEvent("onhitother", OnHitfire)
	inst:ListenForEvent("onhitother", OnHitfreeze)
	inst:ListenForEvent("onhitother", OnHitlightning)
	inst:ListenForEvent("onhitother", OnHittree)
	--inst:ListenForEvent("onhitother", OnHitleap)
	--inst:ListenForEvent("attacked", OnHitleap)
	--赞用于持续掉血
	inst:ListenForEvent("onhitother", OnHitwater)
	inst:ListenForEvent("onhitother", OnHitelectric)
	inst:ListenForEvent("onhitother", OnHitdouble)
	--煞气攻击
	inst:ListenForEvent("onhitother", panduanshaqifx)
	inst:ListenForEvent("attacked",OnHitfanji)


--船	
	inst.talker_path_override = "dontstarve_DLC002/characters/"

	local surfboard_recipe = Recipe("surfboard_item_toso", {Ingredient("boards", 1), Ingredient("seashell", 2)}, RECIPETABS.NAUTICAL, TECH.NONE,  RECIPE_GAME_TYPE.SHIPWRECKED)
	surfboard_recipe.sortkey = 1	

	--死亡
    --inst:ListenForEvent("death", ondeath)
	
    --inst:ListenForEvent("nighttime", function(global, data)
	--换装
    inst:ListenForEvent("daytime", function() applycloth(inst) end , GetWorld())
	--inst:ListenForEvent("dusktime", function() applycloth(inst) end , GetWorld())
    inst:ListenForEvent("nighttime", function() applylingli_up(inst) end , GetWorld())
	--煞气爆发
	inst:ListenForEvent("shaqidelta", shaqi_state)
	--闪避被动
    inst.dorge = inst:DoPeriodicTask(10,dorgecheck)	
	
	--inst.shaqifxattack = inst:DoPeriodicTask(0,panduanshaqifx)
	
	
--[[
	inst:ListenForEvent( "attacked", fshieldupgrade)
inst:ListenForEvent("hungerdelta", removeskill)
--inst:ListenForEvent("healthdelta", removeskill)
inst:ListenForEvent("hungerdelta", shadows)
inst:ListenForEvent("hungerdelta", active_sparkshield)
inst.powerattack = inst:DoPeriodicTask(0, powerattack)
inst.phasechange = inst:DoPeriodicTask(0, phasechange)
inst.sleep_test = inst:DoPeriodicTask(0, sleep_test)
inst.monster_test = inst:DoPeriodicTask(0, monster_test)
inst.consume_sleep1 = inst:DoPeriodicTask(1, consume_stamina)
inst.sleepheal = inst:DoPeriodicTask(6,onsleepheal)
inst.summon_drake = inst:DoPeriodicTask(6,summon_drake)
inst.summon_lighting = inst:DoPeriodicTask(15,summon_lighting)
inst.assasin_webber = inst:DoPeriodicTask(2,assasin_webber)
inst.berserk_changer = inst:DoPeriodicTask(1,berserk_changer)
inst.dizzy = inst:DoPeriodicTask(15,dizzy)
]]
	
	applyupgrades(inst)
	inst.OnSave = onsave
	inst.OnLoad = onload
    inst.OnPreLoad = onpreload

		--再次添加专属制作栏----------------------------------------------------------------------------------------------------
	--CustomRecipe()
	
end


return MakePlayerCharacter("toso", prefabs, assets, fn, start_inv)