name: Stairway
card_text: When this card is activated: You can send 1 "Fabled" monster from your Deck to the GY. You can target 1 "Fabled" monster in your GY; discard 2 cards, and if you do, add that monster to your hand. You can only use this effect of "Stairway to a Fabled Realm" once per turn. If your "Fabled" monster attacks an opponent's monster and you have less cards in your hand than your opponent, your monster gains ATK equal to the difference in the number of cards x 200, during damage calculation only. You can only activate 1 "Stairway to a Fabled Realm" per turn.
external_ids: local SETCARD_ID_0 = 0x35 --Fabled
  
lua: local SETCARD_ID_0 = 0x35 --Fabled
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOGRAVE + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_HANDES)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--atk
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_UPDATE_ATTACK)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetTargetRange(LOCATION_MZONE, 0)
  	e3:SetTarget(s.atktg)
  	e3:SetCondition(s.atkcon)
  	e3:SetValue(s.atkval)
  	c:RegisterEffect(e3)
  end
  function s.tgfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGrave()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.tgfilter, tp, LOCATION_DECK, 0, nil)
  	if g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SendtoGrave(sg, REASON_EFFECT)
  	end
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_GRAVE, 0, 1, nil) and Duel.IsExistingMatchingCard(nil, tp, LOCATION_HAND, 0, 2, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g1 = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 2)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if Duel.DiscardHand(tp, aux.TRUE, 2, 2, REASON_EFFECT + REASON_DISCARD) > 1 and tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  	end
  end
  function s.atktg(e, c)
  	return c:IsSetCard(SETCARD_ID_0) and Duel.GetAttacker() == c
  end
  function s.atkcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetCurrentPhase() == PHASE_DAMAGE_CAL and Duel.GetAttackTarget() ~= nil and Duel.GetFieldGroupCount(tp, 0, LOCATION_HAND) > Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0)
  end
  function s.atkval(e, c)
  	local ct = Duel.GetFieldGroupCount(e:GetHandlerPlayer(), 0, LOCATION_HAND) - Duel.GetFieldGroupCount(e:GetHandlerPlayer(), LOCATION_HAND, 0)
  	return ct > 0 and ct * 200
  end
  
  
source_text: Stairway
  When this card is activated: You can send 1 "Fabled" monster from your Deck to the GY. You can target 1 "Fabled" monster in your GY; discard 2 cards, and if you do, add that monster to your hand. You can only use this effect of "Stairway to a Fabled Realm" once per turn. If your "Fabled" monster attacks an opponent's monster and you have less cards in your hand than your opponent, your monster gains ATK equal to the difference in the number of cards x 200, during damage calculation only. You can only activate 1 "Stairway to a Fabled Realm" per turn.
target_text: -- Stairway
  local SETCARD_ID_0 = 0x35 --Fabled
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOGRAVE + CATEGORY_DECKDES)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_HANDES)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--atk
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_UPDATE_ATTACK)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetTargetRange(LOCATION_MZONE, 0)
  	e3:SetTarget(s.atktg)
  	e3:SetCondition(s.atkcon)
  	e3:SetValue(s.atkval)
  	c:RegisterEffect(e3)
  end
  function s.tgfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGrave()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.tgfilter, tp, LOCATION_DECK, 0, nil)
  	if g:GetCount() > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SendtoGrave(sg, REASON_EFFECT)
  	end
  end
  function s.thfilter(c)
  	return c:IsType(TYPE_MONSTER) and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_GRAVE, 0, 1, nil) and Duel.IsExistingMatchingCard(nil, tp, LOCATION_HAND, 0, 2, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g1 = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, tp, 2)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if Duel.DiscardHand(tp, aux.TRUE, 2, 2, REASON_EFFECT + REASON_DISCARD) > 1 and tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  	end
  end
  function s.atktg(e, c)
  	return c:IsSetCard(SETCARD_ID_0) and Duel.GetAttacker() == c
  end
  function s.atkcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetCurrentPhase() == PHASE_DAMAGE_CAL and Duel.GetAttackTarget() ~= nil and Duel.GetFieldGroupCount(tp, 0, LOCATION_HAND) > Duel.GetFieldGroupCount(tp, LOCATION_HAND, 0)
  end
  function s.atkval(e, c)
  	local ct = Duel.GetFieldGroupCount(e:GetHandlerPlayer(), 0, LOCATION_HAND) - Duel.GetFieldGroupCount(e:GetHandlerPlayer(), LOCATION_HAND, 0)
  	return ct > 0 and ct * 200
  end
  
  