name: Corrupted
card_text: Target 1 face-up Xyz Monster you control; Special Summon 3 "Umbral Horror Mirage Tokens" (Fiend-Type/DARK/Level 1/ATK ?/DEF 0). (When Summoned, each Token's ATK becomes equal to that Xyz Monster's current ATK. They cannot attack your opponent directly or be Tributed, except for a Tribute Summon.) When that face-up Xyz Monster leaves the field, destroy these tokens.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 67949764 --Umbral Horror Mirage Token
  local SETCARD_ID_0 = 0x87 --Umbral
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 67949764 --Umbral Horror Mirage Token
  local SETCARD_ID_0 = 0x87 --Umbral
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c, tp)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, -2, 0, 1, RACE_FIEND, ATTRIBUTE_DARK)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc, tp)
  	end
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 2 and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 3, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 3, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) > 2 then
  		local rfid = tc:GetRealFieldID()
  		local atk = 0
  		local cr = false
  		if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  			atk = tc:GetAttack()
  			cr = true
  		end
  		if not Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, -2, 0, 1, RACE_FIEND, ATTRIBUTE_DARK) then
  			return
  		end
  		if cr then
  			local de = Effect.CreateEffect(e:GetHandler())
  			de:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  			de:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			de:SetCode(EVENT_LEAVE_FIELD)
  			de:SetOperation(s.desop)
  			de:SetLabel(rfid)
  			tc:RegisterEffect(de, true)
  		end
  		for i = 1, 3 do
  			local token = Duel.CreateToken(tp, id + 1)
  			if Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP) then
  				local e1 = Effect.CreateEffect(e:GetHandler())
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_CANNOT_DIRECT_ATTACK)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  				token:RegisterEffect(e1)
  				local e2 = Effect.CreateEffect(e:GetHandler())
  				e2:SetType(EFFECT_TYPE_SINGLE)
  				e2:SetCode(EFFECT_UNRELEASABLE_NONSUM)
  				e2:SetValue(1)
  				e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  				token:RegisterEffect(e2)
  				local e3 = Effect.CreateEffect(e:GetHandler())
  				e3:SetType(EFFECT_TYPE_SINGLE)
  				e3:SetCode(EFFECT_SET_ATTACK)
  				e3:SetValue(atk)
  				e3:SetReset(RESET_EVENT + RESETS_STANDARD)
  				token:RegisterEffect(e3)
  			end
  			if cr then
  				token:RegisterFlagEffect(67949764, RESET_EVENT + RESETS_STANDARD, 0, 0, rfid)
  				tc:CreateRelation(token, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET)
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  function s.desfilter(c, rfid)
  	return c:GetFlagEffectLabel(67949764) == rfid
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_MZONE, 0, nil, e:GetLabel())
  	Duel.Destroy(g, REASON_EFFECT)
  	e:Reset()
  end
  
  
source_text: Corrupted
  Target 1 face-up Xyz Monster you control; Special Summon 3 "Umbral Horror Mirage Tokens" (Fiend-Type/DARK/Level 1/ATK ?/DEF 0). (When Summoned, each Token's ATK becomes equal to that Xyz Monster's current ATK. They cannot attack your opponent directly or be Tributed, except for a Tribute Summon.) When that face-up Xyz Monster leaves the field, destroy these tokens.
target_text: -- Corrupted
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 67949764 --Umbral Horror Mirage Token
  local SETCARD_ID_0 = 0x87 --Umbral
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c, tp)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, -2, 0, 1, RACE_FIEND, ATTRIBUTE_DARK)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc, tp)
  	end
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 2 and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 3, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 3, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) > 2 then
  		local rfid = tc:GetRealFieldID()
  		local atk = 0
  		local cr = false
  		if tc:IsFaceup() and tc:IsRelateToEffect(e) then
  			atk = tc:GetAttack()
  			cr = true
  		end
  		if not Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, -2, 0, 1, RACE_FIEND, ATTRIBUTE_DARK) then
  			return
  		end
  		if cr then
  			local de = Effect.CreateEffect(e:GetHandler())
  			de:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  			de:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			de:SetCode(EVENT_LEAVE_FIELD)
  			de:SetOperation(s.desop)
  			de:SetLabel(rfid)
  			tc:RegisterEffect(de, true)
  		end
  		for i = 1, 3 do
  			local token = Duel.CreateToken(tp, id + 1)
  			if Duel.SpecialSummonStep(token, 0, tp, tp, false, false, POS_FACEUP) then
  				local e1 = Effect.CreateEffect(e:GetHandler())
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_CANNOT_DIRECT_ATTACK)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  				token:RegisterEffect(e1)
  				local e2 = Effect.CreateEffect(e:GetHandler())
  				e2:SetType(EFFECT_TYPE_SINGLE)
  				e2:SetCode(EFFECT_UNRELEASABLE_NONSUM)
  				e2:SetValue(1)
  				e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  				token:RegisterEffect(e2)
  				local e3 = Effect.CreateEffect(e:GetHandler())
  				e3:SetType(EFFECT_TYPE_SINGLE)
  				e3:SetCode(EFFECT_SET_ATTACK)
  				e3:SetValue(atk)
  				e3:SetReset(RESET_EVENT + RESETS_STANDARD)
  				token:RegisterEffect(e3)
  			end
  			if cr then
  				token:RegisterFlagEffect(67949764, RESET_EVENT + RESETS_STANDARD, 0, 0, rfid)
  				tc:CreateRelation(token, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET)
  			end
  		end
  		Duel.SpecialSummonComplete()
  	end
  end
  function s.desfilter(c, rfid)
  	return c:GetFlagEffectLabel(67949764) == rfid
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_MZONE, 0, nil, e:GetLabel())
  	Duel.Destroy(g, REASON_EFFECT)
  	e:Reset()
  end
  
  