name: Reign-Beaux,
card_text: You can Special Summon this card (from your GY) by returning 1 Level 7 or lower "Dark World" monster you control to the hand. If this card is discarded to the GY by card effect: You can add 1 Level 5 or higher "Dark World" monster from your Deck to your hand, except "Reign-Beaux, Overking of Dark World", then, if this card was discarded from your hand to your GY by an opponent's card effect, you can Special Summon 1 Level 4 or lower "Dark World" monster from your Deck or GY to either field.
external_ids: local SETCARD_ID_0 = 0x6 --Dark
  
lua: local SETCARD_ID_0 = 0x6 --Dark
  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
  
  
source_text: Reign-Beaux,
  You can Special Summon this card (from your GY) by returning 1 Level 7 or lower "Dark World" monster you control to the hand. If this card is discarded to the GY by card effect: You can add 1 Level 5 or higher "Dark World" monster from your Deck to your hand, except "Reign-Beaux, Overking of Dark World", then, if this card was discarded from your hand to your GY by an opponent's card effect, you can Special Summon 1 Level 4 or lower "Dark World" monster from your Deck or GY to either field.
target_text: -- Reign-Beaux,
  local SETCARD_ID_0 = 0x6 --Dark
  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
  
  