name: Legion
card_text: During your Main Phase, you can Tribute Summon 1 Spellcaster monster in Attack Position, in addition to your Normal Summon/Set. (You can only gain this effect once per turn.) If this card is sent from the field to the GY: You can add 1 Spellcaster Normal Monster from your Deck or GY to your hand. You can only use this effect of "Legion the Fiend Jester" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--extra summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 1))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_EXTRA_SUMMON_COUNT)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetTargetRange(LOCATION_HAND, 0)
  	e1:SetTarget(aux.TargetBoolFunction(Card.IsRace, RACE_SPELLCASTER))
  	e1:SetValue(0x1)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.filter(c)
  	return c:IsType(TYPE_NORMAL) and c:IsRace(RACE_SPELLCASTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Legion
  During your Main Phase, you can Tribute Summon 1 Spellcaster monster in Attack Position, in addition to your Normal Summon/Set. (You can only gain this effect once per turn.) If this card is sent from the field to the GY: You can add 1 Spellcaster Normal Monster from your Deck or GY to your hand. You can only use this effect of "Legion the Fiend Jester" once per turn.
target_text: -- Legion
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--extra summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 1))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_EXTRA_SUMMON_COUNT)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetTargetRange(LOCATION_HAND, 0)
  	e1:SetTarget(aux.TargetBoolFunction(Card.IsRace, RACE_SPELLCASTER))
  	e1:SetValue(0x1)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.filter(c)
  	return c:IsType(TYPE_NORMAL) and c:IsRace(RACE_SPELLCASTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  