name: Solfachord
card_text: While you have a "Solfachord" card(s) in your Pendulum Zone, when your opponent activates a Spell/Trap Card, or monster effect: Shuffle 1 face-up Pendulum Monster from your Extra Deck into the Deck, then apply these effects.
  ● Pendulum Monsters you control are unaffected by that effect.
  ● Cards in your Pendulum Zone cannot be destroyed by that effect.
  ● Cards in your Pendulum Zone cannot be banished by that effect.
  You can only activate 1 "Solfachord Formal" per turn.
external_ids: local SETCARD_ID_0 = 0x162 --Solfachord
  
lua: local SETCARD_ID_0 = 0x162 --Solfachord
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.confilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.confilter, tp, LOCATION_PZONE, 0, 1, nil) and (re:IsActiveType(TYPE_MONSTER) or re:IsHasType(EFFECT_TYPE_ACTIVATE)) and rp == 1 - tp
  end
  function s.tdfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_PENDULUM) and c:IsAbleToDeck()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.tdfilter, tp, LOCATION_EXTRA, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_PENDULUM)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(tp, s.tdfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil)
  	local tc = g:GetFirst()
  	if tc and Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 and tc:IsLocation(LOCATION_DECK) and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_ONFIELD, 0, 1, nil) then
  		Duel.BreakEffect()
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetCode(EFFECT_IMMUNE_EFFECT)
  		e1:SetTargetRange(LOCATION_MZONE, 0)
  		e1:SetTarget(aux.TargetBoolFunction(Card.IsType, TYPE_PENDULUM))
  		e1:SetValue(s.efilter)
  		e1:SetLabelObject(re)
  		e1:SetReset(RESET_EVENT + RESET_CHAIN)
  		Duel.RegisterEffect(e1, tp)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  		e2:SetTargetRange(LOCATION_PZONE, 0)
  		Duel.RegisterEffect(e2, tp)
  		local e3 = e2:Clone()
  		e3:SetCode(EFFECT_CANNOT_REMOVE)
  		e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e3:SetTargetRange(1, 1)
  		e3:SetTarget(s.rmlimit)
  		Duel.RegisterEffect(e3, tp)
  	end
  end
  function s.efilter(e, re)
  	return re == e:GetLabelObject()
  end
  function s.rmlimit(e, c, tp, r, re)
  	return c:IsControler(e:GetHandlerPlayer()) and c:IsLocation(LOCATION_PZONE) and r & REASON_EFFECT ~= 0 and re and re == e:GetLabelObject()
  end
  
  
source_text: Solfachord
  While you have a "Solfachord" card(s) in your Pendulum Zone, when your opponent activates a Spell/Trap Card, or monster effect: Shuffle 1 face-up Pendulum Monster from your Extra Deck into the Deck, then apply these effects.
  ● Pendulum Monsters you control are unaffected by that effect.
  ● Cards in your Pendulum Zone cannot be destroyed by that effect.
  ● Cards in your Pendulum Zone cannot be banished by that effect.
  You can only activate 1 "Solfachord Formal" per turn.
target_text: -- Solfachord
  local SETCARD_ID_0 = 0x162 --Solfachord
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.confilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.confilter, tp, LOCATION_PZONE, 0, 1, nil) and (re:IsActiveType(TYPE_MONSTER) or re:IsHasType(EFFECT_TYPE_ACTIVATE)) and rp == 1 - tp
  end
  function s.tdfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_PENDULUM) and c:IsAbleToDeck()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.tdfilter, tp, LOCATION_EXTRA, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsType(TYPE_PENDULUM)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(tp, s.tdfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil)
  	local tc = g:GetFirst()
  	if tc and Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 and tc:IsLocation(LOCATION_DECK) and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_ONFIELD, 0, 1, nil) then
  		Duel.BreakEffect()
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetCode(EFFECT_IMMUNE_EFFECT)
  		e1:SetTargetRange(LOCATION_MZONE, 0)
  		e1:SetTarget(aux.TargetBoolFunction(Card.IsType, TYPE_PENDULUM))
  		e1:SetValue(s.efilter)
  		e1:SetLabelObject(re)
  		e1:SetReset(RESET_EVENT + RESET_CHAIN)
  		Duel.RegisterEffect(e1, tp)
  		local e2 = e1:Clone()
  		e2:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  		e2:SetTargetRange(LOCATION_PZONE, 0)
  		Duel.RegisterEffect(e2, tp)
  		local e3 = e2:Clone()
  		e3:SetCode(EFFECT_CANNOT_REMOVE)
  		e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e3:SetTargetRange(1, 1)
  		e3:SetTarget(s.rmlimit)
  		Duel.RegisterEffect(e3, tp)
  	end
  end
  function s.efilter(e, re)
  	return re == e:GetLabelObject()
  end
  function s.rmlimit(e, c, tp, r, re)
  	return c:IsControler(e:GetHandlerPlayer()) and c:IsLocation(LOCATION_PZONE) and r & REASON_EFFECT ~= 0 and re and re == e:GetLabelObject()
  end
  
  