name: Staysailor
card_text: (Quick Effect): You can target 1 face-up monster you control; send 1 other Plant monster you control to the GY, and if you do, the next time the targeted monster would be destroyed by battle or card effect this turn, it is not destroyed. If this card is sent to the GY by a card effect: You can send 1 Level 5 or lower Plant monster from your Deck or Extra Deck to the GY. You can only use each effect of "Staysailor Romarin" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--indes
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOGRAVE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.indtg)
  	e1:SetOperation(s.indop)
  	c:RegisterEffect(e1)
  	--to grave
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOGRAVE)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.tgcon)
  	e2:SetTarget(s.tgtg)
  	e2:SetOperation(s.tgop)
  	c:RegisterEffect(e2)
  end
  function s.indfilter(c, tp)
  	return c:IsFaceup() and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, c)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_PLANT) and c:IsAbleToGrave()
  end
  function s.indtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.indfilter(chkc, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.indfilter, tp, LOCATION_MZONE, 0, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.indfilter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_MZONE)
  end
  function s.indop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	local exc = nil
  	if tc:IsRelateToEffect(e) then
  		exc = tc
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, exc)
  	local sc = g:GetFirst()
  	if sc then
  		Duel.HintSelection(g)
  		if Duel.SendtoGrave(sc, REASON_EFFECT) ~= 0 and sc:IsLocation(LOCATION_GRAVE) and tc:IsRelateToEffect(e) then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetDescription(aux.Stringid(id, 2))
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_CLIENT_HINT)
  			e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  			e1:SetCountLimit(1)
  			e1:SetValue(s.valcon)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  		end
  	end
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE + REASON_EFFECT) ~= 0
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_EFFECT)
  end
  function s.tgfilter(c)
  	return c:IsLevelBelow(5) and c:IsRace(RACE_PLANT) and c:IsAbleToGrave()
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_DECK + LOCATION_EXTRA)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectMatchingCard(tp, s.tgfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoGrave(g, REASON_EFFECT)
  	end
  end
  
  
source_text: Staysailor
  (Quick Effect): You can target 1 face-up monster you control; send 1 other Plant monster you control to the GY, and if you do, the next time the targeted monster would be destroyed by battle or card effect this turn, it is not destroyed. If this card is sent to the GY by a card effect: You can send 1 Level 5 or lower Plant monster from your Deck or Extra Deck to the GY. You can only use each effect of "Staysailor Romarin" once per turn.
target_text: -- Staysailor
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--indes
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TOGRAVE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.indtg)
  	e1:SetOperation(s.indop)
  	c:RegisterEffect(e1)
  	--to grave
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOGRAVE)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.tgcon)
  	e2:SetTarget(s.tgtg)
  	e2:SetOperation(s.tgop)
  	c:RegisterEffect(e2)
  end
  function s.indfilter(c, tp)
  	return c:IsFaceup() and Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, c)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsRace(RACE_PLANT) and c:IsAbleToGrave()
  end
  function s.indtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(tp) and s.indfilter(chkc, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.indfilter, tp, LOCATION_MZONE, 0, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.indfilter, tp, LOCATION_MZONE, 0, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_MZONE)
  end
  function s.indop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	local exc = nil
  	if tc:IsRelateToEffect(e) then
  		exc = tc
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, exc)
  	local sc = g:GetFirst()
  	if sc then
  		Duel.HintSelection(g)
  		if Duel.SendtoGrave(sc, REASON_EFFECT) ~= 0 and sc:IsLocation(LOCATION_GRAVE) and tc:IsRelateToEffect(e) then
  			local e1 = Effect.CreateEffect(c)
  			e1:SetDescription(aux.Stringid(id, 2))
  			e1:SetType(EFFECT_TYPE_SINGLE)
  			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_CLIENT_HINT)
  			e1:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  			e1:SetCountLimit(1)
  			e1:SetValue(s.valcon)
  			e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  			tc:RegisterEffect(e1)
  		end
  	end
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE + REASON_EFFECT) ~= 0
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_EFFECT)
  end
  function s.tgfilter(c)
  	return c:IsLevelBelow(5) and c:IsRace(RACE_PLANT) and c:IsAbleToGrave()
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_DECK + LOCATION_EXTRA)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectMatchingCard(tp, s.tgfilter, tp, LOCATION_DECK + LOCATION_EXTRA, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoGrave(g, REASON_EFFECT)
  	end
  end
  
  