name: Brigrand
card_text: "Fallen of Albaz" + 1 Level 8 or higher monster
  Cannot be destroyed by battle. While you control this Fusion Summoned card, your opponent cannot target other monsters you control with monster effects. During the End Phase, if this card is in the GY because it was sent there this turn: You can add to your hand, or Special Summon, 1 "Tri-Brigade" monster or 1 "Fallen of Albaz" from your Deck. You can only use this effect of "Brigrand the Glory Dragon" once per turn.
external_ids: local CARD_ID_0 = 68468459 --Fallen of Albaz
  local SETCARD_ID_0 = 0x14d --Tri-Brigade
  
lua: local CARD_ID_0 = 68468459 --Fallen of Albaz
  local SETCARD_ID_0 = 0x14d --Tri-Brigade
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--fusion material
  	c:EnableReviveLimit()
  	aux.AddFusionProcCodeFun(c, CARD_ID_0, aux.FilterBoolFunction(Card.IsLevelAbove, 8), 1, true, true)
  	--indes battle
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e1:SetValue(1)
  	c:RegisterEffect(e1)
  	--cannot be target
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
  	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE + EFFECT_FLAG_SET_AVAILABLE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetCondition(s.imcon)
  	e2:SetTarget(s.imval)
  	e2:SetValue(s.imfilter)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetOperation(s.regop)
  	c:RegisterEffect(e3)
  	--to hand/spsummon
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetCategory(CATEGORY_SEARCH + CATEGORY_SPECIAL_SUMMON + CATEGORY_DECKDES)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_PHASE + PHASE_END)
  	e4:SetRange(LOCATION_GRAVE)
  	e4:SetCountLimit(1, id)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	c:RegisterEffect(e4)
  end
  function s.branded_fusion_check(tp, sg, fc)
  	return aux.gffcheck(sg, Card.IsFusionCode, CARD_ID_0, Card.IsLevelAbove, 8)
  end
  function s.imcon(e)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_FUSION)
  end
  function s.imval(e, c)
  	return c ~= e:GetHandler()
  end
  function s.imfilter(e, re, rp)
  	return aux.tgoval(e, re, rp) and re:IsActiveType(TYPE_MONSTER)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFlagEffect(id) > 0
  end
  function s.thfilter(c, e, tp)
  	if not (c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) or c:IsCode(CARD_ID_0)) then
  		return false
  	end
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	return c:IsAbleToHand() or (ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false))
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  	end
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local tc = g:GetFirst()
  	if tc then
  		if tc:IsAbleToHand() and (not tc:IsCanBeSpecialSummoned(e, 0, tp, false, false) or ft <= 0 or Duel.SelectOption(tp, 1190, 1152) == 0) then
  			Duel.SendtoHand(tc, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tc)
  		else
  			Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  
  
source_text: Brigrand
  "Fallen of Albaz" + 1 Level 8 or higher monster
  Cannot be destroyed by battle. While you control this Fusion Summoned card, your opponent cannot target other monsters you control with monster effects. During the End Phase, if this card is in the GY because it was sent there this turn: You can add to your hand, or Special Summon, 1 "Tri-Brigade" monster or 1 "Fallen of Albaz" from your Deck. You can only use this effect of "Brigrand the Glory Dragon" once per turn.
target_text: -- Brigrand
  local CARD_ID_0 = 68468459 --Fallen of Albaz
  local SETCARD_ID_0 = 0x14d --Tri-Brigade
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--fusion material
  	c:EnableReviveLimit()
  	aux.AddFusionProcCodeFun(c, CARD_ID_0, aux.FilterBoolFunction(Card.IsLevelAbove, 8), 1, true, true)
  	--indes battle
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e1:SetValue(1)
  	c:RegisterEffect(e1)
  	--cannot be target
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
  	e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE + EFFECT_FLAG_SET_AVAILABLE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTargetRange(LOCATION_MZONE, 0)
  	e2:SetCondition(s.imcon)
  	e2:SetTarget(s.imval)
  	e2:SetValue(s.imfilter)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_CONTINUOUS)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetOperation(s.regop)
  	c:RegisterEffect(e3)
  	--to hand/spsummon
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetCategory(CATEGORY_SEARCH + CATEGORY_SPECIAL_SUMMON + CATEGORY_DECKDES)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_PHASE + PHASE_END)
  	e4:SetRange(LOCATION_GRAVE)
  	e4:SetCountLimit(1, id)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	c:RegisterEffect(e4)
  end
  function s.branded_fusion_check(tp, sg, fc)
  	return aux.gffcheck(sg, Card.IsFusionCode, CARD_ID_0, Card.IsLevelAbove, 8)
  end
  function s.imcon(e)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_FUSION)
  end
  function s.imval(e, c)
  	return c ~= e:GetHandler()
  end
  function s.imfilter(e, re, rp)
  	return aux.tgoval(e, re, rp) and re:IsActiveType(TYPE_MONSTER)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1)
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetFlagEffect(id) > 0
  end
  function s.thfilter(c, e, tp)
  	if not (c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) or c:IsCode(CARD_ID_0)) then
  		return false
  	end
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	return c:IsAbleToHand() or (ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false))
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp)
  	end
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e, tp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local tc = g:GetFirst()
  	if tc then
  		if tc:IsAbleToHand() and (not tc:IsCanBeSpecialSummoned(e, 0, tp, false, false) or ft <= 0 or Duel.SelectOption(tp, 1190, 1152) == 0) then
  			Duel.SendtoHand(tc, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tc)
  		else
  			Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  
  