name: Melffy
card_text: If a face-up Beast monster you control was returned to the hand this turn, except "Melffy Pinny" (Quick Effect): You can Special Summon this card from your hand, then immediately after this effect resolves, Synchro Summon 1 Synchro Monster using only this card you control and "Melffy" monsters in your hand. During your End Phase, if this card is in your GY: You can target 1 Beast Xyz Monster you control; attach this card to it as material. You can only use each effect of "Melffy Pinny" once per turn.
external_ids: local SETCARD_ID_0 = 0x146 --Melffy
  
lua: local SETCARD_ID_0 = 0x146 --Melffy
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--synchro summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id)
  	e1:SetHintTiming(0, TIMING_CHAIN_END + TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCondition(s.syncon)
  	e1:SetTarget(s.syntg)
  	e1:SetOperation(s.synop)
  	c:RegisterEffect(e1)
  	--be material
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_PHASE + PHASE_END)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.xyzcon)
  	e2:SetTarget(s.xyztg)
  	e2:SetOperation(s.xyzop)
  	c:RegisterEffect(e2)
  	--
  	if not s.global_check then
  		s.global_check = true
  		local e0 = Effect.CreateEffect(c)
  		e0:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e0:SetCode(EVENT_TO_HAND)
  		e0:SetOperation(s.regop)
  		Duel.RegisterEffect(e0, 0)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE) and (c:GetPreviousRaceOnField() & RACE_BEAST) > 0 and c:IsPreviousPosition(POS_FACEUP) and not c:IsCode(id)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	for p = 0, 1 do
  		if eg:IsExists(s.cfilter, 1, nil, p) then
  			Duel.RegisterFlagEffect(p, id, RESET_PHASE + PHASE_END, 0, 1)
  		end
  	end
  end
  function s.syncon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) > 0
  end
  function s.syntg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.synhfilter(c, sc, tuner)
  	return c:IsCanBeSynchroMaterial(sc, tuner) and c:IsSetCard(SETCARD_ID_0)
  end
  function s.synfilter(c, mc, tp)
  	local mg = Duel.GetMatchingGroup(s.synhfilter, tp, LOCATION_HAND, 0, nil, c, mc)
  	mg:AddCard(mc)
  	return c:IsSynchroSummonable(mc, mg) and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.synop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) == 0 then
  		return
  	end
  	if Duel.IsExistingMatchingCard(s.synfilter, tp, LOCATION_EXTRA, 0, 1, nil, c, tp) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local fc = Duel.SelectMatchingCard(tp, s.synfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, c, tp):GetFirst()
  		local mg = Duel.GetMatchingGroup(s.synhfilter, tp, LOCATION_HAND, 0, nil, fc, c)
  		mg:AddCard(c)
  		Duel.SynchroSummon(tp, fc, c, mg)
  	end
  end
  function s.xyzcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.xyzfilter(c)
  	return c:IsType(TYPE_XYZ) and c:IsRace(RACE_BEAST) and c:IsFaceup()
  end
  function s.xyztg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.xyzfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.xyzfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.xyzfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, c, 1, 0, 0)
  end
  function s.xyzop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and c:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) then
  		Duel.Overlay(tc, Group.FromCards(c))
  	end
  end
  
  
source_text: Melffy
  If a face-up Beast monster you control was returned to the hand this turn, except "Melffy Pinny" (Quick Effect): You can Special Summon this card from your hand, then immediately after this effect resolves, Synchro Summon 1 Synchro Monster using only this card you control and "Melffy" monsters in your hand. During your End Phase, if this card is in your GY: You can target 1 Beast Xyz Monster you control; attach this card to it as material. You can only use each effect of "Melffy Pinny" once per turn.
target_text: -- Melffy
  local SETCARD_ID_0 = 0x146 --Melffy
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--synchro summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id)
  	e1:SetHintTiming(0, TIMING_CHAIN_END + TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCondition(s.syncon)
  	e1:SetTarget(s.syntg)
  	e1:SetOperation(s.synop)
  	c:RegisterEffect(e1)
  	--be material
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_PHASE + PHASE_END)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.xyzcon)
  	e2:SetTarget(s.xyztg)
  	e2:SetOperation(s.xyzop)
  	c:RegisterEffect(e2)
  	--
  	if not s.global_check then
  		s.global_check = true
  		local e0 = Effect.CreateEffect(c)
  		e0:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		e0:SetCode(EVENT_TO_HAND)
  		e0:SetOperation(s.regop)
  		Duel.RegisterEffect(e0, 0)
  	end
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_MZONE) and (c:GetPreviousRaceOnField() & RACE_BEAST) > 0 and c:IsPreviousPosition(POS_FACEUP) and not c:IsCode(id)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	for p = 0, 1 do
  		if eg:IsExists(s.cfilter, 1, nil, p) then
  			Duel.RegisterFlagEffect(p, id, RESET_PHASE + PHASE_END, 0, 1)
  		end
  	end
  end
  function s.syncon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) > 0
  end
  function s.syntg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, c, 1, 0, 0)
  end
  function s.synhfilter(c, sc, tuner)
  	return c:IsCanBeSynchroMaterial(sc, tuner) and c:IsSetCard(SETCARD_ID_0)
  end
  function s.synfilter(c, mc, tp)
  	local mg = Duel.GetMatchingGroup(s.synhfilter, tp, LOCATION_HAND, 0, nil, c, mc)
  	mg:AddCard(mc)
  	return c:IsSynchroSummonable(mc, mg) and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.synop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) == 0 then
  		return
  	end
  	if Duel.IsExistingMatchingCard(s.synfilter, tp, LOCATION_EXTRA, 0, 1, nil, c, tp) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  		local fc = Duel.SelectMatchingCard(tp, s.synfilter, tp, LOCATION_EXTRA, 0, 1, 1, nil, c, tp):GetFirst()
  		local mg = Duel.GetMatchingGroup(s.synhfilter, tp, LOCATION_HAND, 0, nil, fc, c)
  		mg:AddCard(c)
  		Duel.SynchroSummon(tp, fc, c, mg)
  	end
  end
  function s.xyzcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.xyzfilter(c)
  	return c:IsType(TYPE_XYZ) and c:IsRace(RACE_BEAST) and c:IsFaceup()
  end
  function s.xyztg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	local c = e:GetHandler()
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.xyzfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.xyzfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.xyzfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, c, 1, 0, 0)
  end
  function s.xyzop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and c:IsRelateToEffect(e) and not tc:IsImmuneToEffect(e) then
  		Duel.Overlay(tc, Group.FromCards(c))
  	end
  end
  
  