name: Dark
card_text: When your opponent activates a monster effect, if you have 3 or more cards in your hand (except during the Damage Step): You can target 1 "Dark World" monster on the field; return it to the hand, and if you do, the activated effect becomes "Your opponent randomly discards 1 card".
external_ids: local SETCARD_ID_0 = 0x6 --Dark
  
lua: local SETCARD_ID_0 = 0x6 --Dark
  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)
  	c:RegisterEffect(e1)
  	--replace effect
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, EFFECT_COUNT_CODE_CHAIN)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetFieldGroup(tp, 0, LOCATION_HAND)
  	if g:GetCount() > 0 then
  		local sg = g:RandomSelect(1 - tp, 1)
  		Duel.SendtoGrave(sg, REASON_EFFECT + REASON_DISCARD)
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return ep ~= tp and re:IsActiveType(TYPE_MONSTER) and Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0) >= 3
  end
  function s.thfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.SendtoHand(tc, nil, REASON_EFFECT) > 0 then
  		local g = Group.CreateGroup()
  		Duel.ChangeTargetCard(ev, g)
  		Duel.ChangeChainOperation(ev, s.repop)
  	end
  end
  
  
source_text: Dark
  When your opponent activates a monster effect, if you have 3 or more cards in your hand (except during the Damage Step): You can target 1 "Dark World" monster on the field; return it to the hand, and if you do, the activated effect becomes "Your opponent randomly discards 1 card".
target_text: -- Dark
  local SETCARD_ID_0 = 0x6 --Dark
  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)
  	c:RegisterEffect(e1)
  	--replace effect
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCode(EVENT_CHAINING)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, EFFECT_COUNT_CODE_CHAIN)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.target)
  	e2:SetOperation(s.operation)
  	c:RegisterEffect(e2)
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetFieldGroup(tp, 0, LOCATION_HAND)
  	if g:GetCount() > 0 then
  		local sg = g:RandomSelect(1 - tp, 1)
  		Duel.SendtoGrave(sg, REASON_EFFECT + REASON_DISCARD)
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return ep ~= tp and re:IsActiveType(TYPE_MONSTER) and Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0) >= 3
  end
  function s.thfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.SendtoHand(tc, nil, REASON_EFFECT) > 0 then
  		local g = Group.CreateGroup()
  		Duel.ChangeTargetCard(ev, g)
  		Duel.ChangeChainOperation(ev, s.repop)
  	end
  end
  
  