local assets=
{
	Asset("ANIM", "anim/armor_mushaa.zip"),
  Asset("ATLAS", "images/inventoryimages/armor_mushaa.xml"),
  Asset("IMAGE", "images/inventoryimages/armor_mushaa.tex"),
	Asset("ANIM", "anim/ui_mushab_2x3.zip")
}

local function levelexp(inst,data)

	local max_exp = 1110
	local exp = math.min(inst.level, max_exp)

if inst.level <= 1000 then
inst.components.talker:Say("-[Musha Armor]-\n[Grow Points]".. (inst.level))
inst:RemoveTag("levelup")
end
if inst.level >=0 and inst.level <10 then
inst.components.talker:Say("-[Musha Armor] \nNext:[10]\n[Grow Points]".. (inst.level))
inst:AddTag("level1")
elseif inst.level >=10 and inst.level <30 then
inst.components.talker:Say("-[Musha Armor] \nNext:[30]\n[Grow Points]".. (inst.level))
inst:AddTag("level2")
inst:RemoveTag("level1")
elseif inst.level >=30 and inst.level <60 then
inst.components.talker:Say("-[Musha Armor] \nNext:[60]\n[Grow Points]".. (inst.level))
inst:AddTag("level3")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
elseif inst.level >=60 and inst.level <100 then
inst.components.talker:Say("-[Musha Armor] \nNext:[100]\n[Grow Points]".. (inst.level))
inst:AddTag("level4")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
elseif inst.level >=100 and inst.level <150 then
inst.components.talker:Say("-[Musha Armor] \nNext:[150]\n[Grow Points]".. (inst.level))
inst:AddTag("level5")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
elseif inst.level >=150 and inst.level <200 then
inst.components.talker:Say("-[Musha Armor] \nNext:[200]\n[Grow Points]".. (inst.level))
inst:AddTag("level6")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
elseif inst.level >=200 and inst.level <250 then
inst.components.talker:Say("-[Musha Armor] \nNext:[250]\n[Grow Points]".. (inst.level))
inst:AddTag("level7")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
elseif inst.level >=250 and inst.level <300 then
inst.components.talker:Say("-[Musha Armor] \nNext:[300]\n[Grow Points]".. (inst.level))
inst:AddTag("level8")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
elseif inst.level >=300 and inst.level <350 then
inst.components.talker:Say("-[Musha Armor] \nNext:[350]\n[Grow Points]".. (inst.level))
inst:AddTag("level9")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
elseif inst.level >=350 and inst.level <450 then
inst.components.talker:Say("-[Musha Armor] \nNext:[450]\n[Grow Points]".. (inst.level))
inst:AddTag("level10")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")
elseif inst.level >=450 and inst.level <600 then
inst.components.talker:Say("-[Musha Armor] \nNext:[600]\n[Grow Points]".. (inst.level))
inst:AddTag("level11")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")
inst:RemoveTag("level10")
elseif inst.level >=600 and inst.level <700 then
inst.components.talker:Say("-[Musha Armor] \nNext:[700]\n[Grow Points]".. (inst.level))
inst:AddTag("level12")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")
inst:RemoveTag("level10")
inst:RemoveTag("level11")
elseif inst.level >=700 and inst.level <800 then
inst.components.talker:Say("-[Musha Armor] \nNext:[800]\n[Grow Points]".. (inst.level))
inst:AddTag("level13")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")
inst:RemoveTag("level10")
inst:RemoveTag("level11")
inst:RemoveTag("level12")
elseif inst.level >=800 and inst.level <900 then
inst.components.talker:Say("-[Musha Armor] \nNext:[1000]\n[Grow Points]".. (inst.level))
inst:AddTag("level14")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")
inst:RemoveTag("level10")
inst:RemoveTag("level11")
inst:RemoveTag("level12")
inst:RemoveTag("level13")
elseif inst.level >=1000 then
inst.components.talker:Say("-[Musha Armor] \n[MAX]")
inst:AddTag("level15")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")
inst:RemoveTag("level10")
inst:RemoveTag("level11")
inst:RemoveTag("level12")
inst:RemoveTag("level13")
inst:RemoveTag("level14")
end
end

local function onpreload(inst, data)
	if data then
		if data.level then
		inst.level = data.level
			levelexp(inst)
end
	end
		end
local function onsave(inst, data)
	data.level = inst.level
	data.charge_time = inst.charge_time
end


local function OnDurability(inst, data)
inst:AddTag("broken")
inst.components.fueled:StopConsuming()        
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0)
	inst.components.talker:Say("-[Broken MushaArmor]\nArmor (0)\nDurability (0)")
end

local function UpgradeArmor(inst)
if inst:HasTag("broken") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0)
	inst.components.talker:Say("-[Broken MushaArmor]\nArmor (0)\nDurability (0)")

   end
if not inst:HasTag("broken") then
        if inst:HasTag("level1") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.61)
	inst.components.talker:Say("-[Musha Armor (LV 1)]\nArmor (61)")
        elseif inst:HasTag("level2") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.62)
	inst.components.talker:Say("-[Musha Armor (LV 2)]\nArmor (62)")
        elseif inst:HasTag("level3") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.63)
	inst.components.talker:Say("-[Musha Armor (LV 3)]\nArmor (63)")
        elseif inst:HasTag("level4") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.64)
	inst.components.talker:Say("-[Musha Armor (LV 4)]\nArmor (64)")
        elseif inst:HasTag("level5") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.65)
	inst.components.talker:Say("-[Musha Armor (LV 5)]\nArmor (65)")
        elseif inst:HasTag("level6") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.66)
	inst.components.talker:Say("-[Musha Armor (LV 6)]\nArmor (66)")
        elseif inst:HasTag("level7") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.67)
	inst.components.talker:Say("-[Musha Armor (LV 7)]\nArmor (67)")
        elseif inst:HasTag("level8") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.68)
	inst.components.talker:Say("-[Musha Armor (LV 8)]\nArmor (68)")
        elseif inst:HasTag("level9") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.69)
	inst.components.talker:Say("-[Musha Armor (LV 9)]\nArmor (69)")
        elseif inst:HasTag("level10") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.7)
	inst.components.talker:Say("-[Musha Armor (LV 10)]\nArmor (70)")
        elseif inst:HasTag("level11") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.71)
	inst.components.talker:Say("-[Musha Armor (LV 11)]\nArmor (71)")
        elseif inst:HasTag("level12") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.72)
	inst.components.talker:Say("-[Musha Armor (LV 12)]\nArmor (72)")
        elseif inst:HasTag("level13") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.73)
	inst.components.talker:Say("-[Musha Armor (LV 13)]\nArmor (73)")
        elseif inst:HasTag("level14") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.74)
	inst.components.talker:Say("-[Musha Armor (LV 14)]\nArmor (74)")
        elseif inst:HasTag("level15") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.75)
	inst.components.talker:Say("-[Musha Armor (LV 15)]\nArmor (75)")
end
end
end

local function OnLoad(inst, data)
    UpgradeArmor(inst)
end

--------
-------- --------
local function TakeItem(inst, item, data)
local expchance0 = 1
local expchance1 = 0.3
local expchance2 = 0.2
local expchance3 = 0.12
	inst.components.fueled:DoDelta(5000000)
	SpawnPrefab("splash").Transform:SetPosition(inst:GetPosition():Get())
inst:RemoveTag ("broken")     
UpgradeArmor(inst)
   if math.random() < expchance1 and inst.level <= 4005 then
	inst.level = inst.level + 2
	levelexp(inst)
	inst.components.talker:Say("Lucky Points ! +(2)\n[Grow Points]".. (inst.level))
    elseif  math.random() < expchance2 and inst.level <= 4005 then
	inst.level = inst.level + 5
	levelexp(inst)
	inst.components.talker:Say("Lucky Points ! +(5)\n[Grow Points]".. (inst.level))
	elseif  math.random() < expchance3 and inst.level <= 4005 then
	inst.level = inst.level + 8
	levelexp(inst)
	inst.components.talker:Say("Lucky Points ! +(8)\n[Grow Points]".. (inst.level))
	elseif  math.random() < expchance0 and inst.level <= 4005 then
	inst.level = inst.level + 1
	levelexp(inst)
    end
end
--------------
local function close(inst)
inst.components.resurrector.active = true
end

local function far(inst)
inst.components.resurrector.active = false
end

local function makeused4(inst)
	inst.AnimState:PlayAnimation("debris")
end

local function doresurrect4(inst, dude)
    dude.entity:AddLight()
	dude.Light:SetRadius(3)
    dude.Light:SetFalloff(10)
    dude.Light:SetIntensity(.5)
    dude.Light:SetColour(15/255,165/255,165/255)
	dude.Light:Enable(true)

	inst:AddTag("busy")	
	inst.persists = false
    if inst.Physics then
		RemovePhysicsColliders(inst)
    end
    --dude:Hide()
    if dude.HUD then
        dude.HUD:Hide()
    end
    if dude.components.playercontroller then
        dude.components.playercontroller:Enable(false)
    end

    inst:RemoveComponent("inspectable")
    dude:ClearBufferedAction()
------
    scheduler:ExecuteInTime(2, function()
        dude:Show()
       -- inst:Hide()

inst.AnimState:PlayAnimation("debris")

-------
        dude.sg:GoToState("amulet_rebirth")
        dude:DoTaskInTime(4, function() 

            if dude.HUD then
                dude.HUD:Show()
            end
            if dude.components.health then
				dude.components.health:RecalculatePenalty()
                dude.components.health:Respawn(100)
                dude.components.health:SetInvincible(true)
            end
          if dude.components.sanity then
			    dude.components.sanity:SetPercent(.5)
            end
          if dude.components.hunger then
                dude.components.hunger:SetPercent(.5)
            end
        if dude.components.playercontroller then
                dude.components.playercontroller:Enable(true)
            end
            inst:RemoveTag("busy")
            dude.components.health:SetInvincible(false)
	dude.Light:Enable(false)

 end)
	inst:Show()

 end)
	if inst.components.container then 
	inst.components.container:DropEverything() 
    end
	local dark2 = SpawnPrefab("statue_transition_2")
	local pos = Vector3(inst.Transform:GetWorldPosition())
dark2.Transform:SetPosition(pos:Get())
	local gdust = SpawnPrefab("glowdust")
gdust.Transform:SetPosition(pos:Get())

	inst:Remove()
            end
----------------

local function OnBlocked(owner) 
    owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour") 

end

local function onequip(inst, owner) 
   inst:AddTag("talkf")
    UpgradeArmor(inst)
inst.components.fueled:StartConsuming()        

    owner.AnimState:OverrideSymbol("swap_body", "armor_mushaa", "swap_body")
    owner.components.inventory:SetOverflow(inst)
    inst.components.container:Open(owner)

    inst.expfn = function(attacked, data)
local expchance = 0.5
local damagedur1 = 0.2
local damagedur2 = 0.5
local damagedur3 = 0.7
local damagedur4 = 1

if data and data.attacker and math.random() < damagedur1 then
inst.components.fueled:DoDelta(-100000)
elseif data and data.attacker and math.random() < damagedur2 then
inst.components.fueled:DoDelta(-50000)
elseif data and data.attacker and math.random() < damagedur3 then
inst.components.fueled:DoDelta(-25000)
elseif data and data.attacker and math.random() < damagedur4 then
inst.components.fueled:DoDelta(-50000)

end
if data and data.attacker and math.random() < expchance and inst.level < 4010 then
inst.components.fueled:DoDelta(-25000)
	inst.level = inst.level + 1
			levelexp(inst)

		end 
	    end

    inst:ListenForEvent("attacked", inst.expfn, owner)
    inst:ListenForEvent("blocked", OnBlocked, owner)
end

local function onunequip(inst, owner) 
   inst:RemoveTag("talkf")
    UpgradeArmor(inst)
inst.components.fueled:StopConsuming()        
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
    owner.components.inventory:SetOverflow(nil)
    inst.components.container:Close(owner)

    inst:RemoveEventCallback("attacked", inst.expfn, owner)
    inst:RemoveEventCallback("blocked", OnBlocked, owner)
end
----------------

local function OnOpen(inst)
    inst.SoundEmitter:PlaySound("dontstarve/wilson/lighter_on")
end 

local function OnClose(inst) 
    inst.SoundEmitter:PlaySound("dontstarve/wilson/lighter_on")
end 


local slotpos = {}

for y = 0, 2 do
	table.insert(slotpos, Vector3(-162, -y*75 + 75 ,0))
	table.insert(slotpos, Vector3(-162 +75, -y*75 + 75 ,0))
end


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

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

local function fn(Sim)
	local inst = CreateEntity()
    	inst.entity:AddSoundEmitter()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("armor_wood")
    inst.AnimState:SetBuild("armor_mushaa")
    inst.AnimState:PlayAnimation("anim")
    
    inst:AddTag("metal")
    
    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/marblearmour"
	inst.components.inventoryitem.atlasname = "images/inventoryimages/armor_mushaa.xml"
   
    inst:AddComponent("armor")
   -- inst.components.armor:InitCondition(999999999999999999999999999999999999999999999999999999999999999, 0.7 )

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BODY
    
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )

    inst:AddComponent("container")
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.onopenfn = OnOpen
    inst.components.container.onclosefn = OnClose
    
    inst.components.container.widgetslotpos = slotpos
    inst.components.container.widgetanimbank = "ui_backpack_2x4"
    inst.components.container.widgetanimbuild = "ui_mushab_2x3"

	--[[inst.components.container.widgetpos = Vector3(-145,-180,0)
    inst.components.container.widgetpos = Vector3(-140,-50,0)]]

    inst.components.container.side_widget = true

    inst.components.container.widgetpos = Vector3(-5,-70,0)
    inst.components.container.type = "pack"

 -------------------------   
---resurrect
        inst:AddComponent("playerprox")
    inst:AddComponent("resurrector")
   -- inst.components.resurrector.active = true
	inst.components.resurrector.doresurrect = doresurrect4
	inst.components.resurrector.makeusedfn = makeused4
    inst.components.playerprox:SetDist(10, 11)
    inst.components.playerprox:SetOnPlayerNear(close)
    inst.components.playerprox:SetOnPlayerFar(far)

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

inst:AddTag("level1")
    inst.OnLoad = OnLoad

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 19
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
    inst.components.talker.offset = Vector3(0,100,0)
    inst.components.talker.symbol = "swap_object"

	inst.level = 0
inst:ListenForEvent("levelup", levelexp)
	inst.OnSave = onsave
	inst.OnPreLoad = onpreload

        inst:AddComponent("fueled")
       inst.components.fueled.fueltype = "CHEMICAL"
        inst.components.fueled:InitializeFuelLevel(20000000)
       inst.components.fueled:SetDepletedFn(OnDurability)
        inst.components.fueled.ontakefuelfn = TakeItem
        inst.components.fueled.accepting = true
inst.components.fueled:StartConsuming()        

    return inst
end

return Prefab( "common/inventory/armor_mushaa", fn, assets) 
