--归魂复仇死者·荡魔侠
function c114514807.initial_effect(c)
	c:EnableReviveLimit()
	--code
	aux.EnableChangeCode(c,4388680,LOCATION_MZONE)  
	--negate and remove 
	local e1=Effect.CreateEffect(c) 
	e1:SetCategory(CATEGORY_DISABLE+CATEGORY_REMOVE)
	e1:SetType(EFFECT_TYPE_QUICK_O)
	e1:SetCode(EVENT_FREE_CHAIN) 
	e1:SetHintTiming(0,TIMINGS_CHECK_MONSTER) 
	e1:SetProperty(EFFECT_FLAG_NO_TURN_RESET)
	e1:SetRange(LOCATION_MZONE) 
	e1:SetCountLimit(1,114514807)
	e1:SetCondition(function(e) 
	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL) end)
	e1:SetTarget(c114514807.negtg)
	e1:SetOperation(c114514807.negop)
	c:RegisterEffect(e1) 
	--to hand and to grave 
	local e2=Effect.CreateEffect(c) 
	e2:SetCategory(CATEGORY_TOHAND+CATEGORY_TOGRAVE)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetCode(EVENT_TO_GRAVE)  
	e2:SetProperty(EFFECT_FLAG_DELAY) 
	e2:SetCondition(function(e) 
	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL) and e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD) end)
	e2:SetCountLimit(1,214514807)
	e2:SetTarget(c114514807.thgtg)
	e2:SetOperation(c114514807.thgop)
	c:RegisterEffect(e2) 
end
function c114514807.negtg(e,tp,eg,ep,ev,re,r,rp,chk)  
	local g1=Duel.GetMatchingGroup(aux.NegateAnyFilter,tp,0,LOCATION_ONFIELD,nil)
	local g2=Duel.GetMatchingGroup(function(c) return c:IsFacedown() and c:IsAbleToRemove() end,tp,0,LOCATION_ONFIELD,nil)
	if chk==0 then return g1:GetCount()>0 or g2:GetCount()>0 end 
	Duel.SetOperationInfo(0,CATEGORY_DISABLE,g1,g1:GetCount(),0,0)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,g2,g2:GetCount(),0,0)
	e:GetHandler():RegisterFlagEffect(0,RESET_EVENT+RESETS_STANDARD,EFFECT_FLAG_CLIENT_HINT,1,0,aux.Stringid(114514807,2)) 
end
function c114514807.negop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local g1=Duel.GetMatchingGroup(aux.NegateAnyFilter,tp,0,LOCATION_ONFIELD,nil)
	local g2=Duel.GetMatchingGroup(function(c) return c:IsFacedown() and c:IsAbleToRemove() end,tp,0,LOCATION_ONFIELD,nil)
	if g1:GetCount()>0 then 
		local tc=g1:GetFirst() 
		while tc do 
		Duel.NegateRelatedChain(tc,RESET_TURN_SET)
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetCode(EFFECT_DISABLE) 
		e1:SetReset(RESET_EVENT+RESETS_STANDARD)  
		tc:RegisterEffect(e1)
		local e2=e1:Clone()
		e2:SetCode(EFFECT_DISABLE_EFFECT)
		e2:SetValue(RESET_TURN_SET)
		tc:RegisterEffect(e2)
		if tc:IsType(TYPE_TRAPMONSTER) then
			local e3=e1:Clone()
			e3:SetCode(EFFECT_DISABLE_TRAPMONSTER)
			tc:RegisterEffect(e3)
		end 
		tc=g1:GetNext()
		end	
	end
	if g2:GetCount()>0 then 
		Duel.Remove(g2,POS_FACEUP,REASON_EFFECT) 
	end 
end
function c114514807.thgtg(e,tp,eg,ep,ev,re,r,rp,chk)   
	if chk==0 then return Duel.IsExistingMatchingCard(function(c) return c:IsSetCard(0x106) and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsAbleToHand() end,tp,LOCATION_GRAVE+LOCATION_REMOVED,0,1,nil) and Duel.IsExistingMatchingCard(function(c) return c:IsSetCard(0x106) and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsAbleToGrave() end,tp,LOCATION_DECK,0,1,nil) end 
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,tp,LOCATION_GRAVE+LOCATION_REMOVED)
	Duel.SetOperationInfo(0,CATEGORY_TOGRAVE,nil,1,tp,LOCATION_DECK) 
end
function c114514807.thgop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if Duel.IsExistingMatchingCard(function(c) return c:IsSetCard(0x106) and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsAbleToHand() end,tp,LOCATION_GRAVE+LOCATION_REMOVED,0,1,nil) then 
		local sg=Duel.SelectMatchingCard(tp,function(c) return c:IsSetCard(0x106) and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsAbleToHand() end,tp,LOCATION_GRAVE+LOCATION_REMOVED,0,1,1,nil) 
		Duel.SendtoHand(sg,tp,REASON_EFFECT) 
		Duel.ConfirmCards(1-tp,sg) 
		if Duel.IsExistingMatchingCard(function(c) return c:IsSetCard(0x106) and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsAbleToGrave() end,tp,LOCATION_DECK,0,1,nil) then 
			local rg=Duel.SelectMatchingCard(tp,function(c) return c:IsSetCard(0x106) and c:IsType(TYPE_SPELL+TYPE_TRAP) and c:IsAbleToGrave() end,tp,LOCATION_DECK,0,1,1,nil) 
			Duel.SendtoGrave(rg,REASON_EFFECT) 
		end 
	end 
end 



