name: Mecha
card_text: When a Token(s) is Special Summoned to your side of the field: Special Summon 1 "Mecha Phantom Beast Token" (Machine-Type/WIND/Level 3/ATK 0/DEF 0). You can only use this effect of "Mecha Phantom Beast Megaraptor" once per turn. 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. Once per turn: You can Tribute 1 Token; add 1 "Mecha Phantom Beast" monster from your Deck to your hand.
external_ids: local CARD_ID_0 = 31533705 --Mecha Phantom Beast Token
  local SETCARD_ID_0 = 0x101b --Mecha
  
lua: local CARD_ID_0 = 31533705 --Mecha Phantom Beast Token
  local SETCARD_ID_0 = 0x101b --Mecha
  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)
  	--token
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e4:SetCountLimit(1, id)
  	e4:SetCondition(s.spcon)
  	e4:SetTarget(s.sptg)
  	e4:SetOperation(s.spop)
  	c:RegisterEffect(e4)
  	--search
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 1))
  	e5:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e5:SetType(EFFECT_TYPE_IGNITION)
  	e5:SetRange(LOCATION_MZONE)
  	e5:SetCountLimit(1)
  	e5:SetCost(s.thcost)
  	e5:SetTarget(s.thtg)
  	e5:SetOperation(s.thop)
  	c:RegisterEffect(e5)
  end
  function s.lvval(e, c)
  	local tp = c:GetControler()
  	return Duel.GetMatchingGroup(Card.IsCode, tp, LOCATION_MZONE, 0, nil, id + 1):GetSum(Card.GetLevel)
  end
  function s.spfilter(c, tp)
  	return c:IsControler(tp) and c:IsType(TYPE_TOKEN)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.spfilter, 1, nil, tp)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	if Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, 0, 0, 3, RACE_MACHINE, ATTRIBUTE_WIND) then
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummon(token, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckReleaseGroup(tp, Card.IsType, 1, nil, TYPE_TOKEN)
  	end
  	local g = Duel.SelectReleaseGroup(tp, Card.IsType, 1, 1, nil, TYPE_TOKEN)
  	Duel.Release(g, REASON_COST)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Mecha
  When a Token(s) is Special Summoned to your side of the field: Special Summon 1 "Mecha Phantom Beast Token" (Machine-Type/WIND/Level 3/ATK 0/DEF 0). You can only use this effect of "Mecha Phantom Beast Megaraptor" once per turn. 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. Once per turn: You can Tribute 1 Token; add 1 "Mecha Phantom Beast" monster from your Deck to your hand.
target_text: -- Mecha
  local CARD_ID_0 = 31533705 --Mecha Phantom Beast Token
  local SETCARD_ID_0 = 0x101b --Mecha
  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)
  	--token
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e4:SetCountLimit(1, id)
  	e4:SetCondition(s.spcon)
  	e4:SetTarget(s.sptg)
  	e4:SetOperation(s.spop)
  	c:RegisterEffect(e4)
  	--search
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 1))
  	e5:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e5:SetType(EFFECT_TYPE_IGNITION)
  	e5:SetRange(LOCATION_MZONE)
  	e5:SetCountLimit(1)
  	e5:SetCost(s.thcost)
  	e5:SetTarget(s.thtg)
  	e5:SetOperation(s.thop)
  	c:RegisterEffect(e5)
  end
  function s.lvval(e, c)
  	local tp = c:GetControler()
  	return Duel.GetMatchingGroup(Card.IsCode, tp, LOCATION_MZONE, 0, nil, id + 1):GetSum(Card.GetLevel)
  end
  function s.spfilter(c, tp)
  	return c:IsControler(tp) and c:IsType(TYPE_TOKEN)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.spfilter, 1, nil, tp)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	if Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, SETCARD_ID_0, TYPES_TOKEN_MONSTER, 0, 0, 3, RACE_MACHINE, ATTRIBUTE_WIND) then
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummon(token, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.CheckReleaseGroup(tp, Card.IsType, 1, nil, TYPE_TOKEN)
  	end
  	local g = Duel.SelectReleaseGroup(tp, Card.IsType, 1, 1, nil, TYPE_TOKEN)
  	Duel.Release(g, REASON_COST)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  