name: Draw
card_text: If your LP are lower than your opponent's and your opponent controls a monster with the highest ATK on the field (even if it's tied): Reveal 3 cards with different names from your Deck, place them on top of your Deck in random order, then draw 1 card. For the rest of this turn after this card resolves, you cannot Set Spells/Traps, also you can only activate one more card or effect. You can only activate 1 "Draw of Fate" per turn.
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:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if g:GetCount() == 0 then
  		return false
  	end
  	local tg = g:GetMaxGroup(Card.GetAttack)
  	return tg:IsExists(Card.IsControler, 1, nil, 1 - tp) and Duel.GetLP(tp) < Duel.GetLP(1 - tp)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		if not Duel.IsPlayerCanDraw(tp, 1) then
  			return false
  		end
  		local g = Duel.GetMatchingGroup(nil, tp, LOCATION_DECK, 0, nil)
  		return g:GetClassCount(Card.GetCode) >= 3
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = Duel.GetMatchingGroup(nil, tp, LOCATION_DECK, 0, nil)
  	if g:GetClassCount(Card.GetCode) >= 3 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CONFIRM)
  		local sg = g:SelectSubGroup(tp, aux.dncheck, false, 3, 3)
  		Duel.ConfirmCards(1 - tp, sg)
  		if Duel.ShuffleDeck(tp) ~= 0 then
  			for i = 1, 3 do
  				local tc
  				if i < 3 then
  					tc = sg:RandomSelect(tp, 1):GetFirst()
  				else
  					tc = sg:GetFirst()
  				end
  				Duel.MoveSequence(tc, SEQ_DECKTOP)
  				sg:RemoveCard(tc)
  			end
  		end
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  	if not e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		return
  	end
  	--cannot set spell/trap card
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetCode(EFFECT_CANNOT_SSET)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetTargetRange(1, 0)
  	Duel.RegisterEffect(e1, tp)
  	Duel.AddCustomActivityCounter(id, ACTIVITY_CHAIN, s.chainfilter)
  	--activate limit
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e3:SetCode(EFFECT_CANNOT_ACTIVATE)
  	e3:SetTargetRange(1, 0)
  	e3:SetCondition(s.actcon)
  	e3:SetValue(1)
  	e3:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e3, tp)
  end
  function s.chainfilter(re, tp, cid)
  	return false
  end
  function s.actcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetCustomActivityCount(id, tp, ACTIVITY_CHAIN) ~= 0
  end
  
  
source_text: Draw
  If your LP are lower than your opponent's and your opponent controls a monster with the highest ATK on the field (even if it's tied): Reveal 3 cards with different names from your Deck, place them on top of your Deck in random order, then draw 1 card. For the rest of this turn after this card resolves, you cannot Set Spells/Traps, also you can only activate one more card or effect. You can only activate 1 "Draw of Fate" per turn.
target_text: -- Draw
  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:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, LOCATION_MZONE, LOCATION_MZONE, nil)
  	if g:GetCount() == 0 then
  		return false
  	end
  	local tg = g:GetMaxGroup(Card.GetAttack)
  	return tg:IsExists(Card.IsControler, 1, nil, 1 - tp) and Duel.GetLP(tp) < Duel.GetLP(1 - tp)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		if not Duel.IsPlayerCanDraw(tp, 1) then
  			return false
  		end
  		local g = Duel.GetMatchingGroup(nil, tp, LOCATION_DECK, 0, nil)
  		return g:GetClassCount(Card.GetCode) >= 3
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local g = Duel.GetMatchingGroup(nil, tp, LOCATION_DECK, 0, nil)
  	if g:GetClassCount(Card.GetCode) >= 3 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_CONFIRM)
  		local sg = g:SelectSubGroup(tp, aux.dncheck, false, 3, 3)
  		Duel.ConfirmCards(1 - tp, sg)
  		if Duel.ShuffleDeck(tp) ~= 0 then
  			for i = 1, 3 do
  				local tc
  				if i < 3 then
  					tc = sg:RandomSelect(tp, 1):GetFirst()
  				else
  					tc = sg:GetFirst()
  				end
  				Duel.MoveSequence(tc, SEQ_DECKTOP)
  				sg:RemoveCard(tc)
  			end
  		end
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  	if not e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		return
  	end
  	--cannot set spell/trap card
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetCode(EFFECT_CANNOT_SSET)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetTargetRange(1, 0)
  	Duel.RegisterEffect(e1, tp)
  	Duel.AddCustomActivityCounter(id, ACTIVITY_CHAIN, s.chainfilter)
  	--activate limit
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e3:SetCode(EFFECT_CANNOT_ACTIVATE)
  	e3:SetTargetRange(1, 0)
  	e3:SetCondition(s.actcon)
  	e3:SetValue(1)
  	e3:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e3, tp)
  end
  function s.chainfilter(re, tp, cid)
  	return false
  end
  function s.actcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetCustomActivityCount(id, tp, ACTIVITY_CHAIN) ~= 0
  end
  
  