name: K9-LC
card_text: Target 1 "K9" Xyz Monster or 1 Rank 5 Xyz Monster you control; Special Summon from your Extra Deck, 1 "K9" Xyz Monster with a different name, by using that monster you control (this is treated as an Xyz Summon, transfer its materials to the Summoned monster). At the end of the Battle Phase, if your "K9" monster battled this turn: You can Set this card from your GY. You can only use each effect of "K9-LC Release Restraint" once per turn.
external_ids: local SETCARD_ID_0 = 0x1cb --K9
  
lua: local SETCARD_ID_0 = 0x1cb --K9
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_PHASE + PHASE_BATTLE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetCondition(s.setcon)
  	e2:SetTarget(s.settg)
  	e2:SetOperation(s.setop)
  	c:RegisterEffect(e2)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.GlobalEffect()
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_BATTLED)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttackTarget()
  	local ar = Duel.GetAttacker()
  	if at and at:IsSetCard(SETCARD_ID_0) then
  		Duel.RegisterFlagEffect(at:GetControler(), id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  	if ar and ar:IsSetCard(SETCARD_ID_0) then
  		Duel.RegisterFlagEffect(ar:GetControler(), id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  end
  function s.filter1(c, e, tp)
  	return c:IsFaceup()
  		and c:IsType(TYPE_XYZ)
  		and (c:IsSetCard(SETCARD_ID_0) or c:IsRank(5))
  		and aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL)
  		and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c, c:GetCode())
  end
  function s.filter2(c, e, tp, mc, code)
  	return c:IsSetCard(SETCARD_ID_0)
  		and not c:IsCode(code)
  		and mc:IsCanBeXyzMaterial(c)
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
  		and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter1(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter1, tp, LOCATION_MZONE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter1, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if not aux.MustMaterialCheck(tc, tp, EFFECT_MUST_BE_XMATERIAL) or tc:IsFacedown() or not tc:IsRelateToChain() or tc:IsControler(1 - tp) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc, tc:GetCode())
  	local sc = g:GetFirst()
  	if sc then
  		local mg = tc:GetOverlayGroup()
  		if mg:GetCount() ~= 0 then
  			Duel.Overlay(sc, mg)
  		end
  		sc:SetMaterial(Group.FromCards(tc))
  		Duel.Overlay(sc, Group.FromCards(tc))
  		Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP)
  		sc:CompleteProcedure()
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) > 0
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsSSetable()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, e:GetHandler(), 1, 0, 0)
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToChain() and aux.NecroValleyFilter()(c) then
  		Duel.SSet(tp, c)
  	end
  end
  
  
source_text: K9-LC
  Target 1 "K9" Xyz Monster or 1 Rank 5 Xyz Monster you control; Special Summon from your Extra Deck, 1 "K9" Xyz Monster with a different name, by using that monster you control (this is treated as an Xyz Summon, transfer its materials to the Summoned monster). At the end of the Battle Phase, if your "K9" monster battled this turn: You can Set this card from your GY. You can only use each effect of "K9-LC Release Restraint" once per turn.
target_text: -- K9-LC
  local SETCARD_ID_0 = 0x1cb --K9
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--set
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_PHASE + PHASE_BATTLE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + o)
  	e2:SetCondition(s.setcon)
  	e2:SetTarget(s.settg)
  	e2:SetOperation(s.setop)
  	c:RegisterEffect(e2)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.GlobalEffect()
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_BATTLED)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttackTarget()
  	local ar = Duel.GetAttacker()
  	if at and at:IsSetCard(SETCARD_ID_0) then
  		Duel.RegisterFlagEffect(at:GetControler(), id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  	if ar and ar:IsSetCard(SETCARD_ID_0) then
  		Duel.RegisterFlagEffect(ar:GetControler(), id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  end
  function s.filter1(c, e, tp)
  	return c:IsFaceup()
  		and c:IsType(TYPE_XYZ)
  		and (c:IsSetCard(SETCARD_ID_0) or c:IsRank(5))
  		and aux.MustMaterialCheck(c, tp, EFFECT_MUST_BE_XMATERIAL)
  		and Duel.IsExistingMatchingCard(s.filter2, tp, LOCATION_EXTRA, 0, 1, nil, e, tp, c, c:GetCode())
  end
  function s.filter2(c, e, tp, mc, code)
  	return c:IsSetCard(SETCARD_ID_0)
  		and not c:IsCode(code)
  		and mc:IsCanBeXyzMaterial(c)
  		and c:IsCanBeSpecialSummoned(e, SUMMON_TYPE_XYZ, tp, false, false)
  		and Duel.GetLocationCountFromEx(tp, tp, mc, c) > 0
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE) and s.filter1(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter1, tp, LOCATION_MZONE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter1, tp, LOCATION_MZONE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_EXTRA)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if not aux.MustMaterialCheck(tc, tp, EFFECT_MUST_BE_XMATERIAL) or tc:IsFacedown() or not tc:IsRelateToChain() or tc:IsControler(1 - tp) or tc:IsImmuneToEffect(e) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.filter2, tp, LOCATION_EXTRA, 0, 1, 1, nil, e, tp, tc, tc:GetCode())
  	local sc = g:GetFirst()
  	if sc then
  		local mg = tc:GetOverlayGroup()
  		if mg:GetCount() ~= 0 then
  			Duel.Overlay(sc, mg)
  		end
  		sc:SetMaterial(Group.FromCards(tc))
  		Duel.Overlay(sc, Group.FromCards(tc))
  		Duel.SpecialSummon(sc, SUMMON_TYPE_XYZ, tp, tp, false, false, POS_FACEUP)
  		sc:CompleteProcedure()
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) > 0
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsSSetable()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, e:GetHandler(), 1, 0, 0)
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToChain() and aux.NecroValleyFilter()(c) then
  		Duel.SSet(tp, c)
  	end
  end
  
  