name: Metalfoes
card_text: 1 "Metalfoes" monster + 1 Pendulum Monster
  You can target 2 "Metalfoes" cards in your GY and 1 card on the field; shuffle the targets from your GY into the Deck, and if you do, return the target on the field to the hand. You can only use this effect of "Metalfoes Mithrilium" once per turn. If this card is sent from the field to the GY: You can Special Summon 1 "Metalfoes" Pendulum Monster from your GY or face-up Extra Deck.
external_ids: local SETCARD_ID_0 = 0xe1 --Metalfoes
  
lua: local SETCARD_ID_0 = 0xe1 --Metalfoes
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--fusion material
  	c:EnableReviveLimit()
  	aux.AddFusionProcFun2(c, aux.FilterBoolFunction(Card.IsFusionSetCard, SETCARD_ID_0), aux.FilterBoolFunction(Card.IsFusionType, TYPE_PENDULUM), true)
  	--return
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_TOHAND)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.rettg)
  	e2:SetOperation(s.retop)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.retfilter1(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToDeck()
  end
  function s.retfilter2(c)
  	return c:IsAbleToHand()
  end
  function s.rettg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.retfilter1, tp, LOCATION_GRAVE, 0, 2, nil) and Duel.IsExistingTarget(s.retfilter2, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g1 = Duel.SelectTarget(tp, s.retfilter1, tp, LOCATION_GRAVE, 0, 2, 2, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g2 = Duel.SelectTarget(tp, s.retfilter2, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g1, g1:GetCount(), 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g2, 1, 0, 0)
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	local g1 = g:Filter(Card.IsLocation, nil, LOCATION_GRAVE)
  	if Duel.SendtoDeck(g1, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 then
  		local og = Duel.GetOperatedGroup()
  		if og:IsExists(Card.IsLocation, 1, nil, LOCATION_DECK) then
  			Duel.ShuffleDeck(tp)
  		end
  		local g2 = g:Filter(Card.IsLocation, nil, LOCATION_ONFIELD)
  		Duel.SendtoHand(g2, nil, REASON_EFFECT)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_PENDULUM)
  		and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  		and (c:IsLocation(LOCATION_GRAVE) and Duel.GetMZoneCount(tp) > 0 or c:IsLocation(LOCATION_EXTRA) and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE + LOCATION_EXTRA)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Metalfoes
  1 "Metalfoes" monster + 1 Pendulum Monster
  You can target 2 "Metalfoes" cards in your GY and 1 card on the field; shuffle the targets from your GY into the Deck, and if you do, return the target on the field to the hand. You can only use this effect of "Metalfoes Mithrilium" once per turn. If this card is sent from the field to the GY: You can Special Summon 1 "Metalfoes" Pendulum Monster from your GY or face-up Extra Deck.
target_text: -- Metalfoes
  local SETCARD_ID_0 = 0xe1 --Metalfoes
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--fusion material
  	c:EnableReviveLimit()
  	aux.AddFusionProcFun2(c, aux.FilterBoolFunction(Card.IsFusionSetCard, SETCARD_ID_0), aux.FilterBoolFunction(Card.IsFusionType, TYPE_PENDULUM), true)
  	--return
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_TOHAND)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.rettg)
  	e2:SetOperation(s.retop)
  	c:RegisterEffect(e2)
  	--spsummon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCondition(s.spcon)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.retfilter1(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToDeck()
  end
  function s.retfilter2(c)
  	return c:IsAbleToHand()
  end
  function s.rettg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.retfilter1, tp, LOCATION_GRAVE, 0, 2, nil) and Duel.IsExistingTarget(s.retfilter2, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g1 = Duel.SelectTarget(tp, s.retfilter1, tp, LOCATION_GRAVE, 0, 2, 2, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local g2 = Duel.SelectTarget(tp, s.retfilter2, tp, LOCATION_ONFIELD, LOCATION_ONFIELD, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, g1, g1:GetCount(), 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g2, 1, 0, 0)
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS):Filter(Card.IsRelateToEffect, nil, e)
  	local g1 = g:Filter(Card.IsLocation, nil, LOCATION_GRAVE)
  	if Duel.SendtoDeck(g1, nil, SEQ_DECKSHUFFLE, REASON_EFFECT) ~= 0 then
  		local og = Duel.GetOperatedGroup()
  		if og:IsExists(Card.IsLocation, 1, nil, LOCATION_DECK) then
  			Duel.ShuffleDeck(tp)
  		end
  		local g2 = g:Filter(Card.IsLocation, nil, LOCATION_ONFIELD)
  		Duel.SendtoHand(g2, nil, REASON_EFFECT)
  	end
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0)
  		and c:IsType(TYPE_PENDULUM)
  		and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())
  		and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  		and (c:IsLocation(LOCATION_GRAVE) and Duel.GetMZoneCount(tp) > 0 or c:IsLocation(LOCATION_EXTRA) and Duel.GetLocationCountFromEx(tp, tp, nil, c) > 0)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_GRAVE + LOCATION_EXTRA)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_GRAVE + LOCATION_EXTRA, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  