name: Mecha
card_text: This card's Level is increased by the total Levels of all "Mecha Phantom Beast Tokens" you control. While you control a Token, this card cannot be destroyed by battle or card effects. Each turn, the first "Mecha Phantom Beast Token" you control that would be destroyed by battle, is not destroyed.
external_ids: local CARD_ID_0 = 31533705 --Mecha Phantom Beast Token
  
lua: local CARD_ID_0 = 31533705 --Mecha Phantom Beast Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--level
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_UPDATE_LEVEL)
  	e1:SetValue(s.lvval)
  	c:RegisterEffect(e1)
  	--
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e2:SetCondition(aux.tkfcon)
  	e2:SetValue(1)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	c:RegisterEffect(e3)
  	--
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetTargetRange(LOCATION_MZONE, 0)
  	e4:SetTarget(s.indtg)
  	e4:SetCountLimit(1)
  	e4:SetValue(s.valcon)
  	c:RegisterEffect(e4)
  end
  function s.lvval(e, c)
  	local tp = c:GetControler()
  	return Duel.GetMatchingGroup(Card.IsCode, tp, LOCATION_MZONE, 0, nil, CARD_ID_0):GetSum(Card.GetLevel)
  end
  function s.indtg(e, c)
  	return c:IsCode(CARD_ID_0)
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  
  
source_text: Mecha
  This card's Level is increased by the total Levels of all "Mecha Phantom Beast Tokens" you control. While you control a Token, this card cannot be destroyed by battle or card effects. Each turn, the first "Mecha Phantom Beast Token" you control that would be destroyed by battle, is not destroyed.
target_text: -- Mecha
  local CARD_ID_0 = 31533705 --Mecha Phantom Beast Token
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--level
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCode(EFFECT_UPDATE_LEVEL)
  	e1:SetValue(s.lvval)
  	c:RegisterEffect(e1)
  	--
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_SINGLE)
  	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  	e2:SetCondition(aux.tkfcon)
  	e2:SetValue(1)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_INDESTRUCTABLE_EFFECT)
  	c:RegisterEffect(e3)
  	--
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD)
  	e4:SetCode(EFFECT_INDESTRUCTABLE_COUNT)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetTargetRange(LOCATION_MZONE, 0)
  	e4:SetTarget(s.indtg)
  	e4:SetCountLimit(1)
  	e4:SetValue(s.valcon)
  	c:RegisterEffect(e4)
  end
  function s.lvval(e, c)
  	local tp = c:GetControler()
  	return Duel.GetMatchingGroup(Card.IsCode, tp, LOCATION_MZONE, 0, nil, CARD_ID_0):GetSum(Card.GetLevel)
  end
  function s.indtg(e, c)
  	return c:IsCode(CARD_ID_0)
  end
  function s.valcon(e, re, r, rp)
  	return bit.band(r, REASON_BATTLE) ~= 0
  end
  
  