--虎猩熊豹！
local s,id,o=GetID()
function c114514472.initial_effect(c)
	--Activate
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
    e1:SetCondition(s.spcon)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
end
s.spchecks=aux.CreateChecks(Card.IsCode,{3030892,92373006,90582719,66863374})
function s.spcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.IsExistingMatchingCard(aux.TRUE,tp,0,LOCATION_MZONE,1,nil)
end
function s.spfilter(c,e,tp)
	return c:IsCode(3030892,92373006,90582719,66863374) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	local res=Duel.GetLocationCount(tp,LOCATION_MZONE)>=3
    local rea=Duel.GetLocationCountFromEx(tp)>0
	if chk==0 then
		local g=Duel.GetMatchingGroup(s.spfilter,tp,LOCATION_EXTRA+LOCATION_DECK,0,nil,e,tp)
		return res and rea and not Duel.IsPlayerAffectedByEffect(tp,59822133) and g:CheckSubGroupEach(s.spchecks)
	end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,4,tp,LOCATION_EXTRA+LOCATION_DECK)
end
function s.def(c)
    return c:IsFaceup() and c:IsDefenseAbove(1)
end
function s.pos(c)
    return c:IsFaceup() and c:IsAttackPos()
end
function s.neg(c)
	return c:IsType(TYPE_SPELL+TYPE_TRAP) and aux.NegateAnyFilter(c)
end
function s.atk(c)
    return c:IsFaceup() and c:IsAttackAbove(1)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
    local g1=Duel.GetMatchingGroup(s.def,tp,0,LOCATION_MZONE,nil)
    local g2=Duel.GetMatchingGroup(s.pos,tp,0,LOCATION_MZONE,nil)
	local g3=Duel.GetMatchingGroup(s.neg,tp,0,LOCATION_ONFIELD,nil)
    local g4=Duel.GetMatchingGroup(s.atk,tp,0,LOCATION_MZONE,nil)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)>=3 and Duel.GetLocationCountFromEx(tp)>0
     and not Duel.IsPlayerAffectedByEffect(tp,59822133)then
		local g=Duel.GetMatchingGroup(s.spfilter,tp,LOCATION_EXTRA+LOCATION_DECK,0,nil,e,tp)
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
		local sg=g:SelectSubGroupEach(tp,s.spchecks,false)
		if #sg>0 and Duel.SpecialSummon(sg,0,tp,tp,false,false,POS_FACEUP)~=0 then
            if #g1>0 then
            	local tc=g1:GetFirst()
	            while tc do
		        local e1=Effect.CreateEffect(e:GetHandler())
		        e1:SetType(EFFECT_TYPE_SINGLE)
		        e1:SetCode(EFFECT_UPDATE_DEFENSE)
		        e1:SetValue(-800)
		        tc:RegisterEffect(e1)
		        tc=g1:GetNext()
                end
	        end
            if #g2>0 then
                Duel.ChangePosition(g2,POS_FACEUP_DEFENSE,POS_FACEDOWN_DEFENSE,POS_FACEUP_DEFENSE,POS_FACEDOWN_DEFENSE)
            end
			if #g3>0 then
				Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DISABLE)
				local nt=Duel.SelectMatchingCard(tp,s.neg,tp,0,LOCATION_ONFIELD,1,1,nil)
				if nt:IsFaceup() and nt:IsRelateToEffect(e) and nt:IsCanBeDisabledByEffect(e,false) then
					Duel.NegateRelatedChain(nt,RESET_TURN_SET)
					local e1=Effect.CreateEffect(c)
					e1:SetType(EFFECT_TYPE_SINGLE)
					e1:SetCode(EFFECT_DISABLE)
					e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
					e1:SetReset(RESET_EVENT+RESETS_STANDARD)
					nt:RegisterEffect(e1)
					local e2=Effect.CreateEffect(c)
					e2:SetType(EFFECT_TYPE_SINGLE)
					e2:SetCode(EFFECT_DISABLE_EFFECT)
					e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
					e2:SetValue(RESET_TURN_SET)
					e2:SetReset(RESET_EVENT+RESETS_STANDARD)
					nt:RegisterEffect(e2)
					if tc:IsType(TYPE_TRAPMONSTER) then
						local e3=Effect.CreateEffect(c)
						e3:SetType(EFFECT_TYPE_SINGLE)
						e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
						e3:SetCode(EFFECT_DISABLE_TRAPMONSTER)
						e3:SetReset(RESET_EVENT+RESETS_STANDARD)
						nt:RegisterEffect(e3)
					end
				end
			end
            if #g4>0 then
                local tc=g4:GetFirst()
	            while tc do
		        local e1=Effect.CreateEffect(e:GetHandler())
		        e1:SetType(EFFECT_TYPE_SINGLE)
		        e1:SetCode(EFFECT_UPDATE_ATTACK)
		        e1:SetValue(-800)
		        tc:RegisterEffect(e1)
		        tc=g4:GetNext()
                end
            end
		end
	end
end
function s.indct(e,re,r,rp)
	if bit.band(r,REASON_BATTLE)~=0 then
		return 1
	else return 0 end
end
