--No.39 羁望皇 霍普
local cm,m,o=GetID()
function cm.initial_effect(c)
    --xyz summon
    c:EnableReviveLimit()
    aux.AddXyzProcedureLevelFree(c,cm.mfilter,nil,2,99)
    --xyzlimit
    local e1=Effect.CreateEffect(c)
    e1:SetType(EFFECT_TYPE_SINGLE)
    e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
    e1:SetCode(EFFECT_CANNOT_BE_XYZ_MATERIAL)
    e1:SetValue(1)
    c:RegisterEffect(e1)
    local e3=Effect.CreateEffect(c)
    e3:SetDescription(aux.Stringid(m,0))
    e3:SetType(EFFECT_TYPE_IGNITION)
    e3:SetRange(LOCATION_MZONE)
    e3:SetCost(cm.cost)
    e3:SetTarget(cm.tg)
    e3:SetOperation(cm.op)
    c:RegisterEffect(e3)
end
aux.xyz_number[m]=39
function cm.mfilter(c,xyzc)
    return c:IsXyzType(TYPE_XYZ) and c:IsSetCard(0x48)
end
function cm.cfilter(c,e)
    local chk=true
    local id=e:GetHandler():GetFlagEffect(m)
    if id>0 then
        for a=1,id,1 do
            if not chk then break end
            local m1=m+a
            local code=e:GetHandler():GetFlagEffectLabel(m1)
            if c:IsCode(code) then chk=false end
        end
    end
    return c:IsXyzType(TYPE_XYZ) and c:IsXyzType(TYPE_EFFECT) and c:IsSetCard(0x48) and not c:IsCode(m) and chk
end
function cm.cfilter1(c)
    return c:IsSetCard(0x107f)
end
function cm.cost(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return e:GetHandler():CheckRemoveOverlayCard(tp,1,REASON_COST) and e:GetHandler():GetOverlayGroup():FilterCount(cm.cfilter,nil,e)>0 and e:GetHandler():GetOverlayGroup():FilterCount(cm.cfilter1,nil)>0 end
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVEXYZ)
    local sg=e:GetHandler():GetOverlayGroup():FilterSelect(tp,cm.cfilter,1,1,nil,e)
    if not sg then return end
    Duel.SendtoGrave(sg,REASON_COST)
    sg:KeepAlive()
    e:SetLabelObject(sg)
end
function cm.tg(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>=1 end
end
function cm.op(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local tc=e:GetLabelObject():GetFirst()
    if tc and c:IsRelateToEffect(e) and c:IsFaceup() then
        local code=tc:GetOriginalCodeRule()
        c:CopyEffect(code,RESET_EVENT+RESETS_STANDARD,1)
        c:RegisterFlagEffect(m,RESET_EVENT+RESETS_STANDARD,0,1)
        local id=c:GetFlagEffect(m)+m
        c:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD,0,1,code)
        if Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>=1 then
            local g=Duel.GetDecktopGroup(tp,1)
            Duel.DisableShuffleCheck()
            Duel.Overlay(c,g)
        end
    end
end