name: Labrynth
card_text: (Quick Effect): You can discard this card; this turn, you can activate 1 Normal Trap Card that was Set this turn, if you control a "Labrynth" monster. If a card(s) is sent from your hand to the GY to activate your Normal Trap Card or your "Labrynth" card or effect, except "Labrynth Cooclock", while this card is in your GY (except during the Damage Step): You can add this card to your hand, or Special Summon it. You can only use each effect of "Labrynth Cooclock" once 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)
  	--Trap activate in set turn
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCost(s.cost)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--to hand or spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOHAND + CATEGORY_GRAVE_ACTION + CATEGORY_GRAVE_SPSUMMON)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, 2512)
  	e2:SetCondition(s.tscon)
  	e2:SetTarget(s.tstg)
  	e2:SetOperation(s.tsop)
  	c:RegisterEffect(e2)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsDiscardable()
  	end
  	Duel.SendtoGrave(c, REASON_COST + REASON_DISCARD)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsFaceup()
  end
  function s.actcon(e)
  	return Duel.IsExistingMatchingCard(s.filter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.acttg(e, c)
  	return c:GetType() == TYPE_TRAP
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetDescription(aux.Stringid(id, 2))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_TRAP_ACT_IN_SET_TURN)
  	e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
  	e1:SetTargetRange(LOCATION_SZONE, 0)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.actcon)
  	e1:SetTarget(s.acttg)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.tscon(e, tp, eg, ep, ev, re, r, rp)
  	if not re then
  		return false
  	end
  	local rc = re:GetHandler()
  	return rp == tp
  		and r & REASON_COST > 0
  		and (rc:IsSetCard(SETCARD_ID_0) and not rc:IsCode(id) or re:IsHasType(EFFECT_TYPE_ACTIVATE) and rc:GetType() == TYPE_TRAP)
  		and eg:IsExists(Card.IsPreviousLocation, 1, nil, LOCATION_HAND)
  		and not eg:IsContains(e:GetHandler())
  end
  function s.tstg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand() or (Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false))
  	end
  end
  function s.tsop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) then
  		return
  	end
  	if aux.NecroValleyNegateCheck(c) then
  		return
  	end
  	if not aux.NecroValleyFilter()(c) then
  		return
  	end
  	local b1 = c:IsAbleToHand()
  	local b2 = Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	local op = aux.SelectFromOptions(tp, { b1, 1190 }, { b2, 1152 })
  	if op == 1 then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  	if op == 2 then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Labrynth
  (Quick Effect): You can discard this card; this turn, you can activate 1 Normal Trap Card that was Set this turn, if you control a "Labrynth" monster. If a card(s) is sent from your hand to the GY to activate your Normal Trap Card or your "Labrynth" card or effect, except "Labrynth Cooclock", while this card is in your GY (except during the Damage Step): You can add this card to your hand, or Special Summon it. You can only use each effect of "Labrynth Cooclock" once per turn.
target_text: -- Labrynth
  local SETCARD_ID_0 = 0x17e --Labrynth
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Trap activate in set turn
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCost(s.cost)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--to hand or spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOHAND + CATEGORY_GRAVE_ACTION + CATEGORY_GRAVE_SPSUMMON)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCountLimit(1, 2512)
  	e2:SetCondition(s.tscon)
  	e2:SetTarget(s.tstg)
  	e2:SetOperation(s.tsop)
  	c:RegisterEffect(e2)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsDiscardable()
  	end
  	Duel.SendtoGrave(c, REASON_COST + REASON_DISCARD)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsFaceup()
  end
  function s.actcon(e)
  	return Duel.IsExistingMatchingCard(s.filter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.acttg(e, c)
  	return c:GetType() == TYPE_TRAP
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetDescription(aux.Stringid(id, 2))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_TRAP_ACT_IN_SET_TURN)
  	e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
  	e1:SetTargetRange(LOCATION_SZONE, 0)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.actcon)
  	e1:SetTarget(s.acttg)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.tscon(e, tp, eg, ep, ev, re, r, rp)
  	if not re then
  		return false
  	end
  	local rc = re:GetHandler()
  	return rp == tp
  		and r & REASON_COST > 0
  		and (rc:IsSetCard(SETCARD_ID_0) and not rc:IsCode(id) or re:IsHasType(EFFECT_TYPE_ACTIVATE) and rc:GetType() == TYPE_TRAP)
  		and eg:IsExists(Card.IsPreviousLocation, 1, nil, LOCATION_HAND)
  		and not eg:IsContains(e:GetHandler())
  end
  function s.tstg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand() or (Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false))
  	end
  end
  function s.tsop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) then
  		return
  	end
  	if aux.NecroValleyNegateCheck(c) then
  		return
  	end
  	if not aux.NecroValleyFilter()(c) then
  		return
  	end
  	local b1 = c:IsAbleToHand()
  	local b2 = Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	local op = aux.SelectFromOptions(tp, { b1, 1190 }, { b2, 1152 })
  	if op == 1 then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  	if op == 2 then
  		Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  