name: Secret
card_text: You can banish 2 "Six Samurai" monsters from your GY and/or face-up from your field, then target 1 "Six Samurai" monster in your GY, except "Secret Six Samurai - Hatsume"; Special Summon it. You can only use this effect of "Secret Six Samurai - Hatsume" once per turn. If exactly 1 "Six Samurai" monster you control (and no other cards) would be destroyed by card effect, you can banish this card from your GY instead.
external_ids: local SETCARD_ID_0 = 0x103d --Six
  
lua: local SETCARD_ID_0 = 0x103d --Six
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetCountLimit(1, id)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	e2:SetOperation(s.repop)
  	c:RegisterEffect(e2)
  end
  function s.filter0(c)
  	return c:IsLocation(LOCATION_MZONE) and c:GetSequence() < 5
  end
  function s.filter1(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToRemoveAsCost() and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())
  end
  function s.filter3(c, e, tp)
  	return s.filter1(c) and Duel.IsExistingMatchingCard(s.filter4, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, c, e, tp, c)
  end
  function s.filter4(c, e, tp, rc)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local g = Group.FromCards(c, rc)
  	local ct = g:FilterCount(s.filter0, nil)
  	return s.filter1(c) and ft + ct > 0 and Duel.IsExistingTarget(s.filter2, tp, LOCATION_GRAVE, 0, 1, g, e, tp)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter3, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g1 = Duel.SelectMatchingCard(tp, s.filter3, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, 1, nil, e, tp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g2 = Duel.SelectMatchingCard(tp, s.filter4, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, 1, g1:GetFirst(), e, tp, g1:GetFirst())
  	g1:Merge(g2)
  	Duel.Remove(g1, POS_FACEUP, REASON_COST)
  end
  function s.filter2(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter2(chkc, e, tp)
  	end
  	if chk == 0 then
  		return true
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.filter2, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc and tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsReason(REASON_EFFECT) and not c:IsReason(REASON_REPLACE)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemove() and eg:IsExists(s.repfilter, 1, nil, tp) and eg:GetCount() == 1
  	end
  	return Duel.SelectEffectYesNo(tp, e:GetHandler(), 96)
  end
  function s.repval(e, c)
  	return s.repfilter(c, e:GetHandlerPlayer())
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_EFFECT)
  end
  
  
source_text: Secret
  You can banish 2 "Six Samurai" monsters from your GY and/or face-up from your field, then target 1 "Six Samurai" monster in your GY, except "Secret Six Samurai - Hatsume"; Special Summon it. You can only use this effect of "Secret Six Samurai - Hatsume" once per turn. If exactly 1 "Six Samurai" monster you control (and no other cards) would be destroyed by card effect, you can banish this card from your GY instead.
target_text: -- Secret
  local SETCARD_ID_0 = 0x103d --Six
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetType(EFFECT_TYPE_IGNITION)
  	e1:SetCountLimit(1, id)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--destroy replace
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EFFECT_DESTROY_REPLACE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetTarget(s.reptg)
  	e2:SetValue(s.repval)
  	e2:SetOperation(s.repop)
  	c:RegisterEffect(e2)
  end
  function s.filter0(c)
  	return c:IsLocation(LOCATION_MZONE) and c:GetSequence() < 5
  end
  function s.filter1(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToRemoveAsCost() and (c:IsLocation(LOCATION_GRAVE) or c:IsFaceup())
  end
  function s.filter3(c, e, tp)
  	return s.filter1(c) and Duel.IsExistingMatchingCard(s.filter4, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, c, e, tp, c)
  end
  function s.filter4(c, e, tp, rc)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local g = Group.FromCards(c, rc)
  	local ct = g:FilterCount(s.filter0, nil)
  	return s.filter1(c) and ft + ct > 0 and Duel.IsExistingTarget(s.filter2, tp, LOCATION_GRAVE, 0, 1, g, e, tp)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter3, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g1 = Duel.SelectMatchingCard(tp, s.filter3, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, 1, nil, e, tp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g2 = Duel.SelectMatchingCard(tp, s.filter4, tp, LOCATION_GRAVE + LOCATION_ONFIELD, 0, 1, 1, g1:GetFirst(), e, tp, g1:GetFirst())
  	g1:Merge(g2)
  	Duel.Remove(g1, POS_FACEUP, REASON_COST)
  end
  function s.filter2(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and not c:IsCode(id) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.filter2(chkc, e, tp)
  	end
  	if chk == 0 then
  		return true
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.filter2, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc and tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.repfilter(c, tp)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsReason(REASON_EFFECT) and not c:IsReason(REASON_REPLACE)
  end
  function s.reptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToRemove() and eg:IsExists(s.repfilter, 1, nil, tp) and eg:GetCount() == 1
  	end
  	return Duel.SelectEffectYesNo(tp, e:GetHandler(), 96)
  end
  function s.repval(e, c)
  	return s.repfilter(c, e:GetHandlerPlayer())
  end
  function s.repop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Remove(e:GetHandler(), POS_FACEUP, REASON_EFFECT)
  end
  
  