name: Clear
card_text: When your opponent activates a monster effect that would inflict damage (Quick Effect): You can discard this card; negate the activation. When an opponent's monster declares a direct attack: You can banish this card from your GY; draw 1 card, and if it is a monster, you can Special Summon it, then change the attack target to it. You can only use this effect of "Clear Kuriboh" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--negate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_NEGATE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e1:SetCondition(s.negcon)
  	e1:SetCost(s.negcost)
  	e1:SetTarget(s.negtg)
  	e1:SetOperation(s.negop)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DRAW + CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.drcon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsChainNegatable(ev) and (aux.damcon1(e, tp, eg, ep, ev, re, r, rp) or aux.damcon1(e, 1 - tp, eg, ep, ev, re, r, rp)) and re:IsActiveType(TYPE_MONSTER) and ep ~= tp
  end
  function s.negcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsDiscardable()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST + REASON_DISCARD)
  end
  function s.negtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.NegateActivation(ev)
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetAttacker():IsControler(1 - tp) and Duel.GetAttackTarget() == nil
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.Draw(tp, 1, REASON_EFFECT) == 0 then
  		return
  	end
  	local tc = Duel.GetOperatedGroup():GetFirst()
  	if tc:IsType(TYPE_MONSTER) then
  		if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  			return
  		end
  		if tc:IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.ConfirmCards(1 - tp, tc)
  			Duel.BreakEffect()
  			if Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) ~= 0 and not Duel.GetAttacker():IsImmuneToEffect(e) then
  				Duel.BreakEffect()
  				Duel.ChangeAttackTarget(tc)
  			end
  		end
  	end
  end
  
  
source_text: Clear
  When your opponent activates a monster effect that would inflict damage (Quick Effect): You can discard this card; negate the activation. When an opponent's monster declares a direct attack: You can banish this card from your GY; draw 1 card, and if it is a monster, you can Special Summon it, then change the attack target to it. You can only use this effect of "Clear Kuriboh" once per turn.
target_text: -- Clear
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--negate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_NEGATE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e1:SetCondition(s.negcon)
  	e1:SetCost(s.negcost)
  	e1:SetTarget(s.negtg)
  	e1:SetOperation(s.negop)
  	c:RegisterEffect(e1)
  	--draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DRAW + CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.drcon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.drtg)
  	e2:SetOperation(s.drop)
  	c:RegisterEffect(e2)
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsChainNegatable(ev) and (aux.damcon1(e, tp, eg, ep, ev, re, r, rp) or aux.damcon1(e, 1 - tp, eg, ep, ev, re, r, rp)) and re:IsActiveType(TYPE_MONSTER) and ep ~= tp
  end
  function s.negcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsDiscardable()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST + REASON_DISCARD)
  end
  function s.negtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.NegateActivation(ev)
  end
  function s.drcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetAttacker():IsControler(1 - tp) and Duel.GetAttackTarget() == nil
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, 1)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.Draw(tp, 1, REASON_EFFECT) == 0 then
  		return
  	end
  	local tc = Duel.GetOperatedGroup():GetFirst()
  	if tc:IsType(TYPE_MONSTER) then
  		if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  			return
  		end
  		if tc:IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.ConfirmCards(1 - tp, tc)
  			Duel.BreakEffect()
  			if Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP) ~= 0 and not Duel.GetAttacker():IsImmuneToEffect(e) then
  				Duel.BreakEffect()
  				Duel.ChangeAttackTarget(tc)
  			end
  		end
  	end
  end
  
  