name: Ghostrick
card_text: Cannot be Normal Summoned, unless you control a "Ghostrick" monster. Once per turn: You can change this card to face-down Defense Position. When an opponent's monster declares a direct attack, or when a "Ghostrick" monster you control is targeted for an attack: You can negate the attack, and if you do, Special Summon this card from your hand in face-down Defense Position.
external_ids: local SETCARD_ID_0 = 0x8d --Ghostrick
  
lua: local SETCARD_ID_0 = 0x8d --Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--summon limit
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_CANNOT_SUMMON)
  	e1:SetCondition(s.sumcon)
  	c:RegisterEffect(e1)
  	--turn set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.postg)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  	--negate attack
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetRange(LOCATION_HAND)
  	e3:SetCondition(s.spcon1)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_BE_BATTLE_TARGET)
  	e4:SetRange(LOCATION_HAND)
  	e4:SetCondition(s.spcon2)
  	e4:SetTarget(s.sptg)
  	e4:SetOperation(s.spop)
  	c:RegisterEffect(e4)
  end
  function s.sfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.sumcon(e)
  	return not Duel.IsExistingMatchingCard(s.sfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsCanTurnSet() and c:GetFlagEffect(id) == 0
  	end
  	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_PHASE + PHASE_END, 0, 1)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, c, 1, 0, 0)
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() then
  		Duel.ChangePosition(c, POS_FACEDOWN_DEFENSE)
  	end
  end
  function s.spcon1(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttacker()
  	return at:IsControler(1 - tp) and Duel.GetAttackTarget() == nil
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttackTarget()
  	return at:IsControler(tp) and at:IsFaceup() and at:IsSetCard(SETCARD_ID_0)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEDOWN_DEFENSE)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if Duel.NegateAttack() then
  		if not c:IsRelateToEffect(e) then
  			return
  		end
  		if Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEDOWN_DEFENSE) ~= 0 then
  			Duel.ConfirmCards(1 - tp, c)
  		end
  	end
  end
  
  
source_text: Ghostrick
  Cannot be Normal Summoned, unless you control a "Ghostrick" monster. Once per turn: You can change this card to face-down Defense Position. When an opponent's monster declares a direct attack, or when a "Ghostrick" monster you control is targeted for an attack: You can negate the attack, and if you do, Special Summon this card from your hand in face-down Defense Position.
target_text: -- Ghostrick
  local SETCARD_ID_0 = 0x8d --Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--summon limit
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_CANNOT_SUMMON)
  	e1:SetCondition(s.sumcon)
  	c:RegisterEffect(e1)
  	--turn set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetTarget(s.postg)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  	--negate attack
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetRange(LOCATION_HAND)
  	e3:SetCondition(s.spcon1)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_BE_BATTLE_TARGET)
  	e4:SetRange(LOCATION_HAND)
  	e4:SetCondition(s.spcon2)
  	e4:SetTarget(s.sptg)
  	e4:SetOperation(s.spop)
  	c:RegisterEffect(e4)
  end
  function s.sfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.sumcon(e)
  	return not Duel.IsExistingMatchingCard(s.sfilter, e:GetHandlerPlayer(), LOCATION_MZONE, 0, 1, nil)
  end
  function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsCanTurnSet() and c:GetFlagEffect(id) == 0
  	end
  	c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD - RESET_TURN_SET + RESET_PHASE + PHASE_END, 0, 1)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, c, 1, 0, 0)
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and c:IsFaceup() then
  		Duel.ChangePosition(c, POS_FACEDOWN_DEFENSE)
  	end
  end
  function s.spcon1(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttacker()
  	return at:IsControler(1 - tp) and Duel.GetAttackTarget() == nil
  end
  function s.spcon2(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttackTarget()
  	return at:IsControler(tp) and at:IsFaceup() and at:IsSetCard(SETCARD_ID_0)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEDOWN_DEFENSE)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if Duel.NegateAttack() then
  		if not c:IsRelateToEffect(e) then
  			return
  		end
  		if Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEDOWN_DEFENSE) ~= 0 then
  			Duel.ConfirmCards(1 - tp, c)
  		end
  	end
  end
  
  