name: Bujintei
card_text: 2 Level 4 Beast-Warrior-Type monsters
  When this card is Xyz Summoned: Send the top 5 cards of your Deck to the Graveyard (or your entire Deck, if less than 5), also this card gains 100 ATK for each "Bujin" card sent to the Graveyard by this effect. If a "Bujin" Beast-Warrior-Type monster(s) you control would be destroyed by battle or by card effect, you can detach 1 Xyz Material from this card instead of destroying 1 of those monsters. You can only control 1 "Bujintei Kagutsuchi".
external_ids: local SETCARD_ID_0 = 0x88 --Bujin
  
lua: local SETCARD_ID_0 = 0x88 --Bujin
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:SetUniqueOnField(1, 0, id)
  	--xyz summon
  	aux.AddXyzProcedure(c, aux.FilterBoolFunction(Card.IsRace, RACE_BEASTWARRIOR), 4, 2)
  	c:EnableReviveLimit()
  	--
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCategory(CATEGORY_DECKDES)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCondition(s.discon)
  	e1:SetTarget(s.distg)
  	e1:SetOperation(s.disop)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	c:RegisterEffect(e2)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_XYZ)
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, tp, 5)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.DiscardDeck(tp, 5, REASON_EFFECT)
  	local c = e:GetHandler()
  	if c:IsFaceup() and c:IsRelateToEffect(e) then
  		local ct = Duel.GetOperatedGroup():FilterCount(Card.IsSetCard, nil, 0x88)
  		if ct > 0 then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_ATTACK)
  			e1:SetValue(ct * 100)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  			c:RegisterEffect(e1)
  		end
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and c:IsControler(tp) and c:IsLocation(LOCATION_MZONE) and c:IsSetCard(SETCARD_ID_0) and c:IsRace(RACE_BEASTWARRIOR)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return eg:IsExists(s.repfilter, 1, nil, tp)
  	end
  	if e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_EFFECT) and Duel.SelectEffectYesNo(tp, e:GetHandler(), 96) then
  		e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_EFFECT)
  		local g = eg:Filter(s.repfilter, nil, tp)
  		if g:GetCount() == 1 then
  			e:SetLabelObject(g:GetFirst())
  		else
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESREPLACE)
  			local cg = g:Select(tp, 1, 1, nil)
  			e:SetLabelObject(cg:GetFirst())
  		end
  		return true
  	else
  		return false
  	end
  end
  function s.repval(e, c)
  	return c == e:GetLabelObject()
  end
  
  
source_text: Bujintei
  2 Level 4 Beast-Warrior-Type monsters
  When this card is Xyz Summoned: Send the top 5 cards of your Deck to the Graveyard (or your entire Deck, if less than 5), also this card gains 100 ATK for each "Bujin" card sent to the Graveyard by this effect. If a "Bujin" Beast-Warrior-Type monster(s) you control would be destroyed by battle or by card effect, you can detach 1 Xyz Material from this card instead of destroying 1 of those monsters. You can only control 1 "Bujintei Kagutsuchi".
target_text: -- Bujintei
  local SETCARD_ID_0 = 0x88 --Bujin
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:SetUniqueOnField(1, 0, id)
  	--xyz summon
  	aux.AddXyzProcedure(c, aux.FilterBoolFunction(Card.IsRace, RACE_BEASTWARRIOR), 4, 2)
  	c:EnableReviveLimit()
  	--
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCategory(CATEGORY_DECKDES)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetCondition(s.discon)
  	e1:SetTarget(s.distg)
  	e1:SetOperation(s.disop)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	c:RegisterEffect(e2)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_XYZ)
  end
  function s.distg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, tp, 5)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.DiscardDeck(tp, 5, REASON_EFFECT)
  	local c = e:GetHandler()
  	if c:IsFaceup() and c:IsRelateToEffect(e) then
  		local ct = Duel.GetOperatedGroup():FilterCount(Card.IsSetCard, nil, 0x88)
  		if ct > 0 then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetCode(EFFECT_UPDATE_ATTACK)
  			e1:SetValue(ct * 100)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  			c:RegisterEffect(e1)
  		end
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and c:IsControler(tp) and c:IsLocation(LOCATION_MZONE) and c:IsSetCard(SETCARD_ID_0) and c:IsRace(RACE_BEASTWARRIOR)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return eg:IsExists(s.repfilter, 1, nil, tp)
  	end
  	if e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_EFFECT) and Duel.SelectEffectYesNo(tp, e:GetHandler(), 96) then
  		e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_EFFECT)
  		local g = eg:Filter(s.repfilter, nil, tp)
  		if g:GetCount() == 1 then
  			e:SetLabelObject(g:GetFirst())
  		else
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESREPLACE)
  			local cg = g:Select(tp, 1, 1, nil)
  			e:SetLabelObject(cg:GetFirst())
  		end
  		return true
  	else
  		return false
  	end
  end
  function s.repval(e, c)
  	return c == e:GetLabelObject()
  end
  
  