Bujintei
2 Level 4 LIGHT monsters
Once per turn: You can detach 1 material from this card; send your entire hand to the GY (min. 1), and if you do, draw 2 cards. When this card you controlled while face-up leaves the field because of an opponent's card effect: You can target Level 4 Beast-Warrior "Bujin" monsters in your GY, up to the number of Xyz Materials this card had on the field; Special Summon those targets. You can only control 1 "Bujintei Tsukuyomi".
local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
local SETCARD_ID_0 = 0x88 --Bujin

local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
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.IsAttribute, ATTRIBUTE_LIGHT), 4, 2)
	c:EnableReviveLimit()
	--draw
	local e1 = Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id, 0))
	e1:SetCategory(CATEGORY_TOGRAVE + CATEGORY_DRAW)
	e1:SetType(EFFECT_TYPE_IGNITION)
	e1:SetRange(LOCATION_MZONE)
	e1:SetCountLimit(1)
	e1:SetCost(s.cost)
	e1:SetTarget(s.target)
	e1:SetOperation(s.operation)
	c:RegisterEffect(e1)
	--spsummon
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 1))
	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e2:SetCode(EVENT_LEAVE_FIELD)
	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
	e2:SetCondition(s.spcon)
	e2:SetTarget(s.sptg)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)
end
function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
	end
	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
end
function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsPlayerCanDraw(tp, 2) and Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0) > 0
	end
	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, 0, LOCATION_HAND)
end
function s.operation(e, tp, eg, ep, ev, re, r, rp)
	local g = Duel.GetFieldGroup(tp, LOCATION_HAND, 0)
	if g:GetCount() > 0 then
		Duel.SendtoGrave(g, REASON_EFFECT)
		Duel.Draw(tp, 2, REASON_EFFECT)
	end
end
function s.spcon(e, tp, eg, ep, ev, re, r, rp)
	local ct = e:GetHandler():GetPreviousOverlayCountOnField()
	e:SetLabel(ct)
	return rp == 1 - tp and bit.band(r, REASON_EFFECT) ~= 0 and ct > 0 and e:GetHandler():IsPreviousPosition(POS_FACEUP)
end
function s.spfilter(c, e, tp)
	return c:IsLevel(4) and c:IsSetCard(SETCARD_ID_0) and c:IsRace(RACE_BEASTWARRIOR) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
	if chkc then
		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
	end
	if chk == 0 then
		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
	end
	local ct = e:GetLabel()
	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
	if ct > ft then
		ct = ft
	end
	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
		ct = 1
	end
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, ct, nil, e, tp)
	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, g:GetCount(), 0, 0)
end
function s.spop(e, tp, eg, ep, ev, re, r, rp)
	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
	if ft <= 0 then
		return
	end
	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
		ft = 1
	end
	if ft < g:GetCount() then
		return
	end
	if g:GetCount() > 0 then
		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
	end
end


Bujintei
2 Level 4 LIGHT monsters
Once per turn: You can detach 1 material from this card; send your entire hand to the GY (min. 1), and if you do, draw 2 cards. When this card you controlled while face-up leaves the field because of an opponent's card effect: You can target Level 4 Beast-Warrior "Bujin" monsters in your GY, up to the number of Xyz Materials this card had on the field; Special Summon those targets. You can only control 1 "Bujintei Tsukuyomi".
-- Bujintei
local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
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.IsAttribute, ATTRIBUTE_LIGHT), 4, 2)
	c:EnableReviveLimit()
	--draw
	local e1 = Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id, 0))
	e1:SetCategory(CATEGORY_TOGRAVE + CATEGORY_DRAW)
	e1:SetType(EFFECT_TYPE_IGNITION)
	e1:SetRange(LOCATION_MZONE)
	e1:SetCountLimit(1)
	e1:SetCost(s.cost)
	e1:SetTarget(s.target)
	e1:SetOperation(s.operation)
	c:RegisterEffect(e1)
	--spsummon
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 1))
	e2:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e2:SetCode(EVENT_LEAVE_FIELD)
	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
	e2:SetCondition(s.spcon)
	e2:SetTarget(s.sptg)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2)
end
function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
	end
	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
end
function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsPlayerCanDraw(tp, 2) and Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0) > 0
	end
	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, 0, LOCATION_HAND)
end
function s.operation(e, tp, eg, ep, ev, re, r, rp)
	local g = Duel.GetFieldGroup(tp, LOCATION_HAND, 0)
	if g:GetCount() > 0 then
		Duel.SendtoGrave(g, REASON_EFFECT)
		Duel.Draw(tp, 2, REASON_EFFECT)
	end
end
function s.spcon(e, tp, eg, ep, ev, re, r, rp)
	local ct = e:GetHandler():GetPreviousOverlayCountOnField()
	e:SetLabel(ct)
	return rp == 1 - tp and bit.band(r, REASON_EFFECT) ~= 0 and ct > 0 and e:GetHandler():IsPreviousPosition(POS_FACEUP)
end
function s.spfilter(c, e, tp)
	return c:IsLevel(4) and c:IsSetCard(SETCARD_ID_0) and c:IsRace(RACE_BEASTWARRIOR) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
	if chkc then
		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
	end
	if chk == 0 then
		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
	end
	local ct = e:GetLabel()
	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
	if ct > ft then
		ct = ft
	end
	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
		ct = 1
	end
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, ct, nil, e, tp)
	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, g:GetCount(), 0, 0)
end
function s.spop(e, tp, eg, ep, ev, re, r, rp)
	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
	if ft <= 0 then
		return
	end
	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
		ft = 1
	end
	if ft < g:GetCount() then
		return
	end
	if g:GetCount() > 0 then
		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
	end
end

