name: Hapi,
card_text: If you control "King's Sarcophagus", you can Special Summon this card (from your GY). You can only Special Summon "Hapi, Guidance of Horus" once per turn this way. If another card(s) you control leaves the field by an opponent's card effect, while this card is in your Monster Zone (except during the Damage Step): You can target 2 cards that are banished and/or in the GYs; either add both to the hand or shuffle both into the Deck. You can only use this effect of "Hapi, Guidance of Horus" once per turn.
external_ids: local CARD_ID_0 = 16528181 --King's Sarcophagus
  
lua: local CARD_ID_0 = 16528181 --King's Sarcophagus
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetRange(LOCATION_GRAVE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.sprcon)
  	c:RegisterEffect(e1)
  	--Leave Field
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_TODECK + CATEGORY_GRAVE_ACTION)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_LEAVE_FIELD)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.descon)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.sprfilter(c)
  	return c:IsFaceup() and c:IsCode(CARD_ID_0)
  end
  function s.sprcon(e, c)
  	if c == nil then
  		return true
  	end
  	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
  		return false
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.sprfilter, tp, LOCATION_ONFIELD, 0, 1, nil)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:GetReasonPlayer() == 1 - tp and c:IsReason(REASON_EFFECT)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp) and not eg:IsContains(e:GetHandler())
  end
  function s.tgfilter(c, tp)
  	return c:IsAbleToDeck() or c:IsAbleToHand()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.tgfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, LOCATION_GRAVE + LOCATION_REMOVED, 2, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.tgfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, LOCATION_GRAVE + LOCATION_REMOVED, 2, 2, nil)
  	if not g:FilterCount(Card.IsAbleToHand, nil, e) == g:GetCount() then
  		Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 2, 0, 0)
  	elseif not g:FilterCount(Card.IsAbleToDeck, nil, e) == g:GetCount() then
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 2, 0, 0)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 2, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetTargetsRelateToChain()
  	if tg:GetCount() == 2 then
  		if tg:FilterCount(Card.IsAbleToHand, nil) == 2 and (tg:FilterCount(Card.IsAbleToDeck, nil) < 2 or Duel.SelectOption(tp, aux.Stringid(id, 2), aux.Stringid(id, 3)) == 0) then
  			Duel.SendtoHand(tg, nil, REASON_EFFECT)
  		elseif tg:FilterCount(Card.IsAbleToDeck, nil) == 2 then
  			Duel.SendtoDeck(tg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  		end
  	end
  end
  
  
source_text: Hapi,
  If you control "King's Sarcophagus", you can Special Summon this card (from your GY). You can only Special Summon "Hapi, Guidance of Horus" once per turn this way. If another card(s) you control leaves the field by an opponent's card effect, while this card is in your Monster Zone (except during the Damage Step): You can target 2 cards that are banished and/or in the GYs; either add both to the hand or shuffle both into the Deck. You can only use this effect of "Hapi, Guidance of Horus" once per turn.
target_text: -- Hapi,
  local CARD_ID_0 = 16528181 --King's Sarcophagus
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_0)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetRange(LOCATION_GRAVE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.sprcon)
  	c:RegisterEffect(e1)
  	--Leave Field
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_TODECK + CATEGORY_GRAVE_ACTION)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_LEAVE_FIELD)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.descon)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.sprfilter(c)
  	return c:IsFaceup() and c:IsCode(CARD_ID_0)
  end
  function s.sprcon(e, c)
  	if c == nil then
  		return true
  	end
  	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
  		return false
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.sprfilter, tp, LOCATION_ONFIELD, 0, 1, nil)
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:GetReasonPlayer() == 1 - tp and c:IsReason(REASON_EFFECT)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp) and not eg:IsContains(e:GetHandler())
  end
  function s.tgfilter(c, tp)
  	return c:IsAbleToDeck() or c:IsAbleToHand()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.tgfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, LOCATION_GRAVE + LOCATION_REMOVED, 2, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.tgfilter, tp, LOCATION_GRAVE + LOCATION_REMOVED, LOCATION_GRAVE + LOCATION_REMOVED, 2, 2, nil)
  	if not g:FilterCount(Card.IsAbleToHand, nil, e) == g:GetCount() then
  		Duel.SetOperationInfo(0, CATEGORY_TODECK, g, 2, 0, 0)
  	elseif not g:FilterCount(Card.IsAbleToDeck, nil, e) == g:GetCount() then
  		Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 2, 0, 0)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_LEAVE_GRAVE, g, 2, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetTargetsRelateToChain()
  	if tg:GetCount() == 2 then
  		if tg:FilterCount(Card.IsAbleToHand, nil) == 2 and (tg:FilterCount(Card.IsAbleToDeck, nil) < 2 or Duel.SelectOption(tp, aux.Stringid(id, 2), aux.Stringid(id, 3)) == 0) then
  			Duel.SendtoHand(tg, nil, REASON_EFFECT)
  		elseif tg:FilterCount(Card.IsAbleToDeck, nil) == 2 then
  			Duel.SendtoDeck(tg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  		end
  	end
  end
  
  