--DDD 识慧王 末日征服神
local s,id,o=GetID()
function s.initial_effect(c)
    --pendulum summon
    aux.EnablePendulumAttribute(c)
    --[[给与自己伤害的效果变成让自己基本分回复的效果。]]
    local e0=Effect.CreateEffect(c)
    e0:SetType(EFFECT_TYPE_FIELD)
    e0:SetCode(EFFECT_REVERSE_DAMAGE)
    e0:SetRange(LOCATION_PZONE)
    e0:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
    e0:SetTargetRange(1,0)
    e0:SetValue(s.rev0)
    c:RegisterEffect(e0)
    --[[双方回合主要阶段才能发动。
    选自己手卡·双方场上1张卡破坏，这张卡的攻击力上升1000。]]
    local e1=Effect.CreateEffect(c)
    e1:SetDescription(aux.Stringid(id,0))
    e1:SetCategory(CATEGORY_DESTROY+CATEGORY_ATKCHANGE)
    e1:SetType(EFFECT_TYPE_QUICK_O)
    e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetRange(LOCATION_MZONE)
    e1:SetCountLimit(1,id)
    e1:SetHintTiming(0,TIMINGS_CHECK_MONSTER+TIMING_END_PHASE)
    e1:SetCondition(s.con1)
    e1:SetTarget(s.tg1)
    e1:SetOperation(s.op1)
    c:RegisterEffect(e1)
    --[[这张卡不受对方的魔法·陷阱卡的效果影响]]
    local e2=Effect.CreateEffect(c)
    e2:SetType(EFFECT_TYPE_SINGLE)
    e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
    e2:SetCode(EFFECT_IMMUNE_EFFECT)
    e2:SetRange(LOCATION_MZONE)
    e2:SetValue(s.efilter)
    c:RegisterEffect(e2)
    --[[场上的卡被战斗·效果破坏的场合才能发动。
    从卡组把1张「契约书」卡加入手卡。]]
    local e3=Effect.CreateEffect(c)
    e3:SetDescription(aux.Stringid(id,1))
    e3:SetCategory(CATEGORY_SEARCH+CATEGORY_TOHAND)
    e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
    e3:SetCode(EVENT_CUSTOM+id)
    e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_DELAY)
    e3:SetRange(LOCATION_MZONE)
    e3:SetCondition(s.con3)
    e3:SetTarget(s.tg3)
    e3:SetOperation(s.op3)
    c:RegisterEffect(e3)
    if not s.global_check then
        s.global_check=true
        local ge1=Effect.CreateEffect(c)
        ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
        ge1:SetCode(EVENT_DESTROYED)
        ge1:SetCondition(s.regcon)
        ge1:SetOperation(s.regop)
        Duel.RegisterEffect(ge1,0)
    end
end
function s.rev0(e,re,r,rp,rc)
    return bit.band(r,REASON_EFFECT)~=0
end
function s.con1(e,tp,eg,ep,ev,re,r,rp)
    return Duel.GetCurrentPhase()==PHASE_MAIN1 or Duel.GetCurrentPhase()==PHASE_MAIN2
end
function s.tg1(e,tp,eg,ep,ev,re,r,rp,chk)
    if chk==0 then return Duel.IsExistingMatchingCard(nil,tp,LOCATION_ONFIELD+LOCATION_HAND,LOCATION_ONFIELD,1,nil) end
    local g=Duel.GetMatchingGroup(nil,tp,LOCATION_ONFIELD+LOCATION_HAND,LOCATION_ONFIELD,nil)
    Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,1,0,0)
end
function s.op1(e,tp,eg,ep,ev,re,r,rp)
    local g=Duel.GetMatchingGroup(nil,tp,LOCATION_ONFIELD+LOCATION_HAND,LOCATION_ONFIELD,nil)
    if #g==0 then return end
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
    local g1=g:Select(tp,1,1,nil)
    if g1:GetFirst():IsLocation(LOCATION_ONFIELD) then Duel.HintSelection(g1) end
    if Duel.Destroy(g1,REASON_EFFECT)>0 then
        local e1=Effect.CreateEffect(e:GetHandler())
        e1:SetType(EFFECT_TYPE_SINGLE)
        e1:SetProperty(EFFECT_FLAG_COPY_INHERIT)
        e1:SetCode(EFFECT_UPDATE_ATTACK)
        e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_DISABLE)
        e1:SetValue(1000)
        e:GetHandler():RegisterEffect(e1)
    end
end
function s.efilter(e,te)
    return te:IsActiveType(TYPE_TRAP+TYPE_SPELL)
end
function s.spcfilter(c,tp)
    return c:IsReason(REASON_BATTLE+REASON_EFFECT)
        and c:IsPreviousLocation(LOCATION_ONFIELD)
end
function s.regcon(e,tp,eg,ep,ev,re,r,rp)
    local v=0
    if eg:IsExists(s.spcfilter,1,nil,0) then v=v+1 end
    if eg:IsExists(s.spcfilter,1,nil,1) then v=v+2 end
    if v==0 then return false end
    e:SetLabel(({0,1,PLAYER_ALL})[v])
    return true
end
function s.regop(e,tp,eg,ep,ev,re,r,rp)
    Duel.RaiseEvent(eg,EVENT_CUSTOM+id,re,r,rp,ep,e:GetLabel())
end
function s.con3(e,tp,eg,ep,ev,re,r,rp)
    return ev==tp or ev==PLAYER_ALL
end
function s.filter3(c)
    return c:IsSetCard(0xae) and c:IsAbleToHand()
end
function s.tg3(e,tp,eg,ep,ev,re,r,rp,chk)
    local b=Duel.IsExistingMatchingCard(s.filter3,tp,LOCATION_DECK,0,1,nil)
    if chk==0 then return b end
    Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_DECK)
end
function s.op3(e,tp,eg,ep,ev,re,r,rp)
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
    local g=Duel.SelectMatchingCard(tp,s.filter3,tp,LOCATION_DECK,0,1,1,nil)
    if g:GetCount()>0 then
        Duel.SendtoHand(g,nil,REASON_EFFECT)
        Duel.ConfirmCards(1-tp,g)
    end
end