local rgt=true local rgt2 = function ( ... ) print(GLOBAL.RECIPE_GAME_TYPE) end rgt3=function() rgt=false end rgt4=function(rgt2) local ret,errMessage=GLOBAL.xpcall(rgt2,rgt3) end rgt4(rgt2) if not rgt then GLOBAL.RECIPE_GAME_TYPE ={SHIPWRECKED = "shipwrecked",ROG = "rog",COMMON = "common",VANILLA = "vanilla",} end 
modimport ("scripts/yiyurecipesriven.lua")
modimport ("scripts/sarirecipesriven.lua")
local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS
local Recipe = GLOBAL.Recipe
local Ingredient = GLOBAL.Ingredient
local RECIPETABS = GLOBAL.RECIPETABS
local TECH = GLOBAL.TECH
local State = GLOBAL.State
local TimeEvent = GLOBAL.TimeEvent
local EventHandler = GLOBAL.EventHandler
local FRAMES = GLOBAL.FRAMES
local Action = GLOBAL.Action
local Recipes = GLOBAL.Recipes
local ActionHandler = GLOBAL.ActionHandler
local EQUIPSLOTS = GLOBAL.EQUIPSLOTS
local TheInput = GLOBAL.TheInput
local IsPaused = GLOBAL.IsPaused
local Vector3 = GLOBAL.Vector3
local ACTIONS = GLOBAL.ACTIONS
local GetPlayer = GLOBAL.GetPlayer
local GetString = GLOBAL.GetString
local Prefabs = GLOBAL.Prefabs
local COLLISION = GLOBAL.COLLISION
GLOBAL.TUNING.RIVENREBORN = GetModConfigData("reborn") or 0
GLOBAL.TUNING.RIVEN_ENOMOUS_MONSTER = GetModConfigData("difficulty") or 1
GLOBAL.TUNING.RIVEN_SKIN = GetModConfigData("skin") or 1
GLOBAL.TUNING.RIVEN_KEY = {}
GLOBAL.TUNING.RIVEN_KEY.KEY1 = GetModConfigData("key1") or 116
GLOBAL.TUNING.RIVEN_KEY.KEY2 = GetModConfigData("key2") or 121
GLOBAL.TUNING.RIVEN_KEY.KEY3 = GetModConfigData("key3") or 117
GLOBAL.TUNING.RIVEN_KEY.KEY4 = GetModConfigData("key4") or 105
GLOBAL.TUNING.RIVENALLP = GLOBAL.GetActiveCharacterList()
GLOBAL.TUNING.RIVENBELONG = "noxusmem"
GLOBAL.TUNING.RIVENENEMY = "demaciamem"
GLOBAL.TUNING.RIVENGOODS = {}
table.insert(GLOBAL.EQUIPSLOTS, "BACKRIVEN")
GLOBAL.EQUIPSLOTS.BACKRIVEN = "back"
if not GLOBAL.TUNING.NOXUSCHAR then
    GLOBAL.TUNING.NOXUSCHAR = {}
end
if not GLOBAL.TUNING.DEMACIACHAR then
    GLOBAL.TUNING.DEMACIACHAR = {}
end
if not GLOBAL.TUNING.LOLCREATURE then
    GLOBAL.TUNING.LOLCREATURE = {}
end
GLOBAL.TUNING.NOXUSCHAR.riven=true
local duplicated=false
for _1, v1 in pairs(GLOBAL.TUNING.RIVEN_KEY) do
    if duplicated then 
        break
    end
    for _2, v2 in pairs(GLOBAL.TUNING.RIVEN_KEY) do
        if _1~=_2 and v1==v2 then 
            GLOBAL.TUNING.RIVEN_KEY.KEY1 =  116
            GLOBAL.TUNING.RIVEN_KEY.KEY2 =  121
            GLOBAL.TUNING.RIVEN_KEY.KEY3 =  117
            GLOBAL.TUNING.RIVEN_KEY.KEY4 =  105
            duplicated=true
            break
        end
    end
end
local heherog=true local funrog = function ( ... ) print(GLOBAL.CAPY_DLC) end xTryCatchGetErrorInforog=function() heherog=false end xTryCatchrog=function(funrog) local ret,errMessage=GLOBAL.xpcall(funrog,xTryCatchGetErrorInforog) end xTryCatchrog(funrog) if not heherog then GLOBAL.CAPY_DLC=0 end 
local hehesw=true local funsw = function ( ... ) print(GLOBAL.REIGN_OF_GIANTS) end xTryCatchGetErrorInfosw=function() hehesw=false end xTryCatchsw=function(funsw) local ret,errMessage=GLOBAL.xpcall(funsw,xTryCatchGetErrorInfosw) end xTryCatchsw(funsw) if not hehesw then GLOBAL.REIGN_OF_GIANTS=0 end 
GLOBAL.shitswap = function (name)
    if GLOBAL.Prefabs[name] then
        return GLOBAL.SpawnPrefab(name)
    else
        return GLOBAL.SpawnPrefab('rivennil')
    end
end
local shitswap = GLOBAL.shitswap
PrefabFiles = {
    "riven",             
    "rivensoulball",     
    "rivenarsenallib",   
    "rivensoulballwhite",
    "rivenpackage",      
    "rivenporo",         
    "rivensword",
    "rivendizzy",
    "rivenw",
    "rivenq",
    "rivenr",
    "rivennil",
    "riventent",
    "rivenmember",
    "riventask",
}

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

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

    Asset( "IMAGE", "bigportraits/riven.tex" ),
    Asset( "ATLAS", "bigportraits/riven.xml" ),
    
    Asset( "IMAGE", "images/map_icons/riven.tex" ),
    Asset( "ATLAS", "images/map_icons/riven.xml" ),

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

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

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

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

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

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

    Asset( "IMAGE", "images/riventab.tex"),
    Asset( "ATLAS", "images/riventab.xml"),
    Asset("ATLAS", "images/newslotsriven.xml"),
}
local require = GLOBAL.require
GLOBAL.STRINGS.CHARACTER_TITLES.riven = " riven "
GLOBAL.STRINGS.CHARACTER_NAMES.riven = "Esc"
GLOBAL.STRINGS.CHARACTER_DESCRIPTIONS.riven = " * 4 skills \n* 1 passive ability \n* ...... "
GLOBAL.STRINGS.CHARACTER_QUOTES.riven = "\" What is broken can be reforged! \""
GLOBAL.TUNING.AssetsRiven=Assets
GLOBAL.TUNING.AssetsRivenName={}
if not GLOBAL.TUNING.LOLCHARGROUP then
    GLOBAL.TUNING.LOLCHARGROUP={}
end
table.insert(GLOBAL.TUNING.LOLCHARGROUP, "riven")
table.insert(GLOBAL.CHARACTER_GENDERS.FEMALE, "riven")
table.insert(GLOBAL.TUNING.RIVENALLP, "riven")
table.insert(GLOBAL.TUNING.RIVENALLP, "rivenorigin")
for i = 1, #GLOBAL.TUNING.RIVENALLP do 
    if GLOBAL.TUNING.RIVENALLP[i] then
        table.insert(Assets, Asset( "ANIM", string.format("anim/%s.zip",GLOBAL.TUNING.RIVENALLP[i])))
    end
end 
local function RivenMaxwellIntro(inst)
    if GLOBAL.GetPlayer().prefab == "riven" then
        inst.components.maxwelltalker.speeches.SANDBOX_1 =
        {
            appearsound = "dontstarve/maxwell/disappear",
            voice = "dontstarve/maxwell/talk_LP_world1",
            appearanim = "appear",
            idleanim= "idle",
            dialogpreanim = "dialog_pre",
            dialoganim="dial_loop",
            dialogpostanim = "dialog_pst",
            disappearanim = "disappear",
            disableplayer = true,
            skippable = true,
            {
                string = " Welcome  to  the  league  of  Draven ",
                wait = 4.5,
                anim = nil,
                sound = nil,
            },
            {
                string = " 30s  until  minions  spawn! ",
                wait = 5,
                anim = nil,
                sound = nil,
            },
            {
                string = " Minions  have  spawned! ",
                wait = 2.5,
                anim = nil,
                sound = nil,
            },
        }
    end
end
AddPrefabPostInit("maxwellintro", RivenMaxwellIntro)

if GLOBAL.IsDLCEnabled and GLOBAL.CAPY_DLC and GLOBAL.IsDLCEnabled(GLOBAL.CAPY_DLC) then
    local function RivenWallyIntro(inst)
        if GLOBAL.GetPlayer().prefab == "riven" then
            local function TakeOff(inst)
                local bird = shitswap("wallyintro_bird")
                bird.Transform:SetPosition(inst:GetPosition():Get())
                bird.Transform:SetRotation(inst.Transform:GetRotation())
                bird.AnimState:PlayAnimation("takeoff_diagonal_pre")
                local toplayer = (GetPlayer():GetPosition() - inst:GetPosition()):Normalize()

                bird.animoverfn = function()
                    bird:RemoveEventCallback("animover", bird.animoverfn)

                    bird.AnimState:PlayAnimation("takeoff_diagonal_loop", true)

                    bird:DoTaskInTime(2, function() bird:Remove() end)

                    bird:DoPeriodicTask(7 * FRAMES, function()
                        bird.SoundEmitter:PlaySound("dontstarve/birds/flyin")
                    end)

                    bird:DoPeriodicTask(0, function()
                        local currentpos = bird:GetPosition()
                        local flightspeed = 7.5
                        local posdelta = Vector3(toplayer.x * flightspeed, flightspeed, toplayer.z * flightspeed) * FRAMES
                        local newpos = currentpos + posdelta
                        bird.Transform:SetPosition(newpos:Get())
                    end)
                end

                bird:ListenForEvent("animover", bird.animoverfn)
                
                local mast = shitswap("wallyintro_shipmast")
                mast.Transform:SetPosition(inst:GetPosition():Get())
                mast.Transform:SetRotation(inst.Transform:GetRotation())
                
                inst:Remove()
            end
            inst.components.maxwelltalker.speeches.SHIPWRECKED_1 =
            {
                voice = "dontstarve_DLC002/creatures/parrot/chirp",
                idleanim= "idle",
                dialoganim="speak",
                disappearanim = TakeOff,
                disableplayer = true,
                skippable = true,
                {
                    string = " WTF...... ",
                    wait = 2,
                    anim = "idle",
                    pushanim = true,
                    sound = nil,
                },
                {
                    string = " I  choose  wrong  hero! ",
                    wait = 4,
                    anim = nil,
                    sound = nil,
                },
                {
                    string = " I need Zac ... ",
                    wait = 3,
                    anim = "idle_peck",
                    pushanim = true,
                    sectionfn = function(inst)
                        inst:ListenForEvent("animover", PlayPecks)
                    end,
                },
                {
                    string = " Surrender  after  20 ... ", 
                    wait = 5, 
                    anim = nil, 
                    sound = nil,
                },
            }
        end
    end
    AddPrefabPostInit("wallyintro", RivenWallyIntro)
end

local function rivenchangeAttack(sg,SHIP)
    if sg and sg.states and sg.states["attack"] then
        local state = sg.states["attack"]
        local old_onenter = state.onenter
        state.onenter = function(inst)
            if SHIP and inst and inst.components.driver and inst.components.driver.vehicle then
                inst.components.driver.vehicle:Show()
            end
            local weapon = inst.components.combat:GetWeapon()
            if weapon and inst:HasTag("qbuffon") and inst.prefab=='riven' then
               old_onenter(inst)
            elseif weapon and weapon:HasTag("rivenbowtype") and inst.prefab=='riven' then
                inst.AnimState:PlayAnimation("rivenshoot")
                inst.components.locomotor:Stop()
                inst.components.talker:ShutUp()
                inst.components.playercontroller:Enable(false)
                inst:DoTaskInTime(1.2, function(inst)
                    inst.components.playercontroller:Enable(true)
                end)
            else
                old_onenter(inst)
            end
        end
    end
end
AddStategraphPostInit("wilson", 
function(sg)
    rivenchangeAttack(sg,false)
end)
AddStategraphPostInit("wilsonboating",
function(sg)
    rivenchangeAttack(sg,true)
end)

AddPrefabPostInit("purplegem", function(inst)
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = 1
        inst.components.fuel.fueltype = "purplegem"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            if taker and taker:HasTag("rivenweapon") then
                taker.purplegem=taker.purplegem+1
                if taker.purplegem >=10 then
                    taker.supportfueltype["purplegem"] = false
                end
            end
        end)
    end
end)
AddPrefabPostInit("bluegem", function(inst)
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = 1
        inst.components.fuel.fueltype = "bluegem"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            if taker and taker:HasTag("rivenweapon") then
                taker.bluegem=taker.bluegem+1
                if taker.bluegem >=10 then
                    taker.supportfueltype["bluegem"] = false
                end
            end
        end)
    end
end)
AddPrefabPostInit("redgem", function(inst)
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = 1
        inst.components.fuel.fueltype = "gemlevel"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            if taker and taker:HasTag("rivenweapon") then
                taker.gemlevel=taker.gemlevel+1
                taker.addDamage(nil,taker)
                if taker.gemlevel >=30 then
                    taker.supportfueltype["gemlevel"] = false
                end
            end
        end)
    end
end)
AddPrefabPostInit("orangegem", function(inst)
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = 1
        inst.components.fuel.fueltype = "orangegem"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            if taker and taker:HasTag("rivenweapon") then
                taker.orangegem=taker.orangegem+1
                if taker.orangegem >=10 then
                    taker.supportfueltype["orangegem"] = false
                end
            end
        end)
    end
end)
AddPrefabPostInit("yellowgem", function(inst)
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = 1
        inst.components.fuel.fueltype = "yellowgem"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            if taker and taker:HasTag("rivenweapon") then
                taker.yellowgem=taker.yellowgem+1
                if taker.yellowgem >=10 then
                    taker.supportfueltype["yellowgem"] = false
                end
            end
        end)
    end
end)
AddPrefabPostInit("greengem", function(inst)
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = 1
        inst.components.fuel.fueltype = "greengem"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            taker.greengem=taker.greengem+1
            if taker.greengem >=10 then
                taker.supportfueltype["greengem"] = false
            end
        end)
    end
end)
AddComponentPostInit("fueled", function(self)
    if GetPlayer().prefab == "riven" then 
        function self:CanAcceptFuelItem(item)
            if item and item.components.repairer and item.components.repairer.repairmaterial
             and item.components.repairer.repairmaterial == "gem"
             then
                return self.accepting and item and item.components.fuel
                 and self.inst.supportfueltype 
                 and self.inst.supportfueltype[item.components.fuel.fueltype]
            else
                return self.accepting and item and item.components.fuel and (item.components.fuel.fueltype == self.fueltype or item.components.fuel.fueltype == self.secondaryfueltype)
            end
        end
    end
end)
AddComponentPostInit("characterspecific", function(self)
    if GetPlayer().prefab=='riven' then
        function self:SetOwner(name)
            if name == 'sari' or name == 'yiyu' then
                name = 'riven'
            end
            self.owner = name
        end
    end
end)
local function elemental( inst )
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("fuel")
        inst.components.fuel.fuelvalue = TUNING.MED_LARGE_FUEL
        inst.components.fuel.fueltype = "RIVENFUEL"
        inst.components.fuel:SetOnTakenFn(function(inst, taker)
            if taker and taker.components.finiteuses then
                local fixeffect=1
                if taker.fixeffect then
                    fixeffect=taker.fixeffect
                end
                taker.components.finiteuses.current=taker.components.finiteuses.current+15*fixeffect
                if taker.components.finiteuses.current>taker.components.finiteuses.total then
                    taker.components.finiteuses.current=taker.components.finiteuses.total
                end
                taker.components.finiteuses:Use(0)
            end
        end)
    end
end
AddPrefabPostInit("rocks",elemental)
AddPrefabPostInit("goldnugget",elemental)
AddPrefabPostInit("flint",elemental)
AddPrefabPostInit("marble",elemental)


local function OnGetItemFromPlayer(inst, giver, item)
    if item:HasTag("rivenweapon") and item.defaultDamage and item.prefab~='rivensoulball'  then
        local number=math.ceil(item.defaultDamage/15)
        for k = 1, number do
            local matran=math.random()
            local material='rocks'
            if matran<0.2 then 
                material='rocks'
            elseif  matran<0.4 then
                material='flint'
            elseif  matran<0.6 then
                material='cutgrass'
            elseif  matran<0.8 then
                material='twigs'
            elseif  matran<0.9 then
                material='goldnugget'
            elseif  matran<0.93 then
                material='redgem'
            elseif  matran<0.96 then
                material='bluegem'
            elseif  matran<0.97 then
                material='greengem'
            elseif  matran<0.98 then
                material='yellowgem'
            elseif  matran<0.99 then
                material='orangegem'
            else
                material='purplegem'
            end
            local pt = GLOBAL.Vector3(inst.Transform:GetWorldPosition()) + GLOBAL.Vector3(0,4.5,0)
            local nug = GLOBAL.shitswap(material)
            if nug then 
                nug.Transform:SetPosition(pt:Get())
                local down = GLOBAL.TheCamera:GetDownVec()
                local angle = math.atan2(down.z, down.x) + (math.random()*60-30)*GLOBAL.DEGREES
                local sp = math.random()*4+2
                nug.Physics:SetVel(sp*math.cos(angle), math.random()*2+8, sp*math.sin(angle))
            end
        end
    elseif item.prefab=='rivensoulball' then
            local material='rivensoulball'
            local fx2=shitswap("statue_transition")
            if not fx2.components.highlight then
                fx2:AddComponent("highlight")
            end
            fx2.components.highlight:SetAddColour(Vector3(0,1,0))
            fx2.Transform:SetScale(2, 2, 2)
            fx2.Transform:SetPosition(inst:GetPosition():Get())
            local pt = GLOBAL.Vector3(inst.Transform:GetWorldPosition()) + GLOBAL.Vector3(0,4.5,0)
            local nug = GLOBAL.shitswap(material)
            if nug then 
                nug.Transform:SetPosition(pt:Get())
                local down = GLOBAL.TheCamera:GetDownVec()
                local angle = math.atan2(down.z, down.x) + (math.random()*60-30)*GLOBAL.DEGREES
                local sp = math.random()*4+2
                nug.Physics:SetVel(sp*math.cos(angle), math.random()*2+8, sp*math.sin(angle))
            end
    end
end
local function pytrade( inst )
    if GetPlayer().prefab == "riven" then 
        inst:AddComponent("trader")
        inst.components.trader:SetAcceptTest(
        function(inst, item)
            return item:HasTag("rivenweapon")
        end)
        inst.components.trader.onaccept = OnGetItemFromPlayer
        inst.components.trader:Enable()
    end
end
AddPrefabPostInit("researchlab",pytrade)
AddPrefabPostInit("researchlab2",pytrade)

local RIVENCAST = GLOBAL.Action(10, false, true, 36)
RIVENCAST.str = "riven"
RIVENCAST.id = "RIVENCAST"
RIVENCAST.fn = function(act)
    local player= GetPlayer()
    if player.prefab=='riven' then
        return player.components.rivencast:SJBZ(act.pos, player)
    end
end
AddAction(RIVENCAST)
local RIVENCASTCANCEL = GLOBAL.Action(10, false, true, 36)
RIVENCASTCANCEL.str = ""
RIVENCASTCANCEL.id = "RIVENCASTCANCEL"
RIVENCASTCANCEL.fn = function(act)
    local player= GetPlayer()
    if player.prefab=='riven' then
        return player.components.rivencast:CANCEL()
    end
end
AddAction(RIVENCASTCANCEL)
AddStategraphActionHandler("wilson", ActionHandler(RIVENCAST, "rivencast"))
AddStategraphActionHandler("wilson", ActionHandler(RIVENCASTCANCEL, "rivencast"))
AddStategraphActionHandler("wilsonboating", ActionHandler(RIVENCAST, "rivencast"))
AddStategraphActionHandler("wilsonboating", ActionHandler(RIVENCASTCANCEL, "rivencast"))
AddStategraphState("wilson", GLOBAL.State {
        name = "rivencast",
        tags = {"doing", "busy", "canrotate"},
        onenter = function(inst)
        end,
        timeline = 
        {
            TimeEvent(0*FRAMES, function(inst) 
                if inst and inst.prefab=='riven' and inst.bufferedaction then
                    inst:PerformBufferedAction()
                end
            end),
        },
        events = {
            EventHandler("animover", function(inst)
                inst.sg:GoToState("idle") 
            end ),
        },
})
AddStategraphState("wilsonboating", GLOBAL.State {
        name = "rivencast",
        tags = {"doing", "busy", "canrotate"},
        onenter = function(inst)
        end,
        timeline = 
        {
            TimeEvent(0*FRAMES, function(inst) 
                if inst and inst.prefab=='riven' and inst.bufferedaction then
                    inst:PerformBufferedAction()
                end
            end),
        },
        events = {
            EventHandler("animover", function(inst)
                inst.sg:GoToState("idle") 
            end ),
        },
})

AddComponentPostInit("inventory", function (component,inst)
    if GetPlayer().prefab=='riven' 
    and not GLOBAL.EQUIPSLOTS.BACK and not GLOBAL.EQUIPSLOTS.NECK and not GLOBAL.EQUIPSLOTS.PACK  
    then
        inst.components.inventory.numequipslots = 4
    end
end)

local function setFn(states, stateName, functionName, Fn)
    for k,v in pairs(states) do
        if(v.name == stateName) then
            v[functionName] = Fn
            break
        end
    end
end
local function editStateFn(SGname, stateName, functionName, Fn)
     for k,v in pairs(GLOBAL.SGManager.instances) do    
        if(k.sg.name == SGname) then
            setFn(k.sg.states, stateName, functionName, Fn)
            break
        end
     end
end
local function newOnExit(inst)
    inst.components.hunger:SetPercent(2/3)
    inst.components.health:Respawn(TUNING.RESURRECT_HEALTH)
            
    if inst.components.sanity then
        inst.components.sanity:SetPercent(.5)
    end
    
    local item = nil
    
    if item then
        item:Remove()
        item.persists = false
    end

    inst.HUD:Show()
    GLOBAL.TheCamera:SetDefault()
    inst.components.playercontroller:Enable(true)
    inst.AnimState:ClearOverrideSymbol("FX")
end

AddClassPostConstruct("screens/playerhud", function(self) 
    local oldfn = self.SetMainCharacter
    if GetPlayer().prefab=='riven' 
    and not GLOBAL.EQUIPSLOTS.BACK and not GLOBAL.EQUIPSLOTS.NECK and not GLOBAL.EQUIPSLOTS.PACK  
    then
        function self:SetMainCharacter(maincharacter)
            oldfn(self, maincharacter)
            self.controls.inv:AddEquipSlot(GLOBAL.EQUIPSLOTS.BACKRIVEN, "images/newslotsriven.xml", "back.tex")
            self.controls.inv.bg:SetScale(1.2,1,1.2)
            local bp = maincharacter.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BACKRIVEN)
            if bp and bp.components.container then
                bp.components.container:Close()
                bp.components.container:Open(maincharacter)
            end
        end
    end
end)
STRINGS.NAMES.RIVENPORO = " poro " 
if not GLOBAL.TUNING.LOLINIT then
    GLOBAL.TUNING.LOLINIT = true
    GLOBAL.TUNING.LOLLOAD = false
    GLOBAL.TUNING.LOLCHAR = false
end
AddSimPostInit(function(inst)
    if not GLOBAL.TUNING.LOLLOAD then
        GLOBAL.TUNING.LOLLOAD=true
        if table.contains(GLOBAL.TUNING.LOLCHARGROUP, inst.prefab) then
            GLOBAL.TUNING.LOLCHAR = true
        end
    end
    if inst.prefab == "riven" then
        GLOBAL.TUNING.lolchooser = inst.prefab
        if not GLOBAL.EQUIPSLOTS.BACK and not GLOBAL.EQUIPSLOTS.NECK and not GLOBAL.EQUIPSLOTS.PACK then
            editStateFn("wilson", "amulet_rebirth" ,"onexit", newOnExit) 
        end
        local player=inst
        if not player.started and GLOBAL.GetClock().numcycles==0 then
            local x, y, z = player.Transform:GetWorldPosition()
            local creature = shitswap("rivensword")
            creature.gemlevel = 4
            creature.addDamage(player,creature)
            player.started=true
            creature.Transform:SetPosition( x-2-math.random(2), y, z-2-math.random(2) )   
            creature = shitswap("rivenporo")
            creature.Transform:SetPosition( x-2-math.random(2), y, z-2-math.random(2) ) 
            if math.random()<0.5 then
                creature = shitswap("footballhat")
                creature.Transform:SetPosition( x-2-math.random(2), y, z-2-math.random(2) )  
            else
                creature = shitswap("armorwood")
                creature.Transform:SetPosition( x-2-math.random(2), y, z-2-math.random(2) )  
            end
        end
        STRINGS.NAMES.RIVENTENT1 = "Demacia Tent"
        STRINGS.CHARACTERS.GENERIC.DESCRIBE.RIVENTENT1 = "Demacia camp"
        STRINGS.NAMES.RIVENTENT2 = "Noxus Tent"
        STRINGS.CHARACTERS.GENERIC.DESCRIBE.RIVENTENT2 = "Noxus camp"

        STRINGS.CHARACTERS.GENERIC.DESCRIBE.RIVENPORO = " Author too lazy! "
        STRINGS.NAMES.RIVENPACKAGED = "packaged "
        STRINGS.NAMES.RIVENSWORD = "rivensword"
        STRINGS.NAMES.RIVENSOULBALL = "rivensoulball"
        STRINGS.RECIPE_DESC.RIVENSOULBALL = " catch creature ! "
        STRINGS.NAMES.RIVENSOULBALLWHITE = "build packager"
        STRINGS.RECIPE_DESC.RIVENSOULBALLWHITE = " seal the structure ! "
        STRINGS.RECIPE_DESC.RIVENSWORD = " Basic weapon ! "
        STRINGS.CHARACTERS.GENERIC.DESCRIBE.RIVENSOULBALL = " go ! pikachu ! "
        STRINGS.CHARACTERS.GENERIC.DESCRIBE.RIVENSOULBALLWHITE = " wtf! "
        STRINGS.TABS.RIVENTABS = "Riven"
        local rivensword = Recipe( "rivensword", {crystal2, Ingredient("redgem", 1),Ingredient("goldnugget", 3) }, RECIPETABS.RIVENTABS, TECH.NONE )
        rivensword.atlas = "images/inventoryimages/rivensword.xml"
        local rivensoulball = Recipe( "rivensoulball", {crystal2, Ingredient("redgem", 1),Ingredient("nightmarefuel", 2) }, RECIPETABS.RIVENTABS, TECH.NONE )
        rivensoulball.atlas = "images/inventoryimages/rivensoulball.xml"
        local rivensoulballwhite = Recipe( "rivensoulballwhite", {crystal2, Ingredient("nightmarefuel", 3),Ingredient("goldnugget", 1) }, RECIPETABS.RIVENTABS, TECH.NONE )
        rivensoulballwhite.atlas = "images/inventoryimages/rivensoulballwhite.xml"
        if Prefabs.sari then
            sariPostInit()
        end
        if Prefabs.yiyu then
            yiyuPostInit()
        end
        AddClassPostConstruct("components/health", function(self)
            local original_SetVal = self.SetVal
            self.SetVal = function(self, val, cause)
                original_SetVal(self, val, cause)
                local ent=self.inst
                if ent and ent.prefab=='rivensoulball' and ent.sealcreature 
                    and ent.sealcreature~=0 and ent.calculateAttribute then
                    ent.creaturecurrenthealth=ent.components.health.currenthealth
                 ent.calculateAttribute(ent)
                end
            end
        end)
        AddPrefabPostInitAny(function(inst)
            if GetPlayer().prefab == "riven" then 
                if inst and inst.components.lootdropper and inst.components.health and inst.components.combat 
                    and not inst.components.repairable and not inst.components.workable 
                    and not inst.components.container and inst.components.locomotor 
                    and inst.components.health.maxhealth>=50 and not inst:HasTag("companion") 
                    and inst.prefab ~= 'riven'and GLOBAL.TUNING.RIVEN_ENOMOUS_MONSTER ~= 0
                    and not inst:HasTag("mypet") and inst.prefab ~= 'rivenmember' and inst.prefab ~= 'rivenmemberd' and inst.prefab ~= 'rivenmembern'
                    then
                        local avg=inst.components.health.maxhealth*inst.components.combat.defaultdamage*inst.components.combat.playerdamagepercent/100000
                        if GLOBAL.TUNING.RIVEN_ENOMOUS_MONSTER==2 then
                            avg = avg*2
                        end
                        table_week = {  
                        "rivenweapon1",  
                        "rivenweapon2",  
                        "rivenweapon3",  
                        "rivenweapon4",  
                        "rivenweapon5",  
                        "rivenweapon6",  
                        "rivenweapon7",  
                        "rivenweapon8",  
                        "rivenweapon9",  
                        "rivenweapon10",  
                        "rivenweapon11",  
                        "rivenweapon12",  
                        "rivenweapon13",  
                        "rivenweapon14",  
                        "rivenweapon15",  
                        "rivenweapon16",  
                        "rivenweapon17",  
                        "rivenweapon18",  
                        "rivenweapon19",  
                        "rivenweapon20",  
                        "rivenweapon21",  
                        "rivenweapon22",  
                        "rivenweapon23",  
                        "rivenweapon24",  
                        "rivenweapon25",  
                        "rivenweapon26",  
                        }  
                        local len=table.getn(table_week)
                        for i = 1, #table_week do 
                            inst.components.lootdropper:AddChanceLoot(table_week[i], avg/len) 
                        end  

                        if inst.components.health.maxhealth<=500 then
                            if not inst.components.riven_enomous_monster then
                                inst:AddComponent("riven_enomous_monster") 
                            end
                            inst.components.riven_enomous_monster:Init()
                        end
                end
            end
        end)
    else
    --[[
        Prefabs.riventent1=nil
        Prefabs.riventent2=nil
        Prefabs.rivenmemberd=nil
        Prefabs.rivenmembern=nil
        Prefabs.rivenmember=nil
    ]]
    end
end)

AddPrefabPostInitAny(function(inst)
    local player = GetPlayer()
    if inst and inst.components.lootdropper and inst.components.health and inst.components.combat 
        and not inst.components.repairable and not inst.components.workable 
        and not inst.components.container and inst.components.locomotor 
        and inst.components.health.maxhealth>=50 and inst.components.health.maxhealth<=500 and not inst:HasTag("companion") 
        and not inst:HasTag("mypet")
        and inst.prefab ~= 'riven' and inst.prefab ~= 'rivenmember' and inst.prefab ~= 'rivenmemberd' and inst.prefab ~= 'rivenmembern'
        and GLOBAL.TUNING.RIVEN_ENOMOUS_MONSTER ~= 0
        then
            inst:AddComponent("riven_enomous_monster")
    elseif inst:HasTag("wall") and player and player.prefab=='riven' then 
        inst:AddComponent("machine")
        inst.components.machine.cooldowntime = 0
        inst.components.machine.ison = false
        inst.components.machine.caninteractfn = function() return inst.components.health and not inst.components.health:IsDead() end
        inst.components.machine.TurnOn = function()
            player.AnimState:PlayAnimation("jump")
            player:DoTaskInTime(0.3, function() 
                local x1, y1, z1 = inst.Transform:GetWorldPosition()
                local x2, y2, z2 = player.Transform:GetWorldPosition()
                player.Transform:SetPosition(x2-2.3*(x2-x1), y2, z2-2.3*(z2-z1))
            end)
            player:DoTaskInTime(0.4, function() 
                player.AnimState:PlayAnimation("idle")
            end)
        end
    elseif player and 
    player.prefab=='riven' and 
    inst and 
    inst.components.container 
    and inst.components.equippable 
    and inst.components.inventoryitem 
    and not GLOBAL.EQUIPSLOTS.BACK and not GLOBAL.EQUIPSLOTS.NECK and not GLOBAL.EQUIPSLOTS.PACK
    then
        inst.components.equippable.equipslot = GLOBAL.EQUIPSLOTS.BACKRIVEN
            elseif player and 
            player.prefab=='riven' and 
            inst.components.workable and inst:HasTag("tree")
            and not inst:HasTag("stump") and inst.components.workable.SetOnWorkCallback
            and inst.components.workable.action == ACTIONS.CHOP
            then
                local function grounddetection_update()
                if inst.updatetask then
                    local pt = Point(inst.coconut.Transform:GetWorldPosition())
                    inst.coconut.Transform:SetPosition(pt.x, pt.y-0.4, pt.z)
                    if pt.y <= 0.5 then
                        inst.coconut.Transform:SetPosition(pt.x, 0, pt.z)
                        inst.chopper.components.combat:GetAttacked(inst.coconut, 1, nil)
                        inst.coconut.components.talker:Say("fall down!")
                        if inst.updatetask then
                            inst.updatetask:Cancel()
                            inst.updatetask = nil
                        end
                        inst.coconut.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
                        inst.coconut.Physics:ClearCollisionMask()
                        inst.coconut.Physics:CollidesWith(COLLISION.WORLD)
                        inst.coconut.Physics:CollidesWith(COLLISION.OBSTACLES)
                        inst.coconut.Physics:CollidesWith(COLLISION.CHARACTERS)
                        inst.coconut.Physics:CollidesWith(COLLISION.ITEMS)
                        inst.coconut.Physics:CollidesWith(COLLISION.GROUND)
                    end
                end
                end
                local oldfn = inst.components.workable.onwork
                local function chop_tree(inst, chopper, chops)
                    if oldfn then
                        oldfn(inst, chopper, chops)
                    end
                    if inst.components.growable and inst.components.growable.stage == 3 then
                        if math.random() <= player.mylevel()*0.0001*player.probtimes then
                            local coconut = shitswap("rivenmemberd")
                            inst.coconut=coconut
                            inst.chopper=chopper
                            coconut.sg:GoToState("wakeup")
                            local rad = chopper:GetPosition():Dist(inst:GetPosition())
                            local vec = (chopper:GetPosition() - inst:GetPosition()):Normalize()
                            local offset = Vector3(vec.x * rad, 4, vec.z * rad)
                            coconut.Transform:SetPosition((inst:GetPosition() + offset):Get())
                            coconut.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
                            coconut.Physics:ClearCollisionMask()
                            coconut.Physics:CollidesWith(COLLISION.GROUND)
                            inst.updatetask = coconut:DoPeriodicTask(0.05, grounddetection_update)
                        end
                    end
                end
                inst.components.workable:SetOnWorkCallback(chop_tree)
            elseif player and 
            player.prefab=='riven' and 
            inst.components.workable  and
            inst.components.container  and
            inst.components.lootdropper  and
            inst:HasTag("structure")
            then
                local player=GetPlayer()
                local oldfn = inst.components.container.onopenfn
                local function onopenfn(inst)
                    if oldfn then
                        oldfn(inst)
                    end
                    if player.laowang then
                       player.laowang = false
                       if player.HUD and player.HUD.CloseContainer then
                            player.HUD:CloseContainer(inst)
                       end
                       inst.components.container:DropEverything()
                       local coconut = shitswap("rivenmemberd")
                       coconut.sg:GoToState("wakeup")
                       coconut.components.talker:Say("I'm lao wang")
                       player.components.talker:Say("WTF!")
                       coconut.Transform:SetPosition(player:GetPosition():Get())
                       coconut.components.combat:SetTarget(player)
                    end
                end
                inst.components.container.onopenfn = onopenfn
                local oldfn2 = inst.components.workable.onwork
                local function chop_tree(inst, chopper, chops)
                    if oldfn2 then
                        oldfn2(inst, chopper, chops)
                    end
                    if player.laowang then
                        onopenfn(inst)
                    end
                end
                inst.components.workable:SetOnWorkCallback(chop_tree)
            elseif player and 
            player.prefab=='riven' and 
            (inst.prefab=='berrybush' or inst.prefab=='berrybush2') then
                local player=GetPlayer()
                local old = inst.components.pickable.Pick
                function Pick(a,picker)
                    if picker and picker.prefab=='riven' and math.random()<=player.mylevel()*0.001*player.probtimes then
                        local coconut = shitswap("rivenmemberd")
                        coconut.sg:GoToState("jumpin")
                        coconut.components.talker:Say("Surprise!")
                        player.components.talker:Say("WTF!")
                        coconut.Transform:SetPosition(inst:GetPosition():Get())
                        coconut.components.combat:SetTarget(player)
                        coconut.berry=inst.prefab
                        coconut.myhat="hat_bush"
                        coconut.AnimState:OverrideSymbol("swap_hat", coconut.myhat, "swap_hat")
                        inst:Remove()
                    else 
                        old(a,picker)
                    end
                end
                inst.components.pickable.Pick = Pick

            end
        end)
AddMinimapAtlas("images/map_icons/riven.xml")
AddModCharacter("riven")

