Primathmech
2+ Level 4 monsters
You can Tribute 1 monster; Special Summon 1 Level 4 "Mathmech" monster from your hand or GY. If this card is Xyz Summoned: You can detach 2 to 4 materials from this card, then activate the appropriate effect;
● 2: Add 1 "Mathmech" card from your Deck to your hand.
● 3: Add 1 Level 4 monster from your Deck to your hand.
● 4: Add 1 Spell/Trap from your Deck to your hand.
You can only use each effect of "Primathmech Alembertian" once per turn.
local SETCARD_ID_0 = 0x132 --Mathmech

local SETCARD_ID_0 = 0x132 --Mathmech
local s, id, o = GetID()
function s.initial_effect(c)
	--xyz summon
	c:EnableReviveLimit()
	aux.AddXyzProcedure(c, nil, 4, 2, nil, nil, 99)
	--to hand
	local e1 = Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id, 0))
	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e1:SetProperty(EFFECT_FLAG_DELAY)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetCountLimit(1, id)
	e1:SetCondition(s.thcon)
	e1:SetCost(s.thcost)
	e1:SetTarget(s.thtg)
	e1:SetOperation(s.thop)
	e1:SetLabel(2)
	c:RegisterEffect(e1)
	local e2 = e1:Clone()
	e2:SetDescription(aux.Stringid(id, 1))
	e2:SetLabel(3)
	c:RegisterEffect(e2)
	local e3 = e1:Clone()
	e3:SetDescription(aux.Stringid(id, 2))
	e3:SetLabel(4)
	c:RegisterEffect(e3)
	--special summon
	local e4 = Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id, 3))
	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e4:SetType(EFFECT_TYPE_IGNITION)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCountLimit(1, id + 1)
	e4:SetCost(s.spcost)
	e4:SetTarget(s.sptg)
	e4:SetOperation(s.spop)
	c:RegisterEffect(e4)
end
function s.thcon(e, tp, eg, ep, ev, re, r, rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_XYZ)
end
function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
	local ct = e:GetLabel()
	if chk == 0 then
		return e:GetHandler():CheckRemoveOverlayCard(tp, ct, REASON_COST)
	end
	Duel.Hint(HINT_OPSELECTED, 1 - tp, e:GetDescription())
	e:GetHandler():RemoveOverlayCard(tp, ct, ct, REASON_COST)
end
function s.thfilter(c, ct)
	if not c:IsAbleToHand() then
		return false
	end
	if ct == 2 then
		return c:IsSetCard(SETCARD_ID_0)
	elseif ct == 3 then
		return c:IsType(TYPE_MONSTER) and c:IsLevel(4)
	else
		return c:IsType(TYPE_SPELL + TYPE_TRAP)
	end
end
function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e:GetLabel())
	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.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e:GetLabel())
	if g:GetCount() > 0 then
		Duel.SendtoHand(g, nil, REASON_EFFECT)
		Duel.ConfirmCards(1 - tp, g)
	end
end
function s.costfilter(c, tp)
	return Duel.GetMZoneCount(tp, c) > 0
end
function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.CheckReleaseGroup(tp, s.costfilter, 1, nil, tp)
	end
	local g = Duel.SelectReleaseGroup(tp, s.costfilter, 1, 1, nil, tp)
	Duel.Release(g, REASON_COST)
end
function s.spfilter(c, e, tp)
	return c:IsSetCard(SETCARD_ID_0) and c:IsLevel(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp)
	end
	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
end
function s.spop(e, tp, eg, ep, ev, re, r, rp)
	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
		return
	end
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
	if g:GetCount() > 0 then
		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
	end
end


Primathmech
2+ Level 4 monsters
You can Tribute 1 monster; Special Summon 1 Level 4 "Mathmech" monster from your hand or GY. If this card is Xyz Summoned: You can detach 2 to 4 materials from this card, then activate the appropriate effect;
● 2: Add 1 "Mathmech" card from your Deck to your hand.
● 3: Add 1 Level 4 monster from your Deck to your hand.
● 4: Add 1 Spell/Trap from your Deck to your hand.
You can only use each effect of "Primathmech Alembertian" once per turn.
-- Primathmech
local SETCARD_ID_0 = 0x132 --Mathmech
local s, id, o = GetID()
function s.initial_effect(c)
	--xyz summon
	c:EnableReviveLimit()
	aux.AddXyzProcedure(c, nil, 4, 2, nil, nil, 99)
	--to hand
	local e1 = Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id, 0))
	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
	e1:SetProperty(EFFECT_FLAG_DELAY)
	e1:SetCode(EVENT_SPSUMMON_SUCCESS)
	e1:SetCountLimit(1, id)
	e1:SetCondition(s.thcon)
	e1:SetCost(s.thcost)
	e1:SetTarget(s.thtg)
	e1:SetOperation(s.thop)
	e1:SetLabel(2)
	c:RegisterEffect(e1)
	local e2 = e1:Clone()
	e2:SetDescription(aux.Stringid(id, 1))
	e2:SetLabel(3)
	c:RegisterEffect(e2)
	local e3 = e1:Clone()
	e3:SetDescription(aux.Stringid(id, 2))
	e3:SetLabel(4)
	c:RegisterEffect(e3)
	--special summon
	local e4 = Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id, 3))
	e4:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e4:SetType(EFFECT_TYPE_IGNITION)
	e4:SetRange(LOCATION_MZONE)
	e4:SetCountLimit(1, id + 1)
	e4:SetCost(s.spcost)
	e4:SetTarget(s.sptg)
	e4:SetOperation(s.spop)
	c:RegisterEffect(e4)
end
function s.thcon(e, tp, eg, ep, ev, re, r, rp)
	return e:GetHandler():IsSummonType(SUMMON_TYPE_XYZ)
end
function s.thcost(e, tp, eg, ep, ev, re, r, rp, chk)
	local ct = e:GetLabel()
	if chk == 0 then
		return e:GetHandler():CheckRemoveOverlayCard(tp, ct, REASON_COST)
	end
	Duel.Hint(HINT_OPSELECTED, 1 - tp, e:GetDescription())
	e:GetHandler():RemoveOverlayCard(tp, ct, ct, REASON_COST)
end
function s.thfilter(c, ct)
	if not c:IsAbleToHand() then
		return false
	end
	if ct == 2 then
		return c:IsSetCard(SETCARD_ID_0)
	elseif ct == 3 then
		return c:IsType(TYPE_MONSTER) and c:IsLevel(4)
	else
		return c:IsType(TYPE_SPELL + TYPE_TRAP)
	end
end
function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil, e:GetLabel())
	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.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil, e:GetLabel())
	if g:GetCount() > 0 then
		Duel.SendtoHand(g, nil, REASON_EFFECT)
		Duel.ConfirmCards(1 - tp, g)
	end
end
function s.costfilter(c, tp)
	return Duel.GetMZoneCount(tp, c) > 0
end
function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.CheckReleaseGroup(tp, s.costfilter, 1, nil, tp)
	end
	local g = Duel.SelectReleaseGroup(tp, s.costfilter, 1, 1, nil, tp)
	Duel.Release(g, REASON_COST)
end
function s.spfilter(c, e, tp)
	return c:IsSetCard(SETCARD_ID_0) and c:IsLevel(4) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
end
function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, e, tp)
	end
	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_GRAVE)
end
function s.spop(e, tp, eg, ep, ev, re, r, rp)
	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
		return
	end
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.spfilter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
	if g:GetCount() > 0 then
		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP)
	end
end

