name: Background
card_text: If this card is in the GY and you control no cards: You can Special Summon both this card, and 1 Level 4 or lower Dragon monster from your hand, in Defense Position, but banish this card when it leaves the field. You can only use this effect of "Background Dragon" once per turn.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_GRAVE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFieldGroupCount(tp, LOCATION_ONFIELD, 0) == 0
  end
  function s.filter(c, e, tp)
  	return c:IsLevelBelow(4) and c:IsRace(RACE_DRAGON) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 1
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  			and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_HAND, 0, 1, e:GetHandler(), e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 2, tp, LOCATION_HAND)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or not c:IsCanBeSpecialSummoned(e, 0, tp, false, false) then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		return
  	end
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) < 2 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_HAND, 0, 1, 1, c, e, tp)
  	if g:GetCount() > 0 then
  		g:AddCard(c)
  		if Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE) > 0 then
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_SINGLE)
  			e2:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  			e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e2:SetReset(RESET_EVENT + RESETS_REDIRECT)
  			e2:SetValue(LOCATION_REMOVED)
  			c:RegisterEffect(e2, true)
  		end
  	end
  end
  
  
source_text: Background
  If this card is in the GY and you control no cards: You can Special Summon both this card, and 1 Level 4 or lower Dragon monster from your hand, in Defense Position, but banish this card when it leaves the field. You can only use this effect of "Background Dragon" once per turn.
target_text: -- Background
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetRange(LOCATION_GRAVE)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFieldGroupCount(tp, LOCATION_ONFIELD, 0) == 0
  end
  function s.filter(c, e, tp)
  	return c:IsLevelBelow(4) and c:IsRace(RACE_DRAGON) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0)
  			and Duel.GetLocationCount(tp, LOCATION_MZONE) > 1
  			and e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false)
  			and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_HAND, 0, 1, e:GetHandler(), e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 2, tp, LOCATION_HAND)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or not c:IsCanBeSpecialSummoned(e, 0, tp, false, false) then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		return
  	end
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) < 2 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_HAND, 0, 1, 1, c, e, tp)
  	if g:GetCount() > 0 then
  		g:AddCard(c)
  		if Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE) > 0 then
  			local e2 = Effect.CreateEffect(c)
  			e2:SetType(EFFECT_TYPE_SINGLE)
  			e2:SetCode(EFFECT_LEAVE_FIELD_REDIRECT)
  			e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  			e2:SetReset(RESET_EVENT + RESETS_REDIRECT)
  			e2:SetValue(LOCATION_REMOVED)
  			c:RegisterEffect(e2, true)
  		end
  	end
  end
  
  