name: Infinitrack
card_text: 3 Xyz Monsters
  Unaffected by other monsters' effects, except those of Xyz Monsters. Cannot be destroyed by battle, except with an Xyz Monster. You can target 1 Xyz Monster in your GY and 1 card your opponent controls; Special Summon that monster from your GY, and if you do, attach that opponent's card to it as material, also for the rest of this turn, any damage your opponent takes is halved. You can only use this effect of "Infinitrack Fortress Megaclops" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkType, TYPE_XYZ), 3, 3)
  	--immune effect
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_IMMUNE_EFFECT)
  	e1:SetValue(s.immunefilter)
  	c:RegisterEffect(e1)
  	--indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e2:SetValue(s.indes)
  	c:RegisterEffect(e2)
  	--special summon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.immunefilter(e, te)
  	return te:IsActiveType(TYPE_MONSTER) and te:GetOwner() ~= e:GetOwner() and not te:GetHandler():IsType(TYPE_XYZ)
  end
  function s.indes(e, c)
  	return not c:IsType(TYPE_XYZ)
  end
  function s.spfilter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(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.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  			and Duel.IsExistingTarget(Card.IsCanOverlay, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	e:SetLabelObject(g:GetFirst())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  	Duel.SelectTarget(tp, Card.IsCanOverlay, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local hc = e:GetLabelObject()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc = g:GetFirst()
  	if tc == hc then
  		tc = g:GetNext()
  	end
  	if hc:IsRelateToEffect(e) and Duel.SpecialSummon(hc, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		if tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) and tc:IsControler(1 - tp) and tc:IsCanOverlay() then
  			local og = tc:GetOverlayGroup()
  			if og:GetCount() > 0 then
  				Duel.SendtoGrave(og, REASON_RULE)
  			end
  			tc:CancelToGrave()
  			Duel.Overlay(hc, tc)
  		end
  	end
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_CHANGE_DAMAGE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetTargetRange(0, 1)
  	e1:SetValue(s.damval)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.damval(e, re, val, r, rp, rc)
  	return math.floor(val / 2)
  end
  
  
source_text: Infinitrack
  3 Xyz Monsters
  Unaffected by other monsters' effects, except those of Xyz Monsters. Cannot be destroyed by battle, except with an Xyz Monster. You can target 1 Xyz Monster in your GY and 1 card your opponent controls; Special Summon that monster from your GY, and if you do, attach that opponent's card to it as material, also for the rest of this turn, any damage your opponent takes is halved. You can only use this effect of "Infinitrack Fortress Megaclops" once per turn.
target_text: -- Infinitrack
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkType, TYPE_XYZ), 3, 3)
  	--immune effect
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_IMMUNE_EFFECT)
  	e1:SetValue(s.immunefilter)
  	c:RegisterEffect(e1)
  	--indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e2:SetValue(s.indes)
  	c:RegisterEffect(e2)
  	--special summon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.immunefilter(e, te)
  	return te:IsActiveType(TYPE_MONSTER) and te:GetOwner() ~= e:GetOwner() and not te:GetHandler():IsType(TYPE_XYZ)
  end
  function s.indes(e, c)
  	return not c:IsType(TYPE_XYZ)
  end
  function s.spfilter(c, e, tp)
  	return c:IsType(TYPE_XYZ) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(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.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  			and Duel.IsExistingTarget(Card.IsCanOverlay, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	e:SetLabelObject(g:GetFirst())
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_XMATERIAL)
  	Duel.SelectTarget(tp, Card.IsCanOverlay, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local hc = e:GetLabelObject()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc = g:GetFirst()
  	if tc == hc then
  		tc = g:GetNext()
  	end
  	if hc:IsRelateToEffect(e) and Duel.SpecialSummon(hc, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		if tc:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) and tc:IsControler(1 - tp) and tc:IsCanOverlay() then
  			local og = tc:GetOverlayGroup()
  			if og:GetCount() > 0 then
  				Duel.SendtoGrave(og, REASON_RULE)
  			end
  			tc:CancelToGrave()
  			Duel.Overlay(hc, tc)
  		end
  	end
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_CHANGE_DAMAGE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetTargetRange(0, 1)
  	e1:SetValue(s.damval)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.damval(e, re, val, r, rp, rc)
  	return math.floor(val / 2)
  end
  
  