name: Morphtronic
card_text: Cannot be Normal Summoned/Set. Must first be Special Summoned (from your hand) by banishing 1 "Morphtronic" monster from your Graveyard.
  ● While in Attack Position: Once per turn: You can roll a six-sided die, excavate that many cards from the top of your Deck, add 1 excavated "Morphtronic" card to your hand, also shuffle the rest back into the Deck.
  ● While in Defense Position: Once per turn: You can roll a six-sided die, look at that many cards from the top of your Deck, then place all of them on either the top or the bottom of the Deck, but in any order.
external_ids: local SETCARD_ID_0 = 0x26 --Morphtronic
  
lua: local SETCARD_ID_0 = 0x26 --Morphtronic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DICE + CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.cona)
  	e2:SetTarget(s.tga)
  	e2:SetOperation(s.opa)
  	c:RegisterEffect(e2)
  	--sort deck
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_DICE)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.cond)
  	e3:SetTarget(s.tgd)
  	e3:SetOperation(s.opd)
  	c:RegisterEffect(e3)
  end
  function s.spfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToRemoveAsCost()
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_GRAVE, 0, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local tc = g:SelectUnselect(nil, tp, false, true, 1, 1)
  	if tc then
  		e:SetLabelObject(tc)
  		return true
  	else
  		return false
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = e:GetLabelObject()
  	Duel.Remove(g, POS_FACEUP, REASON_SPSUMMON)
  end
  function s.cona(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsAttackPos()
  end
  function s.tga(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.opa(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) == 0 then
  		return
  	end
  	local dc = Duel.TossDice(tp, 1)
  	Duel.ConfirmDecktop(tp, dc)
  	local dg = Duel.GetDecktopGroup(tp, dc)
  	local g = dg:Filter(s.filter, nil)
  	if g:GetCount() > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  	Duel.ShuffleDeck(tp)
  end
  function s.cond(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsDefensePos()
  end
  function s.tgd(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
  end
  function s.opd(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) == 0 then
  		return
  	end
  	local dc = Duel.TossDice(tp, 1)
  	local g = Duel.GetDecktopGroup(tp, dc)
  	local ct = g:GetCount()
  	Duel.ConfirmCards(tp, g)
  	local op = Duel.SelectOption(tp, aux.Stringid(id, 3), aux.Stringid(id, 4))
  	Duel.SortDecktop(tp, tp, ct)
  	if op == 0 then
  		return
  	end
  	for i = 1, ct do
  		local tg = Duel.GetDecktopGroup(tp, 1)
  		Duel.MoveSequence(tg:GetFirst(), SEQ_DECKBOTTOM)
  	end
  end
  
  
source_text: Morphtronic
  Cannot be Normal Summoned/Set. Must first be Special Summoned (from your hand) by banishing 1 "Morphtronic" monster from your Graveyard.
  ● While in Attack Position: Once per turn: You can roll a six-sided die, excavate that many cards from the top of your Deck, add 1 excavated "Morphtronic" card to your hand, also shuffle the rest back into the Deck.
  ● While in Defense Position: Once per turn: You can roll a six-sided die, look at that many cards from the top of your Deck, then place all of them on either the top or the bottom of the Deck, but in any order.
target_text: -- Morphtronic
  local SETCARD_ID_0 = 0x26 --Morphtronic
  local s, id, o = GetID()
  function s.initial_effect(c)
  	c:EnableReviveLimit()
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_DICE + CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.cona)
  	e2:SetTarget(s.tga)
  	e2:SetOperation(s.opa)
  	c:RegisterEffect(e2)
  	--sort deck
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 2))
  	e3:SetCategory(CATEGORY_DICE)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_MZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.cond)
  	e3:SetTarget(s.tgd)
  	e3:SetOperation(s.opd)
  	c:RegisterEffect(e3)
  end
  function s.spfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToRemoveAsCost()
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_GRAVE, 0, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_REMOVE)
  	local tc = g:SelectUnselect(nil, tp, false, true, 1, 1)
  	if tc then
  		e:SetLabelObject(tc)
  		return true
  	else
  		return false
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = e:GetLabelObject()
  	Duel.Remove(g, POS_FACEUP, REASON_SPSUMMON)
  end
  function s.cona(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsAttackPos()
  end
  function s.tga(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.opa(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) == 0 then
  		return
  	end
  	local dc = Duel.TossDice(tp, 1)
  	Duel.ConfirmDecktop(tp, dc)
  	local dg = Duel.GetDecktopGroup(tp, dc)
  	local g = dg:Filter(s.filter, nil)
  	if g:GetCount() > 0 then
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  		local sg = g:Select(tp, 1, 1, nil)
  		Duel.SendtoHand(sg, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, sg)
  	end
  	Duel.ShuffleDeck(tp)
  end
  function s.cond(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsDefensePos()
  end
  function s.tgd(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
  end
  function s.opd(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) == 0 then
  		return
  	end
  	local dc = Duel.TossDice(tp, 1)
  	local g = Duel.GetDecktopGroup(tp, dc)
  	local ct = g:GetCount()
  	Duel.ConfirmCards(tp, g)
  	local op = Duel.SelectOption(tp, aux.Stringid(id, 3), aux.Stringid(id, 4))
  	Duel.SortDecktop(tp, tp, ct)
  	if op == 0 then
  		return
  	end
  	for i = 1, ct do
  		local tg = Duel.GetDecktopGroup(tp, 1)
  		Duel.MoveSequence(tg:GetFirst(), SEQ_DECKBOTTOM)
  	end
  end
  
  