--不知火的祭官
local s,id,o=GetID()
function s.initial_effect(c)
	--sp summon
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_REMOVE)
	e1:SetType(EFFECT_TYPE_IGNITION)
	e1:SetRange(LOCATION_HAND)
	e1:SetCountLimit(1,id)
	e1:SetCost(s.spcost)
	e1:SetTarget(s.tgtg)
	e1:SetOperation(s.tgop)
	c:RegisterEffect(e1)
	--set
	local e12=Effect.CreateEffect(c)
	e12:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e12:SetCountLimit(1,id+o)
	e12:SetCode(EVENT_REMOVE)
	e12:SetProperty(EFFECT_FLAG_DELAY)
	e12:SetTarget(s.target)
	e12:SetOperation(s.operation)
	c:RegisterEffect(e12)   
end
function s.tgfilter(c)
	return c:IsType(TYPE_MONSTER) and c:IsAbleToRemove() and c:IsRace(RACE_ZOMBIE)
end
function s.cfilter(c)
	return c:IsRace(RACE_ZOMBIE) and c:IsAbleToRemoveAsCost()
end
function s.spcost(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_HAND+LOCATION_MZONE,0,1,e:GetHandler()) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectMatchingCard(tp,s.cfilter,tp,LOCATION_HAND+LOCATION_MZONE,0,1,1,e:GetHandler())
	Duel.Remove(g,POS_FACEUP,REASON_COST)
end
function s.tgtg(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
		and Duel.IsPlayerCanDiscardDeck(tp,4) end
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,0,tp,4)
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,c,1,0,0)
end
function s.tgfilter1(c)
	return c:IsFaceup() and c:IsRace(RACE_ZOMBIE) and c:IsType(TYPE_MONSTER)
end
function s.tgop(e,tp,eg,ep,ev,re,r,rp)
	if e:GetHandler():IsRelateToEffect(e) and Duel.SpecialSummon(e:GetHandler(),0,tp,tp,false,false,POS_FACEUP)>0 then
		local ct=Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)
		if ct==0 then return end
		if ct>4 then ct=4 end
		local g=Duel.GetDecktopGroup(tp,ct)
		Duel.DisableShuffleCheck()
		if Duel.Remove(g,POS_FACEUP,REASON_EFFECT)~=0  then
			local g1=Duel.GetMatchingGroup(s.tgfilter1,tp,LOCATION_REMOVED,0,nil)
			if #g1>0 and Duel.SelectYesNo(tp,aux.Stringid(id,0)) then
				Duel.BreakEffect()
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
				local sg=g1:FilterSelect(tp,Card.IsRace,1,2,nil,RACE_ZOMBIE)
				Duel.SendtoGrave(sg,REASON_EFFECT+REASON_RETURN)  
			end
		end
	end
end
function s.alfilter(c)
	return c:IsSetCard(0xd9) and c:IsType(TYPE_SPELL+TYPE_TRAP)
end
function s.pfilter(c,tp,ft)
	return (not c:IsForbidden()) and c:CheckUniqueOnField(tp) and (c:IsType(TYPE_FIELD) or (ft>0 and c:IsType(TYPE_CONTINUOUS)))
end
function s.setfilter(c,tp,ft)
	return c:IsSSetable(true) and (c:IsType(TYPE_FIELD) or ft>0)
end
function s.chkfilter(c)
	return c:IsCode(40005099) and c:IsFaceup()
end
function s.fselect(g,tp,ft,check)
	if check then
		return g:IsExists(s.pfilter,1,nil,tp,ft) or g:IsExists(s.setfilter,1,nil,tp,ft)
	else
		return g:IsExists(s.pfilter,1,nil,tp,ft)
	end
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	local ft=Duel.GetLocationCount(tp,LOCATION_SZONE)
	local g=Duel.GetMatchingGroup(s.alfilter,tp,LOCATION_DECK,0,nil)
	local check=Duel.IsExistingMatchingCard(s.chkfilter,tp,LOCATION_ONFIELD,0,1,nil)
	if chk==0 then return g:CheckSubGroup(s.fselect,1,1,tp,ft,check) end
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local ft=Duel.GetLocationCount(tp,LOCATION_SZONE)
	local g=Duel.GetMatchingGroup(s.alfilter,tp,LOCATION_DECK,0,nil)
	local check=Duel.IsExistingMatchingCard(s.chkfilter,tp,LOCATION_ONFIELD,0,1,nil)
	local sg=g:SelectSubGroup(tp,s.fselect,false,1,1,tp,ft,check)
	if not sg then return end
	local tc=sg:GetFirst()
	if (tc:IsType(TYPE_FIELD) or tc:IsType(TYPE_CONTINUOUS)) then
		if check and Duel.SelectYesNo(tp,aux.Stringid(id,1)) then
			if Duel.SSet(tp,tc)>0 then
				if tc:IsType(TYPE_TRAP) then
					local e1=Effect.CreateEffect(c)
					e1:SetType(EFFECT_TYPE_SINGLE)
					e1:SetCode(EFFECT_TRAP_ACT_IN_SET_TURN)
					e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
					e1:SetReset(RESET_EVENT+RESETS_STANDARD)
					tc:RegisterEffect(e1)
				end
			end
		else
			if tc:IsType(TYPE_FIELD) then 
				Duel.MoveToField(tc,tp,tp,LOCATION_FZONE,POS_FACEUP,true) 
			else
				Duel.MoveToField(tc,tp,tp,LOCATION_SZONE,POS_FACEUP,true) 
			end
		end
	else
		if Duel.SSet(tp,tc)>0 then
			if tc:IsType(TYPE_QUICKPLAY) then
				local e1=Effect.CreateEffect(c)
				e1:SetType(EFFECT_TYPE_SINGLE)
				e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
				e1:SetCode(EFFECT_QP_ACT_IN_SET_TURN)
				e1:SetReset(RESET_EVENT+RESETS_STANDARD)
				tc:RegisterEffect(e1)
			end
			if tc:IsType(TYPE_TRAP) then
				local e1=Effect.CreateEffect(c)
				e1:SetType(EFFECT_TYPE_SINGLE)
				e1:SetCode(EFFECT_TRAP_ACT_IN_SET_TURN)
				e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
				e1:SetReset(RESET_EVENT+RESETS_STANDARD)
				tc:RegisterEffect(e1)
			end
		end 
	end
end