name: Evigishki
card_text: You can Ritual Summon this card with any "Gishki" Ritual Spell. If this card is Ritual Summoned: You can target 1 WATER monster in your GY; Special Summon it. Cannot be destroyed by battle with a monster Special Summoned from the Extra Deck. Once per turn, when your opponent activates a monster effect (Quick Effect): You can return 1 "Gishki" Ritual Monster you control to the hand, and if you do, negate the activation, and if you do that, shuffle it into the Deck.
external_ids: local SETCARD_ID_0 = 0x3a --Gishki
  
lua: local SETCARD_ID_0 = 0x3a --Gishki
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--battle indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetValue(s.indes)
  	c:RegisterEffect(e2)
  	--negate
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_NEGATE + CATEGORY_TOHAND + CATEGORY_TODECK)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetCode(EVENT_CHAINING)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.negcon)
  	e3:SetTarget(s.negtg)
  	e3:SetOperation(s.negop)
  	c:RegisterEffect(e3)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.spfilter(c, e, tp)
  	return c:IsAttribute(ATTRIBUTE_WATER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.indes(e, c)
  	return c:IsSummonLocation(LOCATION_EXTRA)
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	return not e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) and rp == 1 - tp and Duel.IsChainNegatable(ev) and re:IsActiveType(TYPE_MONSTER)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_RITUAL) and c:IsAbleToHand() and c:IsFaceup()
  end
  function s.negtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local rc = re:GetHandler()
  	if chk == 0 then
  		return (not rc:IsRelateToEffect(re) or rc:IsAbleToDeck()) and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_MZONE)
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if rc:IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_TODECK, eg, 1, 0, 0)
  	end
  	if re:GetActivateLocation() == LOCATION_GRAVE then
  		e:SetCategory(e:GetCategory() | CATEGORY_GRAVE_ACTION)
  	else
  		e:SetCategory(e:GetCategory() & ~CATEGORY_GRAVE_ACTION)
  	end
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local tc = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil):GetFirst()
  	if tc and Duel.SendtoHand(tc, nil, REASON_EFFECT) > 0 and tc:IsLocation(LOCATION_HAND) and Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SendtoDeck(eg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  	end
  end
  
  
source_text: Evigishki
  You can Ritual Summon this card with any "Gishki" Ritual Spell. If this card is Ritual Summoned: You can target 1 WATER monster in your GY; Special Summon it. Cannot be destroyed by battle with a monster Special Summoned from the Extra Deck. Once per turn, when your opponent activates a monster effect (Quick Effect): You can return 1 "Gishki" Ritual Monster you control to the hand, and if you do, negate the activation, and if you do that, shuffle it into the Deck.
target_text: -- Evigishki
  local SETCARD_ID_0 = 0x3a --Gishki
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--spsummon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e1:SetProperty(EFFECT_FLAG_DELAY + EFFECT_FLAG_CARD_TARGET)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--battle indes
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetValue(s.indes)
  	c:RegisterEffect(e2)
  	--negate
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_NEGATE + CATEGORY_TOHAND + CATEGORY_TODECK)
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetCode(EVENT_CHAINING)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DAMAGE_CAL)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.negcon)
  	e3:SetTarget(s.negtg)
  	e3:SetOperation(s.negop)
  	c:RegisterEffect(e3)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsSummonType(SUMMON_TYPE_RITUAL)
  end
  function s.spfilter(c, e, tp)
  	return c:IsAttribute(ATTRIBUTE_WATER) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_GRAVE) and chkc:IsControler(tp) and s.spfilter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.indes(e, c)
  	return c:IsSummonLocation(LOCATION_EXTRA)
  end
  function s.negcon(e, tp, eg, ep, ev, re, r, rp)
  	return not e:GetHandler():IsStatus(STATUS_BATTLE_DESTROYED) and rp == 1 - tp and Duel.IsChainNegatable(ev) and re:IsActiveType(TYPE_MONSTER)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_RITUAL) and c:IsAbleToHand() and c:IsFaceup()
  end
  function s.negtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local rc = re:GetHandler()
  	if chk == 0 then
  		return (not rc:IsRelateToEffect(re) or rc:IsAbleToDeck()) and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_MZONE)
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if rc:IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_TODECK, eg, 1, 0, 0)
  	end
  	if re:GetActivateLocation() == LOCATION_GRAVE then
  		e:SetCategory(e:GetCategory() | CATEGORY_GRAVE_ACTION)
  	else
  		e:SetCategory(e:GetCategory() & ~CATEGORY_GRAVE_ACTION)
  	end
  end
  function s.negop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RTOHAND)
  	local tc = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_MZONE, 0, 1, 1, nil):GetFirst()
  	if tc and Duel.SendtoHand(tc, nil, REASON_EFFECT) > 0 and tc:IsLocation(LOCATION_HAND) and Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SendtoDeck(eg, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  	end
  end
  
  