name: Ogdoadic
card_text: If a face-up Reptile monster(s) you control is destroyed by battle or an opponent's card effect: You can target 1 card your opponent controls; send it to the GY. If this card in the Field Zone is destroyed by your opponent's card effect: You can send cards from the top of your opponent's Deck to the GY, equal to the number of Reptile monsters with different names in your GY. You can only use each effect of "Ogdoadic Origin" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--to grave
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOGRAVE)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.tgcon1)
  	e2:SetTarget(s.tgtg1)
  	e2:SetOperation(s.tgop1)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DECKDES)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.tgcon2)
  	e3:SetTarget(s.tgtg2)
  	e3:SetOperation(s.tgop2)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:GetPreviousRaceOnField() & RACE_REPTILE ~= 0
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.tgcon1(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.tgtg1(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(1 - tp) and chkc:IsOnField() and chkc:IsAbleToGrave()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsAbleToGrave, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToGrave, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, g, 1, 0, 0)
  end
  function s.tgop1(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SendtoGrave(tc, REASON_EFFECT)
  	end
  end
  function s.tgcon2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return rp == 1 - tp and c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_FZONE) and c:IsPreviousControler(tp)
  end
  function s.tgtg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ct = Duel.GetMatchingGroup(Card.IsRace, tp, LOCATION_GRAVE, 0, nil, RACE_REPTILE):GetClassCount(Card.GetCode)
  	if chk == 0 then
  		return ct > 0 and Duel.IsPlayerCanDiscardDeck(1 - tp, ct)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, 1 - tp, ct)
  end
  function s.tgop2(e, tp, eg, ep, ev, re, r, rp)
  	local ct = Duel.GetMatchingGroup(Card.IsRace, tp, LOCATION_GRAVE, 0, nil, RACE_REPTILE):GetClassCount(Card.GetCode)
  	Duel.DiscardDeck(1 - tp, ct, REASON_EFFECT)
  end
  
  
source_text: Ogdoadic
  If a face-up Reptile monster(s) you control is destroyed by battle or an opponent's card effect: You can target 1 card your opponent controls; send it to the GY. If this card in the Field Zone is destroyed by your opponent's card effect: You can send cards from the top of your opponent's Deck to the GY, equal to the number of Reptile monsters with different names in your GY. You can only use each effect of "Ogdoadic Origin" once per turn.
target_text: -- Ogdoadic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--to grave
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOGRAVE)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCode(EVENT_DESTROYED)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.tgcon1)
  	e2:SetTarget(s.tgtg1)
  	e2:SetOperation(s.tgop1)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DECKDES)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.tgcon2)
  	e3:SetTarget(s.tgtg2)
  	e3:SetOperation(s.tgop2)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:GetPreviousRaceOnField() & RACE_REPTILE ~= 0
  		and (c:IsReason(REASON_BATTLE) or c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp)
  end
  function s.tgcon1(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.tgtg1(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(1 - tp) and chkc:IsOnField() and chkc:IsAbleToGrave()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsAbleToGrave, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToGrave, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, g, 1, 0, 0)
  end
  function s.tgop1(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SendtoGrave(tc, REASON_EFFECT)
  	end
  end
  function s.tgcon2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return rp == 1 - tp and c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_FZONE) and c:IsPreviousControler(tp)
  end
  function s.tgtg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ct = Duel.GetMatchingGroup(Card.IsRace, tp, LOCATION_GRAVE, 0, nil, RACE_REPTILE):GetClassCount(Card.GetCode)
  	if chk == 0 then
  		return ct > 0 and Duel.IsPlayerCanDiscardDeck(1 - tp, ct)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, 1 - tp, ct)
  end
  function s.tgop2(e, tp, eg, ep, ev, re, r, rp)
  	local ct = Duel.GetMatchingGroup(Card.IsRace, tp, LOCATION_GRAVE, 0, nil, RACE_REPTILE):GetClassCount(Card.GetCode)
  	Duel.DiscardDeck(1 - tp, ct, REASON_EFFECT)
  end
  
  