暗黒界の魔神王
1:このカードは自分フィールドのレベル7以下の「暗黒界」モンスター1体を持ち主の手札に戻し、墓地から特殊召喚できる。
2:このカードが効果で手札から墓地へ捨てられた場合に発動できる。
デッキから「暗黒界の魔神王 レイン」以外のレベル5以上の「暗黒界」モンスター1体を手札に加える。
相手の効果で捨てられた場合、さらに自分のデッキ・墓地からレベル4以下の「暗黒界」モンスター1体を選んで自分または相手フィールドに特殊召喚できる。
local SETCARD_ID_0 = 0x6 --暗黒界

local SETCARD_ID_0 = 0x6 --暗黒界
local s, id, o = GetID()
function s.initial_effect(c)
	--special summon
	local e1 = Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id, 0))
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_SPSUMMON_PROC)
	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
	e1:SetRange(LOCATION_GRAVE)
	e1:SetCondition(s.spcon)
	e1:SetTarget(s.sptg)
	e1:SetOperation(s.spop)
	c:RegisterEffect(e1)
	--search
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 1))
	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetCondition(s.condition)
	e2:SetTarget(s.target)
	e2:SetOperation(s.operation)
	c:RegisterEffect(e2)
end
function s.spfilter(c, tp)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsLevelBelow(7) and c:IsAbleToHandAsCost() and Duel.GetMZoneCount(tp, c) > 0
end
function s.spcon(e, c)
	if c == nil then
		return true
	end
	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
		return false
	end
	local tp = c:GetControler()
	return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_MZONE, 0, 1, nil, tp)
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_MZONE, 0, nil, tp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
	local tc = g:SelectUnselect(nil, tp, false, true, 1, 1)
	if tc then
		e:SetLabelObject(tc)
		return true
	else
		return false
	end
end
function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
	local g = e:GetLabelObject()
	Duel.SendtoHand(g, nil, REASON_SPSUMMON)
end
function s.condition(e, tp, eg, ep, ev, re, r, rp)
	local c = e:GetHandler()
	e:SetLabel(c:GetPreviousControler())
	return c:IsPreviousLocation(LOCATION_HAND) and (r & (REASON_EFFECT + REASON_DISCARD)) == REASON_EFFECT + REASON_DISCARD
end
function s.filter1(c)
	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand() and c:IsLevelAbove(5) and not c:IsCode(id)
end
function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.filter1, tp, LOCATION_DECK, 0, 1, nil)
	end
	if rp == 1 - tp and tp == e:GetLabel() then
		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
	else
		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
end
function s.filter2(c, e, tp, ft, ft2)
	return c:IsSetCard(SETCARD_ID_0)
		and c:IsLevelBelow(4)
		and ((ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)) or (ft2 > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP, 1 - tp)))
end
function s.operation(e, tp, eg, ep, ev, re, r, rp)
	local ft = Duel.GetMZoneCount(tp)
	local ft2 = Duel.GetMZoneCount(1 - tp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
	local g = Duel.SelectMatchingCard(tp, s.filter1, tp, LOCATION_DECK, 0, 1, 1, nil)
	if #g > 0 and Duel.SendtoHand(g, nil, REASON_EFFECT) > 0 then
		Duel.ConfirmCards(1 - tp, g)
		if
			rp == 1 - tp
			and tp == e:GetLabel()
			and Duel.IsExistingMatchingCard(aux.NecroValleyFilter(s.filter2), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil, e, tp, ft, ft2)
			and Duel.SelectYesNo(tp, aux.Stringid(id, 2))
		then
			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
			local tg = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter2), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil, e, tp, ft, ft2)
			Duel.BreakEffect()
			local tc = tg:GetFirst()
			local o1 = ft > 0 and tc:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)
			local o2 = ft2 > 0 and tc:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP, 1 - tp)
			local opt = 0
			if o1 and o2 then
				opt = Duel.SelectOption(tp, aux.Stringid(id, 3), aux.Stringid(id, 4))
			elseif o1 then
				opt = 0
			else
				opt = 1
			end
			local p = tp
			if opt == 1 then
				p = 1 - tp
			end
			Duel.SpecialSummon(tc, 0, tp, p, false, false, POS_FACEUP)
		end
	end
end


暗黒界の魔神王
1:このカードは自分フィールドのレベル7以下の「暗黒界」モンスター1体を持ち主の手札に戻し、墓地から特殊召喚できる。
2:このカードが効果で手札から墓地へ捨てられた場合に発動できる。
デッキから「暗黒界の魔神王 レイン」以外のレベル5以上の「暗黒界」モンスター1体を手札に加える。
相手の効果で捨てられた場合、さらに自分のデッキ・墓地からレベル4以下の「暗黒界」モンスター1体を選んで自分または相手フィールドに特殊召喚できる。
-- 暗黒界の魔神王
local SETCARD_ID_0 = 0x6 --暗黒界
local s, id, o = GetID()
function s.initial_effect(c)
	--special summon
	local e1 = Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id, 0))
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_SPSUMMON_PROC)
	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
	e1:SetRange(LOCATION_GRAVE)
	e1:SetCondition(s.spcon)
	e1:SetTarget(s.sptg)
	e1:SetOperation(s.spop)
	c:RegisterEffect(e1)
	--search
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 1))
	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetCondition(s.condition)
	e2:SetTarget(s.target)
	e2:SetOperation(s.operation)
	c:RegisterEffect(e2)
end
function s.spfilter(c, tp)
	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsLevelBelow(7) and c:IsAbleToHandAsCost() and Duel.GetMZoneCount(tp, c) > 0
end
function s.spcon(e, c)
	if c == nil then
		return true
	end
	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
		return false
	end
	local tp = c:GetControler()
	return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_MZONE, 0, 1, nil, tp)
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_MZONE, 0, nil, tp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
	local tc = g:SelectUnselect(nil, tp, false, true, 1, 1)
	if tc then
		e:SetLabelObject(tc)
		return true
	else
		return false
	end
end
function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
	local g = e:GetLabelObject()
	Duel.SendtoHand(g, nil, REASON_SPSUMMON)
end
function s.condition(e, tp, eg, ep, ev, re, r, rp)
	local c = e:GetHandler()
	e:SetLabel(c:GetPreviousControler())
	return c:IsPreviousLocation(LOCATION_HAND) and (r & (REASON_EFFECT + REASON_DISCARD)) == REASON_EFFECT + REASON_DISCARD
end
function s.filter1(c)
	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand() and c:IsLevelAbove(5) and not c:IsCode(id)
end
function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.filter1, tp, LOCATION_DECK, 0, 1, nil)
	end
	if rp == 1 - tp and tp == e:GetLabel() then
		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH + CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
	else
		e:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
end
function s.filter2(c, e, tp, ft, ft2)
	return c:IsSetCard(SETCARD_ID_0)
		and c:IsLevelBelow(4)
		and ((ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)) or (ft2 > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP, 1 - tp)))
end
function s.operation(e, tp, eg, ep, ev, re, r, rp)
	local ft = Duel.GetMZoneCount(tp)
	local ft2 = Duel.GetMZoneCount(1 - tp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
	local g = Duel.SelectMatchingCard(tp, s.filter1, tp, LOCATION_DECK, 0, 1, 1, nil)
	if #g > 0 and Duel.SendtoHand(g, nil, REASON_EFFECT) > 0 then
		Duel.ConfirmCards(1 - tp, g)
		if
			rp == 1 - tp
			and tp == e:GetLabel()
			and Duel.IsExistingMatchingCard(aux.NecroValleyFilter(s.filter2), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil, e, tp, ft, ft2)
			and Duel.SelectYesNo(tp, aux.Stringid(id, 2))
		then
			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
			local tg = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter2), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil, e, tp, ft, ft2)
			Duel.BreakEffect()
			local tc = tg:GetFirst()
			local o1 = ft > 0 and tc:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP)
			local o2 = ft2 > 0 and tc:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP, 1 - tp)
			local opt = 0
			if o1 and o2 then
				opt = Duel.SelectOption(tp, aux.Stringid(id, 3), aux.Stringid(id, 4))
			elseif o1 then
				opt = 0
			else
				opt = 1
			end
			local p = tp
			if opt == 1 then
				p = 1 - tp
			end
			Duel.SpecialSummon(tc, 0, tp, p, false, false, POS_FACEUP)
		end
	end
end

