name: Fabled
card_text: If this card is in your hand: You can discard up to 2 monsters, except "Fabled Marcosia", but including another "Fabled" monster, and if you do, Special Summon this card. If Summoned this way, this card gains 200 ATK for each monster discarded. If this card is discarded to the GY: Add 1 "Fabled" Spell/Trap from your Deck to your hand. You can only use each effect of "Fabled Marcosia" once per turn.
external_ids: local SETCARD_ID_0 = 0x35 --Fabled
  
lua: local SETCARD_ID_0 = 0x35 --Fabled
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_HANDES)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.tg)
  	e1:SetOperation(s.op)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.dhfilter(c)
  	return not c:IsCode(id) and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsDiscardable()
  end
  function s.dhfilter1(c)
  	return not c:IsCode(id) and c:IsType(TYPE_MONSTER) and c:IsDiscardable()
  end
  function s.gselect(g)
  	return g:IsExists(s.dhfilter, 1, nil)
  end
  function s.tg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.dhfilter, tp, LOCATION_HAND, 0, 1, e:GetHandler())
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  end
  function s.op(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.dhfilter, tp, LOCATION_HAND, 0, e:GetHandler())
  	local hg = Duel.GetMatchingGroup(s.dhfilter1, tp, LOCATION_HAND, 0, e:GetHandler())
  	if #g < 1 then
  		return
  	end
  	if #g == 1 and #hg == 0 then
  		Duel.SendtoGrave(g, REASON_EFFECT + REASON_DISCARD)
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DISCARD)
  		local tg = hg:SelectSubGroup(tp, s.gselect, false, 1, 2)
  		Duel.SendtoGrave(tg, REASON_DISCARD + REASON_EFFECT)
  	end
  	local og, c = Duel.GetOperatedGroup(), e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummonStep(c, 0, tp, tp, false, false, POS_FACEUP) and #og > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetValue(#og * 200)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  		c:RegisterEffect(e1)
  	end
  	Duel.SpecialSummonComplete()
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_HAND) and bit.band(r, REASON_DISCARD) ~= 0
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if #g > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Fabled
  If this card is in your hand: You can discard up to 2 monsters, except "Fabled Marcosia", but including another "Fabled" monster, and if you do, Special Summon this card. If Summoned this way, this card gains 200 ATK for each monster discarded. If this card is discarded to the GY: Add 1 "Fabled" Spell/Trap from your Deck to your hand. You can only use each effect of "Fabled Marcosia" once per turn.
target_text: -- Fabled
  local SETCARD_ID_0 = 0x35 --Fabled
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_HANDES)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.tg)
  	e1:SetOperation(s.op)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.dhfilter(c)
  	return not c:IsCode(id) and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsDiscardable()
  end
  function s.dhfilter1(c)
  	return not c:IsCode(id) and c:IsType(TYPE_MONSTER) and c:IsDiscardable()
  end
  function s.gselect(g)
  	return g:IsExists(s.dhfilter, 1, nil)
  end
  function s.tg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.dhfilter, tp, LOCATION_HAND, 0, 1, e:GetHandler())
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  end
  function s.op(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.dhfilter, tp, LOCATION_HAND, 0, e:GetHandler())
  	local hg = Duel.GetMatchingGroup(s.dhfilter1, tp, LOCATION_HAND, 0, e:GetHandler())
  	if #g < 1 then
  		return
  	end
  	if #g == 1 and #hg == 0 then
  		Duel.SendtoGrave(g, REASON_EFFECT + REASON_DISCARD)
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DISCARD)
  		local tg = hg:SelectSubGroup(tp, s.gselect, false, 1, 2)
  		Duel.SendtoGrave(tg, REASON_DISCARD + REASON_EFFECT)
  	end
  	local og, c = Duel.GetOperatedGroup(), e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummonStep(c, 0, tp, tp, false, false, POS_FACEUP) and #og > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetValue(#og * 200)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  		c:RegisterEffect(e1)
  	end
  	Duel.SpecialSummonComplete()
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_HAND) and bit.band(r, REASON_DISCARD) ~= 0
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if #g > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  