--コラプス・チェア
--Collapsing Chairs

--Substitute ID
local s,id=GetID()
function s.initial_effect(c)
	--Destroy all level 8 or lower monsters on the field
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_DESTROY)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_BATTLE_DESTROYED)
	e1:SetCondition(s.condition)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
end
	--If your attack position DARK monster with 0 ATK was destroyed by opponent's attack
function s.filter(c,tp)
	return c:IsAttribute(ATTRIBUTE_DARK) and c:GetPreviousAttackOnField()==0 and c:IsPreviousPosition(POS_ATTACK)
		and c:GetReasonPlayer()==1-tp and c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE)
		and (c:IsReason(REASON_BATTLE) and Duel.GetAttacker():IsControler(1-tp))
end
	--If it ever happened
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	return eg:IsExists(s.filter,1,nil,tp)
end
	--Check for level 8 or lower attack position monsters
function s.desfilter(c)
	return c:IsAttackPos() and c:IsLevelBelow(8) and c:IsNotMaximumModeSide()
end
	--Activation legality
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsExistingMatchingCard(s.desfilter,tp,LOCATION_MZONE,LOCATION_MZONE,1,nil) end
	local g=Duel.GetMatchingGroup(s.desfilter,tp,LOCATION_MZONE,LOCATION_MZONE,nil)
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,#g,0,0)
end
	--Destroy all level 8 or lower monsters on the field
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(s.desfilter,tp,LOCATION_MZONE,LOCATION_MZONE,nil)
	if #g>0 then
		ct=Duel.Destroy(g,REASON_EFFECT)
		if ct>0 then
			Duel.BreakEffect()
			Duel.DiscardDeck(tp,ct,REASON_EFFECT)
			Duel.DiscardDeck(1-tp,ct,REASON_EFFECT)
		end
	end
end