require "class"
local Badge = require "widgets/badge"
local easing = require "easing"
local brain = require "brains/summonclonebrain"
local brain_boat = require "brains/summonclonebrain_boat"
local function OnSane(inst)
	print ("SANE!")
end

local function OnInsane(inst)
---	inst.SoundEmitter:PlaySound("dontstarve/sanity/gonecrazy_stinger")
end

--[[
local _assets =
{
    dont_starve =
    {
        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_cave_enter.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/player_slurtle_armor.zip"),
        Asset("ANIM", "anim/player_staff.zip"),

		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),

        Asset("ANIM", "anim/fish01.zip"),   --These are used for the fishing animations.
        Asset("ANIM", "anim/eel01.zip"),
		
		
		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),
		Asset("ANIM", "anim/willow.zip"),
		Asset("SOUND", "sound/willow.fsb"),
		Asset("ANIM", "anim/wes.zip"),
		Asset("ANIM", "anim/wolfgang.zip"),
		Asset("SOUND", "sound/wolfgang.fsb"),
		Asset("ANIM", "anim/wendy.zip"),
		Asset("SOUND", "sound/wendy.fsb"),
		Asset("ANIM", "anim/wx78.zip"),
		Asset("SOUND", "sound/wx78.fsb"),
		Asset("SOUND", "sound/maxwell.fsb"),
		Asset("ANIM", "anim/waxwell.zip"),
		Asset("SOUND", "sound/wickerbottom.fsb"),
		Asset("ANIM", "anim/wickerbottom.zip"),
		Asset("SOUND", "sound/woodie.fsb"),
		Asset("ANIM", "anim/woodie.zip"),
    },
	
	
    shipwrecked =
    {
		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_speargun.zip"),//
		--Asset("ANIM", "anim/player_actions_machete.zip"), --This file overwrites the axe actions file and is broken...
		Asset("ANIM", "anim/player_actions_eat.zip"),
		Asset("ANIM", "anim/player_actions_item.zip"),
		Asset("ANIM", "anim/player_actions_paddle.zip"),
		Asset("ANIM", "anim/player_cave_enter.zip"),
		Asset("ANIM", "anim/player_actions_uniqueitem.zip"),
		Asset("ANIM", "anim/book_uniqueitem_swap.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/player_slurtle_armor.zip"),
		Asset("ANIM", "anim/player_staff.zip"),
		Asset("ANIM", "anim/player_boat_onoff.zip"),
		Asset("ANIM", "anim/player_boat_death.zip"),
		Asset("ANIM", "anim/player_actions_trawl.zip"),

		Asset("ANIM", "anim/player_actions_telescope.zip"),
		Asset("ANIM", "anim/player_idles_poison.zip"),


		Asset("ANIM", "anim/player_frozen.zip"),
		Asset("ANIM", "anim/player_shock.zip"),
		Asset("ANIM", "anim/shock_fx.zip"),
		Asset("ANIM", "anim/player_tornado.zip"),


		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),
		Asset("ANIM", "anim/willow.zip"),
		Asset("SOUND", "sound/willow.fsb"),
		Asset("ANIM", "anim/wes.zip"),
		Asset("ANIM", "anim/wolfgang.zip"),
		Asset("SOUND", "sound/wolfgang.fsb"),
		Asset("ANIM", "anim/wendy.zip"),
		Asset("SOUND", "sound/wendy.fsb"),
		Asset("ANIM", "anim/wx78.zip"),
		Asset("SOUND", "sound/wx78.fsb"),
		Asset("SOUND", "sound/maxwell.fsb"),
		Asset("ANIM", "anim/waxwell.zip"),
		Asset("SOUND", "sound/wickerbottom.fsb"),
		Asset("ANIM", "anim/wickerbottom.zip"),
		Asset("SOUND", "sound/woodie.fsb"),
		Asset("ANIM", "anim/woodie.zip"),
	
	},
   
    reign_of_giants =
    {
        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_cave_enter.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/player_slurtle_armor.zip"),
        Asset("ANIM", "anim/player_staff.zip"),        

		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),
		Asset("ANIM", "anim/willow.zip"),
		Asset("SOUND", "sound/willow.fsb"),
		Asset("ANIM", "anim/wes.zip"),
		Asset("ANIM", "anim/wolfgang.zip"),
		Asset("SOUND", "sound/wolfgang.fsb"),
		Asset("ANIM", "anim/wendy.zip"),
		Asset("SOUND", "sound/wendy.fsb"),
		Asset("ANIM", "anim/wx78.zip"),
		Asset("SOUND", "sound/wx78.fsb"),
		Asset("SOUND", "sound/maxwell.fsb"),
		Asset("ANIM", "anim/waxwell.zip"),
		Asset("SOUND", "sound/wickerbottom.fsb"),
		Asset("ANIM", "anim/wickerbottom.zip"),
		Asset("SOUND", "sound/woodie.fsb"),
		Asset("ANIM", "anim/woodie.zip"),

	},
   

}
 
--local assets = {}
 



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_speargun.zip"),
		--Asset("ANIM", "anim/player_actions_machete.zip"), --This file overwrites the axe actions file and is broken...
		Asset("ANIM", "anim/player_actions_eat.zip"),
		Asset("ANIM", "anim/player_actions_item.zip"),
		Asset("ANIM", "anim/player_actions_paddle.zip"),
		Asset("ANIM", "anim/player_cave_enter.zip"),
		Asset("ANIM", "anim/player_actions_uniqueitem.zip"),
		Asset("ANIM", "anim/book_uniqueitem_swap.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/player_slurtle_armor.zip"),
		Asset("ANIM", "anim/player_staff.zip"),
		Asset("ANIM", "anim/player_boat_onoff.zip"),
		Asset("ANIM", "anim/player_boat_death.zip"),
		Asset("ANIM", "anim/player_actions_trawl.zip"),

		--Asset("ANIM", "anim/player_actions_telescope.zip"),
		Asset("ANIM", "anim/player_idles_poison.zip"),


		Asset("ANIM", "anim/player_frozen.zip"),
		Asset("ANIM", "anim/player_shock.zip"),
		Asset("ANIM", "anim/shock_fx.zip"),
		Asset("ANIM", "anim/player_tornado.zip"),


		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),
		Asset("ANIM", "anim/willow.zip"),
		Asset("SOUND", "sound/willow.fsb"),
		Asset("ANIM", "anim/wes.zip"),
		Asset("ANIM", "anim/player_mime.zip"),    
		Asset("ANIM", "anim/wolfgang.zip"),
		Asset("SOUND", "sound/wolfgang.fsb"),
		Asset("ANIM", "anim/wendy.zip"),
		Asset("SOUND", "sound/wendy.fsb"),
		Asset("ANIM", "anim/wx78.zip"),
		Asset("SOUND", "sound/wx78.fsb"),
		Asset("SOUND", "sound/maxwell.fsb"),
		Asset("ANIM", "anim/waxwell.zip"),
		Asset("SOUND", "sound/wickerbottom.fsb"),
		Asset("ANIM", "anim/wickerbottom.zip"),
		Asset("SOUND", "sound/woodie.fsb"),
		Asset("ANIM", "anim/woodie.zip"),
	
}

--]]

local prefabs =
{
"abigail_3","lucy","lighter"
}

local OnLoad = function(inst, data)
	if data and data.char then
		print("data.char = "..data.char)
		GetPlayer().components.p3stats_saver.shstats[inst.plnum]["char"] = data.char
		inst.newplayername = data.char
	end
end

local OnSave = function(inst, data)

		data.sleeping = true
		data.char = GetPlayer().components.p3stats_saver.shstats[inst.plnum]["char"]
		
end

local function CalcSanityAura(inst, observer)
	
	if inst.components.follower and inst.components.follower.leader == observer then
		return -TUNING.SANITYAURA_LARGE
	end
	
	return 0
end


local function ontalk(inst, script)
	if not inst:HasTag("p3talkcd") then
	inst:AddTag("p3talkcd")
	inst.SoundEmitter:PlaySound("dontstarve/characters/"..inst.typeoftalks.."/talk_LP","p3talksound")
	print("dark clone sound")
	inst:DoTaskInTime(1.8, function()
	inst.SoundEmitter:KillSound("p3talksound")
	
    inst:RemoveTag("p3talkcd")
    end)
	end
end



local function oneatwx78(inst, food)
	
	if food and food.components.edible and math.random()<0.05 then
		inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")

		
		--inst.components.hunger:DoDelta(225)
		--inst.components.sanity:DoDelta(225)
	--	inst.components.health:DoDelta(225)	
		local goldnugget = SpawnPrefab("goldnugget")
		local pt = inst:GetPosition()
		goldnugget.Transform:SetPosition(pt.x, pt.y, pt.z)
	end
end



local function CalcSanityAura_Maxwell(inst, observer)
	return 0
end

local function ShouldAcceptItem(inst, item)
        	return true
end

local function OnGetItemFromPlayer(inst, giver, item)
  --  	inst.components.container:GiveItem(item)
  
			inst.components.inventory:DropItem(item, true)		
--			inst.components.inventory:RemoveItemBySlot(num)	
---			inst.components.container:RemoveItemBySlot(num)		
end

local function OnRefuseItem(inst, item)

end

local function NormalRetargetFn(inst)
    return FindEntity(inst, TUNING.PIG_TARGET_DIST,
        function(guy)
                return guy:HasTag("monster") and guy.components.health and not guy.components.health:IsDead() and inst.components.combat:CanTarget(guy)
        end)
end


local function OnOpen(inst)
end 

local function OnClose(inst) 
end 

local slotpos = {}


	for y = 0, 4 do		--5 rows, 3 cols
		--							 ([hor] [loc], [hor width between rows] + [ver] ,0))	
		table.insert(slotpos, Vector3(-25 -150, -y*75 + 185 ,0)) --New column
		table.insert(slotpos, Vector3(-25  -75, -y*75 + 185 ,0)) --original column
		table.insert(slotpos, Vector3(-25     , -y*75 + 185 ,0)) --col at screen edge
	end


local function OnAttackedWendy(inst, data)
    inst:DoTaskInTime(0, function() 
    		local found = false
    		for k,v in pairs(Ents) do
    			if v.prefab == "abigail_3" then
    				found = true
    				break
    			end
    		end
    		if not found then
    local attacker = data.attacker
    local abigail_3 = SpawnPrefab("abigail_3")
    abigail_3.Transform:SetPosition(inst.Transform:GetWorldPosition())
    abigail_3.components.combat:SetTarget(attacker)
    		end
    	end)
end

local function DodgeTrick(inst, data)

    local attacker = data.attacker
	local dmg = data.damage
	if attacker and (not inst:HasTag("dodgetrick_cd")) and dmg>4 and not inst.components.combat:InCooldown() then
		inst.components.health:DoDelta(dmg)
		inst:AddTag("dodgetrick_cd")
		inst:DoTaskInTime(6, function() 
			inst:RemoveTag("dodgetrick_cd")
		end)
	end
	 
	

end


---OnWendyAttack

local function OnWendyAttack(inst, other)
    inst:DoTaskInTime(0, function() 
    		local found = false
    		for k,v in pairs(Ents) do
    			if v.prefab == "abigail_3" then
    				found = true
    				break
    			end
    		end
    		if not found then
    local target = other
    local abigail_3 = SpawnPrefab("abigail_3")
    abigail_3.Transform:SetPosition(inst.Transform:GetWorldPosition())
    abigail_3.components.combat:SetTarget(other)
    		end
    	end)
end

local function onwigfridkill(inst, data)
	if data.cause == inst.prefab 
		and not data.inst:HasTag("prey") 
		and not data.inst:HasTag("veggie") 
		and not data.inst:HasTag("structure") then
		local delta = (data.inst.components.combat.defaultdamage) * 0.25
        inst.components.health:DoDelta(delta, false, "battleborn")
        inst.components.sanity:DoDelta(delta)

        if math.random() < .1 and not data.inst.components.health.nofadeout then
        	local time = data.inst.components.health.destroytime or 2
        	inst:DoTaskInTime(time, function()
        		local s = medScale
        		if data.inst:HasTag("smallcreature") then
        			s = smallScale
    			elseif data.inst:HasTag("largecreature") then
    				s = largeScale
    			end
        		local fx = SpawnPrefab("wathgrithr_spirit")
        		fx.Transform:SetPosition(data.inst:GetPosition():Get())
        		fx.Transform:SetScale(s,s,s)
    		end)
        end

	end
end


local function OnDamagedByPlayer(inst, data)
    local attacker = data.attacker
	local dmg = data.damage
	if attacker and attacker:HasTag("player_coop_team") then
		inst.components.health:DoDelta(dmg)
		attacker.components.combat:GiveUp()
	elseif inst:HasTag("isghost") then
		inst.components.health:SetPercent(1)
	end
end

local function CanBeAttacked(inst, attacker)
    return not (attacker:HasTag("player_coop_team"))
end




local function DropLastItem(inst,data)
	inst.SoundEmitter:PlaySound("dontstarve/HUD/collect_resource")
	
	local last = data.item
	if inst.components.inventory:IsFull() then
			inst:AddTag("no_actions_full_inv")


	--	local item = last
		local item = inst.components.inventory:GetItemInSlot(16)
	--	print("item last="..item.prefab)
		
		inst.components.talker:Say("I can not carry any more!")
		
		
		inst:DoTaskInTime(0.5, function() 
			inst.components.inventory:DropItem(item, true)	
			--inst.components.container:DropItem(item, true)	
			inst.components.inventory:RemoveItem(item,true)	
		--	inst.components.container:RemoveItem(item,true)	
			inst:RemoveTag("no_actions_full_inv")
		end)	
		
		
		
	end
end

local function OnAttackedWaxwell(inst, data)
    local attacker = data.attacker
	local dmg = data.damage
	if not inst:HasTag("cdlf") and attacker.components.health.currenthealth > 1 then
	attacker.components.health:DoDelta(-dmg)
	inst.components.health:DoDelta(dmg)
	print("lifeSteal")
	
	inst:AddTag("cdlf")
	inst:DoTaskInTime(0.7, function() 
    inst:RemoveTag("cdlf")
    end)

end
end



local function makeactive(inst)
	inst.components.activatable.inactive = nil
	inst.components.resurrector.used = false
	inst.components.resurrector.active = true
end




local function makeused(inst)
	inst.components.activatable.inactive = nil
	inst.components.resurrector.used = false
	inst.components.resurrector.active = true
end

local function doresurrect(inst, dude)
	dude.components.p3stats_saver:RespawnDelta(-1,dude)
	
	--[[local canrespawn = 	dude.components.p3stats_saver:CanRespawn()
	if canrespawn == false then
		print("I am dying just to let you live...")
	end--]]
	local phrase = ""
	local respawnsleft = dude.components.p3stats_saver.stat["respawnsleft"] or 322
	if respawnsleft == 322 then
		phrase = "Rise!"
	else
		phrase = "We've got "..respawnsleft.." resurrections left!"
	end
	inst.components.talker:Say(phrase)
	local wasonwater = false
	if dude.components.driver then
		dude:RemoveComponent("keeponland")

		if dude.components.driver:GetIsDriving() then 
			dude.components.driver:OnDismount()
		end
	end 
	
	inst.components.health:SetPercent(.35)
	inst.components.sanity:DoDelta(-25)
	inst.components.hunger:DoDelta(-25)

    dude.Transform:SetPosition(inst.Transform:GetWorldPosition())
    dude:Hide()
    TheCamera:SetDistance(12)
	dude.components.hunger:Pause()
	
    scheduler:ExecuteInTime(3, function()
        dude:Show()
        
        if dude.components.hunger then
            dude.components.hunger:SetPercent(2/3)
        end

        if dude.components.health then
            dude.components.health:Respawn(TUNING.RESURRECT_HEALTH)
        end
        
        if dude.components.sanity then
			dude.components.sanity:SetPercent(.5)
        end
		if dude.components.moisture then
        	dude.components.moisture.moisture = 0
        end

        if dude.components.temperature then
        	dude.components.temperature:SetTemperature(40)
        end
		
        dude.components.hunger:Resume()
        
        
		dude.HUD:Show()
		
        dude:RemoveTag("alreadyresseructing")
		if (inst.components.driver and inst.components.driver:GetIsDriving()) or inst.boatingwith~=0 then 
			wasonwater=true
		end
		if wasonwater==true then
			local boat = SpawnPrefab("lograft")
			boat.Transform:SetPosition(inst:GetPosition():Get())
			dude.components.driver:OnMount(boat)
			dude.components.playercontroller:Enable(true)
			boat.components.boathealth.currenthealth = 10
			boat.components.boathealth:SetInvincible(false)
			boat.components.boathealth:StopConsuming()
			boat.components.boathealth:StartConsuming()
		else
			dude.sg:GoToState("wakeup")
		end

			
			inst:RemoveComponent("resurrector")
			
			inst:AddComponent("resurrector")
			inst.components.resurrector.makeactivefn = makeactive
			inst.components.resurrector.makeusedfn = makeused
			inst.components.resurrector.doresurrect = doresurrect
			inst.components.resurrector.active = true
			inst.components.resurrector:OnBuilt()
			
			if dude.components.driver then
				dude:DoTaskInTime(4, function()
					dude:AddComponent("keeponland")
				end)
			end
    end)
	
	
	
	if dude.components.p3stats_saver.stat["respawnsleft"]<1 then
		inst.components.health:Kill()
	
	end
end

local function DropItem(inst, target, item)
	inst.components.inventory:Unequip(EQUIPSLOTS.HANDS, true)
	inst.components.inventory:DropItem(item)
	if item.Physics then

		local x, y, z = item:GetPosition():Get()
		y = .3
		item.Physics:Teleport(x,y,z)

		local hp = target:GetPosition()
		local pt = inst:GetPosition()
		local vel = (hp - pt):GetNormalized()
		local speed = 3 + (math.random() * 2)
		local angle = -math.atan2(vel.z, vel.x) + (math.random() * 20 - 10) * DEGREES
		item.Physics:SetVel(math.cos(angle) * speed, 10, math.sin(angle) * speed)
		inst.components.talker:Say("Damn it!")
	end
end

local function OnWork(inst, data)
	--Tool slip.
	local m = inst.components.moisture

	if m:GetSegs() < 4 then
		return
	end

	local mm = GetWorld().components.moisturemanager
	local tool = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if tool and mm:IsEntityWet(tool) and math.random() < easing.inSine(m:GetMoisture(), 0, 0.15, m.moistureclamp.max)  then
		if not tool:HasTag("notslippery") then
			DropItem(inst, data.target, tool)
			--Lock out from picking up for a while?
		end
	end
end

local function onworked(inst, data)
	if inst and data.target and data.target.components.workable and data.target.components.workable.action == ACTIONS.CHOP then
		inst.components.health:DoDelta(0.33,true)
		inst.components.sanity:DoDelta(0.66,true)
		inst.components.hunger:DoDelta(0.33,true)
	end
end

function MakePlayer3(plnum, forreturn, version)

	local neededassets = 
	{
		Asset("ANIM", "anim/willow.zip"),
		Asset("SOUND", "sound/willow.fsb"),
		Asset("ANIM", "anim/wes.zip"),
		Asset("ANIM", "anim/player_mime.zip"),    
		Asset("ANIM", "anim/wolfgang.zip"),
		Asset("SOUND", "sound/wolfgang.fsb"),
		Asset("ANIM", "anim/wendy.zip"),
		Asset("SOUND", "sound/wendy.fsb"),
		Asset("ANIM", "anim/wx78.zip"),
		Asset("SOUND", "sound/wx78.fsb"),
		Asset("SOUND", "sound/maxwell.fsb"),
		Asset("ANIM", "anim/waxwell.zip"),
		Asset("SOUND", "sound/wickerbottom.fsb"),
		Asset("ANIM", "anim/wickerbottom.zip"),
		Asset("SOUND", "sound/woodie.fsb"),
		Asset("ANIM", "anim/woodie.zip"),
		Asset("SOUND", "sound/webber.fsb"),
		Asset("ANIM", "anim/webber.zip"),	
		Asset("ANIM", "anim/wathgrithr.zip"),	
		Asset("SOUND", "sound/wathgrithr.fsb"),
		
        Asset("ANIM", "anim/player_revive_ghosthat.zip"),
        Asset("ANIM", "anim/player_ghost_withhat.zip"),
		Asset("ANIM", "anim/ghost_wx78_build.zip"),		
		Asset("ANIM", "anim/ghost_wickerbottom_build.zip"),
		Asset("ANIM", "anim/ghost_woodie_build.zip"),
		Asset("ANIM", "anim/ghost_winona_build.zip"),			
		Asset("ANIM", "anim/ghost_waxwell_build.zip"),		
		Asset("ANIM", "anim/ghost_wathgrithr_build.zip"),
		Asset("ANIM", "anim/ghost_willow_build.zip"),		
		Asset("ANIM", "anim/ghost_wes_build.zip"),
		Asset("ANIM", "anim/ghost_wolfgang_build.zip"),		
		
		Asset("ANIM", "anim/ghost_webber_build.zip"),		
		Asset("ANIM", "anim/ghost_wilson_build.zip"),
		Asset("ANIM", "anim/ghost_wendy_build.zip"),
		Asset("SOUND", "sound/ghost.fsb"),
	}
	
	local dsassets = 
    {
        Asset("ANIM", "anim/player_revive_ghosthat.zip"),
        Asset("ANIM", "anim/player_ghost_withhat.zip"),
        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_cave_enter.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/player_slurtle_armor.zip"),
        Asset("ANIM", "anim/player_staff.zip"),

		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),

        Asset("ANIM", "anim/fish01.zip"),   --These are used for the fishing animations.
        Asset("ANIM", "anim/eel01.zip"),
    }
	
	local rogassets = 
    {
	    Asset("ANIM", "anim/wathgrithr.zip"),
		Asset("SOUND", "sound/wathgrithr.fsb"),	
		
        Asset("ANIM", "anim/player_revive_ghosthat.zip"),
        Asset("ANIM", "anim/player_ghost_withhat.zip"),
        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_cave_enter.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/player_slurtle_armor.zip"),
        Asset("ANIM", "anim/player_staff.zip"),

        Asset("ANIM", "anim/player_frozen.zip"),
        Asset("ANIM", "anim/player_shock.zip"),
        Asset("ANIM", "anim/shock_fx.zip"),
        Asset("ANIM", "anim/player_tornado.zip"),

		Asset("ANIM", "anim/shadow_hands.zip"),

        Asset("SOUND", "sound/sfx.fsb"),
        Asset("SOUND", "sound/wilson.fsb"),

        Asset("ANIM", "anim/fish01.zip"),   --These are used for the fishing animations.
        Asset("ANIM", "anim/eel01.zip"),
    }

	local swassets = 
	{
		Asset("SOUND", "sound/webber.fsb"),
		Asset("ANIM", "anim/webber.zip"),
		Asset("ANIM", "anim/wathgrithr.zip"),
		Asset("SOUND", "sound/wathgrithr.fsb"),	
		Asset("ANIM", "anim/walani.zip"),

        Asset("ANIM", "anim/player_revive_ghosthat.zip"),
        Asset("ANIM", "anim/player_ghost_withhat.zip"),	
		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_speargun.zip"),
		--Asset("ANIM", "anim/player_actions_machete.zip"), --This file overwrites the axe actions file and is broken...
		Asset("ANIM", "anim/player_actions_eat.zip"),
		Asset("ANIM", "anim/player_actions_item.zip"),
		Asset("ANIM", "anim/player_actions_paddle.zip"),
		Asset("ANIM", "anim/player_cave_enter.zip"),
		Asset("ANIM", "anim/player_actions_uniqueitem.zip"),
		Asset("ANIM", "anim/book_uniqueitem_swap.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/player_slurtle_armor.zip"),
		Asset("ANIM", "anim/player_staff.zip"),
		Asset("ANIM", "anim/player_boat_onoff.zip"),
		Asset("ANIM", "anim/player_boat_death.zip"),
		Asset("ANIM", "anim/player_actions_trawl.zip"),

		Asset("ANIM", "anim/player_actions_telescope.zip"),
		Asset("ANIM", "anim/player_idles_poison.zip"),


		Asset("ANIM", "anim/player_frozen.zip"),
		Asset("ANIM", "anim/player_shock.zip"),
		Asset("ANIM", "anim/shock_fx.zip"),
		Asset("ANIM", "anim/player_tornado.zip"),

		Asset("ANIM", "anim/shadow_hands.zip"),

		Asset("SOUND", "sound/sfx.fsb"),
		Asset("SOUND", "sound/wilson.fsb"),

		Asset("ANIM", "anim/fish01.zip"),   --These are used for the fishing animations.
		Asset("ANIM", "anim/eel01.zip"),
		
	}
	
	local dlcassets = {}
	
	if TUNING.COOP_FIX_DLC_CONFIG == "Unused" or TUNING.COOP_FIX_DLC_CONFIG == "RoG+SW" then
		if REIGN_OF_GIANTS and IsDLCEnabled(REIGN_OF_GIANTS) and version=="rog" then
			dlcassets = rogassets
			print("ASSETS LOADED == REIGN_OF_GIANTS")
		elseif CAPY_DLC and IsDLCEnabled(CAPY_DLC) and version=="sw" then
			dlcassets = swassets
			print("ASSETS LOADED == CAPY_DLC")
		else 
			dlcassets = dsassets
			print("ASSETS LOADED == BASE GAME")
		end
		
	elseif TUNING.COOP_FIX_DLC_CONFIG == "RoG" and version=="rog" then
		dlcassets = rogassets
		print("ASSETS LOADED == REIGN_OF_GIANTS")
		
	elseif TUNING.COOP_FIX_DLC_CONFIG == "SW" and version=="sw" then
		dlcassets = swassets
		print("ASSETS LOADED == CAPY_DLC")	
		
	elseif TUNING.COOP_FIX_DLC_CONFIG == "Vanilla" then
		dlcassets = dsassets
		print("ASSETS LOADED == BASE GAME")	
		
	else
		dlcassets = dsassets
		print("ASSETS LOADED == BASE GAME")
	end
	
	if dlcassets then
		for k,v in ipairs(dlcassets) do
			table.insert(neededassets, v)
		end
	end





	
local function fn(Sim)
    
	
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	local sound = inst.entity:AddSoundEmitter()
	local shadow = inst.entity:AddDynamicShadow()
	local whoisplayer = GetPlayer()
	inst.anim = anim
	
	
	shadow:SetSize( 1.5, .75 )
    inst.Transform:SetFourFaced()
	
    MakeCharacterPhysics(inst, 30, .3)

    inst:AddTag("NOCLICK")
    inst:AddTag("summonclone")
    inst:AddTag("scarytoprey")
	inst:AddTag("player3")
	inst:AddTag("character")
	inst:AddTag("player_coop_team")
	inst:AddTag("playerfriends")
	inst:AddTag("companion")

    anim:SetBank("wilson")
	
	
	local light = inst.entity:AddLight()
	inst.light = light
	light:SetIntensity(.6)
	light:SetRadius(.5)
	light:SetFalloff(.6)
	light:Enable(false)

	
	light:SetColour(175/255, 70/255, 0/255)
	inst.colour = {175/255, 70/255, 0/255}
    ------------------------------------------

	
	
    inst:AddComponent("locomotor") -- locomotor must be constructed before the stategraph
    inst.components.locomotor.runspeed = 6
    inst.components.locomotor.walkspeed = 6
	inst.components.locomotor:SetSlowMultiplier( 0.6 )
	--inst.components.locomotor.pathcaps = { player = true, ignorecreep = true } -- 'player' cap not actually used, just useful for testing
	inst.components.locomotor.fasteronroad = true
    ------------------------------------------
    ------------------------------------------


    MakeMediumBurnableCharacter(inst, "pig_torso")
    ------------------
    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(TUNING.WILSON_HEALTH)
	
	
    inst:AddComponent("hunger")
    inst.components.hunger:SetMax(TUNING.WILSON_HUNGER)
    inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE)
    inst.components.hunger:SetKillRate(TUNING.WILSON_HEALTH/TUNING.STARVE_KILL_TIME)
    ------------------
    
    inst:AddComponent("combat")
    inst.components.combat:SetDefaultDamage(TUNING.UNARMED_DAMAGE)
	inst.components.combat.damagemultiplier = 1
    --inst.components.combat.hiteffectsymbol = "pig_torso"
    inst.components.combat:SetAttackPeriod(TUNING.WILSON_ATTACK_PERIOD)
	inst:ListenForEvent("attacked", OnDamagedByPlayer) ---restore health if attacked by player
	inst.components.combat.canbeattackedfn = CanBeAttacked------player coop team can't attack each other
	
    inst:AddComponent("talker")
    inst.components.talker:StopIgnoringAll()
	inst.components.talker.ontalk = ontalk
	------------------------------------------
    
    inst:AddComponent("inventory")
	
	inst.components.inventory.maxslots = 16

	local lightwatch = inst.entity:AddLightWatcher()
    lightwatch:SetLightThresh(.075)
    lightwatch:SetDarkThresh(.05)

	

	
    inst:AddComponent("temperature")
	inst:AddComponent("eater")

	inst:AddComponent("follower")
	
	inst:AddComponent("catcher")
	
	
		--[[local function loadassets( _assets )
			for _, asset in pairs(_assets) do
				table.insert(assets, asset)
			end
		end
		 

		if IsDLCEnabled(REIGN_OF_GIANTS) then
			loadassets(_assets.reign_of_giants)
			print("ASSETS LOADED == REIGN_OF_GIANTS")
		elseif IsDLCEnabled(CAPY_DLC) then
			loadassets(_assets.shipwrecked)
			print("ASSETS LOADED == CAPY_DLC")

		else
					print("ASSETS LOADED == BASE GAME")

			loadassets(_assets.dont_starve)
		end			--]]
				
	
	--//----------------------------------------------------------------//--
	---------------------/////////////Customising character/////////////---------------------

	anim:SetBuild("wilson")
	
	local newtagforclone = ""
	
	
	newtagforclone = whoisplayer.components.p3stats_saver.shstats[plnum]["char"]--- or "willow"
	
	if version=="ds" and newtagforclone == "wathgrithr" then 
		newtagforclone="wilson"
		whoisplayer.components.p3stats_saver.shstats[plnum]["char"]="wilson"
	end
	
	inst.newplayername = newtagforclone
	if not inst.newplayername then
		inst.newplayername = "wilson"
		whoisplayer.components.p3stats_saver.shstats[plnum]["char"]="wilson"		
	end

	
	inst:AddTag(inst.newplayername)
	local name = inst.newplayername
	anim:SetBuild(inst.newplayername)

	print("ADDING TAG: "..inst.newplayername)
	
	inst.plnum = plnum
	
	inst.typeoftalks = inst.newplayername
	if inst.typeoftalks == "waxwell" or inst.typeoftalks == "maxwell" then inst.typeoftalks = "maxwell"	end

	if inst.plnum == 3 then
		inst.plclr = {200/255, 75/255, 125/255}
	    --inst.components.talker.colour = Vector3(200/255, 75/255, 125/255)	
	elseif inst.plnum == 2 then
		inst.plclr = {255/255, 160/255, 40/255}

	--    inst.components.talker.colour = Vector3(255/255, 160/255, 40/255)
	else 
	
		inst.plclr = {255/255, 255/255, 255/255}
	
	end

	
	
	--//----------------------------------------------------------------//--
	---------------------/////////////R o G/////////////---------------------
	if GetPlayer().components.moisture then
		print("adding moisture component")
		inst:AddComponent("moisture")
		
		
		inst:ListenForEvent("working", OnWork)
		inst:AddComponent("sanity")
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY)
		inst.components.sanity.onSane = OnSane
		inst.components.sanity.onInsane = OnInsane
	end
	---------------------/////////////rog end/////////////---------------------
	--//----------------------------------------------------------------//--
	--//----------------------------------------------------------------//--
	


	--//----------------------------------------------------------------//--
	---------------------/////////////Sheep Wrecked/////////////---------------------
	if GetPlayer().components.poisonable then
		MakePoisonableCharacter(inst)
		inst.components.poisonable.duration = TUNING.POISON_DURATION*3
		inst.components.poisonable.transfer_poison_on_attack = false
		
		inst:AddComponent("Player3KeepOnLand")
		
		inst.Soak = function()
			local percent = 1 - inst.components.inventory:GetWaterproofness()
			print("Soaker!! "..percent)
			inst.components.moisture:Soak(percent)
			--inst.components.inventory:Soak()
			GetWorld().components.inventorymoisture:Soak(percent)
		end
		
		inst.components.combat.hiteffectsymbol = "torso"
		
		MakeHugeFreezableCharacter(inst)
		inst.components.freezable:SetDefaultWearOffTime(TUNING.PLAYER_FREEZE_WEAR_OFF_TIME)
		
		
		inst:AddComponent("driver")
		inst.components.driver.landstategraph = "SGcloneplayer"
		inst.components.driver.boatingstategraph = "SGcloneboating_alone"
		inst.components.driver.onStartDriving = function()
			--Tell the state graph that we're about to start driving
			--inst:PushEvent("driving_start")
			--This is now handled by an event sent to the state graph
		end

		inst.components.driver.onStopDriving = function()
			--Tell the state graph that we're about to stop driving
			--inst:PushEvent("driving_stop")
			--This is now handled by an event sent to the state graph
		end
		

		
		
		
		
		if( name == "wolfgang" or name == "wickerbottom" or name == "wes" ) then
			anim:OverrideSymbol("torso_pelvis", name, "torso" ) --put the torso in pelvis slot to go behind
			anim:OverrideSymbol("torso", name, "torso_pelvis" ) --put the pelvis on top of the base torso by putting it in the torso slot
        end

		anim:PlayAnimation("idle")

		anim:Hide("ARM_carry")
		anim:Hide("hat")
		anim:Hide("hat_hair")
		anim:Hide("PROPDROP")
		anim:OverrideSymbol("fx_wipe", "wilson_fx", "fx_wipe")
		anim:OverrideSymbol("fx_liquid", "wilson_fx", "fx_liquid")
		anim:OverrideSymbol("shadow_hands", "shadow_hands", "shadow_hands")

		anim:OverrideSymbol("ripplebase", "player_boat_death", "ripplebase")
		anim:OverrideSymbol("waterline", "player_boat_death", "waterline")

	end
	---------------------/////////////end sw/////////////---------------------
	--//----------------------------------------------------------------//--
	--//----------------------------------------------------------------//--
	--//----------------------------------------------------------------//--
	
		inst:AddComponent("sanity")
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY)
		inst.components.sanity.onSane = OnSane
		inst.components.sanity.onInsane = OnInsane
	
	
	--//----------------------------------------------------------------//--
	---------------------/////////////Customising stats of character/////////////---------------------


	inst.colour = {1,1,1}---for ghosts
	inst.ghostmaxinv = 5
	if inst:HasTag("wilson") then
	
		inst:AddTag("bearded")	
		
	    inst:AddComponent("beard")
    	inst.components.beard.onreset = function()
        	inst.AnimState:ClearOverrideSymbol("beard")
		end
	    local beard_days = {4, 8, 16}
	    local beard_bits = {1, 3,  9}
		
       		inst.components.beard:AddCallback(beard_days[1], function()
        		inst.AnimState:OverrideSymbol("beard", "beard", "beard_short")
        		inst.components.beard.bits = beard_bits[1]
    		end)   
    		inst.components.beard:AddCallback(beard_days[2], function()
        		inst.AnimState:OverrideSymbol("beard", "beard", "beard_medium")
        		inst.components.beard.bits = beard_bits[2]
    		end) 
    		inst.components.beard:AddCallback(beard_days[3], function()
        		inst.AnimState:OverrideSymbol("beard", "beard", "beard_long")
        		inst.components.beard.bits = beard_bits[3]
    		end)
	elseif inst:HasTag("willow") then
		inst.components.health.fire_damage_scale = 0
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY*0.75)
		inst.specialitem = "lighter_player_3"
		inst:AddTag("girl")

		----willows ghost is bright
		light:SetIntensity(Lerp(0.4, 0.6, 1))
		light:SetRadius(Lerp(3, 5, 1))
		light:SetFalloff(.9)
		
	elseif inst:HasTag("wolfgang") then
		inst.components.health:SetMaxHealth(TUNING.WILSON_HEALTH*1.75)
		inst.components.combat.damagemultiplier = 1.35
		inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE*1.3)
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY*0.75)
		inst.components.hunger:SetMax(250)
		inst.components.health:StartRegen(0.2, 1)

		inst.ghostmaxinv = 10
		
	elseif inst:HasTag("wendy") then
		inst:AddTag("girl")

		inst.components.combat.damagemultiplier = 0.75
		inst:ListenForEvent("attacked", OnAttackedWendy)    
		inst.components.combat.onhitotherfn = function(inst, other, damage) OnWendyAttack(inst,other) end
	elseif inst:HasTag("wx78") then
		inst.components.eater.ignoresspoilage = true
	--	table.insert(inst.components.eater.foodprefs, "GEARS")
	--	table.insert(inst.components.eater.ablefoods, "GEARS")

		inst.components.eater:SetOnEatFn(oneatwx78)
	elseif inst:HasTag("wickerbottom") then
		inst:AddComponent("reader")
		inst:AddComponent("prototyper")
		inst:AddTag("prototyper")
        inst:AddTag("level1")
		inst.components.prototyper.trees = TUNING.PROTOTYPER_TREES.SCIENCEMACHINE
		inst.components.prototyper.on = true
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY*1.5)
		inst.specialitem = "bookplayer3"
		
		inst:AddTag("shy_ghost")
		inst:AddTag("girl")

	elseif inst:HasTag("waxwell") then 
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY*1.25)
		inst.components.health:SetMaxHealth(TUNING.WILSON_HEALTH/2)
		inst.specialitem = "nightsword_player3"

	
	elseif inst:HasTag("woodie") then
		inst:ListenForEvent("working", onworked)
		inst.specialitem = "axe_player_3"
	elseif inst:HasTag("wes") then
		inst.components.talker.special_speech = true
		inst.components.combat.damagemultiplier = 0.75
		inst.components.health:SetMaxHealth(TUNING.WILSON_HEALTH * .75 )
		inst.components.hunger:SetMax(TUNING.WILSON_HUNGER * .75 )
		inst.components.hunger:SetRate(TUNING.WILSON_HUNGER_RATE*1.25)
		inst.components.sanity:SetMax(TUNING.WILSON_SANITY*.75)
		inst.specialitem = "balloons_empty_player_3"
		
		inst:AddTag("shy_ghost")

	elseif inst:HasTag("wathgrithr") then
		anim:SetBuild("wathgrithr")
		inst:AddTag("girl")
	    inst.soundsname = "wathgrithr"
	    inst.talker_path_override = "dontstarve_DLC001/characters/"
		
		inst.components.health:SetMaxHealth(TUNING.WATHGRITHR_HEALTH)
		inst.components.hunger:SetMax(TUNING.WATHGRITHR_HUNGER)
		inst.components.sanity:SetMax(TUNING.WATHGRITHR_SANITY)
		inst.components.combat.damagemultiplier = TUNING.WATHGRITHR_DAMAGE_MULT
		inst.components.health:SetAbsorptionAmount(TUNING.WATHGRITHR_ABSORPTION)
		inst:ListenForEvent("entity_death", function(wrld, data) onwigfridkill(inst, data) end, GetWorld())
		if GetPlayer().prefab ~= "wathgrithr" then
			if version == "sw" then
				local spear_recipe = Recipe("spear_wathgrithr", {Ingredient("twigs", 2), Ingredient("flint", 2), Ingredient("goldnugget", 2)}, RECIPETABS.WAR, {SCIENCE = 0, MAGIC = 0, ANCIENT = 0}, RECIPE_GAME_TYPE.SHIPWRECKED)
				local helm_recipe = Recipe("wathgrithrhat", {Ingredient("goldnugget", 2), Ingredient("rocks", 2)}, RECIPETABS.WAR, {SCIENCE = 0, MAGIC = 0, ANCIENT = 0}, RECIPE_GAME_TYPE.SHIPWRECKED)
				spear_recipe.sortkey = 1
				helm_recipe.sortkey = 2
			elseif version == "rog" then
				local spear_recipe = Recipe("spear_wathgrithr", {Ingredient("twigs", 2), Ingredient("flint", 2), Ingredient("goldnugget", 2)}, RECIPETABS.WAR, {SCIENCE = 0, MAGIC = 0, ANCIENT = 0}, nil, nil, nil, nil, true)
				local helm_recipe = Recipe("wathgrithrhat", {Ingredient("goldnugget", 2), Ingredient("rocks", 2)}, RECIPETABS.WAR, {SCIENCE = 0, MAGIC = 0, ANCIENT = 0}, nil, nil, nil, nil, true)
				spear_recipe.sortkey = 1
				helm_recipe.sortkey = 2
			end
		end

		elseif inst:HasTag("webber") then
	    inst.soundsname = "webber"
	    inst.talker_path_override = "dontstarve_DLC001/characters/"
				   
	   -- inst:AddTag("spiderwhisperer")
	    --inst:AddTag("monster")
	    inst.components.locomotor.triggerscreep = false
		
	    inst.components.eater.monsterimmune = true
	
		inst.components.health:SetMaxHealth(TUNING.WEBBER_HEALTH)
		inst.components.hunger:SetMax(TUNING.WEBBER_HUNGER)
		inst.components.sanity:SetMax(TUNING.WEBBER_SANITY)
		
	    inst:AddComponent("beard")
    	inst.components.beard.onreset = function()
        	inst.AnimState:ClearOverrideSymbol("beard")
		end
		local beard_days = {3, 6, 9}
	    local beard_bits = {1, 3, 6}
       		inst.components.beard:AddCallback(beard_days[1], function()
        		inst.AnimState:OverrideSymbol("beard", "beard_silk", "beardsilk_short")
        		inst.components.beard.bits = beard_bits[1]
    		end)   
    		inst.components.beard:AddCallback(beard_days[2], function()
        		inst.AnimState:OverrideSymbol("beard", "beard_silk", "beardsilk_medium")
        		inst.components.beard.bits = beard_bits[2]
    		end) 
    		inst.components.beard:AddCallback(beard_days[3], function()
        		inst.AnimState:OverrideSymbol("beard", "beard_silk", "beardsilk_long")
        		inst.components.beard.bits = beard_bits[3]
    		end)
	
	
	elseif inst:HasTag("walani") then
		inst:AddTag("girl")
	    inst.components.health:SetMaxHealth(TUNING.WALANI_HEALTH)
	    inst.components.hunger:SetMax(TUNING.WALANI_HUNGER)
	    inst.components.sanity:SetMax(TUNING.WALANI_SANITY)

	    inst.soundsname = "walani"
	    inst.talker_path_override = "dontstarve_DLC002/characters/"

	    inst.components.moisture.baseDryingRate = 0.2
	    inst.components.sanity:AddRateModifier("walani", TUNING.WALANI_SANITY_RATE_MODIFIER)
	    inst.components.sanity.wetnessImmune = true 

		
	elseif inst:HasTag("warly") then
	    inst.components.hunger:SetMax(TUNING.WARLY_HUNGER)

	    inst.soundsname = "warly"
	    inst.talker_path_override = "dontstarve_DLC002/characters/"

	   --- inst.components.hunger:AddBurnRateModifier("warly", TUNING.WARLY_HUNGER_RATE_MODIFIER)
		
	elseif inst:HasTag("wilbur") then
	    inst.soundsname = "wilbur"
	    inst.talker_path_override = "dontstarve_DLC002/characters/"

	    inst:AddTag("wilbur")
	    inst:AddTag("monkey")

	    inst.components.health:SetMaxHealth(TUNING.WILBUR_HEALTH)
	    inst.components.hunger:SetMax(TUNING.WILBUR_HUNGER)
	    inst.components.sanity:SetMax(TUNING.WILBUR_SANITY)

	    ---inst.components.locomotor:AddSpeedModifier_Additive("WILBUR_WALK", TUNING.WILBUR_WALK_SPEED_PENALTY)

        inst:AddComponent("periodicspawner")
        inst.components.periodicspawner:SetPrefab("poop")
        inst.components.periodicspawner:SetRandomTimes(TUNING.TOTAL_DAY_TIME * 2, TUNING.SEG_TIME * 2)
        inst.components.periodicspawner:Start()
	    inst.components.eater.ignoresspoilage = true
	
	    inst.components.hunger.hungerrate = 0.17
        ---inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED - .5
	
	    inst.timeinmotion = 0

        inst:ListenForEvent("locomote", function()
		--[[
		inst.startpos = inst:GetPosition()
		inst.checktask = inst:DoTaskInTime(0, function()
		    inst.endpos = inst:GetPosition()
			if inst.startpos == inst.endpos then
			    inst.timeinmotion = 0
			end
			if inst.startpos ~= inst.endpos then
			    inst.timeinmotion = inst.timeinmotion + 1
			end
			if inst.timeinmotion == 0 and inst:HasTag("faster") then
        		    inst:AddTag("slowed")
        		    inst:RemoveTag("faster")
			    inst.components.hunger.hungerrate = 0.17
			    inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED - .5
			end
			if inst.timeinmotion == 62 and inst:HasTag("slowed") then
        		    inst:AddTag("faster")
        		    inst:RemoveTag("slowed")
			    inst.components.hunger.hungerrate = 0.28
			    inst.components.locomotor.runspeed = TUNING.WILSON_RUN_SPEED + 2
			end
		end)--]]
	end)
		
	elseif inst:HasTag("woodlegs") then
	inst.soundsname = "woodlegs"
	inst.talker_path_override = "dontstarve_DLC002/characters/"
	inst.footstep_path_override = "dontstarve_DLC002/movement/woodleg/"

	inst:AddTag("pirate")

	inst.components.sanity:SetMax(TUNING.WILLOW_SANITY)

	inst.AnimState:OverrideSymbol('beard', 'beard_woodlegs', 'beard_short')

end
	---------------------/////////////Customising end/////////////---------------------
	--//----------------------------------------------------------------//--

	
	

	inst:ListenForEvent("attacked", DodgeTrick)  
	
	

    inst:AddComponent("frostybreather")
    inst:AddComponent("playercontroller_3")
    inst:AddComponent("playeractionpicker_3")


	inst:AddComponent("grue")
	inst.components.grue:SetSounds("dontstarve/charlie/warn","dontstarve/charlie/attack")



    ------------------------------------------
    inst.entity:AddLabel()
    inst.Label:SetFontSize(25)
    inst.Label:SetFont(DEFAULTFONT)
    inst.Label:SetPos(0,3,0)
    inst.Label:SetColour(1, 1, 1)
    inst.Label:Enable(true)
    ------------------------------------------


    ------------------------------------------


	
	
	
	
	
    ------------------------------------------
    local minimap = inst.entity:AddMiniMapEntity()
	local newicon = newtagforclone
    minimap:SetIcon(newicon..".png")
    ------------------------------------------
   
    inst:SetBrain(brain)
    inst:AddComponent("lootdropper")

    inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader.onrefuse = OnRefuseItem

    inst.AnimState:Hide("ARM_carry") 
    inst.AnimState:Show("ARM_normal") 

    inst:AddComponent("wisecrackerclone")


------------//////--SANITY REBALANCE-//////--------------
    inst:DoPeriodicTask(8, function()
		if inst:HasTag("wendy") then
			inst.components.sanity:DoDelta(2,true)
		elseif inst:HasTag("waxwell") then
			inst.components.sanity:DoDelta(8,true)
		end
		
		if not (inst:HasTag("no_stats_transfer") or inst.components.health:IsDead()) then
			if inst.components.sanity:GetPercent() > 0.7 and inst.components.sanity:GetPercent()>GetPlayer().components.sanity:GetPercent() then
			
			inst.components.sanity:DoDelta(-7,true)
			GetPlayer().components.sanity:DoDelta(8,true)
			end
			if inst.components.sanity:GetPercent() < 0.5 and inst.components.sanity:GetPercent()<GetPlayer().components.sanity:GetPercent() then
			
			inst.components.sanity:DoDelta(7,true)
			GetPlayer().components.sanity:DoDelta(-6,true)
			end
		end
    end)
------------//////--MOISTURE REBALANCE-//////--------------

	inst:DoPeriodicTask(4, function()
			--inst.components.follower:AddLoyaltyTime(9999999)	
		if inst.components.moisture and inst.boatingwith and inst.boatingwith==1 then
			if GetPlayer().components.moisture:GetMoisturePercent()>0.25 and inst.components.moisture:GetMoisturePercent()<GetPlayer().components.moisture:GetMoisturePercent() and inst.components.moisture:GetMoisturePercent()<0.5 then
				inst.components.moisture:DoDelta(5)
				GetPlayer().components.moisture:DoDelta(-4)
			end
		end
    end)
------------//////--MOISTURE end REBALANCE-//////--------------

	
	
    inst:DoPeriodicTask(5, function()
		if whoisplayer.components.health:IsDead()==true and not whoisplayer:HasTag("alreadyresseructing") then
			whoisplayer.HUD:Show()
			whoisplayer:AddTag("alreadyresseructing")
			--inst.components.resurrector:Resurrect(whoisplayer)
			
			--inst:RemoveComponent("resurrector")

		end

			if whoisplayer:GetDistanceSqToInst(inst) > TUNING.MAXDIST then
			if (whoisplayer.components.driver and whoisplayer.components.driver.driving ==false)
				and (inst.components.driver and inst.components.driver.driving ==true) then
					inst.components.playercontroller_3:UnMountBoat(true)
				end
		
		
			if whoisplayer.components.health:IsDead()==false then
				inst.Transform:SetPosition(whoisplayer:GetPosition():Get())
			else
				whoisplayer.Transform:SetPosition(inst:GetPosition():Get())
			end
		end
		whoisplayer.components.p3stats_saver:RecordStats(inst," PERIODIC")
		whoisplayer.components.p3stats_saver:RecordStatsSeparate(inst," PERIODIC")

		
    end)


	
---	inst:AddComponent("dialog_des")
	inst.boatingwith = 0
	inst.passanger = 0
	
	
	inst.mountingboat = false
	inst.startupdatingboatwithplayer = false
	
	---  inst:ListenForEvent("attacked", OnAttacked)    
	inst:AddComponent("resurrector")
	inst.components.resurrector.makeactivefn = makeactive
	inst.components.resurrector.makeusedfn = makeused
	inst.components.resurrector.doresurrect = doresurrect
	
	inst:AddComponent("activatable")
	inst.components.resurrector:OnBuilt()
	inst.components.resurrector.active = true
	
	inst.components.activatable.inactive = nil



	inst:ListenForEvent("itemget", DropLastItem)  


	local StatusDisplays = require "widgets/statusdisplays"
	local Inventorybarcloneplayer = require "widgets/Inventorybarcloneplayer"

---	local crafttabscloneplayer = require "widgets/crafttabscloneplayer"

	require "prefabs/hud"
	local doer = GetPlayer()
			

-- --	inst:AddComponent("builder")
	-- --local valid_recipes = GetAllRecipes()
-- --	for k,v in pairs(valid_recipes) do
 -- --           if v.level == 0 then
			-- inst.components.builder:AddRecipe(v.name)
			-- end
	-- end

		
	
	if GetPlayer().components.driver then
		inst:SetStateGraph("SGcloneplayer")
	else 
		inst:SetStateGraph("SGcloneplayerROG")
	end
	
	inst.components.talker.colour = Vector3(inst.plclr[1], inst.plclr[2], inst.plclr[3])	



	
	inst:DoTaskInTime(0.25, function()
		inst.components.follower.maxfollowtime = 1
		if not inst:HasTag("isghost") then
			if doer.HUD and doer.HUD.controls then
				if inst.plnum==2 then
					if doer.HUD.controls.statusplayer2 then doer.HUD.controls.statusplayer2:Kill() end
					if doer.HUD.controls.Inventorybarcloneplayer2 then doer.HUD.controls.Inventorybarcloneplayer2:Kill() end
					doer.HUD.controls.Inventorybarcloneplayer2 = GetPlayer().HUD.controls.sidepanel:AddChild(Inventorybarcloneplayer(inst))
					doer.HUD.controls.Inventorybarcloneplayer2:SetPosition(-775+(TUNING.COOP_BADGE_POSITION2*TUNING.COOP_OFFEST_X),-15+(TUNING.COOP_BADGE_POSITION1*TUNING.COOP_OFFEST_Y),0)
					
					doer.HUD.controls.statusplayer2 = GetPlayer().HUD.controls.sidepanel:AddChild(StatusDisplays(inst))
					doer.HUD.controls.statusplayer2:SetPosition(-135,0,0)
					
				--	doer.HUD.controls.crafttabscloneplayer = GetPlayer().HUD.controls.sidepanel:AddChild(crafttabscloneplayer(inst))
				--	doer.HUD.controls.crafttabscloneplayer:SetPosition(-135,0,0)
					
				elseif inst.plnum==3 then
					if doer.HUD.controls.statusplayer3 then doer.HUD.controls.statusplayer3:Kill() end
					if doer.HUD.controls.Inventorybarcloneplayer3 then doer.HUD.controls.Inventorybarcloneplayer3:Kill() end
					doer.HUD.controls.Inventorybarcloneplayer3 = GetPlayer().HUD.controls.sidepanel:AddChild(Inventorybarcloneplayer(inst))
					doer.HUD.controls.Inventorybarcloneplayer3:SetPosition(25,-475,0)
					doer.HUD.controls.statusplayer3 = GetPlayer().HUD.controls.mapcontrols:AddChild(StatusDisplays(inst))
					doer.HUD.controls.statusplayer3:SetPosition(-75,100,0)
				end--
			end
			inst:DoTaskInTime(4, function()
				inst.components.playercontroller_3:GiveSpecialItems()
			end)
		end
		GetPlayer().components.playercontroller_spawner:CameraPrefab()

	end)
		

		return inst
	end
		
	print("common/summonclone"..version..forreturn)
    return Prefab("common/summonclone"..version..forreturn, fn, neededassets, prefabs) 
end
	
return MakePlayer3(2,"2","ds"),
MakePlayer3(3,"3", "ds"),
MakePlayer3(2,"2","rog"),
MakePlayer3(3,"3", "rog"),
MakePlayer3(2,"2","sw"),
MakePlayer3(3,"3", "sw")