name: Fateful
card_text: Once per turn, the first time a monster you control equipped with an Equip Spell would be destroyed by battle, it is not destroyed. You can only use each of the following effects of "Fateful Adventure" once per turn. During your Main Phase: You can add 1 monster that mentions "Adventurer Token" from your Deck to your hand, then send 1 card from your hand to the GY. If a monster(s) is Normal or Special Summoned: You can take 1 Equip Spell that mentions "Adventurer Token" from your Deck, and either add it to your hand or equip it to 1 "Adventurer Token" you control.
external_ids: local CARD_ID_1 = 3285552 --Adventurer Token
  
lua: local CARD_ID_1 = 3285552 --Adventurer Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e0)
  	--to hand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_HANDES)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--equip
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_EQUIP)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.eqtg)
  	e2:SetOperation(s.eqop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--indes
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetTargetRange(LOCATION_MZONE, 0)
  	e4:SetTarget(s.indtg)
  	e4:SetCountLimit(1)
  	e4:SetValue(s.indct)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return aux.IsCodeListed(c, CARD_ID_1) and c:IsAbleToHand() and c:IsType(TYPE_MONSTER)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.ShuffleHand(tp)
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT)
  	end
  end
  function s.eqfilter(c, equip, tp)
  	return c:IsType(TYPE_EQUIP) and aux.IsCodeListed(c, CARD_ID_1) and (c:IsAbleToHand() or equip and c:CheckUniqueOnField(tp) and not c:IsForbidden())
  end
  function s.cfilter(c)
  	return c:IsCode(CARD_ID_1) and c:IsFaceup()
  end
  function s.eqtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local equip = Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  		return Duel.IsExistingMatchingCard(s.eqfilter, tp, LOCATION_DECK, 0, 1, nil, equip, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, 0)
  end
  function s.eqop(e, tp, eg, ep, ev, re, r, rp)
  	local equip = Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, s.eqfilter, tp, LOCATION_DECK, 0, 1, 1, nil, equip, tp)
  	local tc = g:GetFirst()
  	if g:GetCount() > 0 then
  		if equip and tc:CheckUniqueOnField(tp) and not tc:IsForbidden() and (not tc:IsAbleToHand() or Duel.SelectOption(tp, 1190, aux.Stringid(id, 2)) == 1) then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  			local sc = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  			Duel.Equip(tp, tc, sc:GetFirst())
  		else
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  		end
  	end
  end
  function s.indtg(e, c)
  	return c:GetEquipCount() > 0
  end
  function s.indct(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  
  
source_text: Fateful
  Once per turn, the first time a monster you control equipped with an Equip Spell would be destroyed by battle, it is not destroyed. You can only use each of the following effects of "Fateful Adventure" once per turn. During your Main Phase: You can add 1 monster that mentions "Adventurer Token" from your Deck to your hand, then send 1 card from your hand to the GY. If a monster(s) is Normal or Special Summoned: You can take 1 Equip Spell that mentions "Adventurer Token" from your Deck, and either add it to your hand or equip it to 1 "Adventurer Token" you control.
target_text: -- Fateful
  local CARD_ID_1 = 3285552 --Adventurer Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e0)
  	--to hand
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_HANDES)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_SZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.thtg)
  	e1:SetOperation(s.thop)
  	c:RegisterEffect(e1)
  	--equip
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_EQUIP)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SUMMON_SUCCESS)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.eqtg)
  	e2:SetOperation(s.eqop)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	--indes
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetTargetRange(LOCATION_MZONE, 0)
  	e4:SetTarget(s.indtg)
  	e4:SetCountLimit(1)
  	e4:SetValue(s.indct)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return aux.IsCodeListed(c, CARD_ID_1) and c:IsAbleToHand() and c:IsType(TYPE_MONSTER)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  		Duel.ShuffleHand(tp)
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT)
  	end
  end
  function s.eqfilter(c, equip, tp)
  	return c:IsType(TYPE_EQUIP) and aux.IsCodeListed(c, CARD_ID_1) and (c:IsAbleToHand() or equip and c:CheckUniqueOnField(tp) and not c:IsForbidden())
  end
  function s.cfilter(c)
  	return c:IsCode(CARD_ID_1) and c:IsFaceup()
  end
  function s.eqtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local equip = Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  		return Duel.IsExistingMatchingCard(s.eqfilter, tp, LOCATION_DECK, 0, 1, nil, equip, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, 0)
  end
  function s.eqop(e, tp, eg, ep, ev, re, r, rp)
  	local equip = Duel.GetLocationCount(tp, LOCATION_SZONE) > 0 and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, s.eqfilter, tp, LOCATION_DECK, 0, 1, 1, nil, equip, tp)
  	local tc = g:GetFirst()
  	if g:GetCount() > 0 then
  		if equip and tc:CheckUniqueOnField(tp) and not tc:IsForbidden() and (not tc:IsAbleToHand() or Duel.SelectOption(tp, 1190, aux.Stringid(id, 2)) == 1) then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  			local sc = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  			Duel.Equip(tp, tc, sc:GetFirst())
  		else
  			Duel.SendtoHand(g, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, g)
  		end
  	end
  end
  function s.indtg(e, c)
  	return c:GetEquipCount() > 0
  end
  function s.indct(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  
  