name: Pendulum
card_text: If you have 2 cards in your Pendulum Zones: Apply the following effect, based on the difference in the Pendulum Scales.
  ● 0: Destroy 2 Spells/Traps on the field.
  ● 1, 2, or 3: Add 1 Level 2, 3, or 4 Pendulum Monster from your Deck to your hand.
  ● 4, 5, or 6: Add 1 Level 5, 6, or 7 Pendulum Monster from your Deck to your hand.
  ● 7+: Return up to 2 cards from your Pendulum Zone(s) to the hand, then you can Special Summon 1 Pendulum Monster from your hand.
  You can only activate 1 "Pendulum Scale" per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DESTROY + CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFieldGroupCount(tp, LOCATION_PZONE, 0) == 2
  end
  function s.desfilter(c)
  	return c:IsType(TYPE_SPELL + TYPE_TRAP)
  end
  function s.thfilter1(c)
  	return c:IsType(TYPE_PENDULUM) and c:IsLevelAbove(2) and c:IsLevelBelow(4) and c:IsAbleToHand()
  end
  function s.thfilter2(c)
  	return c:IsType(TYPE_PENDULUM) and c:IsLevelAbove(5) and c:IsLevelBelow(7) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tc1 = Duel.GetFieldCard(tp, LOCATION_PZONE, 0)
  	local tc2 = Duel.GetFieldCard(tp, LOCATION_PZONE, 1)
  	local scl1 = tc1:GetLeftScale()
  	local scl2 = tc2:GetRightScale()
  	local dif = math.abs(scl1 - scl2)
  	if chk == 0 then
  		return (dif == 0 and Duel.IsExistingMatchingCard(s.desfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 2, e:GetHandler()))
  			or (dif >= 1 and dif <= 3 and Duel.IsExistingMatchingCard(s.thfilter1, tp, LOCATION_DECK, 0, 1, nil))
  			or (dif >= 4 and dif <= 6 and Duel.IsExistingMatchingCard(s.thfilter2, tp, LOCATION_DECK, 0, 1, nil))
  			or (dif >= 7 and Duel.IsExistingMatchingCard(Card.IsAbleToHand, tp, LOCATION_PZONE, 0, 1, nil))
  	end
  	if dif == 0 then
  		local g = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 2, 0, 0)
  	end
  	if dif >= 1 and dif <= 6 then
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_PZONE, 0) < 2 then
  		return
  	end
  	local tc1 = Duel.GetFieldCard(tp, LOCATION_PZONE, 0)
  	local tc2 = Duel.GetFieldCard(tp, LOCATION_PZONE, 1)
  	local scl1 = tc1:GetLeftScale()
  	local scl2 = tc2:GetRightScale()
  	local dif = math.abs(scl1 - scl2)
  	local g1 = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  	local g2 = Duel.GetMatchingGroup(s.thfilter1, tp, LOCATION_DECK, 0, nil)
  	local g3 = Duel.GetMatchingGroup(s.thfilter2, tp, LOCATION_DECK, 0, nil)
  	local g4 = Duel.GetMatchingGroup(Card.IsAbleToHand, tp, LOCATION_PZONE, 0, nil)
  	local b1 = dif == 0 and g1:GetCount() >= 2
  	local b2 = dif >= 1 and dif <= 3 and g2:GetCount() >= 1
  	local b3 = dif >= 4 and dif <= 6 and g3:GetCount() >= 1
  	local b4 = dif >= 7 and g4:GetCount() >= 1
  	if b1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local sg = g1:Select(tp, 2, 2, e:GetHandler())
  		Duel.Destroy(sg, REASON_EFFECT)
  	end
  	if b2 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g2:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  	if b3 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g3:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  	if b4 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  		local sg = g4:Select(tp, 1, 2, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		local fg = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_HAND, 0, nil, e, tp)
  		if Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and fg:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local mg = fg:Select(tp, 1, 1, nil)
  			Duel.SpecialSummon(mg, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  function s.spfilter(c, e, tp)
  	return c:IsType(TYPE_PENDULUM) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  
  
source_text: Pendulum
  If you have 2 cards in your Pendulum Zones: Apply the following effect, based on the difference in the Pendulum Scales.
  ● 0: Destroy 2 Spells/Traps on the field.
  ● 1, 2, or 3: Add 1 Level 2, 3, or 4 Pendulum Monster from your Deck to your hand.
  ● 4, 5, or 6: Add 1 Level 5, 6, or 7 Pendulum Monster from your Deck to your hand.
  ● 7+: Return up to 2 cards from your Pendulum Zone(s) to the hand, then you can Special Summon 1 Pendulum Monster from your hand.
  You can only activate 1 "Pendulum Scale" per turn.
target_text: -- Pendulum
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DESTROY + CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFieldGroupCount(tp, LOCATION_PZONE, 0) == 2
  end
  function s.desfilter(c)
  	return c:IsType(TYPE_SPELL + TYPE_TRAP)
  end
  function s.thfilter1(c)
  	return c:IsType(TYPE_PENDULUM) and c:IsLevelAbove(2) and c:IsLevelBelow(4) and c:IsAbleToHand()
  end
  function s.thfilter2(c)
  	return c:IsType(TYPE_PENDULUM) and c:IsLevelAbove(5) and c:IsLevelBelow(7) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tc1 = Duel.GetFieldCard(tp, LOCATION_PZONE, 0)
  	local tc2 = Duel.GetFieldCard(tp, LOCATION_PZONE, 1)
  	local scl1 = tc1:GetLeftScale()
  	local scl2 = tc2:GetRightScale()
  	local dif = math.abs(scl1 - scl2)
  	if chk == 0 then
  		return (dif == 0 and Duel.IsExistingMatchingCard(s.desfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 2, e:GetHandler()))
  			or (dif >= 1 and dif <= 3 and Duel.IsExistingMatchingCard(s.thfilter1, tp, LOCATION_DECK, 0, 1, nil))
  			or (dif >= 4 and dif <= 6 and Duel.IsExistingMatchingCard(s.thfilter2, tp, LOCATION_DECK, 0, 1, nil))
  			or (dif >= 7 and Duel.IsExistingMatchingCard(Card.IsAbleToHand, tp, LOCATION_PZONE, 0, 1, nil))
  	end
  	if dif == 0 then
  		local g = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, g, 2, 0, 0)
  	end
  	if dif >= 1 and dif <= 6 then
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_PZONE, 0) < 2 then
  		return
  	end
  	local tc1 = Duel.GetFieldCard(tp, LOCATION_PZONE, 0)
  	local tc2 = Duel.GetFieldCard(tp, LOCATION_PZONE, 1)
  	local scl1 = tc1:GetLeftScale()
  	local scl2 = tc2:GetRightScale()
  	local dif = math.abs(scl1 - scl2)
  	local g1 = Duel.GetMatchingGroup(s.desfilter, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, e:GetHandler())
  	local g2 = Duel.GetMatchingGroup(s.thfilter1, tp, LOCATION_DECK, 0, nil)
  	local g3 = Duel.GetMatchingGroup(s.thfilter2, tp, LOCATION_DECK, 0, nil)
  	local g4 = Duel.GetMatchingGroup(Card.IsAbleToHand, tp, LOCATION_PZONE, 0, nil)
  	local b1 = dif == 0 and g1:GetCount() >= 2
  	local b2 = dif >= 1 and dif <= 3 and g2:GetCount() >= 1
  	local b3 = dif >= 4 and dif <= 6 and g3:GetCount() >= 1
  	local b4 = dif >= 7 and g4:GetCount() >= 1
  	if b1 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local sg = g1:Select(tp, 2, 2, e:GetHandler())
  		Duel.Destroy(sg, REASON_EFFECT)
  	end
  	if b2 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g2:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  	if b3 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g3:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  	if b4 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  		local sg = g4:Select(tp, 1, 2, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		local fg = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_HAND, 0, nil, e, tp)
  		if Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and fg:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 1)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  			local mg = fg:Select(tp, 1, 1, nil)
  			Duel.SpecialSummon(mg, 0, tp, tp, false, false, POS_FACEUP)
  		end
  	end
  end
  function s.spfilter(c, e, tp)
  	return c:IsType(TYPE_PENDULUM) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  
  