name: Heritage
card_text: Add 1 "Noble Knight" monster or 1 "Noble Arms" card from your Deck or GY to your hand. If your "Noble Knight" monster equipped with a "Noble Arms" Equip Spell is destroyed by battle and sent to the GY, while this card is in your GY: You can add this card to your hand. You can only use 1 "Heritage of the Chalice" effect per turn, and only once that turn.
external_ids: local SETCARD_ID_0 = 0x107a --Noble
  
lua: local SETCARD_ID_0 = 0x107a --Noble
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetCode(EVENT_LEAVE_FIELD_P)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetOperation(s.checkop)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_TOHAND)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_CUSTOM + id)
  	e3:SetRange(LOCATION_GRAVE)
  	e3:SetCountLimit(1, id)
  	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e3:SetCondition(s.thcon)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.filter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER)) or c:IsSetCard(0x207a)) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.checkfilter(c, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsReason(REASON_BATTLE) and c:IsControler(tp) and c:GetEquipCount() > 0 and c:GetEquipGroup():IsExists(Card.IsSetCard, 1, nil, 0x207a)
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local v = 0
  	if eg:IsExists(s.checkfilter, 1, nil, 0) then
  		v = v + 1
  	end
  	if eg:IsExists(s.checkfilter, 1, nil, 1) then
  		v = v + 2
  	end
  	if v > 0 then
  		local evp = ({ 0, 1, PLAYER_ALL })[v]
  		Duel.RaiseSingleEvent(e:GetHandler(), EVENT_CUSTOM + id, e, 0, rp, ep, evp)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return ev == tp or ev == PLAYER_ALL
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SendtoHand(e:GetHandler(), nil, REASON_EFFECT)
  	end
  end
  
  
source_text: Heritage
  Add 1 "Noble Knight" monster or 1 "Noble Arms" card from your Deck or GY to your hand. If your "Noble Knight" monster equipped with a "Noble Arms" Equip Spell is destroyed by battle and sent to the GY, while this card is in your GY: You can add this card to your hand. You can only use 1 "Heritage of the Chalice" effect per turn, and only once that turn.
target_text: -- Heritage
  local SETCARD_ID_0 = 0x107a --Noble
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetCode(EVENT_LEAVE_FIELD_P)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetOperation(s.checkop)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_TOHAND)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetCode(EVENT_CUSTOM + id)
  	e3:SetRange(LOCATION_GRAVE)
  	e3:SetCountLimit(1, id)
  	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e3:SetCondition(s.thcon)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.filter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER)) or c:IsSetCard(0x207a)) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  function s.checkfilter(c, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsReason(REASON_BATTLE) and c:IsControler(tp) and c:GetEquipCount() > 0 and c:GetEquipGroup():IsExists(Card.IsSetCard, 1, nil, 0x207a)
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local v = 0
  	if eg:IsExists(s.checkfilter, 1, nil, 0) then
  		v = v + 1
  	end
  	if eg:IsExists(s.checkfilter, 1, nil, 1) then
  		v = v + 2
  	end
  	if v > 0 then
  		local evp = ({ 0, 1, PLAYER_ALL })[v]
  		Duel.RaiseSingleEvent(e:GetHandler(), EVENT_CUSTOM + id, e, 0, rp, ep, evp)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return ev == tp or ev == PLAYER_ALL
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SendtoHand(e:GetHandler(), nil, REASON_EFFECT)
  	end
  end
  
  