The
Each time a card(s) you control is destroyed by battle or card effect, place 1 Emperor's Key Counter on this card (max. 1). If your opponent Special Summons a monster(s) from the Extra Deck (except during the Damage Step): You can remove 1 Emperor's Key Counter from this card; send 1 "The Door of Destiny" from your hand, Deck, or face-up field to the GY, Special Summon 1 LIGHT "Utopia" or "Utopic" Xyz Monster from your Extra Deck, and if you do, attach this card to it as material. (This is treated as an Xyz Summon.) You can only use this effect of "The Deal of Destiny" once per turn.
local CARD_ID_0 = 27062594 --The Door of Destiny
local SETCARD_ID_0 = 0x7f --Utopic
local COUNTER_ID_0 = 0x5e --Emperor's

local CARD_ID_0 = 27062594 --The Door of Destiny
local SETCARD_ID_0 = 0x7f --Utopic
local COUNTER_ID_0 = 0x5e --Emperor's
local s, id, o = GetID()
function s.initial_effect(c)
	c:EnableCounterPermit(0x5e, LOCATION_SZONE)
	c:SetCounterLimit(0x5e, 1)
	--Activate
	local e0 = Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e0)
	--counter
	local e1 = Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_DESTROYED)
	e1:SetRange(LOCATION_SZONE)
	e1:SetOperation(s.ctop)
	c:RegisterEffect(e1)
	--special summon
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetCategory(CATEGORY_TOGRAVE + CATEGORY_SPECIAL_SUMMON)
	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCountLimit(1, id)
	e2:SetCondition(s.spcon)
	e2:SetCost(s.spcost)
	e2:SetTarget(s.sptg)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)
end
function s.cfilter(c, tp)
	return c:IsPreviousControler(tp) and c:IsReason(REASON_BATTLE + REASON_EFFECT) and c:IsPreviousLocation(LOCATION_ONFIELD)
end
function s.ctop(e, tp, eg, ep, ev, re, r, rp)
	if eg:IsExists(s.cfilter, 1, nil, tp) then
		e:GetHandler():AddCounter(COUNTER_ID_0, 1)
	end
end
function s.cfilter2(c, tp)
	return c:IsSummonLocation(LOCATION_EXTRA) and c:IsSummonPlayer(1 - tp)
end
function s.spcon(e, tp, eg, ep, ev, re, r, rp)
	return eg:IsExists(s.cfilter2, 1, nil, tp)
end
function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return e:GetHandler():IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
	end
	e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
end
function s.tgfilter(c)
	return (c:IsLocation(LOCATION_HAND + LOCATION_DECK) or c:IsFaceup()) and c:IsCode(CARD_ID_0) and c:IsAbleToGrave()
end
function s.spfilter(c, e, tp)
	return c:IsSetCard(SETCARD_ID_0)
		and c:IsType(TYPE_XYZ)
		and c:IsAttribute(ATTRIBUTE_LIGHT)
		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
		and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_ONFIELD, 0, 1, nil)
			and aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_XMATERIAL)
			and e:GetHandler():IsCanOverlay()
			and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_EXTRA, 0, 1, nil, e, tp)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_ONFIELD)
	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
end
function s.spop(e, tp, eg, ep, ev, re, r, rp)
	local c = e:GetHandler()
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
	local g = Duel.SelectMatchingCard(tp, s.tgfilter, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_ONFIELD, 0, 1, 1, nil)
	if g:GetCount() > 0 and Duel.SendtoGrave(g, REASON_EFFECT) ~= 0 then
		if not aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_XMATERIAL) then
			return
		end
		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
		local sg = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp)
		local sc = sg:GetFirst()
		if sc then
			sc:SetMaterial(nil)
			if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) ~= 0 then
				sc:CompleteProcedure()
				if c:IsRelateToEffect(e) and c:IsCanOverlay() then
					Duel.Overlay(sc, Group.FromCards(c))
				end
			end
		end
	end
end


The
Each time a card(s) you control is destroyed by battle or card effect, place 1 Emperor's Key Counter on this card (max. 1). If your opponent Special Summons a monster(s) from the Extra Deck (except during the Damage Step): You can remove 1 Emperor's Key Counter from this card; send 1 "The Door of Destiny" from your hand, Deck, or face-up field to the GY, Special Summon 1 LIGHT "Utopia" or "Utopic" Xyz Monster from your Extra Deck, and if you do, attach this card to it as material. (This is treated as an Xyz Summon.) You can only use this effect of "The Deal of Destiny" once per turn.
-- The
local CARD_ID_0 = 27062594 --The Door of Destiny
local SETCARD_ID_0 = 0x7f --Utopic
local COUNTER_ID_0 = 0x5e --Emperor's
local s, id, o = GetID()
function s.initial_effect(c)
	c:EnableCounterPermit(0x5e, LOCATION_SZONE)
	c:SetCounterLimit(0x5e, 1)
	--Activate
	local e0 = Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e0)
	--counter
	local e1 = Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_DESTROYED)
	e1:SetRange(LOCATION_SZONE)
	e1:SetOperation(s.ctop)
	c:RegisterEffect(e1)
	--special summon
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetCategory(CATEGORY_TOGRAVE + CATEGORY_SPECIAL_SUMMON)
	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCountLimit(1, id)
	e2:SetCondition(s.spcon)
	e2:SetCost(s.spcost)
	e2:SetTarget(s.sptg)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)
end
function s.cfilter(c, tp)
	return c:IsPreviousControler(tp) and c:IsReason(REASON_BATTLE + REASON_EFFECT) and c:IsPreviousLocation(LOCATION_ONFIELD)
end
function s.ctop(e, tp, eg, ep, ev, re, r, rp)
	if eg:IsExists(s.cfilter, 1, nil, tp) then
		e:GetHandler():AddCounter(COUNTER_ID_0, 1)
	end
end
function s.cfilter2(c, tp)
	return c:IsSummonLocation(LOCATION_EXTRA) and c:IsSummonPlayer(1 - tp)
end
function s.spcon(e, tp, eg, ep, ev, re, r, rp)
	return eg:IsExists(s.cfilter2, 1, nil, tp)
end
function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return e:GetHandler():IsCanRemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
	end
	e:GetHandler():RemoveCounter(tp, COUNTER_ID_0, 1, REASON_COST)
end
function s.tgfilter(c)
	return (c:IsLocation(LOCATION_HAND + LOCATION_DECK) or c:IsFaceup()) and c:IsCode(CARD_ID_0) and c:IsAbleToGrave()
end
function s.spfilter(c, e, tp)
	return c:IsSetCard(SETCARD_ID_0)
		and c:IsType(TYPE_XYZ)
		and c:IsAttribute(ATTRIBUTE_LIGHT)
		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
		and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_ONFIELD, 0, 1, nil)
			and aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_XMATERIAL)
			and e:GetHandler():IsCanOverlay()
			and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_EXTRA, 0, 1, nil, e, tp)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_ONFIELD)
	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
end
function s.spop(e, tp, eg, ep, ev, re, r, rp)
	local c = e:GetHandler()
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
	local g = Duel.SelectMatchingCard(tp, s.tgfilter, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_ONFIELD, 0, 1, 1, nil)
	if g:GetCount() > 0 and Duel.SendtoGrave(g, REASON_EFFECT) ~= 0 then
		if not aux.MustMaterialCheck(nil, tp, EFFECT_MUST_BE_XMATERIAL) then
			return
		end
		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
		local sg = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp)
		local sc = sg:GetFirst()
		if sc then
			sc:SetMaterial(nil)
			if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) ~= 0 then
				sc:CompleteProcedure()
				if c:IsRelateToEffect(e) and c:IsCanOverlay() then
					Duel.Overlay(sc, Group.FromCards(c))
				end
			end
		end
	end
end

