Dream
During the End Phase: You can banish this card; activate 1 "Dream Mirror of Terror" directly from your hand or Deck. You can only use this effect of "Dream Mirror of Joy" once per turn. "Dream Mirror" monsters you control cannot be targeted by your opponent's card effects, or their monsters' attacks, except your "Dream Mirror" monster(s) with the highest Level. This effect is only applied while you control a LIGHT "Dream Mirror" monster.
local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
local CARD_ID_1 = 1050355 --Dream Mirror of Terror
local SETCARD_ID_0 = 0x131 --Dream

local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
local CARD_ID_1 = 1050355 --Dream Mirror of Terror
local SETCARD_ID_0 = 0x131 --Dream
local s, id, o = GetID()
function s.initial_effect(c)
	aux.AddCodeList(c, CARD_ID_1)
	--Activate
	local e1 = Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e1)
	--activate
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e2:SetCode(EVENT_PHASE + PHASE_END)
	e2:SetRange(LOCATION_FZONE)
	e2:SetCountLimit(1, id)
	e2:SetCost(aux.bfgcost)
	e2:SetTarget(s.acttg)
	e2:SetOperation(s.actop)
	c:RegisterEffect(e2)
	--cannot be target
	local e3 = Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetCode(EFFECT_CANNOT_SELECT_BATTLE_TARGET)
	e3:SetRange(LOCATION_FZONE)
	e3:SetTargetRange(0, LOCATION_MZONE)
	e3:SetCondition(s.limcon)
	e3:SetValue(s.atlimit)
	c:RegisterEffect(e3)
	local e4 = Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_FIELD)
	e4:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
	e4:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
	e4:SetRange(LOCATION_FZONE)
	e4:SetTargetRange(LOCATION_MZONE, 0)
	e4:SetCondition(s.limcon)
	e4:SetTarget(s.tglimit)
	e4:SetValue(aux.tgoval)
	c:RegisterEffect(e4)
end
function s.actfilter(c, tp)
	return c:IsCode(CARD_ID_1) and c:GetActivateEffect() and c:GetActivateEffect():IsActivatable(tp, true, true)
end
function s.acttg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.actfilter, tp, LOCATION_HAND + LOCATION_DECK, 0, 1, nil, tp)
	end
end
function s.actop(e, tp, eg, ep, ev, re, r, rp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOFIELD)
	local tc = Duel.SelectMatchingCard(tp, s.actfilter, tp, LOCATION_HAND + LOCATION_DECK, 0, 1, 1, nil, tp):GetFirst()
	if tc then
		local fc = Duel.GetFieldCard(tp, LOCATION_FZONE, 0)
		if fc then
			Duel.SendtoGrave(fc, REASON_RULE)
			Duel.BreakEffect()
		end
		Duel.MoveToField(tc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
		local te = tc:GetActivateEffect()
		te:UseCountLimit(tp, 1, true)
		local tep = tc:GetControler()
		local cost = te:GetCost()
		if cost then
			cost(te, tep, eg, ep, ev, re, r, rp, 1)
		end
		Duel.RaiseEvent(tc, CARD_ID_0, te, 0, tp, tp, Duel.GetCurrentChain())
	end
end
function s.cfilter(c)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAttribute(ATTRIBUTE_LIGHT)
end
function s.limcon(e)
	return Duel.IsExistingMatchingCard(s.cfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
end
function s.limfilter(c, lv)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:GetLevel() > lv
end
function s.atlimit(e, c)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.limfilter, c:GetControler(), LOCATION_MZONE, 0, 1, nil, c:GetLevel())
end
function s.tglimit(e, c)
	return c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.limfilter, c:GetControler(), LOCATION_MZONE, 0, 1, nil, c:GetLevel())
end


Dream
During the End Phase: You can banish this card; activate 1 "Dream Mirror of Terror" directly from your hand or Deck. You can only use this effect of "Dream Mirror of Joy" once per turn. "Dream Mirror" monsters you control cannot be targeted by your opponent's card effects, or their monsters' attacks, except your "Dream Mirror" monster(s) with the highest Level. This effect is only applied while you control a LIGHT "Dream Mirror" monster.
-- Dream
local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
local CARD_ID_1 = 1050355 --Dream Mirror of Terror
local SETCARD_ID_0 = 0x131 --Dream
local s, id, o = GetID()
function s.initial_effect(c)
	aux.AddCodeList(c, CARD_ID_1)
	--Activate
	local e1 = Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e1)
	--activate
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e2:SetCode(EVENT_PHASE + PHASE_END)
	e2:SetRange(LOCATION_FZONE)
	e2:SetCountLimit(1, id)
	e2:SetCost(aux.bfgcost)
	e2:SetTarget(s.acttg)
	e2:SetOperation(s.actop)
	c:RegisterEffect(e2)
	--cannot be target
	local e3 = Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetCode(EFFECT_CANNOT_SELECT_BATTLE_TARGET)
	e3:SetRange(LOCATION_FZONE)
	e3:SetTargetRange(0, LOCATION_MZONE)
	e3:SetCondition(s.limcon)
	e3:SetValue(s.atlimit)
	c:RegisterEffect(e3)
	local e4 = Effect.CreateEffect(c)
	e4:SetType(EFFECT_TYPE_FIELD)
	e4:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
	e4:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
	e4:SetRange(LOCATION_FZONE)
	e4:SetTargetRange(LOCATION_MZONE, 0)
	e4:SetCondition(s.limcon)
	e4:SetTarget(s.tglimit)
	e4:SetValue(aux.tgoval)
	c:RegisterEffect(e4)
end
function s.actfilter(c, tp)
	return c:IsCode(CARD_ID_1) and c:GetActivateEffect() and c:GetActivateEffect():IsActivatable(tp, true, true)
end
function s.acttg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.actfilter, tp, LOCATION_HAND + LOCATION_DECK, 0, 1, nil, tp)
	end
end
function s.actop(e, tp, eg, ep, ev, re, r, rp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOFIELD)
	local tc = Duel.SelectMatchingCard(tp, s.actfilter, tp, LOCATION_HAND + LOCATION_DECK, 0, 1, 1, nil, tp):GetFirst()
	if tc then
		local fc = Duel.GetFieldCard(tp, LOCATION_FZONE, 0)
		if fc then
			Duel.SendtoGrave(fc, REASON_RULE)
			Duel.BreakEffect()
		end
		Duel.MoveToField(tc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
		local te = tc:GetActivateEffect()
		te:UseCountLimit(tp, 1, true)
		local tep = tc:GetControler()
		local cost = te:GetCost()
		if cost then
			cost(te, tep, eg, ep, ev, re, r, rp, 1)
		end
		Duel.RaiseEvent(tc, CARD_ID_0, te, 0, tp, tp, Duel.GetCurrentChain())
	end
end
function s.cfilter(c)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAttribute(ATTRIBUTE_LIGHT)
end
function s.limcon(e)
	return Duel.IsExistingMatchingCard(s.cfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
end
function s.limfilter(c, lv)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:GetLevel() > lv
end
function s.atlimit(e, c)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.limfilter, c:GetControler(), LOCATION_MZONE, 0, 1, nil, c:GetLevel())
end
function s.tglimit(e, c)
	return c:IsSetCard(SETCARD_ID_0) and Duel.IsExistingMatchingCard(s.limfilter, c:GetControler(), LOCATION_MZONE, 0, 1, nil, c:GetLevel())
end

