local STRINGS = GLOBAL.STRINGS
local require = GLOBAL.require
local Ingredient = GLOBAL.Ingredient
local RECIPETABS = GLOBAL.RECIPETABS
GetPlayer = GLOBAL.GetPlayer
GetWorld = GLOBAL.GetWorld
STRINGS = GLOBAL.STRINGS
RECIPETABS = GLOBAL.RECIPETABS
Recipe = GLOBAL.Recipe
Ingredient = GLOBAL.Ingredient
TECH = GLOBAL.TECH
Action = GLOBAL.Action
Vector3 = GLOBAL.Vector3
local Vector3 = GLOBAL.Vector3
require "class"
local FRAMES = GLOBAL.FRAMES
FRAMES = GLOBAL.FRAMES
local TimeEvent = GLOBAL.TimeEvent
TimeEvent = GLOBAL.TimeEvent
local EventHandler = GLOBAL.EventHandler
EventHandler = GLOBAL.EventHandler
localEQUIPSLOTS = GLOBAL.EQUIPSLOTS
EQUIPSLOTS = GLOBAL.EQUIPSLOTS
local SpawnPrefab = GLOBAL.SpawnPrefab
SpawnPrefab = GLOBAL.SpawnPrefab

STRINGS.NAMES.HAT_WARFARINHOOD = "Ç± ÐÐ ·ç Ã±"
STRINGS.RECIPE_DESC.HAT_WARFARINHOOD = "ÈÃ ÄÇ Ð© ß´ ß´ Ôû Ôû µÄ ¶¯ Îï ÍË É¢."
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINKNIFE = "nil"

STRINGS.NAMES.WARFARINKNIFE = "Í¶ ÖÀ Ø° Ê×"
STRINGS.RECIPE_DESC.WARFARINKNIFE = "·æ Àû ÇÒ ÄÜ Ôì ³É ÉË º¦"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINKNIFE = "nil"

STRINGS.NAMES.WARFARINBOW = "Ç¿ ¹­"
STRINGS.RECIPE_DESC.WARFARINBOW = "±ø ²» Ñá Õ© !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINBOW = "nil"

STRINGS.NAMES.WARFARINBOWARM = "Ç¿ ¹­ ±Û"
STRINGS.RECIPE_DESC.WARFARINBOWARM = "Ö» ÓÐ Ò» ¸ö ×ö ²» ÁË Ê² Ã´"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINBOWARM = "nil"

STRINGS.NAMES.WARFARINARROW_NORMAL = "¼ý Ê¸"
STRINGS.RECIPE_DESC.WARFARINARROW_NORMAL = "Óð Ã« ·¢ »Ó ¼ý Í· µÄ Á¦ Á¿ !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINARROW_NORMAL = "nil"

STRINGS.NAMES.WARFARINARROW_WATER = "Ë® ¼ý"
STRINGS.RECIPE_DESC.WARFARINARROW_WATER = "Ï¨ Ãð ÄÇ Ð© ·³ ÈË µÄ ÁÁ ¹â !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINARROW_WATER = "nil"

STRINGS.NAMES.WARFARINARROW_ROCK = "Ê¯ ¼ý"
STRINGS.RECIPE_DESC.WARFARINARROW_ROCK = "Òò Îª ¹Ö ÊÞ Ï² »¶ ´ó Éù µÄ »Ø Ïì !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINARROW_ROCK = "nil"

STRINGS.NAMES.WARFARINARROW_FIRE = "±¬ ÆÆ ¼ý"
STRINGS.RECIPE_DESC.WARFARINARROW_FIRE = "°Ñ Äã µÄ µÐ ÈË±ä ³É ¶¨ Ê± Õ¨ µ¯ !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINARROW_FIRE = "nil"

STRINGS.NAMES.WARFARINKNIFEEX = "±¬ ÆÆ Í¶ ÖÀ Ø° Ê×"
STRINGS.RECIPE_DESC.WARFARINKNIFEEX = "¼â Èñ ·æ Àû . È» ºó ±¬ Õ¨ ."
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINKNIFEEX = "nil"

STRINGS.NAMES.WARFARINARROW_SPIKE = "¶¤ ´Ì ¼ý"
STRINGS.RECIPE_DESC.WARFARINARROW_SPIKE = "ÈÎ ºÎ Çé ¿ö ¶¼ ÄÜ Ê¹ ÓÃ !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINARROW_SPIKE = "nil"

STRINGS.NAMES.WARFARINDAGGER = "Ø° Ê×  "
STRINGS.RECIPE_DESC.WARFARINDAGGER = "ÏÖ ÔÚ µÄ Äã ºÜ ±© Á¦ !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINDAGGER = "nil"

STRINGS.NAMES.WARFARINDAGGER_GOLD = "»Æ ½ð Ø° Ê×"
STRINGS.RECIPE_DESC.WARFARINDAGGER_GOLD = "ÉÁ Ò« , ·æ Àû , ¹» ÐÍ !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINDAGGER_GOLD = "nil"

STRINGS.NAMES.WARFARINDAGGER_MARBLE = "´ó Àí Ê¯ Ø° Ê×"
STRINGS.RECIPE_DESC.WARFARINDAGGER_MARBLE = "Ìê µ¶ ´ó Ê¦ !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINDAGGER_MARBLE = "nil"

STRINGS.NAMES.WARFARINDAGGER_NIGHT = "Ò¹ Ìì Ê¹ Ø° Ê×"
STRINGS.RECIPE_DESC.WARFARINDAGGER_NIGHT = "Å· Ò® !"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.WARFARINDAGGER_NIGHT = "nil"

PrefabFiles = {
	"warfarin",
	"warfarinknife",
	"warfarinarrows",
	"warfarinbowstring",
	"warfarinbow",
	"warfarinbowarm",
	"warfarinarroweffects",
	"hat_warfarinhood",
	"warfarindagger",
	"hat_warfarinmask",
}

Assets = {
    Asset( "IMAGE", "images/saveslot_portraits/warfarin.tex" ),
    Asset( "ATLAS", "images/saveslot_portraits/warfarin.xml" ),

    Asset( "IMAGE", "images/selectscreen_portraits/warfarin.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/warfarin.xml" ),

    Asset( "IMAGE", "images/selectscreen_portraits/warfarin_silho.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/warfarin_silho.xml" ),

    Asset( "IMAGE", "bigportraits/warfarin.tex" ),
    Asset( "ATLAS", "bigportraits/warfarin.xml" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinknife.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinknife.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinbow.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinbow.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinbowarm.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinbowarm.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinknifeex.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinknifeex.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinarrow_normal.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinarrow_normal.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinarrow_rock.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinarrow_rock.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinarrow_water.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinarrow_water.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinarrow_fire.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinarrow_fire.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarinarrow_spike.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarinarrow_spike.tex" ),

	Asset( "ATLAS", "images/inventoryimages/hat_warfarinhood.xml" ),
	Asset( "IMAGE", "images/inventoryimages/hat_warfarinhood.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarindagger.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarindagger.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarindagger_gold.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarindagger_gold.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarindagger_marble.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarindagger_marble.tex" ),

	Asset( "ATLAS", "images/inventoryimages/warfarindagger_night.xml" ),
	Asset( "IMAGE", "images/inventoryimages/warfarindagger_night.tex" ),


	Asset("IMAGE", "images/warfarin.tex"),
	Asset("ATLAS", "images/warfarin.xml"),

    Asset("ATLAS", "tabs/warfarintab.xml"),
        Asset("IMAGE", "tabs/warfarintab.tex"),

}

AddMinimapAtlas("images/warfarin.xml")

STRINGS.TABS.WARFARIN = "µÁ Ôô"
GLOBAL.RECIPETABS['WARFARIN'] = {str = "WARFARIN", sort=998, icon = "warfarintab.tex", icon_atlas = "tabs/warfarintab.xml"}

GetPlayer = GLOBAL.GetPlayer

AddPrefabPostInit("pighouse", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("cover")
	end
end)

AddPrefabPostInit("grass", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("sneak")
	end
end)

AddPrefabPostInit("sapling", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("sneak")
	end
end)

AddPrefabPostInit("marsh_bush", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("sneak")
	end
end)

AddPrefabPostInit("berrybush", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("sneak")
	end
end)

AddPrefabPostInit("berrybush2", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("sneak")
	end
end)

AddPrefabPostInit("reeds", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("sneak")
	end
end)

AddPrefabPostInit("wall_hay", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("haywall")
	end
end)

AddPrefabPostInit("wall_wood", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("woodwall")
	end
end)

AddPrefabPostInit("wall_stone", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("stonewall")
	end
end)

AddPrefabPostInit("wall_ruins", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("ruinswall")
	end
end)

AddPrefabPostInit("rock1", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("cover")
	end
end)

AddPrefabPostInit("rock2", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("cover")
	end
end)

AddPrefabPostInit("rock_flintless", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("cover")
	end
end)

AddPrefabPostInit("rock_flintless_med", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("cover")
	end
end)

AddPrefabPostInit("rock_flintless_low", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("cover")
	end
end)

AddPrefabPostInit("bunnyman", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("wallerhit")
	end
end)

AddPrefabPostInit("pigman", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("wallerhit")
local grasschance = 0.8
local twigchance = 0.8
local poopchance = 0.2
local robochance = 0.04
local cupchance = 0.1
local goldchance = 0.1
local findchance = 0.1
	inst:ListenForEvent("dusktime", function(it, data) 
        inst:AddComponent("pickable")
    inst.components.pickable.picksound = "dontstarve/wilson/harvest_sticks"
    inst.components.pickable:SetUp("cutgrass", 10)
    inst.components.pickable.quickpick = true
		inst.components.pickable.onpickedfn = function(inst, picker)
   if math.random() < findchance then
	inst.components.combat:SuggestTarget(picker)
			end
	        inst:RemoveComponent("pickable")	
		end
   if math.random() < grasschance then
    inst.components.pickable:SetUp("cutgrass", 10)
	end
   if math.random() < twigchance then
    inst.components.pickable:SetUp("twigs", 10)
	end
   if math.random() < poopchance then
    inst.components.pickable:SetUp("poop", 10)
	end
   if math.random() < robochance then
    inst.components.pickable:SetUp("trinket_11", 10)
	end
   if math.random() < cupchance then
    inst.components.pickable:SetUp("trinket_7", 10)
	end
   if math.random() < goldchance then
    inst.components.pickable:SetUp("goldnugget", 10)
	end
	end, GetWorld())

	inst:ListenForEvent("daytime", function(it, data) 
	if inst.components.pickable then
	        inst:RemoveComponent("pickable")
	end
	end, GetWorld())
	end
end)

AddPrefabPostInit("nightlight", function(inst)
	if GetPlayer().prefab == "warfarin" then 
    inst:AddTag("fire")
	end
end)

AddPrefabPostInit("minerhat", function(inst)
	if GetPlayer().prefab == "warfarin" then 

	inst:DoPeriodicTask(1/100, function()
	if inst.components.equippable.isequipped then
    inst:AddTag("fire")
	else
	    inst:RemoveTag("fire")
	end
end) 
	end
end)

AddPrefabPostInit("lantern", function(inst)
	if GetPlayer().prefab == "warfarin" then 

	inst:DoPeriodicTask(1/100, function()
	if inst.components.equippable.isequipped or inst.components.machine.ison then
    inst:AddTag("fire")
	else
	    inst:RemoveTag("fire")
	end
end) 

	end
end)

AddStategraphPostInit("wilson", function(sg)
    local old_doattack = sg.events.doattack.fn
    sg.events.doattack.fn = function(inst, data)
        if not inst.components.health:IsDead() and not inst.sg:HasStateTag("attack") then
            local weapon = inst.components.combat and inst.components.combat:GetWeapon()
            if weapon and weapon:HasTag("blowdart") then
                inst.sg:GoToState("blowdart")
            elseif weapon and weapon:HasTag("thrown") then
                inst.sg:GoToState("throw")
            elseif weapon and weapon:HasTag("bow") then
                inst.sg:GoToState("bow")
            elseif weapon and weapon:HasTag("lildagger") then
                inst.sg:GoToState("thiefattack")
            else
                inst.sg:GoToState("attack")
                return old_doattack(inst, data)
            end
        end
    end
end)
 
AddStategraphState("wilson", GLOBAL.State {
        name = "thiefattack",
        tags = {"attack", "notalking", "abouttoattack", "busy"},
        
        onenter = function(inst)
			--print(debugstack())
            inst.sg.statemem.target = inst.components.combat.target
            inst.components.combat:StartAttack()
            inst.components.locomotor:Stop()
            local weapon = inst.components.combat:GetWeapon()
        inst.SoundEmitter:PlaySound("dontstarve/creatures/monkey/throw")
	if (inst:HasTag("shadow")) and inst.components.combat.target and not (inst.components.combat.target:HasTag("stabbed")) or (inst:HasTag("notarget")) and inst.components.combat.target and not (inst.components.combat.target:HasTag("stabbed")) then
                inst.AnimState:PlayAnimation("fish_nocatch")
				inst.sg.statemem.backstab = true
	else
                inst.AnimState:PlayAnimation("atk")
		end
		if weapon:HasTag("shadow") then
                    inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_nightsword")
		end
        end,
        
        timeline=
        {
            TimeEvent(5*FRAMES, function(inst) inst.components.combat:DoAttack(inst.sg.statemem.target) inst.sg:RemoveStateTag("abouttoattack") end),
            TimeEvent(5*FRAMES, function(inst)
				if inst.sg.statemem.backstab and not (inst.components.combat.target:HasTag("stabbed")) then
			inst.components.combat:DoAttack(inst.sg.statemem.target)
    local target = inst.components.combat.target
    local blood = SpawnPrefab("warfarin_bleed")
	    local follower = blood.entity:AddFollower()
	    follower:FollowSymbol(target.GUID, target.components.combat.hiteffectsymbol, 0, 0, 0 )
        blood:FacePoint(inst.Transform:GetWorldPosition())
	blood.components.combat:SetTarget(target)
	    inst.components.combat.target:AddTag("stabbed")
				end
            end),
            TimeEvent(8*FRAMES, function(inst) 
				inst.sg:RemoveStateTag("busy")
			end),				
            TimeEvent(9*FRAMES, function(inst)
					inst.sg:RemoveStateTag("attack")
            end),
            
            
        },
        
        events=
        {
            EventHandler("animover", function(inst)
                inst.sg:GoToState("idle")
            end ),
        },
})   

AddStategraphState("wilson", GLOBAL.State {
        name = "bow_idle",
        tags = {"idle", "canrotate"},
        onenter = function(inst, pushanim)
            
            inst.components.locomotor:Stop()
            
                inst.AnimState:PlayAnimation("fishing_idle", true)
            
            inst.sg:SetTimeout(math.random()*4+2)
        end,
        
        ontimeout= function(inst)
            inst.sg:GoToState("funnyidle")
        end,
})

AddStategraphState("wilson", GLOBAL.State {
    name = "bow",
        tags = {"attack", "notalking", "abouttoattack"},
        
        onenter = function(inst)
            inst.sg.statemem.target = inst.components.combat.target
            inst.components.combat:StartAttack()
            inst.components.locomotor:Stop()
            inst.AnimState:PlayAnimation("bite_light_pre")
            inst.AnimState:PushAnimation("bite_light_loop")
inst.SoundEmitter:PlaySound("dontstarve/common/fishingpole_strain")
            if inst.components.combat.target then
                if inst.components.combat.target and inst.components.combat.target:IsValid() then
                    inst:FacePoint(inst.components.combat.target.Transform:GetWorldPosition())
                end
            end
            
        end,

        timeline=
        {
            TimeEvent(6*FRAMES, function(inst) inst.SoundEmitter:PlaySound("dontstarve/common/fishingpole_cast") end),
            TimeEvent(5*FRAMES, function(inst) inst.AnimState:PlayAnimation("fishing_idle") end),
            TimeEvent(8*FRAMES, function(inst) inst.AnimState:PlayAnimation("idle_inaction_sanity") end),
            TimeEvent(11*FRAMES, function(inst) inst.components.combat:DoAttack(inst.sg.statemem.target) inst.sg:RemoveStateTag("abouttoattack") end),
            TimeEvent(14*FRAMES, function(inst) inst.AnimState:PlayAnimation("item_out") inst.SoundEmitter:PlaySound("dontstarve/wilson/equip_item") end),
            TimeEvent(15*FRAMES, function(inst) inst.sg:RemoveStateTag("attack") end),
            TimeEvent(15*FRAMES, function(inst) 
    if inst.components.inventory:Has("warfarinarrow_normal", 1) then
		        inst.components.inventory:Equip(warfarinarrow_normal)
		    end
	end),
        },

        events=
        {
            EventHandler("animqueueover", function(inst)
            local weapon = inst.components.combat and inst.components.combat:GetWeapon()
            if weapon and weapon:HasTag("bow") then
                inst.sg:GoToState("bow_idle")
		else
                inst.sg:GoToState("idle")
		end
            end),
        },
})


table.insert(GLOBAL.CHARACTER_GENDERS.ROBOT, "warfarin")

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


AddModCharacter("warfarin")