name: Spellbook
card_text: During the End Phase of the turn this card was activated, add "Spellbook" Spells from your Deck to your hand, except "Spellbook of Judgment", up to the number of Spell Cards activated after this card's resolution, then, you can Special Summon from your Deck 1 Spellcaster monster whose Level is less than or equal to the number of cards added to your hand by this effect. You can only activate 1 "Spellbook of Judgment" per turn.
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:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_SEARCH + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCondition(s.regcon)
  	e1:SetOperation(s.regop1)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_NEGATED)
  	e2:SetCondition(s.regcon)
  	e2:SetOperation(s.regop2)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e2, tp)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.effcon)
  	e3:SetOperation(s.effop)
  	e3:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e3, tp)
  	e1:SetLabelObject(e3)
  	e2:SetLabelObject(e3)
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	return re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL)
  end
  function s.regop1(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetLabelObject():GetLabel()
  	e:GetLabelObject():SetLabel(ct + 1)
  end
  function s.regop2(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetLabelObject():GetLabel()
  	if ct == 0 then
  		ct = 1
  	end
  	e:GetLabelObject():SetLabel(ct - 1)
  end
  function s.effcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetLabel() > 0
  end
  function s.sfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsType(TYPE_SPELL) and c:IsAbleToHand()
  end
  function s.spfilter(c, lv, e, tp)
  	return c:IsLevelBelow(lv) and c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.effop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_CARD, 0, id)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.sfilter, tp, LOCATION_DECK, 0, 1, e:GetLabel(), nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  		if
  			Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK, 0, 1, nil, g:GetCount(), e, tp)
  			and Duel.SelectYesNo(tp, aux.Stringid(id, 1))
  		then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local sg = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK, 0, 1, 1, nil, g:GetCount(), e, tp)
  			Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  
  
source_text: Spellbook
  During the End Phase of the turn this card was activated, add "Spellbook" Spells from your Deck to your hand, except "Spellbook of Judgment", up to the number of Spell Cards activated after this card's resolution, then, you can Special Summon from your Deck 1 Spellcaster monster whose Level is less than or equal to the number of cards added to your hand by this effect. You can only activate 1 "Spellbook of Judgment" per turn.
target_text: -- Spellbook
  local SETCARD_ID_0 = 0x106e --Spellbook
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_SEARCH + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCondition(s.regcon)
  	e1:SetOperation(s.regop1)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_NEGATED)
  	e2:SetCondition(s.regcon)
  	e2:SetOperation(s.regop2)
  	e2:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e2, tp)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.effcon)
  	e3:SetOperation(s.effop)
  	e3:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e3, tp)
  	e1:SetLabelObject(e3)
  	e2:SetLabelObject(e3)
  end
  function s.regcon(e, tp, eg, ep, ev, re, r, rp)
  	return re:IsHasType(EFFECT_TYPE_ACTIVATE) and re:IsActiveType(TYPE_SPELL)
  end
  function s.regop1(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetLabelObject():GetLabel()
  	e:GetLabelObject():SetLabel(ct + 1)
  end
  function s.regop2(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetLabelObject():GetLabel()
  	if ct == 0 then
  		ct = 1
  	end
  	e:GetLabelObject():SetLabel(ct - 1)
  end
  function s.effcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetLabel() > 0
  end
  function s.sfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsType(TYPE_SPELL) and c:IsAbleToHand()
  end
  function s.spfilter(c, lv, e, tp)
  	return c:IsLevelBelow(lv) and c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.effop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_CARD, 0, id)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.sfilter, tp, LOCATION_DECK, 0, 1, e:GetLabel(), nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  		if
  			Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK, 0, 1, nil, g:GetCount(), e, tp)
  			and Duel.SelectYesNo(tp, aux.Stringid(id, 1))
  		then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local sg = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK, 0, 1, 1, nil, g:GetCount(), e, tp)
  			Duel.SpecialSummon(sg, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  
  