name: Ghostrick
card_text: Special Summon 1 "Ghostrick" monster from your hand or GY, then you can change 1 face-down "Ghostrick" monster you control to face-up Attack Position. You can banish this card from your GY, then target 1 "Ghostrick" Xyz Monster you control; attach 1 "Ghostrick" card from your GY to that monster as material. You can only use each effect of "Ghostrick Shot" once per turn.
external_ids: local SETCARD_ID_0 = 0x8d --Ghostrick
  
lua: local SETCARD_ID_0 = 0x8d --Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_POSITION)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--grave
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.mattg)
  	e2:SetOperation(s.matop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) 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
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.cfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsFacedown() and c:IsCanChangePosition()
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	if
  		g:GetCount() > 0
  		and Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP) > 0
  		and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  		and Duel.SelectYesNo(tp, aux.Stringid(id, 2))
  	then
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local sg = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		Duel.ChangePosition(sg, POS_FACEUP_ATTACK)
  	end
  end
  function s.xyzfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:IsSetCard(SETCARD_ID_0)
  end
  function s.mfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanOverlay()
  end
  function s.mattg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsPosition(LOCATION_MZONE) and s.xyzfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.xyzfilter, tp, LOCATION_MZONE, 0, 1, nil) and Duel.IsExistingMatchingCard(s.mfilter, tp, LOCATION_GRAVE, 0, 1, e:GetHandler())
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	Duel.SelectTarget(tp, s.xyzfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.matop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.mfilter), tp, LOCATION_GRAVE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.Overlay(tc, g)
  		end
  	end
  end
  
  
source_text: Ghostrick
  Special Summon 1 "Ghostrick" monster from your hand or GY, then you can change 1 face-down "Ghostrick" monster you control to face-up Attack Position. You can banish this card from your GY, then target 1 "Ghostrick" Xyz Monster you control; attach 1 "Ghostrick" card from your GY to that monster as material. You can only use each effect of "Ghostrick Shot" once per turn.
target_text: -- Ghostrick
  local SETCARD_ID_0 = 0x8d --Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_POSITION)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--grave
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.mattg)
  	e2:SetOperation(s.matop)
  	c:RegisterEffect(e2)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) 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
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
  end
  function s.cfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsFacedown() and c:IsCanChangePosition()
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	if
  		g:GetCount() > 0
  		and Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP) > 0
  		and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  		and Duel.SelectYesNo(tp, aux.Stringid(id, 2))
  	then
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local sg = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		Duel.ChangePosition(sg, POS_FACEUP_ATTACK)
  	end
  end
  function s.xyzfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_XYZ) and c:IsSetCard(SETCARD_ID_0)
  end
  function s.mfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanOverlay()
  end
  function s.mattg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsPosition(LOCATION_MZONE) and s.xyzfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.xyzfilter, tp, LOCATION_MZONE, 0, 1, nil) and Duel.IsExistingMatchingCard(s.mfilter, tp, LOCATION_GRAVE, 0, 1, e:GetHandler())
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUP)
  	Duel.SelectTarget(tp, s.xyzfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  end
  function s.matop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  		local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.mfilter), tp, LOCATION_GRAVE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.Overlay(tc, g)
  		end
  	end
  end
  
  