--神心一刀流·樱飞闪影
function c66914003.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(c66914003.thtg)
	e1:SetOperation(c66914003.thop)
	c:RegisterEffect(e1)	   
end
function c66914003.thtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	local count=0
	local chain=Duel.GetCurrentChain()
	for i=1,chain do
		local te,tgp=Duel.GetChainInfo(i,CHAININFO_TRIGGERING_EFFECT,CHAININFO_TRIGGERING_PLAYER)
		if tgp==tp and te:GetHandler():IsSetCard(0x373) and te:GetHandler()~=e:GetHandler() then
			count=count+1
		end
	end
	local reverse=Duel.GetFlagEffect(tp,66914009)
	if chk==0 then 
		if reverse==0 then
			return Duel.IsExistingMatchingCard(aux.TRUE,tp,LOCATION_ONFIELD,0,1,nil) and (count~=3 or Duel.IsExistingMatchingCard(Card.IsCode,tp,LOCATION_ONFIELD,0,1,nil,66914000))
		else
			return Duel.IsExistingMatchingCard(Card.IsCode,tp,LOCATION_ONFIELD,0,1,nil,66914000) and (count~=3 or Duel.IsExistingMatchingCard(aux.TRUE,tp,LOCATION_ONFIELD,0,1,nil))
		end
	end
	count=count+1
	e:SetLabel(count)
end
function c66914003.filter(c)
	return c:IsFaceup() and c:IsCode(66914000)
end
function c66914003.thop(e,tp,eg,ep,ev,re,r,rp)
	local count=e:GetLabel()
	local reverse=Duel.GetFlagEffect(tp,66914009)
	if (reverse==0) or (reverse>0 and count==4) then
		--indes
		local e21=Effect.CreateEffect(e:GetHandler())
		e21:SetType(EFFECT_TYPE_FIELD)
		e21:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
		e21:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
		e21:SetRange(LOCATION_MZONE)
		e21:SetTargetRange(LOCATION_ONFIELD,0)
		e21:SetReset(RESET_PHASE+PHASE_END)
		e21:SetValue(c66914003.indct)
		Duel.RegisterEffect(e21,tp)
	end
	if count==2 then
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD)
		e1:SetCode(EFFECT_CHANGE_DAMAGE)
		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
		e1:SetTargetRange(1,0)
		e1:SetValue(c66914003.damval2)
		e1:SetReset(RESET_PHASE+PHASE_END)
		Duel.RegisterEffect(e1,tp)
		local e2=e1:Clone()
		e2:SetCode(EFFECT_NO_EFFECT_DAMAGE)
		e2:SetCondition(c66914003.damcon)
		e2:SetReset(RESET_PHASE+PHASE_END)
		Duel.RegisterEffect(e2,tp)
	end
	if count==4 or reverse>0 then
		local g=Duel.GetMatchingGroup(c66914003.filter,tp,LOCATION_ONFIELD,0,nil)
		local tc=g:GetFirst()
		while tc do
			local e4=Effect.CreateEffect(e:GetHandler())
			e4:SetType(EFFECT_TYPE_SINGLE)
			e4:SetCode(EFFECT_IMMUNE_EFFECT)
			e4:SetValue(c66914003.efilter1)
			e4:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END)
			e4:SetOwnerPlayer(tp)
			tc:RegisterEffect(e4)
			tc=g:GetNext()
		end
	end
	if (Duel.GetFlagEffect(tp,66914007)>0 or Duel.GetFlagEffect(tp,66914006)>0) and Duel.IsExistingMatchingCard(aux.TRUE,tp,0,LOCATION_ONFIELD,1,nil) and Duel.IsPlayerCanDraw(tp,1) and Duel.SelectYesNo(tp,aux.Stringid(66914007,0)) then
		local g=Duel.SelectMatchingCard(tp,aux.TRUE,tp,0,LOCATION_ONFIELD,1,1,nil):GetFirst()
		Duel.Destroy(g,REASON_EFFECT)
		Duel.Draw(tp,1,REASON_EFFECT)
		if Duel.GetFlagEffect(tp,66914006)~=0 then
			Duel.ResetFlagEffect(tp,66914006)
		else
			Duel.ResetFlagEffect(tp,66914007)
		end
	end
end
function c66914003.indct(e,re,r,rp)
	if bit.band(r,REASON_BATTLE+REASON_EFFECT)~=0 then
		return 1
	else return 0 end
end
function c66914003.efilter1(e,re)
	return e:GetOwnerPlayer()~=re:GetOwnerPlayer()
end
function c66914003.efilter(e,re)
	return re==e:GetLabelObject()
end
function c66914003.etarget(e,c)
	return c:IsType(TYPE_MONSTER)
end
function c66914003.damval2(e,re,val,r,rp,rc)
	local c=e:GetHandler()
	if bit.band(r,REASON_BATTLE+REASON_EFFECT)~=0 and c:GetFlagEffect(2220237)==0 then
		c:RegisterFlagEffect(2220237,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END,0,1)
		return 0
	end
	return val
end
function c66914003.damcon(e)
	return e:GetHandler():GetFlagEffect(66914003)==0
end