name: Doodlebook
card_text: When your Dinosaur monster is targeted for an attack: Negate that attack, then you can Special Summon 1 "Doodle Beast" monster from your GY. It cannot be destroyed by battle, also destroy it during the End Phase. If this card is in your GY, except the turn it was sent there: You can banish this card from your GY; add 1 Level 5 or higher Dinosaur monster from your Deck to your hand. You can only use each effect of "Doodlebook - Uh uh uh!" once per turn.
external_ids: local SETCARD_ID_0 = 0x1185 --Doodle
  
lua: local SETCARD_ID_0 = 0x1185 --Doodle
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--negate attack
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_BE_BATTLE_TARGET)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.negcon)
  	e1:SetOperation(s.negop)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e2:SetCondition(aux.exccon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	return tc:IsControler(tp) and tc:IsFaceup() and tc:IsRace(RACE_DINOSAUR)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateAttack() then
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.spfilter), tp, LOCATION_GRAVE, 0, nil, e, tp)
  		local ct = Duel.GetLocationCount(tp, LOCATION_MZONE)
  		if ct > 0 and g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local tc = g:Select(tp, 1, 1, nil):GetFirst()
  			if tc then
  				Duel.BreakEffect()
  				Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  				local fid = e:GetHandler():GetFieldID()
  				tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 1, fid)
  				local e1 = Effect.CreateEffect(e:GetHandler())
  				e1:SetDescription(aux.Stringid(id, 2))
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  				e1:SetProperty(EFFECT_FLAG_CLIENT_HINT)
  				e1:SetValue(1)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  				tc:RegisterEffect(e1)
  				local e2 = Effect.CreateEffect(e:GetHandler())
  				e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  				e2:SetCode(EVENT_PHASE + PHASE_END)
  				e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  				e2:SetCountLimit(1)
  				e2:SetLabel(fid)
  				e2:SetLabelObject(tc)
  				e2:SetCondition(s.descon)
  				e2:SetOperation(s.desop)
  				Duel.RegisterEffect(e2, tp)
  			end
  		end
  	end
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:GetFlagEffectLabel(id) ~= e:GetLabel() then
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetLabelObject(), REASON_EFFECT)
  end
  function s.thfilter(c)
  	return c:IsRace(RACE_DINOSAUR) and c:IsLevelAbove(5) 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: Doodlebook
  When your Dinosaur monster is targeted for an attack: Negate that attack, then you can Special Summon 1 "Doodle Beast" monster from your GY. It cannot be destroyed by battle, also destroy it during the End Phase. If this card is in your GY, except the turn it was sent there: You can banish this card from your GY; add 1 Level 5 or higher Dinosaur monster from your Deck to your hand. You can only use each effect of "Doodlebook - Uh uh uh!" once per turn.
target_text: -- Doodlebook
  local SETCARD_ID_0 = 0x1185 --Doodle
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--negate attack
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_BE_BATTLE_TARGET)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.negcon)
  	e1:SetOperation(s.negop)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e2:SetCondition(aux.exccon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	return tc:IsControler(tp) and tc:IsFaceup() and tc:IsRace(RACE_DINOSAUR)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateAttack() then
  		local g = Duel.GetMatchingGroup(aux.NecroValleyFilter(s.spfilter), tp, LOCATION_GRAVE, 0, nil, e, tp)
  		local ct = Duel.GetLocationCount(tp, LOCATION_MZONE)
  		if ct > 0 and g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local tc = g:Select(tp, 1, 1, nil):GetFirst()
  			if tc then
  				Duel.BreakEffect()
  				Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  				local fid = e:GetHandler():GetFieldID()
  				tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD, 0, 1, fid)
  				local e1 = Effect.CreateEffect(e:GetHandler())
  				e1:SetDescription(aux.Stringid(id, 2))
  				e1:SetType(EFFECT_TYPE_SINGLE)
  				e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  				e1:SetProperty(EFFECT_FLAG_CLIENT_HINT)
  				e1:SetValue(1)
  				e1:SetReset(RESET_EVENT + RESETS_STANDARD)
  				tc:RegisterEffect(e1)
  				local e2 = Effect.CreateEffect(e:GetHandler())
  				e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  				e2:SetCode(EVENT_PHASE + PHASE_END)
  				e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  				e2:SetCountLimit(1)
  				e2:SetLabel(fid)
  				e2:SetLabelObject(tc)
  				e2:SetCondition(s.descon)
  				e2:SetOperation(s.desop)
  				Duel.RegisterEffect(e2, tp)
  			end
  		end
  	end
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetLabelObject()
  	if tc:GetFlagEffectLabel(id) ~= e:GetLabel() then
  		e:Reset()
  		return false
  	else
  		return true
  	end
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetLabelObject(), REASON_EFFECT)
  end
  function s.thfilter(c)
  	return c:IsRace(RACE_DINOSAUR) and c:IsLevelAbove(5) 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
  
  