local function 	dodamage(inst,damage)
		local x, y, z = inst.Transform:GetWorldPosition()
        local ents = TheSim:FindEntities(x, y, z, 4)
		if not damage then 
		damage = 100
		end
        for i, v in ipairs(ents) do
            if v:IsValid() and not v:IsInLimbo() and  not v:HasTag("player") and not v:HasTag("companion")  then
						if v.components.health ~= nil and
						not v.components.health:IsDead() and
						v.components.combat ~= nil and
						v.components.combat:CanBeAttacked() then
						v.components.combat:GetAttacked(inst, damage)
						end
            end
        end
end

AddStategraphState("wilson", 
    State{ 
        name = "combat_lunge_start",
        tags = { "aoe", "doing", "busy", "nointerrupt", "nomorph" },

        onenter = function(inst)
            inst.components.locomotor:Stop()
            inst.AnimState:PlayAnimation("lunge_pre")
                local buffaction = inst:GetBufferedAction()
                if buffaction ~= nil and buffaction.pos ~= nil then
                    inst:ForceFacePoint(buffaction.pos:Get())
                end
        end,

        timeline =
        {
            TimeEvent(4 * FRAMES, function(inst)
                inst.SoundEmitter:PlaySound("banhua/banhua/twirl", nil, nil, true)
            end),
        },

        events =
        {
            EventHandler("combat_lunge", function(inst, data)
                inst.sg:GoToState("combat_lunge", data)
            end),
            EventHandler("animover", function(inst)
               inst.AnimState:PlayAnimation("lunge_lag")
			   inst.sg:GoToState("combat_lunge")
            end),
        },
    }
)
AddStategraphState("wilson", --砍掘挖
    State{
        name = "combat_lunge",
        tags = { "aoe", "doing", "busy", "nopredict", "nomorph" },

        onenter = function(inst, data)
            if --data ~= nil and
               -- data.targetpos ~= nil and
                --data.weapon ~= nil and
                --data.weapon.components.aoeweapon_lunge ~= nil and
                inst.AnimState:IsCurrentAnimation("lunge_lag") then
                inst.AnimState:PlayAnimation("lunge_pst")
                inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon")
                local pos = inst:GetPosition()
				--inst.sg.statemem.targetpos = data.targetpos
                --if pos.x ~= data.targetpos.x or pos.z ~= data.targetpos.z then
                --    inst:ForceFacePoint(data.targetpos:Get())
               -- end
				-- local angle = (inst.Transform:GetRotation() + 90) * DEGREES  
				-- local step = .75
				-- local offset = 0 - steplocal pos = inst:GetPosition()
				local ents = {}
				ents = TheSim:FindEntities(pos.x,0, pos.z, 2)
				for k,v in ipairs(ents) do 
				 if v.components.workable ~= nil 
					and v.components.workable:IsActionValid(ACTIONS.CHOP)
					and v.components.workable.action == ACTIONS.CHOP
						then
							v.components.workable:WorkedBy(inst,2) 
							inst.components.sanity:DoDelta(-3)
							inst.components.sanity:DoDelta(5)
						return true
					end
				if v.components.workable ~= nil 
					and v.components.workable:IsActionValid(ACTIONS.MINE)
					and v.components.workable.action == ACTIONS.MINE
						then
							v.components.workable:WorkedBy(inst,2) 
							inst.components.sanity:DoDelta(-3)
							inst.components.sanity:DoDelta(5)
						return true
					end
				if v.components.workable ~= nil 
					and v.components.workable:IsActionValid(ACTIONS.DIG)
					and v.components.workable.action == ACTIONS.DIG
						then
							v.components.workable:WorkedBy(inst,1) 
							inst.components.sanity:DoDelta(-8)
							inst.components.sanity:DoDelta(5)
						return true
					end
				end
				
				
				-- dist = 12 * .75 + offset 
				-- x1 = pos.x + dist * math.sin(angle) 
				-- z1 = pos.z + dist * math.cos(angle)
                -- if data.weapon.components.aoeweapon_lunge:DoLunge(inst, pos, data.targetpos) then
                    inst.SoundEmitter:PlaySound("banhua/banhua/fireball")
					-- inst.Physics:SetMotorVel(math.sqrt(distsq(pos.x, pos.z, x1, z1)) / (13 * FRAMES), 0 ,0)
                    -- inst.components.bloomer:PushBloom("lunge", "shaders/anim.ksh", -2)  
                    -- inst.components.colouradder:PushColour("lunge", 1, 1, 0, 0) 
                    -- inst.sg.statemem.flash = 1
                    -- return
                -- end
            end
            --Failed
            --inst.sg:GoToState("idle", true)
			
        end,

        timeline =
        {
            -- TimeEvent(13 * FRAMES, function(inst)
                -- inst.components.bloomer:PopBloom("lunge")
                -- inst.Physics:Stop()
                -- inst.Physics:SetMotorVel(0, 0, 0)
                -- inst.Physics:Teleport(x1, 0, z1)
            -- end),
        },

        events =
        {
            EventHandler("animover", function(inst)
                if inst.AnimState:AnimDone() then
                    inst.sg:GoToState("idle")
                end
            end),
        },


    }
)


AddStategraphState("wilson",--攻击
    State{
        name = "combat_leap_start",
        tags = { "aoe", "doing", "busy", "nointerrupt", "nomorph" },

        onenter = function(inst)
            inst.components.locomotor:Stop()
            inst.AnimState:PlayAnimation("atk_leap_pre")
                local buffaction = inst:GetBufferedAction()
                if buffaction ~= nil and buffaction.pos ~= nil then
                    inst:ForceFacePoint(buffaction.pos:Get())
                end
        end,

        events =
        {
            EventHandler("combat_leap", function(inst, data)
                inst.sg.statemem.leap = true
                inst.sg:GoToState("combat_leap", data)
            end),
            EventHandler("animover", function(inst)
                if TheSim:IsKeyDown(KEY_H) then
                        inst.AnimState:PlayAnimation("atk_leap_lag")
						inst.sg:GoToState("combat_leap")
						inst.components.sanity:DoDelta(-8)
				else
						inst.sg:GoToState("idle")
                end
            end),
        },
    }
)
AddStategraphState("wilson",
    State{
        name = "combat_leap",
        tags = { "aoe", "doing", "busy", "nointerrupt", "nopredict", "nomorph" },

        onenter = function(inst, data)
                if 
                    inst.AnimState:IsCurrentAnimation("atk_leap_lag") then
                    inst.AnimState:PlayAnimation("atk_leap")
                    inst.SoundEmitter:PlaySound("banhua/banhua/deathpoof")
                    pos = inst:GetPosition()
					local qitiaofx = SpawnPrefab("hammer_mjolnir_cracklehit")
						if qitiaofx then
						qitiaofx.Transform:SetPosition(pos:Get())
						end
                    return
                end
            inst.sg:GoToState("idle", true)
        end,

        timeline =
        {
            TimeEvent(4 * FRAMES, function(inst)
				dodamage(inst,100)
            end),
            TimeEvent(10 * FRAMES, function(inst)
            end),
            TimeEvent(11 * FRAMES, function(inst)
            end),
            TimeEvent(12 * FRAMES, function(inst)
			inst.components.sanity:DoDelta(-5)
			local x,y,z=inst:GetPosition():Get()
                inst.Physics:Stop()
                inst.Physics:SetMotorVel(0, 0, 0)
               local jieshuafx = SpawnPrefab("hammer_mjolnir_cracklebase") 
				if jieshuafx then jieshuafx.Transform:SetPosition(x, 0, z) end
                local jieshubfx = SpawnPrefab("hammer_mjolnir_crackle") 
                if jieshubfx then jieshubfx.Transform:SetPosition(x, 0, z) end				
            end),
            TimeEvent(13 * FRAMES, function(inst)
                --ShakeAllCameras(CAMERASHAKE.VERTICAL, .7, .015, .8, inst, 20)

				dodamage(inst,150)
                --if inst.sg.statemem.weapon:IsValid() then
                --    inst.sg.statemem.weapon.components.aoeweapon_leap:DoLeap(inst, inst.sg.statemem.startingpos, inst.sg.statemem.targetpos)
                --end
            end),
            TimeEvent(25 * FRAMES, function(inst)

            end),
        },

        events =
        {
            EventHandler("animover", function(inst)
                    inst.sg:GoToState("idle")
            end),
        },

        onexit = function(inst)
        end,
    }
)

AddStategraphState("wilson",--吹箭
    State
    {
        name = "blowdart_special",
        tags = { "doing", "busy", "nointerrupt", "nomorph" },

        onenter = function(inst)
            inst.components.locomotor:Stop()
            inst.AnimState:PlayAnimation("dart_pre")
			inst.components.sanity:DoDelta(-5)
                inst.sg.statemem.long = true
                inst.AnimState:PushAnimation("dart_long", false)
                inst.sg:SetTimeout(29 * FRAMES)
				inst:DoTaskInTime(10*FRAMES,function(inst)
					if not TheInput:IsKeyDown(KEY_K) then 
						inst.sg:GoToState("idle")
					else 
						inst.components.sanity:DoDelta(-10)
					end
				end)
        end,


        timeline =
        {
            TimeEvent(13 * FRAMES, function(inst)
                inst.SoundEmitter:PlaySound("dontstarve/wilson/blowdart_shoot")
            end),
            TimeEvent(14 * FRAMES, function(inst)

            end),
            TimeEvent(23 * FRAMES, function(inst)
				local mpos = TheInput:GetWorldPosition()
				local ipos = inst:GetPosition()
				local tile = GetWorld().Map:GetTileAtPoint(mpos:Get())
				if 
				tile and tile ~= -1 and tile ~= 1
				then 
				inst.Transform:SetPosition(mpos:Get())
				end
				dodamage(inst,100)
				
                inst.SoundEmitter:PlaySound("dontstarve/wilson/blowdart_shoot", nil, .4)
 
            end),
			TimeEvent(26 * FRAMES, function(inst)
				dodamage(inst,75)
            end),
        },

        ontimeout = function(inst)
            inst.sg:GoToState("idle", true)
        end,

        events =
        {
            EventHandler("animqueueover", function(inst)
                if inst.AnimState:AnimDone() then
                    inst.sg:GoToState("attack")
                end
            end),
        },
    }
)

