name: Labrynth
card_text: Target 2 of your "Labrynth" Spells/Traps that are banished or in your GY, except "Labrynth Set-Up"; shuffle them into the Deck, then if you control a Fiend monster, you can Set non-"Labrynth" Normal Traps with different names directly from your Deck equal to the number shuffled. You can only activate 1 "Labrynth Set-Up" per turn.
external_ids: local SETCARD_ID_0 = 0x17e --Labrynth
  
lua: local SETCARD_ID_0 = 0x17e --Labrynth
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.tdfilter(c)
  	return (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup()) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsAbleToDeck()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED) and chkc:IsControler(tp) and s.tdfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.tdfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 2, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.tdfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 2, 2, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 2, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tg = g:Filter(Card.IsRelateToEffect, nil, e)
  	if tg:GetCount() > 0 then
  		Duel.SendtoDeck(tg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  		local ct = tg:FilterCount(Card.IsLocation, nil, LOCATION_DECK)
  		local sg = Duel.GetMatchingGroup(s.stfilter, tp, LOCATION_DECK, 0, nil)
  		local ft = Duel.GetLocationCount(tp, LOCATION_SZONE)
  		if ct > 0 and Duel.IsExistingMatchingCard(s.demfilter, tp, LOCATION_MZONE, 0, 1, nil) and sg:GetClassCount(Card.GetCode) >= ct and ft >= ct and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  			local stg = sg:SelectSubGroup(tp, aux.dncheck, false, ct, ct)
  			Duel.SSet(tp, stg)
  		end
  	end
  end
  function s.demfilter(c)
  	return c:IsRace(RACE_FIEND) and c:IsFaceup()
  end
  function s.stfilter(c)
  	return c:GetType() == TYPE_TRAP and not c:IsSetCard(SETCARD_ID_0) and c:IsSSetable()
  end
  
  
source_text: Labrynth
  Target 2 of your "Labrynth" Spells/Traps that are banished or in your GY, except "Labrynth Set-Up"; shuffle them into the Deck, then if you control a Fiend monster, you can Set non-"Labrynth" Normal Traps with different names directly from your Deck equal to the number shuffled. You can only activate 1 "Labrynth Set-Up" per turn.
target_text: -- Labrynth
  local SETCARD_ID_0 = 0x17e --Labrynth
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.tdfilter(c)
  	return (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup()) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsAbleToDeck()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE + LOCATION_REMOVED) and chkc:IsControler(tp) and s.tdfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.tdfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 2, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.tdfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, 0, 2, 2, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 2, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tg = g:Filter(Card.IsRelateToEffect, nil, e)
  	if tg:GetCount() > 0 then
  		Duel.SendtoDeck(tg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  		local ct = tg:FilterCount(Card.IsLocation, nil, LOCATION_DECK)
  		local sg = Duel.GetMatchingGroup(s.stfilter, tp, LOCATION_DECK, 0, nil)
  		local ft = Duel.GetLocationCount(tp, LOCATION_SZONE)
  		if ct > 0 and Duel.IsExistingMatchingCard(s.demfilter, tp, LOCATION_MZONE, 0, 1, nil) and sg:GetClassCount(Card.GetCode) >= ct and ft >= ct and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  			local stg = sg:SelectSubGroup(tp, aux.dncheck, false, ct, ct)
  			Duel.SSet(tp, stg)
  		end
  	end
  end
  function s.demfilter(c)
  	return c:IsRace(RACE_FIEND) and c:IsFaceup()
  end
  function s.stfilter(c)
  	return c:GetType() == TYPE_TRAP and not c:IsSetCard(SETCARD_ID_0) and c:IsSSetable()
  end
  
  