name: Revenge
card_text: Target 1 face-up monster your opponent controls; equip this card to that target. It gains 500 ATK/DEF. If this card is sent to the GY because the equipped monster has left the field: Draw 1 card, or if you activated this card this turn, draw 2 cards then discard 1 card, instead. You can only use this effect of "Revenge Rally" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_EQUIP)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(TIMING_DAMAGE_STEP)
  	e1:SetCondition(aux.dscon)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.drcon)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local c = e:GetHandler()
  	local cid = Duel.GetChainInfo(0, CHAININFO_CHAIN_ID)
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_REMAIN_FIELD)
  	e1:SetProperty(EFFECT_FLAG_OATH + EFFECT_FLAG_CANNOT_DISABLE)
  	e1:SetReset(RESET_CHAIN)
  	c:RegisterEffect(e1)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_DISABLED)
  	e2:SetOperation(s.tgop)
  	e2:SetLabel(cid)
  	e2:SetReset(RESET_CHAIN)
  	Duel.RegisterEffect(e2, tp)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local cid = Duel.GetChainInfo(ev, CHAININFO_CHAIN_ID)
  	if cid ~= e:GetLabel() then
  		return
  	end
  	if e:GetOwner():IsRelateToChain(ev) then
  		e:GetOwner():CancelToGrave(false)
  	end
  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(1 - tp) and chkc:IsFaceup()
  	end
  	if chk == 0 then
  		return e:IsCostChecked() and Duel.IsExistingTarget(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  	Duel.SelectTarget(tp, Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_EQUIP, e:GetHandler(), 1, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsLocation(LOCATION_SZONE) then
  		return
  	end
  	if not c:IsRelateToEffect(e) or c:IsStatus(STATUS_LEAVE_CONFIRMED) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsControler(1 - tp) then
  		Duel.Equip(tp, c, tc)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCode(EFFECT_EQUIP_LIMIT)
  		e1:SetValue(s.eqlimit)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		c:RegisterEffect(e1)
  		local e2 = Effect.CreateEffect(c)
  		e2:SetType(EFFECT_TYPE_EQUIP)
  		e2:SetCode(EFFECT_UPDATE_ATTACK)
  		e2:SetValue(500)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		c:RegisterEffect(e2)
  		local e3 = e2:Clone()
  		e3:SetCode(EFFECT_UPDATE_DEFENSE)
  		c:RegisterEffect(e3)
  		c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_LEAVE - RESET_TOGRAVE + RESET_PHASE + PHASE_END, 0, 1)
  	else
  		c:CancelToGrave(false)
  	end
  end
  function s.eqlimit(e, c)
  	return e:GetHandler():GetEquipTarget() == c or c:IsControler(1 - e:GetHandlerPlayer())
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_LOST_TARGET) and not e:GetHandler():GetPreviousEquipTarget():IsLocation(LOCATION_ONFIELD + LOCATION_OVERLAY)
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	if e:GetHandler():GetFlagEffect(id) > 0 then
  		e:SetLabel(1)
  		Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
  		Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  	else
  		e:SetLabel(0)
  		Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  	end
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetLabel() == 1 then
  		if Duel.Draw(tp, 2, REASON_EFFECT) ~= 0 then
  			Duel.ShuffleHand(tp)
  			Duel.BreakEffect()
  			Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT + REASON_DISCARD)
  		end
  	else
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  end
  
  
source_text: Revenge
  Target 1 face-up monster your opponent controls; equip this card to that target. It gains 500 ATK/DEF. If this card is sent to the GY because the equipped monster has left the field: Draw 1 card, or if you activated this card this turn, draw 2 cards then discard 1 card, instead. You can only use this effect of "Revenge Rally" once per turn.
target_text: -- Revenge
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_EQUIP)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(TIMING_DAMAGE_STEP)
  	e1:SetCondition(aux.dscon)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.drcon)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local c = e:GetHandler()
  	local cid = Duel.GetChainInfo(0, CHAININFO_CHAIN_ID)
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_REMAIN_FIELD)
  	e1:SetProperty(EFFECT_FLAG_OATH + EFFECT_FLAG_CANNOT_DISABLE)
  	e1:SetReset(RESET_CHAIN)
  	c:RegisterEffect(e1)
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_DISABLED)
  	e2:SetOperation(s.tgop)
  	e2:SetLabel(cid)
  	e2:SetReset(RESET_CHAIN)
  	Duel.RegisterEffect(e2, tp)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local cid = Duel.GetChainInfo(ev, CHAININFO_CHAIN_ID)
  	if cid ~= e:GetLabel() then
  		return
  	end
  	if e:GetOwner():IsRelateToChain(ev) then
  		e:GetOwner():CancelToGrave(false)
  	end
  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(1 - tp) and chkc:IsFaceup()
  	end
  	if chk == 0 then
  		return e:IsCostChecked() and Duel.IsExistingTarget(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_EQUIP)
  	Duel.SelectTarget(tp, Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_EQUIP, e:GetHandler(), 1, 0, 0)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsLocation(LOCATION_SZONE) then
  		return
  	end
  	if not c:IsRelateToEffect(e) or c:IsStatus(STATUS_LEAVE_CONFIRMED) then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsControler(1 - tp) then
  		Duel.Equip(tp, c, tc)
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  		e1:SetCode(EFFECT_EQUIP_LIMIT)
  		e1:SetValue(s.eqlimit)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  		c:RegisterEffect(e1)
  		local e2 = Effect.CreateEffect(c)
  		e2:SetType(EFFECT_TYPE_EQUIP)
  		e2:SetCode(EFFECT_UPDATE_ATTACK)
  		e2:SetValue(500)
  		e2:SetReset(RESET_EVENT + RESETS_STANDARD)
  		c:RegisterEffect(e2)
  		local e3 = e2:Clone()
  		e3:SetCode(EFFECT_UPDATE_DEFENSE)
  		c:RegisterEffect(e3)
  		c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_LEAVE - RESET_TOGRAVE + RESET_PHASE + PHASE_END, 0, 1)
  	else
  		c:CancelToGrave(false)
  	end
  end
  function s.eqlimit(e, c)
  	return e:GetHandler():GetEquipTarget() == c or c:IsControler(1 - e:GetHandlerPlayer())
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_LOST_TARGET) and not e:GetHandler():GetPreviousEquipTarget():IsLocation(LOCATION_ONFIELD + LOCATION_OVERLAY)
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	if e:GetHandler():GetFlagEffect(id) > 0 then
  		e:SetLabel(1)
  		Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 2)
  		Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 1)
  	else
  		e:SetLabel(0)
  		Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  	end
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetLabel() == 1 then
  		if Duel.Draw(tp, 2, REASON_EFFECT) ~= 0 then
  			Duel.ShuffleHand(tp)
  			Duel.BreakEffect()
  			Duel.DiscardHand(tp, nil, 1, 1, REASON_EFFECT + REASON_DISCARD)
  		end
  	else
  		Duel.Draw(tp, 1, REASON_EFFECT)
  	end
  end
  
  