name: Safe
card_text: Activate this card by targeting 1 face-up Attack Position monster on the field; that face-up monster cannot be destroyed by battle or your opponent's card effects, your opponent cannot target it with card effects, also it cannot attack your opponent directly. When this card leaves the field, destroy that monster. When that monster leaves the field, destroy this card.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--Destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetCode(EVENT_LEAVE_FIELD_P)
  	e2:SetOperation(s.checkop)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  	e3:SetCode(EVENT_LEAVE_FIELD)
  	e3:SetOperation(s.desop)
  	e3:SetLabelObject(e2)
  	c:RegisterEffect(e3)
  	--Destroy2
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCode(EVENT_LEAVE_FIELD)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCondition(s.descon2)
  	e4:SetOperation(s.desop2)
  	c:RegisterEffect(e4)
  	--indes
  	local e5 = Effect.CreateEffect(c)
  	e5:SetType(EFFECT_TYPE_TARGET)
  	e5:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e5:SetRange(LOCATION_SZONE)
  	e5:SetValue(1)
  	c:RegisterEffect(e5)
  	local e6 = e5:Clone()
  	e6:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e6:SetValue(s.efilter)
  	c:RegisterEffect(e6)
  	--
  	local e7 = e6:Clone()
  	e7:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
  	e7:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e7:SetValue(s.tgval)
  	c:RegisterEffect(e7)
  	--
  	local e8 = e5:Clone()
  	e8:SetCode(EFFECT_CANNOT_DIRECT_ATTACK)
  	e8:SetCondition(s.acon)
  	c:RegisterEffect(e8)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsAttackPos()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUPATTACK)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if c:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		c:SetCardTarget(tc)
  	end
  end
  function s.acon(e)
  	return e:GetHandlerPlayer() == e:GetHandler():GetFirstCardTarget():GetControler()
  end
  function s.efilter(e, re)
  	return e:GetHandlerPlayer() ~= re:GetOwnerPlayer()
  end
  function s.tgval(e, re, rp)
  	return rp == 1 - e:GetHandlerPlayer()
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsDisabled() then
  		e:SetLabel(1)
  	else
  		e:SetLabel(0)
  	end
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetLabelObject():GetLabel() ~= 0 then
  		return
  	end
  	local tc = e:GetHandler():GetFirstCardTarget()
  	if tc and tc:IsLocation(LOCATION_MZONE) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  function s.descon2(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetHandler():GetFirstCardTarget()
  	return tc and eg:IsContains(tc)
  end
  function s.desop2(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetHandler(), REASON_EFFECT)
  end
  
  
source_text: Safe
  Activate this card by targeting 1 face-up Attack Position monster on the field; that face-up monster cannot be destroyed by battle or your opponent's card effects, your opponent cannot target it with card effects, also it cannot attack your opponent directly. When this card leaves the field, destroy that monster. When that monster leaves the field, destroy this card.
target_text: -- Safe
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--Destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e2:SetCode(EVENT_LEAVE_FIELD_P)
  	e2:SetOperation(s.checkop)
  	c:RegisterEffect(e2)
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_SINGLE)
  	e3:SetCode(EVENT_LEAVE_FIELD)
  	e3:SetOperation(s.desop)
  	e3:SetLabelObject(e2)
  	c:RegisterEffect(e3)
  	--Destroy2
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_CONTINUOUS + EFFECT_TYPE_FIELD)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCode(EVENT_LEAVE_FIELD)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCondition(s.descon2)
  	e4:SetOperation(s.desop2)
  	c:RegisterEffect(e4)
  	--indes
  	local e5 = Effect.CreateEffect(c)
  	e5:SetType(EFFECT_TYPE_TARGET)
  	e5:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e5:SetRange(LOCATION_SZONE)
  	e5:SetValue(1)
  	c:RegisterEffect(e5)
  	local e6 = e5:Clone()
  	e6:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	e6:SetValue(s.efilter)
  	c:RegisterEffect(e6)
  	--
  	local e7 = e6:Clone()
  	e7:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
  	e7:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
  	e7:SetValue(s.tgval)
  	c:RegisterEffect(e7)
  	--
  	local e8 = e5:Clone()
  	e8:SetCode(EFFECT_CANNOT_DIRECT_ATTACK)
  	e8:SetCondition(s.acon)
  	c:RegisterEffect(e8)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsAttackPos()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and s.filter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_FACEUPATTACK)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, 1, nil)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local tc = Duel.GetFirstTarget()
  	if c:IsRelateToEffect(e) and tc:IsFaceup() and tc:IsRelateToEffect(e) then
  		c:SetCardTarget(tc)
  	end
  end
  function s.acon(e)
  	return e:GetHandlerPlayer() == e:GetHandler():GetFirstCardTarget():GetControler()
  end
  function s.efilter(e, re)
  	return e:GetHandlerPlayer() ~= re:GetOwnerPlayer()
  end
  function s.tgval(e, re, rp)
  	return rp == 1 - e:GetHandlerPlayer()
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsDisabled() then
  		e:SetLabel(1)
  	else
  		e:SetLabel(0)
  	end
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetLabelObject():GetLabel() ~= 0 then
  		return
  	end
  	local tc = e:GetHandler():GetFirstCardTarget()
  	if tc and tc:IsLocation(LOCATION_MZONE) then
  		Duel.Destroy(tc, REASON_EFFECT)
  	end
  end
  function s.descon2(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetHandler():GetFirstCardTarget()
  	return tc and eg:IsContains(tc)
  end
  function s.desop2(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetHandler(), REASON_EFFECT)
  end
  
  