--黄昏交易
function c114514980.initial_effect(c)
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_REMOVE)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCountLimit(1,114514980+EFFECT_COUNT_CODE_OATH)
	e1:SetCost(c114514980.cost)
	e1:SetTarget(c114514980.target)
	e1:SetOperation(c114514980.activate)
	e1:SetLabel(0)
	c:RegisterEffect(e1)
end
function c114514980.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	e:SetLabel(100)
	if chk==0 then return true end
end
function c114514980.costfilter(c,e,tp)
	return c:GetOriginalLevel()>0 and Duel.IsExistingMatchingCard(c114514980.spfilter,tp,LOCATION_DECK,0,1,nil,c,e,tp) and c:IsSetCard(0x38)
		and Duel.GetMZoneCount(tp,c)>0 and c:IsAbleToRemoveAsCost()
end
function c114514980.spfilter(c,tc,e,tp)
	return c:GetOriginalLevel()==tc:GetOriginalLevel() and c:IsSetCard(0x38)
		and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function c114514980.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		if e:GetLabel()~=100 then return false end
		e:SetLabel(0)
		return e:IsHasType(EFFECT_TYPE_ACTIVATE) and Duel.IsExistingMatchingCard(c114514980.costfilter,tp,LOCATION_GRAVE+LOCATION_HAND,0,1,nil,e,tp)
		and Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>2
	end
	e:SetLabel(0)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectMatchingCard(tp,c114514980.costfilter,tp,LOCATION_GRAVE+LOCATION_HAND,0,1,1,nil,e,tp)
	Duel.Remove(g,POS_FACEUP,REASON_COST)
	Duel.SetTargetCard(g)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_DECK)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,2,tp,LOCATION_DECK)
end
function c114514980.activate(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectMatchingCard(tp,c114514980.spfilter,tp,LOCATION_DECK,0,1,1,nil,tc,e,tp)
	if g:GetCount()>0 and Duel.SpecialSummon(g,0,tp,tp,false,false,POS_FACEUP)~=0 then
		Duel.BreakEffect()
		local rg=Duel.GetDecktopGroup(tp,2)
		if #rg<=0 then return end
		Duel.DisableShuffleCheck()
		Duel.Remove(rg,POS_FACEUP,REASON_EFFECT)
	end
end