--created by Jake, coded by Keddy, updated by Lyris --Steinitz's Castling local s,id,o=GetID() function s.initial_effect(c) local e1=Effect.CreateEffect(c) e1:SetType(EFFECT_TYPE_ACTIVATE) e1:SetCode(EVENT_FREE_CHAIN) e1:HOPT(true) e1:SetTarget(s.target) e1:SetOperation(s.activate) c:RegisterEffect(e1) local e2=Effect.CreateEffect(c) e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O) e2:SetCode(EVENT_PHASE+PHASE_END) e2:SetRange(LOCATION_GRAVE) e2:SetCategory(CATEGORY_TODECK) e2:SetTarget(s.tdtg) e2:SetOperation(s.tdop) c:RegisterEffect(e2) end function s.colfilter(c,tp) return (c:IsType(TYPE_MONSTER) or c:IsLocation(LOCATION_MZONE)) and c:IsControler(1-tp) end function s.cfilter(c,e) return c:IsFaceup() and c:IsSetCard(0x63d0) and c:GetSequence()<5 and c:IsCanBeEffectTarget(e) end function s.gchk(g,tp) return g:IsExists(s.cfilter2,1,nil,tp) end function s.cfilter2(c,tp) return c:GetColumnGroup():FilterCount(s.colfilter,nil,tp)==0 end function s.target(e,tp,eg,ep,ev,re,r,rp,chk) local g=Duel.GetMatchingGroup(s.cfilter,tp,LOCATION_MZONE,0,nil,e) if chk==0 then return g:CheckSubGroup(s.gchk,2,2,tp) end Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_CONTROL) Duel.SetTargetCard(g:SelectSubGroup(tp,s.gchk,false,2,2,tp)) end function s.activate(e,tp,eg,ep,ev,re,r,rp) local g=Duel.GetTargetsRelateToChain() local tc1=g:GetFirst() local tc2=g:GetNext() if not (tc1 and tc2) then return end local c=e:GetHandler() local seq1=tc1:GetSequence() local seq2=tc2:GetSequence() Duel.SwapSequence(tc1,tc2) for _,tc in ipairs{tc1,tc2} do local e1=Effect.CreateEffect(c) e1:SetType(EFFECT_TYPE_SINGLE) e1:SetCode(EFFECT_IMMUNE_EFFECT) e1:SetOwnerPlayer(tp) e1:SetReset(RESET_EVENT+RESETS_STANDARD) e1:SetValue(s.efilter) tc:RegisterEffect(e1) local e2=Effect.CreateEffect(c) e2:SetType(EFFECT_TYPE_SINGLE) e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE) e2:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END) e2:SetValue(1) tc:RegisterEffect(e2) end end function s.efilter(e,re) return e:GetOwnerPlayer()~=re:GetOwnerPlayer() end function s.tdtg(e,tp,eg,ep,ev,re,r,rp,chk) local c=e:GetHandler() if chk==0 then return c:IsAbleToDeck() and Duel.GetFlagEffect(tp,id)<1 end Duel.SetOperationInfo(0,CATEGORY_TODECK,c,1,0,0) end function s.tdop(e,tp,eg,ep,ev,re,r,rp) local c=e:GetHandler() if not c:IsRelateToChain() or Duel.SendtoDeck(c,nil,SEQ_DECKSHUFFLE,REASON_EFFECT)<1 or not c:IsLocation(LOCATION_DECK) then return end local e1=Effect.CreateEffect(c) e1:SetType(EFFECT_TYPE_FIELD) e1:SetCode(EFFECT_CANNOT_TO_HAND) e1:SetTargetRange(LOCATION_MZONE,0) e1:SetTarget(aux.TargetBoolFunction(Card.IsSetCard,0x63d0)) e1:SetReset(RESET_PHASE+PHASE_END) Duel.RegisterEffect(e1,tp) Duel.RegisterFlagEffect(tp,id,RESET_PHASE+PHASE_END,0,1) end