name: Spellbook
card_text: Target 1 Spellcaster-Type Xyz Monster in your Graveyard and up to 2 of your banished "Spellbook" Spell Cards; Special Summon that monster, and if you do, attach the targeted "Spellbook" Spell Card(s) to it as Xyz Material. You can only activate 1 "Spellbook of Miracles" 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)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	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.filter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.filter2(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL) and c:IsCanOverlay()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  			and Duel.IsExistingTarget(s.filter2, tp, LOCATION_REMOVED, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g1 = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	e:SetLabelObject(g1:GetFirst())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g2 = Duel.SelectTarget(tp, s.filter2, tp, LOCATION_REMOVED, 0, 1, 2, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g1, 1, 0, 0)
  end
  function s.ovfilter(c, e)
  	return c:IsRelateToEffect(e) and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL) and c:IsCanOverlay()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc = e:GetLabelObject()
  	local sg = g:Filter(s.ovfilter, tc, e)
  	if tc:IsRelateToEffect(e) and Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		Duel.BreakEffect()
  		if sg:GetCount() > 0 then
  			Duel.Overlay(tc, sg)
  		end
  	end
  end
  
  
source_text: Spellbook
  Target 1 Spellcaster-Type Xyz Monster in your Graveyard and up to 2 of your banished "Spellbook" Spell Cards; Special Summon that monster, and if you do, attach the targeted "Spellbook" Spell Card(s) to it as Xyz Material. You can only activate 1 "Spellbook of Miracles" 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)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	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.filter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:IsRace(RACE_SPELLCASTER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.filter2(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL) and c:IsCanOverlay()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  			and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  			and Duel.IsExistingTarget(s.filter2, tp, LOCATION_REMOVED, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g1 = Duel.SelectTarget(tp, s.filter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	e:SetLabelObject(g1:GetFirst())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g2 = Duel.SelectTarget(tp, s.filter2, tp, LOCATION_REMOVED, 0, 1, 2, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g1, 1, 0, 0)
  end
  function s.ovfilter(c, e)
  	return c:IsRelateToEffect(e) and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL) and c:IsCanOverlay()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc = e:GetLabelObject()
  	local sg = g:Filter(s.ovfilter, tc, e)
  	if tc:IsRelateToEffect(e) and Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		Duel.BreakEffect()
  		if sg:GetCount() > 0 then
  			Duel.Overlay(tc, sg)
  		end
  	end
  end
  
  