--真・デーモンの召喚
function c114514302.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_TOHAND+CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCountLimit(1,114514302+EFFECT_COUNT_CODE_OATH)
	e1:SetCost(c114514302.cost)
	e1:SetTarget(c114514302.target)
	e1:SetOperation(c114514302.activate)
	c:RegisterEffect(e1)
	--mat
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
	e2:SetRange(LOCATION_GRAVE)
	e2:SetTarget(c114514302.mattg)
	e2:SetOperation(c114514302.matop)
	c:RegisterEffect(e2)
end
c114514302.spchecks=aux.CreateChecks(Card.IsCode,{70781052,95825679,32775808,64635042,61248471})
function c114514302.cfilter(c)
	return c:IsSetCard(0x45) and c:IsLevel(6) and c:IsAttack(2500) and c:IsType(TYPE_MONSTER) and c:IsAbleToGraveAsCost()
end
function c114514302.cost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then
		return Duel.IsExistingMatchingCard(c114514302.cfilter,tp,LOCATION_DECK,0,1,nil) and Duel.IsExistingMatchingCard(c114514302.cfilter,tp,LOCATION_HAND,0,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
	local g1=Duel.SelectMatchingCard(tp,c114514302.cfilter,tp,LOCATION_HAND,0,1,1,nil)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
	local g2=Duel.SelectMatchingCard(tp,c114514302.cfilter,tp,LOCATION_DECK,0,1,1,nil)
	Duel.SendtoGrave(g1,REASON_COST)
	Duel.SendtoGrave(g2,REASON_COST)
end
function c114514302.spfilter(c,e,tp)
	return c:IsCode(70781052,95825679,32775808,64635042,61248471) and c:IsCanBeSpecialSummoned(e,0,tp,true,false)
end
function c114514302.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return not Duel.IsPlayerAffectedByEffect(tp,59822133)
		and Duel.GetLocationCount(tp,LOCATION_MZONE)>=5
		and Duel.IsExistingMatchingCard(c114514302.spfilter,tp,LOCATION_DECK+LOCATION_EXTRA,0,1,nil,e,tp)
    end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,5,tp,LOCATION_DECK+LOCATION_EXTRA)
end
function c114514302.activate(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)>=5 and not Duel.IsPlayerAffectedByEffect(tp,59822133) then
	local g=Duel.GetMatchingGroup(c114514302.spfilter,tp,LOCATION_DECK+LOCATION_EXTRA,0,nil,e,tp)
    Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
    local sg=g:SelectSubGroupEach(tp,c114514302.spchecks,false)
    if sg then
        Duel.SpecialSummon(sg,0,tp,tp,true,false,POS_FACEUP)
	end
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e1:SetTargetRange(1,0)
	e1:SetReset(RESET_PHASE+PHASE_END)
	Duel.RegisterEffect(e1,tp)
	end
end
function c114514302.tgfilter(c)
	return c:IsFaceup() and c:IsRace(RACE_FIEND) and c:IsType(TYPE_XYZ)
end
function c114514302.mattg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_MZONE) and c114514302.tgfilter(chkc,eg) end
	if chk==0 then return Duel.IsExistingTarget(c114514302.tgfilter,tp,LOCATION_MZONE,0,1,nil,eg)
		and e:GetHandler():IsCanOverlay() end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FACEUP)
	Duel.SelectTarget(tp,c114514302.tgfilter,tp,LOCATION_MZONE,0,1,1,nil,eg)
	Duel.SetOperationInfo(0,CATEGORY_LEAVE_GRAVE,e:GetHandler(),1,0,0)
end
function c114514302.matop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local tc=Duel.GetFirstTarget()
	if c:IsRelateToEffect(e) and tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) then
		Duel.Overlay(tc,Group.FromCards(c))
	end
end
