name: Torpedo
card_text: Target 1 WATER Xyz Monster you control; detach 1 material from it, inflict 400 damage to your opponent for each card in your hand, and if you do, that monster gains ATK equal to the damage inflicted, until the end of this turn. You can banish this card and 1 WATER Xyz Monster from your GY; draw 2 cards. You can only use 1 "Torpedo Takedown" effect per turn, and only once that turn.
lua: 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_DAMAGE + CATEGORY_ATKCHANGE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.drcost)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsFaceup() and c:IsAttribute(ATTRIBUTE_WATER) and c:IsType(TYPE_XYZ) and c:CheckRemoveOverlayCard(tp, 1, REASON_EFFECT)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local b = e:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler():IsLocation(LOCATION_HAND)
  	local ct = Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0)
  	if b then
  		ct = ct - 1
  	end
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.cfilter(chkc, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil, tp) and ct > 0
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, 1 - tp, ct * 400)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:RemoveOverlayCard(tp, 1, 1, REASON_EFFECT) then
  		local ct = Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0)
  		if ct > 0 then
  			local atk = Duel.Damage(1 - tp, ct * 400, REASON_EFFECT)
  			if tc:IsFaceup() then
  				local e1 = Effect.CreateEffect(e:GetHandler())
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_UPDATE_ATTACK)
  				e1:SetValue(atk)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  				tc:RegisterEffect(e1)
  			end
  		end
  	end
  end
  function s.drfilter(c)
  	return c:IsAttribute(ATTRIBUTE_WATER) and c:IsType(TYPE_XYZ) and c:IsAbleToRemoveAsCost()
  end
  function s.drcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsAbleToRemoveAsCost() and Duel.IsExistingMatchingCard(s.drfilter, tp, LOCATION_GRAVE, 0, 1, c)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.drfilter, tp, LOCATION_GRAVE, 0, 1, 1, c)
  	g:AddCard(c)
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 2)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(2)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Draw(p, d, REASON_EFFECT)
  end
  
  
source_text: Torpedo
  Target 1 WATER Xyz Monster you control; detach 1 material from it, inflict 400 damage to your opponent for each card in your hand, and if you do, that monster gains ATK equal to the damage inflicted, until the end of this turn. You can banish this card and 1 WATER Xyz Monster from your GY; draw 2 cards. You can only use 1 "Torpedo Takedown" effect per turn, and only once that turn.
target_text: -- Torpedo
  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_DAMAGE + CATEGORY_ATKCHANGE)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.drcost)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c, tp)
  	return c:IsFaceup() and c:IsAttribute(ATTRIBUTE_WATER) and c:IsType(TYPE_XYZ) and c:CheckRemoveOverlayCard(tp, 1, REASON_EFFECT)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local b = e:IsHasType(EFFECT_TYPE_ACTIVATE) and e:GetHandler():IsLocation(LOCATION_HAND)
  	local ct = Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0)
  	if b then
  		ct = ct - 1
  	end
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.cfilter(chkc, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil, tp) and ct > 0
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, 1 - tp, ct * 400)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:RemoveOverlayCard(tp, 1, 1, REASON_EFFECT) then
  		local ct = Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0)
  		if ct > 0 then
  			local atk = Duel.Damage(1 - tp, ct * 400, REASON_EFFECT)
  			if tc:IsFaceup() then
  				local e1 = Effect.CreateEffect(e:GetHandler())
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_UPDATE_ATTACK)
  				e1:SetValue(atk)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  				tc:RegisterEffect(e1)
  			end
  		end
  	end
  end
  function s.drfilter(c)
  	return c:IsAttribute(ATTRIBUTE_WATER) and c:IsType(TYPE_XYZ) and c:IsAbleToRemoveAsCost()
  end
  function s.drcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsAbleToRemoveAsCost() and Duel.IsExistingMatchingCard(s.drfilter, tp, LOCATION_GRAVE, 0, 1, c)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.drfilter, tp, LOCATION_GRAVE, 0, 1, 1, c)
  	g:AddCard(c)
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 2)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetTargetParam(2)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	local p, d = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER, CHAININFO_TARGET_PARAM)
  	Duel.Draw(p, d, REASON_EFFECT)
  end
  
  