name: The
card_text: Once per turn, during your Standby Phase, if you have a Spellcaster-Type monster on your side of the field or in your Graveyard: You can place 1 "Spellbook" Spell Card from your Graveyard on the bottom of your Deck, except "The Grand Spellbook Tower", then draw 1 card. When this card in your possession is destroyed by your opponent's card and sent to your Graveyard: You can Special Summon 1 Spellcaster-Type monster from your hand or Deck whose Level is less than or equal to the number of "Spellbook" Spell Cards in your Graveyard.
external_ids: local SETCARD_ID_0 = 0x106e --Spellbook
  
lua: local SETCARD_ID_0 = 0x106e --Spellbook
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCode(EVENT_PHASE + PHASE_STANDBY)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.drcon)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c)
  	return c:IsRace(RACE_SPELLCASTER) and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, 0, 1, nil)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsType(TYPE_SPELL) and c:IsAbleToDeck()
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 1) and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_GRAVE)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.HintSelection(g)
  	if g:GetCount() > 0 and Duel.SendtoDeck(g, nil, SEQ_DECKBOTTOM, REASON_EFFECT) > 0 then
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_DESTROY) and not e:GetHandler():IsReason(REASON_RULE) and rp == 1 - tp
  end
  function s.ctfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL)
  end
  function s.spfilter(c, e, tp, lv)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsLevelBelow(lv) 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
  		if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  			return false
  		end
  		local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_GRAVE, 0, nil)
  		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp, ct)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_HAND)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return false
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_GRAVE, 0, nil)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, 1, nil, e, tp, ct)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: The
  Once per turn, during your Standby Phase, if you have a Spellcaster-Type monster on your side of the field or in your Graveyard: You can place 1 "Spellbook" Spell Card from your Graveyard on the bottom of your Deck, except "The Grand Spellbook Tower", then draw 1 card. When this card in your possession is destroyed by your opponent's card and sent to your Graveyard: You can Special Summon 1 Spellcaster-Type monster from your hand or Deck whose Level is less than or equal to the number of "Spellbook" Spell Cards in your Graveyard.
target_text: -- The
  local SETCARD_ID_0 = 0x106e --Spellbook
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCode(EVENT_PHASE + PHASE_STANDBY)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.drcon)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.cfilter(c)
  	return c:IsRace(RACE_SPELLCASTER) and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE + LOCATION_GRAVE, 0, 1, nil)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsType(TYPE_SPELL) and c:IsAbleToDeck()
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 1) and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_GRAVE)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.HintSelection(g)
  	if g:GetCount() > 0 and Duel.SendtoDeck(g, nil, SEQ_DECKBOTTOM, REASON_EFFECT) > 0 then
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_DESTROY) and not e:GetHandler():IsReason(REASON_RULE) and rp == 1 - tp
  end
  function s.ctfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL)
  end
  function s.spfilter(c, e, tp, lv)
  	return c:IsRace(RACE_SPELLCASTER) and c:IsLevelBelow(lv) 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
  		if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  			return false
  		end
  		local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_GRAVE, 0, nil)
  		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, nil, e, tp, ct)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_HAND)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return false
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local ct = Duel.GetMatchingGroupCount(s.ctfilter, tp, LOCATION_GRAVE, 0, nil)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK + LOCATION_HAND, 0, 1, 1, nil, e, tp, ct)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  