Generate a YGOPro Lua script for the following Yu-Gi-Oh card effect:
If you have 3 or more different Monster Types on your field: You can choose 3 monsters from your Deck with different Types from each other but with the same Types as the monsters you control, then, your opponent randomly chooses 1 for you to either add to your hand or Special Summon, also place the remaining monsters on the bottom of your Deck in any order. You can only activate 1 "Tribe Drive" per turn.
--トライブ・ドライブ
--Tribe Drive
--Scripted by Hatter
local s,id=GetID()
function s.initial_effect(c)
	--Add to hand or Special Summon
	local e1=Effect.CreateEffect(c)
	e1:SetDescription(aux.Stringid(id,0))
	e1:SetCategory(CATEGORY_TOHAND+CATEGORY_SEARCH+CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetHintTiming(0,TIMINGS_CHECK_MONSTER_E)
	e1:SetCountLimit(1,id,EFFECT_COUNT_CODE_OATH)
	e1:SetCondition(s.thspcon)
	e1:SetOperation(s.thspop)
	c:RegisterEffect(e1)
end
function s.getfieldraces(tp)
	local g=Duel.GetMatchingGroup(Card.IsFaceup,tp,LOCATION_MZONE,0,nil)
	local races=0
	for tc in aux.Next(g) do
		races=races|tc:GetRace()
	end
	local pop,count=races,0
	while pop>0 do
		if (pop&1)>0 then count=count+1 end
		pop=pop>>1
	end
	return races,count
end
function s.thspfilter(c,e,tp,ft,races)
	return c:IsRace(races) and (c:IsAbleToHand() or (ft>0 and c:IsCanBeSpecialSummoned(e,0,tp,false,false)))
end
function s.thsprescon(sg)
	return sg:GetClassCount(Card.GetRace)==#sg
end
function s.thspcon(e,tp,eg,ep,ev,re,r,rp,chk)
	local races,count=s.getfieldraces(tp)
	if count<3 then return false end
	local ft=Duel.GetLocationCount(tp,LOCATION_MZONE)
	local g=Duel.GetMatchingGroup(s.thspfilter,tp,LOCATION_DECK,0,nil,e,tp,ft,races)
	return aux.SelectUnselectGroup(g,e,tp,3,3,s.thsprescon,0)
end
function s.thspop(e,tp,eg,ep,ev,re,r,rp)
	local races,count=s.getfieldraces(tp)
	if count<3 then return end
	Duel.DisableShuffleCheck()
	local ft=Duel.GetLocationCount(tp,LOCATION_MZONE)
	local g=Duel.GetMatchingGroup(s.thspfilter,tp,LOCATION_DECK,0,nil,e,tp,ft,races)
	local sg=aux.SelectUnselectGroup(g,e,tp,3,3,s.thsprescon,1,tp,HINTMSG_CONFIRM)
	if #sg~=3 then return end
	Duel.ConfirmCards(1-tp,sg)
	Duel.ShuffleDeck(tp)
	local sc=sg:RandomSelect(1-tp,1):GetFirst()
	if not sc then return end
	Duel.ConfirmCards(1-tp,sc)
	aux.ToHandOrElse(sc,tp,
		function(sc)
			return ft>0 and sc:IsCanBeSpecialSummoned(e,0,tp,false,false)
		end,
		function(sc)
			Duel.SpecialSummon(sc,0,tp,tp,false,false,POS_FACEUP)
		end,
		aux.Stringid(id,1))
	sg=sg-sc
	if #sg>0 then
		Duel.MoveToDeckBottom(sg,tp)
		Duel.SortDeckbottom(tp,tp,#sg)
	end
end