name: Megalith
card_text: You can Ritual Summon this card with a "Megalith" card. You can discard this card; Ritual Summon 1 "Megalith" Ritual Monster from your hand, by Tributing monsters from your hand or field whose total Levels equal or exceed its Level. If this card is Ritual Summoned: You can target cards your opponent controls, up to the number of Ritual Monsters with different names in your GY; destroy them. You can only use each effect of "Megalith Bethor" once per turn.
external_ids: local SETCARD_ID_0 = 0x138 --Megalith
  
lua: local SETCARD_ID_0 = 0x138 --Megalith
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--ritual summon
  	local e1 = aux.AddRitualProcGreater2(c, s.filter, nil, nil, s.matfilter, true)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetCode(0)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCost(s.rscost)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.descon)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.filter(c, e, tp, chk)
  	return c:IsSetCard(SETCARD_ID_0) and (not chk or c ~= e:GetHandler())
  end
  function s.matfilter(c, e, tp, chk)
  	return not chk or c ~= e:GetHandler()
  end
  function s.rscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsDiscardable()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST + REASON_DISCARD)
  end
  function s.cfilter(c)
  	return c:IsType(TYPE_RITUAL) and c:IsType(TYPE_MONSTER)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsControler(1 - tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, nil) and Duel.IsExistingMatchingCard(aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	local g = Duel.GetMatchingGroup(s.cfilter, tp, LOCATION_GRAVE, 0, nil)
  	local gc = g:GetClassCount(Card.GetCode)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local sg = Duel.SelectTarget(tp, aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, gc, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, sg, sg:GetCount(), 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	if tg:GetCount() > 0 then
  		Duel.Destroy(tg, REASON_EFFECT)
  	end
  end
  
  
source_text: Megalith
  You can Ritual Summon this card with a "Megalith" card. You can discard this card; Ritual Summon 1 "Megalith" Ritual Monster from your hand, by Tributing monsters from your hand or field whose total Levels equal or exceed its Level. If this card is Ritual Summoned: You can target cards your opponent controls, up to the number of Ritual Monsters with different names in your GY; destroy them. You can only use each effect of "Megalith Bethor" once per turn.
target_text: -- Megalith
  local SETCARD_ID_0 = 0x138 --Megalith
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--ritual summon
  	local e1 = aux.AddRitualProcGreater2(c, s.filter, nil, nil, s.matfilter, true)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetCode(0)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCost(s.rscost)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DESTROY)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.descon)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.filter(c, e, tp, chk)
  	return c:IsSetCard(SETCARD_ID_0) and (not chk or c ~= e:GetHandler())
  end
  function s.matfilter(c, e, tp, chk)
  	return not chk or c ~= e:GetHandler()
  end
  function s.rscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsDiscardable()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST + REASON_DISCARD)
  end
  function s.cfilter(c)
  	return c:IsType(TYPE_RITUAL) and c:IsType(TYPE_MONSTER)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsControler(1 - tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, nil) and Duel.IsExistingMatchingCard(aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	local g = Duel.GetMatchingGroup(s.cfilter, tp, LOCATION_GRAVE, 0, nil)
  	local gc = g:GetClassCount(Card.GetCode)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local sg = Duel.SelectTarget(tp, aux.TRUE, tp, 0, LOCATION_ONFIELD, 1, gc, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, sg, sg:GetCount(), 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	if tg:GetCount() > 0 then
  		Duel.Destroy(tg, REASON_EFFECT)
  	end
  end
  
  