name: Dual
card_text: Discard 1 card, and if you do, Special Summon as many "Dual Avatar Spirit Tokens" as possible (Warrior/LIGHT/Level 2/ATK 0/DEF 0), also for the rest of this turn you cannot Special Summon monsters from the Extra Deck, except Fusion Monsters, and all Tokens you control cannot be Tributed and are destroyed during the End Phase. After Summoning these Token(s), you can apply this effect up to twice.
  ● Fusion Summon 1 "Dual Avatar" Fusion Monster from your Extra Deck, using monsters from your hand or field as Fusion Material.
  You can only activate 1 "Dual Avatar Invitation" per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 87669905 --Dual Avatar Spirit Token
  local SETCARD_ID_0 = 0x14f --Dual
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 87669905 --Dual Avatar Spirit Token
  local SETCARD_ID_0 = 0x14f --Dual
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN + CATEGORY_HANDES + CATEGORY_FUSION_SUMMON)
  	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)
  end
  
  function s.fusfilter(c)
  	return c:IsSetCard(SETCARD_ID_0)
  end
  
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, e:GetHandler(), REASON_EFFECT)
  			and ft > 0
  			and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, 0, 0, 2, RACE_WARRIOR, ATTRIBUTE_LIGHT)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, ft, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, ft, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local can_fusion = false
  	if Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_EFFECT + REASON_DISCARD, nil, REASON_EFFECT) ~= 0 then
  		local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  		if ft > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, 0, 0, 2, RACE_WARRIOR, ATTRIBUTE_LIGHT) then
  			if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  				ft = 1
  			end
  			for i = 1, ft do
  				local token = Duel.CreateToken(tp, id + 1)
  				if Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP) then
  					can_fusion = true
  				end
  			end
  			Duel.SpecialSummonComplete()
  		end
  	end
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetCountLimit(1)
  	e1:SetOperation(s.desop)
  	Duel.RegisterEffect(e1, tp)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_UNRELEASABLE_SUM)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsType, TYPE_TOKEN))
  	e2:SetTargetRange(LOCATION_ONFIELD, 0)
  	e2:SetValue(1)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e2, tp)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_UNRELEASABLE_NONSUM)
  	Duel.RegisterEffect(e3, tp)
  	local e4 = Effect.CreateEffect(e:GetHandler())
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e4:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e4:SetTargetRange(1, 0)
  	e4:SetTarget(s.splimit)
  	e4:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e4, tp)
  	if can_fusion == true then
  		local fusion_effect = FusionSpell.CreateSummonEffect(c, {
  			fusfilter = s.fusfilter,
  			matfilter = aux.NecroValleyFilter(),
  		})
  		for i = 1, 2 do
  			if fusion_effect:GetTarget()(e, tp, eg, ep, ev, re, r, rp, 0) == false then
  				break
  			end
  			if Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  				Duel.BreakEffect()
  				fusion_effect:GetOperation()(e, tp, eg, ep, ev, re, r, rp)
  			else
  				break
  			end
  		end
  	end
  end
  
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(Card.IsType, tp, LOCATION_MZONE, 0, nil, TYPE_TOKEN)
  	Duel.Destroy(tg, REASON_EFFECT)
  end
  
  function s.splimit(e, c)
  	return not c:IsType(TYPE_FUSION) and c:IsLocation(LOCATION_EXTRA)
  end
  
  
source_text: Dual
  Discard 1 card, and if you do, Special Summon as many "Dual Avatar Spirit Tokens" as possible (Warrior/LIGHT/Level 2/ATK 0/DEF 0), also for the rest of this turn you cannot Special Summon monsters from the Extra Deck, except Fusion Monsters, and all Tokens you control cannot be Tributed and are destroyed during the End Phase. After Summoning these Token(s), you can apply this effect up to twice.
  ● Fusion Summon 1 "Dual Avatar" Fusion Monster from your Extra Deck, using monsters from your hand or field as Fusion Material.
  You can only activate 1 "Dual Avatar Invitation" per turn.
target_text: -- Dual
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 87669905 --Dual Avatar Spirit Token
  local SETCARD_ID_0 = 0x14f --Dual
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN + CATEGORY_HANDES + CATEGORY_FUSION_SUMMON)
  	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)
  end
  
  function s.fusfilter(c)
  	return c:IsSetCard(SETCARD_ID_0)
  end
  
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, e:GetHandler(), REASON_EFFECT)
  			and ft > 0
  			and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, 0, 0, 2, RACE_WARRIOR, ATTRIBUTE_LIGHT)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, ft, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, ft, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local can_fusion = false
  	if Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_EFFECT + REASON_DISCARD, nil, REASON_EFFECT) ~= 0 then
  		local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  		if ft > 0 and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, 0, 0, 2, RACE_WARRIOR, ATTRIBUTE_LIGHT) then
  			if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  				ft = 1
  			end
  			for i = 1, ft do
  				local token = Duel.CreateToken(tp, id + 1)
  				if Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP) then
  					can_fusion = true
  				end
  			end
  			Duel.SpecialSummonComplete()
  		end
  	end
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_PHASE + PHASE_END)
  	e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	e1:SetCountLimit(1)
  	e1:SetOperation(s.desop)
  	Duel.RegisterEffect(e1, tp)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_UNRELEASABLE_SUM)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsType, TYPE_TOKEN))
  	e2:SetTargetRange(LOCATION_ONFIELD, 0)
  	e2:SetValue(1)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e2, tp)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_UNRELEASABLE_NONSUM)
  	Duel.RegisterEffect(e3, tp)
  	local e4 = Effect.CreateEffect(e:GetHandler())
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e4:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  	e4:SetTargetRange(1, 0)
  	e4:SetTarget(s.splimit)
  	e4:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e4, tp)
  	if can_fusion == true then
  		local fusion_effect = FusionSpell.CreateSummonEffect(c, {
  			fusfilter = s.fusfilter,
  			matfilter = aux.NecroValleyFilter(),
  		})
  		for i = 1, 2 do
  			if fusion_effect:GetTarget()(e, tp, eg, ep, ev, re, r, rp, 0) == false then
  				break
  			end
  			if Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  				Duel.BreakEffect()
  				fusion_effect:GetOperation()(e, tp, eg, ep, ev, re, r, rp)
  			else
  				break
  			end
  		end
  	end
  end
  
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(Card.IsType, tp, LOCATION_MZONE, 0, nil, TYPE_TOKEN)
  	Duel.Destroy(tg, REASON_EFFECT)
  end
  
  function s.splimit(e, c)
  	return not c:IsType(TYPE_FUSION) and c:IsLocation(LOCATION_EXTRA)
  end
  
  