name: Rainbow
card_text: During your Main Phase, you can Normal Summon 1 "Crystal Beast" monster, in addition to your Normal Summon/Set. (You can only gain this effect once per turn.) You can only use each of the following effects of "Rainbow Bridge of the Heart" once per turn. During your Main Phase: You can destroy 1 "Crystal Beast" card you control or in your hand, and if you do, add 1 "Crystal" Spell/Trap from your Deck to your hand. If a "Crystal Beast" card(s) is placed in your Spell & Trap Zone, even during the Damage Step: You can target 1 card your opponent controls; return both that card and this card to the hand.
external_ids: local SETCARD_ID_0 = 0x1034 --Crystal
  local SETCARD_ID_1 = 0x34 --Crystal
  
lua: local SETCARD_ID_0 = 0x1034 --Crystal
  local SETCARD_ID_1 = 0x34 --Crystal
  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)
  	--extra summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetTargetRange(LOCATION_HAND + LOCATION_MZONE, 0)
  	e2:SetCode(EFFECT_EXTRA_SUMMON_COUNT)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_1))
  	c:RegisterEffect(e2)
  	--destroy search
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DESTROY + CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  	--return to hand
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 2))
  	e4:SetCategory(CATEGORY_TOHAND)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_MOVE)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetCountLimit(1, id + o)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	c:RegisterEffect(e4)
  end
  function s.desfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and (c:IsFaceup() or c:IsLocation(LOCATION_HAND))
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.desfilter, tp, LOCATION_HAND + LOCATION_ONFIELD, 0, 1, nil) and Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	local g = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_HAND + LOCATION_ONFIELD, 0, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectMatchingCard(tp, s.desfilter, tp, LOCATION_HAND + LOCATION_ONFIELD, 0, 1, 1, nil)
  	if g:GetCount() > 0 and Duel.Destroy(g, REASON_EFFECT) ~= 0 then
  		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
  end
  function s.cfilter(c, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_1) and c:IsControler(tp) and c:IsLocation(LOCATION_SZONE) and c:GetSequence() < 5
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsControler(1 - tp) and chkc:IsAbleToHand()
  	end
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsAbleToHand() and Duel.IsExistingTarget(Card.IsAbleToHand, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToHand, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	g:AddCard(c)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 2, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if c:IsRelateToEffect(e) and tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(Group.FromCards(c, tc), nil, REASON_EFFECT)
  	end
  end
  
  
source_text: Rainbow
  During your Main Phase, you can Normal Summon 1 "Crystal Beast" monster, in addition to your Normal Summon/Set. (You can only gain this effect once per turn.) You can only use each of the following effects of "Rainbow Bridge of the Heart" once per turn. During your Main Phase: You can destroy 1 "Crystal Beast" card you control or in your hand, and if you do, add 1 "Crystal" Spell/Trap from your Deck to your hand. If a "Crystal Beast" card(s) is placed in your Spell & Trap Zone, even during the Damage Step: You can target 1 card your opponent controls; return both that card and this card to the hand.
target_text: -- Rainbow
  local SETCARD_ID_0 = 0x1034 --Crystal
  local SETCARD_ID_1 = 0x34 --Crystal
  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)
  	--extra summon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetTargetRange(LOCATION_HAND + LOCATION_MZONE, 0)
  	e2:SetCode(EFFECT_EXTRA_SUMMON_COUNT)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_1))
  	c:RegisterEffect(e2)
  	--destroy search
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_DESTROY + CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1, id)
  	e3:SetTarget(s.destg)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  	--return to hand
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 2))
  	e4:SetCategory(CATEGORY_TOHAND)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_MOVE)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY + EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetCountLimit(1, id + o)
  	e4:SetCondition(s.thcon)
  	e4:SetTarget(s.thtg)
  	e4:SetOperation(s.thop)
  	c:RegisterEffect(e4)
  end
  function s.desfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and (c:IsFaceup() or c:IsLocation(LOCATION_HAND))
  end
  function s.thfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.desfilter, tp, LOCATION_HAND + LOCATION_ONFIELD, 0, 1, nil) and Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	local g = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_HAND + LOCATION_ONFIELD, 0, nil)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  	local g = Duel.SelectMatchingCard(tp, s.desfilter, tp, LOCATION_HAND + LOCATION_ONFIELD, 0, 1, 1, nil)
  	if g:GetCount() > 0 and Duel.Destroy(g, REASON_EFFECT) ~= 0 then
  		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
  end
  function s.cfilter(c, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_1) and c:IsControler(tp) and c:IsLocation(LOCATION_SZONE) and c:GetSequence() < 5
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsOnField() and chkc:IsControler(1 - tp) and chkc:IsAbleToHand()
  	end
  	local c = e:GetHandler()
  	if chk == 0 then
  		return c:IsAbleToHand() and Duel.IsExistingTarget(Card.IsAbleToHand, tp, 0, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g = Duel.SelectTarget(tp, Card.IsAbleToHand, tp, 0, LOCATION_ONFIELD, 1, 1, nil)
  	g:AddCard(c)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 2, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if c:IsRelateToEffect(e) and tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(Group.FromCards(c, tc), nil, REASON_EFFECT)
  	end
  end
  
  