local s,id,o=GetID()
function s.initial_effect(c)
    aux.EnableExtraDeckSummonCountLimit()
    --activate
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_ACTIVATE)
	e0:SetCode(EVENT_FREE_CHAIN)
	c:RegisterEffect(e0)
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_TOHAND+CATEGORY_SEARCH)
	e1:SetType(EFFECT_TYPE_IGNITION)
	e1:SetRange(LOCATION_FZONE)
	e1:SetCountLimit(1,id)
    e1:SetCondition(s.spcon)
	e1:SetTarget(s.sptg)
	e1:SetOperation(s.spop)
	c:RegisterEffect(e1)
    local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,1))
	e3:SetCategory(CATEGORY_TODECK+CATEGORY_DRAW)
	e3:SetType(EFFECT_TYPE_IGNITION)
	e3:SetRange(LOCATION_FZONE)
	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e3:SetCountLimit(1,id+id)
	e3:SetTarget(s.drtg)
	e3:SetOperation(s.drop)
	c:RegisterEffect(e3)
    local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,2))
	e4:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
	e4:SetCode(EVENT_BATTLE_DESTROYED)
	e4:SetRange(LOCATION_FZONE)
	e4:SetCondition(s.damcon)
	e4:SetTarget(s.damtg)
	e4:SetOperation(s.damop)
	c:RegisterEffect(e4)
end
function s.spcfilter(c)
	return c:GetSequence()<5
end
function s.spcon(e,tp,eg,ep,ev,re,r,rp)
	return not Duel.IsExistingMatchingCard(s.spcfilter,tp,LOCATION_MZONE,0,1,nil)
end
function s.thfilter(c,e,tp)
	return c:IsSetCard(0x195) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) and (c:IsAbleToHand(1-tp) or c:IsCanBeSpecialSummoned(e,0,tp,false,false,1-tp))
end
function s.sptg(e,tp,eg,ep,ev,re,r,rp,chk)
	local g=Duel.GetMatchingGroup(s.thfilter,tp,LOCATION_DECK,0,nil,e,tp)
	if chk==0 then return g:GetClassCount(Card.GetCode)>=4 and Duel.GetMZoneCount(1-tp)~=0 and Duel.GetMZoneCount(tp)~=0 end
	Duel.SetOperationInfo(0,CATEGORY_TOHAND,nil,2,1-tp,LOCATION_DECK)
    Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,2,tp,LOCATION_DECK)
end
function s.spop(e,tp,eg,ep,ev,re,r,rp)
    local c=e:GetHandler()
    if Duel.GetMZoneCount(tp)==0 or Duel.GetMZoneCount(1-tp)==0 then return end
	local g=Duel.GetMatchingGroup(s.thfilter,tp,LOCATION_DECK,0,nil,e,tp)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
	local sg=g:SelectSubGroup(tp,aux.dncheck,false,4,4)
	if sg then
		Duel.ConfirmCards(1-tp,sg)
        local res=Duel.RockPaperScissors()
        if res==1-tp then
            Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_SPSUMMON)
            local oc=sg:Select(1-tp,1,1,nil):GetFirst()
            Duel.SpecialSummonStep(oc,0,tp,1-tp,false,false,POS_FACEUP_ATTACK)
            sg:RemoveCard(oc)
            Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
            local sc=sg:Select(tp,1,1,nil):GetFirst()
            Duel.SpecialSummonStep(sc,0,tp,tp,false,false,POS_FACEUP_ATTACK)
            sg:RemoveCard(sc)
            Duel.SendtoHand(sg,1-tp,REASON_EFFECT)
            local e1=Effect.CreateEffect(e:GetHandler())
            e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_MUST_ATTACK)
            e1:SetReset(RESET_EVENT+RESETS_STANDARD)
            e1:SetDescription(aux.Stringid(id,4))
            e1:SetProperty(EFFECT_FLAG_CLIENT_HINT)
            sc:RegisterEffect(e1)
            local e2=e1:Clone()
            oc:RegisterEffect(e2)
            local e1=Effect.CreateEffect(e:GetHandler())
            e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_CANNOT_CHANGE_POSITION)
            e1:SetReset(RESET_EVENT+RESETS_STANDARD)
            sc:RegisterEffect(e1)
            local e2=e1:Clone()
            oc:RegisterEffect(e2)
            Duel.SpecialSummonComplete()
        else Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
            local oc=sg:Select(tp,1,1,nil):GetFirst()
            Duel.SpecialSummonStep(oc,0,tp,tp,false,false,POS_FACEUP_ATTACK)
            sg:RemoveCard(oc)
            Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_SPSUMMON)
            local sc=sg:Select(1-tp,1,1,nil):GetFirst()
            Duel.SpecialSummonStep(sc,0,tp,1-tp,false,false,POS_FACEUP_ATTACK)
            sg:RemoveCard(sc)
            Duel.SendtoHand(sg,1-tp,REASON_EFFECT)
            local e1=Effect.CreateEffect(e:GetHandler())
            e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_MUST_ATTACK)
            e1:SetReset(RESET_EVENT+RESETS_STANDARD)
            e1:SetDescription(aux.Stringid(id,4))
            e1:SetProperty(EFFECT_FLAG_CLIENT_HINT)
            sc:RegisterEffect(e1)
            local e2=e1:Clone()
            oc:RegisterEffect(e2)
            local e1=Effect.CreateEffect(e:GetHandler())
            e1:SetType(EFFECT_TYPE_SINGLE)
            e1:SetCode(EFFECT_CANNOT_CHANGE_POSITION)
            e1:SetReset(RESET_EVENT+RESETS_STANDARD)
            sc:RegisterEffect(e1)
            local e2=e1:Clone()
            oc:RegisterEffect(e2)
            Duel.SpecialSummonComplete()
        end
        local e1=Effect.CreateEffect(c)
        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)
        local e2=Effect.CreateEffect(c)
        e2:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
        e2:SetCode(EVENT_SPSUMMON_SUCCESS)
        e2:SetOperation(s.checkop)
        e2:SetReset(RESET_PHASE+PHASE_END)
        Duel.RegisterEffect(e2,tp)
	end
end
function s.splimit(e,c,sump,sumtype,sumpos,targetp,se)
	return c:IsLocation(LOCATION_EXTRA) and aux.ExtraDeckSummonCountLimit[sump]<=0
end
function s.scfilter(c,tp)
	return c:IsSummonPlayer(tp) and c:IsPreviousLocation(LOCATION_EXTRA)
end
function s.checkop(e,tp,eg,ep,ev,re,r,rp)
	if eg:IsExists(s.scfilter,1,nil,tp) then
		aux.ExtraDeckSummonCountLimit[tp]=aux.ExtraDeckSummonCountLimit[tp]-1
	end
	if eg:IsExists(s.scfilter,1,nil,1-tp) then
		aux.ExtraDeckSummonCountLimit[1-tp]=aux.ExtraDeckSummonCountLimit[1-tp]-1
	end
end
function s.damcon(e,tp,eg,ep,ev,re,r,rp)
	local des=eg:GetFirst()
	local rc=des:GetReasonCard()
	return des:IsType(TYPE_MONSTER) and rc:IsRelateToBattle() and rc:IsSetCard(0x195)
end
function s.damtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)>0
		and Duel.IsExistingMatchingCard(nil,tp,LOCATION_HAND,0,1,e:GetHandler()) end
end
function s.damop(e,tp,eg,ep,ev,re,r,rp)
    local damp=eg:GetFirst():GetPreviousControler()
	local g1=Duel.GetFieldGroup(tp,LOCATION_HAND,0)
	local g2=Duel.GetFieldGroup(tp,0,LOCATION_HAND)
    if damp==tp and g1:GetCount()~=0 then
        Duel.ConfirmCards(1-tp,g1)
        Duel.Hint(HINT_SELECTMSG,1-tp,HINTMSG_ATOHAND)
        local ag2=g1:Select(1-tp,1,1,nil)
        Duel.SendtoHand(ag2,1-tp,REASON_EFFECT)
        Duel.ShuffleHand(tp)
    end
    if damp~=tp and g2:GetCount()~=0 then
        Duel.ConfirmCards(tp,g2)
        Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATOHAND)
        local ag1=g2:Select(tp,1,1,nil)
        Duel.SendtoHand(ag1,tp,REASON_EFFECT)
        Duel.ShuffleHand(1-tp)
    end
end
function s.drfilter(c)
	return c:IsSetCard(0x195) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck()
end
function s.drtg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.IsPlayerCanDraw(tp)
		and Duel.IsExistingMatchingCard(s.drfilter,tp,LOCATION_HAND,0,1,nil) end
	Duel.SetTargetPlayer(tp)
	Duel.SetOperationInfo(0,CATEGORY_TODECK,nil,1,tp,LOCATION_HAND)
end
function s.drop(e,tp,eg,ep,ev,re,r,rp)
	local p=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER)
	Duel.Hint(HINT_SELECTMSG,p,HINTMSG_TODECK)
	local g=Duel.SelectMatchingCard(p,s.drfilter,p,LOCATION_HAND,0,1,63,nil)
	if g:GetCount()>0 then
		Duel.ConfirmCards(1-p,g)
		local ct=Duel.SendtoDeck(g,nil,SEQ_DECKSHUFFLE,REASON_EFFECT)
		Duel.ShuffleDeck(p)
		Duel.BreakEffect()
		Duel.Draw(p,ct,REASON_EFFECT)
	end
end