name: Goblin
card_text: Target 1 Xyz Monster you control; Special Summon 1 "Goblin" Xyz Monster from your Extra Deck, by using that target as material, and if you do, attach this card to it as material. (This is treated as an Xyz Summon. Transfer its materials to the Summoned monster.) You can banish this card from your GY, then target 1 "Goblin" Xyz Monster you control and 1 card in either GY; attach the targeted card from the GY to that monster on the field as material. You can only use each effect of "Goblin Biker Grand Pileup" once per turn.
external_ids: local SETCARD_ID_0 = 0xac --Goblin
  
lua: local SETCARD_ID_0 = 0xac --Goblin
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--material
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.mattg)
  	e2:SetOperation(s.matop)
  	c:RegisterEffect(e2)
  end
  function s.filter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:IsFaceup() and aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL) and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c)
  end
  function s.filter2(c, e, tp, mc)
  	return c:IsSetCard(SETCARD_ID_0) and mc:IsCanBeXyzMaterial(c) and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false) and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return e:GetHandler():IsCanOverlay() and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if not aux.MustMaterialCheck(tc, tp, EFFECT_MUST_BE_XMATERIAL) then
  		return
  	end
  	if tc:IsFacedown() or not tc:IsRelateToEffect(e) or tc:IsControler(1 - tp) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc)
  	local sc = g:GetFirst()
  	if sc then
  		local mg = tc:GetOverlayGroup()
  		if mg:GetCount() ~= 0 then
  			Duel.Overlay(sc, mg)
  		end
  		sc:SetMaterial(Group.FromCards(tc))
  		Duel.Overlay(sc, Group.FromCards(tc))
  		if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) > 0 then
  			sc:CompleteProcedure()
  			if c:IsRelateToEffect(e) then
  				c:CancelToGrave()
  				Duel.Overlay(sc, c)
  			end
  		end
  	end
  end
  function s.mfilter(c, e, tp)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:IsSetCard(SETCARD_ID_0)
  end
  function s.ofilter(c)
  	return c:IsCanOverlay() and not c:IsForbidden()
  end
  function s.mattg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.mfilter, tp, LOCATION_MZONE, 0, 1, nil, e, tp) and Duel.IsExistingTarget(s.ofilter, tp, LOCATION_GRAVE, LOCATION_GRAVE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	local g1 = Duel.SelectTarget(tp, s.mfilter, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  	local g2 = Duel.SelectTarget(tp, s.ofilter, tp, LOCATION_GRAVE, LOCATION_GRAVE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g2, 1, 0, 0)
  end
  function s.matop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tg = Duel.GetTargetsRelateToChain()
  	local tc1 = tg:Filter(Card.IsLocation, nil, LOCATION_MZONE):GetFirst()
  	local tc2 = tg:Filter(Card.IsLocation, nil, LOCATION_GRAVE):GetFirst()
  	if tc1 and tc1:IsControler(tp) and tc2 then
  		Duel.Overlay(tc1, tc2)
  	end
  end
  
  
source_text: Goblin
  Target 1 Xyz Monster you control; Special Summon 1 "Goblin" Xyz Monster from your Extra Deck, by using that target as material, and if you do, attach this card to it as material. (This is treated as an Xyz Summon. Transfer its materials to the Summoned monster.) You can banish this card from your GY, then target 1 "Goblin" Xyz Monster you control and 1 card in either GY; attach the targeted card from the GY to that monster on the field as material. You can only use each effect of "Goblin Biker Grand Pileup" once per turn.
target_text: -- Goblin
  local SETCARD_ID_0 = 0xac --Goblin
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--material
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.mattg)
  	e2:SetOperation(s.matop)
  	c:RegisterEffect(e2)
  end
  function s.filter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:IsFaceup() and aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL) and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c)
  end
  function s.filter2(c, e, tp, mc)
  	return c:IsSetCard(SETCARD_ID_0) and mc:IsCanBeXyzMaterial(c) and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false) and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return e:GetHandler():IsCanOverlay() and Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if not aux.MustMaterialCheck(tc, tp, EFFECT_MUST_BE_XMATERIAL) then
  		return
  	end
  	if tc:IsFacedown() or not tc:IsRelateToEffect(e) or tc:IsControler(1 - tp) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc)
  	local sc = g:GetFirst()
  	if sc then
  		local mg = tc:GetOverlayGroup()
  		if mg:GetCount() ~= 0 then
  			Duel.Overlay(sc, mg)
  		end
  		sc:SetMaterial(Group.FromCards(tc))
  		Duel.Overlay(sc, Group.FromCards(tc))
  		if Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP) > 0 then
  			sc:CompleteProcedure()
  			if c:IsRelateToEffect(e) then
  				c:CancelToGrave()
  				Duel.Overlay(sc, c)
  			end
  		end
  	end
  end
  function s.mfilter(c, e, tp)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:IsSetCard(SETCARD_ID_0)
  end
  function s.ofilter(c)
  	return c:IsCanOverlay() and not c:IsForbidden()
  end
  function s.mattg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.mfilter, tp, LOCATION_MZONE, 0, 1, nil, e, tp) and Duel.IsExistingTarget(s.ofilter, tp, LOCATION_GRAVE, LOCATION_GRAVE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	local g1 = Duel.SelectTarget(tp, s.mfilter, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  	local g2 = Duel.SelectTarget(tp, s.ofilter, tp, LOCATION_GRAVE, LOCATION_GRAVE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g2, 1, 0, 0)
  end
  function s.matop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tg = Duel.GetTargetsRelateToChain()
  	local tc1 = tg:Filter(Card.IsLocation, nil, LOCATION_MZONE):GetFirst()
  	local tc2 = tg:Filter(Card.IsLocation, nil, LOCATION_GRAVE):GetFirst()
  	if tc1 and tc1:IsControler(tp) and tc2 then
  		Duel.Overlay(tc1, tc2)
  	end
  end
  
  