U.A.
At the start of the Damage Step, if your "U.A." monster battles an opponent's monster: You can banish that opponent's monster until your opponent's 2nd End Phase. You can only use this effect of "U.A. Penalty Box" once per turn. You can banish this card from your Graveyard; add 1 "U.A." Spell Card from your Deck to your hand.
local SETCARD_ID_0 = 0xb2 --U.A.

local SETCARD_ID_0 = 0xb2 --U.A.
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:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
	e1:SetHintTiming(TIMING_DAMAGE_STEP)
	e1:SetTarget(s.target)
	c:RegisterEffect(e1)
	--remove
	local e2 = Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_REMOVE)
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_BATTLE_START)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCountLimit(1, id)
	e2:SetCondition(s.rmcon)
	e2:SetTarget(s.rmtg)
	e2:SetOperation(s.rmop)
	c:RegisterEffect(e2)
	--to hand
	local e3 = Effect.CreateEffect(c)
	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetCode(EVENT_FREE_CHAIN)
	e3:SetRange(LOCATION_GRAVE)
	e3:SetCost(aux.bfgcost)
	e3:SetTarget(s.thtg)
	e3:SetOperation(s.thop)
	c:RegisterEffect(e3)
end
function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
	local b1 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE
	local b2 = Duel.CheckEvent(EVENT_BATTLE_START) and s.rmcon(e, tp, eg, ep, ev, re, r, rp) and s.rmtg(e, tp, eg, ep, ev, re, r, rp, 0)
	if chk == 0 then
		return b1 or b2
	end
	if b2 then
		e:SetCategory(CATEGORY_REMOVE)
		e:SetOperation(s.rmop)
		s.rmtg(e, tp, eg, ep, ev, re, r, rp, 1)
	else
		e:SetCategory(0)
		e:SetOperation(nil)
	end
end
function s.rmcon(e, tp, eg, ep, ev, re, r, rp)
	local tc = Duel.GetAttacker()
	local bc = Duel.GetAttackTarget()
	if not bc then
		return false
	end
	if tc:IsControler(1 - tp) then
		tc, bc = bc, tc
	end
	if tc:IsFaceup() and tc:IsSetCard(SETCARD_ID_0) then
		e:SetLabelObject(bc)
		return true
	else
		return false
	end
end
function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
	local bc = e:GetLabelObject()
	if chk == 0 then
		return bc:IsAbleToRemove() and Duel.GetFlagEffect(tp, id) == 0
	end
	Duel.SetOperationInfo(0, CATEGORY_REMOVE, bc, 1, 0, 0)
	Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
end
function s.rmop(e, tp, eg, ep, ev, re, r, rp)
	local bc = e:GetLabelObject()
	if bc:IsRelateToBattle() and bc:IsControler(1 - tp) and Duel.Remove(bc, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 then
		bc:SetTurnCounter(0)
		local e1 = Effect.CreateEffect(e:GetHandler())
		e1:SetDescription(aux.Stringid(id, 0))
		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
		e1:SetCode(EVENT_PHASE + PHASE_END)
		e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 2)
		e1:SetLabelObject(bc)
		e1:SetCountLimit(1)
		e1:SetCondition(s.retcon)
		e1:SetOperation(s.retop)
		Duel.RegisterEffect(e1, tp)
	end
end
function s.retcon(e, tp, eg, ep, ev, re, r, rp)
	return Duel.GetTurnPlayer() ~= tp
end
function s.retop(e, tp, eg, ep, ev, re, r, rp)
	local tc = e:GetLabelObject()
	local ct = tc:GetTurnCounter()
	ct = ct + 1
	tc:SetTurnCounter(ct)
	if ct == 2 then
		Duel.ReturnToField(tc)
	end
end
function s.thfilter(c)
	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL) and c:IsAbleToHand()
end
function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
end
function s.thop(e, tp, eg, ep, ev, re, r, rp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
	if g:GetCount() > 0 then
		Duel.SendtoHand(g, nil, REASON_EFFECT)
		Duel.ConfirmCards(1 - tp, g)
	end
end


U.A.
At the start of the Damage Step, if your "U.A." monster battles an opponent's monster: You can banish that opponent's monster until your opponent's 2nd End Phase. You can only use this effect of "U.A. Penalty Box" once per turn. You can banish this card from your Graveyard; add 1 "U.A." Spell Card from your Deck to your hand.
-- U.A.
local SETCARD_ID_0 = 0xb2 --U.A.
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:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
	e1:SetHintTiming(TIMING_DAMAGE_STEP)
	e1:SetTarget(s.target)
	c:RegisterEffect(e1)
	--remove
	local e2 = Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_REMOVE)
	e2:SetType(EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_BATTLE_START)
	e2:SetRange(LOCATION_SZONE)
	e2:SetCountLimit(1, id)
	e2:SetCondition(s.rmcon)
	e2:SetTarget(s.rmtg)
	e2:SetOperation(s.rmop)
	c:RegisterEffect(e2)
	--to hand
	local e3 = Effect.CreateEffect(c)
	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
	e3:SetType(EFFECT_TYPE_QUICK_O)
	e3:SetCode(EVENT_FREE_CHAIN)
	e3:SetRange(LOCATION_GRAVE)
	e3:SetCost(aux.bfgcost)
	e3:SetTarget(s.thtg)
	e3:SetOperation(s.thop)
	c:RegisterEffect(e3)
end
function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
	local b1 = Duel.GetCurrentPhase() ~= PHASE_DAMAGE
	local b2 = Duel.CheckEvent(EVENT_BATTLE_START) and s.rmcon(e, tp, eg, ep, ev, re, r, rp) and s.rmtg(e, tp, eg, ep, ev, re, r, rp, 0)
	if chk == 0 then
		return b1 or b2
	end
	if b2 then
		e:SetCategory(CATEGORY_REMOVE)
		e:SetOperation(s.rmop)
		s.rmtg(e, tp, eg, ep, ev, re, r, rp, 1)
	else
		e:SetCategory(0)
		e:SetOperation(nil)
	end
end
function s.rmcon(e, tp, eg, ep, ev, re, r, rp)
	local tc = Duel.GetAttacker()
	local bc = Duel.GetAttackTarget()
	if not bc then
		return false
	end
	if tc:IsControler(1 - tp) then
		tc, bc = bc, tc
	end
	if tc:IsFaceup() and tc:IsSetCard(SETCARD_ID_0) then
		e:SetLabelObject(bc)
		return true
	else
		return false
	end
end
function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
	local bc = e:GetLabelObject()
	if chk == 0 then
		return bc:IsAbleToRemove() and Duel.GetFlagEffect(tp, id) == 0
	end
	Duel.SetOperationInfo(0, CATEGORY_REMOVE, bc, 1, 0, 0)
	Duel.RegisterFlagEffect(tp, id, RESET_PHASE + PHASE_END, 0, 1)
end
function s.rmop(e, tp, eg, ep, ev, re, r, rp)
	local bc = e:GetLabelObject()
	if bc:IsRelateToBattle() and bc:IsControler(1 - tp) and Duel.Remove(bc, 0, REASON_EFFECT + REASON_TEMPORARY) ~= 0 then
		bc:SetTurnCounter(0)
		local e1 = Effect.CreateEffect(e:GetHandler())
		e1:SetDescription(aux.Stringid(id, 0))
		e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
		e1:SetCode(EVENT_PHASE + PHASE_END)
		e1:SetReset(RESET_PHASE + PHASE_END + RESET_OPPO_TURN, 2)
		e1:SetLabelObject(bc)
		e1:SetCountLimit(1)
		e1:SetCondition(s.retcon)
		e1:SetOperation(s.retop)
		Duel.RegisterEffect(e1, tp)
	end
end
function s.retcon(e, tp, eg, ep, ev, re, r, rp)
	return Duel.GetTurnPlayer() ~= tp
end
function s.retop(e, tp, eg, ep, ev, re, r, rp)
	local tc = e:GetLabelObject()
	local ct = tc:GetTurnCounter()
	ct = ct + 1
	tc:SetTurnCounter(ct)
	if ct == 2 then
		Duel.ReturnToField(tc)
	end
end
function s.thfilter(c)
	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL) and c:IsAbleToHand()
end
function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
end
function s.thop(e, tp, eg, ep, ev, re, r, rp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
	if g:GetCount() > 0 then
		Duel.SendtoHand(g, nil, REASON_EFFECT)
		Duel.ConfirmCards(1 - tp, g)
	end
end

