--起动指令 齿轮集结
local s,id,o=GetID()
function s.initial_effect(c)
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetCountLimit(1,id+EFFECT_COUNT_CODE_OATH)
	e1:SetTarget(s.target)
	e1:SetOperation(s.activate)
	c:RegisterEffect(e1)
end
function s.cfilter(c)
	return c:IsSetCard(0x51) and c:IsFaceup() and c:IsRace(RACE_MACHINE)
end
function s.filter2(c,e,tp)
	local g=Duel.GetMatchingGroup(s.cfilter,tp,LOCATION_MZONE,0,nil)
	return c:IsSetCard(0x51) and c:IsRace(RACE_MACHINE) and c:IsCanBeSpecialSummoned(e,0,tp,false,false) and not g:IsExists(Card.IsCode,1,nil,c:GetCode())
end
function s.check(c,e,tp)
	local g=Duel.GetMatchingGroup(s.filter2,tp,LOCATION_DECK,0,nil,e,tp)
	return g:GetClassCount(Card.GetCode)>1 and Duel.GetLocationCount(tp,LOCATION_MZONE)>1
end
function s.stfilter(c)
	return c:IsCode(13955608,27157727,35100834,42237854,114514440) and c:IsSSetable()
end
function s.cfilter2(c)
	return c:IsSetCard(0x51) and c:IsRace(RACE_MACHINE) and c:CheckUniqueOnField(tp) and not c:IsForbidden()
end
function s.cfilter3(c)
	return c:IsCode(13955608,27157727,35100834,42237854,114514440) and c:IsFaceup()
end
function s.check2(c,e,tp)
	local g=Duel.GetMatchingGroup(s.cfilter2,tp,LOCATION_DECK,0,nil)
	return g:GetClassCount(Card.GetCode)>2 and Duel.IsExistingMatchingCard(s.cfilter3,tp,LOCATION_MZONE,0,1,nil) and s.szcheck(e,tp)
end
function s.szcheck(e,tp)
	if e:GetHandler():IsLocation(LOCATION_HAND) then
			return Duel.GetLocationCount(tp,LOCATION_SZONE)>3
		else return Duel.GetLocationCount(tp,LOCATION_SZONE)>2 end
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	local b0=Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_MZONE,0,1,nil)
	local b1=Duel.IsExistingMatchingCard(s.check,tp,LOCATION_DECK,0,1,nil,e,tp)
	local b2=Duel.IsExistingMatchingCard(s.stfilter,tp,LOCATION_DECK,0,1,nil)
	local b3=Duel.IsExistingMatchingCard(s.check2,tp,LOCATION_DECK,0,1,nil,e,tp)
	if chk==0 then return (b0 and b1) or (b0 and b2) or b3 end
	op=aux.SelectFromOptions(tp,{(b0 and b1),aux.Stringid(id,0)},{(b0 and b2),aux.Stringid(id,1)},{b3,aux.Stringid(id,2)})
	if op==1 then 
		Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,2,tp,LOCATION_DECK)
	elseif op==3 then
		e:SetProperty(EFFECT_FLAG_CARD_TARGET)
		Duel.SelectTarget(tp,s.cfilter3,tp,LOCATION_MZONE,0,1,1,nil)
	end
	e:SetLabel(op)
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	if e:GetLabel()==1 then
		local g=Duel.GetMatchingGroup(s.cfilter,tp,LOCATION_MZONE,0,nil)
		if g:GetCount()>0 then
			local sg=Duel.GetMatchingGroup(s.filter2,tp,LOCATION_DECK,0,nil,e,tp)
			local cg=sg:SelectSubGroup(tp,aux.dncheck,false,2,2)
			Duel.SpecialSummon(cg,0,tp,tp,false,false,POS_FACEUP)
		end
	elseif e:GetLabel()==2 then
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SET)
		local g=Duel.SelectMatchingCard(tp,s.stfilter,tp,LOCATION_DECK,0,1,1,nil)
		if g:GetCount()>0 then
			if Duel.SSet(tp,g)>0 then
				local e1=Effect.CreateEffect(e:GetHandler())
				e1:SetType(EFFECT_TYPE_SINGLE)
				e1:SetCode(EFFECT_TRAP_ACT_IN_SET_TURN)
				e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
				e1:SetReset(RESET_EVENT+RESETS_STANDARD)
				g:GetFirst():RegisterEffect(e1)
			end
		end
	else
		local tc=Duel.GetFirstTarget()
		if tc and tc:IsRelateToEffect(e) then
			local g=Duel.GetMatchingGroup(s.cfilter2,tp,LOCATION_DECK,0,nil)
			local sg=g:SelectSubGroup(tp,aux.dncheck,false,3,3)
			local sc=sg:GetFirst()
			while sc do
				if not Duel.Equip(tp,sc,tc) then return end
				--equip limit
				local e1=Effect.CreateEffect(e:GetHandler())
				e1:SetType(EFFECT_TYPE_SINGLE)
				e1:SetProperty(EFFECT_FLAG_OWNER_RELATE)
				e1:SetCode(EFFECT_EQUIP_LIMIT)
				e1:SetReset(RESET_EVENT+RESETS_STANDARD)
				e1:SetLabelObject(tc)
				e1:SetValue(s.eqlimit)
				sc:RegisterEffect(e1)
				sc=sg:GetNext()
			end
			Duel.EquipComplete()
		end
	end
end
function s.eqlimit(e,tc)
	return tc==e:GetLabelObject()
end