name: Adamancipator
card_text: Excavate the top cards of your Deck equal to the number of Rock monsters you control +5, and if you do, you can add to your hand 1 excavated Rock monster with a Level equal to or lower than the number of the excavated cards, also place the remaining cards on the bottom of your Deck in any order. For the rest of this turn after this card resolves, you cannot Special Summon monsters, except Rock monsters.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsRace(RACE_ROCK)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, 0, nil)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 4 + g:GetCount()
  	end
  end
  function s.thfilter(c, lv)
  	return c:IsRace(RACE_ROCK) and c:IsLevelBelow(lv) and c:IsAbleToHand()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, 0, nil)
  	local count = 5 + tg:GetCount()
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) >= count then
  		Duel.ConfirmDecktop(tp, count)
  		local g = Duel.GetDecktopGroup(tp, count)
  		local ct = g:GetCount()
  		if ct > 0 and g:FilterCount(s.thfilter, nil, ct) > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.DisableShuffleCheck()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  			local sg = g:FilterSelect(tp, s.thfilter, 1, 1, nil, ct)
  			Duel.SendtoHand(sg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, sg)
  			Duel.ShuffleHand(tp)
  			ct = g:GetCount() - sg:GetCount()
  		end
  		if ct > 0 then
  			Duel.SortDecktop(tp, tp, ct)
  			for i = 1, ct do
  				local mg = Duel.GetDecktopGroup(tp, 1)
  				Duel.MoveSequence(mg:GetFirst(), SEQ_DECKBOTTOM)
  			end
  		end
  	end
  	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  		e1:SetTargetRange(1, 0)
  		e1:SetTarget(s.splimit)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.splimit(e, c)
  	return not c:IsRace(RACE_ROCK)
  end
  
  
source_text: Adamancipator
  Excavate the top cards of your Deck equal to the number of Rock monsters you control +5, and if you do, you can add to your hand 1 excavated Rock monster with a Level equal to or lower than the number of the excavated cards, also place the remaining cards on the bottom of your Deck in any order. For the rest of this turn after this card resolves, you cannot Special Summon monsters, except Rock monsters.
target_text: -- Adamancipator
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.filter(c)
  	return c:IsFaceup() and c:IsRace(RACE_ROCK)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local g = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, 0, nil)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) > 4 + g:GetCount()
  	end
  end
  function s.thfilter(c, lv)
  	return c:IsRace(RACE_ROCK) and c:IsLevelBelow(lv) and c:IsAbleToHand()
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local tg = Duel.GetMatchingGroup(s.filter, tp, LOCATION_MZONE, 0, nil)
  	local count = 5 + tg:GetCount()
  	if Duel.GetFieldGroupCount(tp, LOCATION_DECK, 0) >= count then
  		Duel.ConfirmDecktop(tp, count)
  		local g = Duel.GetDecktopGroup(tp, count)
  		local ct = g:GetCount()
  		if ct > 0 and g:FilterCount(s.thfilter, nil, ct) > 0 and Duel.SelectYesNo(tp, aux.Stringid(id, 0)) then
  			Duel.DisableShuffleCheck()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  			local sg = g:FilterSelect(tp, s.thfilter, 1, 1, nil, ct)
  			Duel.SendtoHand(sg, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, sg)
  			Duel.ShuffleHand(tp)
  			ct = g:GetCount() - sg:GetCount()
  		end
  		if ct > 0 then
  			Duel.SortDecktop(tp, tp, ct)
  			for i = 1, ct do
  				local mg = Duel.GetDecktopGroup(tp, 1)
  				Duel.MoveSequence(mg:GetFirst(), SEQ_DECKBOTTOM)
  			end
  		end
  	end
  	if e:IsHasType(EFFECT_TYPE_ACTIVATE) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
  		e1:SetTargetRange(1, 0)
  		e1:SetTarget(s.splimit)
  		e1:SetReset(RESET_PHASE + PHASE_END)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.splimit(e, c)
  	return not c:IsRace(RACE_ROCK)
  end
  
  