name: Dogmatikalamity
card_text: This card can be used to Ritual Summon any "Dogmatika" Ritual Monster. You must also Tribute monsters from your hand or field, whose total Levels exactly equal the Level of the Ritual Monster you Ritual Summon, or send 1 monster from your Extra Deck to the GY, whose Level equals the Level of the Ritual Monster you Ritual Summon. For the rest of this turn after this card resolves, you cannot Special Summon monsters from the Extra Deck. You can only activate 1 "Dogmatikalamity" per turn.
external_ids: local SETCARD_ID_0 = 0x145 --Dogmatika
  
lua: local SETCARD_ID_0 = 0x145 --Dogmatika
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1=Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1,id+EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c,e,tp)
  	return c:IsSetCard(SETCARD_ID_0)
  end
  function s.mfilter(c)
  	return c:GetLevel()>0 and c:IsType(TYPE_MONSTER) and c:IsAbleToGrave()
  end
  function s.rfilter2(c,e,tp,m1)
  	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return false end
  	if bit.band(c:GetType(),0x81)~=0x81 or not c:IsSetCard(SETCARD_ID_0)
  		or not c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_RITUAL,tp,false,true) then return false end
  	local mg=m1:Filter(Card.IsCanBeRitualMaterial,c,c)
  	if c.mat_filter then
  		mg=mg:Filter(c.mat_filter,c,tp)
  	end
  	return mg:IsExists(Card.IsLevel,1,nil,c:GetLevel())
  end
  function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
  	if chk==0 then
  		local mg1=Duel.GetRitualMaterial(tp)
  		local mg2=Duel.GetMatchingGroup(s.mfilter,tp,LOCATION_EXTRA,0,nil)
  		return Duel.IsExistingMatchingCard(s.rfilter2,tp,LOCATION_HAND,0,1,nil,e,tp,mg2)
  			or Duel.IsExistingMatchingCard(aux.RitualUltimateFilter,tp,LOCATION_HAND,0,1,nil,s.filter,e,tp,mg1,nil,Card.GetLevel,"Equal")
  	end
  	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_HAND)
  end
  function s.activate(e,tp,eg,ep,ev,re,r,rp)
  	local c=e:GetHandler()
  	::cancel::
  	local mg1=Duel.GetRitualMaterial(tp)
  	local mg2=Duel.GetMatchingGroup(s.mfilter,tp,LOCATION_EXTRA,0,nil)
  	local g1=Duel.GetMatchingGroup(aux.RitualUltimateFilter,tp,LOCATION_HAND,0,nil,s.filter,e,tp,mg1,nil,Card.GetLevel,"Equal")
  	local g2=Duel.GetMatchingGroup(s.rfilter2,tp,LOCATION_HAND,0,nil,e,tp,mg2)
  	local g=g1+g2
  	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
  	local tc=g:Select(tp,1,1,nil):GetFirst()
  	if tc then
  		local mg=mg1:Filter(Card.IsCanBeRitualMaterial,tc,tc)
  		if tc.mat_filter then
  			mg=mg:Filter(tc.mat_filter,tc,tp)
  		else
  			mg:RemoveCard(tc)
  		end
  		if g1:IsContains(tc) and (not g2:IsContains(tc) or not Duel.SelectYesNo(tp,aux.Stringid(id,0))) then
  			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
  			aux.GCheckAdditional=aux.RitualCheckAdditional(tc,tc:GetLevel(),"Equal")
  			local mat=mg:SelectSubGroup(tp,aux.RitualCheck,true,1,tc:GetLevel(),tp,tc,tc:GetLevel(),"Equal")
  			aux.GCheckAdditional=nil
  			if not mat then goto cancel end
  			tc:SetMaterial(mat)
  			Duel.ReleaseRitualMaterial(mat)
  		else
  			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
  			local matc=mg2:Filter(Card.IsLevel,nil,tc:GetLevel()):SelectUnselect(nil,tp,false,true,1,1)
  			if not matc then goto cancel end
  			local mat=Group.FromCards(matc)
  			tc:SetMaterial(mat)
  			Duel.SendtoGrave(mat,REASON_EFFECT+REASON_MATERIAL+REASON_RITUAL)
  		end
  		Duel.BreakEffect()
  		Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)
  		tc:CompleteProcedure()
  	end
  	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local e1=Effect.CreateEffect(c)
  		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
  end
  function s.splimit(e,c)
  	return c:IsLocation(LOCATION_EXTRA)
  end
  
  
source_text: Dogmatikalamity
  This card can be used to Ritual Summon any "Dogmatika" Ritual Monster. You must also Tribute monsters from your hand or field, whose total Levels exactly equal the Level of the Ritual Monster you Ritual Summon, or send 1 monster from your Extra Deck to the GY, whose Level equals the Level of the Ritual Monster you Ritual Summon. For the rest of this turn after this card resolves, you cannot Special Summon monsters from the Extra Deck. You can only activate 1 "Dogmatikalamity" per turn.
target_text: -- Dogmatikalamity
  local SETCARD_ID_0 = 0x145 --Dogmatika
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1=Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1,id+EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c,e,tp)
  	return c:IsSetCard(SETCARD_ID_0)
  end
  function s.mfilter(c)
  	return c:GetLevel()>0 and c:IsType(TYPE_MONSTER) and c:IsAbleToGrave()
  end
  function s.rfilter2(c,e,tp,m1)
  	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return false end
  	if bit.band(c:GetType(),0x81)~=0x81 or not c:IsSetCard(SETCARD_ID_0)
  		or not c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_RITUAL,tp,false,true) then return false end
  	local mg=m1:Filter(Card.IsCanBeRitualMaterial,c,c)
  	if c.mat_filter then
  		mg=mg:Filter(c.mat_filter,c,tp)
  	end
  	return mg:IsExists(Card.IsLevel,1,nil,c:GetLevel())
  end
  function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
  	if chk==0 then
  		local mg1=Duel.GetRitualMaterial(tp)
  		local mg2=Duel.GetMatchingGroup(s.mfilter,tp,LOCATION_EXTRA,0,nil)
  		return Duel.IsExistingMatchingCard(s.rfilter2,tp,LOCATION_HAND,0,1,nil,e,tp,mg2)
  			or Duel.IsExistingMatchingCard(aux.RitualUltimateFilter,tp,LOCATION_HAND,0,1,nil,s.filter,e,tp,mg1,nil,Card.GetLevel,"Equal")
  	end
  	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_HAND)
  end
  function s.activate(e,tp,eg,ep,ev,re,r,rp)
  	local c=e:GetHandler()
  	::cancel::
  	local mg1=Duel.GetRitualMaterial(tp)
  	local mg2=Duel.GetMatchingGroup(s.mfilter,tp,LOCATION_EXTRA,0,nil)
  	local g1=Duel.GetMatchingGroup(aux.RitualUltimateFilter,tp,LOCATION_HAND,0,nil,s.filter,e,tp,mg1,nil,Card.GetLevel,"Equal")
  	local g2=Duel.GetMatchingGroup(s.rfilter2,tp,LOCATION_HAND,0,nil,e,tp,mg2)
  	local g=g1+g2
  	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
  	local tc=g:Select(tp,1,1,nil):GetFirst()
  	if tc then
  		local mg=mg1:Filter(Card.IsCanBeRitualMaterial,tc,tc)
  		if tc.mat_filter then
  			mg=mg:Filter(tc.mat_filter,tc,tp)
  		else
  			mg:RemoveCard(tc)
  		end
  		if g1:IsContains(tc) and (not g2:IsContains(tc) or not Duel.SelectYesNo(tp,aux.Stringid(id,0))) then
  			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
  			aux.GCheckAdditional=aux.RitualCheckAdditional(tc,tc:GetLevel(),"Equal")
  			local mat=mg:SelectSubGroup(tp,aux.RitualCheck,true,1,tc:GetLevel(),tp,tc,tc:GetLevel(),"Equal")
  			aux.GCheckAdditional=nil
  			if not mat then goto cancel end
  			tc:SetMaterial(mat)
  			Duel.ReleaseRitualMaterial(mat)
  		else
  			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
  			local matc=mg2:Filter(Card.IsLevel,nil,tc:GetLevel()):SelectUnselect(nil,tp,false,true,1,1)
  			if not matc then goto cancel end
  			local mat=Group.FromCards(matc)
  			tc:SetMaterial(mat)
  			Duel.SendtoGrave(mat,REASON_EFFECT+REASON_MATERIAL+REASON_RITUAL)
  		end
  		Duel.BreakEffect()
  		Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)
  		tc:CompleteProcedure()
  	end
  	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local e1=Effect.CreateEffect(c)
  		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
  end
  function s.splimit(e,c)
  	return c:IsLocation(LOCATION_EXTRA)
  end
  
  