name: Resh
card_text: Target 1 "Shaddoll" monster in your GY; Special Summon it in face-up or face-down Defense Position.
  You can banish this card and 1 "Shaddoll" card from your GY, then activate 1 of these effects;
  ● Change 1 face-down monster you control to face-up Defense Position.
  ● Change 1 face-up monster you control to face-down Defense Position.
  You can only use 1 "Resh Shaddoll Incarnation" effect per turn, and only once that turn.
external_ids: local SETCARD_ID_0 = 0x9d --Shaddoll
  
lua: local SETCARD_ID_0 = 0x9d --Shaddoll
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMING_MAIN_END + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--pos change
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetHintTiming(0, TIMING_MAIN_END + TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.poscost)
  	e2:SetTarget(s.postg)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_DEFENSE)
  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.filter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.filter, 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 not tc:IsRelateToEffect(e) then
  		return
  	end
  	if Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_DEFENSE) ~= 0 and tc:IsFacedown() then
  		Duel.ConfirmCards(1 - tp, tc)
  	end
  end
  function s.cfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToRemoveAsCost()
  end
  function s.poscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, c) and c:IsAbleToRemoveAsCost()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_GRAVE, 0, 1, 1, c)
  	g:AddCard(c)
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.IsExistingMatchingCard(Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, nil)
  	local b2 = Duel.IsExistingMatchingCard(Card.IsCanTurnSet, tp, LOCATION_MZONE, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local s = 0
  	if b1 and not b2 then
  		s = Duel.SelectOption(tp, aux.Stringid(id, 2))
  	end
  	if not b1 and b2 then
  		s = Duel.SelectOption(tp, aux.Stringid(id, 3)) + 1
  	end
  	if b1 and b2 then
  		s = Duel.SelectOption(tp, aux.Stringid(id, 2), aux.Stringid(id, 3))
  	end
  	e:SetLabel(s)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, nil, 1, 0, 0)
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetLabel() == 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.ChangePosition(g:GetFirst(), POS_FACEUP_DEFENSE)
  		end
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, Card.IsCanTurnSet, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.ChangePosition(g:GetFirst(), POS_FACEDOWN_DEFENSE)
  		end
  	end
  end
  
  
source_text: Resh
  Target 1 "Shaddoll" monster in your GY; Special Summon it in face-up or face-down Defense Position.
  You can banish this card and 1 "Shaddoll" card from your GY, then activate 1 of these effects;
  ● Change 1 face-down monster you control to face-up Defense Position.
  ● Change 1 face-up monster you control to face-down Defense Position.
  You can only use 1 "Resh Shaddoll Incarnation" effect per turn, and only once that turn.
target_text: -- Resh
  local SETCARD_ID_0 = 0x9d --Shaddoll
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetHintTiming(0, TIMING_MAIN_END + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--pos change
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_POSITION)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetHintTiming(0, TIMING_MAIN_END + TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetCost(s.poscost)
  	e2:SetTarget(s.postg)
  	e2:SetOperation(s.posop)
  	c:RegisterEffect(e2)
  end
  function s.filter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_DEFENSE)
  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.filter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.filter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.filter, 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 not tc:IsRelateToEffect(e) then
  		return
  	end
  	if Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_DEFENSE) ~= 0 and tc:IsFacedown() then
  		Duel.ConfirmCards(1 - tp, tc)
  	end
  end
  function s.cfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToRemoveAsCost()
  end
  function s.poscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_GRAVE, 0, 1, c) and c:IsAbleToRemoveAsCost()
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_GRAVE, 0, 1, 1, c)
  	g:AddCard(c)
  	Duel.Remove(g, POS_FACEUP, REASON_COST)
  end
  function s.postg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local b1 = Duel.IsExistingMatchingCard(Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, nil)
  	local b2 = Duel.IsExistingMatchingCard(Card.IsCanTurnSet, tp, LOCATION_MZONE, 0, 1, nil)
  	if chk == 0 then
  		return b1 or b2
  	end
  	local s = 0
  	if b1 and not b2 then
  		s = Duel.SelectOption(tp, aux.Stringid(id, 2))
  	end
  	if not b1 and b2 then
  		s = Duel.SelectOption(tp, aux.Stringid(id, 3)) + 1
  	end
  	if b1 and b2 then
  		s = Duel.SelectOption(tp, aux.Stringid(id, 2), aux.Stringid(id, 3))
  	end
  	e:SetLabel(s)
  	Duel.SetOperationInfo(0, CATEGORY_POSITION, nil, 1, 0, 0)
  end
  function s.posop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetLabel() == 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, Card.IsFacedown, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.ChangePosition(g:GetFirst(), POS_FACEUP_DEFENSE)
  		end
  	else
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_POSCHANGE)
  		local g = Duel.SelectMatchingCard(tp, Card.IsCanTurnSet, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.ChangePosition(g:GetFirst(), POS_FACEDOWN_DEFENSE)
  		end
  	end
  end
  
  