--暗黑界的静寂 无色
local s,id,o=GetID()
function s.initial_effect(c)
	--spsummon
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_TO_GRAVE)
	e1:SetCondition(s.putcon)
	e1:SetOperation(s.putop)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_F)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetOperation(s.rcounter)
	c:RegisterEffect(e2)
end
function s.putcon(e,tp,eg,ep,ev,re,r,rp)
	e:SetLabel(e:GetHandler():GetPreviousControler())
	return e:GetHandler():IsPreviousLocation(LOCATION_HAND) and bit.band(r,0x4040)==0x4040
end
function s.putop(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(s.filter,tp,LOCATION_ONFIELD,nil,nil)
	if g:GetCount()==0 then return end
	Duel.BreakEffect()
	Duel.Hint(HINT_CARD,0,id)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_COUNTER)
	local g1=g:Select(tp,1,1,nil)
	local tc=g1:GetFirst()
	if tc:IsFaceup() then
		tc:AddCounter(0x10e9,1)
		if tc:GetFlagEffect(id)~=0 then return end
		local te=Effect.CreateEffect(tc)
		te:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		te:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
		if tc:IsType(TYPE_FUSION) and tc:IsType(TYPE_MONSTER) then
			te:SetType(EFFECT_TYPE_FIELD)
			te:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
			te:SetCode(EFFECT_CANNOT_ACTIVATE)
			te:SetRange(LOCATION_MZONE)
			te:SetTargetRange(1,1)
			te:SetCondition(s.countercon)
			te:SetValue(s.aclimit)
		elseif tc:IsType(TYPE_MONSTER) then
			te:SetRange(LOCATION_ONFIELD)
			te:SetCode(EVENT_CHAINING)
			te:SetRange(LOCATION_MZONE)
			te:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
			te:SetCondition(s.countercon)
			te:SetOperation(s.chainop)
		elseif tc:IsType(TYPE_SPELL) then
			te:SetRange(LOCATION_ONFIELD)
			te:SetCode(EVENT_CHAIN_SOLVING)
			te:SetCondition(s.negcon)
			te:SetOperation(s.negop)
		elseif tc:IsType(TYPE_TRAP) then
			te:SetType(EFFECT_TYPE_FIELD)
			te:SetRange(LOCATION_ONFIELD)
			te:SetCode(EFFECT_DISABLE)
			te:SetCondition(s.countercon)
			te:SetTargetRange(LOCATION_ONFIELD,LOCATION_ONFIELD)
			te:SetTarget(s.negtg)
		end
		tc:RegisterEffect(te)
		tc:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD,0,0)
	end
end

function s.filter(c,e,tp)
	return c:IsFaceup() and c:IsSetCard(0x6)
end
function s.nfilter(c,e,tp)
	return not c:IsSetCard(0x6)
end
function s.cofilter(c)
	return c:GetCounter(0x10e9)~=0
end
function s.countercon(e,tp,eg,ep,ev,re,r,rp)
	return e:GetHandler():GetCounter(0x10e9)~=0 
end
function s.aclimit(e,re,tp)
	local rc=re:GetHandler()
	return not rc:IsSetCard(0x6)
end
function s.chainop(e,tp,eg,ep,ev,re,r,rp)
	local rc=re:GetHandler()
	if rc:IsSetCard(0x6) and ep==tp then 
		Duel.SetChainLimit(aux.FALSE)
	end
end
function s.negcon(e,tp,eg,ep,ev,re,r,rp)
	local rc=re:GetHandler()
	return s.countercon(e,tp,eg,ep,ev,re,r,rp) and not rc:IsSetCard(0x6)
end
function s.negop(e,tp,eg,ep,ev,re,r,rp)
	Duel.NegateEffect(ev,true)
end
function s.negtg(e,c)
	return s.nfilter(c)
end
-- function s.redraw(e,tp,eg,ep,ev,re,r,rp)
-- 	local g=Duel.GetFieldGroup(tp,LOCATION_HAND,LOCATION_HAND)
-- 	Duel.SendtoGrave(g,REASON_EFFECT+REASON_DISCARD)
-- 	Duel.BreakEffect()
-- 	Duel.Draw(tp,2,REASON_EFFECT)
-- 	Duel.Draw(1-tp,2,REASON_EFFECT)
-- end
function s.rcounter(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(s.cofilter,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,nil)
	local tc=g:GetFirst()
	local ss=false
	while tc do
		local sct=tc:GetCounter(0x10e9)
		if sct>0 then ss=true end
		tc:RemoveCounter(tp,0x10e9,sct,0)
		tc:ResetFlagEffect(id)
		tc=g:GetNext()
	end
	if ss then
		Duel.SpecialSummon(e:GetHandler(),0,tp,tp,false,false,POS_FACEUP)
	end
end
