name: Impenetrable
card_text: Select 1 monster on your side of the field to activate this card. While you have 2 or more monsters on your side of the field, and all monsters on your side of the field are in Defense Position, the selected monster gains 700 DEF. When the selected monster is removed from 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:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_CONTINUOUS_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_FIELD)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCode(EVENT_LEAVE_FIELD)
  	e2:SetCondition(s.descon)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--def up
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_TARGET)
  	e3:SetCode(EFFECT_UPDATE_DEFENSE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.defcon)
  	e3:SetValue(700)
  	c:RegisterEffect(e3)
  end
  function s.filter(c)
  	return c:IsDefenseAbove(0)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 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:IsRelateToEffect(e) then
  		c:SetCardTarget(tc)
  	end
  end
  function s.defcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetFieldGroupCount(tp, LOCATION_MZONE, 0) >= 2 and not Duel.IsExistingMatchingCard(Card.IsAttackPos, tp, LOCATION_MZONE, 0, 1, nil)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetHandler():GetFirstCardTarget()
  	return tc and eg:IsContains(tc)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetHandler(), REASON_EFFECT)
  end
  
  
source_text: Impenetrable
  Select 1 monster on your side of the field to activate this card. While you have 2 or more monsters on your side of the field, and all monsters on your side of the field are in Defense Position, the selected monster gains 700 DEF. When the selected monster is removed from the field, destroy this card.
target_text: -- Impenetrable
  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:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_CONTINUOUS_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_FIELD)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCode(EVENT_LEAVE_FIELD)
  	e2:SetCondition(s.descon)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  	--def up
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_TARGET)
  	e3:SetCode(EFFECT_UPDATE_DEFENSE)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.defcon)
  	e3:SetValue(700)
  	c:RegisterEffect(e3)
  end
  function s.filter(c)
  	return c:IsDefenseAbove(0)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_MZONE)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	Duel.SelectTarget(tp, s.filter, tp, LOCATION_MZONE, 0, 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:IsRelateToEffect(e) then
  		c:SetCardTarget(tc)
  	end
  end
  function s.defcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetFieldGroupCount(tp, LOCATION_MZONE, 0) >= 2 and not Duel.IsExistingMatchingCard(Card.IsAttackPos, tp, LOCATION_MZONE, 0, 1, nil)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = e:GetHandler():GetFirstCardTarget()
  	return tc and eg:IsContains(tc)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Destroy(e:GetHandler(), REASON_EFFECT)
  end
  
  