name: Tri-Brigade
card_text: 2+ Beast, Beast-Warrior, and/or Winged Beast monsters
  During your opponent's Main Phase (Quick Effect): You can Special Summon 1 Level 4 or lower Beast, Beast-Warrior, or Winged Beast monster from your hand or GY, but negate its effects, also, return it to the hand during the End Phase. If this card is sent to the GY: You can make all monsters your opponent currently controls lose 300 ATK for each Monster Type you control, until the end of this turn. You can only use each effect of "Tri-Brigade Rugal the Silver Sheller" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_BEAST + RACE_BEASTWARRIOR + RACE_WINDBEAST), 2, 3)
  	c:EnableReviveLimit()
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetHintTiming(0, TIMING_MAIN_END)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--atkchange
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_ATKCHANGE)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.atktg)
  	e2:SetOperation(s.atkop)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() ~= tp and (Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_BEAST + RACE_BEASTWARRIOR + RACE_WINDBEAST) and c:IsLevelBelow(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	local tc = g:GetFirst()
  	if tc then
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_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)
  		Duel.SpecialSummonComplete()
  		local fid = e:GetHandler():GetFieldID()
  		tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 1, fid)
  		local e3 = Effect.CreateEffect(e:GetHandler())
  		e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e3:SetCode(EVENT_PHASE + PHASE_END)
  		e3:SetCountLimit(1)
  		e3:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e3:SetLabel(fid)
  		e3:SetLabelObject(tc)
  		e3:SetCondition(s.thcon)
  		e3:SetOperation(s.thop)
  		Duel.RegisterEffect(e3, tp)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:GetFlagEffectLabel(id) ~= e:GetLabel() then
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.SendtoHand(e:GetLabelObject(), nil, REASON_EFFECT)
  end
  function s.atkfilter(c)
  	return c:IsFaceup() and c:GetRace() ~= 0
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.atkfilter, tp, LOCATION_MZONE, 0, 1, nil) and Duel.IsExistingMatchingCard(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(s.atkfilter, tp, LOCATION_MZONE, 0, nil)
  	local ct = tg:GetClassCount(Card.GetRace)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	local tc = g:GetFirst()
  	while tc do
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetValue(-ct * 300)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		tc = g:GetNext()
  	end
  end
  
  
source_text: Tri-Brigade
  2+ Beast, Beast-Warrior, and/or Winged Beast monsters
  During your opponent's Main Phase (Quick Effect): You can Special Summon 1 Level 4 or lower Beast, Beast-Warrior, or Winged Beast monster from your hand or GY, but negate its effects, also, return it to the hand during the End Phase. If this card is sent to the GY: You can make all monsters your opponent currently controls lose 300 ATK for each Monster Type you control, until the end of this turn. You can only use each effect of "Tri-Brigade Rugal the Silver Sheller" once per turn.
target_text: -- Tri-Brigade
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--link summon
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_BEAST + RACE_BEASTWARRIOR + RACE_WINDBEAST), 2, 3)
  	c:EnableReviveLimit()
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetHintTiming(0, TIMING_MAIN_END)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--atkchange
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_ATKCHANGE)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetTarget(s.atktg)
  	e2:SetOperation(s.atkop)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() ~= tp and (Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsRace(RACE_BEAST + RACE_BEASTWARRIOR + RACE_WINDBEAST) and c:IsLevelBelow(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	local tc = g:GetFirst()
  	if tc then
  		Duel.SpecialSummonStep(tc, 0, tp, tp, false, false, POS_FACEUP)
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_DISABLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_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)
  		Duel.SpecialSummonComplete()
  		local fid = e:GetHandler():GetFieldID()
  		tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 1, fid)
  		local e3 = Effect.CreateEffect(e:GetHandler())
  		e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e3:SetCode(EVENT_PHASE + PHASE_END)
  		e3:SetCountLimit(1)
  		e3:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  		e3:SetLabel(fid)
  		e3:SetLabelObject(tc)
  		e3:SetCondition(s.thcon)
  		e3:SetOperation(s.thop)
  		Duel.RegisterEffect(e3, tp)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:GetFlagEffectLabel(id) ~= e:GetLabel() then
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.SendtoHand(e:GetLabelObject(), nil, REASON_EFFECT)
  end
  function s.atkfilter(c)
  	return c:IsFaceup() and c:GetRace() ~= 0
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.atkfilter, tp, LOCATION_MZONE, 0, 1, nil) and Duel.IsExistingMatchingCard(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(s.atkfilter, tp, LOCATION_MZONE, 0, nil)
  	local ct = tg:GetClassCount(Card.GetRace)
  	local g = Duel.GetMatchingGroup(Card.IsFaceup, tp, 0, LOCATION_MZONE, nil)
  	local tc = g:GetFirst()
  	while tc do
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_UPDATE_ATTACK)
  		e1:SetValue(-ct * 300)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		tc = g:GetNext()
  	end
  end
  
  