name: PSY-Frame
card_text: Once per turn: You can banish 1 "PSY-Frame" monster from your hand or face-up from your side of the field, then target 1 card on the field; banish it face-down. During either player's turn, except the turn this card was sent to the Graveyard: You can banish this card from your Graveyard; add 1 "PSY-Frame" card from your Deck to your hand, except "PSY-Frame Overload".
external_ids: local SETCARD_ID_0 = 0xc1 --PSY-Frame
  
lua: local SETCARD_ID_0 = 0xc1 --PSY-Frame
  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)
  	--remove
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCountLimit(1)
  	e2:SetCost(s.cost)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  	--to hand
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetRange(LOCATION_GRAVE)
  	e3:SetCode(EVENT_FREE_CHAIN)
  	e3:SetCondition(aux.exccon)
  	e3:SetCost(aux.bfgcost)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.tgfilter(c, tp, xc)
  	return c:IsAbleToRemove(tp, POS_FACEDOWN) and c ~= xc
  end
  function s.cfilter(c, tp, xc)
  	return c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_MONSTER)
  		and (c:IsLocation(LOCATION_HAND) or c:IsFaceup())
  		and c:IsAbleToRemoveAsCost()
  		and Duel.IsExistingTarget(s.tgfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, c, tp, xc)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local xc = nil
  	if not e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED) then
  		xc = e:GetHandler()
  	end
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_HAND + LOCATION_MZONE, 0, 1, nil, tp, xc)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local cg = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_HAND + LOCATION_MZONE, 0, 1, 1, nil, tp, xc)
  	Duel.Remove(cg, POS_FACEUP, REASON_COST)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsAbleToRemove(tp, POS_FACEDOWN)
  	end
  	if chk == 0 then
  		return true
  	end
  	local xg = nil
  	if not e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED) then
  		xg = e:GetHandler()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToRemove, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, xg, tp, POS_FACEDOWN)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.Remove(tc, POS_FACEDOWN, REASON_EFFECT)
  	end
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) 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
  
  
source_text: PSY-Frame
  Once per turn: You can banish 1 "PSY-Frame" monster from your hand or face-up from your side of the field, then target 1 card on the field; banish it face-down. During either player's turn, except the turn this card was sent to the Graveyard: You can banish this card from your Graveyard; add 1 "PSY-Frame" card from your Deck to your hand, except "PSY-Frame Overload".
target_text: -- PSY-Frame
  local SETCARD_ID_0 = 0xc1 --PSY-Frame
  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)
  	--remove
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetHintTiming(0, TIMING_END_PHASE)
  	e2:SetCountLimit(1)
  	e2:SetCost(s.cost)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  	--to hand
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetRange(LOCATION_GRAVE)
  	e3:SetCode(EVENT_FREE_CHAIN)
  	e3:SetCondition(aux.exccon)
  	e3:SetCost(aux.bfgcost)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.tgfilter(c, tp, xc)
  	return c:IsAbleToRemove(tp, POS_FACEDOWN) and c ~= xc
  end
  function s.cfilter(c, tp, xc)
  	return c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_MONSTER)
  		and (c:IsLocation(LOCATION_HAND) or c:IsFaceup())
  		and c:IsAbleToRemoveAsCost()
  		and Duel.IsExistingTarget(s.tgfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, c, tp, xc)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local xc = nil
  	if not e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED) then
  		xc = e:GetHandler()
  	end
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_HAND + LOCATION_MZONE, 0, 1, nil, tp, xc)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local cg = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_HAND + LOCATION_MZONE, 0, 1, 1, nil, tp, xc)
  	Duel.Remove(cg, POS_FACEUP, REASON_COST)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsAbleToRemove(tp, POS_FACEDOWN)
  	end
  	if chk == 0 then
  		return true
  	end
  	local xg = nil
  	if not e:GetHandler():IsStatus(STATUS_EFFECT_ENABLED) then
  		xg = e:GetHandler()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToRemove, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, xg, tp, POS_FACEDOWN)
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, g, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.Remove(tc, POS_FACEDOWN, REASON_EFFECT)
  	end
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) 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
  
  