name: Barrage
card_text: Once per turn, you can detach any number of materials from Machine Xyz Monsters you control, then target that many cards on the field; destroy them. If a Machine Xyz Monster(s) you control is destroyed by battle or an opponent's card effect while this card is in your GY: You can banish this card and 1 Machine Xyz Monster from your GY; inflict damage to your opponent equal to the banished monster's Rank x 200.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--damage
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_DAMAGE)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetRange(LOCATION_GRAVE)
  	e3:SetCondition(s.damcon)
  	e3:SetCost(s.damcost)
  	e3:SetTarget(s.damtg)
  	e3:SetOperation(s.damop)
  	c:RegisterEffect(e3)
  end
  function s.rmfilter(c, tp)
  	return c:IsFaceup() and c:IsRace(RACE_MACHINE) and c:IsType(TYPE_XYZ) and c:CheckRemoveOverlayCard(tp, 1, REASON_COST)
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(100)
  	return true
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField()
  	end
  	if chk == 0 then
  		if e:GetLabel() == 100 then
  			e:SetLabel(0)
  			return Duel.IsExistingMatchingCard(s.rmfilter, tp, LOCATION_MZONE, 0, 1, tp) and Duel.IsExistingTarget(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  		else
  			return false
  		end
  	end
  	local rt = Duel.GetTargetCount(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  	local ct = 0
  	local min = 1
  	while ct < rt do
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DEATTACHFROM)
  		local sg = Duel.SelectMatchingCard(tp, s.rmfilter, tp, LOCATION_MZONE, 0, min, 1, nil, tp)
  		if #sg == 0 then
  			break
  		end
  		sg:GetFirst():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  		ct = ct + 1
  		min = 0
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, ct, ct, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, g:GetCount(), 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	Duel.Destroy(g, REASON_EFFECT)
  end
  function s.cfilter(c, e, tp)
  	return (c:IsReason(REASON_BATTLE) or (c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp))
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsType(TYPE_XYZ)
  		and c:IsRace(RACE_MACHINE)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousControler(tp)
  end
  function s.damcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, e, tp)
  end
  function s.damfilter(c)
  	return c:IsType(TYPE_XYZ) and c:IsRace(RACE_MACHINE) and c:IsAbleToRemoveAsCost() and c:GetRank() > 0
  end
  function s.damcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsAbleToRemoveAsCost() and Duel.IsExistingMatchingCard(s.damfilter, tp, LOCATION_GRAVE, 0, 1, c)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.damfilter, tp, LOCATION_GRAVE, 0, 1, 1, c)
  	e:SetLabel(g:GetFirst():GetRank())
  	g:AddCard(c)
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.damtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetTargetPlayer(1 - tp)
  	Duel.SetTargetParam(e:GetLabel() * 200)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, 1 - tp, e:GetLabel() * 200)
  end
  function s.damop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Damage(p, d, REASON_EFFECT)
  end
  
  
source_text: Barrage
  Once per turn, you can detach any number of materials from Machine Xyz Monsters you control, then target that many cards on the field; destroy them. If a Machine Xyz Monster(s) you control is destroyed by battle or an opponent's card effect while this card is in your GY: You can banish this card and 1 Machine Xyz Monster from your GY; inflict damage to your opponent equal to the banished monster's Rank x 200.
target_text: -- Barrage
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMING_END_PHASE)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--damage
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_DAMAGE)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetRange(LOCATION_GRAVE)
  	e3:SetCondition(s.damcon)
  	e3:SetCost(s.damcost)
  	e3:SetTarget(s.damtg)
  	e3:SetOperation(s.damop)
  	c:RegisterEffect(e3)
  end
  function s.rmfilter(c, tp)
  	return c:IsFaceup() and c:IsRace(RACE_MACHINE) and c:IsType(TYPE_XYZ) and c:CheckRemoveOverlayCard(tp, 1, REASON_COST)
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(100)
  	return true
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField()
  	end
  	if chk == 0 then
  		if e:GetLabel() == 100 then
  			e:SetLabel(0)
  			return Duel.IsExistingMatchingCard(s.rmfilter, tp, LOCATION_MZONE, 0, 1, tp) and Duel.IsExistingTarget(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  		else
  			return false
  		end
  	end
  	local rt = Duel.GetTargetCount(aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, nil)
  	local ct = 0
  	local min = 1
  	while ct < rt do
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DEATTACHFROM)
  		local sg = Duel.SelectMatchingCard(tp, s.rmfilter, tp, LOCATION_MZONE, 0, min, 1, nil, tp)
  		if #sg == 0 then
  			break
  		end
  		sg:GetFirst():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  		ct = ct + 1
  		min = 0
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectTarget(tp, aux.TRUE, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, ct, ct, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, g:GetCount(), 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	Duel.Destroy(g, REASON_EFFECT)
  end
  function s.cfilter(c, e, tp)
  	return (c:IsReason(REASON_BATTLE) or (c:IsReason(REASON_EFFECT) and c:GetReasonPlayer() == 1 - tp))
  		and c:IsPreviousPosition(POS_FACEUP)
  		and c:IsType(TYPE_XYZ)
  		and c:IsRace(RACE_MACHINE)
  		and c:IsPreviousLocation(LOCATION_MZONE)
  		and c:IsPreviousControler(tp)
  end
  function s.damcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, e, tp)
  end
  function s.damfilter(c)
  	return c:IsType(TYPE_XYZ) and c:IsRace(RACE_MACHINE) and c:IsAbleToRemoveAsCost() and c:GetRank() > 0
  end
  function s.damcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsAbleToRemoveAsCost() and Duel.IsExistingMatchingCard(s.damfilter, tp, LOCATION_GRAVE, 0, 1, c)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.damfilter, tp, LOCATION_GRAVE, 0, 1, 1, c)
  	e:SetLabel(g:GetFirst():GetRank())
  	g:AddCard(c)
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.damtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetTargetPlayer(1 - tp)
  	Duel.SetTargetParam(e:GetLabel() * 200)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, 1 - tp, e:GetLabel() * 200)
  end
  function s.damop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Damage(p, d, REASON_EFFECT)
  end
  
  