name: Ritual
card_text: Monsters your opponent controls lose 200 ATK for each different Type among the "Ritual Beast" monsters you control. You can only use each of the following effects of "Ritual Beast Inheritance" once per turn. You can reveal 1 "Ritual Beast" monster in your hand; add 1 "Ritual Beast" monster with a different Type from your Deck to your hand, then discard 1 card. If 2 or more monsters are Special Summoned to your field at the same time (except during the Damage Step): You can target 1 monster on the field; change its battle position.
external_ids: local SETCARD_ID_0 = 0xb5 --Ritual
  
lua: local SETCARD_ID_0 = 0xb5 --Ritual
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e0)
  	--atk down
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetTargetRange(0, LOCATION_MZONE)
  	e1:SetValue(s.val)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_HANDES)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.thcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--change position
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_POSITION)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e3:SetCountLimit(1, id + o)
  	e3:SetCondition(s.pcon)
  	e3:SetTarget(s.ptg)
  	e3:SetOperation(s.pop)
  	c:RegisterEffect(e3)
  end
  function s.atkfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsFaceup()
  end
  function s.val(e, c)
  	local tp = e:GetHandlerPlayer()
  	local g = Duel.GetMatchingGroup(s.atkfilter, tp, LOCATION_MZONE, 0, nil)
  	return g:GetClassCount(Card.GetRace) * -200
  end
  function s.filter(c, tp)
  	return c:IsControler(tp)
  end
  function s.costfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsPublic() and Duel.IsExistingMatchingCard(aux.NOT(Card.IsRace), c:GetControler(), LOCATION_DECK, 0, 1, nil, c:GetRace())
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CONFIRM)
  	local g = Duel.SelectMatchingCard(tp, s.costfilter, tp, LOCATION_HAND, 0, 1, 1, nil)
  	Duel.ConfirmCards(1 - tp, g)
  	Duel.ShuffleHand(tp)
  	e:SetLabel(g:GetFirst():GetRace())
  end
  function s.thfilter(c, race)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsRace(race) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local race = e:GetLabel()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, race)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local race = e:GetLabel()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, race)
  	if g:GetCount() > 0 and Duel.SendtoHand(g, nil, REASON_EFFECT) > 0 and g:GetFirst():IsLocation(LOCATION_HAND) then
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.ShuffleHand(tp)
  		Duel.BreakEffect()
  		Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT + REASON_DISCARD)
  	end
  end
  function s.pcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.filter, 2, nil, tp)
  end
  function s.pfilter(c)
  	return c:IsCanChangePosition()
  end
  function s.ptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.pfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.pfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  	local g = Duel.SelectTarget(tp, s.pfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, g, 1, 0, 0)
  end
  function s.pop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.ChangePosition(tc, POS_FACEUP_DEFENSE, POS_FACEUP_ATTACK, POS_FACEUP_ATTACK, POS_FACEUP_ATTACK)
  	end
  end
  
  
source_text: Ritual
  Monsters your opponent controls lose 200 ATK for each different Type among the "Ritual Beast" monsters you control. You can only use each of the following effects of "Ritual Beast Inheritance" once per turn. You can reveal 1 "Ritual Beast" monster in your hand; add 1 "Ritual Beast" monster with a different Type from your Deck to your hand, then discard 1 card. If 2 or more monsters are Special Summoned to your field at the same time (except during the Damage Step): You can target 1 monster on the field; change its battle position.
target_text: -- Ritual
  local SETCARD_ID_0 = 0xb5 --Ritual
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e0)
  	--atk down
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_UPDATE_ATTACK)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetTargetRange(0, LOCATION_MZONE)
  	e1:SetValue(s.val)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_HANDES)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.thcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--change position
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_POSITION)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e3:SetCountLimit(1, id + o)
  	e3:SetCondition(s.pcon)
  	e3:SetTarget(s.ptg)
  	e3:SetOperation(s.pop)
  	c:RegisterEffect(e3)
  end
  function s.atkfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsFaceup()
  end
  function s.val(e, c)
  	local tp = e:GetHandlerPlayer()
  	local g = Duel.GetMatchingGroup(s.atkfilter, tp, LOCATION_MZONE, 0, nil)
  	return g:GetClassCount(Card.GetRace) * -200
  end
  function s.filter(c, tp)
  	return c:IsControler(tp)
  end
  function s.costfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsPublic() and Duel.IsExistingMatchingCard(aux.NOT(Card.IsRace), c:GetControler(), LOCATION_DECK, 0, 1, nil, c:GetRace())
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CONFIRM)
  	local g = Duel.SelectMatchingCard(tp, s.costfilter, tp, LOCATION_HAND, 0, 1, 1, nil)
  	Duel.ConfirmCards(1 - tp, g)
  	Duel.ShuffleHand(tp)
  	e:SetLabel(g:GetFirst():GetRace())
  end
  function s.thfilter(c, race)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and not c:IsRace(race) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local race = e:GetLabel()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, race)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local race = e:GetLabel()
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, race)
  	if g:GetCount() > 0 and Duel.SendtoHand(g, nil, REASON_EFFECT) > 0 and g:GetFirst():IsLocation(LOCATION_HAND) then
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.ShuffleHand(tp)
  		Duel.BreakEffect()
  		Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT + REASON_DISCARD)
  	end
  end
  function s.pcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.filter, 2, nil, tp)
  end
  function s.pfilter(c)
  	return c:IsCanChangePosition()
  end
  function s.ptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.pfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.pfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  	local g = Duel.SelectTarget(tp, s.pfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, g, 1, 0, 0)
  end
  function s.pop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.ChangePosition(tc, POS_FACEUP_DEFENSE, POS_FACEUP_ATTACK, POS_FACEUP_ATTACK, POS_FACEUP_ATTACK)
  	end
  end
  
  