--古代的机械像
local s,id,o=GetID()
function s.initial_effect(c)
	--search
	local e2=Effect.CreateEffect(c)
	e2:SetCategory(CATEGORY_DESTROY+CATEGORY_DRAW)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_SUMMON_SUCCESS)
	e2:SetCountLimit(1,id)
	e2:SetCondition(s.condition)
	e2:SetTarget(s.thtg)
	e2:SetOperation(s.thop)
	c:RegisterEffect(e2)
	local e3=e2:Clone()
	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
	c:RegisterEffect(e3) 
	--remove
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,1))
	e3:SetCategory(CATEGORY_REMOVE)
	e3:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_DESTROYED)
	e3:SetProperty(EFFECT_FLAG_CARD_TARGET+EFFECT_FLAG_DELAY)
	e3:SetCountLimit(1,id+o)
	e3:SetCondition(s.spcon)
	e3:SetTarget(s.rmtg)
	e3:SetOperation(s.rmop)
	c:RegisterEffect(e3)   
end
function s.ffilter1(c)
	return c:IsFaceup() and c:IsSetCard(0x7)
end
function s.ffilter2(c)
	return c:IsFaceup() and c:IsCode(37694547)
end
function s.condition(e,tp,eg,ep,ev,re,r,rp)
	local a=Duel.IsExistingMatchingCard(s.ffilter1,tp,LOCATION_ONFIELD,0,1,e:GetHandler())
	local b=Duel.IsExistingMatchingCard(s.ffilter2,tp,LOCATION_ONFIELD,0,1,e:GetHandler())
	return a or b
end
function s.desfilter(c)
	return c:IsFaceup() and (c:IsSetCard(0x7) or c:IsCode(37694547))
end
function s.thtg(e,tp,eg,ep,ev,re,r,rp,chk)
	local a=Duel.IsExistingMatchingCard(s.ffilter1,tp,LOCATION_ONFIELD,0,1,e:GetHandler())
	local b=Duel.IsExistingMatchingCard(s.ffilter2,tp,LOCATION_ONFIELD,0,1,e:GetHandler())
	if chk==0 then return Duel.IsPlayerCanDraw(tp,2) and (a or b) end
	Duel.SetTargetPlayer(tp)
	Duel.SetTargetParam(2)
	local g=Duel.GetMatchingGroup(s.desfilter,tp,LOCATION_ONFIELD,0,e:GetHandler())
	Duel.SetOperationInfo(0,CATEGORY_DESTROY,g,1,0,0)
	Duel.SetOperationInfo(0,CATEGORY_DRAW,nil,0,tp,2)
end
function s.thop(e,tp,eg,ep,ev,re,r,rp)
	local p,d=Duel.GetChainInfo(0,CHAININFO_TARGET_PLAYER,CHAININFO_TARGET_PARAM)
	Duel.Draw(p,d,REASON_EFFECT)
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
	local g=Duel.SelectMatchingCard(tp,s.desfilter,tp,LOCATION_ONFIELD,0,1,1,aux.ExceptThisCard(e))
	if g:GetCount()>0 then
		Duel.Destroy(g,REASON_EFFECT)
	end
end
function s.spcon(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	return c:IsPreviousLocation(LOCATION_MZONE) and bit.band(r,REASON_EFFECT+REASON_BATTLE)~=0
end
function s.rmtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	if chkc then return chkc:IsLocation(LOCATION_ONFIELD) and chkc:IsAbleToRemove() and chkc:IsControler(1-tp) end
	if chk==0 then return Duel.IsExistingTarget(Card.IsAbleToRemove,tp,0,LOCATION_ONFIELD,1,nil) end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
	local g=Duel.SelectTarget(tp,Card.IsAbleToRemove,tp,0,LOCATION_ONFIELD,1,1,nil)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,g,1,0,0)
end
function s.rmop(e,tp,eg,ep,ev,re,r,rp)
	local tc=Duel.GetFirstTarget()
	local seq=tc:GetSequence()
	if tc:IsRelateToEffect(e) and Duel.Remove(tc,0,REASON_EFFECT+REASON_TEMPORARY)~=0 then
		tc:RegisterFlagEffect(id,RESET_EVENT+RESETS_STANDARD+RESET_PHASE+PHASE_END,0,1)
		local e1=Effect.CreateEffect(e:GetHandler())
		e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		e1:SetCode(EVENT_PHASE+PHASE_END)
		e1:SetReset(RESET_PHASE+PHASE_END)
		e1:SetLabelObject(tc)
		e1:SetLabel(seq)
		e1:SetCountLimit(1)
		e1:SetCondition(s.retcon)
		e1:SetOperation(s.retop)
		Duel.RegisterEffect(e1,tp)
	end
end
function s.retcon(e,tp,eg,ep,ev,re,r,rp)
	return e:GetLabelObject():GetFlagEffect(id)~=0
end
function s.seqfilter(c)
	return c:GetSequence()<=4
end
function s.retop(e,tp,eg,ep,ev,re,r,rp)
	if e:GetLabelObject():IsPreviousLocation(LOCATION_MZONE) then
		Duel.ReturnToField(e:GetLabelObject())
	elseif e:GetLabelObject():IsType(TYPE_FIELD) and e:GetLabelObject():IsPreviousLocation(LOCATION_FZONE) then
		local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
		local pos=e:GetLabelObject():GetPreviousPosition()
		if fc then
			Duel.SendtoGrave(fc,REASON_RULE)
			Duel.BreakEffect()
		end
		Duel.MoveToField(e:GetLabelObject(),tp,tp,LOCATION_FZONE,pos,true)
	elseif e:GetLabelObject():IsType(TYPE_PENDULUM) and e:GetLabelObject():IsPreviousLocation(LOCATION_PZONE) then
		local fc=Duel.CheckLocation(tp,LOCATION_PZONE,0) or Duel.CheckLocation(tp,LOCATION_PZONE,1)
		if not fc then
			local tg=Duel.SelectMatchingCard(tp,aux.TRUE,tp,LOCATION_PZONE,0,1,1,nil)
			Duel.SendtoGrave(tg,REASON_RULE)
			Duel.BreakEffect()
		end
		Duel.MoveToField(e:GetLabelObject(),tp,tp,LOCATION_PZONE,POS_FACEUP,true)
	else
		local pos=e:GetLabelObject():GetPreviousPosition()
		local sc=Duel.GetLocationCount(tp,LOCATION_SZONE)
		if sc==0 then
			local tg=Duel.SelectMatchingCard(tp,s.seqfilter,tp,LOCATION_SZONE,0,1,1,nil)
			Duel.SendtoGrave(tg,REASON_RULE)
			Duel.BreakEffect()
		end
		Duel.MoveToField(e:GetLabelObject(),tp,tp,LOCATION_SZONE,pos,true)
	end
end