--地中族邪界兽的巢穴
local s,id,o=GetID()
function s.initial_effect(c)
	--activate
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e0)
	--reveal
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_TOHAND)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e1:SetCode(EVENT_SUMMON_SUCCESS)
	e1:SetRange(LOCATION_SZONE)
	e1:SetCondition(s.revcon)
	e1:SetTarget(s.revtg)
	e1:SetOperation(s.revop)
	c:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
	c:RegisterEffect(e2)
	local e3=e1:Clone()
	e3:SetCode(EVENT_FLIP_SUMMON_SUCCESS)
	c:RegisterEffect(e3)
	--attack
	local e4=Effect.CreateEffect(c)
	e4:SetCategory(CATEGORY_POSITION)
	e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e4:SetCode(EVENT_ATTACK_ANNOUNCE)
	e4:SetRange(LOCATION_SZONE)
	e4:SetCondition(s.atkcon)
	e4:SetTarget(s.atktg)
	e4:SetOperation(s.atkop)
	c:RegisterEffect(e4)
end

function s.revcon(e,tp,eg,ep,ev,re,r,rp)
	return eg:IsExists(Card.IsSetCard,1,nil,0xed) and ep==tp
end
function s.revtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>=3 end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,1,0,LOCATION_DECK)
end
function s.revop(e,tp,eg,ep,ev,re,r,rp)
	local dk=Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)
	if dk<3 then return end
	local ct=3
	if dk%2==0 then ct=4 end
	local st=(dk-ct)/2
	local g=Duel.GetMatchingGroup(s.revfilter(st,st+ct),tp,LOCATION_DECK,0,nil)
	--Duel.ConfirmCards(tp,g)
	--Duel.ConfirmCards(1-tp,g)
	for c in aux.Next(g) do
		Duel.MoveSequence(c,SEQ_DECKTOP)
	end
	Duel.ConfirmDecktop(tp,ct)
	local tg=g:Filter(Card.IsSetCard,nil,0xed)
	if #tg>0 then
		local tc=tg:Select(tp,1,1,nil)
		Duel.DisableShuffleCheck()
		Duel.SendtoHand(tc,tp,REASON_EFFECT)
		Duel.ConfirmCards(1-tp,tc)
		ct=ct-1
	end
	Duel.SortDecktop(tp,tp,ct)
	if Duel.SelectOption(tp,aux.Stringid(id,1),aux.Stringid(id,2))==1 then
		for i=1,ct do
			local mg=Duel.GetDecktopGroup(tp,1)
			Duel.MoveSequence(mg:GetFirst(),SEQ_DECKBOTTOM)
		end
	end
end
function s.revfilter(from,to)
	return function(c)
		local s=c:GetSequence()
		return s>=from and s<to
	end
end

function s.atkcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetAttacker():IsSetCard(0xed) or (Duel.GetAttackTarget()~=nil and Duel.GetAttackTarget():IsSetCard(0xed))
end
function s.atktg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return true end
	Duel.SetOperationInfo(0,CATEGORY_POSITION,nil,2,0,0)
end
function s.atkop(e,tp,eg,ep,ev,re,r,rp)
	local c1=Duel.GetAttacker()
	local c2=Duel.GetAttackTarget()
	
	local e1=Effect.CreateEffect(e:GetHandler())
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_PHASE+PHASE_BATTLE)
	e1:SetRange(LOCATION_MZONE)
	e1:SetReset(RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_BATTLE)
	e1:SetOperation(s.flipop)
	e1:SetDescription(aux.Stringid(id,0))
	c1:RegisterEffect(e1:Clone())
	if c2~=nil then c2:RegisterEffect(e1) end
end

function s.flipop(e)
	Duel.ChangePosition(e:GetHandler(),POS_FACEDOWN_DEFENSE)
end
