--末日的契约书
local s,id,o=GetID()
function s.initial_effect(c)
    --[[作为这张卡发动时的效果处理，丢弃手卡1只「DD」怪兽，
    从卡组·额外卡组·手卡·墓地把3只「DDD 死伟王 地狱终末神」特殊召唤。
    直到回合结束时，自己不是「DD」怪兽不能特殊召唤]]
    local e0=Effect.CreateEffect(c)
    e0:SetCategory(CATEGORY_HANDES+CATEGORY_SPECIAL_SUMMON+CATEGORY_GRAVE_ACTION)
    e0:SetType(EFFECT_TYPE_ACTIVATE)
    e0:SetCode(EVENT_FREE_CHAIN)
    e0:SetTarget(s.tg0)
    e0:SetOperation(s.op0)
    c:RegisterEffect(e0)
    --[[1回合1次，以自己场上1只「DDD 死伟王 地狱终末神」为对象才能发动。
    那只怪兽解放，从额外卡组把1只8星以上的「DDD」融合怪兽当作融合召唤特殊召唤。]]
    local e1=Effect.CreateEffect(c)
    e1:SetDescription(aux.Stringid(id,1))
    e1:SetCategory(CATEGORY_RELEASE+CATEGORY_SPECIAL_SUMMON+CATEGORY_FUSION_SUMMON)
    e1:SetType(EFFECT_TYPE_IGNITION)
    e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
    e1:SetRange(LOCATION_SZONE)
    e1:SetCountLimit(1)
    e1:SetTarget(s.target(SUMMON_TYPE_FUSION,TYPE_FUSION,EFFECT_MUST_BE_FMATERIAL))
    e1:SetOperation(s.operation(SUMMON_TYPE_FUSION,TYPE_FUSION,EFFECT_MUST_BE_FMATERIAL))
    c:RegisterEffect(e1)
    --[[1回合1次，以自己场上1只「DDD 死伟王 地狱终末神」为对象才能发动。
    那只怪兽解放，从额外卡组把1只8星以上的「DDD」同调怪兽当作同调召唤特殊召唤]]
    local e2=e1:Clone()
    e2:SetDescription(aux.Stringid(id,2))
    e2:SetCategory(CATEGORY_RELEASE+CATEGORY_SPECIAL_SUMMON)
    e2:SetTarget(s.target(SUMMON_TYPE_SYNCHRO,TYPE_SYNCHRO,EFFECT_MUST_BE_SMATERIAL))
    e2:SetOperation(s.operation(SUMMON_TYPE_SYNCHRO,TYPE_SYNCHRO,EFFECT_MUST_BE_SMATERIAL))
    c:RegisterEffect(e2)
    --[[1回合1次，以自己场上1只「DDD 死伟王 地狱终末神」为对象才能发动。
    从额外卡组把1只8阶以上的「DDD」超量怪兽在那只怪兽上面重叠来超量召唤。]]
    local e3=e2:Clone()
    e3:SetDescription(aux.Stringid(id,3))
    e3:SetTarget(s.tg3)
    e3:SetOperation(s.op3)
    c:RegisterEffect(e3)
end
function s.cfilter0(c)
    return c:IsSetCard(0xaf) and c:IsType(TYPE_MONSTER) and c:IsDiscardable(REASON_EFFECT)
end
function s.filter0(c,e,tp)
    return c:IsCode(47198668) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function s.chkfilter0(c,e,tp)
    return c:GetSequence()>4
end
function s.tg0(e,tp,eg,ep,ev,re,r,rp,chk)
    local g1=Duel.GetMatchingGroup(s.filter0,tp,LOCATION_HAND+LOCATION_DECK+LOCATION_GRAVE,0,nil,e,tp)
    local g2=Duel.GetMatchingGroup(s.filter0,tp,LOCATION_EXTRA,0,nil,e,tp)
    local chkct=Duel.GetMatchingGroupCount(s.chkfilter0,tp,LOCATION_MZONE,0,nil,e,tp)
    local ct1=g1:GetCount()
    local ct2=g2:GetCount()
    local b1=ct1+ct2==3 and Duel.GetLocationCountFromEx(tp)>=ct2 and Duel.GetLocationCount(tp,LOCATION_MZONE)>=ct1
    local b2=Duel.GetLocationCount(tp,LOCATION_MZONE)>=3 or (chkct==0 and Duel.GetLocationCount(tp,LOCATION_MZONE)>=2)
    if chk==0 then return Duel.IsExistingMatchingCard(s.cfilter0,tp,LOCATION_HAND,0,1,nil) and b1 and b2 end
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,3,tp,LOCATION_EXTRA+LOCATION_HAND+LOCATION_GRAVE+LOCATION_DECK)
end
function s.op0(e,tp,eg,ep,ev,re,r,rp)
    if Duel.DiscardHand(tp,s.cfilter0,1,1,REASON_EFFECT+REASON_DISCARD,nil)>0 then
        local g1=Duel.GetMatchingGroup(aux.NecroValleyFilter(s.filter0),tp,LOCATION_HAND+LOCATION_DECK+LOCATION_GRAVE,0,nil,e,tp)
        local g2=Duel.GetMatchingGroup(s.filter0,tp,LOCATION_EXTRA,0,nil,e,tp)
        local chkct=Duel.GetMatchingGroupCount(s.chkfilter0,tp,LOCATION_MZONE,0,nil,e,tp)
        local ct1=g1:GetCount()
        local ct2=g2:GetCount()
        local b1=ct1+ct2==3 and Duel.GetLocationCountFromEx(tp)>=ct2 and Duel.GetLocationCount(tp,LOCATION_MZONE)>=ct1
        local b2=Duel.GetLocationCount(tp,LOCATION_MZONE)>=3 or (chkct==0 and Duel.GetLocationCountFromEx(tp,tp,nil,nil,0x60) and Duel.GetLocationCount(tp,LOCATION_MZONE)>=2)
        if b1 and b2 then
            local a=0
            local g=Group.CreateGroup()
            if g1 then g:Merge(g1) end
            if g2 then g:Merge(g2) end
            Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
            local sg=g:Select(tp,3,3,nil)
            local sg1=sg:Filter(Card.IsLocation,nil,LOCATION_EXTRA)
            local sg2=sg:Filter(Card.IsLocation,nil,LOCATION_HAND+LOCATION_DECK+LOCATION_GRAVE)
            if sg1:GetCount()>0 then
                for tc in aux.Next(sg1) do
                    Duel.SpecialSummonStep(tc,0,tp,tp,false,false,POS_FACEUP)
                    a=a+1
                end
            end
            if sg2:GetCount()>0 then
                for tc in aux.Next(sg2) do
                    Duel.SpecialSummonStep(tc,0,tp,tp,false,false,POS_FACEUP)
                    a=a+1
                end
            end
            if a>0 then Duel.SpecialSummonComplete() end
        end
    end
    local e1=Effect.CreateEffect(e:GetHandler())
    e1:SetType(EFFECT_TYPE_FIELD)
    e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
    e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
    e1:SetTargetRange(1,0)
    e1:SetTarget(s.splimit)
    e1:SetReset(RESET_PHASE+PHASE_END)
    Duel.RegisterEffect(e1,tp)
end
function s.splimit(e,c)
    return not c:IsSetCard(0xaf)
end
function s.filter1(c,e,a,b)
    return c:IsFaceup() and c:IsCode(47198668) and c:IsReleasableByEffect() and Duel.IsExistingMatchingCard(s.spfilter1,tp,LOCATION_EXTRA,0,1,nil,c,e,e:GetHandlerPlayer(),a,b)
end
function s.spfilter1(c,mc,e,tp,a,b)
    return Duel.GetLocationCountFromEx(tp,tp,mc,c)>0 and c:IsLevelAbove(8) and c:IsSetCard(0x10af) and c:IsType(b) and c:IsCanBeSpecialSummoned(e,a,tp,false,false)
end
function s.target(a,b,c)
    return  function(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
                if chkc then return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter1(chkc,e,a,b) end
                if chk==0 then return Duel.IsExistingTarget(s.filter1,tp,LOCATION_MZONE,0,1,nil,e,a,b) and aux.MustMaterialCheck(nil,tp,c) end
                Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
                local tc=Duel.SelectTarget(tp,s.filter1,tp,LOCATION_MZONE,0,1,1,nil,e,a,b)
                Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
                Duel.SetOperationInfo(0,CATEGORY_RELEASE,tc,1,0,0)
            end
end
function s.operation(a,b,c)
    return  function(e,tp,eg,ep,ev,re,r,rp)
                local tc=Duel.GetFirstTarget()
                if tc:IsRelateToEffect(e) and Duel.Release(tc,REASON_EFFECT)>0 then
                    if not aux.MustMaterialCheck(nil,tp,c) then return end
                    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
                    local g=Duel.SelectMatchingCard(tp,s.spfilter1,tp,LOCATION_EXTRA,0,1,1,nil,tc,e,tp,a,b)
                    if g and Duel.SpecialSummon(g,a,tp,tp,false,false,POS_FACEUP)>0 then
                        g:GetFirst():CompleteProcedure()
                    end
                end
            end
end
function s.filter3(c,e,tp)
    return c:IsFaceup() and c:IsCode(47198668)
        and Duel.IsExistingMatchingCard(s.spfilter3,tp,LOCATION_EXTRA,0,1,nil,e,tp,c)
        and aux.MustMaterialCheck(c,tp,EFFECT_MUST_BE_XMATERIAL)
end
function s.spfilter3(c,e,tp,mc)
    return c:IsRankAbove(8) and c:IsSetCard(0x10af) and mc:IsCanBeXyzMaterial(c)
        and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_XYZ,tp,false,false) and Duel.GetLocationCountFromEx(tp,tp,mc,c)>0
end
function s.tg3(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
    if chkc then return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter3(chkc,e,tp) end
    if chk==0 then return Duel.IsExistingTarget(s.filter3,tp,LOCATION_MZONE,0,1,nil,e,tp)
        and e:GetHandler():IsCanOverlay() end
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TARGET)
    local g=Duel.SelectTarget(tp,s.filter3,tp,LOCATION_MZONE,0,1,1,nil,e,tp)
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
end
function s.op3(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    local tc=Duel.GetFirstTarget()
    if not aux.MustMaterialCheck(tc,tp,EFFECT_MUST_BE_XMATERIAL) then return end
    if tc:IsFacedown() or not tc:IsRelateToEffect(e) or tc:IsControler(1-tp) or tc:IsImmuneToEffect(e) then return end
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
    local g=Duel.SelectMatchingCard(tp,s.spfilter3,tp,LOCATION_EXTRA,0,1,1,nil,e,tp,tc,tc:GetRank()+1)
    local sc=g:GetFirst()
    if sc then
        local mg=tc:GetOverlayGroup()
        if mg:GetCount()~=0 then
            Duel.Overlay(sc,mg)
        end
        sc:SetMaterial(Group.FromCards(tc))
        Duel.Overlay(sc,Group.FromCards(tc))
        Duel.SpecialSummon(sc,SUMMON_TYPE_XYZ,tp,tp,false,false,POS_FACEUP)
        sc:CompleteProcedure()
    end
end