name: Ghostrick
card_text: Target any number of face-down Defense Position monsters you control; change them to face-up Defense Position, count the number of "Ghostrick" monsters among them, and if you do, change up to that many face-up monsters your opponent controls to face-down Defense Position (min. 1).
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_POSITION)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and chkc:IsFacedown()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEDOWN)
  	local g = Duel.SelectTarget(tp, Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, 7, nil)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, g, g:GetCount(), 0, 0)
  end
  function s.filter(c, e)
  	return c:IsFacedown() and c:IsRelateToEffect(e)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(s.filter, nil, e)
  	Duel.ChangePosition(g, POS_FACEUP_DEFENSE)
  	local ct = g:FilterCount(Card.IsSetCard, nil, 0x8d)
  	if ct > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local sg = Duel.SelectMatchingCard(tp, Card.IsCanTurnSet, tp, 0, LOCATION_MZONE, 1, ct, nil)
  		if sg:GetCount() > 0 then
  			Duel.HintSelection(sg)
  			Duel.ChangePosition(sg, POS_FACEDOWN_DEFENSE)
  		end
  	end
  end
  
  
source_text: Ghostrick
  Target any number of face-down Defense Position monsters you control; change them to face-up Defense Position, count the number of "Ghostrick" monsters among them, and if you do, change up to that many face-up monsters your opponent controls to face-down Defense Position (min. 1).
target_text: -- Ghostrick
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_POSITION)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and chkc:IsFacedown()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEDOWN)
  	local g = Duel.SelectTarget(tp, Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, 7, nil)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, g, g:GetCount(), 0, 0)
  end
  function s.filter(c, e)
  	return c:IsFacedown() and c:IsRelateToEffect(e)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(s.filter, nil, e)
  	Duel.ChangePosition(g, POS_FACEUP_DEFENSE)
  	local ct = g:FilterCount(Card.IsSetCard, nil, 0x8d)
  	if ct > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local sg = Duel.SelectMatchingCard(tp, Card.IsCanTurnSet, tp, 0, LOCATION_MZONE, 1, ct, nil)
  		if sg:GetCount() > 0 then
  			Duel.HintSelection(sg)
  			Duel.ChangePosition(sg, POS_FACEDOWN_DEFENSE)
  		end
  	end
  end
  
  