--时间潜行者·掉链时间机
local s,id,o=GetID()
function s.initial_effect(c)
	--tohand
	local e2=Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id,0))
	e2:SetCategory(CATEGORY_SPECIAL_SUMMON+CATEGORY_TOGRAVE)
	e2:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_TO_GRAVE)
	e2:SetCountLimit(1,id)
	e2:SetTarget(s.sptg)
	e2:SetOperation(s.spop)
	c:RegisterEffect(e2) 
	--destroy replace
	local e5=Effect.CreateEffect(c)
	e5:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e5:SetCode(EFFECT_DESTROY_REPLACE)
	e5:SetRange(LOCATION_GRAVE)
	e5:SetCountLimit(1,id+id)
	e5:SetTarget(s.reptg)
	e5:SetValue(s.repval)
	e5:SetOperation(s.repop)
	c:RegisterEffect(e5) 
	-- --set
	-- local e21=Effect.CreateEffect(c)
	-- e21:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	-- e21:SetCode(EVENT_PHASE+PHASE_END)
	-- e21:SetRange(LOCATION_GRAVE)
	-- e21:SetProperty(EFFECT_FLAG_CARD_TARGET)
	-- e21:SetCountLimit(1,id+id)
	-- e21:SetCondition(s.tgcon)
	-- e21:SetCost(aux.bfgcost)
	-- e21:SetTarget(s.tgtg)
	-- e21:SetOperation(s.tgop)
	-- c:RegisterEffect(e21)   
end
function s.spfilter(c,e,tp)
	return c:IsSetCard(0x126) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end
function s.sptg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
		and Duel.IsExistingMatchingCard(s.spfilter,tp,LOCATION_DECK,0,1,nil,e,tp) end
	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_DECK+LOCATION_GRAVE)
	Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,1,tp,LOCATION_MZONE)
end
function s.tgfilter(c)
	return c:IsSetCard(0x126) and c:IsAbleToRemove()
end
function s.spop(e,tp,eg,ep,ev,re,r,rp)
	if Duel.GetLocationCount(tp,LOCATION_MZONE)<=0 then return end
	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
	local g=Duel.SelectMatchingCard(tp,s.spfilter,tp,LOCATION_DECK,0,1,1,nil,e,tp)
	local tc=g:GetFirst()
	if tc and Duel.SpecialSummon(tc,0,tp,tp,false,false,POS_FACEUP)>0 then
		local sg=Duel.GetMatchingGroup(s.tgfilter,tp,LOCATION_HAND+LOCATION_MZONE,0,nil)
		if sg:GetCount()>0 then
			Duel.BreakEffect()
			Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_REMOVE)
			local sc=sg:Select(tp,1,1,nil)
			Duel.Remove(sc,POS_FACEUP,REASON_EFFECT)
		end
	end
end
-- function s.tgcon(e,tp,eg,ep,ev,re,r,rp)
-- 	return Duel.GetTurnPlayer()==1-tp
-- end
-- function s.tgfilters(c,e,tp)
-- 	return c:IsRace(RACE_PSYCHO) and c:IsSetCard(0x126) and c:IsFaceup() and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
-- end
-- function s.tgtg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
-- 	if chkc then return chkc:IsLocation(LOCATION_REMOVED) and chkc:IsControler(tp) and s.tgfilters(chkc,e,tp) end
-- 	if chk==0 then return Duel.IsExistingTarget(s.tgfilters,tp,LOCATION_REMOVED,0,1,nil,e,tp) end
-- 	Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOGRAVE)
-- 	local g=Duel.SelectTarget(tp,s.tgfilters,tp,LOCATION_REMOVED,0,1,1,nil,e,tp)
-- 	Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,g,1,0,0)
-- end
-- function s.tgop(e,tp,eg,ep,ev,re,r,rp)
-- 	local tc=Duel.GetFirstTarget()
-- 	if tc:IsRelateToEffect(e) then
-- 		Duel.SpecialSummon(tc,0,tp,tp,false,false,POS_FACEUP)
-- 	end
-- end
function s.repfilter(c,tp)
	return c:IsFaceup() and c:IsSetCard(0x126)
		and c:IsLocation(LOCATION_MZONE) and c:IsControler(tp) and c:IsReason(REASON_EFFECT+REASON_BATTLE) and not c:IsReason(REASON_REPLACE)
end
function s.reptg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return e:GetHandler():IsAbleToRemove() and eg:IsExists(s.repfilter,1,nil,tp) end
	return Duel.SelectEffectYesNo(tp,e:GetHandler(),96)
end
function s.repval(e,c)
	return s.repfilter(c,e:GetHandlerPlayer())
end
function s.repop(e,tp,eg,ep,ev,re,r,rp)
	Duel.Remove(e:GetHandler(),POS_FACEUP,REASON_EFFECT)
end