name: Trial
card_text: You can only activate 1 "Trial and Tribulation" per turn. During the End Phase of the turn this card was activated, apply this effect, depending on the number of monsters you Tributed from the field and from your hand this turn, except Tokens.
  ● 1: Draw 1 card.
  ● 2: Add 2 monsters from your Graveyard to your hand.
  ● 3+: Destroy up to 3 face-up cards on the field.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_DRAW)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  		ge1:SetCode(EVENT_RELEASE)
  		ge1:SetOperation(s.addcount)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.addcount(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	while tc do
  		if tc:IsType(TYPE_MONSTER) and not tc:IsType(TYPE_TOKEN) then
  			local p = tc:GetReasonPlayer()
  			Duel.RegisterFlagEffect(p, id + 1, RESET_PHASE + PHASE_END, 0, 1)
  		end
  		tc = eg:GetNext()
  	end
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:IsHasType(EFFECT_TYPE_ACTIVATE)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.effectcon)
  	e1:SetOperation(s.effectop)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.effectcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id + 1) > 0
  end
  function s.filter1(c)
  	return c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.filter2(c)
  	return c:IsFaceup()
  end
  function s.effectop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_CARD, 0, id)
  	local ct = Duel.GetFlagEffect(tp, id + 1)
  	if ct == 1 then
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	elseif ct == 2 then
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.filter1), tp, LOCATION_GRAVE, 0, nil)
  		if g:GetCount() > 1 then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  			local tg = g:Select(tp, 2, 2, nil)
  			Duel.SendtoHand(tg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tg)
  		end
  	else
  		local g = Duel.GetMatchingGroup(s.filter2, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  		if g:GetCount() > 0 then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  			local tg = g:Select(tp, 1, 3, nil)
  			Duel.Destroy(tg, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Trial
  You can only activate 1 "Trial and Tribulation" per turn. During the End Phase of the turn this card was activated, apply this effect, depending on the number of monsters you Tributed from the field and from your hand this turn, except Tokens.
  ● 1: Draw 1 card.
  ● 2: Add 2 monsters from your Graveyard to your hand.
  ● 3+: Destroy up to 3 face-up cards on the field.
target_text: -- Trial
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_DRAW)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  		ge1:SetCode(EVENT_RELEASE)
  		ge1:SetOperation(s.addcount)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.addcount(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	while tc do
  		if tc:IsType(TYPE_MONSTER) and not tc:IsType(TYPE_TOKEN) then
  			local p = tc:GetReasonPlayer()
  			Duel.RegisterFlagEffect(p, id + 1, RESET_PHASE + PHASE_END, 0, 1)
  		end
  		tc = eg:GetNext()
  	end
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:IsHasType(EFFECT_TYPE_ACTIVATE)
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.effectcon)
  	e1:SetOperation(s.effectop)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.effectcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id + 1) > 0
  end
  function s.filter1(c)
  	return c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.filter2(c)
  	return c:IsFaceup()
  end
  function s.effectop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_CARD, 0, id)
  	local ct = Duel.GetFlagEffect(tp, id + 1)
  	if ct == 1 then
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	elseif ct == 2 then
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.filter1), tp, LOCATION_GRAVE, 0, nil)
  		if g:GetCount() > 1 then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  			local tg = g:Select(tp, 2, 2, nil)
  			Duel.SendtoHand(tg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tg)
  		end
  	else
  		local g = Duel.GetMatchingGroup(s.filter2, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  		if g:GetCount() > 0 then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  			local tg = g:Select(tp, 1, 3, nil)
  			Duel.Destroy(tg, REASON_EFFECT)
  		end
  	end
  end
  
  